use super::encoding::glyph_name_to_unicode;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Family {
Helvetica = 0,
HelveticaBold = 1,
TimesRoman = 2,
TimesBold = 3,
TimesItalic = 4,
TimesBoldItalic = 5,
Courier,
Other,
}
#[rustfmt::skip]
const ASCII: [[u16; 6]; 95] = [
[278, 278, 250, 250, 250, 250],
[278, 333, 333, 333, 333, 389],
[355, 474, 408, 555, 420, 555],
[556, 556, 500, 500, 500, 500],
[556, 556, 500, 500, 500, 500],
[889, 889, 833, 1000, 833, 833],
[667, 722, 778, 833, 778, 778],
[191, 238, 180, 278, 214, 278],
[333, 333, 333, 333, 333, 333],
[333, 333, 333, 333, 333, 333],
[389, 389, 500, 500, 500, 500],
[584, 584, 564, 570, 675, 570],
[278, 278, 250, 250, 250, 250],
[333, 333, 333, 333, 333, 333],
[278, 278, 250, 250, 250, 250],
[278, 278, 278, 278, 278, 278],
[556, 556, 500, 500, 500, 500],
[556, 556, 500, 500, 500, 500],
[556, 556, 500, 500, 500, 500],
[556, 556, 500, 500, 500, 500],
[556, 556, 500, 500, 500, 500],
[556, 556, 500, 500, 500, 500],
[556, 556, 500, 500, 500, 500],
[556, 556, 500, 500, 500, 500],
[556, 556, 500, 500, 500, 500],
[556, 556, 500, 500, 500, 500],
[278, 333, 278, 333, 333, 333],
[278, 333, 278, 333, 333, 333],
[584, 584, 564, 570, 675, 570],
[584, 584, 564, 570, 675, 570],
[584, 584, 564, 570, 675, 570],
[556, 611, 444, 500, 500, 500],
[1015, 975, 921, 930, 920, 832],
[667, 722, 722, 722, 611, 667],
[667, 722, 667, 667, 611, 667],
[722, 722, 667, 722, 667, 667],
[722, 722, 722, 722, 722, 722],
[667, 667, 611, 667, 611, 667],
[611, 611, 556, 611, 611, 667],
[778, 778, 722, 778, 722, 722],
[722, 722, 722, 778, 722, 778],
[278, 278, 333, 389, 333, 389],
[500, 556, 389, 500, 444, 500],
[667, 722, 722, 778, 667, 667],
[556, 611, 611, 667, 556, 611],
[833, 833, 889, 944, 833, 889],
[722, 722, 722, 722, 667, 722],
[778, 778, 722, 778, 722, 722],
[667, 667, 556, 611, 611, 611],
[778, 778, 722, 778, 722, 722],
[722, 722, 667, 722, 611, 667],
[667, 667, 556, 556, 500, 556],
[611, 611, 611, 667, 556, 611],
[722, 722, 722, 722, 722, 722],
[667, 667, 722, 722, 611, 667],
[944, 944, 944, 1000, 833, 889],
[667, 667, 722, 722, 611, 667],
[667, 667, 722, 722, 556, 611],
[611, 611, 611, 667, 556, 611],
[278, 333, 333, 333, 389, 333],
[278, 278, 278, 278, 278, 278],
[278, 333, 333, 333, 389, 333],
[469, 584, 469, 581, 422, 570],
[556, 556, 500, 500, 500, 500],
[333, 333, 333, 333, 333, 333],
[556, 556, 444, 500, 500, 500],
[556, 611, 500, 556, 500, 500],
[500, 556, 444, 444, 444, 444],
[556, 611, 500, 556, 500, 500],
[556, 556, 444, 444, 444, 444],
[278, 333, 333, 333, 278, 333],
[556, 611, 500, 500, 500, 500],
[556, 611, 500, 556, 500, 556],
[222, 278, 278, 278, 278, 278],
[222, 278, 278, 333, 278, 278],
[500, 556, 500, 556, 444, 500],
[222, 278, 278, 278, 278, 278],
[833, 889, 778, 833, 722, 778],
[556, 611, 500, 556, 500, 556],
[556, 611, 500, 500, 500, 500],
[556, 611, 500, 556, 500, 500],
[556, 611, 500, 556, 500, 500],
[333, 389, 333, 444, 389, 389],
[500, 556, 389, 389, 389, 389],
[278, 333, 278, 333, 278, 278],
[556, 611, 500, 556, 500, 556],
[500, 556, 500, 500, 444, 444],
[722, 778, 722, 722, 667, 667],
[500, 556, 500, 500, 444, 500],
[500, 556, 500, 500, 444, 444],
[500, 500, 444, 444, 389, 389],
[334, 389, 480, 394, 400, 348],
[260, 280, 200, 220, 275, 220],
[334, 389, 480, 394, 400, 348],
[584, 584, 541, 520, 541, 570],
];
fn family_of(base_font: &str) -> Family {
let name = base_font.split('+').next_back().unwrap_or(base_font);
let lower = name.to_ascii_lowercase();
let bold = lower.contains("bold") || lower.contains("black") || lower.contains("heavy");
let italic = lower.contains("italic") || lower.contains("oblique");
if lower.contains("courier") || lower.contains("mono") {
Family::Courier
} else if lower.contains("times") || lower.contains("serif") || lower.contains("roman") {
match (bold, italic) {
(true, true) => Family::TimesBoldItalic,
(true, false) => Family::TimesBold,
(false, true) => Family::TimesItalic,
(false, false) => Family::TimesRoman,
}
} else if lower.contains("symbol") || lower.contains("dingbat") || lower.contains("zapf") {
Family::Other
} else {
if bold {
Family::HelveticaBold
} else {
Family::Helvetica
}
}
}
pub fn base14_width(base_font: &str, glyph_name: &str) -> Option<f32> {
let family = family_of(base_font);
if family == Family::Courier {
return Some(600.0);
}
let column = match family {
Family::Other => return None,
Family::Courier => unreachable!("handled above"),
f => f as usize,
};
let text = glyph_name_to_unicode(glyph_name)?;
let mut chars = text.chars();
let ch = chars.next()?;
if chars.next().is_some() {
return None; }
let code = u32::from(ch);
if !(32..=126).contains(&code) {
return None;
}
Some(f32::from(ASCII[code as usize - 32][column]))
}
pub fn is_standard_font(base_font: &str) -> bool {
let name = base_font.split('+').next_back().unwrap_or(base_font);
let lower = name.to_ascii_lowercase();
[
"helvetica",
"courier",
"times",
"symbol",
"zapfdingbats",
"arial",
]
.iter()
.any(|f| lower.contains(f))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn helvetica_ascii_widths() {
assert_eq!(base14_width("Helvetica", "space"), Some(278.0));
assert_eq!(base14_width("Helvetica", "A"), Some(667.0));
assert_eq!(base14_width("Helvetica", "i"), Some(222.0));
assert_eq!(base14_width("Helvetica", "W"), Some(944.0));
}
#[test]
fn bold_and_italic_select_columns() {
assert_eq!(base14_width("Helvetica-Bold", "A"), Some(722.0));
assert_eq!(base14_width("Times-Roman", "A"), Some(722.0));
assert_eq!(base14_width("Times-Italic", "A"), Some(611.0));
assert_eq!(base14_width("Times-BoldItalic", "A"), Some(667.0));
assert_eq!(
base14_width("Helvetica-Oblique", "A"),
base14_width("Helvetica", "A")
);
}
#[test]
fn courier_is_monospaced() {
assert_eq!(base14_width("Courier", "A"), Some(600.0));
assert_eq!(base14_width("Courier-BoldOblique", "i"), Some(600.0));
}
#[test]
fn subset_prefixes_and_comma_styles_resolve() {
assert_eq!(base14_width("ABCDEF+Times-Bold", "A"), Some(722.0));
assert_eq!(base14_width("Arial,Bold", "A"), Some(722.0));
}
#[test]
fn untabulated_names_report_nothing() {
assert_eq!(base14_width("Helvetica", "eacute"), None);
assert_eq!(base14_width("Helvetica", "fi"), None);
assert_eq!(base14_width("Symbol", "alpha"), None);
}
#[test]
fn standard_font_detection() {
assert!(is_standard_font("ABCDEF+Helvetica-Bold"));
assert!(is_standard_font("Times-Roman"));
assert!(!is_standard_font("MinionPro-Regular"));
}
}