use pdf2::Encoding;
use crate::geom::Point;
#[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,
},
TrueType {
encoding: Encoding,
data: Vec<u8>,
},
}
impl Font {
pub fn builtin(encoding: Encoding) -> Self {
Self::Builtin { encoding }
}
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;
}
Some(Self::TrueType { encoding, data })
}
pub fn glyph(&self, code: u8) -> Option<Glyph> {
match self {
Self::Builtin { encoding } => builtin_glyph(encoding.character(code)?),
Self::TrueType { encoding, data } => {
let character = encoding.character(code)?;
let face = ttf_parser::Face::parse(data, 0).ok()?;
let glyph_id = face.glyph_index(character)?;
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,
})
}
}
}
pub fn advance(&self, code: u8) -> f64 {
self.glyph(code)
.map_or(BUILTIN_ADVANCE, |glyph| glyph.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 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"
);
}
}