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 | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 27 | #include "hb-ot-shape-complex-indic-private.hh" |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 28 | |
| 29 | /* buffer var allocations */ |
| 30 | #define myanmar_category() complex_var_u8_0() /* myanmar_category_t */ |
Behdad Esfahbod | 3a83d33 | 2013-02-12 12:14:10 -0500 | [diff] [blame] | 31 | #define myanmar_position() complex_var_u8_1() /* myanmar_position_t */ |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 32 | |
| 33 | |
| 34 | /* |
| 35 | * Myanmar shaper. |
| 36 | */ |
| 37 | |
| 38 | static const hb_tag_t |
Behdad Esfahbod | ee9c3a1 | 2013-02-15 06:22:26 -0500 | [diff] [blame] | 39 | basic_features[] = |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 40 | { |
| 41 | /* |
| 42 | * Basic features. |
| 43 | * These features are applied in order, one at a time, after initial_reordering. |
| 44 | */ |
| 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 Esfahbod | ee9c3a1 | 2013-02-15 06:22:26 -0500 | [diff] [blame] | 49 | }; |
| 50 | static const hb_tag_t |
| 51 | other_features[] = |
| 52 | { |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 53 | /* |
| 54 | * Other features. |
| 55 | * These features are applied all at once, after final_reordering. |
| 56 | */ |
| 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'), |
| 61 | /* Positioning features, though we don't care about the types. */ |
| 62 | HB_TAG('d','i','s','t'), |
Behdad Esfahbod | 5e7432b | 2013-10-15 12:28:23 +0200 | [diff] [blame] | 63 | /* Pre-release version of Windows 8 Myanmar font had abvm,blwm |
| 64 | * features. The released Windows 8 version of the font (as well |
| 65 | * as the released spec) used 'mark' instead. The Windows 8 |
| 66 | * shaper however didn't apply 'mark' but did apply 'mkmk'. |
| 67 | * Perhaps it applied abvm/blwm. This was fixed in a Windows 8 |
| 68 | * update, so now it applies mark/mkmk. We are guessing that |
| 69 | * it still applies abvm/blwm too. |
| 70 | */ |
| 71 | HB_TAG('a','b','v','m'), |
| 72 | HB_TAG('b','l','w','m'), |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 73 | }; |
| 74 | |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 75 | static void |
| 76 | setup_syllables (const hb_ot_shape_plan_t *plan, |
| 77 | hb_font_t *font, |
| 78 | hb_buffer_t *buffer); |
| 79 | static void |
| 80 | initial_reordering (const hb_ot_shape_plan_t *plan, |
| 81 | hb_font_t *font, |
| 82 | hb_buffer_t *buffer); |
| 83 | static void |
| 84 | final_reordering (const hb_ot_shape_plan_t *plan, |
| 85 | hb_font_t *font, |
| 86 | hb_buffer_t *buffer); |
| 87 | |
| 88 | static void |
| 89 | collect_features_myanmar (hb_ot_shape_planner_t *plan) |
| 90 | { |
| 91 | hb_ot_map_builder_t *map = &plan->map; |
| 92 | |
| 93 | /* Do this before any lookups have been applied. */ |
| 94 | map->add_gsub_pause (setup_syllables); |
| 95 | |
Behdad Esfahbod | e7ffcfa | 2013-02-14 11:05:56 -0500 | [diff] [blame] | 96 | map->add_global_bool_feature (HB_TAG('l','o','c','l')); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 97 | /* The Indic specs do not require ccmp, but we apply it here since if |
| 98 | * there is a use of it, it's typically at the beginning. */ |
Behdad Esfahbod | e7ffcfa | 2013-02-14 11:05:56 -0500 | [diff] [blame] | 99 | map->add_global_bool_feature (HB_TAG('c','c','m','p')); |
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 | map->add_gsub_pause (initial_reordering); |
Behdad Esfahbod | ee9c3a1 | 2013-02-15 06:22:26 -0500 | [diff] [blame] | 103 | for (unsigned int i = 0; i < ARRAY_LENGTH (basic_features); i++) |
| 104 | { |
Behdad Esfahbod | a8cf7b4 | 2013-03-19 05:53:26 -0400 | [diff] [blame] | 105 | map->add_feature (basic_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 106 | map->add_gsub_pause (NULL); |
| 107 | } |
| 108 | map->add_gsub_pause (final_reordering); |
Behdad Esfahbod | ee9c3a1 | 2013-02-15 06:22:26 -0500 | [diff] [blame] | 109 | for (unsigned int i = 0; i < ARRAY_LENGTH (other_features); i++) |
Behdad Esfahbod | a8cf7b4 | 2013-03-19 05:53:26 -0400 | [diff] [blame] | 110 | map->add_feature (other_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static void |
| 114 | override_features_myanmar (hb_ot_shape_planner_t *plan) |
| 115 | { |
Behdad Esfahbod | ec54486 | 2013-02-14 11:25:10 -0500 | [diff] [blame] | 116 | plan->map.add_feature (HB_TAG('l','i','g','a'), 0, F_GLOBAL); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
| 120 | enum syllable_type_t { |
| 121 | consonant_syllable, |
Behdad Esfahbod | 9174a9d | 2013-11-25 18:10:38 -0500 | [diff] [blame] | 122 | punctuation_cluster, |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 123 | broken_cluster, |
| 124 | non_myanmar_cluster, |
| 125 | }; |
| 126 | |
| 127 | #include "hb-ot-shape-complex-myanmar-machine.hh" |
| 128 | |
| 129 | |
| 130 | /* Note: This enum is duplicated in the -machine.rl source file. |
| 131 | * Not sure how to avoid duplication. */ |
| 132 | enum myanmar_category_t { |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 133 | OT_As = 18, /* Asat */ |
| 134 | OT_D = 19, /* Digits except zero */ |
| 135 | OT_D0 = 20, /* Digit zero */ |
| 136 | OT_DB = OT_N, /* Dot below */ |
Behdad Esfahbod | cf78dd4 | 2014-05-27 17:53:37 -0400 | [diff] [blame] | 137 | OT_GB = OT_PLACEHOLDER, |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 138 | OT_MH = 21, /* Various consonant medial types */ |
| 139 | OT_MR = 22, /* Various consonant medial types */ |
| 140 | OT_MW = 23, /* Various consonant medial types */ |
| 141 | OT_MY = 24, /* Various consonant medial types */ |
| 142 | OT_PT = 25, /* Pwo and other tones */ |
| 143 | OT_VAbv = 26, |
| 144 | OT_VBlw = 27, |
| 145 | OT_VPre = 28, |
| 146 | OT_VPst = 29, |
Behdad Esfahbod | 9174a9d | 2013-11-25 18:10:38 -0500 | [diff] [blame] | 147 | OT_VS = 30, /* Variation selectors */ |
| 148 | OT_P = 31 /* Punctuation */ |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 149 | }; |
| 150 | |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 151 | |
| 152 | static inline bool |
| 153 | is_one_of (const hb_glyph_info_t &info, unsigned int flags) |
| 154 | { |
| 155 | /* If it ligated, all bets are off. */ |
Behdad Esfahbod | a1f7b28 | 2013-10-18 01:09:08 +0200 | [diff] [blame] | 156 | if (_hb_glyph_info_ligated (&info)) return false; |
Behdad Esfahbod | f8160a4 | 2015-07-21 15:50:02 +0100 | [diff] [blame] | 157 | return !!(FLAG_SAFE (info.myanmar_category()) & flags); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 158 | } |
| 159 | |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 160 | static inline bool |
| 161 | is_consonant (const hb_glyph_info_t &info) |
| 162 | { |
| 163 | return is_one_of (info, CONSONANT_FLAGS); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | static inline void |
| 168 | set_myanmar_properties (hb_glyph_info_t &info) |
| 169 | { |
| 170 | hb_codepoint_t u = info.codepoint; |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 171 | unsigned int type = hb_indic_get_categories (u); |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 172 | indic_category_t cat = (indic_category_t) (type & 0x7Fu); |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 173 | indic_position_t pos = (indic_position_t) (type >> 8); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 174 | |
| 175 | /* Myanmar |
| 176 | * http://www.microsoft.com/typography/OpenTypeDev/myanmar/intro.htm#analyze |
| 177 | */ |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 178 | if (unlikely (hb_in_range (u, 0xFE00u, 0xFE0Fu))) |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 179 | cat = (indic_category_t) OT_VS; |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 180 | |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 181 | switch (u) |
| 182 | { |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 183 | case 0x104Eu: |
Behdad Esfahbod | 096b71e | 2013-11-25 18:03:34 -0500 | [diff] [blame] | 184 | cat = (indic_category_t) OT_C; /* The spec says C, IndicSyllableCategory doesn't have. */ |
| 185 | break; |
| 186 | |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 187 | case 0x002Du: case 0x00A0u: case 0x00D7u: case 0x2012u: |
| 188 | case 0x2013u: case 0x2014u: case 0x2015u: case 0x2022u: |
| 189 | case 0x25CCu: case 0x25FBu: case 0x25FCu: case 0x25FDu: |
| 190 | case 0x25FEu: |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 191 | cat = (indic_category_t) OT_GB; |
Behdad Esfahbod | 0572c14 | 2013-02-11 16:06:02 -0500 | [diff] [blame] | 192 | break; |
| 193 | |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 194 | case 0x1004u: case 0x101Bu: case 0x105Au: |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 195 | cat = (indic_category_t) OT_Ra; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 196 | break; |
| 197 | |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 198 | case 0x1032u: case 0x1036u: |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 199 | cat = (indic_category_t) OT_A; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 200 | break; |
| 201 | |
Behdad Esfahbod | 2813e30 | 2015-12-18 11:05:11 +0000 | [diff] [blame] | 202 | case 0x1039u: |
| 203 | cat = (indic_category_t) OT_H; |
| 204 | break; |
| 205 | |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 206 | case 0x103Au: |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 207 | cat = (indic_category_t) OT_As; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 208 | break; |
| 209 | |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 210 | case 0x1041u: case 0x1042u: case 0x1043u: case 0x1044u: |
| 211 | case 0x1045u: case 0x1046u: case 0x1047u: case 0x1048u: |
| 212 | case 0x1049u: case 0x1090u: case 0x1091u: case 0x1092u: |
| 213 | case 0x1093u: case 0x1094u: case 0x1095u: case 0x1096u: |
| 214 | case 0x1097u: case 0x1098u: case 0x1099u: |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 215 | cat = (indic_category_t) OT_D; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 216 | break; |
| 217 | |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 218 | case 0x1040u: |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 219 | cat = (indic_category_t) OT_D; /* XXX The spec says D0, but Uniscribe doesn't seem to do. */ |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 220 | break; |
| 221 | |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 222 | case 0x103Eu: case 0x1060u: |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 223 | cat = (indic_category_t) OT_MH; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 224 | break; |
| 225 | |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 226 | case 0x103Cu: |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 227 | cat = (indic_category_t) OT_MR; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 228 | break; |
| 229 | |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 230 | case 0x103Du: case 0x1082u: |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 231 | cat = (indic_category_t) OT_MW; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 232 | break; |
| 233 | |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 234 | case 0x103Bu: case 0x105Eu: case 0x105Fu: |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 235 | cat = (indic_category_t) OT_MY; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 236 | break; |
| 237 | |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 238 | case 0x1063u: case 0x1064u: case 0x1069u: case 0x106Au: |
| 239 | case 0x106Bu: case 0x106Cu: case 0x106Du: case 0xAA7Bu: |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 240 | cat = (indic_category_t) OT_PT; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 241 | break; |
| 242 | |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 243 | case 0x1038u: case 0x1087u: case 0x1088u: case 0x1089u: |
| 244 | case 0x108Au: case 0x108Bu: case 0x108Cu: case 0x108Du: |
| 245 | case 0x108Fu: case 0x109Au: case 0x109Bu: case 0x109Cu: |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 246 | cat = (indic_category_t) OT_SM; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 247 | break; |
Behdad Esfahbod | 9174a9d | 2013-11-25 18:10:38 -0500 | [diff] [blame] | 248 | |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 249 | case 0x104Au: case 0x104Bu: |
Behdad Esfahbod | 9174a9d | 2013-11-25 18:10:38 -0500 | [diff] [blame] | 250 | cat = (indic_category_t) OT_P; |
| 251 | break; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 252 | } |
| 253 | |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 254 | if (cat == OT_M) |
| 255 | { |
| 256 | switch ((int) pos) |
| 257 | { |
| 258 | case POS_PRE_C: cat = (indic_category_t) OT_VPre; |
| 259 | pos = POS_PRE_M; break; |
| 260 | case POS_ABOVE_C: cat = (indic_category_t) OT_VAbv; break; |
| 261 | case POS_BELOW_C: cat = (indic_category_t) OT_VBlw; break; |
| 262 | case POS_POST_C: cat = (indic_category_t) OT_VPst; break; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | info.myanmar_category() = (myanmar_category_t) cat; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 267 | info.myanmar_position() = pos; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | |
| 272 | static void |
| 273 | setup_masks_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED, |
| 274 | hb_buffer_t *buffer, |
| 275 | hb_font_t *font HB_UNUSED) |
| 276 | { |
| 277 | HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_category); |
| 278 | HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_position); |
| 279 | |
| 280 | /* We cannot setup masks here. We save information about characters |
| 281 | * and setup masks later on in a pause-callback. */ |
| 282 | |
| 283 | unsigned int count = buffer->len; |
Behdad Esfahbod | 7cd33f2 | 2014-07-17 14:22:11 -0400 | [diff] [blame] | 284 | hb_glyph_info_t *info = buffer->info; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 285 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | 7cd33f2 | 2014-07-17 14:22:11 -0400 | [diff] [blame] | 286 | set_myanmar_properties (info[i]); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static void |
| 290 | setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED, |
| 291 | hb_font_t *font HB_UNUSED, |
| 292 | hb_buffer_t *buffer) |
| 293 | { |
| 294 | find_syllables (buffer); |
| 295 | } |
| 296 | |
| 297 | static int |
| 298 | compare_myanmar_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb) |
| 299 | { |
| 300 | int a = pa->myanmar_position(); |
| 301 | int b = pb->myanmar_position(); |
| 302 | |
| 303 | return a < b ? -1 : a == b ? 0 : +1; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /* Rules from: |
| 308 | * http://www.microsoft.com/typography/OpenTypeDev/myanmar/intro.htm */ |
| 309 | |
| 310 | static void |
Behdad Esfahbod | 56f71ff | 2015-07-22 11:58:11 +0100 | [diff] [blame] | 311 | initial_reordering_consonant_syllable (hb_buffer_t *buffer, |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 312 | unsigned int start, unsigned int end) |
| 313 | { |
| 314 | hb_glyph_info_t *info = buffer->info; |
| 315 | |
| 316 | unsigned int base = end; |
| 317 | bool has_reph = false; |
| 318 | |
| 319 | { |
| 320 | unsigned int limit = start; |
| 321 | if (start + 3 <= end && |
| 322 | info[start ].myanmar_category() == OT_Ra && |
| 323 | info[start+1].myanmar_category() == OT_As && |
| 324 | info[start+2].myanmar_category() == OT_H) |
| 325 | { |
| 326 | limit += 3; |
| 327 | base = start; |
| 328 | has_reph = true; |
| 329 | } |
| 330 | |
| 331 | { |
| 332 | if (!has_reph) |
| 333 | base = limit; |
| 334 | |
| 335 | for (unsigned int i = limit; i < end; i++) |
| 336 | if (is_consonant (info[i])) |
| 337 | { |
| 338 | base = i; |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /* Reorder! */ |
| 345 | { |
| 346 | unsigned int i = start; |
| 347 | for (; i < start + (has_reph ? 3 : 0); i++) |
| 348 | info[i].myanmar_position() = POS_AFTER_MAIN; |
| 349 | for (; i < base; i++) |
| 350 | info[i].myanmar_position() = POS_PRE_C; |
| 351 | if (i < end) |
| 352 | { |
| 353 | info[i].myanmar_position() = POS_BASE_C; |
| 354 | i++; |
| 355 | } |
Behdad Esfahbod | f9b6605 | 2013-02-12 16:13:56 -0500 | [diff] [blame] | 356 | indic_position_t pos = POS_AFTER_MAIN; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 357 | /* The following loop may be ugly, but it implements all of |
| 358 | * Myanmar reordering! */ |
| 359 | for (; i < end; i++) |
| 360 | { |
| 361 | if (info[i].myanmar_category() == OT_MR) /* Pre-base reordering */ |
| 362 | { |
| 363 | info[i].myanmar_position() = POS_PRE_C; |
| 364 | continue; |
| 365 | } |
| 366 | if (info[i].myanmar_position() < POS_BASE_C) /* Left matra */ |
| 367 | { |
| 368 | continue; |
| 369 | } |
| 370 | |
| 371 | if (pos == POS_AFTER_MAIN && info[i].myanmar_category() == OT_VBlw) |
| 372 | { |
| 373 | pos = POS_BELOW_C; |
| 374 | info[i].myanmar_position() = pos; |
| 375 | continue; |
| 376 | } |
| 377 | |
| 378 | if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_A) |
| 379 | { |
| 380 | info[i].myanmar_position() = POS_BEFORE_SUB; |
| 381 | continue; |
| 382 | } |
| 383 | if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_VBlw) |
| 384 | { |
| 385 | info[i].myanmar_position() = pos; |
| 386 | continue; |
| 387 | } |
| 388 | if (pos == POS_BELOW_C && info[i].myanmar_category() != OT_A) |
| 389 | { |
| 390 | pos = POS_AFTER_SUB; |
| 391 | info[i].myanmar_position() = pos; |
| 392 | continue; |
| 393 | } |
| 394 | info[i].myanmar_position() = pos; |
| 395 | } |
| 396 | } |
| 397 | |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 398 | /* Sit tight, rock 'n roll! */ |
Behdad Esfahbod | c403d63 | 2015-09-01 16:15:25 +0100 | [diff] [blame] | 399 | buffer->sort (start, end, compare_myanmar_order); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static void |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 403 | initial_reordering_syllable (const hb_ot_shape_plan_t *plan, |
| 404 | hb_face_t *face, |
| 405 | hb_buffer_t *buffer, |
| 406 | unsigned int start, unsigned int end) |
| 407 | { |
| 408 | syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F); |
| 409 | switch (syllable_type) { |
Behdad Esfahbod | 56f71ff | 2015-07-22 11:58:11 +0100 | [diff] [blame] | 410 | |
| 411 | case broken_cluster: /* We already inserted dotted-circles, so just call the consonant_syllable. */ |
| 412 | case consonant_syllable: |
| 413 | initial_reordering_consonant_syllable (buffer, start, end); |
| 414 | break; |
| 415 | |
| 416 | case punctuation_cluster: |
| 417 | case non_myanmar_cluster: |
| 418 | break; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
| 422 | static inline void |
| 423 | insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED, |
| 424 | hb_font_t *font, |
| 425 | hb_buffer_t *buffer) |
| 426 | { |
| 427 | /* Note: This loop is extra overhead, but should not be measurable. */ |
| 428 | bool has_broken_syllables = false; |
| 429 | unsigned int count = buffer->len; |
Behdad Esfahbod | 7cd33f2 | 2014-07-17 14:22:11 -0400 | [diff] [blame] | 430 | hb_glyph_info_t *info = buffer->info; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 431 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | 7cd33f2 | 2014-07-17 14:22:11 -0400 | [diff] [blame] | 432 | if ((info[i].syllable() & 0x0F) == broken_cluster) |
| 433 | { |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 434 | has_broken_syllables = true; |
| 435 | break; |
| 436 | } |
| 437 | if (likely (!has_broken_syllables)) |
| 438 | return; |
| 439 | |
| 440 | |
| 441 | hb_codepoint_t dottedcircle_glyph; |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 442 | if (!font->get_glyph (0x25CCu, 0, &dottedcircle_glyph)) |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 443 | return; |
| 444 | |
| 445 | hb_glyph_info_t dottedcircle = {0}; |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 446 | dottedcircle.codepoint = 0x25CCu; |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 447 | set_myanmar_properties (dottedcircle); |
| 448 | dottedcircle.codepoint = dottedcircle_glyph; |
| 449 | |
| 450 | buffer->clear_output (); |
| 451 | |
| 452 | buffer->idx = 0; |
| 453 | unsigned int last_syllable = 0; |
Behdad Esfahbod | b894a85 | 2016-02-02 16:39:19 +0800 | [diff] [blame^] | 454 | while (buffer->idx < buffer->len && !buffer->in_error) |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 455 | { |
| 456 | unsigned int syllable = buffer->cur().syllable(); |
| 457 | syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F); |
| 458 | if (unlikely (last_syllable != syllable && syllable_type == broken_cluster)) |
| 459 | { |
| 460 | last_syllable = syllable; |
| 461 | |
Behdad Esfahbod | 6f932bc | 2015-10-21 11:16:49 -0200 | [diff] [blame] | 462 | hb_glyph_info_t ginfo = dottedcircle; |
| 463 | ginfo.cluster = buffer->cur().cluster; |
| 464 | ginfo.mask = buffer->cur().mask; |
| 465 | ginfo.syllable() = buffer->cur().syllable(); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 466 | |
Behdad Esfahbod | 6f932bc | 2015-10-21 11:16:49 -0200 | [diff] [blame] | 467 | buffer->output_info (ginfo); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 468 | } |
| 469 | else |
| 470 | buffer->next_glyph (); |
| 471 | } |
| 472 | |
| 473 | buffer->swap_buffers (); |
| 474 | } |
| 475 | |
| 476 | static void |
| 477 | initial_reordering (const hb_ot_shape_plan_t *plan, |
| 478 | hb_font_t *font, |
| 479 | hb_buffer_t *buffer) |
| 480 | { |
| 481 | insert_dotted_circles (plan, font, buffer); |
| 482 | |
Behdad Esfahbod | 56f71ff | 2015-07-22 11:58:11 +0100 | [diff] [blame] | 483 | foreach_syllable (buffer, start, end) |
| 484 | initial_reordering_syllable (plan, font->face, buffer, start, end); |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | static void |
| 488 | final_reordering (const hb_ot_shape_plan_t *plan, |
| 489 | hb_font_t *font HB_UNUSED, |
| 490 | hb_buffer_t *buffer) |
| 491 | { |
| 492 | hb_glyph_info_t *info = buffer->info; |
| 493 | unsigned int count = buffer->len; |
| 494 | |
| 495 | /* Zero syllables now... */ |
| 496 | for (unsigned int i = 0; i < count; i++) |
| 497 | info[i].syllable() = 0; |
| 498 | |
| 499 | HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_category); |
| 500 | HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_position); |
| 501 | } |
| 502 | |
Behdad Esfahbod | 71b4c99 | 2013-10-28 00:20:59 +0100 | [diff] [blame] | 503 | |
Behdad Esfahbod | 6f2d9ba | 2014-07-26 19:17:44 -0400 | [diff] [blame] | 504 | /* Uniscribe seems to have a shaper for 'mymr' that is like the |
| 505 | * generic shaper, except that it zeros mark advances GDEF_LATE. */ |
| 506 | const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar_old = |
| 507 | { |
| 508 | "default", |
| 509 | NULL, /* collect_features */ |
| 510 | NULL, /* override_features */ |
| 511 | NULL, /* data_create */ |
| 512 | NULL, /* data_destroy */ |
| 513 | NULL, /* preprocess_text */ |
Behdad Esfahbod | 1368633 | 2015-11-05 13:24:15 -0800 | [diff] [blame] | 514 | NULL, /* postprocess_glyphs */ |
Behdad Esfahbod | 6f2d9ba | 2014-07-26 19:17:44 -0400 | [diff] [blame] | 515 | HB_OT_SHAPE_NORMALIZATION_MODE_DEFAULT, |
| 516 | NULL, /* decompose */ |
| 517 | NULL, /* compose */ |
| 518 | NULL, /* setup_masks */ |
| 519 | HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE, |
| 520 | true, /* fallback_position */ |
| 521 | }; |
| 522 | |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 523 | const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar = |
| 524 | { |
| 525 | "myanmar", |
| 526 | collect_features_myanmar, |
| 527 | override_features_myanmar, |
| 528 | NULL, /* data_create */ |
| 529 | NULL, /* data_destroy */ |
| 530 | NULL, /* preprocess_text */ |
Behdad Esfahbod | 1368633 | 2015-11-05 13:24:15 -0800 | [diff] [blame] | 531 | NULL, /* postprocess_glyphs */ |
Behdad Esfahbod | 3d6ca0d | 2013-12-31 16:04:35 +0800 | [diff] [blame] | 532 | HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT, |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 533 | NULL, /* decompose */ |
| 534 | NULL, /* compose */ |
| 535 | setup_masks_myanmar, |
Behdad Esfahbod | 71b4c99 | 2013-10-28 00:20:59 +0100 | [diff] [blame] | 536 | HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY, |
Behdad Esfahbod | 98628ca | 2013-02-11 13:36:23 -0500 | [diff] [blame] | 537 | false, /* fallback_position */ |
| 538 | }; |