Apply job-local PDF font maps at execution and shipout

This commit is contained in:
gloamxun committed 2026-11-21 09:53:31 +08:00
1 parent 9527d3bbec
commit 28dfba5a5e
15 files changed
+583 -187

No files matched your search

@@ -24,7 +24,7 @@ pub struct NativeShipout {
#[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_map: Option<Arc<crate::native_font::PdfFontMapInput>>,
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<Arc<PdfColorProgram>>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
@@ -62,7 +62,7 @@ impl NativeShipout {
magnification: self.device.magnification,
units: crate::render::DisplayUnits::TeX,
pdf: self.device.pdf.clone(),
pdf_font_map: self.pdf_font_map.clone(),
pdf_font_bindings: self.pdf_font_bindings.clone(),
pdf_links: self.pdf_links.clone(),
pdf_text_spaces: self.pdf_text_spaces.clone(),
pdf_resources: crate::pdf_device::page_resources::PageResources {
@@ -12,6 +12,7 @@ impl Executor {
use Primitive::*;
match primitive {
Pdfglyphtounicode => self.glyph_to_unicode(files)?,
Pdfmapfile | Pdfmapline => self.pdf_font_map_command(primitive, token.span, files)?,
Pdffontattr | Pdfnobuiltintounicode => {
self.pdf_font_settings(primitive, files)?;
}
@@ -83,6 +84,30 @@ impl Executor {
}
Ok(())
}
fn pdf_font_map_command(
&mut self, primitive: Primitive, span: Span, files: &mut impl SourceProvider,
) -> Result<(), Error> {
if self.read_parameter(Parameter::Pdfoutput).width() <= 0 {
return self.fail("PDF font map commands are not allowed in DVI mode");
}
let tokens = self.scan_expanded_group(files)?;
let bytes = self.pdf_token_bytes(&tokens)?;
let suppress_duplicates = self.read_parameter(Parameter::Pdfsuppresswarningdupmap).width() > 0;
let from = self.source_name(span);
self.reads.insert(Dependency::FontResources);
let warnings = Arc::make_mut(&mut self.pdf_virtual_fonts)
.update_font_map(&bytes, primitive == Primitive::Pdfmapfile, std::path::Path::new(&from),
suppress_duplicates, files)
.map_err(|error| self.error(error, span))?;
for warning in warnings {
let text = format!("native PDF warning: {warning}");
files.observe_diagnostic(WriteDestination::TerminalAndLog, &format!("{text}\n"))
.map_err(|e| self.error(e, span))?;
self.events.push(Event::Message { text, span });
}
Ok(())
}
pub(super) fn glyph_to_unicode(
&mut self,
files: &mut impl SourceProvider,
@@ -326,7 +326,7 @@ impl Executor {
device,
font_overrides,
virtual_glyphs: None,
pdf_font_map: self.pdf_virtual_fonts.captured_font_map(),
pdf_font_bindings: self.pdf_virtual_fonts.captured_font_bindings(),
pdf_color_start,
pdf_color_steps: Vec::new(),
pdf_graphics_steps: Vec::new(),
@@ -391,7 +391,7 @@ impl Executor {
OutputOperation::Write { .. } | OutputOperation::PdfLiteral { shipout: true, .. }
)
{
shipout.pdf_font_map = self.pdf_virtual_fonts.captured_font_map();
shipout.pdf_font_bindings = self.pdf_virtual_fonts.captured_font_bindings();
shipout.pdf_text_spaces =
Some(self.pdf_space_settings(automatic_spaces_at_start, uses_automatic_spaces));
self.current = output.span;
@@ -499,7 +499,7 @@ impl Executor {
for usage in font_uses {
self.prepare_virtual_glyph(shipout.virtual_glyphs.as_mut().unwrap(), usage, files)?;
}
shipout.pdf_font_map = self.pdf_virtual_fonts.captured_font_map();
shipout.pdf_font_bindings = self.pdf_virtual_fonts.captured_font_bindings();
let has_space_settings = has_space_nodes || automatic_spaces_at_start;
let probed_page = (uses_automatic_spaces || has_explicit_space)
&& self.pdf_loaded_space_font.is_none();
@@ -8,9 +8,9 @@ use std::{
sync::{Arc, Mutex, OnceLock, Weak},
};
const SCHEMA: u32 = 54;
const COMPATIBLE_SCHEMAS: [u32; 23] = [
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, SCHEMA,
const SCHEMA: u32 = 55;
const COMPATIBLE_SCHEMAS: [u32; 24] = [
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, SCHEMA,
];
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
@@ -127,8 +127,11 @@ impl FormatImage {
if image.state.fonts.has_unrecorded_expansion_limits() {
return Err("native format must be rebuilt to restore automatic expansion font identities".into());
}
if image.schema < SCHEMA {
if image.schema < 54 {
image.state.restore_legacy_format_fonts()?;
}
if image.schema < 55 {
image.state.restore_legacy_font_map_commands();
image.schema = SCHEMA;
}
Ok(image)
@@ -250,6 +250,13 @@ pub trait SourceProvider {
) -> Result<Option<crate::resources::LoadedResource>, String> {
Ok(None)
}
/// Read an actual PDF font map. A host that can distinguish missing
/// files from other failures can return None for TeX's warning path.
fn optional_font_map(
&mut self, name: &str, from: &str,
) -> Result<Option<crate::resources::LoadedResource>, String> {
self.resource(crate::resources::ResourceKind::Map, name, from).map(Some)
}
/// Read exact bytes through the normal TeX input policy. Missing inputs
/// return None. Denied or unavailable host operations remain errors.
fn optional_tex_input(
@@ -14,6 +14,7 @@ pub enum Parameter {
Pdfpkresolution,
Pdfimageresolution,
Pdfgentounicode,
Pdfsuppresswarningdupmap,
Pdfinfoomitdate,
Tracingcommands,
Tracingrestores,
@@ -135,6 +136,7 @@ impl Parameter {
Self::Pdfpkresolution,
Self::Pdfimageresolution,
Self::Pdfgentounicode,
Self::Pdfsuppresswarningdupmap,
Self::Pdfinfoomitdate,
Self::Tracingcommands,
Self::Tracingrestores,
@@ -124,6 +124,8 @@ pub enum Primitive {
Pdflastypos,
Pdfrestore,
Pdfglyphtounicode,
Pdfmapfile,
Pdfmapline,
Pdffontattr,
Pdfnobuiltintounicode,
Currentgrouplevel,
@@ -1043,6 +1045,8 @@ impl Default for State {
("pdflastypos", Pdflastypos),
("pdfrestore", Pdfrestore),
("pdfglyphtounicode", Pdfglyphtounicode),
("pdfmapfile", Pdfmapfile),
("pdfmapline", Pdfmapline),
("pdffontattr", Pdffontattr),
("pdfnobuiltintounicode", Pdfnobuiltintounicode),
("dump", Dump),
@@ -1479,6 +1483,21 @@ impl State {
Ok(())
}
pub(super) fn restore_legacy_font_map_commands(&mut self) {
for (name, meaning) in [
("pdfmapfile", Meaning::Primitive(Primitive::Pdfmapfile)),
("pdfmapline", Meaning::Primitive(Primitive::Pdfmapline)),
(
"pdfsuppresswarningdupmap",
Meaning::Parameter(Parameter::Pdfsuppresswarningdupmap),
),
] {
self.commands
.entry(CommandName::Control(name.into()))
.or_insert(meaning);
}
}
pub fn is_initex(&self) -> bool {
self.initex
}