hb-ot-shape-complex-indic.cc revision ae9a5834df477006686421d494b55a1569789327
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 map->add_gsub_pause (clear_syllables); 451} 452 453static void 454override_features_indic (hb_ot_shape_planner_t *plan) 455{ 456 /* Uniscribe does not apply 'kern' in Khmer. */ 457 if (hb_options ().uniscribe_bug_compatible) 458 { 459 switch ((hb_tag_t) plan->props.script) 460 { 461 case HB_SCRIPT_KHMER: 462 plan->map.add_feature (HB_TAG('k','e','r','n'), 0, F_GLOBAL); 463 break; 464 } 465 } 466 467 plan->map.add_feature (HB_TAG('l','i','g','a'), 0, F_GLOBAL); 468} 469 470 471struct would_substitute_feature_t 472{ 473 inline void init (const hb_ot_map_t *map, hb_tag_t feature_tag) 474 { 475 map->get_stage_lookups (0/*GSUB*/, 476 map->get_feature_stage (0/*GSUB*/, feature_tag), 477 &lookups, &count); 478 } 479 480 inline bool would_substitute (const hb_codepoint_t *glyphs, 481 unsigned int glyphs_count, 482 bool zero_context, 483 hb_face_t *face) const 484 { 485 for (unsigned int i = 0; i < count; i++) 486 if (hb_ot_layout_lookup_would_substitute_fast (face, lookups[i].index, glyphs, glyphs_count, zero_context)) 487 return true; 488 return false; 489 } 490 491 private: 492 const hb_ot_map_t::lookup_map_t *lookups; 493 unsigned int count; 494}; 495 496struct indic_shape_plan_t 497{ 498 ASSERT_POD (); 499 500 inline bool get_virama_glyph (hb_font_t *font, hb_codepoint_t *pglyph) const 501 { 502 hb_codepoint_t glyph = virama_glyph; 503 if (unlikely (virama_glyph == (hb_codepoint_t) -1)) 504 { 505 if (!config->virama || !font->get_glyph (config->virama, 0, &glyph)) 506 glyph = 0; 507 /* Technically speaking, the spec says we should apply 'locl' to virama too. 508 * Maybe one day... */ 509 510 /* Our get_glyph() function needs a font, so we can't get the virama glyph 511 * during shape planning... Instead, overwrite it here. It's safe. Don't worry! */ 512 (const_cast<indic_shape_plan_t *> (this))->virama_glyph = glyph; 513 } 514 515 *pglyph = glyph; 516 return glyph != 0; 517 } 518 519 const indic_config_t *config; 520 521 bool is_old_spec; 522 hb_codepoint_t virama_glyph; 523 524 would_substitute_feature_t rphf; 525 would_substitute_feature_t pref; 526 would_substitute_feature_t blwf; 527 would_substitute_feature_t pstf; 528 529 hb_mask_t mask_array[INDIC_NUM_FEATURES]; 530}; 531 532static void * 533data_create_indic (const hb_ot_shape_plan_t *plan) 534{ 535 indic_shape_plan_t *indic_plan = (indic_shape_plan_t *) calloc (1, sizeof (indic_shape_plan_t)); 536 if (unlikely (!indic_plan)) 537 return NULL; 538 539 indic_plan->config = &indic_configs[0]; 540 for (unsigned int i = 1; i < ARRAY_LENGTH (indic_configs); i++) 541 if (plan->props.script == indic_configs[i].script) { 542 indic_plan->config = &indic_configs[i]; 543 break; 544 } 545 546 indic_plan->is_old_spec = indic_plan->config->has_old_spec && ((plan->map.chosen_script[0] & 0x000000FF) != '2'); 547 indic_plan->virama_glyph = (hb_codepoint_t) -1; 548 549 indic_plan->rphf.init (&plan->map, HB_TAG('r','p','h','f')); 550 indic_plan->pref.init (&plan->map, HB_TAG('p','r','e','f')); 551 indic_plan->blwf.init (&plan->map, HB_TAG('b','l','w','f')); 552 indic_plan->pstf.init (&plan->map, HB_TAG('p','s','t','f')); 553 554 for (unsigned int i = 0; i < ARRAY_LENGTH (indic_plan->mask_array); i++) 555 indic_plan->mask_array[i] = (indic_features[i].flags & F_GLOBAL) ? 556 0 : plan->map.get_1_mask (indic_features[i].tag); 557 558 return indic_plan; 559} 560 561static void 562data_destroy_indic (void *data) 563{ 564 free (data); 565} 566 567static indic_position_t 568consonant_position_from_face (const indic_shape_plan_t *indic_plan, 569 const hb_codepoint_t glyphs[2], 570 hb_face_t *face) 571{ 572 /* For old-spec, the order of glyphs is Consonant,Virama, 573 * whereas for new-spec, it's Virama,Consonant. However, 574 * some broken fonts (like Free Sans) simply copied lookups 575 * from old-spec to new-spec without modification. 576 * And oddly enough, Uniscribe seems to respect those lookups. 577 * Eg. in the sequence U+0924,U+094D,U+0930, Uniscribe finds 578 * base at 0. The font however, only has lookups matching 579 * 930,94D in 'blwf', not the expected 94D,930 (with new-spec 580 * table). As such, we simply match both sequences. Seems 581 * to work. */ 582 bool zero_context = indic_plan->is_old_spec ? false : true; 583 hb_codepoint_t glyphs_r[2] = {glyphs[1], glyphs[0]}; 584 if (indic_plan->blwf.would_substitute (glyphs , 2, zero_context, face) || 585 indic_plan->blwf.would_substitute (glyphs_r, 2, zero_context, face)) 586 return POS_BELOW_C; 587 if (indic_plan->pstf.would_substitute (glyphs , 2, zero_context, face) || 588 indic_plan->pstf.would_substitute (glyphs_r, 2, zero_context, face)) 589 return POS_POST_C; 590 if (indic_plan->pref.would_substitute (glyphs , 2, zero_context, face) || 591 indic_plan->pref.would_substitute (glyphs_r, 2, zero_context, face)) 592 return POS_POST_C; 593 return POS_BASE_C; 594} 595 596 597enum syllable_type_t { 598 consonant_syllable, 599 vowel_syllable, 600 standalone_cluster, 601 avagraha_cluster, 602 broken_cluster, 603 non_indic_cluster, 604}; 605 606#include "hb-ot-shape-complex-indic-machine.hh" 607 608 609static void 610setup_masks_indic (const hb_ot_shape_plan_t *plan HB_UNUSED, 611 hb_buffer_t *buffer, 612 hb_font_t *font HB_UNUSED) 613{ 614 HB_BUFFER_ALLOCATE_VAR (buffer, indic_category); 615 HB_BUFFER_ALLOCATE_VAR (buffer, indic_position); 616 617 /* We cannot setup masks here. We save information about characters 618 * and setup masks later on in a pause-callback. */ 619 620 unsigned int count = buffer->len; 621 for (unsigned int i = 0; i < count; i++) 622 set_indic_properties (buffer->info[i]); 623} 624 625static void 626setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED, 627 hb_font_t *font HB_UNUSED, 628 hb_buffer_t *buffer) 629{ 630 find_syllables (buffer); 631} 632 633static int 634compare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb) 635{ 636 int a = pa->indic_position(); 637 int b = pb->indic_position(); 638 639 return a < b ? -1 : a == b ? 0 : +1; 640} 641 642 643 644static void 645update_consonant_positions (const hb_ot_shape_plan_t *plan, 646 hb_font_t *font, 647 hb_buffer_t *buffer) 648{ 649 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data; 650 651 hb_codepoint_t glyphs[2]; 652 if (indic_plan->get_virama_glyph (font, &glyphs[0])) 653 { 654 hb_face_t *face = font->face; 655 unsigned int count = buffer->len; 656 for (unsigned int i = 0; i < count; i++) 657 if (buffer->info[i].indic_position() == POS_BASE_C) { 658 glyphs[1] = buffer->info[i].codepoint; 659 buffer->info[i].indic_position() = consonant_position_from_face (indic_plan, glyphs, face); 660 } 661 } 662} 663 664 665/* Rules from: 666 * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */ 667 668static void 669initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan, 670 hb_face_t *face, 671 hb_buffer_t *buffer, 672 unsigned int start, unsigned int end) 673{ 674 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data; 675 hb_glyph_info_t *info = buffer->info; 676 677 678 /* 1. Find base consonant: 679 * 680 * The shaping engine finds the base consonant of the syllable, using the 681 * following algorithm: starting from the end of the syllable, move backwards 682 * until a consonant is found that does not have a below-base or post-base 683 * form (post-base forms have to follow below-base forms), or that is not a 684 * pre-base reordering Ra, or arrive at the first consonant. The consonant 685 * stopped at will be the base. 686 * 687 * o If the syllable starts with Ra + Halant (in a script that has Reph) 688 * and has more than one consonant, Ra is excluded from candidates for 689 * base consonants. 690 */ 691 692 unsigned int base = end; 693 bool has_reph = false; 694 695 { 696 /* -> If the syllable starts with Ra + Halant (in a script that has Reph) 697 * and has more than one consonant, Ra is excluded from candidates for 698 * base consonants. */ 699 unsigned int limit = start; 700 if (indic_plan->mask_array[RPHF] && 701 start + 3 <= end && 702 ( 703 (indic_plan->config->reph_mode == REPH_MODE_IMPLICIT && !is_joiner (info[start + 2])) || 704 (indic_plan->config->reph_mode == REPH_MODE_EXPLICIT && info[start + 2].indic_category() == OT_ZWJ) 705 )) 706 { 707 /* See if it matches the 'rphf' feature. */ 708 hb_codepoint_t glyphs[2] = {info[start].codepoint, info[start + 1].codepoint}; 709 if (indic_plan->rphf.would_substitute (glyphs, ARRAY_LENGTH (glyphs), true, face)) 710 { 711 limit += 2; 712 while (limit < end && is_joiner (info[limit])) 713 limit++; 714 base = start; 715 has_reph = true; 716 } 717 } else if (indic_plan->config->reph_mode == REPH_MODE_LOG_REPHA && info[start].indic_category() == OT_Repha) 718 { 719 limit += 1; 720 while (limit < end && is_joiner (info[limit])) 721 limit++; 722 base = start; 723 has_reph = true; 724 } 725 726 switch (indic_plan->config->base_pos) 727 { 728 default: 729 assert (false); 730 /* fallthrough */ 731 732 case BASE_POS_LAST: 733 { 734 /* -> starting from the end of the syllable, move backwards */ 735 unsigned int i = end; 736 bool seen_below = false; 737 do { 738 i--; 739 /* -> until a consonant is found */ 740 if (is_consonant (info[i])) 741 { 742 /* -> that does not have a below-base or post-base form 743 * (post-base forms have to follow below-base forms), */ 744 if (info[i].indic_position() != POS_BELOW_C && 745 (info[i].indic_position() != POS_POST_C || seen_below)) 746 { 747 base = i; 748 break; 749 } 750 if (info[i].indic_position() == POS_BELOW_C) 751 seen_below = true; 752 753 /* -> or that is not a pre-base reordering Ra, 754 * 755 * IMPLEMENTATION NOTES: 756 * 757 * Our pre-base reordering Ra's are marked POS_POST_C, so will be skipped 758 * by the logic above already. 759 */ 760 761 /* -> or arrive at the first consonant. The consonant stopped at will 762 * be the base. */ 763 base = i; 764 } 765 else 766 { 767 /* A ZWJ after a Halant stops the base search, and requests an explicit 768 * half form. 769 * A ZWJ before a Halant, requests a subjoined form instead, and hence 770 * search continues. This is particularly important for Bengali 771 * sequence Ra,H,Ya that should form Ya-Phalaa by subjoining Ya. */ 772 if (start < i && 773 info[i].indic_category() == OT_ZWJ && 774 info[i - 1].indic_category() == OT_H) 775 break; 776 } 777 } while (i > limit); 778 } 779 break; 780 781 case BASE_POS_FIRST: 782 { 783 /* In scripts without half forms (eg. Khmer), the first consonant is always the base. */ 784 785 if (!has_reph) 786 base = limit; 787 788 /* Find the last base consonant that is not blocked by ZWJ. If there is 789 * a ZWJ right before a base consonant, that would request a subjoined form. */ 790 for (unsigned int i = limit; i < end; i++) 791 if (is_consonant (info[i]) && info[i].indic_position() == POS_BASE_C) 792 { 793 if (limit < i && info[i - 1].indic_category() == OT_ZWJ) 794 break; 795 else 796 base = i; 797 } 798 799 /* Mark all subsequent consonants as below. */ 800 for (unsigned int i = base + 1; i < end; i++) 801 if (is_consonant (info[i]) && info[i].indic_position() == POS_BASE_C) 802 info[i].indic_position() = POS_BELOW_C; 803 } 804 break; 805 } 806 807 /* -> If the syllable starts with Ra + Halant (in a script that has Reph) 808 * and has more than one consonant, Ra is excluded from candidates for 809 * base consonants. 810 * 811 * Only do this for unforced Reph. (ie. not for Ra,H,ZWJ. */ 812 if (has_reph && base == start && limit - base <= 2) { 813 /* Have no other consonant, so Reph is not formed and Ra becomes base. */ 814 has_reph = false; 815 } 816 } 817 818 819 /* 2. Decompose and reorder Matras: 820 * 821 * Each matra and any syllable modifier sign in the cluster are moved to the 822 * appropriate position relative to the consonant(s) in the cluster. The 823 * shaping engine decomposes two- or three-part matras into their constituent 824 * parts before any repositioning. Matra characters are classified by which 825 * consonant in a conjunct they have affinity for and are reordered to the 826 * following positions: 827 * 828 * o Before first half form in the syllable 829 * o After subjoined consonants 830 * o After post-form consonant 831 * o After main consonant (for above marks) 832 * 833 * IMPLEMENTATION NOTES: 834 * 835 * The normalize() routine has already decomposed matras for us, so we don't 836 * need to worry about that. 837 */ 838 839 840 /* 3. Reorder marks to canonical order: 841 * 842 * Adjacent nukta and halant or nukta and vedic sign are always repositioned 843 * if necessary, so that the nukta is first. 844 * 845 * IMPLEMENTATION NOTES: 846 * 847 * We don't need to do this: the normalize() routine already did this for us. 848 */ 849 850 851 /* Reorder characters */ 852 853 for (unsigned int i = start; i < base; i++) 854 info[i].indic_position() = MIN (POS_PRE_C, (indic_position_t) info[i].indic_position()); 855 856 if (base < end) 857 info[base].indic_position() = POS_BASE_C; 858 859 /* Mark final consonants. A final consonant is one appearing after a matra, 860 * like in Khmer. */ 861 for (unsigned int i = base + 1; i < end; i++) 862 if (info[i].indic_category() == OT_M) { 863 for (unsigned int j = i + 1; j < end; j++) 864 if (is_consonant (info[j])) { 865 info[j].indic_position() = POS_FINAL_C; 866 break; 867 } 868 break; 869 } 870 871 /* Handle beginning Ra */ 872 if (has_reph) 873 info[start].indic_position() = POS_RA_TO_BECOME_REPH; 874 875 /* For old-style Indic script tags, move the first post-base Halant after 876 * last consonant. Only do this if there is *not* a Halant after last 877 * consonant. Otherwise it becomes messy. */ 878 if (indic_plan->is_old_spec) { 879 for (unsigned int i = base + 1; i < end; i++) 880 if (info[i].indic_category() == OT_H) { 881 unsigned int j; 882 for (j = end - 1; j > i; j--) 883 if (is_consonant (info[j]) || info[j].indic_category() == OT_H) 884 break; 885 if (info[j].indic_category() != OT_H && j > i) { 886 /* Move Halant to after last consonant. */ 887 hb_glyph_info_t t = info[i]; 888 memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0])); 889 info[j] = t; 890 } 891 break; 892 } 893 } 894 895 /* Attach misc marks to previous char to move with them. */ 896 { 897 indic_position_t last_pos = POS_START; 898 for (unsigned int i = start; i < end; i++) 899 { 900 if ((FLAG (info[i].indic_category()) & (JOINER_FLAGS | FLAG (OT_N) | FLAG (OT_RS) | HALANT_OR_COENG_FLAGS))) 901 { 902 info[i].indic_position() = last_pos; 903 if (unlikely (info[i].indic_category() == OT_H && 904 info[i].indic_position() == POS_PRE_M)) 905 { 906 /* 907 * Uniscribe doesn't move the Halant with Left Matra. 908 * TEST: U+092B,U+093F,U+094DE 909 * We follow. This is important for the Sinhala 910 * U+0DDA split matra since it decomposes to U+0DD9,U+0DCA 911 * where U+0DD9 is a left matra and U+0DCA is the virama. 912 * We don't want to move the virama with the left matra. 913 * TEST: U+0D9A,U+0DDA 914 */ 915 for (unsigned int j = i; j > start; j--) 916 if (info[j - 1].indic_position() != POS_PRE_M) { 917 info[i].indic_position() = info[j - 1].indic_position(); 918 break; 919 } 920 } 921 } else if (info[i].indic_position() != POS_SMVD) { 922 last_pos = (indic_position_t) info[i].indic_position(); 923 } 924 } 925 } 926 /* Re-attach ZWJ, ZWNJ, and halant to next char, for after-base consonants. */ 927 { 928 unsigned int last_halant = end; 929 for (unsigned int i = base + 1; i < end; i++) 930 if (is_halant_or_coeng (info[i])) 931 last_halant = i; 932 else if (is_consonant (info[i])) { 933 for (unsigned int j = last_halant; j < i; j++) 934 if (info[j].indic_position() != POS_SMVD) 935 info[j].indic_position() = info[i].indic_position(); 936 last_halant = end; 937 } 938 } 939 940 941 { 942 /* Use syllable() for sort accounting temporarily. */ 943 unsigned int syllable = info[start].syllable(); 944 for (unsigned int i = start; i < end; i++) 945 info[i].syllable() = i - start; 946 947 /* Sit tight, rock 'n roll! */ 948 hb_bubble_sort (info + start, end - start, compare_indic_order); 949 /* Find base again */ 950 base = end; 951 for (unsigned int i = start; i < end; i++) 952 if (info[i].indic_position() == POS_BASE_C) 953 { 954 base = i; 955 break; 956 } 957 /* Things are out-of-control for post base positions, they may shuffle 958 * around like crazy. In old-spec mode, we move halants around, so in 959 * that case merge all clusters after base. Otherwise, check the sort 960 * order and merge as needed. 961 * For pre-base stuff, we handle cluster issues in final reordering. */ 962 if (indic_plan->is_old_spec || end - base > 127) 963 buffer->merge_clusters (base, end); 964 else 965 { 966 /* Note! syllable() is a one-byte field. */ 967 for (unsigned int i = base; i < end; i++) 968 if (info[i].syllable() != 255) 969 { 970 unsigned int max = i; 971 unsigned int j = start + info[i].syllable(); 972 while (j != i) 973 { 974 max = MAX (max, j); 975 unsigned int next = start + info[j].syllable(); 976 info[j].syllable() = 255; /* So we don't process j later again. */ 977 j = next; 978 } 979 if (i != max) 980 buffer->merge_clusters (i, max + 1); 981 } 982 } 983 984 /* Put syllable back in. */ 985 for (unsigned int i = start; i < end; i++) 986 info[i].syllable() = syllable; 987 } 988 989 /* Setup masks now */ 990 991 { 992 hb_mask_t mask; 993 994 /* Reph */ 995 for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++) 996 info[i].mask |= indic_plan->mask_array[RPHF]; 997 998 /* Pre-base */ 999 mask = indic_plan->mask_array[HALF]; 1000 if (!indic_plan->is_old_spec && 1001 indic_plan->config->blwf_mode == BLWF_MODE_PRE_AND_POST) 1002 mask |= indic_plan->mask_array[BLWF]; 1003 for (unsigned int i = start; i < base; i++) 1004 info[i].mask |= mask; 1005 /* Base */ 1006 mask = 0; 1007 if (base < end) 1008 info[base].mask |= mask; 1009 /* Post-base */ 1010 mask = indic_plan->mask_array[BLWF] | indic_plan->mask_array[ABVF] | indic_plan->mask_array[PSTF]; 1011 for (unsigned int i = base + 1; i < end; i++) 1012 info[i].mask |= mask; 1013 } 1014 1015 if (indic_plan->is_old_spec && 1016 buffer->props.script == HB_SCRIPT_DEVANAGARI) 1017 { 1018 /* Old-spec eye-lash Ra needs special handling. From the 1019 * spec: 1020 * 1021 * "The feature 'below-base form' is applied to consonants 1022 * having below-base forms and following the base consonant. 1023 * The exception is vattu, which may appear below half forms 1024 * as well as below the base glyph. The feature 'below-base 1025 * form' will be applied to all such occurrences of Ra as well." 1026 * 1027 * Test case: U+0924,U+094D,U+0930,U+094d,U+0915 1028 * with Sanskrit 2003 font. 1029 * 1030 * However, note that Ra,Halant,ZWJ is the correct way to 1031 * request eyelash form of Ra, so we wouldbn't inhibit it 1032 * in that sequence. 1033 * 1034 * Test case: U+0924,U+094D,U+0930,U+094d,U+200D,U+0915 1035 */ 1036 for (unsigned int i = start; i + 1 < base; i++) 1037 if (info[i ].indic_category() == OT_Ra && 1038 info[i+1].indic_category() == OT_H && 1039 (i + 2 == base || 1040 info[i+2].indic_category() != OT_ZWJ)) 1041 { 1042 info[i ].mask |= indic_plan->mask_array[BLWF]; 1043 info[i+1].mask |= indic_plan->mask_array[BLWF]; 1044 } 1045 } 1046 1047 if (indic_plan->mask_array[PREF] && base + 2 < end) 1048 { 1049 /* Find a Halant,Ra sequence and mark it for pre-base reordering processing. */ 1050 for (unsigned int i = base + 1; i + 1 < end; i++) { 1051 hb_codepoint_t glyphs[2] = {info[i].codepoint, info[i + 1].codepoint}; 1052 if (indic_plan->pref.would_substitute (glyphs, ARRAY_LENGTH (glyphs), true, face)) 1053 { 1054 info[i++].mask |= indic_plan->mask_array[PREF]; 1055 info[i++].mask |= indic_plan->mask_array[PREF]; 1056 1057 /* Mark the subsequent stuff with 'cfar'. Used in Khmer. 1058 * Read the feature spec. 1059 * This allows distinguishing the following cases with MS Khmer fonts: 1060 * U+1784,U+17D2,U+179A,U+17D2,U+1782 1061 * U+1784,U+17D2,U+1782,U+17D2,U+179A 1062 */ 1063 for (; i < end; i++) 1064 info[i].mask |= indic_plan->mask_array[CFAR]; 1065 1066 break; 1067 } 1068 } 1069 } 1070 1071 /* Apply ZWJ/ZWNJ effects */ 1072 for (unsigned int i = start + 1; i < end; i++) 1073 if (is_joiner (info[i])) { 1074 bool non_joiner = info[i].indic_category() == OT_ZWNJ; 1075 unsigned int j = i; 1076 1077 do { 1078 j--; 1079 1080 /* ZWJ/ZWNJ should disable CJCT. They do that by simply 1081 * being there, since we don't skip them for the CJCT 1082 * feature (ie. F_MANUAL_ZWJ) */ 1083 1084 /* A ZWNJ disables HALF. */ 1085 if (non_joiner) 1086 info[j].mask &= ~indic_plan->mask_array[HALF]; 1087 1088 } while (j > start && !is_consonant (info[j])); 1089 } 1090} 1091 1092 1093static void 1094initial_reordering_vowel_syllable (const hb_ot_shape_plan_t *plan, 1095 hb_face_t *face, 1096 hb_buffer_t *buffer, 1097 unsigned int start, unsigned int end) 1098{ 1099 /* We made the vowels look like consonants. So let's call the consonant logic! */ 1100 initial_reordering_consonant_syllable (plan, face, buffer, start, end); 1101} 1102 1103static void 1104initial_reordering_standalone_cluster (const hb_ot_shape_plan_t *plan, 1105 hb_face_t *face, 1106 hb_buffer_t *buffer, 1107 unsigned int start, unsigned int end) 1108{ 1109 /* We treat NBSP/dotted-circle as if they are consonants, so we should just chain. 1110 * Only if not in compatibility mode that is... */ 1111 1112 if (hb_options ().uniscribe_bug_compatible) 1113 { 1114 /* For dotted-circle, this is what Uniscribe does: 1115 * If dotted-circle is the last glyph, it just does nothing. 1116 * Ie. It doesn't form Reph. */ 1117 if (buffer->info[end - 1].indic_category() == OT_DOTTEDCIRCLE) 1118 return; 1119 } 1120 1121 initial_reordering_consonant_syllable (plan, face, buffer, start, end); 1122} 1123 1124static void 1125initial_reordering_broken_cluster (const hb_ot_shape_plan_t *plan, 1126 hb_face_t *face, 1127 hb_buffer_t *buffer, 1128 unsigned int start, unsigned int end) 1129{ 1130 /* We already inserted dotted-circles, so just call the standalone_cluster. */ 1131 initial_reordering_standalone_cluster (plan, face, buffer, start, end); 1132} 1133 1134static void 1135initial_reordering_avagraha_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED, 1136 hb_face_t *face HB_UNUSED, 1137 hb_buffer_t *buffer HB_UNUSED, 1138 unsigned int start HB_UNUSED, unsigned int end HB_UNUSED) 1139{ 1140 /* Nothing to do right now. If we ever switch to using the output 1141 * buffer in the reordering process, we'd need to next_glyph() here. */ 1142} 1143 1144static void 1145initial_reordering_non_indic_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED, 1146 hb_face_t *face HB_UNUSED, 1147 hb_buffer_t *buffer HB_UNUSED, 1148 unsigned int start HB_UNUSED, unsigned int end HB_UNUSED) 1149{ 1150 /* Nothing to do right now. If we ever switch to using the output 1151 * buffer in the reordering process, we'd need to next_glyph() here. */ 1152} 1153 1154 1155static void 1156initial_reordering_syllable (const hb_ot_shape_plan_t *plan, 1157 hb_face_t *face, 1158 hb_buffer_t *buffer, 1159 unsigned int start, unsigned int end) 1160{ 1161 syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F); 1162 switch (syllable_type) { 1163 case consonant_syllable: initial_reordering_consonant_syllable (plan, face, buffer, start, end); return; 1164 case vowel_syllable: initial_reordering_vowel_syllable (plan, face, buffer, start, end); return; 1165 case standalone_cluster: initial_reordering_standalone_cluster (plan, face, buffer, start, end); return; 1166 case avagraha_cluster: initial_reordering_avagraha_cluster (plan, face, buffer, start, end); return; 1167 case broken_cluster: initial_reordering_broken_cluster (plan, face, buffer, start, end); return; 1168 case non_indic_cluster: initial_reordering_non_indic_cluster (plan, face, buffer, start, end); return; 1169 } 1170} 1171 1172static inline void 1173insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED, 1174 hb_font_t *font, 1175 hb_buffer_t *buffer) 1176{ 1177 /* Note: This loop is extra overhead, but should not be measurable. */ 1178 bool has_broken_syllables = false; 1179 unsigned int count = buffer->len; 1180 for (unsigned int i = 0; i < count; i++) 1181 if ((buffer->info[i].syllable() & 0x0F) == broken_cluster) { 1182 has_broken_syllables = true; 1183 break; 1184 } 1185 if (likely (!has_broken_syllables)) 1186 return; 1187 1188 1189 hb_codepoint_t dottedcircle_glyph; 1190 if (!font->get_glyph (0x25CC, 0, &dottedcircle_glyph)) 1191 return; 1192 1193 hb_glyph_info_t dottedcircle = {0}; 1194 dottedcircle.codepoint = 0x25CC; 1195 set_indic_properties (dottedcircle); 1196 dottedcircle.codepoint = dottedcircle_glyph; 1197 1198 buffer->clear_output (); 1199 1200 buffer->idx = 0; 1201 unsigned int last_syllable = 0; 1202 while (buffer->idx < buffer->len) 1203 { 1204 unsigned int syllable = buffer->cur().syllable(); 1205 syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F); 1206 if (unlikely (last_syllable != syllable && syllable_type == broken_cluster)) 1207 { 1208 last_syllable = syllable; 1209 1210 hb_glyph_info_t info = dottedcircle; 1211 info.cluster = buffer->cur().cluster; 1212 info.mask = buffer->cur().mask; 1213 info.syllable() = buffer->cur().syllable(); 1214 1215 /* Insert dottedcircle after possible Repha. */ 1216 while (buffer->idx < buffer->len && 1217 last_syllable == buffer->cur().syllable() && 1218 buffer->cur().indic_category() == OT_Repha) 1219 buffer->next_glyph (); 1220 1221 buffer->output_info (info); 1222 } 1223 else 1224 buffer->next_glyph (); 1225 } 1226 1227 buffer->swap_buffers (); 1228} 1229 1230static void 1231initial_reordering (const hb_ot_shape_plan_t *plan, 1232 hb_font_t *font, 1233 hb_buffer_t *buffer) 1234{ 1235 update_consonant_positions (plan, font, buffer); 1236 insert_dotted_circles (plan, font, buffer); 1237 1238 hb_glyph_info_t *info = buffer->info; 1239 unsigned int count = buffer->len; 1240 if (unlikely (!count)) return; 1241 unsigned int last = 0; 1242 unsigned int last_syllable = info[0].syllable(); 1243 for (unsigned int i = 1; i < count; i++) 1244 if (last_syllable != info[i].syllable()) { 1245 initial_reordering_syllable (plan, font->face, buffer, last, i); 1246 last = i; 1247 last_syllable = info[last].syllable(); 1248 } 1249 initial_reordering_syllable (plan, font->face, buffer, last, count); 1250} 1251 1252static void 1253final_reordering_syllable (const hb_ot_shape_plan_t *plan, 1254 hb_buffer_t *buffer, 1255 unsigned int start, unsigned int end) 1256{ 1257 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data; 1258 hb_glyph_info_t *info = buffer->info; 1259 1260 /* 4. Final reordering: 1261 * 1262 * After the localized forms and basic shaping forms GSUB features have been 1263 * applied (see below), the shaping engine performs some final glyph 1264 * reordering before applying all the remaining font features to the entire 1265 * cluster. 1266 */ 1267 1268 /* Find base again */ 1269 unsigned int base; 1270 for (base = start; base < end; base++) 1271 if (info[base].indic_position() >= POS_BASE_C) { 1272 if (start < base && info[base].indic_position() > POS_BASE_C) 1273 base--; 1274 break; 1275 } 1276 if (base == end && start < base && 1277 info[base - 1].indic_category() != OT_ZWJ) 1278 base--; 1279 while (start < base && 1280 (info[base].indic_category() == OT_H || 1281 info[base].indic_category() == OT_N)) 1282 base--; 1283 1284 1285 /* o Reorder matras: 1286 * 1287 * If a pre-base matra character had been reordered before applying basic 1288 * features, the glyph can be moved closer to the main consonant based on 1289 * whether half-forms had been formed. Actual position for the matra is 1290 * defined as “after last standalone halant glyph, after initial matra 1291 * position and before the main consonant”. If ZWJ or ZWNJ follow this 1292 * halant, position is moved after it. 1293 */ 1294 1295 if (start + 1 < end && start < base) /* Otherwise there can't be any pre-base matra characters. */ 1296 { 1297 /* If we lost track of base, alas, position before last thingy. */ 1298 unsigned int new_pos = base == end ? base - 2 : base - 1; 1299 1300 /* Malayalam / Tamil do not have "half" forms or explicit virama forms. 1301 * The glyphs formed by 'half' are Chillus or ligated explicit viramas. 1302 * We want to position matra after them. 1303 */ 1304 if (buffer->props.script != HB_SCRIPT_MALAYALAM && buffer->props.script != HB_SCRIPT_TAMIL) 1305 { 1306 while (new_pos > start && 1307 !(is_one_of (info[new_pos], (FLAG (OT_M) | FLAG (OT_H) | FLAG (OT_Coeng))))) 1308 new_pos--; 1309 1310 /* If we found no Halant we are done. 1311 * Otherwise only proceed if the Halant does 1312 * not belong to the Matra itself! */ 1313 if (is_halant_or_coeng (info[new_pos]) && 1314 info[new_pos].indic_position() != POS_PRE_M) 1315 { 1316 /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */ 1317 if (new_pos + 1 < end && is_joiner (info[new_pos + 1])) 1318 new_pos++; 1319 } 1320 else 1321 new_pos = start; /* No move. */ 1322 } 1323 1324 if (start < new_pos && info[new_pos].indic_position () != POS_PRE_M) 1325 { 1326 /* Now go see if there's actually any matras... */ 1327 for (unsigned int i = new_pos; i > start; i--) 1328 if (info[i - 1].indic_position () == POS_PRE_M) 1329 { 1330 unsigned int old_pos = i - 1; 1331 hb_glyph_info_t tmp = info[old_pos]; 1332 memmove (&info[old_pos], &info[old_pos + 1], (new_pos - old_pos) * sizeof (info[0])); 1333 info[new_pos] = tmp; 1334 if (old_pos < base && base <= new_pos) /* Shouldn't actually happen. */ 1335 base--; 1336 buffer->merge_clusters (new_pos, MIN (end, base + 1)); 1337 new_pos--; 1338 } 1339 } else { 1340 for (unsigned int i = start; i < base; i++) 1341 if (info[i].indic_position () == POS_PRE_M) { 1342 buffer->merge_clusters (i, MIN (end, base + 1)); 1343 break; 1344 } 1345 } 1346 } 1347 1348 1349 /* o Reorder reph: 1350 * 1351 * Reph’s original position is always at the beginning of the syllable, 1352 * (i.e. it is not reordered at the character reordering stage). However, 1353 * it will be reordered according to the basic-forms shaping results. 1354 * Possible positions for reph, depending on the script, are; after main, 1355 * before post-base consonant forms, and after post-base consonant forms. 1356 */ 1357 1358 /* Two cases: 1359 * 1360 * - If repha is encoded as a sequence of characters (Ra,H or Ra,H,ZWJ), then 1361 * we should only move it if the sequence ligated to the repha form. 1362 * 1363 * - If repha is encoded separately and in the logical position, we should only 1364 * move it if it did NOT ligate. If it ligated, it's probably the font trying 1365 * to make it work without the reordering. 1366 */ 1367 if (start + 1 < end && 1368 info[start].indic_position() == POS_RA_TO_BECOME_REPH && 1369 ((info[start].indic_category() == OT_Repha) ^ is_a_ligature (info[start]))) 1370 { 1371 unsigned int new_reph_pos; 1372 reph_position_t reph_pos = indic_plan->config->reph_pos; 1373 1374 /* XXX Figure out old behavior too */ 1375 1376 /* 1. If reph should be positioned after post-base consonant forms, 1377 * proceed to step 5. 1378 */ 1379 if (reph_pos == REPH_POS_AFTER_POST) 1380 { 1381 goto reph_step_5; 1382 } 1383 1384 /* 2. If the reph repositioning class is not after post-base: target 1385 * position is after the first explicit halant glyph between the 1386 * first post-reph consonant and last main consonant. If ZWJ or ZWNJ 1387 * are following this halant, position is moved after it. If such 1388 * position is found, this is the target position. Otherwise, 1389 * proceed to the next step. 1390 * 1391 * Note: in old-implementation fonts, where classifications were 1392 * fixed in shaping engine, there was no case where reph position 1393 * will be found on this step. 1394 */ 1395 { 1396 new_reph_pos = start + 1; 1397 while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos])) 1398 new_reph_pos++; 1399 1400 if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos])) 1401 { 1402 /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */ 1403 if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1])) 1404 new_reph_pos++; 1405 goto reph_move; 1406 } 1407 } 1408 1409 /* 3. If reph should be repositioned after the main consonant: find the 1410 * first consonant not ligated with main, or find the first 1411 * consonant that is not a potential pre-base reordering Ra. 1412 */ 1413 if (reph_pos == REPH_POS_AFTER_MAIN) 1414 { 1415 new_reph_pos = base; 1416 /* XXX Skip potential pre-base reordering Ra. */ 1417 while (new_reph_pos + 1 < end && info[new_reph_pos + 1].indic_position() <= POS_AFTER_MAIN) 1418 new_reph_pos++; 1419 if (new_reph_pos < end) 1420 goto reph_move; 1421 } 1422 1423 /* 4. If reph should be positioned before post-base consonant, find 1424 * first post-base classified consonant not ligated with main. If no 1425 * consonant is found, the target position should be before the 1426 * first matra, syllable modifier sign or vedic sign. 1427 */ 1428 /* This is our take on what step 4 is trying to say (and failing, BADLY). */ 1429 if (reph_pos == REPH_POS_AFTER_SUB) 1430 { 1431 new_reph_pos = base; 1432 while (new_reph_pos < end && 1433 !( FLAG (info[new_reph_pos + 1].indic_position()) & (FLAG (POS_POST_C) | FLAG (POS_AFTER_POST) | FLAG (POS_SMVD)))) 1434 new_reph_pos++; 1435 if (new_reph_pos < end) 1436 goto reph_move; 1437 } 1438 1439 /* 5. If no consonant is found in steps 3 or 4, move reph to a position 1440 * immediately before the first post-base matra, syllable modifier 1441 * sign or vedic sign that has a reordering class after the intended 1442 * reph position. For example, if the reordering position for reph 1443 * is post-main, it will skip above-base matras that also have a 1444 * post-main position. 1445 */ 1446 reph_step_5: 1447 { 1448 /* Copied from step 2. */ 1449 new_reph_pos = start + 1; 1450 while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos])) 1451 new_reph_pos++; 1452 1453 if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos])) 1454 { 1455 /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */ 1456 if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1])) 1457 new_reph_pos++; 1458 goto reph_move; 1459 } 1460 } 1461 1462 /* 6. Otherwise, reorder reph to the end of the syllable. 1463 */ 1464 { 1465 new_reph_pos = end - 1; 1466 while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD) 1467 new_reph_pos--; 1468 1469 /* 1470 * If the Reph is to be ending up after a Matra,Halant sequence, 1471 * position it before that Halant so it can interact with the Matra. 1472 * However, if it's a plain Consonant,Halant we shouldn't do that. 1473 * Uniscribe doesn't do this. 1474 * TEST: U+0930,U+094D,U+0915,U+094B,U+094D 1475 */ 1476 if (!hb_options ().uniscribe_bug_compatible && 1477 unlikely (is_halant_or_coeng (info[new_reph_pos]))) { 1478 for (unsigned int i = base + 1; i < new_reph_pos; i++) 1479 if (info[i].indic_category() == OT_M) { 1480 /* Ok, got it. */ 1481 new_reph_pos--; 1482 } 1483 } 1484 goto reph_move; 1485 } 1486 1487 reph_move: 1488 { 1489 /* Yay, one big cluster! Merge before moving. */ 1490 buffer->merge_clusters (start, end); 1491 1492 /* Move */ 1493 hb_glyph_info_t reph = info[start]; 1494 memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0])); 1495 info[new_reph_pos] = reph; 1496 if (start < base && base <= new_reph_pos) 1497 base--; 1498 } 1499 } 1500 1501 1502 /* o Reorder pre-base reordering consonants: 1503 * 1504 * If a pre-base reordering consonant is found, reorder it according to 1505 * the following rules: 1506 */ 1507 1508 if (indic_plan->mask_array[PREF] && base + 1 < end) /* Otherwise there can't be any pre-base reordering Ra. */ 1509 { 1510 for (unsigned int i = base + 1; i < end; i++) 1511 if ((info[i].mask & indic_plan->mask_array[PREF]) != 0) 1512 { 1513 /* 1. Only reorder a glyph produced by substitution during application 1514 * of the <pref> feature. (Note that a font may shape a Ra consonant with 1515 * the feature generally but block it in certain contexts.) 1516 */ 1517 if (i + 1 == end || (info[i + 1].mask & indic_plan->mask_array[PREF]) == 0) 1518 { 1519 /* 1520 * 2. Try to find a target position the same way as for pre-base matra. 1521 * If it is found, reorder pre-base consonant glyph. 1522 * 1523 * 3. If position is not found, reorder immediately before main 1524 * consonant. 1525 */ 1526 1527 unsigned int new_pos = base; 1528 /* Malayalam / Tamil do not have "half" forms or explicit virama forms. 1529 * The glyphs formed by 'half' are Chillus or ligated explicit viramas. 1530 * We want to position matra after them. 1531 */ 1532 if (buffer->props.script != HB_SCRIPT_MALAYALAM && buffer->props.script != HB_SCRIPT_TAMIL) 1533 { 1534 while (new_pos > start && 1535 !(is_one_of (info[new_pos - 1], FLAG(OT_M) | HALANT_OR_COENG_FLAGS))) 1536 new_pos--; 1537 1538 /* In Khmer coeng model, a V,Ra can go *after* matras. If it goes after a 1539 * split matra, it should be reordered to *before* the left part of such matra. */ 1540 if (new_pos > start && info[new_pos - 1].indic_category() == OT_M) 1541 { 1542 unsigned int old_pos = i; 1543 for (unsigned int i = base + 1; i < old_pos; i++) 1544 if (info[i].indic_category() == OT_M) 1545 { 1546 new_pos--; 1547 break; 1548 } 1549 } 1550 } 1551 1552 if (new_pos > start && is_halant_or_coeng (info[new_pos - 1])) 1553 { 1554 /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */ 1555 if (new_pos < end && is_joiner (info[new_pos])) 1556 new_pos++; 1557 } 1558 1559 { 1560 unsigned int old_pos = i; 1561 buffer->merge_clusters (new_pos, old_pos + 1); 1562 hb_glyph_info_t tmp = info[old_pos]; 1563 memmove (&info[new_pos + 1], &info[new_pos], (old_pos - new_pos) * sizeof (info[0])); 1564 info[new_pos] = tmp; 1565 if (new_pos <= base && base < old_pos) 1566 base++; 1567 } 1568 } 1569 1570 break; 1571 } 1572 } 1573 1574 1575 /* Apply 'init' to the Left Matra if it's a word start. */ 1576 if (info[start].indic_position () == POS_PRE_M && 1577 (!start || 1578 !(FLAG (_hb_glyph_info_get_general_category (&info[start - 1])) & 1579 FLAG_RANGE (HB_UNICODE_GENERAL_CATEGORY_FORMAT, HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))) 1580 info[start].mask |= indic_plan->mask_array[INIT]; 1581 1582 1583 /* 1584 * Finish off the clusters and go home! 1585 */ 1586 if (hb_options ().uniscribe_bug_compatible && buffer->props.script != HB_SCRIPT_TAMIL) 1587 { 1588 /* Uniscribe merges the entire cluster... Except for Tamil. 1589 * This means, half forms are submerged into the main consonants cluster. 1590 * This is unnecessary, and makes cursor positioning harder, but that's what 1591 * Uniscribe does. */ 1592 buffer->merge_clusters (start, end); 1593 } 1594} 1595 1596 1597static void 1598final_reordering (const hb_ot_shape_plan_t *plan, 1599 hb_font_t *font HB_UNUSED, 1600 hb_buffer_t *buffer) 1601{ 1602 unsigned int count = buffer->len; 1603 if (unlikely (!count)) return; 1604 1605 hb_glyph_info_t *info = buffer->info; 1606 unsigned int last = 0; 1607 unsigned int last_syllable = info[0].syllable(); 1608 for (unsigned int i = 1; i < count; i++) 1609 if (last_syllable != info[i].syllable()) { 1610 final_reordering_syllable (plan, buffer, last, i); 1611 last = i; 1612 last_syllable = info[last].syllable(); 1613 } 1614 final_reordering_syllable (plan, buffer, last, count); 1615 1616 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category); 1617 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position); 1618} 1619 1620 1621static void 1622clear_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED, 1623 hb_font_t *font HB_UNUSED, 1624 hb_buffer_t *buffer) 1625{ 1626 hb_glyph_info_t *info = buffer->info; 1627 unsigned int count = buffer->len; 1628 for (unsigned int i = 0; i < count; i++) 1629 info[i].syllable() = 0; 1630} 1631 1632 1633static hb_ot_shape_normalization_mode_t 1634normalization_preference_indic (const hb_segment_properties_t *props HB_UNUSED) 1635{ 1636 return HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT; 1637} 1638 1639static bool 1640decompose_indic (const hb_ot_shape_normalize_context_t *c, 1641 hb_codepoint_t ab, 1642 hb_codepoint_t *a, 1643 hb_codepoint_t *b) 1644{ 1645 switch (ab) 1646 { 1647 /* Don't decompose these. */ 1648 case 0x0931 : return false; 1649 case 0x0B94 : return false; 1650 1651 1652 /* 1653 * Decompose split matras that don't have Unicode decompositions. 1654 */ 1655 1656 case 0x0F77 : *a = 0x0FB2; *b= 0x0F81; return true; 1657 case 0x0F79 : *a = 0x0FB3; *b= 0x0F81; return true; 1658 case 0x17BE : *a = 0x17C1; *b= 0x17BE; return true; 1659 case 0x17BF : *a = 0x17C1; *b= 0x17BF; return true; 1660 case 0x17C0 : *a = 0x17C1; *b= 0x17C0; return true; 1661 case 0x17C4 : *a = 0x17C1; *b= 0x17C4; return true; 1662 case 0x17C5 : *a = 0x17C1; *b= 0x17C5; return true; 1663 case 0x1925 : *a = 0x1920; *b= 0x1923; return true; 1664 case 0x1926 : *a = 0x1920; *b= 0x1924; return true; 1665 case 0x1B3C : *a = 0x1B42; *b= 0x1B3C; return true; 1666 case 0x1112E : *a = 0x11127; *b= 0x11131; return true; 1667 case 0x1112F : *a = 0x11127; *b= 0x11132; return true; 1668#if 0 1669 /* This one has no decomposition in Unicode, but needs no decomposition either. */ 1670 /* case 0x0AC9 : return false; */ 1671 case 0x0B57 : *a = no decomp, -> RIGHT; return true; 1672 case 0x1C29 : *a = no decomp, -> LEFT; return true; 1673 case 0xA9C0 : *a = no decomp, -> RIGHT; return true; 1674 case 0x111BF : *a = no decomp, -> ABOVE; return true; 1675#endif 1676 } 1677 1678 if ((ab == 0x0DDA || hb_in_range<hb_codepoint_t> (ab, 0x0DDC, 0x0DDE))) 1679 { 1680 /* 1681 * Sinhala split matras... Let the fun begin. 1682 * 1683 * These four characters have Unicode decompositions. However, Uniscribe 1684 * decomposes them "Khmer-style", that is, it uses the character itself to 1685 * get the second half. The first half of all four decompositions is always 1686 * U+0DD9. 1687 * 1688 * Now, there are buggy fonts, namely, the widely used lklug.ttf, that are 1689 * broken with Uniscribe. But we need to support them. As such, we only 1690 * do the Uniscribe-style decomposition if the character is transformed into 1691 * its "sec.half" form by the 'pstf' feature. Otherwise, we fall back to 1692 * Unicode decomposition. 1693 * 1694 * Note that we can't unconditionally use Unicode decomposition. That would 1695 * break some other fonts, that are designed to work with Uniscribe, and 1696 * don't have positioning features for the Unicode-style decomposition. 1697 * 1698 * Argh... 1699 * 1700 * The Uniscribe behavior is now documented in the newly published Sinhala 1701 * spec in 2012: 1702 * 1703 * http://www.microsoft.com/typography/OpenTypeDev/sinhala/intro.htm#shaping 1704 */ 1705 1706 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) c->plan->data; 1707 1708 hb_codepoint_t glyph; 1709 1710 if (hb_options ().uniscribe_bug_compatible || 1711 (c->font->get_glyph (ab, 0, &glyph) && 1712 indic_plan->pstf.would_substitute (&glyph, 1, true, c->font->face))) 1713 { 1714 /* Ok, safe to use Uniscribe-style decomposition. */ 1715 *a = 0x0DD9; 1716 *b = ab; 1717 return true; 1718 } 1719 } 1720 1721 return c->unicode->decompose (ab, a, b); 1722} 1723 1724static bool 1725compose_indic (const hb_ot_shape_normalize_context_t *c, 1726 hb_codepoint_t a, 1727 hb_codepoint_t b, 1728 hb_codepoint_t *ab) 1729{ 1730 /* Avoid recomposing split matras. */ 1731 if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (c->unicode->general_category (a))) 1732 return false; 1733 1734 /* Composition-exclusion exceptions that we want to recompose. */ 1735 if (a == 0x09AF && b == 0x09BC) { *ab = 0x09DF; return true; } 1736 1737 return c->unicode->compose (a, b, ab); 1738} 1739 1740 1741const hb_ot_complex_shaper_t _hb_ot_complex_shaper_indic = 1742{ 1743 "indic", 1744 collect_features_indic, 1745 override_features_indic, 1746 data_create_indic, 1747 data_destroy_indic, 1748 NULL, /* preprocess_text */ 1749 normalization_preference_indic, 1750 decompose_indic, 1751 compose_indic, 1752 setup_masks_indic, 1753 HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE, 1754 false, /* fallback_position */ 1755}; 1756