From 662bccc42d4a6ebfb6ff3a75b34900aedd414064 Mon Sep 17 00:00:00 2001 From: +merlan #flirora Date: Sat, 15 Feb 2025 01:00:00 -0500 Subject: [PATCH] Add `idx` field to `Run` --- crates/typst-layout/src/inline/prepare.rs | 18 ++++++++++++++++-- crates/typst-layout/src/inline/shaping.rs | 4 +++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/typst-layout/src/inline/prepare.rs b/crates/typst-layout/src/inline/prepare.rs index e7bef812c..85461c45a 100644 --- a/crates/typst-layout/src/inline/prepare.rs +++ b/crates/typst-layout/src/inline/prepare.rs @@ -7,6 +7,7 @@ use super::*; pub struct Run<'a> { pub item: Item<'a>, pub range: Range, + pub idx: usize, } /// A representation in which children are already layouted and text is already @@ -79,6 +80,7 @@ pub fn prepare<'a>( let mut cursor = 0; let mut items = Vec::with_capacity(segments.len()); + let mut next_idx = 0; // Shape the text to finalize the items. for segment in segments { @@ -88,9 +90,20 @@ pub fn prepare<'a>( match segment { Segment::Text(_, styles) => { - shape_range(&mut items, engine, text, &bidi, range, styles); + shape_range( + &mut items, + engine, + text, + &bidi, + range, + styles, + &mut next_idx, + ); + } + Segment::Item(item) => { + items.push(Run { range, item, idx: next_idx }); + next_idx += 1; } - Segment::Item(item) => items.push(Run { range, item }), } cursor = end; @@ -105,6 +118,7 @@ pub fn prepare<'a>( if config.cjk_latin_spacing { add_cjk_latin_spacing(&mut items); } + dbg!(&items); Ok(Preparation { config, diff --git a/crates/typst-layout/src/inline/shaping.rs b/crates/typst-layout/src/inline/shaping.rs index 3f8bf140e..78c5789a0 100644 --- a/crates/typst-layout/src/inline/shaping.rs +++ b/crates/typst-layout/src/inline/shaping.rs @@ -599,6 +599,7 @@ pub fn shape_range<'a>( bidi: &BidiInfo<'a>, range: Range, styles: StyleChain<'a>, + next_idx: &mut usize, ) { let script = TextElem::script_in(styles); let lang = TextElem::lang_in(styles); @@ -607,7 +608,8 @@ pub fn shape_range<'a>( let dir = if level.is_ltr() { Dir::LTR } else { Dir::RTL }; let shaped = shape(engine, range.start, &text[range.clone()], styles, dir, lang, region); - items.push(Run { range, item: Item::Text(shaped) }); + items.push(Run { range, item: Item::Text(shaped), idx: *next_idx }); + *next_idx += 1; }; let mut prev_level = BidiLevel::ltr();