GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
//! Laid-out type in SVG: every line of area type is its own positioned `<tspan>`, rich text keeps
//! its leading, baseline shift, scaling and kerning, and a renderer with the same font draws the
//! export where the canvas draws the text.

use craft_svg::tree::usvg;
use std::sync::Arc;

use vectorcraft_color::{Color, Paint};
use vectorcraft_doc::{CharStyle, Document, Justify, Node, NodeKind, TextKind, TextObject, TextRun};
use vectorcraft_geom::{Affine, BezPath, PathData, Point, Rect, shapes};
use vectorcraft_svg::{ExportOptions, export};
use vectorcraft_testkit::raster::{assert_similar, render_artboard};

fn style(size: f64) -> CharStyle {
    CharStyle { size, fill: Paint::solid(Color::BLACK), ..CharStyle::default() }
}

/// Area type `w` wide at (20, 20) in a 300 × 200 document.
fn area_doc(runs: Vec<TextRun>, w: f64, justify: Justify) -> Document {
    area_doc_justified(runs, w, justify, vectorcraft_doc::JustificationOptions::default())
}

/// [`area_doc`] with the paragraph's Justification options `j`.
fn area_doc_justified(runs: Vec<TextRun>, w: f64, justify: Justify, j: vectorcraft_doc::JustificationOptions) -> Document {
    let mut t = TextObject::point(Point::new(20.0, 20.0), "", style(12.0));
    t.runs = runs;
    t.kind = TextKind::Area { frame: shapes::rectangle(Rect::new(0.0, 0.0, w, 160.0)) };
    t.para.justify = justify;
    t.para.justification = j;
    let mut d = Document::new(300.0, 200.0);
    let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    d
}

fn run(text: &str, st: CharStyle) -> TextRun {
    TextRun { text: text.into(), style: st, inline: None }
}

/// The `<tspan …>` start tags.
fn tspans(svg: &str) -> Vec<&str> {
    svg.match_indices("<tspan").map(|(i, _)| &svg[i..i + svg[i..].find('>').unwrap()]).collect()
}

fn attr(tag: &str, name: &str) -> Option<f64> {
    let k = format!(" {name}=\"");
    let i = tag.find(&k)? + k.len();
    tag[i..i + tag[i..].find('"')?].parse().ok()
}

#[test]
fn wrapped_lines_get_one_tspan_each() {
    let d = area_doc(vec![run("alpha beta gamma delta epsilon zeta", style(12.0))], 70.0, Justify::Left);
    let svg = export(&d, &ExportOptions::default());
    let tags = tspans(&svg);
    let ys: Vec<f64> = tags.iter().map(|t| attr(t, "y").expect("each line is placed")).collect();
    assert!(ys.len() >= 3, "the text wraps: {svg}");
    assert!(ys.windows(2).all(|w| w[1] > w[0]), "baselines go down: {ys:?}");
    // Auto leading: 120 % of the size between baselines.
    assert!((ys[1] - ys[0] - 14.4).abs() < 0.01, "{ys:?}");
    assert!(tags.iter().all(|t| attr(t, "x") == Some(0.0)), "left aligned at the frame's edge: {svg}");
    // Reading it back gives a line per baseline, starting at the first baseline.
    let back = vectorcraft_svg::import(&svg).unwrap();
    let text = back.layers[0].children().unwrap().iter().find_map(|n| if let NodeKind::Text(t) = &n.kind { Some(t.clone()) } else { None }).unwrap();
    assert_eq!(text.plain_text().lines().count(), ys.len(), "{:?}", text.plain_text());
    assert!((text.xf.translation().y - (20.0 + ys[0])).abs() < 0.01);
    // Centred lines are anchored at the frame's centre, so they stay centred in any font.
    let svg = export(&area_doc(vec![run("alpha beta gamma delta", style(12.0))], 70.0, Justify::Center), &ExportOptions::default());
    assert!(svg.contains("text-anchor=\"middle\""), "{svg}");
    assert!(tspans(&svg).iter().all(|t| (attr(t, "x").unwrap() - 35.0).abs() < 0.01), "{svg}");
    // Left-aligned (and justified) lines are placed absolutely.
    assert!(!export(&d, &ExportOptions::default()).contains("text-anchor"));
}

#[test]
fn vertically_aligned_area_type_exports_where_it_is_drawn() {
    let top = area_doc(vec![run("alpha beta", style(12.0))], 200.0, Justify::Left);
    let mut centred = top.clone();
    let id = centred.layers[0].children().unwrap()[0].id;
    if let Some(NodeKind::Text(t)) = centred.node_mut(id).map(|n| &mut n.kind) {
        t.area.vertical_align = vectorcraft_doc::VerticalAlign::Center;
    }
    let y = |d: &Document| attr(tspans(&export(d, &ExportOptions::default()))[0], "y").unwrap();
    let (yt, yc) = (y(&top), y(&centred));
    // The 160 pt frame holds one 12 pt line: centring moves it down about (160 - 14.4) / 2.
    assert!(yc - yt > 60.0 && yc - yt < 80.0, "{yt} → {yc}");
}

#[test]
fn rich_text_writes_spacing_shift_and_scale() {
    let shifted = CharStyle { baseline_shift: 4.0, ..style(12.0) };
    let wide = CharStyle { h_scale: 150.0, ..style(12.0) };
    let kerned = CharStyle { tracking: 50.0, kerning: Some(-20.0), ..style(12.0) };
    let tall = CharStyle { v_scale: 200.0, h_scale: 200.0, leading: Some(30.0), ..style(12.0) };
    let d = area_doc(
        vec![run("Base ", style(12.0)), run("up ", shifted), run("wide ", wide), run("kern\n", kerned), run("tall", tall)],
        280.0,
        Justify::Left,
    );
    let svg = export(&d, &ExportOptions::default());
    let tags = tspans(&svg);
    assert_eq!(tags.len(), 5, "{svg}");
    assert_eq!(attr(tags[1], "dy"), Some(-4.0), "baseline shift: {}", tags[1]);
    assert!(attr(tags[2], "textLength").is_some() && tags[2].contains("lengthAdjust=\"spacingAndGlyphs\""), "{}", tags[2]);
    assert!(tags[3].contains("letter-spacing=\"0.36\"") && tags[3].contains("font-kerning:none"), "tracking + kerning: {}", tags[3]);
    assert!(tags[4].contains("font-size=\"24\"") && attr(tags[4], "textLength").is_none(), "uniform scale is the size: {}", tags[4]);
    assert!((attr(tags[4], "y").unwrap() - attr(tags[0], "y").unwrap() - 30.0).abs() < 0.01, "leading");
    // Segments on one baseline share its y; the shifted one is placed with dy.
    assert_eq!(attr(tags[1], "y"), attr(tags[0], "y"));
}

/// The space before a line's first character (`kerningBefore`, #5111) moves the line's first
/// `<tspan>` to where the character is drawn, left aligned or anchored at a centred line's centre.
#[test]
fn the_space_before_a_lines_first_character_moves_its_tspan() {
    let before = CharStyle { kerning_before: 250.0, ..style(12.0) };
    // 250/1000 em of 12 pt: 3 pt. The second paragraph's first character takes it too; the "O" after
    // the first "H" is inside its line, so its value does nothing.
    let runs = vec![run("HO", before.clone()), run("H\n", style(12.0)), run("H", before.clone()), run("O", style(12.0))];
    let svg = export(&area_doc(runs, 200.0, Justify::Left), &ExportOptions::default());
    // The first <tspan> on each baseline.
    let mut starts: Vec<(f64, f64)> = vec![];
    for t in tspans(&svg) {
        let (x, y) = (attr(t, "x").unwrap(), attr(t, "y").unwrap());
        if starts.last().is_none_or(|&(_, last)| last != y) {
            starts.push((x, y));
        }
    }
    assert_eq!(starts.iter().map(|s| s.0).collect::<Vec<_>>(), vec![3.0, 3.0], "{svg}");
    // Centred, the line is 3 pt longer and the anchor sits at the middle of what a reader draws.
    let svg = export(&area_doc(vec![run("H", before.clone()), run("OH", style(12.0))], 200.0, Justify::Center), &ExportOptions::default());
    let x = tspans(&svg).iter().find_map(|t| attr(t, "x")).unwrap();
    assert!((x - 101.5).abs() < 0.01, "{svg}");
    // A justified line, which a reader's own advances place word by word, starts there too.
    let words = vec![run("H", before.clone()), run("OH OH OH OH OH OH OH OH OH OH", style(12.0))];
    let svg = export(&area_doc(words, 90.0, Justify::JustifyLeft), &ExportOptions::default());
    assert_eq!(attr(tspans(&svg)[0], "x"), Some(3.0), "{svg}");
}

