Honour per-class insertion page limits
This commit is contained in:
1 parent
ff3fff343d
commit
a8e7499ce3
3 files changed
+69
-7
No files matched your search
@@ -512,6 +512,14 @@ impl Unit {
|
||||
}
|
||||
}
|
||||
|
||||
/// A TeX insertion class (`\insert` class) and its maximum share of the
|
||||
/// page. A value of 100 means the insertion may use the whole page.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct InsertionClass {
|
||||
pub number: u8,
|
||||
pub fraction_percent: u8,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Document {
|
||||
pub units: Vec<Unit>,
|
||||
@@ -525,6 +533,9 @@ pub struct Document {
|
||||
pub text_fraction_percent: u8,
|
||||
/// `\floatpagefraction` equivalent for dedicated float pages.
|
||||
pub float_page_fraction_percent: u8,
|
||||
/// Per-class insertion limits. Missing classes use the full page.
|
||||
#[serde(default)]
|
||||
pub insertion_classes: Vec<InsertionClass>,
|
||||
}
|
||||
|
||||
fn default_text_fraction() -> u8 {
|
||||
@@ -539,9 +550,19 @@ impl Document {
|
||||
top_skip: 0,
|
||||
text_fraction_percent: default_text_fraction(),
|
||||
float_page_fraction_percent: 50,
|
||||
insertion_classes: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Maximum height available to one insertion class on a page.
|
||||
pub fn insertion_class_limit(&self, class: u8) -> i64 {
|
||||
self.insertion_classes
|
||||
.iter()
|
||||
.find(|entry| entry.number == class)
|
||||
.map_or(self.page_goal, |entry| {
|
||||
self.page_goal * i64::from(entry.fraction_percent) / 100
|
||||
})
|
||||
}
|
||||
pub fn with_top_skip(mut self, top_skip: i64) -> Self {
|
||||
self.top_skip = top_skip;
|
||||
self
|
||||
@@ -832,9 +853,21 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
VerticalItem::Insertion(insertion) => {
|
||||
let page_capacity = document.page_goal.saturating_sub(document.top_skip);
|
||||
if insertion.height > page_capacity && !insertion.items.is_empty() {
|
||||
if !current.items.is_empty() {
|
||||
let class_limit = document
|
||||
.insertion_class_limit(insertion.class)
|
||||
.min(document.page_goal.saturating_sub(document.top_skip))
|
||||
.max(1);
|
||||
let class_height: i64 = current
|
||||
.insertions
|
||||
.iter()
|
||||
.filter(|entry| entry.class == insertion.class)
|
||||
.map(|entry| entry.height)
|
||||
.sum();
|
||||
if (class_height + insertion.height > class_limit
|
||||
|| 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;
|
||||
@@ -843,7 +876,7 @@ impl Engine {
|
||||
let mut chunk_height = 0;
|
||||
for sub_item in &insertion.items {
|
||||
let sub_height = item_height(sub_item);
|
||||
if chunk_height + sub_height > page_capacity && !chunk.is_empty() {
|
||||
if chunk_height + sub_height > class_limit && !chunk.is_empty() {
|
||||
current.insertions.push(Insertion {
|
||||
class: insertion.class,
|
||||
height: chunk_height,
|
||||
@@ -867,7 +900,10 @@ impl Engine {
|
||||
}
|
||||
} else {
|
||||
let goal = document.page_goal - insertion_height;
|
||||
if !current.items.is_empty() && used + insertion.height > goal {
|
||||
if (!current.items.is_empty() || !current.insertions.is_empty())
|
||||
&& (used + insertion.height > goal
|
||||
|| class_height + insertion.height > class_limit)
|
||||
{
|
||||
pages.push(std::mem::replace(&mut current, Page::empty()));
|
||||
used = document.top_skip;
|
||||
insertion_height = 0;
|
||||
@@ -1503,6 +1539,29 @@ mod tests {
|
||||
assert_eq!(Engine::layout(&document).pages().len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insertion_class_limit_ships_the_overflow_to_the_next_page() {
|
||||
let rule = VerticalItem::Rule {
|
||||
width: 65_536,
|
||||
height: 200,
|
||||
};
|
||||
let mut document = Document::new(
|
||||
1000,
|
||||
vec![
|
||||
Unit::insertion(1, 1, 200, vec![rule.clone()]),
|
||||
Unit::insertion(2, 1, 200, vec![rule]),
|
||||
],
|
||||
);
|
||||
document.insertion_classes = vec![InsertionClass {
|
||||
number: 1,
|
||||
fraction_percent: 30,
|
||||
}];
|
||||
let layout = Engine::layout(&document);
|
||||
assert_eq!(layout.pages().len(), 2);
|
||||
assert_eq!(layout.pages()[0].insertions.len(), 1);
|
||||
assert_eq!(layout.pages()[1].insertions.len(), 1);
|
||||
}
|
||||
|
||||
#[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