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
@@ -21,6 +21,7 @@ use crate::render::{DisplayItem, DisplayList, FontFace};
|
||||
use crate::type1::Type1Font;
|
||||
#[path = "pdf_text.rs"]
|
||||
pub(crate) mod native_text;
|
||||
pub(crate) mod type1_fonts;
|
||||
#[path = "pdf_objects.rs"]
|
||||
pub(crate) mod objects;
|
||||
#[path = "pdf_pk.rs"]
|
||||
@@ -1653,21 +1654,11 @@ fn native_pdf_name(name: &str) -> String {
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
struct NativeType1Descriptor<'a> {
|
||||
metrics: &'a crate::tfm::TfmMetrics,
|
||||
object: Option<usize>,
|
||||
}
|
||||
|
||||
fn write_native_type1_descriptor(
|
||||
objects: &mut Vec<Vec<u8>>,
|
||||
fn native_type1_parameters(
|
||||
font: &Type1Font,
|
||||
paint: &crate::native_font::NativeFontPaint,
|
||||
metrics: &crate::tfm::TfmMetrics,
|
||||
compression: u8,
|
||||
) -> usize {
|
||||
let descriptor = objects.len() + 1;
|
||||
let program = descriptor + 1;
|
||||
let name = native_pdf_name(&font.name);
|
||||
) -> String {
|
||||
let [left, bottom, right, top] = paint.bbox;
|
||||
let units = |n: i64| native_font_units(n, native_pdf_font_size(metrics.size_sp()), 1000);
|
||||
// TFM slot conventions of the profile, independent of glyph names in
|
||||
@@ -1683,38 +1674,29 @@ fn write_native_type1_descriptor(
|
||||
} else {
|
||||
font.italic_angle.parse::<f64>().unwrap_or(0.).round().to_string()
|
||||
};
|
||||
let descriptor_bytes = if let Some(xdv) = paint.xdv_descriptor {
|
||||
if let Some(xdv) = paint.xdv_descriptor {
|
||||
let [left, bottom, right, top] = font.bbox;
|
||||
let [ascent, descent, cap, stem_v] = xdv.metrics();
|
||||
let flags = flags | (i32::from(!font.name.contains("Sans")) << 1)
|
||||
| (i32::from(font.name.contains("Caps")) << 17)
|
||||
| (i32::from(xdv.force_bold) << 18);
|
||||
format!("<< /Type /FontDescriptor /FontName /{name} /Flags {flags} /FontBBox [{left} {bottom} {right} {top}] /ItalicAngle {angle} /Ascent {ascent} /Descent {descent} /CapHeight {cap} /StemV {stem_v} /FontFile {program} 0 R >>")
|
||||
format!("/Flags {flags} /FontBBox [{left} {bottom} {right} {top}] /ItalicAngle {angle} /Ascent {ascent} /Descent {descent} /CapHeight {cap} /StemV {stem_v}")
|
||||
} else {
|
||||
let flags = paint.descriptor_flags.unwrap_or(4);
|
||||
let stem_v = paint.stem_v;
|
||||
format!("<< /Type /FontDescriptor /FontName /{name} /Flags {flags} /FontBBox [{left} {bottom} {right} {top}] /ItalicAngle {angle} /Ascent {ascent} /Descent {descent} /CapHeight {cap} /XHeight {x_height} /StemV {stem_v} /FontFile {program} 0 R >>")
|
||||
};
|
||||
objects.push(descriptor_bytes.into_bytes());
|
||||
objects.push(objects::stream(
|
||||
&format!(
|
||||
"/Length1 {} /Length2 {} /Length3 {}",
|
||||
font.length1, font.length2, font.length3
|
||||
),
|
||||
&font.program,
|
||||
compression,
|
||||
));
|
||||
descriptor
|
||||
format!("/Flags {flags} /FontBBox [{left} {bottom} {right} {top}] /ItalicAngle {angle} /Ascent {ascent} /Descent {descent} /CapHeight {cap} /XHeight {x_height} /StemV {stem_v}")
|
||||
}
|
||||
}
|
||||
|
||||
fn write_native_font<'font>(
|
||||
fn write_native_font(
|
||||
objects: &mut Vec<Vec<u8>>,
|
||||
face: &crate::native_font::NativeFont,
|
||||
paint: &'font crate::native_font::NativeFontPaint,
|
||||
paint: &crate::native_font::NativeFontPaint,
|
||||
used: &BTreeMap<u8, char>,
|
||||
compression: u8,
|
||||
options: Option<crate::pdf_unicode::PdfFinalOptions<'_>>,
|
||||
diagnostics: &mut Vec<String>,
|
||||
descriptors: &mut HashMap<crate::native_font::Type1ProgramKey<'font>, NativeType1Descriptor<'_>>,
|
||||
reference: Option<&type1_fonts::Reference>,
|
||||
) -> usize {
|
||||
let font = match &paint.program {
|
||||
crate::native_font::NativeFontProgram::Type1(font) => font,
|
||||
@@ -1725,12 +1707,10 @@ fn write_native_font<'font>(
|
||||
let id = objects.len() + 1;
|
||||
objects.push(Vec::new());
|
||||
let m = face.metrics();
|
||||
let descriptor = match paint.type1_program_key().and_then(|key| descriptors.get_mut(&key)) {
|
||||
Some(shared) => *shared.object.get_or_insert_with(|| {
|
||||
write_native_type1_descriptor(objects, font, paint, shared.metrics, compression)
|
||||
}),
|
||||
None => write_native_type1_descriptor(objects, font, paint, m, compression),
|
||||
};
|
||||
let descriptor = reference.map_or_else(
|
||||
|| type1_fonts::write_full(objects, font, &native_type1_parameters(font, paint, m), compression),
|
||||
|shared| shared.object,
|
||||
);
|
||||
let unicode = objects.len() + 1;
|
||||
let first = *used.keys().next().unwrap();
|
||||
let last = *used.keys().next_back().unwrap();
|
||||
@@ -1791,7 +1771,7 @@ fn write_native_font<'font>(
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
let name = native_pdf_name(&font.name);
|
||||
let name = native_pdf_name(reference.map_or(font.name.as_str(), |shared| shared.name.as_str()));
|
||||
objects[id - 1] = format!("<< /Type /Font /Subtype /Type1 /BaseFont /{name} /FirstChar {first} /LastChar {last} /Widths [{widths}] /Encoding << /Type /Encoding /Differences [0 {differences}] >> /FontDescriptor {descriptor} 0 R {unicode_ref} >>").into_bytes();
|
||||
if let Some(cmap) = cmap {
|
||||
objects.push(objects::stream("", &cmap, compression));
|
||||
@@ -2095,7 +2075,7 @@ fn write_pdf(
|
||||
}
|
||||
let mut native_used = BTreeMap::new();
|
||||
let mut type1_names = HashSet::new();
|
||||
let mut type1_descriptors = HashMap::new();
|
||||
let mut type1_descriptor_faces = HashMap::new();
|
||||
let mut opentype_used = opentype_fonts::UsedFonts::default();
|
||||
for page in pages {
|
||||
for item in &page.items {
|
||||
@@ -2117,10 +2097,7 @@ fn write_pdf(
|
||||
// descriptor, while each font keeps its own encoding.
|
||||
if let Some(program) = paint.type1_program_key() {
|
||||
if type1_names.insert((program, font.name.as_str())) {
|
||||
type1_descriptors.insert(
|
||||
program,
|
||||
NativeType1Descriptor { metrics: font.metrics(), object: None },
|
||||
);
|
||||
type1_descriptor_faces.insert(program, font.id);
|
||||
}
|
||||
}
|
||||
(font.clone(), paint.clone(), BTreeMap::new())
|
||||
@@ -2150,6 +2127,57 @@ fn write_pdf(
|
||||
));
|
||||
}
|
||||
}
|
||||
// Use the form registered by shipout when one exists. A placement
|
||||
// retains its measured source image and can precede that registration.
|
||||
let declarations = pages.iter().flat_map(|list| &list.items).filter_map(|item| {
|
||||
if let DisplayItem::PdfImageObject(object) = item {
|
||||
Some((object.image.id, object.image.image.as_ref(), object.attributes.as_slice(), object.compression))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
let placements = pages.iter().flat_map(|list| {
|
||||
let compression = list.pdf.as_ref().map_or(0, |pdf| pdf.stream_compression);
|
||||
list.items.iter().filter_map(move |item| {
|
||||
if let DisplayItem::PdfImage { image, .. } = item {
|
||||
Some((image.id, image.image.as_ref(), &[][..], compression))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
});
|
||||
let mut image_ids = HashSet::new();
|
||||
let images: Vec<_> = declarations.chain(placements)
|
||||
.filter(|(id, ..)| image_ids.insert(*id))
|
||||
.collect();
|
||||
|
||||
let compression = final_compression.unwrap_or(0).clamp(0, 9) as u8;
|
||||
let mut type1_plans = type1_fonts::FontPlans::default();
|
||||
// pdfTeX collects included font descriptors as it writes forms. The
|
||||
// TeX font dictionaries and their glyph marks are finalized afterward.
|
||||
for (_, image, _, _) in &images {
|
||||
for font in image.mapped_type1_fonts() {
|
||||
type1_plans.include(font);
|
||||
}
|
||||
}
|
||||
for (face, paint, used) in native_used.values() {
|
||||
let crate::native_font::NativeFontProgram::Type1(program) = &paint.program else {
|
||||
continue;
|
||||
};
|
||||
let Some(key) = paint.type1_program_key() else {
|
||||
continue;
|
||||
};
|
||||
let (owner, owner_paint) = type1_descriptor_faces.get(&key)
|
||||
.map_or((face, paint), |id| {
|
||||
let (face, paint, _) = &native_used[id];
|
||||
(face, paint)
|
||||
});
|
||||
let glyphs = used.keys().map(|slot| paint.encoding[usize::from(*slot)].as_str());
|
||||
type1_plans.paint(key, program, owner_paint.subset,
|
||||
|| native_type1_parameters(program, owner_paint, owner.metrics()), glyphs);
|
||||
}
|
||||
let type1_references = type1_plans.write(&mut objects, compression)?;
|
||||
|
||||
let mut native_font_ids = BTreeMap::new();
|
||||
for (&id, (face, paint, used)) in &native_used {
|
||||
native_font_ids.insert(
|
||||
@@ -2159,10 +2187,10 @@ fn write_pdf(
|
||||
face,
|
||||
paint,
|
||||
used,
|
||||
final_compression.unwrap_or(0).max(0).min(9) as u8,
|
||||
compression,
|
||||
unicode,
|
||||
diagnostics,
|
||||
&mut type1_descriptors,
|
||||
paint.type1_program_key().and_then(|key| type1_references.get(&key)),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -2355,31 +2383,9 @@ fn write_pdf(
|
||||
.first()
|
||||
.and_then(|page| page.pdf.as_ref())
|
||||
.map_or((1, 4), |pdf| (pdf.file.major, i32::from(pdf.file.minor)));
|
||||
let mut form_ids = BTreeMap::new();
|
||||
for list in pages {
|
||||
for item in &list.items {
|
||||
if let DisplayItem::PdfImageObject(object) = item {
|
||||
form_ids.entry(object.image.id).or_insert_with(|| {
|
||||
object.image.image.append_to(
|
||||
&mut objects,
|
||||
&object.attributes,
|
||||
object.compression,
|
||||
pdf_version,
|
||||
)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
for list in pages {
|
||||
for item in &list.items {
|
||||
if let DisplayItem::PdfImage { image, .. } = item {
|
||||
form_ids.entry(image.id).or_insert_with(|| {
|
||||
let compression = list.pdf.as_ref().map_or(0, |pdf| pdf.stream_compression);
|
||||
image.image.append_to(&mut objects, &[], compression, pdf_version)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
let form_ids: BTreeMap<_, _> = images.into_iter().map(|(id, image, attributes, compression)| {
|
||||
(id, image.append_to(&mut objects, attributes, compression, pdf_version, &type1_references))
|
||||
}).collect();
|
||||
let mut png_group = None;
|
||||
for (page_index, list) in pages.iter().enumerate() {
|
||||
let png_group_ref = if !list.pdf_resources.has_page_group
|
||||
|
||||
Reference in new issue
Block a user