/// On a path, the space before the first character is its `dx` along the path. One before a
/// character after a line break, which the `<textPath>` can't write, is kept as outlines.
#[test]
fn the_space_before_the_first_character_on_a_path_is_its_dx() {
    let doc = |runs: Vec<TextRun>| {
        let mut t = TextObject::point(Point::ZERO, "", style(12.0));
        t.runs = runs;
        let mut line = BezPath::new();
        line.move_to((20.0, 100.0));
        line.line_to((280.0, 100.0));
        t.kind = TextKind::OnPath { path: PathData::from_bezpath(&line), start: 0.0, end: None };
        let mut d = Document::new(300.0, 200.0);
        let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
        let l = d.layers[0].id;
        d.insert(Some(l), 0, n).unwrap();
        vectorcraft_svg::export_full(&d, &ExportOptions::default(), None)
    };
    let before = CharStyle { kerning_before: 250.0, ..style(12.0) };
    let out = doc(vec![run("HO", before.clone()), run("H", style(12.0))]);
    let tags = tspans(&out.svg);
    assert!(out.svg.contains("<textPath"), "{}", out.svg);
    assert_eq!((attr(tags[0], "dx"), attr(tags[1], "dx")), (Some(3.0), None), "{}", out.svg);
    let out = doc(vec![run("HO\n", style(12.0)), run("H", before)]);
    assert!(!out.svg.contains("<textPath") && out.warnings.iter().any(|w| w.contains("line break")), "{:?}", out.warnings);
}

/// Insert Aki: each character the layout moves by its Aki starts a `<tspan>` where the character is
/// drawn, so a reader draws the text where the canvas does, on left-aligned, centered and
/// right-aligned lines and with fewer `<tspan>`s asked for too. On a path the type is outlined.
#[test]
fn insert_aki_places_each_moved_character() {
    // 250/1000 em of 12 pt before the O (3 pt) and 500/1000 after it (6 pt), then a run with Aki
    // after every character, and a second line.
    let spaced = CharStyle { aki_left: 250.0, aki_right: 500.0, ..style(12.0) };
    let runs = vec![run("H", style(12.0)), run("O", spaced), run("H ", style(12.0)), run("OHO\nHO", CharStyle { aki_right: 500.0, ..style(12.0) })];
    // A centered or right-aligned line whose characters the Aki moves is placed segment by
    // segment, not anchored.
    for justify in [Justify::Center, Justify::Right] {
        assert_drawn_in_place(&area_doc(runs.clone(), 200.0, justify));
    }
    let d = area_doc(runs, 200.0, Justify::Left);
    assert_drawn_in_place(&d);
    let plain = tspans(&export(&area_doc(vec![run("HOH OHO\nHO", style(12.0))], 200.0, Justify::Left), &ExportOptions::default())).len();
    let fewer = tspans(&export(&d, &ExportOptions { fewer_tspans: true, ..Default::default() })).len();
    assert!(fewer > plain, "each moved character is placed with fewer tspans too: {plain} {fewer}");
    let mut t = TextObject::point(Point::ZERO, "", style(12.0));
    t.runs = vec![run("HO", CharStyle { aki_left: 250.0, ..style(12.0) })];
    let mut line = BezPath::new();
    line.move_to((20.0, 100.0));
    line.line_to((280.0, 100.0));
    t.kind = TextKind::OnPath { path: PathData::from_bezpath(&line), start: 0.0, end: None };
    let mut d = Document::new(300.0, 200.0);
    let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let out = vectorcraft_svg::export_full(&d, &ExportOptions::default(), None);
    assert!(!out.svg.contains("<textPath") && out.warnings.iter().any(|w| w.contains("Insert Aki")), "{:?}", out.warnings);
}

/// Tsume: each character the layout moves by its Tsume starts a `<tspan>` where the character is
/// drawn, so a reader draws the text where the canvas does, with fewer `<tspan>`s asked for too.
/// On a path the type is outlined.
#[test]
fn tsume_places_each_moved_character() {
    let tight = CharStyle { tsume: 50.0, ..style(12.0) };
    let runs =
        vec![run("H", style(12.0)), run("O", tight.clone()), run("H ", style(12.0)), run("OHO\nHO", CharStyle { tsume: 100.0, ..style(12.0) })];
    for justify in [Justify::Left, Justify::Center, Justify::Right] {
        assert_drawn_in_place(&area_doc(runs.clone(), 200.0, justify));
    }
    let d = area_doc(runs, 200.0, Justify::Left);
    let plain = tspans(&export(&area_doc(vec![run("HOH OHO\nHO", style(12.0))], 200.0, Justify::Left), &ExportOptions::default())).len();
    let fewer = tspans(&export(&d, &ExportOptions { fewer_tspans: true, ..Default::default() })).len();
    assert!(fewer > plain, "each moved character is placed with fewer tspans too: {plain} {fewer}");
    let mut t = TextObject::point(Point::ZERO, "", style(12.0));
    t.runs = vec![run("HO", tight)];
    let mut line = BezPath::new();
    line.move_to((20.0, 100.0));
    line.line_to((280.0, 100.0));
    t.kind = TextKind::OnPath { path: PathData::from_bezpath(&line), start: 0.0, end: None };
    let mut d = Document::new(300.0, 200.0);
    let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let out = vectorcraft_svg::export_full(&d, &ExportOptions::default(), None);
    assert!(!out.svg.contains("<textPath") && out.warnings.iter().any(|w| w.contains("Tsume")), "{:?}", out.warnings);
}

/// Proportional Metrics (#966) is written as the `palt` feature, beside the character's own.
#[test]
fn proportional_metrics_write_palt() {
    let proportional = CharStyle { proportional_metrics: true, ..style(12.0) };
    let both = CharStyle { proportional_metrics: true, features: vec!["dlig".into()], ..style(12.0) };
    let d = area_doc(vec![run("雅楽 ", style(12.0)), run("「あ」 ", proportional), run("ab", both)], 280.0, Justify::Left);
    let svg = export(&d, &ExportOptions::default());
    let tags = tspans(&svg);
    assert_eq!(tags.len(), 3, "{svg}");
    assert!(!tags[0].contains("palt"), "{}", tags[0]);
    assert!(tags[1].contains("font-feature-settings:&quot;palt&quot; 1"), "{}", tags[1]);
    assert!(tags[2].contains("dlig") && tags[2].contains("palt"), "{}", tags[2]);
}

/// A feature with a value (`salt=2`) is written as that value, `-tag` as 0, a plain tag as 1; a
/// tag whose value is not a number is left out.
#[test]
fn feature_values_write_their_number() {
    let valued = CharStyle { features: vec!["salt=2".into(), "-liga".into(), "ss01".into(), "swsh=12".into(), "salt=x".into()], ..style(12.0) };
    let d = area_doc(vec![run("ab ", style(12.0)), run("cd", valued)], 280.0, Justify::Left);
    let svg = export(&d, &ExportOptions::default());
    let tags = tspans(&svg);
    assert_eq!(tags.len(), 2, "{svg}");
    assert!(tags[1].contains("font-feature-settings:&quot;salt&quot; 2, &quot;liga&quot; 0, &quot;ss01&quot; 1, &quot;swsh&quot; 12"), "{}", tags[1]);
    assert!(!tags[1].contains("salt=x") && !tags[0].contains("salt"), "{svg}");
}

