GitCorner

Code

ossartcraftwordcraft🌐
6ef13b2cc8a6
//! Right-to-left and bidirectional layout: Persian paragraphs, mixed Persian/Latin lines,
//! numbers and punctuation, caret and selection geometry.

use crate::para::{bidi_levels, visual_order};
#[allow(unused_imports)]
use crate::*;
use wordcraft_doc::props::{Align, ParaProps};
use wordcraft_doc::{Pos, StoryRef};

fn lay(doc: &Document) -> DocLayout {
    let mut c = LayoutCache::new();
    layout(doc, &mut c, &LayoutOptions::default())
}

/// A document of `paras`, each right to left when its flag is set.
fn doc_of(paras: &[(&str, bool)]) -> Document {
    let text: Vec<&str> = paras.iter().map(|p| p.0).collect();
    let mut d = Document::from_text(&text.join("\n"));
    for (i, (_, rtl)) in paras.iter().enumerate() {
        if *rtl {
            let at = Pos::body(i, 0);
            d.format_paragraphs(&at, &at, &|p: &mut ParaProps| p.bidi = Some(true)).unwrap();
        }
    }
    d
}

/// The first line of body paragraph `i` with its x offset on the page.
fn first_line(l: &DocLayout, i: u32) -> (&ParaLayout, f32) {
    for p in &l.pages {
        for it in &p.items {
            if let Placed::Lines { story: StoryRef::Body, path, para, x, .. } = it
                && path.0 == vec![i]
            {
                return (para, *x);
            }
        }
    }
    panic!("paragraph {i} not laid out");
}

/// Page x of the caret at `off` in paragraph `i`.
fn caret_x(l: &DocLayout, i: usize, off: usize) -> f32 {
    l.caret(&Pos::body(i, off)).unwrap().x
}

/// Left edge (page x) of the cluster starting at byte `off` of paragraph `i`'s first line.
fn cluster_x(l: &DocLayout, i: u32, off: usize) -> f32 {
    let (pl, x) = first_line(l, i);
    let line = &pl.lines[0];
    let k = (line.c0..line.c1).find(|&k| pl.clusters[k].start == off).unwrap_or_else(|| panic!("no cluster at {off}"));
    x + line.cl_left(k).unwrap()
}

/// Byte offset of the `n`th char of `s`.
fn at(s: &str, n: usize) -> usize {
    s.char_indices().nth(n).map(|(i, _)| i).unwrap_or(s.len())
}

#[test]
fn visual_order_follows_uax9_rule_l2() {
    crate::test_fonts();
    // The example from UAX #9 / unicode-bidi: levels 0 0 0 1 1 1 2 2.
    assert_eq!(visual_order(&[0, 0, 0, 1, 1, 1, 2, 2]), vec![0, 1, 2, 6, 7, 5, 4, 3]);
    assert_eq!(visual_order(&[1, 1, 1]), vec![2, 1, 0]);
    assert_eq!(visual_order(&[0, 0]), vec![0, 1]);
    assert_eq!(visual_order(&[]), Vec::<usize>::new());
    // Hostile levels never panic.
    assert_eq!(visual_order(&[125, 0, 125]).len(), 3);
}

#[test]
fn levels_for_persian_with_latin_and_numbers() {
    crate::test_fonts();
    assert!(bidi_levels("plain English", false).is_empty(), "pure LTR paragraphs skip the algorithm");
    let s = "سلام Word ۱۴۰۳";
    let lv = bidi_levels(s, true);
    assert_eq!(lv.len(), s.len());
    assert_eq!(lv[0] % 2, 1, "Persian letters are right to left");
    assert_eq!(lv[at(s, 5)], 2, "Latin inside a right-to-left paragraph is embedded at level 2");
    assert_eq!(lv[at(s, 10)], 2, "Persian digits are numbers: left to right inside right-to-left text");
    // In a left-to-right paragraph Persian is embedded at level 1.
    let lv = bidi_levels("Hello سلام", false);
    assert_eq!(lv[0], 0);
    assert_eq!(lv[at("Hello سلام", 6)], 1);
}

#[test]
fn rtl_paragraph_is_right_aligned_and_reads_right_to_left() {
    crate::test_fonts();
    let s = "سلام دنیا";
    let d = doc_of(&[(s, true)]);
    let l = lay(&d);
    let right = 612.0 - 72.0;
    // The caret at the start of a right-to-left paragraph is at the right margin.
    assert!((caret_x(&l, 0, 0) - right).abs() < 0.5, "start caret at {}", caret_x(&l, 0, 0));
    // Each next character sits further left.
    let xs: Vec<f32> = (0..s.chars().count()).map(|n| caret_x(&l, 0, at(s, n))).collect();
    assert!(xs.windows(2).all(|w| w[1] < w[0] + 0.01), "caret moves leftwards: {xs:?}");
    assert!(caret_x(&l, 0, s.len()) < xs[xs.len() - 1]);
    // Glyph boxes: the first letter is the rightmost.
    assert!(cluster_x(&l, 0, 0) > cluster_x(&l, 0, at(s, 5)));
}

#[test]
fn ltr_paragraph_with_embedded_persian_word() {
    crate::test_fonts();
    let s = "Hello سلام world";
    let d = doc_of(&[(s, false)]);
    let l = lay(&d);
    let (h, sin, mim, w) = (cluster_x(&l, 0, 0), cluster_x(&l, 0, at(s, 6)), cluster_x(&l, 0, at(s, 9)), cluster_x(&l, 0, at(s, 11)));
    assert!((h - 72.0).abs() < 0.5, "left-to-right paragraphs still start at the left margin");
    assert!(h < mim && mim < sin && sin < w, "the Persian word reads right to left between the English ones: {h} {mim} {sin} {w}");
}

