blob: 3c57bc1ff66cf8a502aa38926d5e5d324161668e [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 Esfahbodeffddd02018-02-13 21:38:15 -080027#include "hb-ot-shape-complex-myanmar-private.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'),
57 /* Positioning features, though we don't care about the types. */
58 HB_TAG('d','i','s','t'),
Behdad Esfahbod5e7432b2013-10-15 12:28:23 +020059 /* Pre-release version of Windows 8 Myanmar font had abvm,blwm
60 * features. The released Windows 8 version of the font (as well
61 * as the released spec) used 'mark' instead. The Windows 8
62 * shaper however didn't apply 'mark' but did apply 'mkmk'.
63 * Perhaps it applied abvm/blwm. This was fixed in a Windows 8
64 * update, so now it applies mark/mkmk. We are guessing that
65 * it still applies abvm/blwm too.
66 */
67 HB_TAG('a','b','v','m'),
68 HB_TAG('b','l','w','m'),
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050069};
70
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050071static void
72setup_syllables (const hb_ot_shape_plan_t *plan,
73 hb_font_t *font,
74 hb_buffer_t *buffer);
75static void
76initial_reordering (const hb_ot_shape_plan_t *plan,
77 hb_font_t *font,
78 hb_buffer_t *buffer);
79static void
80final_reordering (const hb_ot_shape_plan_t *plan,
81 hb_font_t *font,
82 hb_buffer_t *buffer);
83
84static void
85collect_features_myanmar (hb_ot_shape_planner_t *plan)
86{
87 hb_ot_map_builder_t *map = &plan->map;
88
89 /* Do this before any lookups have been applied. */
90 map->add_gsub_pause (setup_syllables);
91
Behdad Esfahbode7ffcfa2013-02-14 11:05:56 -050092 map->add_global_bool_feature (HB_TAG('l','o','c','l'));
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050093 /* The Indic specs do not require ccmp, but we apply it here since if
94 * there is a use of it, it's typically at the beginning. */
Behdad Esfahbode7ffcfa2013-02-14 11:05:56 -050095 map->add_global_bool_feature (HB_TAG('c','c','m','p'));
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050096
97
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050098 map->add_gsub_pause (initial_reordering);
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -050099 for (unsigned int i = 0; i < ARRAY_LENGTH (basic_features); i++)
100 {
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400101 map->add_feature (basic_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200102 map->add_gsub_pause (nullptr);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500103 }
104 map->add_gsub_pause (final_reordering);
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -0500105 for (unsigned int i = 0; i < ARRAY_LENGTH (other_features); i++)
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400106 map->add_feature (other_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500107}
108
109static void
110override_features_myanmar (hb_ot_shape_planner_t *plan)
111{
Behdad Esfahbodec544862013-02-14 11:25:10 -0500112 plan->map.add_feature (HB_TAG('l','i','g','a'), 0, F_GLOBAL);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500113}
114
115
116enum syllable_type_t {
117 consonant_syllable,
Behdad Esfahbod9174a9d2013-11-25 18:10:38 -0500118 punctuation_cluster,
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500119 broken_cluster,
120 non_myanmar_cluster,
121};
122
123#include "hb-ot-shape-complex-myanmar-machine.hh"
124
125
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500126static void
127setup_masks_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
128 hb_buffer_t *buffer,
129 hb_font_t *font HB_UNUSED)
130{
131 HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_category);
132 HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_position);
133
134 /* We cannot setup masks here. We save information about characters
135 * and setup masks later on in a pause-callback. */
136
137 unsigned int count = buffer->len;
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400138 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500139 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400140 set_myanmar_properties (info[i]);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500141}
142
143static void
144setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED,
145 hb_font_t *font HB_UNUSED,
146 hb_buffer_t *buffer)
147{
148 find_syllables (buffer);
Behdad Esfahbod9e005c52017-08-10 18:45:33 -0700149 foreach_syllable (buffer, start, end)
150 buffer->unsafe_to_break (start, end);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500151}
152
153static int
154compare_myanmar_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
155{
156 int a = pa->myanmar_position();
157 int b = pb->myanmar_position();
158
159 return a < b ? -1 : a == b ? 0 : +1;
160}
161
162
163/* Rules from:
164 * http://www.microsoft.com/typography/OpenTypeDev/myanmar/intro.htm */
165
166static void
Behdad Esfahbod56f71ff2015-07-22 11:58:11 +0100167initial_reordering_consonant_syllable (hb_buffer_t *buffer,
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500168 unsigned int start, unsigned int end)
169{
170 hb_glyph_info_t *info = buffer->info;
171
172 unsigned int base = end;
173 bool has_reph = false;
174
175 {
176 unsigned int limit = start;
177 if (start + 3 <= end &&
178 info[start ].myanmar_category() == OT_Ra &&
179 info[start+1].myanmar_category() == OT_As &&
180 info[start+2].myanmar_category() == OT_H)
181 {
182 limit += 3;
183 base = start;
184 has_reph = true;
185 }
186
187 {
188 if (!has_reph)
189 base = limit;
190
191 for (unsigned int i = limit; i < end; i++)
192 if (is_consonant (info[i]))
193 {
194 base = i;
195 break;
196 }
197 }
198 }
199
200 /* Reorder! */
201 {
202 unsigned int i = start;
203 for (; i < start + (has_reph ? 3 : 0); i++)
204 info[i].myanmar_position() = POS_AFTER_MAIN;
205 for (; i < base; i++)
206 info[i].myanmar_position() = POS_PRE_C;
207 if (i < end)
208 {
209 info[i].myanmar_position() = POS_BASE_C;
210 i++;
211 }
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500212 indic_position_t pos = POS_AFTER_MAIN;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500213 /* The following loop may be ugly, but it implements all of
214 * Myanmar reordering! */
215 for (; i < end; i++)
216 {
217 if (info[i].myanmar_category() == OT_MR) /* Pre-base reordering */
218 {
219 info[i].myanmar_position() = POS_PRE_C;
220 continue;
221 }
222 if (info[i].myanmar_position() < POS_BASE_C) /* Left matra */
223 {
224 continue;
225 }
David Corbettccb03672018-02-02 12:04:04 -0500226 if (info[i].myanmar_category() == OT_VS)
227 {
228 info[i].myanmar_position() = info[i - 1].myanmar_position();
229 continue;
230 }
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500231
232 if (pos == POS_AFTER_MAIN && info[i].myanmar_category() == OT_VBlw)
233 {
234 pos = POS_BELOW_C;
235 info[i].myanmar_position() = pos;
236 continue;
237 }
238
239 if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_A)
240 {
241 info[i].myanmar_position() = POS_BEFORE_SUB;
242 continue;
243 }
244 if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_VBlw)
245 {
246 info[i].myanmar_position() = pos;
247 continue;
248 }
249 if (pos == POS_BELOW_C && info[i].myanmar_category() != OT_A)
250 {
251 pos = POS_AFTER_SUB;
252 info[i].myanmar_position() = pos;
253 continue;
254 }
255 info[i].myanmar_position() = pos;
256 }
257 }
258
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500259 /* Sit tight, rock 'n roll! */
Behdad Esfahbodc403d632015-09-01 16:15:25 +0100260 buffer->sort (start, end, compare_myanmar_order);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500261}
262
263static void
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500264initial_reordering_syllable (const hb_ot_shape_plan_t *plan,
265 hb_face_t *face,
266 hb_buffer_t *buffer,
267 unsigned int start, unsigned int end)
268{
269 syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F);
270 switch (syllable_type) {
Behdad Esfahbod56f71ff2015-07-22 11:58:11 +0100271
272 case broken_cluster: /* We already inserted dotted-circles, so just call the consonant_syllable. */
273 case consonant_syllable:
274 initial_reordering_consonant_syllable (buffer, start, end);
275 break;
276
277 case punctuation_cluster:
278 case non_myanmar_cluster:
279 break;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500280 }
281}
282
283static inline void
284insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED,
285 hb_font_t *font,
286 hb_buffer_t *buffer)
287{
288 /* Note: This loop is extra overhead, but should not be measurable. */
289 bool has_broken_syllables = false;
290 unsigned int count = buffer->len;
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400291 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500292 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400293 if ((info[i].syllable() & 0x0F) == broken_cluster)
294 {
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500295 has_broken_syllables = true;
296 break;
297 }
298 if (likely (!has_broken_syllables))
299 return;
300
301
302 hb_codepoint_t dottedcircle_glyph;
Behdad Esfahbod8b5bc142016-02-24 19:05:23 +0900303 if (!font->get_nominal_glyph (0x25CCu, &dottedcircle_glyph))
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500304 return;
305
306 hb_glyph_info_t dottedcircle = {0};
Behdad Esfahbod76271002014-07-11 14:54:42 -0400307 dottedcircle.codepoint = 0x25CCu;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500308 set_myanmar_properties (dottedcircle);
309 dottedcircle.codepoint = dottedcircle_glyph;
310
311 buffer->clear_output ();
312
313 buffer->idx = 0;
314 unsigned int last_syllable = 0;
Behdad Esfahbodb894a852016-02-02 16:39:19 +0800315 while (buffer->idx < buffer->len && !buffer->in_error)
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500316 {
317 unsigned int syllable = buffer->cur().syllable();
318 syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F);
319 if (unlikely (last_syllable != syllable && syllable_type == broken_cluster))
320 {
321 last_syllable = syllable;
322
Behdad Esfahbod6f932bc2015-10-21 11:16:49 -0200323 hb_glyph_info_t ginfo = dottedcircle;
324 ginfo.cluster = buffer->cur().cluster;
325 ginfo.mask = buffer->cur().mask;
326 ginfo.syllable() = buffer->cur().syllable();
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500327
Behdad Esfahbod6f932bc2015-10-21 11:16:49 -0200328 buffer->output_info (ginfo);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500329 }
330 else
331 buffer->next_glyph ();
332 }
333
334 buffer->swap_buffers ();
335}
336
337static void
338initial_reordering (const hb_ot_shape_plan_t *plan,
339 hb_font_t *font,
340 hb_buffer_t *buffer)
341{
342 insert_dotted_circles (plan, font, buffer);
343
Behdad Esfahbod56f71ff2015-07-22 11:58:11 +0100344 foreach_syllable (buffer, start, end)
345 initial_reordering_syllable (plan, font->face, buffer, start, end);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500346}
347
348static void
349final_reordering (const hb_ot_shape_plan_t *plan,
350 hb_font_t *font HB_UNUSED,
351 hb_buffer_t *buffer)
352{
353 hb_glyph_info_t *info = buffer->info;
354 unsigned int count = buffer->len;
355
356 /* Zero syllables now... */
357 for (unsigned int i = 0; i < count; i++)
358 info[i].syllable() = 0;
359
360 HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_category);
361 HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_position);
362}
363
Behdad Esfahbod71b4c992013-10-28 00:20:59 +0100364
Behdad Esfahbod6f2d9ba2014-07-26 19:17:44 -0400365/* Uniscribe seems to have a shaper for 'mymr' that is like the
366 * generic shaper, except that it zeros mark advances GDEF_LATE. */
367const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar_old =
368{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200369 nullptr, /* collect_features */
370 nullptr, /* override_features */
371 nullptr, /* data_create */
372 nullptr, /* data_destroy */
373 nullptr, /* preprocess_text */
374 nullptr, /* postprocess_glyphs */
Behdad Esfahbod6f2d9ba2014-07-26 19:17:44 -0400375 HB_OT_SHAPE_NORMALIZATION_MODE_DEFAULT,
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200376 nullptr, /* decompose */
377 nullptr, /* compose */
378 nullptr, /* setup_masks */
379 nullptr, /* disable_otl */
380 nullptr, /* reorder_marks */
Behdad Esfahbod6f2d9ba2014-07-26 19:17:44 -0400381 HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE,
382 true, /* fallback_position */
383};
384
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500385const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar =
386{
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500387 collect_features_myanmar,
388 override_features_myanmar,
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200389 nullptr, /* data_create */
390 nullptr, /* data_destroy */
391 nullptr, /* preprocess_text */
392 nullptr, /* postprocess_glyphs */
Behdad Esfahbod3d6ca0d2013-12-31 16:04:35 +0800393 HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT,
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200394 nullptr, /* decompose */
395 nullptr, /* compose */
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500396 setup_masks_myanmar,
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200397 nullptr, /* disable_otl */
398 nullptr, /* reorder_marks */
Behdad Esfahbod71b4c992013-10-28 00:20:59 +0100399 HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY,
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500400 false, /* fallback_position */
401};