1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
//! 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<FontFamily> =
Smart::Custom(FontFamily::new("Latin Modern Math"));
fn construct(_: &mut Context, args: &mut Args) -> TypResult<Content> {
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<Content> {
Ok(Content::Text(self.formula.trim().into()))
}
fn finalize(
&self,
_: &mut Context,
styles: StyleChain,
mut realized: Content,
) -> TypResult<Content> {
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)
}
}
|