Allocate mapped Type 1 import objects in PDF write order

This commit is contained in:
gloamxun committed 2026-11-28 00:30:57 +08:00
1 parent 861c9dbfc9
commit c1c3c25b3f
9 files changed
+466 -95

No files matched your search

+35 -5
View File
@@ -14,6 +14,7 @@ use std::{
pub(crate) struct Reference {
pub object: usize,
pub name: String,
pub name_object: Option<usize>,
}
pub(crate) type References<'a> = HashMap<Type1ProgramKey<'a>, Reference>;
@@ -84,6 +85,7 @@ impl<'a> FontPlans<'a> {
objects: &mut Vec<Vec<u8>>,
compression: u8,
omit_charset: bool,
reserved: impl Fn(Type1ProgramKey<'a>) -> Option<(usize, Option<usize>)>,
) -> Result<References<'a>, String> {
let mut plans: Vec<_> = self.0.into_iter().collect();
plans.sort_unstable_by(|((a, at), _), ((b, bt), _)| {
@@ -98,6 +100,8 @@ impl<'a> FontPlans<'a> {
let mut tags = BTreeSet::new();
let mut references = HashMap::new();
for (key, plan) in plans {
let slots = reserved(key);
let name_object = slots.and_then(|(_, name)| name);
let reference = if plan.subset {
let tag = (0..100)
.map(|salt| subset_tag(&plan.font.name, &plan.glyphs, salt))
@@ -114,13 +118,25 @@ impl<'a> FontPlans<'a> {
&subset.program,
lengths,
compression,
slots,
),
name: subset.name,
name_object,
}
} else {
Reference {
object: write_full(objects, plan.font, &plan.parameters, compression),
object: write_embedding(
objects,
&plan.font.name,
&plan.parameters,
None,
&plan.font.program,
[plan.font.length1, plan.font.length2, plan.font.length3],
compression,
slots,
),
name: plan.font.name.clone(),
name_object,
}
};
references.insert(key, reference);
@@ -143,6 +159,7 @@ pub(super) fn write_full(
&font.program,
[font.length1, font.length2, font.length3],
compression,
None,
)
}
@@ -154,11 +171,24 @@ fn write_embedding(
program: &[u8],
lengths: [usize; 3],
compression: u8,
slots: Option<(usize, Option<usize>)>,
) -> usize {
let descriptor = objects.len() + 1;
let file = descriptor + 1;
let descriptor = slots.map_or_else(
|| {
objects.push(Vec::new());
objects.len()
},
|(descriptor, _)| descriptor,
);
let file = objects.len() + 1;
let name = native_pdf_name(name);
let mut body = format!("<< /Type /FontDescriptor /FontName /{name} {parameters}");
let font_name = if let Some((_, Some(name_id))) = slots {
objects[name_id - 1] = format!("/{name}").into_bytes();
format!("{name_id} 0 R")
} else {
format!("/{name}")
};
let mut body = format!("<< /Type /FontDescriptor /FontName {font_name} {parameters}");
if let Some(glyphs) = glyphs {
body.push_str(" /CharSet <");
for glyph in glyphs {
@@ -170,7 +200,7 @@ fn write_embedding(
body.push('>');
}
write!(body, " /FontFile {file} 0 R >>").unwrap();
objects.push(body.into_bytes());
objects[descriptor - 1] = body.into_bytes();
let [clear, private, trailer] = lengths;
let keys = format!("/Length1 {clear} /Length2 {private} /Length3 {trailer}");
objects.push(objects::stream(&keys, program, compression));