Embed hinted Type 1C subsets for XeTeX PDF output

This commit is contained in:
gloamxun committed 2026-11-28 11:07:18 +08:00
1 parent 4ae4275cf3
commit 01c6f1d4bd
9 files changed
+629 -26

No files matched your search

+42 -10
View File
@@ -4,10 +4,10 @@ use crate::{
native_font::Type1ProgramKey,
pdf_image::ImportedType1Use,
type1::Type1Font,
type1_outline::{subset_tag, write_subset},
type1_outline::{subset_tag, write_subset, write_type1c},
};
use std::{
collections::{BTreeSet, HashMap},
collections::{BTreeMap, BTreeSet, HashMap},
fmt::Write as _,
};
@@ -163,6 +163,45 @@ pub(super) fn write_full(
)
}
pub(super) fn write_xdv(
objects: &mut Vec<Vec<u8>>,
font: &Type1Font,
parameters: &str,
slots: &BTreeMap<u8, &str>,
compression: u8,
subset_tags: &mut BTreeSet<String>,
omit_charset: bool,
) -> Result<(usize, String), String> {
let marked = slots.values().map(|&name| name.to_owned()).collect();
let tag = (0..100)
.map(|salt| subset_tag(&font.name, &marked, salt))
.find(|tag| subset_tags.insert(tag.clone()))
.ok_or("Type1C subset name collision limit exceeded")?;
let name = format!("{tag}+{}", font.name);
let (program, glyphs) = write_type1c(font, &name, slots)?;
let file = objects.len() + 1;
objects.push(objects::stream("/Subtype /Type1C", &program, compression));
let name = native_pdf_name(&name);
let mut descriptor = format!("<< /Type /FontDescriptor /FontName /{name} {parameters}");
if !omit_charset {
append_charset(&mut descriptor, &glyphs);
}
write!(descriptor, " /FontFile3 {file} 0 R >>").unwrap();
objects.push(descriptor.into_bytes());
Ok((objects.len(), name))
}
fn append_charset(body: &mut String, glyphs: &BTreeSet<String>) {
body.push_str(" /CharSet <");
for glyph in glyphs {
body.push_str("2F");
for byte in glyph.bytes() {
write!(body, "{byte:02X}").unwrap();
}
}
body.push('>');
}
fn write_embedding(
objects: &mut Vec<Vec<u8>>,
name: &str,
@@ -190,14 +229,7 @@ fn write_embedding(
};
let mut body = format!("<< /Type /FontDescriptor /FontName {font_name} {parameters}");
if let Some(glyphs) = glyphs {
body.push_str(" /CharSet <");
for glyph in glyphs {
body.push_str("2F");
for byte in glyph.bytes() {
write!(body, "{byte:02X}").unwrap();
}
}
body.push('>');
append_charset(&mut body, glyphs);
}
write!(body, " /FontFile {file} 0 R >>").unwrap();
objects[descriptor - 1] = body.into_bytes();