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

LibWeb: Implement SVG fill-rule attribute

Previously, we did an evenodd fill for everything which while for most
SVGs works, it is not correct default (it should be nonzero), and broke
some SVGs. This fixes a few of the icons on https://shopify.com/.
This commit is contained in:
MacDue 2023-06-11 16:43:46 +01:00 committed by Andreas Kling
parent ead56e88db
commit 377ff0ac26
13 changed files with 74 additions and 3 deletions

View file

@ -120,6 +120,11 @@ private:
bool m_is_percentage { false };
};
enum class FillRule {
Nonzero,
Evenodd
};
class AttributeParser final {
public:
~AttributeParser() = default;

View file

@ -9,6 +9,7 @@
#include <AK/Vector.h>
#include <LibGfx/Color.h>
#include <LibGfx/Rect.h>
#include <LibWeb/SVG/AttributeParser.h>
namespace Web {
@ -20,12 +21,14 @@ public:
m_states.append(State());
}
SVG::FillRule fill_rule() const { return state().fill_rule; }
Gfx::Color fill_color() const { return state().fill_color; }
Gfx::Color stroke_color() const { return state().stroke_color; }
float stroke_width() const { return state().stroke_width; }
float fill_opacity() const { return state().fill_opacity; }
float stroke_opacity() const { return state().stroke_opacity; }
void set_fill_rule(SVG::FillRule fill_rule) { state().fill_rule = fill_rule; }
void set_fill_color(Gfx::Color color) { state().fill_color = color; }
void set_stroke_color(Gfx::Color color) { state().stroke_color = color; }
void set_stroke_width(float width) { state().stroke_width = width; }
@ -40,6 +43,7 @@ public:
private:
struct State {
SVG::FillRule fill_rule { SVG::FillRule::Nonzero };
Gfx::Color fill_color { Gfx::Color::Transparent };
Gfx::Color stroke_color { Gfx::Color::Transparent };
float stroke_width { 1.0f };

View file

@ -129,6 +129,9 @@ void SVGGraphicsElement::apply_presentational_hints(CSS::StyleProperties& style)
} else if (name.equals_ignoring_ascii_case("stroke-width"sv)) {
if (auto stroke_width_value = parse_css_value(parsing_context, value, CSS::PropertyID::StrokeWidth).release_value_but_fixme_should_propagate_errors())
style.set_property(CSS::PropertyID::StrokeWidth, stroke_width_value.release_nonnull());
} else if (name.equals_ignoring_ascii_case("fill-rule"sv)) {
if (auto fill_rule_value = parse_css_value(parsing_context, value, CSS::PropertyID::FillRule).release_value_but_fixme_should_propagate_errors())
style.set_property(CSS::PropertyID::FillRule, fill_rule_value.release_nonnull());
} else if (name.equals_ignoring_ascii_case("fill-opacity"sv)) {
if (auto fill_opacity_value = parse_css_value(parsing_context, value, CSS::PropertyID::FillOpacity).release_value_but_fixme_should_propagate_errors())
style.set_property(CSS::PropertyID::FillOpacity, fill_opacity_value.release_nonnull());
@ -139,6 +142,20 @@ void SVGGraphicsElement::apply_presentational_hints(CSS::StyleProperties& style)
});
}
Optional<FillRule> SVGGraphicsElement::fill_rule() const
{
if (!layout_node())
return {};
switch (layout_node()->computed_values().fill_rule()) {
case CSS::FillRule::Nonzero:
return FillRule::Nonzero;
case CSS::FillRule::Evenodd:
return FillRule::Evenodd;
default:
VERIFY_NOT_REACHED();
}
}
Optional<Gfx::Color> SVGGraphicsElement::fill_color() const
{
if (!layout_node())

View file

@ -27,7 +27,7 @@ public:
virtual void parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value) override;
Optional<Gfx::Color> fill_color() const;
Gfx::Painter::WindingRule fill_rule() const;
Optional<FillRule> fill_rule() const;
Optional<Gfx::Color> stroke_color() const;
Optional<float> stroke_width() const;
Optional<float> fill_opacity() const;