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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
//! Mathematical formulas.
#[macro_use]
mod ctx;
mod accent;
mod align;
mod atom;
mod braced;
mod frac;
mod fragment;
mod lr;
mod matrix;
mod op;
mod root;
mod row;
mod script;
mod spacing;
mod stretch;
mod style;
mod symbols;
pub use self::accent::*;
pub use self::align::*;
pub use self::atom::*;
pub use self::braced::*;
pub use self::frac::*;
pub use self::lr::*;
pub use self::matrix::*;
pub use self::op::*;
pub use self::root::*;
pub use self::script::*;
pub use self::style::*;
use ttf_parser::GlyphId;
use ttf_parser::Rect;
use typst::font::Font;
use typst::model::{Guard, Module, Scope, SequenceNode};
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::prelude::*;
use crate::text::LinebreakNode;
use crate::text::TextNode;
use crate::text::TextSize;
use crate::text::{families, variant, FallbackList, FontFamily, SpaceNode};
/// 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::<LrNode>("lr");
math.def_func::<OpNode>("op");
math.def_func::<FloorFunc>("floor");
math.def_func::<CeilFunc>("ceil");
math.def_func::<AbsFunc>("abs");
math.def_func::<NormFunc>("norm");
math.def_func::<AccentNode>("accent");
math.def_func::<FracNode>("frac");
math.def_func::<BinomNode>("binom");
math.def_func::<ScriptNode>("script");
math.def_func::<SqrtNode>("sqrt");
math.def_func::<RootNode>("root");
math.def_func::<VecNode>("vec");
math.def_func::<CasesNode>("cases");
math.def_func::<UnderbraceNode>("underbrace");
math.def_func::<OverbraceNode>("overbrace");
math.def_func::<BoldNode>("bold");
math.def_func::<ItalicNode>("italic");
math.def_func::<SerifNode>("serif");
math.def_func::<SansNode>("sans");
math.def_func::<CalNode>("cal");
math.def_func::<FrakNode>("frak");
math.def_func::<MonoNode>("mono");
math.def_func::<BbNode>("bb");
spacing::define(&mut math);
symbols::define(&mut math);
op::define(&mut math);
math.copy_from(sym.scope());
Module::new("math").with_scope(math)
}
/// # Formula
/// A mathematical formula.
///
/// ## Syntax
/// This function also has dedicated syntax: Write mathematical markup within
/// dollar signs to create a formula. Starting and ending the formula with at
/// least one space lifts it into a separate block that is centered
/// horizontally.
///
/// In math, single letters are always displayed as is. Multiple letters,
/// however, are interpreted as variables, symbols or functions. To display
/// multiple letters verbatim, you can place them into quotes. Math mode also
/// supports extra shorthands to easily type various arrows and other symbols.
/// The page on the [`symbol`](@symbol) function lists all of them.
///
/// When a variable and a symbol share the same name, the variable is preferred.
/// To force the symbol, surround it with colons. To access a variable with a
/// single letter name, you can prefix it with a `#`.
///
/// In math mode, the arguments to a function call are always parsed as
/// mathematical content. To work with other kinds of values, you first need to
/// enter a code block using the `[$#{..}$]` syntax.
///
/// ## Example
/// ```
/// #set text("Latin Modern Roman")
///
/// Let $a$, $b$, and $c$ be the side
/// lengths of right-angled triangle.
/// Then, we know that:
/// $ a^2 + b^2 = c^2 $
///
/// Prove by induction:
/// $ sum_(k=1)^n k = (n(n+1)) / 2 $
///
/// We define the following set:
/// $ cal(A) :=
/// { x in RR | x "is natural" } $
/// ```
///
/// ## Parameters
/// - body: Content (positional, required)
/// The contents of the formula.
///
/// - block: bool (named)
/// Whether the formula is displayed as a separate block.
///
/// ## Category
/// math
#[func]
#[capable(Show, Finalize, Layout, Inline, LayoutMath)]
#[derive(Debug, Clone, Hash)]
pub struct FormulaNode {
/// Whether the formula is displayed as a separate block.
pub block: bool,
/// The content of the formula.
pub body: Content,
}
#[node]
impl FormulaNode {
fn construct(_: &Vm, args: &mut Args) -> SourceResult<Content> {
let body = args.expect("body")?;
let block = args.named("block")?.unwrap_or(false);
Ok(Self { block, body }.pack())
}
fn field(&self, name: &str) -> Option<Value> {
match name {
"body" => Some(Value::Content(self.body.clone())),
"block" => Some(Value::Bool(self.block)),
_ => None,
}
}
}
impl Show for FormulaNode {
fn show(&self, _: &mut Vt, _: &Content, _: StyleChain) -> SourceResult<Content> {
let mut realized = self.clone().pack().guarded(Guard::Base(NodeId::of::<Self>()));
if self.block {
realized = realized.aligned(Axes::with_x(Some(Align::Center.into())))
}
Ok(realized)
}
}
impl Finalize for FormulaNode {
fn finalize(&self, realized: Content) -> Content {
realized.styled(
TextNode::FAMILY,
FallbackList(vec![FontFamily::new("New Computer Modern Math")]),
)
}
}
impl Layout for FormulaNode {
fn layout(
&self,
vt: &mut Vt,
styles: StyleChain,
regions: Regions,
) -> SourceResult<Fragment> {
// Find a math font.
let variant = variant(styles);
let world = vt.world();
let Some(font) = families(styles)
.find_map(|family| {
let id = world.book().select(family, variant)?;
let font = world.font(id)?;
let _ = font.ttf().tables().math?.constants?;
Some(font)
})
else {
return Ok(Fragment::frame(Frame::new(Size::zero())))
};
let mut ctx = MathContext::new(vt, styles, regions, &font, self.block);
let frame = ctx.layout_frame(self)?;
Ok(Fragment::frame(frame))
}
}
impl Inline for FormulaNode {}
#[capability]
trait LayoutMath {
fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()>;
}
impl LayoutMath for FormulaNode {
fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
self.body.layout_math(ctx)
}
}
impl LayoutMath for Content {
fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
if self.is::<SpaceNode>() {
return Ok(());
}
if self.is::<LinebreakNode>() {
ctx.push(MathFragment::Linebreak);
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);
}
let frame = ctx.layout_non_math(self)?;
ctx.push(frame);
Ok(())
}
}
|