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