blob: e201a230938ab67bc5ab82efc45bf0f2105d75cb [file] [log] [blame]
Behdad Esfahbod98628ca2013-02-11 13:36:23 -05001/*
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
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070027#include "hb-ot-shape-complex-myanmar.hh"
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050028
29
30/*
31 * Myanmar shaper.
32 */
33
34static const hb_tag_t
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -050035basic_features[] =
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050036{
37 /*
38 * Basic features.
39 * These features are applied in order, one at a time, after initial_reordering.
40 */
41 HB_TAG('r','p','h','f'),
42 HB_TAG('p','r','e','f'),
43 HB_TAG('b','l','w','f'),
44 HB_TAG('p','s','t','f'),
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -050045};
46static const hb_tag_t
47other_features[] =
48{
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050049 /*
50 * Other features.
51 * These features are applied all at once, after final_reordering.
52 */
53 HB_TAG('p','r','e','s'),
54 HB_TAG('a','b','v','s'),
55 HB_TAG('b','l','w','s'),
56 HB_TAG('p','s','t','s'),
Behdad Esfahbod3583fb02018-09-23 22:33:38 -040057};
58static const hb_tag_t
59positioning_features[] =
60{
61 /*
62 * Positioning features.
63 * We don't care about the types.
64 */
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050065 HB_TAG('d','i','s','t'),
Behdad Esfahbod5e7432b2013-10-15 12:28:23 +020066 /* Pre-release version of Windows 8 Myanmar font had abvm,blwm
67 * features. The released Windows 8 version of the font (as well
68 * as the released spec) used 'mark' instead. The Windows 8
69 * shaper however didn't apply 'mark' but did apply 'mkmk'.
70 * Perhaps it applied abvm/blwm. This was fixed in a Windows 8
71 * update, so now it applies mark/mkmk. We are guessing that
72 * it still applies abvm/blwm too.
73 */
74 HB_TAG('a','b','v','m'),
75 HB_TAG('b','l','w','m'),
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050076};
77
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050078static void
79setup_syllables (const hb_ot_shape_plan_t *plan,
80 hb_font_t *font,
81 hb_buffer_t *buffer);
82static void
83initial_reordering (const hb_ot_shape_plan_t *plan,
84 hb_font_t *font,
85 hb_buffer_t *buffer);
86static void
87final_reordering (const hb_ot_shape_plan_t *plan,
88 hb_font_t *font,
89 hb_buffer_t *buffer);
90
91static void
92collect_features_myanmar (hb_ot_shape_planner_t *plan)
93{
94 hb_ot_map_builder_t *map = &plan->map;
95
96 /* Do this before any lookups have been applied. */
97 map->add_gsub_pause (setup_syllables);
98
Behdad Esfahbodf048ead2018-09-24 18:01:53 -040099 map->enable_feature (HB_TAG('l','o','c','l'));
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500100 /* The Indic specs do not require ccmp, but we apply it here since if
101 * there is a use of it, it's typically at the beginning. */
Behdad Esfahbodf048ead2018-09-24 18:01:53 -0400102 map->enable_feature (HB_TAG('c','c','m','p'));
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500103
104
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500105 map->add_gsub_pause (initial_reordering);
Behdad Esfahbod3583fb02018-09-23 22:33:38 -0400106
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -0500107 for (unsigned int i = 0; i < ARRAY_LENGTH (basic_features); i++)
108 {
Behdad Esfahbod0a371fe2018-10-02 14:48:39 +0200109 map->enable_feature (basic_features[i], F_MANUAL_ZWJ);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200110 map->add_gsub_pause (nullptr);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500111 }
Behdad Esfahbod3583fb02018-09-23 22:33:38 -0400112
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500113 map->add_gsub_pause (final_reordering);
Behdad Esfahbod3583fb02018-09-23 22:33:38 -0400114
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -0500115 for (unsigned int i = 0; i < ARRAY_LENGTH (other_features); i++)
Behdad Esfahbod0a371fe2018-10-02 14:48:39 +0200116 map->enable_feature (other_features[i], F_MANUAL_ZWJ);
Behdad Esfahbod3583fb02018-09-23 22:33:38 -0400117
118 for (unsigned int i = 0; i < ARRAY_LENGTH (positioning_features); i++)
Behdad Esfahbodf048ead2018-09-24 18:01:53 -0400119 map->enable_feature (positioning_features[i]);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500120}
121
122static void
123override_features_myanmar (hb_ot_shape_planner_t *plan)
124{
Behdad Esfahbod1676f602018-09-24 17:55:03 -0400125 plan->map.disable_feature (HB_TAG('l','i','g','a'));
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500126}
127
128
129enum syllable_type_t {
130 consonant_syllable,
Behdad Esfahbod9174a9d2013-11-25 18:10:38 -0500131 punctuation_cluster,
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500132 broken_cluster,
133 non_myanmar_cluster,
134};
135
136#include "hb-ot-shape-complex-myanmar-machine.hh"
137
138
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500139static void
140setup_masks_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
141 hb_buffer_t *buffer,
142 hb_font_t *font HB_UNUSED)
143{
144 HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_category);
145 HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_position);
146
147 /* We cannot setup masks here. We save information about characters
148 * and setup masks later on in a pause-callback. */
149
150 unsigned int count = buffer->len;
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400151 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500152 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400153 set_myanmar_properties (info[i]);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500154}
155
156static void
157setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED,
158 hb_font_t *font HB_UNUSED,
159 hb_buffer_t *buffer)
160{
161 find_syllables (buffer);
Behdad Esfahbod9e005c52017-08-10 18:45:33 -0700162 foreach_syllable (buffer, start, end)
163 buffer->unsafe_to_break (start, end);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500164}
165
166static int
167compare_myanmar_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
168{
169 int a = pa->myanmar_position();
170 int b = pb->myanmar_position();
171
172 return a < b ? -1 : a == b ? 0 : +1;
173}
174
175
176/* Rules from:
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +0430177 * https://docs.microsoft.com/en-us/typography/script-development/myanmar */
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500178
179static void
Behdad Esfahbod56f71ff2015-07-22 11:58:11 +0100180initial_reordering_consonant_syllable (hb_buffer_t *buffer,
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500181 unsigned int start, unsigned int end)
182{
183 hb_glyph_info_t *info = buffer->info;
184
185 unsigned int base = end;
186 bool has_reph = false;
187
188 {
189 unsigned int limit = start;
190 if (start + 3 <= end &&
191 info[start ].myanmar_category() == OT_Ra &&
192 info[start+1].myanmar_category() == OT_As &&
193 info[start+2].myanmar_category() == OT_H)
194 {
195 limit += 3;
196 base = start;
197 has_reph = true;
198 }
199
200 {
201 if (!has_reph)
202 base = limit;
203
204 for (unsigned int i = limit; i < end; i++)
205 if (is_consonant (info[i]))
206 {
207 base = i;
208 break;
209 }
210 }
211 }
212
213 /* Reorder! */
214 {
215 unsigned int i = start;
216 for (; i < start + (has_reph ? 3 : 0); i++)
217 info[i].myanmar_position() = POS_AFTER_MAIN;
218 for (; i < base; i++)
219 info[i].myanmar_position() = POS_PRE_C;
220 if (i < end)
221 {
222 info[i].myanmar_position() = POS_BASE_C;
223 i++;
224 }
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500225 indic_position_t pos = POS_AFTER_MAIN;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500226 /* The following loop may be ugly, but it implements all of
227 * Myanmar reordering! */
228 for (; i < end; i++)
229 {
230 if (info[i].myanmar_category() == OT_MR) /* Pre-base reordering */
231 {
232 info[i].myanmar_position() = POS_PRE_C;
233 continue;
234 }
235 if (info[i].myanmar_position() < POS_BASE_C) /* Left matra */
236 {
237 continue;
238 }
David Corbettccb03672018-02-02 12:04:04 -0500239 if (info[i].myanmar_category() == OT_VS)
240 {
241 info[i].myanmar_position() = info[i - 1].myanmar_position();
242 continue;
243 }
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500244
245 if (pos == POS_AFTER_MAIN && info[i].myanmar_category() == OT_VBlw)
246 {
247 pos = POS_BELOW_C;
248 info[i].myanmar_position() = pos;
249 continue;
250 }
251
252 if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_A)
253 {
254 info[i].myanmar_position() = POS_BEFORE_SUB;
255 continue;
256 }
257 if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_VBlw)
258 {
259 info[i].myanmar_position() = pos;
260 continue;
261 }
262 if (pos == POS_BELOW_C && info[i].myanmar_category() != OT_A)
263 {
264 pos = POS_AFTER_SUB;
265 info[i].myanmar_position() = pos;
266 continue;
267 }
268 info[i].myanmar_position() = pos;
269 }
270 }
271
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500272 /* Sit tight, rock 'n roll! */
Behdad Esfahbodc403d632015-09-01 16:15:25 +0100273 buffer->sort (start, end, compare_myanmar_order);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500274}
275
276static void
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500277initial_reordering_syllable (const hb_ot_shape_plan_t *plan,
278 hb_face_t *face,
279 hb_buffer_t *buffer,
280 unsigned int start, unsigned int end)
281{
282 syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F);
283 switch (syllable_type) {
Behdad Esfahbod56f71ff2015-07-22 11:58:11 +0100284
285 case broken_cluster: /* We already inserted dotted-circles, so just call the consonant_syllable. */
286 case consonant_syllable:
287 initial_reordering_consonant_syllable (buffer, start, end);
288 break;
289
290 case punctuation_cluster:
291 case non_myanmar_cluster:
292 break;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500293 }
294}
295
296static inline void
297insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED,
298 hb_font_t *font,
299 hb_buffer_t *buffer)
300{
301 /* Note: This loop is extra overhead, but should not be measurable. */
302 bool has_broken_syllables = false;
303 unsigned int count = buffer->len;
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400304 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500305 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400306 if ((info[i].syllable() & 0x0F) == broken_cluster)
307 {
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500308 has_broken_syllables = true;
309 break;
310 }
311 if (likely (!has_broken_syllables))
312 return;
313
314
315 hb_codepoint_t dottedcircle_glyph;
Behdad Esfahbod8b5bc142016-02-24 19:05:23 +0900316 if (!font->get_nominal_glyph (0x25CCu, &dottedcircle_glyph))
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500317 return;
318
319 hb_glyph_info_t dottedcircle = {0};
Behdad Esfahbod76271002014-07-11 14:54:42 -0400320 dottedcircle.codepoint = 0x25CCu;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500321 set_myanmar_properties (dottedcircle);
322 dottedcircle.codepoint = dottedcircle_glyph;
323
324 buffer->clear_output ();
325
326 buffer->idx = 0;
327 unsigned int last_syllable = 0;
Behdad Esfahbod7185b272018-05-31 20:03:00 -0700328 while (buffer->idx < buffer->len && buffer->successful)
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500329 {
330 unsigned int syllable = buffer->cur().syllable();
331 syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F);
332 if (unlikely (last_syllable != syllable && syllable_type == broken_cluster))
333 {
334 last_syllable = syllable;
335
Behdad Esfahbod6f932bc2015-10-21 11:16:49 -0200336 hb_glyph_info_t ginfo = dottedcircle;
337 ginfo.cluster = buffer->cur().cluster;
338 ginfo.mask = buffer->cur().mask;
339 ginfo.syllable() = buffer->cur().syllable();
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500340
Behdad Esfahbod6f932bc2015-10-21 11:16:49 -0200341 buffer->output_info (ginfo);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500342 }
343 else
344 buffer->next_glyph ();
345 }
346
347 buffer->swap_buffers ();
348}
349
350static void
351initial_reordering (const hb_ot_shape_plan_t *plan,
352 hb_font_t *font,
353 hb_buffer_t *buffer)
354{
355 insert_dotted_circles (plan, font, buffer);
356
Behdad Esfahbod56f71ff2015-07-22 11:58:11 +0100357 foreach_syllable (buffer, start, end)
358 initial_reordering_syllable (plan, font->face, buffer, start, end);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500359}
360
361static void
362final_reordering (const hb_ot_shape_plan_t *plan,
363 hb_font_t *font HB_UNUSED,
364 hb_buffer_t *buffer)
365{
366 hb_glyph_info_t *info = buffer->info;
367 unsigned int count = buffer->len;
368
369 /* Zero syllables now... */
370 for (unsigned int i = 0; i < count; i++)
371 info[i].syllable() = 0;
372
373 HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_category);
374 HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_position);
375}
376
Behdad Esfahbod71b4c992013-10-28 00:20:59 +0100377
Behdad Esfahbod00c5c4a2018-10-11 20:15:31 -0400378const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar =
379{
380 collect_features_myanmar,
381 override_features_myanmar,
382 nullptr, /* data_create */
383 nullptr, /* data_destroy */
384 nullptr, /* preprocess_text */
385 nullptr, /* postprocess_glyphs */
386 HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT,
387 nullptr, /* decompose */
388 nullptr, /* compose */
389 setup_masks_myanmar,
390 HB_TAG_NONE, /* gpos_tag */
391 nullptr, /* reorder_marks */
392 HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY,
393 false, /* fallback_position */
394};
395
396
Behdad Esfahbod6f2d9ba2014-07-26 19:17:44 -0400397/* Uniscribe seems to have a shaper for 'mymr' that is like the
398 * generic shaper, except that it zeros mark advances GDEF_LATE. */
399const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar_old =
400{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200401 nullptr, /* collect_features */
402 nullptr, /* override_features */
403 nullptr, /* data_create */
404 nullptr, /* data_destroy */
405 nullptr, /* preprocess_text */
406 nullptr, /* postprocess_glyphs */
Behdad Esfahbod6f2d9ba2014-07-26 19:17:44 -0400407 HB_OT_SHAPE_NORMALIZATION_MODE_DEFAULT,
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200408 nullptr, /* decompose */
409 nullptr, /* compose */
410 nullptr, /* setup_masks */
Behdad Esfahbod48c513f2018-10-02 14:17:42 +0200411 HB_TAG_NONE, /* gpos_tag */
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200412 nullptr, /* reorder_marks */
Behdad Esfahbod6f2d9ba2014-07-26 19:17:44 -0400413 HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE,
414 true, /* fallback_position */
415};
Behdad Esfahbodbdb53ca2018-10-11 20:20:00 -0400416
417
418/* Ugly Zawgyi encoding.
419 * Disable all auto processing.
420 * https://github.com/harfbuzz/harfbuzz/issues/1162 */
421const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar_zawgyi =
422{
423 nullptr, /* collect_features */
424 nullptr, /* override_features */
425 nullptr, /* data_create */
426 nullptr, /* data_destroy */
427 nullptr, /* preprocess_text */
428 nullptr, /* postprocess_glyphs */
429 HB_OT_SHAPE_NORMALIZATION_MODE_NONE,
430 nullptr, /* decompose */
431 nullptr, /* compose */
432 nullptr, /* setup_masks */
433 HB_TAG_NONE, /* gpos_tag */
434 nullptr, /* reorder_marks */
435 HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
436 false, /* fallback_position */
437};