#[test]
fn numbers_and_latin_inside_persian_keep_their_order() {
    crate::test_fonts();
    // "Price 250 toman" in Persian, with a Latin product name and Persian punctuation.
    let s = "قیمت 250 تومان برای Word، امسال ۱۴۰۳.";
    let d = doc_of(&[(s, true)]);
    let l = lay(&d);
    let x = |n: usize| cluster_x(&l, 0, at(s, n));
    // Digits run left to right.
    assert!(x(5) < x(6) && x(6) < x(7), "250 reads left to right: {} {} {}", x(5), x(6), x(7));
    assert!(x(32) < x(33) && x(33) < x(34) && x(34) < x(35), "۱۴۰۳ reads left to right");
    // "Word" reads left to right, and the words around it right to left.
    assert!(x(20) < x(21) && x(21) < x(22) && x(22) < x(23), "Word reads left to right");
    assert!(x(0) > x(9), "قیمت comes before تومان, so it is further right");
    assert!(x(9) > x(20), "تومان is right of Word");
    // The final full stop is at the paragraph's end: the far left.
    let dot = x(s.chars().count() - 1);
    for n in 0..s.chars().count() - 1 {
        assert!(dot <= x(n) + 0.01, "the full stop is leftmost (char {n})");
    }
}

#[test]
fn caret_and_hit_agree_in_bidi_text() {
    crate::test_fonts();
    for (s, rtl) in [("سلام world ۱۲۳", true), ("Hello سلام دنیا end", false), ("کتاب «WordCraft» (نسخه ۲)", true)] {
        let d = doc_of(&[(s, rtl)]);
        let l = lay(&d);
        let (pl, _) = first_line(&l, 0);
        let offs = pl.line_offsets(0);
        let mut seen: Vec<(usize, f32)> = Vec::new();
        for off in offs {
            let c = l.caret(&Pos::body(0, off)).unwrap();
            let back = l.hit(c.page, c.x, c.top + c.height / 2.0, StoryRef::Body).unwrap();
            // A click on a caret position finds an offset drawn at the same place.
            let bx = l.caret(&back).unwrap().x;
            assert!((bx - c.x).abs() < 0.01, "{s:?}: offset {off} at {} came back as {} at {bx}", c.x, back.off);
            seen.push((off, c.x));
        }
        assert!(seen.len() > 5);
    }
}

#[test]
fn visual_steps_cover_the_line_in_order() {
    crate::test_fonts();
    let s = "سلام world";
    let d = doc_of(&[(s, true)]);
    let l = lay(&d);
    // From the logical start (right edge), stepping left visits every position once.
    let mut pos = Pos::body(0, 0);
    let mut xs = vec![l.caret(&pos).unwrap().x];
    for _ in 0..40 {
        match l.visual_step(&pos, true, 0).unwrap() {
            hit::VisualStep::Moved(p) => {
                pos = p;
                xs.push(l.caret(&pos).unwrap().x);
            }
            hit::VisualStep::Edge { rtl, .. } => {
                assert!(rtl);
                break;
            }
        }
    }
    assert!(xs.windows(2).all(|w| w[1] < w[0]), "each step moves left: {xs:?}");
    // Every distinct caret position is visited (a lam-alef ligature, لا in سلام, puts two
    // offsets at one spot).
    let (pl, _) = first_line(&l, 0);
    let mut distinct: Vec<f32> = pl.line_offsets(0).iter().filter_map(|o| pl.x_of(0, *o)).collect();
    distinct.sort_by(f32::total_cmp);
    distinct.dedup_by(|a, b| (*a - *b).abs() < 0.01);
    assert_eq!(xs.len(), distinct.len(), "every caret position on the line is visited");
}

#[test]
fn selection_of_mixed_text_can_be_two_pieces() {
    crate::test_fonts();
    // Logical range "lo wo" of "Hello سلام world"? Select across the Persian word's start:
    // "lo سل" is two visual pieces (the end of Hello, and the right part of سلام).
    let s = "Hello سلام world";
    let d = doc_of(&[(s, false)]);
    let l = lay(&d);
    let rects = l.selection_rects(&d, &Pos::body(0, at(s, 3)), &Pos::body(0, at(s, 8)), 0);
    assert_eq!(rects.len(), 2, "{rects:?}");
    // A whole right-to-left word is one piece.
    let rects = l.selection_rects(&d, &Pos::body(0, at(s, 6)), &Pos::body(0, at(s, 10)), 0);
    assert_eq!(rects.len(), 1, "{rects:?}");
}

#[test]
fn rtl_indents_and_alignment_are_measured_from_the_right() {
    crate::test_fonts();
    let s = "متن فارسی";
    let mut d = doc_of(&[(s, true), (s, true), (s, true)]);
    let p0 = Pos::body(0, 0);
    d.format_paragraphs(&p0, &p0, &|p: &mut ParaProps| p.indent_left = Some(36.0)).unwrap();
    let p1 = Pos::body(1, 0);
    // `Align::Right` is the end edge: the left in a right-to-left paragraph.
    d.format_paragraphs(&p1, &p1, &|p: &mut ParaProps| p.align = Some(Align::Right)).unwrap();
    let p2 = Pos::body(2, 0);
    d.format_paragraphs(&p2, &p2, &|p: &mut ParaProps| p.align = Some(Align::Center)).unwrap();
    let l = lay(&d);
    assert!((caret_x(&l, 0, 0) - (540.0 - 36.0)).abs() < 0.5, "start indent on the right: {}", caret_x(&l, 0, 0));
    assert!((caret_x(&l, 1, s.len()) - 72.0).abs() < 0.5, "end-aligned text ends at the left margin: {}", caret_x(&l, 1, s.len()));
    let mid = (caret_x(&l, 2, 0) + caret_x(&l, 2, s.len())) / 2.0;
    assert!((mid - 306.0).abs() < 1.0, "centred: {mid}");
}

