Render and embed TrueType fonts selected by pdfTeX maps

This commit is contained in:
gloamxun committed 2026-11-28 02:30:30 +08:00
1 parent aa780a800b
commit 46ef76f890
9 files changed
+570 -36

No files matched your search

+42 -23
View File
@@ -26,6 +26,7 @@ pub(crate) mod type1_fonts;
pub(crate) mod objects;
#[path = "pdf_pk.rs"]
mod pk_fonts;
mod truetype_fonts;
#[path = "pdf_opentype.rs"]
mod opentype_fonts;
#[path = "pdf_opentype_unicode.rs"]
@@ -1768,23 +1769,17 @@ fn write_native_font(
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, target);
}
};
subset_tags: &mut BTreeSet<String>,
) -> Result<usize, String> {
use crate::native_font::NativeFontProgram;
if let NativeFontProgram::Pk(pk) = &paint.program {
return Ok(pk_fonts::write_font(objects, face.id, pk, used, compression, target));
}
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),
|shared| shared.object,
);
let unicode = objects.len() + 1;
let first = *used.keys().next().unwrap();
let last = *used.keys().next_back().unwrap();
let widths = (first..=last)
@@ -1801,13 +1796,34 @@ fn write_native_font(
})
.collect::<Vec<_>>()
.join(" ");
let differences = paint
.encoding
.iter()
.map(|n| format!("/{}", native_pdf_name(n)))
.collect::<Vec<_>>()
.join(" ");
let cmap = if let Some(options) = options {
let (subtype, descriptor, name, widths, encoding, has_glyph_unicode) = match &paint.program {
NativeFontProgram::Type1(font) => {
let descriptor = reference.map_or_else(
|| type1_fonts::write_full(objects, font, &native_type1_parameters(font, paint, m), compression),
|shared| shared.object,
);
let name = native_pdf_name(reference.map_or(font.name.as_str(), |shared| shared.name.as_str()));
let differences = paint.encoding.iter()
.map(|n| format!("/{}", native_pdf_name(n)))
.collect::<Vec<_>>()
.join(" ");
let encoding = format!(" /Encoding << /Type /Encoding /Differences [0 {differences}] >>");
("Type1", descriptor, name, format!("[{widths}]"), encoding, true)
}
NativeFontProgram::TrueType(program) => {
let width_object = objects.len() + 1;
objects.push(format!("[{widths}]").into_bytes());
let (descriptor, name) = truetype_fonts::write(
objects, m, paint, program, used.keys().copied(), compression, subset_tags,
)?;
("TrueType", descriptor, name, format!("{width_object} 0 R"), String::new(), program.reencoded)
}
NativeFontProgram::Pk(_) => unreachable!(),
};
let unicode = objects.len() + 1;
let cmap = if !has_glyph_unicode {
None
} else if let Some(options) = options {
if options.fonts.pdf_settings(face.id).is_some_and(|settings| settings.omit_builtin_unicode) {
None
} else if options.generate_unicode > 0 && !options.glyph_unicode.is_empty() {
@@ -1844,12 +1860,11 @@ fn write_native_font(
} else {
String::new()
};
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();
objects[id - 1] = format!("<< /Type /Font /Subtype /{subtype} /BaseFont /{name} /FirstChar {first} /LastChar {last} /Widths {widths}{encoding} /FontDescriptor {descriptor} 0 R {unicode_ref} >>").into_bytes();
if let Some(cmap) = cmap {
objects.push(objects::stream("", &cmap, compression));
}
id
Ok(id)
}
fn pdf_file_specification(out: &mut Vec<u8>, file: &[u8]) {
@@ -2374,6 +2389,9 @@ fn write_pdf(
Some((physical(descriptor), name.map(physical)))
})?;
let mut subset_tags = type1_references.values()
.filter_map(|font| font.name.split_once('+').map(|(tag, _)| tag.to_owned()))
.collect();
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))
@@ -2390,7 +2408,8 @@ fn write_pdf(
diagnostics,
paint.type1_program_key().and_then(|key| type1_references.get(&key)),
target,
),
&mut subset_tags,
)?,
);
}
for (font, owner) in aliases {