use std::path::Path; use crate::math_expr::{MathExpr, MathFontFamilies, MathFontStyle}; use crate::{MathClass, MathStyle}; /// A Unicode mathematical alphabet selected by a TeX math font command. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum MathAlphabet { Bold, BoldItalic, BoldScript, BoldFraktur, Italic, Script, Fraktur, DoubleStruck, SansSerif, SansSerifBold, SansSerifItalic, SansSerifBoldItalic, } /// Selects a Unicode mathematical alphabet for a TeX math font style. pub fn for_font_style(font: MathFontStyle, bold: bool, italic: bool) -> Option { Some(match font { MathFontStyle::Bold => { if italic { MathAlphabet::BoldItalic } else { MathAlphabet::Bold } } MathFontStyle::BoldRoman => MathAlphabet::Bold, MathFontStyle::Italic => MathAlphabet::Italic, MathFontStyle::Calligraphic => { if bold { MathAlphabet::BoldScript } else { MathAlphabet::Script } } MathFontStyle::Fraktur => { if bold { MathAlphabet::BoldFraktur } else { MathAlphabet::Fraktur } } MathFontStyle::Blackboard => MathAlphabet::DoubleStruck, MathFontStyle::SansSerif => match (bold, italic) { (false, false) => MathAlphabet::SansSerif, (true, false) => MathAlphabet::SansSerifBold, (false, true) => MathAlphabet::SansSerifItalic, (true, true) => MathAlphabet::SansSerifBoldItalic, }, MathFontStyle::Monospace | MathFontStyle::Roman | MathFontStyle::Text => return None, }) } /// Applies a Unicode math alphabet to ordinary atoms, consulting the backend /// only for glyph availability in the selected family and default math font. pub fn transform_expression( expression: &MathExpr, alphabet: MathAlphabet, bold: bool, italic: bool, math_style: MathStyle, has_selected_font_glyph: impl Fn(&Path, char) -> bool, has_default_math_glyph: impl Fn(char) -> bool, ) -> MathExpr { fn transform( expression: &MathExpr, alphabet: MathAlphabet, bold: bool, italic: bool, math_style: MathStyle, has_selected_font_glyph: &impl Fn(&Path, char) -> bool, has_default_math_glyph: &impl Fn(char) -> bool, ) -> MathExpr { match expression { MathExpr::Spanned { location, expression, } => MathExpr::Spanned { location: location.clone(), expression: Box::new(transform( expression, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, )), }, MathExpr::Styled { .. } => expression.clone(), MathExpr::Classified { class, body } => MathExpr::Classified { class: *class, body: Box::new(transform( body, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, )), }, MathExpr::Phantom { kind, body } => MathExpr::Phantom { kind: *kind, body: Box::new(transform( body, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, )), }, MathExpr::Accent { position, body, family, math_fonts, } => MathExpr::Accent { position: *position, body: Box::new(transform( body, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, )), family: *family, math_fonts: math_fonts.clone(), }, MathExpr::Sequence(items) => MathExpr::Sequence( items .iter() .map(|item| { transform( item, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, ) }) .collect(), ), MathExpr::Alignment { rows, alignment, baseline, math_fonts, cell_style, row_spacing, } => MathExpr::Alignment { rows: rows .iter() .map(|row| { row.iter() .map(|item| { transform( item, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, ) }) .collect() }) .collect(), alignment: alignment.clone(), baseline: *baseline, math_fonts: math_fonts.clone(), cell_style: *cell_style, row_spacing: *row_spacing, }, MathExpr::Fraction { numerator, denominator, style, has_rule, rule_thickness, math_fonts, } => MathExpr::Fraction { numerator: Box::new(transform( numerator, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, )), denominator: Box::new(transform( denominator, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, )), style: *style, has_rule: *has_rule, rule_thickness: *rule_thickness, math_fonts: math_fonts.clone(), }, MathExpr::MathChoice { display, text, script, scriptscript, } => MathExpr::MathChoice { display: Box::new(transform( display, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, )), text: Box::new(transform( text, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, )), script: Box::new(transform( script, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, )), scriptscript: Box::new(transform( scriptscript, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, )), }, MathExpr::Root { index, body, radical, math_fonts, } => MathExpr::Root { index: index.as_deref().map(|index| { Box::new(transform( index, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, )) }), body: Box::new(transform( body, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, )), radical: *radical, math_fonts: math_fonts.clone(), }, MathExpr::Delimited { left, body, right, left_settings, right_settings, } => MathExpr::Delimited { left: left.clone(), body: Box::new(transform( body, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, )), right: right.clone(), left_settings: *left_settings, right_settings: *right_settings, }, MathExpr::Fenced { delimiters, delimiter_settings, segments, } => MathExpr::Fenced { delimiters: delimiters.clone(), delimiter_settings: delimiter_settings.clone(), segments: segments .iter() .map(|segment| { transform( segment, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, ) }) .collect(), }, MathExpr::Script { base, sub, superscript, limits, } => MathExpr::Script { base: Box::new(transform( base, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, )), sub: sub.as_deref().map(|sub| { Box::new(transform( sub, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, )) }), superscript: superscript.as_deref().map(|superscript| { Box::new(transform( superscript, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, )) }), limits: *limits, }, MathExpr::Atom { class: MathClass::Ordinary, text, math_family, math_slots, math_fonts, } => transform_atom( expression, text, *math_family, math_slots, math_fonts, alphabet, bold, italic, math_style, has_selected_font_glyph, has_default_math_glyph, ), MathExpr::Text(_) | MathExpr::TextStyled { .. } | MathExpr::Atom { .. } | MathExpr::MathSpacingChange(_) | MathExpr::MathLineBreakPenaltiesChange(_) | MathExpr::BreakPenalty(_) | MathExpr::MathStyleChange(_) | MathExpr::Space(_) | MathExpr::Glue(_) | MathExpr::Kern(_) | MathExpr::SizedDelimiter { .. } | MathExpr::UnicodeDelimiter { .. } => expression.clone(), } } transform( expression, alphabet, bold, italic, math_style, &has_selected_font_glyph, &has_default_math_glyph, ) } fn transform_atom( original: &MathExpr, text: &str, math_family: Option, math_slots: &[u8], math_fonts: &MathFontFamilies, alphabet: MathAlphabet, bold: bool, italic: bool, math_style: MathStyle, has_selected_font_glyph: &impl Fn(&Path, char) -> bool, has_default_math_glyph: &impl Fn(char) -> bool, ) -> MathExpr { let glyph_text = crate::math_glyph::math_atom_glyph_text(text, math_family, math_slots); let characters = glyph_text.chars().collect::>(); let has_aligned_slots = math_family.is_some() && math_slots.len() == characters.len(); let selected_font = math_family .and_then(|family| math_fonts.get(&family)) .and_then(|selection| selection.for_style(math_style)); let mut transformed = false; let mut items = Vec::with_capacity(characters.len()); for (index, character) in characters.into_iter().enumerate() { let alphabet_character = self::character(character, alphabet); let mapped = alphabet_character.and_then(|mapped| { if selected_font.is_some_and(|path| has_selected_font_glyph(path.as_path(), mapped)) { Some((mapped, true)) } else if has_default_math_glyph(mapped) { Some((mapped, false)) } else { None } }); if let Some((mapped, use_selected_font)) = mapped { transformed = true; items.push(MathExpr::Atom { class: MathClass::Ordinary, text: mapped.to_string(), math_family: use_selected_font.then_some(math_family).flatten(), math_slots: Vec::new(), math_fonts: math_fonts.clone(), }); } else if alphabet_character.is_some() { transformed = true; items.push(MathExpr::TextStyled { text: character.to_string(), bold, italic, monospace: false, }); } else { items.push(MathExpr::Atom { class: MathClass::Ordinary, text: character.to_string(), math_family, math_slots: if has_aligned_slots { vec![math_slots[index]] } else { Vec::new() }, math_fonts: math_fonts.clone(), }); } } if !transformed { return original.clone(); } match items.len() { 0 => MathExpr::Sequence(Vec::new()), 1 => items.pop().expect("one alphabet atom was built"), _ => MathExpr::Sequence(items), } } /// Maps a TeX math letter or supported Greek symbol to its Unicode alphabet form. pub fn character(character: char, alphabet: MathAlphabet) -> Option { let codepoint = match character { 'A'..='Z' => { let offset = u32::from(character) - u32::from('A'); match alphabet { MathAlphabet::Bold => 0x1D400 + offset, MathAlphabet::BoldItalic => 0x1D468 + offset, MathAlphabet::Italic => 0x1D434 + offset, MathAlphabet::Script => match character { 'B' => 0x212C, 'E' => 0x2130, 'F' => 0x2131, 'H' => 0x210B, 'I' => 0x2110, 'L' => 0x2112, 'M' => 0x2133, 'R' => 0x211B, _ => 0x1D49C + offset, }, MathAlphabet::BoldScript => 0x1D4D0 + offset, MathAlphabet::Fraktur => match character { 'C' => 0x212D, 'H' => 0x210C, 'I' => 0x2111, 'R' => 0x211C, _ => 0x1D504 + offset, }, MathAlphabet::BoldFraktur => 0x1D56C + offset, MathAlphabet::DoubleStruck => match character { 'C' => 0x2102, 'H' => 0x210D, 'N' => 0x2115, 'P' => 0x2119, 'Q' => 0x211A, 'R' => 0x211D, 'Z' => 0x2124, _ => 0x1D538 + offset, }, MathAlphabet::SansSerif => 0x1D5A0 + offset, MathAlphabet::SansSerifBold => 0x1D5D4 + offset, MathAlphabet::SansSerifItalic => 0x1D608 + offset, MathAlphabet::SansSerifBoldItalic => 0x1D63C + offset, } } 'a'..='z' => { let offset = u32::from(character) - u32::from('a'); match alphabet { MathAlphabet::Bold => 0x1D41A + offset, MathAlphabet::BoldItalic => 0x1D482 + offset, MathAlphabet::Italic if character == 'h' => 0x210E, MathAlphabet::Italic => 0x1D44E + offset, MathAlphabet::Script => match character { 'e' => 0x212F, 'g' => 0x210A, 'l' => 0x2113, 'o' => 0x2134, _ => 0x1D4B6 + offset, }, MathAlphabet::BoldScript => 0x1D4EA + offset, MathAlphabet::Fraktur => 0x1D51E + offset, MathAlphabet::BoldFraktur => 0x1D586 + offset, MathAlphabet::DoubleStruck => 0x1D552 + offset, MathAlphabet::SansSerif => 0x1D5BA + offset, MathAlphabet::SansSerifBold => 0x1D5EE + offset, MathAlphabet::SansSerifItalic => 0x1D622 + offset, MathAlphabet::SansSerifBoldItalic => 0x1D656 + offset, } } '0'..='9' => { let offset = u32::from(character) - u32::from('0'); match alphabet { MathAlphabet::Bold => 0x1D7CE + offset, MathAlphabet::DoubleStruck => 0x1D7D8 + offset, MathAlphabet::SansSerif => 0x1D7E2 + offset, MathAlphabet::SansSerifBold => 0x1D7EC + offset, _ => return None, } } _ => return greek_math_alphabet_character(character, alphabet), }; char::from_u32(codepoint) } fn greek_math_alphabet_character(character: char, alphabet: MathAlphabet) -> Option { let style_base = match alphabet { MathAlphabet::Bold => 0x1D6A8, MathAlphabet::Italic => 0x1D6E2, MathAlphabet::BoldItalic => 0x1D71C, MathAlphabet::SansSerifBold => 0x1D756, MathAlphabet::SansSerifBoldItalic => 0x1D790, MathAlphabet::BoldScript | MathAlphabet::BoldFraktur | MathAlphabet::Script | MathAlphabet::Fraktur | MathAlphabet::DoubleStruck | MathAlphabet::SansSerif | MathAlphabet::SansSerifItalic => return None, }; let offset = match character as u32 { 0x0391..=0x03A1 => character as u32 - 0x0391, 0x03A3..=0x03A9 => character as u32 - 0x03A3 + 18, 0x03F4 => 17, 0x2207 => 25, 0x03B1..=0x03C1 => 26 + character as u32 - 0x03B1, 0x03C2 => 43, 0x03C3..=0x03C9 => 44 + character as u32 - 0x03C3, 0x2202 => 51, 0x03F5 => 52, 0x03D1 => 53, 0x03F0 => 54, 0x03D5 => 55, 0x03F1 => 56, 0x03D6 => 57, _ => return None, }; char::from_u32(style_base + offset) }