/// Character Direction: live text writes each run set Left to Right as a bidi embedding
/// (`direction` and `unicode-bidi`). Type with a run set Right to Left is written as outlines.
#[test]
fn character_direction_is_written_on_its_runs() {
    use vectorcraft_doc::ParaDirection;
    let ltr = CharStyle { char_direction: Some(ParaDirection::LeftToRight), ..style(12.0) };
    let d = area_doc(vec![run("Due ", style(12.0)), run("21 10 2026", ltr.clone()), run(" ok", style(12.0))], 280.0, Justify::Left);
    let svg = export(&d, &ExportOptions::default());
    let tags = tspans(&svg);
    assert_eq!(tags.len(), 3, "{svg}");
    assert!(!tags[0].contains("direction") && !tags[2].contains("direction"), "{svg}");
    assert!(tags[1].contains(" direction=\"ltr\" unicode-bidi=\"embed\""), "{}", tags[1]);
    // Type on a path: on the run's `<tspan>` in the `<textPath>` too.
    let mut on_path = TextObject::point(Point::ZERO, "", style(12.0));
    on_path.runs = vec![run("Due ", style(12.0)), run("21 10 2026", ltr)];
    let mut line = BezPath::new();
    line.move_to((20.0, 100.0));
    line.line_to((280.0, 100.0));
    on_path.kind = TextKind::OnPath { path: PathData::from_bezpath(&line), start: 0.0, end: None };
    let mut d = Document::new(300.0, 200.0);
    let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(on_path)));
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let svg = export(&d, &ExportOptions::default());
    let tags = tspans(&svg);
    assert!(svg.contains("<textPath") && tags.len() == 2, "{svg}");
    assert!(!tags[0].contains("direction") && tags[1].contains(" direction=\"ltr\" unicode-bidi=\"embed\""), "{svg}");
    // A run set Right to Left is written as outlines, also when its characters are drawn left to
    // right (digits inside an embedding stay left to right).
    let rtl = CharStyle { char_direction: Some(ParaDirection::RightToLeft), ..style(12.0) };
    for date in ["21 10 2026", "2026"] {
        let d = area_doc(vec![run("Due ", style(12.0)), run(date, rtl.clone())], 280.0, Justify::Left);
        let svg = export(&d, &ExportOptions::default());
        assert!(!svg.contains("<text"), "{date}: {svg}");
    }
}

/// A run set Left to Right keeps its Character Direction when its SVG is opened again, on a line
/// and on a path.
#[test]
fn character_direction_reads_back() {
    use vectorcraft_doc::ParaDirection;
    let ltr = CharStyle { char_direction: Some(ParaDirection::LeftToRight), ..style(12.0) };
    let mut on_path = TextObject::point(Point::ZERO, "", style(12.0));
    on_path.runs = vec![run("Due ", style(12.0)), run("21 10 2026", ltr.clone())];
    let mut line = BezPath::new();
    line.move_to((20.0, 100.0));
    line.line_to((280.0, 100.0));
    on_path.kind = TextKind::OnPath { path: PathData::from_bezpath(&line), start: 0.0, end: None };
    let mut path_doc = Document::new(300.0, 200.0);
    let n = Node::new(path_doc.alloc_id(), NodeKind::Text(Box::new(on_path)));
    let l = path_doc.layers[0].id;
    path_doc.insert(Some(l), 0, n).unwrap();
    let area = area_doc(vec![run("Due ", style(12.0)), run("21 10 2026", ltr), run(" ok", style(12.0))], 280.0, Justify::Left);
    for (name, d) in [("area", area), ("path", path_doc)] {
        let svg = export(&d, &ExportOptions::default());
        let back = vectorcraft_svg::import(&svg).unwrap_or_else(|e| panic!("{name}: {e}\n{svg}"));
        let mut texts = vec![];
        back.walk(|n| {
            if let NodeKind::Text(t) = &n.kind {
                texts.push((**t).clone());
            }
        });
        assert_eq!(texts.len(), 1, "{name}: {svg}");
        let t = &texts[0];
        let dir = |part: &str| t.runs.iter().find(|r| r.text.contains(part)).map(|r| r.style.char_direction);
        assert_eq!(dir("2026"), Some(Some(ParaDirection::LeftToRight)), "{name}: {:?}\n{svg}", t.runs);
        assert_eq!(dir("Due"), Some(None), "{name}: {:?}", t.runs);
    }
}

/// Digits are written as drawn: the style's numeral system, not the digits that were typed (as
/// All Caps writes capitals).
#[test]
fn digits_are_written_as_drawn() {
    use vectorcraft_doc::Digits;
    let hindi = CharStyle { digits: Digits::Hindi, ..style(12.0) };
    let farsi = CharStyle { digits: Digits::Farsi, ..style(12.0) };
    let d = area_doc(vec![run("page 3, ", style(12.0)), run("page 4, ", hindi), run("page ٥", farsi)], 280.0, Justify::Left);
    let svg = export(&d, &ExportOptions::default());
    let text = svg.as_str();
    assert!(text.contains("page 3,"), "typed Western digits stay in Default: {svg}");
    assert!(text.contains("page ٤,"), "Hindi draws 4 as ٤: {svg}");
    assert!(text.contains("page ۵"), "Farsi draws ٥ as ۵: {svg}");
    assert!(!text.contains("page 4") && !text.contains("page ٥"), "{svg}");
}

/// A character drawn with a chosen glyph is written as outlines: a reader's `<text>` would draw the
/// default glyph.
#[test]
fn a_chosen_glyph_is_written_as_outlines() {
    use vectorcraft_text::test_fonts::{alternates_family, alternates_font, unencoded_glyphs};
    let db = vectorcraft_text::FontDb::global();
    db.add_font(alternates_font(true).unwrap());
    let [u1, ..] = unencoded_glyphs().unwrap();
    let face = db.face(alternates_family(true), "Regular").unwrap();
    let plain = CharStyle { font_family: alternates_family(true).into(), ..style(12.0) };
    let chosen = CharStyle { glyph_alternate: Some(face.choice('a', u1)), ..plain.clone() };
    let svg = export(&area_doc(vec![run("xay", plain.clone())], 280.0, Justify::Left), &ExportOptions::default());
    assert!(svg.contains("<text"), "without a choice the text stays live: {svg}");
    let d = area_doc(vec![run("x", plain.clone()), run("a", chosen), run("y", plain)], 280.0, Justify::Left);
    let svg = export(&d, &ExportOptions::default());
    assert!(!svg.contains("<text") && svg.contains("<path"), "{svg}");
}

/// Type with an Adjust Diacritic Position is written as outlines: a reader's `<text>` would draw
/// the marks where the font puts them.
#[test]
fn moved_diacritics_are_written_as_outlines() {
    let svg = export(&area_doc(vec![run("re\u{301}sume\u{301}", style(12.0))], 280.0, Justify::Left), &ExportOptions::default());
    assert!(svg.contains("<text"), "without a moved mark the text stays live: {svg}");
    for moved in [CharStyle { diacritic_horizontal: 100.0, ..style(12.0) }, CharStyle { diacritic_vertical: -100.0, ..style(12.0) }] {
        let d = area_doc(vec![run("re\u{301}", style(12.0)), run("sume\u{301}", moved)], 280.0, Justify::Left);
        let svg = export(&d, &ExportOptions::default());
        assert!(!svg.contains("<text") && svg.contains("<path"), "{svg}");
    }
}

