Support native PDF font dictionaries and running link controls
This commit is contained in:
1 parent
60d7411dfe
commit
b647bd8dcd
19 files changed
+180
-13
No files matched your search
@@ -611,6 +611,8 @@ impl Printer<'_> {
|
||||
self.command(match node {
|
||||
LinkNode::Start(_) => "pdfstartlink",
|
||||
LinkNode::End => "pdfendlink",
|
||||
LinkNode::Running(false) => "pdfrunninglinkoff",
|
||||
LinkNode::Running(true) => "pdfrunninglinkon",
|
||||
LinkNode::Destination { .. } => "pdfdest",
|
||||
});
|
||||
}
|
||||
|
||||
@@ -218,6 +218,7 @@ pub struct Executor {
|
||||
io: IoState,
|
||||
pdf_colors: crate::pdf_device::PdfColorStacks,
|
||||
pdf_link_stack: Vec<pdf_links::ActiveLink>,
|
||||
pdf_running_links: bool,
|
||||
#[cfg(all(target_os = "linux", target_env = "gnu"))]
|
||||
pdf_last_match: Option<Arc<pdf_match::PdfMatch>>,
|
||||
pdf_catalog_tokens: Vec<Token>,
|
||||
@@ -382,6 +383,7 @@ impl Executor {
|
||||
io: IoState::default(),
|
||||
pdf_colors: Default::default(),
|
||||
pdf_link_stack: Vec::new(),
|
||||
pdf_running_links: true,
|
||||
#[cfg(all(target_os = "linux", target_env = "gnu"))]
|
||||
pdf_last_match: None,
|
||||
pdf_catalog_tokens: Vec::new(),
|
||||
@@ -3603,6 +3605,9 @@ impl Executor {
|
||||
return self.fail("misplaced native alignment command");
|
||||
}
|
||||
Pdfglyphtounicode => self.glyph_to_unicode(files)?,
|
||||
Pdffontattr | Pdfnobuiltintounicode => {
|
||||
self.pdf_font_settings(primitive, files)?;
|
||||
}
|
||||
Explicitspace => {
|
||||
if self.paragraph.is_some() {
|
||||
let space = self.explicit_space()?;
|
||||
@@ -3786,7 +3791,7 @@ impl Executor {
|
||||
Pdfsave | Pdfsetmatrix | Pdfrestore | Pdfliteral => {
|
||||
self.scan_pdf_graphics(primitive, token.span, files)?
|
||||
}
|
||||
Pdfstartlink | Pdfendlink | Pdfdest => {
|
||||
Pdfstartlink | Pdfendlink | Pdfdest | Pdfrunninglinkoff | Pdfrunninglinkon => {
|
||||
self.scan_pdf_link(primitive, token.span, files)?;
|
||||
}
|
||||
Pdfsetrandomseed => {
|
||||
|
||||
@@ -44,6 +44,31 @@ impl Executor {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub(super) fn pdf_font_settings(
|
||||
&mut self,
|
||||
primitive: Primitive,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<(), Error> {
|
||||
if self.read_parameter(Parameter::Pdfoutput).width() <= 0 {
|
||||
return self.fail("native PDF font settings are unavailable in DVI mode");
|
||||
}
|
||||
let font = self.font_identifier(files)?;
|
||||
if font == FontId::NULL {
|
||||
return self.fail("invalid PDF font identifier");
|
||||
}
|
||||
match primitive {
|
||||
Primitive::Pdffontattr => {
|
||||
let tokens = self.scan_expanded_group(files)?;
|
||||
let attributes = self.pdf_token_bytes(&tokens)?;
|
||||
self.state.fonts.pdf_settings.entry(font).or_default().attributes = attributes;
|
||||
}
|
||||
Primitive::Pdfnobuiltintounicode => {
|
||||
self.state.fonts.pdf_settings.entry(font).or_default().omit_builtin_unicode = true;
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub(super) fn pdf_page_data(
|
||||
&mut self,
|
||||
files: &mut impl SourceProvider,
|
||||
|
||||
@@ -548,6 +548,8 @@ impl Executor {
|
||||
Hbox | Vbox | Vtop | Box | Copy | Setbox | Wd | Ht | Dp | Lastbox | Raise
|
||||
| Lower | Vsplit | Mark | Marks | Vadjust | Insert | Shipout | Openout
|
||||
| Closeout | Write | Pdfcolorstack | Pdfsave | Pdfsetmatrix | Pdfrestore | Pdfcatalog | Pdfoutline
|
||||
| Pdfglyphtounicode | Pdffontattr | Pdfnobuiltintounicode
|
||||
| Pdfrunninglinkoff | Pdfrunninglinkon
|
||||
| Ignorespaces | Unskip | Unkern | Unpenalty => {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
@@ -353,7 +353,7 @@ impl Executor {
|
||||
return self.fail("\\pdfendlink used without \\pdfstartlink");
|
||||
}
|
||||
LinkNode::End => link_depth -= 1,
|
||||
LinkNode::Destination { .. } => {}
|
||||
LinkNode::Running(_) | LinkNode::Destination { .. } => {}
|
||||
}
|
||||
} else {
|
||||
self.output_node(output, files)?;
|
||||
|
||||
@@ -111,7 +111,10 @@ impl Executor {
|
||||
if self.native_page.is_none() || self.read_parameter(Parameter::Pdfoutput).width() <= 0 {
|
||||
return self.fail("PDF link operation not allowed in DVI mode");
|
||||
}
|
||||
if command != Primitive::Pdfdest && self.paragraph.is_none() && self.math.is_empty() {
|
||||
if matches!(command, Primitive::Pdfstartlink | Primitive::Pdfendlink)
|
||||
&& self.paragraph.is_none()
|
||||
&& self.math.is_empty()
|
||||
{
|
||||
return self.fail(if command == Primitive::Pdfstartlink {
|
||||
"\\pdfstartlink cannot be used in vertical mode"
|
||||
} else {
|
||||
@@ -167,6 +170,9 @@ impl Executor {
|
||||
})
|
||||
}
|
||||
Primitive::Pdfendlink => LinkNode::End,
|
||||
Primitive::Pdfrunninglinkoff | Primitive::Pdfrunninglinkon => {
|
||||
LinkNode::Running(command == Primitive::Pdfrunninglinkon)
|
||||
}
|
||||
Primitive::Pdfdest => {
|
||||
let destination = if self.keyword("name", files)? {
|
||||
DestinationId::Name(self.scan_pdf_tokens(files)?)
|
||||
@@ -248,7 +254,7 @@ impl Executor {
|
||||
Some(PdfGraphics::Literal { .. }) => {}
|
||||
None => failure = Some(self.error("missing PDF transform at link shipout", self.current)),
|
||||
},
|
||||
BoxPaint::RowStart(row) => {
|
||||
BoxPaint::RowStart(row) if self.pdf_running_links => {
|
||||
for link in active.iter_mut().filter(|link| {
|
||||
link.level == row.level && link.layout.width.is_none()
|
||||
}) {
|
||||
@@ -292,6 +298,7 @@ impl Executor {
|
||||
}
|
||||
link.end_segment(&mut output, x, margin);
|
||||
}
|
||||
LinkNode::Running(enabled) => self.pdf_running_links = *enabled,
|
||||
LinkNode::Destination { destination, view } => {
|
||||
let destination = match destination {
|
||||
DestinationId::Name(tokens) => DestinationId::Name(self.token_list_text(tokens)),
|
||||
|
||||
@@ -145,6 +145,12 @@ pub(super) struct FontExpansion {
|
||||
pub step: i32,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub(crate) struct PdfFontSettings {
|
||||
pub(crate) attributes: Vec<u8>,
|
||||
pub(crate) omit_builtin_unicode: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub struct FontCatalog {
|
||||
fonts: Vec<FontInstance>,
|
||||
@@ -157,6 +163,8 @@ pub struct FontCatalog {
|
||||
expansion: BTreeMap<FontId, FontExpansion>,
|
||||
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
||||
expansion_codes: BTreeMap<FontId, BTreeMap<u8, u16>>,
|
||||
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
||||
pub(super) pdf_settings: BTreeMap<FontId, PdfFontSettings>,
|
||||
}
|
||||
impl Default for FontCatalog {
|
||||
fn default() -> Self {
|
||||
@@ -189,8 +197,12 @@ impl FontCatalog {
|
||||
protrusion: BTreeMap::new(),
|
||||
expansion: BTreeMap::new(),
|
||||
expansion_codes: BTreeMap::new(),
|
||||
pdf_settings: BTreeMap::new(),
|
||||
})
|
||||
}
|
||||
pub(crate) fn pdf_settings(&self, id: FontId) -> Option<&PdfFontSettings> {
|
||||
self.pdf_settings.get(&id)
|
||||
}
|
||||
pub fn fonts(&self) -> &[FontInstance] {
|
||||
&self.fonts
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ use std::{
|
||||
sync::{Arc, Mutex, OnceLock, Weak},
|
||||
};
|
||||
|
||||
const SCHEMA: u32 = 40;
|
||||
const SCHEMA: u32 = 41;
|
||||
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"));
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ pub use statistics::{
|
||||
HostResourceUsage, MaterialUsage, ResourceUsage, StackUsage, StateUsage, StatisticsPhase,
|
||||
StatisticsReport,
|
||||
};
|
||||
use std::collections::BTreeSet;
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::fmt;
|
||||
|
||||
pub use boxes::{
|
||||
@@ -692,10 +692,14 @@ impl Execution {
|
||||
&self,
|
||||
pages: &mut [crate::render::DisplayList],
|
||||
) -> Result<(), Error> {
|
||||
use crate::pdf_device::page_resources::{PdfObjects, PageResources};
|
||||
use crate::pdf_device::page_resources::{
|
||||
PageResources, PdfObjects, font_attributes_affect_paint,
|
||||
};
|
||||
use std::collections::btree_map::Entry;
|
||||
use crate::render::DisplayItem;
|
||||
let empty = PdfObjects::default();
|
||||
let objects = self.state.pdf_objects.as_deref().unwrap_or(&empty);
|
||||
let mut font_affects_paint = BTreeMap::new();
|
||||
for page in pages {
|
||||
let states = page.items.iter().filter_map(|item| match item {
|
||||
DisplayItem::PdfColor(program) => Some(&program.graphics_states),
|
||||
@@ -706,6 +710,32 @@ impl Execution {
|
||||
).map_err(|message| Error::at(
|
||||
&self.sources, self.state.input_profile(), message, Span::default(),
|
||||
))?;
|
||||
if self.state.fonts.pdf_settings.is_empty() {
|
||||
continue;
|
||||
}
|
||||
for item in &page.items {
|
||||
let DisplayItem::NativeGlyph { font, .. } = item else {
|
||||
continue;
|
||||
};
|
||||
let affects_paint = match font_affects_paint.entry(font.id) {
|
||||
Entry::Occupied(entry) => *entry.get(),
|
||||
Entry::Vacant(entry) => {
|
||||
let affects_paint = self.state.fonts.pdf_settings(font.id)
|
||||
.filter(|settings| !settings.attributes.is_empty())
|
||||
.map(|settings| font_attributes_affect_paint(&settings.attributes))
|
||||
.transpose()
|
||||
.map_err(|message| Error::at(
|
||||
&self.sources, self.state.input_profile(), message, Span::default(),
|
||||
))?
|
||||
.unwrap_or(false);
|
||||
*entry.insert(affects_paint)
|
||||
}
|
||||
};
|
||||
if affects_paint {
|
||||
page.pdf_resources.needs_pdf_for_preview = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -107,6 +107,8 @@ pub enum Primitive {
|
||||
Pdfsave,
|
||||
Pdfstartlink,
|
||||
Pdfendlink,
|
||||
Pdfrunninglinkoff,
|
||||
Pdfrunninglinkon,
|
||||
Pdfdest,
|
||||
Pdfcatalog,
|
||||
Pdfoutline,
|
||||
@@ -114,6 +116,8 @@ pub enum Primitive {
|
||||
Pdfsetmatrix,
|
||||
Pdfrestore,
|
||||
Pdfglyphtounicode,
|
||||
Pdffontattr,
|
||||
Pdfnobuiltintounicode,
|
||||
Currentgrouplevel,
|
||||
Currentgrouptype,
|
||||
Unless,
|
||||
@@ -984,6 +988,8 @@ impl Default for State {
|
||||
("pdfsave", Pdfsave),
|
||||
("pdfstartlink", Pdfstartlink),
|
||||
("pdfendlink", Pdfendlink),
|
||||
("pdfrunninglinkoff", Pdfrunninglinkoff),
|
||||
("pdfrunninglinkon", Pdfrunninglinkon),
|
||||
("pdfdest", Pdfdest),
|
||||
("pdfcatalog", Pdfcatalog),
|
||||
("pdfoutline", Pdfoutline),
|
||||
@@ -991,6 +997,8 @@ impl Default for State {
|
||||
("pdfsetmatrix", Pdfsetmatrix),
|
||||
("pdfrestore", Pdfrestore),
|
||||
("pdfglyphtounicode", Pdfglyphtounicode),
|
||||
("pdffontattr", Pdffontattr),
|
||||
("pdfnobuiltintounicode", Pdfnobuiltintounicode),
|
||||
("dump", Dump),
|
||||
("indent", Indent),
|
||||
("noindent", Noindent),
|
||||
@@ -1406,6 +1414,7 @@ impl State {
|
||||
self.pdf_objects = None;
|
||||
}
|
||||
self.pdf_info.clear();
|
||||
self.fonts.pdf_settings.clear();
|
||||
self
|
||||
}
|
||||
pub fn is_initex(&self) -> bool {
|
||||
@@ -1418,6 +1427,7 @@ impl State {
|
||||
compression: self.parameter(Parameter::Pdfcompresslevel).width(),
|
||||
generate_unicode: self.parameter(Parameter::Pdfgentounicode).width(),
|
||||
glyph_unicode: &self.glyph_unicode,
|
||||
fonts: &self.fonts,
|
||||
catalog: self.pdf_catalog.as_deref(),
|
||||
objects: self.pdf_objects.as_deref(),
|
||||
info: crate::pdf_device::PdfInfo {
|
||||
|
||||
Reference in new issue
Block a user