Write PDF interword spaces with actual device fonts

This commit is contained in:
gloamxun committed 2026-11-21 03:09:10 +08:00
1 parent b93d5f55da
commit 514a2d3344
24 files changed
+664 -65

No files matched your search

@@ -150,6 +150,7 @@ pub(super) enum BoxPaint<'a> {
Box(&'a HorizontalBox),
PdfColor,
PdfGraphics,
PdfTextSpace(crate::pdf_device::text_spaces::SpaceOperation),
RowStart(RowLayout),
PdfLink(&'a OutputNode, Option<RowLayout>),
Output(&'a OutputNode),
@@ -209,6 +210,7 @@ pub(super) enum GeometryItem {
Paint(crate::math_engine_tex::PositionedItem),
PdfColor,
PdfGraphics { x: i64, y: i64 },
PdfTextSpace { x: i64, y: i64, operation: crate::pdf_device::text_spaces::SpaceOperation },
}
impl NativeBox {
@@ -734,6 +736,10 @@ impl NativeBox {
emit(GeometryItem::PdfGraphics { x, y });
return;
}
BoxPaint::PdfTextSpace(operation) => {
emit(GeometryItem::PdfTextSpace { x, y, operation });
return;
}
BoxPaint::RowStart(_) | BoxPaint::PdfLink(_, _) | BoxPaint::Output(_) => return,
};
if let Some(child) = &b.math_geometry {
@@ -977,6 +983,8 @@ fn walk_output(
emit(x, y, BoxPaint::PdfColor);
} else if node.is_pdf_graphics() {
emit(x, y, BoxPaint::PdfGraphics);
} else if let OutputOperation::PdfTextSpace(operation) = node.operation {
emit(x, y, BoxPaint::PdfTextSpace(operation));
} else if links && node.is_pdf_link() {
emit(x, y, BoxPaint::PdfLink(node, row));
}
@@ -25,6 +25,8 @@ pub struct NativeShipout {
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 = "crate::pdf_device::links::PageLinks::is_empty")]
@@ -43,6 +45,7 @@ impl NativeShipout {
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(),
..Default::default()
@@ -74,6 +77,14 @@ impl NativeShipout {
}
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 {
@@ -606,6 +606,9 @@ impl Printer<'_> {
}
OutputOperation::PdfSave => self.command("pdfsave"),
OutputOperation::PdfRestore => self.command("pdfrestore"),
OutputOperation::PdfTextSpace(operation) => {
self.command(operation.name());
}
OutputOperation::PdfLink(node) => {
use crate::pdf_device::links::LinkNode;
self.command(match node {
@@ -219,6 +219,8 @@ pub struct Executor {
pdf_colors: crate::pdf_device::PdfColorStacks,
pdf_link_stack: Vec<pdf_links::ActiveLink>,
pdf_running_links: bool,
pdf_interword_spaces: bool,
pdf_space_font: Option<crate::pdf_device::text_spaces::SpaceFontRequest>,
#[cfg(all(target_os = "linux", target_env = "gnu"))]
pdf_last_match: Option<Arc<pdf_match::PdfMatch>>,
pdf_catalog_tokens: Vec<Token>,
@@ -384,6 +386,8 @@ impl Executor {
pdf_colors: Default::default(),
pdf_link_stack: Vec::new(),
pdf_running_links: true,
pdf_interword_spaces: false,
pdf_space_font: None,
#[cfg(all(target_os = "linux", target_env = "gnu"))]
pdf_last_match: None,
pdf_catalog_tokens: Vec::new(),
@@ -2814,6 +2818,9 @@ impl Executor {
OutputOperation::PdfColor { .. } => {
return self.fail("PDF color operation requires native PDF paint");
}
OutputOperation::PdfTextSpace(_) => {
return self.fail("PDF text space operation requires a positioned shipout");
}
OutputOperation::PdfLink(_) => {
return self.fail("PDF link operation requires a positioned shipout");
}
@@ -3794,6 +3801,9 @@ impl Executor {
Pdfstartlink | Pdfendlink | Pdfdest | Pdfrunninglinkoff | Pdfrunninglinkon => {
self.scan_pdf_link(primitive, token.span, files)?;
}
Pdfinterwordspaceon | Pdfinterwordspaceoff | Pdffakespace | Pdfspacefont => {
self.scan_pdf_space(primitive, token.span, files)?;
}
Pdfsetrandomseed => {
let seed = self.integer(files)?;
self.reads.insert(Dependency::PdfRandomState);
@@ -69,6 +69,35 @@ impl Executor {
}
Ok(())
}
pub(super) fn scan_pdf_space(
&mut self,
command: Primitive,
span: Span,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
use crate::pdf_device::text_spaces::{SpaceFontRequest, SpaceOperation};
if self.read_parameter(Parameter::Pdfoutput).width() <= 0 {
return self.fail("PDF text space operation not allowed in DVI mode");
}
let operation = match command {
Primitive::Pdfspacefont => {
let tokens = self.scan_expanded_group(files)?;
self.pdf_space_font = Some(SpaceFontRequest {
name: self.token_list_text(&tokens),
from: self.source_name(span),
});
return Ok(());
}
Primitive::Pdfinterwordspaceon => SpaceOperation::Automatic(true),
Primitive::Pdfinterwordspaceoff => SpaceOperation::Automatic(false),
Primitive::Pdffakespace => SpaceOperation::Explicit,
_ => unreachable!(),
};
self.defer_output(OutputNode {
operation: OutputOperation::PdfTextSpace(operation),
span,
})
}
pub(super) fn pdf_page_data(
&mut self,
files: &mut impl SourceProvider,
@@ -550,6 +550,7 @@ impl Executor {
| Closeout | Write | Pdfcolorstack | Pdfsave | Pdfsetmatrix | Pdfrestore | Pdfcatalog | Pdfoutline
| Pdfglyphtounicode | Pdffontattr | Pdfnobuiltintounicode
| Pdfrunninglinkoff | Pdfrunninglinkon
| Pdfinterwordspaceon | Pdfinterwordspaceoff | Pdffakespace | Pdfspacefont
| Ignorespaces | Unskip | Unkern | Unpenalty => {
return Ok(false);
}
@@ -310,6 +310,10 @@ impl Executor {
} else {
Vec::new().into_iter()
};
use crate::pdf_device::text_spaces::{SpaceFontRequest, SpaceOperation, SpaceSettings};
let automatic_spaces_at_start = self.pdf_interword_spaces;
let mut uses_automatic_spaces = automatic_spaces_at_start;
let mut has_space_nodes = false;
let mut pdf_objects = Vec::new();
for output in outputs {
if let OutputOperation::PdfObject(number) = output.operation {
@@ -337,6 +341,17 @@ impl Executor {
&mut graphics_stack,
files,
)?);
} else if let OutputOperation::PdfTextSpace(operation) = output.operation {
self.current = output.span;
self.tick()?;
if device.pdf.is_none() {
return self.fail("cannot ship a PDF text space operation to DVI");
}
has_space_nodes = true;
if let SpaceOperation::Automatic(enabled) = operation {
self.pdf_interword_spaces = enabled;
uses_automatic_spaces |= enabled;
}
} else if let OutputOperation::PdfLink(link) = &output.operation {
use crate::pdf_device::links::LinkNode;
self.current = output.span;
@@ -380,6 +395,25 @@ impl Executor {
} else {
Default::default()
};
let pdf_text_spaces = (has_space_nodes || automatic_spaces_at_start).then(|| {
let parameters = if uses_automatic_spaces {
self.reads.insert(Dependency::FontResources);
self.state.fonts.fonts().iter().map(|font| {
let glue = font.space();
(glue.width, glue.shrink.amount)
}).collect()
} else {
Vec::new()
};
SpaceSettings {
automatic_at_start: automatic_spaces_at_start,
font: self.pdf_space_font.clone().unwrap_or_else(|| SpaceFontRequest {
name: "pdftexspace".into(),
from: self.source_name(current),
}),
parameters,
}
});
self.current = current;
self.native_page.as_mut().unwrap().dead_cycles = 0;
let before = self.statistics_before_shipout(&b, files)?;
@@ -390,6 +424,7 @@ impl Executor {
pdf_color_start,
pdf_color_steps,
pdf_graphics_steps,
pdf_text_spaces,
pdf_resource_entries,
pdf_links,
})));
@@ -8,7 +8,7 @@ use std::{
sync::{Arc, Mutex, OnceLock, Weak},
};
const SCHEMA: u32 = 41;
const SCHEMA: u32 = 42;
const COMPATIBLE_SCHEMAS: [u32; 12] = [27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, SCHEMA];
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
@@ -241,6 +241,7 @@ pub enum OutputOperation {
PdfSave,
PdfSetMatrix(Vec<Token>),
PdfRestore,
PdfTextSpace(crate::pdf_device::text_spaces::SpaceOperation),
PdfLink(crate::pdf_device::links::LinkNode),
PdfImage {
image: std::sync::Arc<crate::pdf_image::PdfImageUse>,
@@ -109,6 +109,10 @@ pub enum Primitive {
Pdfendlink,
Pdfrunninglinkoff,
Pdfrunninglinkon,
Pdfinterwordspaceon,
Pdfinterwordspaceoff,
Pdffakespace,
Pdfspacefont,
Pdfdest,
Pdfcatalog,
Pdfoutline,
@@ -990,6 +994,10 @@ impl Default for State {
("pdfendlink", Pdfendlink),
("pdfrunninglinkoff", Pdfrunninglinkoff),
("pdfrunninglinkon", Pdfrunninglinkon),
("pdfinterwordspaceon", Pdfinterwordspaceon),
("pdfinterwordspaceoff", Pdfinterwordspaceoff),
("pdffakespace", Pdffakespace),
("pdfspacefont", Pdfspacefont),
("pdfdest", Pdfdest),
("pdfcatalog", Pdfcatalog),
("pdfoutline", Pdfoutline),