Run the output routine after each paged page
This commit is contained in:
1 parent
23956a012b
commit
1dc3e95a04
3 files changed
+304
-115
No files matched your search
+300
-110
@@ -813,14 +813,21 @@ impl Document {
|
||||
|
||||
/// Maximum height available to one insertion class on a page.
|
||||
pub fn insertion_class_limit(&self, class: u8) -> i64 {
|
||||
let fraction = self.insertion_fraction_limit(class);
|
||||
self.insertion_classes
|
||||
.iter()
|
||||
.find(|entry| entry.number == class)
|
||||
.and_then(|entry| entry.max_height)
|
||||
.map_or(fraction, |absolute| fraction.min(absolute))
|
||||
}
|
||||
|
||||
/// Percentage share of the page that insertion class `class` may occupy.
|
||||
pub fn insertion_fraction_limit(&self, class: u8) -> i64 {
|
||||
self.insertion_classes
|
||||
.iter()
|
||||
.find(|entry| entry.number == class)
|
||||
.map_or(self.page_goal, |entry| {
|
||||
let fraction = self.page_goal * i64::from(entry.fraction_percent) / 100;
|
||||
entry
|
||||
.max_height
|
||||
.map_or(fraction, |absolute| fraction.min(absolute))
|
||||
self.page_goal * i64::from(entry.fraction_percent) / 100
|
||||
})
|
||||
}
|
||||
pub fn with_top_skip(mut self, top_skip: i64) -> Self {
|
||||
@@ -1122,17 +1129,17 @@ fn held_insertion_material(
|
||||
}
|
||||
|
||||
fn take_insertion_prefix(
|
||||
document: &Document,
|
||||
class: u8,
|
||||
_class: u8,
|
||||
items: Vec<VerticalItem>,
|
||||
capacity: i64,
|
||||
multiplier: i64,
|
||||
) -> (Vec<VerticalItem>, Vec<VerticalItem>, i64) {
|
||||
let mut accepted = Vec::new();
|
||||
let mut reserved = 0_i64;
|
||||
let mut rest = items.into_iter();
|
||||
let mut remainder = Vec::new();
|
||||
while let Some(item) = rest.next() {
|
||||
let reserve = document.insertion_reserve(class, item_height(&item));
|
||||
let reserve = item_height(&item).saturating_mul(multiplier) / 1000;
|
||||
if reserved + reserve <= capacity {
|
||||
accepted.push(item);
|
||||
reserved += reserve;
|
||||
@@ -1501,6 +1508,159 @@ impl OutputRoutine for NoOutput {
|
||||
fn shipout(&mut self, _page: &mut Page, _page_number: usize) {}
|
||||
}
|
||||
|
||||
/// State carried between interpreted output-routine runs in a paged layout.
|
||||
///
|
||||
/// TeX's output registers are global: an assignment made while shipping one
|
||||
/// page changes what later pages see. Keeping the state next to the page loop
|
||||
/// lets a `\dimen<n>` assignment shrink the capacity of the next page's
|
||||
/// insertion class, as it does inside TeX's output routine.
|
||||
struct PagedOutput {
|
||||
program: output::OutputProgram,
|
||||
pageno: i64,
|
||||
deadcycles: i64,
|
||||
counts: [i64; 256],
|
||||
dimens: [i64; 256],
|
||||
skips: [i64; 256],
|
||||
initial_dimens: Vec<(u8, i64)>,
|
||||
}
|
||||
|
||||
impl PagedOutput {
|
||||
fn new(document: &Document, program: output::OutputProgram) -> Self {
|
||||
let mut counts = [1000_i64; 256];
|
||||
let mut dimens = [0_i64; 256];
|
||||
let mut skips = [0_i64; 256];
|
||||
let mut initial_dimens = Vec::new();
|
||||
for class in &document.insertion_classes {
|
||||
let slot = class.number as usize;
|
||||
counts[slot] = i64::from(class.multiplier);
|
||||
dimens[slot] = class.max_height.unwrap_or(0);
|
||||
skips[slot] = class.skip.unwrap_or(document.insertion_skip);
|
||||
initial_dimens.push((class.number, dimens[slot]));
|
||||
}
|
||||
Self {
|
||||
program,
|
||||
pageno: 1,
|
||||
deadcycles: 0,
|
||||
counts,
|
||||
dimens,
|
||||
skips,
|
||||
initial_dimens,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn ship(
|
||||
&mut self,
|
||||
document: &Document,
|
||||
page: &Page,
|
||||
page_goal: i64,
|
||||
penalty: i64,
|
||||
depth: i64,
|
||||
stretch: i64,
|
||||
stretches: [i64; 4],
|
||||
shrink: i64,
|
||||
badness: i64,
|
||||
insert_penalties: i64,
|
||||
) -> Option<bool> {
|
||||
let page_total: i64 = page.items.iter().map(item_height).sum();
|
||||
let mut context = output::OutputContext {
|
||||
page_goal,
|
||||
page_total,
|
||||
page_depth: depth,
|
||||
page_stretch: stretch,
|
||||
page_fil_stretch: stretches[1],
|
||||
page_fill_stretch: stretches[2],
|
||||
page_filll_stretch: stretches[3],
|
||||
page_shrink: shrink,
|
||||
vbadness: badness,
|
||||
hsize: document.text_width,
|
||||
vsize: document.page_goal,
|
||||
top_skip: document.top_skip,
|
||||
max_depth: document.max_depth,
|
||||
output_penalty: penalty,
|
||||
max_deadcycles: document.max_deadcycles,
|
||||
insertion_counts: self.counts,
|
||||
insertion_dimens: self.dimens,
|
||||
insertion_skips: self.skips,
|
||||
pageno: self.pageno,
|
||||
deadcycles: self.deadcycles,
|
||||
prev_graf: page_prev_graf(page),
|
||||
insert_penalties,
|
||||
};
|
||||
let forced = self.deadcycles >= document.max_deadcycles.max(0);
|
||||
if forced {
|
||||
context.deadcycles = 0;
|
||||
}
|
||||
let shipped_now = if forced {
|
||||
1
|
||||
} else {
|
||||
self.program.shipout_count(&mut context).ok()?
|
||||
};
|
||||
self.counts = context.insertion_counts;
|
||||
self.dimens = context.insertion_dimens;
|
||||
self.skips = context.insertion_skips;
|
||||
self.pageno = context.pageno.wrapping_add(1);
|
||||
self.deadcycles = context.deadcycles;
|
||||
if shipped_now > 0 {
|
||||
self.deadcycles = 0;
|
||||
Some(true)
|
||||
} else {
|
||||
self.deadcycles += 1;
|
||||
Some(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Insertion capacity in force before the next page is assembled.
|
||||
/// TeX's `\count<number>` multiplier in force before the next page.
|
||||
fn output_insertion_multiplier(
|
||||
document: &Document,
|
||||
class: u8,
|
||||
routine: Option<&PagedOutput>,
|
||||
) -> i64 {
|
||||
let initial = document
|
||||
.insertion_classes
|
||||
.iter()
|
||||
.find(|entry| entry.number == class)
|
||||
.map_or(1000, |entry| i64::from(entry.multiplier));
|
||||
match routine {
|
||||
Some(routine) if routine.counts[class as usize] != initial => {
|
||||
routine.counts[class as usize].max(0)
|
||||
}
|
||||
_ => initial,
|
||||
}
|
||||
}
|
||||
|
||||
fn output_insertion_limit(document: &Document, class: u8, routine: Option<&PagedOutput>) -> i64 {
|
||||
let fraction = document.insertion_fraction_limit(class);
|
||||
let absolute = match routine {
|
||||
Some(routine) => {
|
||||
let slot = class as usize;
|
||||
let initial = routine
|
||||
.initial_dimens
|
||||
.iter()
|
||||
.find(|(number, _)| *number == class)
|
||||
.map_or(0, |(_, value)| *value);
|
||||
if routine.dimens[slot] == initial {
|
||||
class_backstop(document, class, fraction)
|
||||
} else {
|
||||
routine.dimens[slot]
|
||||
}
|
||||
}
|
||||
None => class_backstop(document, class, fraction),
|
||||
};
|
||||
fraction.min(absolute.max(0))
|
||||
}
|
||||
|
||||
fn class_backstop(document: &Document, class: u8, fraction: i64) -> i64 {
|
||||
document
|
||||
.insertion_classes
|
||||
.iter()
|
||||
.find(|entry| entry.number == class)
|
||||
.and_then(|entry| entry.max_height)
|
||||
.unwrap_or(fraction)
|
||||
}
|
||||
|
||||
pub struct Engine {
|
||||
layout: Layout,
|
||||
}
|
||||
@@ -1990,17 +2150,14 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
let builder = Self::paged_builder(document);
|
||||
let mut routine = match &document.output_routine {
|
||||
Some(source) => Some(PagedOutput::new(
|
||||
document,
|
||||
output::OutputProgram::parse(source).ok()?,
|
||||
)),
|
||||
None => None,
|
||||
};
|
||||
let mut pages = Vec::new();
|
||||
let mut page_penalties = Vec::new();
|
||||
let mut page_depths = Vec::new();
|
||||
let mut page_stretches = Vec::new();
|
||||
let mut page_fil_stretches = Vec::new();
|
||||
let mut page_fill_stretches = Vec::new();
|
||||
let mut page_filll_stretches = Vec::new();
|
||||
let mut page_shrinks = Vec::new();
|
||||
let mut page_badnesses = Vec::new();
|
||||
let mut page_insert_penalties = Vec::new();
|
||||
let mut page_goals: Vec<i64> = Vec::new();
|
||||
let mut held: BTreeMap<u8, Vec<VerticalItem>> = BTreeMap::new();
|
||||
let fragments = builder.build_with_deferred_bottom_floats(&nodes);
|
||||
for fragment in &fragments {
|
||||
@@ -2027,11 +2184,12 @@ impl Engine {
|
||||
let held_classes: Vec<u8> = held.keys().copied().collect();
|
||||
for class in held_classes {
|
||||
let material = held.remove(&class).unwrap_or_default();
|
||||
let limit = document.insertion_class_limit(class);
|
||||
let limit = output_insertion_limit(document, class, routine.as_ref());
|
||||
let capacity = (limit - used.get(&class).copied().unwrap_or(0)).max(0);
|
||||
let material = held_insertion_material(document, capacity, material);
|
||||
let multiplier = output_insertion_multiplier(document, class, routine.as_ref());
|
||||
let (accepted, remainder, reserved) =
|
||||
take_insertion_prefix(document, class, material, capacity);
|
||||
take_insertion_prefix(class, material, capacity, multiplier);
|
||||
attach_insertion(&mut insertions, &mut used, class, accepted, reserved);
|
||||
if !remainder.is_empty() {
|
||||
held.insert(class, remainder);
|
||||
@@ -2048,13 +2206,15 @@ impl Engine {
|
||||
VerticalItem::Mark(mark) => marks.push(mark.clone()),
|
||||
VerticalItem::Insertion(insertion) => {
|
||||
let class = insertion.class;
|
||||
let limit = document.insertion_class_limit(class);
|
||||
let limit = output_insertion_limit(document, class, routine.as_ref());
|
||||
let capacity = (limit - used.get(&class).copied().unwrap_or(0)).max(0);
|
||||
let multiplier =
|
||||
output_insertion_multiplier(document, class, routine.as_ref());
|
||||
let (accepted, remainder, reserved) = take_insertion_prefix(
|
||||
document,
|
||||
class,
|
||||
insertion.items.clone(),
|
||||
capacity,
|
||||
multiplier,
|
||||
);
|
||||
attach_insertion(&mut insertions, &mut used, class, accepted, reserved);
|
||||
if !remainder.is_empty() {
|
||||
@@ -2079,20 +2239,27 @@ impl Engine {
|
||||
page.marks = marks;
|
||||
page.floats = floats;
|
||||
let reserved: i64 = insertions.values().map(|insertion| insertion.height).sum();
|
||||
page_goals.push((document.page_goal - reserved).max(0));
|
||||
page.insertions = insertions.into_values().collect();
|
||||
page.sources = sources;
|
||||
page.seal_in_place();
|
||||
pages.push(page);
|
||||
page_penalties.push(fragment.penalty);
|
||||
page_depths.push(fragment.depth);
|
||||
page_stretches.push(fragment.stretch);
|
||||
page_fil_stretches.push(fragment.stretches[1]);
|
||||
page_fill_stretches.push(fragment.stretches[2]);
|
||||
page_filll_stretches.push(fragment.stretches[3]);
|
||||
page_shrinks.push(fragment.shrink);
|
||||
page_badnesses.push(fragment.badness);
|
||||
page_insert_penalties.push(insert_penalties);
|
||||
let shipped = match routine.as_mut() {
|
||||
Some(routine) => routine.ship(
|
||||
document,
|
||||
&page,
|
||||
(document.page_goal - reserved).max(0),
|
||||
fragment.penalty,
|
||||
fragment.depth,
|
||||
fragment.stretch,
|
||||
fragment.stretches,
|
||||
fragment.shrink,
|
||||
fragment.badness,
|
||||
insert_penalties,
|
||||
)?,
|
||||
None => true,
|
||||
};
|
||||
if shipped {
|
||||
pages.push(page);
|
||||
}
|
||||
}
|
||||
|
||||
// Insertion material that did not fit its class limit on the page where
|
||||
@@ -2105,8 +2272,13 @@ impl Engine {
|
||||
let material = held.remove(&class).unwrap_or_default();
|
||||
let limit = document.insertion_class_limit(class);
|
||||
let material = held_insertion_material(document, limit, material);
|
||||
let multiplier = document
|
||||
.insertion_classes
|
||||
.iter()
|
||||
.find(|entry| entry.number == class)
|
||||
.map_or(1000, |entry| i64::from(entry.multiplier));
|
||||
let (accepted, remainder, reserved) =
|
||||
take_insertion_prefix(document, class, material, limit);
|
||||
take_insertion_prefix(class, material, limit, multiplier);
|
||||
attach_insertion(&mut insertions, &mut used, class, accepted, reserved);
|
||||
if !remainder.is_empty() {
|
||||
held.insert(class, remainder);
|
||||
@@ -2114,86 +2286,26 @@ impl Engine {
|
||||
}
|
||||
let mut page = Page::empty();
|
||||
let reserved: i64 = insertions.values().map(|insertion| insertion.height).sum();
|
||||
page_goals.push((document.page_goal - reserved).max(0));
|
||||
page.insertions = insertions.into_values().collect();
|
||||
page.seal_in_place();
|
||||
pages.push(page);
|
||||
page_penalties.push(10_000);
|
||||
page_depths.push(0);
|
||||
page_stretches.push(0);
|
||||
page_fil_stretches.push(0);
|
||||
page_fill_stretches.push(0);
|
||||
page_filll_stretches.push(0);
|
||||
page_shrinks.push(0);
|
||||
page_badnesses.push(10_000);
|
||||
page_insert_penalties.push(0);
|
||||
}
|
||||
if let Some(source) = &document.output_routine {
|
||||
let program = output::OutputProgram::parse(source).ok()?;
|
||||
let mut shipped = Vec::new();
|
||||
let mut deadcycles = 0_i64;
|
||||
let mut pageno = 1_i64;
|
||||
// TeX's output registers are global: assignments made by the
|
||||
// routine on one page stay visible on later pages.
|
||||
let mut insertion_counts = [1000_i64; 256];
|
||||
let mut insertion_dimens = [0_i64; 256];
|
||||
let mut insertion_skips = [0_i64; 256];
|
||||
for class in &document.insertion_classes {
|
||||
let slot = class.number as usize;
|
||||
insertion_counts[slot] = i64::from(class.multiplier);
|
||||
insertion_dimens[slot] = class.max_height.unwrap_or(0);
|
||||
insertion_skips[slot] = class.skip.unwrap_or(document.insertion_skip);
|
||||
let shipped = match routine.as_mut() {
|
||||
Some(routine) => routine.ship(
|
||||
document,
|
||||
&page,
|
||||
(document.page_goal - reserved).max(0),
|
||||
10_000,
|
||||
0,
|
||||
0,
|
||||
[0; 4],
|
||||
0,
|
||||
10_000,
|
||||
0,
|
||||
)?,
|
||||
None => true,
|
||||
};
|
||||
if shipped {
|
||||
pages.push(page);
|
||||
}
|
||||
for (index, page) in pages.into_iter().enumerate() {
|
||||
let page_total: i64 = page.items.iter().map(item_height).sum();
|
||||
let mut context = output::OutputContext {
|
||||
page_goal: page_goals.get(index).copied().unwrap_or(document.page_goal),
|
||||
page_total,
|
||||
page_depth: page_depths.get(index).copied().unwrap_or(0),
|
||||
page_stretch: page_stretches.get(index).copied().unwrap_or(0),
|
||||
page_fil_stretch: page_fil_stretches.get(index).copied().unwrap_or(0),
|
||||
page_fill_stretch: page_fill_stretches.get(index).copied().unwrap_or(0),
|
||||
page_filll_stretch: page_filll_stretches.get(index).copied().unwrap_or(0),
|
||||
page_shrink: page_shrinks.get(index).copied().unwrap_or(0),
|
||||
vbadness: page_badnesses.get(index).copied().unwrap_or(10_000),
|
||||
hsize: document.text_width,
|
||||
vsize: document.page_goal,
|
||||
top_skip: document.top_skip,
|
||||
max_depth: document.max_depth,
|
||||
output_penalty: page_penalties.get(index).copied().unwrap_or(10_000),
|
||||
max_deadcycles: document.max_deadcycles,
|
||||
insertion_counts,
|
||||
insertion_dimens,
|
||||
insertion_skips,
|
||||
pageno,
|
||||
deadcycles,
|
||||
prev_graf: page_prev_graf(&page),
|
||||
insert_penalties: page_insert_penalties.get(index).copied().unwrap_or(0),
|
||||
};
|
||||
let forced = deadcycles >= document.max_deadcycles.max(0);
|
||||
if forced {
|
||||
context.deadcycles = 0;
|
||||
}
|
||||
let shipped_now = if forced {
|
||||
1
|
||||
} else {
|
||||
program.shipout_count(&mut context).ok()?
|
||||
};
|
||||
insertion_counts = context.insertion_counts;
|
||||
insertion_dimens = context.insertion_dimens;
|
||||
insertion_skips = context.insertion_skips;
|
||||
pageno = context.pageno.wrapping_add(1);
|
||||
deadcycles = context.deadcycles;
|
||||
if shipped_now > 0 {
|
||||
shipped.push(page);
|
||||
deadcycles = 0;
|
||||
} else {
|
||||
// TeX counts a page the output routine does not ship as a
|
||||
// dead cycle; the count resets on the next shipout.
|
||||
deadcycles += 1;
|
||||
}
|
||||
}
|
||||
pages = shipped;
|
||||
}
|
||||
let paged_page_nodes =
|
||||
if document.output_routine.is_none() && pages.len() == fragments.len() {
|
||||
@@ -3615,6 +3727,84 @@ mod tests {
|
||||
assert_eq!(Engine::layout(&document).pages().len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn output_routine_dimen_limits_later_insertions() {
|
||||
let items = || {
|
||||
(0..8)
|
||||
.map(|_| VerticalItem::Rule {
|
||||
width: 65_536,
|
||||
height: 10,
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
let mut document = Document::new(
|
||||
100,
|
||||
vec![
|
||||
Unit::insertion(1, 1, 80, items()),
|
||||
Unit::vskip(2, 65_536),
|
||||
Unit::insertion(3, 1, 80, items()),
|
||||
],
|
||||
);
|
||||
document.insertion_classes = vec![InsertionClass {
|
||||
number: 1,
|
||||
fraction_percent: 100,
|
||||
max_height: None,
|
||||
skip: None,
|
||||
multiplier: 1000,
|
||||
}];
|
||||
|
||||
let without = Engine::layout_paged(&document).expect("paged layout");
|
||||
assert_eq!(
|
||||
without.layout.pages.len(),
|
||||
2,
|
||||
"both insertions should fit before any assignment"
|
||||
);
|
||||
assert_eq!(without.layout.pages[0].insertions[0].height, 80);
|
||||
|
||||
document.output_routine =
|
||||
Some(r"\ifnum\pageno=1 \dimen1=30 \fi \shipout\box255".to_string());
|
||||
let with = Engine::layout_paged(&document).expect("paged layout");
|
||||
assert_eq!(with.layout.pages.len(), 3);
|
||||
assert_eq!(with.layout.pages[0].insertions[0].height, 80);
|
||||
assert_eq!(with.layout.pages[1].insertions[0].height, 30);
|
||||
assert_eq!(with.layout.pages[2].insertions[0].height, 50);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn output_routine_count_changes_later_insertion_reservations() {
|
||||
let items = || {
|
||||
(0..4)
|
||||
.map(|_| VerticalItem::Rule {
|
||||
width: 65_536,
|
||||
height: 10,
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
let mut document = Document::new(
|
||||
100,
|
||||
vec![
|
||||
Unit::insertion(1, 1, 40, items()),
|
||||
Unit::vskip(2, 65_536),
|
||||
Unit::insertion(3, 1, 40, items()),
|
||||
],
|
||||
);
|
||||
document.insertion_classes = vec![InsertionClass {
|
||||
number: 1,
|
||||
fraction_percent: 100,
|
||||
max_height: None,
|
||||
skip: None,
|
||||
multiplier: 1000,
|
||||
}];
|
||||
let without = Engine::layout_paged(&document).expect("paged layout");
|
||||
assert_eq!(without.layout.pages[0].insertions[0].height, 40);
|
||||
assert_eq!(without.layout.pages[1].insertions[0].height, 40);
|
||||
|
||||
document.output_routine =
|
||||
Some(r"\ifnum\pageno=1 \count1=2000 \fi \shipout\box255".to_string());
|
||||
let with = Engine::layout_paged(&document).expect("paged layout");
|
||||
assert_eq!(with.layout.pages[1].insertions[0].height, 80);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insertion_class_limit_ships_the_overflow_to_the_next_page() {
|
||||
let rule = VerticalItem::Rule {
|
||||
|
||||
Reference in new issue
Block a user