Files
texrra-engine/crates/texrra-tex-core/src/execution/device.rs
T

156 lines
6.3 KiB
Rust

//! Per-shipout device state. Original TeX boxes remain unscaled semantic lists.
use super::*;
use crate::pdf_device::{PdfColorProgram, PdfGraphics};
use std::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>,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct NativeShipout {
pub box_node: NativeBox,
pub device: ShipoutDevice,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub(super) pdf_color_start: Vec<Arc<PdfColorProgram>>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub(super) pdf_color_steps: Vec<Option<Arc<PdfColorProgram>>>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub(super) pdf_graphics_steps: Vec<PdfGraphics>,
#[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> {
let mut page = crate::render::DisplayList {
width: self.device.page_width,
height: self.device.page_height,
magnification: self.device.magnification,
units: crate::render::DisplayUnits::TeX,
pdf: self.device.pdf.clone(),
pdf_links: self.pdf_links.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};
page.items.extend(
self.pdf_color_start
.iter()
.cloned()
.map(DisplayItem::PdfColor),
);
let mut color_steps = self.pdf_color_steps.iter();
let mut graphics_steps = self.pdf_graphics_steps.iter();
let mut invalid_geometry = false;
let mut missing_color_step = false;
let mut missing_graphics_step = false;
self.box_node.visit_geometry(|item| {
let p = match item {
boxes::GeometryItem::Paint(p) => p,
boxes::GeometryItem::PdfColor => {
match color_steps.next() {
Some(Some(program)) => {
page.items.push(DisplayItem::PdfColor(program.clone()))
}
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 } => {
if let Some(operation) = graphics_steps.next() {
page.items.push(DisplayItem::PdfGraphics {
x: self.device.horizontal_origin + x,
y: self.device.vertical_origin + self.box_node.height + y,
operation: operation.clone(),
});
} else {
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;
page.items.push(match p.item {
MathItem::NativeGlyph {
font,
slot,
size,
character,
..
} => DisplayItem::NativeGlyph {
x,
y,
size,
font,
slot,
character,
paint: None,
},
MathItem::Rule { width, height } => DisplayItem::Rule {
x,
y,
width,
height,
},
MathItem::PdfImage(image) => DisplayItem::PdfImage {
x,
y: y - image.height,
width: image.width,
height: image.height + image.depth,
image,
},
_ => {
invalid_geometry = true;
return;
}
});
})?;
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)
}
}