pub mod cmap;
pub mod encoding;
pub mod glyph;
mod metrics;
pub mod system;
mod type1;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use crate::document::Document;
use crate::geom::Matrix;
use crate::object::{Dict, Object};
use cmap::CMap;
use encoding::{base_encoding_name, glyph_name_to_unicode, Encoding};
use glyph::{Outline, OutlineBuilder};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct FontFlags(pub u32);
impl FontFlags {
pub const FIXED_PITCH: u32 = 1 << 0;
pub const SERIF: u32 = 1 << 1;
pub const SYMBOLIC: u32 = 1 << 2;
pub const CURSIVE: u32 = 1 << 3;
pub const ITALIC: u32 = 1 << 4;
pub const ALL_CAPS: u32 = 1 << 5;
pub const SMALL_CAPS: u32 = 1 << 6;
pub const BOLD: u32 = 1 << 7;
pub const SANS_SERIF: u32 = 1 << 8;
pub fn has(self, flag: u32) -> bool {
self.0 & flag != 0
}
fn set(&mut self, flag: u32, on: bool) {
if on {
self.0 |= flag;
} else {
self.0 &= !flag;
}
}
pub fn is_bold(self) -> bool {
self.has(Self::BOLD)
}
pub fn is_italic(self) -> bool {
self.has(Self::ITALIC)
}
pub fn is_serif(self) -> bool {
self.has(Self::SERIF)
}
pub fn is_fixed_pitch(self) -> bool {
self.has(Self::FIXED_PITCH)
}
pub fn is_symbolic(self) -> bool {
self.has(Self::SYMBOLIC)
}
}
mod desc_flag {
pub const FIXED_PITCH: i64 = 1 << 0;
pub const SERIF: i64 = 1 << 1;
pub const SYMBOLIC: i64 = 1 << 2;
pub const SCRIPT: i64 = 1 << 3;
pub const NONSYMBOLIC: i64 = 1 << 5;
pub const ITALIC: i64 = 1 << 6;
pub const ALL_CAP: i64 = 1 << 16;
pub const SMALL_CAP: i64 = 1 << 17;
pub const FORCE_BOLD: i64 = 1 << 18;
}
fn derive_flags(doc: &Document, descriptor: Option<&Dict>, base_font: &str) -> FontFlags {
let num = |key: &str| {
descriptor
.and_then(|d| doc.dict_get(d, key))
.and_then(|o| o.as_f32())
};
let raw = descriptor
.and_then(|d| doc.dict_get(d, "Flags"))
.and_then(|o| o.as_int())
.unwrap_or(0);
let name = base_font
.split('+')
.next_back()
.unwrap_or(base_font)
.to_ascii_lowercase();
let mut flags = FontFlags::default();
let symbolic = raw & desc_flag::SYMBOLIC != 0 && raw & desc_flag::NONSYMBOLIC == 0;
flags.set(FontFlags::SYMBOLIC, symbolic);
let serif = raw & desc_flag::SERIF != 0
|| [
"times", "roman", "georgia", "garamond", "minion", "cambria", "book",
]
.iter()
.any(|f| name.contains(f));
flags.set(FontFlags::SERIF, serif);
flags.set(FontFlags::SANS_SERIF, !serif && !symbolic);
flags.set(
FontFlags::FIXED_PITCH,
raw & desc_flag::FIXED_PITCH != 0 || name.contains("courier") || name.contains("mono"),
);
let italic = raw & desc_flag::ITALIC != 0
|| num("ItalicAngle").is_some_and(|a| a.abs() > 0.5)
|| name.contains("italic")
|| name.contains("oblique");
flags.set(FontFlags::ITALIC, italic);
let bold = raw & desc_flag::FORCE_BOLD != 0
|| num("FontWeight").is_some_and(|w| w >= 600.0)
|| num("StemV").is_some_and(|s| s >= 120.0)
|| name.contains("bold")
|| name.contains("black")
|| name.contains("heavy");
flags.set(FontFlags::BOLD, bold);
flags.set(
FontFlags::CURSIVE,
raw & desc_flag::SCRIPT != 0 || name.contains("script"),
);
flags.set(FontFlags::ALL_CAPS, raw & desc_flag::ALL_CAP != 0);
flags.set(FontFlags::SMALL_CAPS, raw & desc_flag::SMALL_CAP != 0);
flags
}
#[derive(Debug, Clone)]
pub struct CodeItem {
pub code: u32,
pub cid: u32,
pub unicode: Option<String>,
pub width: f32,
pub is_word_space: bool,
}
enum Program {
Sfnt(Arc<[u8]>),
Cff(Arc<[u8]>),
Type1(Arc<[u8]>),
None,
}
impl Program {
fn builtin_encoding(&self) -> Option<type1::Builtin> {
match self {
Program::Type1(data) => type1::builtin_encoding(data),
_ => None,
}
}
}
enum Widths {
Simple {
first: u32,
widths: Vec<f32>,
missing: Option<f32>,
},
Cid {
default: f32,
map: HashMap<u32, f32>,
},
}
struct Composite {
cmap: CMap,
cid_to_gid: Option<Arc<[u8]>>,
}
pub struct Font {
pub base_font: String,
pub flags: FontFlags,
pub is_type3: bool,
pub font_matrix: Matrix,
names: Vec<String>,
symbolic: bool,
composite: Option<Composite>,
to_unicode: Option<CMap>,
widths: Widths,
program: Program,
char_procs: Dict,
outline_cache: Mutex<HashMap<u32, Option<Arc<Outline>>>>,
substitute: std::sync::OnceLock<Option<Arc<[u8]>>>,
cff_cid_to_gid: std::sync::OnceLock<HashMap<u16, u16>>,
}
impl std::fmt::Debug for Font {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Font")
.field("base_font", &self.base_font)
.field("flags", &self.flags)
.field("is_type3", &self.is_type3)
.finish_non_exhaustive()
}
}
impl Font {
pub fn load(doc: &Document, dict: &Dict) -> Font {
let subtype = dict.get("Subtype").and_then(|o| o.as_name()).unwrap_or("");
let base_font = doc
.dict_get(dict, "BaseFont")
.as_ref()
.and_then(|o| o.as_name())
.unwrap_or("")
.to_string();
match subtype {
"Type0" => Self::load_type0(doc, dict, base_font),
"Type3" => Self::load_type3(doc, dict, base_font),
_ => Self::load_simple(doc, dict, base_font),
}
}
fn load_simple(doc: &Document, dict: &Dict, base_font: String) -> Font {
let descriptor = doc
.dict_get(dict, "FontDescriptor")
.and_then(|o| o.as_dict().cloned());
let flags = derive_flags(doc, descriptor.as_ref(), &base_font);
let symbolic = flags.is_symbolic();
let program = load_program(doc, descriptor.as_ref());
let names = build_encoding(doc, dict, &base_font, symbolic, &program);
let widths = simple_widths(doc, dict, descriptor.as_ref());
Font {
base_font,
flags,
is_type3: false,
font_matrix: Matrix::scale(0.001, 0.001),
names,
symbolic,
composite: None,
to_unicode: load_to_unicode(doc, dict),
widths,
program,
char_procs: Dict::new(),
outline_cache: Mutex::new(HashMap::new()),
substitute: std::sync::OnceLock::new(),
cff_cid_to_gid: std::sync::OnceLock::new(),
}
}
fn load_type0(doc: &Document, dict: &Dict, base_font: String) -> Font {
let cmap = match doc.dict_get(dict, "Encoding") {
Some(Object::Name(n)) => {
if n.starts_with("Identity") {
CMap::identity()
} else {
CMap::identity()
}
}
Some(obj) => obj
.as_stream()
.and_then(|s| doc.file.decode_stream(s).ok())
.map(|d| CMap::parse(&d.data))
.unwrap_or_else(CMap::identity),
None => CMap::identity(),
};
let descendant = doc
.dict_get(dict, "DescendantFonts")
.and_then(|o| match o {
Object::Array(items) => items.first().map(|i| doc.resolve(i)),
other => Some(other),
})
.and_then(|o| o.as_dict().cloned())
.unwrap_or_default();
let descriptor = doc
.dict_get(&descendant, "FontDescriptor")
.and_then(|o| o.as_dict().cloned());
let cid_to_gid = match doc.dict_get(&descendant, "CIDToGIDMap") {
Some(Object::Stream(s)) => doc
.file
.decode_stream(&s)
.ok()
.map(|d| Arc::from(d.data.as_slice())),
_ => None,
};
Font {
flags: derive_flags(doc, descriptor.as_ref(), &base_font),
base_font,
is_type3: false,
font_matrix: Matrix::scale(0.001, 0.001),
names: Vec::new(),
symbolic: true,
composite: Some(Composite { cmap, cid_to_gid }),
to_unicode: load_to_unicode(doc, dict),
widths: cid_widths(doc, &descendant),
program: load_program(doc, descriptor.as_ref()),
char_procs: Dict::new(),
outline_cache: Mutex::new(HashMap::new()),
substitute: std::sync::OnceLock::new(),
cff_cid_to_gid: std::sync::OnceLock::new(),
}
}
fn load_type3(doc: &Document, dict: &Dict, base_font: String) -> Font {
let font_matrix = doc
.dict_get(dict, "FontMatrix")
.as_ref()
.and_then(|o| o.as_array())
.map(|a| {
let v: Vec<f32> = a.iter().filter_map(|o| o.as_f32()).collect();
if v.len() >= 6 {
Matrix::new(v[0], v[1], v[2], v[3], v[4], v[5])
} else {
Matrix::scale(0.001, 0.001)
}
})
.unwrap_or_else(|| Matrix::scale(0.001, 0.001));
Font {
flags: FontFlags(FontFlags::SYMBOLIC),
base_font,
is_type3: true,
font_matrix,
names: build_encoding(doc, dict, "", true, &Program::None),
symbolic: true,
composite: None,
to_unicode: load_to_unicode(doc, dict),
widths: simple_widths(doc, dict, None),
program: Program::None,
char_procs: doc
.dict_get(dict, "CharProcs")
.and_then(|o| o.as_dict().cloned())
.unwrap_or_default(),
outline_cache: Mutex::new(HashMap::new()),
substitute: std::sync::OnceLock::new(),
cff_cid_to_gid: std::sync::OnceLock::new(),
}
}
pub fn decode(&self, bytes: &[u8]) -> Vec<CodeItem> {
match &self.composite {
Some(comp) => comp
.cmap
.codespace
.decode(bytes)
.into_iter()
.map(|code| {
let cid = comp.cmap.cid(code);
CodeItem {
code,
cid,
unicode: self.unicode_for(code),
width: self.width_for(code, cid),
is_word_space: false,
}
})
.collect(),
None => bytes
.iter()
.map(|&b| {
let code = u32::from(b);
CodeItem {
code,
cid: code,
unicode: self.unicode_for(code),
width: self.width_for(code, code),
is_word_space: b == 32,
}
})
.collect(),
}
}
fn unicode_for(&self, code: u32) -> Option<String> {
if let Some(map) = &self.to_unicode {
if let Some(s) = map.unicode(code) {
return Some(undo_symbol_pua(&s));
}
}
glyph_name_to_unicode(self.glyph_name(code))
}
fn glyph_name(&self, code: u32) -> &str {
self.names
.get(code as usize)
.map(String::as_str)
.unwrap_or("")
}
fn width_for(&self, code: u32, cid: u32) -> f32 {
match &self.widths {
Widths::Simple {
first,
widths,
missing,
} => {
if let Some(w) = code
.checked_sub(*first)
.and_then(|i| widths.get(i as usize))
.copied()
{
return w;
}
let standard = metrics::is_standard_font(&self.base_font);
if standard {
if let Some(w) = metrics::base14_width(&self.base_font, self.glyph_name(code)) {
return w;
}
}
missing
.or_else(|| metrics::base14_width(&self.base_font, self.glyph_name(code)))
.unwrap_or(500.0)
}
Widths::Cid { default, map } => map.get(&cid).copied().unwrap_or(*default),
}
}
pub fn outline(&self, item: &CodeItem) -> Option<Arc<Outline>> {
if let Some(hit) = self.outline_cache.lock().unwrap().get(&item.cid) {
return hit.clone();
}
let built = self.build_outline(item).map(Arc::new);
self.outline_cache
.lock()
.unwrap()
.insert(item.cid, built.clone());
built
}
fn build_outline(&self, item: &CodeItem) -> Option<Outline> {
self.embedded_outline(item)
.or_else(|| self.substitute_outline(item))
}
fn substitute_outline(&self, item: &CodeItem) -> Option<Outline> {
if self.is_type3 {
return None;
}
let data = self.substitute.get_or_init(|| {
system::substitute(system::Style {
serif: self.flags.is_serif(),
fixed_pitch: self.flags.is_fixed_pitch(),
bold: self.flags.is_bold(),
italic: self.flags.is_italic(),
})
});
let face = ttf_parser::Face::parse(data.as_ref()?, 0).ok()?;
let ch = item.unicode.as_deref()?.chars().next()?;
let gid = face.glyph_index(ch)?;
let mut sink = Sink::default();
face.outline_glyph(gid, &mut sink)?;
let upem = f32::from(face.units_per_em()).max(1.0);
Some(Outline {
is_substitute: true,
..sink.finish(1000.0 / upem, item.width)
})
}
fn embedded_outline(&self, item: &CodeItem) -> Option<Outline> {
match &self.program {
Program::Sfnt(data) => {
let face = ttf_parser::Face::parse(data, 0).ok()?;
let gid = self.sfnt_glyph_id(&face, item)?;
let mut sink = Sink::default();
face.outline_glyph(gid, &mut sink)?;
let upem = f32::from(face.units_per_em()).max(1.0);
Some(sink.finish(1000.0 / upem, item.width))
}
Program::Cff(data) => {
let table = ttf_parser::cff::Table::parse(data)?;
let gid = self.cff_glyph_id(&table, item)?;
let mut sink = Sink::default();
table.outline(gid, &mut sink).ok()?;
let scale = table.matrix().sx * 1000.0;
Some(sink.finish(if scale.abs() < 1e-6 { 1.0 } else { scale }, item.width))
}
Program::Type1(_) | Program::None => None,
}
}
fn sfnt_glyph_id(
&self,
face: &ttf_parser::Face,
item: &CodeItem,
) -> Option<ttf_parser::GlyphId> {
if self.composite.is_some() {
return Some(ttf_parser::GlyphId(self.composite_gid(item.cid)));
}
let name = self.glyph_name(item.code);
if self.symbolic {
if let Some(gid) = symbol_cmap_lookup(face, item.code) {
return Some(gid);
}
}
if !name.is_empty() {
if let Some(gid) = face.glyph_index_by_name(name) {
return Some(gid);
}
if let Some(ch) = glyph_name_to_unicode(name).and_then(|s| s.chars().next()) {
if let Some(gid) = face.glyph_index(ch) {
return Some(gid);
}
}
}
if !self.symbolic {
if let Some(gid) = symbol_cmap_lookup(face, item.code) {
return Some(gid);
}
}
Some(ttf_parser::GlyphId(item.code as u16))
}
fn cff_glyph_id(
&self,
table: &ttf_parser::cff::Table,
item: &CodeItem,
) -> Option<ttf_parser::GlyphId> {
if self.composite.is_some() {
if let Some(gid) = self.cff_cid_to_gid(table, item.cid) {
return Some(ttf_parser::GlyphId(gid));
}
return Some(ttf_parser::GlyphId(self.composite_gid(item.cid)));
}
let name = self.glyph_name(item.code);
if !name.is_empty() {
if let Some(gid) = table.glyph_index_by_name(name) {
return Some(gid);
}
}
if let Ok(code) = u8::try_from(item.code) {
if let Some(gid) = table.glyph_index(code) {
return Some(gid);
}
}
Some(ttf_parser::GlyphId(item.cid as u16))
}
fn cff_cid_to_gid(&self, table: &ttf_parser::cff::Table, cid: u32) -> Option<u16> {
let map = self.cff_cid_to_gid.get_or_init(|| {
let mut map = HashMap::new();
for gid in 0..table.number_of_glyphs() {
if let Some(cid) = table.glyph_cid(ttf_parser::GlyphId(gid)) {
map.entry(cid).or_insert(gid);
}
}
map
});
map.get(&u16::try_from(cid).ok()?).copied()
}
fn composite_gid(&self, cid: u32) -> u16 {
let Some(map) = self.composite.as_ref().and_then(|c| c.cid_to_gid.as_ref()) else {
return cid as u16;
};
let i = cid as usize * 2;
match (map.get(i), map.get(i + 1)) {
(Some(&hi), Some(&lo)) => u16::from_be_bytes([hi, lo]),
_ => 0,
}
}
pub fn type3_proc(&self, doc: &Document, code: u32) -> Option<Object> {
let name = self.glyph_name(code);
if name.is_empty() {
return None;
}
doc.dict_get(&self.char_procs, name)
}
}
fn symbol_cmap_lookup(face: &ttf_parser::Face, code: u32) -> Option<ttf_parser::GlyphId> {
use ttf_parser::PlatformId;
let cmap = face.tables().cmap?;
for sub in cmap.subtables {
match sub.platform_id {
PlatformId::Windows if sub.encoding_id == 0 => {
for candidate in [0xF000 + (code & 0xFF), code] {
if let Some(gid) = sub.glyph_index(candidate) {
return Some(gid);
}
}
}
PlatformId::Macintosh if sub.encoding_id == 0 => {
if let Some(gid) = sub.glyph_index(code) {
return Some(gid);
}
}
_ => {}
}
}
None
}
fn undo_symbol_pua(text: &str) -> String {
if !text.chars().any(|c| ('\u{F020}'..='\u{F0FF}').contains(&c)) {
return text.to_string();
}
text.chars()
.map(|c| {
if !('\u{F020}'..='\u{F0FF}').contains(&c) {
return c.to_string();
}
let code = (u32::from(c) & 0xFF) as u8;
let name = base_encoding_name(Encoding::Symbol, code);
glyph_name_to_unicode(name).unwrap_or_else(|| c.to_string())
})
.collect()
}
#[derive(Default)]
struct Sink {
builder: OutlineBuilder,
}
impl Sink {
fn finish(self, scale: f32, advance: f32) -> Outline {
let mut cmds = self.builder.finish();
if (scale - 1.0).abs() > 1e-6 {
for cmd in &mut cmds {
scale_cmd(cmd, scale);
}
}
Outline {
cmds,
advance,
is_substitute: false,
}
}
}
fn scale_cmd(cmd: &mut glyph::PathCmd, s: f32) {
use glyph::PathCmd::*;
let scale_point = |p: &mut crate::geom::Point| {
p.x *= s;
p.y *= s;
};
match cmd {
MoveTo(p) | LineTo(p) => scale_point(p),
CurveTo(a, b, c) => {
scale_point(a);
scale_point(b);
scale_point(c);
}
Close => {}
}
}
impl ttf_parser::OutlineBuilder for Sink {
fn move_to(&mut self, x: f32, y: f32) {
self.builder.move_to(x, y);
}
fn line_to(&mut self, x: f32, y: f32) {
self.builder.line_to(x, y);
}
fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
let (x0, y0) = (self.builder.x, self.builder.y);
self.builder.curve_to(
x0 + 2.0 / 3.0 * (x1 - x0),
y0 + 2.0 / 3.0 * (y1 - y0),
x + 2.0 / 3.0 * (x1 - x),
y + 2.0 / 3.0 * (y1 - y),
x,
y,
);
}
fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
self.builder.curve_to(x1, y1, x2, y2, x, y);
}
fn close(&mut self) {
self.builder.close();
}
}
fn load_to_unicode(doc: &Document, dict: &Dict) -> Option<CMap> {
let stream = doc.dict_get(dict, "ToUnicode")?;
let decoded = doc.file.decode_stream(stream.as_stream()?).ok()?;
let map = CMap::parse(&decoded.data);
map.has_unicode().then_some(map)
}
fn load_program(doc: &Document, descriptor: Option<&Dict>) -> Program {
let Some(desc) = descriptor else {
return Program::None;
};
for key in ["FontFile2", "FontFile3", "FontFile"] {
let Some(obj) = doc.dict_get(desc, key) else {
continue;
};
let Some(stream) = obj.as_stream() else {
continue;
};
let Ok(decoded) = doc.file.decode_stream(stream) else {
continue;
};
let data: Arc<[u8]> = Arc::from(decoded.data.as_slice());
let subtype = stream.dict.get("Subtype").and_then(|o| o.as_name());
return match key {
"FontFile2" => Program::Sfnt(data),
"FontFile3" => match subtype {
Some("OpenType") => Program::Sfnt(data),
_ => Program::Cff(data),
},
_ => Program::Type1(data),
};
}
Program::None
}
fn build_encoding(
doc: &Document,
dict: &Dict,
base_font: &str,
symbolic: bool,
program: &Program,
) -> Vec<String> {
let encoding_obj = doc.dict_get(dict, "Encoding");
let named = |n: &str| match n {
"WinAnsiEncoding" => Some(Encoding::WinAnsi),
"MacRomanEncoding" => Some(Encoding::MacRoman),
"StandardEncoding" | "MacExpertEncoding" => Some(Encoding::Standard),
_ => None,
};
let mut base = match &encoding_obj {
Some(Object::Name(n)) => named(n),
Some(obj) => obj
.as_dict()
.and_then(|d| d.get("BaseEncoding"))
.and_then(|o| o.as_name())
.and_then(named),
None => None,
};
let mut names = vec![String::new(); 256];
let mut from_program = false;
if base.is_none() {
match program.builtin_encoding() {
Some(type1::Builtin::Standard) => base = Some(Encoding::Standard),
Some(type1::Builtin::Custom(builtin)) => {
names = builtin;
from_program = true;
}
None => {}
}
}
if base.is_none() && !from_program {
let lower = base_font.to_ascii_lowercase();
if lower.contains("symbol") {
base = Some(Encoding::Symbol);
} else if !symbolic {
base = Some(Encoding::Standard);
}
}
if let Some(enc) = base {
for (code, slot) in names.iter_mut().enumerate() {
*slot = base_encoding_name(enc, code as u8).to_string();
}
}
if let Some(diffs) = encoding_obj
.as_ref()
.and_then(|o| o.as_dict())
.and_then(|d| doc.dict_get(d, "Differences"))
.as_ref()
.and_then(|o| o.as_array())
{
let mut code: i64 = 0;
for item in diffs {
match doc.resolve(item) {
Object::Int(i) => code = i,
Object::Real(r) => code = r as i64,
Object::Name(n) => {
if let Some(slot) = usize::try_from(code).ok().and_then(|c| names.get_mut(c)) {
*slot = n;
}
code += 1;
}
_ => {}
}
}
}
names
}
fn simple_widths(doc: &Document, dict: &Dict, descriptor: Option<&Dict>) -> Widths {
let first = doc
.dict_get(dict, "FirstChar")
.and_then(|o| o.as_int())
.unwrap_or(0)
.clamp(0, u32::MAX as i64) as u32;
let widths: Vec<f32> = doc
.dict_get(dict, "Widths")
.as_ref()
.and_then(|o| o.as_array())
.map(|a| {
a.iter()
.map(|o| doc.resolve(o).as_f32().unwrap_or(0.0))
.collect()
})
.unwrap_or_default();
let missing = descriptor
.and_then(|d| doc.dict_get(d, "MissingWidth"))
.and_then(|o| o.as_f32());
Widths::Simple {
first,
widths,
missing,
}
}
fn cid_widths(doc: &Document, descendant: &Dict) -> Widths {
const MAX_RANGE: u32 = 65_536;
let default = doc
.dict_get(descendant, "DW")
.and_then(|o| o.as_f32())
.unwrap_or(1000.0);
let mut map = HashMap::new();
if let Some(w) = doc
.dict_get(descendant, "W")
.as_ref()
.and_then(|o| o.as_array())
{
let items: Vec<Object> = w.iter().map(|o| doc.resolve(o)).collect();
let mut i = 0;
while i < items.len() {
let Some(start) = items[i].as_f32().map(|f| f.max(0.0) as u32) else {
i += 1;
continue;
};
match items.get(i + 1) {
Some(Object::Array(list)) => {
for (k, item) in list.iter().enumerate() {
let Some(cid) = start.checked_add(k as u32) else {
break;
};
if let Some(width) = doc.resolve(item).as_f32() {
map.insert(cid, width);
}
}
i += 2;
}
Some(end_obj) => {
let end = end_obj.as_f32().map(|f| f.max(0.0) as u32).unwrap_or(start);
let width = items.get(i + 2).and_then(|o| o.as_f32()).unwrap_or(default);
for cid in start..=end.min(start.saturating_add(MAX_RANGE)) {
map.insert(cid, width);
}
i += 3;
}
None => break,
}
}
}
Widths::Cid { default, map }
}
#[cfg(test)]
mod symbol_pua_tests {
use super::undo_symbol_pua;
#[test]
fn microsofts_symbol_range_becomes_real_characters() {
assert_eq!(undo_symbol_pua("\u{F061}"), "α");
assert_eq!(undo_symbol_pua("\u{F062}"), "β");
assert_eq!(undo_symbol_pua("\u{F06D}"), "\u{00B5}");
assert_eq!(undo_symbol_pua("\u{F03D}"), "=");
assert_eq!(undo_symbol_pua("\u{F061}\u{F03D}\u{F062}"), "α=β");
}
#[test]
fn ordinary_text_is_untouched() {
assert_eq!(undo_symbol_pua("hello"), "hello");
assert_eq!(undo_symbol_pua("α = β"), "α = β");
assert_eq!(undo_symbol_pua(""), "");
}
#[test]
fn private_use_outside_the_symbol_range_is_left_alone() {
assert_eq!(undo_symbol_pua("\u{E000}"), "\u{E000}");
assert_eq!(undo_symbol_pua("\u{F8FF}"), "\u{F8FF}");
}
#[test]
fn an_unmapped_symbol_slot_keeps_its_character() {
assert_eq!(undo_symbol_pua("\u{F010}"), "\u{F010}");
}
}