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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
pub mod arabic;
pub mod hangul;
pub mod hebrew;
pub mod indic;
pub mod khmer;
pub mod myanmar;
pub mod thai;
pub mod universal;
mod arabic_table;
mod indic_machine;
mod indic_table;
mod khmer_machine;
mod myanmar_machine;
mod universal_machine;
mod universal_table;
mod vowel_constraints;
use alloc::boxed::Box;
use core::any::Any;
use crate::{script, Direction, Face, Script, Tag};
use crate::buffer::Buffer;
use crate::common::TagExt;
use crate::normalize::{ShapeNormalizeContext, ShapeNormalizationMode};
use crate::plan::{ShapePlan, ShapePlanner};
pub const MAX_COMBINING_MARKS: usize = 32;
pub const DEFAULT_SHAPER: ComplexShaper = ComplexShaper {
collect_features: None,
override_features: None,
create_data: None,
preprocess_text: None,
postprocess_glyphs: None,
normalization_mode: Some(ShapeNormalizationMode::Auto),
decompose: None,
compose: None,
setup_masks: None,
gpos_tag: None,
reorder_marks: None,
zero_width_marks: Some(ZeroWidthMarksMode::ByGdefLate),
fallback_position: true,
};
// Same as default but no mark advance zeroing / fallback positioning.
// Dumbest shaper ever, basically.
pub const DUMBER_SHAPER: ComplexShaper = ComplexShaper {
collect_features: None,
override_features: None,
create_data: None,
preprocess_text: None,
postprocess_glyphs: None,
normalization_mode: Some(ShapeNormalizationMode::Auto),
decompose: None,
compose: None,
setup_masks: None,
gpos_tag: None,
reorder_marks: None,
zero_width_marks: None,
fallback_position: false,
};
pub struct ComplexShaper {
/// Called during `shape_plan()`.
/// Shapers should use plan.map to add their features and callbacks.
pub collect_features: Option<fn(&mut ShapePlanner)>,
/// Called during `shape_plan()`.
/// Shapers should use plan.map to override features and add callbacks after
/// common features are added.
pub override_features: Option<fn(&mut ShapePlanner)>,
/// Called at the end of `shape_plan()`.
/// Whatever shapers return will be accessible through `plan.data()` later.
pub create_data: Option<fn(&ShapePlan) -> Box<dyn Any>>,
/// Called during `shape()`.
/// Shapers can use to modify text before shaping starts.
pub preprocess_text: Option<fn(&ShapePlan, &Face, &mut Buffer)>,
/// Called during `shape()`.
/// Shapers can use to modify text before shaping starts.
pub postprocess_glyphs: Option<fn(&ShapePlan, &Face, &mut Buffer)>,
/// How to normalize.
pub normalization_mode: Option<ShapeNormalizationMode>,
/// Called during `shape()`'s normalization.
pub decompose: Option<fn(&ShapeNormalizeContext, char) -> Option<(char, char)>>,
/// Called during `shape()`'s normalization.
pub compose: Option<fn(&ShapeNormalizeContext, char, char) -> Option<char>>,
/// Called during `shape()`.
/// Shapers should use map to get feature masks and set on buffer.
/// Shapers may NOT modify characters.
pub setup_masks: Option<fn(&ShapePlan, &Face, &mut Buffer)>,
/// If not `None`, then must match found GPOS script tag for
/// GPOS to be applied. Otherwise, fallback positioning will be used.
pub gpos_tag: Option<Tag>,
/// Called during `shape()`.
/// Shapers can use to modify ordering of combining marks.
pub reorder_marks: Option<fn(&ShapePlan, &mut Buffer, usize, usize)>,
/// If and when to zero-width marks.
pub zero_width_marks: Option<ZeroWidthMarksMode>,
/// Whether to use fallback mark positioning.
pub fallback_position: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ZeroWidthMarksMode {
ByGdefEarly,
ByGdefLate,
}
pub fn complex_categorize(
script: Script,
direction: Direction,
chosen_gsub_script: Option<Tag>,
) -> &'static ComplexShaper {
match script {
// Unicode-1.1 additions
script::ARABIC
// Unicode-3.0 additions
| script::MONGOLIAN
| script::SYRIAC
// Unicode-5.0 additions
| script::NKO
| script::PHAGS_PA
// Unicode-6.0 additions
| script::MANDAIC
// Unicode-7.0 additions
| script::MANICHAEAN
| script::PSALTER_PAHLAVI
// Unicode-9.0 additions
| script::ADLAM
// Unicode-11.0 additions
| script::HANIFI_ROHINGYA
| script::SOGDIAN => {
// For Arabic script, use the Arabic shaper even if no OT script tag was found.
// This is because we do fallback shaping for Arabic script (and not others).
// But note that Arabic shaping is applicable only to horizontal layout; for
// vertical text, just use the generic shaper instead.
//
// TODO: Does this still apply? Arabic fallback shaping was removed.
if (chosen_gsub_script != Some(Tag::default_script()) || script == script::ARABIC)
&& direction.is_horizontal()
{
&arabic::ARABIC_SHAPER
} else {
&DEFAULT_SHAPER
}
}
// Unicode-1.1 additions
script::THAI
| script::LAO => &thai::THAI_SHAPER,
// Unicode-1.1 additions
script::HANGUL => &hangul::HANGUL_SHAPER,
// Unicode-1.1 additions
script::HEBREW => &hebrew::HEBREW_SHAPER,
// Unicode-1.1 additions
script::BENGALI
| script::DEVANAGARI
| script::GUJARATI
| script::GURMUKHI
| script::KANNADA
| script::MALAYALAM
| script::ORIYA
| script::TAMIL
| script::TELUGU
// Unicode-3.0 additions
| script::SINHALA => {
// If the designer designed the font for the 'DFLT' script,
// (or we ended up arbitrarily pick 'latn'), use the default shaper.
// Otherwise, use the specific shaper.
//
// If it's indy3 tag, send to USE.
if chosen_gsub_script == Some(Tag::default_script()) ||
chosen_gsub_script == Some(Tag::from_bytes(b"latn")) {
&DEFAULT_SHAPER
} else if chosen_gsub_script.map_or(false, |tag| tag.to_bytes()[3] == b'3') {
&universal::UNIVERSAL_SHAPER
} else {
&indic::INDIC_SHAPER
}
}
script::KHMER => &khmer::KHMER_SHAPER,
script::MYANMAR => {
// If the designer designed the font for the 'DFLT' script,
// (or we ended up arbitrarily pick 'latn'), use the default shaper.
// Otherwise, use the specific shaper.
//
// If designer designed for 'mymr' tag, also send to default
// shaper. That's tag used from before Myanmar shaping spec
// was developed. The shaping spec uses 'mym2' tag.
if chosen_gsub_script == Some(Tag::default_script()) ||
chosen_gsub_script == Some(Tag::from_bytes(b"latn")) ||
chosen_gsub_script == Some(Tag::from_bytes(b"mymr"))
{
&DEFAULT_SHAPER
} else {
&myanmar::MYANMAR_SHAPER
}
}
// https://github.com/harfbuzz/harfbuzz/issues/1162
script::MYANMAR_ZAWGYI => &myanmar::MYANMAR_ZAWGYI_SHAPER,
// Unicode-2.0 additions
script::TIBETAN
// Unicode-3.0 additions
// | script::MONGOLIAN
// | script::SINHALA
// Unicode-3.2 additions
| script::BUHID
| script::HANUNOO
| script::TAGALOG
| script::TAGBANWA
// Unicode-4.0 additions
| script::LIMBU
| script::TAI_LE
// Unicode-4.1 additions
| script::BUGINESE
| script::KHAROSHTHI
| script::SYLOTI_NAGRI
| script::TIFINAGH
// Unicode-5.0 additions
| script::BALINESE
// | script::NKO
// | script::PHAGS_PA
// Unicode-5.1 additions
| script::CHAM
| script::KAYAH_LI
| script::LEPCHA
| script::REJANG
| script::SAURASHTRA
| script::SUNDANESE
// Unicode-5.2 additions
| script::EGYPTIAN_HIEROGLYPHS
| script::JAVANESE
| script::KAITHI
| script::MEETEI_MAYEK
| script::TAI_THAM
| script::TAI_VIET
// Unicode-6.0 additions
| script::BATAK
| script::BRAHMI
// | script::MANDAIC
// Unicode-6.1 additions
| script::CHAKMA
| script::SHARADA
| script::TAKRI
// Unicode-7.0 additions
| script::DUPLOYAN
| script::GRANTHA
| script::KHOJKI
| script::KHUDAWADI
| script::MAHAJANI
// | script::MANICHAEAN
| script::MODI
| script::PAHAWH_HMONG
// | script::PSALTER_PAHLAVI
| script::SIDDHAM
| script::TIRHUTA
// Unicode-8.0 additions
| script::AHOM
// Unicode-9.0 additions
// | script::ADLAM
| script::BHAIKSUKI
| script::MARCHEN
| script::NEWA
// Unicode-10.0 additions
| script::MASARAM_GONDI
| script::SOYOMBO
| script::ZANABAZAR_SQUARE
// Unicode-11.0 additions
| script::DOGRA
| script::GUNJALA_GONDI
// | script::HANIFI_ROHINGYA
| script::MAKASAR
// | script::SOGDIAN
// Unicode-12.0 additions
| script::NANDINAGARI
// Unicode-13.0 additions
| script::CHORASMIAN
| script::DIVES_AKURU => {
// If the designer designed the font for the 'DFLT' script,
// (or we ended up arbitrarily pick 'latn'), use the default shaper.
// Otherwise, use the specific shaper.
// Note that for some simple scripts, there may not be *any*
// GSUB/GPOS needed, so there may be no scripts found!
if chosen_gsub_script == Some(Tag::default_script()) ||
chosen_gsub_script == Some(Tag::from_bytes(b"latn")) {
&DEFAULT_SHAPER
} else {
&universal::UNIVERSAL_SHAPER
}
}
_ => &DEFAULT_SHAPER
}
}
// TODO: find a better name
#[inline]
pub const fn rb_flag(x: u32) -> u32 {
1 << x
}
#[inline]
pub fn rb_flag_unsafe(x: u32) -> u32 {
if x < 32 { 1 << x } else { 0 }
}
#[inline]
pub fn rb_flag_range(x: u32, y: u32) -> u32 {
(x < y) as u32 + rb_flag(y + 1) - rb_flag(x)
}
#[inline]
pub const fn rb_flag64(x: u32) -> u64 {
1 << x as u64
}
#[inline]
pub fn rb_flag64_unsafe(x: u32) -> u64 {
if x < 64 { 1 << (x as u64) } else { 0 }
}