use crate::quickdraw::fonts::{
get_font_face_or_default, get_italic_glyph as get_italic_glyph_fn, get_macroman_glyph,
override_format, FontMetrics, Glyph,
};
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(crate) struct QuickDrawTextStyle(u8);
impl QuickDrawTextStyle {
pub(crate) const BOLD_BIT: u8 = 0x01;
pub(crate) const ITALIC_BIT: u8 = 0x02;
pub(crate) const UNDERLINE_BIT: u8 = 0x04;
pub(crate) const OUTLINE_BIT: u8 = 0x08;
pub(crate) const SHADOW_BIT: u8 = 0x10;
pub(crate) const CONDENSE_BIT: u8 = 0x20;
pub(crate) const EXTEND_BIT: u8 = 0x40;
const EFFECT_BITS: u8 = Self::BOLD_BIT
| Self::ITALIC_BIT
| Self::UNDERLINE_BIT
| Self::OUTLINE_BIT
| Self::SHADOW_BIT
| Self::CONDENSE_BIT
| Self::EXTEND_BIT;
const PER_GLYPH_EFFECT_BITS: u8 = Self::EFFECT_BITS & !Self::UNDERLINE_BIT;
pub(crate) const fn from_bits(bits: u8) -> Self {
Self(bits & Self::EFFECT_BITS)
}
pub(crate) const fn plain() -> Self {
Self(0)
}
pub(crate) const fn is_plain(self) -> bool {
self.0 == 0
}
pub(crate) const fn has_per_glyph_effect(self) -> bool {
self.0 & Self::PER_GLYPH_EFFECT_BITS != 0
}
pub(crate) const fn bold(self) -> bool {
self.0 & Self::BOLD_BIT != 0
}
pub(crate) const fn italic(self) -> bool {
self.0 & Self::ITALIC_BIT != 0
}
pub(crate) const fn underline(self) -> bool {
self.0 & Self::UNDERLINE_BIT != 0
}
pub(crate) const fn outline(self) -> bool {
self.0 & Self::OUTLINE_BIT != 0
}
pub(crate) const fn shadow(self) -> bool {
self.0 & Self::SHADOW_BIT != 0
}
pub(crate) const fn condensed(self) -> bool {
self.0 & Self::CONDENSE_BIT != 0
}
pub(crate) const fn extended(self) -> bool {
self.0 & Self::EXTEND_BIT != 0
}
pub(crate) fn glyph_advance(self, glyph_advance: i32) -> i32 {
let mut advance = glyph_advance;
if self.bold() {
advance += 1;
}
if self.outline() {
advance += 1;
}
if self.shadow() {
advance += 2;
}
if self.condensed() && advance >= 6 {
advance -= 1;
}
if self.extended() {
advance += 1;
}
advance.max(1)
}
pub(crate) const fn glyph_y_offset(self) -> i32 {
if self.shadow() {
-1
} else {
0
}
}
pub(crate) const fn smear_max(self) -> Option<i32> {
if self.shadow() && self.outline() {
Some(3)
} else if self.shadow() {
Some(2)
} else if self.outline() {
Some(1)
} else {
None
}
}
}
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);
}
if let Some(hit) = crate::quickdraw::fonts::pixel_font::menu_symbols::get_glyph(ch) {
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);
}
if let Some(hit) = crate::quickdraw::fonts::pixel_font::menu_symbols::get_glyph(ch) {
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);
}
if let Some(mac_code @ 0x80..=0xFF) = crate::mac_roman::encode_mac_roman_char(ch) {
return macroman_or_ascii_fallback(font_id, size, mac_code);
}
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);
}
if mac_code == 0xAA {
return crate::quickdraw::fonts::pixel_font::menu_symbols::get_glyph('\u{2122}');
}
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
}
#[cfg(test)]
mod tests {
use super::{get_glyph, QuickDrawTextStyle};
#[test]
fn quickdraw_style_plan_combines_all_low_order_face_bits() {
let style = QuickDrawTextStyle::from_bits(0xff);
assert!(style.bold());
assert!(style.italic());
assert!(style.underline());
assert!(style.outline());
assert!(style.shadow());
assert!(style.condensed());
assert!(style.extended());
assert_eq!(style.glyph_y_offset(), -1);
assert_eq!(style.smear_max(), Some(3));
assert_eq!(style.glyph_advance(6), 10);
assert!(QuickDrawTextStyle::from_bits(0x80).is_plain());
}
#[test]
fn built_in_system_font_renders_menu_symbols() {
for (symbol, name) in [('\u{2318}', "Command"), ('\u{2713}', "checkmark")] {
let (glyph, data) = get_glyph(0, 12, symbol)
.unwrap_or_else(|| panic!("{name} symbol should resolve to a bitmap glyph"));
let glyph_len = usize::from(glyph.width) * usize::from(glyph.height);
assert!(
data[glyph.data_offset..glyph.data_offset + glyph_len]
.iter()
.any(|pixel| *pixel != 0),
"{name} symbol should contain visible pixels"
);
}
}
#[test]
fn unicode_hle_text_uses_mac_roman_extended_glyphs() {
let (glyph, data) = get_glyph(0, 12, '™').expect("Mac Roman trademark glyph");
let glyph_len = usize::from(glyph.width) * usize::from(glyph.height);
assert!(data[glyph.data_offset..glyph.data_offset + glyph_len]
.iter()
.any(|pixel| *pixel != 0));
}
}