//! Mathematical formulas. use crate::library::prelude::*; use crate::library::text::FontFamily; /// A mathematical formula. #[derive(Debug, Hash)] pub struct MathNode { /// The formula. pub formula: EcoString, /// Whether the formula is display-level. pub display: bool, } #[node(showable)] impl MathNode { /// The raw text's font family. Just the normal text family if `auto`. #[property(referenced)] pub const FAMILY: Smart = Smart::Custom(FontFamily::new("Latin Modern Math")); fn construct(_: &mut Context, args: &mut Args) -> TypResult { Ok(Content::show(Self { formula: args.expect("formula")?, display: args.named("display")?.unwrap_or(false), })) } } impl Show for MathNode { fn encode(&self) -> Dict { dict! { "formula" => Value::Str(self.formula.clone()), "display" => Value::Bool(self.display) } } fn realize(&self, _: &mut Context, _: StyleChain) -> TypResult { Ok(Content::Text(self.formula.trim().into())) } fn finalize( &self, _: &mut Context, styles: StyleChain, mut realized: Content, ) -> TypResult { let mut map = StyleMap::new(); if let Smart::Custom(family) = styles.get(Self::FAMILY) { map.set_family(family.clone(), styles); } realized = realized.styled_with_map(map); if self.display { realized = Content::block(realized); } Ok(realized) } }