Split insertions at the page break and hold over the tail
This commit is contained in:
1 parent
b7b75fbff1
commit
b5ec36ae2a
4 files changed
+202
-144
No files matched your search
+189
-138
@@ -927,6 +927,122 @@ fn place_paragraph_flow(
|
||||
}
|
||||
}
|
||||
|
||||
/// TeX insertion placement shared by the initial and incremental builders.
|
||||
///
|
||||
/// Insertion material stays on the page where its anchor text ends. When the
|
||||
/// page cannot hold all of it, the part that fits at an item boundary stays
|
||||
/// and the remainder is held over to the next page (TeX's split insertions).
|
||||
fn place_insertion(
|
||||
document: &Document,
|
||||
pages: &mut Vec<Page>,
|
||||
current: &mut Page,
|
||||
used: &mut i64,
|
||||
insertion_height: &mut i64,
|
||||
insertion: &Insertion,
|
||||
) {
|
||||
let mut pending = insertion.clone();
|
||||
loop {
|
||||
let class = pending.class;
|
||||
let gap = if current.insertions.is_empty() {
|
||||
document.insertion_skip_for(class).max(0)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let capacity = document
|
||||
.page_goal
|
||||
.saturating_sub(document.top_skip)
|
||||
.saturating_sub(gap)
|
||||
.max(1);
|
||||
let class_limit = document.insertion_class_limit(class).min(capacity).max(1);
|
||||
let class_height: i64 = current
|
||||
.insertions
|
||||
.iter()
|
||||
.filter(|entry| entry.class == class)
|
||||
.map(|entry| document.insertion_reserve(entry.class, entry.height))
|
||||
.sum();
|
||||
let reserved = document.insertion_reserve(class, pending.height);
|
||||
let room = document
|
||||
.page_goal
|
||||
.saturating_sub(*used)
|
||||
.saturating_sub(gap)
|
||||
.saturating_sub(*insertion_height)
|
||||
.max(0);
|
||||
if class_height + reserved <= class_limit && reserved <= room {
|
||||
*insertion_height += gap + reserved;
|
||||
current.insertions.push(pending);
|
||||
return;
|
||||
}
|
||||
let page_has_content = !current.items.is_empty() || !current.insertions.is_empty();
|
||||
if page_has_content {
|
||||
let available = room.min(class_limit.saturating_sub(class_height));
|
||||
if available > 0
|
||||
&& let Some((head, tail)) = split_insertion(document, &pending, available)
|
||||
{
|
||||
*insertion_height += gap + document.insertion_reserve(class, head.height);
|
||||
current.insertions.push(head);
|
||||
pages.push(std::mem::replace(current, Page::empty()));
|
||||
*used = document.top_skip;
|
||||
*insertion_height = 0;
|
||||
pending = tail;
|
||||
continue;
|
||||
}
|
||||
pages.push(std::mem::replace(current, Page::empty()));
|
||||
*used = document.top_skip;
|
||||
*insertion_height = 0;
|
||||
continue;
|
||||
}
|
||||
if let Some((head, tail)) = split_insertion(document, &pending, class_limit) {
|
||||
*insertion_height = gap + document.insertion_reserve(class, head.height);
|
||||
current.insertions.push(head);
|
||||
pages.push(std::mem::replace(current, Page::empty()));
|
||||
*used = document.top_skip;
|
||||
*insertion_height = 0;
|
||||
pending = tail;
|
||||
continue;
|
||||
}
|
||||
*insertion_height = gap + reserved;
|
||||
current.insertions.push(pending);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/// Split an insertion at an item boundary, returning the part that fits in
|
||||
/// `available` reserved space and the held-over remainder.
|
||||
fn split_insertion(
|
||||
document: &Document,
|
||||
insertion: &Insertion,
|
||||
available: i64,
|
||||
) -> Option<(Insertion, Insertion)> {
|
||||
let class = insertion.class;
|
||||
let mut head = Vec::new();
|
||||
let mut head_height = 0i64;
|
||||
for (index, item) in insertion.items.iter().enumerate() {
|
||||
let height = item_height(item);
|
||||
if head.is_empty() && document.insertion_reserve(class, height) > available {
|
||||
return None;
|
||||
}
|
||||
if !head.is_empty() && document.insertion_reserve(class, head_height + height) > available {
|
||||
let tail_items = insertion.items[index..].to_vec();
|
||||
let tail_height: i64 = tail_items.iter().map(item_height).sum();
|
||||
return Some((
|
||||
Insertion {
|
||||
class,
|
||||
height: head_height,
|
||||
items: head,
|
||||
},
|
||||
Insertion {
|
||||
class,
|
||||
height: tail_height,
|
||||
items: tail_items,
|
||||
},
|
||||
));
|
||||
}
|
||||
head.push(item.clone());
|
||||
head_height += height;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn top_float_exceeds_page_fraction(page: &Page, page_goal: i64, percent: u8) -> bool {
|
||||
let height = top_float_height(page);
|
||||
height > 0 && page.items.is_empty() && height * 100 > i64::from(percent) * page_goal
|
||||
@@ -1203,106 +1319,14 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
VerticalItem::Insertion(insertion) => {
|
||||
let gap = if current.insertions.is_empty() {
|
||||
document.insertion_skip_for(insertion.class).max(0)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let capacity = document
|
||||
.page_goal
|
||||
.saturating_sub(document.top_skip)
|
||||
.saturating_sub(gap)
|
||||
.max(1);
|
||||
if insertion_height
|
||||
+ document.insertion_reserve(insertion.class, insertion.height)
|
||||
> capacity
|
||||
&& (!current.items.is_empty() || !current.insertions.is_empty())
|
||||
{
|
||||
pages.push(std::mem::replace(&mut current, Page::empty()));
|
||||
used = document.top_skip;
|
||||
insertion_height = 0;
|
||||
}
|
||||
let class_limit = document
|
||||
.insertion_class_limit(insertion.class)
|
||||
.min(
|
||||
document
|
||||
.page_goal
|
||||
.saturating_sub(document.top_skip)
|
||||
.saturating_sub(gap),
|
||||
)
|
||||
.max(1);
|
||||
let class_height: i64 = current
|
||||
.insertions
|
||||
.iter()
|
||||
.filter(|entry| entry.class == insertion.class)
|
||||
.map(|entry| document.insertion_reserve(entry.class, entry.height))
|
||||
.sum();
|
||||
if (class_height
|
||||
+ document.insertion_reserve(insertion.class, insertion.height)
|
||||
> class_limit
|
||||
|| document.insertion_reserve(insertion.class, insertion.height)
|
||||
> class_limit)
|
||||
&& !insertion.items.is_empty()
|
||||
{
|
||||
if !current.items.is_empty() || !current.insertions.is_empty() {
|
||||
pages.push(std::mem::replace(&mut current, Page::empty()));
|
||||
used = document.top_skip;
|
||||
insertion_height = 0;
|
||||
}
|
||||
let mut chunk = Vec::new();
|
||||
let mut chunk_height = 0;
|
||||
let mut chunk_gap = gap;
|
||||
for sub_item in &insertion.items {
|
||||
let sub_height = item_height(sub_item);
|
||||
if document
|
||||
.insertion_reserve(insertion.class, chunk_height + sub_height)
|
||||
> class_limit
|
||||
&& !chunk.is_empty()
|
||||
{
|
||||
current.insertions.push(Insertion {
|
||||
class: insertion.class,
|
||||
height: chunk_height,
|
||||
items: std::mem::take(&mut chunk),
|
||||
});
|
||||
insertion_height = chunk_gap
|
||||
+ document.insertion_reserve(insertion.class, chunk_height);
|
||||
chunk_height = 0;
|
||||
pages.push(std::mem::replace(&mut current, Page::empty()));
|
||||
used = document.top_skip;
|
||||
chunk_gap = gap;
|
||||
}
|
||||
chunk.push(sub_item.clone());
|
||||
chunk_height += sub_height;
|
||||
}
|
||||
if !chunk.is_empty() {
|
||||
current.insertions.push(Insertion {
|
||||
class: insertion.class,
|
||||
height: chunk_height,
|
||||
items: chunk,
|
||||
});
|
||||
insertion_height = chunk_gap
|
||||
+ document.insertion_reserve(insertion.class, chunk_height);
|
||||
}
|
||||
} else {
|
||||
let goal = document.page_goal - insertion_height;
|
||||
if (!current.items.is_empty() || !current.insertions.is_empty())
|
||||
&& (used
|
||||
+ gap
|
||||
+ document.insertion_reserve(insertion.class, insertion.height)
|
||||
> goal
|
||||
|| class_height
|
||||
+ document
|
||||
.insertion_reserve(insertion.class, insertion.height)
|
||||
> class_limit)
|
||||
{
|
||||
pages.push(std::mem::replace(&mut current, Page::empty()));
|
||||
used = document.top_skip;
|
||||
insertion_height = 0;
|
||||
}
|
||||
insertion_height +=
|
||||
gap + document.insertion_reserve(insertion.class, insertion.height);
|
||||
current.insertions.push(insertion.clone());
|
||||
}
|
||||
place_insertion(
|
||||
document,
|
||||
&mut pages,
|
||||
&mut current,
|
||||
&mut used,
|
||||
&mut insertion_height,
|
||||
insertion,
|
||||
);
|
||||
}
|
||||
VerticalItem::Mark(mark) => {
|
||||
current.marks.push(mark.clone());
|
||||
@@ -1863,44 +1887,14 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
VerticalItem::Insertion(insertion) => {
|
||||
let gap = if current.insertions.is_empty() {
|
||||
next.insertion_skip_for(insertion.class).max(0)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let capacity = next
|
||||
.page_goal
|
||||
.saturating_sub(next.top_skip)
|
||||
.saturating_sub(gap)
|
||||
.max(1);
|
||||
let class_limit = next
|
||||
.insertion_class_limit(insertion.class)
|
||||
.min(capacity)
|
||||
.max(1);
|
||||
let class_height: i64 = current
|
||||
.insertions
|
||||
.iter()
|
||||
.filter(|entry| entry.class == insertion.class)
|
||||
.map(|entry| next.insertion_reserve(entry.class, entry.height))
|
||||
.sum();
|
||||
let exceeds = class_height
|
||||
+ next.insertion_reserve(insertion.class, insertion.height)
|
||||
> class_limit
|
||||
|| insertion_height
|
||||
+ next.insertion_reserve(insertion.class, insertion.height)
|
||||
> capacity
|
||||
|| used
|
||||
+ gap
|
||||
+ next.insertion_reserve(insertion.class, insertion.height)
|
||||
> next.page_goal;
|
||||
if exceeds && !current.items.is_empty() {
|
||||
pages.push(std::mem::replace(&mut current, Page::empty()));
|
||||
used = next.top_skip;
|
||||
insertion_height = 0;
|
||||
}
|
||||
insertion_height +=
|
||||
gap + next.insertion_reserve(insertion.class, insertion.height);
|
||||
current.insertions.push(insertion.clone());
|
||||
place_insertion(
|
||||
next,
|
||||
&mut pages,
|
||||
&mut current,
|
||||
&mut used,
|
||||
&mut insertion_height,
|
||||
insertion,
|
||||
);
|
||||
}
|
||||
VerticalItem::Mark(mark) => {
|
||||
current.marks.push(mark.clone());
|
||||
@@ -2699,6 +2693,63 @@ mod tests {
|
||||
assert!(engine.changed_pages().iter().all(|page| *page >= 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insertion_splits_at_the_page_break_and_holds_over_the_tail() {
|
||||
let rule = |height| VerticalItem::Rule {
|
||||
width: 65_536,
|
||||
height,
|
||||
};
|
||||
let mut document = Document::new(
|
||||
100,
|
||||
vec![
|
||||
Unit::vertical(1, vec![rule(40)]),
|
||||
Unit::insertion(2, 1, 80, vec![rule(40), rule(40)]),
|
||||
Unit::vertical(3, vec![rule(20)]),
|
||||
],
|
||||
);
|
||||
document.insertion_skip = 0;
|
||||
let layout = Engine::layout(&document);
|
||||
assert_eq!(layout.pages().len(), 2);
|
||||
assert_eq!(layout.pages()[0].items.len(), 1);
|
||||
assert_eq!(layout.pages()[0].insertions.len(), 1);
|
||||
assert_eq!(layout.pages()[0].insertions[0].height, 40);
|
||||
assert_eq!(layout.pages()[1].insertions.len(), 1);
|
||||
assert_eq!(layout.pages()[1].insertions[0].height, 40);
|
||||
// The held-over tail sits above the text that follows the insertion.
|
||||
assert_eq!(layout.pages()[1].items.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incremental_layout_holds_over_the_insertion_tail() {
|
||||
let rule = |height| VerticalItem::Rule {
|
||||
width: 65_536,
|
||||
height,
|
||||
};
|
||||
let mut document = Document::new(
|
||||
100,
|
||||
vec![
|
||||
Unit::paragraph(1, "body"),
|
||||
Unit::insertion(2, 1, 80, vec![rule(40), rule(40)]),
|
||||
],
|
||||
);
|
||||
document.insertion_skip = 0;
|
||||
document.insertion_classes = vec![InsertionClass {
|
||||
number: 1,
|
||||
fraction_percent: 60,
|
||||
max_height: None,
|
||||
skip: None,
|
||||
multiplier: 1000,
|
||||
}];
|
||||
let mut engine = Engine::layout(&document);
|
||||
assert_eq!(engine.pages().len(), 2);
|
||||
assert_eq!(engine.pages()[0].insertions[0].height, 40);
|
||||
assert_eq!(engine.pages()[1].insertions[0].height, 40);
|
||||
engine.edit(&document, 1, "changed body");
|
||||
assert_eq!(engine.pages().len(), 2);
|
||||
assert_eq!(engine.pages()[0].insertions[0].height, 40);
|
||||
assert_eq!(engine.pages()[1].insertions[0].height, 40);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incremental_layout_respects_insertion_count() {
|
||||
let rule = VerticalItem::Rule {
|
||||
|
||||
Reference in new issue
Block a user