/// A run whose font size measures the cap height is written at the em size it is drawn at: 14 pt
/// measuring a 0.7 em cap height is `font-size="20"`, after a 12 pt run measuring the em box,
/// which takes the `<text>` element's 12.
#[test]
fn font_height_reference_writes_the_size_drawn() {
    use vectorcraft_doc::FontHeightReference;
    use vectorcraft_text::test_fonts::{HEIGHT_FAMILY, height_font};
    vectorcraft_text::FontDb::global().add_font(height_font(true).unwrap());
    let em_box = CharStyle { font_family: HEIGHT_FAMILY.into(), ..style(12.0) };
    let cap = CharStyle { size: 14.0, font_height_reference: FontHeightReference::CapHeight, ..em_box.clone() };
    let d = area_doc(vec![run("Hx ", em_box), run("Hx", cap)], 280.0, Justify::Left);
    let svg = export(&d, &ExportOptions::default());
    assert!(svg.contains("font-size=\"12\" fill"), "{svg}");
    let sizes: Vec<_> = tspans(&svg).iter().map(|t| attr(t, "font-size")).collect();
    assert_eq!(sizes, [None, Some(20.0)], "{svg}");
}

#[test]
fn fewer_tspans_writes_one_per_line() {
    let bold = CharStyle { font_style: "Bold".into(), ..style(12.0) };
    let runs = vec![run("one ", style(12.0)), run("two", bold), run(" three\nfour", style(12.0))];
    let d = area_doc(runs, 280.0, Justify::Left);
    let full = export(&d, &ExportOptions::default());
    let fewer = export(&d, &ExportOptions { fewer_tspans: true, ..Default::default() });
    let placed = |s: &str| tspans(s).iter().filter(|t| attr(t, "y").is_some()).count();
    assert_eq!(placed(&full), 4, "{full}");
    assert_eq!(placed(&fewer), 2, "one positioned tspan per line: {fewer}");
    assert!(fewer.contains("font-weight=\"bold\""), "style changes stay: {fewer}");
    assert!(fewer.len() < full.len());
}

#[test]
fn justified_lines_place_each_word() {
    let d = area_doc(vec![run("aa bb cc dd ee ff gg hh ii jj kk ll", style(12.0))], 90.0, Justify::JustifyLeft);
    let svg = export(&d, &ExportOptions::default());
    let first_line_y = attr(tspans(&svg)[0], "y").unwrap();
    let words = tspans(&svg).iter().filter(|t| attr(t, "y") == Some(first_line_y)).count();
    assert!(words >= 3, "{svg}");
}

/// The only type in `d`.
fn the_text(d: &Document) -> TextObject {
    d.layers[0].children().unwrap().iter().find_map(|n| if let NodeKind::Text(t) = &n.kind { Some((**t).clone()) } else { None }).unwrap()
}

/// A drawn character: its text, where its first glyph is drawn, and the glyph's horizontal scale
/// (font units to user space).
type Drawn = (String, Point, f64);

/// Each character a usvg draws from `svg` (with the bundled font and the font files of `faces`)
/// and how it draws the character's first glyph, in SVG user space and text order; spaces and
/// tabs are left out.
fn viewer_glyph_positions(svg: &str, faces: &[Arc<vectorcraft_text::FontFace>]) -> Vec<Drawn> {
    let mut db = usvg::fontdb::Database::new();
    db.load_font_data(std::fs::read(concat!(env!("CARGO_MANIFEST_DIR"), "/../../assets/fonts/SourceSans3-Regular.ttf")).unwrap());
    for f in faces {
        db.load_font_data(f.file_data().to_vec());
    }
    let opt = usvg::Options { fontdb: Arc::new(db), ..Default::default() };
    let tree = usvg::Tree::from_str(svg, &opt).unwrap();
    fn walk(g: &usvg::Group, out: &mut Vec<Drawn>) {
        for n in g.children() {
            match n {
                usvg::Node::Group(g) => walk(g, out),
                usvg::Node::Text(t) => {
                    for g in t.layouted().iter().flat_map(|span| &span.positioned_glyphs) {
                        if g.text.trim().is_empty() {
                            continue;
                        }
                        let m = t.abs_transform().pre_concat(g.transform());
                        out.push((g.text.clone(), Point::new(m.tx as f64, m.ty as f64), m.sx as f64));
                    }
                }
                _ => {}
            }
        }
    }
    let mut out = vec![];
    walk(tree.root(), &mut out);
    out
}

/// Each character the canvas draws of the only type in `d` and how it draws the character's
/// first glyph, in document space and text order (spaces and tabs left out), with the faces
/// it draws them in.
fn canvas_glyph_positions(d: &Document) -> (Vec<Drawn>, Vec<Arc<vectorcraft_text::FontFace>>) {
    let t = the_text(d);
    let db = vectorcraft_text::FontDb::global();
    let lay = vectorcraft_text::layout(db, &t);
    let plain = t.plain_text();
    let mut out: Vec<Drawn> = vec![];
    let mut faces: Vec<Arc<vectorcraft_text::FontFace>> = vec![];
    let mut last = None;
    for (i, g) in lay.glyphs.iter().enumerate() {
        let mut text = lay.marker_text(i).unwrap_or_else(|| if g.len == 0 { "-" } else { &plain[g.byte..g.byte + g.len] }).to_string();
        // A list marker's characters each have a glyph of their own in these tests, in order.
        if let Some(m) = lay.marker_of(i).filter(|m| i != m.tab) {
            let k = (m.glyphs.start..i).filter(|&j| j != m.tab).count();
            text = m.text.chars().nth(k).map(String::from).unwrap_or_default();
        }
        if text.trim().is_empty() || (g.len > 0 && last == Some(g.byte)) {
            continue;
        }
        // A list marker's glyphs (length 0) start no cluster.
        last = (g.len > 0).then_some(g.byte);
        if let Some(f) = db.face_by_id(g.font_id).filter(|f| !faces.iter().any(|o| o.id() == f.id())) {
            faces.push(f);
        }
        let m = t.xf * g.xf;
        out.push((text.to_string(), m * Point::ZERO, m.as_coeffs()[0]));
    }
    (out, faces)
}

/// Asserts that a viewer draws every character of `d`'s live SVG text where the canvas draws it,
/// as wide as the canvas draws it.
fn assert_drawn_in_place(d: &Document) {
    let svg = export(d, &ExportOptions::default());
    let (canvas, faces) = canvas_glyph_positions(d);
    let viewer = viewer_glyph_positions(&svg, &faces);
    let chars = |v: &[Drawn]| v.iter().map(|(s, _, _)| s.as_str()).collect::<String>();
    assert_eq!(chars(&viewer), chars(&canvas), "{svg}");
    for ((s, v, vs), (_, c, cs)) in viewer.iter().zip(&canvas) {
        assert!((v.x - c.x).abs() < 0.05 && (v.y - c.y).abs() < 0.05, "{s}: the viewer draws it at {v:?}, the canvas at {c:?}: {svg}");
        assert!((vs / cs - 1.0).abs() < 0.002, "{s}: the viewer scales it by {vs}, the canvas by {cs}: {svg}");
    }
}

