Truly independent web browser
LibWeb: Move SVG stroke style and paint color out of paint recording
Aliaksandr Kalenike2c01e40dbe0
diff --git a/Libraries/LibWeb/Rust/src/css/mod.rs b/Libraries/LibWeb/Rust/src/css/mod.rsindex 0851a8f7f80..c7af99ba2a5 100644--- a/Libraries/LibWeb/Rust/src/css/mod.rs+++ b/Libraries/LibWeb/Rust/src/css/mod.rs@@ -66,6 +66,7 @@ pub(crate) mod style_rule;pub(crate) mod style_sheet;pub(crate) mod style_value;pub(crate) mod style_value_arc;+pub(crate) mod svg_paint;pub mod table_group_builder;pub(crate) mod thin_slice;pub(crate) mod transform_functions;diff --git a/Libraries/LibWeb/Rust/src/css/svg_paint.rs b/Libraries/LibWeb/Rust/src/css/svg_paint.rsnew file mode 100644index 00000000000..05803ffb715--- /dev/null+++ b/Libraries/LibWeb/Rust/src/css/svg_paint.rs@@ -0,0 +1,16 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++use crate::css::computed_value_types::{ComputedSvgPaint, SVG_PAINT_COLOR, SVG_PAINT_NONE};++/// The color `paint` paints with, with `currentcolor` resolved to `current_color`.+pub(crate) fn svg_paint_color(paint: &ComputedSvgPaint, current_color: u32) -> Option<u32> {+ match paint.kind {+ SVG_PAINT_NONE => None,+ SVG_PAINT_COLOR => Some(paint.color.resolve(current_color)),+ _ => paint.has_color.then(|| paint.color.resolve(current_color)),+ }+}diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rsindex baac0220d68..a41ff0e2cea 100644--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs@@ -1234,10 +1234,10 @@ pub unsafe extern "C" fn svg_geometry_element_stroke_contains(// SAFETY: Guaranteed by the caller.unsafe {shape.measure(|path, style| {- // The same basis that layout resolves the stroke's percentages against.- // FIXME: The spec formula is the normalized diagonal sqrt((width² + height²) / 2), as for layout.- let basis = (viewport_size.width + viewport_size.height) * CssPixels::nearest_value_for(0.5);- let stroke = crate::painting::record::paint::svg::svg_stroke_style_for_percentage_basis(&style, basis);+ let stroke = crate::svg::stroke_style::svg_stroke_style_for_percentage_basis(+ &style,+ crate::svg::stroke_style::percentage_basis(viewport_size),+ );// https://svgwg.org/svg2-draft/painting.html#PaintingVectorEffects// With the non-scaling-stroke vector effect, stroke outline shall be calculated in the "host" coordinate// space instead of user coordinate system.@@ -1792,9 +1792,6 @@ impl<'pass> SvgFormattingContext<'pass> {}}- // Resolved relative to the "Scaled viewport size": https://www.w3.org/TR/2017/WD-fill-stroke-3-20170413/#scaled-viewport-size- // FIXME: The spec formula is the normalized diagonal sqrt((width² + height²) / 2); this keeps- // the historical (width + height) / 2 approximation.// <symbol> instances establish nested viewports; percentages inside one resolve against it,// not the enclosing <svg>.fn viewport_percentage_basis(&self, node: Node) -> CssPixels {@@ -1803,8 +1800,7 @@ impl<'pass> SvgFormattingContext<'pass> {else {return CssPixels::default();};- let viewport = self.svg_viewport_element_size(element);- (viewport.width + viewport.height) * CssPixels::nearest_value_for(0.5)+ crate::svg::stroke_style::percentage_basis(self.svg_viewport_element_size(element))}// https://svgwg.org/svg2-draft/struct.html#UseElement@@ -1851,9 +1847,8 @@ impl<'pass> SvgFormattingContext<'pass> {fn visible_stroke_width(&self, node: Node, viewport_percentage_basis: CssPixels) -> f32 {let style = self.style(node);let svg = style.inherited_svg();- let stroke_is_visible =- crate::painting::record::paint::svg::svg_paint_color(&svg.stroke, style.current_color())- .is_some_and(|color| color >> 24 != 0);+ let stroke_is_visible = crate::css::svg_paint::svg_paint_color(&svg.stroke, style.current_color())+ .is_some_and(|color| color >> 24 != 0);if !stroke_is_visible {return 0.0;}diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rsindex 49184741368..bbe6254d2a7 100644--- a/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs@@ -6,9 +6,10 @@use crate::painting::record::trace::Observer;-use crate::css::computed_value_types::{ComputedSvgPaint, SVG_PAINT_COLOR, SVG_PAINT_NONE, SVG_PAINT_URL};+use crate::css::computed_value_types::SVG_PAINT_URL;use crate::css::computed_value_views::ComputedValuesView;use crate::css::css_enums::paint_order;+use crate::css::svg_paint::svg_paint_color;use crate::layout::node_data::{NodeKind, NodeSlotId};use crate::painting::display_list::builder::PendingInlineClip;use crate::painting::display_list::commands::DisplayListGradientSpreadMethod;@@ -24,6 +25,7 @@ use crate::painting::record::{PaintPhase, PaintRecorder};use crate::painting::svg_paint_resources::{PublishedSvgGradient, PublishedSvgPaintServer, PublishedSvgPattern, SvgPaintResourceKind,};+use crate::svg::stroke_style::{SvgStrokeStyle, svg_stroke_style_for_percentage_basis};use libgfx_rust::{AffineTransform, CapStyle, Color, FloatRect, JoinStyle, ShouldAntiAlias, WindingRule};#[derive(Clone, Copy, Default)]@@ -49,15 +51,6 @@ struct SvgPaintFacts {references_paint_server: bool,}-/// The color `paint` paints with, with `currentcolor` resolved to `current_color`.-pub(crate) fn svg_paint_color(paint: &ComputedSvgPaint, current_color: u32) -> Option<u32> {- match paint.kind {- SVG_PAINT_NONE => None,- SVG_PAINT_COLOR => Some(paint.color.resolve(current_color)),- _ => paint.has_color.then(|| paint.color.resolve(current_color)),- }-}-fn svg_paint_facts<O: Observer>(recorder: &mut PaintRecorder<'_, O>,paintable: NodeSlotId,@@ -112,17 +105,6 @@ fn svg_paint_facts<O: Observer>((facts, stroke.dash_array)}-/// The properties that shape the stroke of an SVG shape, in its user units.-pub(crate) struct SvgStrokeStyle {- pub(crate) cap_style: CapStyle,- pub(crate) join_style: JoinStyle,- pub(crate) miter_limit: f32,- pub(crate) width: f32,- pub(crate) dash_array: Vec<f32>,- pub(crate) dash_offset: f32,- pub(crate) non_scaling_stroke: bool,-}-pub(crate) fn svg_stroke_style(layout_arena: &impl GeometryRead,paintable: NodeSlotId,@@ -158,57 +140,6 @@ pub(crate) fn svg_stroke_style(stroke}-/// The stroke properties of an SVG shape, with percentages resolved against the given basis.-pub(crate) fn svg_stroke_style_for_percentage_basis(- style: &ComputedValuesView<'_>,- basis: crate::layout::CssPixels,-) -> SvgStrokeStyle {- use crate::css::css_enums::{stroke_linecap, stroke_linejoin, vector_effect};- let svg = style.inherited_svg();- let resolve = |handle: &crate::css::computed_value_types::ComputedStyleValueHandle, default: f32| {- handle- .length_percentage()- .map_or(default, |value| value.to_px(basis).to_double() as f32)- };-- let mut dash_array: Vec<f32> = svg- .stroke_dasharray- .as_slice()- .iter()- .map(|dash| {- if dash.is_number {- dash.number as f32- } else {- resolve(&dash.value, 0.0)- }- })- .collect();- if dash_array.len() % 2 == 1 {- dash_array.extend_from_within(..);- }- if dash_array.iter().any(|value| *value < 0.0) || dash_array.iter().all(|value| *value == 0.0) {- dash_array.clear();- }-- SvgStrokeStyle {- cap_style: match svg.stroke_linecap {- stroke_linecap::ROUND => CapStyle::Round,- stroke_linecap::SQUARE => CapStyle::Square,- _ => CapStyle::Butt,- },- join_style: match svg.stroke_linejoin {- stroke_linejoin::ROUND => JoinStyle::Round,- stroke_linejoin::BEVEL => JoinStyle::Bevel,- _ => JoinStyle::Miter,- },- miter_limit: svg.stroke_miterlimit as f32,- width: resolve(&svg.stroke_width, 1.0),- dash_array,- dash_offset: resolve(&svg.stroke_dashoffset, 0.0),- non_scaling_stroke: style.svg_reset().vector_effect == vector_effect::NON_SCALING_STROKE,- }-}-fn should_anti_alias(value: bool) -> ShouldAntiAlias {if value {ShouldAntiAlias::Yesdiff --git a/Libraries/LibWeb/Rust/src/svg/mod.rs b/Libraries/LibWeb/Rust/src/svg/mod.rsindex d9f7a209475..2a9c1e99ce6 100644--- a/Libraries/LibWeb/Rust/src/svg/mod.rs+++ b/Libraries/LibWeb/Rust/src/svg/mod.rs@@ -7,5 +7,6 @@mod attribute_parser;mod parser;mod path_parser;+pub(crate) mod stroke_style;pub use path_parser::*;diff --git a/Libraries/LibWeb/Rust/src/svg/stroke_style.rs b/Libraries/LibWeb/Rust/src/svg/stroke_style.rsnew file mode 100644index 00000000000..9f9179cfd39--- /dev/null+++ b/Libraries/LibWeb/Rust/src/svg/stroke_style.rs@@ -0,0 +1,79 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++use crate::css::computed_value_views::ComputedValuesView;+use crate::css::css_pixels::{CssPixels, FfiCssPixelSize};+use libgfx_rust::{CapStyle, JoinStyle};++/// The properties that shape the stroke of an SVG shape, in its user units.+pub(crate) struct SvgStrokeStyle {+ pub(crate) cap_style: CapStyle,+ pub(crate) join_style: JoinStyle,+ pub(crate) miter_limit: f32,+ pub(crate) width: f32,+ pub(crate) dash_array: Vec<f32>,+ pub(crate) dash_offset: f32,+ pub(crate) non_scaling_stroke: bool,+}++/// What the stroke percentages of a shape in an SVG viewport of size `viewport` resolve against, its "scaled viewport+/// size": https://www.w3.org/TR/2017/WD-fill-stroke-3-20170413/#scaled-viewport-size+// FIXME: The spec formula is the normalized diagonal sqrt((width² + height²) / 2); this keeps+// the historical (width + height) / 2 approximation.+pub(crate) fn percentage_basis(viewport: FfiCssPixelSize) -> CssPixels {+ (viewport.width + viewport.height) * CssPixels::nearest_value_for(0.5)+}++/// The stroke properties of an SVG shape, with percentages resolved against the given basis.+pub(crate) fn svg_stroke_style_for_percentage_basis(+ style: &ComputedValuesView<'_>,+ basis: CssPixels,+) -> SvgStrokeStyle {+ use crate::css::css_enums::{stroke_linecap, stroke_linejoin, vector_effect};+ let svg = style.inherited_svg();+ let resolve = |handle: &crate::css::computed_value_types::ComputedStyleValueHandle, default: f32| {+ handle+ .length_percentage()+ .map_or(default, |value| value.to_px(basis).to_double() as f32)+ };++ let mut dash_array: Vec<f32> = svg+ .stroke_dasharray+ .as_slice()+ .iter()+ .map(|dash| {+ if dash.is_number {+ dash.number as f32+ } else {+ resolve(&dash.value, 0.0)+ }+ })+ .collect();+ if dash_array.len() % 2 == 1 {+ dash_array.extend_from_within(..);+ }+ if dash_array.iter().any(|value| *value < 0.0) || dash_array.iter().all(|value| *value == 0.0) {+ dash_array.clear();+ }++ SvgStrokeStyle {+ cap_style: match svg.stroke_linecap {+ stroke_linecap::ROUND => CapStyle::Round,+ stroke_linecap::SQUARE => CapStyle::Square,+ _ => CapStyle::Butt,+ },+ join_style: match svg.stroke_linejoin {+ stroke_linejoin::ROUND => JoinStyle::Round,+ stroke_linejoin::BEVEL => JoinStyle::Bevel,+ _ => JoinStyle::Miter,+ },+ miter_limit: svg.stroke_miterlimit as f32,+ width: resolve(&svg.stroke_width, 1.0),+ dash_array,+ dash_offset: resolve(&svg.stroke_dashoffset, 0.0),+ non_scaling_stroke: style.svg_reset().vector_effect == vector_effect::NON_SCALING_STROKE,+ }+}