diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-11-08 11:45:54 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-11-08 11:45:59 +0100 |
| commit | a7a4cae2948176119e8995bd8e1868f2d0e65029 (patch) | |
| tree | 123e8867c939373c1c5ce5a0d20f55192c2467c2 /library/src/math | |
| parent | 0a41844cc4e645e87fe48aa31ed3a4fd40a6ab11 (diff) | |
Less style properties
Diffstat (limited to 'library/src/math')
| -rw-r--r-- | library/src/math/mod.rs | 30 | ||||
| -rw-r--r-- | library/src/math/tex.rs | 19 |
2 files changed, 26 insertions, 23 deletions
diff --git a/library/src/math/mod.rs b/library/src/math/mod.rs index a89b4953..ae3c4a9a 100644 --- a/library/src/math/mod.rs +++ b/library/src/math/mod.rs @@ -7,7 +7,7 @@ use std::fmt::Write; use self::tex::{layout_tex, Texify}; use crate::layout::BlockSpacing; use crate::prelude::*; -use crate::text::FontFamily; +use crate::text::{FallbackList, FontFamily, TextNode}; /// A piece of a mathematical formula. #[derive(Debug, Clone, Hash)] @@ -20,9 +20,6 @@ pub struct MathNode { #[node(Show, Finalize, LayoutInline, Texify)] impl MathNode { - /// The math font family. - #[property(referenced)] - pub const FAMILY: FontFamily = FontFamily::new("NewComputerModernMath"); /// The spacing above display math. #[property(resolve, shorthand(around))] pub const ABOVE: Option<BlockSpacing> = Some(Ratio::one().into()); @@ -44,11 +41,7 @@ impl Show for MathNode { } fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> { - Ok(if self.display { - self.clone().pack().aligned(Axes::with_x(Some(Align::Center.into()))) - } else { - self.clone().pack() - }) + Ok(self.clone().pack()) } } @@ -57,13 +50,20 @@ impl Finalize for MathNode { &self, _: Tracked<dyn World>, styles: StyleChain, - realized: Content, + mut realized: Content, ) -> SourceResult<Content> { - Ok(if self.display { - realized.spaced(styles.get(Self::ABOVE), styles.get(Self::BELOW)) - } else { - realized - }) + realized = realized.styled( + TextNode::FAMILY, + FallbackList(vec![FontFamily::new("NewComputerModernMath")]), + ); + + if self.display { + realized = realized + .aligned(Axes::with_x(Some(Align::Center.into()))) + .spaced(styles.get(Self::ABOVE), styles.get(Self::BELOW)) + } + + Ok(realized) } } diff --git a/library/src/math/tex.rs b/library/src/math/tex.rs index 7de68d7b..f924ebbe 100644 --- a/library/src/math/tex.rs +++ b/library/src/math/tex.rs @@ -5,9 +5,8 @@ use rex::parser::color::RGBA; use rex::render::{Backend, Cursor, Renderer}; use typst::font::Font; -use super::MathNode; use crate::prelude::*; -use crate::text::{variant, LinebreakNode, SpaceNode, TextNode}; +use crate::text::{families, variant, LinebreakNode, SpaceNode, TextNode}; /// Turn a math node into TeX math code. #[capability] @@ -42,17 +41,21 @@ pub fn layout_tex( styles: StyleChain, ) -> SourceResult<Frame> { // Load the font. - let font = world - .book() - .select(styles.get(MathNode::FAMILY).as_str(), variant(styles)) - .and_then(|id| world.font(id)) - .expect("failed to find math font"); + let variant = variant(styles); + let mut font = None; + for family in families(styles) { + font = world.book().select(family, variant).and_then(|id| world.font(id)); + if font.as_ref().map_or(false, |font| font.math().is_some()) { + break; + } + } // Prepare the font context. + let font = font.expect("failed to find suitable math font"); let ctx = font .math() .map(|math| FontContext::new(font.ttf(), math)) - .expect("font is not suitable for math"); + .expect("failed to create font context"); // Layout the formula. let em = styles.get(TextNode::SIZE); |
