Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2015 Mozilla Foundation. |
| 3 | * Copyright © 2015 Google, Inc. |
| 4 | * |
| 5 | * This is part of HarfBuzz, a text shaping library. |
| 6 | * |
| 7 | * Permission is hereby granted, without written agreement and without |
| 8 | * license or royalty fees, to use, copy, modify, and distribute this |
| 9 | * software and its documentation for any purpose, provided that the |
| 10 | * above copyright notice and the following two paragraphs appear in |
| 11 | * all copies of this software. |
| 12 | * |
| 13 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 14 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 15 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 16 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 17 | * DAMAGE. |
| 18 | * |
| 19 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 20 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 22 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 23 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 24 | * |
| 25 | * Mozilla Author(s): Jonathan Kew |
| 26 | * Google Author(s): Behdad Esfahbod |
| 27 | */ |
| 28 | |
| 29 | #include "hb-ot-shape-complex-use-private.hh" |
Behdad Esfahbod | 8ba9e68 | 2015-07-22 11:16:01 +0100 | [diff] [blame] | 30 | #include "hb-ot-shape-complex-arabic-private.hh" |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 31 | |
| 32 | /* buffer var allocations */ |
| 33 | #define use_category() complex_var_u8_0() |
| 34 | |
| 35 | |
| 36 | /* |
| 37 | * Universal Shaping Engine. |
| 38 | * https://www.microsoft.com/typography/OpenTypeDev/USE/intro.htm |
| 39 | */ |
| 40 | |
| 41 | static const hb_tag_t |
| 42 | basic_features[] = |
| 43 | { |
| 44 | /* |
| 45 | * Basic features. |
Behdad Esfahbod | 4febed6 | 2015-07-21 10:24:32 +0100 | [diff] [blame] | 46 | * These features are applied all at once, before reordering. |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 47 | */ |
Behdad Esfahbod | 4febed6 | 2015-07-21 10:24:32 +0100 | [diff] [blame] | 48 | HB_TAG('r','k','r','f'), |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 49 | HB_TAG('a','b','v','f'), |
| 50 | HB_TAG('b','l','w','f'), |
Behdad Esfahbod | 4febed6 | 2015-07-21 10:24:32 +0100 | [diff] [blame] | 51 | HB_TAG('h','a','l','f'), |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 52 | HB_TAG('p','s','t','f'), |
Behdad Esfahbod | 4febed6 | 2015-07-21 10:24:32 +0100 | [diff] [blame] | 53 | HB_TAG('v','a','t','u'), |
| 54 | HB_TAG('c','j','c','t'), |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 55 | }; |
| 56 | static const hb_tag_t |
Behdad Esfahbod | 8ba9e68 | 2015-07-22 11:16:01 +0100 | [diff] [blame] | 57 | arabic_features[] = |
| 58 | { |
| 59 | HB_TAG('i','s','o','l'), |
| 60 | HB_TAG('i','n','i','t'), |
| 61 | HB_TAG('m','e','d','i'), |
| 62 | HB_TAG('f','i','n','a'), |
| 63 | /* The spec doesn't specify these but we apply anyway, since our Arabic shaper |
| 64 | * does. These are only used in Syriac spec. */ |
| 65 | HB_TAG('m','e','d','2'), |
| 66 | HB_TAG('f','i','n','2'), |
| 67 | HB_TAG('f','i','n','3'), |
| 68 | }; |
Behdad Esfahbod | 9cd59db | 2015-07-22 13:27:06 +0100 | [diff] [blame] | 69 | /* Same order as arabic_features. Don't need Syriac stuff.*/ |
| 70 | enum joining_form_t { |
| 71 | ISOL, |
| 72 | INIT, |
| 73 | MEDI, |
| 74 | FINA, |
| 75 | _NONE |
| 76 | }; |
Behdad Esfahbod | 8ba9e68 | 2015-07-22 11:16:01 +0100 | [diff] [blame] | 77 | static const hb_tag_t |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 78 | other_features[] = |
| 79 | { |
| 80 | /* |
| 81 | * Other features. |
Behdad Esfahbod | 4febed6 | 2015-07-21 10:24:32 +0100 | [diff] [blame] | 82 | * These features are applied all at once, after reordering. |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 83 | */ |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 84 | HB_TAG('a','b','v','s'), |
| 85 | HB_TAG('b','l','w','s'), |
Behdad Esfahbod | 4febed6 | 2015-07-21 10:24:32 +0100 | [diff] [blame] | 86 | HB_TAG('h','a','l','n'), |
| 87 | HB_TAG('p','r','e','s'), |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 88 | HB_TAG('p','s','t','s'), |
| 89 | /* Positioning features, though we don't care about the types. */ |
| 90 | HB_TAG('d','i','s','t'), |
Behdad Esfahbod | 4febed6 | 2015-07-21 10:24:32 +0100 | [diff] [blame] | 91 | HB_TAG('a','b','v','m'), |
| 92 | HB_TAG('b','l','w','m'), |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | static void |
| 96 | setup_syllables (const hb_ot_shape_plan_t *plan, |
| 97 | hb_font_t *font, |
| 98 | hb_buffer_t *buffer); |
| 99 | static void |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 100 | clear_substitution_flags (const hb_ot_shape_plan_t *plan, |
| 101 | hb_font_t *font, |
| 102 | hb_buffer_t *buffer); |
| 103 | static void |
| 104 | record_rphf (const hb_ot_shape_plan_t *plan, |
| 105 | hb_font_t *font, |
| 106 | hb_buffer_t *buffer); |
| 107 | static void |
| 108 | record_pref (const hb_ot_shape_plan_t *plan, |
| 109 | hb_font_t *font, |
| 110 | hb_buffer_t *buffer); |
| 111 | static void |
Behdad Esfahbod | 4febed6 | 2015-07-21 10:24:32 +0100 | [diff] [blame] | 112 | reorder (const hb_ot_shape_plan_t *plan, |
| 113 | hb_font_t *font, |
| 114 | hb_buffer_t *buffer); |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 115 | |
| 116 | static void |
| 117 | collect_features_use (hb_ot_shape_planner_t *plan) |
| 118 | { |
| 119 | hb_ot_map_builder_t *map = &plan->map; |
| 120 | |
| 121 | /* Do this before any lookups have been applied. */ |
| 122 | map->add_gsub_pause (setup_syllables); |
| 123 | |
Behdad Esfahbod | 4febed6 | 2015-07-21 10:24:32 +0100 | [diff] [blame] | 124 | /* "Default glyph pre-processing group" */ |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 125 | map->add_global_bool_feature (HB_TAG('l','o','c','l')); |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 126 | map->add_global_bool_feature (HB_TAG('c','c','m','p')); |
Behdad Esfahbod | 4febed6 | 2015-07-21 10:24:32 +0100 | [diff] [blame] | 127 | map->add_global_bool_feature (HB_TAG('n','u','k','t')); |
| 128 | map->add_global_bool_feature (HB_TAG('a','k','h','n')); |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 129 | |
Behdad Esfahbod | 4febed6 | 2015-07-21 10:24:32 +0100 | [diff] [blame] | 130 | /* "Reordering group" */ |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 131 | map->add_gsub_pause (clear_substitution_flags); |
| 132 | map->add_feature (HB_TAG('r','p','h','f'), 1, F_MANUAL_ZWJ); |
| 133 | map->add_gsub_pause (record_rphf); |
Behdad Esfahbod | a85c4da | 2015-07-21 16:07:10 +0100 | [diff] [blame] | 134 | map->add_gsub_pause (clear_substitution_flags); |
Behdad Esfahbod | cf59c75 | 2015-07-21 14:51:45 +0100 | [diff] [blame] | 135 | map->add_feature (HB_TAG('p','r','e','f'), 1, F_GLOBAL | F_MANUAL_ZWJ); |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 136 | map->add_gsub_pause (record_pref); |
Behdad Esfahbod | 4febed6 | 2015-07-21 10:24:32 +0100 | [diff] [blame] | 137 | |
| 138 | /* "Orthographic unit shaping group" */ |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 139 | for (unsigned int i = 0; i < ARRAY_LENGTH (basic_features); i++) |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 140 | map->add_feature (basic_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ); |
Behdad Esfahbod | 4febed6 | 2015-07-21 10:24:32 +0100 | [diff] [blame] | 141 | |
| 142 | map->add_gsub_pause (reorder); |
| 143 | |
| 144 | /* "Topographical features" */ |
Behdad Esfahbod | f1c20e1 | 2015-07-27 12:16:02 +0200 | [diff] [blame] | 145 | for (unsigned int i = 0; i < ARRAY_LENGTH (arabic_features); i++) |
Behdad Esfahbod | 8ba9e68 | 2015-07-22 11:16:01 +0100 | [diff] [blame] | 146 | map->add_feature (arabic_features[i], 1, F_NONE); |
Behdad Esfahbod | 8ba9e68 | 2015-07-22 11:16:01 +0100 | [diff] [blame] | 147 | map->add_gsub_pause (NULL); |
Behdad Esfahbod | 4febed6 | 2015-07-21 10:24:32 +0100 | [diff] [blame] | 148 | |
| 149 | /* "Standard typographic presentation" and "Positional feature application" */ |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 150 | for (unsigned int i = 0; i < ARRAY_LENGTH (other_features); i++) |
| 151 | map->add_feature (other_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ); |
| 152 | } |
| 153 | |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 154 | struct use_shape_plan_t |
| 155 | { |
| 156 | ASSERT_POD (); |
| 157 | |
| 158 | hb_mask_t rphf_mask; |
Behdad Esfahbod | 8ba9e68 | 2015-07-22 11:16:01 +0100 | [diff] [blame] | 159 | |
| 160 | arabic_shape_plan_t *arabic_plan; |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 161 | }; |
| 162 | |
Behdad Esfahbod | 8ba9e68 | 2015-07-22 11:16:01 +0100 | [diff] [blame] | 163 | static bool |
| 164 | has_arabic_joining (hb_script_t script) |
| 165 | { |
| 166 | /* List of scripts that have data in arabic-table. */ |
| 167 | switch ((int) script) |
| 168 | { |
| 169 | /* Unicode-1.1 additions */ |
| 170 | case HB_SCRIPT_ARABIC: |
| 171 | |
| 172 | /* Unicode-3.0 additions */ |
| 173 | case HB_SCRIPT_MONGOLIAN: |
| 174 | case HB_SCRIPT_SYRIAC: |
| 175 | |
| 176 | /* Unicode-5.0 additions */ |
| 177 | case HB_SCRIPT_NKO: |
| 178 | case HB_SCRIPT_PHAGS_PA: |
| 179 | |
| 180 | /* Unicode-6.0 additions */ |
| 181 | case HB_SCRIPT_MANDAIC: |
| 182 | |
| 183 | /* Unicode-7.0 additions */ |
| 184 | case HB_SCRIPT_MANICHAEAN: |
| 185 | case HB_SCRIPT_PSALTER_PAHLAVI: |
| 186 | |
| 187 | return true; |
| 188 | |
| 189 | default: |
| 190 | return false; |
| 191 | } |
| 192 | } |
| 193 | |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 194 | static void * |
| 195 | data_create_use (const hb_ot_shape_plan_t *plan) |
| 196 | { |
| 197 | use_shape_plan_t *use_plan = (use_shape_plan_t *) calloc (1, sizeof (use_shape_plan_t)); |
| 198 | if (unlikely (!use_plan)) |
| 199 | return NULL; |
| 200 | |
| 201 | use_plan->rphf_mask = plan->map.get_1_mask (HB_TAG('r','p','h','f')); |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 202 | |
Behdad Esfahbod | 8ba9e68 | 2015-07-22 11:16:01 +0100 | [diff] [blame] | 203 | if (has_arabic_joining (plan->props.script)) |
| 204 | { |
| 205 | use_plan->arabic_plan = (arabic_shape_plan_t *) data_create_arabic (plan); |
| 206 | if (unlikely (!use_plan->arabic_plan)) |
| 207 | { |
| 208 | free (use_plan); |
| 209 | return NULL; |
| 210 | } |
| 211 | } |
| 212 | |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 213 | return use_plan; |
| 214 | } |
| 215 | |
| 216 | static void |
| 217 | data_destroy_use (void *data) |
| 218 | { |
Behdad Esfahbod | 8ba9e68 | 2015-07-22 11:16:01 +0100 | [diff] [blame] | 219 | use_shape_plan_t *use_plan = (use_shape_plan_t *) data; |
| 220 | |
| 221 | if (use_plan->arabic_plan) |
| 222 | data_destroy_arabic (use_plan->arabic_plan); |
| 223 | |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 224 | free (data); |
| 225 | } |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 226 | |
| 227 | enum syllable_type_t { |
| 228 | independent_cluster, |
| 229 | virama_terminated_cluster, |
| 230 | consonant_cluster, |
| 231 | vowel_cluster, |
| 232 | number_joiner_terminated_cluster, |
| 233 | numeral_cluster, |
| 234 | symbol_cluster, |
Behdad Esfahbod | 40c4a99 | 2015-07-21 17:14:54 +0100 | [diff] [blame] | 235 | broken_cluster, |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | #include "hb-ot-shape-complex-use-machine.hh" |
| 239 | |
| 240 | |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 241 | static void |
Behdad Esfahbod | 8ba9e68 | 2015-07-22 11:16:01 +0100 | [diff] [blame] | 242 | setup_masks_use (const hb_ot_shape_plan_t *plan, |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 243 | hb_buffer_t *buffer, |
| 244 | hb_font_t *font HB_UNUSED) |
| 245 | { |
Behdad Esfahbod | 8ba9e68 | 2015-07-22 11:16:01 +0100 | [diff] [blame] | 246 | const use_shape_plan_t *use_plan = (const use_shape_plan_t *) plan->data; |
| 247 | |
| 248 | /* Do this before allocating use_category(). */ |
| 249 | if (use_plan->arabic_plan) |
| 250 | { |
| 251 | setup_masks_arabic_plan (use_plan->arabic_plan, buffer, plan->props.script); |
| 252 | } |
| 253 | |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 254 | HB_BUFFER_ALLOCATE_VAR (buffer, use_category); |
| 255 | |
| 256 | /* We cannot setup masks here. We save information about characters |
| 257 | * and setup masks later on in a pause-callback. */ |
| 258 | |
| 259 | unsigned int count = buffer->len; |
| 260 | hb_glyph_info_t *info = buffer->info; |
| 261 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | b4c0829 | 2015-07-21 09:31:19 +0100 | [diff] [blame] | 262 | info[i].use_category() = hb_use_get_categories (info[i].codepoint); |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static void |
Behdad Esfahbod | 9cd59db | 2015-07-22 13:27:06 +0100 | [diff] [blame] | 266 | setup_rphf_mask (const hb_ot_shape_plan_t *plan, |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 267 | hb_buffer_t *buffer) |
| 268 | { |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 269 | const use_shape_plan_t *use_plan = (const use_shape_plan_t *) plan->data; |
Behdad Esfahbod | ac59651 | 2015-07-22 11:54:02 +0100 | [diff] [blame] | 270 | |
| 271 | hb_mask_t mask = use_plan->rphf_mask; |
| 272 | if (!mask) return; |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 273 | |
| 274 | hb_glyph_info_t *info = buffer->info; |
Behdad Esfahbod | ac59651 | 2015-07-22 11:54:02 +0100 | [diff] [blame] | 275 | |
| 276 | foreach_syllable (buffer, start, end) |
| 277 | { |
| 278 | unsigned int limit = info[start].use_category() == USE_R ? 1 : MIN (3u, end - start); |
| 279 | for (unsigned int i = start; i < start + limit; i++) |
| 280 | info[i].mask |= mask; |
| 281 | } |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 284 | static void |
Behdad Esfahbod | 9cd59db | 2015-07-22 13:27:06 +0100 | [diff] [blame] | 285 | setup_topographical_masks (const hb_ot_shape_plan_t *plan, |
| 286 | hb_buffer_t *buffer) |
| 287 | { |
Behdad Esfahbod | 862b164 | 2015-12-18 13:54:06 +0000 | [diff] [blame] | 288 | const use_shape_plan_t *use_plan = (const use_shape_plan_t *) plan->data; |
| 289 | if (use_plan->arabic_plan) |
| 290 | return; |
Behdad Esfahbod | 9cd59db | 2015-07-22 13:27:06 +0100 | [diff] [blame] | 291 | |
| 292 | ASSERT_STATIC (INIT < 4 && ISOL < 4 && MEDI < 4 && FINA < 4); |
| 293 | hb_mask_t masks[4], all_masks = 0; |
| 294 | for (unsigned int i = 0; i < 4; i++) |
| 295 | { |
| 296 | masks[i] = plan->map.get_1_mask (arabic_features[i]); |
| 297 | if (masks[i] == plan->map.get_global_mask ()) |
| 298 | masks[i] = 0; |
| 299 | all_masks |= masks[i]; |
| 300 | } |
| 301 | if (!all_masks) |
| 302 | return; |
| 303 | hb_mask_t other_masks = ~all_masks; |
| 304 | |
| 305 | unsigned int last_start = 0; |
| 306 | joining_form_t last_form = _NONE; |
| 307 | hb_glyph_info_t *info = buffer->info; |
| 308 | foreach_syllable (buffer, start, end) |
| 309 | { |
| 310 | syllable_type_t syllable_type = (syllable_type_t) (info[start].syllable() & 0x0F); |
| 311 | switch (syllable_type) |
| 312 | { |
| 313 | case independent_cluster: |
| 314 | case symbol_cluster: |
| 315 | /* These don't join. Nothing to do. */ |
| 316 | last_form = _NONE; |
| 317 | break; |
| 318 | |
| 319 | case virama_terminated_cluster: |
| 320 | case consonant_cluster: |
| 321 | case vowel_cluster: |
| 322 | case number_joiner_terminated_cluster: |
| 323 | case numeral_cluster: |
| 324 | case broken_cluster: |
| 325 | |
| 326 | bool join = last_form == FINA || last_form == ISOL; |
| 327 | |
| 328 | if (join) |
| 329 | { |
| 330 | /* Fixup previous syllable's form. */ |
| 331 | last_form = last_form == FINA ? MEDI : INIT; |
| 332 | for (unsigned int i = last_start; i < start; i++) |
| 333 | info[i].mask = (info[i].mask & other_masks) | masks[last_form]; |
| 334 | } |
| 335 | |
| 336 | /* Form for this syllable. */ |
| 337 | last_form = join ? FINA : ISOL; |
| 338 | for (unsigned int i = start; i < end; i++) |
| 339 | info[i].mask = (info[i].mask & other_masks) | masks[last_form]; |
| 340 | |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | last_start = start; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | static void |
| 349 | setup_syllables (const hb_ot_shape_plan_t *plan, |
| 350 | hb_font_t *font HB_UNUSED, |
| 351 | hb_buffer_t *buffer) |
| 352 | { |
| 353 | find_syllables (buffer); |
| 354 | setup_rphf_mask (plan, buffer); |
| 355 | setup_topographical_masks (plan, buffer); |
| 356 | } |
| 357 | |
| 358 | static void |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 359 | clear_substitution_flags (const hb_ot_shape_plan_t *plan, |
| 360 | hb_font_t *font HB_UNUSED, |
| 361 | hb_buffer_t *buffer) |
| 362 | { |
| 363 | hb_glyph_info_t *info = buffer->info; |
| 364 | unsigned int count = buffer->len; |
| 365 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | 2ab0de9 | 2015-12-17 11:59:15 +0000 | [diff] [blame] | 366 | _hb_glyph_info_clear_substituted (&info[i]); |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static void |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 370 | record_rphf (const hb_ot_shape_plan_t *plan, |
| 371 | hb_font_t *font, |
| 372 | hb_buffer_t *buffer) |
| 373 | { |
| 374 | const use_shape_plan_t *use_plan = (const use_shape_plan_t *) plan->data; |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 375 | |
Behdad Esfahbod | ac59651 | 2015-07-22 11:54:02 +0100 | [diff] [blame] | 376 | hb_mask_t mask = use_plan->rphf_mask; |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 377 | if (!mask) return; |
| 378 | hb_glyph_info_t *info = buffer->info; |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 379 | |
Behdad Esfahbod | ac59651 | 2015-07-22 11:54:02 +0100 | [diff] [blame] | 380 | foreach_syllable (buffer, start, end) |
| 381 | { |
| 382 | /* Mark a substituted repha as USE_R. */ |
| 383 | for (unsigned int i = start; i < end && (info[i].mask & mask); i++) |
| 384 | if (_hb_glyph_info_substituted (&info[i])) |
| 385 | { |
| 386 | info[i].use_category() = USE_R; |
| 387 | break; |
| 388 | } |
| 389 | } |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static void |
| 393 | record_pref (const hb_ot_shape_plan_t *plan, |
| 394 | hb_font_t *font, |
| 395 | hb_buffer_t *buffer) |
| 396 | { |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 397 | hb_glyph_info_t *info = buffer->info; |
Behdad Esfahbod | ac59651 | 2015-07-22 11:54:02 +0100 | [diff] [blame] | 398 | |
| 399 | foreach_syllable (buffer, start, end) |
| 400 | { |
| 401 | /* Mark a substituted pref as VPre, as they behave the same way. */ |
| 402 | for (unsigned int i = start; i < end; i++) |
| 403 | if (_hb_glyph_info_substituted (&info[i])) |
| 404 | { |
| 405 | info[i].use_category() = USE_VPre; |
| 406 | break; |
| 407 | } |
| 408 | } |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 409 | } |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 410 | |
Behdad Esfahbod | 2ab0de9 | 2015-12-17 11:59:15 +0000 | [diff] [blame] | 411 | static inline bool |
| 412 | is_halant (const hb_glyph_info_t &info) |
| 413 | { |
| 414 | return info.use_category() == USE_H && !_hb_glyph_info_ligated (&info); |
| 415 | } |
| 416 | |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 417 | static void |
Behdad Esfahbod | ac59651 | 2015-07-22 11:54:02 +0100 | [diff] [blame] | 418 | reorder_syllable (hb_buffer_t *buffer, unsigned int start, unsigned int end) |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 419 | { |
| 420 | syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F); |
Behdad Esfahbod | a85c4da | 2015-07-21 16:07:10 +0100 | [diff] [blame] | 421 | /* Only a few syllable types need reordering. */ |
| 422 | if (unlikely (!(FLAG_SAFE (syllable_type) & |
| 423 | (FLAG (virama_terminated_cluster) | |
| 424 | FLAG (consonant_cluster) | |
Behdad Esfahbod | 40c4a99 | 2015-07-21 17:14:54 +0100 | [diff] [blame] | 425 | FLAG (vowel_cluster) | |
| 426 | FLAG (broken_cluster) | |
| 427 | 0)))) |
Behdad Esfahbod | a85c4da | 2015-07-21 16:07:10 +0100 | [diff] [blame] | 428 | return; |
| 429 | |
| 430 | hb_glyph_info_t *info = buffer->info; |
| 431 | |
Behdad Esfahbod | a85c4da | 2015-07-21 16:07:10 +0100 | [diff] [blame] | 432 | #define BASE_FLAGS (FLAG (USE_B) | FLAG (USE_GB) | FLAG (USE_IV)) |
| 433 | |
| 434 | /* Move things forward. */ |
| 435 | if (info[start].use_category() == USE_R && end - start > 1) |
| 436 | { |
| 437 | /* Got a repha. Reorder it to after first base, before first halant. */ |
| 438 | for (unsigned int i = start + 1; i < end; i++) |
Behdad Esfahbod | 2ab0de9 | 2015-12-17 11:59:15 +0000 | [diff] [blame] | 439 | if ((FLAG_UNSAFE (info[i].use_category()) & (BASE_FLAGS)) || is_halant (info[i])) |
Behdad Esfahbod | a85c4da | 2015-07-21 16:07:10 +0100 | [diff] [blame] | 440 | { |
| 441 | /* If we hit a halant, move before it; otherwise it's a base: move to it's |
| 442 | * place, and shift things in between backward. */ |
| 443 | |
Behdad Esfahbod | 2ab0de9 | 2015-12-17 11:59:15 +0000 | [diff] [blame] | 444 | if (is_halant (info[i])) |
Behdad Esfahbod | a85c4da | 2015-07-21 16:07:10 +0100 | [diff] [blame] | 445 | i--; |
| 446 | |
Behdad Esfahbod | 5b31fe3 | 2015-09-01 16:24:34 +0100 | [diff] [blame] | 447 | buffer->merge_clusters (start, i + 1); |
Behdad Esfahbod | a85c4da | 2015-07-21 16:07:10 +0100 | [diff] [blame] | 448 | hb_glyph_info_t t = info[start]; |
| 449 | memmove (&info[start], &info[start + 1], (i - start) * sizeof (info[0])); |
| 450 | info[i] = t; |
Behdad Esfahbod | a85c4da | 2015-07-21 16:07:10 +0100 | [diff] [blame] | 451 | |
| 452 | break; |
| 453 | } |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 454 | } |
Behdad Esfahbod | a85c4da | 2015-07-21 16:07:10 +0100 | [diff] [blame] | 455 | |
Behdad Esfahbod | 7ce03eb | 2015-07-21 16:55:26 +0100 | [diff] [blame] | 456 | /* Move things back. */ |
| 457 | unsigned int j = end; |
| 458 | for (unsigned int i = start; i < end; i++) |
| 459 | { |
| 460 | uint32_t flag = FLAG_UNSAFE (info[i].use_category()); |
Behdad Esfahbod | 2ab0de9 | 2015-12-17 11:59:15 +0000 | [diff] [blame] | 461 | if ((flag & (BASE_FLAGS)) || is_halant (info[i])) |
Behdad Esfahbod | 7ce03eb | 2015-07-21 16:55:26 +0100 | [diff] [blame] | 462 | { |
Behdad Esfahbod | 2ab0de9 | 2015-12-17 11:59:15 +0000 | [diff] [blame] | 463 | /* If we hit a halant, move after it; otherwise it's a base: move to it's |
Behdad Esfahbod | 7ce03eb | 2015-07-21 16:55:26 +0100 | [diff] [blame] | 464 | * place, and shift things in between backward. */ |
Behdad Esfahbod | 2ab0de9 | 2015-12-17 11:59:15 +0000 | [diff] [blame] | 465 | if (is_halant (info[i])) |
Behdad Esfahbod | 7ce03eb | 2015-07-21 16:55:26 +0100 | [diff] [blame] | 466 | j = i + 1; |
| 467 | else |
| 468 | j = i; |
| 469 | } |
Behdad Esfahbod | a51a661 | 2015-07-21 18:24:21 +0100 | [diff] [blame] | 470 | else if (((flag) & (FLAG (USE_VPre) | FLAG (USE_VMPre))) && |
| 471 | /* Only move the first component of a MultipleSubst. */ |
| 472 | 0 == _hb_glyph_info_get_lig_comp (&info[i]) && |
| 473 | j < i) |
Behdad Esfahbod | 7ce03eb | 2015-07-21 16:55:26 +0100 | [diff] [blame] | 474 | { |
Behdad Esfahbod | 5b31fe3 | 2015-09-01 16:24:34 +0100 | [diff] [blame] | 475 | buffer->merge_clusters (j, i + 1); |
Behdad Esfahbod | 7ce03eb | 2015-07-21 16:55:26 +0100 | [diff] [blame] | 476 | hb_glyph_info_t t = info[i]; |
| 477 | memmove (&info[j + 1], &info[j], (i - j) * sizeof (info[0])); |
| 478 | info[j] = t; |
Behdad Esfahbod | 7ce03eb | 2015-07-21 16:55:26 +0100 | [diff] [blame] | 479 | } |
| 480 | } |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static inline void |
| 484 | insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED, |
| 485 | hb_font_t *font, |
| 486 | hb_buffer_t *buffer) |
| 487 | { |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 488 | /* Note: This loop is extra overhead, but should not be measurable. */ |
| 489 | bool has_broken_syllables = false; |
| 490 | unsigned int count = buffer->len; |
| 491 | hb_glyph_info_t *info = buffer->info; |
| 492 | for (unsigned int i = 0; i < count; i++) |
| 493 | if ((info[i].syllable() & 0x0F) == broken_cluster) |
| 494 | { |
| 495 | has_broken_syllables = true; |
| 496 | break; |
| 497 | } |
| 498 | if (likely (!has_broken_syllables)) |
| 499 | return; |
| 500 | |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 501 | hb_glyph_info_t dottedcircle = {0}; |
Behdad Esfahbod | 4febed6 | 2015-07-21 10:24:32 +0100 | [diff] [blame] | 502 | if (!font->get_glyph (0x25CCu, 0, &dottedcircle.codepoint)) |
| 503 | return; |
| 504 | dottedcircle.use_category() = hb_use_get_categories (0x25CC); |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 505 | |
| 506 | buffer->clear_output (); |
| 507 | |
| 508 | buffer->idx = 0; |
| 509 | unsigned int last_syllable = 0; |
Behdad Esfahbod | abadc17 | 2015-11-18 17:52:08 -0800 | [diff] [blame] | 510 | while (buffer->idx < buffer->len && !buffer->in_error) |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 511 | { |
| 512 | unsigned int syllable = buffer->cur().syllable(); |
| 513 | syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F); |
| 514 | if (unlikely (last_syllable != syllable && syllable_type == broken_cluster)) |
| 515 | { |
| 516 | last_syllable = syllable; |
| 517 | |
Behdad Esfahbod | 6f932bc | 2015-10-21 11:16:49 -0200 | [diff] [blame] | 518 | hb_glyph_info_t ginfo = dottedcircle; |
| 519 | ginfo.cluster = buffer->cur().cluster; |
| 520 | ginfo.mask = buffer->cur().mask; |
| 521 | ginfo.syllable() = buffer->cur().syllable(); |
Behdad Esfahbod | 40c4a99 | 2015-07-21 17:14:54 +0100 | [diff] [blame] | 522 | /* TODO Set glyph_props? */ |
| 523 | |
| 524 | /* Insert dottedcircle after possible Repha. */ |
Behdad Esfahbod | b894a85 | 2016-02-02 16:39:19 +0800 | [diff] [blame] | 525 | while (buffer->idx < buffer->len && !buffer->in_error && |
Behdad Esfahbod | 40c4a99 | 2015-07-21 17:14:54 +0100 | [diff] [blame] | 526 | last_syllable == buffer->cur().syllable() && |
| 527 | buffer->cur().use_category() == USE_R) |
| 528 | buffer->next_glyph (); |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 529 | |
Behdad Esfahbod | 6f932bc | 2015-10-21 11:16:49 -0200 | [diff] [blame] | 530 | buffer->output_info (ginfo); |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 531 | } |
| 532 | else |
| 533 | buffer->next_glyph (); |
| 534 | } |
| 535 | |
| 536 | buffer->swap_buffers (); |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static void |
Behdad Esfahbod | 4febed6 | 2015-07-21 10:24:32 +0100 | [diff] [blame] | 540 | reorder (const hb_ot_shape_plan_t *plan, |
| 541 | hb_font_t *font, |
| 542 | hb_buffer_t *buffer) |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 543 | { |
| 544 | insert_dotted_circles (plan, font, buffer); |
| 545 | |
| 546 | hb_glyph_info_t *info = buffer->info; |
Behdad Esfahbod | ac59651 | 2015-07-22 11:54:02 +0100 | [diff] [blame] | 547 | |
| 548 | foreach_syllable (buffer, start, end) |
| 549 | reorder_syllable (buffer, start, end); |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 550 | |
| 551 | /* Zero syllables now... */ |
Behdad Esfahbod | ac59651 | 2015-07-22 11:54:02 +0100 | [diff] [blame] | 552 | unsigned int count = buffer->len; |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 553 | for (unsigned int i = 0; i < count; i++) |
| 554 | info[i].syllable() = 0; |
| 555 | |
| 556 | HB_BUFFER_DEALLOCATE_VAR (buffer, use_category); |
| 557 | } |
| 558 | |
Behdad Esfahbod | a08a278 | 2015-07-21 18:09:40 +0100 | [diff] [blame] | 559 | static bool |
| 560 | compose_use (const hb_ot_shape_normalize_context_t *c, |
| 561 | hb_codepoint_t a, |
| 562 | hb_codepoint_t b, |
| 563 | hb_codepoint_t *ab) |
| 564 | { |
| 565 | /* Avoid recomposing split matras. */ |
| 566 | if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (c->unicode->general_category (a))) |
| 567 | return false; |
| 568 | |
ThePhD | f798b8e | 2015-11-21 16:57:26 -0500 | [diff] [blame] | 569 | return (bool)c->unicode->compose (a, b, ab); |
Behdad Esfahbod | a08a278 | 2015-07-21 18:09:40 +0100 | [diff] [blame] | 570 | } |
| 571 | |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 572 | |
| 573 | const hb_ot_complex_shaper_t _hb_ot_complex_shaper_use = |
| 574 | { |
| 575 | "use", |
| 576 | collect_features_use, |
Behdad Esfahbod | 4febed6 | 2015-07-21 10:24:32 +0100 | [diff] [blame] | 577 | NULL, /* override_features */ |
Behdad Esfahbod | 595936e | 2015-07-21 14:15:35 +0100 | [diff] [blame] | 578 | data_create_use, |
| 579 | data_destroy_use, |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 580 | NULL, /* preprocess_text */ |
Behdad Esfahbod | 1368633 | 2015-11-05 13:24:15 -0800 | [diff] [blame] | 581 | NULL, /* postprocess_glyphs */ |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 582 | HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT, |
| 583 | NULL, /* decompose */ |
Behdad Esfahbod | a08a278 | 2015-07-21 18:09:40 +0100 | [diff] [blame] | 584 | compose_use, |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 585 | setup_masks_use, |
Behdad Esfahbod | da41e48 | 2016-02-16 17:16:33 +0700 | [diff] [blame^] | 586 | HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY, |
Behdad Esfahbod | a966395 | 2015-07-20 14:24:55 +0100 | [diff] [blame] | 587 | false, /* fallback_position */ |
| 588 | }; |