Skip to content

Truly independent web browser

LibWeb: Align SVG text to its alignment-baseline

sideshowbarker8bdac480de90
diff --git a/Libraries/LibWeb/CSS/Properties.json b/Libraries/LibWeb/CSS/Properties.json
index cb821540574..42675432dee 100644
--- a/Libraries/LibWeb/CSS/Properties.json
+++ b/Libraries/LibWeb/CSS/Properties.json
@@ -419,6 +419,7 @@
]
},
"alignment-baseline": {
+ "style-group": "SVGResetValues",
"animation-type": "discrete",
"inherited": false,
"initial": "baseline",
@@ -427,7 +428,9 @@
"baseline-metric"
],
"valid-identifiers": [
- "baseline"
+ "baseline",
+ "text-after-edge>text-bottom",
+ "text-before-edge>text-top"
]
},
"all": {
diff --git a/Libraries/LibWeb/Rust/src/css/computed_value_types.rs b/Libraries/LibWeb/Rust/src/css/computed_value_types.rs
index 1e7f4cbafe7..ca36981936b 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_value_types.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_value_types.rs
@@ -943,6 +943,9 @@ pub struct SVGResetValues {
pub flood_color: StyleColor,
pub flood_opacity: f32,
pub vector_effect: u8,
+ /// Whether alignment-baseline names a baseline metric, rather than being 'baseline'.
+ pub has_alignment_baseline: bool,
+ pub alignment_baseline: u8,
}
/// A Rust-owned list of computed TextDecorationLine enum codes.
diff --git a/Libraries/LibWeb/Rust/src/css/computed_values.rs b/Libraries/LibWeb/Rust/src/css/computed_values.rs
index aeaca8fc6ee..df88771b490 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_values.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_values.rs
@@ -6066,6 +6066,8 @@ impl SVGResetValues {
flood_color: crate::css::computed_value_types::StyleColor::rgba(OPAQUE_BLACK_BGRA),
flood_opacity: 1.0,
vector_effect: vector_effect::NONE,
+ has_alignment_baseline: false,
+ alignment_baseline: 0,
}
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/table_group_builder.rs b/Libraries/LibWeb/Rust/src/css/table_group_builder.rs
index a43affd75b3..3feceb5f6fd 100644
--- a/Libraries/LibWeb/Rust/src/css/table_group_builder.rs
+++ b/Libraries/LibWeb/Rust/src/css/table_group_builder.rs
@@ -545,6 +545,10 @@ fn build_svg_reset_group(
.expect("a computed SVG reset opacity resolves")
};
let retained = |property| ComputedStyleValueHandle::from_retained(values.handle(property));
+ let alignment_baseline = values
+ .value(property_id::ALIGNMENT_BASELINE)
+ .and_then(keyword_of)
+ .and_then(crate::css::css_enums::keyword_to_baseline_metric);
let built = SVGResetValues {
cx: retained(property_id::CX),
cy: retained(property_id::CY),
@@ -563,6 +567,8 @@ fn build_svg_reset_group(
property_id::VECTOR_EFFECT,
crate::css::css_enums::keyword_to_vector_effect,
),
+ has_alignment_baseline: alignment_baseline.is_some(),
+ alignment_baseline: alignment_baseline.unwrap_or(0),
};
build_svg_reset_group_payload(group_index::SVG_RESET, built, parent)
}
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index bfb598d2914..b481e1f2707 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -1967,9 +1967,15 @@ impl<'pass> SvgFormattingContext<'pass> {
}
/// https://drafts.csswg.org/css-inline/#dominant-baseline-property
+ /// https://drafts.csswg.org/css-inline/#alignment-baseline-property
+ /// NB: The text of an element sits on the baseline its alignment-baseline names, where that's other than 'baseline',
+ /// and on its dominant baseline otherwise.
fn svg_dominant_baseline_offset(&self, style: StyleValues<'_>) -> f32 {
let svg = style.inherited_svg();
- let metric = if svg.has_dominant_baseline {
+ let svg_reset = style.svg_reset();
+ let metric = if svg_reset.has_alignment_baseline {
+ svg_reset.alignment_baseline
+ } else if svg.has_dominant_baseline {
svg.dominant_baseline
} else {
// https://drafts.csswg.org/css-inline/#valdef-dominant-baseline-auto
diff --git a/Tests/LibWeb/Text/expected/SVG/text-alignment-baseline.txt b/Tests/LibWeb/Text/expected/SVG/text-alignment-baseline.txt
new file mode 100644
index 00000000000..543135ac187
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/text-alignment-baseline.txt
@@ -0,0 +1,9 @@
+plain: 84
+attribute: 90
+property: 90
+tspan: 90
+hanging: 96.796875
+child: 84
+on-path: 240
+computed on the tspan's parent: central
+computed on its child: baseline
diff --git a/Tests/LibWeb/Text/input/SVG/text-alignment-baseline.html b/Tests/LibWeb/Text/input/SVG/text-alignment-baseline.html
new file mode 100644
index 00000000000..a7c7833cd9b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/text-alignment-baseline.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ @font-face {
+ font-family: Ahem;
+ src: url("../../../Ref/data/fonts/Ahem.ttf");
+ }
+</style>
+<svg width="400" height="300" style="font: 20px Ahem">
+ <path id="path" d="M 0 250 H 400" />
+ <text id="plain" x="0" y="100">X</text>
+ <text id="attribute" x="40" y="100" alignment-baseline="central">X</text>
+ <text id="property" x="80" y="100" style="alignment-baseline: central">X</text>
+ <text x="120" y="100">X<tspan id="tspan" alignment-baseline="central">X</tspan></text>
+ <text x="200" y="100" dominant-baseline="central">X<tspan id="hanging" alignment-baseline="hanging">X</tspan></text>
+ <text id="not-inherited" x="280" y="100" alignment-baseline="central">X<tspan id="child">X</tspan></text>
+ <text><textPath id="on-path" href="#path" alignment-baseline="central">X</textPath></text>
+</svg>
+<script>
+ asyncTest(async done => {
+ await document.fonts.ready;
+ for (const id of ["plain", "attribute", "property", "tspan", "hanging", "child", "on-path"])
+ println(`${id}: ${document.getElementById(id).getBBox().y}`);
+ println(`computed on the tspan's parent: ${getComputedStyle(document.getElementById("not-inherited")).alignmentBaseline}`);
+ println(`computed on its child: ${getComputedStyle(document.getElementById("child")).alignmentBaseline}`);
+ done();
+ });
+</script>