diff --git a/crates/compose/src/breaker.rs b/crates/compose/src/breaker.rs index 46fb3bdff..bd9351119 100644 --- a/crates/compose/src/breaker.rs +++ b/crates/compose/src/breaker.rs @@ -53,8 +53,12 @@ pub struct Spacing { pub optical_size: Option, /// Korean text breaks between syllables instead of at spaces. pub korean_char_breaks: bool, - /// 0: minimum adjustment; 1: push in; 2: push out first; 3: push out only. + /// JIS X 4051 禁則処理 type: 0: minimum adjustment (調整量優先); 1: push in first (追い込み + /// 優先, the default); 2: push out first (追い出し優先); 3: push out only (追い出しのみ). pub kinsoku_priority: u8, + /// The paragraph has CJK text, so the 禁則 priority biases its breaks. A Latin paragraph's + /// breaks are left alone by the (default) push-in-first policy. + pub kinsoku_cjk: bool, } impl Default for Spacing { @@ -77,19 +81,25 @@ impl Default for Spacing { optical: false, optical_size: None, korean_char_breaks: false, - kinsoku_priority: 0, + kinsoku_priority: 1, + kinsoku_cjk: false, } } } impl Spacing { + /// Push out only: the 追い込み (compression that would pull the next character in) is + /// unavailable, so every line end is pushed out (and no line is compressed at its end). + pub(crate) fn push_out_only(&self) -> bool { + self.kinsoku_cjk && self.kinsoku_priority == 3 + } /// Per-tier (word, letter, glyph) stretch and shrink of glyph `g` as a box. fn box_elastic(&self, g: &Glyph) -> ([f64; 3], [f64; 3]) { if g.locked_advance { return ([0.0; 3], [0.0; 3]); } let moji_stretch = if self.justify { g.moji.stretch } else { 0.0 }; - let moji_shrink = if self.kinsoku_priority == 3 { 0.0 } else { g.moji.shrink }; + let moji_shrink = if self.push_out_only() { 0.0 } else { g.moji.shrink }; if !self.justify { return ([0.0; 3], [moji_shrink, 0.0, 0.0]); } @@ -276,13 +286,18 @@ fn kinsoku_allows(a: char, b: char) -> bool { !NO_START.contains(b) && !NO_END.contains(a) } +/// Hangul: Jamo, compatibility Jamo, the syllables, and Jamo Extended-A/B. Shared with the +/// paragraph's CJK gate so the two classifications cannot drift apart. +pub(crate) fn is_hangul(c: char) -> bool { + matches!(c as u32, 0x1100..=0x11FF | 0x3130..=0x318F | 0xA960..=0xA97F | 0xAC00..=0xD7FF) +} + /// Hangul, and Han ideographs (hanja) in Korean text: Korean is set word by word (KLREQ), so /// these letters break like Western ones, at spaces. fn korean_letter(g: &Glyph) -> bool { - let c = g.ch as u32; - let hangul = matches!(c, 0x1100..=0x11FF | 0x3130..=0x318F | 0xA960..=0xA97F | 0xAC00..=0xD7FF); - let han = matches!(c, 0x3400..=0x4DBF | 0x4E00..=0x9FFF | 0xF900..=0xFAFF | 0x20000..=0x3FFFF); - hangul || (han && g.lang.is_some_and(|t| designcraft_doc::language_subtag(t) == "ko")) + let c = g.ch; + let han = matches!(c as u32, 0x3400..=0x4DBF | 0x4E00..=0x9FFF | 0xF900..=0xFAFF | 0x20000..=0x3FFFF); + is_hangul(c) || (han && g.lang.is_some_and(|t| designcraft_doc::language_subtag(t) == "ko")) } /// Is the place between glyphs `a` and `b` one where CJK text may break (next to an ideograph, kana @@ -413,7 +428,7 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S let mut p = Item::penalty(0.0, -INF, false); p.hang = prev_hang; if let Some(prev) = i.checked_sub(1).and_then(|j| glyphs.get(j)) { - p.edge_elastic = crate::mojikumi::end_elastic(prev, sp.justify, sp.kinsoku_priority != 3); + p.edge_elastic = crate::mojikumi::end_elastic(prev, sp.justify, !sp.push_out_only()); } it.push(p); ig.push(i + 1); @@ -427,7 +442,7 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S let mut gl = Item::glue(w, st, sh); gl.hang = prev_hang; if let Some(prev) = i.checked_sub(1).and_then(|j| glyphs.get(j)) { - gl.edge_elastic = crate::mojikumi::end_elastic(prev, sp.justify, sp.kinsoku_priority != 3); + gl.edge_elastic = crate::mojikumi::end_elastic(prev, sp.justify, !sp.push_out_only()); } gl.over = sp.space_overflow(g); last_space = it.len(); @@ -440,7 +455,7 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S let mut p = Item::penalty(0.0, 0.0, false); p.hang = prev_hang; if let Some(prev) = i.checked_sub(1).and_then(|j| glyphs.get(j)) { - p.edge_elastic = crate::mojikumi::end_elastic(prev, sp.justify, sp.kinsoku_priority != 3); + p.edge_elastic = crate::mojikumi::end_elastic(prev, sp.justify, !sp.push_out_only()); } last_space = it.len(); it.push(p); @@ -462,7 +477,7 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S let (st, sh) = sp.box_elastic(g); let mut bx = Item::boxed(g.adv, st, sh); bx.hang = hang_left(g, sp) - g.moji.start; - bx.start_elastic = crate::mojikumi::start_elastic(g, sp.justify, sp.kinsoku_priority != 3); + bx.start_elastic = crate::mojikumi::start_elastic(g, sp.justify, !sp.push_out_only()); it.push(bx); ig.push(i); let next_is_space = glyphs.get(i + 1).is_none_or(|n| n.is_space()); @@ -470,12 +485,12 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S if matches!(g.ch, '-' | '\u{2010}') { let mut p = Item::penalty(0.0, 50.0, true); p.hang = right_hang(g, sp); - p.edge_elastic = crate::mojikumi::end_elastic(g, sp.justify, sp.kinsoku_priority != 3); + p.edge_elastic = crate::mojikumi::end_elastic(g, sp.justify, !sp.push_out_only()); push_inword(&mut it, &mut ig, p, i + 1, sp); } else if matches!(g.ch, '\u{2013}' | '\u{2014}' | '/') || g.break_after.unwrap_or_else(|| cjk_break(g, &glyphs[i + 1], sp)) { let mut p = Item::penalty(0.0, 0.0, false); p.hang = right_hang(g, sp); - p.edge_elastic = crate::mojikumi::end_elastic(g, sp.justify, sp.kinsoku_priority != 3); + p.edge_elastic = crate::mojikumi::end_elastic(g, sp.justify, !sp.push_out_only()); push_inword(&mut it, &mut ig, p, i + 1, sp); } else if hyph_after[i] { let hw = hyphen_width(g, &mut hy_cache); @@ -497,7 +512,7 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S let mut p = Item::penalty(0.0, -INF, false); p.hang = if n > 0 { right_hang(&glyphs[n - 1], sp) } else { 0.0 }; if let Some(g) = glyphs.last() { - p.edge_elastic = crate::mojikumi::end_elastic(g, sp.justify, sp.kinsoku_priority != 3); + p.edge_elastic = crate::mojikumi::end_elastic(g, sp.justify, !sp.push_out_only()); } it.push(p); ig.push(n); @@ -638,11 +653,19 @@ fn hyphen_allowed(it: Item, n: Node, sp: &Spacing, widths: &[f64], items: &[Item } /// Fitness class and cumulative penalty for a candidate line break. -fn break_demerits(n: Node, r: f64, pp: f64, flagged: bool, priority: u8) -> (usize, f64) { - let preference = match priority { - 1 if r > 0.0 => 4.0, - 2 if r < 0.0 => 4.0, - _ => 1.0, +fn break_demerits(n: Node, r: f64, pp: f64, flagged: bool, priority: u8, cjk: bool) -> (usize, f64) { + // 禁則 priority (JIS X 4051): push-in-first rates breaks whose line would have to be + // stretched (r > 0: the next character is pushed out) worse than ones it can reach by + // compressing (追い込み); push-out-first the other way. Adjustment-amount mode and a + // paragraph without CJK text rate every candidate by its adjustment alone. + let preference = if !cjk { + 1.0 + } else { + match priority { + 1 if r > 0.0 => 4.0, + 2 if r < 0.0 => 4.0, + _ => 1.0, + } }; let bad = preference * 100.0 * r.abs().powi(3); let lp = 10.0 + bad; @@ -770,7 +793,7 @@ fn kp_pass(input: PassInput<'_>) -> Option> { if flagged && !hyphen_allowed(it, n, sp, &sw, items, target) { continue; } - let (fit, total) = break_demerits(n, r, pp, flagged, sp.kinsoku_priority); + let (fit, total) = break_demerits(n, r, pp, flagged, sp.kinsoku_priority, sp.kinsoku_cjk); let hs = if flagged { (n.hyphens as usize + 1).min(states - 1) } else { 0 }; let slot = &mut best[fit * states + hs]; if slot.is_none_or(|(bd, _)| total < bd) { @@ -925,7 +948,7 @@ pub fn greedy( while start < n { let w = width(line).max(1.0); let mut x = -hang_left(&glyphs[start], sp) + glyphs[start].moji.start; - let mut shrink = crate::mojikumi::start_elastic(&glyphs[start], sp.justify, sp.kinsoku_priority != 3)[1]; + let mut shrink = crate::mojikumi::start_elastic(&glyphs[start], sp.justify, !sp.push_out_only())[1]; let mut last_natural: Option<(usize, bool)> = None; let mut last_ok: Option<(usize, bool)> = None; // (break position = end glyph, hyphen) let mut last_space: Option<(usize, f64)> = None; // (break position, line width there) @@ -952,9 +975,10 @@ pub fn greedy( } let hang_r = right_hang(g, sp); let adv = if g.ch == '\t' { tab(line, x, i) } else { g.adv }; - let end_shrink = crate::mojikumi::end_elastic(g, sp.justify, sp.kinsoku_priority != 3)[1]; + let end_shrink = crate::mojikumi::end_elastic(g, sp.justify, !sp.push_out_only())[1]; let available = shrink + sp.box_elastic(g).1.iter().sum::() + end_shrink; - if sp.kinsoku_priority == 2 + if sp.kinsoku_cjk + && sp.kinsoku_priority == 2 && x + adv - hang_r > w + FIT_EPS && let Some((end, hyphen)) = last_natural { diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs index bb5106208..3c468b0bf 100644 --- a/crates/compose/src/lib.rs +++ b/crates/compose/src/lib.rs @@ -1640,6 +1640,8 @@ struct NestedLineStyleInput<'a, 'b, 'font> { range: Range, props: &'a designcraft_doc::ParaProps, para_chars: &'a designcraft_doc::CharProps, + /// The paragraph has CJK text, so its 禁則 priority applies. + cjk: bool, drop_end: usize, base_leading: f64, base_cap: f64, @@ -1665,6 +1667,7 @@ fn apply_nested_line_styles(input: NestedLineStyleInput<'_, '_, '_>) { range: prange, props: pp, para_chars: base_chars, + cjk, drop_end, base_leading, base_cap, @@ -1687,7 +1690,7 @@ fn apply_nested_line_styles(input: NestedLineStyleInput<'_, '_, '_>) { let cols_here = &cols[cur.fi]; let col = span_rect(cols_here, cols_here[cur.col.min(cols_here.len() - 1)], pp.span_columns); let col = SplitCfg::of(pp).map_or(col, |c| c.sub_rect(col, 0)); - let spacing = spacing_for(pp, base_chars.size, story); + let spacing = spacing_for(pp, base_chars.size, story, cjk); let mut lines: Vec<(std::ops::Range, String)> = Vec::new(); for _ in 0..3 { // The same drop cap, spacing, hyphenation and breaker as the layout below. @@ -2795,6 +2798,10 @@ fn shape_paragraph(input: ParagraphShapeInput<'_, '_>) -> ShapedParagraph { }, &mut table, ); + // The paragraph's script decides whether its 禁則 priority applies. Classify the shaped + // glyphs before split_drop_cap removes the drop cap and prepend_label adds the list + // label: a CJK drop cap is part of the paragraph, a generated label is not. + let cjk = paragraph_is_cjk(&sp.glyphs); // An empty paragraph has no glyphs of its own, so its only addressable character is its // hidden paragraph mark (the `\n` just past `prange.end`). A leading/size override applied // there (e.g. selecting the pilcrow to adjust line height) still needs to set the line's @@ -2827,6 +2834,7 @@ fn shape_paragraph(input: ParagraphShapeInput<'_, '_>) -> ShapedParagraph { range: prange.clone(), props: pp, para_chars: base_chars, + cjk, drop_end, base_leading, base_cap, @@ -2941,7 +2949,7 @@ fn shape_paragraph(input: ParagraphShapeInput<'_, '_>) -> ShapedParagraph { let mut hyph_after = hyphenation_points(&story.text, &glyphs, pp, hyph_exceptions, &foreign_ranges(doc, story, prange.clone(), base_chars)); suppress_hyphens(&mut hyph_after, &glyphs, suppressed_hyphens); let base_size = base_chars.size; - let spacing = spacing_for(pp, base_size, story); + let spacing = spacing_for(pp, base_size, story, cjk); // An empty paragraph takes its height from its paragraph mark's character format. let mark_chars = if glyphs.is_empty() { doc.styles.resolve_char(base_chars, story.char_format_of(prange.start)) } else { base_chars.clone() }; let mark_leading = match mark_chars.leading { @@ -3942,8 +3950,9 @@ fn optical_for(pp: &ParaProps, story: &Story) -> (bool, Option) { } } -/// Breaker parameters from the paragraph's settings. -fn spacing_for(pp: &ParaProps, base_size: f64, story: &Story) -> Spacing { +/// Breaker parameters from the paragraph's settings; `cjk` says the paragraph has CJK text (so +/// its 禁則 priority applies). +fn spacing_for(pp: &ParaProps, base_size: f64, story: &Story, cjk: bool) -> Spacing { let (optical, optical_size) = optical_for(pp, story); Spacing { justify: pp.align.is_justified(), @@ -3965,15 +3974,51 @@ fn spacing_for(pp: &ParaProps, base_size: f64, story: &Story) -> Spacing { optical, optical_size, korean_char_breaks: pp.korean_char_breaks, + kinsoku_cjk: cjk, + // JIS X 4051 禁則処理: the empty value is InDesign's default and the UI's "Push In First", + // and 調整量優先 (adjustment amount) is the breaker's minimum-adjustment mode 0. kinsoku_priority: match pp.kinsoku_type.as_str() { - "KinsokuPushInFirst" => 1, + "" | "KinsokuPushInFirst" => 1, "KinsokuPushOutFirst" => 2, "KinsokuPushOutOnly" => 3, + "KinsokuPrioritizeAdjustmentAmount" => 0, _ => 0, }, } } +/// Whether the paragraph's own shaped glyphs contain CJK text, the gate for its 禁則 priority. +/// Called before `split_drop_cap` and `prepend_label`, so a drop cap counts and generated list +/// and story labels do not. A text variable or page number keeps its marker in [`Glyph::ch`], so +/// its replacement text is what the paragraph shows; inspect that too. +fn paragraph_is_cjk(glyphs: &[Glyph]) -> bool { + glyphs.iter().any(|g| is_cjk_char(g.ch) || g.generated_text.as_deref().is_some_and(|text| text.chars().any(is_cjk_char))) +} + +/// Is `c` CJK text (the scripts the 禁則 line-start/line-end rules are about): ideographs, kana, +/// CJK punctuation and full/half-width forms, bopomofo, Hangul. +fn is_cjk_char(c: char) -> bool { + // Hangul comes from the breaker's classification (Jamo including Extended-A/B, syllables), + // so the two sets agree. Bopomofo is 0x3100..=0x312F; 0x3130..=0x318F is Hangul. + breaker::is_hangul(c) + || matches!( + c as u32, + 0x2E80..=0x2FFF + | 0x3000..=0x30FF + | 0x3100..=0x312F + | 0x3190..=0x33FF + | 0x3400..=0x4DBF + | 0x4E00..=0x9FFF + | 0xA000..=0xA4CF + | 0xF900..=0xFAFF + | 0xFE30..=0xFE4F + | 0xFF00..=0xFFEF + // Extension B and later (U+20000..): the same range as the breaker's Han check, + // so the kinsoku settings apply to a paragraph of supplementary-plane text alone. + | 0x20000..=0x3FFFF + ) +} + /// Desired word spacing, letter spacing and glyph scaling apply to every line (any alignment). fn apply_desired_spacing(glyphs: &mut [Glyph], pp: &ParaProps) { for i in 0..glyphs.len() { @@ -5226,7 +5271,7 @@ fn layout_line( let mut offset = 0.0; // Kashidas: in justified Arabic, the joins of words take the extra length first. let kashidas = if justify_this { allocate_kashidas(&line, &mut add, pp, seg, &mut extra) } else { Vec::new() }; - if (justify_this || squeeze_last) && (extra >= 0.0 || sp.kinsoku_priority != 3) { + if (justify_this || squeeze_last) && (extra >= 0.0 || !sp.push_out_only()) { extra = mojikumi::distribute(line.get_mut(seg..).unwrap_or_default(), extra, add.get_mut(seg..).unwrap_or_default()); } if (justify_this || squeeze_last) && !spaces.is_empty() { diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index fb8290b69..8e03b35d0 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -7005,6 +7005,224 @@ fn mojikumi_kinsoku_push_in_and_push_out_select_different_legal_breaks() { } } +/// A paragraph that can compress: the Mojikumi table gives its ideographs shrinkable space, so +/// a line ending before a 禁則 boundary can be pulled in (追い込み) or pushed out (追い出し). +fn kinsoku_push_pull_doc(kind: Option<&str>) -> (Document, StoryId) { + kinsoku_push_pull_doc_text("甲乙丙丁戊己庚辛", kind) +} + +/// Same paragraph with a different body: used for the supplementary-plane ideographs, whose +/// CJK classification must match the breaker's Han set. +fn kinsoku_push_pull_doc_text(text: &str, kind: Option<&str>) -> (Document, StoryId) { + use designcraft_doc::cjk::{MojikumiAki, MojikumiTable}; + let (mut d, sid, _) = doc_with( + text, + Rect::new(0.0, 0.0, 45.0, 300.0), + ParaAttrs { + composer: Some(Composer::SingleLine), + mojikumi: Some("MojikumiTable/Compress".into()), + kinsoku_type: kind.map(str::to_string), + ..Default::default() + }, + ); + Arc::make_mut(&mut d.styles).mojikumi_tables.push(MojikumiTable { + name: "Compress".into(), + based_on: "SimpChineseDefault".into(), + overrides: vec![MojikumiAki { + target_class: 12, + side_class: 12, + after: true, + minimum: 0.0, + desired: 1.0, + maximum: 2.0, + priority: 1, + ..Default::default() + }], + }); + (d, sid) +} + +fn first_line_end(cs: &ComposedStory) -> (usize, f64) { + let line = all_lines(cs)[0]; + (line.range.end, line.end_x) +} + +#[test] +fn default_kinsoku_type_composes_as_push_in_first() { + // The empty value is InDesign's default (and what the UI's "Push In First" stores): it must + // behave as push-in-first, not as the minimum-adjustment mode. + let (d, sid) = kinsoku_push_pull_doc(None); + let default = compose_story(&d, sid, &ComposeOptions::default()); + let (d, sid) = kinsoku_push_pull_doc(Some("KinsokuPushInFirst")); + let push_in = compose_story(&d, sid, &ComposeOptions::default()); + let (d, sid) = kinsoku_push_pull_doc(Some("KinsokuPushOutFirst")); + let push_out = compose_story(&d, sid, &ComposeOptions::default()); + assert_eq!(first_line_end(&default).0, first_line_end(&push_in).0); + assert!(first_line_end(&default).0 > first_line_end(&push_out).0, "{:?} vs {:?}", first_line_end(&default), first_line_end(&push_out)); +} + +#[test] +fn push_out_only_leaves_the_line_end_uncompressed() { + // Push-out-only turns the 追い込み compression off: the line cannot be squeezed to reach a + // break, so it ends earlier (and never past the measure). + let (d, sid) = kinsoku_push_pull_doc(None); + let push_in = compose_story(&d, sid, &ComposeOptions::default()); + let (d, sid) = kinsoku_push_pull_doc(Some("KinsokuPushOutOnly")); + let push_out_only = compose_story(&d, sid, &ComposeOptions::default()); + assert!(first_line_end(&push_out_only).0 <= first_line_end(&push_in).0); + for cs in [&push_in, &push_out_only] { + for line in all_lines(cs) { + assert!(line.end_x <= line.x1 + 0.01, "line end {} past the measure {}", line.end_x, line.x1); + } + } + // Push-out-only's first line is not compressed to the measure: it ends short of it. + let (_, short) = first_line_end(&push_out_only); + let line = all_lines(&push_out_only)[0]; + assert!(line.x1 - short > 0.5, "no compression: end {short} vs measure {}", line.x1); +} + +#[test] +fn supplementary_ideograph_paragraph_keeps_the_kinsoku_gate() { + // Extension B 以降の漢字 (U+20000..) は breaker では Han として扱われる。段落全体の CJK 判定 + // (is_cjk_char) がそこを落とすと、補助面だけの段落で禁則の指定が効かなくなる。 + // 補助面だけの本文でも、追い込みと追い出しで行末が変わる (指定が効いている) こと。 + let first_end = |text: &str, kind: Option<&str>| { + let (d, sid) = kinsoku_push_pull_doc_text(text, kind); + first_line_end(&compose_story(&d, sid, &ComposeOptions::default())).0 + }; + let bmp = "甲乙丙丁戊己庚辛"; + let sup = "\u{20000}\u{20001}\u{20002}\u{20003}\u{20004}\u{20005}\u{20006}\u{20007}"; + // 対照: BMP の漢字だけの段落では、追い込みと追い出しで行末が変わる + assert_ne!(first_end(bmp, None), first_end(bmp, Some("KinsokuPushOutOnly")), "BMP の対照で禁則の指定が効いていない"); + // 補助面だけの段落でも同じように効く (判定から漏れると、どちらも同じ行末になる) + assert_ne!(first_end(sup, None), first_end(sup, Some("KinsokuPushOutOnly")), "補助面だけの段落で禁則の指定が効いていない"); + // 判定そのもの (Extension B の最初と最後、Ext G の終わり) + assert!(is_cjk_char('\u{20000}')); + assert!(is_cjk_char('\u{2A6DF}')); + assert!(is_cjk_char('\u{3FFFF}')); + assert!(!is_cjk_char('A')); +} + +#[test] +fn hangul_jamo_extended_blocks_count_as_cjk() { + // `is_cjk_char` takes Hangul from the breaker's classification, so the syllables and the + // Jamo Extended-A/B blocks count as well: a Hangul-only paragraph keeps its 禁則 priority. + for c in ['\u{A960}', '\u{A97F}', '\u{D7B0}', '\u{D7FF}'] { + assert!(is_cjk_char(c), "U+{:04X} is Hangul", c as u32); + } + assert!(is_cjk_char('\u{3105}'), "bopomofo is still CJK"); +} + +/// Shape `text` as a one-paragraph story with the default font and styles: the glyph list the +/// composition classifies before `split_drop_cap` and `prepend_label`. +fn shape_for_script_test(text: &str) -> Vec { + shape_for_script_test_with(text, &SubstCtx::default()) +} + +/// Same, with text variables resolved through `sub`. +fn shape_for_script_test_with(text: &str, sub: &SubstCtx) -> Vec { + let db = designcraft_fonts::FontDb::global().scoped(0); + let mut styles = Vec::new(); + let mut missing = std::collections::HashMap::new(); + let mut table = shape::StyleTable { styles: &mut styles, missing: &mut missing }; + let mut story = Story::new(StoryId(0)); + story.insert(0, text); + let doc_styles = designcraft_doc::Styles::default(); + shape::shape_para( + shape::ShapeInput { + db: &db, + styles: &doc_styles, + story: &story, + range: 0..text.len(), + para_chars: &designcraft_doc::CharProps::default(), + auto_leading: shape::TypeEnv { auto_leading: 1.2, adv: Default::default(), glyph_fallback: true }, + sub, + nested: &[], + grep: &[], + lines: &[], + drop_cap: (0, ""), + }, + &mut table, + ) + .glyphs +} + +#[test] +fn paragraph_script_counts_a_drop_cap_but_not_a_generated_label() { + // The 禁則 gate reads the paragraph's own shaped glyphs. The drop cap has not been split off + // yet, so a CJK one still makes the paragraph CJK. + assert!(paragraph_is_cjk(&shape_for_script_test("漢abc"))); + // A generated list label is prepended after the gate, so it is not part of the paragraph. + let mut glyphs = shape_for_script_test("abc"); + assert!(!paragraph_is_cjk(&glyphs)); + let db = designcraft_fonts::FontDb::global().scoped(0); + let mut styles = Vec::new(); + let mut missing = std::collections::HashMap::new(); + let mut table = shape::StyleTable { styles: &mut styles, missing: &mut missing }; + let n = prepend_label( + &db, + &mut glyphs, + "第1章", + 0, + &designcraft_doc::CharProps::default(), + shape::TypeEnv { auto_leading: 1.2, adv: Default::default(), glyph_fallback: true }, + &mut table, + ); + assert!(n > 0); + // The label's glyphs are CJK, so a gate computed after prepend_label would flip the answer. + assert!(glyphs.iter().take(n).any(|g| is_cjk_char(g.ch))); +} + +#[test] +fn paragraph_script_reads_a_text_variable_replacement() { + // A text variable's glyphs keep the source marker in `ch`; the paragraph shows the replacement, + // so a CJK replacement makes it CJK text even though no source character is. + let marker = designcraft_doc::vars::var_char(0).expect("variable 0"); + let text = format!("{marker}abc"); + let sub = SubstCtx { vars: Arc::new(vec!["漢字".into()]), ..Default::default() }; + let glyphs = shape_for_script_test_with(&text, &sub); + assert!(!glyphs.iter().any(|g| is_cjk_char(g.ch)), "the source is only the marker and Latin text"); + assert!(glyphs.iter().any(|g| g.generated_text.as_deref().is_some_and(|t| t.chars().any(is_cjk_char)))); + assert!(paragraph_is_cjk(&glyphs), "the CJK replacement is the paragraph's text"); +} + +#[test] +fn kinsoku_priority_modes_map_to_breaker_policies() { + let mut pp = ParaProps::default(); + let story = designcraft_doc::Story::new(designcraft_doc::StoryId::default()); + for (kind, priority) in [ + ("", 1u8), + ("KinsokuPushInFirst", 1), + ("KinsokuPushOutFirst", 2), + ("KinsokuPushOutOnly", 3), + ("KinsokuPrioritizeAdjustmentAmount", 0), + ("KinsokuSomethingElse", 0), + ] { + pp.kinsoku_type = kind.to_string(); + let sp = spacing_for(&pp, 12.0, &story, true); + assert_eq!(sp.kinsoku_priority, priority, "{kind}"); + assert!(sp.kinsoku_cjk, "{kind}"); + assert!(!spacing_for(&pp, 12.0, &story, false).kinsoku_cjk, "{kind}: Latin text is not biased"); + } +} + +#[test] +fn latin_paragraphs_are_not_biased_by_the_kinsoku_priority() { + // A justified Latin paragraph composes the same with push-in-first, push-out-first and + // minimum adjustment: the policy is about CJK 禁則, and a Latin paragraph has none. + let compose_with = |kind: &str| { + let (mut d, sid, _) = + doc_with(LOREM, Rect::new(0.0, 0.0, 200.0, 500.0), ParaAttrs { align: Some(Align::FullyJustified), ..Default::default() }); + d.story_mut(sid).unwrap().paras[0].para.kinsoku_type = Some(kind.to_string()); + compose_story(&d, sid, &ComposeOptions::default()) + }; + let ends = |cs: &ComposedStory| all_lines(cs).iter().map(|l| l.range.end).collect::>(); + let push_in = ends(&compose_with("")); + assert_eq!(ends(&compose_with("KinsokuPushInFirst")), push_in); + assert_eq!(ends(&compose_with("KinsokuPushOutFirst")), push_in); + assert_eq!(ends(&compose_with("KinsokuPrioritizeAdjustmentAmount")), push_in); +} + #[test] fn mojikumi_priority_and_elastic_line_start_are_used_in_placement() { use designcraft_doc::cjk::{MojikumiAki, MojikumiTable}; diff --git a/crates/doc/src/attrs/para_attrs.rs b/crates/doc/src/attrs/para_attrs.rs index 0077c8e12..b4f356b10 100644 --- a/crates/doc/src/attrs/para_attrs.rs +++ b/crates/doc/src/attrs/para_attrs.rs @@ -113,7 +113,10 @@ attr_set! { kinsoku: Option = None => "deserialize_explicit_value", /// Preserved spacing-table reference; compatibility audit reports unsupported composition. mojikumi: String = String::new(), - /// Preserved priority policy; the current breaker does not implement these priorities. + /// JIS X 4051 禁則処理 type, applied by both composers to CJK text: empty (the default) + /// and `KinsokuPushInFirst` compose as push-in-first, `KinsokuPushOutFirst`, + /// `KinsokuPushOutOnly` and `KinsokuPrioritizeAdjustmentAmount` (minimum adjustment) as + /// their names say. kinsoku_type: String = String::new(), kinsoku_hang: crate::cjk::KinsokuHang = crate::cjk::KinsokuHang::None, bunri_kinshi: bool = false, diff --git a/docs/cjk-typography.md b/docs/cjk-typography.md index 49d350b88..e46fb3c51 100644 --- a/docs/cjk-typography.md +++ b/docs/cjk-typography.md @@ -1,6 +1,6 @@ # CJK import and composition -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-07 · **Change:** trivial (status line and revision history added; content unchanged in the 2026-10-10 review) · **Target:** Adobe InDesign 2026 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (the JIS X 4051 禁則処理 push-in/push-out priority modes are applied by both composers over CJK text: the empty default and Push In First compose as push-in-first, Push Out First, Push Out Only and adjustment-amount as their names say; Latin paragraphs are not biased; preflight reports only unknown values) · **Target:** Adobe InDesign 2026 Task: CJK1. This is incremental support, not a claim of InDesign composition parity. @@ -27,6 +27,7 @@ invalidation. Font listing, replacement and preflight resolve component fonts. | Tsume | Compress nonnegative glyph sidebearings without scaling ink; full compression regression | | Jidori | Fixed group width with protected internal boundaries and justification advances; both composers tested | | Kinsoku sets | Custom character sets and independent common punctuation defaults for named sets; explicit disabling retained | +| Kinsoku priorities | The four JIS X 4051 禁則処理 types, applied by both composers to CJK text: push-in-first (the empty default, as in InDesign) and push-out-first weight the break candidates by the adjustment direction, push-out-only turns the end-of-line compression off, and adjustment-amount (調整量優先) takes the candidate with the smallest adjustment; Latin paragraphs are not biased. Regressions pin the breaks, the line-end fits and the mode mapping | | Bunri kinshi / rensuuji | Protect repeated dash/ellipsis pairs and numeric sequences | | Hanging punctuation | Regular/forced edge allowance integrated with line breaking and placement | | Ideographic spaces | Paragraph switch controls whether fullwidth spaces participate in word spacing | @@ -39,9 +40,14 @@ invalidation. Font listing, replacement and preflight resolve component fonts. - **Mojikumi**: definitions, base-set names, all override rows and paragraph references survive import/export. The numeric character-class spacing tables and their compression priorities are **not executed**. Preflight reports this explicitly. -- **Kinsoku push-in/push-out priorities**: retained, but not used in candidate - selection. Preflight reports this explicitly. Boundary prohibition alone is not - equivalent to implementing this priority policy. +- **Kinsoku priority policy** is applied as a line-cost weighting in the total-fit composer (and + as the single-line composer's early-break rule): push-in-first rates breaks that would have to + stretch the line worse than ones it can reach by compressing, push-out-first the other way, + push-out-only leaves no compression at the line end, and adjustment-amount rates every + candidate by its adjustment alone. It is a preference within the composer's other costs, not a + hard "always 追い込み before 追い出し" rule. Preflight reports only values it does not + know: the four JIS X 4051 modes are not reported as unapplied. Boundary prohibition, hanging + punctuation and the named sets are unchanged. - Named kinsoku defaults are clean-room common punctuation rules, not verified reproductions of every vendor preset. Explicit custom tables take precedence. - ICF alignment currently uses font ascender/descender metrics; exact ideographic @@ -69,5 +75,6 @@ are not committed as fixtures. | Date | Change | Summary | |---|---|---| +| 2026-10-22 | minor | 禁則処理 priority modes applied: the empty default and Push In First compose as push-in-first (as InDesign's Japanese default), Push Out First, Push Out Only and adjustment-amount (minimum adjustment) as their names say, for CJK text; Latin paragraphs are not biased; preflight reports only unknown values; breaks, line-end fits and the mode mapping regression-tested | | 2026-10-10 | trivial | Reviewed in the full re-measure; status line and this table added; summarized in [typography-parity.md](typography-parity.md) and [localization-parity.md](localization-parity.md) | | 2026-10-07 | major | Created: CJK1: IDML CJK import and composition |