/// Bullets: live text writes each list paragraph's bullet as its character before the paragraph's
/// text, not as a hyphen, and a viewer draws the bullet and the text where the canvas draws them.
#[test]
fn bullets_are_written_as_their_characters_where_they_are_drawn() {
    use vectorcraft_doc::text::{Bullet, ParaList};
    for (bullet, character) in [(Bullet::Dot, "\u{2022}"), (Bullet::Dash, "\u{2013}"), (Bullet::Square, "\u{25AA}")] {
        let mut t = TextObject::point(Point::new(20.0, 20.0), "alpha beta gamma delta epsilon\nzeta", style(12.0));
        t.kind = TextKind::Area { frame: shapes::rectangle(Rect::new(0.0, 0.0, 120.0, 160.0)) };
        t.para.list = Some(ParaList::bulleted(bullet));
        t.para.left_indent = 18.0;
        t.para.first_line_indent = -18.0;
        let mut d = Document::new(300.0, 200.0);
        let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
        let l = d.layers[0].id;
        d.insert(Some(l), 0, n).unwrap();
        let svg = export(&d, &ExportOptions::default());
        assert_eq!(svg.matches(character).count(), 2, "one bullet before each paragraph: {svg}");
        assert!(svg.find(character) < svg.find("alpha") && !svg.contains(">-<") && !svg.contains("-alpha"), "{svg}");
        if bullet == Bullet::Dot {
            assert_drawn_in_place(&d);
        }
    }
    // A raised and rotated first character: the bullet is written on the baseline, upright.
    let raised = CharStyle { baseline_shift: 4.0, rotation: 20.0, ..style(12.0) };
    let mut t = TextObject::point(Point::new(20.0, 20.0), "", style(12.0));
    t.runs = vec![run("R", raised), run("aised", style(12.0))];
    t.para.list = Some(ParaList::bulleted(Bullet::Dot));
    let mut d = Document::new(300.0, 200.0);
    let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let svg = export(&d, &ExportOptions::default());
    // Each `<tspan …>` start tag with the text after it.
    let pieces: Vec<(&str, &str)> = svg.split("<tspan").skip(1).filter_map(|p| p.split_once('>')).collect();
    let (tag, _) = pieces.iter().find(|(_, text)| text.starts_with('\u{2022}')).expect("the bullet's tspan");
    assert!(!tag.contains("dy=") && !tag.contains("rotate="), "{svg}");
    assert!(
        pieces.iter().any(|(tag, text)| text.starts_with('R') && tag.contains("dy=") && tag.contains("rotate=")),
        "the R keeps its shift and rotation: {svg}"
    );
    // Horizontally scaled type: a viewer stretches the bullet to the bullet's own width, not to the
    // gap after it.
    let mut t = TextObject::point(Point::new(20.0, 20.0), "Narrow", CharStyle { h_scale: 50.0, ..style(12.0) });
    t.para.list = Some(ParaList::bulleted(Bullet::Dot));
    t.para.left_indent = 18.0;
    t.para.first_line_indent = -18.0;
    let lay = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), &t);
    let width = lay.glyphs[lay.markers[0].glyphs.start].advance;
    let mut d = Document::new(300.0, 200.0);
    let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let svg = export(&d, &ExportOptions::default());
    let pieces: Vec<(&str, &str)> = svg.split("<tspan").skip(1).filter_map(|p| p.split_once('>')).collect();
    let (tag, text) = pieces.iter().find(|(_, text)| text.starts_with('\u{2022}')).expect("the bullet's tspan");
    let length: f64 = tag.split("textLength=\"").nth(1).and_then(|r| r.split('"').next()).and_then(|v| v.parse().ok()).expect("a textLength");
    assert!(text.starts_with("\u{2022}<") && (length - width).abs() < 0.01, "the bullet alone, {width} wide: {svg}");
    // Underlined text: the underline is the text's, not the bullet's.
    let mut t = TextObject::point(Point::new(20.0, 20.0), "Underlined", CharStyle { underline: true, ..style(12.0) });
    t.para.list = Some(ParaList::bulleted(Bullet::Dot));
    let mut d = Document::new(300.0, 200.0);
    let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let svg = export(&d, &ExportOptions::default());
    let text_tag = svg.split("<text").nth(1).and_then(|p| p.split('>').next()).expect("a <text>");
    let pieces: Vec<(&str, &str)> = svg.split("<tspan").skip(1).filter_map(|p| p.split_once('>')).collect();
    let (bullet_tag, _) = pieces.iter().find(|(_, text)| text.starts_with('\u{2022}')).expect("the bullet's tspan");
    assert!(!text_tag.contains("text-decoration") && !bullet_tag.contains("text-decoration"), "{svg}");
    assert!(pieces.iter().any(|(tag, text)| text.starts_with("Underlined") && tag.contains("text-decoration=\"underline\"")), "{svg}");
    // Type on a path with a bullet is written as outlines, with a warning: a `<textPath>` holds the
    // characters of the text only.
    for list in [None, Some(ParaList::bulleted(Bullet::Dot))] {
        let mut line = BezPath::new();
        line.move_to((0.0, 50.0));
        line.line_to((250.0, 50.0));
        let mut t = TextObject::point(Point::new(20.0, 20.0), "Path", style(12.0));
        t.kind = TextKind::OnPath { path: PathData::from_bezpath(&line), start: 0.0, end: None };
        t.para.list = list;
        let mut d = Document::new(300.0, 200.0);
        let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
        let l = d.layers[0].id;
        d.insert(Some(l), 0, n).unwrap();
        let out = vectorcraft_svg::export_full(&d, &ExportOptions::default(), None);
        let warned = out.warnings.iter().any(|w| w.contains("bullets"));
        assert_eq!((out.svg.contains("<textPath"), warned), (list.is_none(), list.is_some()), "{:?}", out.warnings);
    }
}

/// Numbered lists: live text writes each list paragraph's number and period before the
/// paragraph's text, and a viewer draws them where the canvas draws them. Type on a path with
/// numbers is written as outlines, with a warning.
#[test]
fn numbers_are_written_as_their_characters_where_they_are_drawn() {
    use vectorcraft_doc::text::{NumberStyle, ParaList};
    let doc = |t: TextObject| {
        let mut d = Document::new(300.0, 200.0);
        let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
        let l = d.layers[0].id;
        d.insert(Some(l), 0, n).unwrap();
        d
    };
    let mut t = TextObject::point(Point::new(20.0, 20.0), "alpha beta gamma delta epsilon\nzeta\neta", style(12.0));
    t.kind = TextKind::Area { frame: shapes::rectangle(Rect::new(0.0, 0.0, 120.0, 160.0)) };
    t.para.list = Some(ParaList::numbered(NumberStyle::UpperRoman));
    t.para.left_indent = 18.0;
    t.para.first_line_indent = -18.0;
    let d = doc(t);
    let svg = export(&d, &ExportOptions::default());
    let at = |s: &str| svg.find(s).unwrap_or_else(|| panic!("{s} in {svg}"));
    assert!(at(">I.\t<") < at("alpha") && at("alpha") < at(">II.\t<") && at(">II.\t<") < at("zeta") && at("zeta") < at(">III.\t<"), "{svg}");
    assert!(!svg.contains(">-<"), "{svg}");
    assert_drawn_in_place(&d);
    // Aligned right: the gap comes before each number, which a viewer draws where the canvas does,
    // against the text.
    let mut t = TextObject::point(Point::new(20.0, 20.0), "alpha beta gamma delta epsilon\nzeta\neta", style(12.0));
    t.kind = TextKind::Area { frame: shapes::rectangle(Rect::new(0.0, 0.0, 120.0, 160.0)) };
    t.para.list = Some(ParaList::numbered(NumberStyle::UpperRoman).aligned(vectorcraft_doc::text::ListAlign::Right));
    t.para.left_indent = 18.0;
    t.para.first_line_indent = -18.0;
    let d = doc(t);
    let svg = export(&d, &ExportOptions::default());
    assert!(svg.contains(">III.<") && !svg.contains(">III.\t<") && !svg.contains(">-<"), "{svg}");
    assert_drawn_in_place(&d);
    let mut line = BezPath::new();
    line.move_to((0.0, 50.0));
    line.line_to((250.0, 50.0));
    let mut t = TextObject::point(Point::new(20.0, 20.0), "Path", style(12.0));
    t.kind = TextKind::OnPath { path: PathData::from_bezpath(&line), start: 0.0, end: None };
    t.para.list = Some(ParaList::numbered(NumberStyle::Arabic));
    let out = vectorcraft_svg::export_full(&doc(t), &ExportOptions::default(), None);
    assert!(!out.svg.contains("<textPath") && out.warnings.iter().any(|w| w.contains("numbers")), "{:?}", out.warnings);
}

