Add a shipout hook for the output stage
This commit is contained in:
1 parent
bef442684e
commit
17bd2d6237
2 files changed
+51
-4
No files matched your search
@@ -172,12 +172,31 @@ impl Layout {
|
||||
}
|
||||
}
|
||||
|
||||
/// Shipout hook corresponding to TeX's `\output` stage.
|
||||
///
|
||||
/// This is deliberately small: the routine can inspect and mutate each
|
||||
/// assembled page (and later change page styles) but it is Rust code rather
|
||||
/// than an interpreted token list.
|
||||
pub trait OutputRoutine {
|
||||
fn shipout(&mut self, page: &mut Page, page_number: usize);
|
||||
}
|
||||
|
||||
pub struct NoOutput;
|
||||
|
||||
impl OutputRoutine for NoOutput {
|
||||
fn shipout(&mut self, _page: &mut Page, _page_number: usize) {}
|
||||
}
|
||||
|
||||
pub struct Engine {
|
||||
layout: Layout,
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
pub fn layout(document: &Document) -> Self {
|
||||
Self::layout_with_output(document, &mut NoOutput)
|
||||
}
|
||||
|
||||
pub fn layout_with_output(document: &Document, output: &mut impl OutputRoutine) -> Self {
|
||||
let mut pages = Vec::new();
|
||||
let mut checkpoints = Vec::with_capacity(document.units.len());
|
||||
let mut current = Page::empty();
|
||||
@@ -247,7 +266,8 @@ impl Engine {
|
||||
page.sources.push((unit.id, source_hash(&unit.source)));
|
||||
}
|
||||
}
|
||||
for page in &mut pages {
|
||||
for (index, page) in pages.iter_mut().enumerate() {
|
||||
output.shipout(page, index + 1);
|
||||
page.seal_in_place();
|
||||
}
|
||||
Self {
|
||||
@@ -567,6 +587,31 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
struct PageStamp {
|
||||
numbers: Vec<usize>,
|
||||
}
|
||||
|
||||
impl OutputRoutine for PageStamp {
|
||||
fn shipout(&mut self, page: &mut Page, page_number: usize) {
|
||||
self.numbers.push(page_number);
|
||||
page.marks.push(Mark {
|
||||
class: 9,
|
||||
text: format!("page {page_number}"),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn output_routine_sees_and_mutates_each_page() {
|
||||
let mut stamp = PageStamp {
|
||||
numbers: Vec::new(),
|
||||
};
|
||||
let engine = Engine::layout_with_output(&document(), &mut stamp);
|
||||
assert_eq!(stamp.numbers.len(), engine.layout.pages.len());
|
||||
assert_eq!(stamp.numbers[0], 1);
|
||||
assert_eq!(engine.layout.pages[0].marks[0].text, "page 1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checkpoint_round_trips_through_serde() {
|
||||
let engine = Engine::layout(&document());
|
||||
|
||||
Reference in new issue
Block a user