summaryrefslogtreecommitdiff
path: root/crates/typst-layout/src
diff options
context:
space:
mode:
author+merlan #flirora <flirora@flirora.xyz>2025-06-27 04:36:46 -0400
committerGitHub <noreply@github.com>2025-06-27 08:36:46 +0000
commitb9f3a95e03bd792bca4926959eab5f9f6edd1a9d (patch)
tree2457267e13d2872844145ee8a77fcf68ef33ec25 /crates/typst-layout/src
parente8ce894ee7317654a0de1c822d14b3e169dde4d9 (diff)
Sort line items by logical order when constructing frame (#5887)
Co-authored-by: Laurenz <laurmaedje@gmail.com>
Diffstat (limited to 'crates/typst-layout/src')
-rw-r--r--crates/typst-layout/src/inline/line.rs58
1 files changed, 34 insertions, 24 deletions
diff --git a/crates/typst-layout/src/inline/line.rs b/crates/typst-layout/src/inline/line.rs
index f0518927..7bf4d4c7 100644
--- a/crates/typst-layout/src/inline/line.rs
+++ b/crates/typst-layout/src/inline/line.rs
@@ -219,7 +219,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);
}
}
@@ -270,10 +270,10 @@ fn collect_range<'a>(
items: &mut Items<'a>,
fallback: &mut Option<ItemEntry<'a>>,
) {
- for (subrange, item) in p.slice(range.clone()) {
+ for (idx, (subrange, item)) in p.slice(range.clone()).enumerate() {
// All non-text items are just kept, they can't be split.
let Item::Text(shaped) = item else {
- items.push(item);
+ items.push(item, idx);
continue;
};
@@ -293,10 +293,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), idx);
} else {
// When the item is fully contained, just keep it.
- items.push(item);
+ items.push(item, idx);
}
}
}
@@ -499,16 +499,16 @@ pub fn commit(
// Build the frames and determine the height and baseline.
let mut frames = vec![];
- for item in line.items.iter() {
- let mut push = |offset: &mut Abs, frame: Frame| {
+ for &(idx, ref item) in line.items.indexed_iter() {
+ let mut push = |offset: &mut Abs, frame: Frame, idx: usize| {
let width = frame.width();
top.set_max(frame.baseline());
bottom.set_max(frame.size().y - frame.baseline());
- frames.push((*offset, frame));
+ frames.push((*offset, frame, idx));
*offset += width;
};
- match item {
+ match &**item {
Item::Absolute(v, _) => {
offset += *v;
}
@@ -520,7 +520,7 @@ pub fn commit(
layout_box(elem, engine, loc.relayout(), styles, region)
})?;
apply_baseline_shift(&mut frame, *styles);
- push(&mut offset, frame);
+ push(&mut offset, frame, idx);
} else {
offset += amount;
}
@@ -532,15 +532,15 @@ pub fn commit(
justification_ratio,
extra_justification,
);
- push(&mut offset, frame);
+ push(&mut offset, frame, idx);
}
Item::Frame(frame) => {
- push(&mut offset, frame.clone());
+ push(&mut offset, frame.clone(), idx);
}
Item::Tag(tag) => {
let mut frame = Frame::soft(Size::zero());
frame.push(Point::zero(), FrameItem::Tag((*tag).clone()));
- frames.push((offset, frame));
+ frames.push((offset, frame, idx));
}
Item::Skip(_) => {}
}
@@ -559,8 +559,13 @@ pub fn commit(
add_par_line_marker(&mut output, marker, engine, locator, top);
}
+ // Ensure that the final frame's items are in logical order rather than in
+ // visual order. This is important because it affects the order of elements
+ // during introspection and thus things like counters.
+ frames.sort_unstable_by_key(|(_, _, idx)| *idx);
+
// Construct the line's frame.
- for (offset, frame) in frames {
+ for (offset, frame, _) in frames {
let x = offset + p.config.align.position(remaining);
let y = top - frame.baseline();
output.push_frame(Point::new(x, y), frame);
@@ -627,7 +632,7 @@ fn overhang(c: char) -> f64 {
}
/// A collection of owned or borrowed inline items.
-pub struct Items<'a>(Vec<ItemEntry<'a>>);
+pub struct Items<'a>(Vec<(usize, ItemEntry<'a>)>);
impl<'a> Items<'a> {
/// Create empty items.
@@ -636,33 +641,38 @@ impl<'a> Items<'a> {
}
/// Push a new item.
- pub fn push(&mut self, entry: impl Into<ItemEntry<'a>>) {
- self.0.push(entry.into());
+ pub fn push(&mut self, entry: impl Into<ItemEntry<'a>>, idx: usize) {
+ self.0.push((idx, entry.into()));
}
/// Iterate over the items.
pub fn iter(&self) -> impl Iterator<Item = &Item<'a>> {
- self.0.iter().map(|item| &**item)
+ self.0.iter().map(|(_, item)| &**item)
+ }
+
+ /// Iterate over the items with indices
+ pub fn indexed_iter(&self) -> impl Iterator<Item = &(usize, ItemEntry<'a>)> {
+ self.0.iter()
}
/// Access the first item.
pub fn first(&self) -> Option<&Item<'a>> {
- self.0.first().map(|item| &**item)
+ self.0.first().map(|(_, 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)
}
/// 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()?.1.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()?.1.text_mut()
}
/// Reorder the items starting at the given index to RTL.
@@ -673,12 +683,12 @@ impl<'a> Items<'a> {
impl<'a> FromIterator<ItemEntry<'a>> for Items<'a> {
fn from_iter<I: IntoIterator<Item = ItemEntry<'a>>>(iter: I) -> Self {
- Self(iter.into_iter().collect())
+ Self(iter.into_iter().enumerate().collect())
}
}
impl<'a> Deref for Items<'a> {
- type Target = Vec<ItemEntry<'a>>;
+ type Target = Vec<(usize, ItemEntry<'a>)>;
fn deref(&self) -> &Self::Target {
&self.0