Reject automatic expansion of bitmap fonts at PDF shipout
This commit is contained in:
1 parent
45b79fc5a5
commit
9527d3bbec
8 files changed
+211
-46
No files matched your search
@@ -130,8 +130,37 @@ impl NativeFont {
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct MapEntry {
|
||||
file: String,
|
||||
file: Option<String>,
|
||||
encoding: Option<String>,
|
||||
postscript_name: bool,
|
||||
unsupported_transform: Option<String>,
|
||||
}
|
||||
|
||||
impl MapEntry {
|
||||
fn is_scalable(&self) -> bool {
|
||||
self.file.is_some() || self.postscript_name
|
||||
}
|
||||
|
||||
fn type1_file(&self, name: &str) -> Result<&str, String> {
|
||||
if let Some(value) = &self.unsupported_transform {
|
||||
return Err(format!("native font map transform is not implemented: {value}"));
|
||||
}
|
||||
let file = self.file.as_deref().ok_or_else(|| {
|
||||
format!("font map {name} has no embedded Type1 program; mapped device fonts require their actual support")
|
||||
})?;
|
||||
if !file.ends_with(".pfb") {
|
||||
return Err(format!("native font program format is not implemented: {file}"));
|
||||
}
|
||||
Ok(file)
|
||||
}
|
||||
}
|
||||
|
||||
/// Version and lookup context of the PDF map read during shipout.
|
||||
#[doc(hidden)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct PdfFontMapInput {
|
||||
pub(crate) from: std::path::PathBuf,
|
||||
pub(crate) version: ResourceVersion,
|
||||
}
|
||||
fn map_entry(data: &[u8], name: &str) -> Result<Option<MapEntry>, String> {
|
||||
let text = std::str::from_utf8(data).map_err(|_| "font map is not UTF-8/ASCII")?;
|
||||
@@ -167,8 +196,13 @@ fn map_entry(data: &[u8], name: &str) -> Result<Option<MapEntry>, String> {
|
||||
if !current.is_empty() {
|
||||
fields.push((false, current));
|
||||
}
|
||||
let (mut file, mut encoding) = (None, None);
|
||||
for (quote, value) in fields.into_iter().skip(1) {
|
||||
let mut entry = MapEntry {
|
||||
file: None,
|
||||
encoding: None,
|
||||
postscript_name: false,
|
||||
unsupported_transform: None,
|
||||
};
|
||||
for (index, (quote, value)) in fields.into_iter().skip(1).enumerate() {
|
||||
if quote {
|
||||
let words: Vec<_> = value.split_whitespace().collect();
|
||||
// pdfTeX ignores PostScript names and ReEncodeFont words;
|
||||
@@ -177,27 +211,52 @@ fn map_entry(data: &[u8], name: &str) -> Result<Option<MapEntry>, String> {
|
||||
.iter()
|
||||
.any(|w| matches!(*w, "SlantFont" | "ExtendFont"))
|
||||
{
|
||||
return Err(format!(
|
||||
"native font map transform is not implemented: {value}"
|
||||
));
|
||||
entry.unsupported_transform.get_or_insert(value);
|
||||
}
|
||||
} else if value.starts_with('<') {
|
||||
let path = value.trim_start_matches('<').trim_start_matches('[');
|
||||
if path.ends_with(".enc") {
|
||||
encoding = Some(path.to_owned());
|
||||
} else if path.ends_with(".pfb") {
|
||||
file = Some(path.to_owned());
|
||||
} else {
|
||||
return Err(format!(
|
||||
"native font program format is not implemented: {path}"
|
||||
));
|
||||
entry.encoding = Some(path.to_owned());
|
||||
} else if !path.is_empty() {
|
||||
entry.file = Some(path.to_owned());
|
||||
}
|
||||
} else if index == 0 {
|
||||
entry.postscript_name = true;
|
||||
}
|
||||
}
|
||||
return Ok(Some(MapEntry { file: file.ok_or_else(||format!("font map {name} has no embedded Type1 program; mapped device fonts require their actual support"))?, encoding }));
|
||||
return Ok(Some(entry));
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
fn mapped_font(
|
||||
map: &LoadedResource,
|
||||
font: &NativeFont,
|
||||
auto_expanded: bool,
|
||||
) -> Result<Option<MapEntry>, String> {
|
||||
let entry = if font.pdf_space_font && font.name == "pdftexspace" {
|
||||
// pdfTeX installs the dedicated spacing font on first use.
|
||||
Some(MapEntry {
|
||||
file: Some("pdftexspace.pfb".into()),
|
||||
encoding: None,
|
||||
postscript_name: false,
|
||||
unsupported_transform: None,
|
||||
})
|
||||
} else {
|
||||
map_entry(&map.data, &font.name)?.filter(MapEntry::is_scalable)
|
||||
};
|
||||
if auto_expanded && entry.is_none() {
|
||||
return Err("font expansion: auto expansion is only possible with scalable fonts".into());
|
||||
}
|
||||
Ok(entry)
|
||||
}
|
||||
|
||||
fn load_font_map(
|
||||
files: &mut impl crate::execution::SourceProvider,
|
||||
from: &std::path::Path,
|
||||
) -> Result<LoadedResource, String> {
|
||||
actual_resource(files, crate::resources::ResourceKind::Map, "pdftex.map", &from.to_string_lossy())
|
||||
}
|
||||
|
||||
fn encoding_vector(data: &[u8]) -> Result<Vec<String>, String> {
|
||||
let text = std::str::from_utf8(data).map_err(|_| "font encoding is not UTF-8/ASCII")?;
|
||||
let text = text
|
||||
@@ -394,29 +453,30 @@ fn resolve_physical_fonts(
|
||||
) -> Result<(), String> {
|
||||
use crate::{render::DisplayItem, resources::ResourceKind};
|
||||
use std::collections::BTreeSet;
|
||||
let mut used: BTreeMap<FontId, (Arc<NativeFont>, BTreeSet<u8>)> = BTreeMap::new();
|
||||
let mut used: BTreeMap<FontId, (Arc<NativeFont>, BTreeSet<u8>, bool)> = BTreeMap::new();
|
||||
for page in pages.iter() {
|
||||
for item in &page.items {
|
||||
if let DisplayItem::NativeGlyph { font, slot, .. } = item {
|
||||
let entry = used
|
||||
.entry(font.id)
|
||||
.or_insert_with(|| (font.clone(), BTreeSet::new()));
|
||||
.or_insert_with(|| (font.clone(), BTreeSet::new(), false));
|
||||
if !Arc::ptr_eq(&entry.0, font) && !entry.0.same_base(font) {
|
||||
return Err("native font ids from different jobs cannot share output".into());
|
||||
}
|
||||
entry.1.insert(*slot);
|
||||
entry.2 |= font.expansion != 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
let Some((first, _)) = used.values().next() else {
|
||||
let Some((first, _, _)) = used.values().next() else {
|
||||
return Ok(());
|
||||
};
|
||||
let map = actual_resource(
|
||||
files,
|
||||
ResourceKind::Map,
|
||||
"pdftex.map",
|
||||
&first.resource.version.path.to_string_lossy(),
|
||||
)?;
|
||||
let captured_map = pages.iter().find_map(|page| page.pdf_font_map.as_deref());
|
||||
let from = captured_map.map_or(first.resource.version.path.as_path(), |map| map.from.as_path());
|
||||
let map = load_font_map(files, from)?;
|
||||
if captured_map.is_some_and(|captured| captured.version != map.version) {
|
||||
return Err("PDF font map changed after shipout".into());
|
||||
}
|
||||
let mut resolved = BTreeMap::new();
|
||||
let mut pk_resolved = BTreeMap::new();
|
||||
let device = pages
|
||||
@@ -424,14 +484,8 @@ fn resolve_physical_fonts(
|
||||
.find_map(|p| p.pdf.as_ref())
|
||||
.map(|p| p.file.clone());
|
||||
let mut cache: BTreeMap<_, (NativeFontPaint, Arc<DecodedType1>)> = BTreeMap::new();
|
||||
for (id, (font, slots)) in used {
|
||||
let entry = if font.pdf_space_font && font.name == "pdftexspace" {
|
||||
// The pdfTeX device adds its own map entry on first use.
|
||||
Some(MapEntry { file: "pdftexspace.pfb".into(), encoding: None })
|
||||
} else {
|
||||
map_entry(&map.data, &font.name)?
|
||||
};
|
||||
let Some(entry) = entry else {
|
||||
for (id, (font, slots, expanded)) in used {
|
||||
let Some(entry) = mapped_font(&map, &font, expanded)? else {
|
||||
let (resolution, digits) = if let Some(device) = &device {
|
||||
(device.pk_resolution, device.decimal_digits)
|
||||
} else {
|
||||
@@ -495,13 +549,13 @@ fn resolve_physical_fonts(
|
||||
);
|
||||
continue;
|
||||
};
|
||||
let file = entry.file;
|
||||
let key = (file.clone(), entry.encoding.clone());
|
||||
let file = entry.type1_file(&font.name)?;
|
||||
let key = (file.to_owned(), entry.encoding.clone());
|
||||
if !cache.contains_key(&key) {
|
||||
let resource = actual_resource(
|
||||
files,
|
||||
ResourceKind::Type1,
|
||||
&file,
|
||||
file,
|
||||
&map.version.path.to_string_lossy(),
|
||||
)?;
|
||||
let decoded = font_cache.type1(&resource)?;
|
||||
|
||||
Reference in new issue
Block a user