use crate::quickdraw::fonts::{
get_font_face_or_default, get_italic_glyph as get_italic_glyph_fn, get_macroman_glyph,
override_format, FontMetrics, Glyph,
};
pub fn get_font_metrics(font_id: i16, size: i16) -> FontMetrics {
get_font_face_or_default(font_id, size).metrics
}
pub fn get_glyph(font_id: i16, size: i16, ch: char) -> Option<(&'static Glyph, &'static [u8])> {
let face = get_font_face_or_default(font_id, size);
let glyphs = face.glyphs;
let data = face.data;
if (' '..='~').contains(&ch) {
let idx = (ch as usize) - 32;
if idx < glyphs.len() {
let glyph = &glyphs[idx];
if glyph.width != 0 || glyph.height != 0 || glyph.advance != 0 {
return Some((glyph, data));
}
}
return None;
}
let mac_code = ch as u32;
if (0x80..=0xFF).contains(&mac_code) {
return macroman_or_ascii_fallback(font_id, size, mac_code as u8);
}
if ch == '\u{2318}' {
if let Some(hit) =
override_symbol_glyph(font_id, size, override_format::COMMAND_SYMBOL_GLYPH_INDEX)
{
return Some(hit);
}
return get_macroman_glyph(font_id, size, 0x11);
}
if ch == '\u{2713}' {
if let Some(hit) =
override_symbol_glyph(font_id, size, override_format::CHECKMARK_SYMBOL_GLYPH_INDEX)
{
return Some(hit);
}
return get_macroman_glyph(font_id, size, 0x12);
}
if ch == '\u{14}' || ch == '\u{F8FF}' {
if let Some(hit) =
override_symbol_glyph(font_id, size, override_format::APPLE_SYMBOL_GLYPH_INDEX)
{
return Some(hit);
}
return get_macroman_glyph(font_id, size, 0x14);
}
None
}
fn override_symbol_glyph(
font_id: i16,
size: i16,
index: usize,
) -> Option<(&'static Glyph, &'static [u8])> {
let face = get_font_face_or_default(font_id, size);
let glyph = face.glyphs.get(index)?;
if glyph.width == 0 && glyph.height == 0 && glyph.advance == 0 {
return None;
}
Some((glyph, face.data))
}
fn macroman_or_ascii_fallback(
font_id: i16,
size: i16,
mac_code: u8,
) -> Option<(&'static Glyph, &'static [u8])> {
if let Some(hit) = get_macroman_glyph(font_id, size, mac_code) {
return Some(hit);
}
let ascii_fallback: char = match mac_code {
0xD0 | 0xD1 => '-', 0xD2 | 0xD3 => '"', 0xD4 | 0xD5 => '\'', 0xA5 => '*', 0xCA => ' ', 0xE1 | 0xE5 => '.', _ => return None,
};
get_glyph(font_id, size, ascii_fallback)
}
pub fn get_glyph_italic(
font_id: i16,
size: i16,
ch: char,
) -> Option<(&'static Glyph, &'static [u8])> {
get_italic_glyph_fn(font_id, size, ch)
}
pub fn get_underline_thickness(_font_id: i16, _size: i16) -> i16 {
1
}