#[test]
fn rtl_list_label_sits_on_the_right() {
    crate::test_fonts();
    let mut d = doc_of(&[("مورد اول", true), ("مورد دوم", true)]);
    let id = Arc::make_mut(&mut d.numbering).add_list(wordcraft_doc::ListKind::Numbered);
    d.format_paragraphs(&Pos::body(0, 0), &Pos::body(1, 0), &|p: &mut ParaProps| {
        p.numbering = Some(wordcraft_doc::props::NumRef { num: id, level: 0 })
    })
    .unwrap();
    let l = lay(&d);
    let (pl, x) = first_line(&l, 0);
    let lab = pl.label.as_ref().expect("numbered");
    let text_right = caret_x(&l, 0, 0);
    assert!(x + lab.x >= text_right - 0.5, "label {} is right of the text start {text_right}", x + lab.x);
    assert!(x + lab.x + lab.width <= 540.5);
    // "1." reads right to left: the full stop is drawn left of the number.
    let st = &pl.styles[lab.style as usize];
    let (one, dot) = (st.face.glyph_for('1'), st.face.glyph_for('.'));
    let gx = |gid: u32| lab.glyphs.iter().find(|g| g.gid == gid).map(|g| g.dx).unwrap();
    assert!(gx(dot) < gx(one), "label shows .1");
}

#[test]
fn rtl_empty_and_trailing_lines_mirror() {
    crate::test_fonts();
    let d = doc_of(&[("", true)]);
    let l = lay(&d);
    assert!((caret_x(&l, 0, 0) - 540.0).abs() < 0.5, "empty right-to-left paragraph: caret at the right margin");
    // A paragraph ending in a line break gets an empty last line, also on the right.
    let mut d = doc_of(&[("سطر", true)]);
    let end = "سطر".len();
    d.para_mut(StoryRef::Body, &wordcraft_doc::Path::top(0)).unwrap().insert_text(end, "\n", &Default::default()).unwrap();
    let l = lay(&d);
    assert!((caret_x(&l, 0, end + 1) - 540.0).abs() < 0.5, "{}", caret_x(&l, 0, end + 1));
}

#[test]
fn bidi_display_draws_every_glyph_inside_the_line() {
    crate::test_fonts();
    let s = "این یک متن آزمایشی با English و عدد ۱۲۳ است. ".repeat(12);
    let d = doc_of(&[(&s, true)]);
    let l = lay(&d);
    let items = display::page_display(&d, &l.pages[0], &Default::default());
    let mut n = 0;
    for it in &items {
        if let display::Draw::Glyphs { glyphs, text, ranges, .. } = it {
            for (k, (_, x, _)) in glyphs.iter().enumerate() {
                // A wrapped line's trailing spaces hang past its end edge: the left margin here.
                if ranges.get(k).and_then(|r| text.get(r.clone())).is_some_and(|t| t.trim().is_empty()) {
                    continue;
                }
                assert!(*x >= 72.0 - 1.0 && *x <= 540.0 + 1.0, "glyph at {x} outside the text column");
                n += 1;
            }
        }
    }
    assert!(n > 100);
    // Lines wrap and every line but the last is filled to the right margin.
    let (pl, x) = first_line(&l, 0);
    assert!(pl.lines.len() > 3);
    assert!((x + pl.lines[0].vis.iter().map(|v| v.x + v.w).fold(0.0f32, f32::max) - 540.0).abs() < 1.0);
}

#[test]
fn hostile_bidi_text_never_panics() {
    crate::test_fonts();
    // Explicit embeddings, isolates, unmatched pops, marks with no base, lone surrogates' stand-ins.
    let junk = [
        "\u{202B}\u{202B}\u{202C}\u{202C}\u{202C}abc\u{2067}سلام\u{2069}\u{2069}",
        "\u{064E}\u{064E}\u{200C}\u{200D}",
        "\u{200F}\u{200E}\t\u{200F}\n\u{200E}",
        "(((((((((سلام]]]]]]]",
        &"\u{202E}".repeat(200),
    ];
    for s in junk {
        for rtl in [false, true] {
            let d = doc_of(&[(s, rtl)]);
            let l = lay(&d);
            let _ = display::page_display(&d, &l.pages[0], &Default::default());
            for off in 0..=s.len() {
                if s.is_char_boundary(off) {
                    let _ = l.caret(&Pos::body(0, off));
                    let _ = l.visual_step(&Pos::body(0, off), true, 0);
                    let _ = l.visual_step(&Pos::body(0, off), false, 0);
                }
            }
            let _ = l.selection_rects(&d, &Pos::body(0, 0), &Pos::body(0, s.len()), 0);
        }
    }
}

/// A face that has Arabic letters (installed system fonts), or `None` to skip.
fn arabic_face() -> Option<wordcraft_fonts::FaceRef> {
    crate::test_fonts();
    wordcraft_fonts::FontDb::global().fallback_for('ب', 0).map(|f| wordcraft_fonts::FaceRef::of(&f)).filter(|f| f.covers('پ'))
}

/// The glyph ids of each cluster, in order. A font may draw a letter as several glyphs (Noto
/// Naskh Arabic draws ب as its dotless form plus the dot), so letters are compared by cluster.
fn cluster_glyphs(glyphs: &[wordcraft_fonts::ShapedGlyph]) -> Vec<Vec<u32>> {
    let mut out: Vec<(usize, Vec<u32>)> = Vec::new();
    for g in glyphs {
        match out.last_mut() {
            Some((c, ids)) if *c == g.cluster => ids.push(g.gid),
            _ => out.push((g.cluster, vec![g.gid])),
        }
    }
    out.into_iter().map(|(_, ids)| ids).collect()
}

