use crate::MathClass; /// The corner of a math glyph used to query an OpenType MATH kern. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum MathKernCorner { TopRight, TopLeft, BottomRight, BottomLeft, } /// The slant selected by TeX's ordinary math-atom family rules. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum MathAtomFontShape { Inherited, Upright, Italic, } /// Selects the font shape for a math atom before backend style conversion. pub fn math_atom_font_shape(class: MathClass, math_family: Option) -> MathAtomFontShape { if class != MathClass::Ordinary { return MathAtomFontShape::Upright; } match math_family { Some(1) => MathAtomFontShape::Italic, Some(_) => MathAtomFontShape::Upright, None => MathAtomFontShape::Inherited, } } /// Returns the characters represented by a math atom's stored source and slots. pub fn math_atom_glyph_text(text: &str, math_family: Option, math_slots: &[u8]) -> String { if math_family.is_some() && math_slots.len() == text.chars().count() && math_slots.iter().all(|slot| (b' '..=b'~').contains(slot)) { math_slots.iter().map(|slot| char::from(*slot)).collect() } else { text.to_string() } } /// Whether this atom's character is selected from the OpenType math face. pub fn prefers_math_font( class: MathClass, character: char, math_family: Option, bold: bool, italic: bool, ) -> bool { let greek = is_greek_letter(character); let unicode_math_alphanumeric = is_math_alphanumeric_character(character); let is_math_symbol = !character.is_alphanumeric() || unicode_math_alphanumeric; let is_math_italic_letter = class == MathClass::Ordinary && character.is_ascii_alphabetic() && (math_family == Some(1) || italic); (is_math_symbol || greek || is_math_italic_letter) && (!bold || unicode_math_alphanumeric) && (!italic || !character.is_alphanumeric() || greek || unicode_math_alphanumeric || is_math_italic_letter) } fn is_math_alphanumeric_character(character: char) -> bool { matches!(character as u32, 0x1D400..=0x1D7FF) || matches!( character as u32, 0x2102 | 0x210A..=0x210E | 0x2110..=0x2115 | 0x2119..=0x211D | 0x2124 | 0x212C..=0x212F | 0x2130..=0x2131 | 0x2133..=0x2134 ) } fn is_greek_letter(character: char) -> bool { character.is_alphabetic() && matches!(character as u32, 0x0370..=0x03FF | 0x1F00..=0x1FFF) } /// Engine-owned kern breakpoints and values, measured in font design units. #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct MathKernTable { pub correction_heights: Vec>, pub values: Vec>, } /// MATH metrics for one glyph, independent of the font parser's table types. #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct MathGlyphMetrics { /// Italic correction. `Some(0)` means a correction table was present but /// had no explicit entry for this glyph. pub italic_correction: Option, pub top_accent_attachment: Option, pub top_right_kern: Option, pub top_left_kern: Option, pub bottom_right_kern: Option, pub bottom_left_kern: Option, } /// Return a glyph's MATH italic correction in design units. pub fn italic_correction(metrics: &MathGlyphMetrics) -> Option { metrics.italic_correction } /// Return a glyph's top accent attachment point in design units. pub fn top_accent_attachment(metrics: &MathGlyphMetrics) -> Option { metrics.top_accent_attachment } /// Select the MATH kern value for a corner and correction height. /// /// `correction_height` is expressed in the font's design units. OpenType kern /// tables select the value following every breakpoint at or below that height. pub fn math_kern_at_height( metrics: &MathGlyphMetrics, corner: MathKernCorner, correction_height: f64, ) -> Option { let kern = match corner { MathKernCorner::TopRight => metrics.top_right_kern.as_ref(), MathKernCorner::TopLeft => metrics.top_left_kern.as_ref(), MathKernCorner::BottomRight => metrics.bottom_right_kern.as_ref(), MathKernCorner::BottomLeft => metrics.bottom_left_kern.as_ref(), }?; let kern_index = kern .correction_heights .iter() .take_while(|height| height.is_some_and(|height| f64::from(height) <= correction_height)) .count(); kern.values.get(kern_index).copied().flatten() }