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