/// A Justify All line without word spaces spreads its letters; live text keeps the gaps, with
/// tracking and manual kerning too.
#[test]
fn justify_all_lines_keep_their_letter_gaps() {
    let tracked = CharStyle { tracking: 80.0, ..style(12.0) };
    let kerned = CharStyle { kerning: Some(-40.0), ..style(12.0) };
    for runs in [vec![run("abcdefgh", style(12.0))], vec![run("abc", style(12.0)), run("def", tracked), run("ghi", kerned)]] {
        let d = area_doc(runs, 200.0, Justify::JustifyAll);
        assert_drawn_in_place(&d);
    }
}

/// Justification…: Desired word and letter spacing and glyph scaling, and the ranges a justified
/// line uses, are written so a viewer draws each character where and as wide as the canvas does.
#[test]
fn justification_spacing_and_glyph_scaling_are_drawn_in_place() {
    use vectorcraft_doc::JustificationOptions;
    let text = "Typography arranges type to make written language legible and readable";
    for (justify, j) in [
        (
            Justify::Left,
            JustificationOptions { word_spacing: [0.0, 180.0, 200.0], letter_spacing: [0.0, 10.0, 10.0], ..JustificationOptions::default() },
        ),
        (Justify::Left, JustificationOptions { glyph_scaling: [50.0, 120.0, 200.0], ..JustificationOptions::default() }),
        (
            Justify::JustifyLeft,
            JustificationOptions {
                word_spacing: [100.0, 100.0, 100.0],
                letter_spacing: [0.0, 0.0, 5.0],
                glyph_scaling: [95.0, 100.0, 110.0],
                ..JustificationOptions::default()
            },
        ),
    ] {
        assert_drawn_in_place(&area_doc_justified(vec![run(text, style(12.0))], 160.0, justify, j));
    }
}

/// Type on a path with Justification…'s Desired word or letter spacing or glyph scaling is outlined
/// with a warning, since a `<textPath>` places its characters one after the other at their own
/// advances. With the default Desired values it stays a `<textPath>`.
#[test]
fn justification_spacing_on_a_path_is_outlined() {
    use vectorcraft_doc::JustificationOptions;
    let doc = |j: JustificationOptions| {
        let mut t = TextObject::point(Point::ZERO, "HO HO", style(12.0));
        t.para.justification = j;
        let mut line = BezPath::new();
        line.move_to((20.0, 100.0));
        line.line_to((280.0, 100.0));
        t.kind = TextKind::OnPath { path: PathData::from_bezpath(&line), start: 0.0, end: None };
        let mut d = Document::new(300.0, 200.0);
        let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
        let l = d.layers[0].id;
        d.insert(Some(l), 0, n).unwrap();
        vectorcraft_svg::export_full(&d, &ExportOptions::default(), None)
    };
    let out = doc(JustificationOptions::default());
    assert!(out.svg.contains("<textPath") && out.warnings.is_empty(), "{:?}", out.warnings);
    for j in [
        JustificationOptions { word_spacing: [0.0, 200.0, 300.0], ..JustificationOptions::default() },
        JustificationOptions { letter_spacing: [0.0, 50.0, 50.0], ..JustificationOptions::default() },
        JustificationOptions { glyph_scaling: [50.0, 150.0, 200.0], ..JustificationOptions::default() },
    ] {
        let out = doc(j);
        assert!(!out.svg.contains("<textPath") && out.warnings.iter().any(|w| w.contains("Justification")), "{:?}", out.warnings);
    }
}

