Skip to content

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

Merge #317: Text: combining marks that start a run are shaped with their base letter

aionggaaff49833c5a
diff --cc crates/compose/src/shape.rs
index ae0617afe,c956c529f..171ca30d4
--- a/crates/compose/src/shape.rs
+++ b/crates/compose/src/shape.rs
@@@ -332,7 -258,7 +332,8 @@@ pub(crate) fn shape_para
resolved.push((a, b, props, fmt, para_chars.clone()));
}
}
+ let resolved = marks_join_their_base(&story.text, resolved);
+ let mut pieces: Vec<Piece> = Vec::with_capacity(resolved.len());
for (a, b, props, fmt, resolved_base) in resolved {
let style = table.intern(db, &props);
// A missing font's substitute stands in for the whole font: fallback fonts help it.
diff --cc crates/compose/src/tests.rs
index 1c7fd67c8,39b35b697..7f36d709f
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@@ -2591,641 -1983,17 +2591,675 @@@ fn empty_paragraph_uses_its_own_mark_no
}
#[test]
-fn korean_breaks_at_spaces() {
- for composer in [designcraft_doc::Composer::Paragraph, designcraft_doc::Composer::SingleLine] {
- for align in [Align::Left, Align::LeftJustified] {
- let para = ParaAttrs { composer: Some(composer), align: Some(align), ..Default::default() };
- // By script: Hangul is set word by word whatever the language.
- for language in ["Korean", "English: USA"] {
- let (d, sid) = korean_doc(KO_WORDS, language, para.clone());
- let mid = lines_starting_mid_word(&d, sid, KO_WORDS);
- assert!(mid.is_empty(), "{composer:?} {align:?} {language}: {mid:?}");
- }
- }
+fn spanish_text_gets_spanish_hyphenation() {
+ let text = "La transición democrática fue una construcción colectiva extraordinariamente compleja y desesperadamente necesaria.";
+ let narrow = Rect::new(36.0, 36.0, 120.0, 700.0);
+ let (mut d, sid, _) = doc_with(text, narrow, ParaAttrs::default());
+ let st = d.story_mut(sid).unwrap();
+ let n = st.len();
+ st.format_chars(0..n, |f| f.over.language = Some("Spanish".into()));
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let lines = all_lines(&cs);
+ let hy: Vec<&str> = lines.iter().filter(|l| l.hyphenated).map(|l| text[l.range.clone()].trim_end()).collect();
+ assert!(!hy.is_empty(), "Spanish hyphenates");
+ for l in &hy {
+ let last: String = l.chars().rev().take_while(|c| c.is_alphabetic()).collect::<Vec<_>>().into_iter().rev().collect();
+ let whole = text[text.find(l).unwrap() + l.len() - last.len()..].split(|c: char| !c.is_alphabetic()).next().unwrap();
+ let es = hyphen::Lang::for_language("Spanish").unwrap();
+ let pts = hyphen::hyphen_points_in(whole, &hyphen::Limits::default(), es);
+ assert!(pts.contains(&last.chars().count()), "break {last}- in {whole} is a Bezos point");
+ }
+}
+
+#[test]
+fn cjk_aki_adds_space_without_scaling_outlines() {
+ let (mut d, sid, _) = doc_with("AB", Rect::new(0.0, 0.0, 300.0, 100.0), ParaAttrs::default());
+ let old = compose_story(&d, sid, &ComposeOptions::default());
+ d.story_mut(sid).unwrap().format_chars(0..2, |f| {
+ f.over.leading_aki = Some(Some(0.25));
+ f.over.trailing_aki = Some(Some(0.5));
+ });
+ let new = compose_story(&d, sid, &ComposeOptions::default());
+ let a = &old.frames[0].lines[0];
+ let b = &new.frames[0].lines[0];
+ assert!((b.end_x - a.end_x - 18.0).abs() < 1e-6);
+ assert_eq!(a.glyphs[0].sx, b.glyphs[0].sx);
+ assert!((b.glyphs[0].x - a.glyphs[0].x - 3.0).abs() < 1e-6);
+}
+
+#[test]
+fn cjk_jidori_sets_group_width_and_survives_narrow_frames() {
+ for composer in [designcraft_doc::Composer::SingleLine, designcraft_doc::Composer::Paragraph] {
+ let (mut d, sid, _) = doc_with(
+ "AB",
+ Rect::new(0.0, 0.0, 30.0, 150.0),
+ ParaAttrs { composer: Some(composer), glyph_scale_desired: Some(1.2), letter_space_desired: Some(0.2), ..Default::default() },
+ );
+ d.story_mut(sid).unwrap().format_chars(0..2, |f| f.over.jidori = Some(4));
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let lines = all_lines(&cs);
+ assert_eq!(lines.len(), 1, "an indivisible jidori group must not be emergency-split");
+ assert!((lines[0].end_x - lines[0].x0 - 48.0).abs() < 1e-6);
+ assert_eq!(lines[0].range, 0..2);
+ }
+}
+
+#[test]
+fn cjk_tsume_removes_sidebearings_without_collapsing_ink() {
+ let (mut d, sid, _) = doc_with("HH", Rect::new(0.0, 0.0, 300.0, 100.0), ParaAttrs::default());
+ let old = compose_story(&d, sid, &ComposeOptions::default());
+ d.story_mut(sid).unwrap().format_chars(0..2, |f| f.over.tsume = Some(1.0));
+ let new = compose_story(&d, sid, &ComposeOptions::default());
+ let a = &old.frames[0].lines[0];
+ let b = &new.frames[0].lines[0];
+ let g = &b.glyphs[0];
+ let outline = designcraft_fonts::FontDb::global().outline(&g.face, g.gid);
+ let ink_width = designcraft_geom::Shape::bounding_box(&*outline).width() * g.sx;
+ assert!(ink_width > 0.0);
+ assert!((b.end_x - b.x0 - 2.0 * ink_width).abs() < 1e-6);
+ assert!(b.end_x < a.end_x);
+ assert_eq!(a.glyphs[0].sx, g.sx);
+}
+
+#[test]
+fn cjk_composite_font_selects_entries_before_shaping() {
+ use designcraft_doc::cjk::{CompositeFont, CompositeFontEntry};
+ let (mut d, sid, _) = doc_with("A1B", Rect::new(0.0, 0.0, 300.0, 100.0), ParaAttrs::default());
+ std::sync::Arc::make_mut(&mut d.styles).composite_fonts.push(CompositeFont {
+ name: "Mixed".into(),
+ entries: vec![
+ CompositeFontEntry { family: "Source Serif 4".into(), ..Default::default() },
+ CompositeFontEntry { characters: "0123456789".into(), family: "Source Sans 3".into(), relative_size: 0.5, ..Default::default() },
+ ],
+ });
+ d.story_mut(sid).unwrap().format_chars(0..3, |f| f.over.font_family = Some("CompositeFont/Mixed".into()));
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let line = &cs.frames[0].lines[0];
+ let a = line.glyphs.iter().find(|g| g.byte == 0).unwrap();
+ let n = line.glyphs.iter().find(|g| g.byte == 1).unwrap();
+ assert_ne!(a.face.id(), n.face.id());
+ assert_eq!(cs.styles[a.style as usize].size, 12.0);
+ assert_eq!(cs.styles[n.style as usize].size, 6.0);
+ assert!(!cs.styles.iter().any(|s| s.missing_font));
+}
+
+#[test]
+fn cjk_bunri_does_not_split_double_dash() {
+ for composer in [designcraft_doc::Composer::SingleLine, designcraft_doc::Composer::Paragraph] {
+ let (d, sid, _) =
+ doc_with("--", Rect::new(0.0, 0.0, 5.0, 100.0), ParaAttrs { composer: Some(composer), bunri_kinshi: Some(true), ..Default::default() });
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ assert_eq!(all_lines(&cs).len(), 1);
+ }
+}
+
+#[test]
+fn cjk_custom_kinsoku_disables_selected_breaks() {
+ let (mut d, sid, _) = doc_with(
+ "甲乙丙丁",
+ Rect::new(0.0, 0.0, 25.0, 200.0),
+ ParaAttrs {
+ composer: Some(designcraft_doc::Composer::SingleLine),
+ kinsoku: Some(Some(designcraft_doc::cjk::Kinsoku { name: "Test".into(), no_start: "乙丙丁".into(), ..Default::default() })),
+ ..Default::default()
+ },
+ );
+ d.story_mut(sid).unwrap().format_chars(0.."甲乙丙丁".len(), |f| f.over.size = Some(12.0));
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ assert_eq!(all_lines(&cs).len(), 1);
+}
+
+#[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
+ };
+ assert!((center(lines[1]) - center(lines[0]) - 30.0).abs() < 1e-6);
+ assert!((lines[1].baseline - lines[0].baseline - 30.0).abs() > 0.1);
+}
+
+#[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());
+ d.story_mut(sid).unwrap().format_chars(0..1, |f| f.over.size = Some(24.0));
+ d.story_mut(sid).unwrap().format_chars(1..2, |f| f.over.character_alignment = Some(designcraft_doc::cjk::CharacterAlignment::EmCenter));
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let line = &cs.frames[0].lines[0];
+ 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
+ };
+ assert!((center(&line.glyphs[0]) - center(&line.glyphs[1])).abs() < 1e-6);
+}
+
+#[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() };
+ let (mut d, sid, fid) = doc_with(
+ "AB,",
+ Rect::new(0.0, 0.0, 300.0, 100.0),
+ ParaAttrs {
+ composer: Some(designcraft_doc::Composer::SingleLine),
+ kinsoku: Some(Some(k)),
+ kinsoku_hang: Some(designcraft_doc::cjk::KinsokuHang::Regular),
+ ..Default::default()
+ },
+ );
+ let initial = compose_story(&d, sid, &ComposeOptions::default());
+ let line = &initial.frames[0].lines[0];
+ let text_w = line.glyphs.iter().take(2).map(|g| g.adv).sum::<f64>();
+ // A fresh frame whose measure fits the letters, but not the comma.
+ let frame = d.item_mut(fid).unwrap();
+ frame.path = designcraft_geom::shapes::rectangle(Rect::new(0.0, 0.0, text_w + 1.0, 100.0));
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ assert_eq!(all_lines(&cs).len(), 1);
+ assert_eq!(cs.frames[0].lines[0].glyphs.last().unwrap().sx, line.glyphs.last().unwrap().sx);
+}
+
+#[test]
+fn cjk_fullwidth_space_obeys_the_paragraph_spacing_switch() {
+ let text = "A B";
+ let (mut d, sid, _) = doc_with(
+ text,
+ Rect::new(0.0, 0.0, 300.0, 100.0),
+ ParaAttrs { word_space_desired: Some(2.0), treat_ideographic_space_as_space: Some(false), ..Default::default() },
+ );
+ let fixed = compose_story(&d, sid, &ComposeOptions::default());
+ d.story_mut(sid).unwrap().paras[0].para.treat_ideographic_space_as_space = Some(true);
+ let elastic = compose_story(&d, sid, &ComposeOptions::default());
+ let space = fixed.frames[0].lines[0].glyphs.iter().find(|g| g.byte == 1).unwrap().adv;
+ assert!((elastic.frames[0].lines[0].end_x - fixed.frames[0].lines[0].end_x - space).abs() < 1e-6);
+}
+
+#[test]
+fn arabic_bidi_keeps_paragraph_context_across_forced_lines() {
+ let text = "مرحبا\u{2028}123 - 456";
+ let (d, sid, _) = doc_with(text, Rect::new(0.0, 0.0, 600.0, 200.0), ParaAttrs::default());
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let lines = all_lines(&cs);
+ assert_eq!(lines.len(), 2);
+ let mut gs: Vec<_> = lines[1].glyphs.iter().filter(|g| g.len > 0).collect();
+ gs.sort_by(|a, b| a.x.total_cmp(&b.x));
+ let actual: String = gs.iter().map(|g| text[g.byte..].chars().next().unwrap()).collect();
+ let info = unicode_bidi::BidiInfo::new(text, Some(unicode_bidi::Level::ltr()));
+ let start = text.find('1').unwrap();
+ let expected = info.reorder_line(&info.paragraphs[0], start..text.len()).to_string();
+ assert_eq!(actual, expected);
+ assert_ne!(actual, "123 - 456", "the previous Arabic strong character affects number ordering");
+}
+
+#[test]
+fn arabic_character_direction_override_survives_line_layout() {
+ let text = "abc 123 def";
+ let (mut d, sid, _) = doc_with(text, Rect::new(0.0, 0.0, 600.0, 200.0), ParaAttrs::default());
+ d.story_mut(sid).unwrap().format_chars(4..7, |f| f.over.character_direction = Some(designcraft_doc::arabic::CharacterDirection::RightToLeft));
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let line = all_lines(&cs)[0];
+ let x = |byte| line.glyphs.iter().find(|g| g.byte == byte).unwrap().x;
+ assert!(x(4) > x(5) && x(5) > x(6));
+ assert!(x(0) < x(6) && x(4) < x(8));
+}
+
+#[test]
+fn arabic_fallback_marks_stay_with_bases_and_custom_offsets_move_only_marks() {
+ let db = designcraft_fonts::FontDb::global();
+ let Some(face) = db.fallback_for('ب', db.face(designcraft_fonts::DEFAULT_FAMILY, "Regular").id(), None) else { return };
+ if !face.covers('ُ') {
+ return;
+ }
+ let text = "بُبَ";
+ let (mut d, sid, _) = doc_with(text, Rect::new(0.0, 0.0, 600.0, 200.0), ParaAttrs::default());
+ d.settings.glyph_fallback = true;
+ let plain = compose_story(&d, sid, &ComposeOptions::default()).frames[0].lines[0].glyphs.clone();
+ assert!(plain.iter().all(|g| g.face.id() == face.id()), "fallback must not split Arabic marks into the Latin face");
+ assert!(plain.iter().all(|g| g.gid != 0));
+ d.story_mut(sid).unwrap().format_chars(0..text.len(), |f| {
+ f.over.diacritic_x_offset = Some(200.0);
+ f.over.diacritic_y_offset = Some(-100.0);
+ });
+ let shifted = compose_story(&d, sid, &ComposeOptions::default()).frames[0].lines[0].glyphs.clone();
+ let mut marks = 0;
+ for (a, b) in plain.iter().zip(&shifted) {
+ assert_eq!(a.gid, b.gid);
+ assert_eq!(a.adv, b.adv);
+ if face.glyph_is_mark(a.gid) {
+ marks += 1;
+ assert!((b.x - a.x - 2.4).abs() < 1e-6);
+ assert!((b.y - a.y - 1.2).abs() < 1e-6);
+ } else {
+ assert!((a.x - b.x).abs() < 1e-6 && (a.y - b.y).abs() < 1e-6);
+ }
+ }
+ assert!(marks > 0);
+}
+
+#[test]
+fn arabic_joining_context_crosses_character_style_boundaries() {
+ let text = "ببب";
+ let (mut d, sid, _) = doc_with(text, Rect::new(0.0, 0.0, 600.0, 200.0), ParaAttrs::default());
+ d.settings.glyph_fallback = true;
+ let plain = compose_story(&d, sid, &ComposeOptions::default()).frames[0].lines[0].glyphs.clone();
+ if plain.iter().any(|g| g.gid == 0) {
+ return;
+ }
+ d.story_mut(sid).unwrap().format_chars(2..4, |f| f.over.fill = Some("Paper".into()));
+ let split = compose_story(&d, sid, &ComposeOptions::default()).frames[0].lines[0].glyphs.clone();
+ assert_eq!(plain.iter().map(|g| (g.byte, g.gid)).collect::<Vec<_>>(), split.iter().map(|g| (g.byte, g.gid)).collect::<Vec<_>>());
+}
+
+#[test]
+fn arabic_character_kashida_switch_prevents_insertion() {
+ let text = "بسم الله الرحمن الرحيم الحمد لله رب العالمين";
+ let (mut d, sid, _) = doc_with(
+ text,
+ Rect::new(0.0, 0.0, 160.0, 300.0),
+ ParaAttrs { direction: Some(designcraft_doc::TextDirection::RightToLeft), align: Some(Align::RightJustified), ..Default::default() },
+ );
+ d.settings.glyph_fallback = true;
+ d.story_mut(sid).unwrap().format_chars(0..text.len(), |f| f.over.allow_kashidas = Some(false));
+ let off = compose_story(&d, sid, &ComposeOptions::default());
+ assert!(all_lines(&off).iter().all(|l| l.glyphs.iter().all(|g| g.len > 0 || g.face.glyph_for('\u{0640}') != g.gid)));
+}
+
+#[test]
+fn arabic_indic_digit_conversion_retains_original_utf8_ranges() {
+ let text = "١٢٣";
+ let (mut d, sid, _) = doc_with(text, Rect::new(0.0, 0.0, 200.0, 100.0), ParaAttrs::default());
+ d.story_mut(sid).unwrap().format_chars(0..text.len(), |f| f.over.digits = Some(designcraft_doc::Digits::Arabic));
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let gs = &cs.frames[0].lines[0].glyphs;
+ assert_eq!(gs.iter().map(|g| (g.byte, g.len)).collect::<Vec<_>>(), [(0, 2), (2, 2), (4, 2)]);
+ for (g, c) in gs.iter().zip("123".chars()) {
+ assert_eq!(g.gid, g.face.glyph_for(c));
+ }
+}
+
+#[test]
+fn arabic_contextual_digits_follow_strong_text_across_style_changes() {
+ let text = "ب 12 A 34";
+ let (mut d, sid, _) = doc_with(text, Rect::new(0.0, 0.0, 300.0, 100.0), ParaAttrs::default());
+ d.settings.glyph_fallback = true;
+ let arabic_digits = text.find('1').unwrap();
+ d.story_mut(sid).unwrap().format_chars(arabic_digits..arabic_digits + 2, |f| f.over.fill = Some("Paper".into()));
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let gs = &cs.frames[0].lines[0].glyphs;
+ let g = gs.iter().find(|g| g.byte == arabic_digits).unwrap();
+ assert_eq!(g.gid, g.face.glyph_for('١'));
+ let g = gs.iter().find(|g| g.byte == text.find('3').unwrap()).unwrap();
+ assert_eq!(g.gid, g.face.glyph_for('3'));
+}
+
+#[test]
+fn arabic_story_direction_flows_right_column_first() {
+ let text = format!("first{}second", designcraft_doc::story::COLUMN_BREAK);
+ let (mut d, sid, fid) = doc_with(&text, Rect::new(0.0, 0.0, 300.0, 200.0), ParaAttrs::default());
+ d.story_mut(sid).unwrap().direction = designcraft_doc::TextDirection::RightToLeft;
+ d.item_mut(fid).unwrap().text_frame_mut().unwrap().options.columns = 2;
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let lines = all_lines(&cs);
+ assert_eq!(lines.len(), 2);
+ assert!(lines[0].x0 > lines[1].x0);
+ assert!(cs.frames[0].columns[0].x0 > cs.frames[0].columns[1].x0);
+}
+
+#[test]
+fn arabic_rtl_table_keeps_logical_indices_and_merged_cell_hit_testing() {
+ let mut t = designcraft_doc::Table::new(987, 2, 3, 0, 0, 240.0);
+ t.options.direction = designcraft_doc::TextDirection::RightToLeft;
+ t.cell_mut(0, 0).unwrap().text = designcraft_doc::Story::with_text(StoryId(0), "Right", ParaFormat::default());
+ t.cell_mut(0, 2).unwrap().text = designcraft_doc::Story::with_text(StoryId(0), "Left", ParaFormat::default());
+ t.merge(designcraft_doc::CellRange::new(1, 0, 1, 1)).unwrap();
+ let (d, sid, _) = table_doc(Rect::new(0.0, 0.0, 400.0, 400.0), t);
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let (_, _, right) = table::find_cell(&cs, 987, 0, 0).unwrap();
+ let (_, _, left) = table::find_cell(&cs, 987, 0, 2).unwrap();
+ assert!(right.rect.x0 > left.rect.x0);
+ let (_, _, merged) = table::find_cell(&cs, 987, 1, 0).unwrap();
+ assert!((merged.rect.width() - 160.0).abs() < 1e-6);
+ assert_eq!(table::hit_cell(&cs, 0, right.rect.center()).map(|(_, r, c, _)| (r, c)), Some((0, 0)));
+}
+
+#[test]
+fn arabic_kashida_moves_marks_with_their_cluster_and_respects_nonjoiners() {
+ let text = "بُسْمِ بُسْمِ";
+ let (mut d, sid, _) = doc_with(
+ text,
+ Rect::new(0.0, 0.0, 180.0, 100.0),
+ ParaAttrs { direction: Some(designcraft_doc::TextDirection::RightToLeft), align: Some(Align::FullyJustified), ..Default::default() },
+ );
+ d.settings.glyph_fallback = true;
+ let with = compose_story(&d, sid, &ComposeOptions::default()).frames[0].lines[0].glyphs.clone();
+ if with.iter().any(|g| g.gid == 0) {
+ return;
+ }
+ assert!(with.iter().any(|g| g.len == 0 && g.gid == g.face.glyph_for('\u{0640}')));
+ d.story_mut(sid).unwrap().paras[0].para.kashidas = Some(false);
+ let without = compose_story(&d, sid, &ComposeOptions::default()).frames[0].lines[0].glyphs.clone();
+ for (a, b) in with.iter().zip(&without) {
+ assert_eq!((a.gid, a.byte), (b.gid, b.byte));
+ let first_a = with.iter().find(|g| g.byte == a.byte).unwrap();
+ let first_b = without.iter().find(|g| g.byte == b.byte).unwrap();
+ assert!(((a.x - first_a.x) - (b.x - first_b.x)).abs() < 1e-6, "marks detached from their cluster");
+ }
+ let text = "ب\u{200C}ب ب\u{200C}ب";
+ let (mut d, sid, _) = doc_with(
+ text,
+ Rect::new(0.0, 0.0, 180.0, 100.0),
+ ParaAttrs { direction: Some(designcraft_doc::TextDirection::RightToLeft), align: Some(Align::FullyJustified), ..Default::default() },
+ );
+ d.settings.glyph_fallback = true;
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ assert!(cs.frames[0].lines[0].glyphs.iter().all(|g| g.gid != g.face.glyph_for('\u{0640}') || g.len > 0));
+}
+
+#[test]
+fn arabic_explicit_isolated_forms_disable_contextual_joining() {
+ let text = "بب";
+ let (mut d, sid, _) = doc_with(text, Rect::new(0.0, 0.0, 180.0, 100.0), ParaAttrs::default());
+ d.settings.glyph_fallback = true;
+ d.story_mut(sid).unwrap().format_chars(0..text.len(), |f| f.over.positional_form = Some("Isolated".into()));
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let gs = &cs.frames[0].lines[0].glyphs;
+ for g in gs {
+ assert_eq!(g.gid, g.face.glyph_for('ب'));
+ }
+}
+
+#[test]
+fn arabic_character_overrides_mirror_brackets_using_final_levels() {
+ use designcraft_doc::arabic::CharacterDirection as D;
+ for (text, direction, mirrored) in [("(123)", D::RightToLeft, true), ("(ب)", D::LeftToRight, false)] {
+ let (mut d, sid, _) = doc_with(text, Rect::new(0.0, 0.0, 300.0, 100.0), ParaAttrs::default());
+ d.story_mut(sid).unwrap().format_chars(0..text.len(), |f| f.over.character_direction = Some(direction));
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let gs = &cs.frames[0].lines[0].glyphs;
+ for (byte, ch) in [(0, '('), (text.len() - 1, ')')] {
+ let g = gs.iter().find(|g| g.byte == byte).unwrap();
+ let expected = if mirrored { unicode_bidi_mirroring::get_mirrored(ch).unwrap() } else { ch };
+ assert_eq!(g.gid, g.face.glyph_for(expected), "{text:?} {direction:?} byte {byte}");
+ }
+ }
+}
+
+#[test]
+fn language_picks_localized_forms_through_the_word_cache() {
+ // Source Serif 4 has a Turkish `locl` form of `i`; the same words in English and Turkish
+ // (both shaped word by word through the cache) keep their own forms.
+ let text = "in in in in";
+ let (mut d, sid, _) = doc_with(text, Rect::new(36.0, 36.0, 500.0, 200.0), ParaAttrs::default());
+ d.story_mut(sid).unwrap().format_chars(6..text.len(), |f| f.over.language = Some("Turkish".into()));
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let gid_at = |byte: usize| all_lines(&cs).iter().flat_map(|l| l.glyphs.iter()).find(|g| g.byte == byte).map(|g| g.gid).unwrap();
+ assert_eq!(gid_at(0), gid_at(3), "English i");
+ assert_eq!(gid_at(6), gid_at(9), "Turkish i");
+ assert_ne!(gid_at(0), gid_at(6), "Turkish i takes its localized form");
+ // A single word is shaped directly, in its language too.
+ let (mut d, sid, _) = doc_with("i", Rect::new(36.0, 36.0, 500.0, 200.0), ParaAttrs::default());
+ d.story_mut(sid).unwrap().format_chars(0..1, |f| f.over.language = Some("Turkish".into()));
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ assert_eq!(all_lines(&cs)[0].glyphs[0].gid, gid_at(6), "Turkish i, shaped on its own");
+}
+
+#[test]
+fn chinese_and_japanese_take_their_localized_forms() {
+ // Noto Sans CJK SC (craft-fonts) draws 直 differently for Japanese (`locl`).
+ let Some(noto) = designcraft_fonts::CRAFT_FONTS.iter().find(|f| f.family == "Noto Sans CJK SC") else { return };
+ FontDb::global().add_font(noto.bytes.to_vec());
+ let gid = |language: &str| {
+ let (mut d, sid, _) = doc_with("直", Rect::new(36.0, 36.0, 300.0, 100.0), ParaAttrs::default());
+ d.story_mut(sid).unwrap().format_chars(0.."直".len(), |f| {
+ f.over.font_family = Some(noto.family.into());
+ f.over.language = Some(language.into());
+ });
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let g = &all_lines(&cs)[0].glyphs[0];
+ assert_eq!(g.face.family, noto.family);
+ g.gid
+ };
+ let chinese = gid("Chinese: Simplified");
+ assert_eq!(chinese, FontDb::global().face(noto.family, noto.style).glyph_for('直'));
+ assert_ne!(gid("Japanese"), chinese, "Japanese 直");
+}
+
+#[test]
+fn cjk_fallback_follows_the_language() {
+ use designcraft_fonts::testing::font_with;
+ // Stand-ins for Hiragino Mincho ProN (the Japanese chain's first system font) and Songti SC
+ // (first in the Simplified Chinese chain); the real ones when installed.
+ let db = designcraft_fonts::FontDb::global();
+ db.add_font(font_with("Hiragino Mincho ProN", &['直']).unwrap());
+ db.add_font(font_with("Songti SC", &['直']).unwrap());
+ // (face of the Latin letter, face of the ideograph)
+ let faces_of = |language: &str| {
+ let (mut d, sid, _) = doc_with("a直", Rect::new(36.0, 36.0, 300.0, 100.0), ParaAttrs::default());
+ d.settings.glyph_fallback = true;
+ d.story_mut(sid).unwrap().format_chars(0..4, |f| f.over.language = Some(language.into()));
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let face_at = |byte: usize| all_lines(&cs).iter().flat_map(|l| l.glyphs.iter()).find(|g| g.byte == byte).map(|g| g.face).unwrap();
+ (face_at(0), face_at(1))
+ };
+ assert_eq!(faces_of("Chinese: Simplified").1.family, "Songti SC");
+ // Japanese: the craft-fonts faces lead the chain (Mincho first), then the system fonts.
+ let japanese = designcraft_fonts::japanese_document_fonts().first().map_or("Hiragino Mincho ProN", |f| f.family);
+ assert_eq!(faces_of("Japanese").1.family, japanese);
+ // Without a CJK language, the language-blind search.
+ let (primary, han) = faces_of("English: USA");
+ assert_eq!(Some(han.family.clone()), db.fallback_for('直', primary.id(), None).map(|f| f.family.clone()));
+}
+
+#[test]
+fn missing_glyphs_are_the_fonts_box_unless_fallback_is_on() {
+ use designcraft_fonts::testing::font_with;
+ // Some font draws 語 when fallback fonts are allowed (a system CJK font, or this stand-in).
+ designcraft_fonts::FontDb::global().add_font(font_with("DC Test Missing Glyph Helper", &['語']).unwrap());
+ let text = "a語";
+ let (mut d, sid, _) = doc_with(text, Rect::new(36.0, 36.0, 300.0, 100.0), ParaAttrs::default());
+ d.story_mut(sid).unwrap().format_chars(0..text.len(), |f| f.over.language = Some("Japanese".into()));
+ let glyph_at = |d: &Document, byte: usize| {
+ let cs = compose_story(d, sid, &ComposeOptions::default());
+ all_lines(&cs).iter().flat_map(|l| l.glyphs.iter()).find(|g| g.byte == byte).map(|g| (g.face, g.gid, g.adv)).unwrap()
+ };
+ assert!(!d.settings.glyph_fallback, "new documents draw missing glyphs as InDesign does");
+ let (face, gid, adv) = glyph_at(&d, 1);
+ assert_eq!((face.family.as_str(), gid), (designcraft_fonts::DEFAULT_FAMILY, 0), "Source Serif 4's .notdef");
+ assert!((adv - face.advance(0) * 12.0 / face.upem).abs() < 1e-6, "its advance: {adv}");
+ d.settings.glyph_fallback = true;
+ let (face, gid, _) = glyph_at(&d, 1);
+ assert_ne!(face.family, designcraft_fonts::DEFAULT_FAMILY);
+ assert_ne!(gid, 0, "a fallback font draws it");
+}
+
+/// A missing font's substitute draws what it lacks from fallback fonts whatever the setting, also
+/// in text shaped as one piece across a colour change.
+#[test]
+fn a_missing_fonts_substitute_takes_fallback_fonts() {
+ use designcraft_fonts::testing::font_with;
+ designcraft_fonts::FontDb::global().add_font(font_with("DC Test Missing Glyph Helper", &['語']).unwrap());
+ let text = "a語b語";
+ let (mut d, sid, _) = doc_with(text, Rect::new(36.0, 36.0, 300.0, 100.0), ParaAttrs::default());
+ {
+ let s = d.story_mut(sid).unwrap();
+ s.format_chars(0..text.len(), |f| {
+ f.over.language = Some("Japanese".into());
+ f.over.font_family = Some("DC Test No Such Family".into());
+ });
+ s.format_chars("a語".len()..text.len(), |f| f.over.fill = Some("Red".into()));
+ }
+ assert!(!d.settings.glyph_fallback);
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let glyphs: Vec<&PlacedGlyph> = all_lines(&cs).into_iter().flat_map(|l| l.glyphs.iter()).filter(|g| text[g.byte..].starts_with('語')).collect();
+ assert_eq!(glyphs.len(), 2);
+ for g in glyphs {
+ assert_ne!(g.face.family, designcraft_fonts::DEFAULT_FAMILY, "byte {}", g.byte);
+ assert_ne!(g.gid, 0, "byte {}: a fallback font draws it", g.byte);
+ }
+}
+
++#[test]
++fn a_mark_starting_a_run_is_shaped_with_its_base() {
++ use designcraft_fonts::testing::font_with;
++ const HEBREW: &str = "DC Test Hebrew Points";
++ designcraft_fonts::FontDb::global().add_font(font_with(HEBREW, &['ש', '\u{5C2}', 'ל']).unwrap());
++ // Shin, then sin dot and lamed in a run that only turns ligatures off.
++ let text = "ש\u{5C2}ל";
++ let mark = "ש".len();
++ let glyphs = |family: &str, mark_family: Option<&str>| {
++ let (mut d, sid, _) = doc_with(text, Rect::new(36.0, 36.0, 300.0, 100.0), ParaAttrs::default());
++ let story = d.story_mut(sid).unwrap();
++ story.format_chars(0..text.len(), |f| f.over.font_family = Some(family.into()));
++ story.format_chars(mark..text.len(), |f| {
++ f.over.ligatures = Some(false);
++ if let Some(m) = mark_family {
++ f.over.font_family = Some(m.into());
++ }
++ });
++ let cs = compose_story(&d, sid, &ComposeOptions::default());
++ all_lines(&cs).iter().flat_map(|l| l.glyphs.iter()).map(|g| (g.face, g.gid, g.byte)).collect::<Vec<_>>()
++ };
++ // A missing font: its substitute lacks both, so the letter comes from a fallback font, and the
++ // mark from the same font, in its letter's cluster.
++ let g = glyphs("DC Test No Such Family", None);
++ let (base, dot) = (g[0], g[1]);
++ assert_ne!(base.1, 0, "{g:?}");
++ assert_eq!((dot.0.family.as_str(), dot.2), (base.0.family.as_str(), 0), "the sin dot goes with its shin: {g:?}");
++ assert_ne!(dot.1, 0, "{g:?}");
++ // The run's own font lacks the mark: its box, whatever font the letter before it has.
++ let g = glyphs(HEBREW, Some(designcraft_fonts::DEFAULT_FAMILY));
++ let dot = g.iter().find(|x| x.2 == mark).unwrap();
++ assert_eq!((dot.0.family.as_str(), dot.1), (designcraft_fonts::DEFAULT_FAMILY, 0), "{g:?}");
++}
++
+#[test]
+fn kenten_missing_from_the_font_are_its_box_unless_fallback_is_on() {
+ use designcraft_fonts::testing::font_with;
+ // Some font draws the sesame dot when fallback fonts are allowed (a system font, or this one).
+ designcraft_fonts::FontDb::global().add_font(font_with("DC Test Kenten Helper", &['\u{FE45}']).unwrap());
+ let (mut d, sid, _) = doc_with("ab", Rect::new(36.0, 36.0, 300.0, 100.0), ParaAttrs::default());
+ let plain = compose_story(&d, sid, &ComposeOptions::default()).frames[0].lines[0].glyphs.len();
+ d.story_mut(sid).unwrap().format_chars(0..2, |f| f.over.kenten = Some(true));
+ let marks = |d: &Document| {
+ let cs = compose_story(d, sid, &ComposeOptions::default());
+ cs.frames[0].lines[0].glyphs[plain..].iter().map(|g| (g.face.family.clone(), g.gid)).collect::<Vec<_>>()
+ };
+ let missing = marks(&d);
+ assert_eq!(missing.len(), 2);
+ assert!(missing.iter().all(|(family, gid)| family == designcraft_fonts::DEFAULT_FAMILY && *gid == 0), "{missing:?}");
+ d.settings.glyph_fallback = true;
+ let drawn = marks(&d);
+ assert_eq!(drawn.len(), 2);
+ assert!(drawn.iter().all(|(family, gid)| family != designcraft_fonts::DEFAULT_FAMILY && *gid != 0), "{drawn:?}");
+}
+
+#[test]
+fn each_document_shapes_words_in_its_own_fonts() {
+ use designcraft_fonts::testing::font_with_glyph;
+ const FAMILY: &str = "DocFont Word Cache";
+ // Two documents' fonts of one name: the same words, drawn as `X` in one and `O` in the other.
+ let root = std::env::temp_dir().join(format!("dc-compose-docfonts-{}", std::process::id()));
+ let mut scopes = Vec::new();
+ for (name, glyph) in [("x", 'X'), ("o", 'O')] {
+ let folder = root.join(name);
+ std::fs::create_dir_all(&folder).unwrap();
+ std::fs::write(folder.join("word.ttf"), font_with_glyph(FAMILY, &['a', 'b', ' '], glyph).unwrap()).unwrap();
+ scopes.push((designcraft_fonts::FontDb::global().load_document_fonts(&folder).scope, glyph));
+ }
+ let text = "ab ab ab ab";
+ let mut faces = Vec::new();
+ for (scope, glyph) in scopes {
+ let (mut d, sid, _) = doc_with(text, Rect::new(36.0, 36.0, 300.0, 100.0), ParaAttrs::default());
+ d.font_scope = scope;
+ d.story_mut(sid).unwrap().format_chars(0..text.len(), |f| f.over.font_family = Some(FAMILY.into()));
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let glyphs: Vec<&PlacedGlyph> =
+ all_lines(&cs).into_iter().flat_map(|l| l.glyphs.iter()).filter(|g| text[g.byte..].starts_with('a')).collect();
+ assert_eq!(glyphs.len(), 4);
+ let face = glyphs[0].face;
+ let drawn = designcraft_fonts::FontDb::global().face(designcraft_fonts::FALLBACK_FAMILY, "Regular").glyph_for(glyph);
+ assert!(glyphs.iter().all(|g| g.face == face && g.gid == drawn), "scope {scope}: {glyph}");
+ faces.push(face);
+ }
+ assert!(faces[0] != faces[1] && faces.iter().all(|f| f.family == FAMILY));
+ let _ = std::fs::remove_dir_all(&root);
+}
+
+#[test]
+fn composite_fonts_draw_from_the_documents_fonts() {
+ use designcraft_doc::cjk::{CompositeFont, CompositeFontEntry};
+ use designcraft_fonts::testing::font_with_glyph;
+ const FAMILY: &str = "DocFont Composite Member";
+ // A composite font whose digits come from a font only the document has.
+ let folder = std::env::temp_dir().join(format!("dc-compose-composite-docfont-{}", std::process::id()));
+ std::fs::create_dir_all(&folder).unwrap();
+ std::fs::write(folder.join("digits.ttf"), font_with_glyph(FAMILY, &['1'], 'X').unwrap()).unwrap();
+ let scope = FontDb::global().load_document_fonts(&folder).scope;
+ let (mut d, sid, _) = doc_with("A1B", Rect::new(0.0, 0.0, 300.0, 100.0), ParaAttrs::default());
+ d.font_scope = scope;
+ std::sync::Arc::make_mut(&mut d.styles).composite_fonts.push(CompositeFont {
+ name: "Mixed".into(),
+ entries: vec![
+ CompositeFontEntry { family: designcraft_fonts::DEFAULT_FAMILY.into(), ..Default::default() },
+ CompositeFontEntry { characters: "0123456789".into(), family: FAMILY.into(), ..Default::default() },
+ ],
+ });
+ d.story_mut(sid).unwrap().format_chars(0..3, |f| f.over.font_family = Some("CompositeFont/Mixed".into()));
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let digit = cs.frames[0].lines[0].glyphs.iter().find(|g| g.byte == 1).unwrap();
+ assert_eq!(digit.face.family, FAMILY);
+ assert_ne!(digit.gid, 0);
+ assert!(!cs.styles.iter().any(|s| s.missing_font), "the document's font isn't missing");
+ let _ = std::fs::remove_dir_all(&folder);
+}
+
+const KO_WORDS: &str = "한국어 문장은 띄어쓰기 단위로 줄을 바꿉니다 한국어 문장은 띄어쓰기 단위로 줄을 바꿉니다";
+const KO_HANJA: &str = "大韓民國 憲法은 國民의 權利를 保障한다 大韓民國 憲法은 國民의 權利를 保障한다";
+const KO_PUNCT: &str = "가나다라마。바사아자차」카타파하가、나다라마바。사아자차카」타파하가나、다라마바사。아자차카타」파하가나다、";
+/// Syllables for words wider than the frame.
+const KO_LONG: &str = "가나다라마바사아자차카타파하」";
+
+/// A Korean paragraph in a narrow frame (70 pt), set in a stand-in font covering the samples.
+fn korean_doc(text: &str, language: &str, para: ParaAttrs) -> (Document, StoryId) {
+ let chars: Vec<char> = [KO_WORDS, KO_HANJA, KO_PUNCT, KO_LONG].concat().chars().collect();
+ let font = designcraft_fonts::testing::font_with("DC Test Korean", &chars).unwrap();
+ designcraft_fonts::FontDb::global().add_font(font);
+ let (mut d, sid, _) = doc_with(text, Rect::new(36.0, 36.0, 106.0, 400.0), para);
+ d.story_mut(sid).unwrap().format_chars(0..text.len(), |f| {
+ f.over.font_family = Some("DC Test Korean".into());
+ f.over.language = Some(language.into());
+ });
+ (d, sid)
+}
+
+/// The lines (after the first) that start inside a space-separated word.
+fn lines_starting_mid_word(d: &Document, sid: StoryId, text: &str) -> Vec<String> {
+ let cs = compose_story(d, sid, &ComposeOptions::default());
+ let lines = all_lines(&cs);
+ assert!(lines.len() > 2, "wraps: {} lines", lines.len());
+ lines.iter().skip(1).filter(|l| !text[..l.range.start].ends_with(' ')).map(|l| text[l.range.clone()].to_string()).collect()
+}
+
+#[test]
+fn korean_breaks_at_spaces() {
+ for composer in [designcraft_doc::Composer::Paragraph, designcraft_doc::Composer::SingleLine] {
+ for align in [Align::Left, Align::LeftJustified] {
+ let para = ParaAttrs { composer: Some(composer), align: Some(align), ..Default::default() };
+ // By script: Hangul is set word by word whatever the language.
+ for language in ["Korean", "English: USA"] {
+ let (d, sid) = korean_doc(KO_WORDS, language, para.clone());
+ let mid = lines_starting_mid_word(&d, sid, KO_WORDS);
+ assert!(mid.is_empty(), "{composer:?} {align:?} {language}: {mid:?}");
+ }
+ }
}
}