hb-ot-shape-complex-indic.cc revision 9ac6b01e0cd8e2d66dfc727157f45b615bc77109
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 27#include "hb-ot-shape-complex-indic-private.hh" 28#include "hb-ot-layout-private.hh" 29 30/* buffer var allocations */ 31#define indic_category() complex_var_u8_0() /* indic_category_t */ 32#define indic_position() complex_var_u8_1() /* indic_position_t */ 33 34 35/* 36 * Indic shaper. 37 */ 38 39 40#define IN_HALF_BLOCK(u, Base) (((u) & ~0x7F) == (Base)) 41 42#define IS_DEVA(u) (IN_HALF_BLOCK (u, 0x0900)) 43#define IS_BENG(u) (IN_HALF_BLOCK (u, 0x0980)) 44#define IS_GURU(u) (IN_HALF_BLOCK (u, 0x0A00)) 45#define IS_GUJR(u) (IN_HALF_BLOCK (u, 0x0A80)) 46#define IS_ORYA(u) (IN_HALF_BLOCK (u, 0x0B00)) 47#define IS_TAML(u) (IN_HALF_BLOCK (u, 0x0B80)) 48#define IS_TELU(u) (IN_HALF_BLOCK (u, 0x0C00)) 49#define IS_KNDA(u) (IN_HALF_BLOCK (u, 0x0C80)) 50#define IS_MLYM(u) (IN_HALF_BLOCK (u, 0x0D00)) 51#define IS_SINH(u) (IN_HALF_BLOCK (u, 0x0D80)) 52#define IS_KHMR(u) (IN_HALF_BLOCK (u, 0x1780)) 53 54 55#define MATRA_POS_LEFT(u) POS_PRE_M 56#define MATRA_POS_RIGHT(u) ( \ 57 IS_DEVA(u) ? POS_AFTER_SUB : \ 58 IS_BENG(u) ? POS_AFTER_POST : \ 59 IS_GURU(u) ? POS_AFTER_POST : \ 60 IS_GUJR(u) ? POS_AFTER_POST : \ 61 IS_ORYA(u) ? POS_AFTER_POST : \ 62 IS_TAML(u) ? POS_AFTER_POST : \ 63 IS_TELU(u) ? (u <= 0x0C42 ? POS_BEFORE_SUB : POS_AFTER_SUB) : \ 64 IS_KNDA(u) ? (u < 0x0CC3 || u > 0xCD6 ? POS_BEFORE_SUB : POS_AFTER_SUB) : \ 65 IS_MLYM(u) ? POS_AFTER_POST : \ 66 IS_SINH(u) ? POS_AFTER_SUB : \ 67 IS_KHMR(u) ? POS_AFTER_POST : \ 68 /*default*/ POS_AFTER_SUB \ 69 ) 70#define MATRA_POS_TOP(u) ( /* BENG and MLYM don't have top matras. */ \ 71 IS_DEVA(u) ? POS_AFTER_SUB : \ 72 IS_GURU(u) ? POS_AFTER_POST : /* Deviate from spec */ \ 73 IS_GUJR(u) ? POS_AFTER_SUB : \ 74 IS_ORYA(u) ? POS_AFTER_MAIN : \ 75 IS_TAML(u) ? POS_AFTER_SUB : \ 76 IS_TELU(u) ? POS_BEFORE_SUB : \ 77 IS_KNDA(u) ? POS_BEFORE_SUB : \ 78 IS_SINH(u) ? POS_AFTER_SUB : \ 79 IS_KHMR(u) ? POS_AFTER_POST : \ 80 /*default*/ POS_AFTER_SUB \ 81 ) 82#define MATRA_POS_BOTTOM(u) ( \ 83 IS_DEVA(u) ? POS_AFTER_SUB : \ 84 IS_BENG(u) ? POS_AFTER_SUB : \ 85 IS_GURU(u) ? POS_AFTER_POST : \ 86 IS_GUJR(u) ? POS_AFTER_POST : \ 87 IS_ORYA(u) ? POS_AFTER_SUB : \ 88 IS_TAML(u) ? POS_AFTER_POST : \ 89 IS_TELU(u) ? POS_BEFORE_SUB : \ 90 IS_KNDA(u) ? POS_BEFORE_SUB : \ 91 IS_MLYM(u) ? POS_AFTER_POST : \ 92 IS_SINH(u) ? POS_AFTER_SUB : \ 93 IS_KHMR(u) ? POS_AFTER_POST : \ 94 /*default*/ POS_AFTER_SUB \ 95 ) 96 97static inline indic_position_t 98matra_position (hb_codepoint_t u, indic_position_t side) 99{ 100 switch ((int) side) 101 { 102 case POS_PRE_C: return MATRA_POS_LEFT (u); 103 case POS_POST_C: return MATRA_POS_RIGHT (u); 104 case POS_ABOVE_C: return MATRA_POS_TOP (u); 105 case POS_BELOW_C: return MATRA_POS_BOTTOM (u); 106 }; 107 return side; 108} 109 110/* XXX 111 * This is a hack for now. We should move this data into the main Indic table. 112 * Or completely remove it and just check in the tables. 113 */ 114static const hb_codepoint_t ra_chars[] = { 115 0x0930, /* Devanagari */ 116 0x09B0, /* Bengali */ 117 0x09F0, /* Bengali */ 118 0x0A30, /* Gurmukhi */ /* No Reph */ 119 0x0AB0, /* Gujarati */ 120 0x0B30, /* Oriya */ 121 0x0BB0, /* Tamil */ /* No Reph */ 122 0x0C30, /* Telugu */ /* Reph formed only with ZWJ */ 123 0x0CB0, /* Kannada */ 124 0x0D30, /* Malayalam */ /* No Reph, Logical Repha */ 125 126 0x0DBB, /* Sinhala */ /* Reph formed only with ZWJ */ 127 128 0x179A, /* Khmer */ /* No Reph, Visual Repha */ 129}; 130 131static inline indic_position_t 132consonant_position (hb_codepoint_t u) 133{ 134 if ((u & ~0x007F) == 0x1780) 135 return POS_BELOW_C; /* In Khmer coeng model, post and below forms should not be reordered. */ 136 return POS_BASE_C; /* Will recategorize later based on font lookups. */ 137} 138 139static inline bool 140is_ra (hb_codepoint_t u) 141{ 142 for (unsigned int i = 0; i < ARRAY_LENGTH (ra_chars); i++) 143 if (u == ra_chars[i]) 144 return true; 145 return false; 146} 147 148static inline bool 149is_one_of (const hb_glyph_info_t &info, unsigned int flags) 150{ 151 /* If it ligated, all bets are off. */ 152 if (is_a_ligature (info)) return false; 153 return !!(FLAG (info.indic_category()) & flags); 154} 155 156#define JOINER_FLAGS (FLAG (OT_ZWJ) | FLAG (OT_ZWNJ)) 157static inline bool 158is_joiner (const hb_glyph_info_t &info) 159{ 160 return is_one_of (info, JOINER_FLAGS); 161} 162 163/* Note: 164 * 165 * We treat Vowels and placeholders as if they were consonants. This is safe because Vowels 166 * cannot happen in a consonant syllable. The plus side however is, we can call the 167 * consonant syllable logic from the vowel syllable function and get it all right! */ 168#define CONSONANT_FLAGS (FLAG (OT_C) | FLAG (OT_CM) | FLAG (OT_Ra) | FLAG (OT_V) | FLAG (OT_NBSP) | FLAG (OT_DOTTEDCIRCLE)) 169static inline bool 170is_consonant (const hb_glyph_info_t &info) 171{ 172 return is_one_of (info, CONSONANT_FLAGS); 173} 174 175#define HALANT_OR_COENG_FLAGS (FLAG (OT_H) | FLAG (OT_Coeng)) 176static inline bool 177is_halant_or_coeng (const hb_glyph_info_t &info) 178{ 179 return is_one_of (info, HALANT_OR_COENG_FLAGS); 180} 181 182static inline void 183set_indic_properties (hb_glyph_info_t &info) 184{ 185 hb_codepoint_t u = info.codepoint; 186 unsigned int type = hb_indic_get_categories (u); 187 indic_category_t cat = (indic_category_t) (type & 0x7F); 188 indic_position_t pos = (indic_position_t) (type >> 8); 189 190 191 /* 192 * Re-assign category 193 */ 194 195 196 /* The spec says U+0952 is OT_A. However, testing shows that Uniscribe 197 * treats U+0951..U+0954 all behave similarly. 198 * TESTS: 199 * U+092E,U+0947,U+0952 200 * U+092E,U+0952,U+0947 201 * U+092E,U+0947,U+0951 202 * U+092E,U+0951,U+0947 203 */ 204 if (unlikely (hb_in_range<hb_codepoint_t> (u, 0x0951, 0x0954))) 205 cat = OT_A; 206 207 if (unlikely (u == 0x17D1)) 208 cat = OT_X; 209 if (cat == OT_X && 210 unlikely (hb_in_range<hb_codepoint_t> (u, 0x17CB, 0x17D3))) /* Khmer Various signs */ 211 { 212 /* These are like Top Matras. */ 213 cat = OT_M; 214 pos = POS_ABOVE_C; 215 } 216 if (u == 0x17C6) /* Khmer Bindu doesn't like to be repositioned. */ 217 cat = OT_N; 218 219 if (unlikely (u == 0x17D2)) cat = OT_Coeng; /* Khmer coeng */ 220 else if (unlikely (u == 0x200C)) cat = OT_ZWNJ; 221 else if (unlikely (u == 0x200D)) cat = OT_ZWJ; 222 else if (unlikely (u == 0x25CC)) cat = OT_DOTTEDCIRCLE; 223 else if (unlikely (u == 0x0A71)) cat = OT_SM; /* GURMUKHI ADDAK. Move it to the end. */ 224 225 if (cat == OT_Repha) { 226 /* There are two kinds of characters marked as Repha: 227 * - The ones that are GenCat=Mn are already positioned visually, ie. after base. (eg. Khmer) 228 * - The ones that are GenCat=Lo is encoded logically, ie. beginning of syllable. (eg. Malayalam) 229 * 230 * We recategorize the first kind to look like a Nukta and attached to the base directly. 231 */ 232 if (_hb_glyph_info_get_general_category (&info) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) 233 cat = OT_N; 234 } 235 236 237 238 /* 239 * Re-assign position. 240 */ 241 242 if ((FLAG (cat) & CONSONANT_FLAGS)) 243 { 244 pos = consonant_position (u); 245 if (is_ra (u)) 246 cat = OT_Ra; 247 } 248 else if (cat == OT_M) 249 { 250 pos = matra_position (u, pos); 251 } 252 else if ((FLAG (cat) & (FLAG (OT_SM) | FLAG (OT_VD) | FLAG (OT_A) | FLAG (OT_Avag)))) 253 { 254 pos = POS_SMVD; 255 } 256 257 if (unlikely (u == 0x0B01)) pos = POS_BEFORE_SUB; /* Oriya Bindu is BeforeSub in the spec. */ 258 259 260 261 info.indic_category() = cat; 262 info.indic_position() = pos; 263} 264 265/* 266 * Things above this line should ideally be moved to the Indic table itself. 267 */ 268 269 270/* 271 * Indic configurations. Note that we do not want to keep every single script-specific 272 * behavior in these tables necessarily. This should mainly be used for per-script 273 * properties that are cheaper keeping here, than in the code. Ie. if, say, one and 274 * only one script has an exception, that one script can be if'ed directly in the code, 275 * instead of adding a new flag in these structs. 276 */ 277 278enum base_position_t { 279 BASE_POS_FIRST, 280 BASE_POS_LAST 281}; 282enum reph_position_t { 283 REPH_POS_DEFAULT = POS_BEFORE_POST, 284 285 REPH_POS_AFTER_MAIN = POS_AFTER_MAIN, 286 REPH_POS_BEFORE_SUB = POS_BEFORE_SUB, 287 REPH_POS_AFTER_SUB = POS_AFTER_SUB, 288 REPH_POS_BEFORE_POST = POS_BEFORE_POST, 289 REPH_POS_AFTER_POST = POS_AFTER_POST 290}; 291enum reph_mode_t { 292 REPH_MODE_IMPLICIT, /* Reph formed out of initial Ra,H sequence. */ 293 REPH_MODE_EXPLICIT, /* Reph formed out of initial Ra,H,ZWJ sequence. */ 294 REPH_MODE_VIS_REPHA, /* Encoded Repha character, no reordering needed. */ 295 REPH_MODE_LOG_REPHA /* Encoded Repha character, needs reordering. */ 296}; 297enum blwf_mode_t { 298 BLWF_MODE_PRE_AND_POST, /* Below-forms feature applied to pre-base and post-base. */ 299 BLWF_MODE_POST_ONLY /* Below-forms feature applied to post-base only. */ 300}; 301struct indic_config_t 302{ 303 hb_script_t script; 304 bool has_old_spec; 305 hb_codepoint_t virama; 306 base_position_t base_pos; 307 reph_position_t reph_pos; 308 reph_mode_t reph_mode; 309 blwf_mode_t blwf_mode; 310}; 311 312static const indic_config_t indic_configs[] = 313{ 314 /* Default. Should be first. */ 315 {HB_SCRIPT_INVALID, false, 0,BASE_POS_LAST, REPH_POS_DEFAULT, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST}, 316 {HB_SCRIPT_DEVANAGARI,true, 0x094D,BASE_POS_LAST, REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST}, 317 {HB_SCRIPT_BENGALI, true, 0x09CD,BASE_POS_LAST, REPH_POS_AFTER_SUB, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST}, 318 {HB_SCRIPT_GURMUKHI, true, 0x0A4D,BASE_POS_LAST, REPH_POS_BEFORE_SUB, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST}, 319 {HB_SCRIPT_GUJARATI, true, 0x0ACD,BASE_POS_LAST, REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST}, 320 {HB_SCRIPT_ORIYA, true, 0x0B4D,BASE_POS_LAST, REPH_POS_AFTER_MAIN, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST}, 321 {HB_SCRIPT_TAMIL, true, 0x0BCD,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST}, 322 {HB_SCRIPT_TELUGU, true, 0x0C4D,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_EXPLICIT, BLWF_MODE_POST_ONLY}, 323 {HB_SCRIPT_KANNADA, true, 0x0CCD,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_IMPLICIT, BLWF_MODE_POST_ONLY}, 324 {HB_SCRIPT_MALAYALAM, true, 0x0D4D,BASE_POS_LAST, REPH_POS_AFTER_MAIN, REPH_MODE_LOG_REPHA,BLWF_MODE_PRE_AND_POST}, 325 {HB_SCRIPT_SINHALA, false,0x0DCA,BASE_POS_FIRST,REPH_POS_AFTER_MAIN, REPH_MODE_EXPLICIT, BLWF_MODE_PRE_AND_POST}, 326 {HB_SCRIPT_KHMER, false,0x17D2,BASE_POS_FIRST,REPH_POS_DEFAULT, REPH_MODE_VIS_REPHA,BLWF_MODE_PRE_AND_POST}, 327 {HB_SCRIPT_JAVANESE, false,0xA9C0,BASE_POS_LAST, REPH_POS_DEFAULT, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST}, 328}; 329 330 331 332/* 333 * Indic shaper. 334 */ 335 336struct feature_list_t { 337 hb_tag_t tag; 338 hb_ot_map_feature_flags_t flags; 339}; 340 341static const feature_list_t 342indic_features[] = 343{ 344 /* 345 * Basic features. 346 * These features are applied in order, one at a time, after initial_reordering. 347 */ 348 {HB_TAG('n','u','k','t'), F_GLOBAL}, 349 {HB_TAG('a','k','h','n'), F_GLOBAL}, 350 {HB_TAG('r','p','h','f'), F_NONE}, 351 {HB_TAG('r','k','r','f'), F_GLOBAL}, 352 {HB_TAG('p','r','e','f'), F_NONE}, 353 {HB_TAG('b','l','w','f'), F_NONE}, 354 {HB_TAG('a','b','v','f'), F_NONE}, 355 {HB_TAG('h','a','l','f'), F_NONE}, 356 {HB_TAG('p','s','t','f'), F_NONE}, 357 {HB_TAG('v','a','t','u'), F_GLOBAL}, 358 {HB_TAG('c','j','c','t'), F_GLOBAL}, 359 {HB_TAG('c','f','a','r'), F_NONE}, 360 /* 361 * Other features. 362 * These features are applied all at once, after final_reordering. 363 * Default Bengali font in Windows for example has intermixed 364 * lookups for init,pres,abvs,blws features. 365 */ 366 {HB_TAG('i','n','i','t'), F_NONE}, 367 {HB_TAG('p','r','e','s'), F_GLOBAL}, 368 {HB_TAG('a','b','v','s'), F_GLOBAL}, 369 {HB_TAG('b','l','w','s'), F_GLOBAL}, 370 {HB_TAG('p','s','t','s'), F_GLOBAL}, 371 {HB_TAG('h','a','l','n'), F_GLOBAL}, 372 /* Positioning features, though we don't care about the types. */ 373 {HB_TAG('d','i','s','t'), F_GLOBAL}, 374 {HB_TAG('a','b','v','m'), F_GLOBAL}, 375 {HB_TAG('b','l','w','m'), F_GLOBAL}, 376}; 377 378/* 379 * Must be in the same order as the indic_features array. 380 */ 381enum { 382 _NUKT, 383 _AKHN, 384 RPHF, 385 _RKRF, 386 PREF, 387 BLWF, 388 ABVF, 389 HALF, 390 PSTF, 391 _VATU, 392 _CJCT, 393 CFAR, 394 395 INIT, 396 _PRES, 397 _ABVS, 398 _BLWS, 399 _PSTS, 400 _HALN, 401 _DIST, 402 _ABVM, 403 _BLWM, 404 405 INDIC_NUM_FEATURES, 406 INDIC_BASIC_FEATURES = INIT /* Don't forget to update this! */ 407}; 408 409static void 410setup_syllables (const hb_ot_shape_plan_t *plan, 411 hb_font_t *font, 412 hb_buffer_t *buffer); 413static void 414initial_reordering (const hb_ot_shape_plan_t *plan, 415 hb_font_t *font, 416 hb_buffer_t *buffer); 417static void 418final_reordering (const hb_ot_shape_plan_t *plan, 419 hb_font_t *font, 420 hb_buffer_t *buffer); 421static void 422clear_syllables (const hb_ot_shape_plan_t *plan, 423 hb_font_t *font, 424 hb_buffer_t *buffer); 425 426static void 427collect_features_indic (hb_ot_shape_planner_t *plan) 428{ 429 hb_ot_map_builder_t *map = &plan->map; 430 431 /* Do this before any lookups have been applied. */ 432 map->add_gsub_pause (setup_syllables); 433 434 map->add_global_bool_feature (HB_TAG('l','o','c','l')); 435 /* The Indic specs do not require ccmp, but we apply it here since if 436 * there is a use of it, it's typically at the beginning. */ 437 map->add_global_bool_feature (HB_TAG('c','c','m','p')); 438 439 440 unsigned int i = 0; 441 map->add_gsub_pause (initial_reordering); 442 for (; i < INDIC_BASIC_FEATURES; i++) { 443 map->add_feature (indic_features[i].tag, 1, indic_features[i].flags | F_MANUAL_ZWJ); 444 map->add_gsub_pause (NULL); 445 } 446 map->add_gsub_pause (final_reordering); 447 for (; i < INDIC_NUM_FEATURES; i++) { 448 map->add_feature (indic_features[i].tag, 1, indic_features[i].flags | F_MANUAL_ZWJ); 449 } 450 451 map->add_global_bool_feature (HB_TAG('c','a','l','t')); 452 map->add_global_bool_feature (HB_TAG('c','l','i','g')); 453 454 map->add_gsub_pause (clear_syllables); 455} 456 457static void 458override_features_indic (hb_ot_shape_planner_t *plan) 459{ 460 /* Uniscribe does not apply 'kern' in Khmer. */ 461 if (hb_options ().uniscribe_bug_compatible) 462 { 463 switch ((hb_tag_t) plan->props.script) 464 { 465 case HB_SCRIPT_KHMER: 466 plan->map.add_feature (HB_TAG('k','e','r','n'), 0, F_GLOBAL); 467 break; 468 } 469 } 470 471 plan->map.add_feature (HB_TAG('l','i','g','a'), 0, F_GLOBAL); 472} 473 474 475struct would_substitute_feature_t 476{ 477 inline void init (const hb_ot_map_t *map, hb_tag_t feature_tag) 478 { 479 map->get_stage_lookups (0/*GSUB*/, 480 map->get_feature_stage (0/*GSUB*/, feature_tag), 481 &lookups, &count); 482 } 483 484 inline bool would_substitute (const hb_codepoint_t *glyphs, 485 unsigned int glyphs_count, 486 bool zero_context, 487 hb_face_t *face) const 488 { 489 for (unsigned int i = 0; i < count; i++) 490 if (hb_ot_layout_lookup_would_substitute_fast (face, lookups[i].index, glyphs, glyphs_count, zero_context)) 491 return true; 492 return false; 493 } 494 495 private: 496 const hb_ot_map_t::lookup_map_t *lookups; 497 unsigned int count; 498}; 499 500struct indic_shape_plan_t 501{ 502 ASSERT_POD (); 503 504 inline bool get_virama_glyph (hb_font_t *font, hb_codepoint_t *pglyph) const 505 { 506 hb_codepoint_t glyph = virama_glyph; 507 if (unlikely (virama_glyph == (hb_codepoint_t) -1)) 508 { 509 if (!config->virama || !font->get_glyph (config->virama, 0, &glyph)) 510 glyph = 0; 511 /* Technically speaking, the spec says we should apply 'locl' to virama too. 512 * Maybe one day... */ 513 514 /* Our get_glyph() function needs a font, so we can't get the virama glyph 515 * during shape planning... Instead, overwrite it here. It's safe. Don't worry! */ 516 (const_cast<indic_shape_plan_t *> (this))->virama_glyph = glyph; 517 } 518 519 *pglyph = glyph; 520 return glyph != 0; 521 } 522 523 const indic_config_t *config; 524 525 bool is_old_spec; 526 hb_codepoint_t virama_glyph; 527 528 would_substitute_feature_t rphf; 529 would_substitute_feature_t pref; 530 would_substitute_feature_t blwf; 531 would_substitute_feature_t pstf; 532 533 hb_mask_t mask_array[INDIC_NUM_FEATURES]; 534}; 535 536static void * 537data_create_indic (const hb_ot_shape_plan_t *plan) 538{ 539 indic_shape_plan_t *indic_plan = (indic_shape_plan_t *) calloc (1, sizeof (indic_shape_plan_t)); 540 if (unlikely (!indic_plan)) 541 return NULL; 542 543 indic_plan->config = &indic_configs[0]; 544 for (unsigned int i = 1; i < ARRAY_LENGTH (indic_configs); i++) 545 if (plan->props.script == indic_configs[i].script) { 546 indic_plan->config = &indic_configs[i]; 547 break; 548 } 549 550 indic_plan->is_old_spec = indic_plan->config->has_old_spec && ((plan->map.chosen_script[0] & 0x000000FF) != '2'); 551 indic_plan->virama_glyph = (hb_codepoint_t) -1; 552 553 indic_plan->rphf.init (&plan->map, HB_TAG('r','p','h','f')); 554 indic_plan->pref.init (&plan->map, HB_TAG('p','r','e','f')); 555 indic_plan->blwf.init (&plan->map, HB_TAG('b','l','w','f')); 556 indic_plan->pstf.init (&plan->map, HB_TAG('p','s','t','f')); 557 558 for (unsigned int i = 0; i < ARRAY_LENGTH (indic_plan->mask_array); i++) 559 indic_plan->mask_array[i] = (indic_features[i].flags & F_GLOBAL) ? 560 0 : plan->map.get_1_mask (indic_features[i].tag); 561 562 return indic_plan; 563} 564 565static void 566data_destroy_indic (void *data) 567{ 568 free (data); 569} 570 571static indic_position_t 572consonant_position_from_face (const indic_shape_plan_t *indic_plan, 573 const hb_codepoint_t glyphs[2], 574 hb_face_t *face) 575{ 576 /* For old-spec, the order of glyphs is Consonant,Virama, 577 * whereas for new-spec, it's Virama,Consonant. However, 578 * some broken fonts (like Free Sans) simply copied lookups 579 * from old-spec to new-spec without modification. 580 * And oddly enough, Uniscribe seems to respect those lookups. 581 * Eg. in the sequence U+0924,U+094D,U+0930, Uniscribe finds 582 * base at 0. The font however, only has lookups matching 583 * 930,94D in 'blwf', not the expected 94D,930 (with new-spec 584 * table). As such, we simply match both sequences. Seems 585 * to work. */ 586 bool zero_context = indic_plan->is_old_spec ? false : true; 587 hb_codepoint_t glyphs_r[2] = {glyphs[1], glyphs[0]}; 588 if (indic_plan->blwf.would_substitute (glyphs , 2, zero_context, face) || 589 indic_plan->blwf.would_substitute (glyphs_r, 2, zero_context, face)) 590 return POS_BELOW_C; 591 if (indic_plan->pstf.would_substitute (glyphs , 2, zero_context, face) || 592 indic_plan->pstf.would_substitute (glyphs_r, 2, zero_context, face)) 593 return POS_POST_C; 594 if (indic_plan->pref.would_substitute (glyphs , 2, zero_context, face) || 595 indic_plan->pref.would_substitute (glyphs_r, 2, zero_context, face)) 596 return POS_POST_C; 597 return POS_BASE_C; 598} 599 600 601enum syllable_type_t { 602 consonant_syllable, 603 vowel_syllable, 604 standalone_cluster, 605 avagraha_cluster, 606 broken_cluster, 607 non_indic_cluster, 608}; 609 610#include "hb-ot-shape-complex-indic-machine.hh" 611 612 613static void 614setup_masks_indic (const hb_ot_shape_plan_t *plan HB_UNUSED, 615 hb_buffer_t *buffer, 616 hb_font_t *font HB_UNUSED) 617{ 618 HB_BUFFER_ALLOCATE_VAR (buffer, indic_category); 619 HB_BUFFER_ALLOCATE_VAR (buffer, indic_position); 620 621 /* We cannot setup masks here. We save information about characters 622 * and setup masks later on in a pause-callback. */ 623 624 unsigned int count = buffer->len; 625 for (unsigned int i = 0; i < count; i++) 626 set_indic_properties (buffer->info[i]); 627} 628 629static void 630setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED, 631 hb_font_t *font HB_UNUSED, 632 hb_buffer_t *buffer) 633{ 634 find_syllables (buffer); 635} 636 637static int 638compare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb) 639{ 640 int a = pa->indic_position(); 641 int b = pb->indic_position(); 642 643 return a < b ? -1 : a == b ? 0 : +1; 644} 645 646 647 648static void 649update_consonant_positions (const hb_ot_shape_plan_t *plan, 650 hb_font_t *font, 651 hb_buffer_t *buffer) 652{ 653 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data; 654 655 hb_codepoint_t glyphs[2]; 656 if (indic_plan->get_virama_glyph (font, &glyphs[0])) 657 { 658 hb_face_t *face = font->face; 659 unsigned int count = buffer->len; 660 for (unsigned int i = 0; i < count; i++) 661 if (buffer->info[i].indic_position() == POS_BASE_C) { 662 glyphs[1] = buffer->info[i].codepoint; 663 buffer->info[i].indic_position() = consonant_position_from_face (indic_plan, glyphs, face); 664 } 665 } 666} 667 668 669/* Rules from: 670 * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */ 671 672static void 673initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan, 674 hb_face_t *face, 675 hb_buffer_t *buffer, 676 unsigned int start, unsigned int end) 677{ 678 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data; 679 hb_glyph_info_t *info = buffer->info; 680 681 682 /* 1. Find base consonant: 683 * 684 * The shaping engine finds the base consonant of the syllable, using the 685 * following algorithm: starting from the end of the syllable, move backwards 686 * until a consonant is found that does not have a below-base or post-base 687 * form (post-base forms have to follow below-base forms), or that is not a 688 * pre-base reordering Ra, or arrive at the first consonant. The consonant 689 * stopped at will be the base. 690 * 691 * o If the syllable starts with Ra + Halant (in a script that has Reph) 692 * and has more than one consonant, Ra is excluded from candidates for 693 * base consonants. 694 */ 695 696 unsigned int base = end; 697 bool has_reph = false; 698 699 { 700 /* -> If the syllable starts with Ra + Halant (in a script that has Reph) 701 * and has more than one consonant, Ra is excluded from candidates for 702 * base consonants. */ 703 unsigned int limit = start; 704 if (indic_plan->mask_array[RPHF] && 705 start + 3 <= end && 706 ( 707 (indic_plan->config->reph_mode == REPH_MODE_IMPLICIT && !is_joiner (info[start + 2])) || 708 (indic_plan->config->reph_mode == REPH_MODE_EXPLICIT && info[start + 2].indic_category() == OT_ZWJ) 709 )) 710 { 711 /* See if it matches the 'rphf' feature. */ 712 hb_codepoint_t glyphs[2] = {info[start].codepoint, info[start + 1].codepoint}; 713 if (indic_plan->rphf.would_substitute (glyphs, ARRAY_LENGTH (glyphs), true, face)) 714 { 715 limit += 2; 716 while (limit < end && is_joiner (info[limit])) 717 limit++; 718 base = start; 719 has_reph = true; 720 } 721 } else if (indic_plan->config->reph_mode == REPH_MODE_LOG_REPHA && info[start].indic_category() == OT_Repha) 722 { 723 limit += 1; 724 while (limit < end && is_joiner (info[limit])) 725 limit++; 726 base = start; 727 has_reph = true; 728 } 729 730 switch (indic_plan->config->base_pos) 731 { 732 default: 733 assert (false); 734 /* fallthrough */ 735 736 case BASE_POS_LAST: 737 { 738 /* -> starting from the end of the syllable, move backwards */ 739 unsigned int i = end; 740 bool seen_below = false; 741 do { 742 i--; 743 /* -> until a consonant is found */ 744 if (is_consonant (info[i])) 745 { 746 /* -> that does not have a below-base or post-base form 747 * (post-base forms have to follow below-base forms), */ 748 if (info[i].indic_position() != POS_BELOW_C && 749 (info[i].indic_position() != POS_POST_C || seen_below)) 750 { 751 base = i; 752 break; 753 } 754 if (info[i].indic_position() == POS_BELOW_C) 755 seen_below = true; 756 757 /* -> or that is not a pre-base reordering Ra, 758 * 759 * IMPLEMENTATION NOTES: 760 * 761 * Our pre-base reordering Ra's are marked POS_POST_C, so will be skipped 762 * by the logic above already. 763 */ 764 765 /* -> or arrive at the first consonant. The consonant stopped at will 766 * be the base. */ 767 base = i; 768 } 769 else 770 { 771 /* A ZWJ after a Halant stops the base search, and requests an explicit 772 * half form. 773 * A ZWJ before a Halant, requests a subjoined form instead, and hence 774 * search continues. This is particularly important for Bengali 775 * sequence Ra,H,Ya that should form Ya-Phalaa by subjoining Ya. */ 776 if (start < i && 777 info[i].indic_category() == OT_ZWJ && 778 info[i - 1].indic_category() == OT_H) 779 break; 780 } 781 } while (i > limit); 782 } 783 break; 784 785 case BASE_POS_FIRST: 786 { 787 /* In scripts without half forms (eg. Khmer), the first consonant is always the base. */ 788 789 if (!has_reph) 790 base = limit; 791 792 /* Find the last base consonant that is not blocked by ZWJ. If there is 793 * a ZWJ right before a base consonant, that would request a subjoined form. */ 794 for (unsigned int i = limit; i < end; i++) 795 if (is_consonant (info[i]) && info[i].indic_position() == POS_BASE_C) 796 { 797 if (limit < i && info[i - 1].indic_category() == OT_ZWJ) 798 break; 799 else 800 base = i; 801 } 802 803 /* Mark all subsequent consonants as below. */ 804 for (unsigned int i = base + 1; i < end; i++) 805 if (is_consonant (info[i]) && info[i].indic_position() == POS_BASE_C) 806 info[i].indic_position() = POS_BELOW_C; 807 } 808 break; 809 } 810 811 /* -> If the syllable starts with Ra + Halant (in a script that has Reph) 812 * and has more than one consonant, Ra is excluded from candidates for 813 * base consonants. 814 * 815 * Only do this for unforced Reph. (ie. not for Ra,H,ZWJ. */ 816 if (has_reph && base == start && limit - base <= 2) { 817 /* Have no other consonant, so Reph is not formed and Ra becomes base. */ 818 has_reph = false; 819 } 820 } 821 822 823 /* 2. Decompose and reorder Matras: 824 * 825 * Each matra and any syllable modifier sign in the cluster are moved to the 826 * appropriate position relative to the consonant(s) in the cluster. The 827 * shaping engine decomposes two- or three-part matras into their constituent 828 * parts before any repositioning. Matra characters are classified by which 829 * consonant in a conjunct they have affinity for and are reordered to the 830 * following positions: 831 * 832 * o Before first half form in the syllable 833 * o After subjoined consonants 834 * o After post-form consonant 835 * o After main consonant (for above marks) 836 * 837 * IMPLEMENTATION NOTES: 838 * 839 * The normalize() routine has already decomposed matras for us, so we don't 840 * need to worry about that. 841 */ 842 843 844 /* 3. Reorder marks to canonical order: 845 * 846 * Adjacent nukta and halant or nukta and vedic sign are always repositioned 847 * if necessary, so that the nukta is first. 848 * 849 * IMPLEMENTATION NOTES: 850 * 851 * We don't need to do this: the normalize() routine already did this for us. 852 */ 853 854 855 /* Reorder characters */ 856 857 for (unsigned int i = start; i < base; i++) 858 info[i].indic_position() = MIN (POS_PRE_C, (indic_position_t) info[i].indic_position()); 859 860 if (base < end) 861 info[base].indic_position() = POS_BASE_C; 862 863 /* Mark final consonants. A final consonant is one appearing after a matra, 864 * like in Khmer. */ 865 for (unsigned int i = base + 1; i < end; i++) 866 if (info[i].indic_category() == OT_M) { 867 for (unsigned int j = i + 1; j < end; j++) 868 if (is_consonant (info[j])) { 869 info[j].indic_position() = POS_FINAL_C; 870 break; 871 } 872 break; 873 } 874 875 /* Handle beginning Ra */ 876 if (has_reph) 877 info[start].indic_position() = POS_RA_TO_BECOME_REPH; 878 879 /* For old-style Indic script tags, move the first post-base Halant after 880 * last consonant. Only do this if there is *not* a Halant after last 881 * consonant. Otherwise it becomes messy. */ 882 if (indic_plan->is_old_spec) { 883 for (unsigned int i = base + 1; i < end; i++) 884 if (info[i].indic_category() == OT_H) { 885 unsigned int j; 886 for (j = end - 1; j > i; j--) 887 if (is_consonant (info[j]) || info[j].indic_category() == OT_H) 888 break; 889 if (info[j].indic_category() != OT_H && j > i) { 890 /* Move Halant to after last consonant. */ 891 hb_glyph_info_t t = info[i]; 892 memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0])); 893 info[j] = t; 894 } 895 break; 896 } 897 } 898 899 /* Attach misc marks to previous char to move with them. */ 900 { 901 indic_position_t last_pos = POS_START; 902 for (unsigned int i = start; i < end; i++) 903 { 904 if ((FLAG (info[i].indic_category()) & (JOINER_FLAGS | FLAG (OT_N) | FLAG (OT_RS) | HALANT_OR_COENG_FLAGS))) 905 { 906 info[i].indic_position() = last_pos; 907 if (unlikely (info[i].indic_category() == OT_H && 908 info[i].indic_position() == POS_PRE_M)) 909 { 910 /* 911 * Uniscribe doesn't move the Halant with Left Matra. 912 * TEST: U+092B,U+093F,U+094DE 913 * We follow. This is important for the Sinhala 914 * U+0DDA split matra since it decomposes to U+0DD9,U+0DCA 915 * where U+0DD9 is a left matra and U+0DCA is the virama. 916 * We don't want to move the virama with the left matra. 917 * TEST: U+0D9A,U+0DDA 918 */ 919 for (unsigned int j = i; j > start; j--) 920 if (info[j - 1].indic_position() != POS_PRE_M) { 921 info[i].indic_position() = info[j - 1].indic_position(); 922 break; 923 } 924 } 925 } else if (info[i].indic_position() != POS_SMVD) { 926 last_pos = (indic_position_t) info[i].indic_position(); 927 } 928 } 929 } 930 /* Re-attach ZWJ, ZWNJ, and halant to next char, for after-base consonants. */ 931 { 932 unsigned int last_halant = end; 933 for (unsigned int i = base + 1; i < end; i++) 934 if (is_halant_or_coeng (info[i])) 935 last_halant = i; 936 else if (is_consonant (info[i])) { 937 for (unsigned int j = last_halant; j < i; j++) 938 if (info[j].indic_position() != POS_SMVD) 939 info[j].indic_position() = info[i].indic_position(); 940 last_halant = end; 941 } 942 } 943 944 945 { 946 /* Use syllable() for sort accounting temporarily. */ 947 unsigned int syllable = info[start].syllable(); 948 for (unsigned int i = start; i < end; i++) 949 info[i].syllable() = i - start; 950 951 /* Sit tight, rock 'n roll! */ 952 hb_bubble_sort (info + start, end - start, compare_indic_order); 953 /* Find base again */ 954 base = end; 955 for (unsigned int i = start; i < end; i++) 956 if (info[i].indic_position() == POS_BASE_C) 957 { 958 base = i; 959 break; 960 } 961 /* Things are out-of-control for post base positions, they may shuffle 962 * around like crazy. In old-spec mode, we move halants around, so in 963 * that case merge all clusters after base. Otherwise, check the sort 964 * order and merge as needed. 965 * For pre-base stuff, we handle cluster issues in final reordering. */ 966 if (indic_plan->is_old_spec || end - base > 127) 967 buffer->merge_clusters (base, end); 968 else 969 { 970 /* Note! syllable() is a one-byte field. */ 971 for (unsigned int i = base; i < end; i++) 972 if (info[i].syllable() != 255) 973 { 974 unsigned int max = i; 975 unsigned int j = start + info[i].syllable(); 976 while (j != i) 977 { 978 max = MAX (max, j); 979 unsigned int next = start + info[j].syllable(); 980 info[j].syllable() = 255; /* So we don't process j later again. */ 981 j = next; 982 } 983 if (i != max) 984 buffer->merge_clusters (i, max + 1); 985 } 986 } 987 988 /* Put syllable back in. */ 989 for (unsigned int i = start; i < end; i++) 990 info[i].syllable() = syllable; 991 } 992 993 /* Setup masks now */ 994 995 { 996 hb_mask_t mask; 997 998 /* Reph */ 999 for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++) 1000 info[i].mask |= indic_plan->mask_array[RPHF]; 1001 1002 /* Pre-base */ 1003 mask = indic_plan->mask_array[HALF]; 1004 if (!indic_plan->is_old_spec && 1005 indic_plan->config->blwf_mode == BLWF_MODE_PRE_AND_POST) 1006 mask |= indic_plan->mask_array[BLWF]; 1007 for (unsigned int i = start; i < base; i++) 1008 info[i].mask |= mask; 1009 /* Base */ 1010 mask = 0; 1011 if (base < end) 1012 info[base].mask |= mask; 1013 /* Post-base */ 1014 mask = indic_plan->mask_array[BLWF] | indic_plan->mask_array[ABVF] | indic_plan->mask_array[PSTF]; 1015 for (unsigned int i = base + 1; i < end; i++) 1016 info[i].mask |= mask; 1017 } 1018 1019 if (indic_plan->is_old_spec && 1020 buffer->props.script == HB_SCRIPT_DEVANAGARI) 1021 { 1022 /* Old-spec eye-lash Ra needs special handling. From the 1023 * spec: 1024 * 1025 * "The feature 'below-base form' is applied to consonants 1026 * having below-base forms and following the base consonant. 1027 * The exception is vattu, which may appear below half forms 1028 * as well as below the base glyph. The feature 'below-base 1029 * form' will be applied to all such occurrences of Ra as well." 1030 * 1031 * Test case: U+0924,U+094D,U+0930,U+094d,U+0915 1032 * with Sanskrit 2003 font. 1033 * 1034 * However, note that Ra,Halant,ZWJ is the correct way to 1035 * request eyelash form of Ra, so we wouldbn't inhibit it 1036 * in that sequence. 1037 * 1038 * Test case: U+0924,U+094D,U+0930,U+094d,U+200D,U+0915 1039 */ 1040 for (unsigned int i = start; i + 1 < base; i++) 1041 if (info[i ].indic_category() == OT_Ra && 1042 info[i+1].indic_category() == OT_H && 1043 (i + 2 == base || 1044 info[i+2].indic_category() != OT_ZWJ)) 1045 { 1046 info[i ].mask |= indic_plan->mask_array[BLWF]; 1047 info[i+1].mask |= indic_plan->mask_array[BLWF]; 1048 } 1049 } 1050 1051 if (indic_plan->mask_array[PREF] && base + 2 < end) 1052 { 1053 /* Find a Halant,Ra sequence and mark it for pre-base reordering processing. */ 1054 for (unsigned int i = base + 1; i + 1 < end; i++) { 1055 hb_codepoint_t glyphs[2] = {info[i].codepoint, info[i + 1].codepoint}; 1056 if (indic_plan->pref.would_substitute (glyphs, ARRAY_LENGTH (glyphs), true, face)) 1057 { 1058 info[i++].mask |= indic_plan->mask_array[PREF]; 1059 info[i++].mask |= indic_plan->mask_array[PREF]; 1060 1061 /* Mark the subsequent stuff with 'cfar'. Used in Khmer. 1062 * Read the feature spec. 1063 * This allows distinguishing the following cases with MS Khmer fonts: 1064 * U+1784,U+17D2,U+179A,U+17D2,U+1782 1065 * U+1784,U+17D2,U+1782,U+17D2,U+179A 1066 */ 1067 for (; i < end; i++) 1068 info[i].mask |= indic_plan->mask_array[CFAR]; 1069 1070 break; 1071 } 1072 } 1073 } 1074 1075 /* Apply ZWJ/ZWNJ effects */ 1076 for (unsigned int i = start + 1; i < end; i++) 1077 if (is_joiner (info[i])) { 1078 bool non_joiner = info[i].indic_category() == OT_ZWNJ; 1079 unsigned int j = i; 1080 1081 do { 1082 j--; 1083 1084 /* ZWJ/ZWNJ should disable CJCT. They do that by simply 1085 * being there, since we don't skip them for the CJCT 1086 * feature (ie. F_MANUAL_ZWJ) */ 1087 1088 /* A ZWNJ disables HALF. */ 1089 if (non_joiner) 1090 info[j].mask &= ~indic_plan->mask_array[HALF]; 1091 1092 } while (j > start && !is_consonant (info[j])); 1093 } 1094} 1095 1096 1097static void 1098initial_reordering_vowel_syllable (const hb_ot_shape_plan_t *plan, 1099 hb_face_t *face, 1100 hb_buffer_t *buffer, 1101 unsigned int start, unsigned int end) 1102{ 1103 /* We made the vowels look like consonants. So let's call the consonant logic! */ 1104 initial_reordering_consonant_syllable (plan, face, buffer, start, end); 1105} 1106 1107static void 1108initial_reordering_standalone_cluster (const hb_ot_shape_plan_t *plan, 1109 hb_face_t *face, 1110 hb_buffer_t *buffer, 1111 unsigned int start, unsigned int end) 1112{ 1113 /* We treat NBSP/dotted-circle as if they are consonants, so we should just chain. 1114 * Only if not in compatibility mode that is... */ 1115 1116 if (hb_options ().uniscribe_bug_compatible) 1117 { 1118 /* For dotted-circle, this is what Uniscribe does: 1119 * If dotted-circle is the last glyph, it just does nothing. 1120 * Ie. It doesn't form Reph. */ 1121 if (buffer->info[end - 1].indic_category() == OT_DOTTEDCIRCLE) 1122 return; 1123 } 1124 1125 initial_reordering_consonant_syllable (plan, face, buffer, start, end); 1126} 1127 1128static void 1129initial_reordering_broken_cluster (const hb_ot_shape_plan_t *plan, 1130 hb_face_t *face, 1131 hb_buffer_t *buffer, 1132 unsigned int start, unsigned int end) 1133{ 1134 /* We already inserted dotted-circles, so just call the standalone_cluster. */ 1135 initial_reordering_standalone_cluster (plan, face, buffer, start, end); 1136} 1137 1138static void 1139initial_reordering_avagraha_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED, 1140 hb_face_t *face HB_UNUSED, 1141 hb_buffer_t *buffer HB_UNUSED, 1142 unsigned int start HB_UNUSED, unsigned int end HB_UNUSED) 1143{ 1144 /* Nothing to do right now. If we ever switch to using the output 1145 * buffer in the reordering process, we'd need to next_glyph() here. */ 1146} 1147 1148static void 1149initial_reordering_non_indic_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED, 1150 hb_face_t *face HB_UNUSED, 1151 hb_buffer_t *buffer HB_UNUSED, 1152 unsigned int start HB_UNUSED, unsigned int end HB_UNUSED) 1153{ 1154 /* Nothing to do right now. If we ever switch to using the output 1155 * buffer in the reordering process, we'd need to next_glyph() here. */ 1156} 1157 1158 1159static void 1160initial_reordering_syllable (const hb_ot_shape_plan_t *plan, 1161 hb_face_t *face, 1162 hb_buffer_t *buffer, 1163 unsigned int start, unsigned int end) 1164{ 1165 syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F); 1166 switch (syllable_type) { 1167 case consonant_syllable: initial_reordering_consonant_syllable (plan, face, buffer, start, end); return; 1168 case vowel_syllable: initial_reordering_vowel_syllable (plan, face, buffer, start, end); return; 1169 case standalone_cluster: initial_reordering_standalone_cluster (plan, face, buffer, start, end); return; 1170 case avagraha_cluster: initial_reordering_avagraha_cluster (plan, face, buffer, start, end); return; 1171 case broken_cluster: initial_reordering_broken_cluster (plan, face, buffer, start, end); return; 1172 case non_indic_cluster: initial_reordering_non_indic_cluster (plan, face, buffer, start, end); return; 1173 } 1174} 1175 1176static inline void 1177insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED, 1178 hb_font_t *font, 1179 hb_buffer_t *buffer) 1180{ 1181 /* Note: This loop is extra overhead, but should not be measurable. */ 1182 bool has_broken_syllables = false; 1183 unsigned int count = buffer->len; 1184 for (unsigned int i = 0; i < count; i++) 1185 if ((buffer->info[i].syllable() & 0x0F) == broken_cluster) { 1186 has_broken_syllables = true; 1187 break; 1188 } 1189 if (likely (!has_broken_syllables)) 1190 return; 1191 1192 1193 hb_codepoint_t dottedcircle_glyph; 1194 if (!font->get_glyph (0x25CC, 0, &dottedcircle_glyph)) 1195 return; 1196 1197 hb_glyph_info_t dottedcircle = {0}; 1198 dottedcircle.codepoint = 0x25CC; 1199 set_indic_properties (dottedcircle); 1200 dottedcircle.codepoint = dottedcircle_glyph; 1201 1202 buffer->clear_output (); 1203 1204 buffer->idx = 0; 1205 unsigned int last_syllable = 0; 1206 while (buffer->idx < buffer->len) 1207 { 1208 unsigned int syllable = buffer->cur().syllable(); 1209 syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F); 1210 if (unlikely (last_syllable != syllable && syllable_type == broken_cluster)) 1211 { 1212 last_syllable = syllable; 1213 1214 hb_glyph_info_t info = dottedcircle; 1215 info.cluster = buffer->cur().cluster; 1216 info.mask = buffer->cur().mask; 1217 info.syllable() = buffer->cur().syllable(); 1218 1219 /* Insert dottedcircle after possible Repha. */ 1220 while (buffer->idx < buffer->len && 1221 last_syllable == buffer->cur().syllable() && 1222 buffer->cur().indic_category() == OT_Repha) 1223 buffer->next_glyph (); 1224 1225 buffer->output_info (info); 1226 } 1227 else 1228 buffer->next_glyph (); 1229 } 1230 1231 buffer->swap_buffers (); 1232} 1233 1234static void 1235initial_reordering (const hb_ot_shape_plan_t *plan, 1236 hb_font_t *font, 1237 hb_buffer_t *buffer) 1238{ 1239 update_consonant_positions (plan, font, buffer); 1240 insert_dotted_circles (plan, font, buffer); 1241 1242 hb_glyph_info_t *info = buffer->info; 1243 unsigned int count = buffer->len; 1244 if (unlikely (!count)) return; 1245 unsigned int last = 0; 1246 unsigned int last_syllable = info[0].syllable(); 1247 for (unsigned int i = 1; i < count; i++) 1248 if (last_syllable != info[i].syllable()) { 1249 initial_reordering_syllable (plan, font->face, buffer, last, i); 1250 last = i; 1251 last_syllable = info[last].syllable(); 1252 } 1253 initial_reordering_syllable (plan, font->face, buffer, last, count); 1254} 1255 1256static void 1257final_reordering_syllable (const hb_ot_shape_plan_t *plan, 1258 hb_buffer_t *buffer, 1259 unsigned int start, unsigned int end) 1260{ 1261 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data; 1262 hb_glyph_info_t *info = buffer->info; 1263 1264 /* 4. Final reordering: 1265 * 1266 * After the localized forms and basic shaping forms GSUB features have been 1267 * applied (see below), the shaping engine performs some final glyph 1268 * reordering before applying all the remaining font features to the entire 1269 * cluster. 1270 */ 1271 1272 /* Find base again */ 1273 unsigned int base; 1274 for (base = start; base < end; base++) 1275 if (info[base].indic_position() >= POS_BASE_C) { 1276 if (start < base && info[base].indic_position() > POS_BASE_C) 1277 base--; 1278 break; 1279 } 1280 if (base == end && start < base && 1281 info[base - 1].indic_category() != OT_ZWJ) 1282 base--; 1283 while (start < base && 1284 (info[base].indic_category() == OT_H || 1285 info[base].indic_category() == OT_N)) 1286 base--; 1287 1288 1289 /* o Reorder matras: 1290 * 1291 * If a pre-base matra character had been reordered before applying basic 1292 * features, the glyph can be moved closer to the main consonant based on 1293 * whether half-forms had been formed. Actual position for the matra is 1294 * defined as “after last standalone halant glyph, after initial matra 1295 * position and before the main consonant”. If ZWJ or ZWNJ follow this 1296 * halant, position is moved after it. 1297 */ 1298 1299 if (start + 1 < end && start < base) /* Otherwise there can't be any pre-base matra characters. */ 1300 { 1301 /* If we lost track of base, alas, position before last thingy. */ 1302 unsigned int new_pos = base == end ? base - 2 : base - 1; 1303 1304 /* Malayalam / Tamil do not have "half" forms or explicit virama forms. 1305 * The glyphs formed by 'half' are Chillus or ligated explicit viramas. 1306 * We want to position matra after them. 1307 */ 1308 if (buffer->props.script != HB_SCRIPT_MALAYALAM && buffer->props.script != HB_SCRIPT_TAMIL) 1309 { 1310 while (new_pos > start && 1311 !(is_one_of (info[new_pos], (FLAG (OT_M) | FLAG (OT_H) | FLAG (OT_Coeng))))) 1312 new_pos--; 1313 1314 /* If we found no Halant we are done. 1315 * Otherwise only proceed if the Halant does 1316 * not belong to the Matra itself! */ 1317 if (is_halant_or_coeng (info[new_pos]) && 1318 info[new_pos].indic_position() != POS_PRE_M) 1319 { 1320 /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */ 1321 if (new_pos + 1 < end && is_joiner (info[new_pos + 1])) 1322 new_pos++; 1323 } 1324 else 1325 new_pos = start; /* No move. */ 1326 } 1327 1328 if (start < new_pos && info[new_pos].indic_position () != POS_PRE_M) 1329 { 1330 /* Now go see if there's actually any matras... */ 1331 for (unsigned int i = new_pos; i > start; i--) 1332 if (info[i - 1].indic_position () == POS_PRE_M) 1333 { 1334 unsigned int old_pos = i - 1; 1335 hb_glyph_info_t tmp = info[old_pos]; 1336 memmove (&info[old_pos], &info[old_pos + 1], (new_pos - old_pos) * sizeof (info[0])); 1337 info[new_pos] = tmp; 1338 if (old_pos < base && base <= new_pos) /* Shouldn't actually happen. */ 1339 base--; 1340 buffer->merge_clusters (new_pos, MIN (end, base + 1)); 1341 new_pos--; 1342 } 1343 } else { 1344 for (unsigned int i = start; i < base; i++) 1345 if (info[i].indic_position () == POS_PRE_M) { 1346 buffer->merge_clusters (i, MIN (end, base + 1)); 1347 break; 1348 } 1349 } 1350 } 1351 1352 1353 /* o Reorder reph: 1354 * 1355 * Reph’s original position is always at the beginning of the syllable, 1356 * (i.e. it is not reordered at the character reordering stage). However, 1357 * it will be reordered according to the basic-forms shaping results. 1358 * Possible positions for reph, depending on the script, are; after main, 1359 * before post-base consonant forms, and after post-base consonant forms. 1360 */ 1361 1362 /* Two cases: 1363 * 1364 * - If repha is encoded as a sequence of characters (Ra,H or Ra,H,ZWJ), then 1365 * we should only move it if the sequence ligated to the repha form. 1366 * 1367 * - If repha is encoded separately and in the logical position, we should only 1368 * move it if it did NOT ligate. If it ligated, it's probably the font trying 1369 * to make it work without the reordering. 1370 */ 1371 if (start + 1 < end && 1372 info[start].indic_position() == POS_RA_TO_BECOME_REPH && 1373 ((info[start].indic_category() == OT_Repha) ^ is_a_ligature (info[start]))) 1374 { 1375 unsigned int new_reph_pos; 1376 reph_position_t reph_pos = indic_plan->config->reph_pos; 1377 1378 1379 /* 1. If reph should be positioned after post-base consonant forms, 1380 * proceed to step 5. 1381 */ 1382 if (reph_pos == REPH_POS_AFTER_POST) 1383 { 1384 goto reph_step_5; 1385 } 1386 1387 /* 2. If the reph repositioning class is not after post-base: target 1388 * position is after the first explicit halant glyph between the 1389 * first post-reph consonant and last main consonant. If ZWJ or ZWNJ 1390 * are following this halant, position is moved after it. If such 1391 * position is found, this is the target position. Otherwise, 1392 * proceed to the next step. 1393 * 1394 * Note: in old-implementation fonts, where classifications were 1395 * fixed in shaping engine, there was no case where reph position 1396 * will be found on this step. 1397 */ 1398 { 1399 new_reph_pos = start + 1; 1400 while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos])) 1401 new_reph_pos++; 1402 1403 if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos])) 1404 { 1405 /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */ 1406 if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1])) 1407 new_reph_pos++; 1408 goto reph_move; 1409 } 1410 } 1411 1412 /* 3. If reph should be repositioned after the main consonant: find the 1413 * first consonant not ligated with main, or find the first 1414 * consonant that is not a potential pre-base reordering Ra. 1415 */ 1416 if (reph_pos == REPH_POS_AFTER_MAIN) 1417 { 1418 new_reph_pos = base; 1419 while (new_reph_pos + 1 < end && info[new_reph_pos + 1].indic_position() <= POS_AFTER_MAIN) 1420 new_reph_pos++; 1421 if (new_reph_pos < end) 1422 goto reph_move; 1423 } 1424 1425 /* 4. If reph should be positioned before post-base consonant, find 1426 * first post-base classified consonant not ligated with main. If no 1427 * consonant is found, the target position should be before the 1428 * first matra, syllable modifier sign or vedic sign. 1429 */ 1430 /* This is our take on what step 4 is trying to say (and failing, BADLY). */ 1431 if (reph_pos == REPH_POS_AFTER_SUB) 1432 { 1433 new_reph_pos = base; 1434 while (new_reph_pos < end && 1435 !( FLAG (info[new_reph_pos + 1].indic_position()) & (FLAG (POS_POST_C) | FLAG (POS_AFTER_POST) | FLAG (POS_SMVD)))) 1436 new_reph_pos++; 1437 if (new_reph_pos < end) 1438 goto reph_move; 1439 } 1440 1441 /* 5. If no consonant is found in steps 3 or 4, move reph to a position 1442 * immediately before the first post-base matra, syllable modifier 1443 * sign or vedic sign that has a reordering class after the intended 1444 * reph position. For example, if the reordering position for reph 1445 * is post-main, it will skip above-base matras that also have a 1446 * post-main position. 1447 */ 1448 reph_step_5: 1449 { 1450 /* Copied from step 2. */ 1451 new_reph_pos = start + 1; 1452 while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos])) 1453 new_reph_pos++; 1454 1455 if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos])) 1456 { 1457 /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */ 1458 if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1])) 1459 new_reph_pos++; 1460 goto reph_move; 1461 } 1462 } 1463 1464 /* 6. Otherwise, reorder reph to the end of the syllable. 1465 */ 1466 { 1467 new_reph_pos = end - 1; 1468 while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD) 1469 new_reph_pos--; 1470 1471 /* 1472 * If the Reph is to be ending up after a Matra,Halant sequence, 1473 * position it before that Halant so it can interact with the Matra. 1474 * However, if it's a plain Consonant,Halant we shouldn't do that. 1475 * Uniscribe doesn't do this. 1476 * TEST: U+0930,U+094D,U+0915,U+094B,U+094D 1477 */ 1478 if (!hb_options ().uniscribe_bug_compatible && 1479 unlikely (is_halant_or_coeng (info[new_reph_pos]))) { 1480 for (unsigned int i = base + 1; i < new_reph_pos; i++) 1481 if (info[i].indic_category() == OT_M) { 1482 /* Ok, got it. */ 1483 new_reph_pos--; 1484 } 1485 } 1486 goto reph_move; 1487 } 1488 1489 reph_move: 1490 { 1491 buffer->merge_clusters (start, new_reph_pos + 1); 1492 1493 /* Move */ 1494 hb_glyph_info_t reph = info[start]; 1495 memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0])); 1496 info[new_reph_pos] = reph; 1497 if (start < base && base <= new_reph_pos) 1498 base--; 1499 } 1500 } 1501 1502 1503 /* o Reorder pre-base reordering consonants: 1504 * 1505 * If a pre-base reordering consonant is found, reorder it according to 1506 * the following rules: 1507 */ 1508 1509 if (indic_plan->mask_array[PREF] && base + 1 < end) /* Otherwise there can't be any pre-base reordering Ra. */ 1510 { 1511 for (unsigned int i = base + 1; i < end; i++) 1512 if ((info[i].mask & indic_plan->mask_array[PREF]) != 0) 1513 { 1514 /* 1. Only reorder a glyph produced by substitution during application 1515 * of the <pref> feature. (Note that a font may shape a Ra consonant with 1516 * the feature generally but block it in certain contexts.) 1517 */ 1518 if (i + 1 == end || (info[i + 1].mask & indic_plan->mask_array[PREF]) == 0) 1519 { 1520 /* 1521 * 2. Try to find a target position the same way as for pre-base matra. 1522 * If it is found, reorder pre-base consonant glyph. 1523 * 1524 * 3. If position is not found, reorder immediately before main 1525 * consonant. 1526 */ 1527 1528 unsigned int new_pos = base; 1529 /* Malayalam / Tamil do not have "half" forms or explicit virama forms. 1530 * The glyphs formed by 'half' are Chillus or ligated explicit viramas. 1531 * We want to position matra after them. 1532 */ 1533 if (buffer->props.script != HB_SCRIPT_MALAYALAM && buffer->props.script != HB_SCRIPT_TAMIL) 1534 { 1535 while (new_pos > start && 1536 !(is_one_of (info[new_pos - 1], FLAG(OT_M) | HALANT_OR_COENG_FLAGS))) 1537 new_pos--; 1538 1539 /* In Khmer coeng model, a V,Ra can go *after* matras. If it goes after a 1540 * split matra, it should be reordered to *before* the left part of such matra. */ 1541 if (new_pos > start && info[new_pos - 1].indic_category() == OT_M) 1542 { 1543 unsigned int old_pos = i; 1544 for (unsigned int i = base + 1; i < old_pos; i++) 1545 if (info[i].indic_category() == OT_M) 1546 { 1547 new_pos--; 1548 break; 1549 } 1550 } 1551 } 1552 1553 if (new_pos > start && is_halant_or_coeng (info[new_pos - 1])) 1554 { 1555 /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */ 1556 if (new_pos < end && is_joiner (info[new_pos])) 1557 new_pos++; 1558 } 1559 1560 { 1561 unsigned int old_pos = i; 1562 buffer->merge_clusters (new_pos, old_pos + 1); 1563 hb_glyph_info_t tmp = info[old_pos]; 1564 memmove (&info[new_pos + 1], &info[new_pos], (old_pos - new_pos) * sizeof (info[0])); 1565 info[new_pos] = tmp; 1566 if (new_pos <= base && base < old_pos) 1567 base++; 1568 } 1569 } 1570 1571 break; 1572 } 1573 } 1574 1575 1576 /* Apply 'init' to the Left Matra if it's a word start. */ 1577 if (info[start].indic_position () == POS_PRE_M && 1578 (!start || 1579 !(FLAG (_hb_glyph_info_get_general_category (&info[start - 1])) & 1580 FLAG_RANGE (HB_UNICODE_GENERAL_CATEGORY_FORMAT, HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))) 1581 info[start].mask |= indic_plan->mask_array[INIT]; 1582 1583 1584 /* 1585 * Finish off the clusters and go home! 1586 */ 1587 if (hb_options ().uniscribe_bug_compatible) 1588 { 1589 switch ((hb_tag_t) plan->props.script) 1590 { 1591 case HB_SCRIPT_TAMIL: 1592 case HB_SCRIPT_SINHALA: 1593 break; 1594 1595 default: 1596 /* Uniscribe merges the entire cluster... Except for Tamil & Sinhala. 1597 * This means, half forms are submerged into the main consonants cluster. 1598 * This is unnecessary, and makes cursor positioning harder, but that's what 1599 * Uniscribe does. */ 1600 buffer->merge_clusters (start, end); 1601 break; 1602 } 1603 } 1604} 1605 1606 1607static void 1608final_reordering (const hb_ot_shape_plan_t *plan, 1609 hb_font_t *font HB_UNUSED, 1610 hb_buffer_t *buffer) 1611{ 1612 unsigned int count = buffer->len; 1613 if (unlikely (!count)) return; 1614 1615 hb_glyph_info_t *info = buffer->info; 1616 unsigned int last = 0; 1617 unsigned int last_syllable = info[0].syllable(); 1618 for (unsigned int i = 1; i < count; i++) 1619 if (last_syllable != info[i].syllable()) { 1620 final_reordering_syllable (plan, buffer, last, i); 1621 last = i; 1622 last_syllable = info[last].syllable(); 1623 } 1624 final_reordering_syllable (plan, buffer, last, count); 1625 1626 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category); 1627 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position); 1628} 1629 1630 1631static void 1632clear_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED, 1633 hb_font_t *font HB_UNUSED, 1634 hb_buffer_t *buffer) 1635{ 1636 hb_glyph_info_t *info = buffer->info; 1637 unsigned int count = buffer->len; 1638 for (unsigned int i = 0; i < count; i++) 1639 info[i].syllable() = 0; 1640} 1641 1642 1643static hb_ot_shape_normalization_mode_t 1644normalization_preference_indic (const hb_segment_properties_t *props HB_UNUSED) 1645{ 1646 return HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT; 1647} 1648 1649static bool 1650decompose_indic (const hb_ot_shape_normalize_context_t *c, 1651 hb_codepoint_t ab, 1652 hb_codepoint_t *a, 1653 hb_codepoint_t *b) 1654{ 1655 switch (ab) 1656 { 1657 /* Don't decompose these. */ 1658 case 0x0931 : return false; 1659 case 0x0B94 : return false; 1660 1661 1662 /* 1663 * Decompose split matras that don't have Unicode decompositions. 1664 */ 1665 1666 case 0x0F77 : *a = 0x0FB2; *b= 0x0F81; return true; 1667 case 0x0F79 : *a = 0x0FB3; *b= 0x0F81; return true; 1668 case 0x17BE : *a = 0x17C1; *b= 0x17BE; return true; 1669 case 0x17BF : *a = 0x17C1; *b= 0x17BF; return true; 1670 case 0x17C0 : *a = 0x17C1; *b= 0x17C0; return true; 1671 case 0x17C4 : *a = 0x17C1; *b= 0x17C4; return true; 1672 case 0x17C5 : *a = 0x17C1; *b= 0x17C5; return true; 1673 case 0x1925 : *a = 0x1920; *b= 0x1923; return true; 1674 case 0x1926 : *a = 0x1920; *b= 0x1924; return true; 1675 case 0x1B3C : *a = 0x1B42; *b= 0x1B3C; return true; 1676 case 0x1112E : *a = 0x11127; *b= 0x11131; return true; 1677 case 0x1112F : *a = 0x11127; *b= 0x11132; return true; 1678#if 0 1679 /* This one has no decomposition in Unicode, but needs no decomposition either. */ 1680 /* case 0x0AC9 : return false; */ 1681 case 0x0B57 : *a = no decomp, -> RIGHT; return true; 1682 case 0x1C29 : *a = no decomp, -> LEFT; return true; 1683 case 0xA9C0 : *a = no decomp, -> RIGHT; return true; 1684 case 0x111BF : *a = no decomp, -> ABOVE; return true; 1685#endif 1686 } 1687 1688 if ((ab == 0x0DDA || hb_in_range<hb_codepoint_t> (ab, 0x0DDC, 0x0DDE))) 1689 { 1690 /* 1691 * Sinhala split matras... Let the fun begin. 1692 * 1693 * These four characters have Unicode decompositions. However, Uniscribe 1694 * decomposes them "Khmer-style", that is, it uses the character itself to 1695 * get the second half. The first half of all four decompositions is always 1696 * U+0DD9. 1697 * 1698 * Now, there are buggy fonts, namely, the widely used lklug.ttf, that are 1699 * broken with Uniscribe. But we need to support them. As such, we only 1700 * do the Uniscribe-style decomposition if the character is transformed into 1701 * its "sec.half" form by the 'pstf' feature. Otherwise, we fall back to 1702 * Unicode decomposition. 1703 * 1704 * Note that we can't unconditionally use Unicode decomposition. That would 1705 * break some other fonts, that are designed to work with Uniscribe, and 1706 * don't have positioning features for the Unicode-style decomposition. 1707 * 1708 * Argh... 1709 * 1710 * The Uniscribe behavior is now documented in the newly published Sinhala 1711 * spec in 2012: 1712 * 1713 * http://www.microsoft.com/typography/OpenTypeDev/sinhala/intro.htm#shaping 1714 */ 1715 1716 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) c->plan->data; 1717 1718 hb_codepoint_t glyph; 1719 1720 if (hb_options ().uniscribe_bug_compatible || 1721 (c->font->get_glyph (ab, 0, &glyph) && 1722 indic_plan->pstf.would_substitute (&glyph, 1, true, c->font->face))) 1723 { 1724 /* Ok, safe to use Uniscribe-style decomposition. */ 1725 *a = 0x0DD9; 1726 *b = ab; 1727 return true; 1728 } 1729 } 1730 1731 return c->unicode->decompose (ab, a, b); 1732} 1733 1734static bool 1735compose_indic (const hb_ot_shape_normalize_context_t *c, 1736 hb_codepoint_t a, 1737 hb_codepoint_t b, 1738 hb_codepoint_t *ab) 1739{ 1740 /* Avoid recomposing split matras. */ 1741 if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (c->unicode->general_category (a))) 1742 return false; 1743 1744 /* Composition-exclusion exceptions that we want to recompose. */ 1745 if (a == 0x09AF && b == 0x09BC) { *ab = 0x09DF; return true; } 1746 1747 return c->unicode->compose (a, b, ab); 1748} 1749 1750 1751const hb_ot_complex_shaper_t _hb_ot_complex_shaper_indic = 1752{ 1753 "indic", 1754 collect_features_indic, 1755 override_features_indic, 1756 data_create_indic, 1757 data_destroy_indic, 1758 NULL, /* preprocess_text */ 1759 normalization_preference_indic, 1760 decompose_indic, 1761 compose_indic, 1762 setup_masks_indic, 1763 HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE, 1764 false, /* fallback_position */ 1765}; 1766