Revert of Update the extension whitelist for application host change. (patchset ...
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-ot-shape-complex-myanmar.cc
blobd016380cc6382f02f5b07dcf8481a235a9c95a24
1 /*
2 * Copyright © 2011,2012,2013 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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.
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.
24 * Google Author(s): Behdad Esfahbod
27 #include "hb-ot-shape-complex-indic-private.hh"
29 /* buffer var allocations */
30 #define myanmar_category() complex_var_u8_0() /* myanmar_category_t */
31 #define myanmar_position() complex_var_u8_1() /* myanmar_position_t */
35 * Myanmar shaper.
38 static const hb_tag_t
39 basic_features[] =
42 * Basic features.
43 * These features are applied in order, one at a time, after initial_reordering.
45 HB_TAG('r','p','h','f'),
46 HB_TAG('p','r','e','f'),
47 HB_TAG('b','l','w','f'),
48 HB_TAG('p','s','t','f'),
50 static const hb_tag_t
51 other_features[] =
54 * Other features.
55 * These features are applied all at once, after final_reordering.
57 HB_TAG('p','r','e','s'),
58 HB_TAG('a','b','v','s'),
59 HB_TAG('b','l','w','s'),
60 HB_TAG('p','s','t','s'),
61 /* Positioning features, though we don't care about the types. */
62 HB_TAG('d','i','s','t'),
63 /* Pre-release version of Windows 8 Myanmar font had abvm,blwm
64 * features. The released Windows 8 version of the font (as well
65 * as the released spec) used 'mark' instead. The Windows 8
66 * shaper however didn't apply 'mark' but did apply 'mkmk'.
67 * Perhaps it applied abvm/blwm. This was fixed in a Windows 8
68 * update, so now it applies mark/mkmk. We are guessing that
69 * it still applies abvm/blwm too.
71 HB_TAG('a','b','v','m'),
72 HB_TAG('b','l','w','m'),
75 static void
76 setup_syllables (const hb_ot_shape_plan_t *plan,
77 hb_font_t *font,
78 hb_buffer_t *buffer);
79 static void
80 initial_reordering (const hb_ot_shape_plan_t *plan,
81 hb_font_t *font,
82 hb_buffer_t *buffer);
83 static void
84 final_reordering (const hb_ot_shape_plan_t *plan,
85 hb_font_t *font,
86 hb_buffer_t *buffer);
88 static void
89 collect_features_myanmar (hb_ot_shape_planner_t *plan)
91 hb_ot_map_builder_t *map = &plan->map;
93 /* Do this before any lookups have been applied. */
94 map->add_gsub_pause (setup_syllables);
96 map->add_global_bool_feature (HB_TAG('l','o','c','l'));
97 /* The Indic specs do not require ccmp, but we apply it here since if
98 * there is a use of it, it's typically at the beginning. */
99 map->add_global_bool_feature (HB_TAG('c','c','m','p'));
102 map->add_gsub_pause (initial_reordering);
103 for (unsigned int i = 0; i < ARRAY_LENGTH (basic_features); i++)
105 map->add_feature (basic_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ);
106 map->add_gsub_pause (NULL);
108 map->add_gsub_pause (final_reordering);
109 for (unsigned int i = 0; i < ARRAY_LENGTH (other_features); i++)
110 map->add_feature (other_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ);
113 static void
114 override_features_myanmar (hb_ot_shape_planner_t *plan)
116 plan->map.add_feature (HB_TAG('l','i','g','a'), 0, F_GLOBAL);
120 enum syllable_type_t {
121 consonant_syllable,
122 punctuation_cluster,
123 broken_cluster,
124 non_myanmar_cluster,
127 #include "hb-ot-shape-complex-myanmar-machine.hh"
130 /* Note: This enum is duplicated in the -machine.rl source file.
131 * Not sure how to avoid duplication. */
132 enum myanmar_category_t {
133 OT_As = 18, /* Asat */
134 OT_D = 19, /* Digits except zero */
135 OT_D0 = 20, /* Digit zero */
136 OT_DB = OT_N, /* Dot below */
137 OT_GB = OT_PLACEHOLDER,
138 OT_MH = 21, /* Various consonant medial types */
139 OT_MR = 22, /* Various consonant medial types */
140 OT_MW = 23, /* Various consonant medial types */
141 OT_MY = 24, /* Various consonant medial types */
142 OT_PT = 25, /* Pwo and other tones */
143 OT_VAbv = 26,
144 OT_VBlw = 27,
145 OT_VPre = 28,
146 OT_VPst = 29,
147 OT_VS = 30, /* Variation selectors */
148 OT_P = 31 /* Punctuation */
152 static inline bool
153 is_one_of (const hb_glyph_info_t &info, unsigned int flags)
155 /* If it ligated, all bets are off. */
156 if (_hb_glyph_info_ligated (&info)) return false;
157 return !!(FLAG (info.myanmar_category()) & flags);
160 static inline bool
161 is_consonant (const hb_glyph_info_t &info)
163 return is_one_of (info, CONSONANT_FLAGS);
167 static inline void
168 set_myanmar_properties (hb_glyph_info_t &info)
170 hb_codepoint_t u = info.codepoint;
171 unsigned int type = hb_indic_get_categories (u);
172 indic_category_t cat = (indic_category_t) (type & 0x7Fu);
173 indic_position_t pos = (indic_position_t) (type >> 8);
175 /* Myanmar
176 * http://www.microsoft.com/typography/OpenTypeDev/myanmar/intro.htm#analyze
178 if (unlikely (hb_in_range (u, 0xFE00u, 0xFE0Fu)))
179 cat = (indic_category_t) OT_VS;
181 switch (u)
183 case 0x104Eu:
184 cat = (indic_category_t) OT_C; /* The spec says C, IndicSyllableCategory doesn't have. */
185 break;
187 case 0x002Du: case 0x00A0u: case 0x00D7u: case 0x2012u:
188 case 0x2013u: case 0x2014u: case 0x2015u: case 0x2022u:
189 case 0x25CCu: case 0x25FBu: case 0x25FCu: case 0x25FDu:
190 case 0x25FEu:
191 cat = (indic_category_t) OT_GB;
192 break;
194 case 0x1004u: case 0x101Bu: case 0x105Au:
195 cat = (indic_category_t) OT_Ra;
196 break;
198 case 0x1032u: case 0x1036u:
199 cat = (indic_category_t) OT_A;
200 break;
202 case 0x103Au:
203 cat = (indic_category_t) OT_As;
204 break;
206 case 0x1041u: case 0x1042u: case 0x1043u: case 0x1044u:
207 case 0x1045u: case 0x1046u: case 0x1047u: case 0x1048u:
208 case 0x1049u: case 0x1090u: case 0x1091u: case 0x1092u:
209 case 0x1093u: case 0x1094u: case 0x1095u: case 0x1096u:
210 case 0x1097u: case 0x1098u: case 0x1099u:
211 cat = (indic_category_t) OT_D;
212 break;
214 case 0x1040u:
215 cat = (indic_category_t) OT_D; /* XXX The spec says D0, but Uniscribe doesn't seem to do. */
216 break;
218 case 0x103Eu: case 0x1060u:
219 cat = (indic_category_t) OT_MH;
220 break;
222 case 0x103Cu:
223 cat = (indic_category_t) OT_MR;
224 break;
226 case 0x103Du: case 0x1082u:
227 cat = (indic_category_t) OT_MW;
228 break;
230 case 0x103Bu: case 0x105Eu: case 0x105Fu:
231 cat = (indic_category_t) OT_MY;
232 break;
234 case 0x1063u: case 0x1064u: case 0x1069u: case 0x106Au:
235 case 0x106Bu: case 0x106Cu: case 0x106Du: case 0xAA7Bu:
236 cat = (indic_category_t) OT_PT;
237 break;
239 case 0x1038u: case 0x1087u: case 0x1088u: case 0x1089u:
240 case 0x108Au: case 0x108Bu: case 0x108Cu: case 0x108Du:
241 case 0x108Fu: case 0x109Au: case 0x109Bu: case 0x109Cu:
242 cat = (indic_category_t) OT_SM;
243 break;
245 case 0x104Au: case 0x104Bu:
246 cat = (indic_category_t) OT_P;
247 break;
250 if (cat == OT_M)
252 switch ((int) pos)
254 case POS_PRE_C: cat = (indic_category_t) OT_VPre;
255 pos = POS_PRE_M; break;
256 case POS_ABOVE_C: cat = (indic_category_t) OT_VAbv; break;
257 case POS_BELOW_C: cat = (indic_category_t) OT_VBlw; break;
258 case POS_POST_C: cat = (indic_category_t) OT_VPst; break;
262 info.myanmar_category() = (myanmar_category_t) cat;
263 info.myanmar_position() = pos;
268 static void
269 setup_masks_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
270 hb_buffer_t *buffer,
271 hb_font_t *font HB_UNUSED)
273 HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_category);
274 HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_position);
276 /* We cannot setup masks here. We save information about characters
277 * and setup masks later on in a pause-callback. */
279 unsigned int count = buffer->len;
280 hb_glyph_info_t *info = buffer->info;
281 for (unsigned int i = 0; i < count; i++)
282 set_myanmar_properties (info[i]);
285 static void
286 setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED,
287 hb_font_t *font HB_UNUSED,
288 hb_buffer_t *buffer)
290 find_syllables (buffer);
293 static int
294 compare_myanmar_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
296 int a = pa->myanmar_position();
297 int b = pb->myanmar_position();
299 return a < b ? -1 : a == b ? 0 : +1;
303 /* Rules from:
304 * http://www.microsoft.com/typography/OpenTypeDev/myanmar/intro.htm */
306 static void
307 initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan,
308 hb_face_t *face,
309 hb_buffer_t *buffer,
310 unsigned int start, unsigned int end)
312 hb_glyph_info_t *info = buffer->info;
314 unsigned int base = end;
315 bool has_reph = false;
318 unsigned int limit = start;
319 if (start + 3 <= end &&
320 info[start ].myanmar_category() == OT_Ra &&
321 info[start+1].myanmar_category() == OT_As &&
322 info[start+2].myanmar_category() == OT_H)
324 limit += 3;
325 base = start;
326 has_reph = true;
330 if (!has_reph)
331 base = limit;
333 for (unsigned int i = limit; i < end; i++)
334 if (is_consonant (info[i]))
336 base = i;
337 break;
342 /* Reorder! */
344 unsigned int i = start;
345 for (; i < start + (has_reph ? 3 : 0); i++)
346 info[i].myanmar_position() = POS_AFTER_MAIN;
347 for (; i < base; i++)
348 info[i].myanmar_position() = POS_PRE_C;
349 if (i < end)
351 info[i].myanmar_position() = POS_BASE_C;
352 i++;
354 indic_position_t pos = POS_AFTER_MAIN;
355 /* The following loop may be ugly, but it implements all of
356 * Myanmar reordering! */
357 for (; i < end; i++)
359 if (info[i].myanmar_category() == OT_MR) /* Pre-base reordering */
361 info[i].myanmar_position() = POS_PRE_C;
362 continue;
364 if (info[i].myanmar_position() < POS_BASE_C) /* Left matra */
366 continue;
369 if (pos == POS_AFTER_MAIN && info[i].myanmar_category() == OT_VBlw)
371 pos = POS_BELOW_C;
372 info[i].myanmar_position() = pos;
373 continue;
376 if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_A)
378 info[i].myanmar_position() = POS_BEFORE_SUB;
379 continue;
381 if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_VBlw)
383 info[i].myanmar_position() = pos;
384 continue;
386 if (pos == POS_BELOW_C && info[i].myanmar_category() != OT_A)
388 pos = POS_AFTER_SUB;
389 info[i].myanmar_position() = pos;
390 continue;
392 info[i].myanmar_position() = pos;
396 buffer->merge_clusters (start, end);
397 /* Sit tight, rock 'n roll! */
398 hb_bubble_sort (info + start, end - start, compare_myanmar_order);
401 static void
402 initial_reordering_broken_cluster (const hb_ot_shape_plan_t *plan,
403 hb_face_t *face,
404 hb_buffer_t *buffer,
405 unsigned int start, unsigned int end)
407 /* We already inserted dotted-circles, so just call the consonant_syllable. */
408 initial_reordering_consonant_syllable (plan, face, buffer, start, end);
411 static void
412 initial_reordering_punctuation_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED,
413 hb_face_t *face HB_UNUSED,
414 hb_buffer_t *buffer HB_UNUSED,
415 unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
417 /* Nothing to do right now. If we ever switch to using the output
418 * buffer in the reordering process, we'd need to next_glyph() here. */
421 static void
422 initial_reordering_non_myanmar_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED,
423 hb_face_t *face HB_UNUSED,
424 hb_buffer_t *buffer HB_UNUSED,
425 unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
427 /* Nothing to do right now. If we ever switch to using the output
428 * buffer in the reordering process, we'd need to next_glyph() here. */
432 static void
433 initial_reordering_syllable (const hb_ot_shape_plan_t *plan,
434 hb_face_t *face,
435 hb_buffer_t *buffer,
436 unsigned int start, unsigned int end)
438 syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F);
439 switch (syllable_type) {
440 case consonant_syllable: initial_reordering_consonant_syllable (plan, face, buffer, start, end); return;
441 case punctuation_cluster: initial_reordering_punctuation_cluster (plan, face, buffer, start, end); return;
442 case broken_cluster: initial_reordering_broken_cluster (plan, face, buffer, start, end); return;
443 case non_myanmar_cluster: initial_reordering_non_myanmar_cluster (plan, face, buffer, start, end); return;
447 static inline void
448 insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED,
449 hb_font_t *font,
450 hb_buffer_t *buffer)
452 /* Note: This loop is extra overhead, but should not be measurable. */
453 bool has_broken_syllables = false;
454 unsigned int count = buffer->len;
455 hb_glyph_info_t *info = buffer->info;
456 for (unsigned int i = 0; i < count; i++)
457 if ((info[i].syllable() & 0x0F) == broken_cluster)
459 has_broken_syllables = true;
460 break;
462 if (likely (!has_broken_syllables))
463 return;
466 hb_codepoint_t dottedcircle_glyph;
467 if (!font->get_glyph (0x25CCu, 0, &dottedcircle_glyph))
468 return;
470 hb_glyph_info_t dottedcircle = {0};
471 dottedcircle.codepoint = 0x25CCu;
472 set_myanmar_properties (dottedcircle);
473 dottedcircle.codepoint = dottedcircle_glyph;
475 buffer->clear_output ();
477 buffer->idx = 0;
478 unsigned int last_syllable = 0;
479 while (buffer->idx < buffer->len)
481 unsigned int syllable = buffer->cur().syllable();
482 syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F);
483 if (unlikely (last_syllable != syllable && syllable_type == broken_cluster))
485 last_syllable = syllable;
487 hb_glyph_info_t info = dottedcircle;
488 info.cluster = buffer->cur().cluster;
489 info.mask = buffer->cur().mask;
490 info.syllable() = buffer->cur().syllable();
492 buffer->output_info (info);
494 else
495 buffer->next_glyph ();
498 buffer->swap_buffers ();
501 static void
502 initial_reordering (const hb_ot_shape_plan_t *plan,
503 hb_font_t *font,
504 hb_buffer_t *buffer)
506 insert_dotted_circles (plan, font, buffer);
508 hb_glyph_info_t *info = buffer->info;
509 unsigned int count = buffer->len;
510 if (unlikely (!count)) return;
511 unsigned int last = 0;
512 unsigned int last_syllable = info[0].syllable();
513 for (unsigned int i = 1; i < count; i++)
514 if (last_syllable != info[i].syllable()) {
515 initial_reordering_syllable (plan, font->face, buffer, last, i);
516 last = i;
517 last_syllable = info[last].syllable();
519 initial_reordering_syllable (plan, font->face, buffer, last, count);
522 static void
523 final_reordering (const hb_ot_shape_plan_t *plan,
524 hb_font_t *font HB_UNUSED,
525 hb_buffer_t *buffer)
527 hb_glyph_info_t *info = buffer->info;
528 unsigned int count = buffer->len;
530 /* Zero syllables now... */
531 for (unsigned int i = 0; i < count; i++)
532 info[i].syllable() = 0;
534 HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_category);
535 HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_position);
539 /* Uniscribe seems to have a shaper for 'mymr' that is like the
540 * generic shaper, except that it zeros mark advances GDEF_LATE. */
541 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar_old =
543 "default",
544 NULL, /* collect_features */
545 NULL, /* override_features */
546 NULL, /* data_create */
547 NULL, /* data_destroy */
548 NULL, /* preprocess_text */
549 HB_OT_SHAPE_NORMALIZATION_MODE_DEFAULT,
550 NULL, /* decompose */
551 NULL, /* compose */
552 NULL, /* setup_masks */
553 HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE,
554 true, /* fallback_position */
557 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar =
559 "myanmar",
560 collect_features_myanmar,
561 override_features_myanmar,
562 NULL, /* data_create */
563 NULL, /* data_destroy */
564 NULL, /* preprocess_text */
565 HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT,
566 NULL, /* decompose */
567 NULL, /* compose */
568 setup_masks_myanmar,
569 HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY,
570 false, /* fallback_position */