#[test]
fn persian_letters_join_and_brackets_mirror() {
    crate::test_fonts();
    let Some(face) = arabic_face() else {
        eprintln!("skipped: no font with Persian letters installed");
        return;
    };
    let isolated = cluster_glyphs(&wordcraft_fonts::shape_run(&face, "ب", &[], |c| c, true));
    assert_eq!(isolated.len(), 1, "{isolated:?}");
    let shaped = wordcraft_fonts::shape_run(&face, "ببب", &[], |c| c, true);
    let joined = cluster_glyphs(&shaped);
    assert_eq!(joined.len(), 3, "one cluster per letter: {joined:?}");
    assert!(shaped.iter().all(|g| g.gid != 0), "the font has the letters");
    // Initial, medial and final forms differ from the isolated letter.
    assert!(joined.iter().any(|g| *g != isolated[0]), "cursive joining picks contextual forms: {joined:?} {isolated:?}");
    // Clusters come back in logical order.
    assert!(shaped.windows(2).all(|w| w[0].cluster <= w[1].cluster));
    // ZWNJ (نیم‌فاصله) breaks the join: "می‌خواهم" keeps می separate.
    let with_zwnj = cluster_glyphs(&wordcraft_fonts::shape_run(&face, "ب\u{200C}ب", &[], |c| c, true));
    assert_eq!(with_zwnj.first(), isolated.first(), "a letter before ZWNJ takes its isolated form");
    // Farsi Yeh (ی, U+06CC) joins like any letter (#824).
    let mi = wordcraft_fonts::shape_run(&face, "می", &[], |c| c, true);
    let mi_cl = cluster_glyphs(&mi);
    assert_eq!(mi_cl.len(), 2, "meem + Farsi Yeh: {mi_cl:?}");
    assert!(mi.iter().all(|g| g.gid != 0), "the font has Farsi Yeh");
    let meem_alone = cluster_glyphs(&wordcraft_fonts::shape_run(&face, "م", &[], |c| c, true));
    assert_ne!(mi_cl[0], meem_alone[0], "meem takes its initial form before Yeh: {mi_cl:?} vs {meem_alone:?}");
    // In right-to-left text, "(" is drawn with the mirrored glyph.
    let open = wordcraft_fonts::shape_run(&face, "(", &[], |c| c, true);
    let close = wordcraft_fonts::shape_run(&face, ")", &[], |c| c, false);
    if let (Some(o), Some(c)) = (open.first(), close.first())
        && c.gid != 0
    {
        assert_eq!(o.gid, c.gid, "bidi mirroring");
    }
}

#[test]
fn persian_text_in_a_latin_font_stays_in_one_fallback_face() {
    crate::test_fonts();
    if arabic_face().is_none() {
        eprintln!("skipped: no font with Persian letters installed");
        return;
    }
    // Calibri (substituted) has no Persian: the whole word must come from one fallback face so
    // its letters join, including the Persian-only letters and a diacritic.
    let s = "پژوهشگرِ گرامی";
    let d = doc_of(&[(s, true)]);
    let l = lay(&d);
    let (pl, _) = first_line(&l, 0);
    let word: Vec<u32> = pl.clusters.iter().filter(|c| c.end <= at(s, 8)).map(|c| pl.styles[c.style as usize].face.id()).collect();
    assert!(word.windows(2).all(|w| w[0] == w[1]), "one face for the word: {word:?}");
    // No .notdef glyphs.
    assert!(pl.glyphs.iter().all(|g| g.gid != 0));
}

#[test]
fn glyph_runs_carry_their_text_for_pdf_export() {
    crate::test_fonts();
    let s = "سلام WordCraft می‌خواهم";
    let d = doc_of(&[(s, true)]);
    let l = lay(&d);
    let lam_alef_ligature = arabic_face().is_some_and(|f| {
        let g = wordcraft_fonts::shape_run(&f, "لا", &[], |c| c, true);
        !g.is_empty() && g.iter().all(|g| g.cluster == 0)
    });
    let mut all = String::new();
    for it in display::page_display(&d, &l.pages[0], &Default::default()) {
        if let display::Draw::Glyphs { glyphs, text, ranges, .. } = it {
            assert_eq!(ranges.len(), glyphs.len(), "one text range per glyph");
            for r in &ranges {
                assert!(text.get(r.clone()).is_some_and(|t| !t.is_empty()), "{r:?} in {text:?}");
            }
            // A ligature glyph (لا) shows both its letters (when the Persian font draws one: some,
            // such as Noto Naskh Arabic, draw the lam and the alef as glyphs of their own).
            for r in ranges.iter().filter(|_| lam_alef_ligature) {
                if text.get(r.clone()) == Some("ل") {
                    panic!("the lam of لا is drawn as one glyph with the alef: {text:?} {ranges:?}");
                }
            }
            all.push_str(&text);
        }
    }
    assert_eq!(all.replace(' ', ""), s.replace(' ', ""), "every character is accounted for, in logical order");
}

