Apply insertion class limits in incremental edits
This commit is contained in:
1 parent
68d2cef346
commit
506904bbdc
2 files changed
+49
-4
No files matched your search
@@ -1323,8 +1323,25 @@ impl Engine {
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let goal = next.page_goal - insertion_height;
|
||||
if !current.items.is_empty() && used + gap + insertion.height > goal {
|
||||
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| entry.height)
|
||||
.sum();
|
||||
let exceeds = class_height + insertion.height > class_limit
|
||||
|| insertion_height + insertion.height > capacity
|
||||
|| used + gap + 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;
|
||||
@@ -1824,6 +1841,34 @@ mod tests {
|
||||
assert_eq!(layout.pages()[1].insertions.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incremental_layout_respects_insertion_class_limits() {
|
||||
let rule = VerticalItem::Rule {
|
||||
width: 65_536,
|
||||
height: 200,
|
||||
};
|
||||
let mut document = Document::new(
|
||||
1000,
|
||||
vec![
|
||||
Unit::paragraph(1, "body"),
|
||||
Unit::insertion(2, 1, 200, vec![rule.clone()]),
|
||||
Unit::insertion(3, 1, 200, vec![rule]),
|
||||
],
|
||||
);
|
||||
document.insertion_classes = vec![InsertionClass {
|
||||
number: 1,
|
||||
fraction_percent: 30,
|
||||
max_height: None,
|
||||
}];
|
||||
let mut engine = Engine::layout(&document);
|
||||
assert_eq!(engine.pages().len(), 2);
|
||||
assert_eq!(engine.pages()[0].insertions.len(), 1);
|
||||
engine.edit(&document, 1, "changed body");
|
||||
assert_eq!(engine.pages().len(), 2);
|
||||
assert_eq!(engine.pages()[0].insertions.len(), 1);
|
||||
assert_eq!(engine.pages()[1].insertions.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insertion_skip_reserves_space_before_the_first_insertion() {
|
||||
let insertion = || {
|
||||
|
||||
Reference in new issue
Block a user