Render legacy XeLaTeX composite TrueType fonts
This commit is contained in:
1 parent
7d460e0fa1
commit
bbeeca0317
11 files changed
+687
-55
No files matched your search
@@ -62,3 +62,111 @@ pub(super) fn write(
|
||||
);
|
||||
Ok((descriptor, name))
|
||||
}
|
||||
|
||||
pub(super) fn write_cid(
|
||||
objects: &mut Vec<Vec<u8>>,
|
||||
program: &TrueTypeProgram,
|
||||
slots: impl Iterator<Item = u8>,
|
||||
compression: u8,
|
||||
subset_tags: &mut BTreeSet<String>,
|
||||
target: Option<usize>,
|
||||
) -> Result<usize, String> {
|
||||
use crate::native_font::cid_width;
|
||||
use std::{collections::BTreeMap, fmt::Write as _};
|
||||
use subsetter::GlyphRemapper;
|
||||
|
||||
let mapping = program.composite.as_ref().expect("composite TrueType program");
|
||||
let face = program.face()?;
|
||||
let mut glyphs = BTreeMap::new();
|
||||
let mut unicode = BTreeMap::new();
|
||||
for slot in slots {
|
||||
let cid = mapping.cid(slot);
|
||||
let glyph = program.glyph_id(slot);
|
||||
if glyphs.insert(cid, glyph).is_some_and(|old| old != glyph) {
|
||||
return Err(format!("conflicting glyph for CID {cid} in {}", program.name));
|
||||
}
|
||||
if cid != 0
|
||||
&& let Some(text) = mapping.unicode(slot)
|
||||
{
|
||||
unicode.entry(cid).or_insert_with(|| {
|
||||
text.encode_utf16().map(|unit| format!("{unit:04X}")).collect()
|
||||
});
|
||||
}
|
||||
}
|
||||
let names = glyphs.keys().map(|cid| format!("/c{cid:04X}")).collect();
|
||||
let tag = (0..100).map(|salt| subset_tag(&program.name, &names, salt))
|
||||
.find(|tag| subset_tags.insert(tag.clone()))
|
||||
.ok_or("CID subset name collision limit exceeded")?;
|
||||
let name = format!("{tag}+{}", native_pdf_name(&program.name));
|
||||
|
||||
let mut remapper = GlyphRemapper::new();
|
||||
let max = usize::from(glyphs.keys().next_back().copied().unwrap_or(0));
|
||||
let mut cid_to_gid = vec![0; (max + 1) * 2];
|
||||
let mut cid_set = vec![0; max / 8 + 1];
|
||||
let width = |glyph| cid_width(&face, glyph);
|
||||
let default = width(0);
|
||||
let mut widths = String::new();
|
||||
let mut previous: Option<u16> = None;
|
||||
let mut identity = true;
|
||||
for (&cid, &glyph) in &glyphs {
|
||||
let subset_id = remapper.remap(glyph);
|
||||
let index = usize::from(cid);
|
||||
cid_to_gid[index * 2..index * 2 + 2].copy_from_slice(&subset_id.to_be_bytes());
|
||||
if cid != 0 {
|
||||
cid_set[index / 8] |= 1 << (7 - index % 8);
|
||||
}
|
||||
identity &= cid == subset_id;
|
||||
let advance = width(glyph);
|
||||
if advance == default {
|
||||
continue;
|
||||
}
|
||||
if previous.and_then(|id| id.checked_add(1)) != Some(cid) {
|
||||
if previous.is_some() {
|
||||
widths.push_str("] ");
|
||||
}
|
||||
write!(widths, "{cid} [").unwrap();
|
||||
}
|
||||
write!(widths, "{advance} ").unwrap();
|
||||
previous = Some(cid);
|
||||
}
|
||||
let widths = if widths.is_empty() { widths } else { format!(" /W [{widths}]]") };
|
||||
|
||||
let subset = program.embed_cid_subset(&remapper)?;
|
||||
let id = target.unwrap_or_else(|| {
|
||||
objects.push(Vec::new());
|
||||
objects.len()
|
||||
});
|
||||
let fontfile = objects.len() + 1;
|
||||
objects.push(objects::stream(&format!("/Length1 {}", subset.len()), &subset, compression));
|
||||
let cid_set_id = objects.len() + 1;
|
||||
objects.push(objects::stream("", &cid_set, compression));
|
||||
let descriptor = objects.len() + 1;
|
||||
let mut contents = super::opentype_fonts::font_descriptor(&face, &name, "FontFile2", fontfile);
|
||||
assert!(contents.ends_with(b" >>"));
|
||||
contents.truncate(contents.len() - 3);
|
||||
contents.extend_from_slice(format!(" /CIDSet {cid_set_id} 0 R >>").as_bytes());
|
||||
objects.push(contents);
|
||||
let cid_mapping = if identity {
|
||||
"/Identity".to_owned()
|
||||
} else {
|
||||
objects.push(objects::stream("", &cid_to_gid, compression));
|
||||
format!("{} 0 R", objects.len())
|
||||
};
|
||||
let descendant = objects.len() + 1;
|
||||
let registry = super::escape(&mapping.registry);
|
||||
let ordering = super::escape(&mapping.ordering);
|
||||
let supplement = mapping.supplement;
|
||||
objects.push(format!(
|
||||
"<< /Type /Font /Subtype /CIDFontType2 /BaseFont /{name} /CIDSystemInfo << /Registry ({registry}) /Ordering ({ordering}) /Supplement {supplement} >> /FontDescriptor {descriptor} 0 R /DW {default}{widths} /CIDToGIDMap {cid_mapping} >>"
|
||||
).into_bytes());
|
||||
let unicode_ref = if unicode.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
objects.push(objects::stream("", &super::to_unicode_map(&unicode, 4), compression));
|
||||
format!(" /ToUnicode {} 0 R", objects.len())
|
||||
};
|
||||
objects[id - 1] = format!(
|
||||
"<< /Type /Font /Subtype /Type0 /BaseFont /{name} /Encoding /Identity-H /DescendantFonts [{descendant} 0 R]{unicode_ref} >>"
|
||||
).into_bytes();
|
||||
Ok(id)
|
||||
}
|
||||
Reference in new issue
Block a user