#[test]
fn character_border_boxes_a_whole_rtl_word() {
    crate::test_fonts();
    let s = "کتاب خوب";
    let mut d = doc_of(&[(s, true)]);
    let b = wordcraft_doc::props::Border { space: 0.0, ..wordcraft_doc::props::Border::single(0.5) };
    d.format_range(&Pos::body(0, 0), &Pos::body(0, at(s, 4)), &|c| c.border = Some(b)).unwrap();
    let l = lay(&d);
    let (pl, x) = first_line(&l, 0);
    let line = &pl.lines[0];
    let word: Vec<usize> = (line.c0..line.c1).filter(|&k| pl.clusters[k].end <= at(s, 4)).collect();
    let lo = word.iter().filter_map(|&k| line.cl_left(k)).fold(f32::MAX, f32::min) + x;
    let hi = word.iter().filter_map(|&k| line.cl_right(k)).fold(f32::MIN, f32::max) + x;
    // The box's top edge spans the whole word, which reads right to left.
    let top = display::page_display(&d, &l.pages[0], &Default::default())
        .into_iter()
        .filter_map(|it| match it {
            display::Draw::Line { x0, y0, x1, y1, .. } if (y0 - y1).abs() < 0.01 && (x1 - x0).abs() > 1.0 => Some((x0.min(x1), x0.max(x1))),
            _ => None,
        })
        .next()
        .expect("a border");
    assert!((top.0 - lo).abs() < 0.5 && (top.1 - hi).abs() < 0.5, "box {top:?} vs word {lo}..{hi}");
}

#[test]
fn inline_picture_in_rtl_text_sits_in_its_place() {
    crate::test_fonts();
    // "متن [picture] بیشتر": in a right-to-left paragraph the picture is drawn where its cluster
    // is on the line (left of the first word), not at its logical x.
    let s = "متن  بیشتر";
    let mut d = doc_of(&[(s, true)]);
    let off = at(s, 4);
    let pic = wordcraft_doc::InlineObject::Image {
        media: "m".into(),
        w: 30.0,
        h: 20.0,
        alt: String::new(),
        float: Default::default(),
        crop: [0.0; 4],
        fx: Default::default(),
        ole: None,
    };
    d.para_mut(StoryRef::Body, &wordcraft_doc::Path::top(0)).unwrap().insert_object(off, pic, &Default::default()).unwrap();
    let l = lay(&d);
    let (pl, x) = first_line(&l, 0);
    let line = &pl.lines[0];
    let k = (line.c0..line.c1).find(|&k| matches!(pl.clusters[k].kind, para::ClKind::Object(_))).unwrap();
    let (a, b) = (x + line.cl_left(k).unwrap(), x + line.cl_right(k).unwrap());
    let drawn: Vec<Rect> = display::page_display(&d, &l.pages[0], &Default::default())
        .into_iter()
        .filter_map(|it| if let display::Draw::Image { rect, .. } = it { Some(rect) } else { None })
        .collect();
    assert_eq!(drawn.len(), 1);
    assert!((drawn[0].x - a).abs() < 0.5 && (drawn[0].x + drawn[0].w - b).abs() < 0.5, "picture {:?} vs its slot {a}..{b}", drawn[0]);
    // The object placed for selection and dragging is at the same spot.
    let placed: Vec<Rect> = l.pages[0].items.iter().filter_map(|it| if let Placed::Object { rect, .. } = it { Some(*rect) } else { None }).collect();
    assert!(placed.iter().any(|r| (r.x - a).abs() < 0.5), "placed {placed:?} vs {a}");
    assert!(a < 540.0 - 20.0, "after the first word, so left of the right margin");
}

/// A right-to-left table (`w:bidiVisual`, #362): the first logical column stands at the right
/// edge, the table is start-aligned against the right margin, and each cell's start (left)
/// margin and border are drawn on its right. Clicks find cells by where they are drawn.
#[test]
fn rtl_table_lays_its_columns_out_right_to_left() {
    crate::test_fonts();
    use wordcraft_doc::props::{Border, Borders};
    use wordcraft_doc::{Path, Table};
    let mut d = Document::from_text("before\nafter");
    let mut t = Table::new(1, 3, 300.0);
    t.props.bidi_visual = true;
    // Start margin 10pt, end margin 2pt.
    t.props.cell_margins = Some([0.0, 10.0, 0.0, 2.0]);
    for (i, c) in t.rows[0].cells.iter_mut().enumerate() {
        c.blocks = vec![wordcraft_doc::para_block(wordcraft_doc::Paragraph::with_text(&format!("c{i}"), Default::default()))];
    }
    // A thick start border on the first cell.
    t.rows[0].cells[0].props.borders = Some(Borders { left: Some(Border::single(3.0)), ..Default::default() });
    d.insert_block(StoryRef::Body, &Path::top(1), Block::Table(t)).unwrap();
    let l = lay(&d);
    let items = &l.pages[0].items;
    let cell =
        |k: usize| items.iter().find_map(|i| if let Placed::Cell { rect, cell, .. } = i { (*cell == k).then_some(*rect) } else { None }).unwrap();
    let (c0, c1, c2) = (cell(0), cell(1), cell(2));
    // Start-aligned against the right margin (72 + 468), first column rightmost.
    let right = 72.0 + 468.0;
    assert!((c0.right() - right).abs() < 2.0, "{c0:?}");
    assert!(c0.x > c1.x && c1.x > c2.x, "{c0:?} {c1:?} {c2:?}");
    assert!((c1.right() - c0.x).abs() < 0.01 && (c2.right() - c1.x).abs() < 0.01);
    assert!((c2.x - (right - 300.0)).abs() < 2.0, "{c2:?}");
    // The first cell's text sits its end margin (2pt) in from the cell's left edge.
    let text_x =
        items.iter().find_map(|i| if let Placed::Lines { path, x, .. } = i { (path.0 == [1, 0, 0, 0]).then_some(*x) } else { None }).unwrap();
    assert!((text_x - (c0.x + 2.0)).abs() < 0.5, "text at {text_x}, cell {c0:?}");
    // The thick start border is drawn on the cell's right edge.
    let thick: Vec<f32> = items
        .iter()
        .filter_map(|i| match i {
            Placed::Rule { x0, x1, border, .. } if (x0 - x1).abs() < 0.01 && (border.width - 3.0).abs() < 0.01 => Some(*x0),
            _ => None,
        })
        .collect();
    assert_eq!(thick.len(), 1, "{thick:?}");
    assert!((thick[0] - c0.right()).abs() < 0.01, "{thick:?} vs {c0:?}");
    // A click in the rightmost cell lands in the first logical cell.
    let hit = l.cell_at(0, c0.x + c0.w / 2.0, c0.y + 2.0).unwrap();
    assert_eq!((hit.2, hit.3), (0, 0), "{hit:?}");
}

