diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-01-29 16:14:04 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-01-29 17:44:50 +0100 |
| commit | 60dfe8f8931ca0a18f07e281bdd9751fe61e5adf (patch) | |
| tree | 5184c386d8ec779b9a91f3f2a7da80dab2f06aae /library/src/math/mod.rs | |
| parent | 8fbfa594e072aba384becaf5f4bd358bcc2ebc68 (diff) | |
Fix math styling
Diffstat (limited to 'library/src/math/mod.rs')
| -rw-r--r-- | library/src/math/mod.rs | 48 |
1 files changed, 28 insertions, 20 deletions
diff --git a/library/src/math/mod.rs b/library/src/math/mod.rs index f1bcd8f4..ab67a0d3 100644 --- a/library/src/math/mod.rs +++ b/library/src/math/mod.rs @@ -29,29 +29,27 @@ pub use self::root::*; pub use self::stack::*; pub use self::style::*; -use ttf_parser::GlyphId; -use ttf_parser::Rect; +use ttf_parser::{GlyphId, Rect}; use typst::font::Font; -use typst::model::{Guard, Module, Scope, SequenceNode}; +use typst::model::{Guard, Module, Scope, SequenceNode, StyledNode}; use unicode_math_class::MathClass; use self::ctx::*; use self::fragment::*; use self::row::*; use self::spacing::*; -use crate::layout::HNode; -use crate::layout::ParNode; -use crate::layout::Spacing; +use crate::layout::{HNode, ParNode, Spacing}; use crate::prelude::*; -use crate::text::LinebreakNode; -use crate::text::TextNode; -use crate::text::TextSize; -use crate::text::{families, variant, FallbackList, FontFamily, SpaceNode}; +use crate::text::{ + families, variant, FallbackList, FontFamily, LinebreakNode, SpaceNode, TextNode, + TextSize, +}; /// Create a module with all math definitions. pub fn module(sym: &Module) -> Module { let mut math = Scope::deduplicating(); math.def_func::<FormulaNode>("formula"); + math.def_func::<TextNode>("text"); // Grouping. math.def_func::<LrNode>("lr"); @@ -83,6 +81,7 @@ pub fn module(sym: &Module) -> Module { math.def_func::<RootNode>("root"); // Styles. + math.def_func::<UprightNode>("upright"); math.def_func::<BoldNode>("bold"); math.def_func::<ItalicNode>("italic"); math.def_func::<SerifNode>("serif"); @@ -243,6 +242,24 @@ impl LayoutMath for FormulaNode { impl LayoutMath for Content { fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> { + if let Some(node) = self.to::<SequenceNode>() { + for child in &node.0 { + child.layout_math(ctx)?; + } + return Ok(()); + } + + if let Some(styled) = self.to::<StyledNode>() { + let prev_map = std::mem::replace(&mut ctx.map, styled.map.clone()); + let prev_size = ctx.size; + ctx.map.apply(prev_map.clone()); + ctx.size = ctx.styles().get(TextNode::SIZE); + styled.sub.layout_math(ctx)?; + ctx.size = prev_size; + ctx.map = prev_map; + return Ok(()); + } + if self.is::<SpaceNode>() { ctx.push(MathFragment::Space); return Ok(()); @@ -256,9 +273,7 @@ impl LayoutMath for Content { if let Some(node) = self.to::<HNode>() { if let Spacing::Relative(rel) = node.amount { if rel.rel.is_zero() { - ctx.push(MathFragment::Spacing( - rel.abs.resolve(ctx.outer.chain(&ctx.map)), - )); + ctx.push(MathFragment::Spacing(rel.abs.resolve(ctx.styles()))); } } return Ok(()); @@ -269,13 +284,6 @@ impl LayoutMath for Content { return Ok(()); } - if let Some(node) = self.to::<SequenceNode>() { - for child in &node.0 { - child.layout_math(ctx)?; - } - return Ok(()); - } - if let Some(node) = self.with::<dyn LayoutMath>() { return node.layout_math(ctx); } |
