Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright © 2011,2012,2013 Google, Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * This is part of HarfBuzz, a text shaping library. |
michael@0 | 5 | * |
michael@0 | 6 | * Permission is hereby granted, without written agreement and without |
michael@0 | 7 | * license or royalty fees, to use, copy, modify, and distribute this |
michael@0 | 8 | * software and its documentation for any purpose, provided that the |
michael@0 | 9 | * above copyright notice and the following two paragraphs appear in |
michael@0 | 10 | * all copies of this software. |
michael@0 | 11 | * |
michael@0 | 12 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
michael@0 | 13 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
michael@0 | 14 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
michael@0 | 15 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
michael@0 | 16 | * DAMAGE. |
michael@0 | 17 | * |
michael@0 | 18 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
michael@0 | 19 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
michael@0 | 20 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
michael@0 | 21 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
michael@0 | 22 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
michael@0 | 23 | * |
michael@0 | 24 | * Google Author(s): Behdad Esfahbod |
michael@0 | 25 | */ |
michael@0 | 26 | |
michael@0 | 27 | #include "hb-ot-shape-complex-indic-private.hh" |
michael@0 | 28 | |
michael@0 | 29 | /* buffer var allocations */ |
michael@0 | 30 | #define sea_category() complex_var_u8_0() /* indic_category_t */ |
michael@0 | 31 | #define sea_position() complex_var_u8_1() /* indic_position_t */ |
michael@0 | 32 | |
michael@0 | 33 | |
michael@0 | 34 | /* |
michael@0 | 35 | * South-East Asian shaper. |
michael@0 | 36 | * Loosely based on the Myanmar spec / shaper. |
michael@0 | 37 | * There is no OpenType spec for this. |
michael@0 | 38 | */ |
michael@0 | 39 | |
michael@0 | 40 | static const hb_tag_t |
michael@0 | 41 | basic_features[] = |
michael@0 | 42 | { |
michael@0 | 43 | /* |
michael@0 | 44 | * Basic features. |
michael@0 | 45 | * These features are applied in order, one at a time, after initial_reordering. |
michael@0 | 46 | */ |
michael@0 | 47 | HB_TAG('p','r','e','f'), |
michael@0 | 48 | HB_TAG('a','b','v','f'), |
michael@0 | 49 | HB_TAG('b','l','w','f'), |
michael@0 | 50 | HB_TAG('p','s','t','f'), |
michael@0 | 51 | }; |
michael@0 | 52 | static const hb_tag_t |
michael@0 | 53 | other_features[] = |
michael@0 | 54 | { |
michael@0 | 55 | /* |
michael@0 | 56 | * Other features. |
michael@0 | 57 | * These features are applied all at once, after final_reordering. |
michael@0 | 58 | */ |
michael@0 | 59 | HB_TAG('p','r','e','s'), |
michael@0 | 60 | HB_TAG('a','b','v','s'), |
michael@0 | 61 | HB_TAG('b','l','w','s'), |
michael@0 | 62 | HB_TAG('p','s','t','s'), |
michael@0 | 63 | /* Positioning features, though we don't care about the types. */ |
michael@0 | 64 | HB_TAG('d','i','s','t'), |
michael@0 | 65 | }; |
michael@0 | 66 | |
michael@0 | 67 | static void |
michael@0 | 68 | setup_syllables (const hb_ot_shape_plan_t *plan, |
michael@0 | 69 | hb_font_t *font, |
michael@0 | 70 | hb_buffer_t *buffer); |
michael@0 | 71 | static void |
michael@0 | 72 | initial_reordering (const hb_ot_shape_plan_t *plan, |
michael@0 | 73 | hb_font_t *font, |
michael@0 | 74 | hb_buffer_t *buffer); |
michael@0 | 75 | static void |
michael@0 | 76 | final_reordering (const hb_ot_shape_plan_t *plan, |
michael@0 | 77 | hb_font_t *font, |
michael@0 | 78 | hb_buffer_t *buffer); |
michael@0 | 79 | |
michael@0 | 80 | static void |
michael@0 | 81 | collect_features_sea (hb_ot_shape_planner_t *plan) |
michael@0 | 82 | { |
michael@0 | 83 | hb_ot_map_builder_t *map = &plan->map; |
michael@0 | 84 | |
michael@0 | 85 | /* Do this before any lookups have been applied. */ |
michael@0 | 86 | map->add_gsub_pause (setup_syllables); |
michael@0 | 87 | |
michael@0 | 88 | map->add_global_bool_feature (HB_TAG('l','o','c','l')); |
michael@0 | 89 | /* The Indic specs do not require ccmp, but we apply it here since if |
michael@0 | 90 | * there is a use of it, it's typically at the beginning. */ |
michael@0 | 91 | map->add_global_bool_feature (HB_TAG('c','c','m','p')); |
michael@0 | 92 | |
michael@0 | 93 | map->add_gsub_pause (initial_reordering); |
michael@0 | 94 | for (unsigned int i = 0; i < ARRAY_LENGTH (basic_features); i++) |
michael@0 | 95 | { |
michael@0 | 96 | map->add_feature (basic_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ); |
michael@0 | 97 | map->add_gsub_pause (NULL); |
michael@0 | 98 | } |
michael@0 | 99 | map->add_gsub_pause (final_reordering); |
michael@0 | 100 | for (unsigned int i = 0; i < ARRAY_LENGTH (other_features); i++) |
michael@0 | 101 | map->add_feature (other_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | static void |
michael@0 | 105 | override_features_sea (hb_ot_shape_planner_t *plan) |
michael@0 | 106 | { |
michael@0 | 107 | plan->map.add_feature (HB_TAG('l','i','g','a'), 0, F_GLOBAL); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | |
michael@0 | 111 | enum syllable_type_t { |
michael@0 | 112 | consonant_syllable, |
michael@0 | 113 | broken_cluster, |
michael@0 | 114 | non_sea_cluster, |
michael@0 | 115 | }; |
michael@0 | 116 | |
michael@0 | 117 | #include "hb-ot-shape-complex-sea-machine.hh" |
michael@0 | 118 | |
michael@0 | 119 | |
michael@0 | 120 | /* Note: This enum is duplicated in the -machine.rl source file. |
michael@0 | 121 | * Not sure how to avoid duplication. */ |
michael@0 | 122 | enum sea_category_t { |
michael@0 | 123 | // OT_C = 1, |
michael@0 | 124 | OT_GB = 12, /* Generic Base XXX DOTTED CIRCLE only for now */ |
michael@0 | 125 | // OT_H = 4, /* Halant */ |
michael@0 | 126 | OT_IV = 2, /* Independent Vowel */ |
michael@0 | 127 | OT_MR = 22, /* Medial Ra */ |
michael@0 | 128 | // OT_CM = 17, /* Consonant Medial */ |
michael@0 | 129 | OT_VAbv = 26, |
michael@0 | 130 | OT_VBlw = 27, |
michael@0 | 131 | OT_VPre = 28, |
michael@0 | 132 | OT_VPst = 29, |
michael@0 | 133 | OT_T = 3, /* Tone Marks */ |
michael@0 | 134 | // OT_A = 10, /* Anusvara */ |
michael@0 | 135 | }; |
michael@0 | 136 | |
michael@0 | 137 | static inline void |
michael@0 | 138 | set_sea_properties (hb_glyph_info_t &info) |
michael@0 | 139 | { |
michael@0 | 140 | hb_codepoint_t u = info.codepoint; |
michael@0 | 141 | unsigned int type = hb_indic_get_categories (u); |
michael@0 | 142 | indic_category_t cat = (indic_category_t) (type & 0x7F); |
michael@0 | 143 | indic_position_t pos = (indic_position_t) (type >> 8); |
michael@0 | 144 | |
michael@0 | 145 | /* Medial Ra */ |
michael@0 | 146 | if (u == 0x1A55 || u == 0xAA34) |
michael@0 | 147 | cat = (indic_category_t) OT_MR; |
michael@0 | 148 | |
michael@0 | 149 | if (cat == OT_M) |
michael@0 | 150 | { |
michael@0 | 151 | switch ((int) pos) |
michael@0 | 152 | { |
michael@0 | 153 | case POS_PRE_C: cat = (indic_category_t) OT_VPre; break; |
michael@0 | 154 | case POS_ABOVE_C: cat = (indic_category_t) OT_VAbv; break; |
michael@0 | 155 | case POS_BELOW_C: cat = (indic_category_t) OT_VBlw; break; |
michael@0 | 156 | case POS_POST_C: cat = (indic_category_t) OT_VPst; break; |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | info.sea_category() = (sea_category_t) cat; |
michael@0 | 161 | info.sea_position() = pos; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | |
michael@0 | 165 | static void |
michael@0 | 166 | setup_masks_sea (const hb_ot_shape_plan_t *plan HB_UNUSED, |
michael@0 | 167 | hb_buffer_t *buffer, |
michael@0 | 168 | hb_font_t *font HB_UNUSED) |
michael@0 | 169 | { |
michael@0 | 170 | HB_BUFFER_ALLOCATE_VAR (buffer, sea_category); |
michael@0 | 171 | HB_BUFFER_ALLOCATE_VAR (buffer, sea_position); |
michael@0 | 172 | |
michael@0 | 173 | /* We cannot setup masks here. We save information about characters |
michael@0 | 174 | * and setup masks later on in a pause-callback. */ |
michael@0 | 175 | |
michael@0 | 176 | unsigned int count = buffer->len; |
michael@0 | 177 | for (unsigned int i = 0; i < count; i++) |
michael@0 | 178 | set_sea_properties (buffer->info[i]); |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | static void |
michael@0 | 182 | setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED, |
michael@0 | 183 | hb_font_t *font HB_UNUSED, |
michael@0 | 184 | hb_buffer_t *buffer) |
michael@0 | 185 | { |
michael@0 | 186 | find_syllables (buffer); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | static int |
michael@0 | 190 | compare_sea_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb) |
michael@0 | 191 | { |
michael@0 | 192 | int a = pa->sea_position(); |
michael@0 | 193 | int b = pb->sea_position(); |
michael@0 | 194 | |
michael@0 | 195 | return a < b ? -1 : a == b ? 0 : +1; |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | |
michael@0 | 199 | static void |
michael@0 | 200 | initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan, |
michael@0 | 201 | hb_face_t *face, |
michael@0 | 202 | hb_buffer_t *buffer, |
michael@0 | 203 | unsigned int start, unsigned int end) |
michael@0 | 204 | { |
michael@0 | 205 | hb_glyph_info_t *info = buffer->info; |
michael@0 | 206 | unsigned int base = start; |
michael@0 | 207 | |
michael@0 | 208 | /* Reorder! */ |
michael@0 | 209 | unsigned int i = start; |
michael@0 | 210 | for (; i < base; i++) |
michael@0 | 211 | info[i].sea_position() = POS_PRE_C; |
michael@0 | 212 | if (i < end) |
michael@0 | 213 | { |
michael@0 | 214 | info[i].sea_position() = POS_BASE_C; |
michael@0 | 215 | i++; |
michael@0 | 216 | } |
michael@0 | 217 | for (; i < end; i++) |
michael@0 | 218 | { |
michael@0 | 219 | if (info[i].sea_category() == OT_MR) /* Pre-base reordering */ |
michael@0 | 220 | { |
michael@0 | 221 | info[i].sea_position() = POS_PRE_C; |
michael@0 | 222 | continue; |
michael@0 | 223 | } |
michael@0 | 224 | if (info[i].sea_category() == OT_VPre) /* Left matra */ |
michael@0 | 225 | { |
michael@0 | 226 | info[i].sea_position() = POS_PRE_M; |
michael@0 | 227 | continue; |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | info[i].sea_position() = POS_AFTER_MAIN; |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | buffer->merge_clusters (start, end); |
michael@0 | 234 | /* Sit tight, rock 'n roll! */ |
michael@0 | 235 | hb_bubble_sort (info + start, end - start, compare_sea_order); |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | static void |
michael@0 | 239 | initial_reordering_broken_cluster (const hb_ot_shape_plan_t *plan, |
michael@0 | 240 | hb_face_t *face, |
michael@0 | 241 | hb_buffer_t *buffer, |
michael@0 | 242 | unsigned int start, unsigned int end) |
michael@0 | 243 | { |
michael@0 | 244 | /* We already inserted dotted-circles, so just call the consonant_syllable. */ |
michael@0 | 245 | initial_reordering_consonant_syllable (plan, face, buffer, start, end); |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | static void |
michael@0 | 249 | initial_reordering_non_sea_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED, |
michael@0 | 250 | hb_face_t *face HB_UNUSED, |
michael@0 | 251 | hb_buffer_t *buffer HB_UNUSED, |
michael@0 | 252 | unsigned int start HB_UNUSED, unsigned int end HB_UNUSED) |
michael@0 | 253 | { |
michael@0 | 254 | /* Nothing to do right now. If we ever switch to using the output |
michael@0 | 255 | * buffer in the reordering process, we'd need to next_glyph() here. */ |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | |
michael@0 | 259 | static void |
michael@0 | 260 | initial_reordering_syllable (const hb_ot_shape_plan_t *plan, |
michael@0 | 261 | hb_face_t *face, |
michael@0 | 262 | hb_buffer_t *buffer, |
michael@0 | 263 | unsigned int start, unsigned int end) |
michael@0 | 264 | { |
michael@0 | 265 | syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F); |
michael@0 | 266 | switch (syllable_type) { |
michael@0 | 267 | case consonant_syllable: initial_reordering_consonant_syllable (plan, face, buffer, start, end); return; |
michael@0 | 268 | case broken_cluster: initial_reordering_broken_cluster (plan, face, buffer, start, end); return; |
michael@0 | 269 | case non_sea_cluster: initial_reordering_non_sea_cluster (plan, face, buffer, start, end); return; |
michael@0 | 270 | } |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | static inline void |
michael@0 | 274 | insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED, |
michael@0 | 275 | hb_font_t *font, |
michael@0 | 276 | hb_buffer_t *buffer) |
michael@0 | 277 | { |
michael@0 | 278 | /* Note: This loop is extra overhead, but should not be measurable. */ |
michael@0 | 279 | bool has_broken_syllables = false; |
michael@0 | 280 | unsigned int count = buffer->len; |
michael@0 | 281 | for (unsigned int i = 0; i < count; i++) |
michael@0 | 282 | if ((buffer->info[i].syllable() & 0x0F) == broken_cluster) { |
michael@0 | 283 | has_broken_syllables = true; |
michael@0 | 284 | break; |
michael@0 | 285 | } |
michael@0 | 286 | if (likely (!has_broken_syllables)) |
michael@0 | 287 | return; |
michael@0 | 288 | |
michael@0 | 289 | |
michael@0 | 290 | hb_codepoint_t dottedcircle_glyph; |
michael@0 | 291 | if (!font->get_glyph (0x25CC, 0, &dottedcircle_glyph)) |
michael@0 | 292 | return; |
michael@0 | 293 | |
michael@0 | 294 | hb_glyph_info_t dottedcircle = {0}; |
michael@0 | 295 | dottedcircle.codepoint = 0x25CC; |
michael@0 | 296 | set_sea_properties (dottedcircle); |
michael@0 | 297 | dottedcircle.codepoint = dottedcircle_glyph; |
michael@0 | 298 | |
michael@0 | 299 | buffer->clear_output (); |
michael@0 | 300 | |
michael@0 | 301 | buffer->idx = 0; |
michael@0 | 302 | unsigned int last_syllable = 0; |
michael@0 | 303 | while (buffer->idx < buffer->len) |
michael@0 | 304 | { |
michael@0 | 305 | unsigned int syllable = buffer->cur().syllable(); |
michael@0 | 306 | syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F); |
michael@0 | 307 | if (unlikely (last_syllable != syllable && syllable_type == broken_cluster)) |
michael@0 | 308 | { |
michael@0 | 309 | last_syllable = syllable; |
michael@0 | 310 | |
michael@0 | 311 | hb_glyph_info_t info = dottedcircle; |
michael@0 | 312 | info.cluster = buffer->cur().cluster; |
michael@0 | 313 | info.mask = buffer->cur().mask; |
michael@0 | 314 | info.syllable() = buffer->cur().syllable(); |
michael@0 | 315 | |
michael@0 | 316 | buffer->output_info (info); |
michael@0 | 317 | } |
michael@0 | 318 | else |
michael@0 | 319 | buffer->next_glyph (); |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | buffer->swap_buffers (); |
michael@0 | 323 | } |
michael@0 | 324 | |
michael@0 | 325 | static void |
michael@0 | 326 | initial_reordering (const hb_ot_shape_plan_t *plan, |
michael@0 | 327 | hb_font_t *font, |
michael@0 | 328 | hb_buffer_t *buffer) |
michael@0 | 329 | { |
michael@0 | 330 | insert_dotted_circles (plan, font, buffer); |
michael@0 | 331 | |
michael@0 | 332 | hb_glyph_info_t *info = buffer->info; |
michael@0 | 333 | unsigned int count = buffer->len; |
michael@0 | 334 | if (unlikely (!count)) return; |
michael@0 | 335 | unsigned int last = 0; |
michael@0 | 336 | unsigned int last_syllable = info[0].syllable(); |
michael@0 | 337 | for (unsigned int i = 1; i < count; i++) |
michael@0 | 338 | if (last_syllable != info[i].syllable()) { |
michael@0 | 339 | initial_reordering_syllable (plan, font->face, buffer, last, i); |
michael@0 | 340 | last = i; |
michael@0 | 341 | last_syllable = info[last].syllable(); |
michael@0 | 342 | } |
michael@0 | 343 | initial_reordering_syllable (plan, font->face, buffer, last, count); |
michael@0 | 344 | } |
michael@0 | 345 | |
michael@0 | 346 | static void |
michael@0 | 347 | final_reordering (const hb_ot_shape_plan_t *plan, |
michael@0 | 348 | hb_font_t *font HB_UNUSED, |
michael@0 | 349 | hb_buffer_t *buffer) |
michael@0 | 350 | { |
michael@0 | 351 | hb_glyph_info_t *info = buffer->info; |
michael@0 | 352 | unsigned int count = buffer->len; |
michael@0 | 353 | |
michael@0 | 354 | /* Zero syllables now... */ |
michael@0 | 355 | for (unsigned int i = 0; i < count; i++) |
michael@0 | 356 | info[i].syllable() = 0; |
michael@0 | 357 | |
michael@0 | 358 | HB_BUFFER_DEALLOCATE_VAR (buffer, sea_category); |
michael@0 | 359 | HB_BUFFER_DEALLOCATE_VAR (buffer, sea_position); |
michael@0 | 360 | } |
michael@0 | 361 | |
michael@0 | 362 | |
michael@0 | 363 | const hb_ot_complex_shaper_t _hb_ot_complex_shaper_sea = |
michael@0 | 364 | { |
michael@0 | 365 | "sea", |
michael@0 | 366 | collect_features_sea, |
michael@0 | 367 | override_features_sea, |
michael@0 | 368 | NULL, /* data_create */ |
michael@0 | 369 | NULL, /* data_destroy */ |
michael@0 | 370 | NULL, /* preprocess_text */ |
michael@0 | 371 | HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT, |
michael@0 | 372 | NULL, /* decompose */ |
michael@0 | 373 | NULL, /* compose */ |
michael@0 | 374 | setup_masks_sea, |
michael@0 | 375 | HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE, |
michael@0 | 376 | false, /* fallback_position */ |
michael@0 | 377 | }; |