Bug 1862332 [wpt PR 42877] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / gfx / harfbuzz / src / hb-ot-shaper-myanmar.cc
blob1b2a085a8d7c6819e242f155e8825fd8301b58de
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.hh"
29 #ifndef HB_NO_OT_SHAPE
31 #include "hb-ot-shaper-myanmar-machine.hh"
32 #include "hb-ot-shaper-indic.hh"
33 #include "hb-ot-layout.hh"
37 * Myanmar shaper.
41 static const hb_tag_t
42 myanmar_basic_features[] =
45 * Basic features.
46 * These features are applied in order, one at a time, after reordering,
47 * constrained to the syllable.
49 HB_TAG('r','p','h','f'),
50 HB_TAG('p','r','e','f'),
51 HB_TAG('b','l','w','f'),
52 HB_TAG('p','s','t','f'),
54 static const hb_tag_t
55 myanmar_other_features[] =
58 * Other features.
59 * These features are applied all at once, after clearing syllables.
61 HB_TAG('p','r','e','s'),
62 HB_TAG('a','b','v','s'),
63 HB_TAG('b','l','w','s'),
64 HB_TAG('p','s','t','s'),
67 static inline void
68 set_myanmar_properties (hb_glyph_info_t &info)
70 hb_codepoint_t u = info.codepoint;
71 unsigned int type = hb_indic_get_categories (u);
73 info.myanmar_category() = (myanmar_category_t) (type & 0xFFu);
77 static inline bool
78 is_one_of_myanmar (const hb_glyph_info_t &info, unsigned int flags)
80 /* If it ligated, all bets are off. */
81 if (_hb_glyph_info_ligated (&info)) return false;
82 return !!(FLAG_UNSAFE (info.myanmar_category()) & flags);
85 /* Note:
87 * We treat Vowels and placeholders as if they were consonants. This is safe because Vowels
88 * cannot happen in a consonant syllable. The plus side however is, we can call the
89 * consonant syllable logic from the vowel syllable function and get it all right!
91 * Keep in sync with consonant_categories in the generator. */
92 #define CONSONANT_FLAGS_MYANMAR (FLAG (M_Cat(C)) | FLAG (M_Cat(CS)) | FLAG (M_Cat(Ra)) | /* FLAG (M_Cat(CM)) | */ FLAG (M_Cat(IV)) | FLAG (M_Cat(GB)) | FLAG (M_Cat(DOTTEDCIRCLE)))
94 static inline bool
95 is_consonant_myanmar (const hb_glyph_info_t &info)
97 return is_one_of_myanmar (info, CONSONANT_FLAGS_MYANMAR);
101 static bool
102 setup_syllables_myanmar (const hb_ot_shape_plan_t *plan,
103 hb_font_t *font,
104 hb_buffer_t *buffer);
105 static bool
106 reorder_myanmar (const hb_ot_shape_plan_t *plan,
107 hb_font_t *font,
108 hb_buffer_t *buffer);
110 static void
111 collect_features_myanmar (hb_ot_shape_planner_t *plan)
113 hb_ot_map_builder_t *map = &plan->map;
115 /* Do this before any lookups have been applied. */
116 map->add_gsub_pause (setup_syllables_myanmar);
118 map->enable_feature (HB_TAG('l','o','c','l'), F_PER_SYLLABLE);
119 /* The Indic specs do not require ccmp, but we apply it here since if
120 * there is a use of it, it's typically at the beginning. */
121 map->enable_feature (HB_TAG('c','c','m','p'), F_PER_SYLLABLE);
124 map->add_gsub_pause (reorder_myanmar);
126 for (unsigned int i = 0; i < ARRAY_LENGTH (myanmar_basic_features); i++)
128 map->enable_feature (myanmar_basic_features[i], F_MANUAL_ZWJ | F_PER_SYLLABLE);
129 map->add_gsub_pause (nullptr);
131 map->add_gsub_pause (hb_syllabic_clear_var); // Don't need syllables anymore, use stop to free buffer var
133 for (unsigned int i = 0; i < ARRAY_LENGTH (myanmar_other_features); i++)
134 map->enable_feature (myanmar_other_features[i], F_MANUAL_ZWJ);
137 static void
138 setup_masks_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
139 hb_buffer_t *buffer,
140 hb_font_t *font HB_UNUSED)
142 HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_category);
143 HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_position);
145 /* No masks, we just save information about characters. */
147 unsigned int count = buffer->len;
148 hb_glyph_info_t *info = buffer->info;
149 for (unsigned int i = 0; i < count; i++)
150 set_myanmar_properties (info[i]);
153 static bool
154 setup_syllables_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
155 hb_font_t *font HB_UNUSED,
156 hb_buffer_t *buffer)
158 HB_BUFFER_ALLOCATE_VAR (buffer, syllable);
159 find_syllables_myanmar (buffer);
160 foreach_syllable (buffer, start, end)
161 buffer->unsafe_to_break (start, end);
162 return false;
165 static int
166 compare_myanmar_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
168 int a = pa->myanmar_position();
169 int b = pb->myanmar_position();
171 return (int) a - (int) b;
175 /* Rules from:
176 * https://docs.microsoft.com/en-us/typography/script-development/myanmar */
178 static void
179 initial_reordering_consonant_syllable (hb_buffer_t *buffer,
180 unsigned int start, unsigned int end)
182 hb_glyph_info_t *info = buffer->info;
184 unsigned int base = end;
185 bool has_reph = false;
188 unsigned int limit = start;
189 if (start + 3 <= end &&
190 info[start ].myanmar_category() == M_Cat(Ra) &&
191 info[start+1].myanmar_category() == M_Cat(As) &&
192 info[start+2].myanmar_category() == M_Cat(H))
194 limit += 3;
195 base = start;
196 has_reph = true;
200 if (!has_reph)
201 base = limit;
203 for (unsigned int i = limit; i < end; i++)
204 if (is_consonant_myanmar (info[i]))
206 base = i;
207 break;
212 /* Reorder! */
214 unsigned int i = start;
215 for (; i < start + (has_reph ? 3 : 0); i++)
216 info[i].myanmar_position() = POS_AFTER_MAIN;
217 for (; i < base; i++)
218 info[i].myanmar_position() = POS_PRE_C;
219 if (i < end)
221 info[i].myanmar_position() = POS_BASE_C;
222 i++;
224 myanmar_position_t pos = POS_AFTER_MAIN;
225 /* The following loop may be ugly, but it implements all of
226 * Myanmar reordering! */
227 for (; i < end; i++)
229 if (info[i].myanmar_category() == M_Cat(MR)) /* Pre-base reordering */
231 info[i].myanmar_position() = POS_PRE_C;
232 continue;
234 if (info[i].myanmar_category() == M_Cat(VPre)) /* Left matra */
236 info[i].myanmar_position() = POS_PRE_M;
237 continue;
239 if (info[i].myanmar_category() == M_Cat(VS))
241 info[i].myanmar_position() = info[i - 1].myanmar_position();
242 continue;
245 if (pos == POS_AFTER_MAIN && info[i].myanmar_category() == M_Cat(VBlw))
247 pos = POS_BELOW_C;
248 info[i].myanmar_position() = pos;
249 continue;
252 if (pos == POS_BELOW_C && info[i].myanmar_category() == M_Cat(A))
254 info[i].myanmar_position() = POS_BEFORE_SUB;
255 continue;
257 if (pos == POS_BELOW_C && info[i].myanmar_category() == M_Cat(VBlw))
259 info[i].myanmar_position() = pos;
260 continue;
262 if (pos == POS_BELOW_C && info[i].myanmar_category() != M_Cat(A))
264 pos = POS_AFTER_SUB;
265 info[i].myanmar_position() = pos;
266 continue;
268 info[i].myanmar_position() = pos;
272 /* Sit tight, rock 'n roll! */
273 buffer->sort (start, end, compare_myanmar_order);
275 /* Flip left-matra sequence. */
276 unsigned first_left_matra = end;
277 unsigned last_left_matra = end;
278 for (unsigned int i = start; i < end; i++)
280 if (info[i].myanmar_position() == POS_PRE_M)
282 if (first_left_matra == end)
283 first_left_matra = i;
284 last_left_matra = i;
287 /* https://github.com/harfbuzz/harfbuzz/issues/3863 */
288 if (first_left_matra < last_left_matra)
290 /* No need to merge clusters, done already? */
291 buffer->reverse_range (first_left_matra, last_left_matra + 1);
292 /* Reverse back VS, etc. */
293 unsigned i = first_left_matra;
294 for (unsigned j = i; j <= last_left_matra; j++)
295 if (info[j].myanmar_category() == M_Cat(VPre))
297 buffer->reverse_range (i, j + 1);
298 i = j + 1;
303 static void
304 reorder_syllable_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
305 hb_face_t *face HB_UNUSED,
306 hb_buffer_t *buffer,
307 unsigned int start, unsigned int end)
309 myanmar_syllable_type_t syllable_type = (myanmar_syllable_type_t) (buffer->info[start].syllable() & 0x0F);
310 switch (syllable_type) {
312 case myanmar_broken_cluster: /* We already inserted dotted-circles, so just call the consonant_syllable. */
313 case myanmar_consonant_syllable:
314 initial_reordering_consonant_syllable (buffer, start, end);
315 break;
317 case myanmar_non_myanmar_cluster:
318 break;
322 static bool
323 reorder_myanmar (const hb_ot_shape_plan_t *plan,
324 hb_font_t *font,
325 hb_buffer_t *buffer)
327 bool ret = false;
328 if (buffer->message (font, "start reordering myanmar"))
330 if (hb_syllabic_insert_dotted_circles (font, buffer,
331 myanmar_broken_cluster,
332 M_Cat(DOTTEDCIRCLE)))
333 ret = true;
335 foreach_syllable (buffer, start, end)
336 reorder_syllable_myanmar (plan, font->face, buffer, start, end);
337 (void) buffer->message (font, "end reordering myanmar");
340 HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_category);
341 HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_position);
343 return ret;
347 const hb_ot_shaper_t _hb_ot_shaper_myanmar =
349 collect_features_myanmar,
350 nullptr, /* override_features */
351 nullptr, /* data_create */
352 nullptr, /* data_destroy */
353 nullptr, /* preprocess_text */
354 nullptr, /* postprocess_glyphs */
355 nullptr, /* decompose */
356 nullptr, /* compose */
357 setup_masks_myanmar,
358 nullptr, /* reorder_marks */
359 HB_TAG_NONE, /* gpos_tag */
360 HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT,
361 HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY,
362 false, /* fallback_position */
366 #ifndef HB_NO_OT_SHAPER_MYANMAR_ZAWGYI
367 /* Ugly Zawgyi encoding.
368 * Disable all auto processing.
369 * https://github.com/harfbuzz/harfbuzz/issues/1162 */
370 const hb_ot_shaper_t _hb_ot_shaper_myanmar_zawgyi =
372 nullptr, /* collect_features */
373 nullptr, /* override_features */
374 nullptr, /* data_create */
375 nullptr, /* data_destroy */
376 nullptr, /* preprocess_text */
377 nullptr, /* postprocess_glyphs */
378 nullptr, /* decompose */
379 nullptr, /* compose */
380 nullptr, /* setup_masks */
381 nullptr, /* reorder_marks */
382 HB_TAG_NONE, /* gpos_tag */
383 HB_OT_SHAPE_NORMALIZATION_MODE_NONE,
384 HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
385 false, /* fallback_position */
387 #endif
390 #endif