blob: e6ae75e8f2a2b2bed4c90feb7991a497a154b427 [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 Esfahbodc417e0d2021-01-28 20:27:59 -070032#include "hb-ot-shape-complex-myanmar-machine.hh"
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050033
34
35/*
36 * Myanmar shaper.
37 */
38
39static const hb_tag_t
Behdad Esfahbodd8b53532019-07-02 15:09:26 -070040myanmar_basic_features[] =
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050041{
42 /*
43 * Basic features.
Behdad Esfahbod62a535f2021-08-03 10:11:27 -060044 * These features are applied in order, one at a time, after reordering,
45 * constrained to the syllable.
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050046 */
47 HB_TAG('r','p','h','f'),
48 HB_TAG('p','r','e','f'),
49 HB_TAG('b','l','w','f'),
50 HB_TAG('p','s','t','f'),
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -050051};
52static const hb_tag_t
Behdad Esfahbodd8b53532019-07-02 15:09:26 -070053myanmar_other_features[] =
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -050054{
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050055 /*
56 * Other features.
Behdad Esfahbod982c2f42018-10-26 15:40:12 -070057 * These features are applied all at once, after clearing syllables.
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050058 */
59 HB_TAG('p','r','e','s'),
60 HB_TAG('a','b','v','s'),
61 HB_TAG('b','l','w','s'),
62 HB_TAG('p','s','t','s'),
Behdad Esfahbod3583fb02018-09-23 22:33:38 -040063};
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050064
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050065static void
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -070066setup_syllables_myanmar (const hb_ot_shape_plan_t *plan,
67 hb_font_t *font,
68 hb_buffer_t *buffer);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050069static void
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -070070reorder_myanmar (const hb_ot_shape_plan_t *plan,
Behdad Esfahbod982c2f42018-10-26 15:40:12 -070071 hb_font_t *font,
72 hb_buffer_t *buffer);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050073
74static void
75collect_features_myanmar (hb_ot_shape_planner_t *plan)
76{
77 hb_ot_map_builder_t *map = &plan->map;
78
79 /* Do this before any lookups have been applied. */
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -070080 map->add_gsub_pause (setup_syllables_myanmar);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050081
Behdad Esfahbodf048ead2018-09-24 18:01:53 -040082 map->enable_feature (HB_TAG('l','o','c','l'));
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050083 /* The Indic specs do not require ccmp, but we apply it here since if
84 * there is a use of it, it's typically at the beginning. */
Behdad Esfahbodf048ead2018-09-24 18:01:53 -040085 map->enable_feature (HB_TAG('c','c','m','p'));
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050086
87
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -070088 map->add_gsub_pause (reorder_myanmar);
Behdad Esfahbod3583fb02018-09-23 22:33:38 -040089
Behdad Esfahbodd8b53532019-07-02 15:09:26 -070090 for (unsigned int i = 0; i < ARRAY_LENGTH (myanmar_basic_features); i++)
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -050091 {
Behdad Esfahbodd8b53532019-07-02 15:09:26 -070092 map->enable_feature (myanmar_basic_features[i], F_MANUAL_ZWJ);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +020093 map->add_gsub_pause (nullptr);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -050094 }
Behdad Esfahbod3583fb02018-09-23 22:33:38 -040095
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -070096 map->add_gsub_pause (_hb_clear_syllables);
Behdad Esfahbod3583fb02018-09-23 22:33:38 -040097
Behdad Esfahbodd8b53532019-07-02 15:09:26 -070098 for (unsigned int i = 0; i < ARRAY_LENGTH (myanmar_other_features); i++)
99 map->enable_feature (myanmar_other_features[i], F_MANUAL_ZWJ);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500100}
101
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500102static void
103setup_masks_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
Ebrahim Byagowi05584132019-10-01 13:49:55 +0330104 hb_buffer_t *buffer,
105 hb_font_t *font HB_UNUSED)
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500106{
107 HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_category);
108 HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_position);
109
110 /* We cannot setup masks here. We save information about characters
111 * and setup masks later on in a pause-callback. */
112
113 unsigned int count = buffer->len;
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400114 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500115 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400116 set_myanmar_properties (info[i]);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500117}
118
119static void
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -0700120setup_syllables_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
121 hb_font_t *font HB_UNUSED,
122 hb_buffer_t *buffer)
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500123{
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -0700124 find_syllables_myanmar (buffer);
Behdad Esfahbod9e005c52017-08-10 18:45:33 -0700125 foreach_syllable (buffer, start, end)
126 buffer->unsafe_to_break (start, end);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500127}
128
129static int
130compare_myanmar_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
131{
132 int a = pa->myanmar_position();
133 int b = pb->myanmar_position();
134
135 return a < b ? -1 : a == b ? 0 : +1;
136}
137
138
139/* Rules from:
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +0430140 * https://docs.microsoft.com/en-us/typography/script-development/myanmar */
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500141
142static void
Behdad Esfahbod56f71ff2015-07-22 11:58:11 +0100143initial_reordering_consonant_syllable (hb_buffer_t *buffer,
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500144 unsigned int start, unsigned int end)
145{
146 hb_glyph_info_t *info = buffer->info;
147
148 unsigned int base = end;
149 bool has_reph = false;
150
151 {
152 unsigned int limit = start;
153 if (start + 3 <= end &&
154 info[start ].myanmar_category() == OT_Ra &&
155 info[start+1].myanmar_category() == OT_As &&
156 info[start+2].myanmar_category() == OT_H)
157 {
158 limit += 3;
159 base = start;
160 has_reph = true;
161 }
162
163 {
164 if (!has_reph)
165 base = limit;
166
167 for (unsigned int i = limit; i < end; i++)
168 if (is_consonant (info[i]))
169 {
170 base = i;
171 break;
172 }
173 }
174 }
175
176 /* Reorder! */
177 {
178 unsigned int i = start;
179 for (; i < start + (has_reph ? 3 : 0); i++)
180 info[i].myanmar_position() = POS_AFTER_MAIN;
181 for (; i < base; i++)
182 info[i].myanmar_position() = POS_PRE_C;
183 if (i < end)
184 {
185 info[i].myanmar_position() = POS_BASE_C;
186 i++;
187 }
Behdad Esfahbod5d957ff2021-09-20 11:33:01 -0600188 myanmar_position_t pos = POS_AFTER_MAIN;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500189 /* The following loop may be ugly, but it implements all of
190 * Myanmar reordering! */
191 for (; i < end; i++)
192 {
193 if (info[i].myanmar_category() == OT_MR) /* Pre-base reordering */
194 {
195 info[i].myanmar_position() = POS_PRE_C;
196 continue;
197 }
198 if (info[i].myanmar_position() < POS_BASE_C) /* Left matra */
199 {
200 continue;
201 }
David Corbettccb03672018-02-02 12:04:04 -0500202 if (info[i].myanmar_category() == OT_VS)
203 {
204 info[i].myanmar_position() = info[i - 1].myanmar_position();
205 continue;
206 }
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500207
208 if (pos == POS_AFTER_MAIN && info[i].myanmar_category() == OT_VBlw)
209 {
210 pos = POS_BELOW_C;
211 info[i].myanmar_position() = pos;
212 continue;
213 }
214
215 if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_A)
216 {
217 info[i].myanmar_position() = POS_BEFORE_SUB;
218 continue;
219 }
220 if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_VBlw)
221 {
222 info[i].myanmar_position() = pos;
223 continue;
224 }
225 if (pos == POS_BELOW_C && info[i].myanmar_category() != OT_A)
226 {
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430227 pos = POS_AFTER_SUB;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500228 info[i].myanmar_position() = pos;
229 continue;
230 }
231 info[i].myanmar_position() = pos;
232 }
233 }
234
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500235 /* Sit tight, rock 'n roll! */
Behdad Esfahbodc403d632015-09-01 16:15:25 +0100236 buffer->sort (start, end, compare_myanmar_order);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500237}
238
239static void
Behdad Esfahbodd8b53532019-07-02 15:09:26 -0700240reorder_syllable_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
241 hb_face_t *face HB_UNUSED,
242 hb_buffer_t *buffer,
243 unsigned int start, unsigned int end)
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500244{
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -0700245 myanmar_syllable_type_t syllable_type = (myanmar_syllable_type_t) (buffer->info[start].syllable() & 0x0F);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500246 switch (syllable_type) {
Behdad Esfahbod56f71ff2015-07-22 11:58:11 +0100247
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -0700248 case myanmar_broken_cluster: /* We already inserted dotted-circles, so just call the consonant_syllable. */
249 case myanmar_consonant_syllable:
Behdad Esfahbod56f71ff2015-07-22 11:58:11 +0100250 initial_reordering_consonant_syllable (buffer, start, end);
251 break;
252
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -0700253 case myanmar_punctuation_cluster:
254 case myanmar_non_myanmar_cluster:
Behdad Esfahbod56f71ff2015-07-22 11:58:11 +0100255 break;
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500256 }
257}
258
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500259static void
Behdad Esfahbodd115a9e2019-07-02 14:42:45 -0700260reorder_myanmar (const hb_ot_shape_plan_t *plan,
261 hb_font_t *font,
262 hb_buffer_t *buffer)
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500263{
Behdad Esfahbod0ddade42021-01-15 19:13:47 -0700264 if (buffer->message (font, "start reordering myanmar"))
265 {
266 hb_syllabic_insert_dotted_circles (font, buffer,
267 myanmar_broken_cluster,
268 OT_GB);
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500269
Simon Cozensb50099b2020-09-17 14:25:30 +0100270 foreach_syllable (buffer, start, end)
271 reorder_syllable_myanmar (plan, font->face, buffer, start, end);
Simon Cozens428c1112020-09-18 16:24:47 +0100272 (void) buffer->message (font, "end reordering myanmar");
Simon Cozensb50099b2020-09-17 14:25:30 +0100273 }
Behdad Esfahbod98628ca2013-02-11 13:36:23 -0500274
275 HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_category);
276 HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_position);
277}
278
Behdad Esfahbod71b4c992013-10-28 00:20:59 +0100279
Behdad Esfahbod00c5c4a2018-10-11 20:15:31 -0400280const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar =
281{
282 collect_features_myanmar,
Khaled Hosny7d64b092020-09-12 21:17:18 +0200283 nullptr, /* override_features */
Behdad Esfahbod00c5c4a2018-10-11 20:15:31 -0400284 nullptr, /* data_create */
285 nullptr, /* data_destroy */
286 nullptr, /* preprocess_text */
287 nullptr, /* postprocess_glyphs */
288 HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT,
289 nullptr, /* decompose */
290 nullptr, /* compose */
291 setup_masks_myanmar,
292 HB_TAG_NONE, /* gpos_tag */
293 nullptr, /* reorder_marks */
294 HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY,
295 false, /* fallback_position */
296};
297
298
Behdad Esfahbodbdb53ca2018-10-11 20:20:00 -0400299/* Ugly Zawgyi encoding.
300 * Disable all auto processing.
301 * https://github.com/harfbuzz/harfbuzz/issues/1162 */
302const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar_zawgyi =
303{
304 nullptr, /* collect_features */
305 nullptr, /* override_features */
306 nullptr, /* data_create */
307 nullptr, /* data_destroy */
308 nullptr, /* preprocess_text */
309 nullptr, /* postprocess_glyphs */
310 HB_OT_SHAPE_NORMALIZATION_MODE_NONE,
311 nullptr, /* decompose */
312 nullptr, /* compose */
313 nullptr, /* setup_masks */
314 HB_TAG_NONE, /* gpos_tag */
315 nullptr, /* reorder_marks */
316 HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
317 false, /* fallback_position */
318};
Behdad Esfahbod7aad5362019-06-26 13:21:03 -0700319
320
321#endif