Truly independent web browser
LibWeb: Scale the dashes of an SVG stroke by its pathLength
sideshowbarker5d8d0a0efe94
diff --git a/Libraries/LibWeb/Layout/LayoutRustBridge.cpp b/Libraries/LibWeb/Layout/LayoutRustBridge.cppindex 0a7074ca3b3..631c96a5c03 100644--- a/Libraries/LibWeb/Layout/LayoutRustBridge.cpp+++ b/Libraries/LibWeb/Layout/LayoutRustBridge.cpp@@ -211,6 +211,12 @@ RustFFI::FfiSvgAttributeFacts svg_geometry_facts(DOM::Element const& element)facts.line_x2 = to_ffi_number_percentage(line_element->x2_value());facts.line_y2 = to_ffi_number_percentage(line_element->y2_value());}+ if (auto const* geometry_element = as_if<SVG::SVGGeometryElement>(element)) {+ if (auto path_length = geometry_element->author_path_length(); path_length.has_value()) {+ facts.has_path_length = true;+ facts.path_length = *path_length;+ }+ }return facts;}@@ -333,6 +339,8 @@ static RustFFI::FfiSvgAttributeFacts build_svg_attribute_facts(DOM::Element& dom.line_y1 = geometry.line_y1,.line_x2 = geometry.line_x2,.line_y2 = geometry.line_y2,+ .has_path_length = geometry.has_path_length,+ .path_length = geometry.path_length,.text_x = to_ffi_svg_length_value(text_positioning.x),.text_y = to_ffi_svg_length_value(text_positioning.y),.text_dx = to_ffi_svg_length_value(text_positioning.dx),diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rsindex f2b4821a4af..1ef7718f93c 100644--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs@@ -220,6 +220,10 @@ pub struct FfiSvgAttributeFacts {pub line_y1: FfiSvgNumberPercentage,pub line_x2: FfiSvgNumberPercentage,pub line_y2: FfiSvgNumberPercentage,+ /// The `pathLength` a geometry element parses, where it parses one: the length its stroke's dashes measure the+ /// path in.+ pub has_path_length: bool,+ pub path_length: f32,/// The text positioning attributes of a `<text>` or `<tspan>`./// FIXME: These only carry a single value each, not a list.pub text_x: FfiSvgLengthValue,@@ -1213,12 +1217,21 @@ pub unsafe extern "C" fn svg_geometry_element_stroke_contains(cap_style: stroke.cap_style,join_style: stroke.join_style,miter_limit: stroke.miter_limit,- dash_array: stroke- .dash_array- .iter()- .map(|dash| dash * dash_scale * stroke_scale)- .collect(),- dash_offset: stroke.dash_offset * dash_scale * stroke_scale,+ // An infinite dash scale makes every dash infinitely long, so the stroke draws solid.+ dash_array: if dash_scale.is_finite() {+ stroke+ .dash_array+ .iter()+ .map(|dash| dash * dash_scale * stroke_scale)+ .collect()+ } else {+ Vec::new()+ },+ dash_offset: if dash_scale.is_finite() {+ stroke.dash_offset * dash_scale * stroke_scale+ } else {+ 0.0+ },};if stroke_style.thickness <= 0.0 {return false;@@ -1863,6 +1876,7 @@ impl<'pass> SvgFormattingContext<'pass> {svg.viewport_percentage_basis = facts.viewport_percentage_basis;svg.resource_content_units_are_object_bounding_box =attributes.content_units == SVG_UNITS_OBJECT_BOUNDING_BOX;+ svg.author_path_length = attributes.has_path_length.then_some(attributes.path_length);});}diff --git a/Libraries/LibWeb/Rust/src/layout/used_values.rs b/Libraries/LibWeb/Rust/src/layout/used_values.rsindex 722049f031b..d04eda39282 100644--- a/Libraries/LibWeb/Rust/src/layout/used_values.rs+++ b/Libraries/LibWeb/Rust/src/layout/used_values.rs@@ -127,6 +127,8 @@ pub(crate) struct CommittedSvgFacts {pub(crate) mask_area_facts: Option<svg_formatting_context::SvgMaskAreaFacts>,pub(crate) viewport_percentage_basis: CssPixels,pub(crate) resource_content_units_are_object_bounding_box: bool,+ /// The `pathLength` of a geometry element, where its element has one.+ pub(crate) author_path_length: Option<f32>,}/// The sizes, size constraints and box sides of a box.diff --git a/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs b/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rsindex 93db3ee197a..8e243489612 100644--- a/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs+++ b/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs@@ -356,6 +356,14 @@ pub(crate) fn committed_svg_viewport_percentage_basis(arena: &impl GeometryRead,})}+/// The `pathLength` the box of a geometry element committed, where its element has one.+pub(crate) fn committed_svg_author_path_length(arena: &impl GeometryRead, slot: NodeSlotId) -> Option<f32> {+ arena.with_committed_fragment_link(slot, |link| {+ link.and_then(|link| link.fragment.svg.as_ref())+ .and_then(|svg| svg.author_path_length)+ })+}+pub(crate) fn absolute_rect_or_default(arena: &impl GeometryRead, slot: NodeSlotId) -> CssPixelRect {if !arena.paintable_row_is_populated(slot) {return CssPixelRect::default();diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rsindex ab5f20ad841..537213b67f9 100644--- a/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs@@ -129,7 +129,33 @@ pub(crate) fn svg_stroke_style(style: &ComputedValuesView<'_>,) -> SvgStrokeStyle {let basis = crate::painting::paintable_geometry::committed_svg_viewport_percentage_basis(layout_arena, paintable);- svg_stroke_style_for_percentage_basis(style, basis)+ let mut stroke = svg_stroke_style_for_percentage_basis(style, basis);+ if !stroke.dash_array.is_empty()+ && let Some(path_length) =+ crate::painting::paintable_geometry::committed_svg_author_path_length(layout_arena, paintable)+ {+ // https://svgwg.org/svg2-draft/paths.html#PathLengthAttribute+ // The user agent will scale all distance-along-a-path computations by the ratio of 'pathLength' to the user+ // agent's own computed value for total path length.+ // AD-HOC: The spec says "'pathLength' has no effect on percentage distance-along-a-path calculations", but a+ // percentage in the dash array or offset is resolved first and then scaled like any other length, for+ // web compatibility.+ if path_length == 0.0 {+ // A value of zero is valid and must be treated as a scaling factor of infinity. A value of zero scaled+ // infinitely must remain zero, while any non-percentage value greater than zero must become +Infinity.+ // AD-HOC: The stroke draws solid, for web compatibility, rather than keeping each zero-length dash or+ // gap at zero; that's the same stroke for any dash array without a zero in it.+ stroke.dash_array.clear();+ stroke.dash_offset = 0.0;+ } else if path_length > 0.0+ && let Some(path) = crate::painting::paintable_geometry::committed_svg_path(layout_arena, paintable)+ {+ let scale = path.length() / path_length;+ stroke.dash_array.iter_mut().for_each(|dash| *dash *= scale);+ stroke.dash_offset *= scale;+ }+ }+ stroke}/// The stroke properties of an SVG shape, with percentages resolved against the given basis.diff --git a/Libraries/LibWeb/SVG/SVGGeometryElement.cpp b/Libraries/LibWeb/SVG/SVGGeometryElement.cppindex e4896ac66b3..48774cc0210 100644--- a/Libraries/LibWeb/SVG/SVGGeometryElement.cpp+++ b/Libraries/LibWeb/SVG/SVGGeometryElement.cpp@@ -197,9 +197,11 @@ WebIDL::ExceptionOr<bool> SVGGeometryElement::is_point_in_stroke(Bindings::DOMPo// https://svgwg.org/svg2-draft/paths.html#PathLengthAttribute// The user agent will scale all distance-along-a-path computations by the ratio of ‘pathLength’ to the// user agent's own computed value for total path length.- // FIXME: A value of zero must be treated as a scaling factor of infinity.+ // A value of zero is valid and must be treated as a scaling factor of infinity.float dash_scale = 1;- if (auto path_length = parse_number(get_attribute_value(AttributeNames::pathLength)); path_length.has_value() && *path_length > 0)+ if (auto path_length = author_path_length(); path_length.has_value() && *path_length == 0)+ dash_scale = AK::Infinity<float>;+ else if (path_length.has_value() && *path_length > 0)dash_scale = Layout::RustFFI::svg_geometry_element_total_length(&shape) / *path_length;return Layout::RustFFI::svg_geometry_element_stroke_contains(&shape, dash_scale, host_scale, static_cast<float>(point.x), static_cast<float>(point.y));});@@ -250,6 +252,20 @@ WebIDL::ExceptionOr<GC::Ref<Geometry::DOMPoint>> SVGGeometryElement::get_point_areturn Geometry::DOMPoint::create(point_along_path->x(), point_along_path->y(), 0, 1);}+void SVGGeometryElement::attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_)+{+ Base::attribute_changed(name, old_value, value, namespace_);++ // The pathLength scales the dashes of the stroke, which layout reads with the element's geometry.+ if (name == AttributeNames::pathLength && !namespace_.has_value() && old_value != value)+ set_needs_layout_update(DOM::SetNeedsLayoutReason::StyleChange);+}++Optional<float> SVGGeometryElement::author_path_length() const+{+ return parse_number(get_attribute_value(AttributeNames::pathLength));+}+GC::Ref<SVGAnimatedNumber> SVGGeometryElement::path_length(){if (!m_path_length)diff --git a/Libraries/LibWeb/SVG/SVGGeometryElement.h b/Libraries/LibWeb/SVG/SVGGeometryElement.hindex 89e5057a68b..dd20a483231 100644--- a/Libraries/LibWeb/SVG/SVGGeometryElement.h+++ b/Libraries/LibWeb/SVG/SVGGeometryElement.h@@ -31,9 +31,13 @@ public:GC::Ref<SVGAnimatedNumber> path_length();+ // The pathLength attribute's value, where it parses as a number.+ Optional<float> author_path_length() const;+protected:SVGGeometryElement(DOM::Document& document, DOM::QualifiedName qualified_name);virtual void visit_edges(Cell::Visitor&) override;+ virtual void attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_) override;private:bool with_computed_values(Function<void(CSSPixelSize viewport_size, CSS::ComputedValues const&)> const&);diff --git a/Tests/LibWeb/Ref/expected/svg/path-length-scales-stroke-dashes-ref.html b/Tests/LibWeb/Ref/expected/svg/path-length-scales-stroke-dashes-ref.htmlnew file mode 100644index 00000000000..dbef9dcc3dc--- /dev/null+++ b/Tests/LibWeb/Ref/expected/svg/path-length-scales-stroke-dashes-ref.html@@ -0,0 +1,12 @@+<!DOCTYPE html>+<style>+ svg * { fill: none; stroke: black; stroke-width: 4; }+</style>+<svg width="240" height="140">+ <path d="M 10 10 H 110" stroke-dasharray="20 30" />+ <line x1="10" y1="30" x2="110" y2="30" stroke-dasharray="20" stroke-dashoffset="10" />+ <rect x="10" y="50" width="100" height="20" stroke-dasharray="50 70" />+ <polyline points="130 10 230 10 230 70" stroke-dasharray="12 4" />+ <path d="M 10 100 H 110" stroke-dasharray="20 30" />+ <path d="M 10 120 H 110" stroke-dasharray="20 30" />+</svg>diff --git a/Tests/LibWeb/Ref/input/svg/path-length-scales-stroke-dashes.html b/Tests/LibWeb/Ref/input/svg/path-length-scales-stroke-dashes.htmlnew file mode 100644index 00000000000..4fab975376f--- /dev/null+++ b/Tests/LibWeb/Ref/input/svg/path-length-scales-stroke-dashes.html@@ -0,0 +1,20 @@+<!DOCTYPE html>+<link rel="match" href="../../expected/svg/path-length-scales-stroke-dashes-ref.html" />+<style>+ svg * { fill: none; stroke: black; stroke-width: 4; }+</style>+<svg width="240" height="140">+ <path d="M 10 10 H 110" pathLength="10" stroke-dasharray="2 3" />+ <line x1="10" y1="30" x2="110" y2="30" pathLength="50" stroke-dasharray="10" stroke-dashoffset="5" />+ <rect x="10" y="50" width="100" height="20" pathLength="24" stroke-dasharray="5 7" />+ <polyline points="130 10 230 10 230 70" pathLength="40" stroke-dasharray="3 1" />+ <path d="M 10 100 H 110" pathLength="-5" stroke-dasharray="20 30" />+ <path id="dynamic" d="M 10 120 H 110" stroke-dasharray="2 3" />+</svg>+<script>+ requestAnimationFrame(() => {+ requestAnimationFrame(() => {+ dynamic.setAttribute("pathLength", "10");+ });+ });+</script>