Skip to content

Truly independent web browser

LibWeb: Resolve SVG stroke percentages against the normalized diagonal

Aliaksandr Kalenik6562616bbe90
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index 248c04e67bd..cd6f8ffbe1b 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -910,7 +910,11 @@ impl SvgShapeGeometry {
let cy = to_px(&svg_reset.cy, self.viewport_height).to_float();
// Percentages refer to the normalized diagonal of the current SVG viewport
// (see Units: https://svgwg.org/svg2-draft/coords.html#Units)
- let r = to_px(&svg_reset.r, self.normalized_diagonal_length()).to_float();
+ let normalized_diagonal = crate::svg::stroke_style::percentage_basis(FfiCssPixelSize {
+ width: self.viewport_width,
+ height: self.viewport_height,
+ });
+ let r = to_px(&svg_reset.r, normalized_diagonal).to_float();
// A zero radius disables rendering.
if r == 0.0 {
@@ -931,19 +935,6 @@ impl SvgShapeGeometry {
path
}
- fn normalized_diagonal_length(&self) -> CssPixels {
- if self.viewport_width == self.viewport_height {
- return self.viewport_width;
- }
- let sum_of_squares = self.viewport_width * self.viewport_width + self.viewport_height * self.viewport_height;
- CssPixels::nearest_value_for_f32(
- sum_of_squares
- .div_as_fraction(CssPixels::from_integer(2))
- .to_float()
- .sqrt(),
- )
- }
-
fn svg_ellipse_path(&self, style: StyleValues<'_>) -> libgfx_rust::path::Path {
let mut path = libgfx_rust::path::Path::new();
let svg_reset = style.svg_reset();
diff --git a/Libraries/LibWeb/Rust/src/svg/stroke_style.rs b/Libraries/LibWeb/Rust/src/svg/stroke_style.rs
index 9f9179cfd39..3554db66a3a 100644
--- a/Libraries/LibWeb/Rust/src/svg/stroke_style.rs
+++ b/Libraries/LibWeb/Rust/src/svg/stroke_style.rs
@@ -19,12 +19,20 @@ pub(crate) struct SvgStrokeStyle {
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.
+/// The normalized diagonal of an SVG viewport of size `viewport`, which percentages of lengths that are neither
+/// horizontal nor vertical, such as a stroke's width, resolve against.
+/// https://svgwg.org/svg2-draft/coords.html#Units
pub(crate) fn percentage_basis(viewport: FfiCssPixelSize) -> CssPixels {
- (viewport.width + viewport.height) * CssPixels::nearest_value_for(0.5)
+ if viewport.width == viewport.height {
+ return viewport.width;
+ }
+ let sum_of_squares = viewport.width * viewport.width + viewport.height * viewport.height;
+ CssPixels::nearest_value_for_f32(
+ sum_of_squares
+ .div_as_fraction(CssPixels::from_integer(2))
+ .to_float()
+ .sqrt(),
+ )
}
/// The stroke properties of an SVG shape, with percentages resolved against the given basis.
diff --git a/Tests/LibWeb/Ref/expected/svg/stroke-width-percentage-of-normalized-diagonal-ref.html b/Tests/LibWeb/Ref/expected/svg/stroke-width-percentage-of-normalized-diagonal-ref.html
new file mode 100644
index 00000000000..f0724b878eb
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/svg/stroke-width-percentage-of-normalized-diagonal-ref.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<style>
+ body {
+ margin: 0;
+ }
+ svg {
+ display: block;
+ }
+</style>
+<svg width="350" height="50">
+ <rect x="10" y="15" width="330" height="20" fill="green" />
+</svg>
+<svg width="350" height="50">
+ <rect x="10" y="15" width="330" height="20" fill="green" />
+</svg>
diff --git a/Tests/LibWeb/Ref/input/svg/stroke-width-percentage-of-normalized-diagonal.html b/Tests/LibWeb/Ref/input/svg/stroke-width-percentage-of-normalized-diagonal.html
new file mode 100644
index 00000000000..576cd9e82fa
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/svg/stroke-width-percentage-of-normalized-diagonal.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<link rel="match" href="../../expected/svg/stroke-width-percentage-of-normalized-diagonal-ref.html" />
+<style>
+ body {
+ margin: 0;
+ }
+ svg {
+ display: block;
+ }
+ div {
+ width: 350px;
+ height: 50px;
+ }
+</style>
+<!-- The normalized diagonal of a 350x50 viewport is sqrt((350² + 50²) / 2) = 250. -->
+<div>
+ <svg width="100%" height="100%">
+ <line x1="10" y1="25" x2="340" y2="25" stroke="green" stroke-width="8%" />
+ </svg>
+</div>
+<svg width="350" height="50">
+ <line x1="10" y1="25" x2="340" y2="25" stroke="green" stroke-width="8%" />
+</svg>