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
|
use typst::model::SequenceNode;
use super::{variant, SpaceNode, TextNode, TextSize};
use crate::prelude::*;
/// Set text in subscript.
///
/// The text is rendered smaller and its baseline is lowered.
///
/// ## Example
/// ```example
/// Revenue#sub[yearly]
/// ```
///
/// Display: Subscript
/// Category: text
#[node(Show)]
pub struct SubNode {
/// Whether to prefer the dedicated subscript characters of the font.
///
/// If this is enabled, Typst first tries to transform the text to subscript
/// codepoints. If that fails, it falls back to rendering lowered and shrunk
/// normal letters.
///
/// ```example
/// N#sub(typographic: true)[1]
/// N#sub(typographic: false)[1]
/// ```
#[default(true)]
pub typographic: bool,
/// The baseline shift for synthetic subscripts. Does not apply if
/// `typographic` is true and the font has subscript codepoints for the
/// given `body`.
#[default(Em::new(0.2).into())]
pub baseline: Length,
/// The font size for synthetic subscripts. Does not apply if
/// `typographic` is true and the font has subscript codepoints for the
/// given `body`.
#[default(TextSize(Em::new(0.6).into()))]
pub size: TextSize,
/// The text to display in subscript.
#[required]
pub body: Content,
}
impl Show for SubNode {
fn show(&self, vt: &mut Vt, styles: StyleChain) -> SourceResult<Content> {
let body = self.body();
let mut transformed = None;
if self.typographic(styles) {
if let Some(text) = search_text(&body, true) {
if is_shapable(vt, &text, styles) {
transformed = Some(TextNode::packed(text));
}
}
};
Ok(transformed.unwrap_or_else(|| {
body.styled(TextNode::set_baseline(self.baseline(styles)))
.styled(TextNode::set_size(self.size(styles)))
}))
}
}
/// Set text in superscript.
///
/// The text is rendered smaller and its baseline is raised.
///
/// ## Example
/// ```example
/// 1#super[st] try!
/// ```
///
/// Display: Superscript
/// Category: text
#[node(Show)]
pub struct SuperNode {
/// Whether to prefer the dedicated superscript characters of the font.
///
/// If this is enabled, Typst first tries to transform the text to
/// superscript codepoints. If that fails, it falls back to rendering
/// raised and shrunk normal letters.
///
/// ```example
/// N#super(typographic: true)[1]
/// N#super(typographic: false)[1]
/// ```
#[default(true)]
pub typographic: bool,
/// The baseline shift for synthetic superscripts. Does not apply if
/// `typographic` is true and the font has superscript codepoints for the
/// given `body`.
#[default(Em::new(-0.5).into())]
pub baseline: Length,
/// The font size for synthetic superscripts. Does not apply if
/// `typographic` is true and the font has superscript codepoints for the
/// given `body`.
#[default(TextSize(Em::new(0.6).into()))]
pub size: TextSize,
/// The text to display in superscript.
#[required]
pub body: Content,
}
impl Show for SuperNode {
fn show(&self, vt: &mut Vt, styles: StyleChain) -> SourceResult<Content> {
let body = self.body();
let mut transformed = None;
if self.typographic(styles) {
if let Some(text) = search_text(&body, false) {
if is_shapable(vt, &text, styles) {
transformed = Some(TextNode::packed(text));
}
}
};
Ok(transformed.unwrap_or_else(|| {
body.styled(TextNode::set_baseline(self.baseline(styles)))
.styled(TextNode::set_size(self.size(styles)))
}))
}
}
/// Find and transform the text contained in `content` to the given script kind
/// if and only if it only consists of `Text`, `Space`, and `Empty` leaf nodes.
fn search_text(content: &Content, sub: bool) -> Option<EcoString> {
if content.is::<SpaceNode>() {
Some(' '.into())
} else if let Some(node) = content.to::<TextNode>() {
convert_script(&node.text(), sub)
} else if let Some(seq) = content.to::<SequenceNode>() {
let mut full = EcoString::new();
for item in seq.children() {
match search_text(&item, sub) {
Some(text) => full.push_str(&text),
None => return None,
}
}
Some(full)
} else {
None
}
}
/// Checks whether the first retrievable family contains all code points of the
/// given string.
fn is_shapable(vt: &Vt, text: &str, styles: StyleChain) -> bool {
let world = vt.world();
for family in TextNode::font_in(styles) {
if let Some(font) = world
.book()
.select(family.as_str(), variant(styles))
.and_then(|id| world.font(id))
{
return text.chars().all(|c| font.ttf().glyph_index(c).is_some());
}
}
false
}
/// Convert a string to sub- or superscript codepoints if all characters
/// can be mapped to such a codepoint.
fn convert_script(text: &str, sub: bool) -> Option<EcoString> {
let mut result = EcoString::with_capacity(text.len());
let converter = if sub { to_subscript_codepoint } else { to_superscript_codepoint };
for c in text.chars() {
match converter(c) {
Some(c) => result.push(c),
None => return None,
}
}
Some(result)
}
/// Convert a character to its corresponding Unicode superscript.
fn to_superscript_codepoint(c: char) -> Option<char> {
char::from_u32(match c {
'0' => 0x2070,
'1' => 0x00B9,
'2' => 0x00B2,
'3' => 0x00B3,
'4'..='9' => 0x2070 + (c as u32 + 4 - '4' as u32),
'+' => 0x207A,
'-' => 0x207B,
'=' => 0x207C,
'(' => 0x207D,
')' => 0x207E,
'n' => 0x207F,
'i' => 0x2071,
' ' => 0x0020,
_ => return None,
})
}
/// Convert a character to its corresponding Unicode subscript.
fn to_subscript_codepoint(c: char) -> Option<char> {
char::from_u32(match c {
'0' => 0x2080,
'1'..='9' => 0x2080 + (c as u32 - '0' as u32),
'+' => 0x208A,
'-' => 0x208B,
'=' => 0x208C,
'(' => 0x208D,
')' => 0x208E,
'a' => 0x2090,
'e' => 0x2091,
'o' => 0x2092,
'x' => 0x2093,
'h' => 0x2095,
'k' => 0x2096,
'l' => 0x2097,
'm' => 0x2098,
'n' => 0x2099,
'p' => 0x209A,
's' => 0x209B,
't' => 0x209C,
' ' => 0x0020,
_ => return None,
})
}
|