/// A right-to-left section (`w:sectPr/w:bidi`, #362): newspaper columns run right to left, the
/// first column at the right, and the line between them stays midway.
#[test]
fn rtl_section_puts_its_first_column_on_the_right() {
    crate::test_fonts();
    use wordcraft_doc::section::Columns;
    let text = "Columns flow from the right edge in a right-to-left section. ".repeat(150);
    let mut d = Document::from_text(&text);
    d.last_section.rtl = true;
    d.last_section.columns = Columns { count: 2, space: 36.0, separator: true, widths: Vec::new() };
    let l = lay(&d);
    let page = &l.pages[0];
    let xs: Vec<f32> = page.items.iter().filter_map(|i| if let Placed::Lines { x, .. } = i { Some(*x) } else { None }).collect();
    // Columns are 216pt wide: the first at 72 + 252, the second at the left margin.
    assert!((xs[0] - (72.0 + 252.0)).abs() < 1.0, "first column on the right: {xs:?}");
    assert!(xs.iter().any(|x| (x - 72.0).abs() < 1.0), "second column on the left: {xs:?}");
    let caret = l.caret(&Pos::body(0, 0)).unwrap();
    assert!(caret.x > 72.0 + 252.0 - 1.0, "{caret:?}");
    let sep: Vec<f32> =
        page.items.iter().filter_map(|i| if let Placed::Rule { x0, x1, .. } = i { ((x0 - x1).abs() < 0.01).then_some(*x0) } else { None }).collect();
    assert_eq!(sep, vec![72.0 + 234.0], "line between the columns");
}

#[test]
fn list_label_moves_with_its_text_when_the_paragraph_is_aligned() {
    crate::test_fonts();
    // Issue #1159: centring or end-aligning a list item moved the text and left the bullet at the
    // margin. In Word the label stays attached to the start of the text, in both directions.
    for (rtl, text) in [(false, "alpha beta"), (true, "مورد اول")] {
        let mut d = doc_of(&[(text, rtl), (text, rtl), (text, rtl), (text, rtl)]);
        let id = Arc::make_mut(&mut d.numbering).add_list(wordcraft_doc::ListKind::Bullet);
        d.format_paragraphs(&Pos::body(0, 0), &Pos::body(3, 1), &|p: &mut ParaProps| {
            p.numbering = Some(wordcraft_doc::props::NumRef { num: id, level: 0 })
        })
        .unwrap();
        // Start edge (the default), centre, end edge, and a justified one.
        for (i, a) in [(1, Align::Center), (2, Align::Right), (3, Align::Justify)] {
            let at = Pos::body(i, 0);
            d.format_paragraphs(&at, &at, &|p: &mut ParaProps| p.align = Some(a)).unwrap();
        }
        let l = lay(&d);
        // How far the label's near edge is from where the text starts.
        let gap = |i: u32| {
            let (pl, x) = first_line(&l, i);
            let lab = pl.label.as_ref().unwrap_or_else(|| panic!("no label: rtl={rtl} i={i} lines={}", pl.lines.len()));
            let text_start = caret_x(&l, i as usize, 0);
            if rtl { x + lab.x - text_start } else { text_start - (x + lab.x + lab.width) }
        };
        let g0 = gap(0);
        assert!(g0 > 0.0 && g0 < 40.0, "rtl={rtl}: label then a tab, then text: {g0}");
        for i in 1..4 {
            assert!((gap(i) - g0).abs() < 0.5, "rtl={rtl}: paragraph {i}: label stays {g0} from the text, not {}", gap(i));
        }
        // The text really moved: further from the start edge when centred, furthest at the end.
        let from_start = |i: u32| if rtl { 540.0 - caret_x(&l, i as usize, 0) } else { caret_x(&l, i as usize, 0) - 72.0 };
        assert!(from_start(1) > from_start(0) + 5.0 && from_start(2) > from_start(1) + 5.0, "rtl={rtl}");
        assert!((from_start(3) - from_start(0)).abs() < 0.5, "rtl={rtl}: justified text starts where the left-aligned one does");
        // The label is inside the text area.
        for i in 0..4 {
            let (pl, x) = first_line(&l, i);
            let lab = pl.label.as_ref().unwrap();
            assert!(x + lab.x >= 71.5 && x + lab.x + lab.width <= 540.5, "rtl={rtl}: paragraph {i}");
        }
    }
}

/// A numbered label's brackets are mirrored in a right-to-left paragraph (UAX #9 L4), so a
/// `(%1)` label reads `(1)` and not `)1(`.
#[test]
fn rtl_list_label_mirrors_its_brackets() {
    let mut d = doc_of(&[("סעיף", true)]);
    let lvl = wordcraft_doc::numbering::Level { text: "(%1)".into(), ..Default::default() };
    let num = Arc::make_mut(&mut d.numbering).try_add_levels(None, vec![lvl]).unwrap();
    d.format_paragraphs(&Pos::body(0, 0), &Pos::body(0, 0), &|p: &mut ParaProps| p.numbering = Some(wordcraft_doc::props::NumRef { num, level: 0 }))
        .unwrap();
    let l = lay(&d);
    let (pl, _) = first_line(&l, 0);
    let lab = pl.label.as_ref().expect("numbered");
    assert_eq!(lab.text, "(1)");
    let st = &pl.styles[lab.style as usize];
    let (open, digit, close) = (st.face.glyph_for('('), st.face.glyph_for('1'), st.face.glyph_for(')'));
    let mut drawn: Vec<(f32, u32)> = lab.glyphs.iter().map(|g| (g.dx, g.gid)).collect();
    drawn.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
    let gids: Vec<u32> = drawn.iter().map(|(_, gid)| *gid).collect();
    assert_eq!(gids, vec![open, digit, close], "left to right, the label reads (1)");
}

