Resolve pdfTeX SFD subfonts against actual TrueType cmaps
This commit is contained in:
1 parent
fd5ab59f42
commit
868814288c
10 files changed
+481
-43
No files matched your search
@@ -1,5 +1,5 @@
|
||||
//! PDF font map entries, lookup bindings, and the lifetime of the default map.
|
||||
use super::{MappedOutline, MappedType1, NativeFont, actual_resource, encoding_vector, verify_resource};
|
||||
use super::{MappedOutline, MappedType1, NativeFont, actual_resource, encoding_vector, sfd, verify_resource};
|
||||
use crate::{
|
||||
execution::{FontId, SourceProvider},
|
||||
resources::{LoadedResource, ResourceKind, ResourceVersion},
|
||||
@@ -19,6 +19,10 @@ pub(super) struct MapEntry {
|
||||
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||
pub(super) subset: bool,
|
||||
pub encoding: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(super) subfont: Option<sfd::Subfont>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
cmap: Option<[u16; 2]>,
|
||||
postscript_name: bool,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
ps_name: Option<String>,
|
||||
@@ -29,6 +33,30 @@ pub(super) struct MapEntry {
|
||||
}
|
||||
|
||||
type PostscriptKey = (String, i32, i32);
|
||||
type OutlineKey = (ResourceKind, PathBuf, String);
|
||||
|
||||
/// Share the inspected bytes of a physical outline within an output pass.
|
||||
#[derive(Default)]
|
||||
pub(super) struct OutlineFiles(BTreeMap<OutlineKey, (ResourceVersion, Arc<[u8]>)>);
|
||||
|
||||
impl OutlineFiles {
|
||||
fn read(
|
||||
&mut self,
|
||||
files: &mut impl SourceProvider,
|
||||
kind: ResourceKind,
|
||||
file: &str,
|
||||
from: &Path,
|
||||
) -> Result<(ResourceVersion, Arc<[u8]>), String> {
|
||||
let key = (kind, from.to_owned(), file.to_owned());
|
||||
if let Some(found) = self.0.get(&key) {
|
||||
return Ok(found.clone());
|
||||
}
|
||||
let loaded = actual_resource(files, kind, file, &from.to_string_lossy())?;
|
||||
let value = (loaded.version, Arc::from(loaded.data));
|
||||
self.0.insert(key, value.clone());
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl MapEntry {
|
||||
fn postscript_key(&self) -> Option<PostscriptKey> {
|
||||
@@ -60,14 +88,18 @@ impl MapEntry {
|
||||
pub(super) fn read_outline(
|
||||
&self,
|
||||
files: &mut impl SourceProvider,
|
||||
outlines: &mut OutlineFiles,
|
||||
kind: ResourceKind,
|
||||
file: &str,
|
||||
) -> Result<MappedOutline, String> {
|
||||
let from = self.from.to_string_lossy();
|
||||
let program = actual_resource(files, kind, file, &from)?;
|
||||
let (program, data) = outlines.read(files, kind, file, &self.from)?;
|
||||
let mut resources: Vec<_> = self.version.iter().cloned().collect();
|
||||
if let Some(subfont) = &self.subfont {
|
||||
resources.push(subfont.version.as_ref().clone());
|
||||
}
|
||||
let program_index = resources.len();
|
||||
resources.push(program.version);
|
||||
resources.push(program);
|
||||
let encoding = if let Some(name) = &self.encoding {
|
||||
let file = actual_resource(files, ResourceKind::Encoding, name, &from)?;
|
||||
let names = encoding_vector(&file.data)?;
|
||||
@@ -76,7 +108,20 @@ impl MapEntry {
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Ok(MappedOutline { data: program.data, encoding, resources, program_index })
|
||||
Ok(MappedOutline { data, encoding, resources, program_index })
|
||||
}
|
||||
|
||||
fn for_subfont(mut self, definition: &sfd::Definition, codes: &sfd::Codes) -> Self {
|
||||
self.ps_name = None;
|
||||
self.postscript_name = false;
|
||||
self.descriptor_flags = None;
|
||||
self.encoding = None;
|
||||
self.subfont = Some(sfd::Subfont {
|
||||
version: definition.version.clone(),
|
||||
codes: codes.clone(),
|
||||
cmap: self.cmap.unwrap_or([3, 1]),
|
||||
});
|
||||
self
|
||||
}
|
||||
|
||||
pub(super) fn postscript_name(&self) -> Option<&str> {
|
||||
@@ -140,12 +185,15 @@ fn parse_map_line(
|
||||
file: None,
|
||||
subset: false,
|
||||
encoding: None,
|
||||
subfont: None,
|
||||
cmap: None,
|
||||
postscript_name: false,
|
||||
ps_name: None,
|
||||
descriptor_flags: None,
|
||||
transform: Type1Transform::default(),
|
||||
};
|
||||
for (index, (quote, value)) in fields.into_iter().skip(1).enumerate() {
|
||||
let mut fields = fields.into_iter().skip(1).enumerate();
|
||||
while let Some((index, (quote, value))) = fields.next() {
|
||||
if quote {
|
||||
let mut previous = "";
|
||||
for word in value.split_ascii_whitespace() {
|
||||
@@ -166,6 +214,22 @@ fn parse_map_line(
|
||||
}
|
||||
previous = word;
|
||||
}
|
||||
} else if let Some(spec) = value.strip_prefix("PidEid=") {
|
||||
let (platform, encoding) = spec.split_once(',')
|
||||
.ok_or_else(|| format!("invalid PidEid for {name}"))?;
|
||||
let continuation;
|
||||
let encoding = if encoding.is_empty() {
|
||||
continuation = fields.next().and_then(|(_, (quote, value))| (!quote).then_some(value))
|
||||
.ok_or_else(|| format!("invalid PidEid for {name}"))?;
|
||||
&continuation
|
||||
} else {
|
||||
encoding
|
||||
};
|
||||
let parse = |value| sfd::integer(value).and_then(|value| u16::try_from(value).ok());
|
||||
entry.cmap = Some([
|
||||
parse(platform).ok_or_else(|| format!("invalid PidEid for {name}"))?,
|
||||
parse(encoding).ok_or_else(|| format!("invalid PidEid for {name}"))?,
|
||||
]);
|
||||
} else if value.starts_with('<') {
|
||||
let path = value.trim_start_matches('<').trim_start_matches('[');
|
||||
if path.ends_with(".enc") {
|
||||
@@ -202,6 +266,11 @@ fn parse_map_line(
|
||||
"SlantFont/ExtendFont require an embedded Type1 program for {name}"
|
||||
));
|
||||
}
|
||||
if entry.cmap.is_some()
|
||||
&& (entry.true_type_file().is_none() || !entry.subset || entry.encoding.is_some())
|
||||
{
|
||||
return Err(format!("PidEid requires a subsetted non-reencoded TrueType font: {name}"));
|
||||
}
|
||||
Ok(Some((name, entry)))
|
||||
}
|
||||
|
||||
@@ -237,6 +306,7 @@ pub(super) struct JobFontMap {
|
||||
pending_fonts: Vec<(FontId, String)>,
|
||||
pending_ids: BTreeSet<FontId>,
|
||||
entries: Arc<BTreeMap<String, Arc<MapEntry>>>,
|
||||
subfonts: sfd::Files,
|
||||
used_names: BTreeSet<String>,
|
||||
// PDF inclusion addresses the PostScript name independently of the TFM.
|
||||
postscript_entries: Arc<BTreeMap<PostscriptKey, (String, Arc<MapEntry>)>>,
|
||||
@@ -310,7 +380,7 @@ impl JobFontMap {
|
||||
let from = self.first_font_source.as_deref().unwrap_or(from);
|
||||
if let Some(read) = load_map(files, "pdftex.map", from)? {
|
||||
let mut ignored = Vec::new();
|
||||
self.apply_file(&read, MapMode::Add, true, &mut ignored)?;
|
||||
self.apply_file(&read, MapMode::Add, true, &mut ignored, files)?;
|
||||
bindings.default_map = Some(read.version);
|
||||
}
|
||||
}
|
||||
@@ -384,13 +454,13 @@ impl JobFontMap {
|
||||
};
|
||||
match load_map(files, &name, from)? {
|
||||
Some(read) => {
|
||||
self.apply_file(&read, mode, suppress_duplicates, &mut warnings)?
|
||||
self.apply_file(&read, mode, suppress_duplicates, &mut warnings, files)?
|
||||
}
|
||||
None => warnings.push(format!("cannot open font map file {name}")),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.apply_line(item, from, None, mode, suppress_duplicates, &mut warnings);
|
||||
self.apply_line(item, from, None, mode, suppress_duplicates, &mut warnings, files)?;
|
||||
}
|
||||
Ok(warnings)
|
||||
}
|
||||
@@ -401,6 +471,7 @@ impl JobFontMap {
|
||||
mode: MapMode,
|
||||
suppress_duplicates: bool,
|
||||
warnings: &mut Vec<String>,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<(), String> {
|
||||
let text = std::str::from_utf8(&read.data).map_err(|_| "font map is not UTF-8/ASCII")?;
|
||||
for line in text.lines() {
|
||||
@@ -411,7 +482,8 @@ impl JobFontMap {
|
||||
mode,
|
||||
suppress_duplicates,
|
||||
warnings,
|
||||
);
|
||||
files,
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -424,15 +496,39 @@ impl JobFontMap {
|
||||
mode: MapMode,
|
||||
suppress_duplicates: bool,
|
||||
warnings: &mut Vec<String>,
|
||||
) {
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<(), String> {
|
||||
let (name, entry) = match parse_map_line(line, from, version) {
|
||||
Ok(Some(entry)) => entry,
|
||||
Ok(None) => return,
|
||||
Ok(None) => return Ok(()),
|
||||
Err(error) => {
|
||||
warnings.push(error);
|
||||
return;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
if let Some((prefix, file)) = sfd::template(&name) {
|
||||
let definition = format!("{file}.sfd");
|
||||
if let Some(definition) = self.subfonts.get(files, &definition, &entry.from)? {
|
||||
for (suffix, codes) in &definition.subfonts {
|
||||
let subfont = entry.clone().for_subfont(&definition, codes);
|
||||
self.apply_entry(format!("{prefix}{suffix}"), subfont, mode, suppress_duplicates, warnings);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
warnings.push(format!("cannot open SFD file {definition} for reading"));
|
||||
}
|
||||
self.apply_entry(name, entry, mode, suppress_duplicates, warnings);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn apply_entry(
|
||||
&mut self,
|
||||
name: String,
|
||||
entry: MapEntry,
|
||||
mode: MapMode,
|
||||
suppress_duplicates: bool,
|
||||
warnings: &mut Vec<String>,
|
||||
) {
|
||||
if self.entries.contains_key(&name) {
|
||||
if mode == MapMode::Add {
|
||||
if !suppress_duplicates {
|
||||
@@ -503,15 +599,28 @@ pub(super) fn load_map(
|
||||
.transpose()
|
||||
}
|
||||
|
||||
pub(super) fn find_map_entry(map: &LoadedResource, name: &str) -> Result<Option<MapEntry>, String> {
|
||||
pub(super) fn find_map_entry(
|
||||
map: &LoadedResource,
|
||||
name: &str,
|
||||
files: &mut impl SourceProvider,
|
||||
definitions: &mut sfd::Files,
|
||||
) -> Result<Option<MapEntry>, String> {
|
||||
let text = std::str::from_utf8(&map.data).map_err(|_| "font map is not UTF-8/ASCII")?;
|
||||
let Some(line) = text
|
||||
.lines()
|
||||
.find(|line| line.split_ascii_whitespace().next() == Some(name))
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
Ok(parse_map_line(line, &map.version.path, Some(&map.version))?.map(|(_, entry)| entry))
|
||||
for line in text.lines() {
|
||||
let Some(candidate) = line.split_ascii_whitespace().next() else { continue };
|
||||
if candidate == name {
|
||||
return Ok(parse_map_line(line, &map.version.path, Some(&map.version))?.map(|(_, entry)| entry));
|
||||
}
|
||||
if let Some((prefix, file)) = sfd::template(candidate)
|
||||
&& let Some(suffix) = name.strip_prefix(prefix)
|
||||
&& let Some((_, entry)) = parse_map_line(line, &map.version.path, Some(&map.version))?
|
||||
&& let Some(definition) = definitions.get(files, &format!("{file}.sfd"), &entry.from)?
|
||||
&& let Some((_, codes)) = definition.subfonts.iter().find(|(infix, _)| infix == suffix)
|
||||
{
|
||||
return Ok(Some(entry.for_subfont(&definition, codes)));
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub(super) fn selected_outline<'a>(
|
||||
@@ -532,6 +641,8 @@ pub(super) fn dedicated_space_map(font: &NativeFont) -> MapEntry {
|
||||
version: None,
|
||||
file: Some("pdftexspace.pfb".into()),
|
||||
encoding: None,
|
||||
subfont: None,
|
||||
cmap: None,
|
||||
postscript_name: false,
|
||||
ps_name: None,
|
||||
descriptor_flags: None,
|
||||
|
||||
Reference in new issue
Block a user