[Indic] Apply Indic features
[framework/uifw/harfbuzz.git] / src / hb-ot-shape-complex-indic.cc
1 /*
2  * Copyright © 2011  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 #include "hb-ot-shape-complex-private.hh"
28
29 HB_BEGIN_DECLS
30
31
32 /* buffer var allocations */
33 #define indic_category() complex_var_persistent_u8_0() /* indic_category_t */
34 #define indic_position() complex_var_persistent_u8_1() /* indic_matra_category_t */
35
36 #define INDIC_TABLE_ELEMENT_TYPE uint8_t
37
38 /* Cateories used in the OpenType spec:
39  * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx
40  */
41 /* Note: This enum is duplicated in the -machine.rl source file.
42  * Not sure how to avoid duplication. */
43 enum indic_category_t {
44   OT_X = 0,
45   OT_C,
46   OT_Ra, /* Not explicitly listed in the OT spec, but used in the grammar. */
47   OT_V,
48   OT_N,
49   OT_H,
50   OT_ZWNJ,
51   OT_ZWJ,
52   OT_M,
53   OT_SM,
54   OT_VD,
55   OT_A,
56   OT_NBSP
57 };
58
59 /* Visual positions in a syllable from left to right. */
60 enum indic_position_t {
61   POS_PRE,
62   POS_BASE,
63   POS_ABOVE,
64   POS_BELOW,
65   POS_POST,
66
67   POS_INHERIT /* For Halant, Nukta, ZWJ, ZWNJ */
68 };
69
70 /* Categories used in IndicSyllabicCategory.txt from UCD */
71 /* The assignments are guesswork */
72 enum indic_syllabic_category_t {
73   INDIC_SYLLABIC_CATEGORY_OTHER                 = OT_X,
74
75   INDIC_SYLLABIC_CATEGORY_AVAGRAHA              = OT_X,
76   INDIC_SYLLABIC_CATEGORY_BINDU                 = OT_SM,
77   INDIC_SYLLABIC_CATEGORY_CONSONANT             = OT_C,
78   INDIC_SYLLABIC_CATEGORY_CONSONANT_DEAD        = OT_C,
79   INDIC_SYLLABIC_CATEGORY_CONSONANT_FINAL       = OT_C,
80   INDIC_SYLLABIC_CATEGORY_CONSONANT_HEAD_LETTER = OT_C,
81   INDIC_SYLLABIC_CATEGORY_CONSONANT_MEDIAL      = OT_C,
82   INDIC_SYLLABIC_CATEGORY_CONSONANT_PLACEHOLDER = OT_NBSP,
83   INDIC_SYLLABIC_CATEGORY_CONSONANT_SUBJOINED   = OT_C,
84   INDIC_SYLLABIC_CATEGORY_CONSONANT_REPHA       = OT_C,
85   INDIC_SYLLABIC_CATEGORY_MODIFYING_LETTER      = OT_X,
86   INDIC_SYLLABIC_CATEGORY_NUKTA                 = OT_N,
87   INDIC_SYLLABIC_CATEGORY_REGISTER_SHIFTER      = OT_X,
88   INDIC_SYLLABIC_CATEGORY_TONE_LETTER           = OT_X,
89   INDIC_SYLLABIC_CATEGORY_TONE_MARK             = OT_X,
90   INDIC_SYLLABIC_CATEGORY_VIRAMA                = OT_H,
91   INDIC_SYLLABIC_CATEGORY_VISARGA               = OT_SM,
92   INDIC_SYLLABIC_CATEGORY_VOWEL                 = OT_V,
93   INDIC_SYLLABIC_CATEGORY_VOWEL_DEPENDENT       = OT_M,
94   INDIC_SYLLABIC_CATEGORY_VOWEL_INDEPENDENT     = OT_V
95 };
96
97 /* Categories used in IndicSMatraCategory.txt from UCD */
98 enum indic_matra_category_t {
99   INDIC_MATRA_CATEGORY_NOT_APPLICABLE           = POS_BASE,
100
101   INDIC_MATRA_CATEGORY_LEFT                     = POS_PRE,
102   INDIC_MATRA_CATEGORY_TOP                      = POS_ABOVE,
103   INDIC_MATRA_CATEGORY_BOTTOM                   = POS_BELOW,
104   INDIC_MATRA_CATEGORY_RIGHT                    = POS_POST,
105
106   /* We don't really care much about these since we decompose them
107    * in the generic pre-shaping layer.  They will only be used if
108    * the font does not cover the decomposition.  In which case, we
109    * define these as aliases to the place we want the split-matra
110    * glyph to show up.  Quite arbitrary. */
111   INDIC_MATRA_CATEGORY_BOTTOM_AND_RIGHT         = INDIC_MATRA_CATEGORY_BOTTOM,
112   INDIC_MATRA_CATEGORY_LEFT_AND_RIGHT           = INDIC_MATRA_CATEGORY_LEFT,
113   INDIC_MATRA_CATEGORY_TOP_AND_BOTTOM           = INDIC_MATRA_CATEGORY_BOTTOM,
114   INDIC_MATRA_CATEGORY_TOP_AND_BOTTOM_AND_RIGHT = INDIC_MATRA_CATEGORY_BOTTOM,
115   INDIC_MATRA_CATEGORY_TOP_AND_LEFT             = INDIC_MATRA_CATEGORY_LEFT,
116   INDIC_MATRA_CATEGORY_TOP_AND_LEFT_AND_RIGHT   = INDIC_MATRA_CATEGORY_LEFT,
117   INDIC_MATRA_CATEGORY_TOP_AND_RIGHT            = INDIC_MATRA_CATEGORY_RIGHT,
118
119   INDIC_MATRA_CATEGORY_INVISIBLE                = INDIC_MATRA_CATEGORY_NOT_APPLICABLE,
120   INDIC_MATRA_CATEGORY_OVERSTRUCK               = INDIC_MATRA_CATEGORY_NOT_APPLICABLE,
121   INDIC_MATRA_CATEGORY_VISUAL_ORDER_LEFT        = INDIC_MATRA_CATEGORY_NOT_APPLICABLE
122 };
123
124 /* Note: We use ASSERT_STATIC_EXPR_ZERO() instead of ASSERT_STATIC_EXPR() and the comma operation
125  * because gcc fails to optimize the latter and fills the table in at runtime. */
126 #define INDIC_COMBINE_CATEGORIES(S,M) \
127   (ASSERT_STATIC_EXPR_ZERO (M == INDIC_MATRA_CATEGORY_NOT_APPLICABLE || (S == INDIC_SYLLABIC_CATEGORY_VIRAMA || S == INDIC_SYLLABIC_CATEGORY_VOWEL_DEPENDENT)) + \
128    ASSERT_STATIC_EXPR_ZERO (S < 16 && M < 16) + \
129    ((M << 4) | S))
130
131 #include "hb-ot-shape-complex-indic-table.hh"
132
133 /* XXX
134  * This is a hack for now.  We should:
135  * 1. Move this data into the main Indic table,
136  * and/or
137  * 2. Probe font lookups to determine consonant positions.
138  */
139 static const struct consonant_position_t {
140   hb_codepoint_t u;
141   indic_position_t position;
142 } consonant_positions[] = {
143   {0x0930, POS_BELOW},
144   {0x09AC, POS_BELOW},
145   {0x09AF, POS_POST},
146   {0x09B0, POS_BELOW},
147   {0x09F0, POS_BELOW},
148   {0x0A2F, POS_POST},
149   {0x0A30, POS_BELOW},
150   {0x0A35, POS_BELOW},
151   {0x0A39, POS_BELOW},
152   {0x0AB0, POS_BELOW},
153   {0x0B24, POS_BELOW},
154   {0x0B28, POS_BELOW},
155   {0x0B2C, POS_BELOW},
156   {0x0B2D, POS_BELOW},
157   {0x0B2E, POS_BELOW},
158   {0x0B2F, POS_POST},
159   {0x0B30, POS_BELOW},
160   {0x0B32, POS_BELOW},
161   {0x0B33, POS_BELOW},
162   {0x0B5F, POS_POST},
163   {0x0B71, POS_BELOW},
164   {0x0C15, POS_BELOW},
165   {0x0C16, POS_BELOW},
166   {0x0C17, POS_BELOW},
167   {0x0C18, POS_BELOW},
168   {0x0C19, POS_BELOW},
169   {0x0C1A, POS_BELOW},
170   {0x0C1B, POS_BELOW},
171   {0x0C1C, POS_BELOW},
172   {0x0C1D, POS_BELOW},
173   {0x0C1E, POS_BELOW},
174   {0x0C1F, POS_BELOW},
175   {0x0C20, POS_BELOW},
176   {0x0C21, POS_BELOW},
177   {0x0C22, POS_BELOW},
178   {0x0C23, POS_BELOW},
179   {0x0C24, POS_BELOW},
180   {0x0C25, POS_BELOW},
181   {0x0C26, POS_BELOW},
182   {0x0C27, POS_BELOW},
183   {0x0C28, POS_BELOW},
184   {0x0C2A, POS_BELOW},
185   {0x0C2B, POS_BELOW},
186   {0x0C2C, POS_BELOW},
187   {0x0C2D, POS_BELOW},
188   {0x0C2E, POS_BELOW},
189   {0x0C2F, POS_BELOW},
190   {0x0C30, POS_BELOW},
191   {0x0C32, POS_BELOW},
192   {0x0C33, POS_BELOW},
193   {0x0C35, POS_BELOW},
194   {0x0C36, POS_BELOW},
195   {0x0C37, POS_BELOW},
196   {0x0C38, POS_BELOW},
197   {0x0C39, POS_BELOW},
198   {0x0C95, POS_BELOW},
199   {0x0C96, POS_BELOW},
200   {0x0C97, POS_BELOW},
201   {0x0C98, POS_BELOW},
202   {0x0C99, POS_BELOW},
203   {0x0C9A, POS_BELOW},
204   {0x0C9B, POS_BELOW},
205   {0x0C9C, POS_BELOW},
206   {0x0C9D, POS_BELOW},
207   {0x0C9E, POS_BELOW},
208   {0x0C9F, POS_BELOW},
209   {0x0CA0, POS_BELOW},
210   {0x0CA1, POS_BELOW},
211   {0x0CA2, POS_BELOW},
212   {0x0CA3, POS_BELOW},
213   {0x0CA4, POS_BELOW},
214   {0x0CA5, POS_BELOW},
215   {0x0CA6, POS_BELOW},
216   {0x0CA7, POS_BELOW},
217   {0x0CA8, POS_BELOW},
218   {0x0CAA, POS_BELOW},
219   {0x0CAB, POS_BELOW},
220   {0x0CAC, POS_BELOW},
221   {0x0CAD, POS_BELOW},
222   {0x0CAE, POS_BELOW},
223   {0x0CAF, POS_BELOW},
224   {0x0CB0, POS_BELOW},
225   {0x0CB2, POS_BELOW},
226   {0x0CB3, POS_BELOW},
227   {0x0CB5, POS_BELOW},
228   {0x0CB6, POS_BELOW},
229   {0x0CB7, POS_BELOW},
230   {0x0CB8, POS_BELOW},
231   {0x0CB9, POS_BELOW},
232   {0x0CDE, POS_BELOW},
233   {0x0D2F, POS_POST},
234   {0x0D30, POS_POST},
235   {0x0D32, POS_BELOW},
236   {0x0D35, POS_POST},
237 };
238
239 static int
240 compare_codepoint (const void *pa, const void *pb)
241 {
242   hb_codepoint_t a = * (hb_codepoint_t *) pa;
243   hb_codepoint_t b = * (hb_codepoint_t *) pb;
244
245   return a < b ? -1 : a == b ? 0 : +1;
246 }
247
248 static indic_position_t
249 consonant_position (hb_codepoint_t u)
250 {
251   consonant_position_t *record;
252
253   record = (consonant_position_t *) bsearch (&u, consonant_positions,
254                                              ARRAY_LENGTH (consonant_positions),
255                                              sizeof (consonant_positions[0]),
256                                              compare_codepoint);
257
258   return record ? record->position : POS_BASE;
259 }
260
261
262 static const struct {
263   hb_tag_t tag;
264   hb_bool_t is_global;
265 } indic_basic_features[] =
266 {
267   {HB_TAG('n','u','k','t'), true},
268   {HB_TAG('a','k','h','n'), false},
269   {HB_TAG('r','p','h','f'), false},
270   {HB_TAG('r','k','r','f'), false},
271   {HB_TAG('p','r','e','f'), false},
272   {HB_TAG('b','l','w','f'), false},
273   {HB_TAG('h','a','l','f'), false},
274   {HB_TAG('v','a','t','u'), true},
275   {HB_TAG('p','s','t','f'), false},
276   {HB_TAG('c','j','c','t'), true},
277 };
278
279 /* Same order as the indic_basic_features array */
280 enum {
281   _NUKT,
282   AKHN,
283   RPHF,
284   RKRF,
285   PREF,
286   BLWF,
287   HALF,
288   _VATU,
289   PSTF,
290   _CJCT,
291 };
292
293 static const hb_tag_t indic_other_features[] =
294 {
295   HB_TAG('p','r','e','s'),
296   HB_TAG('a','b','v','s'),
297   HB_TAG('b','l','w','s'),
298   HB_TAG('p','s','t','s'),
299   HB_TAG('h','a','l','n'),
300
301   HB_TAG('d','i','s','t'),
302   HB_TAG('a','b','v','m'),
303   HB_TAG('b','l','w','m'),
304 };
305
306
307 static void
308 initial_reordering (const hb_ot_map_t *map,
309                     hb_face_t *face,
310                     hb_buffer_t *buffer,
311                     void *user_data HB_UNUSED);
312 static void
313 final_reordering (const hb_ot_map_t *map,
314                   hb_face_t *face,
315                   hb_buffer_t *buffer,
316                   void *user_data HB_UNUSED);
317
318 void
319 _hb_ot_shape_complex_collect_features_indic (hb_ot_map_builder_t *map, const hb_segment_properties_t  *props)
320 {
321   map->add_bool_feature (HB_TAG('l','o','c','l'));
322   /* The Indic specs do not require ccmp, but we apply it here since if
323    * there is a use of it, it's typically at the beginning. */
324   map->add_bool_feature (HB_TAG('c','c','m','p'));
325
326   map->add_gsub_pause (initial_reordering, NULL);
327
328   for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++)
329     map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
330
331   map->add_gsub_pause (final_reordering, NULL);
332
333   for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++)
334     map->add_bool_feature (indic_other_features[i], true);
335 }
336
337
338 bool
339 _hb_ot_shape_complex_prefer_decomposed_indic (void)
340 {
341   /* We want split matras decomposed by the common shaping logic. */
342   return TRUE;
343 }
344
345
346 void
347 _hb_ot_shape_complex_setup_masks_indic (hb_ot_map_t *map, hb_buffer_t *buffer)
348 {
349   HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
350   HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
351
352   /* We cannot setup masks here.  We save information about characters
353    * and setup masks later on in a pause-callback. */
354
355   unsigned int count = buffer->len;
356   for (unsigned int i = 0; i < count; i++)
357   {
358     unsigned int type = get_indic_categories (buffer->info[i].codepoint);
359
360     buffer->info[i].indic_category() = type & 0x0F;
361     buffer->info[i].indic_position() = type >> 4;
362
363     if (buffer->info[i].indic_category() == OT_C)
364       buffer->info[i].indic_position() = consonant_position (buffer->info[i].codepoint);
365   }
366 }
367
368
369 static void
370 found_consonant_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
371                           unsigned int start, unsigned int end)
372 {
373   unsigned int i;
374
375   /* Comments from:
376    * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
377
378   /* 1. Find base consonant:
379    *
380    * The shaping engine finds the base consonant of the syllable, using the
381    * following algorithm: starting from the end of the syllable, move backwards
382    * until a consonant is found that does not have a below-base or post-base
383    * form (post-base forms have to follow below-base forms), or that is not a
384    * pre-base reordering Ra, or arrive at the first consonant. The consonant
385    * stopped at will be the base.
386    *
387    *   o If the syllable starts with Ra + Halant (in a script that has Reph)
388    *     and has more than one consonant, Ra is excluded from candidates for
389    *     base consonants.
390    */
391
392   unsigned int base = 0;
393
394   /* -> starting from the end of the syllable, move backwards */
395   i = end;
396   do {
397     i--;
398     /* -> until a consonant is found */
399     if (buffer->info[i].indic_category() == OT_C)
400     {
401       /* -> that does not have a below-base or post-base form
402        * (post-base forms have to follow below-base forms), */
403       if (buffer->info[i].indic_position() != POS_BELOW &&
404           buffer->info[i].indic_position() != POS_POST)
405       {
406         base = i;
407         break;
408       }
409
410       /* TODO: or that is not a pre-base reordering Ra, */
411
412       /* -> or arrive at the first consonant. The consonant stopped at will be the base. */
413       base = i;
414     }
415   } while (i > start);
416   if (base < start)
417     base = start; /* Just in case... */
418
419   /* TODO
420    * If the syllable starts with Ra + Halant (in a script that has Reph)
421    * and has more than one consonant, Ra is excluded from candidates for
422    * base consonants. */
423
424
425   /* 2. Decompose and reorder Matras:
426    *
427    * Each matra and any syllable modifier sign in the cluster are moved to the
428    * appropriate position relative to the consonant(s) in the cluster. The
429    * shaping engine decomposes two- or three-part matras into their constituent
430    * parts before any repositioning. Matra characters are classified by which
431    * consonant in a conjunct they have affinity for and are reordered to the
432    * following positions:
433    *
434    *   o Before first half form in the syllable
435    *   o After subjoined consonants
436    *   o After post-form consonant
437    *   o After main consonant (for above marks)
438    *
439    * IMPLEMENTATION NOTES:
440    *
441    * The normalize() routine has already decomposed matras for us, so we don't
442    * need to worry about that.
443    */
444
445
446   /* 3.  Reorder marks to canonical order:
447    *
448    * Adjacent nukta and halant or nukta and vedic sign are always repositioned
449    * if necessary, so that the nukta is first.
450    *
451    * IMPLEMENTATION NOTES:
452    *
453    * We don't need to do this: the normalize() routine already did this for us.
454    */
455
456
457   /* Setup masks now */
458
459   /* Pre-base */
460   for (i = start; i < base; i++)
461     buffer->info[i].mask  |= mask_array[HALF] | mask_array[AKHN];
462   /* Base */
463   buffer->info[base].mask |= mask_array[AKHN];
464   /* Post-base */
465   for (i = base + 1; i < end; i++)
466     buffer->info[i].mask  |= mask_array[BLWF] | mask_array[PSTF];
467 }
468
469
470 static void
471 found_vowel_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
472                       unsigned int start, unsigned int end)
473 {
474   /* TODO
475    * Not clear to me how this should work.  Do the matras move to before the
476    * independent vowel?  No idea.
477    */
478 }
479
480 static void
481 found_standalone_cluster (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
482                           unsigned int start, unsigned int end)
483 {
484   /* TODO
485    * Easiest thing to do here is to convert the NBSP to consonant and
486    * call found_consonant_syllable.
487    */
488 }
489
490 static void
491 found_non_indic (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
492                  unsigned int start, unsigned int end)
493 {
494   /* Nothing to do right now.  If we ever switch to using the output
495    * buffer in the reordering process, we'd need to next_glyph() here. */
496 }
497
498 #include "hb-ot-shape-complex-indic-machine.hh"
499
500 static void
501 initial_reordering (const hb_ot_map_t *map,
502                     hb_face_t *face,
503                     hb_buffer_t *buffer,
504                     void *user_data HB_UNUSED)
505 {
506   hb_mask_t mask_array[ARRAY_LENGTH (indic_basic_features)] = {0};
507   unsigned int num_masks = ARRAY_LENGTH (indic_basic_features);
508   for (unsigned int i = 0; i < num_masks; i++)
509     mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
510
511   find_syllables (map, buffer, mask_array);
512 }
513
514 static void
515 final_reordering (const hb_ot_map_t *map,
516                   hb_face_t *face,
517                   hb_buffer_t *buffer,
518                   void *user_data HB_UNUSED)
519 {
520   HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
521   HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
522 }
523
524
525
526 HB_END_DECLS