#[test]
fn list_label_after_leading_page_break_uses_its_text_lines_alignment() {
    // #2964: the number moved to page 2, but kept the centring of the empty break line.
    // Adding the break must leave both the label and text at the same horizontal positions.
    for (rtl, heading) in [(false, "SUBJECT OF THE AGREEMENT"), (true, "عنوان القسم")] {
        for kind in [wordcraft_doc::ListKind::Numbered, wordcraft_doc::ListKind::Bullet] {
            for align in [Align::Left, Align::Center, Align::Right, Align::Justify] {
                for tab in [false, true] {
                    let text = if tab { format!("{heading}\tTail") } else { heading.into() };
                    let make = |page_break: bool| {
                        let contents = if page_break { format!("\u{000C}{text}") } else { text.clone() };
                        let mut d = doc_of(&[("Before", false), (&contents, rtl)]);
                        let id = Arc::make_mut(&mut d.numbering).add_list(kind);
                        for a in &mut Arc::make_mut(&mut d.numbering).abstracts {
                            let level = a.levels.first_mut().unwrap();
                            level.start = 2;
                            level.indent = 14.2;
                            level.hanging = 14.2;
                        }
                        let at = Pos::body(1, 0);
                        d.format_paragraphs(&at, &at, &|p| {
                            p.numbering = Some(wordcraft_doc::props::NumRef { num: id, level: 0 });
                            p.align = Some(align);
                        })
                        .unwrap();
                        lay(&d)
                    };
                    let without = make(false);
                    let with = make(true);
                    let (plain, plain_x) = first_line(&without, 1);
                    let (broken, broken_x) = first_line(&with, 1);
                    let plain_label = plain.label.as_ref().unwrap();
                    let broken_label = broken.label.as_ref().unwrap();
                    assert_eq!(broken.label_line(), 1);
                    assert_eq!(with.pages.len(), 2);
                    assert!(with.pages[1].items.iter().any(|item| matches!(item,
                        Placed::Lines { path, l0, l1, .. } if path.0 == vec![1] && *l0 <= 1 && *l1 > 1
                    )));
                    let label_delta = broken_x + broken_label.x - plain_x - plain_label.x;
                    assert!(label_delta.abs() < 0.01, "rtl={rtl}, kind={kind:?}, align={align:?}, tab={tab}: label moved {label_delta}");
                    let text_delta = caret_x(&with, 1, 1) - caret_x(&without, 1, 0);
                    assert!(text_delta.abs() < 0.01, "rtl={rtl}, kind={kind:?}, align={align:?}, tab={tab}: text moved {text_delta}");
                }
            }
        }
    }
}

/// The glyph ids of each letter of paragraph 0 as laid out (marks of runs left out have none).
fn letters(l: &DocLayout) -> Vec<Vec<u32>> {
    let (pl, _) = first_line(l, 0);
    pl.clusters.iter().filter(|c| c.g1 > c.g0).map(|c| pl.glyphs[c.g0 as usize..c.g1 as usize].iter().map(|g| g.gid).collect()).collect()
}

/// `text` as one right-to-left paragraph with `format` applied to each `(start, end)` byte range.
fn formatted(text: &str, splits: &[(usize, usize)], format: &dyn Fn(&mut wordcraft_doc::props::CharProps)) -> Document {
    let mut d = doc_of(&[(text, true)]);
    for &(start, end) in splits {
        d.format_range(&Pos::body(0, start), &Pos::body(0, end), format).unwrap();
    }
    d
}

const BEH: usize = 'ب'.len_utf8();

fn red(c: &mut wordcraft_doc::props::CharProps) {
    c.color = Some(wordcraft_doc::props::TextColor::Rgb(wordcraft_doc::props::Rgb(0xC0, 0, 0)));
}

/// The reporter's symptom: after a change of formatting inside a word, the letters on either side
/// took the forms of the end and the start of a word, which reads as a space in it (#4093).
#[test]
fn letters_join_across_a_change_of_formatting() {
    if arabic_face().is_none() {
        eprintln!("skipped: no font with Persian letters installed");
        return;
    }
    let word = "بببب";
    let whole = letters(&lay(&doc_of(&[(word, true)])));
    assert_eq!(whole.len(), 4, "one cluster per letter: {whole:?}");
    assert!(whole.first() != whole.get(1) && whole.get(1) != whole.last(), "initial, medial and final forms: {whole:?}");
    // A letter of its own at each place, then the two middle letters, then every other letter.
    let pieces: [&[(usize, usize)]; 6] =
        [&[(0, BEH)], &[(BEH, 2 * BEH)], &[(2 * BEH, 3 * BEH)], &[(3 * BEH, 4 * BEH)], &[(BEH, 3 * BEH)], &[(0, BEH), (2 * BEH, 3 * BEH)]];
    for runs in pieces {
        let d = formatted(word, runs, &red);
        assert!(d.para_at(&Pos::body(0, 0)).unwrap().runs.len() > 1, "{runs:?} split the run");
        assert_eq!(letters(&lay(&d)), whole, "split at {runs:?}");
    }
}

