Emit native PDF page resources and named transparency states
This commit is contained in:
1 parent
2bced813a2
commit
4f4a4567ea
23 files changed
+791
-37
No files matched your search
@@ -25,9 +25,15 @@ 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 = "pdf_resource_entries_empty")]
|
||||
pub(super) pdf_resource_entries: 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 {
|
||||
@@ -37,6 +43,10 @@ impl NativeShipout {
|
||||
units: crate::render::DisplayUnits::TeX,
|
||||
pdf: self.device.pdf.clone(),
|
||||
pdf_links: self.pdf_links.clone(),
|
||||
pdf_resources: crate::pdf_device::page_resources::PageResources {
|
||||
entries: self.pdf_resource_entries.clone(),
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
use crate::{math_engine_tex::MathItem, render::DisplayItem};
|
||||
|
||||
@@ -596,6 +596,10 @@ impl Printer<'_> {
|
||||
self.literal(&format!(" {{{}}}", program.literal()));
|
||||
}
|
||||
}
|
||||
OutputOperation::PdfObject(number) => {
|
||||
self.command("pdfrefobj");
|
||||
self.literal(&format!(" {number}"));
|
||||
}
|
||||
OutputOperation::PdfImage { image, .. } => {
|
||||
self.command("pdfrefximage");
|
||||
self.literal(&format!(" {}", image.id));
|
||||
|
||||
@@ -25,6 +25,7 @@ mod pdf_catalog;
|
||||
mod pdf_color;
|
||||
mod pdf_graphics;
|
||||
mod pdf_image;
|
||||
mod pdf_objects;
|
||||
mod pdf_links;
|
||||
#[cfg(all(target_os = "linux", target_env = "gnu"))]
|
||||
pub(super) mod pdf_match;
|
||||
@@ -2097,6 +2098,10 @@ impl Executor {
|
||||
Meaning::Primitive(Primitive::Interactionmode) => Ok(self.interaction_mode_value()),
|
||||
Meaning::Primitive(Primitive::Pdftexversion) => Ok(Self::PDFTEX_VERSION),
|
||||
Meaning::Primitive(Primitive::Pdfshellescape) => Ok(Self::SHELL_ESCAPE),
|
||||
Meaning::Primitive(Primitive::Pdflastobj) => {
|
||||
self.reads.insert(Dependency::PdfObjects);
|
||||
Ok(self.state.pdf_objects.as_deref().map_or(0, |objects| objects.last()))
|
||||
}
|
||||
Meaning::Primitive(
|
||||
p @ (Primitive::Pdflastximage
|
||||
| Primitive::Pdflastximagepages
|
||||
@@ -2664,6 +2669,7 @@ impl Executor {
|
||||
if immediate {
|
||||
let next = self.required_expanded(files)?;
|
||||
match self.primitive(&next.token) {
|
||||
Some(Primitive::Pdfobj) => return self.scan_pdf_object(true, files),
|
||||
Some(Primitive::Pdfximage) => return self.scan_pdf_image(&next.token, true, files),
|
||||
Some(p @ (Primitive::Openout | Primitive::Closeout | Primitive::Write)) => {
|
||||
primitive = p
|
||||
@@ -2741,6 +2747,7 @@ impl Executor {
|
||||
self.current = node.span;
|
||||
self.tick()?;
|
||||
match &node.operation {
|
||||
OutputOperation::PdfObject(number) => return self.write_pdf_object(*number),
|
||||
OutputOperation::PdfImage { image, .. } => {
|
||||
return self.declare_pdf_image(image.id);
|
||||
}
|
||||
@@ -3708,6 +3715,8 @@ impl Executor {
|
||||
Pdfstartlink | Pdfendlink | Pdfdest => {
|
||||
self.scan_pdf_link(primitive, token.span, files)?;
|
||||
}
|
||||
Pdfobj => self.scan_pdf_object(false, files)?,
|
||||
Pdfrefobj => self.scan_pdf_object_reference(token.span, files)?,
|
||||
Pdfcatalog => self.scan_pdf_catalog(files)?,
|
||||
Pdfoutline => self.scan_pdf_outline(files)?,
|
||||
Pdfinfo => self.scan_pdf_info(files)?,
|
||||
|
||||
@@ -264,6 +264,17 @@ impl Executor {
|
||||
};
|
||||
self.trace_shipout(&b, files)?;
|
||||
let current = self.current;
|
||||
let pdf_resource_entries = if device.pdf.is_some() {
|
||||
let parameter = TokenParameter::Pdfpageresources;
|
||||
self.reads.insert(Dependency::TokenParameter(parameter));
|
||||
if let Some(tokens) = self.state.token_parameter(parameter).cloned() {
|
||||
self.pdf_token_bytes(&tokens)?.into()
|
||||
} else {
|
||||
Default::default()
|
||||
}
|
||||
} else {
|
||||
Default::default()
|
||||
};
|
||||
let pdf_color_start = if device.pdf.is_some() {
|
||||
self.pdf_colors.start_page()
|
||||
} else {
|
||||
@@ -367,6 +378,7 @@ impl Executor {
|
||||
pdf_color_start,
|
||||
pdf_color_steps,
|
||||
pdf_graphics_steps,
|
||||
pdf_resource_entries,
|
||||
pdf_links,
|
||||
})));
|
||||
self.statistics_after_shipout(before, files)?;
|
||||
|
||||
@@ -26,7 +26,7 @@ impl Executor {
|
||||
Ok(number)
|
||||
}
|
||||
|
||||
fn pdf_token_bytes(&mut self, tokens: &[Token]) -> Result<Vec<u8>, Error> {
|
||||
pub(super) fn pdf_token_bytes(&mut self, tokens: &[Token]) -> Result<Vec<u8>, Error> {
|
||||
let text = self.token_list_text(tokens);
|
||||
if text.len() > self.limits.tokens {
|
||||
return self.fail("native PDF token output limit exceeded");
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
use super::*;
|
||||
use crate::pdf_device::page_resources::ObjectBody;
|
||||
|
||||
impl Executor {
|
||||
pub(super) fn scan_pdf_object(
|
||||
&mut self,
|
||||
immediate: bool,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<(), Error> {
|
||||
if self.read_parameter(Parameter::Pdfoutput).width() <= 0 {
|
||||
return self.fail("\\pdfobj not allowed in DVI mode");
|
||||
}
|
||||
self.reads.insert(Dependency::PdfObjects);
|
||||
if self.keyword("reserveobjnum", files)? {
|
||||
self.optional_space(files)?;
|
||||
if immediate {
|
||||
return self.fail("\\pdfobj reserveobjnum cannot be used with \\immediate");
|
||||
}
|
||||
return self
|
||||
.state
|
||||
.pdf_objects
|
||||
.get_or_insert_with(Default::default)
|
||||
.reserve(self.limits.tokens)
|
||||
.map(|_| ())
|
||||
.map_err(|error| self.error(error, self.current));
|
||||
}
|
||||
|
||||
let requested = if self.keyword("useobjnum", files)? {
|
||||
let number = self.integer(files)?;
|
||||
let reserved = u32::try_from(number).ok().filter(|number| {
|
||||
self.state
|
||||
.pdf_objects
|
||||
.as_deref()
|
||||
.is_some_and(|objects| objects.is_reserved(*number))
|
||||
});
|
||||
if reserved.is_none() {
|
||||
let warning = "pdfTeX warning (\\pdfobj): invalid object number being ignored\n";
|
||||
files
|
||||
.observe_message(warning)
|
||||
.map_err(|error| self.error(error, self.current))?;
|
||||
self.events.push(Event::Message {
|
||||
text: warning.into(),
|
||||
span: self.current,
|
||||
});
|
||||
}
|
||||
reserved
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let attributes = if self.keyword("stream", files)? {
|
||||
Some(if self.keyword("attr", files)? {
|
||||
self.scan_pdf_bytes(files)?
|
||||
} else {
|
||||
Vec::new()
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if self.keyword("file", files)? {
|
||||
return self.fail("file-backed \\pdfobj payloads are unsupported in native PDF output");
|
||||
}
|
||||
let bytes = self.scan_pdf_bytes(files)?;
|
||||
let value = match attributes {
|
||||
Some(attributes) => ObjectBody::Stream { attributes, bytes },
|
||||
None => ObjectBody::Direct(bytes),
|
||||
};
|
||||
let number = self
|
||||
.state
|
||||
.pdf_objects
|
||||
.get_or_insert_with(Default::default)
|
||||
.define(requested, value, self.limits.tokens)
|
||||
.map_err(|error| self.error(error, self.current))?;
|
||||
if immediate {
|
||||
self.pdf_page_data(files)?;
|
||||
self.write_pdf_object(number)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn scan_pdf_object_reference(
|
||||
&mut self,
|
||||
span: Span,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<(), Error> {
|
||||
if self.read_parameter(Parameter::Pdfoutput).width() <= 0 {
|
||||
return self.fail("\\pdfrefobj not allowed in DVI mode");
|
||||
}
|
||||
self.reads.insert(Dependency::PdfObjects);
|
||||
let number = self.positive_pdf_number("object reference", files)? as u32;
|
||||
if !self
|
||||
.state
|
||||
.pdf_objects
|
||||
.as_deref()
|
||||
.is_some_and(|objects| objects.contains(number))
|
||||
{
|
||||
return self.fail("cannot find referenced PDF object");
|
||||
}
|
||||
self.defer_output(OutputNode {
|
||||
span,
|
||||
operation: OutputOperation::PdfObject(number),
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn write_pdf_object(&mut self, number: u32) -> Result<(), Error> {
|
||||
self.reads.insert(Dependency::PdfObjects);
|
||||
self.state
|
||||
.pdf_objects
|
||||
.as_deref_mut()
|
||||
.ok_or_else(|| String::from("unknown native PDF object"))
|
||||
.and_then(|objects| objects.write(number))
|
||||
.map_err(|error| self.error(error, self.current))
|
||||
}
|
||||
}
|
||||
@@ -207,6 +207,7 @@ impl Executor {
|
||||
| Primitive::Month
|
||||
| Primitive::Year
|
||||
| Primitive::Inputlineno
|
||||
| Primitive::Pdflastobj
|
||||
| Primitive::Pdflastximage
|
||||
| Primitive::Pdflastximagepages
|
||||
| Primitive::Pdflastximagecolordepth
|
||||
|
||||
@@ -8,8 +8,8 @@ use std::{
|
||||
sync::{Arc, Mutex, OnceLock, Weak},
|
||||
};
|
||||
|
||||
const SCHEMA: u32 = 33;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 5] = [27, 28, 31, 32, SCHEMA];
|
||||
const SCHEMA: u32 = 34;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 6] = [27, 28, 31, 32, 33, SCHEMA];
|
||||
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
|
||||
@@ -15,6 +15,7 @@ pub enum TokenParameter {
|
||||
Output,
|
||||
Errhelp,
|
||||
Everycr,
|
||||
Pdfpageresources,
|
||||
}
|
||||
impl TokenParameter {
|
||||
pub const ALL: &'static [Self] = &[
|
||||
@@ -28,6 +29,7 @@ impl TokenParameter {
|
||||
Self::Output,
|
||||
Self::Errhelp,
|
||||
Self::Everycr,
|
||||
Self::Pdfpageresources,
|
||||
];
|
||||
pub fn name(self) -> String {
|
||||
format!("{self:?}").to_ascii_lowercase()
|
||||
|
||||
@@ -228,6 +228,7 @@ pub enum OutputOperation {
|
||||
stack: usize,
|
||||
action: crate::pdf_device::PdfColorStackAction,
|
||||
},
|
||||
PdfObject(u32),
|
||||
PdfSave,
|
||||
PdfSetMatrix(Vec<Token>),
|
||||
PdfRestore,
|
||||
|
||||
@@ -367,6 +367,7 @@ pub enum Dependency {
|
||||
MagnificationState,
|
||||
PdfFileState,
|
||||
PdfCatalogState,
|
||||
PdfObjects,
|
||||
PdfImages,
|
||||
GlyphUnicode,
|
||||
ResourceStatistics,
|
||||
@@ -674,6 +675,7 @@ impl Execution {
|
||||
})?);
|
||||
}
|
||||
}
|
||||
self.resolve_page_graphics_resources(&mut pages)?;
|
||||
if let Some(first) = pages.first_mut() {
|
||||
first.items.extend(
|
||||
image_objects
|
||||
@@ -683,6 +685,28 @@ impl Execution {
|
||||
}
|
||||
Ok(pages)
|
||||
}
|
||||
fn resolve_page_graphics_resources(
|
||||
&self,
|
||||
pages: &mut [crate::render::DisplayList],
|
||||
) -> Result<(), Error> {
|
||||
use crate::pdf_device::page_resources::{PdfObjects, PageResources};
|
||||
use crate::render::DisplayItem;
|
||||
let empty = PdfObjects::default();
|
||||
let objects = self.state.pdf_objects.as_deref().unwrap_or(&empty);
|
||||
for page in pages {
|
||||
let states = page.items.iter().filter_map(|item| match item {
|
||||
DisplayItem::PdfColor(program) => Some(&program.graphics_states),
|
||||
_ => None,
|
||||
}).flatten().cloned().collect();
|
||||
page.pdf_resources = PageResources::from_page(
|
||||
page.pdf_resources.entries.clone(), &states, objects,
|
||||
).map_err(|message| Error::at(
|
||||
&self.sources, self.state.input_profile(), message, Span::default(),
|
||||
))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Export the paragraphs already measured during token execution. No text
|
||||
/// concatenation, source parser or final-state restyling participates.
|
||||
pub fn document(&self) -> Result<crate::Document, Error> {
|
||||
|
||||
@@ -319,6 +319,9 @@ pub enum Primitive {
|
||||
Rpcode,
|
||||
Efcode,
|
||||
Pdffontexpand,
|
||||
Pdfobj,
|
||||
Pdfrefobj,
|
||||
Pdflastobj,
|
||||
}
|
||||
|
||||
impl Primitive {
|
||||
@@ -611,6 +614,8 @@ pub struct State {
|
||||
pub(super) pdf_file_options: Option<crate::pdf_device::PdfFileOptions>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(super) pdf_catalog: Option<Box<crate::pdf_device::catalog::PdfCatalog>>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(super) pdf_objects: Option<Box<crate::pdf_device::page_resources::PdfObjects>>,
|
||||
pub(super) pdf_info: Vec<u8>,
|
||||
pub(super) glyph_unicode: crate::pdf_unicode::GlyphUnicodeMap,
|
||||
pub(super) hyphenation: super::hyphenation::HyphenationData,
|
||||
@@ -712,6 +717,7 @@ impl Default for State {
|
||||
output_mode_set: None,
|
||||
pdf_file_options: None,
|
||||
pdf_catalog: None,
|
||||
pdf_objects: None,
|
||||
pdf_info: Vec::new(),
|
||||
glyph_unicode: crate::pdf_unicode::GlyphUnicodeMap::default(),
|
||||
hyphenation: super::hyphenation::HyphenationData::default(),
|
||||
@@ -937,6 +943,9 @@ impl Default for State {
|
||||
#[cfg(all(target_os = "linux", target_env = "gnu"))]
|
||||
("pdflastmatch", Pdflastmatch),
|
||||
("pdfcolorstackinit", Pdfcolorstackinit),
|
||||
("pdfobj", Pdfobj),
|
||||
("pdfrefobj", Pdfrefobj),
|
||||
("pdflastobj", Pdflastobj),
|
||||
("pdfcolorstack", Pdfcolorstack),
|
||||
("pdfsave", Pdfsave),
|
||||
("pdfstartlink", Pdfstartlink),
|
||||
@@ -1334,6 +1343,7 @@ impl State {
|
||||
self.output_mode_set = None;
|
||||
self.pdf_file_options = None;
|
||||
self.pdf_catalog = None;
|
||||
self.pdf_objects = None;
|
||||
self.pdf_info.clear();
|
||||
self
|
||||
}
|
||||
@@ -1348,6 +1358,7 @@ impl State {
|
||||
generate_unicode: self.parameter(Parameter::Pdfgentounicode).width(),
|
||||
glyph_unicode: &self.glyph_unicode,
|
||||
catalog: self.pdf_catalog.as_deref(),
|
||||
objects: self.pdf_objects.as_deref(),
|
||||
info: crate::pdf_device::PdfInfo {
|
||||
entries: &self.pdf_info,
|
||||
date: (self.parameter(Parameter::Pdfinfoomitdate).width() == 0)
|
||||
|
||||
Reference in new issue
Block a user