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
@@ -166,6 +166,16 @@ impl SourceProvider for RecordingHost<'_> {
|
||||
})).map_err(Clone::clone)
|
||||
})
|
||||
}
|
||||
fn pdf_cff(
|
||||
&mut self, name: &str, from: &str, cid_keyed: bool,
|
||||
) -> Result<Option<texrra_tex_core::resources::NamedFont>, String> {
|
||||
let (name, from) = (name.to_owned(), from.to_owned());
|
||||
call!(self, pdf_cff(&name, &from, cid_keyed); |result| {
|
||||
result.as_ref().map(|result| result.as_ref().map(|font| {
|
||||
(font.resource.version.clone(), font.index, font.full_name.clone())
|
||||
})).map_err(Clone::clone)
|
||||
})
|
||||
}
|
||||
fn optional_font_file(&mut self, kind: ResourceKind, name: &str, from: &str) -> Result<Option<LoadedResource>, String> {
|
||||
let (name, from) = (name.to_owned(), from.to_owned());
|
||||
call!(self, optional_font_file(kind, &name, &from); |result| {
|
||||
|
||||
@@ -1685,6 +1685,15 @@ impl SourceProvider for Files {
|
||||
}
|
||||
Ok(selected)
|
||||
}
|
||||
fn pdf_cff(
|
||||
&mut self, name: &str, from: &str, cid_keyed: bool,
|
||||
) -> Result<Option<texrra_tex_core::resources::NamedFont>, String> {
|
||||
let selected = self.world.read_pdf_cff(name, Path::new(from), cid_keyed)?;
|
||||
if let Some(font) = &selected {
|
||||
self.observe_resource(ResourceKind::OpenType, &font.resource);
|
||||
}
|
||||
Ok(selected)
|
||||
}
|
||||
fn optional_font_file(&mut self, kind: ResourceKind, name: &str, from: &str) -> Result<Option<LoadedResource>, String> {
|
||||
let resource = self.world.read_optional(kind, name, Path::new(from))?;
|
||||
if let Some(resource) = &resource {
|
||||
|
||||
@@ -17,6 +17,8 @@ mod overlay;
|
||||
pub(crate) struct PreviewFonts {
|
||||
type1: BTreeMap<String, Arc<Type1Font>>,
|
||||
truetype: BTreeMap<String, Arc<[u8]>>,
|
||||
cff: BTreeMap<String, Arc<[u8]>>,
|
||||
cid_cff: BTreeMap<String, Arc<[u8]>>,
|
||||
missing: bool,
|
||||
conflict: Option<String>,
|
||||
}
|
||||
@@ -40,6 +42,7 @@ impl PreviewFonts {
|
||||
let mut visited = BTreeSet::new();
|
||||
let mut required_type1: BTreeSet<(&Path, &str)> = BTreeSet::new();
|
||||
let mut required_truetype = BTreeSet::new();
|
||||
let mut required_cid_cff = BTreeSet::new();
|
||||
while let Some(list) = pending.pop() {
|
||||
for item in &list.items {
|
||||
match item {
|
||||
@@ -70,6 +73,12 @@ impl PreviewFonts {
|
||||
.iter()
|
||||
.map(|name| (source.version.path.as_path(), name.as_str())),
|
||||
);
|
||||
required_cid_cff.extend(
|
||||
source
|
||||
.device_cid_cff_fonts()
|
||||
.iter()
|
||||
.map(|name| (source.version.path.as_path(), name.as_str())),
|
||||
);
|
||||
}
|
||||
DisplayItem::PdfForm { form, .. } => {
|
||||
if visited.insert(form.number)
|
||||
@@ -83,14 +92,26 @@ impl PreviewFonts {
|
||||
}
|
||||
}
|
||||
for (from, name) in required_type1 {
|
||||
let program = cache.pdf_device_type1(files, name, &from.to_string_lossy())?;
|
||||
record(
|
||||
&mut fonts.type1,
|
||||
&mut fonts.missing,
|
||||
&mut fonts.conflict,
|
||||
name,
|
||||
program.as_ref(),
|
||||
);
|
||||
let from = from.to_string_lossy();
|
||||
let program = cache.pdf_device_type1(files, name, &from)?;
|
||||
if let Some(program) = &program {
|
||||
record(
|
||||
&mut fonts.type1,
|
||||
&mut fonts.missing,
|
||||
&mut fonts.conflict,
|
||||
name,
|
||||
Some(program),
|
||||
);
|
||||
} else {
|
||||
let program = NativeFontCache::pdf_device_cff(files, name, &from, false)?;
|
||||
record(
|
||||
&mut fonts.cff,
|
||||
&mut fonts.missing,
|
||||
&mut fonts.conflict,
|
||||
name,
|
||||
program.as_ref(),
|
||||
);
|
||||
}
|
||||
}
|
||||
for (from, name) in required_truetype {
|
||||
let program =
|
||||
@@ -103,11 +124,36 @@ impl PreviewFonts {
|
||||
program.as_ref(),
|
||||
);
|
||||
}
|
||||
for (from, name) in required_cid_cff {
|
||||
let program =
|
||||
NativeFontCache::pdf_device_cff(files, name, &from.to_string_lossy(), true)?;
|
||||
record(
|
||||
&mut fonts.cid_cff,
|
||||
&mut fonts.missing,
|
||||
&mut fonts.conflict,
|
||||
name,
|
||||
program.as_ref(),
|
||||
);
|
||||
}
|
||||
if let Some(name) = fonts
|
||||
.type1
|
||||
.keys()
|
||||
.find(|name| fonts.cff.contains_key(*name))
|
||||
{
|
||||
fonts.conflict = Some(name.clone());
|
||||
}
|
||||
Ok(fonts)
|
||||
}
|
||||
|
||||
pub(super) fn includes_device_fonts(&self) -> bool {
|
||||
self.missing || !self.type1.is_empty() || !self.truetype.is_empty()
|
||||
self.missing || !self.programs_empty()
|
||||
}
|
||||
|
||||
fn programs_empty(&self) -> bool {
|
||||
self.type1.is_empty()
|
||||
&& self.truetype.is_empty()
|
||||
&& self.cff.is_empty()
|
||||
&& self.cid_cff.is_empty()
|
||||
}
|
||||
|
||||
/// Attach the captured programs to a private copy for PDF-backed previews.
|
||||
@@ -117,7 +163,7 @@ impl PreviewFonts {
|
||||
"conflicting actual fonts for PDF preview device name {name}"
|
||||
));
|
||||
}
|
||||
if self.type1.is_empty() && self.truetype.is_empty() {
|
||||
if self.programs_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut document = Document::load_mem(pdf)
|
||||
@@ -162,11 +208,15 @@ impl PreviewFonts {
|
||||
(name.as_bytes(), stream)
|
||||
})
|
||||
.collect();
|
||||
let cff = cff_streams(&mut document, &self.cff, b"Type1C");
|
||||
let cid_cff = cff_streams(&mut document, &self.cid_cff, b"CIDFontType0C");
|
||||
let attachment = Attachment {
|
||||
names,
|
||||
descriptors,
|
||||
type1,
|
||||
truetype,
|
||||
cff,
|
||||
cid_cff,
|
||||
};
|
||||
let mut attached = false;
|
||||
let changes: Vec<_> = document
|
||||
@@ -190,6 +240,22 @@ impl PreviewFonts {
|
||||
}
|
||||
}
|
||||
|
||||
fn cff_streams<'a>(
|
||||
document: &mut Document,
|
||||
programs: &'a BTreeMap<String, Arc<[u8]>>,
|
||||
subtype: &[u8],
|
||||
) -> BTreeMap<&'a [u8], ObjectId> {
|
||||
programs
|
||||
.iter()
|
||||
.map(|(name, bytes)| {
|
||||
let mut dictionary = Dictionary::new();
|
||||
dictionary.set("Subtype", Object::Name(subtype.to_vec()));
|
||||
let stream = document.add_object(Stream::new(dictionary, bytes.to_vec()));
|
||||
(name.as_bytes(), stream)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn record<T: PartialEq + ?Sized>(
|
||||
programs: &mut BTreeMap<String, Arc<T>>,
|
||||
missing: &mut bool,
|
||||
@@ -215,6 +281,8 @@ struct Attachment<'a> {
|
||||
descriptors: BTreeMap<ObjectId, Dictionary>,
|
||||
type1: BTreeMap<&'a [u8], (&'a Arc<Type1Font>, ObjectId)>,
|
||||
truetype: BTreeMap<&'a [u8], ObjectId>,
|
||||
cff: BTreeMap<&'a [u8], ObjectId>,
|
||||
cid_cff: BTreeMap<&'a [u8], ObjectId>,
|
||||
}
|
||||
|
||||
impl Attachment<'_> {
|
||||
@@ -251,10 +319,19 @@ impl Attachment<'_> {
|
||||
};
|
||||
let (file_key, stream, bbox) = match name(b"Subtype") {
|
||||
Some(b"Type1") => {
|
||||
let Some((program, stream)) = self.type1.get(base) else {
|
||||
if let Some((program, stream)) = self.type1.get(base) {
|
||||
("FontFile", stream, Some(program.bbox))
|
||||
} else if let Some(stream) = self.cff.get(base) {
|
||||
("FontFile3", stream, None)
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Some(b"CIDFontType0") => {
|
||||
let Some(stream) = self.cid_cff.get(base) else {
|
||||
return false;
|
||||
};
|
||||
("FontFile", stream, Some(program.bbox))
|
||||
("FontFile3", stream, None)
|
||||
}
|
||||
Some(b"TrueType" | b"CIDFontType2") => {
|
||||
let Some(stream) = self.truetype.get(base) else {
|
||||
|
||||
Reference in new issue
Block a user