Ship the page after maxdeadcycles
This commit is contained in:
1 parent
8007fd1b6c
commit
4111080422
4 files changed
+84
-8
No files matched your search
@@ -673,6 +673,10 @@ pub struct Document {
|
||||
/// Extra vertical displacement of the page origin (`\voffset`).
|
||||
#[serde(default)]
|
||||
pub v_offset: i64,
|
||||
/// TeX `\maxdeadcycles`: how many pages the output routine may decline to
|
||||
/// ship before TeX ships the page itself. Defaults to 25.
|
||||
#[serde(default = "default_max_deadcycles")]
|
||||
pub max_deadcycles: i64,
|
||||
/// TeX `\output` token list, interpreted by [`output::OutputProgram`].
|
||||
/// `None` ships every page the builder produces.
|
||||
#[serde(default)]
|
||||
@@ -731,6 +735,10 @@ fn default_total_number() -> u8 {
|
||||
12
|
||||
}
|
||||
|
||||
fn default_max_deadcycles() -> i64 {
|
||||
25
|
||||
}
|
||||
|
||||
impl Document {
|
||||
pub fn new(page_goal: i64, units: Vec<Unit>) -> Self {
|
||||
Self {
|
||||
@@ -757,6 +765,7 @@ impl Document {
|
||||
par_skip: 0,
|
||||
h_offset: 0,
|
||||
v_offset: 0,
|
||||
max_deadcycles: default_max_deadcycles(),
|
||||
output_routine: None,
|
||||
}
|
||||
}
|
||||
@@ -1972,6 +1981,7 @@ impl Engine {
|
||||
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,
|
||||
@@ -1979,7 +1989,15 @@ impl Engine {
|
||||
deadcycles,
|
||||
prev_graf: page_prev_graf(&page),
|
||||
};
|
||||
let shipped_now = program.shipout_count(&mut context).ok()?;
|
||||
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;
|
||||
@@ -2162,6 +2180,7 @@ impl Engine {
|
||||
top_skip: document.top_skip,
|
||||
max_depth: document.max_depth,
|
||||
output_penalty: fragment.penalty,
|
||||
max_deadcycles: document.max_deadcycles,
|
||||
insertion_counts,
|
||||
insertion_dimens,
|
||||
insertion_skips,
|
||||
@@ -2169,7 +2188,15 @@ impl Engine {
|
||||
pageno,
|
||||
deadcycles,
|
||||
};
|
||||
let shipped = program.shipout_count(&mut context).ok()?;
|
||||
let forced = deadcycles >= document.max_deadcycles.max(0);
|
||||
if forced {
|
||||
context.deadcycles = 0;
|
||||
}
|
||||
let shipped = 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;
|
||||
@@ -6015,6 +6042,35 @@ mod tests {
|
||||
assert_eq!(item_height(&engine.pages()[0].items[0]), 70 * pt);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn max_deadcycles_forces_a_page_out() {
|
||||
let document_with = |limit: i64| {
|
||||
let mut document = Document::new(
|
||||
100,
|
||||
vec![
|
||||
Unit::rule(1, 10, 90),
|
||||
Unit::penalty(2, -10_000),
|
||||
Unit::rule(3, 10, 90),
|
||||
Unit::penalty(4, -10_000),
|
||||
Unit::rule(5, 10, 90),
|
||||
Unit::penalty(6, -10_000),
|
||||
Unit::rule(7, 10, 90),
|
||||
],
|
||||
);
|
||||
document.output_routine = Some(r"\relax".into());
|
||||
document.max_deadcycles = limit;
|
||||
document
|
||||
};
|
||||
// With the default limit no page is ever shipped.
|
||||
let idle = Engine::layout_paged(&document_with(25)).expect("paged layout");
|
||||
assert_eq!(idle.pages().len(), 0);
|
||||
// After two dead cycles TeX ships the third page itself, then the
|
||||
// fourth page starts a fresh dead cycle and is dropped too.
|
||||
let forced = Engine::layout_paged(&document_with(2)).expect("paged layout");
|
||||
assert_eq!(forced.pages().len(), 1);
|
||||
assert_eq!(item_height(&forced.pages()[0].items[0]), 90);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn output_register_assignments_persist_across_pages() {
|
||||
let mut document = Document::new(
|
||||
|
||||
@@ -42,6 +42,9 @@ pub struct OutputContext {
|
||||
pub pageno: i64,
|
||||
/// TeX `\deadcycles`: output routines since the last shipout.
|
||||
pub deadcycles: i64,
|
||||
/// TeX `\maxdeadcycles`: dead cycles allowed before TeX ships the
|
||||
/// page itself.
|
||||
pub max_deadcycles: i64,
|
||||
/// TeX `\prevgraf`: lines in the paragraph most recently broken.
|
||||
pub prev_graf: i64,
|
||||
/// TeX `\count<n>` for insertion class `n`: the insertion multiplier.
|
||||
@@ -69,6 +72,7 @@ impl Default for OutputContext {
|
||||
output_penalty: 0,
|
||||
pageno: 0,
|
||||
deadcycles: 0,
|
||||
max_deadcycles: 25,
|
||||
prev_graf: 0,
|
||||
// TeX's default insertion multiplier is 1000.
|
||||
insertion_counts: [1000; 256],
|
||||
@@ -231,6 +235,7 @@ enum Integer {
|
||||
enum IntegerRegister {
|
||||
PageNo,
|
||||
DeadCycles,
|
||||
MaxDeadCycles,
|
||||
OutputPenalty,
|
||||
PrevGraf,
|
||||
InsertionCount(u8),
|
||||
@@ -243,6 +248,7 @@ impl Integer {
|
||||
Self::Register(register) => match register {
|
||||
IntegerRegister::PageNo => context.pageno,
|
||||
IntegerRegister::DeadCycles => context.deadcycles,
|
||||
IntegerRegister::MaxDeadCycles => context.max_deadcycles,
|
||||
IntegerRegister::OutputPenalty => context.output_penalty,
|
||||
IntegerRegister::PrevGraf => context.prev_graf,
|
||||
IntegerRegister::InsertionCount(class) => context.insertion_counts[class as usize],
|
||||
@@ -420,12 +426,13 @@ fn parse_sequence(tokens: &[Token], index: &mut usize, stop: &[&str]) -> Result<
|
||||
}
|
||||
ops.push(Op::Message(text));
|
||||
}
|
||||
"pageno" | "deadcycles" | "outputpenalty" | "count" | "dimen" | "skip"
|
||||
| "vsize" => {
|
||||
"pageno" | "deadcycles" | "maxdeadcycles" | "outputpenalty" | "count" | "dimen"
|
||||
| "skip" | "vsize" => {
|
||||
*index += 1;
|
||||
let (integer_register, dimension_register) = match name.as_str() {
|
||||
"pageno" => (Some(IntegerRegister::PageNo), None),
|
||||
"deadcycles" => (Some(IntegerRegister::DeadCycles), None),
|
||||
"maxdeadcycles" => (Some(IntegerRegister::MaxDeadCycles), None),
|
||||
"outputpenalty" => (Some(IntegerRegister::OutputPenalty), None),
|
||||
"count" => {
|
||||
let class = parse_insertion_class(tokens, index, "\\count", false)?;
|
||||
@@ -591,6 +598,7 @@ fn parse_integer(tokens: &[Token], index: &mut usize) -> Result<Integer, String>
|
||||
let register = match name.as_str() {
|
||||
"pageno" => IntegerRegister::PageNo,
|
||||
"deadcycles" => IntegerRegister::DeadCycles,
|
||||
"maxdeadcycles" => IntegerRegister::MaxDeadCycles,
|
||||
"outputpenalty" => IntegerRegister::OutputPenalty,
|
||||
"prevgraf" => IntegerRegister::PrevGraf,
|
||||
"count" => {
|
||||
@@ -773,6 +781,7 @@ fn execute(ops: &[Op], context: &mut OutputContext, shipped: &mut usize) -> Resu
|
||||
match register {
|
||||
IntegerRegister::PageNo => context.pageno = value,
|
||||
IntegerRegister::DeadCycles => context.deadcycles = value,
|
||||
IntegerRegister::MaxDeadCycles => context.max_deadcycles = value,
|
||||
IntegerRegister::OutputPenalty => context.output_penalty = value,
|
||||
IntegerRegister::PrevGraf => context.prev_graf = value,
|
||||
IntegerRegister::InsertionCount(class) => {
|
||||
@@ -820,6 +829,7 @@ mod tests {
|
||||
output_penalty: 0,
|
||||
pageno: 1,
|
||||
deadcycles: 0,
|
||||
max_deadcycles: 25,
|
||||
prev_graf: 0,
|
||||
..OutputContext::default()
|
||||
}
|
||||
@@ -979,6 +989,16 @@ mod tests {
|
||||
assert_eq!(program.shipout_count(&mut context).unwrap(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maxdeadcycles_is_readable_and_assignable() {
|
||||
let program =
|
||||
OutputProgram::parse(r"\ifnum\maxdeadcycles=25\shipout\box255\fi\maxdeadcycles=3")
|
||||
.unwrap();
|
||||
let mut context = context(10);
|
||||
assert_eq!(program.shipout_count(&mut context).unwrap(), 1);
|
||||
assert_eq!(context.max_deadcycles, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ifnum_reads_negative_output_penalty() {
|
||||
let program =
|
||||
|
||||
Reference in new issue
Block a user