diff --git a/crates/text/src/layout/flow.rs b/crates/text/src/layout/flow.rs index f028d47898..3999401a05 100644 --- a/crates/text/src/layout/flow.rs +++ b/crates/text/src/layout/flow.rs @@ -13,7 +13,8 @@ use super::{ is_cjk, is_word_space, kashida, mark_word_ends, no_line_end, no_line_start, style_metrics, }; use crate::justify::is_format_control; -use crate::shape::{keep_inseparable_pairs, keep_korean_words, repeated_char, shape_range}; +use crate::shape::{keep_inseparable_pairs, keep_korean_words, repeat_base, repeated_char, shape_range}; +use std::cell::Cell; /// Does paragraph `para` push characters in (Kinsoku Shori Type Push In First or Push Out First, /// with kinsoku on)? @@ -216,6 +217,9 @@ struct ParagraphData<'a, 'text> { /// Kurikaeshi Moji Shori: the character before a `continued` paragraph in the frames before /// ([`TextObject::first_para_lead`]). lead: Option, + /// Kurikaeshi Moji Shori: the byte the paragraph's text has been scanned to for the character a + /// repeat mark could stand for, and that character so far ([`ParagraphData::repeat_base_before`]). + repeat_scan: Cell<(usize, Option)>, sg: Vec, compressed: Vec, wakan: Vec, @@ -234,6 +238,21 @@ struct ParagraphData<'a, 'text> { regions: Option<&'a [Region]>, } +impl ParagraphData<'_, '_> { + /// Kurikaeshi Moji Shori: the character a repeat mark at byte `at` of `text` could stand for, + /// the last one before it in the paragraph that [`repeat_base`] takes, or else the paragraph's + /// `lead`. Lines are laid out in order, so each call scans on from where the call before it + /// stopped; a byte before that starts the scan again at the paragraph's start. None when `at` + /// does not fall on a character boundary after the paragraph's start. + fn repeat_base_before(&self, text: &str, at: usize) -> Option> { + let (from, base) = self.repeat_scan.get(); + let (from, base) = if from <= at { (from, base) } else { (self.pr.start, self.lead) }; + let base = repeat_base(text.get(from..at)?).or(base); + self.repeat_scan.set((at, base)); + Some(base) + } +} + fn prepare_paragraph<'a, 'text>( cx: &mut Ctx<'text>, pen: &mut Pen<'_>, @@ -327,14 +346,16 @@ fn prepare_paragraph<'a, 'text>( let zone = if cx.opts.composer.unwrap_or(para.composer).is_single_line() { para.hyphenation.zone } else { 0.0 }; let hyphen_limit = usize::try_from(para.hyphenation.hyphen_limit).unwrap_or(usize::MAX); let mirror = mirrors_indents(para, rtl, cx.vertical); + let lead = t.first_para_lead.filter(|_| continued); ParagraphData { para, + repeat_scan: Cell::new((pr.start, lead)), pr, marker_text, bidi, rtl, continued, - lead: t.first_para_lead.filter(|_| continued), + lead, sg, compressed, wakan, @@ -1277,18 +1298,18 @@ fn emit_line_glyph( } /// Kurikaeshi Moji Shori (Paragraph panel menu): the glyph drawn in place of `g`, the first glyph of -/// a line of the paragraph starting at byte `para_start`, when `g` is a repeat mark, and the -/// character it shows: the character [`repeated_char`] gives, shaped in the mark's style with the -/// type's features (so vertical type gets the font's vertical forms), from the same face, with the -/// mark's advance and place, so the line breaks stay as they are. None when `g` is no repeat mark, -/// belongs to a tate-chu-yoko or warichu block, nothing before it in the paragraph fits, or the face -/// draws the character with no glyph or with more than one. `lead` stands before a paragraph that -/// continues from the frame before ([`TextObject::first_para_lead`]). -fn repeat_mark_glyph(cx: &Ctx<'_>, para_start: usize, lead: Option, g: &SGlyph) -> Option<(SGlyph, char)> { - if g.tcy.is_some() || g.wari.is_some() || g.inline.is_some() { +/// a line of `paragraph`, when `g` is a repeat mark, and the character it shows: the character +/// [`repeated_char`] gives, shaped in the mark's style with the type's features (so vertical type +/// gets the font's vertical forms), from the same face, with the mark's advance and place, so the +/// line breaks stay as they are. None when `g` is no repeat mark, belongs to a tate-chu-yoko or +/// warichu block, nothing before it in the paragraph fits, or the face draws the character with no +/// glyph or with more than one. The paragraph's `lead` stands before a paragraph that continues +/// from the frame before ([`TextObject::first_para_lead`]). +fn repeat_mark_glyph(cx: &Ctx<'_>, paragraph: &ParagraphData<'_, '_>, g: &SGlyph) -> Option<(SGlyph, char)> { + if g.tcy.is_some() || g.wari.is_some() || g.inline.is_some() || !matches!(g.ch, '々' | 'ゝ' | 'ゞ' | 'ヽ' | 'ヾ') { return None; } - let c = repeated_char(lead, cx.text.get(para_start..g.byte)?, g.ch)?; + let c = repeated_char(paragraph.repeat_base_before(cx.text, g.byte)?, "", g.ch)?; let mut buf = [0; 4]; let s: &str = c.encode_utf8(&mut buf); let mut shaped = Vec::with_capacity(1); @@ -1305,7 +1326,7 @@ fn line_start_repeat(cx: &mut Ctx<'_>, paragraph: &ParagraphData<'_, '_>, first: return None; } let g = paragraph.sg.get(first)?; - let (glyph, c) = repeat_mark_glyph(cx, paragraph.pr.start, paragraph.lead, g)?; + let (glyph, c) = repeat_mark_glyph(cx, paragraph, g)?; cx.out.repeat_marks.push((g.byte, c)); Some(glyph) } diff --git a/crates/text/src/shape.rs b/crates/text/src/shape.rs index 9d09ce076e..097b056d7d 100644 --- a/crates/text/src/shape.rs +++ b/crates/text/src/shape.rs @@ -456,9 +456,23 @@ pub fn inseparable(a: char, b: char) -> bool { /// paragraph continues from the frame before the result for the text before it /// (`TextObject::first_para_lead`). pub fn repeat_base(text: &str) -> Option { + #[cfg(test)] + REPEAT_SCANNED.with(|c| c.set(c.get() + text.len())); text.chars().rev().find(|&c| !matches!(c, '々' | 'ゝ' | 'ゞ' | 'ヽ' | 'ヾ' | '\u{2028}' | '\u{3099}' | '\u{309A}')) } +#[cfg(test)] +thread_local! { + /// Tests: how many bytes of text [`repeat_base`] was given on this thread. + static REPEAT_SCANNED: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// Tests: how many bytes of text [`repeat_base`] has been given on this thread. +#[cfg(test)] +pub(crate) fn repeat_scanned() -> usize { + REPEAT_SCANNED.get() +} + /// Kurikaeshi Moji Shori: the character the repeat mark `mark` stands for after the text `before` /// it, or None when `mark` is not a repeat mark or nothing before it fits. `lead` stands before /// `before` (the text of a threaded paragraph in the frames before). VectorCraft's rules: the diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rs index 477c3e3392..b15b0757be 100644 --- a/crates/text/src/tests.rs +++ b/crates/text/src/tests.rs @@ -2810,6 +2810,28 @@ fn kurikaeshi_moji_shori_draws_the_repeated_character_at_a_line_start() { assert_eq!(gid_of(&on, "々す\n".len()), glyph('々'), "the next paragraph's first mark keeps its glyph"); } +/// Kurikaeshi Moji Shori: a paragraph with a repeat mark at the start of each of many lines is laid +/// out in one scan of its text, not one scan of everything before each line. +#[test] +fn kurikaeshi_moji_shori_scans_a_paragraph_once() { + use crate::shape::repeat_scanned; + use crate::test_fonts::{REPEAT_MARK_FAMILY, repeat_mark_font}; + let db = FontDb::with_font_dirs(vec![]); + db.add_font(repeat_mark_font().expect("test font")); + let st = CharStyle { size: 20.0, font_family: REPEAT_MARK_FAMILY.into(), font_style: "Regular".into(), ..CharStyle::default() }; + let face = db.style_face(&st).expect("face"); + let text = format!("時{}", "\u{2028}々".repeat(2000)); + let mut t = TextObject::point(Point::ZERO, &text, st); + t.para.kurikaeshi_moji_shori = true; + let before = repeat_scanned(); + let l = layout(&db, &t); + let scanned = repeat_scanned() - before; + assert!(scanned <= 2 * text.len(), "{scanned} bytes scanned for a paragraph of {}", text.len()); + // Every line's 々 is still drawn as 時. + assert_eq!(l.repeat_marks.len(), 2000); + assert!(l.glyphs.iter().filter(|g| g.byte > 0 && text.get(g.byte..).is_some_and(|s| s.starts_with('々'))).all(|g| g.gid == face.glyph_for('時'))); +} + /// Bunri-Kinshi: a justified line spreads its room over the gaps between its CJK characters but not /// over the gap inside a vertical kana repeat mark, and still fills its width. #[test]