blob: dad1dba4adf714a90222a07f628feb92687fb2f5 [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 Esfahbod7aad5362019-06-26 13:21:03 -070027#include "hb.hh"
28
29#ifndef HB_NO_OT_SHAPE
30
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070031#include "hb-ot-shape-complex-myanmar.hh"
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050032
33
34/*
35 * Myanmar shaper.
36 */
37
38static const hb_tag_t
Behdad Esfahbodd8b53532019-07-02 15:09:26 -070039myanmar_basic_features[] =
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050040{
41 /*
42 * Basic features.
Behdad Esfahbod982c2f42018-10-26 15:40:12 -070043 * These features are applied in order, one at a time, after reordering.
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050044 */
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'),
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -050049};
50static const hb_tag_t
Behdad Esfahbodd8b53532019-07-02 15:09:26 -070051myanmar_other_features[] =
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -050052{
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050053 /*
54 * Other features.
Behdad Esfahbod982c2f42018-10-26 15:40:12 -070055 * These features are applied all at once, after clearing syllables.
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050056 */
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'),
Behdad Esfahbod3583fb02018-09-23 22:33:38 -040061};
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050062
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050063static void
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -070064setup_syllables_myanmar (const hb_ot_shape_plan_t *plan,
65 hb_font_t *font,
66 hb_buffer_t *buffer);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050067static void
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -070068reorder_myanmar (const hb_ot_shape_plan_t *plan,
Behdad Esfahbod982c2f42018-10-26 15:40:12 -070069 hb_font_t *font,
70 hb_buffer_t *buffer);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050071
72static void
73collect_features_myanmar (hb_ot_shape_planner_t *plan)
74{
75 hb_ot_map_builder_t *map = &plan->map;
76
77 /* Do this before any lookups have been applied. */
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -070078 map->add_gsub_pause (setup_syllables_myanmar);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050079
Behdad Esfahbodf048ead2018-09-24 18:01:53 -040080 map->enable_feature (HB_TAG('l','o','c','l'));
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050081 /* The Indic specs do not require ccmp, but we apply it here since if
82 * there is a use of it, it's typically at the beginning. */
Behdad Esfahbodf048ead2018-09-24 18:01:53 -040083 map->enable_feature (HB_TAG('c','c','m','p'));
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050084
85
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -070086 map->add_gsub_pause (reorder_myanmar);
Behdad Esfahbod3583fb02018-09-23 22:33:38 -040087
Behdad Esfahbodd8b53532019-07-02 15:09:26 -070088 for (unsigned int i = 0; i < ARRAY_LENGTH (myanmar_basic_features); i++)
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -050089 {
Behdad Esfahbodd8b53532019-07-02 15:09:26 -070090 map->enable_feature (myanmar_basic_features[i], F_MANUAL_ZWJ);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +020091 map->add_gsub_pause (nullptr);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050092 }
Behdad Esfahbod3583fb02018-09-23 22:33:38 -040093
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -070094 map->add_gsub_pause (_hb_clear_syllables);
Behdad Esfahbod3583fb02018-09-23 22:33:38 -040095
Behdad Esfahbodd8b53532019-07-02 15:09:26 -070096 for (unsigned int i = 0; i < ARRAY_LENGTH (myanmar_other_features); i++)
97 map->enable_feature (myanmar_other_features[i], F_MANUAL_ZWJ);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050098}
99
100static void
101override_features_myanmar (hb_ot_shape_planner_t *plan)
102{
Behdad Esfahbod1676f602018-09-24 17:55:03 -0400103 plan->map.disable_feature (HB_TAG('l','i','g','a'));
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500104}
105
106
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -0700107enum myanmar_syllable_type_t {
108 myanmar_consonant_syllable,
109 myanmar_punctuation_cluster,
110 myanmar_broken_cluster,
111 myanmar_non_myanmar_cluster,
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500112};
113
114#include "hb-ot-shape-complex-myanmar-machine.hh"
115
116
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500117static void
118setup_masks_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
119 hb_buffer_t *buffer,
120 hb_font_t *font HB_UNUSED)
121{
122 HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_category);
123 HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_position);
124
125 /* We cannot setup masks here. We save information about characters
126 * and setup masks later on in a pause-callback. */
127
128 unsigned int count = buffer->len;
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400129 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500130 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400131 set_myanmar_properties (info[i]);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500132}
133
134static void
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -0700135setup_syllables_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
136 hb_font_t *font HB_UNUSED,
137 hb_buffer_t *buffer)
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500138{
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -0700139 find_syllables_myanmar (buffer);
Behdad Esfahbod9e005c52017-08-10 18:45:33 -0700140 foreach_syllable (buffer, start, end)
141 buffer->unsafe_to_break (start, end);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500142}
143
144static int
145compare_myanmar_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
146{
147 int a = pa->myanmar_position();
148 int b = pb->myanmar_position();
149
150 return a < b ? -1 : a == b ? 0 : +1;
151}
152
153
154/* Rules from:
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +0430155 * https://docs.microsoft.com/en-us/typography/script-development/myanmar */
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500156
157static void
Behdad Esfahbod56f71ff2015-07-22 11:58:11 +0100158initial_reordering_consonant_syllable (hb_buffer_t *buffer,
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500159 unsigned int start, unsigned int end)
160{
161 hb_glyph_info_t *info = buffer->info;
162
163 unsigned int base = end;
164 bool has_reph = false;
165
166 {
167 unsigned int limit = start;
168 if (start + 3 <= end &&
169 info[start ].myanmar_category() == OT_Ra &&
170 info[start+1].myanmar_category() == OT_As &&
171 info[start+2].myanmar_category() == OT_H)
172 {
173 limit += 3;
174 base = start;
175 has_reph = true;
176 }
177
178 {
179 if (!has_reph)
180 base = limit;
181
182 for (unsigned int i = limit; i < end; i++)
183 if (is_consonant (info[i]))
184 {
185 base = i;
186 break;
187 }
188 }
189 }
190
191 /* Reorder! */
192 {
193 unsigned int i = start;
194 for (; i < start + (has_reph ? 3 : 0); i++)
195 info[i].myanmar_position() = POS_AFTER_MAIN;
196 for (; i < base; i++)
197 info[i].myanmar_position() = POS_PRE_C;
198 if (i < end)
199 {
200 info[i].myanmar_position() = POS_BASE_C;
201 i++;
202 }
Behdad Esfahbodf9b66052013-02-12 16:13:56 -0500203 indic_position_t pos = POS_AFTER_MAIN;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500204 /* The following loop may be ugly, but it implements all of
205 * Myanmar reordering! */
206 for (; i < end; i++)
207 {
208 if (info[i].myanmar_category() == OT_MR) /* Pre-base reordering */
209 {
210 info[i].myanmar_position() = POS_PRE_C;
211 continue;
212 }
213 if (info[i].myanmar_position() < POS_BASE_C) /* Left matra */
214 {
215 continue;
216 }
David Corbettccb03672018-02-02 12:04:04 -0500217 if (info[i].myanmar_category() == OT_VS)
218 {
219 info[i].myanmar_position() = info[i - 1].myanmar_position();
220 continue;
221 }
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500222
223 if (pos == POS_AFTER_MAIN && info[i].myanmar_category() == OT_VBlw)
224 {
225 pos = POS_BELOW_C;
226 info[i].myanmar_position() = pos;
227 continue;
228 }
229
230 if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_A)
231 {
232 info[i].myanmar_position() = POS_BEFORE_SUB;
233 continue;
234 }
235 if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_VBlw)
236 {
237 info[i].myanmar_position() = pos;
238 continue;
239 }
240 if (pos == POS_BELOW_C && info[i].myanmar_category() != OT_A)
241 {
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430242 pos = POS_AFTER_SUB;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500243 info[i].myanmar_position() = pos;
244 continue;
245 }
246 info[i].myanmar_position() = pos;
247 }
248 }
249
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500250 /* Sit tight, rock 'n roll! */
Behdad Esfahbodc403d632015-09-01 16:15:25 +0100251 buffer->sort (start, end, compare_myanmar_order);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500252}
253
254static void
Behdad Esfahbodd8b53532019-07-02 15:09:26 -0700255reorder_syllable_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
256 hb_face_t *face HB_UNUSED,
257 hb_buffer_t *buffer,
258 unsigned int start, unsigned int end)
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500259{
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -0700260 myanmar_syllable_type_t syllable_type = (myanmar_syllable_type_t) (buffer->info[start].syllable() & 0x0F);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500261 switch (syllable_type) {
Behdad Esfahbod56f71ff2015-07-22 11:58:11 +0100262
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -0700263 case myanmar_broken_cluster: /* We already inserted dotted-circles, so just call the consonant_syllable. */
264 case myanmar_consonant_syllable:
Behdad Esfahbod56f71ff2015-07-22 11:58:11 +0100265 initial_reordering_consonant_syllable (buffer, start, end);
266 break;
267
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -0700268 case myanmar_punctuation_cluster:
269 case myanmar_non_myanmar_cluster:
Behdad Esfahbod56f71ff2015-07-22 11:58:11 +0100270 break;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500271 }
272}
273
274static inline void
Behdad Esfahbod3724f132019-07-02 15:23:00 -0700275insert_dotted_circles_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
276 hb_font_t *font,
277 hb_buffer_t *buffer)
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500278{
Eric Muller30d7c402019-02-09 02:55:27 -0800279 if (unlikely (buffer->flags & HB_BUFFER_FLAG_DO_NOT_INSERT_DOTTED_CIRCLE))
280 return;
281
Behdad Esfahbod085793d2019-04-24 10:15:59 -0400282 /* Note: This loop is extra overhead, but should not be measurable.
283 * TODO Use a buffer scratch flag to remove the loop. */
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500284 bool has_broken_syllables = false;
285 unsigned int count = buffer->len;
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400286 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500287 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -0700288 if ((info[i].syllable() & 0x0F) == myanmar_broken_cluster)
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400289 {
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500290 has_broken_syllables = true;
291 break;
292 }
293 if (likely (!has_broken_syllables))
294 return;
295
296
297 hb_codepoint_t dottedcircle_glyph;
Behdad Esfahbod8b5bc142016-02-24 19:05:23 +0900298 if (!font->get_nominal_glyph (0x25CCu, &dottedcircle_glyph))
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500299 return;
300
301 hb_glyph_info_t dottedcircle = {0};
Behdad Esfahbod76271002014-07-11 14:54:42 -0400302 dottedcircle.codepoint = 0x25CCu;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500303 set_myanmar_properties (dottedcircle);
304 dottedcircle.codepoint = dottedcircle_glyph;
305
306 buffer->clear_output ();
307
308 buffer->idx = 0;
309 unsigned int last_syllable = 0;
Behdad Esfahbod7185b272018-05-31 20:03:00 -0700310 while (buffer->idx < buffer->len && buffer->successful)
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500311 {
312 unsigned int syllable = buffer->cur().syllable();
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -0700313 myanmar_syllable_type_t syllable_type = (myanmar_syllable_type_t) (syllable & 0x0F);
314 if (unlikely (last_syllable != syllable && syllable_type == myanmar_broken_cluster))
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500315 {
316 last_syllable = syllable;
317
Behdad Esfahbod6f932bc2015-10-21 11:16:49 -0200318 hb_glyph_info_t ginfo = dottedcircle;
319 ginfo.cluster = buffer->cur().cluster;
320 ginfo.mask = buffer->cur().mask;
321 ginfo.syllable() = buffer->cur().syllable();
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500322
Behdad Esfahbod6f932bc2015-10-21 11:16:49 -0200323 buffer->output_info (ginfo);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500324 }
325 else
326 buffer->next_glyph ();
327 }
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500328 buffer->swap_buffers ();
329}
330
331static void
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -0700332reorder_myanmar (const hb_ot_shape_plan_t *plan,
333 hb_font_t *font,
334 hb_buffer_t *buffer)
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500335{
Behdad Esfahbod3724f132019-07-02 15:23:00 -0700336 insert_dotted_circles_myanmar (plan, font, buffer);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500337
Behdad Esfahbod56f71ff2015-07-22 11:58:11 +0100338 foreach_syllable (buffer, start, end)
Behdad Esfahbodd8b53532019-07-02 15:09:26 -0700339 reorder_syllable_myanmar (plan, font->face, buffer, start, end);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500340
341 HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_category);
342 HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_position);
343}
344
Behdad Esfahbod71b4c992013-10-28 00:20:59 +0100345
Behdad Esfahbod00c5c4a2018-10-11 20:15:31 -0400346const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar =
347{
348 collect_features_myanmar,
349 override_features_myanmar,
350 nullptr, /* data_create */
351 nullptr, /* data_destroy */
352 nullptr, /* preprocess_text */
353 nullptr, /* postprocess_glyphs */
354 HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT,
355 nullptr, /* decompose */
356 nullptr, /* compose */
357 setup_masks_myanmar,
358 HB_TAG_NONE, /* gpos_tag */
359 nullptr, /* reorder_marks */
360 HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY,
361 false, /* fallback_position */
362};
363
364
Behdad Esfahbodbdb53ca2018-10-11 20:20:00 -0400365/* Ugly Zawgyi encoding.
366 * Disable all auto processing.
367 * https://github.com/harfbuzz/harfbuzz/issues/1162 */
368const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar_zawgyi =
369{
370 nullptr, /* collect_features */
371 nullptr, /* override_features */
372 nullptr, /* data_create */
373 nullptr, /* data_destroy */
374 nullptr, /* preprocess_text */
375 nullptr, /* postprocess_glyphs */
376 HB_OT_SHAPE_NORMALIZATION_MODE_NONE,
377 nullptr, /* decompose */
378 nullptr, /* compose */
379 nullptr, /* setup_masks */
380 HB_TAG_NONE, /* gpos_tag */
381 nullptr, /* reorder_marks */
382 HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
383 false, /* fallback_position */
384};
Behdad Esfahbod7aad5362019-06-26 13:21:03 -0700385
386
387#endif