1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 08:07:34 +00:00

LibWeb: Replace LibGfx gradient painting with new GradientPainting

This commit is contained in:
MacDue 2022-07-17 19:52:56 +01:00 committed by Linus Groh
parent 469491906f
commit 0c521381a3

View file

@ -12,47 +12,11 @@
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Painting/BackgroundPainting.h>
#include <LibWeb/Painting/BorderRadiusCornerClipper.h>
#include <LibWeb/Painting/GradientPainting.h>
#include <LibWeb/Painting/PaintContext.h>
namespace Web::Painting {
struct GfxGradient {
Gfx::Orientation orientation;
Gfx::Color color_a;
Gfx::Color color_b;
};
static Optional<GfxGradient> linear_gradient_to_gfx_gradient(CSS::LinearGradientStyleValue const& linear_gradient, Gfx::FloatRect const& background_rect)
{
if (linear_gradient.color_stop_list().size() != 2)
return {};
auto angle = round_to<int>(linear_gradient.angle_degrees(background_rect));
auto color_a = linear_gradient.color_stop_list()[0].color_stop.color;
auto color_b = linear_gradient.color_stop_list()[1].color_stop.color;
auto orientation = [&]() -> Optional<Gfx::Orientation> {
switch (angle) {
case 0:
swap(color_a, color_b);
[[fallthrough]];
case 180:
return Gfx::Orientation::Vertical;
case 270:
swap(color_a, color_b);
[[fallthrough]];
case 90:
return Gfx::Orientation::Horizontal;
default:
return {};
}
}();
if (!orientation.has_value())
return {};
return GfxGradient { *orientation, color_a, color_b };
};
// https://www.w3.org/TR/css-backgrounds-3/#backgrounds
void paint_background(PaintContext& context, Layout::NodeWithStyleAndBoxModelMetrics const& layout_node, Gfx::FloatRect const& border_rect, Color background_color, Vector<CSS::BackgroundLayerData> const* background_layers, BorderRadiiData const& border_radii)
{
@ -134,13 +98,10 @@ void paint_background(PaintContext& context, Layout::NodeWithStyleAndBoxModelMet
ScopedCornerRadiusClip corner_clip { painter, clip_rect, clip_box.radii };
if (layer.background_image->is_linear_gradient()) {
// FIXME: Paint non-orthogonal gradients, gradients with > 2 color stops, gradients with custom stop positions ...
// FIXME: Support sizing and positioning rules with gradients.
auto& linear_gradient = layer.background_image->as_linear_gradient();
auto gfx_gradient = linear_gradient_to_gfx_gradient(linear_gradient, border_box.rect);
if (gfx_gradient.has_value()) {
painter.fill_rect_with_gradient(gfx_gradient->orientation, border_box.rect.to_rounded<int>(), gfx_gradient->color_a, gfx_gradient->color_b);
}
auto data = resolve_linear_gradient_data(layout_node, border_box.rect, linear_gradient);
paint_linear_gradient(context, border_box.rect.to_rounded<int>(), data);
continue;
}