Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011,2012 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 | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 27 | #include "hb-ot-shape-complex-khmer.hh" |
| 28 | #include "hb-ot-layout.hh" |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 29 | |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 30 | |
| 31 | /* |
Behdad Esfahbod | 0d3a274 | 2018-01-05 17:01:37 +0000 | [diff] [blame] | 32 | * Khmer shaper. |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 33 | */ |
| 34 | |
Behdad Esfahbod | 1676f60 | 2018-09-24 17:55:03 -0400 | [diff] [blame] | 35 | static const hb_ot_map_feature_t |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 36 | khmer_features[] = |
| 37 | { |
| 38 | /* |
| 39 | * Basic features. |
Behdad Esfahbod | 1a96cc8 | 2018-07-31 11:45:32 -0700 | [diff] [blame] | 40 | * These features are applied in order, one at a time, after reordering. |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 41 | */ |
Behdad Esfahbod | b1e07e1 | 2018-09-30 05:43:22 -0400 | [diff] [blame] | 42 | {HB_TAG('p','r','e','f'), F_MANUAL_JOINERS}, |
| 43 | {HB_TAG('b','l','w','f'), F_MANUAL_JOINERS}, |
| 44 | {HB_TAG('a','b','v','f'), F_MANUAL_JOINERS}, |
| 45 | {HB_TAG('p','s','t','f'), F_MANUAL_JOINERS}, |
| 46 | {HB_TAG('c','f','a','r'), F_MANUAL_JOINERS}, |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 47 | /* |
| 48 | * Other features. |
Behdad Esfahbod | 1a96cc8 | 2018-07-31 11:45:32 -0700 | [diff] [blame] | 49 | * These features are applied all at once. |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 50 | */ |
Behdad Esfahbod | b1e07e1 | 2018-09-30 05:43:22 -0400 | [diff] [blame] | 51 | {HB_TAG('p','r','e','s'), F_GLOBAL_MANUAL_JOINERS}, |
| 52 | {HB_TAG('a','b','v','s'), F_GLOBAL_MANUAL_JOINERS}, |
| 53 | {HB_TAG('b','l','w','s'), F_GLOBAL_MANUAL_JOINERS}, |
| 54 | {HB_TAG('p','s','t','s'), F_GLOBAL_MANUAL_JOINERS}, |
Behdad Esfahbod | 3583fb0 | 2018-09-23 22:33:38 -0400 | [diff] [blame] | 55 | /* |
| 56 | * Positioning features. |
| 57 | * We don't care about the types. |
| 58 | */ |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 59 | {HB_TAG('d','i','s','t'), F_GLOBAL}, |
| 60 | {HB_TAG('a','b','v','m'), F_GLOBAL}, |
| 61 | {HB_TAG('b','l','w','m'), F_GLOBAL}, |
| 62 | }; |
| 63 | |
| 64 | /* |
| 65 | * Must be in the same order as the khmer_features array. |
| 66 | */ |
| 67 | enum { |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 68 | PREF, |
| 69 | BLWF, |
| 70 | ABVF, |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 71 | PSTF, |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 72 | CFAR, |
| 73 | |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 74 | _PRES, |
| 75 | _ABVS, |
| 76 | _BLWS, |
| 77 | _PSTS, |
Behdad Esfahbod | 3583fb0 | 2018-09-23 22:33:38 -0400 | [diff] [blame] | 78 | |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 79 | _DIST, |
| 80 | _ABVM, |
| 81 | _BLWM, |
| 82 | |
Behdad Esfahbod | bfad6ab | 2018-01-05 16:13:09 +0000 | [diff] [blame] | 83 | KHMER_NUM_FEATURES, |
Behdad Esfahbod | 3583fb0 | 2018-09-23 22:33:38 -0400 | [diff] [blame] | 84 | KHMER_BASIC_FEATURES = _PRES, /* Don't forget to update this! */ |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | static void |
| 88 | setup_syllables (const hb_ot_shape_plan_t *plan, |
| 89 | hb_font_t *font, |
| 90 | hb_buffer_t *buffer); |
| 91 | static void |
Behdad Esfahbod | 1a96cc8 | 2018-07-31 11:45:32 -0700 | [diff] [blame] | 92 | reorder (const hb_ot_shape_plan_t *plan, |
| 93 | hb_font_t *font, |
| 94 | hb_buffer_t *buffer); |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 95 | static void |
| 96 | clear_syllables (const hb_ot_shape_plan_t *plan, |
| 97 | hb_font_t *font, |
| 98 | hb_buffer_t *buffer); |
| 99 | |
| 100 | static void |
| 101 | collect_features_khmer (hb_ot_shape_planner_t *plan) |
| 102 | { |
| 103 | hb_ot_map_builder_t *map = &plan->map; |
| 104 | |
| 105 | /* Do this before any lookups have been applied. */ |
| 106 | map->add_gsub_pause (setup_syllables); |
Behdad Esfahbod | 7c658ea | 2018-07-31 12:49:02 -0700 | [diff] [blame] | 107 | map->add_gsub_pause (reorder); |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 108 | |
Behdad Esfahbod | 5772edc | 2018-07-31 14:10:31 -0700 | [diff] [blame] | 109 | /* Testing suggests that Uniscribe does NOT pause between basic |
Behdad Esfahbod | 8eef196 | 2018-07-31 13:35:10 -0700 | [diff] [blame] | 110 | * features. Test with KhmerUI.ttf and the following three |
| 111 | * sequences: |
Behdad Esfahbod | 7c658ea | 2018-07-31 12:49:02 -0700 | [diff] [blame] | 112 | * |
| 113 | * U+1789,U+17BC |
| 114 | * U+1789,U+17D2,U+1789 |
| 115 | * U+1789,U+17D2,U+1789,U+17BC |
| 116 | * |
| 117 | * https://github.com/harfbuzz/harfbuzz/issues/974 |
| 118 | */ |
Behdad Esfahbod | f048ead | 2018-09-24 18:01:53 -0400 | [diff] [blame] | 119 | map->enable_feature (HB_TAG('l','o','c','l')); |
| 120 | map->enable_feature (HB_TAG('c','c','m','p')); |
Behdad Esfahbod | 8eef196 | 2018-07-31 13:35:10 -0700 | [diff] [blame] | 121 | |
| 122 | unsigned int i = 0; |
Behdad Esfahbod | 3583fb0 | 2018-09-23 22:33:38 -0400 | [diff] [blame] | 123 | for (; i < KHMER_BASIC_FEATURES; i++) |
Behdad Esfahbod | 1676f60 | 2018-09-24 17:55:03 -0400 | [diff] [blame] | 124 | map->add_feature (khmer_features[i]); |
Behdad Esfahbod | 8eef196 | 2018-07-31 13:35:10 -0700 | [diff] [blame] | 125 | |
Behdad Esfahbod | 6ddd669 | 2018-07-31 13:38:07 -0700 | [diff] [blame] | 126 | map->add_gsub_pause (clear_syllables); |
Behdad Esfahbod | 7c658ea | 2018-07-31 12:49:02 -0700 | [diff] [blame] | 127 | |
Behdad Esfahbod | 3583fb0 | 2018-09-23 22:33:38 -0400 | [diff] [blame] | 128 | for (; i < KHMER_NUM_FEATURES; i++) |
Behdad Esfahbod | 1676f60 | 2018-09-24 17:55:03 -0400 | [diff] [blame] | 129 | map->add_feature (khmer_features[i]); |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 130 | |
Behdad Esfahbod | f048ead | 2018-09-24 18:01:53 -0400 | [diff] [blame] | 131 | map->enable_feature (HB_TAG('c','a','l','t')); |
| 132 | map->enable_feature (HB_TAG('c','l','i','g')); |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 133 | |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static void |
| 137 | override_features_khmer (hb_ot_shape_planner_t *plan) |
| 138 | { |
| 139 | /* Uniscribe does not apply 'kern' in Khmer. */ |
| 140 | if (hb_options ().uniscribe_bug_compatible) |
| 141 | { |
Behdad Esfahbod | 1676f60 | 2018-09-24 17:55:03 -0400 | [diff] [blame] | 142 | plan->map.disable_feature (HB_TAG('k','e','r','n')); |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Behdad Esfahbod | 1676f60 | 2018-09-24 17:55:03 -0400 | [diff] [blame] | 145 | plan->map.disable_feature (HB_TAG('l','i','g','a')); |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
| 149 | struct would_substitute_feature_t |
| 150 | { |
| 151 | inline void init (const hb_ot_map_t *map, hb_tag_t feature_tag, bool zero_context_) |
| 152 | { |
| 153 | zero_context = zero_context_; |
| 154 | map->get_stage_lookups (0/*GSUB*/, |
| 155 | map->get_feature_stage (0/*GSUB*/, feature_tag), |
| 156 | &lookups, &count); |
| 157 | } |
| 158 | |
| 159 | inline bool would_substitute (const hb_codepoint_t *glyphs, |
| 160 | unsigned int glyphs_count, |
| 161 | hb_face_t *face) const |
| 162 | { |
| 163 | for (unsigned int i = 0; i < count; i++) |
| 164 | if (hb_ot_layout_lookup_would_substitute_fast (face, lookups[i].index, glyphs, glyphs_count, zero_context)) |
| 165 | return true; |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | private: |
| 170 | const hb_ot_map_t::lookup_map_t *lookups; |
| 171 | unsigned int count; |
| 172 | bool zero_context; |
| 173 | }; |
| 174 | |
| 175 | struct khmer_shape_plan_t |
| 176 | { |
| 177 | ASSERT_POD (); |
| 178 | |
| 179 | inline bool get_virama_glyph (hb_font_t *font, hb_codepoint_t *pglyph) const |
| 180 | { |
| 181 | hb_codepoint_t glyph = virama_glyph; |
| 182 | if (unlikely (virama_glyph == (hb_codepoint_t) -1)) |
| 183 | { |
Behdad Esfahbod | 9b135d9 | 2018-01-05 15:43:11 +0000 | [diff] [blame] | 184 | if (!font->get_nominal_glyph (0x17D2u, &glyph)) |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 185 | glyph = 0; |
| 186 | /* Technically speaking, the spec says we should apply 'locl' to virama too. |
| 187 | * Maybe one day... */ |
| 188 | |
| 189 | /* Our get_nominal_glyph() function needs a font, so we can't get the virama glyph |
| 190 | * during shape planning... Instead, overwrite it here. It's safe. Don't worry! */ |
| 191 | virama_glyph = glyph; |
| 192 | } |
| 193 | |
| 194 | *pglyph = glyph; |
| 195 | return glyph != 0; |
| 196 | } |
| 197 | |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 198 | mutable hb_codepoint_t virama_glyph; |
| 199 | |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 200 | would_substitute_feature_t pref; |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 201 | |
Behdad Esfahbod | bfad6ab | 2018-01-05 16:13:09 +0000 | [diff] [blame] | 202 | hb_mask_t mask_array[KHMER_NUM_FEATURES]; |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | static void * |
| 206 | data_create_khmer (const hb_ot_shape_plan_t *plan) |
| 207 | { |
| 208 | khmer_shape_plan_t *khmer_plan = (khmer_shape_plan_t *) calloc (1, sizeof (khmer_shape_plan_t)); |
| 209 | if (unlikely (!khmer_plan)) |
| 210 | return nullptr; |
| 211 | |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 212 | khmer_plan->virama_glyph = (hb_codepoint_t) -1; |
| 213 | |
Behdad Esfahbod | 7eb3ac2 | 2018-01-05 15:41:31 +0000 | [diff] [blame] | 214 | khmer_plan->pref.init (&plan->map, HB_TAG('p','r','e','f'), true); |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 215 | |
| 216 | for (unsigned int i = 0; i < ARRAY_LENGTH (khmer_plan->mask_array); i++) |
| 217 | khmer_plan->mask_array[i] = (khmer_features[i].flags & F_GLOBAL) ? |
| 218 | 0 : plan->map.get_1_mask (khmer_features[i].tag); |
| 219 | |
| 220 | return khmer_plan; |
| 221 | } |
| 222 | |
| 223 | static void |
| 224 | data_destroy_khmer (void *data) |
| 225 | { |
| 226 | free (data); |
| 227 | } |
| 228 | |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 229 | |
| 230 | enum syllable_type_t { |
| 231 | consonant_syllable, |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 232 | broken_cluster, |
| 233 | non_khmer_cluster, |
| 234 | }; |
| 235 | |
| 236 | #include "hb-ot-shape-complex-khmer-machine.hh" |
| 237 | |
| 238 | static void |
| 239 | setup_masks_khmer (const hb_ot_shape_plan_t *plan HB_UNUSED, |
| 240 | hb_buffer_t *buffer, |
| 241 | hb_font_t *font HB_UNUSED) |
| 242 | { |
| 243 | HB_BUFFER_ALLOCATE_VAR (buffer, khmer_category); |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 244 | |
| 245 | /* We cannot setup masks here. We save information about characters |
| 246 | * and setup masks later on in a pause-callback. */ |
| 247 | |
| 248 | unsigned int count = buffer->len; |
| 249 | hb_glyph_info_t *info = buffer->info; |
| 250 | for (unsigned int i = 0; i < count; i++) |
| 251 | set_khmer_properties (info[i]); |
| 252 | } |
| 253 | |
| 254 | static void |
| 255 | setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED, |
| 256 | hb_font_t *font HB_UNUSED, |
| 257 | hb_buffer_t *buffer) |
| 258 | { |
| 259 | find_syllables (buffer); |
| 260 | foreach_syllable (buffer, start, end) |
| 261 | buffer->unsafe_to_break (start, end); |
| 262 | } |
| 263 | |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 264 | |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 265 | /* Rules from: |
Ebrahim Byagowi | f24b0b9 | 2018-04-12 13:40:45 +0430 | [diff] [blame] | 266 | * https://docs.microsoft.com/en-us/typography/script-development/devanagari */ |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 267 | |
| 268 | static void |
Behdad Esfahbod | 1a96cc8 | 2018-07-31 11:45:32 -0700 | [diff] [blame] | 269 | reorder_consonant_syllable (const hb_ot_shape_plan_t *plan, |
| 270 | hb_face_t *face, |
| 271 | hb_buffer_t *buffer, |
| 272 | unsigned int start, unsigned int end) |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 273 | { |
| 274 | const khmer_shape_plan_t *khmer_plan = (const khmer_shape_plan_t *) plan->data; |
| 275 | hb_glyph_info_t *info = buffer->info; |
| 276 | |
Behdad Esfahbod | 1a96cc8 | 2018-07-31 11:45:32 -0700 | [diff] [blame] | 277 | /* Setup masks. */ |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 278 | { |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 279 | /* Post-base */ |
Behdad Esfahbod | 1a96cc8 | 2018-07-31 11:45:32 -0700 | [diff] [blame] | 280 | hb_mask_t mask = khmer_plan->mask_array[BLWF] | khmer_plan->mask_array[ABVF] | khmer_plan->mask_array[PSTF]; |
| 281 | for (unsigned int i = start + 1; i < end; i++) |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 282 | info[i].mask |= mask; |
| 283 | } |
| 284 | |
Behdad Esfahbod | 1a96cc8 | 2018-07-31 11:45:32 -0700 | [diff] [blame] | 285 | unsigned int num_coengs = 0; |
| 286 | for (unsigned int i = start + 1; i < end; i++) |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 287 | { |
Behdad Esfahbod | 1a96cc8 | 2018-07-31 11:45:32 -0700 | [diff] [blame] | 288 | /* """ |
| 289 | * When a COENG + (Cons | IndV) combination are found (and subscript count |
| 290 | * is less than two) the character combination is handled according to the |
| 291 | * subscript type of the character following the COENG. |
| 292 | * |
| 293 | * ... |
| 294 | * |
| 295 | * Subscript Type 2 - The COENG + RO characters are reordered to immediately |
| 296 | * before the base glyph. Then the COENG + RO characters are assigned to have |
| 297 | * the 'pref' OpenType feature applied to them. |
| 298 | * """ |
| 299 | */ |
| 300 | if (info[i].khmer_category() == OT_Coeng && num_coengs <= 2 && i + 1 < end) |
| 301 | { |
| 302 | num_coengs++; |
| 303 | |
| 304 | if (info[i + 1].khmer_category() == OT_Ra) |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 305 | { |
Behdad Esfahbod | 1a96cc8 | 2018-07-31 11:45:32 -0700 | [diff] [blame] | 306 | for (unsigned int j = 0; j < 2; j++) |
| 307 | info[i + j].mask |= khmer_plan->mask_array[PREF]; |
| 308 | |
| 309 | /* Move the Coeng,Ro sequence to the start. */ |
| 310 | buffer->merge_clusters (start, i + 2); |
| 311 | hb_glyph_info_t t0 = info[i]; |
| 312 | hb_glyph_info_t t1 = info[i + 1]; |
| 313 | memmove (&info[start + 2], &info[start], (i - start) * sizeof (info[0])); |
| 314 | info[start] = t0; |
| 315 | info[start + 1] = t1; |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 316 | |
| 317 | /* Mark the subsequent stuff with 'cfar'. Used in Khmer. |
| 318 | * Read the feature spec. |
| 319 | * This allows distinguishing the following cases with MS Khmer fonts: |
| 320 | * U+1784,U+17D2,U+179A,U+17D2,U+1782 |
| 321 | * U+1784,U+17D2,U+1782,U+17D2,U+179A |
| 322 | */ |
| 323 | if (khmer_plan->mask_array[CFAR]) |
Behdad Esfahbod | 1a96cc8 | 2018-07-31 11:45:32 -0700 | [diff] [blame] | 324 | for (unsigned int j = i + 2; j < end; j++) |
| 325 | info[j].mask |= khmer_plan->mask_array[CFAR]; |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 326 | |
Behdad Esfahbod | 1a96cc8 | 2018-07-31 11:45:32 -0700 | [diff] [blame] | 327 | num_coengs = 2; /* Done. */ |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
Behdad Esfahbod | 1a96cc8 | 2018-07-31 11:45:32 -0700 | [diff] [blame] | 330 | |
| 331 | /* Reorder left matra piece. */ |
Behdad Esfahbod | 5143654 | 2018-10-01 19:09:58 +0200 | [diff] [blame] | 332 | else if (info[i].khmer_category() == OT_VPre) |
Behdad Esfahbod | 1a96cc8 | 2018-07-31 11:45:32 -0700 | [diff] [blame] | 333 | { |
| 334 | /* Move to the start. */ |
| 335 | buffer->merge_clusters (start, i + 1); |
| 336 | hb_glyph_info_t t = info[i]; |
| 337 | memmove (&info[start + 1], &info[start], (i - start) * sizeof (info[0])); |
| 338 | info[start] = t; |
| 339 | } |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 340 | } |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static void |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 344 | initial_reordering_syllable (const hb_ot_shape_plan_t *plan, |
| 345 | hb_face_t *face, |
| 346 | hb_buffer_t *buffer, |
| 347 | unsigned int start, unsigned int end) |
| 348 | { |
| 349 | syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F); |
| 350 | switch (syllable_type) |
| 351 | { |
Behdad Esfahbod | e6618f3 | 2018-01-05 17:21:55 +0000 | [diff] [blame] | 352 | case broken_cluster: /* We already inserted dotted-circles, so just call the consonant_syllable. */ |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 353 | case consonant_syllable: |
Behdad Esfahbod | 1a96cc8 | 2018-07-31 11:45:32 -0700 | [diff] [blame] | 354 | reorder_consonant_syllable (plan, face, buffer, start, end); |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 355 | break; |
| 356 | |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 357 | case non_khmer_cluster: |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | static inline void |
| 363 | insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED, |
| 364 | hb_font_t *font, |
| 365 | hb_buffer_t *buffer) |
| 366 | { |
| 367 | /* Note: This loop is extra overhead, but should not be measurable. */ |
| 368 | bool has_broken_syllables = false; |
| 369 | unsigned int count = buffer->len; |
| 370 | hb_glyph_info_t *info = buffer->info; |
| 371 | for (unsigned int i = 0; i < count; i++) |
| 372 | if ((info[i].syllable() & 0x0F) == broken_cluster) |
| 373 | { |
| 374 | has_broken_syllables = true; |
| 375 | break; |
| 376 | } |
| 377 | if (likely (!has_broken_syllables)) |
| 378 | return; |
| 379 | |
| 380 | |
| 381 | hb_codepoint_t dottedcircle_glyph; |
| 382 | if (!font->get_nominal_glyph (0x25CCu, &dottedcircle_glyph)) |
| 383 | return; |
| 384 | |
| 385 | hb_glyph_info_t dottedcircle = {0}; |
| 386 | dottedcircle.codepoint = 0x25CCu; |
| 387 | set_khmer_properties (dottedcircle); |
| 388 | dottedcircle.codepoint = dottedcircle_glyph; |
| 389 | |
| 390 | buffer->clear_output (); |
| 391 | |
| 392 | buffer->idx = 0; |
| 393 | unsigned int last_syllable = 0; |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 394 | while (buffer->idx < buffer->len && buffer->successful) |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 395 | { |
| 396 | unsigned int syllable = buffer->cur().syllable(); |
| 397 | syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F); |
| 398 | if (unlikely (last_syllable != syllable && syllable_type == broken_cluster)) |
| 399 | { |
| 400 | last_syllable = syllable; |
| 401 | |
| 402 | hb_glyph_info_t ginfo = dottedcircle; |
| 403 | ginfo.cluster = buffer->cur().cluster; |
| 404 | ginfo.mask = buffer->cur().mask; |
| 405 | ginfo.syllable() = buffer->cur().syllable(); |
| 406 | /* TODO Set glyph_props? */ |
| 407 | |
| 408 | /* Insert dottedcircle after possible Repha. */ |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 409 | while (buffer->idx < buffer->len && buffer->successful && |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 410 | last_syllable == buffer->cur().syllable() && |
| 411 | buffer->cur().khmer_category() == OT_Repha) |
| 412 | buffer->next_glyph (); |
| 413 | |
| 414 | buffer->output_info (ginfo); |
| 415 | } |
| 416 | else |
| 417 | buffer->next_glyph (); |
| 418 | } |
| 419 | |
| 420 | buffer->swap_buffers (); |
| 421 | } |
| 422 | |
| 423 | static void |
Behdad Esfahbod | 1a96cc8 | 2018-07-31 11:45:32 -0700 | [diff] [blame] | 424 | reorder (const hb_ot_shape_plan_t *plan, |
| 425 | hb_font_t *font, |
| 426 | hb_buffer_t *buffer) |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 427 | { |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 428 | insert_dotted_circles (plan, font, buffer); |
| 429 | |
| 430 | foreach_syllable (buffer, start, end) |
| 431 | initial_reordering_syllable (plan, font->face, buffer, start, end); |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 432 | |
| 433 | HB_BUFFER_DEALLOCATE_VAR (buffer, khmer_category); |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 436 | static void |
| 437 | clear_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED, |
| 438 | hb_font_t *font HB_UNUSED, |
| 439 | hb_buffer_t *buffer) |
| 440 | { |
Behdad Esfahbod | 1a96cc8 | 2018-07-31 11:45:32 -0700 | [diff] [blame] | 441 | /* TODO: In USE, we clear syllables right after reorder. Figure out |
| 442 | * what Uniscribe does. */ |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 443 | hb_glyph_info_t *info = buffer->info; |
| 444 | unsigned int count = buffer->len; |
| 445 | for (unsigned int i = 0; i < count; i++) |
| 446 | info[i].syllable() = 0; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | static bool |
| 451 | decompose_khmer (const hb_ot_shape_normalize_context_t *c, |
| 452 | hb_codepoint_t ab, |
| 453 | hb_codepoint_t *a, |
| 454 | hb_codepoint_t *b) |
| 455 | { |
| 456 | switch (ab) |
| 457 | { |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 458 | /* |
| 459 | * Decompose split matras that don't have Unicode decompositions. |
| 460 | */ |
| 461 | |
| 462 | /* Khmer */ |
| 463 | case 0x17BEu : *a = 0x17C1u; *b= 0x17BEu; return true; |
| 464 | case 0x17BFu : *a = 0x17C1u; *b= 0x17BFu; return true; |
| 465 | case 0x17C0u : *a = 0x17C1u; *b= 0x17C0u; return true; |
| 466 | case 0x17C4u : *a = 0x17C1u; *b= 0x17C4u; return true; |
| 467 | case 0x17C5u : *a = 0x17C1u; *b= 0x17C5u; return true; |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | return (bool) c->unicode->decompose (ab, a, b); |
| 471 | } |
| 472 | |
| 473 | static bool |
| 474 | compose_khmer (const hb_ot_shape_normalize_context_t *c, |
| 475 | hb_codepoint_t a, |
| 476 | hb_codepoint_t b, |
| 477 | hb_codepoint_t *ab) |
| 478 | { |
| 479 | /* Avoid recomposing split matras. */ |
| 480 | if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (c->unicode->general_category (a))) |
| 481 | return false; |
| 482 | |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 483 | return (bool) c->unicode->compose (a, b, ab); |
| 484 | } |
| 485 | |
| 486 | |
| 487 | const hb_ot_complex_shaper_t _hb_ot_complex_shaper_khmer = |
| 488 | { |
| 489 | collect_features_khmer, |
| 490 | override_features_khmer, |
| 491 | data_create_khmer, |
| 492 | data_destroy_khmer, |
| 493 | nullptr, /* preprocess_text */ |
| 494 | nullptr, /* postprocess_glyphs */ |
| 495 | HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT, |
| 496 | decompose_khmer, |
| 497 | compose_khmer, |
| 498 | setup_masks_khmer, |
Behdad Esfahbod | 48c513f | 2018-10-02 14:17:42 +0200 | [diff] [blame] | 499 | HB_TAG_NONE, /* gpos_tag */ |
Behdad Esfahbod | dcf4d95 | 2018-01-05 14:54:31 +0000 | [diff] [blame] | 500 | nullptr, /* reorder_marks */ |
| 501 | HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE, |
| 502 | false, /* fallback_position */ |
| 503 | }; |