Allocate and share native PDF font objects
This commit is contained in:
1 parent
d12c4b034b
commit
51451813dc
12 files changed
+345
-20
No files matched your search
@@ -1542,10 +1542,19 @@ fn native_pdf(
|
||||
}
|
||||
let raw_objects = unicode.and_then(|options| options.objects);
|
||||
let page_tree = unicode.map_or(&[][..], |options| options.page_tree_attributes);
|
||||
let font_attributes: BTreeMap<_, _> = fonts.keys().filter_map(|&font| {
|
||||
let settings = unicode?.fonts.pdf_settings(font)?;
|
||||
(!settings.attributes.is_empty()).then_some((font, settings.attributes.as_slice()))
|
||||
}).collect();
|
||||
let mut font_attributes = BTreeMap::new();
|
||||
if let Some(options) = unicode {
|
||||
for &font in fonts.keys() {
|
||||
let owner = pdf_font_owner(raw_objects, font);
|
||||
let attributes = [owner, font].into_iter().find_map(|id| {
|
||||
let settings = options.fonts.pdf_settings(id)?;
|
||||
(!settings.attributes.is_empty()).then_some(settings.attributes.as_slice())
|
||||
});
|
||||
if let Some(attributes) = attributes {
|
||||
font_attributes.entry(owner).or_insert(attributes);
|
||||
}
|
||||
}
|
||||
}
|
||||
let resources = if raw_objects.is_some()
|
||||
|| !font_attributes.is_empty()
|
||||
|| !page_tree.is_empty()
|
||||
@@ -1737,6 +1746,13 @@ fn native_type1_parameters(
|
||||
}
|
||||
}
|
||||
|
||||
fn pdf_font_owner(
|
||||
objects: Option<&crate::pdf_device::page_resources::PdfObjects>,
|
||||
id: crate::execution::FontId,
|
||||
) -> crate::execution::FontId {
|
||||
objects.and_then(|objects| objects.font_owner(objects.font_number(id)?)).unwrap_or(id)
|
||||
}
|
||||
|
||||
fn write_native_font(
|
||||
objects: &mut Vec<Vec<u8>>,
|
||||
face: &crate::native_font::NativeFont,
|
||||
@@ -1746,15 +1762,18 @@ fn write_native_font(
|
||||
options: Option<crate::pdf_unicode::PdfFinalOptions<'_>>,
|
||||
diagnostics: &mut Vec<String>,
|
||||
reference: Option<&type1_fonts::Reference>,
|
||||
target: Option<usize>,
|
||||
) -> usize {
|
||||
let font = match &paint.program {
|
||||
crate::native_font::NativeFontProgram::Type1(font) => font,
|
||||
crate::native_font::NativeFontProgram::Pk(pk) => {
|
||||
return pk_fonts::write_font(objects, face.id, pk, used, compression);
|
||||
return pk_fonts::write_font(objects, face.id, pk, used, compression, target);
|
||||
}
|
||||
};
|
||||
let id = objects.len() + 1;
|
||||
objects.push(Vec::new());
|
||||
let id = target.unwrap_or_else(|| {
|
||||
objects.push(Vec::new());
|
||||
objects.len()
|
||||
});
|
||||
let m = face.metrics();
|
||||
let descriptor = reference.map_or_else(
|
||||
|| type1_fonts::write_full(objects, font, &native_type1_parameters(font, paint, m), compression),
|
||||
@@ -2226,6 +2245,54 @@ fn write_pdf(
|
||||
));
|
||||
}
|
||||
}
|
||||
let registry = unicode.and_then(|options| options.objects);
|
||||
let mut device_objects = BTreeMap::new();
|
||||
if let Some(registry) = registry {
|
||||
for number in registry.font_object_numbers() {
|
||||
// An unused queried font leaves its reservation free.
|
||||
objects.push(Vec::new());
|
||||
device_objects.insert(number, objects.len() as u32);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
aliases.insert(id, owner);
|
||||
let entry = grouped_fonts.entry(owner).or_insert_with(|| {
|
||||
let face = if owner == id {
|
||||
face
|
||||
} else {
|
||||
let catalog = unicode.expect("font objects have a catalog").fonts;
|
||||
catalog.paint_font(catalog.glyph_font(owner).expect("registered physical font"))
|
||||
};
|
||||
(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());
|
||||
}
|
||||
entry.2.extend(used);
|
||||
}
|
||||
let native_used = grouped_fonts;
|
||||
// Font descriptors are written in reverse registration order.
|
||||
if let Some(registry) = registry {
|
||||
let mut latest = HashMap::new();
|
||||
for (&id, (_, paint, _)) in &native_used {
|
||||
if let Some(number) = registry.font_number(id)
|
||||
&& let Some(program) = paint.type1_program_key()
|
||||
{
|
||||
let entry = latest.entry(program).or_insert((number, id));
|
||||
if number > entry.0 {
|
||||
*entry = (number, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
type1_descriptor_faces.extend(latest.into_iter().map(|(program, (_, id))| (program, id)));
|
||||
}
|
||||
|
||||
// Use the form registered by shipout when one exists. A placement
|
||||
// retains its measured source image and can precede that registration.
|
||||
let declarations = paint_lists.iter().flat_map(|list| &list.items).filter_map(|item| {
|
||||
@@ -2268,7 +2335,7 @@ fn write_pdf(
|
||||
};
|
||||
let (owner, owner_paint) = type1_descriptor_faces.get(&key)
|
||||
.map_or((face, paint), |id| {
|
||||
let (face, paint, _) = &native_used[id];
|
||||
let (face, paint, _) = &native_used[&pdf_font_owner(registry, *id)];
|
||||
(face, paint)
|
||||
});
|
||||
let glyphs = used.keys().map(|slot| paint.encoding[usize::from(*slot)].as_str());
|
||||
@@ -2280,6 +2347,8 @@ fn write_pdf(
|
||||
|
||||
let mut native_font_ids = BTreeMap::new();
|
||||
for (&id, (face, paint, used)) in &native_used {
|
||||
let target = registry.and_then(|objects| objects.font_number(id))
|
||||
.map(|number| device_objects[&number] as usize);
|
||||
native_font_ids.insert(
|
||||
id,
|
||||
write_native_font(
|
||||
@@ -2291,9 +2360,14 @@ fn write_pdf(
|
||||
unicode,
|
||||
diagnostics,
|
||||
paint.type1_program_key().and_then(|key| type1_references.get(&key)),
|
||||
target,
|
||||
),
|
||||
);
|
||||
}
|
||||
for (font, owner) in aliases {
|
||||
let id = native_font_ids[&owner];
|
||||
native_font_ids.insert(font, id);
|
||||
}
|
||||
let mut used_faces = [false; FACE_COUNT];
|
||||
let mut used_chars: [BTreeSet<char>; FACE_COUNT] = Default::default();
|
||||
for page in &paint_lists {
|
||||
@@ -2463,7 +2537,6 @@ fn write_pdf(
|
||||
} else {
|
||||
resources
|
||||
};
|
||||
let mut device_objects = BTreeMap::new();
|
||||
for form in &forms {
|
||||
for number in [form.form.number, form.resource_number] {
|
||||
objects.push(Vec::new());
|
||||
@@ -2578,7 +2651,14 @@ fn write_pdf(
|
||||
for (face, id) in &math_font_ids {
|
||||
fonts.push_str(&format!(" /M{} {id} 0 R", face.0));
|
||||
}
|
||||
for (face, id) in &native_font_ids {
|
||||
let native_fonts: BTreeSet<_> = list.items.iter().filter_map(|item| match item {
|
||||
DisplayItem::NativeGlyph { font, .. } => Some(font.id),
|
||||
_ => None,
|
||||
}).chain(list.pdf_space_plan.iter().filter_map(|plan| {
|
||||
plan.dedicated.as_ref().map(|font| font.font.id)
|
||||
})).collect();
|
||||
for face in native_fonts {
|
||||
let id = native_font_ids[&face];
|
||||
fonts.push_str(&format!(" /N{} {id} 0 R", face.index()));
|
||||
}
|
||||
if !opentype.is_empty() {
|
||||
|
||||
Reference in new issue
Block a user