Revert "Imported Upstream version 1.2.7"
[platform/upstream/harfbuzz.git] / src / hb-ot-shape-complex-sea.cc
1 /*
2  * Copyright © 2011,2012,2013  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-indic-private.hh"
28
29 /* buffer var allocations */
30 #define sea_category() complex_var_u8_0() /* indic_category_t */
31 #define sea_position() complex_var_u8_1() /* indic_position_t */
32
33
34 /*
35  * South-East Asian shaper.
36  * Loosely based on the Myanmar spec / shaper.
37  * There is no OpenType spec for this.
38  */
39
40 static const hb_tag_t
41 basic_features[] =
42 {
43   /*
44    * Basic features.
45    * These features are applied in order, one at a time, after initial_reordering.
46    */
47   HB_TAG('p','r','e','f'),
48   HB_TAG('a','b','v','f'),
49   HB_TAG('b','l','w','f'),
50   HB_TAG('p','s','t','f'),
51 };
52 static const hb_tag_t
53 other_features[] =
54 {
55   /*
56    * Other features.
57    * These features are applied all at once, after final_reordering.
58    */
59   HB_TAG('p','r','e','s'),
60   HB_TAG('a','b','v','s'),
61   HB_TAG('b','l','w','s'),
62   HB_TAG('p','s','t','s'),
63   /* Positioning features, though we don't care about the types. */
64   HB_TAG('d','i','s','t'),
65 };
66
67 static void
68 setup_syllables (const hb_ot_shape_plan_t *plan,
69                  hb_font_t *font,
70                  hb_buffer_t *buffer);
71 static void
72 initial_reordering (const hb_ot_shape_plan_t *plan,
73                     hb_font_t *font,
74                     hb_buffer_t *buffer);
75 static void
76 final_reordering (const hb_ot_shape_plan_t *plan,
77                   hb_font_t *font,
78                   hb_buffer_t *buffer);
79
80 static void
81 collect_features_sea (hb_ot_shape_planner_t *plan)
82 {
83   hb_ot_map_builder_t *map = &plan->map;
84
85   /* Do this before any lookups have been applied. */
86   map->add_gsub_pause (setup_syllables);
87
88   map->add_global_bool_feature (HB_TAG('l','o','c','l'));
89   /* The Indic specs do not require ccmp, but we apply it here since if
90    * there is a use of it, it's typically at the beginning. */
91   map->add_global_bool_feature (HB_TAG('c','c','m','p'));
92
93   map->add_gsub_pause (initial_reordering);
94   for (unsigned int i = 0; i < ARRAY_LENGTH (basic_features); i++)
95   {
96     map->add_feature (basic_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ);
97     map->add_gsub_pause (NULL);
98   }
99   map->add_gsub_pause (final_reordering);
100   for (unsigned int i = 0; i < ARRAY_LENGTH (other_features); i++)
101     map->add_feature (other_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ);
102 }
103
104 static void
105 override_features_sea (hb_ot_shape_planner_t *plan)
106 {
107   plan->map.add_feature (HB_TAG('l','i','g','a'), 0, F_GLOBAL);
108 }
109
110
111 enum syllable_type_t {
112   consonant_syllable,
113   broken_cluster,
114   non_sea_cluster,
115 };
116
117 #include "hb-ot-shape-complex-sea-machine.hh"
118
119
120 /* Note: This enum is duplicated in the -machine.rl source file.
121  * Not sure how to avoid duplication. */
122 enum sea_category_t {
123 //  OT_C    = 1,
124   OT_GB   = 12, /* Generic Base XXX DOTTED CIRCLE only for now */
125 //  OT_H    = 4,  /* Halant */
126   OT_IV   = 2,  /* Independent Vowel */
127   OT_MR   = 22, /* Medial Ra */
128 //  OT_CM   = 17, /* Consonant Medial */
129   OT_VAbv = 26,
130   OT_VBlw = 27,
131   OT_VPre = 28,
132   OT_VPst = 29,
133   OT_T    = 3,  /* Tone Marks */
134 //  OT_A    = 10, /* Anusvara */
135 };
136
137 static inline void
138 set_sea_properties (hb_glyph_info_t &info)
139 {
140   hb_codepoint_t u = info.codepoint;
141   unsigned int type = hb_indic_get_categories (u);
142   indic_category_t cat = (indic_category_t) (type & 0x7Fu);
143   indic_position_t pos = (indic_position_t) (type >> 8);
144
145   /* Medial Ra */
146   if (u == 0x1A55u || u == 0xAA34u)
147     cat = (indic_category_t) OT_MR;
148
149   if (cat == OT_M)
150   {
151     switch ((int) pos)
152     {
153       case POS_PRE_C:   cat = (indic_category_t) OT_VPre; break;
154       case POS_ABOVE_C: cat = (indic_category_t) OT_VAbv; break;
155       case POS_BELOW_C: cat = (indic_category_t) OT_VBlw; break;
156       case POS_POST_C:  cat = (indic_category_t) OT_VPst; break;
157     }
158   }
159
160   info.sea_category() = (sea_category_t) cat;
161   info.sea_position() = pos;
162 }
163
164
165 static void
166 setup_masks_sea (const hb_ot_shape_plan_t *plan HB_UNUSED,
167                  hb_buffer_t              *buffer,
168                  hb_font_t                *font HB_UNUSED)
169 {
170   HB_BUFFER_ALLOCATE_VAR (buffer, sea_category);
171   HB_BUFFER_ALLOCATE_VAR (buffer, sea_position);
172
173   /* We cannot setup masks here.  We save information about characters
174    * and setup masks later on in a pause-callback. */
175
176   unsigned int count = buffer->len;
177   hb_glyph_info_t *info = buffer->info;
178   for (unsigned int i = 0; i < count; i++)
179     set_sea_properties (info[i]);
180 }
181
182 static void
183 setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED,
184                  hb_font_t *font HB_UNUSED,
185                  hb_buffer_t *buffer)
186 {
187   find_syllables (buffer);
188 }
189
190 static int
191 compare_sea_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
192 {
193   int a = pa->sea_position();
194   int b = pb->sea_position();
195
196   return a < b ? -1 : a == b ? 0 : +1;
197 }
198
199
200 static void
201 initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan,
202                                        hb_face_t *face,
203                                        hb_buffer_t *buffer,
204                                        unsigned int start, unsigned int end)
205 {
206   hb_glyph_info_t *info = buffer->info;
207   unsigned int base = start;
208
209   /* Reorder! */
210   unsigned int i = start;
211   for (; i < base; i++)
212     info[i].sea_position() = POS_PRE_C;
213   if (i < end)
214   {
215     info[i].sea_position() = POS_BASE_C;
216     i++;
217   }
218   for (; i < end; i++)
219   {
220     if (info[i].sea_category() == OT_MR) /* Pre-base reordering */
221     {
222       info[i].sea_position() = POS_PRE_C;
223       continue;
224     }
225     if (info[i].sea_category() == OT_VPre) /* Left matra */
226     {
227       info[i].sea_position() = POS_PRE_M;
228       continue;
229     }
230
231     info[i].sea_position() = POS_AFTER_MAIN;
232   }
233
234   buffer->merge_clusters (start, end);
235   /* Sit tight, rock 'n roll! */
236   hb_bubble_sort (info + start, end - start, compare_sea_order);
237 }
238
239 static void
240 initial_reordering_broken_cluster (const hb_ot_shape_plan_t *plan,
241                                    hb_face_t *face,
242                                    hb_buffer_t *buffer,
243                                    unsigned int start, unsigned int end)
244 {
245   /* We already inserted dotted-circles, so just call the consonant_syllable. */
246   initial_reordering_consonant_syllable (plan, face, buffer, start, end);
247 }
248
249 static void
250 initial_reordering_non_sea_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED,
251                                     hb_face_t *face HB_UNUSED,
252                                     hb_buffer_t *buffer HB_UNUSED,
253                                     unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
254 {
255   /* Nothing to do right now.  If we ever switch to using the output
256    * buffer in the reordering process, we'd need to next_glyph() here. */
257 }
258
259
260 static void
261 initial_reordering_syllable (const hb_ot_shape_plan_t *plan,
262                              hb_face_t *face,
263                              hb_buffer_t *buffer,
264                              unsigned int start, unsigned int end)
265 {
266   syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F);
267   switch (syllable_type) {
268   case consonant_syllable:      initial_reordering_consonant_syllable  (plan, face, buffer, start, end); return;
269   case broken_cluster:          initial_reordering_broken_cluster      (plan, face, buffer, start, end); return;
270   case non_sea_cluster:         initial_reordering_non_sea_cluster     (plan, face, buffer, start, end); return;
271   }
272 }
273
274 static inline void
275 insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED,
276                        hb_font_t *font,
277                        hb_buffer_t *buffer)
278 {
279   /* Note: This loop is extra overhead, but should not be measurable. */
280   bool has_broken_syllables = false;
281   unsigned int count = buffer->len;
282   hb_glyph_info_t *info = buffer->info;
283   for (unsigned int i = 0; i < count; i++)
284     if ((info[i].syllable() & 0x0F) == broken_cluster)
285     {
286       has_broken_syllables = true;
287       break;
288     }
289   if (likely (!has_broken_syllables))
290     return;
291
292
293   hb_codepoint_t dottedcircle_glyph;
294   if (!font->get_glyph (0x25CCu, 0, &dottedcircle_glyph))
295     return;
296
297   hb_glyph_info_t dottedcircle = {0};
298   dottedcircle.codepoint = 0x25CCu;
299   set_sea_properties (dottedcircle);
300   dottedcircle.codepoint = dottedcircle_glyph;
301
302   buffer->clear_output ();
303
304   buffer->idx = 0;
305   unsigned int last_syllable = 0;
306   while (buffer->idx < buffer->len)
307   {
308     unsigned int syllable = buffer->cur().syllable();
309     syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F);
310     if (unlikely (last_syllable != syllable && syllable_type == broken_cluster))
311     {
312       last_syllable = syllable;
313
314       hb_glyph_info_t info = dottedcircle;
315       info.cluster = buffer->cur().cluster;
316       info.mask = buffer->cur().mask;
317       info.syllable() = buffer->cur().syllable();
318
319       buffer->output_info (info);
320     }
321     else
322       buffer->next_glyph ();
323   }
324
325   buffer->swap_buffers ();
326 }
327
328 static void
329 initial_reordering (const hb_ot_shape_plan_t *plan,
330                     hb_font_t *font,
331                     hb_buffer_t *buffer)
332 {
333   insert_dotted_circles (plan, font, buffer);
334
335   hb_glyph_info_t *info = buffer->info;
336   unsigned int count = buffer->len;
337   if (unlikely (!count)) return;
338   unsigned int last = 0;
339   unsigned int last_syllable = info[0].syllable();
340   for (unsigned int i = 1; i < count; i++)
341     if (last_syllable != info[i].syllable()) {
342       initial_reordering_syllable (plan, font->face, buffer, last, i);
343       last = i;
344       last_syllable = info[last].syllable();
345     }
346   initial_reordering_syllable (plan, font->face, buffer, last, count);
347 }
348
349 static void
350 final_reordering (const hb_ot_shape_plan_t *plan,
351                   hb_font_t *font HB_UNUSED,
352                   hb_buffer_t *buffer)
353 {
354   hb_glyph_info_t *info = buffer->info;
355   unsigned int count = buffer->len;
356
357   /* Zero syllables now... */
358   for (unsigned int i = 0; i < count; i++)
359     info[i].syllable() = 0;
360
361   HB_BUFFER_DEALLOCATE_VAR (buffer, sea_category);
362   HB_BUFFER_DEALLOCATE_VAR (buffer, sea_position);
363 }
364
365
366 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_sea =
367 {
368   "sea",
369   collect_features_sea,
370   override_features_sea,
371   NULL, /* data_create */
372   NULL, /* data_destroy */
373   NULL, /* preprocess_text */
374   HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT,
375   NULL, /* decompose */
376   NULL, /* compose */
377   setup_masks_sea,
378   HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
379   false, /* fallback_position */
380 };