use rustyfi_backend::{FontKey, FontMetrics, Length};
pub(crate) const FONT_BOLD: FontKey = FontKey(1);
pub(crate) const BASE_FONT_NAMES: [&str; 3] = ["Helvetica", "Helvetica-Bold", "Helvetica-Oblique"];
#[rustfmt::skip]
const HELVETICA: [u16; 95] = [
278, 278, 355, 556, 556, 889, 667, 191, 333, 333, 389, 584, 278, 333, 278, 278, 556, 556, 556, 556, 556, 556, 556, 556, 556, 556, 278, 278, 584, 584, 584, 556, 1015, 667, 667, 722, 722, 667, 611, 778, 722, 278, 500, 667, 556, 833, 722, 778, 667, 778, 722, 667, 611, 722, 667, 944, 667, 667, 611, 278, 278, 278, 469, 556, 333, 556, 556, 500, 556, 556, 278, 556, 556, 222, 222, 500, 222, 833, 556, 556, 556, 556, 333, 500, 278, 556, 500, 722, 500, 500, 500, 334, 260, 334, 584, ];
#[rustfmt::skip]
const HELVETICA_BOLD: [u16; 95] = [
278, 333, 474, 556, 556, 889, 722, 238, 333, 333, 389, 584, 278, 333, 278, 278, 556, 556, 556, 556, 556, 556, 556, 556, 556, 556, 333, 333, 584, 584, 584, 611, 975, 722, 722, 722, 722, 667, 611, 778, 722, 278, 556, 722, 611, 833, 722, 778, 667, 778, 722, 667, 611, 722, 667, 944, 667, 667, 611, 333, 278, 333, 584, 556, 333, 556, 611, 556, 611, 556, 333, 611, 611, 278, 278, 556, 278, 889, 611, 611, 611, 611, 389, 556, 333, 611, 556, 778, 556, 556, 500, 389, 280, 389, 584, ];
const ASCENDER: f64 = 718.0;
const DESCENDER: f64 = 207.0;
pub struct Base14Metrics;
impl Base14Metrics {
fn widths(font: FontKey) -> &'static [u16; 95] {
match font {
FONT_BOLD => &HELVETICA_BOLD,
_ => &HELVETICA,
}
}
}
impl FontMetrics for Base14Metrics {
fn advance(&self, font: FontKey, c: char, size: Length) -> Option<Length> {
let code = c as u32;
if !(32..=126).contains(&code) {
return None;
}
let per_mille = Self::widths(font)[(code - 32) as usize] as f64;
Some(size * (per_mille / 1000.0))
}
fn ascender(&self, _font: FontKey, size: Length) -> Length {
size * (ASCENDER / 1000.0)
}
fn descender(&self, _font: FontKey, size: Length) -> Length {
size * (DESCENDER / 1000.0)
}
}