Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

CJK: leading model space below measures from the em box top, space above from the bottom

aiongg9c935ce50c66
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs
index 8661155c0..fec45383b 100644
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@ -2117,8 +2117,10 @@ fn cjk_line_reference(line: &[Glyph]) -> f64 {
let (top, bottom) = cjk_em_box(g);
match g.leading_model {
L::Roman => 0.0,
- L::AkiBelow => bottom,
- L::AkiAbove => top,
+ // "Space below" measures from the em box top (Em Box Top/Right), "space above" from its
+ // bottom (Em Box Bottom/Left).
+ L::AkiBelow => top,
+ L::AkiAbove => bottom,
L::Center | L::CenterDown => (top + bottom) / 2.0,
}
}
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index d0ea3505c..a94c01187 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -1678,6 +1678,31 @@ fn cjk_center_leading_measures_em_centers_across_different_sizes() {
assert!((lines[1].baseline - lines[0].baseline - 30.0).abs() > 0.1);
}
+#[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), 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(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_em_center_alignment_moves_small_characters() {
let (mut d, sid, _) = doc_with("AB", Rect::new(0.0, 0.0, 300.0, 100.0), ParaAttrs::default());