/// A letter joins what is on the page: with a tracked deletion shown, the deleted letter; with it
/// left out, the letter past it. The same goes for hidden text.
#[test]
fn letters_join_past_text_left_out_of_the_layout() {
    if arabic_face().is_none() {
        eprintln!("skipped: no font with Persian letters installed");
        return;
    }
    let (three, two) = (letters(&lay(&doc_of(&[("ببب", true)]))), letters(&lay(&doc_of(&[("بب", true)]))));
    assert_eq!((three.len(), two.len()), (3, 2));
    let middle = [(BEH, 2 * BEH)];

    let deleted = formatted("ببب", &middle, &|c| c.del = Some(0));
    assert_eq!(letters(&lay(&deleted)), three, "deletion shown: it joins both neighbours");
    let mut c = LayoutCache::new();
    let final_text = layout(&deleted, &mut c, &LayoutOptions { hide_deleted: true, ..LayoutOptions::default() });
    assert_eq!(letters(&final_text), two, "deletion left out: its neighbours join each other");

    let hidden = formatted("ببب", &middle, &|c| c.hidden = Some(true));
    assert_eq!(letters(&lay(&hidden)), two, "hidden text left out: its neighbours join each other");
    let mut c = LayoutCache::new();
    let shown = layout(&hidden, &mut c, &LayoutOptions { show_hidden: true, ..LayoutOptions::default() });
    assert_eq!(letters(&shown), three, "hidden text shown: it joins both neighbours");
}

/// A mark in a run of its own (a coloured vowel sign) doesn't stop the letters around it joining.
#[test]
fn letters_join_past_a_mark_in_a_run_of_its_own() {
    if arabic_face().is_none() {
        eprintln!("skipped: no font with Persian letters installed");
        return;
    }
    let word = "بَب";
    let whole = letters(&lay(&doc_of(&[(word, true)])));
    let split = letters(&lay(&formatted(word, &[(BEH, BEH + 'َ'.len_utf8())], &red)));
    assert_eq!(split.last(), whole.last(), "the letter after the mark stays joined");
    // Together with the mark, the letter before it has more glyphs, in the shaper's order.
    let (before, with_mark) = (split.first().unwrap(), whole.first().unwrap());
    assert!(before.iter().all(|g| with_mark.contains(g)), "and the letter before it: {before:?} {with_mark:?}");
}

/// What can't join still doesn't: a tab between the letters, in one run or at the edge of a run, a
/// Latin neighbour, and a letter beside a drop cap, which is drawn apart from the word.
#[test]
fn letters_stay_apart_where_nothing_joins_them() {
    if arabic_face().is_none() {
        eprintln!("skipped: no font with Persian letters installed");
        return;
    }
    let alone = letters(&lay(&doc_of(&[("ب", true)])));
    assert_eq!(alone.len(), 1);
    let beh = alone.first();
    let tabbed = "ب\tب";
    for d in [doc_of(&[(tabbed, true)]), formatted(tabbed, &[(0, BEH)], &red), formatted(tabbed, &[(BEH + 1, tabbed.len())], &red)] {
        let got = letters(&lay(&d));
        assert_eq!((got.first(), got.last()), (beh, beh), "across a tab: {got:?}");
    }
    let got = letters(&lay(&formatted("xب", &[(0, 1)], &red)));
    assert_eq!(got.last(), beh, "after a Latin letter: {got:?}");

    // The drop cap in the word's run, and formatted on its own (a run of its own).
    let two = letters(&lay(&doc_of(&[("بب", true)])));
    for mut d in [doc_of(&[("ببب", true)]), formatted("ببب", &[(0, BEH)], &red)] {
        let at = Pos::body(0, 0);
        d.format_paragraphs(&at, &at, &|p: &mut ParaProps| p.drop_cap = Some(2)).unwrap();
        let runs = d.para_at(&at).unwrap().runs.len();
        let got = letters(&lay(&d));
        assert_eq!(got.get(got.len().saturating_sub(2)..), two.get(..), "{runs} run(s): the letters after a drop cap start a word: {got:?} {two:?}");
    }
}

#[test]
fn the_shaper_takes_a_letters_form_from_the_text_around_it() {
    use wordcraft_fonts::{ShapeContext, shape_run_in};
    let Some(face) = arabic_face() else {
        eprintln!("skipped: no font with Persian letters installed");
        return;
    };
    let form = |around| cluster_glyphs(&shape_run_in(&face, "ب", &[], |c| c, true, around));
    let isolated = form(ShapeContext::default());
    let joined = cluster_glyphs(&wordcraft_fonts::shape_run(&face, "ببب", &[], |c| c, true));
    assert_eq!(form(ShapeContext { before: "", after: "ب" }).first(), joined.first(), "initial");
    assert_eq!(form(ShapeContext { before: "ب", after: "ب" }).first(), joined.get(1), "medial");
    assert_eq!(form(ShapeContext { before: "ب", after: "" }).first(), joined.get(2), "final");
    // A vowel sign is transparent, and ZWNJ and a Latin letter stop the join.
    assert_eq!(form(ShapeContext { before: "بَ", after: "" }).first(), joined.get(2), "past a mark");
    assert_eq!(form(ShapeContext { before: "ب\u{200C}", after: "" }), isolated, "after ZWNJ");
    assert_eq!(form(ShapeContext { before: "a", after: "b" }), isolated, "between Latin letters");
    // Only the letter's form depends on the context: nothing of it is drawn.
    assert_eq!(form(ShapeContext { before: "ببب", after: "ببب" }).len(), 1);
}

#[test]
fn scripts_that_join_their_letters_are_told_apart() {
    use wordcraft_fonts::joins_cursively;
    for c in ['ب', 'پ', 'ܐ', 'ᠠ', 'ߊ', '𞤀'] {
        assert!(joins_cursively(c), "{c}");
    }
    for c in ['a', 'é', '’', 'я', 'א', '中', ' ', '\t', '\u{FFFC}', '\u{200C}', 'َ'] {
        assert!(!joins_cursively(c), "{c:?}");
    }
}