Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 1 | /* |
| 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 Esfahbod | 7aad536 | 2019-06-26 13:21:03 -0700 | [diff] [blame] | 27 | #include "hb.hh" |
| 28 | |
| 29 | #ifndef HB_NO_OT_SHAPE |
| 30 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 31 | #include "hb-ot-shape-complex-myanmar.hh" |
Behdad Esfahbod | c417e0d | 2021-01-28 20:27:59 -0700 | [diff] [blame] | 32 | #include "hb-ot-shape-complex-myanmar-machine.hh" |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 33 | |
| 34 | |
| 35 | /* |
| 36 | * Myanmar shaper. |
| 37 | */ |
| 38 | |
| 39 | static const hb_tag_t |
Behdad Esfahbod | d8b5353 | 2019-07-02 15:09:26 -0700 | [diff] [blame] | 40 | myanmar_basic_features[] = |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 41 | { |
| 42 | /* |
| 43 | * Basic features. |
Behdad Esfahbod | 62a535f | 2021-08-03 10:11:27 -0600 | [diff] [blame] | 44 | * These features are applied in order, one at a time, after reordering, |
| 45 | * constrained to the syllable. |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 46 | */ |
| 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 Esfahbod | ee9c3a1 | 2013-02-15 06:22:26 -0500 | [diff] [blame] | 51 | }; |
| 52 | static const hb_tag_t |
Behdad Esfahbod | d8b5353 | 2019-07-02 15:09:26 -0700 | [diff] [blame] | 53 | myanmar_other_features[] = |
Behdad Esfahbod | ee9c3a1 | 2013-02-15 06:22:26 -0500 | [diff] [blame] | 54 | { |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 55 | /* |
| 56 | * Other features. |
Behdad Esfahbod | 982c2f4 | 2018-10-26 15:40:12 -0700 | [diff] [blame] | 57 | * These features are applied all at once, after clearing syllables. |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 58 | */ |
| 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 Esfahbod | 3583fb0 | 2018-09-23 22:33:38 -0400 | [diff] [blame] | 63 | }; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 64 | |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 65 | static void |
Behdad Esfahbod | d115a9e | 2019-07-02 14:42:45 -0700 | [diff] [blame] | 66 | setup_syllables_myanmar (const hb_ot_shape_plan_t *plan, |
| 67 | hb_font_t *font, |
| 68 | hb_buffer_t *buffer); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 69 | static void |
Behdad Esfahbod | d115a9e | 2019-07-02 14:42:45 -0700 | [diff] [blame] | 70 | reorder_myanmar (const hb_ot_shape_plan_t *plan, |
Behdad Esfahbod | 982c2f4 | 2018-10-26 15:40:12 -0700 | [diff] [blame] | 71 | hb_font_t *font, |
| 72 | hb_buffer_t *buffer); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 73 | |
| 74 | static void |
| 75 | collect_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 Esfahbod | d115a9e | 2019-07-02 14:42:45 -0700 | [diff] [blame] | 80 | map->add_gsub_pause (setup_syllables_myanmar); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 81 | |
Behdad Esfahbod | f048ead | 2018-09-24 18:01:53 -0400 | [diff] [blame] | 82 | map->enable_feature (HB_TAG('l','o','c','l')); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 83 | /* 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 Esfahbod | f048ead | 2018-09-24 18:01:53 -0400 | [diff] [blame] | 85 | map->enable_feature (HB_TAG('c','c','m','p')); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 86 | |
| 87 | |
Behdad Esfahbod | d115a9e | 2019-07-02 14:42:45 -0700 | [diff] [blame] | 88 | map->add_gsub_pause (reorder_myanmar); |
Behdad Esfahbod | 3583fb0 | 2018-09-23 22:33:38 -0400 | [diff] [blame] | 89 | |
Behdad Esfahbod | d8b5353 | 2019-07-02 15:09:26 -0700 | [diff] [blame] | 90 | for (unsigned int i = 0; i < ARRAY_LENGTH (myanmar_basic_features); i++) |
Behdad Esfahbod | ee9c3a1 | 2013-02-15 06:22:26 -0500 | [diff] [blame] | 91 | { |
Behdad Esfahbod | d8b5353 | 2019-07-02 15:09:26 -0700 | [diff] [blame] | 92 | map->enable_feature (myanmar_basic_features[i], F_MANUAL_ZWJ); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 93 | map->add_gsub_pause (nullptr); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 94 | } |
Behdad Esfahbod | 3583fb0 | 2018-09-23 22:33:38 -0400 | [diff] [blame] | 95 | |
Behdad Esfahbod | d115a9e | 2019-07-02 14:42:45 -0700 | [diff] [blame] | 96 | map->add_gsub_pause (_hb_clear_syllables); |
Behdad Esfahbod | 3583fb0 | 2018-09-23 22:33:38 -0400 | [diff] [blame] | 97 | |
Behdad Esfahbod | d8b5353 | 2019-07-02 15:09:26 -0700 | [diff] [blame] | 98 | for (unsigned int i = 0; i < ARRAY_LENGTH (myanmar_other_features); i++) |
| 99 | map->enable_feature (myanmar_other_features[i], F_MANUAL_ZWJ); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 100 | } |
| 101 | |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 102 | static void |
| 103 | setup_masks_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED, |
Ebrahim Byagowi | 0558413 | 2019-10-01 13:49:55 +0330 | [diff] [blame] | 104 | hb_buffer_t *buffer, |
| 105 | hb_font_t *font HB_UNUSED) |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 106 | { |
| 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 Esfahbod | 7cd33f2 | 2014-07-17 14:22:11 -0400 | [diff] [blame] | 114 | hb_glyph_info_t *info = buffer->info; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 115 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | 7cd33f2 | 2014-07-17 14:22:11 -0400 | [diff] [blame] | 116 | set_myanmar_properties (info[i]); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static void |
Behdad Esfahbod | d115a9e | 2019-07-02 14:42:45 -0700 | [diff] [blame] | 120 | setup_syllables_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED, |
| 121 | hb_font_t *font HB_UNUSED, |
| 122 | hb_buffer_t *buffer) |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 123 | { |
Behdad Esfahbod | d115a9e | 2019-07-02 14:42:45 -0700 | [diff] [blame] | 124 | find_syllables_myanmar (buffer); |
Behdad Esfahbod | 9e005c5 | 2017-08-10 18:45:33 -0700 | [diff] [blame] | 125 | foreach_syllable (buffer, start, end) |
| 126 | buffer->unsafe_to_break (start, end); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static int |
| 130 | compare_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 Byagowi | f24b0b9 | 2018-04-12 13:40:45 +0430 | [diff] [blame] | 140 | * https://docs.microsoft.com/en-us/typography/script-development/myanmar */ |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 141 | |
| 142 | static void |
Behdad Esfahbod | 56f71ff | 2015-07-22 11:58:11 +0100 | [diff] [blame] | 143 | initial_reordering_consonant_syllable (hb_buffer_t *buffer, |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 144 | 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 Esfahbod | 5d957ff | 2021-09-20 11:33:01 -0600 | [diff] [blame] | 188 | myanmar_position_t pos = POS_AFTER_MAIN; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 189 | /* 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 Corbett | ccb0367 | 2018-02-02 12:04:04 -0500 | [diff] [blame] | 202 | if (info[i].myanmar_category() == OT_VS) |
| 203 | { |
| 204 | info[i].myanmar_position() = info[i - 1].myanmar_position(); |
| 205 | continue; |
| 206 | } |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 207 | |
| 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 Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 227 | pos = POS_AFTER_SUB; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 228 | info[i].myanmar_position() = pos; |
| 229 | continue; |
| 230 | } |
| 231 | info[i].myanmar_position() = pos; |
| 232 | } |
| 233 | } |
| 234 | |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 235 | /* Sit tight, rock 'n roll! */ |
Behdad Esfahbod | c403d63 | 2015-09-01 16:15:25 +0100 | [diff] [blame] | 236 | buffer->sort (start, end, compare_myanmar_order); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static void |
Behdad Esfahbod | d8b5353 | 2019-07-02 15:09:26 -0700 | [diff] [blame] | 240 | reorder_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 Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 244 | { |
Behdad Esfahbod | d115a9e | 2019-07-02 14:42:45 -0700 | [diff] [blame] | 245 | myanmar_syllable_type_t syllable_type = (myanmar_syllable_type_t) (buffer->info[start].syllable() & 0x0F); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 246 | switch (syllable_type) { |
Behdad Esfahbod | 56f71ff | 2015-07-22 11:58:11 +0100 | [diff] [blame] | 247 | |
Behdad Esfahbod | d115a9e | 2019-07-02 14:42:45 -0700 | [diff] [blame] | 248 | case myanmar_broken_cluster: /* We already inserted dotted-circles, so just call the consonant_syllable. */ |
| 249 | case myanmar_consonant_syllable: |
Behdad Esfahbod | 56f71ff | 2015-07-22 11:58:11 +0100 | [diff] [blame] | 250 | initial_reordering_consonant_syllable (buffer, start, end); |
| 251 | break; |
| 252 | |
Behdad Esfahbod | d115a9e | 2019-07-02 14:42:45 -0700 | [diff] [blame] | 253 | case myanmar_punctuation_cluster: |
| 254 | case myanmar_non_myanmar_cluster: |
Behdad Esfahbod | 56f71ff | 2015-07-22 11:58:11 +0100 | [diff] [blame] | 255 | break; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 259 | static void |
Behdad Esfahbod | d115a9e | 2019-07-02 14:42:45 -0700 | [diff] [blame] | 260 | reorder_myanmar (const hb_ot_shape_plan_t *plan, |
| 261 | hb_font_t *font, |
| 262 | hb_buffer_t *buffer) |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 263 | { |
Behdad Esfahbod | 0ddade4 | 2021-01-15 19:13:47 -0700 | [diff] [blame] | 264 | if (buffer->message (font, "start reordering myanmar")) |
| 265 | { |
| 266 | hb_syllabic_insert_dotted_circles (font, buffer, |
| 267 | myanmar_broken_cluster, |
| 268 | OT_GB); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 269 | |
Simon Cozens | b50099b | 2020-09-17 14:25:30 +0100 | [diff] [blame] | 270 | foreach_syllable (buffer, start, end) |
| 271 | reorder_syllable_myanmar (plan, font->face, buffer, start, end); |
Simon Cozens | 428c111 | 2020-09-18 16:24:47 +0100 | [diff] [blame] | 272 | (void) buffer->message (font, "end reordering myanmar"); |
Simon Cozens | b50099b | 2020-09-17 14:25:30 +0100 | [diff] [blame] | 273 | } |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 274 | |
| 275 | HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_category); |
| 276 | HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_position); |
| 277 | } |
| 278 | |
Behdad Esfahbod | 71b4c99 | 2013-10-28 00:20:59 +0100 | [diff] [blame] | 279 | |
Behdad Esfahbod | 00c5c4a | 2018-10-11 20:15:31 -0400 | [diff] [blame] | 280 | const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar = |
| 281 | { |
| 282 | collect_features_myanmar, |
Khaled Hosny | 7d64b09 | 2020-09-12 21:17:18 +0200 | [diff] [blame] | 283 | nullptr, /* override_features */ |
Behdad Esfahbod | 00c5c4a | 2018-10-11 20:15:31 -0400 | [diff] [blame] | 284 | 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 Esfahbod | bdb53ca | 2018-10-11 20:20:00 -0400 | [diff] [blame] | 299 | /* Ugly Zawgyi encoding. |
| 300 | * Disable all auto processing. |
| 301 | * https://github.com/harfbuzz/harfbuzz/issues/1162 */ |
| 302 | const 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 Esfahbod | 7aad536 | 2019-06-26 13:21:03 -0700 | [diff] [blame] | 319 | |
| 320 | |
| 321 | #endif |