diff --git a/crates/typst-layout/src/inline/line.rs b/crates/typst-layout/src/inline/line.rs index 2e0e537cd..7ba1e5e2a 100644 --- a/crates/typst-layout/src/inline/line.rs +++ b/crates/typst-layout/src/inline/line.rs @@ -26,6 +26,7 @@ const LINE_SEPARATOR: char = '\u{2028}'; // We use LS to distinguish justified b /// first and last one since they may be broken apart by the start or end of the /// line, respectively. But even those can partially reuse previous results when /// the break index is safe-to-break per rustybuzz. +#[derive(Debug)] pub struct Line<'a> { /// The items the line is made of. pub items: Items<'a>, @@ -219,7 +220,7 @@ fn collect_items<'a>( // Add fallback text to expand the line height, if necessary. if !items.iter().any(|item| matches!(item, Item::Text(_))) { if let Some(fallback) = fallback { - items.push(fallback); + items.push(fallback, usize::MAX); } } @@ -273,7 +274,7 @@ fn collect_range<'a>( for run in p.slice(range.clone()) { // All non-text items are just kept, they can't be split. let Item::Text(shaped) = &run.item else { - items.push(&run.item); + items.push(&run.item, run.idx); continue; }; let subrange = &run.range; @@ -294,10 +295,10 @@ fn collect_range<'a>( } else if split { // When the item is split in half, reshape it. let reshaped = shaped.reshape(engine, sliced); - items.push(Item::Text(reshaped)); + items.push(Item::Text(reshaped), run.idx); } else { // When the item is fully contained, just keep it. - items.push(&run.item); + items.push(&run.item, run.idx); } } } @@ -628,7 +629,7 @@ fn overhang(c: char) -> f64 { } /// A collection of owned or borrowed inline items. -pub struct Items<'a>(Vec>); +pub struct Items<'a>(Vec>); impl<'a> Items<'a> { /// Create empty items. @@ -637,33 +638,33 @@ impl<'a> Items<'a> { } /// Push a new item. - pub fn push(&mut self, entry: impl Into>) { - self.0.push(entry.into()); + pub fn push(&mut self, entry: impl Into>, idx: usize) { + self.0.push(IndexedItemEntry { item: entry.into(), idx }); } /// Iterate over the items pub fn iter(&self) -> impl Iterator> { - self.0.iter().map(|item| &**item) + self.0.iter().map(|item| &*item.item) } /// Access the first item. pub fn first(&self) -> Option<&Item<'a>> { - self.0.first().map(|item| &**item) + self.0.first().map(|item| &*item.item) } /// Access the last item. pub fn last(&self) -> Option<&Item<'a>> { - self.0.last().map(|item| &**item) + self.0.last().map(|item| &*item.item) } /// Access the first item mutably, if it is text. pub fn first_text_mut(&mut self) -> Option<&mut ShapedText<'a>> { - self.0.first_mut()?.text_mut() + self.0.first_mut()?.item.text_mut() } /// Access the last item mutably, if it is text. pub fn last_text_mut(&mut self) -> Option<&mut ShapedText<'a>> { - self.0.last_mut()?.text_mut() + self.0.last_mut()?.item.text_mut() } /// Reorder the items starting at the given index to RTL. @@ -674,12 +675,17 @@ impl<'a> Items<'a> { impl<'a> FromIterator> for Items<'a> { fn from_iter>>(iter: I) -> Self { - Self(iter.into_iter().collect()) + Self( + iter.into_iter() + .enumerate() + .map(|(idx, item)| IndexedItemEntry { item, idx }) + .collect(), + ) } } impl<'a> Deref for Items<'a> { - type Target = Vec>; + type Target = Vec>; fn deref(&self) -> &Self::Target { &self.0 @@ -698,6 +704,13 @@ impl Debug for Items<'_> { } } +/// An item accompanied by its position within a line. +#[derive(Debug)] +pub struct IndexedItemEntry<'a> { + pub item: ItemEntry<'a>, + pub idx: usize, +} + /// A reference to or a boxed item. /// /// This is conceptually similar to a [`Cow<'a, Item<'a>>`][std::borrow::Cow], diff --git a/crates/typst-layout/src/inline/linebreak.rs b/crates/typst-layout/src/inline/linebreak.rs index 5e900da0d..14ff37937 100644 --- a/crates/typst-layout/src/inline/linebreak.rs +++ b/crates/typst-layout/src/inline/linebreak.rs @@ -110,10 +110,10 @@ pub fn linebreak<'a>( p: &'a Preparation<'a>, width: Abs, ) -> Vec> { - match p.config.linebreaks { + dbg!(match p.config.linebreaks { Linebreaks::Simple => linebreak_simple(engine, p, width), Linebreaks::Optimized => linebreak_optimized(engine, p, width), - } + }) } /// Performs line breaking in simple first-fit style. This means that we build diff --git a/crates/typst-layout/src/inline/prepare.rs b/crates/typst-layout/src/inline/prepare.rs index 85461c45a..aec90c501 100644 --- a/crates/typst-layout/src/inline/prepare.rs +++ b/crates/typst-layout/src/inline/prepare.rs @@ -118,7 +118,6 @@ 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-library/src/model/bibliography.rs b/crates/typst-library/src/model/bibliography.rs index 4ea7b535d..a1a7363fe 100644 --- a/crates/typst-library/src/model/bibliography.rs +++ b/crates/typst-library/src/model/bibliography.rs @@ -539,6 +539,7 @@ impl IntoValue for CslSource { /// memoization) for the whole document. This setup is necessary because /// citation formatting is inherently stateful and we need access to all /// citations to do it. +#[derive(Debug)] pub(super) struct Works { /// Maps from the location of a citation group to its rendered content. pub citations: HashMap>,