Skip to main content

harfrust/ot/shaper/
mod.rs

1pub(crate) mod arabic;
2pub(crate) mod arabic_fallback;
3pub(crate) mod arabic_pua;
4#[rustfmt::skip]
5pub(crate) mod arabic_table;
6pub(crate) mod hangul;
7pub(crate) mod hebrew;
8pub(crate) mod indic;
9pub(crate) mod indic_machine;
10#[rustfmt::skip]
11pub(crate) mod indic_table;
12pub(crate) mod khmer;
13pub(crate) mod khmer_machine;
14mod machine_cursor;
15pub(crate) mod myanmar;
16pub(crate) mod myanmar_machine;
17pub(crate) mod syllabic;
18pub(crate) mod thai;
19pub(crate) mod use_;
20pub(crate) mod use_machine;
21#[rustfmt::skip]
22pub(crate) mod use_table;
23#[allow(clippy::collapsible_match)]
24pub(crate) mod vowel_constraints;
25
26use crate::buffer::*;
27use crate::normalize::*;
28use crate::plan::ShapePlan;
29use crate::tag::TagExt;
30use crate::unicode::Codepoint;
31use crate::ShaperFont;
32use crate::*;
33use crate::{Direction, Script, Tag};
34use alloc::boxed::Box;
35use core::any::Any;
36
37impl GlyphInfo {
38    declare_buffer_var!(
39        u8,
40        2,
41        2,
42        OT_SHAPER_VAR_U8_CATEGORY_VAR,
43        ot_shaper_var_u8_category,
44        set_ot_shaper_var_u8_category
45    );
46    declare_buffer_var!(
47        u8,
48        2,
49        3,
50        OT_SHAPER_VAR_U8_AUXILIARY_VAR,
51        ot_shaper_var_u8_auxiliary,
52        set_ot_shaper_var_u8_auxiliary
53    );
54}
55
56pub const MAX_COMBINING_MARKS: usize = 32;
57
58#[derive(Clone, Copy, PartialEq, Eq)]
59pub enum ZeroWidthMarks {
60    None,
61    ByGdefEarly,
62    ByGdefLate,
63}
64
65pub type DecomposeFn = fn(&NormalizeContext, Codepoint) -> Option<(Codepoint, Codepoint)>;
66pub type ComposeFn = fn(&NormalizeContext, Codepoint, Codepoint) -> Option<Codepoint>;
67
68pub const DEFAULT_SHAPER: OtShaper = OtShaper {
69    collect_features: None,
70    override_features: None,
71    create_data: None,
72    preprocess_text: None,
73    postprocess_glyphs: None,
74    normalization_preference: NormalizationMode::Auto,
75    decompose: None,
76    compose: None,
77    setup_masks: None,
78    gpos_tag: None,
79    reorder_marks: None,
80    zero_width_marks: ZeroWidthMarks::ByGdefLate,
81    fallback_position: true,
82};
83
84pub struct OtShaper {
85    /// Called during `shape_plan()`.
86    /// Shapers should use plan.map to add their features and callbacks.
87    pub collect_features: Option<fn(&mut ShapePlanner)>,
88
89    /// Called during `shape_plan()`.
90    /// Shapers should use plan.map to override features and add callbacks after
91    /// common features are added.
92    pub override_features: Option<fn(&mut ShapePlanner)>,
93
94    /// Called at the end of `shape_plan()`.
95    /// Whatever shapers return will be accessible through `plan.data()` later.
96    pub create_data: Option<fn(&ShapePlan) -> Box<dyn Any + Send + Sync>>,
97
98    /// Called during `shape()`.
99    /// Shapers can use to modify text before shaping starts.
100    pub preprocess_text: Option<fn(&ShapePlan, &ShaperFont<'_, '_>, &mut Buffer) -> Option<()>>,
101
102    /// Called during `shape()`.
103    /// Shapers can use to modify text before shaping starts.
104    pub postprocess_glyphs: Option<fn(&ShapePlan, &ShaperFont<'_, '_>, &mut Buffer)>,
105
106    /// How to normalize.
107    pub normalization_preference: NormalizationMode,
108
109    /// Called during `shape()`'s normalization.
110    pub decompose: Option<DecomposeFn>,
111
112    /// Called during `shape()`'s normalization.
113    pub compose: Option<ComposeFn>,
114
115    /// Called during `shape()`.
116    /// Shapers should use map to get feature masks and set on buffer.
117    /// Shapers may NOT modify characters.
118    pub setup_masks: Option<fn(&ShapePlan, &ShaperFont<'_, '_>, &mut Buffer)>,
119
120    /// If not `None`, then must match found GPOS script tag for
121    /// GPOS to be applied.  Otherwise, fallback positioning will be used.
122    pub gpos_tag: Option<Tag>,
123
124    /// Called during `shape()`.
125    /// Shapers can use to modify ordering of combining marks.
126    pub reorder_marks: Option<fn(&ShapePlan, &mut Buffer, usize, usize)>,
127
128    /// If and when to zero-width marks.
129    pub zero_width_marks: ZeroWidthMarks,
130
131    /// Whether to use fallback mark positioning.
132    pub fallback_position: bool,
133}
134
135// Same as default but no mark advance zeroing / fallback positioning.
136// Dumbest shaper ever, basically.
137pub const DUMBER_SHAPER: OtShaper = OtShaper {
138    collect_features: None,
139    override_features: None,
140    create_data: None,
141    preprocess_text: None,
142    postprocess_glyphs: None,
143    normalization_preference: NormalizationMode::Auto,
144    decompose: None,
145    compose: None,
146    setup_masks: None,
147    gpos_tag: None,
148    reorder_marks: None,
149    zero_width_marks: ZeroWidthMarks::None,
150    fallback_position: false,
151};
152
153pub fn categorize(
154    script: Script,
155    direction: Direction,
156    gsub_script: Option<Tag>,
157) -> &'static OtShaper {
158    match script {
159        // Unicode-1.1 additions
160        Script::ARABIC
161
162        // Unicode-3.0 additions
163        | Script::SYRIAC => {
164            // For Arabic script, use the Arabic shaper even if no OT script tag was found.
165            // This is because we do fallback shaping for Arabic script (and not others).
166            // But note that Arabic shaping is applicable only to horizontal layout; for
167            // vertical text, just use the generic shaper instead.
168            //
169            // TODO: Does this still apply? Arabic fallback shaping was removed.
170            if (gsub_script != Some(Tag::default_script()) || script == Script::ARABIC)
171                && direction.is_horizontal()
172            {
173                &arabic::ARABIC_SHAPER
174            } else {
175                &DEFAULT_SHAPER
176            }
177        }
178
179        // Unicode-1.1 additions
180        Script::THAI
181        | Script::LAO => &thai::THAI_SHAPER,
182
183        // Unicode-1.1 additions
184        Script::HANGUL => &hangul::HANGUL_SHAPER,
185
186        // Unicode-1.1 additions
187        Script::HEBREW => &hebrew::HEBREW_SHAPER,
188
189        // Unicode-1.1 additions
190        Script::BENGALI
191        | Script::DEVANAGARI
192        | Script::GUJARATI
193        | Script::GURMUKHI
194        | Script::KANNADA
195        | Script::MALAYALAM
196        | Script::ORIYA
197        | Script::TAMIL
198        | Script::TELUGU => {
199            // If the designer designed the font for the 'DFLT' script,
200            // (or we ended up arbitrarily pick 'latn'), use the default shaper.
201            // Otherwise, use the specific shaper.
202            //
203            // If it's indy3 tag, send to USE.
204            if gsub_script == Some(Tag::default_script()) ||
205               gsub_script == Some(Tag::new(b"latn")) {
206                &DEFAULT_SHAPER
207            } else if gsub_script.is_some_and(|tag| tag.to_be_bytes()[3] == b'3') {
208                &use_::UNIVERSAL_SHAPER
209            } else {
210                &indic::INDIC_SHAPER
211            }
212        }
213
214        Script::KHMER => &khmer::KHMER_SHAPER,
215
216        Script::MYANMAR => {
217            // If the designer designed the font for the 'DFLT' script,
218            // (or we ended up arbitrarily pick 'latn'), use the default shaper.
219            // Otherwise, use the specific shaper.
220            //
221            // If designer designed for 'mymr' tag, also send to default
222            // shaper.  That's tag used from before Myanmar shaping spec
223            // was developed.  The shaping spec uses 'mym2' tag.
224            if gsub_script == Some(Tag::default_script()) ||
225               gsub_script == Some(Tag::new(b"latn")) ||
226               gsub_script == Some(Tag::new(b"mymr"))
227            {
228                &DEFAULT_SHAPER
229            } else {
230                &myanmar::MYANMAR_SHAPER
231            }
232        }
233
234        // https://github.com/harfbuzz/harfbuzz/issues/1162
235        Script::MYANMAR_ZAWGYI => &myanmar::MYANMAR_ZAWGYI_SHAPER,
236
237        // Unicode-2.0 additions
238        Script::TIBETAN
239
240        // Unicode-3.0 additions
241        | Script::MONGOLIAN
242        | Script::SINHALA
243
244        // Unicode-3.2 additions
245        | Script::BUHID
246        | Script::HANUNOO
247        | Script::TAGALOG
248        | Script::TAGBANWA
249
250        // Unicode-4.0 additions
251        | Script::LIMBU
252        | Script::TAI_LE
253
254        // Unicode-4.1 additions
255        | Script::BUGINESE
256        | Script::KHAROSHTHI
257        | Script::SYLOTI_NAGRI
258        | Script::TIFINAGH
259
260        // Unicode-5.0 additions
261        | Script::BALINESE
262        | Script::NKO
263        | Script::PHAGS_PA
264
265        // Unicode-5.1 additions
266        | Script::CHAM
267        | Script::KAYAH_LI
268        | Script::LEPCHA
269        | Script::REJANG
270        | Script::SAURASHTRA
271        | Script::SUNDANESE
272
273        // Unicode-5.2 additions
274        | Script::EGYPTIAN_HIEROGLYPHS
275        | Script::JAVANESE
276        | Script::KAITHI
277        | Script::MEETEI_MAYEK
278        | Script::TAI_THAM
279        | Script::TAI_VIET
280
281        // Unicode-6.0 additions
282        | Script::BATAK
283        | Script::BRAHMI
284        | Script::MANDAIC
285
286        // Unicode-6.1 additions
287        | Script::CHAKMA
288        | Script::MIAO
289        | Script::SHARADA
290        | Script::TAKRI
291
292        // Unicode-7.0 additions
293        | Script::DUPLOYAN
294        | Script::GRANTHA
295        | Script::KHOJKI
296        | Script::KHUDAWADI
297        | Script::MAHAJANI
298        | Script::MANICHAEAN
299        | Script::MODI
300        | Script::PAHAWH_HMONG
301        | Script::PSALTER_PAHLAVI
302        | Script::SIDDHAM
303        | Script::TIRHUTA
304
305        // Unicode-8.0 additions
306        | Script::AHOM
307        | Script::MULTANI
308
309        // Unicode-9.0 additions
310        | Script::ADLAM
311        | Script::BHAIKSUKI
312        | Script::MARCHEN
313        | Script::NEWA
314
315        // Unicode-10.0 additions
316        | Script::MASARAM_GONDI
317        | Script::SOYOMBO
318        | Script::ZANABAZAR_SQUARE
319
320        // Unicode-11.0 additions
321        | Script::DOGRA
322        | Script::GUNJALA_GONDI
323        | Script::HANIFI_ROHINGYA
324        | Script::MAKASAR
325        | Script::MEDEFAIDRIN
326        | Script::OLD_SOGDIAN
327        | Script::SOGDIAN
328
329        // Unicode-12.0 additions
330        | Script::ELYMAIC
331        | Script::NANDINAGARI
332        | Script::NYIAKENG_PUACHUE_HMONG
333        | Script::WANCHO
334
335        // Unicode-13.0 additions
336        | Script::CHORASMIAN
337        | Script::DIVES_AKURU
338        | Script::KHITAN_SMALL_SCRIPT
339        | Script::YEZIDI
340
341        // Unicode-14.0 additions
342        | Script::CYPRO_MINOAN
343        | Script::OLD_UYGHUR
344        | Script::TANGSA
345        | Script::TOTO
346        | Script::VITHKUQI
347
348        // Unicode-15.0 additions
349        | Script::KAWI
350        | Script::NAG_MUNDARI
351
352        // Unicode-16.0 additions
353        | Script::GARAY
354        | Script::GURUNG_KHEMA
355        | Script::KIRAT_RAI
356        | Script::OL_ONAL
357        | Script::SUNUWAR
358        | Script::TODHRI
359        | Script::TULU_TIGALARI
360
361        // Unicode-17.0 additions
362        | Script::BERIA_ERFE
363        | Script::SIDETIC
364        | Script::TAI_YO
365        | Script::TOLONG_SIKI
366
367        // Unicode-18.0 additions
368        | Script::JURCHEN
369        | Script::PROTO_CUNEIFORM
370        | Script::SEAL
371
372        => {
373            // If the designer designed the font for the 'DFLT' script,
374            // (or we ended up arbitrarily pick 'latn'), use the default shaper.
375            // Otherwise, use the specific shaper.
376            // Note that for some simple scripts, there may not be *any*
377            // GSUB/GPOS needed, so there may be no scripts found!
378            if gsub_script == Some(Tag::default_script()) ||
379               gsub_script == Some(Tag::new(b"latn")) {
380                &DEFAULT_SHAPER
381            } else {
382                &use_::UNIVERSAL_SHAPER
383            }
384        }
385
386        _ => &DEFAULT_SHAPER
387    }
388}