Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ This changelog also contains important changes in dependencies.

This release has an MSRV of 1.87.0 for `usvg` and `resvg` and the C API.

### Fixed
- `dominant-baseline` is now correctly inherited by nested `<tspan>` elements,
so a nested span stays on the same baseline as its siblings. (#864)

## [0.47.0] 2026-02-05

This release has an MSRV of 1.87.0 for `usvg` and `resvg` and the C API.
Expand Down
1 change: 1 addition & 0 deletions crates/resvg/tests/integration/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,7 @@ use crate::render;
#[test] fn text_dominant_baseline_hanging() { assert_eq!(render("tests/text/dominant-baseline/hanging"), 0); }
#[test] fn text_dominant_baseline_ideographic() { assert_eq!(render("tests/text/dominant-baseline/ideographic"), 0); }
#[test] fn text_dominant_baseline_inherit() { assert_eq!(render("tests/text/dominant-baseline/inherit"), 0); }
#[test] fn text_dominant_baseline_inherited_through_nested_tspan() { assert_eq!(render("tests/text/dominant-baseline/inherited-through-nested-tspan"), 0); }
#[test] fn text_dominant_baseline_mathematical() { assert_eq!(render("tests/text/dominant-baseline/mathematical"), 0); }
#[test] fn text_dominant_baseline_middle() { assert_eq!(render("tests/text/dominant-baseline/middle"), 0); }
#[test] fn text_dominant_baseline_nested() { assert_eq!(render("tests/text/dominant-baseline/nested"), 0); }
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion crates/usvg/src/parser/svgtree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,11 +823,15 @@ impl AId {
fn is_non_inheritable(id: AId) -> bool {
matches!(
id,
// `dominant-baseline` is intentionally *not* listed here: unlike
// `alignment-baseline` and `baseline-shift`, it is an inherited property
// (CSS Inline Layout 3 / SVG 2, matching Chromium and Inkscape). Treating
// it as non-inheritable broke nested `<tspan>`s, whose direct parent does
// not carry the value (https://github.com/linebender/resvg/issues/864).
AId::AlignmentBaseline
| AId::BaselineShift
| AId::ClipPath
| AId::Display
| AId::DominantBaseline
| AId::Filter
| AId::FloodColor
| AId::FloodOpacity
Expand Down
100 changes: 100 additions & 0 deletions crates/usvg/tests/text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2024 the Resvg Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::sync::Arc;

use once_cell::sync::Lazy;
use usvg::{DominantBaseline, Group, Node, Text};

static GLOBAL_FONTDB: Lazy<Arc<usvg::fontdb::Database>> = Lazy::new(|| {
let mut fontdb = usvg::fontdb::Database::new();
fontdb.load_fonts_dir("../resvg/tests/fonts");
fontdb.set_serif_family("Noto Serif");
fontdb.set_sans_serif_family("Noto Sans");
fontdb.set_cursive_family("Yellowtail");
fontdb.set_fantasy_family("Sedgwick Ave Display");
fontdb.set_monospace_family("Noto Mono");
Arc::new(fontdb)
});

fn parse(svg: &str) -> usvg::Tree {
let opt = usvg::Options {
fontdb: GLOBAL_FONTDB.clone(),
..Default::default()
};
usvg::Tree::from_str(svg, &opt).unwrap()
}

fn first_text(group: &Group) -> Option<&Text> {
for node in group.children() {
match node {
Node::Text(t) => return Some(t),
Node::Group(g) => {
if let Some(t) = first_text(g) {
return Some(t);
}
}
_ => {}
}
}
None
}

// Regression test for https://github.com/linebender/resvg/issues/864
//
// `dominant-baseline` is an inherited property (CSS Inline Layout 3 / SVG 2),
// so a nested `<tspan>` that does not set it must inherit the value from an
// ancestor `<text>` — even when its direct parent `<tspan>` doesn't carry it.
// Previously the nested span fell back to `Auto`, placing it on the alphabetic
// baseline while its sibling text used `text-after-edge`, so the two were no
// longer on the same line.
#[test]
fn nested_tspan_inherits_dominant_baseline() {
let svg = "<svg xmlns='http://www.w3.org/2000/svg' width='400' height='200'>
<text x='20' y='150' font-family='Noto Sans' dominant-baseline='text-after-edge'>
<tspan x='20' font-size='40'><tspan>hello</tspan> world</tspan>
</text>
</svg>";

let tree = parse(svg);
let text = first_text(tree.root()).expect("a text node");

let chunk = text
.chunks()
.iter()
.find(|c| c.text() == "hello world")
.expect("the 'hello world' chunk");

// Two spans: the nested `<tspan>hello` and the trailing ` world`.
assert_eq!(chunk.spans().len(), 2);
for span in chunk.spans() {
assert_eq!(
span.dominant_baseline(),
DominantBaseline::TextAfterEdge,
"span {:?} did not inherit dominant-baseline from <text>",
&chunk.text()[span.start()..span.end()]
);
}
}

// A nested `<tspan>` setting its own `dominant-baseline` still overrides the
// inherited one, and deeper descendants inherit the nested value.
#[test]
fn nested_tspan_dominant_baseline_override() {
let svg = "<svg xmlns='http://www.w3.org/2000/svg' width='400' height='200'>
<text x='20' y='150' font-family='Noto Sans' dominant-baseline='text-after-edge'>
<tspan x='20' font-size='40' dominant-baseline='hanging'><tspan>hi</tspan></tspan>
</text>
</svg>";

let tree = parse(svg);
let text = first_text(tree.root()).expect("a text node");
let chunk = text
.chunks()
.iter()
.find(|c| c.text() == "hi")
.expect("the 'hi' chunk");

let span = &chunk.spans()[0];
assert_eq!(span.dominant_baseline(), DominantBaseline::Hanging);
}
Loading