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
//! suki - LaTeX の数式を HTML と SVG に組み上げるライブラリ
//!
//! 読み取り (parse) と組み上げ (layout) と書き出し (html, svg) を分けている。
//! 位置と寸法は layout が一度だけ決めて、HTML と SVG はそれを書き出すだけである。
//!
//! 組み方はフォントが決める。OpenType の MATH テーブルから、数式軸の高さ、線の太さ、
//! 添字の縮小率と持ち上げ量、根号の空きを読む。伸びる括弧は引き伸ばすのではなく、
//! フォントが持つ大きさ違いの字形と積み木で作る。書体を替えれば組み方もそれに従う。
//!
//! ```
//! use suki::{Mode, Shape};
//!
//! let data = std::fs::read("font/latinmodern-math.otf").unwrap();
//! let font = suki::load_font(data).unwrap();
//! let html = suki::render_html(r"\frac{a}{b}", &font, Mode::Inline).unwrap();
//! let svg = suki::render_svg(r"\sum_{k=1}^{n} k", &font, Mode::Display, Shape::Outline).unwrap();
//! ```
//!
//! HTML は文字を置くので、[`style`] が返す CSS を style 要素に入れて、同じフォントを
//! 表示する側にも用意する。SVG は [`Shape::Outline`] で書き出せばフォントが要らない。
pub mod cff;
pub mod error;
pub mod font;
pub mod glyf;
pub mod html;
pub mod layout;
pub mod math;
pub mod metric;
pub mod node;
pub mod opentype;
pub mod outline;
pub mod parse;
pub mod svg;
pub mod symbol;
pub mod text;
pub mod token;
#[cfg(target_arch = "wasm32")]
pub mod wasm;
pub use error::Error;
pub use font::{Font, load as load_font};
pub use html::style;
pub use layout::Shape;
pub use node::{Align, Class, Node, Spacing, Style};
pub use parse::{Wrapper, parse};
pub use text::escape;
/// 組み方。Display は独立した行、Inline は文中
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
Inline,
Display,
}
/// 囲みが指す組み方を優先し、囲みがなければ渡された組み方を使う
fn choose(wrapper: Wrapper, mode: Mode) -> Mode {
if parse::display_of(wrapper, mode == Mode::Display) {
Mode::Display
} else {
Mode::Inline
}
}
fn build(node: &Node, font: &Font, mode: Mode) -> layout::Layout {
layout::layout(node, font, 1.0, Style::Auto, mode == Mode::Display)
}
/// 構文木を HTML にする。字形は文字として置かれる
pub fn to_html(node: &Node, font: &Font, mode: Mode) -> String {
html::render(&build(node, font, mode), font)
}
/// 構文木を SVG にする。label は代替テキストになる
pub fn to_svg(node: &Node, font: &Font, mode: Mode, shape: Shape, label: &str) -> String {
svg::render(&build(node, font, mode), font, shape, label)
}
/// LaTeX の文字列を HTML にする
///
/// `$…$` や `\[…\]` のような囲みが付いていれば、それが組み方を決める。
/// 付いていなければ mode がそのまま使われる
pub fn render_html(source: &str, font: &Font, mode: Mode) -> Result<String, Error> {
let (node, wrapper) = parse::parse_wrapped(source)?;
Ok(to_html(&node, font, choose(wrapper, mode)))
}
/// LaTeX の文字列を SVG にする
pub fn render_svg(
source: &str,
font: &Font,
mode: Mode,
shape: Shape,
) -> Result<String, Error> {
let (node, wrapper) = parse::parse_wrapped(source)?;
Ok(to_svg(&node, font, choose(wrapper, mode), shape, source))
}
#[cfg(test)]
mod tests {
use super::*;
fn sample() -> Font {
let data = std::fs::read("font/latinmodern-math.otf").expect("試験用のフォントを読めません");
load_font(data).unwrap()
}
#[test]
fn renders_both_shapes() {
let font = sample();
let source = r"x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}";
let html = render_html(source, &font, Mode::Inline).unwrap();
assert!(html.starts_with("<span class=\"suki\""));
assert!(html.ends_with("</span>"));
for shape in [Shape::Text, Shape::Outline] {
let svg = render_svg(source, &font, Mode::Display, shape).unwrap();
assert!(svg.starts_with("<svg"));
assert!(svg.ends_with("</svg>"));
}
}
#[test]
fn outlines_need_no_font_at_the_other_end() {
let font = sample();
let svg = render_svg(r"\sqrt{x}", &font, Mode::Display, Shape::Outline).unwrap();
assert!(!svg.contains("font-family"));
assert!(!svg.contains("<text"));
}
#[test]
fn display_and_inline_differ() {
let font = sample();
let source = r"\sum_{k=1}^{n} k";
let inline = render_svg(source, &font, Mode::Inline, Shape::Outline).unwrap();
let display = render_svg(source, &font, Mode::Display, Shape::Outline).unwrap();
assert_ne!(inline, display);
}
#[test]
fn every_supported_shape_renders() {
let font = sample();
let sources = [
r"x^2 + y^2 = r^2",
r"e^{i\pi} + 1 = 0",
r"\sum_{k=1}^{n} k = \frac{n(n+1)}{2}",
r"\int_0^1 x^2 \, dx = \frac{1}{3}",
r"\lim_{x \to \infty} \frac{1}{x} = 0",
r"\sqrt[3]{x + 1}",
r"\left( \frac{a}{b} \right)^{n}",
r"\left\{ x \in \mathbb{R} \mid x > 0 \right\}",
r"\binom{n}{k} = \frac{n!}{k!(n-k)!}",
r"\begin{pmatrix} a & b \\ c & d \end{pmatrix}",
r"\begin{array}{lr} \hline a & b \\ \hline \end{array}",
r"\begin{cases} 1 & x > 0 \\ 0 & x \leq 0 \end{cases}",
r"\overline{AB} \ne \underline{CD}",
r"\hat{x} + \vec{v} + \bar{y} + \dot{z}",
r"\mathbf{A} \mathrm{d} \mathbb{Z} \mathit{f}",
r"\text{ここは文章です}",
];
for source in sources {
assert!(render_html(source, &font, Mode::Display).is_ok(), "HTML: {source}");
for shape in [Shape::Text, Shape::Outline] {
assert!(
render_svg(source, &font, Mode::Display, shape).is_ok(),
"SVG: {source}"
);
}
}
}
#[test]
fn both_outputs_name_the_same_fallback() {
let font = sample();
let source = r"\text{固有値}";
let html = render_html(source, &font, Mode::Inline).unwrap();
let svg = render_svg(source, &font, Mode::Inline, Shape::Outline).unwrap();
// 書体が持たない字は、どちらの書き出し方でも同じ書体に落ちる
assert!(style(&font).contains(font::FALLBACK));
assert!(svg.contains(font::FALLBACK), "{svg}");
assert!(html.contains("固"));
assert!(svg.contains("固"));
}
/// 書き出しの寸法だけを取り出す。式が同じでも代替テキストは違うので、そこは見ない
fn frame(markup: &str) -> &str {
let at = markup.find("viewBox=\"").expect("viewBox がありません") + 9;
let end = markup[at..].find('"').expect("viewBox が閉じていません");
&markup[at..at + end]
}
#[test]
fn a_wrapper_decides_the_shape() {
let font = sample();
let source = r"\sum_{k=1}^{n}";
let display = render_svg(source, &font, Mode::Display, Shape::Outline).unwrap();
let inline = render_svg(source, &font, Mode::Inline, Shape::Outline).unwrap();
assert_ne!(frame(&display), frame(&inline), "組み方で寸法が変わっていません");
// 囲みが付いていれば、渡した組み方より囲みが優先される
for (wrapped, wanted) in [
(r"$$\sum_{k=1}^{n}$$", &display),
(r"\[\sum_{k=1}^{n}\]", &display),
(r"$\sum_{k=1}^{n}$", &inline),
(r"\(\sum_{k=1}^{n}\)", &inline),
] {
let opposite = if std::ptr::eq(wanted, &display) {Mode::Inline} else {Mode::Display};
let out = render_svg(wrapped, &font, opposite, Shape::Outline).unwrap();
assert_eq!(frame(&out), frame(wanted), "囲みが効いていません: {wrapped}");
}
}
#[test]
fn errors_point_at_the_source() {
let font = sample();
let error = render_html(r"\frac{a}{b", &font, Mode::Inline).unwrap_err();
assert!(error.to_string().contains("文字目"));
}
}