Imported Upstream version 1.7.6
[platform/upstream/harfbuzz.git] / src / hb-ot-shape-complex-private.hh
1 /*
2  * Copyright © 2010,2011,2012  Google, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Google Author(s): Behdad Esfahbod
25  */
26
27 #ifndef HB_OT_SHAPE_COMPLEX_PRIVATE_HH
28 #define HB_OT_SHAPE_COMPLEX_PRIVATE_HH
29
30 #include "hb-private.hh"
31
32 #include "hb-ot-shape-private.hh"
33 #include "hb-ot-shape-normalize-private.hh"
34
35
36
37 /* buffer var allocations, used by complex shapers */
38 #define complex_var_u8_0()      var2.u8[2]
39 #define complex_var_u8_1()      var2.u8[3]
40
41
42 #define HB_OT_SHAPE_COMPLEX_MAX_COMBINING_MARKS 32
43
44 enum hb_ot_shape_zero_width_marks_type_t {
45   HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
46   HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY,
47   HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE
48 };
49
50
51 /* Master OT shaper list */
52 #define HB_COMPLEX_SHAPERS_IMPLEMENT_SHAPERS \
53   HB_COMPLEX_SHAPER_IMPLEMENT (default) /* should be first */ \
54   HB_COMPLEX_SHAPER_IMPLEMENT (arabic) \
55   HB_COMPLEX_SHAPER_IMPLEMENT (hangul) \
56   HB_COMPLEX_SHAPER_IMPLEMENT (hebrew) \
57   HB_COMPLEX_SHAPER_IMPLEMENT (indic) \
58   HB_COMPLEX_SHAPER_IMPLEMENT (khmer) \
59   HB_COMPLEX_SHAPER_IMPLEMENT (myanmar) \
60   HB_COMPLEX_SHAPER_IMPLEMENT (myanmar_old) \
61   HB_COMPLEX_SHAPER_IMPLEMENT (thai) \
62   HB_COMPLEX_SHAPER_IMPLEMENT (tibetan) \
63   HB_COMPLEX_SHAPER_IMPLEMENT (use) \
64   /* ^--- Add new shapers here */
65
66
67 struct hb_ot_complex_shaper_t
68 {
69   /* collect_features()
70    * Called during shape_plan().
71    * Shapers should use plan->map to add their features and callbacks.
72    * May be nullptr.
73    */
74   void (*collect_features) (hb_ot_shape_planner_t *plan);
75
76   /* override_features()
77    * Called during shape_plan().
78    * Shapers should use plan->map to override features and add callbacks after
79    * common features are added.
80    * May be nullptr.
81    */
82   void (*override_features) (hb_ot_shape_planner_t *plan);
83
84
85   /* data_create()
86    * Called at the end of shape_plan().
87    * Whatever shapers return will be accessible through plan->data later.
88    * If nullptr is returned, means a plan failure.
89    */
90   void *(*data_create) (const hb_ot_shape_plan_t *plan);
91
92   /* data_destroy()
93    * Called when the shape_plan is being destroyed.
94    * plan->data is passed here for destruction.
95    * If nullptr is returned, means a plan failure.
96    * May be nullptr.
97    */
98   void (*data_destroy) (void *data);
99
100
101   /* preprocess_text()
102    * Called during shape().
103    * Shapers can use to modify text before shaping starts.
104    * May be nullptr.
105    */
106   void (*preprocess_text) (const hb_ot_shape_plan_t *plan,
107                            hb_buffer_t              *buffer,
108                            hb_font_t                *font);
109
110   /* postprocess_glyphs()
111    * Called during shape().
112    * Shapers can use to modify glyphs after shaping ends.
113    * May be nullptr.
114    */
115   void (*postprocess_glyphs) (const hb_ot_shape_plan_t *plan,
116                               hb_buffer_t              *buffer,
117                               hb_font_t                *font);
118
119
120   hb_ot_shape_normalization_mode_t normalization_preference;
121
122   /* decompose()
123    * Called during shape()'s normalization.
124    * May be nullptr.
125    */
126   bool (*decompose) (const hb_ot_shape_normalize_context_t *c,
127                      hb_codepoint_t  ab,
128                      hb_codepoint_t *a,
129                      hb_codepoint_t *b);
130
131   /* compose()
132    * Called during shape()'s normalization.
133    * May be nullptr.
134    */
135   bool (*compose) (const hb_ot_shape_normalize_context_t *c,
136                    hb_codepoint_t  a,
137                    hb_codepoint_t  b,
138                    hb_codepoint_t *ab);
139
140   /* setup_masks()
141    * Called during shape().
142    * Shapers should use map to get feature masks and set on buffer.
143    * Shapers may NOT modify characters.
144    * May be nullptr.
145    */
146   void (*setup_masks) (const hb_ot_shape_plan_t *plan,
147                        hb_buffer_t              *buffer,
148                        hb_font_t                *font);
149
150   /* disable_otl()
151    * Called during shape().
152    * If set and returns true, GDEF/GSUB/GPOS of the font are ignored
153    * and fallback operations used.
154    * May be nullptr.
155    */
156   bool (*disable_otl) (const hb_ot_shape_plan_t *plan);
157
158   /* reorder_marks()
159    * Called during shape().
160    * Shapers can use to modify ordering of combining marks.
161    * May be nullptr.
162    */
163   void (*reorder_marks) (const hb_ot_shape_plan_t *plan,
164                          hb_buffer_t              *buffer,
165                          unsigned int              start,
166                          unsigned int              end);
167
168   hb_ot_shape_zero_width_marks_type_t zero_width_marks;
169
170   bool fallback_position;
171 };
172
173 #define HB_COMPLEX_SHAPER_IMPLEMENT(name) extern HB_INTERNAL const hb_ot_complex_shaper_t _hb_ot_complex_shaper_##name;
174 HB_COMPLEX_SHAPERS_IMPLEMENT_SHAPERS
175 #undef HB_COMPLEX_SHAPER_IMPLEMENT
176
177
178 static inline const hb_ot_complex_shaper_t *
179 hb_ot_shape_complex_categorize (const hb_ot_shape_planner_t *planner)
180 {
181   switch ((hb_tag_t) planner->props.script)
182   {
183     default:
184       return &_hb_ot_complex_shaper_default;
185
186
187     /* Unicode-1.1 additions */
188     case HB_SCRIPT_ARABIC:
189
190     /* Unicode-3.0 additions */
191     case HB_SCRIPT_MONGOLIAN:
192     case HB_SCRIPT_SYRIAC:
193
194     /* Unicode-5.0 additions */
195     case HB_SCRIPT_NKO:
196     case HB_SCRIPT_PHAGS_PA:
197
198     /* Unicode-6.0 additions */
199     case HB_SCRIPT_MANDAIC:
200
201     /* Unicode-7.0 additions */
202     case HB_SCRIPT_MANICHAEAN:
203     case HB_SCRIPT_PSALTER_PAHLAVI:
204
205     /* Unicode-9.0 additions */
206     case HB_SCRIPT_ADLAM:
207
208       /* For Arabic script, use the Arabic shaper even if no OT script tag was found.
209        * This is because we do fallback shaping for Arabic script (and not others).
210        * But note that Arabic shaping is applicable only to horizontal layout; for
211        * vertical text, just use the generic shaper instead. */
212       if ((planner->map.chosen_script[0] != HB_OT_TAG_DEFAULT_SCRIPT ||
213            planner->props.script == HB_SCRIPT_ARABIC) &&
214           HB_DIRECTION_IS_HORIZONTAL(planner->props.direction))
215         return &_hb_ot_complex_shaper_arabic;
216       else
217         return &_hb_ot_complex_shaper_default;
218
219
220     /* Unicode-1.1 additions */
221     case HB_SCRIPT_THAI:
222     case HB_SCRIPT_LAO:
223
224       return &_hb_ot_complex_shaper_thai;
225
226
227     /* Unicode-1.1 additions */
228     case HB_SCRIPT_HANGUL:
229
230       return &_hb_ot_complex_shaper_hangul;
231
232
233     /* Unicode-2.0 additions */
234     case HB_SCRIPT_TIBETAN:
235
236       return &_hb_ot_complex_shaper_tibetan;
237
238
239     /* Unicode-1.1 additions */
240     case HB_SCRIPT_HEBREW:
241
242       return &_hb_ot_complex_shaper_hebrew;
243
244
245     /* ^--- Add new shapers here */
246
247 #if 0
248     /* Unicode-4.1 additions */
249     case HB_SCRIPT_NEW_TAI_LUE:
250 #endif
251
252     /* Unicode-1.1 additions */
253     case HB_SCRIPT_BENGALI:
254     case HB_SCRIPT_DEVANAGARI:
255     case HB_SCRIPT_GUJARATI:
256     case HB_SCRIPT_GURMUKHI:
257     case HB_SCRIPT_KANNADA:
258     case HB_SCRIPT_MALAYALAM:
259     case HB_SCRIPT_ORIYA:
260     case HB_SCRIPT_TAMIL:
261     case HB_SCRIPT_TELUGU:
262
263     /* Unicode-3.0 additions */
264     case HB_SCRIPT_SINHALA:
265
266       /* If the designer designed the font for the 'DFLT' script,
267        * (or we ended up arbitrarily pick 'latn'), use the default shaper.
268        * Otherwise, use the specific shaper.
269        * Note that for some simple scripts, there may not be *any*
270        * GSUB/GPOS needed, so there may be no scripts found! */
271       if (planner->map.chosen_script[0] == HB_TAG ('D','F','L','T') ||
272           planner->map.chosen_script[0] == HB_TAG ('l','a','t','n'))
273         return &_hb_ot_complex_shaper_default;
274       else
275         return &_hb_ot_complex_shaper_indic;
276
277     case HB_SCRIPT_KHMER:
278       /* A number of Khmer fonts in the wild don't have a 'pref' feature,
279        * and as such won't shape properly via the Indic shaper;
280        * however, they typically have 'liga' / 'clig' features that implement
281        * the necessary "reordering" by means of ligature substitutions.
282        * So we send such pref-less fonts through the generic shaper instead. */
283       if (planner->map.found_script[0] &&
284           hb_ot_layout_language_find_feature (planner->face, HB_OT_TAG_GSUB,
285                                               planner->map.script_index[0],
286                                               planner->map.language_index[0],
287                                               HB_TAG ('p','r','e','f'),
288                                               nullptr))
289         return &_hb_ot_complex_shaper_khmer;
290       else
291         return &_hb_ot_complex_shaper_default;
292
293     case HB_SCRIPT_MYANMAR:
294       if (planner->map.chosen_script[0] == HB_TAG ('m','y','m','2'))
295         return &_hb_ot_complex_shaper_myanmar;
296       else if (planner->map.chosen_script[0] == HB_TAG ('m','y','m','r'))
297         return &_hb_ot_complex_shaper_myanmar_old;
298       else
299         return &_hb_ot_complex_shaper_default;
300
301
302     /* Unicode-2.0 additions */
303     //case HB_SCRIPT_TIBETAN:
304
305     /* Unicode-3.0 additions */
306     //case HB_SCRIPT_MONGOLIAN:
307     //case HB_SCRIPT_SINHALA:
308
309     /* Unicode-3.2 additions */
310     case HB_SCRIPT_BUHID:
311     case HB_SCRIPT_HANUNOO:
312     case HB_SCRIPT_TAGALOG:
313     case HB_SCRIPT_TAGBANWA:
314
315     /* Unicode-4.0 additions */
316     case HB_SCRIPT_LIMBU:
317     case HB_SCRIPT_TAI_LE:
318
319     /* Unicode-4.1 additions */
320     case HB_SCRIPT_BUGINESE:
321     case HB_SCRIPT_KHAROSHTHI:
322     case HB_SCRIPT_SYLOTI_NAGRI:
323     case HB_SCRIPT_TIFINAGH:
324
325     /* Unicode-5.0 additions */
326     case HB_SCRIPT_BALINESE:
327     //case HB_SCRIPT_NKO:
328     //case HB_SCRIPT_PHAGS_PA:
329
330     /* Unicode-5.1 additions */
331     case HB_SCRIPT_CHAM:
332     case HB_SCRIPT_KAYAH_LI:
333     case HB_SCRIPT_LEPCHA:
334     case HB_SCRIPT_REJANG:
335     case HB_SCRIPT_SAURASHTRA:
336     case HB_SCRIPT_SUNDANESE:
337
338     /* Unicode-5.2 additions */
339     case HB_SCRIPT_EGYPTIAN_HIEROGLYPHS:
340     case HB_SCRIPT_JAVANESE:
341     case HB_SCRIPT_KAITHI:
342     case HB_SCRIPT_MEETEI_MAYEK:
343     case HB_SCRIPT_TAI_THAM:
344     case HB_SCRIPT_TAI_VIET:
345
346     /* Unicode-6.0 additions */
347     case HB_SCRIPT_BATAK:
348     case HB_SCRIPT_BRAHMI:
349     //case HB_SCRIPT_MANDAIC:
350
351     /* Unicode-6.1 additions */
352     case HB_SCRIPT_CHAKMA:
353     case HB_SCRIPT_SHARADA:
354     case HB_SCRIPT_TAKRI:
355
356     /* Unicode-7.0 additions */
357     case HB_SCRIPT_DUPLOYAN:
358     case HB_SCRIPT_GRANTHA:
359     case HB_SCRIPT_KHOJKI:
360     case HB_SCRIPT_KHUDAWADI:
361     case HB_SCRIPT_MAHAJANI:
362     //case HB_SCRIPT_MANICHAEAN:
363     case HB_SCRIPT_MODI:
364     case HB_SCRIPT_PAHAWH_HMONG:
365     //case HB_SCRIPT_PSALTER_PAHLAVI:
366     case HB_SCRIPT_SIDDHAM:
367     case HB_SCRIPT_TIRHUTA:
368
369     /* Unicode-8.0 additions */
370     case HB_SCRIPT_AHOM:
371     //case HB_SCRIPT_MULTANI:
372
373     /* Unicode-9.0 additions */
374     case HB_SCRIPT_BHAIKSUKI:
375     case HB_SCRIPT_MARCHEN:
376     case HB_SCRIPT_NEWA:
377
378     /* Unicode-10.0 additions */
379     case HB_SCRIPT_MASARAM_GONDI:
380     case HB_SCRIPT_SOYOMBO:
381     case HB_SCRIPT_ZANABAZAR_SQUARE:
382
383       /* If the designer designed the font for the 'DFLT' script,
384        * (or we ended up arbitrarily pick 'latn'), use the default shaper.
385        * Otherwise, use the specific shaper.
386        * Note that for some simple scripts, there may not be *any*
387        * GSUB/GPOS needed, so there may be no scripts found! */
388       if (planner->map.chosen_script[0] == HB_TAG ('D','F','L','T') ||
389           planner->map.chosen_script[0] == HB_TAG ('l','a','t','n'))
390         return &_hb_ot_complex_shaper_default;
391       else
392         return &_hb_ot_complex_shaper_use;
393   }
394 }
395
396
397 #endif /* HB_OT_SHAPE_COMPLEX_PRIVATE_HH */