diff --git a/crates/typst-layout/src/inline/line.rs b/crates/typst-layout/src/inline/line.rs index 311dc7c69..ba48bfe59 100644 --- a/crates/typst-layout/src/inline/line.rs +++ b/crates/typst-layout/src/inline/line.rs @@ -271,7 +271,7 @@ fn collect_range<'a>( items: &mut Items<'a>, fallback: &mut Option>, ) { - for (idx, (subrange, item)) in p.slice(range.clone()).enumerate() { + for (idx, (subrange, item)) in p.slice(range.clone()) { // All non-text items are just kept, they can't be split. let Item::Text(shaped) = item else { items.push(item, idx); diff --git a/crates/typst-layout/src/inline/prepare.rs b/crates/typst-layout/src/inline/prepare.rs index ab39bdb14..1c876ca94 100644 --- a/crates/typst-layout/src/inline/prepare.rs +++ b/crates/typst-layout/src/inline/prepare.rs @@ -34,8 +34,12 @@ impl<'a> Preparation<'a> { &self.items[idx] } - /// Iterate over the items that intersect the given `sliced` range. - pub fn slice(&self, sliced: Range) -> impl Iterator)> { + /// Iterate over the items that intersect the given `sliced` range alongside + /// their indices in `self.items` and their ranges in the paragraph's text. + pub fn slice( + &self, + sliced: Range, + ) -> impl Iterator))> { // Usually, we don't want empty-range items at the start of the line // (because they will be part of the previous line), but for the first // line, we need to keep them. @@ -43,9 +47,13 @@ impl<'a> Preparation<'a> { 0 => 0, n => self.indices.get(n).copied().unwrap_or(0), }; - self.items[start..].iter().take_while(move |(range, _)| { - range.start < sliced.end || range.end <= sliced.end - }) + self.items + .iter() + .enumerate() + .skip(start) + .take_while(move |(_, (range, _))| { + range.start < sliced.end || range.end <= sliced.end + }) } } diff --git a/tests/ref/counter-rtl.png b/tests/ref/counter-rtl.png new file mode 100644 index 000000000..9fc57c236 Binary files /dev/null and b/tests/ref/counter-rtl.png differ diff --git a/tests/suite/introspection/counter.typ b/tests/suite/introspection/counter.typ index b0657a2ad..10ad5468d 100644 --- a/tests/suite/introspection/counter.typ +++ b/tests/suite/introspection/counter.typ @@ -174,3 +174,12 @@ B #context test(c.get(), (100000000002,)) #c.update(n => n + 2) #context test(c.get(), (100000000004,)) + +--- counter-rtl --- +#set page(width: auto) +#let c = counter("c") +#let s = context c.display() + c.step() +#let tree = [درخت] +#let line = [A #s B #tree #s #tree #s #tree C #s D #s] +#line \ +#line