DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge cjk-run-baseline: CJK: smaller runs align to the line's em box centre
aiongg9d717ea41731
diff --cc crates/compose/src/lib.rsindex 05c158184,56f21361d..0c36070f0--- a/crates/compose/src/lib.rs+++ b/crates/compose/src/lib.rs@@@ -2523,21 -2018,20 +2523,31 @@@ fn cjk_em_box(g: &Glyph) -> (f64, f64)let top = -height * g.ascent / (g.ascent + g.descent).max(1e-9);(top, top + height)}+/// The ideographic character face (ICF) box relative to the baseline: the em box of+/// [`cjk_em_box`] inset by the font's own ICF insets (`BASE` `icft`/`icfb`, else 5% of the em).+fn cjk_icf_box(g: &Glyph) -> (f64, f64) {+ let (top, bottom) = cjk_em_box(g);+ let (em_top, em_bottom) = g.face.em_box();+ let (icf_top, icf_bottom) = g.face.icf_box();+ let em = (em_top - em_bottom).max(1e-9);+ let height = bottom - top;+ let inset = |v: f64| if v.is_finite() { v.clamp(0.0, 0.5) } else { 0.05 };+ (top + inset((em_top - icf_top) / em) * height, bottom - inset((icf_bottom - em_bottom) / em) * height)+}+ /// Em box that character alignment lines up, relative to the baseline (y down) at the glyph's+ /// size: the font's `BASE` em box, else 0.88 em above and 0.12 em below the baseline. A font's+ /// ascender/descender proportions put a Latin em box's centre too low for this.+ fn alignment_em_box(g: &Glyph) -> (f64, f64) {+ let upem = g.face.units_per_em();- let (top, bottom) = g.face.base_em_box().unwrap_or((upem * 0.88, upem * -0.12));++ let (top, bottom) = g.face.declared_em_box().unwrap_or((upem * 0.88, upem * -0.12));+ let k = g.sy.abs();+ (-top * k, -bottom * k)+ }+ /// Upward shift that puts `g`'s em box (or ICF) edge or centre where the reference glyph's is.fn cjk_alignment_shift(g: &Glyph, reference: &Glyph) -> f64 {use designcraft_doc::cjk::CharacterAlignment as A;- let (t, b) = cjk_em_box(g);- let (rt, rb) = cjk_em_box(reference);+ let (t, b) = alignment_em_box(g);+ let (rt, rb) = alignment_em_box(reference);match g.character_alignment {A::Baseline => 0.0,A::EmTop => t - rt,diff --cc crates/compose/src/tests.rsindex edb89fe63,7f3630d54..64f958a9f--- a/crates/compose/src/tests.rs+++ b/crates/compose/src/tests.rs@@@ -3235,113 -1469,62 +3235,151 @@@ fn cjk_custom_kinsoku_disables_selectedassert_eq!(all_lines(&cs).len(), 1);}+fn japanese() -> ParaAttrs {+ ParaAttrs { composer: Some(Composer::Japanese), ..Default::default() }+}++#[test]+fn cjk_center_leading_measures_em_centers_across_different_sizes() {+ let lines_with = |para: ParaAttrs| {+ let (mut d, sid, _) = doc_with("A\nB", Rect::new(0.0, 0.0, 300.0, 200.0), para);+ d.story_mut(sid).unwrap().format_chars(0..3, |f| {+ f.over.leading = Some(designcraft_doc::Leading::Points(30.0));+ f.over.leading_model = Some(designcraft_doc::cjk::LeadingModel::Center);+ });+ d.story_mut(sid).unwrap().format_chars(0..1, |f| f.over.size = Some(24.0));+ let cs = compose_story(&d, sid, &ComposeOptions::default());+ all_lines(&cs).into_iter().cloned().collect::<Vec<_>>()+ };+ let lines = lines_with(japanese());+ assert_eq!(lines.len(), 2);+ let center = |l: &Line| {+ let g = &l.glyphs[0];+ let (a, b) = g.face.vertical_metrics();+ l.baseline + (b - a) / (2.0 * (a + b)) * g.face.units_per_em() * g.sy+ };+ assert!((center(&lines[1]) - center(&lines[0]) - 30.0).abs() < 1e-6);+ assert!((lines[1].baseline - lines[0].baseline - 30.0).abs() > 0.1);+ // The other composers set lines baseline to baseline.+ let lines = lines_with(ParaAttrs::default());+ assert!((lines[1].baseline - lines[0].baseline - 30.0).abs() < 1e-6);+}++#[test]+fn cjk_aki_below_sets_the_upper_lines_leading_below_it() {+ let lines_with = |para: ParaAttrs| {+ let (mut d, sid, _) = doc_with("A\nB", Rect::new(0.0, 0.0, 300.0, 200.0), para);+ let st = d.story_mut(sid).unwrap();+ st.format_chars(0..3, |f| f.over.leading_model = Some(designcraft_doc::cjk::LeadingModel::AkiBelow));+ st.format_chars(0..2, |f| {+ f.over.size = Some(24.0);+ f.over.leading = Some(designcraft_doc::Leading::Points(40.0));+ });+ st.format_chars(2..3, |f| {+ f.over.size = Some(12.0);+ f.over.leading = Some(designcraft_doc::Leading::Points(20.0));+ });+ let cs = compose_story(&d, sid, &ComposeOptions::default());+ all_lines(&cs).into_iter().cloned().collect::<Vec<_>>()+ };+ let lines = lines_with(japanese());+ assert_eq!(lines.len(), 2);+ // Aki Below measures from the em box tops: the lower line's sits the upper line's leading+ // below the upper line's.+ let em_top = |l: &Line| {+ let g = &l.glyphs[0];+ let (a, b) = g.face.vertical_metrics();+ l.baseline - a / (a + b) * g.face.units_per_em() * g.sy+ };+ let gap = em_top(&lines[1]) - em_top(&lines[0]);+ assert!((gap - 40.0).abs() < 1e-6, "{gap}");+ // The other composers advance by the lower line's leading, baseline to baseline.+ let lines = lines_with(ParaAttrs::default());+ assert!((lines[1].baseline - lines[0].baseline - 20.0).abs() < 1e-6);+}++#[test]+fn cjk_aki_below_measures_em_tops_and_aki_above_em_bottoms() {+ use designcraft_doc::cjk::LeadingModel;+ for (model, top) in [(LeadingModel::AkiBelow, true), (LeadingModel::AkiAbove, false)] {+ let (mut d, sid, _) = doc_with("A\nB", Rect::new(0.0, 0.0, 300.0, 200.0), japanese());+ d.story_mut(sid).unwrap().format_chars(0..3, |f| {+ f.over.leading = Some(designcraft_doc::Leading::Points(30.0));+ f.over.leading_model = Some(model);+ });+ d.story_mut(sid).unwrap().format_chars(0..1, |f| f.over.size = Some(24.0));+ let cs = compose_story(&d, sid, &ComposeOptions::default());+ let lines = all_lines(&cs);+ assert_eq!(lines.len(), 2);+ // The em box edge below the baseline (y down): its top, or its bottom one em lower.+ let edge = |l: &Line| {+ let g = &l.glyphs[0];+ let (a, b) = g.face.vertical_metrics();+ let em = g.face.units_per_em() * g.sy;+ let em_top = l.baseline - em * a / (a + b);+ if top { em_top } else { em_top + em }+ };+ assert!((edge(lines[1]) - edge(lines[0]) - 30.0).abs() < 1e-6, "{model:?}");+ }+}+#[test]-fn cjk_center_leading_measures_em_centers_across_different_sizes() {- let (mut d, sid, _) = doc_with("A\nB", Rect::new(0.0, 0.0, 300.0, 200.0), ParaAttrs::default());- d.story_mut(sid).unwrap().format_chars(0..3, |f| {- f.over.leading = Some(designcraft_doc::Leading::Points(30.0));- f.over.leading_model = Some(designcraft_doc::cjk::LeadingModel::Center);- });- d.story_mut(sid).unwrap().format_chars(0..1, |f| f.over.size = Some(24.0));- let cs = compose_story(&d, sid, &ComposeOptions::default());- let lines = all_lines(&cs);- assert_eq!(lines.len(), 2);- let center = |l: &Line| {- let g = &l.glyphs[0];- let (a, b) = g.face.vertical_metrics();- l.baseline + (b - a) / (2.0 * (a + b)) * g.face.units_per_em() * g.sy+fn cjk_em_center_alignment_moves_small_characters() {+ let line_with = |para: ParaAttrs| {+ let (mut d, sid, _) = doc_with("AB", Rect::new(0.0, 0.0, 300.0, 100.0), para);+ d.story_mut(sid).unwrap().format_chars(0..1, |f| f.over.size = Some(24.0));+ d.story_mut(sid).unwrap().format_chars(0..2, |f| f.over.character_alignment = Some(designcraft_doc::cjk::CharacterAlignment::EmCenter));+ compose_story(&d, sid, &ComposeOptions::default()).frames[0].lines[0].clone()};- assert!((center(lines[1]) - center(lines[0]) - 30.0).abs() < 1e-6);- assert!((lines[1].baseline - lines[0].baseline - 30.0).abs() > 0.1);+ let line = line_with(japanese());++ // Em box centre (y down): the font's BASE em box, else 0.88/-0.12 em.+ let center = |g: &PlacedGlyph| {- let (a, b) = g.face.vertical_metrics();- g.y + (b - a) / (2.0 * (a + b)) * g.face.units_per_em() * g.sy++ let upem = g.face.units_per_em();++ let (top, bottom) = g.face.declared_em_box().unwrap_or((upem * 0.88, upem * -0.12));++ g.y - (top + bottom) / 2.0 * g.sy+ };+ assert!((center(&line.glyphs[0]) - center(&line.glyphs[1])).abs() < 1e-6);+ // The other composers keep every character on the baseline.+ let line = line_with(ParaAttrs::default());+ assert!(line.glyphs.iter().all(|g| g.y.abs() < 1e-9), "{:?}", line.glyphs.iter().map(|g| g.y).collect::<Vec<_>>());}+ #[test]+ fn smaller_cjk_run_aligns_to_the_line_em_box_centre() {+ use designcraft_doc::cjk::CharacterAlignment;+ let text = "Ab 甲乙 cd";+ let run = "Ab ".len().."Ab 甲乙".len();+ // Em box centre (y down) of a placed glyph: its font's BASE em box, else 0.88/-0.12 em.+ let centre = |l: &Line, g: &PlacedGlyph| {+ let upem = g.face.units_per_em();- let (top, bottom) = g.face.base_em_box().unwrap_or((upem * 0.88, upem * -0.12));++ let (top, bottom) = g.face.declared_em_box().unwrap_or((upem * 0.88, upem * -0.12));+ l.baseline + g.y - (top + bottom) / 2.0 * g.sy+ };+ for alignment in [CharacterAlignment::EmCenter, CharacterAlignment::Baseline] {- let (mut d, sid, _) = doc_with(text, Rect::new(0.0, 0.0, 300.0, 100.0), ParaAttrs::default());++ let (mut d, sid, _) = doc_with(text, Rect::new(0.0, 0.0, 300.0, 100.0), japanese());+ d.story_mut(sid).unwrap().format_chars(0..text.len(), |f| {+ f.over.size = Some(10.0);+ f.over.character_alignment = Some(alignment);+ });+ d.story_mut(sid).unwrap().format_chars(run.clone(), |f| f.over.size = Some(6.0));+ let cs = compose_story(&d, sid, &ComposeOptions::default());+ let line = &cs.frames[0].lines[0];+ let latin = &line.glyphs[0];+ let cjk: Vec<&PlacedGlyph> = line.glyphs.iter().filter(|g| run.contains(&g.byte)).collect();+ assert_eq!(cjk.len(), 2);+ assert!(latin.y.abs() < 1e-9);+ for g in cjk {+ match alignment {+ CharacterAlignment::EmCenter => {+ assert!((centre(line, g) - centre(line, latin)).abs() < 0.01, "{} vs {}", centre(line, g), centre(line, latin));+ assert!(g.y < -1.0, "a 6 pt run rises in a 10 pt line: {}", g.y);+ }+ _ => assert!(g.y.abs() < 1e-9, "Roman baseline keeps the run on the baseline: {}", g.y),+ }+ }+ }+ }+#[test]fn cjk_hanging_punctuation_uses_frame_edge_without_changing_glyph_size() {let k = designcraft_doc::cjk::Kinsoku { name: "Test".into(), no_start: ",".into(), hanging: ",".into(), ..Default::default() };diff --cc crates/fonts/src/vertical.rsindex 6406cff30,80f2e9833..a85413332--- a/crates/fonts/src/vertical.rs+++ b/crates/fonts/src/vertical.rs@@@ -69,30 -69,14 +69,32 @@@ pub(crate) fn em_box(face: &FontFace) -(top, top - upem)})};- font.and_then(|f| f.os2().ok())+ let em = font+ .and_then(|f| f.os2().ok()).and_then(|t| centred(f64::from(t.s_typo_ascender()), f64::from(t.s_typo_descender()))).or_else(|| centred(face.ascent, -face.descent))- .map_or(((upem * 0.88, upem * -0.12), false), |b| (b, false))+ .unwrap_or((upem * 0.88, upem * -0.12));+ (em, false)}+/// The ideographic character face (ICF) box (top, bottom) in font units, y up: `BASE`+/// `icft`/`icfb` (one of them alone is mirrored inside the em box), else the em box inset by 5% of+/// the em at the top and bottom.+pub(crate) fn icf_box(face: &FontFace) -> (f64, f64) {+ let (top, bottom) = face.em_box();+ let font = face.skrifa();+ let base = match font.as_ref().and_then(|f| base_box(f, b"icfb", b"icft")) {+ Some((Some(b), Some(t))) => Some((t, b)),+ Some((Some(b), None)) => Some((top - (b - bottom), b)),+ Some((None, Some(t))) => Some((t, bottom + (top - t))),+ _ => None,+ };+ base.filter(|(t, b)| t.is_finite() && b.is_finite() && t > b).unwrap_or_else(|| {+ let inset = (top - bottom) * 0.05;+ (top - inset, bottom + inset)+ })+}+/// The em box from the `BASE` table's horizontal axis: the Han script's values, else the default/// script's, else the first script's that has them.fn base_em_box(font: &skrifa::FontRef<'_>, upem: f64) -> Option<(f64, f64)> {