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
@@ -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