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

@@ -0,0 +1,85 @@
use super::{native_pdf_name, objects, write_sfnt_descriptor};
use crate::{
native_font::{NativeFontPaint, OpenTypeCffProgram},
resources::ResourceVersion,
tfm::TfmMetrics,
};
use rustybuzz::ttf_parser::Tag;
use std::{
collections::{BTreeMap, HashMap},
fmt::Write,
};
pub(super) fn write(
objects: &mut Vec<Vec<u8>>,
metrics: &TfmMetrics,
paint: &NativeFontPaint,
program: &OpenTypeCffProgram,
compression: u8,
) -> Result<(usize, String), String> {
let face = program.face()?;
let data = face
.raw_face()
.table(Tag::from_bytes(b"CFF "))
.ok_or("missing CFF data in mapped OpenType font")?;
let name = native_pdf_name(&program.name);
let font_file = objects.len() + 1;
objects.push(objects::stream("/Subtype /Type1C", data, compression));
let descriptor = write_sfnt_descriptor(
objects,
metrics,
&face,
paint,
&name,
("FontFile3", font_file),
);
Ok((descriptor, name))
}
struct Encoding<'a> {
object: usize,
names: BTreeMap<u8, &'a str>,
}
/// A single encoding read is shared by all mapped CFF fonts that use it.
#[derive(Default)]
pub(super) struct Encodings<'a>(HashMap<&'a ResourceVersion, Encoding<'a>>);
impl<'a> Encodings<'a> {
pub(super) fn mark(
&mut self,
objects: &mut Vec<Vec<u8>>,
paint: &'a NativeFontPaint,
slots: impl Iterator<Item = u8>,
) -> Option<usize> {
let version = paint.encoding_version()?;
let encoding = self.0.entry(version).or_insert_with(|| {
objects.push(Vec::new());
Encoding {
object: objects.len(),
names: BTreeMap::new(),
}
});
for slot in slots {
let name = paint.encoding[usize::from(slot)].as_str();
encoding.names.insert(slot, name);
}
Some(encoding.object)
}
pub(super) fn write(self, objects: &mut [Vec<u8>]) {
for encoding in self.0.into_values() {
let mut value = "<< /Type /Encoding /Differences [".to_owned();
let mut next = None;
for (slot, name) in encoding.names {
if next != Some(slot) {
write!(value, " {slot}").unwrap();
}
write!(value, "/{}", native_pdf_name(name)).unwrap();
next = slot.checked_add(1);
}
value.push_str("] >>");
objects[encoding.object - 1] = value.into_bytes();
}
}
}