//! Simple-font encodings: the named base encodings (WinAnsi, MacRoman, //! Standard), the Symbol and ZapfDingbats built-ins, and the //! /Encoding + /Differences resolution that fills a font's per-byte //! unicode table. use super::super::own_pdf::{dget, Dict, Pdf, Val}; use super::glyphlist::glyph_to_unicode_multi; use super::{FontInfo, SymbolFont}; /// MacExpertEncoding (PDF 32000-1:2008, Annex D.4), normalized for /// text recovery: oldstyle/superior/inferior figures map to plain /// digits, small caps to their capitals, fractions and ornaments to /// their Unicode equivalents. The f-ligatures at 0x56..0x5A expand to /// ASCII through the multi-char map. const MAC_EXPERT: [u32; 256] = [ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0020, 0x0021, 0x02DD, 0x00A2, 0x0024, 0x0024, 0x0026, 0x00B4, 0x0028, 0x0029, 0x2025, 0x2024, 0x002C, 0x002D, 0x002E, 0x2044, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003A, 0x003B, 0x0000, 0x2014, 0x0000, 0x003F, 0x0000, 0x0000, 0x0000, 0x0000, 0x00D0, 0x0000, 0x0000, 0x00BC, 0x00BD, 0x00BE, 0x215B, 0x215C, 0x215D, 0x215E, 0x2153, 0x2154, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0028, 0x0000, 0x0029, 0x02C6, 0x002D, 0x0060, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005A, 0x20A1, 0x0031, 0x20A8, 0x02DC, 0x0000, 0x0000, 0x0061, 0x00A2, 0x0000, 0x0000, 0x0000, 0x0000, 0x00C1, 0x00C0, 0x00C2, 0x00C4, 0x00C3, 0x00C5, 0x00C7, 0x00C9, 0x00C8, 0x00CA, 0x00CB, 0x00CD, 0x00CC, 0x00CE, 0x00CF, 0x00D1, 0x00D3, 0x00D2, 0x00D4, 0x00D6, 0x00D5, 0x00DA, 0x00D9, 0x00DB, 0x00DC, 0x0000, 0x0038, 0x0034, 0x0033, 0x0036, 0x0038, 0x0037, 0x0160, 0x0000, 0x00A2, 0x0032, 0x0000, 0x00A8, 0x0000, 0x02C7, 0x006F, 0x0035, 0x0000, 0x002C, 0x002E, 0x00DD, 0x0000, 0x0024, 0x0000, 0x0000, 0x00DE, 0x0000, 0x0039, 0x0030, 0x017D, 0x00C6, 0x00D8, 0x00BF, 0x0031, 0x0141, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00B8, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0152, 0x2012, 0x002D, 0x0000, 0x0000, 0x0000, 0x0000, 0x00A1, 0x0000, 0x0178, 0x0000, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0039, 0x0030, 0x0000, 0x0065, 0x0072, 0x0074, 0x0000, 0x0000, 0x0069, 0x0073, 0x0064, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x006C, 0x02DB, 0x02D8, 0x00AF, 0x0062, 0x006E, 0x006D, 0x002C, 0x002E, 0x02D9, 0x02DA, 0x0000, 0x0000, 0x0000, 0x0000, ]; fn mac_expert_encoding(info: &mut FontInfo) { for (code, &u) in MAC_EXPERT.iter().enumerate() { info.to_unicode_simple[code] = char::from_u32(u).filter(|c| *c != '\0'); } const F_LIGATURES: [(u32, &str); 5] = [ (0x56, "ff"), (0x57, "fi"), (0x58, "fl"), (0x59, "ffi"), (0x5A, "ffl"), ]; for (code, expansion) in F_LIGATURES { info.to_unicode.insert(code, expansion.to_string()); } } /// MacRomanEncoding, code points 128–255 (0 = unmapped). const MAC_ROMAN_HIGH: [u32; 128] = [ 0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, 0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8, 0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3, 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC, 0x2020, 0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF, 0x00AE, 0x00A9, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x00C6, 0x00D8, 0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5, 0x2202, 0x2211, 0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x00E6, 0x00F8, 0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB, 0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3, 0x00D5, 0x0152, 0x0153, 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA, 0x00FF, 0x0178, 0x2044, 0x20AC, 0x2039, 0x203A, 0xFB01, 0xFB02, 0x2021, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1, 0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC, 0x00D3, 0x00D4, 0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC, 0x00AF, 0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD, 0x02DB, 0x02C7, ]; /// StandardEncoding (Adobe), code points 128–255 (0 = unmapped). const STANDARD_HIGH: [u32; 128] = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00A1, 0x00A2, 0x00A3, 0x2044, 0x00A5, 0x0192, 0x00A7, 0x00A4, 0x2018, 0x201C, 0x00AB, 0x2039, 0x203A, 0xFB01, 0xFB02, 0, 0x2013, 0x2020, 0x2021, 0x00B7, 0, 0x00B6, 0x2022, 0x201A, 0x201E, 0x201D, 0x00BB, 0x2026, 0x2030, 0, 0x00BF, 0, 0x0060, 0x00B4, 0x02C6, 0x02DC, 0x00AF, 0x02D8, 0x02D9, 0x00A8, 0, 0x02DA, 0x00B8, 0, 0x02DD, 0x02DB, 0x02C7, 0x2014, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00C6, 0, 0x00AA, 0, 0, 0, 0, 0x0141, 0x00D8, 0x0152, 0x00BA, 0, 0, 0, 0, 0, 0x00E6, 0, 0, 0, 0x0131, 0, 0, 0x0142, 0x00F8, 0x0153, 0x00DF, 0, 0, 0, 0, ]; /// WinAnsiEncoding, code points 128–255 (0 = unmapped). const WIN_ANSI_HIGH: [u32; 128] = [ 0x20AC, 0, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0, 0x017D, 0, 0, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0, 0x017E, 0x0178, 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF, 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7, 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF, 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7, 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF, 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF, 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7, 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF, ]; /// Adobe Glyph List, the practical subset: Latin with full diacritics, /// ligatures, punctuation, Greek, and common math/symbol names — the /// names Differences arrays actually use. Suffixed variants /// ("a.sc", "one.oldstyle") fall back to their base name. fn symbol_encoding(map: &mut [Option; 256]) { *map = [None; 256]; const PAIRS: &[(u8, u32)] = &[ (0x20, 0x0020), (0x21, 0x0021), (0x22, 0x2200), (0x23, 0x0023), (0x24, 0x2203), (0x25, 0x0025), (0x26, 0x0026), (0x27, 0x220B), (0x28, 0x0028), (0x29, 0x0029), (0x2A, 0x2217), (0x2B, 0x002B), (0x2C, 0x002C), (0x2D, 0x2212), (0x2E, 0x002E), (0x2F, 0x002F), (0x3A, 0x003A), (0x3B, 0x003B), (0x3C, 0x003C), (0x3D, 0x003D), (0x3E, 0x003E), (0x3F, 0x003F), (0x40, 0x2245), (0x41, 0x0391), (0x42, 0x0392), (0x43, 0x03A7), (0x44, 0x0394), (0x45, 0x0395), (0x46, 0x03A6), (0x47, 0x0393), (0x48, 0x0397), (0x49, 0x0399), (0x4A, 0x03D1), (0x4B, 0x039A), (0x4C, 0x039B), (0x4D, 0x039C), (0x4E, 0x039D), (0x4F, 0x039F), (0x50, 0x03A0), (0x51, 0x0398), (0x52, 0x03A1), (0x53, 0x03A3), (0x54, 0x03A4), (0x55, 0x03A5), (0x56, 0x03C2), (0x57, 0x03A9), (0x58, 0x039E), (0x59, 0x03A8), (0x5A, 0x0396), (0x5B, 0x005B), (0x5C, 0x2234), (0x5D, 0x005D), (0x5E, 0x22A5), (0x5F, 0x005F), (0x60, 0xF8E5), (0x61, 0x03B1), (0x62, 0x03B2), (0x63, 0x03C7), (0x64, 0x03B4), (0x65, 0x03B5), (0x66, 0x03C6), (0x67, 0x03B3), (0x68, 0x03B7), (0x69, 0x03B9), (0x6A, 0x03D5), (0x6B, 0x03BA), (0x6C, 0x03BB), (0x6D, 0x03BC), (0x6E, 0x03BD), (0x6F, 0x03BF), (0x70, 0x03C0), (0x71, 0x03B8), (0x72, 0x03C1), (0x73, 0x03C3), (0x74, 0x03C4), (0x75, 0x03C5), (0x76, 0x03D6), (0x77, 0x03C9), (0x78, 0x03BE), (0x79, 0x03C8), (0x7A, 0x03B6), (0x7B, 0x007B), (0x7C, 0x007C), (0x7D, 0x007D), (0x7E, 0x223C), (0xA0, 0x20AC), (0xA1, 0x03D2), (0xA2, 0x2032), (0xA3, 0x2264), (0xA4, 0x2044), (0xA5, 0x221E), (0xA6, 0x0192), (0xA7, 0x2663), (0xA8, 0x2666), (0xA9, 0x2665), (0xAA, 0x2660), (0xAB, 0x2194), (0xAC, 0x2190), (0xAD, 0x2191), (0xAE, 0x2192), (0xAF, 0x2193), (0xB0, 0x00B0), (0xB1, 0x00B1), (0xB2, 0x2033), (0xB3, 0x2265), (0xB4, 0x00D7), (0xB5, 0x221D), (0xB6, 0x2202), (0xB7, 0x2022), (0xB8, 0x00F7), (0xB9, 0x2260), (0xBA, 0x2261), (0xBB, 0x2248), (0xBC, 0x2026), (0xBF, 0x21B5), (0xC0, 0x2135), (0xC1, 0x2111), (0xC2, 0x211C), (0xC3, 0x2118), (0xC4, 0x2297), (0xC5, 0x2295), (0xC6, 0x2205), (0xC7, 0x2229), (0xC8, 0x222A), (0xC9, 0x2283), (0xCA, 0x2287), (0xCB, 0x2284), (0xCC, 0x2282), (0xCD, 0x2286), (0xCE, 0x2208), (0xCF, 0x2209), (0xD0, 0x2220), (0xD1, 0x2207), (0xD5, 0x220F), (0xD6, 0x221A), (0xD7, 0x22C5), (0xD8, 0x00AC), (0xD9, 0x2227), (0xDA, 0x2228), (0xDB, 0x21D4), (0xDC, 0x21D0), (0xDD, 0x21D1), (0xDE, 0x21D2), (0xDF, 0x21D3), (0xE5, 0x2211), (0xF2, 0x222B), (0x30, 0x0030), (0x31, 0x0031), (0x32, 0x0032), (0x33, 0x0033), (0x34, 0x0034), (0x35, 0x0035), (0x36, 0x0036), (0x37, 0x0037), (0x38, 0x0038), (0x39, 0x0039), ]; for &(code, u) in PAIRS { map[code as usize] = char::from_u32(u); } } /// ZapfDingbats built-in encoding (the used range). fn zapf_encoding(map: &mut [Option; 256]) { *map = [None; 256]; map[0x20] = Some(' '); // 0x21..=0x7E maps to U+2701..U+275E in order, with a handful of gaps // that don't matter for text recovery. for code in 0x21u16..=0x7E { map[code as usize] = char::from_u32(0x2701 + (code as u32 - 0x21)); } // 0x80..: ornaments; 0xA1..=0xEF maps to U+2761.. block. for code in 0xA1u16..=0xEF { map[code as usize] = char::from_u32(0x2761 + (code as u32 - 0xA1)); } } pub(crate) fn build_simple_encoding(pdf: &Pdf, dict: &Dict, info: &mut FontInfo) { // Base table: the named encoding (direct or /BaseEncoding inside an // encoding dict); WinAnsi as the practical default. let enc_val = pdf.dict_get(dict, b"Encoding").ok().flatten(); let mut base_name: Option<&[u8]> = match &enc_val { Some(Val::Name(n)) => Some(n), Some(Val::Dict(d)) => match dget(d, b"BaseEncoding") { Some(Val::Name(n)) => Some(n), _ => None, }, _ => None, }; // Symbol and ZapfDingbats use built-in encodings, not the Latin set. if base_name.is_none() { if info.symbol_font == SymbolFont::Symbol { base_name = Some(b"Symbol"); } else if info.symbol_font == SymbolFont::ZapfDingbats { base_name = Some(b"ZapfDingbats"); } } if base_name == Some(b"MacExpertEncoding") { // MacExpert is not Latin-compatible: expert-set glyphs // (oldstyle figures, small caps, superiors/inferiors, // fractions, f-ligatures) live at Latin code points. Decode // through the real table, normalized for text recovery // (oldstyle 1 -> '1', Asmall -> 'A'). mac_expert_encoding(info); return; } let high: &[u32; 128] = match base_name { Some(b"MacRomanEncoding") => &MAC_ROMAN_HIGH, Some(b"StandardEncoding") => &STANDARD_HIGH, _ => &WIN_ANSI_HIGH, }; match base_name { Some(b"Symbol") => symbol_encoding(&mut info.to_unicode_simple), Some(b"ZapfDingbats") => zapf_encoding(&mut info.to_unicode_simple), _ => { for b in 0u16..256 { let c = if b < 128 { char::from_u32(b as u32) } else { char::from_u32(high[b as usize - 128]) }; info.to_unicode_simple[b as usize] = c.filter(|c| *c != '\0'); } } } // Type3 /Differences names key the font's CharProcs and are chosen // freely (dvips/Acrobat Capture emit opaque names like /B1, /AC). // An unrecognized name there carries no unicode; falling back to the // base-encoding character for the code recovers the common case // where codes are ASCII-compatible. Non-Type3 fonts keep the strict // behavior: an unknown name unmaps the code. let is_type3 = matches!( pdf.dict_get(dict, b"Subtype").ok().flatten(), Some(Val::Name(b"Type3")) ); if let Ok(Some(Val::Dict(enc))) = pdf.dict_get(dict, b"Encoding") { if let Ok(Some(Val::Array(diffs))) = pdf.dict_get(&enc, b"Differences") { let mut code = 0usize; for o in &diffs { match pdf.resolve(o) { Ok(Val::Num(v)) => code = v as usize, Ok(Val::Name(n)) => { if code < 256 { match glyph_to_unicode_multi(n) { // Ligature compound: the multi-char map // wins over the per-byte table at decode. Some(s) if s.chars().count() > 1 => { info.to_unicode.insert(code as u32, s); info.to_unicode_simple[code] = None; } Some(s) => { info.to_unicode_simple[code] = s.chars().next(); } None => { info.to_unicode_simple[code] = if is_type3 { info.to_unicode_simple[code].filter(|c| !c.is_control()) } else { None }; } } } code += 1; } _ => {} } } } } // TeX text fonts (dvips Type3 output) place the f-ligatures at the // OT1/T1 slots 11–15, which are control characters in every Latin // base encoding. A control character there is never intentional: // recover the ligature so "first" doesn't extract as "\x0Crst". // Expanded to ASCII (not U+FB01 etc.) — the multi-char map wins over // the per-byte table during decode. if is_type3 { const TEX_F_LIGATURES: [(u32, &str); 5] = [ (0x0B, "ff"), (0x0C, "fi"), (0x0D, "fl"), (0x0E, "ffi"), (0x0F, "ffl"), ]; for (code, expansion) in TEX_F_LIGATURES { let simple = info.to_unicode_simple[code as usize]; if simple.is_none_or(|c| c.is_control() || c.is_whitespace()) && !info.to_unicode.contains_key(&code) { info.to_unicode.insert(code, expansion.to_string()); } } } } #[cfg(test)] mod tests { use super::*; #[test] fn standard_encoding_quoteleft_is_typographic() { assert_eq!(STANDARD_HIGH[0xA9 - 0x80], 0x2018); } #[test] fn symbol_special_math_codes_are_present() { let mut map = [None; 256]; symbol_encoding(&mut map); assert_eq!(map[0xC1], Some('ℑ')); assert_eq!(map[0xC2], Some('ℜ')); assert_eq!(map[0xC3], Some('℘')); assert_eq!(map[0xC4], Some('⊗')); } #[test] fn named_high_tables_pin_non_ascii_boundaries() { assert_eq!(MAC_ROMAN_HIGH[0], 0x00C4); assert_eq!(WIN_ANSI_HIGH[0], 0x20AC); } }