380 lines
15 KiB
Rust
380 lines
15 KiB
Rust
use super::super::boxes::{self, PackSpec, VerticalBuilder};
|
|
use super::*;
|
|
|
|
#[derive(Clone)]
|
|
pub(super) struct OutputFrame {
|
|
depth: usize,
|
|
paragraph: Option<super::super::paragraph::Builder>,
|
|
vertical: VerticalBuilder,
|
|
math: Vec<super::super::math::MathBuilder>,
|
|
display: Option<super::display::DisplayFrame>,
|
|
}
|
|
impl OutputFrame {
|
|
pub(super) fn collect_statistics(&self, c: &mut super::super::statistics::Collector) {
|
|
if let Some(p) = &self.paragraph {
|
|
c.paragraph_builder(p);
|
|
}
|
|
c.vertical(
|
|
&self.vertical.nodes,
|
|
super::super::statistics::Ownership::Active,
|
|
);
|
|
for m in &self.math {
|
|
c.math_builder(m);
|
|
}
|
|
}
|
|
}
|
|
impl Executor {
|
|
pub(super) fn outer_vertical(&self) -> bool {
|
|
self.box_frames.is_empty() && self.output_frame.is_none() && self.alignments.is_empty()
|
|
}
|
|
pub(super) fn native_page_mut(&mut self) -> Result<&mut NativePageState, Error> {
|
|
if self.native_page.is_none() {
|
|
return self.fail("native page/output execution requires a native job target");
|
|
}
|
|
self.reads.insert(Dependency::PageState);
|
|
Ok(self.native_page.as_mut().unwrap())
|
|
}
|
|
pub(super) fn page_dimension_value(&mut self, p: Primitive) -> Result<i32, Error> {
|
|
let value = self
|
|
.native_page_mut()?
|
|
.dimension(p.page_dimension().unwrap());
|
|
i32::try_from(value).map_err(|_| self.error("native page dimension overflow", self.current))
|
|
}
|
|
pub(super) fn request_page(&mut self) {
|
|
if self.outer_vertical() {
|
|
if let Some(page) = self.native_page.as_mut() {
|
|
page.requested = true;
|
|
}
|
|
}
|
|
}
|
|
pub(super) fn process_native_pages(
|
|
&mut self,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
while let Some(b) = self.pending_shipouts.pop_front() {
|
|
self.ship_box(b, files)?;
|
|
}
|
|
if self.native_page.is_none() {
|
|
return Ok(());
|
|
}
|
|
loop {
|
|
self.read_parameter(Parameter::Tracingpages);
|
|
let mut trace = Vec::new();
|
|
let next = self.native_page.as_mut().unwrap().advance(
|
|
&mut self.state,
|
|
&mut self.reads,
|
|
&mut self.steps,
|
|
self.limits.steps,
|
|
&mut trace,
|
|
);
|
|
for trace in trace {
|
|
let destination = if self.read_parameter(Parameter::Tracingonline).width() > 0 {
|
|
WriteDestination::TerminalAndLog
|
|
} else {
|
|
WriteDestination::Log
|
|
};
|
|
self.reads.insert(Dependency::NewLineChar);
|
|
let text = self.print_show_parts(&[&trace.text()])?;
|
|
self.diagnostic_bytes = self.diagnostic_bytes.saturating_add(text.len());
|
|
if self.diagnostic_bytes > self.limits.tokens {
|
|
return self.fail("native diagnostic output limit exceeded");
|
|
}
|
|
files
|
|
.observe_diagnostic(destination, &text)
|
|
.map_err(|e| self.error(e, self.current))?;
|
|
self.events.push(Event::PageTrace(Box::new(PageTraceReport {
|
|
trace,
|
|
text,
|
|
destination,
|
|
span: self.current,
|
|
})));
|
|
}
|
|
let next = next.map_err(|e| self.error(e, self.current))?;
|
|
let Some(next) = next else {
|
|
return Ok(());
|
|
};
|
|
self.reads.insert(Dependency::PageState);
|
|
for p in [Parameter::Vsize, Parameter::Maxdepth, Parameter::Topskip] {
|
|
self.reads.insert(Dependency::Parameter(p));
|
|
}
|
|
if self.state.box_register(255).is_some() {
|
|
return self.fail("native box255 is not void before output");
|
|
}
|
|
self.state.assign_parameter(
|
|
Parameter::Outputpenalty,
|
|
ParameterValue::Integer(next.penalty),
|
|
true,
|
|
);
|
|
for marks in self.state.page_marks.values_mut() {
|
|
if marks.bottom.is_some() {
|
|
marks.top = marks.bottom.clone();
|
|
marks.first = None;
|
|
}
|
|
}
|
|
for node in &next.nodes {
|
|
self.tick()?;
|
|
if let NativeVerticalNode::Mark(m) = node {
|
|
let marks = self.state.page_marks.entry(m.class).or_default();
|
|
if marks.first.is_none() {
|
|
marks.first = Some(m.tokens.clone());
|
|
}
|
|
marks.bottom = Some(m.tokens.clone());
|
|
}
|
|
}
|
|
for marks in self.state.page_marks.values_mut() {
|
|
if marks.first.is_none() {
|
|
marks.first = marks.top.clone();
|
|
}
|
|
}
|
|
let b = boxes::vpack(
|
|
next.nodes,
|
|
PackSpec::To(next.goal),
|
|
next.max_depth,
|
|
BoxKind::Vertical,
|
|
self.current,
|
|
)
|
|
.map_err(|e| self.error(e, self.current))?;
|
|
self.state.last_badness = b.badness;
|
|
self.account_box(&b)?;
|
|
self.state.set_box(255, Some(b));
|
|
self.reads
|
|
.insert(Dependency::TokenParameter(TokenParameter::Output));
|
|
if let Some(tokens) = self.state.token_parameter(TokenParameter::Output).cloned() {
|
|
let max = self.read_parameter(Parameter::Maxdeadcycles).width();
|
|
if self.native_page.as_ref().unwrap().dead_cycles >= max {
|
|
return self.fail("native output loop: too many consecutive dead cycles");
|
|
}
|
|
self.native_page.as_mut().unwrap().dead_cycles += 1;
|
|
self.native_page.as_mut().unwrap().output_active = true;
|
|
if self.inputs.len() >= self.limits.input_depth {
|
|
return self.fail("native output input stack limit exceeded");
|
|
}
|
|
self.output_frame = Some(OutputFrame {
|
|
depth: self.state.group_depth() + 1,
|
|
paragraph: self.paragraph.take(),
|
|
vertical: std::mem::take(&mut self.vertical),
|
|
math: std::mem::take(&mut self.math),
|
|
display: self.display.take(),
|
|
});
|
|
self.inputs.push(InputFrame::Tokens {
|
|
tokens: tokens.iter().cloned().map(InputToken::from).collect(),
|
|
cursor: 0,
|
|
kind: TokenInputKind::Output,
|
|
});
|
|
self.begin_group(GroupKind::Output, files)?;
|
|
self.normal_paragraph(files)?;
|
|
let open = loop {
|
|
let t = self.expanded_non_space(files)?.token;
|
|
if self.primitive(&t) != Some(Primitive::Relax) {
|
|
break t;
|
|
}
|
|
};
|
|
if !matches!(
|
|
self.meaning(&open),
|
|
Meaning::Character(TokenKind::Character(_, CatCode::BeginGroup))
|
|
) {
|
|
return self.fail("expected opening brace in native output routine");
|
|
}
|
|
return Ok(());
|
|
}
|
|
let b = self.state.take_box(255).unwrap();
|
|
self.native_page
|
|
.as_mut()
|
|
.unwrap()
|
|
.resume_contributions(Vec::new());
|
|
self.ship_box(b, files)?;
|
|
}
|
|
}
|
|
pub(super) fn close_output_group(
|
|
&mut self,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<bool, Error> {
|
|
if !self
|
|
.output_frame
|
|
.as_ref()
|
|
.is_some_and(|f| f.depth == self.state.group_depth())
|
|
{
|
|
return Ok(false);
|
|
}
|
|
if !matches!(self.inputs.last(),Some(InputFrame::Tokens{tokens,cursor,kind:TokenInputKind::Output|TokenInputKind::BackedUp})if *cursor==tokens.len())
|
|
|| self.inputs.iter().any(|f| matches!(f, InputFrame::Tokens { tokens, cursor, kind: TokenInputKind::Output } if *cursor != tokens.len()))
|
|
{
|
|
return self.fail("unbalanced native output routine");
|
|
}
|
|
self.inputs.pop();
|
|
if matches!(self.inputs.last(),Some(InputFrame::Tokens{tokens,cursor,kind:TokenInputKind::Output})if *cursor==tokens.len())
|
|
{
|
|
self.inputs.pop();
|
|
}
|
|
self.end_paragraph(files)?;
|
|
self.finish_group(GroupKind::Output, self.current, files)?;
|
|
if self.state.box_register(255).is_some() {
|
|
return self.fail("native output routine did not empty box255");
|
|
}
|
|
let nodes = self.vertical.take_nodes();
|
|
let frame = self.output_frame.take().unwrap();
|
|
self.paragraph = frame.paragraph;
|
|
self.vertical = frame.vertical;
|
|
self.math = frame.math;
|
|
self.display = frame.display;
|
|
let page = self.native_page.as_mut().unwrap();
|
|
page.output_active = false;
|
|
page.resume_contributions(nodes);
|
|
page.requested = true;
|
|
Ok(true)
|
|
}
|
|
pub(super) fn ship_box(
|
|
&mut self,
|
|
b: NativeBox,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
self.native_page_mut()?;
|
|
let h_offset = i64::from(self.read_parameter(Parameter::Hoffset).width());
|
|
let v_offset = i64::from(self.read_parameter(Parameter::Voffset).width());
|
|
const MAX_DIMEN: i64 = 1_073_741_823;
|
|
if b.height > MAX_DIMEN
|
|
|| b.depth > MAX_DIMEN
|
|
|| b.height + b.depth + v_offset > MAX_DIMEN
|
|
|| b.width + h_offset > MAX_DIMEN
|
|
{
|
|
return self.fail("huge native page cannot be shipped out");
|
|
}
|
|
let magnification = self.prepare_magnification()?;
|
|
let pdf = self.pdf_page_data(files)?;
|
|
let horizontal_origin =
|
|
i64::from(self.read_parameter(Parameter::Pdfhorigin).width()) + h_offset;
|
|
let vertical_origin =
|
|
i64::from(self.read_parameter(Parameter::Pdfvorigin).width()) + v_offset;
|
|
let mut page_width = i64::from(self.read_parameter(Parameter::Pdfpagewidth).width());
|
|
let mut page_height = i64::from(self.read_parameter(Parameter::Pdfpageheight).width());
|
|
if page_width == 0 {
|
|
page_width = b.width + 2 * horizontal_origin;
|
|
}
|
|
if page_height == 0 {
|
|
page_height = b.height + b.depth + 2 * vertical_origin;
|
|
}
|
|
let device = ShipoutDevice {
|
|
magnification,
|
|
page_width,
|
|
page_height,
|
|
horizontal_origin,
|
|
vertical_origin,
|
|
pdf,
|
|
};
|
|
self.trace_shipout(&b, files)?;
|
|
let current = self.current;
|
|
let pdf_color_start = if device.pdf.is_some() {
|
|
self.pdf_colors.start_page()
|
|
} else {
|
|
Vec::new()
|
|
};
|
|
let mut pdf_color_steps = Vec::new();
|
|
let mut pdf_graphics_steps = Vec::new();
|
|
let mut graphics_stack = Vec::new();
|
|
let outputs = b.output_nodes();
|
|
let mut link_depth = self.pdf_link_stack.len();
|
|
let has_link_material = link_depth != 0 || outputs.iter().any(|node| node.is_pdf_link());
|
|
let mut graphics_positions = if outputs.iter().any(|node| node.is_pdf_graphics()) {
|
|
b.pdf_graphics_positions().into_iter()
|
|
} else {
|
|
Vec::new().into_iter()
|
|
};
|
|
for output in outputs {
|
|
if let OutputOperation::PdfColor { stack, action } = &output.operation {
|
|
if device.pdf.is_none() {
|
|
return self.fail("cannot ship a PDF color operation to DVI");
|
|
}
|
|
pdf_color_steps.push(self.ship_pdf_color(*stack, action, output.span, files)?);
|
|
} else if output.is_pdf_graphics() {
|
|
if device.pdf.is_none() {
|
|
return self.fail("cannot ship a PDF graphics operation to DVI");
|
|
}
|
|
let point = graphics_positions.next().ok_or_else(|| {
|
|
self.error("missing positioned PDF graphics node", output.span)
|
|
})?;
|
|
pdf_graphics_steps.push(self.ship_pdf_graphics(
|
|
output,
|
|
point,
|
|
&mut graphics_stack,
|
|
files,
|
|
)?);
|
|
} else if let OutputOperation::PdfLink(link) = &output.operation {
|
|
use crate::pdf_device::links::LinkNode;
|
|
self.current = output.span;
|
|
self.tick()?;
|
|
if device.pdf.is_none() {
|
|
return self.fail("cannot ship a PDF link operation to DVI");
|
|
}
|
|
match link {
|
|
LinkNode::Start(_) if link_depth == 10 => return self.fail("PDF link stack capacity exceeded"),
|
|
LinkNode::Start(_) => link_depth += 1,
|
|
LinkNode::End if link_depth == 0 => return self.fail("\\pdfendlink used without \\pdfstartlink"),
|
|
LinkNode::End => link_depth -= 1,
|
|
LinkNode::Destination { .. } => {}
|
|
}
|
|
} else {
|
|
self.output_node(output, files)?;
|
|
}
|
|
}
|
|
if graphics_positions.next().is_some() {
|
|
return self.fail("unexpected positioned PDF graphics node");
|
|
}
|
|
if let Some((_, span)) = graphics_stack.last() {
|
|
return Err(self.error(
|
|
format!(
|
|
"{} unmatched \\pdfsave after page shipout",
|
|
graphics_stack.len()
|
|
),
|
|
*span,
|
|
));
|
|
}
|
|
let pdf_links = if has_link_material {
|
|
self.ship_pdf_link_material(&b, &device, &pdf_graphics_steps, files)?
|
|
} else {
|
|
Default::default()
|
|
};
|
|
self.current = current;
|
|
self.native_page.as_mut().unwrap().dead_cycles = 0;
|
|
let before = self.statistics_before_shipout(&b, files)?;
|
|
self.events.push(Event::Shipout(Box::new(NativeShipout {
|
|
box_node: b,
|
|
device,
|
|
pdf_color_start,
|
|
pdf_color_steps,
|
|
pdf_graphics_steps,
|
|
pdf_links,
|
|
})));
|
|
self.statistics_after_shipout(before, files)?;
|
|
Ok(())
|
|
}
|
|
pub(super) fn end_native_job(&mut self, token: Token) -> Result<bool, Error> {
|
|
if !self.outer_vertical() || self.paragraph.is_some() || !self.math.is_empty() {
|
|
return self.fail("end is not allowed in an internal native mode");
|
|
}
|
|
let page = self.native_page.as_ref().unwrap();
|
|
if page.nodes.is_empty() && page.contributions.is_empty() && page.dead_cycles == 0 {
|
|
return Ok(true);
|
|
}
|
|
self.unread(token.clone().into())?;
|
|
let width = self.read_parameter(Parameter::Hsize).width();
|
|
let b = boxes::hpack(Vec::new(), PackSpec::To(width), token.span, &self.state)
|
|
.map_err(|e| self.error(e, token.span))?;
|
|
let page = self.native_page.as_mut().unwrap();
|
|
// its_all_over appends directly, without append_to_vlist/baselineskip.
|
|
page.contributions
|
|
.push_back(NativeVerticalNode::Box(Box::new(b)));
|
|
page.contributions.push_back(NativeVerticalNode::Glue(Glue {
|
|
canonical_zero: false,
|
|
stretch: Flex {
|
|
amount: 65536,
|
|
order: 2,
|
|
},
|
|
..Glue::default()
|
|
}));
|
|
page.contributions
|
|
.push_back(NativeVerticalNode::Penalty(-1_073_741_824));
|
|
page.requested = true;
|
|
Ok(false)
|
|
}
|
|
}
|