Map Type1 glyphs through their requested Unicode

This commit is contained in:
gloamxun committed 2026-10-25 01:14:54 +08:00
1 parent a969b5acdf
commit aaa1ed8213
5 files changed
+42 -7

No files matched your search

+36 -1
View File
@@ -311,6 +311,25 @@ fn tounicode_cmap(face: &'static PdfFace, characters: &BTreeSet<char>) -> Vec<u8
map.into_bytes()
}
/// ToUnicode CMap for a single-byte Type1 font. Glyph names are ambiguous
/// (`Omega` in the Adobe Glyph List is the ohm sign), so the requested
/// characters are mapped explicitly.
fn type1_tounicode(by_code: &BTreeMap<u8, char>) -> Vec<u8> {
let mut map = String::from(
"/CIDInit /ProcSet findresource begin\n12 dict begin\nbegincmap\n /CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n /CMapName /Adobe-Identity-UCS def\n/CMapType 2 def\n 1 begincodespacerange\n<00> <FF>\nendcodespacerange\n",
);
let entries: Vec<(&u8, &char)> = by_code
.iter()
.filter(|(_, character)| (**character as u32) <= 0xffff)
.collect();
map.push_str(&format!("{} beginbfchar\n", entries.len()));
for (code, character) in entries {
map.push_str(&format!("<{code:02X}> <{:04X}>\n", *character as u32));
}
map.push_str("endbfchar\nendcmap\nCMapName currentdict /CMap defineresource pop\nend\nend\n");
map.into_bytes()
}
fn cid_widths(
face: &'static PdfFace,
slot: usize,
@@ -743,6 +762,7 @@ fn write_pdf(
let font_id = objects.len() + 1;
let descriptor_id = font_id + 1;
let file_id = font_id + 2;
let tounicode_id = font_id + 3;
font_ids[slot] = font_id;
let metrics = Cmr10Metrics::for_pdf_slot(slot);
let mut by_code = BTreeMap::new();
@@ -789,7 +809,7 @@ fn write_pdf(
}
objects.push(
format!(
"<< /Type /Font /Subtype /Type1 /BaseFont /{} /FirstChar {first} /LastChar {last} /Widths [{}] /Encoding << /Type /Encoding /Differences [{}] >> /FontDescriptor {descriptor_id} 0 R >>",
"<< /Type /Font /Subtype /Type1 /BaseFont /{} /FirstChar {first} /LastChar {last} /Widths [{}] /Encoding << /Type /Encoding /Differences [{}] >> /FontDescriptor {descriptor_id} 0 R /ToUnicode {tounicode_id} 0 R >>",
font.name,
widths.join(" "),
differences.join(" ")
@@ -814,6 +834,11 @@ fn write_pdf(
stream.extend_from_slice(&font.program);
stream.extend_from_slice(b"\nendstream");
objects.push(stream);
let cmap = type1_tounicode(&by_code);
let mut cmap_object = format!("<< /Length {} >>\nstream\n", cmap.len()).into_bytes();
cmap_object.extend_from_slice(&cmap);
cmap_object.extend_from_slice(b"\nendstream");
objects.push(cmap_object);
if !fallback_chars[slot].is_empty() {
fallback_font_ids[slot] =
write_type0_font(&mut objects, face, slot, &fallback_chars[slot]);
@@ -1036,6 +1061,16 @@ mod type1_pdf_tests {
display_lists_to_pdf_with_cm_type1(list, 200 * 65_536, 800 * 65_536, PageOrigin::default())
}
#[test]
fn type1_fonts_map_omega_to_the_requested_code() {
let font = type1_fonts()[0].as_ref().expect("cmr10 type1 program");
let code = font.code_for('\u{3a9}').expect("omega in cmr10");
let mut by_code = std::collections::BTreeMap::new();
by_code.insert(code, '\u{3a9}');
let cmap = String::from_utf8_lossy(&type1_tounicode(&by_code)).to_string();
assert!(cmap.contains(&format!("<{code:02X}> <03A9>")), "{cmap}");
}
#[test]
fn type1_backend_embeds_computer_modern_outlines() {
let pdf = write(&[hello_list()]);
+1 -1
View File
@@ -578,7 +578,7 @@ fn greek_text_round_trips_through_pdf_fallback() {
return;
}
let body = "\u{3b1}\u{3b2}\u{3b3} \u{3bc}\u{3ad}\u{3b3}\u{3b1}";
let body = "\u{3b1}\u{3b2}\u{3b3} \u{3bc}\u{3ad}\u{3b3}\u{3b1} \u{3a9}";
let document = Document::new(65, vec![Unit::paragraph_with_style(1, body, body_style())]);
let engine = Engine::layout(&document);
let lists: Vec<_> = engine