#![forbid(unsafe_code)]
use std::collections::HashMap;
use std::fmt;
use ab_glyph::{Font, FontArc, FontVec, GlyphId, PxScale, ScaleFont, point};
pub const BUNDLED_FONT: &[u8] = include_bytes!("../assets/fonts/JetBrainsMono-Regular-subset.ttf");
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum FontError {
Invalid,
TooManyFonts,
}
impl fmt::Display for FontError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Invalid => f.write_str("invalid font data"),
Self::TooManyFonts => f.write_str("too many fonts"),
}
}
}
impl std::error::Error for FontError {}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LineMetrics {
pub ascent: f32,
pub descent: f32,
pub line_gap: f32,
}
impl LineMetrics {
pub fn height(self) -> f32 {
self.ascent + self.descent + self.line_gap
}
fn scaled(self, size: f32) -> Self {
Self {
ascent: self.ascent * size,
descent: self.descent * size,
line_gap: self.line_gap * size,
}
}
}
struct LoadedFont {
font: FontArc,
advances: [f32; 128],
subscripts: [f32; 10],
metrics: LineMetrics,
}
impl LoadedFont {
fn new(font: FontArc) -> Self {
let em = em_scale(&font, 1.0);
let scaled = font.as_scaled(em);
let mut advances = [0.0; 128];
for (c, adv) in (0_u8..128).zip(advances.iter_mut()) {
*adv = scaled.h_advance(font.glyph_id(char::from(c)));
}
let mut subscripts = [0.0; 10];
for (c, adv) in ('\u{2080}'..='\u{2089}').zip(subscripts.iter_mut()) {
*adv = scaled.h_advance(font.glyph_id(c));
}
let metrics = LineMetrics {
ascent: scaled.ascent(),
descent: -scaled.descent(),
line_gap: scaled.line_gap(),
};
Self {
font,
advances,
subscripts,
metrics,
}
}
}
fn subscript_digit(c: char) -> Option<usize> {
('\u{2080}'..='\u{2089}')
.contains(&c)
.then(|| c as usize - 0x2080)
}
fn em_scale(font: &FontArc, size: f32) -> PxScale {
let upem = font.units_per_em().unwrap_or(1000.0);
let height = font.height_unscaled();
if height > 0.0 {
PxScale::from(size * height / upem)
} else {
PxScale::from(size)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct GlyphBitmap {
pub width: u32,
pub height: u32,
pub left: i32,
pub top: i32,
pub coverage: Vec<u8>,
}
pub struct TextEngine {
fonts: Vec<LoadedFont>,
}
impl Default for TextEngine {
fn default() -> Self {
Self::new()
}
}
impl fmt::Debug for TextEngine {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TextEngine")
.field("fonts", &self.fonts.len())
.finish()
}
}
impl TextEngine {
pub fn new() -> Self {
let fonts = ab_glyph::FontRef::try_from_slice(BUNDLED_FONT)
.map(|f| vec![LoadedFont::new(FontArc::new(f))])
.unwrap_or_default();
Self { fonts }
}
pub fn add_font(&mut self, data: Vec<u8>) -> Result<u16, FontError> {
let id = u16::try_from(self.fonts.len()).map_err(|_| FontError::TooManyFonts)?;
let font = FontVec::try_from_vec(data).map_err(|_| FontError::Invalid)?;
self.fonts.push(LoadedFont::new(FontArc::new(font)));
Ok(id)
}
pub fn font_count(&self) -> usize {
self.fonts.len()
}
fn font(&self, id: u16) -> Option<&LoadedFont> {
self.fonts
.get(usize::from(id))
.or_else(|| self.fonts.first())
}
pub fn ascii_advances(&self, id: u16) -> &[f32; 128] {
static ZERO: [f32; 128] = [0.0; 128];
self.font(id).map_or(&ZERO, |f| &f.advances)
}
pub fn subscript_advances(&self, id: u16) -> &[f32; 10] {
static ZERO: [f32; 10] = [0.0; 10];
self.font(id).map_or(&ZERO, |f| &f.subscripts)
}
pub fn line_metrics(&self, id: u16, size: f32) -> LineMetrics {
self.font(id).map_or(
LineMetrics {
ascent: size,
descent: 0.0,
line_gap: 0.0,
},
|f| f.metrics.scaled(size),
)
}
pub fn advance(&self, id: u16, c: char, size: f32) -> f32 {
let Some(f) = self.font(id) else {
return 0.0;
};
if c.is_ascii() {
return f.advances[usize::from(c as u8)] * size;
}
if let Some(d) = subscript_digit(c) {
return f.subscripts[d] * size;
}
let scaled = f.font.as_scaled(em_scale(&f.font, size));
scaled.h_advance(f.font.glyph_id(c))
}
pub fn width(&self, id: u16, text: &str, size: f32) -> f32 {
if text.is_ascii() {
let table = self.ascii_advances(id);
return text.bytes().map(|b| table[usize::from(b)]).sum::<f32>() * size;
}
text.chars().map(|c| self.advance(id, c, size)).sum()
}
pub fn rasterize(&self, id: u16, c: char, size: f32) -> Option<GlyphBitmap> {
let f = self.font(id)?;
let glyph = f
.font
.glyph_id(c)
.with_scale_and_position(em_scale(&f.font, size), point(0.0, 0.0));
let outlined = f.font.outline_glyph(glyph)?;
let bounds = outlined.px_bounds();
let width = bounds.width() as u32;
let height = bounds.height() as u32;
let mut coverage = vec![0_u8; (width as usize) * (height as usize)];
outlined.draw(|x, y, v| {
let i = (y as usize) * (width as usize) + x as usize;
if let Some(px) = coverage.get_mut(i) {
*px = (v.clamp(0.0, 1.0) * 255.0 + 0.5) as u8;
}
});
Some(GlyphBitmap {
width,
height,
left: bounds.min.x as i32,
top: bounds.min.y as i32,
coverage,
})
}
pub fn glyph_id(&self, id: u16, c: char) -> u16 {
self.font(id).map_or(0, |f| {
let GlyphId(g) = f.font.glyph_id(c);
g
})
}
}
#[derive(Debug, Default)]
pub struct GlyphCache {
map: HashMap<(u16, u16, u32), Option<GlyphBitmap>>,
}
impl GlyphCache {
pub fn get(
&mut self,
engine: &TextEngine,
id: u16,
c: char,
size: f32,
) -> Option<&GlyphBitmap> {
let key = (id, engine.glyph_id(id, c), size.to_bits());
self.map
.entry(key)
.or_insert_with(|| engine.rasterize(id, c, size))
.as_ref()
}
pub fn len(&self) -> usize {
self.map.len()
}
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
pub fn clear(&mut self) {
self.map.clear();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bundled_font_loads() {
let e = TextEngine::new();
assert_eq!(e.font_count(), 1);
assert!((e.ascii_advances(0)[usize::from(b'5')] - 0.6).abs() < 1e-6);
assert!(BUNDLED_FONT.len() < 100 * 1024);
}
#[test]
fn bundled_font_has_the_listed_symbols() {
let e = TextEngine::new();
for c in "\u{a0}°±·×÷–—…₀₁₂₃₄₅₆₇₈₉←↑→↓−≈≠≤≥▲▼".chars()
{
assert_ne!(e.glyph_id(0, c), 0, "no glyph for {c:?}");
if c != '\u{a0}' {
assert!(e.rasterize(0, c, 16.0).is_some(), "{c:?} draws nothing");
}
}
assert_eq!(e.glyph_id(0, '€'), 0);
}
#[test]
fn width_matches_ascii_sum() {
let e = TextEngine::new();
let w = e.width(0, "123.45", 11.0);
assert!((w - 6.0 * 0.6 * 11.0).abs() < 1e-3);
assert!((e.width(7, "123.45", 11.0) - w).abs() < 1e-6);
}
#[test]
fn glyphs_sit_on_the_baseline() {
let e = TextEngine::new();
let g = e.rasterize(0, 'H', 20.0).unwrap();
let m = e.line_metrics(0, 20.0);
assert!(g.top < 0 && -g.top as f32 <= m.ascent + 1.0);
assert!(g.coverage.contains(&255));
}
}