Share size-independent XDV Type 1 font dictionaries

This commit is contained in:
gloamxun committed 2026-11-28 11:19:57 +08:00
1 parent 01c6f1d4bd
commit e72104eab3
4 files changed
+147 -11

No files matched your search

+41 -7
View File
@@ -732,6 +732,7 @@ fn page_content(
cm_codes: &[Option<&BTreeMap<char, u8>>],
fallback_ids: &[usize; FACE_COUNT],
origin: PageOrigin,
xdv_font_aliases: &BTreeMap<crate::execution::FontId, crate::execution::FontId>,
opentype: &BTreeMap<crate::execution::FontId, Arc<opentype_fonts::EmbeddedFont>>,
font_opacities: &BTreeMap<u8, String>,
) -> Vec<u8> {
@@ -870,6 +871,7 @@ fn page_content(
*x,
*y,
font,
xdv_font_aliases.get(&font.id).copied().unwrap_or(font.id),
paint.as_ref().unwrap(),
*slot,
height,
@@ -2440,10 +2442,20 @@ fn write_pdf(
}
}
let xdv_font_aliases: BTreeMap<_, _> = {
let mut owners = HashMap::new();
native_used.iter().filter_map(|(&id, (_, paint, _))| {
paint.xdv_descriptor?;
let key = (paint.type1_program_key()?, paint.encoding_version());
let owner = *owners.entry(key).or_insert(id);
(owner != id).then_some((id, owner))
}).collect()
};
let mut aliases = BTreeMap::new();
let mut grouped_fonts = BTreeMap::new();
for (id, (face, paint, used)) in native_used {
let owner = pdf_font_owner(registry, id);
let owner = xdv_font_aliases.get(&id).copied()
.unwrap_or_else(|| pdf_font_owner(registry, id));
aliases.insert(id, owner);
let entry = grouped_fonts.entry(owner).or_insert_with(|| {
let face = if owner == id {
@@ -2454,10 +2466,29 @@ fn write_pdf(
};
(face, paint.clone(), BTreeMap::new())
});
if entry.1.encoding != paint.encoding
|| entry.1.type1_program_key() != paint.type1_program_key()
{
return Err("shared PDF font has inconsistent output encoding or program".into());
if !Arc::ptr_eq(&entry.1, &paint) {
if entry.1.encoding != paint.encoding
|| entry.1.type1_program_key() != paint.type1_program_key()
{
return Err("shared PDF font has inconsistent output encoding or program".into());
}
if xdv_font_aliases.contains_key(&id) {
// Separate map entries can resolve to the same physical
// program with disjoint sets of measured glyph widths.
for slot in used.keys() {
let width = *paint.xdv_widths.get(slot)
.ok_or("shared XDV Type 1 font is missing a glyph width")?;
match entry.1.xdv_widths.get(slot) {
Some(old) if *old != width => {
return Err("shared XDV Type 1 font has inconsistent glyph widths".into());
}
Some(_) => {}
None => {
Arc::make_mut(&mut entry.1).xdv_widths.insert(*slot, width);
}
}
}
}
}
entry.2.extend(used);
}
@@ -2893,7 +2924,9 @@ fn write_pdf(
fonts.push_str(&format!(" /M{} {id} 0 R", face.0));
}
let native_fonts: BTreeSet<_> = list.items.iter().filter_map(|item| match item {
DisplayItem::NativeGlyph { font, .. } => Some(font.id),
DisplayItem::NativeGlyph { font, .. } => {
Some(xdv_font_aliases.get(&font.id).copied().unwrap_or(font.id))
}
_ => None,
}).chain(list.pdf_space_plan.iter().filter_map(|plan| {
plan.dedicated.as_ref().map(|font| font.font.id)
@@ -2950,7 +2983,7 @@ fn write_pdf(
let font_opacities = remapped.as_ref().map_or(&no_font_opacity, |r| &r.font_opacities[slot]);
let content = page_content(
list, list.height, &names, faces, &cm_views,
&fallback_font_ids, PageOrigin::default(), &opentype, font_opacities,
&fallback_font_ids, PageOrigin::default(), &xdv_font_aliases, &opentype, font_opacities,
);
objects[device_objects[&form.form.number] as usize - 1] = objects::stream_bytes(
&entries, &content, list.pdf.as_ref().map_or(0, |pdf| pdf.stream_compression),
@@ -3128,6 +3161,7 @@ fn write_pdf(
&cm_views,
&fallback_font_ids,
origin,
&xdv_font_aliases,
&opentype,
font_opacities,
);