Limit paged floats by topnumber and bottomnumber
This commit is contained in:
1 parent
c2ba59906c
commit
54a8488fa2
3 files changed
+104
-5
No files matched your search
@@ -153,6 +153,12 @@ pub struct PageBuilder {
|
||||
pub float_sep: i64,
|
||||
/// TeX bottomfraction: percent of the page bottom floats may occupy.
|
||||
pub bottom_fraction_percent: u8,
|
||||
/// TeX `\topnumber`: maximum top floats on one page.
|
||||
pub top_number: usize,
|
||||
/// TeX `\bottomnumber`: maximum bottom floats on one page.
|
||||
pub bottom_number: usize,
|
||||
/// TeX `\totalnumber`: maximum floats, of any placement, on one page.
|
||||
pub total_number: usize,
|
||||
/// TeX `\raggedbottom` (default) versus `\flushbottom`.
|
||||
pub ragged_bottom: bool,
|
||||
}
|
||||
@@ -167,6 +173,9 @@ impl PageBuilder {
|
||||
text_float_sep: 0,
|
||||
float_sep: 0,
|
||||
bottom_fraction_percent: 100,
|
||||
top_number: 2,
|
||||
bottom_number: 1,
|
||||
total_number: 12,
|
||||
ragged_bottom: true,
|
||||
}
|
||||
}
|
||||
@@ -238,6 +247,9 @@ impl PageBuilder {
|
||||
let mut depth = 0_i64;
|
||||
let mut first_box = true;
|
||||
let mut has_float = false;
|
||||
let mut has_body = false;
|
||||
let mut top_count = 0_usize;
|
||||
let mut float_total = 0_usize;
|
||||
let mut bottom = 0_i64;
|
||||
let mut bottom_count = 0_usize;
|
||||
let mut usage: BTreeMap<u8, i64> = held.clone();
|
||||
@@ -264,12 +276,18 @@ impl PageBuilder {
|
||||
height += contribution;
|
||||
depth = *box_depth;
|
||||
first_box = false;
|
||||
has_body = true;
|
||||
}
|
||||
VerticalNode::Float {
|
||||
height: float_height,
|
||||
} => {
|
||||
if height > 0 {
|
||||
// TeX never places a top float below body material:
|
||||
if height > 0
|
||||
&& (has_body
|
||||
|| top_count >= self.top_number
|
||||
|| float_total >= self.total_number)
|
||||
{
|
||||
// TeX never places a top float below body material,
|
||||
// nor above the `\topnumber`/`\totalnumber` limits:
|
||||
// the float starts the next page instead.
|
||||
let end = index.max(start);
|
||||
let badness =
|
||||
@@ -297,6 +315,8 @@ impl PageBuilder {
|
||||
}
|
||||
height += (*float_height).max(0);
|
||||
has_float = true;
|
||||
top_count += 1;
|
||||
float_total += 1;
|
||||
}
|
||||
VerticalNode::BottomFloat {
|
||||
height: float_height,
|
||||
@@ -310,7 +330,9 @@ impl PageBuilder {
|
||||
let allowed = self.page_goal * i64::from(self.bottom_fraction_percent) / 100;
|
||||
if height > 0
|
||||
&& (height + reserved + bottom + needed > self.page_goal
|
||||
|| bottom + needed > allowed)
|
||||
|| bottom + needed > allowed
|
||||
|| bottom_count >= self.bottom_number
|
||||
|| float_total >= self.total_number)
|
||||
{
|
||||
let end = index.max(start);
|
||||
let badness =
|
||||
@@ -338,6 +360,7 @@ impl PageBuilder {
|
||||
}
|
||||
bottom += needed;
|
||||
bottom_count += 1;
|
||||
float_total += 1;
|
||||
}
|
||||
VerticalNode::Glue(glue) => {
|
||||
if index > start {
|
||||
@@ -948,6 +971,7 @@ mod tests {
|
||||
fn stacked_bottom_floats_add_float_separators() {
|
||||
let builder = PageBuilder {
|
||||
float_sep: 5,
|
||||
bottom_number: 2,
|
||||
..PageBuilder::new(100)
|
||||
};
|
||||
let nodes = vec![
|
||||
@@ -960,6 +984,57 @@ mod tests {
|
||||
assert_eq!(pages[0].bottom, 45);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn top_number_limits_stacked_top_floats() {
|
||||
let builder = PageBuilder {
|
||||
top_number: 1,
|
||||
..PageBuilder::new(100)
|
||||
};
|
||||
let nodes = vec![
|
||||
VerticalNode::Float { height: 20 },
|
||||
VerticalNode::Float { height: 20 },
|
||||
box_(10, 0),
|
||||
];
|
||||
let pages = builder.build(&nodes);
|
||||
assert_eq!(pages.len(), 2);
|
||||
assert_eq!(pages[0].height, 20);
|
||||
assert_eq!(pages[1].height, 30);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bottom_number_limits_stacked_bottom_floats() {
|
||||
let builder = PageBuilder {
|
||||
bottom_number: 1,
|
||||
..PageBuilder::new(100)
|
||||
};
|
||||
let nodes = vec![
|
||||
box_(50, 0),
|
||||
VerticalNode::BottomFloat { height: 20 },
|
||||
VerticalNode::BottomFloat { height: 20 },
|
||||
];
|
||||
let pages = builder.build(&nodes);
|
||||
assert_eq!(pages.len(), 2);
|
||||
assert_eq!(pages[0].bottom, 20);
|
||||
assert_eq!(pages[1].bottom, 20);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn total_number_counts_every_float_placement() {
|
||||
let builder = PageBuilder {
|
||||
total_number: 1,
|
||||
..PageBuilder::new(100)
|
||||
};
|
||||
let nodes = vec![
|
||||
VerticalNode::Float { height: 10 },
|
||||
VerticalNode::BottomFloat { height: 10 },
|
||||
box_(10, 0),
|
||||
];
|
||||
let pages = builder.build(&nodes);
|
||||
assert_eq!(pages.len(), 2);
|
||||
assert_eq!(pages[0].bottom, 0);
|
||||
assert_eq!(pages[1].bottom, 10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bottom_fraction_caps_bottom_float_space() {
|
||||
let builder = PageBuilder {
|
||||
|
||||
Reference in new issue
Block a user