Share physical Type 1 fonts across PDF page resources
This commit is contained in:
1 parent
5edd4ae4ec
commit
38c567623a
11 files changed
+385
-140
No files matched your search
@@ -1,9 +1,9 @@
|
||||
//! Replacement of fonts in the root resources of an included pdfTeX page.
|
||||
use super::{Graph, Part, PdfImage, number, pdf_name, resolved};
|
||||
use crate::type1::{Type1Font, builtin_encoding, standard_encoding};
|
||||
use super::{Graph, Part, PdfImage, number, resolved};
|
||||
use crate::{native_font::MappedType1, type1::{builtin_encoding, standard_encoding}};
|
||||
use lopdf::{Dictionary, Document, Object, Stream};
|
||||
use read_fonts::ps::cff::CffFontRef;
|
||||
use std::{borrow::Cow, collections::BTreeSet};
|
||||
use std::{borrow::Cow, collections::BTreeSet, sync::Arc};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub(super) struct ImportedType1 {
|
||||
@@ -256,79 +256,51 @@ fn replace_implicit_encoding(
|
||||
Ok(Some(generated))
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub(crate) struct ImportedType1Use {
|
||||
pub mapped: MappedType1,
|
||||
pub stem_v: i32,
|
||||
pub glyphs: Option<BTreeSet<String>>,
|
||||
}
|
||||
|
||||
impl PdfImage {
|
||||
pub(crate) fn mapped_type1_fonts(&self) -> impl Iterator<Item = &ImportedType1Use> {
|
||||
self.objects.iter().flatten().filter_map(|part| {
|
||||
if let Part::MappedType1(font) = part {
|
||||
Some(font.as_ref())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn has_imported_fonts(&self) -> bool {
|
||||
!self.imported_fonts.is_empty()
|
||||
}
|
||||
|
||||
pub(crate) fn resolve_imported_fonts(
|
||||
&self,
|
||||
mut load: impl FnMut(&str) -> Result<Option<(Type1Font, i32, bool)>, String>,
|
||||
mut load: impl FnMut(&str) -> Result<Option<MappedType1>, String>,
|
||||
) -> Result<Option<Self>, String> {
|
||||
let mut result: Option<Self> = None;
|
||||
for entry in &self.imported_fonts {
|
||||
let Some((program, flags, allow_subset)) = load(&entry.postscript_name)? else {
|
||||
let Some(mapped) = load(&entry.postscript_name)? else {
|
||||
continue;
|
||||
};
|
||||
let subset = entry.charset.as_deref().filter(|_| allow_subset)
|
||||
let glyphs = entry.charset.as_deref().filter(|_| mapped.subset)
|
||||
.map(|bytes| {
|
||||
let charset = std::str::from_utf8(bytes)
|
||||
let names = std::str::from_utf8(bytes)
|
||||
.map_err(|_| "invalid glyph name in imported Type1 CharSet")?;
|
||||
let glyphs = charset.split('/').filter(|name| !name.is_empty()).map(str::to_owned).collect();
|
||||
crate::type1_outline::write_subset(&program, &glyphs)
|
||||
Ok::<_, &str>(names.split('/').skip(1)
|
||||
.filter_map(|part| part.split_ascii_whitespace().next())
|
||||
.map(str::to_owned).collect())
|
||||
})
|
||||
.transpose()?;
|
||||
let font_name = subset.as_ref().map_or(program.name.as_str(), |subset| subset.name.as_str());
|
||||
let stem_v = crate::type1_outline::pdftex_stem_v(&mapped.font)?.unwrap_or(entry.stem_v);
|
||||
let mut dictionary = entry.dictionary.clone()?;
|
||||
let mut name = b" /BaseFont ".to_vec();
|
||||
name.extend(pdf_name(font_name.as_bytes()));
|
||||
name.extend(b" /FontDescriptor ");
|
||||
let image = result.get_or_insert_with(|| self.clone());
|
||||
let descriptor = image.objects.len();
|
||||
dictionary.push(Part::Bytes(name));
|
||||
dictionary.push(Part::Ref(descriptor));
|
||||
dictionary.push(Part::MappedType1(Arc::new(ImportedType1Use { mapped, stem_v, glyphs })));
|
||||
dictionary.push(Part::Bytes(b">>".to_vec()));
|
||||
image.objects[entry.index] = dictionary;
|
||||
|
||||
let [left, bottom, right, top] = program.bbox;
|
||||
let angle = &program.italic_angle;
|
||||
let stem_v = crate::type1_outline::pdftex_stem_v(&program)?.unwrap_or(entry.stem_v);
|
||||
let mut body = b"<< /Type /FontDescriptor /FontName ".to_vec();
|
||||
body.extend(pdf_name(font_name.as_bytes()));
|
||||
body.extend(format!(
|
||||
" /Flags {flags} /FontBBox [{left} {bottom} {right} {top}] /ItalicAngle {angle} \
|
||||
/Ascent {top} /Descent {bottom} /CapHeight {top} /StemV {stem_v}",
|
||||
).into_bytes());
|
||||
if let Some(subset) = &subset {
|
||||
use std::fmt::Write as _;
|
||||
let mut charset = String::from(" /CharSet <");
|
||||
for glyph in &subset.glyphs {
|
||||
charset.push_str("2F");
|
||||
for byte in glyph.bytes() {
|
||||
write!(charset, "{byte:02X}").unwrap();
|
||||
}
|
||||
}
|
||||
charset.push('>');
|
||||
body.extend(charset.bytes());
|
||||
}
|
||||
body.extend_from_slice(b" /FontFile ");
|
||||
image.objects.push(vec![
|
||||
Part::Bytes(body),
|
||||
Part::Ref(descriptor + 1),
|
||||
Part::Bytes(b">>".to_vec()),
|
||||
]);
|
||||
let ([header, private, trailer], bytes) = subset.as_ref().map_or(
|
||||
([program.length1, program.length2, program.length3], program.program.as_slice()),
|
||||
|subset| ([subset.length1, subset.program.len() - subset.length1, 0], subset.program.as_slice()),
|
||||
);
|
||||
let keys = format!("/Length1 {header} /Length2 {private} /Length3 {trailer}");
|
||||
image
|
||||
.objects
|
||||
.push(vec![Part::Bytes(crate::pdf::objects::stream(
|
||||
&keys,
|
||||
bytes,
|
||||
0,
|
||||
))]);
|
||||
result.get_or_insert_with(|| self.clone()).objects[entry.index] = dictionary;
|
||||
}
|
||||
if let Some(image) = result.as_mut() {
|
||||
image.imported_fonts.clear();
|
||||
|
||||
Reference in new issue
Block a user