Apply the insertion count multiplier to page space
This commit is contained in:
1 parent
169119a48d
commit
fb33ac6327
4 files changed
+153
-20
No files matched your search
@@ -548,6 +548,15 @@ pub struct InsertionClass {
|
||||
/// `None` falls back to `Document::insertion_skip`.
|
||||
#[serde(default)]
|
||||
pub skip: Option<i64>,
|
||||
/// TeX's `\count<number>` in thousandths: page space reserved by this
|
||||
/// class per unit of insertion height. The default `1000` reserves exactly
|
||||
/// the insertion height; footnote and float classes differ in TeX.
|
||||
#[serde(default = "default_insertion_multiplier")]
|
||||
pub multiplier: u32,
|
||||
}
|
||||
|
||||
fn default_insertion_multiplier() -> u32 {
|
||||
1000
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
@@ -667,6 +676,17 @@ impl Document {
|
||||
}
|
||||
}
|
||||
|
||||
/// Page space reserved by `height` of insertion material in `class`,
|
||||
/// after applying TeX's `\count<number>` multiplier (thousandths).
|
||||
pub fn insertion_reserve(&self, class: u8, height: i64) -> i64 {
|
||||
let multiplier = self
|
||||
.insertion_classes
|
||||
.iter()
|
||||
.find(|entry| entry.number == class)
|
||||
.map_or(1000, |entry| entry.multiplier);
|
||||
height.saturating_mul(i64::from(multiplier)) / 1000
|
||||
}
|
||||
|
||||
/// Maximum height available to one insertion class on a page.
|
||||
pub fn insertion_class_limit(&self, class: u8) -> i64 {
|
||||
self.insertion_classes
|
||||
@@ -1124,7 +1144,9 @@ impl Engine {
|
||||
.saturating_sub(document.top_skip)
|
||||
.saturating_sub(gap)
|
||||
.max(1);
|
||||
if insertion_height + insertion.height > capacity
|
||||
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()));
|
||||
@@ -1144,10 +1166,13 @@ impl Engine {
|
||||
.insertions
|
||||
.iter()
|
||||
.filter(|entry| entry.class == insertion.class)
|
||||
.map(|entry| entry.height)
|
||||
.map(|entry| document.insertion_reserve(entry.class, entry.height))
|
||||
.sum();
|
||||
if (class_height + insertion.height > class_limit
|
||||
|| insertion.height > class_limit)
|
||||
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() {
|
||||
@@ -1160,13 +1185,18 @@ impl Engine {
|
||||
let mut chunk_gap = gap;
|
||||
for sub_item in &insertion.items {
|
||||
let sub_height = item_height(sub_item);
|
||||
if chunk_height + sub_height > class_limit && !chunk.is_empty() {
|
||||
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 + chunk_height;
|
||||
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;
|
||||
@@ -1181,19 +1211,27 @@ impl Engine {
|
||||
height: chunk_height,
|
||||
items: chunk,
|
||||
});
|
||||
insertion_height = chunk_gap + chunk_height;
|
||||
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 + insertion.height > goal
|
||||
|| class_height + insertion.height > class_limit)
|
||||
&& (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 + insertion.height;
|
||||
insertion_height +=
|
||||
gap + document.insertion_reserve(insertion.class, insertion.height);
|
||||
current.insertions.push(insertion.clone());
|
||||
}
|
||||
}
|
||||
@@ -1737,17 +1775,25 @@ impl Engine {
|
||||
.insertions
|
||||
.iter()
|
||||
.filter(|entry| entry.class == insertion.class)
|
||||
.map(|entry| entry.height)
|
||||
.map(|entry| next.insertion_reserve(entry.class, entry.height))
|
||||
.sum();
|
||||
let exceeds = class_height + insertion.height > class_limit
|
||||
|| insertion_height + insertion.height > capacity
|
||||
|| used + gap + insertion.height > next.page_goal;
|
||||
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 + insertion.height;
|
||||
insertion_height +=
|
||||
gap + next.insertion_reserve(insertion.class, insertion.height);
|
||||
current.insertions.push(insertion.clone());
|
||||
}
|
||||
VerticalItem::Mark(mark) => {
|
||||
@@ -2320,6 +2366,7 @@ mod tests {
|
||||
fraction_percent: 30,
|
||||
max_height: None,
|
||||
skip: None,
|
||||
multiplier: 1000,
|
||||
}];
|
||||
let layout = Engine::layout(&document);
|
||||
assert_eq!(layout.pages().len(), 2);
|
||||
@@ -2347,12 +2394,14 @@ mod tests {
|
||||
fraction_percent: 60,
|
||||
max_height: None,
|
||||
skip: None,
|
||||
multiplier: 1000,
|
||||
},
|
||||
InsertionClass {
|
||||
number: 2,
|
||||
fraction_percent: 60,
|
||||
max_height: None,
|
||||
skip: None,
|
||||
multiplier: 1000,
|
||||
},
|
||||
];
|
||||
let layout = Engine::layout(&document);
|
||||
@@ -2379,6 +2428,7 @@ mod tests {
|
||||
fraction_percent: 100,
|
||||
max_height: Some(150),
|
||||
skip: None,
|
||||
multiplier: 1000,
|
||||
}];
|
||||
let layout = Engine::layout(&document);
|
||||
assert_eq!(layout.pages().len(), 2);
|
||||
@@ -2507,6 +2557,34 @@ mod tests {
|
||||
assert!(engine.changed_pages().iter().all(|page| *page >= 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incremental_layout_respects_insertion_count() {
|
||||
let rule = VerticalItem::Rule {
|
||||
width: 65_536,
|
||||
height: 200,
|
||||
};
|
||||
let mut document = Document::new(
|
||||
500,
|
||||
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: 100,
|
||||
max_height: None,
|
||||
skip: None,
|
||||
multiplier: 2000,
|
||||
}];
|
||||
let mut engine = Engine::layout(&document);
|
||||
assert_eq!(engine.pages().len(), 2, "2x reserve splits the insertions");
|
||||
engine.edit(&document, 1, "changed body");
|
||||
assert_eq!(engine.pages().len(), 2);
|
||||
assert_eq!(engine.pages()[1].insertions.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incremental_layout_respects_insertion_class_limits() {
|
||||
let rule = VerticalItem::Rule {
|
||||
@@ -2526,6 +2604,7 @@ mod tests {
|
||||
fraction_percent: 30,
|
||||
max_height: None,
|
||||
skip: None,
|
||||
multiplier: 1000,
|
||||
}];
|
||||
let mut engine = Engine::layout(&document);
|
||||
assert_eq!(engine.pages().len(), 2);
|
||||
@@ -2555,6 +2634,7 @@ mod tests {
|
||||
fraction_percent: 100,
|
||||
max_height: None,
|
||||
skip: Some(50),
|
||||
multiplier: 1000,
|
||||
}];
|
||||
let layout = Engine::layout(&document);
|
||||
assert_eq!(layout.pages().len(), 2);
|
||||
@@ -2589,6 +2669,7 @@ mod tests {
|
||||
fraction_percent: 100,
|
||||
max_height: None,
|
||||
skip: Some(50),
|
||||
multiplier: 1000,
|
||||
}];
|
||||
let mut engine = Engine::layout(&document);
|
||||
assert_eq!(engine.pages().len(), 2);
|
||||
@@ -2615,6 +2696,7 @@ mod tests {
|
||||
fraction_percent: 100,
|
||||
max_height: None,
|
||||
skip: None,
|
||||
multiplier: 1000,
|
||||
}];
|
||||
assert_eq!(Engine::layout(&document).pages().len(), 1);
|
||||
|
||||
@@ -2647,6 +2729,7 @@ mod tests {
|
||||
fraction_percent: 100,
|
||||
max_height: None,
|
||||
skip: None,
|
||||
multiplier: 1000,
|
||||
}];
|
||||
assert_eq!(Engine::layout(&exact).pages().len(), 1);
|
||||
}
|
||||
@@ -4373,6 +4456,48 @@ mod tests {
|
||||
assert!(engine.pages().iter().all(|page| page.hash != 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insertion_count_scales_reserved_page_space() {
|
||||
let insertions = |document: &mut Document| {
|
||||
document.units = vec![
|
||||
Unit::insertion(
|
||||
1,
|
||||
0,
|
||||
30,
|
||||
vec![VerticalItem::Rule {
|
||||
width: 10,
|
||||
height: 30,
|
||||
}],
|
||||
),
|
||||
Unit::insertion(
|
||||
2,
|
||||
0,
|
||||
30,
|
||||
vec![VerticalItem::Rule {
|
||||
width: 10,
|
||||
height: 30,
|
||||
}],
|
||||
),
|
||||
];
|
||||
};
|
||||
let mut document = Document::new(100, Vec::new());
|
||||
insertions(&mut document);
|
||||
assert_eq!(Engine::layout(&document).pages().len(), 1);
|
||||
|
||||
document.insertion_classes = vec![InsertionClass {
|
||||
number: 0,
|
||||
fraction_percent: 100,
|
||||
max_height: None,
|
||||
skip: None,
|
||||
multiplier: 2000,
|
||||
}];
|
||||
assert_eq!(
|
||||
Engine::layout(&document).pages().len(),
|
||||
2,
|
||||
"a 2000 multiplier reserves 120pt for 60pt of insertions"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paged_layout_limits_page_depth() {
|
||||
let math = Unit {
|
||||
|
||||
Reference in new issue
Block a user