245 lines
11 KiB
Rust
245 lines
11 KiB
Rust
//! Per-shipout device state. Original TeX boxes remain unscaled semantic lists.
|
|
use super::*;
|
|
use crate::pdf_device::{PdfColorProgram, PdfGraphics, ShippedColor};
|
|
use std::{collections::BTreeMap, sync::Arc};
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub struct ShipoutDevice {
|
|
pub magnification: i32,
|
|
/// Page and reference-point coordinates in original TeX sp.
|
|
pub page_width: i64,
|
|
pub page_height: i64,
|
|
pub horizontal_origin: i64,
|
|
pub vertical_origin: i64,
|
|
/// None denotes a raw DVI job; native PDF export rejects that backend.
|
|
pub pdf: Option<crate::pdf_device::PdfPageData>,
|
|
}
|
|
|
|
/// A resolved device special in packed-list order. The position is in
|
|
/// unscaled TeX sp relative to the box reference point, with y increasing down.
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub struct ShipoutSpecial {
|
|
pub span: Span,
|
|
pub position: [i64; 2],
|
|
pub bytes: Arc<[u8]>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub struct NativeShipout {
|
|
pub box_node: NativeBox,
|
|
pub device: ShipoutDevice,
|
|
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
|
pub(super) font_overrides: BTreeMap<FontId, Arc<crate::native_font::NativeFont>>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub(super) virtual_glyphs: Option<crate::native_font::VirtualGlyphs>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub(super) pdf_font_bindings: Option<Arc<crate::native_font::PdfFontBindings>>,
|
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
|
pub(super) pdf_color_start: Vec<ShippedColor>,
|
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
|
pub(super) pdf_color_steps: Vec<Option<ShippedColor>>,
|
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
|
pub(super) pdf_graphics_steps: Vec<Option<PdfGraphics>>,
|
|
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
|
pub(super) xdv_color_steps: BTreeMap<usize, Arc<PdfColorProgram>>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub(super) pdf_background: Option<Arc<PdfColorProgram>>,
|
|
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
|
pub(super) pdf_special_images: BTreeMap<usize, (Arc<crate::pdf_image::PdfImageUse>, TokenOrigin)>,
|
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
|
pub specials: Vec<ShipoutSpecial>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub(super) pdf_text_spaces: Option<crate::pdf_device::text_spaces::SpaceSettings>,
|
|
#[serde(default, skip_serializing_if = "pdf_resource_entries_empty")]
|
|
pub(super) pdf_resource_entries: Arc<[u8]>,
|
|
#[serde(default, skip_serializing_if = "pdf_resource_entries_empty")]
|
|
pub(super) pdf_page_attributes: Arc<[u8]>,
|
|
#[serde(default, skip_serializing_if = "crate::pdf_device::links::PageLinks::is_empty")]
|
|
pub(super) pdf_links: crate::pdf_device::links::PageLinks,
|
|
}
|
|
fn pdf_resource_entries_empty(entries: &Arc<[u8]>) -> bool {
|
|
entries.is_empty()
|
|
}
|
|
|
|
impl NativeShipout {
|
|
pub fn display_list(&self) -> Result<crate::render::DisplayList, String> {
|
|
self.display_list_prefix(None)
|
|
}
|
|
|
|
/// Use the same packed geometry up to, but excluding, a deferred output
|
|
/// that is about to expand tokens against the live TeX state.
|
|
pub(super) fn display_list_prefix(
|
|
&self,
|
|
before_output: Option<usize>,
|
|
) -> Result<crate::render::DisplayList, String> {
|
|
let mut page = crate::render::DisplayList {
|
|
width: self.device.page_width,
|
|
prepared_virtual_glyphs: self.virtual_glyphs.as_ref().map(|_| BTreeMap::new()),
|
|
height: self.device.page_height,
|
|
magnification: self.device.magnification,
|
|
units: crate::render::DisplayUnits::TeX,
|
|
pdf: self.device.pdf.clone(),
|
|
pdf_font_bindings: self.pdf_font_bindings.clone(),
|
|
pdf_links: self.pdf_links.clone(),
|
|
pdf_background: self.pdf_background.clone(),
|
|
pdf_text_spaces: self.pdf_text_spaces.clone(),
|
|
pdf_resources: crate::pdf_device::page_resources::PageResources {
|
|
entries: self.pdf_resource_entries.clone(),
|
|
attributes: self.pdf_page_attributes.clone(),
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
};
|
|
use crate::{math_engine_tex::MathItem, render::DisplayItem};
|
|
let left = self.device.horizontal_origin;
|
|
let baseline = self.device.vertical_origin + self.box_node.height;
|
|
page.items.extend(
|
|
self.pdf_color_start
|
|
.iter()
|
|
.map(|color| color.display_item(left, baseline)),
|
|
);
|
|
let mut color_steps = self.pdf_color_steps.iter();
|
|
let mut graphics_steps = self.pdf_graphics_steps.iter().enumerate();
|
|
let mut invalid_geometry = false;
|
|
let mut missing_color_step = false;
|
|
let mut missing_graphics_step = false;
|
|
let mut output_index = 0;
|
|
let mut reached_boundary = false;
|
|
self.box_node.visit_geometry(|item| {
|
|
if reached_boundary {
|
|
return;
|
|
}
|
|
let p = match item {
|
|
boxes::GeometryItem::Output => {
|
|
reached_boundary = before_output == Some(output_index);
|
|
output_index += 1;
|
|
return;
|
|
}
|
|
boxes::GeometryItem::Paint(p) => p,
|
|
boxes::GeometryItem::PdfColor { x, y } => {
|
|
match color_steps.next() {
|
|
Some(Some(color)) => {
|
|
page.items.push(color.display_item(left + x, baseline + y))
|
|
}
|
|
Some(None) => {}
|
|
None => missing_color_step = true,
|
|
}
|
|
return;
|
|
}
|
|
boxes::GeometryItem::PdfTextSpace { x, y, operation } => {
|
|
page.items.push(DisplayItem::PdfTextSpace {
|
|
x: self.device.horizontal_origin + x,
|
|
y: self.device.vertical_origin + self.box_node.height + y,
|
|
operation,
|
|
});
|
|
return;
|
|
}
|
|
boxes::GeometryItem::PdfGraphics { x, y } => {
|
|
match graphics_steps.next() {
|
|
Some((step, operation)) => {
|
|
if let Some(operation) = operation {
|
|
page.items.push(DisplayItem::PdfGraphics {
|
|
x: self.device.horizontal_origin + x,
|
|
y: self.device.vertical_origin + self.box_node.height + y,
|
|
operation: operation.clone(),
|
|
});
|
|
}
|
|
if let Some(program) = self.xdv_color_steps.get(&step) {
|
|
page.items.push(DisplayItem::PdfColor {
|
|
x: left + x,
|
|
y: baseline + y,
|
|
program: program.clone(),
|
|
mode: crate::pdf_device::PdfLiteralMode::Page,
|
|
});
|
|
}
|
|
if let Some((image, origin)) = self.pdf_special_images.get(&step) {
|
|
let [left, bottom] = image.xdv.as_ref().unwrap().layout_offset;
|
|
page.sources.insert(
|
|
page.items.len(),
|
|
crate::provenance::PaintSource::token(*origin),
|
|
);
|
|
page.items.push(DisplayItem::PdfImage {
|
|
x: self.device.horizontal_origin + x + left,
|
|
y: self.device.vertical_origin + self.box_node.height + y
|
|
- image.height - bottom,
|
|
width: image.width,
|
|
height: image.height,
|
|
image: image.clone(),
|
|
});
|
|
}
|
|
}
|
|
None => missing_graphics_step = true,
|
|
}
|
|
return;
|
|
}
|
|
};
|
|
if !p.source.tokens.is_empty() {
|
|
page.sources.insert(page.items.len(), p.source);
|
|
}
|
|
let x = self.device.horizontal_origin + p.x;
|
|
let y = self.device.vertical_origin + self.box_node.height + p.y;
|
|
if let MathItem::OpenType(run) = p.item {
|
|
run.paint(x, y, |value| value, |item, source| {
|
|
if !matches!(item, DisplayItem::OpenTypeActualText(_) | DisplayItem::OpenTypeColor(_)) {
|
|
page.sources.insert(page.items.len(), source);
|
|
}
|
|
page.items.push(item);
|
|
});
|
|
return;
|
|
}
|
|
page.items.push(match p.item {
|
|
MathItem::NativeGlyph {
|
|
font,
|
|
slot,
|
|
size,
|
|
character,
|
|
..
|
|
} => {
|
|
if let Some(packet) = self.virtual_glyphs.as_ref()
|
|
.and_then(|fonts| fonts.get(&font.id))
|
|
.and_then(|glyphs| glyphs.get(&slot))
|
|
{
|
|
page.prepared_virtual_glyphs.as_mut().unwrap()
|
|
.insert(page.items.len(), packet.clone());
|
|
}
|
|
DisplayItem::NativeGlyph {
|
|
x, y, size, slot, character, paint: None,
|
|
font: self.font_overrides.get(&font.id).cloned().unwrap_or(font),
|
|
}
|
|
},
|
|
MathItem::Rule { width, height } => DisplayItem::Rule {
|
|
x,
|
|
y,
|
|
width,
|
|
height,
|
|
},
|
|
MathItem::PdfForm(form) => DisplayItem::PdfForm { x, y, form },
|
|
MathItem::PdfImage(image) => DisplayItem::PdfImage {
|
|
x,
|
|
y: y - image.height,
|
|
width: image.width,
|
|
height: image.height + image.depth,
|
|
image,
|
|
},
|
|
_ => {
|
|
invalid_geometry = true;
|
|
return;
|
|
}
|
|
});
|
|
})?;
|
|
if before_output.is_some() && !reached_boundary {
|
|
return Err("missing deferred output boundary in its packed list".into());
|
|
}
|
|
if invalid_geometry {
|
|
return Err("native shipout requires actual native glyph resources".into());
|
|
}
|
|
if missing_color_step || color_steps.next().is_some() {
|
|
return Err("shipped PDF color order does not match its packed list".into());
|
|
}
|
|
if missing_graphics_step || graphics_steps.next().is_some() {
|
|
return Err("shipped PDF graphics order does not match its packed list".into());
|
|
}
|
|
Ok(page)
|
|
}
|
|
}
|