Render and embed OpenType CFF fonts selected by pdfTeX maps

This commit is contained in:
gloamxun committed 2026-11-28 02:46:19 +08:00
1 parent 46ef76f890
commit fd5ab59f42
11 files changed
+433 -57

No files matched your search

+56 -1
View File
@@ -26,6 +26,7 @@ pub(crate) mod type1_fonts;
pub(crate) mod objects;
#[path = "pdf_pk.rs"]
mod pk_fonts;
mod cff_fonts;
mod truetype_fonts;
#[path = "pdf_opentype.rs"]
mod opentype_fonts;
@@ -1524,7 +1525,12 @@ fn native_pdf(
if page.units != crate::render::DisplayUnits::TeX && matches!(paint.program,crate::native_font::NativeFontProgram::Pk(_)) {
return Err("native PK PDF requires actual shipout device coordinates".into());
}
if paint.encoding.len()!=256 || paint.encoding[*slot as usize]==".notdef" || !paint.glyph_paths.contains_key(slot) || !font.has_slot(*slot) {
if paint.encoding.len() != 256
|| (matches!(paint.program, crate::native_font::NativeFontProgram::Type1(_))
&& paint.encoding[usize::from(*slot)] == ".notdef")
|| !paint.glyph_paths.contains_key(slot)
|| !font.has_slot(*slot)
{
return Err(format!("native font {} has no output encoding for slot {slot}",font.name));
}
if let Some((old_font,old_paint))=fonts.insert(font.id,(font,paint)) {
@@ -1752,6 +1758,39 @@ fn native_type1_parameters(
}
}
fn write_sfnt_descriptor(
objects: &mut Vec<Vec<u8>>,
metrics: &crate::tfm::TfmMetrics,
face: &rustybuzz::ttf_parser::Face<'_>,
paint: &crate::native_font::NativeFontPaint,
name: &str,
(file_key, font_file): (&str, usize),
) -> usize {
use rustybuzz::ttf_parser::Tag;
let in_pdf_units = |value: i16| 1000 * i32::from(value) / i32::from(face.units_per_em());
let ascent = in_pdf_units(face.tables().hhea.ascender);
let descent = in_pdf_units(face.tables().hhea.descender);
let pclt = face.raw_face().table(Tag::from_bytes(b"PCLT"));
let pclt_height = |offset| {
let bytes = pclt?.get(offset..offset + 2)?;
Some(in_pdf_units(i16::from_be_bytes(bytes.try_into().ok()?)))
};
let size = native_pdf_font_size(metrics.size_sp());
let cap = pclt_height(16)
.unwrap_or_else(|| native_font_units(metrics.height('H'), size, 1000) as i32);
let x_height =
pclt_height(10).unwrap_or_else(|| native_font_units(metrics.x_height(), size, 1000) as i32);
let stem = native_font_units(metrics.width('.') / 3, size, 1000);
let angle = face.italic_angle().floor() as i32;
let [left, bottom, right, top] = paint.bbox;
let flags = paint.descriptor_flags.unwrap_or(4);
objects.push(format!(
"<< /Type /FontDescriptor /FontName /{name} /Flags {flags} /FontBBox [{left} {bottom} {right} {top}] /Ascent {ascent} /Descent {descent} /CapHeight {cap} /ItalicAngle {angle} /StemV {stem} /XHeight {x_height} /{file_key} {font_file} 0 R >>"
).into_bytes());
objects.len()
}
fn pdf_font_owner(
objects: Option<&crate::pdf_device::page_resources::PdfObjects>,
id: crate::execution::FontId,
@@ -1770,6 +1809,7 @@ fn write_native_font(
reference: Option<&type1_fonts::Reference>,
target: Option<usize>,
subset_tags: &mut BTreeSet<String>,
cff_encoding: Option<usize>,
) -> Result<usize, String> {
use crate::native_font::NativeFontProgram;
if let NativeFontProgram::Pk(pk) = &paint.program {
@@ -1818,6 +1858,13 @@ fn write_native_font(
)?;
("TrueType", descriptor, name, format!("{width_object} 0 R"), String::new(), program.reencoded)
}
NativeFontProgram::OpenTypeCff(program) => {
let width_object = objects.len() + 1;
objects.push(format!("[{widths}]").into_bytes());
let (descriptor, name) = cff_fonts::write(objects, m, paint, program, compression)?;
let encoding = cff_encoding.map_or_else(String::new, |id| format!(" /Encoding {id} 0 R"));
("Type1", descriptor, name, format!("{width_object} 0 R"), encoding, cff_encoding.is_some())
}
NativeFontProgram::Pk(_) => unreachable!(),
};
let unicode = objects.len() + 1;
@@ -2393,9 +2440,15 @@ fn write_pdf(
.filter_map(|font| font.name.split_once('+').map(|(tag, _)| tag.to_owned()))
.collect();
let mut native_font_ids = BTreeMap::new();
let mut cff_encodings = cff_fonts::Encodings::default();
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);
let encoding = if matches!(paint.program, crate::native_font::NativeFontProgram::OpenTypeCff(_)) {
cff_encodings.mark(&mut objects, paint, used.keys().copied())
} else {
None
};
native_font_ids.insert(
id,
write_native_font(
@@ -2409,9 +2462,11 @@ fn write_pdf(
paint.type1_program_key().and_then(|key| type1_references.get(&key)),
target,
&mut subset_tags,
encoding,
)?,
);
}
cff_encodings.write(&mut objects);
for (font, owner) in aliases {
let id = native_font_ids[&owner];
native_font_ids.insert(font, id);