/// Justified and Mojikumi lines whose characters a reader draws where the layout does are written
/// as before: a segment per style run, and per word on a justified line.
#[test]
fn lines_without_moved_characters_are_written_as_before() {
    let text = |d: &Document| {
        let svg = export(d, &ExportOptions::default());
        svg[svg.find("<text").unwrap()..svg.find("</text>").unwrap()].to_string()
    };
    let justified = area_doc(vec![run("aa bb cc dd ee ff", style(12.0))], 60.0, Justify::JustifyLeft);
    let mut composed = area_doc(vec![run("Latin words only", style(12.0))], 200.0, Justify::Center);
    let id = composed.layers[0].children().unwrap()[0].id;
    if let Some(NodeKind::Text(t)) = composed.node_mut(id).map(|n| &mut n.kind) {
        t.para.mojikumi = vectorcraft_doc::Mojikumi::LineEndHalf;
    }
    let open = |x: &str, anchor: &str| {
        format!(
            r##"<text transform="matrix(1 0 0 1 20 20)" x="{x}" y="12" xml:space="preserve" font-family="&apos;Source Sans 3&apos;, sans-serif" font-size="12" fill="#000000"{anchor}>"##
        )
    };
    let words =
        [("aa ", "0", "12"), ("bb ", "15.636", "12"), ("cc ", "32.448", "12"), ("dd", "46.68", "12"), ("ee ", "0", "26.4"), ("ff", "14.304", "26.4")];
    let words: String = words.iter().map(|(w, x, y)| format!(r#"<tspan x="{x}" y="{y}">{w}</tspan>"#)).collect();
    assert_eq!(text(&justified), format!("{}{words}", open("0", "")));
    let centered = format!(r#"{}<tspan x="100" y="12">Latin words only</tspan>"#, open("100", r#" text-anchor="middle""#));
    assert_eq!(text(&composed), centered);
}

/// Type scaled horizontally on a justified line is stretched to its own width, not to the
/// justification space after it, nor to the font's kerning against the letter after it when
/// the letters are spread (AV is a kerned pair).
#[test]
fn justified_lines_keep_scaled_type_at_its_width() {
    let wide = CharStyle { h_scale: 150.0, ..style(12.0) };
    for justify in [Justify::JustifyLeft, Justify::JustifyAll] {
        assert_drawn_in_place(&area_doc(vec![run("aa bb cc dd ee ff gg hh ii jj kk ll", wide.clone())], 120.0, justify));
        assert_drawn_in_place(&area_doc(vec![run("abcdefgh", wide.clone())], 200.0, justify));
    }
    assert_drawn_in_place(&area_doc(vec![run("AVAVAVAV", wide)], 200.0, Justify::JustifyAll));
}

/// On lines that aren't spread, the spacing a reader applies itself (tracking, manual kerning,
/// horizontal scale, baseline shift) puts each character where the canvas draws it.
#[test]
fn ragged_lines_keep_their_character_spacing() {
    let tracked = CharStyle { tracking: 80.0, ..style(12.0) };
    let kerned = CharStyle { kerning: Some(-40.0), ..style(12.0) };
    let wide = CharStyle { h_scale: 150.0, ..style(12.0) };
    let shifted = CharStyle { baseline_shift: 3.0, ..style(12.0) };
    for justify in [Justify::Left, Justify::Center, Justify::Right, Justify::JustifyLeft] {
        let mut runs =
            vec![run("plain ", style(12.0)), run("tracked ", tracked.clone()), run("kerned ", kerned.clone()), run("up ", shifted.clone())];
        // usvg anchors a centered or right-aligned line by its width before `textLength` stretches it.
        if matches!(justify, Justify::Left | Justify::JustifyLeft) {
            runs.push(run("wide ", wide.clone()));
        }
        runs.push(run("end", style(12.0)));
        assert_drawn_in_place(&area_doc(runs, 280.0, justify));
    }
}

/// Tracking and manual kerning in type scaled uniformly, which no `textLength` stretches, take the
/// scale in a viewer as they do on the canvas.
#[test]
fn spacing_in_uniformly_scaled_type_takes_the_scale() {
    for st in [
        CharStyle { h_scale: 200.0, v_scale: 200.0, tracking: 100.0, ..style(12.0) },
        CharStyle { h_scale: 150.0, v_scale: 150.0, kerning: Some(80.0), ..style(12.0) },
    ] {
        assert_drawn_in_place(&area_doc(vec![run("tracked wide", st)], 280.0, Justify::Left));
    }
}

/// Type scaled horizontally before a tab is stretched to its own width, not to the tab's gap.
#[test]
fn scaled_type_before_a_tab_keeps_its_width() {
    for h_scale in [50.0, 150.0] {
        let scaled = CharStyle { h_scale, ..style(12.0) };
        assert_drawn_in_place(&area_doc(vec![run("ab\tcd ef\tgh", scaled)], 280.0, Justify::Left));
    }
}

/// Justified Japanese lines spread between their characters (#344); live text keeps the gaps.
#[test]
fn justified_japanese_lines_keep_the_gaps_between_their_characters() {
    let text = "日本語の文章は文字と文字の間で両端を揃えます。この段落は三行になります。";
    for justify in [Justify::JustifyLeft, Justify::JustifyAll] {
        assert_drawn_in_place(&area_doc(vec![run(text, style(12.0))], 100.0, justify));
    }
}

/// Area type that Shrink Text to Fit scales down to fit its frame is written at the size the
/// canvas draws it at: sizes, tracking, manual kerning and baseline shifts, on ragged, anchored
/// and justified lines.
#[test]
fn shrunk_area_type_is_written_at_the_size_it_draws_at() {
    let tracked = CharStyle { tracking: 80.0, ..style(12.0) };
    let kerned = CharStyle { kerning: Some(-40.0), ..style(12.0) };
    let wide = CharStyle { h_scale: 150.0, ..style(12.0) };
    let shifted = CharStyle { baseline_shift: 3.0, ..style(12.0) };
    for justify in [Justify::Left, Justify::Center, Justify::JustifyLeft, Justify::JustifyAll] {
        // Tracked and kerned letters are inside words: usvg also spaces a line's last letter, which
        // moves an anchored line ending in one.
        let mut runs = vec![
            run("plain words ", style(12.0)),
            run("trac", tracked.clone()),
            run("ked ", style(12.0)),
            run("ker", kerned.clone()),
            run("ned ", style(12.0)),
            run("up ", shifted.clone()),
            run("big ", style(18.0)),
        ];
        // usvg anchors a centered line by its width before `textLength` stretches it.
        if justify != Justify::Center {
            runs.push(run("wide ", wide.clone()));
        }
        runs.push(run("and more words at the end", style(12.0)));
        let mut d = area_doc(runs, 90.0, justify);
        let id = d.layers[0].children().unwrap()[0].id;
        if let Some(NodeKind::Text(t)) = d.node_mut(id).map(|n| &mut n.kind) {
            t.kind = TextKind::Area { frame: shapes::rectangle(Rect::new(0.0, 0.0, 90.0, 40.0)) };
            t.area.fit = vectorcraft_doc::AreaFit::ShrinkText { min_percent: 10.0 };
        }
        let lay = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), &the_text(&d));
        assert!(!lay.overflow && lay.fit_scale < 0.8, "{justify:?}: the text is shrunk to fit ({})", lay.fit_scale);
        assert_drawn_in_place(&d);
        // Fewer `<tspan>` Elements writes the shrunk size too.
        let fewer = export(&d, &ExportOptions { fewer_tspans: true, ..Default::default() });
        let size = attr(&fewer[fewer.find("<text").unwrap()..], "font-size").unwrap();
        assert!((size - 12.0 * lay.fit_scale).abs() < 1e-3, "{justify:?}: {fewer}");
    }
}

/// Japanese composition (Mojikumi) closes up consecutive punctuation and adds a quarter em between
/// Japanese and Latin letters; live text keeps that spacing, on anchored lines too.
#[test]
fn japanese_composition_keeps_its_spacing() {
    let text = "「かっこ」、「かな」。漢字とLatinの間。";
    for justify in [Justify::Left, Justify::Center, Justify::Right] {
        let mut d = area_doc(vec![run(text, style(12.0))], 280.0, justify);
        let id = d.layers[0].children().unwrap()[0].id;
        if let Some(NodeKind::Text(t)) = d.node_mut(id).map(|n| &mut n.kind) {
            t.para.mojikumi = vectorcraft_doc::Mojikumi::LineEndHalf;
        }
        assert_drawn_in_place(&d);
    }
}

/// Glyph outlines of every text a usvg (with the bundled font) lays out from `svg`, in SVG
/// user space.
fn viewer_outlines(svg: &str) -> Vec<BezPath> {
    let mut db = usvg::fontdb::Database::new();
    db.load_font_data(std::fs::read(concat!(env!("CARGO_MANIFEST_DIR"), "/../../assets/fonts/SourceSans3-Regular.ttf")).unwrap());
    let opt = usvg::Options { fontdb: Arc::new(db), ..Default::default() };
    let tree = usvg::Tree::from_str(svg, &opt).unwrap();
    fn walk(g: &usvg::Group, out: &mut Vec<BezPath>) {
        for n in g.children() {
            match n {
                usvg::Node::Group(g) => walk(g, out),
                usvg::Node::Text(t) => walk(t.flattened(), out),
                usvg::Node::Path(p) => {
                    let m = p.abs_transform();
                    let m = Affine::new([m.sx as f64, m.ky as f64, m.kx as f64, m.sy as f64, m.tx as f64, m.ty as f64]);
                    let mut bp = BezPath::new();
                    for s in p.data().segments() {
                        use craft_svg::tree::usvg::tiny_skia_path::PathSegment as S;
                        let pt = |q: usvg::tiny_skia_path::Point| m * Point::new(q.x as f64, q.y as f64);
                        match s {
                            S::MoveTo(a) => bp.move_to(pt(a)),
                            S::LineTo(a) => bp.line_to(pt(a)),
                            S::QuadTo(a, b) => bp.quad_to(pt(a), pt(b)),
                            S::CubicTo(a, b, c) => bp.curve_to(pt(a), pt(b), pt(c)),
                            S::Close => bp.close_path(),
                        }
                    }
                    out.push(bp);
                }
                usvg::Node::Image(_) => {}
            }
        }
    }
    let mut out = vec![];
    walk(tree.root(), &mut out);
    out
}

#[test]
fn a_viewer_draws_the_text_where_the_canvas_does() {
    // With a tab every line is placed absolutely; without one, centred and right-aligned lines are
    // anchored, and a baseline shift mid-line is undone by the next run.
    for (sep, shift) in [("	", 0.0), (" ", 3.0)] {
        let second = |fill: Color| CharStyle { fill: Paint::solid(fill), baseline_shift: shift, ..style(14.0) };
        let runs = |second: CharStyle| {
            vec![
                run("The quick brown fox jumps over the lazy dog. ", style(14.0)),
                run(&format!("Pack my box{sep}with"), second),
                run(" five dozen jugs.", style(14.0)),
            ]
        };
        for justify in [Justify::Left, Justify::Center, Justify::Right] {
            for fewer_tspans in [false, true] {
                let o = ExportOptions { fewer_tspans, ..Default::default() };
                let svg = export(&area_doc(runs(second(Color::rgb(0.8, 0.0, 0.0))), 200.0, justify), &o);
                assert!(svg.contains("fill=\"#cc0000\""), "{svg}");
                // The viewer's glyphs as black shapes, against the canvas drawing the text all black.
                let mut viewer = Document::new(300.0, 200.0);
                let l = viewer.layers[0].id;
                let outlines = viewer_outlines(&svg);
                assert!(outlines.iter().map(|b| b.elements().len()).sum::<usize>() > 200, "the viewer laid the text out");
                for bp in outlines {
                    let ap = vectorcraft_doc::Appearance::basic(Paint::solid(Color::BLACK), Paint::None, 0.0);
                    let n = Node::path(viewer.alloc_id(), PathData::from_bezpath(&bp), ap);
                    viewer.insert(Some(l), usize::MAX, n).unwrap();
                }
                let canvas = area_doc(runs(second(Color::BLACK)), 200.0, justify);
                assert_similar(&render_artboard(&canvas), &render_artboard(&viewer), 40.0, 0.01);
            }
        }
    }
}

#[test]
fn paragraphs_aligned_differently_keep_their_alignment() {
    let mut t = TextObject::point(Point::new(20.0, 20.0), "Left words here\nCentred line\nRight line", style(14.0));
    t.kind = TextKind::Area { frame: shapes::rectangle(Rect::new(0.0, 0.0, 200.0, 160.0)) };
    let para = |j| vectorcraft_doc::ParaStyle { justify: j, ..Default::default() };
    t.set_paragraph_styles(vec![para(Justify::Left), para(Justify::Center), para(Justify::Right)]);
    let mut d = Document::new(300.0, 200.0);
    let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    for fewer_tspans in [false, true] {
        let svg = export(&d, &ExportOptions { fewer_tspans, ..Default::default() });
        // Each anchored line carries its own anchor (the <text> has none).
        assert!(svg.contains("text-anchor=\"middle\"") && svg.contains("text-anchor=\"end\""), "{svg}");
        assert!(!svg.contains("<text text-anchor"), "{svg}");
        // A viewer draws each line where the canvas does.
        let mut viewer = Document::new(300.0, 200.0);
        let vl = viewer.layers[0].id;
        for bp in viewer_outlines(&svg) {
            let ap = vectorcraft_doc::Appearance::basic(Paint::solid(Color::BLACK), Paint::None, 0.0);
            let n = Node::path(viewer.alloc_id(), PathData::from_bezpath(&bp), ap);
            viewer.insert(Some(vl), usize::MAX, n).unwrap();
        }
        assert_similar(&render_artboard(&d), &render_artboard(&viewer), 40.0, 0.01);
        // Read back: one paragraph per line, each with its alignment, drawn where it was.
        let back = vectorcraft_svg::import(&svg).unwrap();
        let text =
            back.layers[0].children().unwrap().iter().find_map(|n| if let NodeKind::Text(t) = &n.kind { Some(t.clone()) } else { None }).unwrap();
        let js: Vec<Justify> = text.paragraph_styles().iter().map(|p| p.justify).collect();
        assert_eq!(js, [Justify::Left, Justify::Center, Justify::Right], "{svg}");
        assert_similar(&render_artboard(&d), &render_artboard(&back), 40.0, 0.01);
    }
}

/// With Kurikaeshi Moji Shori on, live type writes a repeat mark that starts a line as the
/// character it is drawn as and places the text after it anew, with and without embedded fonts; a
/// mark inside a line stays.
#[test]
fn a_repeat_mark_at_a_line_start_is_written_as_the_character_it_draws() {
    use vectorcraft_text::test_fonts::{RARE_REPEAT_MARK_FAMILY, rare_repeat_mark_font};
    vectorcraft_text::FontDb::global().add_font(rare_repeat_mark_font().unwrap());
    let st = CharStyle { font_family: RARE_REPEAT_MARK_FAMILY.into(), ..style(12.0) };
    for on in [false, true] {
        let mut t = TextObject::point(Point::new(20.0, 40.0), "龘\u{2028}々龘龘々", st.clone());
        t.para.kurikaeshi_moji_shori = on;
        let mut d = Document::new(300.0, 200.0);
        let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
        let l = d.layers[0].id;
        d.insert(Some(l), 0, n).unwrap();
        for embed_fonts in [false, true] {
            let svg = export(&d, &ExportOptions { embed_fonts, ..Default::default() });
            if !on {
                assert!(svg.contains(">々龘龘々<"), "{svg}");
                continue;
            }
            // The drawn character takes the mark's width on the canvas, so the text after it is
            // placed where the canvas draws it.
            let rest = svg.find(">龘龘々<").unwrap_or_else(|| panic!("{svg}"));
            let tag = &svg[svg[..rest].rfind('<').unwrap_or(0)..rest];
            assert!(tag.contains(" x=\""), "{tag}: {svg}");
            assert_eq!(svg.matches('々').count(), 1, "only the mark inside the line stays: {svg}");
        }
    }
}

/// Kurikaeshi Moji Shori in horizontally scaled type: the character a repeat mark is drawn as is
/// stretched (`textLength`) to the width the canvas draws that character at, not to the mark's
/// advance, so a reader does not distort it when the two characters' widths differ.
#[test]
fn a_drawn_repeat_mark_in_scaled_type_keeps_the_width_of_its_character() {
    use vectorcraft_text::test_fonts::{RARE_REPEAT_MARK_FAMILY, rare_repeat_mark_font};
    vectorcraft_text::FontDb::global().add_font(rare_repeat_mark_font().unwrap());
    // The test font draws 龘 and 々 with glyphs of different widths.
    let st = CharStyle { font_family: RARE_REPEAT_MARK_FAMILY.into(), h_scale: 50.0, ..style(12.0) };
    let mut t = TextObject::point(Point::new(20.0, 40.0), "龘\u{2028}々", st);
    t.para.kurikaeshi_moji_shori = true;
    let mut d = Document::new(300.0, 200.0);
    let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let svg = export(&d, &ExportOptions::default());
    // The textLength of each `<tspan>` that holds 龘 alone: the one on the first line and the one
    // the mark is drawn as on the second.
    let lengths: Vec<&str> = svg
        .match_indices(">龘<")
        .map(|(at, _)| {
            let tag = &svg[svg[..at].rfind('<').unwrap_or(0)..at];
            let from = tag.find("textLength=\"").unwrap_or_else(|| panic!("{tag}: {svg}")) + "textLength=\"".len();
            &tag[from..from + tag[from..].find('"').unwrap_or(0)]
        })
        .collect();
    assert_eq!(lengths.len(), 2, "{svg}");
    assert_eq!(lengths[0], lengths[1], "{svg}");
}

/// Kurikaeshi Moji Shori: a `<textPath>` cannot draw a character in another character's width, so
/// type on a path whose threaded frame starts with a repeat mark drawn as the character it repeats
/// is outlined, with a warning; with the setting off it stays live text.
#[test]
fn a_repeat_mark_starting_a_frame_on_a_path_is_outlined() {
    use vectorcraft_text::test_fonts::{RARE_REPEAT_MARK_FAMILY, rare_repeat_mark_font};
    vectorcraft_text::FontDb::global().add_font(rare_repeat_mark_font().unwrap());
    let st = CharStyle { font_family: RARE_REPEAT_MARK_FAMILY.into(), ..style(12.0) };
    let mut line = BezPath::new();
    line.move_to((20.0, 40.0));
    line.line_to((280.0, 40.0));
    for on in [false, true] {
        let mut t = TextObject::point(Point::ZERO, "々龘々", st.clone());
        t.kind = TextKind::OnPath { path: PathData::from_bezpath(&line), start: 0.0, end: None };
        t.para.kurikaeshi_moji_shori = on;
        t.first_para_continues = true;
        t.first_para_lead = Some('龘');
        let mut d = Document::new(300.0, 200.0);
        let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
        let l = d.layers[0].id;
        d.insert(Some(l), 0, n).unwrap();
        let out = vectorcraft_svg::export_full(&d, &ExportOptions::default(), None);
        let warned = out.warnings.iter().any(|w| w.contains("repeat mark"));
        if on {
            assert!(!out.svg.contains("<textPath") && warned, "{:?}: {}", out.warnings, out.svg);
        } else {
            assert!(out.svg.contains(">々龘々<") && !warned, "{:?}: {}", out.warnings, out.svg);
        }
    }
}