use ttf_parser::math::MathValue; use typst_library::foundations::{Style, StyleChain}; use typst_library::layout::{Abs, Em, FixedAlignment, Frame, Point, Size, VAlignment}; use typst_library::math::{EquationElem, MathSize}; use typst_library::text::TextElem; use typst_utils::LazyHash; use super::{LeftRightAlternator, MathContext, MathFragment, MathRun}; macro_rules! scaled { ($ctx:expr, $styles:expr, text: $text:ident, display: $display:ident $(,)?) => { match typst_library::math::EquationElem::size_in($styles) { typst_library::math::MathSize::Display => scaled!($ctx, $styles, $display), _ => scaled!($ctx, $styles, $text), } }; ($ctx:expr, $styles:expr, $name:ident) => { $crate::math::Scaled::scaled( $ctx.constants.$name(), $ctx, $crate::math::scaled_font_size($ctx, $styles), ) }; } macro_rules! percent { ($ctx:expr, $name:ident) => { $ctx.constants.$name() as f64 / 100.0 }; } /// How much less high scaled delimiters can be than what they wrap. pub const DELIM_SHORT_FALL: Em = Em::new(0.1); /// Converts some unit to an absolute length with the current font & font size. pub trait Scaled { fn scaled(self, ctx: &MathContext, font_size: Abs) -> Abs; } impl Scaled for i16 { fn scaled(self, ctx: &MathContext, font_size: Abs) -> Abs { ctx.font.to_em(self).at(font_size) } } impl Scaled for u16 { fn scaled(self, ctx: &MathContext, font_size: Abs) -> Abs { ctx.font.to_em(self).at(font_size) } } impl Scaled for MathValue<'_> { fn scaled(self, ctx: &MathContext, font_size: Abs) -> Abs { self.value.scaled(ctx, font_size) } } /// Get the font size scaled with the `MathSize`. pub fn scaled_font_size(ctx: &MathContext, styles: StyleChain) -> Abs { let factor = match EquationElem::size_in(styles) { MathSize::Display | MathSize::Text => 1.0, MathSize::Script => percent!(ctx, script_percent_scale_down), MathSize::ScriptScript => percent!(ctx, script_script_percent_scale_down), }; factor * TextElem::size_in(styles) } /// Styles something as cramped. pub fn style_cramped() -> LazyHash