mirror of
https://github.com/typst/typst
synced 2025-06-28 08:12:53 +08:00
Sort line items by logical order when constructing frame (#5887)
Co-authored-by: Laurenz <laurmaedje@gmail.com>
This commit is contained in:
parent
e8ce894ee7
commit
b9f3a95e03
@ -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
|
||||
|
BIN
tests/ref/issue-5775-cite-order-rtl.png
Normal file
BIN
tests/ref/issue-5775-cite-order-rtl.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
@ -147,3 +147,15 @@ B #cite(<netwok>) #cite(<arrgh>).
|
||||
// Error: 7-17 expected label, found string
|
||||
// Hint: 7-17 use `label("%@&#*!\\")` to create a label
|
||||
#cite("%@&#*!\\")
|
||||
|
||||
--- issue-5775-cite-order-rtl ---
|
||||
// Test citation order in RTL text.
|
||||
#set page(width: 300pt)
|
||||
@netwok
|
||||
aaa
|
||||
این است
|
||||
@tolkien54
|
||||
و این یکی هست
|
||||
@arrgh
|
||||
|
||||
#bibliography("/assets/bib/works.bib")
|
||||
|
Loading…
x
Reference in New Issue
Block a user