Support classed marks in the output routine

This commit is contained in:
gloamxun committed 2026-10-24 00:39:59 +08:00
1 parent 91171f2a90
commit a93200cbfb
4 files changed
+76 -34

No files matched your search

+21 -3
View File
@@ -1524,7 +1524,7 @@ struct PagedOutput {
skips: [i64; 256],
initial_dimens: Vec<(u8, i64)>,
initial_skips: Vec<(u8, i64)>,
pending_marks: Vec<String>,
pending_marks: Vec<(u8, String)>,
}
impl PagedOutput {
@@ -2196,7 +2196,7 @@ impl Engine {
let mut pages = Vec::new();
let mut held: BTreeMap<u8, Vec<VerticalItem>> = BTreeMap::new();
let mut fragments = builder.build_with_deferred_bottom_floats(&nodes);
let mut carried_marks: Vec<String> = Vec::new();
let mut carried_marks: Vec<(u8, String)> = Vec::new();
let mut page_index = 0;
while page_index < fragments.len() {
let fragment = fragments[page_index].clone();
@@ -2279,7 +2279,7 @@ impl Engine {
if !carried_marks.is_empty() {
let mut marks_with_carry: Vec<Mark> = std::mem::take(&mut carried_marks)
.into_iter()
.map(|text| Mark { class: 0, text })
.map(|(class, text)| Mark { class, text })
.collect();
marks_with_carry.append(&mut marks);
marks = marks_with_carry;
@@ -3484,6 +3484,24 @@ mod tests {
assert_eq!(engine.layout.pages[1].marks[0].class, 0);
}
#[test]
fn output_marks_carry_their_class() {
let mut units = Vec::new();
for step in 0..3_u64 {
units.push(Unit::rule(step * 2, 300, 600));
if step < 2 {
units.push(Unit::vskip(step * 2 + 1, 0));
}
}
let mut document = Document::new(1000, units);
document.top_skip = 0;
document.output_routine = Some(r"\marks3{carried}\shipout\box255".to_owned());
let engine = Engine::layout_paged(&document).expect("paged");
assert_eq!(engine.layout.pages.len(), 3);
assert_eq!(engine.layout.pages[1].marks[0].class, 3);
assert_eq!(engine.layout.pages[1].marks[0].text, "carried");
}
#[test]
fn output_routine_can_advance_vsize_for_later_pages() {
let mut units = Vec::new();
+50 -26
View File
@@ -59,8 +59,9 @@ pub struct OutputContext {
pub insertion_dimens: [i64; 256],
/// TeX `\skip<n>` for insertion class `n`.
pub insertion_skips: [i64; 256],
/// `\mark{...}` texts written by the routine; they reach later pages.
pub output_marks: Vec<String>,
/// `\mark{...}`/`\marks<n>{...}` texts written by the routine; they
/// reach later pages with their class.
pub output_marks: Vec<(u8, String)>,
}
impl Default for OutputContext {
@@ -155,7 +156,10 @@ enum Op {
Shipout,
Relax,
Message(String),
Mark(String),
Mark {
class: u8,
text: String,
},
IfDim {
left: Dimension,
relation: Relation,
@@ -520,31 +524,28 @@ fn parse_sequence(tokens: &[Token], index: &mut usize, stop: &[&str]) -> Result<
*index += 1;
ops.push(Op::Relax);
}
"message" | "mark" => {
let op_name = name.to_owned();
"message" => {
*index += 1;
if !matches!(tokens.get(*index), Some(Token::Open)) {
return Err(format!("\\{op_name} expects a braced argument"));
}
let text = read_braced(tokens, index, "\\message")?;
ops.push(Op::Message(text));
}
"mark" | "marks" => {
*index += 1;
let mut text = String::new();
while *index < tokens.len() {
match &tokens[*index] {
Token::Close => {
*index += 1;
break;
}
token => {
text.push_str(&token.to_string());
*index += 1;
}
}
}
ops.push(if op_name == "mark" {
Op::Mark(text)
let class = if name == "mark" {
0_u8
} else {
Op::Message(text)
});
let class = match tokens.get(*index) {
Some(Token::Number { value, unit: None }) => value.round() as i64,
_ => return Err("\\marks expects a class number".to_owned()),
};
*index += 1;
if !(0..=255).contains(&class) {
return Err("\\marks class must be between 0 and 255".to_owned());
}
class as u8
};
let text = read_braced(tokens, index, "\\mark")?;
ops.push(Op::Mark { class, text });
}
"advance" | "multiply" | "divide" => {
*index += 1;
@@ -876,12 +877,35 @@ fn parse_insertion_class(
}
}
fn read_braced(tokens: &[Token], index: &mut usize, name: &str) -> Result<String, String> {
if !matches!(tokens.get(*index), Some(Token::Open)) {
return Err(format!("{name} expects a braced argument"));
}
*index += 1;
let mut text = String::new();
while *index < tokens.len() {
match &tokens[*index] {
Token::Close => {
*index += 1;
break;
}
token => {
text.push_str(&token.to_string());
*index += 1;
}
}
}
Ok(text)
}
fn execute(ops: &[Op], context: &mut OutputContext, shipped: &mut usize) -> Result<(), String> {
for op in ops {
match op {
Op::Shipout => *shipped += 1,
Op::Relax | Op::Message(_) => {}
Op::Mark(text) => context.output_marks.push(text.clone()),
Op::Mark { class, text } => {
context.output_marks.push((*class, text.clone()));
}
Op::IfDim {
left,
relation,