use std::cell::RefCell;
use std::collections::BTreeMap;
use std::rc::Rc;
use pdf2::Encoding;
use crate::geom::Point;
#[derive(Debug, Default)]
pub struct Cache {
cmap: BTreeMap<char, Option<u16>>,
mac: BTreeMap<u8, Option<u16>>,
glyphs: BTreeMap<u16, Option<Glyph>>,
}
pub type GlyphCache = Rc<RefCell<Cache>>;
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Glyph {
pub contours: Vec<Vec<Point>>,
pub advance: f64,
}
#[derive(Debug, Clone)]
pub enum Font {
Builtin {
encoding: Encoding,
},
Type1 {
encoding: Encoding,
program: Rc<crate::type1::Program>,
names: std::collections::HashMap<char, Vec<u8>>,
},
Cff {
encoding: Encoding,
data: Vec<u8>,
cid_to_gid: Vec<(u16, u16)>,
names: std::collections::HashMap<char, u16>,
cache: GlyphCache,
},
TrueType {
encoding: Encoding,
data: Vec<u8>,
has_cmap: bool,
has_mac_cmap: bool,
cache: GlyphCache,
},
}
impl Font {
pub fn builtin(encoding: Encoding) -> Self {
Self::Builtin { encoding }
}
pub fn cff(encoding: Encoding, data: Vec<u8>) -> Option<Self> {
let wrapped = data.starts_with(b"OTTO");
let table: Vec<u8> = if wrapped {
cff_table_of_sfnt(&data)?.to_vec()
} else {
data.clone()
};
let (charset_at, glyphs) = cff_layout(&table)?;
let cid_to_gid = if top_dict_declares_ros(&table) {
cff_charset(&table, charset_at, glyphs)
} else {
Vec::new()
};
let program = if wrapped {
data
} else {
wrap_cff(&table, glyphs)?
};
let face = ttf_parser::Face::parse(&program, 0).ok()?;
let names = if cid_to_gid.is_empty() {
glyph_names(&face)
} else {
std::collections::HashMap::new()
};
Some(Self::Cff {
encoding,
data: program,
cid_to_gid,
names,
cache: GlyphCache::default(),
})
}
pub fn type1(encoding: Encoding, program: crate::type1::Program) -> Option<Self> {
if !program.is_usable() {
return None;
}
let mut names = std::collections::HashMap::new();
for name in program.names() {
if let Some(character) = pdf2::encoding::glyph_name_character(name) {
names.entry(character).or_insert_with(|| name.to_vec());
}
}
Some(Self::Type1 {
encoding,
program: Rc::new(program),
names,
})
}
pub fn true_type(encoding: Encoding, data: Vec<u8>) -> Option<Self> {
let face = ttf_parser::Face::parse(&data, 0).ok()?;
if face.units_per_em() == 0 {
return None;
}
let mut has_cmap = false;
let mut has_mac_cmap = false;
if let Some(cmap) = face.tables().cmap {
for subtable in cmap.subtables {
if subtable.is_unicode() {
has_cmap = true;
}
if subtable.platform_id == ttf_parser::PlatformId::Macintosh
&& subtable.encoding_id == 0
{
has_cmap = true;
has_mac_cmap = true;
}
}
}
Some(Self::TrueType {
encoding,
data,
has_cmap,
has_mac_cmap,
cache: GlyphCache::default(),
})
}
pub fn glyph(&self, code: u8) -> Option<Glyph> {
match self {
Self::Builtin { encoding } => builtin_glyph(encoding.character(code)?),
Self::Type1 {
encoding,
program,
names,
} => {
let character = encoding.character(code)?;
program.glyph(names.get(&character)?)
}
Self::TrueType {
encoding,
data,
cache,
has_mac_cmap,
..
} => {
let character = encoding.character(code)?;
let id = glyph_id(data, cache, character).or_else(|| {
has_mac_cmap
.then(|| mac_glyph_id(data, cache, code))
.flatten()
})?;
cached_glyph(data, cache, id)
}
Self::Cff {
encoding,
data,
names,
cache,
..
} => {
let character = encoding.character(code)?;
let id = if names.is_empty() {
glyph_id(data, cache, character)?
} else {
ttf_parser::GlyphId(*names.get(&character)?)
};
cached_glyph(data, cache, id)
}
}
}
pub fn has_glyph_names(&self) -> bool {
match self {
Self::Type1 { names, .. } => !names.is_empty(),
Self::Cff { names, .. } => !names.is_empty(),
Self::TrueType { has_cmap, .. } => *has_cmap,
Self::Builtin { .. } => false,
}
}
pub fn has_character_map(&self) -> bool {
match self {
Self::Builtin { .. } | Self::Cff { .. } | Self::Type1 { .. } => true,
Self::TrueType { has_cmap, .. } => *has_cmap,
}
}
pub fn advance(&self, code: u8) -> f64 {
self.glyph(code)
.map_or(BUILTIN_ADVANCE, |glyph| glyph.advance)
}
pub fn glyph_by_id(&self, id: u16) -> Option<Glyph> {
match self {
Self::Builtin { .. } => None,
Self::TrueType { data, cache, .. } => {
cached_glyph(data, cache, ttf_parser::GlyphId(id))
}
Self::Type1 { .. } => None,
Self::Cff {
data,
cid_to_gid,
cache,
..
} => {
let glyph = cid_to_gid
.iter()
.find(|(cid, _)| *cid == id)
.map_or(id, |(_, gid)| *gid);
cached_glyph(data, cache, ttf_parser::GlyphId(glyph))
}
}
}
}
fn glyph_names(face: &ttf_parser::Face<'_>) -> std::collections::HashMap<char, u16> {
let mut map = std::collections::HashMap::new();
for id in 0..face.number_of_glyphs() {
let Some(name) = face.glyph_name(ttf_parser::GlyphId(id)) else {
continue;
};
if let Some(character) = pdf2::encoding::glyph_name_character(name.as_bytes()) {
map.entry(character).or_insert(id);
}
}
map
}
fn glyph_id(data: &[u8], cache: &GlyphCache, character: char) -> Option<ttf_parser::GlyphId> {
if let Some(cached) = cache.borrow().cmap.get(&character) {
return cached.map(ttf_parser::GlyphId);
}
let face = ttf_parser::Face::parse(data, 0).ok()?;
let found = face.glyph_index(character).map(|id| id.0);
cache.borrow_mut().cmap.insert(character, found);
found.map(ttf_parser::GlyphId)
}
fn mac_glyph_id(data: &[u8], cache: &GlyphCache, code: u8) -> Option<ttf_parser::GlyphId> {
if let Some(cached) = cache.borrow().mac.get(&code) {
return cached.map(ttf_parser::GlyphId);
}
let face = ttf_parser::Face::parse(data, 0).ok()?;
let found = face.tables().cmap.and_then(|cmap| {
cmap.subtables
.into_iter()
.find(|subtable| {
subtable.platform_id == ttf_parser::PlatformId::Macintosh
&& subtable.encoding_id == 0
})
.and_then(|subtable| subtable.glyph_index(u32::from(code)))
.map(|id| id.0)
});
cache.borrow_mut().mac.insert(code, found);
found.map(ttf_parser::GlyphId)
}
fn cached_glyph(data: &[u8], cache: &GlyphCache, id: ttf_parser::GlyphId) -> Option<Glyph> {
if let Some(cached) = cache.borrow().glyphs.get(&id.0) {
return cached.clone();
}
let glyph = glyph_from_face(data, id);
cache.borrow_mut().glyphs.insert(id.0, glyph.clone());
glyph
}
fn wrap_cff(cff: &[u8], glyphs: u16) -> Option<Vec<u8>> {
let mut head = [0_u8; 54];
head[0..4].copy_from_slice(&0x0001_0000_u32.to_be_bytes());
head[4..8].copy_from_slice(&0x0001_0000_u32.to_be_bytes());
head[12..16].copy_from_slice(&0x5F0F_3CF5_u32.to_be_bytes());
head[18..20].copy_from_slice(&1000_u16.to_be_bytes());
let mut hhea = [0_u8; 36];
hhea[0..4].copy_from_slice(&0x0001_0000_u32.to_be_bytes());
hhea[34..36].copy_from_slice(&glyphs.to_be_bytes());
let mut maxp = [0_u8; 6];
maxp[0..4].copy_from_slice(&0x0000_5000_u32.to_be_bytes());
maxp[4..6].copy_from_slice(&glyphs.to_be_bytes());
let hmtx = vec![0_u8; usize::from(glyphs) * 4];
let tables: [(&[u8; 4], &[u8]); 5] = [
(b"CFF ", cff),
(b"head", &head),
(b"hhea", &hhea),
(b"hmtx", &hmtx),
(b"maxp", &maxp),
];
let count = u16::try_from(tables.len()).ok()?;
let mut out = Vec::with_capacity(cff.len() + 256);
out.extend_from_slice(&0x4F54_544F_u32.to_be_bytes());
out.extend_from_slice(&count.to_be_bytes());
let pow2 = 1_u32 << u32::from(count).ilog2();
#[allow(clippy::cast_possible_truncation)]
let search_range = pow2 * 16;
out.extend_from_slice(&u16::try_from(search_range).ok()?.to_be_bytes());
#[allow(clippy::cast_possible_truncation)]
let entry_selector = count.trailing_zeros() as u16;
out.extend_from_slice(&entry_selector.to_be_bytes());
#[allow(clippy::cast_possible_truncation)]
let range_shift = (u32::from(count) * 16).saturating_sub(search_range) as u16;
out.extend_from_slice(&range_shift.to_be_bytes());
let mut offset = 12 + usize::from(count) * 16;
let mut body: Vec<u8> = Vec::with_capacity(cff.len() + 128);
for (tag, data) in &tables {
let mut table = data.to_vec();
while !table.len().is_multiple_of(4) {
table.push(0);
}
out.extend_from_slice(*tag);
out.extend_from_slice(&0_u32.to_be_bytes());
out.extend_from_slice(&u32::try_from(offset).ok()?.to_be_bytes());
out.extend_from_slice(&u32::try_from(data.len()).ok()?.to_be_bytes());
offset += table.len();
body.extend_from_slice(&table);
}
out.extend_from_slice(&body);
Some(out)
}
fn cff_table_of_sfnt(data: &[u8]) -> Option<&[u8]> {
let count = usize::from(u16::from_be_bytes([*data.get(4)?, *data.get(5)?]));
for index in 0..count {
let at = 12 + index * 16;
if data.get(at..at + 4)? == b"CFF " {
let offset = u32::from_be_bytes([
*data.get(at + 8)?,
*data.get(at + 9)?,
*data.get(at + 10)?,
*data.get(at + 11)?,
]);
let length = u32::from_be_bytes([
*data.get(at + 12)?,
*data.get(at + 13)?,
*data.get(at + 14)?,
*data.get(at + 15)?,
]);
let start = usize::try_from(offset).ok()?;
let end = start.checked_add(usize::try_from(length).ok()?)?;
return data.get(start..end);
}
}
None
}
fn cff_layout(cff: &[u8]) -> Option<(usize, u16)> {
let header_size = usize::from(*cff.get(2)?);
let (_, after_name) = cff_index(cff, header_size)?;
let (top_count, after_top) = cff_index(cff, after_name)?;
if top_count == 0 {
return None;
}
let (_, after_strings) = cff_index(cff, after_top)?;
let (_, after_global_subrs) = cff_index(cff, after_strings)?;
let _ = after_global_subrs;
let top_dict = cff_index_item(cff, after_name, 0)?;
let charstrings_at = top_dict_operator(top_dict, 17)?;
let (glyphs, _) = cff_index(cff, charstrings_at)?;
let charset_at = top_dict_operator(top_dict, 15).unwrap_or(0);
Some((charset_at, u16::try_from(glyphs).ok()?))
}
fn top_dict_declares_ros(cff: &[u8]) -> bool {
let Some(header_size) = cff.get(2).map(|size| usize::from(*size)) else {
return false;
};
let Some((_, after_name)) = cff_index(cff, header_size) else {
return false;
};
let Some(dict) = cff_index_item(cff, after_name, 0) else {
return false;
};
dict.array_windows::<2>().any(|window| window == &[12, 30])
}
fn cff_charset(cff: &[u8], at: usize, glyphs: u16) -> Vec<(u16, u16)> {
let mut pairs = Vec::new();
if at < 3 {
return pairs;
}
let Some(&format) = cff.get(at) else {
return pairs;
};
let mut index = at + 1;
let mut gid = 1u16;
match format {
0 => {
while gid < glyphs {
let cid = cff
.get(index..index + 2)
.map(|bytes| u16::from_be_bytes([bytes[0], bytes[1]]));
let Some(cid) = cid else { break };
pairs.push((cid, gid));
gid += 1;
index += 2;
}
}
1 | 2 => {
while gid < glyphs {
let width = if format == 1 { 1 } else { 2 };
let first = cff
.get(index..index + 2)
.map(|bytes| u16::from_be_bytes([bytes[0], bytes[1]]));
let left = cff.get(index + 2..index + 2 + width).map(|bytes| {
if width == 1 {
u16::from(bytes[0])
} else {
u16::from_be_bytes([bytes[0], bytes[1]])
}
});
let (Some(first), Some(left)) = (first, left) else {
break;
};
for offset in 0..=left {
if gid >= glyphs {
break;
}
pairs.push((first.wrapping_add(offset), gid));
gid += 1;
}
index += 2 + width;
}
}
_ => {}
}
pairs
}
fn cff_index(data: &[u8], at: usize) -> Option<(usize, usize)> {
let count = usize::from(u16::from_be_bytes([*data.get(at)?, *data.get(at + 1)?]));
if count == 0 {
return Some((0, at + 2));
}
let off_size = usize::from(*data.get(at + 2)?);
if !(1..=4).contains(&off_size) {
return None;
}
let offsets_at = at + 3;
let data_at = offsets_at + (count + 1) * off_size;
let mut last = 0;
for index in 0..off_size {
last = (last << 8) | usize::from(*data.get(offsets_at + count * off_size + index)?);
}
Some((count, data_at + last.saturating_sub(1)))
}
fn cff_index_item(data: &[u8], at: usize, item: usize) -> Option<&[u8]> {
let count = usize::from(u16::from_be_bytes([*data.get(at)?, *data.get(at + 1)?]));
if item >= count {
return None;
}
let off_size = usize::from(*data.get(at + 2)?);
let offsets_at = at + 3;
let data_at = offsets_at + (count + 1) * off_size;
let read = |position: usize| -> Option<usize> {
let base = offsets_at + position * off_size;
let mut value = 0;
for index in 0..off_size {
value = (value << 8) | usize::from(*data.get(base + index)?);
}
Some(value)
};
let start = read(item)?;
let end = read(item + 1)?;
data.get(data_at + start - 1..data_at + end - 1)
}
fn top_dict_operator(dict: &[u8], operator: u8) -> Option<usize> {
let mut operands: Vec<f64> = Vec::new();
let mut index = 0;
while index < dict.len() {
let byte = *dict.get(index)?;
index += 1;
match byte {
28 => {
let value = i16::from_be_bytes([*dict.get(index)?, *dict.get(index + 1)?]);
operands.push(f64::from(value));
index += 2;
}
29 => {
let mut bytes = [0_u8; 4];
for (offset, slot) in bytes.iter_mut().enumerate() {
*slot = *dict.get(index + offset)?;
}
operands.push(f64::from(i32::from_be_bytes(bytes)));
index += 4;
}
32..=246 => operands.push(f64::from(byte) - 139.0),
247..=250 => {
let next = f64::from(*dict.get(index)?);
operands.push((f64::from(byte) - 247.0) * 256.0 + next + 108.0);
index += 1;
}
251..=254 => {
let next = f64::from(*dict.get(index)?);
operands.push(-(f64::from(byte) - 251.0) * 256.0 - next - 108.0);
index += 1;
}
30 => {
while index < dict.len() {
let nibble = dict[index];
index += 1;
if nibble & 0x0F == 0x0F || nibble >> 4 == 0x0F {
break;
}
}
operands.push(f64::NAN);
}
0..=21 => {
if byte == 12 {
index += 1;
}
if byte == operator {
if let Some(value) = operands.last()
&& value.is_finite()
&& *value >= 0.0
{
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
return Some(*value as usize);
}
return None;
}
operands.clear();
}
_ => operands.clear(),
}
}
None
}
fn glyph_from_face(data: &[u8], glyph_id: ttf_parser::GlyphId) -> Option<Glyph> {
let face = ttf_parser::Face::parse(data, 0).ok()?;
let units = f64::from(face.units_per_em());
let mut builder = OutlineBuilder::new(units);
face.outline_glyph(glyph_id, &mut builder)?;
let advance = f64::from(face.glyph_hor_advance(glyph_id).unwrap_or(0)) / units;
Some(Glyph {
contours: builder.contours,
advance,
})
}
const BUILTIN_ADVANCE: f64 = 6.0 / 9.0;
const BUILTIN_CELL: f64 = 1.0 / 9.0;
const BUILTIN_WIDTH: u8 = 5;
const BUILTIN_HEIGHT: u8 = 7;
fn builtin_glyph(character: char) -> Option<Glyph> {
let code = u8::try_from(u32::from(character)).ok()?;
if character == ' ' {
return Some(Glyph {
contours: Vec::new(),
advance: BUILTIN_ADVANCE,
});
}
if let Some(rows) = glyph_rows(code) {
return Some(Glyph {
contours: rows_to_contours(&rows),
advance: BUILTIN_ADVANCE,
});
}
if character == '\u{2022}' {
return Some(Glyph {
contours: rows_to_contours(&[0, 0, 0b00100, 0b01110, 0b00100, 0, 0]),
advance: BUILTIN_ADVANCE,
});
}
if let Some((base, accent)) = decompose(character) {
let rows = glyph_rows(u8::try_from(u32::from(base)).unwrap_or(b'?'))
.or_else(|| upper(base).and_then(|upper| glyph_rows(upper as u8)))
.unwrap_or_else(notdef_rows);
let mut contours = rows_to_contours(&rows);
contours.extend(
accent
.cells()
.iter()
.map(|&(column, layer)| mark_cell(column, layer)),
);
return Some(Glyph {
contours,
advance: BUILTIN_ADVANCE,
});
}
if character == '\u{00F8}' || character == '\u{00D8}' {
let mut contours = rows_to_contours(&glyph_rows(b'O').unwrap_or_else(notdef_rows));
contours.extend(
[(0, 5), (1, 4), (2, 3), (3, 2), (4, 1)]
.iter()
.map(|&(column, layer)| mark_cell(column, layer)),
);
return Some(Glyph {
contours,
advance: BUILTIN_ADVANCE,
});
}
let rows = upper(character)
.and_then(|upper| glyph_rows(upper as u8))
.unwrap_or_else(notdef_rows);
Some(Glyph {
contours: rows_to_contours(&rows),
advance: BUILTIN_ADVANCE,
})
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Accent {
Acute,
Grave,
Circumflex,
Diaeresis,
Tilde,
Ring,
Cedilla,
}
impl Accent {
fn cells(self) -> &'static [(u8, i8)] {
match self {
Self::Acute => &[(2, 0), (3, 0), (3, 1)],
Self::Grave => &[(2, 1), (3, 1), (3, 0)],
Self::Circumflex => &[(1, 0), (2, 1), (3, 0)],
Self::Diaeresis => &[(1, 1), (3, 1)],
Self::Tilde => &[(1, 1), (2, 0), (3, 1)],
Self::Ring => &[(1, 0), (2, 1), (3, 0), (2, 0)],
Self::Cedilla => &[(2, -1), (3, -1), (3, -2)],
}
}
}
fn mark_cell(column: u8, layer: i8) -> Vec<Point> {
let x0 = f64::from(column) * BUILTIN_CELL;
let x1 = x0 + BUILTIN_CELL;
let (bottom, top) = if layer < 0 {
(
f64::from(layer) * BUILTIN_CELL,
f64::from(layer + 1) * BUILTIN_CELL,
)
} else {
(
f64::from(BUILTIN_HEIGHT + layer as u8) * BUILTIN_CELL,
f64::from(BUILTIN_HEIGHT + layer as u8 + 1) * BUILTIN_CELL,
)
};
vec![
Point::new(x0, bottom),
Point::new(x1, bottom),
Point::new(x1, top),
Point::new(x0, top),
]
}
fn decompose(character: char) -> Option<(char, Accent)> {
let (base, accent) = match character {
'á' | 'Á' => ('a', Accent::Acute),
'é' | 'É' => ('e', Accent::Acute),
'í' | 'Í' => ('i', Accent::Acute),
'ó' | 'Ó' => ('o', Accent::Acute),
'ú' | 'Ú' => ('u', Accent::Acute),
'ý' | 'Ý' => ('y', Accent::Acute),
'à' | 'À' => ('a', Accent::Grave),
'è' | 'È' => ('e', Accent::Grave),
'ì' | 'Ì' => ('i', Accent::Grave),
'ò' | 'Ò' => ('o', Accent::Grave),
'ù' | 'Ù' => ('u', Accent::Grave),
'â' | 'Â' => ('a', Accent::Circumflex),
'ê' | 'Ê' => ('e', Accent::Circumflex),
'î' | 'Î' => ('i', Accent::Circumflex),
'ô' | 'Ô' => ('o', Accent::Circumflex),
'û' | 'Û' => ('u', Accent::Circumflex),
'ä' | 'Ä' => ('a', Accent::Diaeresis),
'ë' | 'Ë' => ('e', Accent::Diaeresis),
'ï' | 'Ï' => ('i', Accent::Diaeresis),
'ö' | 'Ö' => ('o', Accent::Diaeresis),
'ü' | 'Ü' => ('u', Accent::Diaeresis),
'ÿ' | 'Ÿ' => ('y', Accent::Diaeresis),
'ã' | 'Ã' => ('a', Accent::Tilde),
'õ' | 'Õ' => ('o', Accent::Tilde),
'ñ' | 'Ñ' => ('n', Accent::Tilde),
'å' | 'Å' => ('a', Accent::Ring),
'ç' | 'Ç' => ('c', Accent::Cedilla),
_ => return None,
};
Some((base, accent))
}
fn upper(character: char) -> Option<char> {
character
.to_uppercase()
.next()
.filter(|upper| *upper != character)
}
fn notdef_rows() -> [u8; BUILTIN_HEIGHT as usize] {
[
0b11111, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b11111,
]
}
fn rows_to_contours(rows: &[u8; BUILTIN_HEIGHT as usize]) -> Vec<Vec<Point>> {
let mut contours = Vec::new();
for (row, bits) in rows.iter().enumerate() {
for column in 0..BUILTIN_WIDTH {
let mask = 1_u8 << (BUILTIN_WIDTH - 1 - column);
if bits & mask == 0 {
continue;
}
let x0 = f64::from(column) * BUILTIN_CELL;
let x1 = x0 + BUILTIN_CELL;
let top = f64::from(BUILTIN_HEIGHT - 1 - row as u8) * BUILTIN_CELL;
let bottom = top + BUILTIN_CELL;
contours.push(vec![
Point::new(x0, bottom),
Point::new(x1, bottom),
Point::new(x1, top),
Point::new(x0, top),
]);
}
}
contours
}
fn glyph_rows(code: u8) -> Option<[u8; 7]> {
let rows = match code {
b'A' => [
0b01110, 0b10001, 0b10001, 0b11111, 0b10001, 0b10001, 0b10001,
],
b'B' => [
0b11110, 0b10001, 0b10001, 0b11110, 0b10001, 0b10001, 0b11110,
],
b'C' => [
0b01110, 0b10001, 0b10000, 0b10000, 0b10000, 0b10001, 0b01110,
],
b'D' => [
0b11110, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b11110,
],
b'E' => [
0b11111, 0b10000, 0b10000, 0b11110, 0b10000, 0b10000, 0b11111,
],
b'F' => [
0b11111, 0b10000, 0b10000, 0b11110, 0b10000, 0b10000, 0b10000,
],
b'G' => [
0b01110, 0b10001, 0b10000, 0b10111, 0b10001, 0b10001, 0b01110,
],
b'H' => [
0b10001, 0b10001, 0b10001, 0b11111, 0b10001, 0b10001, 0b10001,
],
b'I' => [
0b01110, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b01110,
],
b'J' => [
0b00111, 0b00010, 0b00010, 0b00010, 0b00010, 0b10010, 0b01100,
],
b'K' => [
0b10001, 0b10010, 0b10100, 0b11000, 0b10100, 0b10010, 0b10001,
],
b'L' => [
0b10000, 0b10000, 0b10000, 0b10000, 0b10000, 0b10000, 0b11111,
],
b'M' => [
0b10001, 0b11011, 0b10101, 0b10101, 0b10001, 0b10001, 0b10001,
],
b'N' => [
0b10001, 0b11001, 0b10101, 0b10011, 0b10001, 0b10001, 0b10001,
],
b'O' => [
0b01110, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b01110,
],
b'P' => [
0b11110, 0b10001, 0b10001, 0b11110, 0b10000, 0b10000, 0b10000,
],
b'Q' => [
0b01110, 0b10001, 0b10001, 0b10001, 0b10101, 0b10010, 0b01101,
],
b'R' => [
0b11110, 0b10001, 0b10001, 0b11110, 0b10100, 0b10010, 0b10001,
],
b'S' => [
0b01111, 0b10000, 0b10000, 0b01110, 0b00001, 0b00001, 0b11110,
],
b'T' => [
0b11111, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100,
],
b'U' => [
0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b01110,
],
b'V' => [
0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b01010, 0b00100,
],
b'W' => [
0b10001, 0b10001, 0b10001, 0b10101, 0b10101, 0b11011, 0b10001,
],
b'X' => [
0b10001, 0b10001, 0b01010, 0b00100, 0b01010, 0b10001, 0b10001,
],
b'Y' => [
0b10001, 0b10001, 0b01010, 0b00100, 0b00100, 0b00100, 0b00100,
],
b'Z' => [
0b11111, 0b00001, 0b00010, 0b00100, 0b01000, 0b10000, 0b11111,
],
b'0' => [
0b01110, 0b10001, 0b10011, 0b10101, 0b11001, 0b10001, 0b01110,
],
b'1' => [
0b00100, 0b01100, 0b00100, 0b00100, 0b00100, 0b00100, 0b01110,
],
b'2' => [
0b01110, 0b10001, 0b00001, 0b00010, 0b00100, 0b01000, 0b11111,
],
b'3' => [
0b11111, 0b00010, 0b00100, 0b00010, 0b00001, 0b10001, 0b01110,
],
b'4' => [
0b00010, 0b00110, 0b01010, 0b10010, 0b11111, 0b00010, 0b00010,
],
b'5' => [
0b11111, 0b10000, 0b11110, 0b00001, 0b00001, 0b10001, 0b01110,
],
b'6' => [
0b00110, 0b01000, 0b10000, 0b11110, 0b10001, 0b10001, 0b01110,
],
b'7' => [
0b11111, 0b00001, 0b00010, 0b00100, 0b01000, 0b01000, 0b01000,
],
b'8' => [
0b01110, 0b10001, 0b10001, 0b01110, 0b10001, 0b10001, 0b01110,
],
b'9' => [
0b01110, 0b10001, 0b10001, 0b01111, 0b00001, 0b00010, 0b01100,
],
b'.' => [0, 0, 0, 0, 0, 0b01100, 0b01100],
b',' => [0, 0, 0, 0, 0b01100, 0b01100, 0b01000],
b'!' => [
0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b00000, 0b00100,
],
b'?' => [
0b01110, 0b10001, 0b00001, 0b00010, 0b00100, 0b00000, 0b00100,
],
b'-' => [0, 0, 0, 0b11111, 0, 0, 0],
b':' => [0, 0b01100, 0b01100, 0, 0b01100, 0b01100, 0],
b';' => [0, 0b01100, 0b01100, 0, 0b01100, 0b01100, 0b01000],
b'\'' => [0b00100, 0b00100, 0b01000, 0, 0, 0, 0],
b'"' => [0b01010, 0b01010, 0, 0, 0, 0, 0],
b'(' => [
0b00010, 0b00100, 0b01000, 0b01000, 0b01000, 0b00100, 0b00010,
],
b')' => [
0b01000, 0b00100, 0b00010, 0b00010, 0b00010, 0b00100, 0b01000,
],
b'/' => [
0b00001, 0b00010, 0b00010, 0b00100, 0b01000, 0b01000, 0b10000,
],
b'+' => [0, 0b00100, 0b00100, 0b11111, 0b00100, 0b00100, 0],
b'=' => [0, 0, 0b11111, 0, 0b11111, 0, 0],
b'*' => [0, 0b10101, 0b01110, 0b11111, 0b01110, 0b10101, 0],
b'#' => [
0b01010, 0b01010, 0b11111, 0b01010, 0b11111, 0b01010, 0b01010,
],
b'%' => [
0b11001, 0b11010, 0b00010, 0b00100, 0b01000, 0b01011, 0b10011,
],
b'&' => [
0b01100, 0b10010, 0b10100, 0b01000, 0b10101, 0b10010, 0b01101,
],
_ => return None,
};
Some(rows)
}
struct OutlineBuilder {
units: f64,
contours: Vec<Vec<Point>>,
current: Vec<Point>,
}
impl OutlineBuilder {
fn new(units: f64) -> Self {
Self {
units,
contours: Vec::new(),
current: Vec::new(),
}
}
fn point(&self, x: f32, y: f32) -> Point {
Point::new(f64::from(x) / self.units, f64::from(y) / self.units)
}
fn finish_contour(&mut self) {
if self.current.len() > 1 {
self.contours.push(std::mem::take(&mut self.current));
}
}
}
impl ttf_parser::OutlineBuilder for OutlineBuilder {
fn move_to(&mut self, x: f32, y: f32) {
self.finish_contour();
self.current.push(self.point(x, y));
}
fn line_to(&mut self, x: f32, y: f32) {
self.current.push(self.point(x, y));
}
fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
let Some(&start) = self.current.last() else {
return;
};
let control = self.point(x1, y1);
let end = self.point(x, y);
for step in 1..=8 {
let t = f64::from(step) / 8.0;
let u = 1.0 - t;
self.current.push(Point::new(
u * u * start.x + 2.0 * u * t * control.x + t * t * end.x,
u * u * start.y + 2.0 * u * t * control.y + t * t * end.y,
));
}
}
fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
let Some(&start) = self.current.last() else {
return;
};
let c1 = self.point(x1, y1);
let c2 = self.point(x2, y2);
let end = self.point(x, y);
for step in 1..=12 {
let t = f64::from(step) / 12.0;
let u = 1.0 - t;
self.current.push(Point::new(
u * u * u * start.x
+ 3.0 * u * u * t * c1.x
+ 3.0 * u * t * t * c2.x
+ t * t * t * end.x,
u * u * u * start.y
+ 3.0 * u * u * t * c1.y
+ 3.0 * u * t * t * c2.y
+ t * t * t * end.y,
));
}
}
fn close(&mut self) {
self.finish_contour();
}
}
#[cfg(test)]
mod tests {
use super::Font;
use pdf2::Encoding;
#[test]
fn the_builtin_font_draws_letters_as_ink() {
let font = Font::builtin(Encoding::standard());
let glyph = font.glyph(b'H').expect("H");
assert!(!glyph.contours.is_empty());
assert!((glyph.advance - 0.6667).abs() < 1e-3);
for contour in &glyph.contours {
assert_eq!(contour.len(), 4);
for point in contour {
assert!(point.x >= 0.0 && point.x <= 6.0 / 9.0 + 1e-9);
assert!(point.y >= 0.0 && point.y <= 7.0 / 9.0 + 1e-9);
}
}
}
#[test]
fn the_builtin_font_is_blank_for_space() {
let font = Font::builtin(Encoding::standard());
let glyph = font.glyph(b' ').expect("space");
assert!(glyph.contours.is_empty());
assert!(glyph.advance > 0.0);
}
#[test]
fn glyph_ids_are_selected_directly_for_composite_fonts() {
let font = Font::builtin(Encoding::standard());
assert!(
font.glyph_by_id(1).is_none(),
"the bitmap alphabet has no glyph ids"
);
}
#[test]
fn a_broken_font_program_is_rejected() {
assert!(Font::true_type(Encoding::standard(), vec![0, 1, 2, 3]).is_none());
assert!(Font::true_type(Encoding::standard(), Vec::new()).is_none());
}
#[test]
fn accented_latin_composes_instead_of_drawing_notdef() {
let font = Font::builtin(Encoding::win_ansi());
for (code, base, marks) in [
(0xE9_u8, b'E', 3), (0xF1, b'N', 3), (0xFC, b'U', 2), (0xE7, b'C', 3), (0xE5, b'A', 4), (0xE2, b'A', 3), ] {
let glyph = font.glyph(code).expect("a composed glyph");
let plain = font.glyph(base).expect("the base letter");
assert_eq!(
glyph.contours.len(),
plain.contours.len() + marks,
"{} on {}",
code as char,
base as char
);
assert_ne!(glyph.contours, plain.contours);
}
let other = font.glyph(0xA4).expect("undefined code");
assert_eq!(other.contours.len(), 20, "the notdef box");
}
#[test]
fn marks_sit_outside_the_glyph_box_and_cedillas_below_the_baseline() {
let font = Font::builtin(Encoding::win_ansi());
let acute = font.glyph(0xE9).expect("é");
let top = acute
.contours
.iter()
.flatten()
.fold(f64::MIN, |max, point| max.max(point.y));
assert!((7.0 / 9.0..=1.0).contains(&top), "accent tops out at {top}");
let cedilla = font.glyph(0xE7).expect("ç");
let bottom = cedilla
.contours
.iter()
.flatten()
.fold(f64::MAX, |min, point| min.min(point.y));
assert!(bottom < 0.0, "the cedilla hangs to {bottom}");
}
#[test]
fn unknown_characters_become_the_notdef_box() {
let font = Font::builtin(Encoding::standard());
assert!(font.glyph(0xA4).is_none());
let glyph = font.glyph(b'@').expect("@");
assert_eq!(
glyph.contours.len(),
20,
"the notdef box is a hollow 5×7 frame"
);
}
}
#[cfg(test)]
mod cff_tests {
use super::{Font, cff_layout, top_dict_operator, wrap_cff};
fn synthetic_cff() -> Vec<u8> {
let mut cff = vec![1, 0, 4, 4];
cff.extend_from_slice(&[0, 1, 1, 1, 2, b'A']);
cff.extend_from_slice(&[0, 1, 1, 1, 7]);
cff.extend_from_slice(&[29, 0, 0, 0, 25, 17]);
cff.extend_from_slice(&[0, 0]);
cff.extend_from_slice(&[0, 0]);
assert_eq!(cff.len(), 25, "the CharStrings offset above");
cff.extend_from_slice(&[0, 3, 1, 1, 1, 1, 1]);
cff
}
#[test]
fn a_bare_cff_becomes_a_face_ttf_parser_reads() {
let cff = synthetic_cff();
let (charset_at, glyphs) = cff_layout(&cff).expect("layout");
assert_eq!((charset_at, glyphs), (0, 3), "predefined charset, 3 glyphs");
let wrapped = wrap_cff(&cff, glyphs).expect("wrap");
let face = ttf_parser::Face::parse(&wrapped, 0).expect("parse the wrapper");
assert_eq!(face.number_of_glyphs(), 3);
assert_eq!(face.units_per_em(), 1000);
assert!(Font::cff(pdf2::Encoding::standard(), cff).is_some());
}
#[test]
fn a_program_without_charstrings_is_refused() {
let cff = synthetic_cff();
assert!(cff_layout(&cff[..8]).is_none());
assert!(Font::cff(pdf2::Encoding::standard(), vec![0, 1]).is_none());
}
#[test]
fn the_top_dict_operator_reader_handles_each_number_encoding() {
assert_eq!(top_dict_operator(&[29, 0, 0, 1, 0, 17], 17), Some(256));
assert_eq!(top_dict_operator(&[28, 1, 0, 17], 17), Some(256));
assert_eq!(top_dict_operator(&[139 + 42, 17], 17), Some(42));
assert_eq!(top_dict_operator(&[251, 0, 17], 17), None);
assert_eq!(
top_dict_operator(&[29, 0, 0, 0, 9], 17),
None,
"no operator"
);
}
}
#[cfg(test)]
mod cache_tests {
use super::Font;
use pdf2::Encoding;
fn subset_font() -> Vec<u8> {
std::fs::read(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../tests/fixtures/fonts/CrimsonText-Regular-subset.ttf"
))
.expect("the committed subset font")
}
#[test]
fn a_glyph_looked_up_twice_is_the_same_outline() {
let font = Font::true_type(Encoding::standard(), subset_font()).expect("font");
let first = font.glyph(b'A').expect("A");
let second = font.glyph(b'A').expect("A");
assert_eq!(first, second, "the cache returns what the parser did");
assert!(!first.contours.is_empty(), "A is a drawn glyph");
assert!(first.advance > 0.0);
let b = font.glyph(b'B').expect("B");
assert_ne!(first, b);
}
#[test]
fn the_cache_is_shared_by_clones_and_survives_them() {
let font = Font::true_type(Encoding::standard(), subset_font()).expect("font");
let clone = font.clone();
let from_original = font.glyph(b'C').expect("C");
let from_clone = clone.glyph(b'C').expect("C");
assert_eq!(from_original, from_clone);
}
#[test]
fn a_program_with_a_cmap_answers_the_question_honestly() {
let with_cmap = Font::true_type(Encoding::standard(), subset_font()).expect("font");
assert!(with_cmap.has_character_map());
assert!(with_cmap.glyph(b'Z').is_some(), "the subset carries Z");
assert!(
with_cmap.glyph(0x00).is_none(),
"an unmapped code has no glyph"
);
}
fn fixture_font() -> Vec<u8> {
std::fs::read(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../tests/fixtures/fonts/CrimsonText-Regular-subset.ttf"
))
.expect("the committed subset font")
}
fn with_one_subtable(program: &[u8], platform: u16, encoding: u16) -> Vec<u8> {
let mut data = program.to_vec();
let tables = u16::from_be_bytes([data[4], data[5]]) as usize;
let mut cmap = None;
for index in 0..tables {
let record = 12 + index * 16;
if &data[record..record + 4] == b"cmap" {
let offset = u32::from_be_bytes([
data[record + 8],
data[record + 9],
data[record + 10],
data[record + 11],
]) as usize;
let length = u32::from_be_bytes([
data[record + 12],
data[record + 13],
data[record + 14],
data[record + 15],
]) as usize;
let face = ttf_parser::Face::parse(program, 0).expect("a parsable subset");
let glyph = face.glyph_index('A').expect("the subset maps A").0;
cmap = Some((offset, length, glyph));
}
}
let (offset, length, glyph) = cmap.expect("a cmap table");
assert!(
length >= 274,
"the table has room for a 262-byte format 0 subtable"
);
let mut table = vec![0_u8; 274];
table[2..4].copy_from_slice(&1_u16.to_be_bytes());
table[4..6].copy_from_slice(&platform.to_be_bytes());
table[6..8].copy_from_slice(&encoding.to_be_bytes());
table[8..12].copy_from_slice(&12_u32.to_be_bytes());
table[14..16].copy_from_slice(&262_u16.to_be_bytes());
table[18 + usize::from(b'A')] = u8::try_from(glyph).expect("a glyph id under 256");
data[offset..offset + 274].copy_from_slice(&table);
data
}
#[test]
fn a_macintosh_only_cmap_is_consulted_by_the_code() {
let font = Font::true_type(
Encoding::from_name(b"MacRomanEncoding"),
with_one_subtable(&fixture_font(), 1, 0),
)
.expect("the patched subset loads");
assert!(font.has_character_map(), "a (1,0) subtable is usable");
let glyph = font
.glyph(b'A')
.expect("A reaches its glyph through the Mac map");
assert!(!glyph.contours.is_empty(), "and the glyph has an outline");
}
#[test]
fn a_cmap_no_reader_can_consult_is_reported_as_such() {
let font = Font::true_type(
Encoding::standard(),
with_one_subtable(&fixture_font(), 3, 2),
)
.expect("the patched subset loads");
assert!(!font.has_character_map());
assert!(font.glyph(b'A').is_none());
}
}