Resolve imported PDF CFF device fonts from real OpenType faces
This commit is contained in:
1 parent
84d6f3ae51
commit
82e54894c4
14 files changed
+326
-38
No files matched your search
@@ -285,6 +285,15 @@ pub trait SourceProvider {
|
||||
) -> Result<Option<crate::resources::NamedFont>, String> {
|
||||
Ok(None)
|
||||
}
|
||||
/// Resolve an exact PDF device name using the requested CFF charset kind.
|
||||
fn pdf_cff(
|
||||
&mut self,
|
||||
_name: &str,
|
||||
_from: &str,
|
||||
_cid_keyed: bool,
|
||||
) -> Result<Option<crate::resources::NamedFont>, String> {
|
||||
Ok(None)
|
||||
}
|
||||
/// An exact lookup that can report a missing font file before trying another format.
|
||||
fn optional_font_file(
|
||||
&mut self, kind: crate::resources::ResourceKind, name: &str, from: &str,
|
||||
|
||||
@@ -324,6 +324,16 @@ impl NativeFontCache {
|
||||
truetype::device_program(files, name, from)
|
||||
}
|
||||
|
||||
/// Resolve the actual CFF table for a PDF device font.
|
||||
pub fn pdf_device_cff(
|
||||
files: &mut impl crate::execution::SourceProvider,
|
||||
name: &str,
|
||||
from: &str,
|
||||
cid_keyed: bool,
|
||||
) -> Result<Option<Arc<[u8]>>, String> {
|
||||
cff::device_program(files, name, from, cid_keyed)
|
||||
}
|
||||
|
||||
// The caller reads the actual resource and verifies its bytes on this
|
||||
// revision before asking for the decoded representation.
|
||||
fn type1(&self, resource: &LoadedResource, transform: Type1Transform) -> Result<Arc<DecodedType1>, String> {
|
||||
|
||||
@@ -21,6 +21,34 @@ impl OpenTypeCffProgram {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn device_program(
|
||||
files: &mut impl SourceProvider,
|
||||
name: &str,
|
||||
from: &str,
|
||||
cid_keyed: bool,
|
||||
) -> Result<Option<Arc<[u8]>>, String> {
|
||||
let Some(selected) = files.pdf_cff(name, from, cid_keyed)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
let resource = super::verify_resource(selected.resource, ResourceKind::OpenType, name)?;
|
||||
use crate::resources::{PdfSfntKind, cff_program, exact_pdf_sfnt_name};
|
||||
if exact_pdf_sfnt_name(
|
||||
&resource.data,
|
||||
selected.index,
|
||||
name,
|
||||
PdfSfntKind::Cff { cid_keyed },
|
||||
)
|
||||
.is_none()
|
||||
{
|
||||
return Err(format!(
|
||||
"PDF device font {name} does not match the selected CFF face"
|
||||
));
|
||||
}
|
||||
let bytes = cff_program(&resource.data, selected.index, cid_keyed)
|
||||
.ok_or_else(|| format!("PDF device font {name} has an invalid CFF program"))?;
|
||||
Ok(Some(bytes.into()))
|
||||
}
|
||||
|
||||
pub(super) fn resolve(
|
||||
files: &mut impl SourceProvider,
|
||||
outlines: &mut OutlineFiles,
|
||||
|
||||
@@ -87,7 +87,9 @@ pub(super) fn device_program(
|
||||
) -> Result<Option<Arc<[u8]>>, String> {
|
||||
let Some(selected) = files.pdf_truetype(name, from)? else { return Ok(None) };
|
||||
let resource = super::verify_resource(selected.resource, ResourceKind::TrueType, name)?;
|
||||
if crate::resources::exact_truetype_name(&resource.data, selected.index, name).is_none() {
|
||||
if crate::resources::exact_pdf_sfnt_name(
|
||||
&resource.data, selected.index, name, crate::resources::PdfSfntKind::TrueType,
|
||||
).is_none() {
|
||||
return Err(format!("PDF device font {name} does not match the selected TrueType face"));
|
||||
}
|
||||
let data = if resource.data.starts_with(b"ttcf") {
|
||||
|
||||
@@ -61,6 +61,8 @@ pub struct PdfImage {
|
||||
device_type1_fonts: Vec<String>,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
device_truetype_fonts: Vec<String>,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
device_cid_cff_fonts: Vec<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pdftex_source: Option<imports::SourceGraph>,
|
||||
}
|
||||
@@ -426,6 +428,7 @@ impl PdfImage {
|
||||
imported_fonts: Vec::new(),
|
||||
device_type1_fonts: Vec::new(),
|
||||
device_truetype_fonts: Vec::new(),
|
||||
device_cid_cff_fonts: Vec::new(),
|
||||
pdftex_source: None,
|
||||
}
|
||||
}
|
||||
@@ -567,6 +570,7 @@ impl PdfImage {
|
||||
imported_fonts: Vec::new(),
|
||||
device_type1_fonts: Vec::new(),
|
||||
device_truetype_fonts: Vec::new(),
|
||||
device_cid_cff_fonts: Vec::new(),
|
||||
pdftex_source: None,
|
||||
}
|
||||
}
|
||||
@@ -618,6 +622,7 @@ impl PdfImage {
|
||||
imported_fonts: Vec::new(),
|
||||
device_type1_fonts: Vec::new(),
|
||||
device_truetype_fonts: Vec::new(),
|
||||
device_cid_cff_fonts: Vec::new(),
|
||||
pdftex_source: None,
|
||||
})
|
||||
}
|
||||
@@ -896,8 +901,7 @@ impl PdfImage {
|
||||
Vec::new()
|
||||
};
|
||||
let pdftex_source = pdftex.then(|| imports::SourceGraph::capture(&mut graph)).transpose()?;
|
||||
let (device_type1_fonts, device_truetype_fonts) =
|
||||
device_fonts::capture(&doc, inherited(b"Resources"));
|
||||
let fonts = device_fonts::capture(&doc, inherited(b"Resources"));
|
||||
Ok((
|
||||
Self {
|
||||
version: resource.version,
|
||||
@@ -909,8 +913,9 @@ impl PdfImage {
|
||||
objects: graph.bodies,
|
||||
color_space: None,
|
||||
imported_fonts,
|
||||
device_type1_fonts,
|
||||
device_truetype_fonts,
|
||||
device_type1_fonts: fonts.type1,
|
||||
device_truetype_fonts: fonts.truetype,
|
||||
device_cid_cff_fonts: fonts.cid_cff,
|
||||
pdftex_source,
|
||||
},
|
||||
[left, bottom],
|
||||
@@ -927,6 +932,11 @@ impl PdfImage {
|
||||
&self.device_truetype_fonts
|
||||
}
|
||||
|
||||
/// Unembedded CIDFontType0 names recorded from this selected PDF page.
|
||||
pub fn device_cid_cff_fonts(&self) -> &[String] {
|
||||
&self.device_cid_cff_fonts
|
||||
}
|
||||
|
||||
pub(crate) fn extent(&self) -> [f64; 2] {
|
||||
self.extent.map(f64::from_bits)
|
||||
}
|
||||
|
||||
@@ -7,11 +7,19 @@ use std::collections::{BTreeSet, HashSet};
|
||||
struct Names {
|
||||
type1: BTreeSet<String>,
|
||||
truetype: BTreeSet<String>,
|
||||
cid_cff: BTreeSet<String>,
|
||||
}
|
||||
|
||||
pub(super) fn capture(doc: &Document, resources: Option<&Object>) -> (Vec<String>, Vec<String>) {
|
||||
#[derive(Default)]
|
||||
pub(super) struct DeviceFontNames {
|
||||
pub(super) type1: Vec<String>,
|
||||
pub(super) truetype: Vec<String>,
|
||||
pub(super) cid_cff: Vec<String>,
|
||||
}
|
||||
|
||||
pub(super) fn capture(doc: &Document, resources: Option<&Object>) -> DeviceFontNames {
|
||||
let Some(resources) = resources.and_then(|value| resolved(doc, value).ok()) else {
|
||||
return (Vec::new(), Vec::new());
|
||||
return DeviceFontNames::default();
|
||||
};
|
||||
let mut pending = vec![resources];
|
||||
let mut seen = HashSet::new();
|
||||
@@ -70,10 +78,11 @@ pub(super) fn capture(doc: &Document, resources: Option<&Object>) -> (Vec<String
|
||||
}
|
||||
}
|
||||
}
|
||||
(
|
||||
names.type1.into_iter().collect(),
|
||||
names.truetype.into_iter().collect(),
|
||||
)
|
||||
DeviceFontNames {
|
||||
type1: names.type1.into_iter().collect(),
|
||||
truetype: names.truetype.into_iter().collect(),
|
||||
cid_cff: names.cid_cff.into_iter().collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get<'a>(doc: &'a Document, dict: &'a Dictionary, key: &[u8]) -> Option<&'a Object> {
|
||||
@@ -106,10 +115,12 @@ fn capture_font<'a>(
|
||||
.first()
|
||||
.and_then(|value| resolved(doc, value).ok())
|
||||
.and_then(|value| value.as_dict().ok())
|
||||
&& get(doc, descendant, b"Subtype").and_then(|value| value.as_name().ok())
|
||||
== Some(b"CIDFontType2")
|
||||
{
|
||||
capture_name(doc, descendant, &mut names.truetype);
|
||||
match get(doc, descendant, b"Subtype").and_then(|value| value.as_name().ok()) {
|
||||
Some(b"CIDFontType2") => capture_name(doc, descendant, &mut names.truetype),
|
||||
Some(b"CIDFontType0") => capture_name(doc, descendant, &mut names.cid_cff),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(b"Type3") => {
|
||||
|
||||
@@ -149,6 +149,7 @@ pub(super) fn read(
|
||||
imported_fonts: Vec::new(),
|
||||
device_type1_fonts: Vec::new(),
|
||||
device_truetype_fonts: Vec::new(),
|
||||
device_cid_cff_fonts: Vec::new(),
|
||||
pdftex_source: None,
|
||||
restored_pdf_origin: None,
|
||||
},
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
mod deferred;
|
||||
mod named_fonts;
|
||||
pub use named_fonts::{FontStyle, NamedFont};
|
||||
pub(crate) use named_fonts::exact_truetype_name;
|
||||
pub(crate) use named_fonts::{PdfSfntKind, cff_program, exact_pdf_sfnt_name};
|
||||
mod project;
|
||||
pub use deferred::{
|
||||
DeferredProbe, DirectoryAbsence, GeneratedLookup, GeneratedLookupOrigin, GeneratedLookupRole,
|
||||
|
||||
@@ -85,9 +85,43 @@ impl NamedFace {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn exact_truetype_name(data: &[u8], index: u32, requested: &str) -> Option<String> {
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) enum PdfSfntKind {
|
||||
TrueType,
|
||||
Cff { cid_keyed: bool },
|
||||
}
|
||||
|
||||
impl PdfSfntKind {
|
||||
fn resource_kind(self) -> ResourceKind {
|
||||
match self {
|
||||
Self::TrueType => ResourceKind::TrueType,
|
||||
Self::Cff { .. } => ResourceKind::OpenType,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn cff_program(data: &[u8], index: u32, cid_keyed: bool) -> Option<&[u8]> {
|
||||
let face = ttf_parser::RawFace::parse(data, index).ok()?;
|
||||
let bytes = face.table(ttf_parser::Tag::from_bytes(b"CFF "))?;
|
||||
let cff = read_fonts::ps::cff::CffFontRef::new_cff(bytes, 0, None).ok()?;
|
||||
(cff.is_cid() == cid_keyed).then_some(bytes)
|
||||
}
|
||||
|
||||
pub(crate) fn exact_pdf_sfnt_name(
|
||||
data: &[u8],
|
||||
index: u32,
|
||||
requested: &str,
|
||||
kind: PdfSfntKind,
|
||||
) -> Option<String> {
|
||||
let face = ttf_parser::Face::parse(data, index).ok()?;
|
||||
face.tables().glyf.as_ref()?;
|
||||
match kind {
|
||||
PdfSfntKind::TrueType => {
|
||||
face.tables().glyf.as_ref()?;
|
||||
}
|
||||
PdfSfntKind::Cff { cid_keyed } => {
|
||||
cff_program(data, index, cid_keyed)?;
|
||||
}
|
||||
}
|
||||
let mut names = face.names().into_iter();
|
||||
names
|
||||
.any(|name| {
|
||||
@@ -220,18 +254,37 @@ impl FileWorld {
|
||||
&mut self,
|
||||
name: &str,
|
||||
from: &Path,
|
||||
) -> Result<Option<NamedFont>, String> {
|
||||
self.read_pdf_sfnt(name, from, PdfSfntKind::TrueType)
|
||||
}
|
||||
|
||||
/// Resolve a versioned OpenType face with the requested CFF charset kind.
|
||||
pub fn read_pdf_cff(
|
||||
&mut self,
|
||||
name: &str,
|
||||
from: &Path,
|
||||
cid_keyed: bool,
|
||||
) -> Result<Option<NamedFont>, String> {
|
||||
self.read_pdf_sfnt(name, from, PdfSfntKind::Cff { cid_keyed })
|
||||
}
|
||||
|
||||
fn read_pdf_sfnt(
|
||||
&mut self,
|
||||
name: &str,
|
||||
from: &Path,
|
||||
kind: PdfSfntKind,
|
||||
) -> Result<Option<NamedFont>, String> {
|
||||
let mut candidates = Vec::new();
|
||||
let result = self.select_pdf_truetype(name, from, &mut candidates);
|
||||
let result = self.select_pdf_sfnt(name, from, kind, &mut candidates);
|
||||
self.trace.lookups.push(ResourceLookup {
|
||||
kind: ResourceKind::TrueType,
|
||||
kind: kind.resource_kind(),
|
||||
name: name.into(),
|
||||
from: from.into(),
|
||||
candidates,
|
||||
resolved: result
|
||||
.as_ref()
|
||||
.ok()
|
||||
.and_then(|font| font.as_ref())
|
||||
.and_then(|result| result.as_ref())
|
||||
.map(|font| font.resource.version.path.clone()),
|
||||
lookup_path: None,
|
||||
error: result.as_ref().err().cloned(),
|
||||
@@ -239,16 +292,23 @@ impl FileWorld {
|
||||
result
|
||||
}
|
||||
|
||||
fn select_pdf_truetype(
|
||||
fn select_pdf_sfnt(
|
||||
&mut self,
|
||||
name: &str,
|
||||
from: &Path,
|
||||
kind: PdfSfntKind,
|
||||
candidates: &mut Vec<PathBuf>,
|
||||
) -> Result<Option<NamedFont>, String> {
|
||||
let mut scoped_paths = self.paths(ResourceKind::TrueType, from);
|
||||
scoped_paths.extend(self.paths(ResourceKind::OpenType, from));
|
||||
let search_order = match kind {
|
||||
PdfSfntKind::TrueType => [ResourceKind::TrueType, ResourceKind::OpenType],
|
||||
PdfSfntKind::Cff { .. } => [ResourceKind::OpenType, ResourceKind::TrueType],
|
||||
};
|
||||
let mut seen = BTreeSet::new();
|
||||
for (entry, _) in scoped_paths {
|
||||
for (entry, _) in search_order
|
||||
.into_iter()
|
||||
.flat_map(|kind| self.paths(kind, from))
|
||||
.collect::<Vec<_>>()
|
||||
{
|
||||
let SearchPath::Directory {
|
||||
path,
|
||||
recursive,
|
||||
@@ -298,7 +358,7 @@ impl FileWorld {
|
||||
continue;
|
||||
}
|
||||
for index in 0..count as u32 {
|
||||
if let Some(full_name) = exact_truetype_name(&data.data, index, name) {
|
||||
if let Some(full_name) = exact_pdf_sfnt_name(&data.data, index, name, kind) {
|
||||
let resource = self.read_path(path)?;
|
||||
return Ok(Some(NamedFont {
|
||||
resource,
|
||||
@@ -333,7 +393,7 @@ impl FileWorld {
|
||||
continue;
|
||||
};
|
||||
let data = self.input_file(path.clone())?;
|
||||
if let Some(full_name) = exact_truetype_name(&data.data, index, name) {
|
||||
if let Some(full_name) = exact_pdf_sfnt_name(&data.data, index, name, kind) {
|
||||
let resource = self.read_path(path)?;
|
||||
return Ok(Some(NamedFont {
|
||||
resource,
|
||||
|
||||
Reference in new issue
Block a user