Place late top floats on the current page when they fit
This commit is contained in:
1 parent
484375fa2b
commit
5b4eadb88f
2 files changed
+86
-6
No files matched your search
@@ -993,6 +993,22 @@ impl Engine {
|
||||
}
|
||||
VerticalItem::Float(float) => {
|
||||
if float.placement == FloatPlacement::Top {
|
||||
let existing_top: i64 = current
|
||||
.floats
|
||||
.iter()
|
||||
.filter(|entry| entry.placement == FloatPlacement::Top)
|
||||
.map(|entry| entry.height)
|
||||
.sum();
|
||||
let fits_on_current = !current.items.is_empty()
|
||||
&& used + float.height <= document.page_goal - insertion_height
|
||||
&& (existing_top + float.height) * 100
|
||||
<= i64::from(document.top_fraction_percent)
|
||||
* document.page_goal;
|
||||
if fits_on_current {
|
||||
used += float.height;
|
||||
current.floats.push(float.clone());
|
||||
continue;
|
||||
}
|
||||
if !current.items.is_empty() || !current.insertions.is_empty() {
|
||||
pages.push(std::mem::replace(&mut current, Page::empty()));
|
||||
used = document.top_skip;
|
||||
@@ -1295,6 +1311,21 @@ impl Engine {
|
||||
}
|
||||
VerticalItem::Float(float) => {
|
||||
if float.placement == FloatPlacement::Top {
|
||||
let existing_top: i64 = current
|
||||
.floats
|
||||
.iter()
|
||||
.filter(|entry| entry.placement == FloatPlacement::Top)
|
||||
.map(|entry| entry.height)
|
||||
.sum();
|
||||
let fits_on_current = !current.items.is_empty()
|
||||
&& used + float.height <= next.page_goal - insertion_height
|
||||
&& (existing_top + float.height) * 100
|
||||
<= i64::from(next.top_fraction_percent) * next.page_goal;
|
||||
if fits_on_current {
|
||||
used += float.height;
|
||||
current.floats.push(float.clone());
|
||||
continue;
|
||||
}
|
||||
if !current.items.is_empty() || !current.insertions.is_empty() {
|
||||
pages.push(std::mem::replace(&mut current, Page::empty()));
|
||||
used = next.top_skip;
|
||||
@@ -1606,7 +1637,38 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn top_float_starts_a_new_page_when_it_arrives_late() {
|
||||
fn top_float_starts_a_new_page_when_it_cannot_fit() {
|
||||
let unit = Unit {
|
||||
id: 0,
|
||||
source: "float".to_owned(),
|
||||
paragraph_style: None,
|
||||
items: vec![
|
||||
VerticalItem::Box(VerticalBox {
|
||||
width: 10,
|
||||
height: 10,
|
||||
}),
|
||||
VerticalItem::Float(Float {
|
||||
placement: FloatPlacement::Top,
|
||||
width: 40,
|
||||
height: 95,
|
||||
content: Vec::new(),
|
||||
}),
|
||||
VerticalItem::Box(VerticalBox {
|
||||
width: 10,
|
||||
height: 10,
|
||||
}),
|
||||
],
|
||||
};
|
||||
let engine = Engine::layout(&Document::new(100, vec![unit]));
|
||||
assert_eq!(engine.layout.pages.len(), 3);
|
||||
assert_eq!(engine.layout.pages[0].items.len(), 1);
|
||||
assert_eq!(engine.layout.pages[1].floats.len(), 1);
|
||||
assert_eq!(engine.layout.pages[1].items.len(), 0);
|
||||
assert_eq!(engine.layout.pages[2].items.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_late_top_float_that_fits_joins_the_current_page() {
|
||||
let unit = Unit {
|
||||
id: 0,
|
||||
source: "float".to_owned(),
|
||||
@@ -1629,10 +1691,9 @@ mod tests {
|
||||
],
|
||||
};
|
||||
let engine = Engine::layout(&Document::new(100, vec![unit]));
|
||||
assert_eq!(engine.layout.pages.len(), 2);
|
||||
assert_eq!(engine.layout.pages[0].items.len(), 1);
|
||||
assert_eq!(engine.layout.pages[1].floats.len(), 1);
|
||||
assert_eq!(engine.layout.pages[1].items.len(), 1);
|
||||
assert_eq!(engine.layout.pages.len(), 1);
|
||||
assert_eq!(engine.layout.pages[0].floats.len(), 1);
|
||||
assert_eq!(engine.layout.pages[0].items.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1737,6 +1798,25 @@ mod tests {
|
||||
assert_eq!(layout.pages()[1].insertions.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incremental_layout_places_a_fitting_late_top_float_the_same_way() {
|
||||
let mut document = Document::new(
|
||||
100,
|
||||
vec![
|
||||
Unit::paragraph(1, "body text"),
|
||||
Unit::float(2, FloatPlacement::Top, 40, 20),
|
||||
Unit::paragraph(3, "more text"),
|
||||
],
|
||||
);
|
||||
document.top_skip = 0;
|
||||
let mut engine = Engine::layout(&document);
|
||||
let full_pages = engine.pages().len();
|
||||
let full_floats = engine.pages()[0].floats.len();
|
||||
engine.edit(&document, 3, "changed text");
|
||||
assert_eq!(engine.pages().len(), full_pages);
|
||||
assert_eq!(engine.pages()[0].floats.len(), full_floats);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_large_top_float_gets_a_float_page() {
|
||||
let mut document = Document::new(100, Vec::new());
|
||||
|
||||
Reference in new issue
Block a user