Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 20503cc | 2011-06-07 17:02:48 -0400 | [diff] [blame] | 2 | * Copyright © 2011 Google, Inc. |
Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 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 | |
Behdad Esfahbod | 3ed4634 | 2012-04-19 22:34:06 -0400 | [diff] [blame] | 27 | #include "hb-ot-shape-complex-indic-private.hh" |
Behdad Esfahbod | 352372a | 2011-07-30 19:04:02 -0400 | [diff] [blame] | 28 | |
Behdad Esfahbod | ebe2973 | 2012-05-11 16:43:12 +0200 | [diff] [blame] | 29 | static const struct indic_options_t |
| 30 | { |
| 31 | indic_options_t (void) |
| 32 | { |
| 33 | char *c = getenv ("HB_OT_INDIC_OPTIONS"); |
| 34 | uniscribe_bug_compatible = c && strstr (c, "uniscribe-bug-compatible"); |
| 35 | } |
| 36 | |
| 37 | bool uniscribe_bug_compatible; |
| 38 | } options; |
| 39 | |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 40 | static int |
| 41 | compare_codepoint (const void *pa, const void *pb) |
| 42 | { |
| 43 | hb_codepoint_t a = * (hb_codepoint_t *) pa; |
| 44 | hb_codepoint_t b = * (hb_codepoint_t *) pb; |
| 45 | |
| 46 | return a < b ? -1 : a == b ? 0 : +1; |
| 47 | } |
| 48 | |
| 49 | static indic_position_t |
| 50 | consonant_position (hb_codepoint_t u) |
| 51 | { |
| 52 | consonant_position_t *record; |
| 53 | |
| 54 | record = (consonant_position_t *) bsearch (&u, consonant_positions, |
| 55 | ARRAY_LENGTH (consonant_positions), |
| 56 | sizeof (consonant_positions[0]), |
| 57 | compare_codepoint); |
| 58 | |
Behdad Esfahbod | dbccf87 | 2012-05-09 17:24:39 +0200 | [diff] [blame] | 59 | return record ? record->position : POS_BASE_C; |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 60 | } |
| 61 | |
Behdad Esfahbod | 352372a | 2011-07-30 19:04:02 -0400 | [diff] [blame] | 62 | static bool |
| 63 | is_ra (hb_codepoint_t u) |
| 64 | { |
| 65 | return !!bsearch (&u, ra_chars, |
| 66 | ARRAY_LENGTH (ra_chars), |
| 67 | sizeof (ra_chars[0]), |
| 68 | compare_codepoint); |
| 69 | } |
| 70 | |
Behdad Esfahbod | 9ee27a9 | 2011-07-31 11:10:14 -0400 | [diff] [blame] | 71 | static bool |
Behdad Esfahbod | 9da0487 | 2011-07-31 13:46:44 -0400 | [diff] [blame] | 72 | is_joiner (const hb_glyph_info_t &info) |
Behdad Esfahbod | 9ee27a9 | 2011-07-31 11:10:14 -0400 | [diff] [blame] | 73 | { |
Behdad Esfahbod | 9da0487 | 2011-07-31 13:46:44 -0400 | [diff] [blame] | 74 | return !!(FLAG (info.indic_category()) & (FLAG (OT_ZWJ) | FLAG (OT_ZWNJ))); |
| 75 | } |
| 76 | |
| 77 | static bool |
| 78 | is_consonant (const hb_glyph_info_t &info) |
| 79 | { |
Behdad Esfahbod | 1a1fa8c | 2012-05-10 12:20:21 +0200 | [diff] [blame] | 80 | /* Note: |
| 81 | * |
Behdad Esfahbod | 18c06e1 | 2012-05-11 20:02:14 +0200 | [diff] [blame^] | 82 | * We treat Vowels and placeholders as if they were consonants. This is safe because Vowels |
Behdad Esfahbod | c5306b6 | 2012-05-10 12:07:33 +0200 | [diff] [blame] | 83 | * cannot happen in a consonant syllable. The plus side however is, we can call the |
| 84 | * consonant syllable logic from the vowel syllable function and get it all right! */ |
Behdad Esfahbod | 18c06e1 | 2012-05-11 20:02:14 +0200 | [diff] [blame^] | 85 | return !!(FLAG (info.indic_category()) & (FLAG (OT_C) | FLAG (OT_Ra) | FLAG (OT_V) | FLAG (OT_NBSP) | FLAG (OT_DOTTEDCIRCLE))); |
Behdad Esfahbod | 9ee27a9 | 2011-07-31 11:10:14 -0400 | [diff] [blame] | 86 | } |
Behdad Esfahbod | c7fe56a | 2011-06-24 19:05:34 -0400 | [diff] [blame] | 87 | |
| 88 | static const struct { |
| 89 | hb_tag_t tag; |
| 90 | hb_bool_t is_global; |
| 91 | } indic_basic_features[] = |
Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 92 | { |
Behdad Esfahbod | c7fe56a | 2011-06-24 19:05:34 -0400 | [diff] [blame] | 93 | {HB_TAG('n','u','k','t'), true}, |
| 94 | {HB_TAG('a','k','h','n'), false}, |
| 95 | {HB_TAG('r','p','h','f'), false}, |
Behdad Esfahbod | 1ac075b | 2012-05-09 11:06:47 +0200 | [diff] [blame] | 96 | {HB_TAG('r','k','r','f'), true}, |
Behdad Esfahbod | c7fe56a | 2011-06-24 19:05:34 -0400 | [diff] [blame] | 97 | {HB_TAG('p','r','e','f'), false}, |
| 98 | {HB_TAG('b','l','w','f'), false}, |
| 99 | {HB_TAG('h','a','l','f'), false}, |
| 100 | {HB_TAG('v','a','t','u'), true}, |
| 101 | {HB_TAG('p','s','t','f'), false}, |
Behdad Esfahbod | 9da0487 | 2011-07-31 13:46:44 -0400 | [diff] [blame] | 102 | {HB_TAG('c','j','c','t'), false}, |
Behdad Esfahbod | c7fe56a | 2011-06-24 19:05:34 -0400 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | /* Same order as the indic_basic_features array */ |
| 106 | enum { |
| 107 | _NUKT, |
| 108 | AKHN, |
| 109 | RPHF, |
Behdad Esfahbod | df6d45c | 2012-05-09 11:38:31 +0200 | [diff] [blame] | 110 | _RKRF, |
Behdad Esfahbod | c7fe56a | 2011-06-24 19:05:34 -0400 | [diff] [blame] | 111 | PREF, |
| 112 | BLWF, |
| 113 | HALF, |
| 114 | _VATU, |
| 115 | PSTF, |
Behdad Esfahbod | e8eedf2 | 2012-01-16 16:39:40 -0500 | [diff] [blame] | 116 | CJCT |
Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | static const hb_tag_t indic_other_features[] = |
| 120 | { |
| 121 | HB_TAG('p','r','e','s'), |
| 122 | HB_TAG('a','b','v','s'), |
| 123 | HB_TAG('b','l','w','s'), |
| 124 | HB_TAG('p','s','t','s'), |
| 125 | HB_TAG('h','a','l','n'), |
| 126 | |
| 127 | HB_TAG('d','i','s','t'), |
| 128 | HB_TAG('a','b','v','m'), |
| 129 | HB_TAG('b','l','w','m'), |
| 130 | }; |
| 131 | |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 132 | |
| 133 | static void |
| 134 | initial_reordering (const hb_ot_map_t *map, |
| 135 | hb_face_t *face, |
| 136 | hb_buffer_t *buffer, |
| 137 | void *user_data HB_UNUSED); |
Behdad Esfahbod | f6fd378 | 2011-07-08 00:22:40 -0400 | [diff] [blame] | 138 | static void |
| 139 | final_reordering (const hb_ot_map_t *map, |
| 140 | hb_face_t *face, |
| 141 | hb_buffer_t *buffer, |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 142 | void *user_data HB_UNUSED); |
Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 143 | |
Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 144 | void |
Behdad Esfahbod | 76f7681 | 2011-07-07 22:25:25 -0400 | [diff] [blame] | 145 | _hb_ot_shape_complex_collect_features_indic (hb_ot_map_builder_t *map, const hb_segment_properties_t *props) |
Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 146 | { |
Behdad Esfahbod | f6fd378 | 2011-07-08 00:22:40 -0400 | [diff] [blame] | 147 | map->add_bool_feature (HB_TAG('l','o','c','l')); |
Behdad Esfahbod | a54a550 | 2011-07-20 16:42:10 -0400 | [diff] [blame] | 148 | /* The Indic specs do not require ccmp, but we apply it here since if |
| 149 | * there is a use of it, it's typically at the beginning. */ |
Behdad Esfahbod | f6fd378 | 2011-07-08 00:22:40 -0400 | [diff] [blame] | 150 | map->add_bool_feature (HB_TAG('c','c','m','p')); |
| 151 | |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 152 | map->add_gsub_pause (initial_reordering, NULL); |
Behdad Esfahbod | f6fd378 | 2011-07-08 00:22:40 -0400 | [diff] [blame] | 153 | |
Behdad Esfahbod | 412b918 | 2012-05-09 11:07:18 +0200 | [diff] [blame] | 154 | for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++) { |
Behdad Esfahbod | 76f7681 | 2011-07-07 22:25:25 -0400 | [diff] [blame] | 155 | map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global); |
Behdad Esfahbod | 412b918 | 2012-05-09 11:07:18 +0200 | [diff] [blame] | 156 | map->add_gsub_pause (NULL, NULL); |
| 157 | } |
Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 158 | |
Behdad Esfahbod | f6fd378 | 2011-07-08 00:22:40 -0400 | [diff] [blame] | 159 | map->add_gsub_pause (final_reordering, NULL); |
| 160 | |
Behdad Esfahbod | 412b918 | 2012-05-09 11:07:18 +0200 | [diff] [blame] | 161 | for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++) { |
Behdad Esfahbod | 76f7681 | 2011-07-07 22:25:25 -0400 | [diff] [blame] | 162 | map->add_bool_feature (indic_other_features[i], true); |
Behdad Esfahbod | 412b918 | 2012-05-09 11:07:18 +0200 | [diff] [blame] | 163 | map->add_gsub_pause (NULL, NULL); |
| 164 | } |
Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Behdad Esfahbod | 867361c | 2011-06-17 18:35:46 -0400 | [diff] [blame] | 167 | |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 168 | hb_ot_shape_normalization_mode_t |
| 169 | _hb_ot_shape_complex_normalization_preference_indic (void) |
Behdad Esfahbod | 02cdf74 | 2011-07-21 12:23:12 -0400 | [diff] [blame] | 170 | { |
| 171 | /* We want split matras decomposed by the common shaping logic. */ |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 172 | return HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED; |
Behdad Esfahbod | 02cdf74 | 2011-07-21 12:23:12 -0400 | [diff] [blame] | 173 | } |
| 174 | |
Behdad Esfahbod | 867361c | 2011-06-17 18:35:46 -0400 | [diff] [blame] | 175 | |
Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 176 | void |
Behdad Esfahbod | acd88e6 | 2012-04-10 18:02:20 -0400 | [diff] [blame] | 177 | _hb_ot_shape_complex_setup_masks_indic (hb_ot_map_t *map, hb_buffer_t *buffer, hb_font_t *font) |
Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 178 | { |
Behdad Esfahbod | 9f9bcce | 2011-07-28 17:06:46 -0400 | [diff] [blame] | 179 | HB_BUFFER_ALLOCATE_VAR (buffer, indic_category); |
| 180 | HB_BUFFER_ALLOCATE_VAR (buffer, indic_position); |
| 181 | |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 182 | /* We cannot setup masks here. We save information about characters |
| 183 | * and setup masks later on in a pause-callback. */ |
| 184 | |
| 185 | unsigned int count = buffer->len; |
Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 186 | for (unsigned int i = 0; i < count; i++) |
| 187 | { |
Behdad Esfahbod | 92332e5 | 2012-05-09 17:40:00 +0200 | [diff] [blame] | 188 | hb_glyph_info_t &info = buffer->info[i]; |
| 189 | unsigned int type = get_indic_categories (info.codepoint); |
Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 190 | |
Behdad Esfahbod | 92332e5 | 2012-05-09 17:40:00 +0200 | [diff] [blame] | 191 | info.indic_category() = type & 0x0F; |
| 192 | info.indic_position() = type >> 4; |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 193 | |
Behdad Esfahbod | 3399a06 | 2012-05-11 17:54:26 +0200 | [diff] [blame] | 194 | /* The spec says U+0952 is OT_A. However, testing shows that Uniscribe |
| 195 | * treats U+0951..U+0952 all as OT_VD. |
| 196 | * TESTS: |
| 197 | * U+092E,U+0947,U+0952 |
| 198 | * U+092E,U+0952,U+0947 |
| 199 | * U+092E,U+0947,U+0951 |
| 200 | * U+092E,U+0951,U+0947 |
| 201 | * */ |
| 202 | if (unlikely (hb_in_range<hb_codepoint_t> (info.codepoint, 0x0951, 0x0954))) |
| 203 | info.indic_category() = OT_VD; |
| 204 | |
Behdad Esfahbod | 92332e5 | 2012-05-09 17:40:00 +0200 | [diff] [blame] | 205 | if (info.indic_category() == OT_C) { |
| 206 | info.indic_position() = consonant_position (info.codepoint); |
| 207 | if (is_ra (info.codepoint)) |
| 208 | info.indic_category() = OT_Ra; |
| 209 | } else if (info.indic_category() == OT_SM || |
| 210 | info.indic_category() == OT_VD) { |
| 211 | info.indic_position() = POS_SMVD; |
| 212 | } else if (unlikely (info.codepoint == 0x200C)) |
| 213 | info.indic_category() = OT_ZWNJ; |
| 214 | else if (unlikely (info.codepoint == 0x200D)) |
| 215 | info.indic_category() = OT_ZWJ; |
Behdad Esfahbod | 18c06e1 | 2012-05-11 20:02:14 +0200 | [diff] [blame^] | 216 | else if (unlikely (info.codepoint == 0x25CC)) |
| 217 | info.indic_category() = OT_DOTTEDCIRCLE; |
Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 218 | } |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 219 | } |
Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 220 | |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 221 | static int |
| 222 | compare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb) |
| 223 | { |
| 224 | int a = pa->indic_position(); |
| 225 | int b = pb->indic_position(); |
| 226 | |
| 227 | return a < b ? -1 : a == b ? 0 : +1; |
| 228 | } |
Behdad Esfahbod | 867361c | 2011-06-17 18:35:46 -0400 | [diff] [blame] | 229 | |
Behdad Esfahbod | 7ea58db | 2012-05-11 18:58:57 +0200 | [diff] [blame] | 230 | /* Rules from: |
| 231 | * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */ |
| 232 | |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 233 | static void |
Behdad Esfahbod | 0831061 | 2012-05-11 19:07:58 +0200 | [diff] [blame] | 234 | initial_reordering_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array, |
| 235 | unsigned int start, unsigned int end, unsigned int base); |
| 236 | |
| 237 | static void |
Behdad Esfahbod | ef24cc8 | 2012-05-09 17:56:03 +0200 | [diff] [blame] | 238 | initial_reordering_consonant_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array, |
| 239 | unsigned int start, unsigned int end) |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 240 | { |
Behdad Esfahbod | ee58f3b | 2011-07-30 19:15:53 -0400 | [diff] [blame] | 241 | hb_glyph_info_t *info = buffer->info; |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 242 | |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 243 | /* 1. Find base consonant: |
| 244 | * |
| 245 | * The shaping engine finds the base consonant of the syllable, using the |
| 246 | * following algorithm: starting from the end of the syllable, move backwards |
| 247 | * until a consonant is found that does not have a below-base or post-base |
| 248 | * form (post-base forms have to follow below-base forms), or that is not a |
| 249 | * pre-base reordering Ra, or arrive at the first consonant. The consonant |
| 250 | * stopped at will be the base. |
| 251 | * |
| 252 | * o If the syllable starts with Ra + Halant (in a script that has Reph) |
| 253 | * and has more than one consonant, Ra is excluded from candidates for |
| 254 | * base consonants. |
| 255 | */ |
| 256 | |
Behdad Esfahbod | 5e72071 | 2011-07-31 17:51:50 -0400 | [diff] [blame] | 257 | unsigned int base = end; |
Behdad Esfahbod | 76b3409 | 2012-05-09 11:43:43 +0200 | [diff] [blame] | 258 | bool has_reph = false; |
| 259 | |
| 260 | /* -> If the syllable starts with Ra + Halant (in a script that has Reph) |
| 261 | * and has more than one consonant, Ra is excluded from candidates for |
| 262 | * base consonants. */ |
| 263 | unsigned int limit = start; |
| 264 | if (mask_array[RPHF] && |
Behdad Esfahbod | 6d8e0cb | 2012-05-10 11:41:51 +0200 | [diff] [blame] | 265 | start + 3 <= end && |
Behdad Esfahbod | 76b3409 | 2012-05-09 11:43:43 +0200 | [diff] [blame] | 266 | info[start].indic_category() == OT_Ra && |
Behdad Esfahbod | 6d8e0cb | 2012-05-10 11:41:51 +0200 | [diff] [blame] | 267 | info[start + 1].indic_category() == OT_H && |
| 268 | !is_joiner (info[start + 2])) |
Behdad Esfahbod | 76b3409 | 2012-05-09 11:43:43 +0200 | [diff] [blame] | 269 | { |
| 270 | limit += 2; |
| 271 | base = start; |
| 272 | has_reph = true; |
| 273 | }; |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 274 | |
Behdad Esfahbod | 3c2ea94 | 2012-05-11 16:23:38 +0200 | [diff] [blame] | 275 | { |
| 276 | /* -> starting from the end of the syllable, move backwards */ |
| 277 | unsigned int i = end; |
| 278 | do { |
| 279 | i--; |
| 280 | /* -> until a consonant is found */ |
| 281 | if (is_consonant (info[i])) |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 282 | { |
Behdad Esfahbod | 3c2ea94 | 2012-05-11 16:23:38 +0200 | [diff] [blame] | 283 | /* -> that does not have a below-base or post-base form |
| 284 | * (post-base forms have to follow below-base forms), */ |
| 285 | if (info[i].indic_position() != POS_BELOW_C && |
| 286 | info[i].indic_position() != POS_POST_C) |
| 287 | { |
| 288 | base = i; |
| 289 | break; |
| 290 | } |
| 291 | |
| 292 | /* -> or that is not a pre-base reordering Ra, |
| 293 | * |
| 294 | * TODO |
| 295 | */ |
| 296 | |
| 297 | /* -> or arrive at the first consonant. The consonant stopped at will |
| 298 | * be the base. */ |
| 299 | base = i; |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 300 | } |
Behdad Esfahbod | 3c2ea94 | 2012-05-11 16:23:38 +0200 | [diff] [blame] | 301 | else |
| 302 | if (is_joiner (info[i])) |
| 303 | break; |
| 304 | } while (i > limit); |
| 305 | if (base < start) |
| 306 | base = start; /* Just in case... */ |
| 307 | } |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 308 | |
Behdad Esfahbod | 0831061 | 2012-05-11 19:07:58 +0200 | [diff] [blame] | 309 | /* Continue reading below ------v */ |
| 310 | initial_reordering_syllable (map, buffer, mask_array, start, end, base); |
| 311 | } |
Behdad Esfahbod | 3d25079 | 2012-05-10 11:37:42 +0200 | [diff] [blame] | 312 | |
Behdad Esfahbod | 0831061 | 2012-05-11 19:07:58 +0200 | [diff] [blame] | 313 | static void |
| 314 | initial_reordering_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array, |
| 315 | unsigned int start, unsigned int end, unsigned int base) |
| 316 | { |
| 317 | hb_glyph_info_t *info = buffer->info; |
Behdad Esfahbod | 3d25079 | 2012-05-10 11:37:42 +0200 | [diff] [blame] | 318 | |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 319 | /* 2. Decompose and reorder Matras: |
| 320 | * |
| 321 | * Each matra and any syllable modifier sign in the cluster are moved to the |
| 322 | * appropriate position relative to the consonant(s) in the cluster. The |
| 323 | * shaping engine decomposes two- or three-part matras into their constituent |
| 324 | * parts before any repositioning. Matra characters are classified by which |
| 325 | * consonant in a conjunct they have affinity for and are reordered to the |
| 326 | * following positions: |
| 327 | * |
| 328 | * o Before first half form in the syllable |
| 329 | * o After subjoined consonants |
| 330 | * o After post-form consonant |
| 331 | * o After main consonant (for above marks) |
| 332 | * |
| 333 | * IMPLEMENTATION NOTES: |
| 334 | * |
| 335 | * The normalize() routine has already decomposed matras for us, so we don't |
| 336 | * need to worry about that. |
| 337 | */ |
| 338 | |
| 339 | |
| 340 | /* 3. Reorder marks to canonical order: |
| 341 | * |
| 342 | * Adjacent nukta and halant or nukta and vedic sign are always repositioned |
| 343 | * if necessary, so that the nukta is first. |
| 344 | * |
| 345 | * IMPLEMENTATION NOTES: |
| 346 | * |
| 347 | * We don't need to do this: the normalize() routine already did this for us. |
| 348 | */ |
| 349 | |
| 350 | |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 351 | /* Reorder characters */ |
| 352 | |
Behdad Esfahbod | 3c2ea94 | 2012-05-11 16:23:38 +0200 | [diff] [blame] | 353 | for (unsigned int i = start; i < base; i++) |
Behdad Esfahbod | dbccf87 | 2012-05-09 17:24:39 +0200 | [diff] [blame] | 354 | info[i].indic_position() = POS_PRE_C; |
| 355 | info[base].indic_position() = POS_BASE_C; |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 356 | |
Behdad Esfahbod | fd06bf5 | 2011-07-30 20:14:44 -0400 | [diff] [blame] | 357 | /* Handle beginning Ra */ |
Behdad Esfahbod | 0831061 | 2012-05-11 19:07:58 +0200 | [diff] [blame] | 358 | if (mask_array[RPHF] && |
| 359 | start != base && |
| 360 | start + 3 <= end && |
| 361 | info[start].indic_category() == OT_Ra && |
| 362 | info[start + 1].indic_category() == OT_H && |
| 363 | !is_joiner (info[start + 2])) |
Behdad Esfahbod | dbb1058 | 2012-05-10 13:45:52 +0200 | [diff] [blame] | 364 | info[start].indic_position() = POS_RA_TO_BECOME_REPH; |
Behdad Esfahbod | fd06bf5 | 2011-07-30 20:14:44 -0400 | [diff] [blame] | 365 | |
Behdad Esfahbod | f5bc272 | 2011-07-30 21:08:10 -0400 | [diff] [blame] | 366 | /* For old-style Indic script tags, move the first post-base Halant after |
| 367 | * last consonant. */ |
| 368 | if ((map->get_chosen_script (0) & 0x000000FF) != '2') { |
| 369 | /* We should only do this for Indic scripts which have a version two I guess. */ |
Behdad Esfahbod | 3c2ea94 | 2012-05-11 16:23:38 +0200 | [diff] [blame] | 370 | for (unsigned int i = base + 1; i < end; i++) |
Behdad Esfahbod | f5bc272 | 2011-07-30 21:08:10 -0400 | [diff] [blame] | 371 | if (info[i].indic_category() == OT_H) { |
| 372 | unsigned int j; |
| 373 | for (j = end - 1; j > i; j--) |
Behdad Esfahbod | 190eb31 | 2012-05-10 12:17:16 +0200 | [diff] [blame] | 374 | if (is_consonant (info[j])) |
Behdad Esfahbod | f5bc272 | 2011-07-30 21:08:10 -0400 | [diff] [blame] | 375 | break; |
| 376 | if (j > i) { |
| 377 | /* Move Halant to after last consonant. */ |
| 378 | hb_glyph_info_t t = info[i]; |
| 379 | memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0])); |
| 380 | info[j] = t; |
| 381 | } |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 386 | /* Attach ZWJ, ZWNJ, nukta, and halant to previous char to move with them. */ |
Behdad Esfahbod | ebe2973 | 2012-05-11 16:43:12 +0200 | [diff] [blame] | 387 | if (!options.uniscribe_bug_compatible) |
| 388 | { |
| 389 | /* Please update the Uniscribe branch when touching this! */ |
| 390 | for (unsigned int i = start + 1; i < end; i++) |
| 391 | if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H)))) |
| 392 | info[i].indic_position() = info[i - 1].indic_position(); |
| 393 | } else { |
Behdad Esfahbod | 67ea29a | 2012-05-11 16:51:23 +0200 | [diff] [blame] | 394 | /* |
| 395 | * Uniscribe doesn't move the Halant with Left Matra. |
| 396 | * TEST: U+092B,U+093F,U+094DE |
| 397 | */ |
Behdad Esfahbod | ebe2973 | 2012-05-11 16:43:12 +0200 | [diff] [blame] | 398 | /* Please update the non-Uniscribe branch when touching this! */ |
| 399 | for (unsigned int i = start + 1; i < end; i++) |
| 400 | if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H)))) { |
| 401 | info[i].indic_position() = info[i - 1].indic_position(); |
| 402 | if (info[i].indic_category() == OT_H && info[i].indic_position() == POS_LEFT_MATRA) |
| 403 | for (unsigned int j = i; j > start; j--) |
| 404 | if (info[j - 1].indic_position() != POS_LEFT_MATRA) { |
Behdad Esfahbod | ebe2973 | 2012-05-11 16:43:12 +0200 | [diff] [blame] | 405 | info[i].indic_position() = info[j - 1].indic_position(); |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | } |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 410 | |
| 411 | /* We do bubble-sort, skip malicious clusters attempts */ |
Behdad Esfahbod | b99d63a | 2012-05-10 11:32:52 +0200 | [diff] [blame] | 412 | if (end - start < 64) |
Behdad Esfahbod | a391ff5 | 2012-05-10 11:31:20 +0200 | [diff] [blame] | 413 | { |
| 414 | /* Sit tight, rock 'n roll! */ |
Behdad Esfahbod | d3637ed | 2012-05-10 10:51:38 +0200 | [diff] [blame] | 415 | hb_bubble_sort (info + start, end - start, compare_indic_order); |
Behdad Esfahbod | a391ff5 | 2012-05-10 11:31:20 +0200 | [diff] [blame] | 416 | /* Find base again */ |
| 417 | base = end; |
Behdad Esfahbod | 3c2ea94 | 2012-05-11 16:23:38 +0200 | [diff] [blame] | 418 | for (unsigned int i = start; i < end; i++) |
Behdad Esfahbod | a391ff5 | 2012-05-10 11:31:20 +0200 | [diff] [blame] | 419 | if (info[i].indic_position() == POS_BASE_C) { |
| 420 | base = i; |
| 421 | break; |
| 422 | } |
| 423 | } |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 424 | |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 425 | /* Setup masks now */ |
| 426 | |
Behdad Esfahbod | 2816839 | 2011-07-31 16:00:35 -0400 | [diff] [blame] | 427 | { |
| 428 | hb_mask_t mask; |
| 429 | |
Behdad Esfahbod | dbb1058 | 2012-05-10 13:45:52 +0200 | [diff] [blame] | 430 | /* Reph */ |
Behdad Esfahbod | 668c604 | 2012-05-11 15:34:13 +0200 | [diff] [blame] | 431 | for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++) |
| 432 | info[i].mask |= mask_array[RPHF]; |
Behdad Esfahbod | dbb1058 | 2012-05-10 13:45:52 +0200 | [diff] [blame] | 433 | |
Behdad Esfahbod | 2816839 | 2011-07-31 16:00:35 -0400 | [diff] [blame] | 434 | /* Pre-base */ |
| 435 | mask = mask_array[HALF] | mask_array[AKHN] | mask_array[CJCT]; |
Behdad Esfahbod | 3c2ea94 | 2012-05-11 16:23:38 +0200 | [diff] [blame] | 436 | for (unsigned int i = start; i < base; i++) |
Behdad Esfahbod | 2816839 | 2011-07-31 16:00:35 -0400 | [diff] [blame] | 437 | info[i].mask |= mask; |
| 438 | /* Base */ |
| 439 | mask = mask_array[AKHN] | mask_array[CJCT]; |
| 440 | info[base].mask |= mask; |
| 441 | /* Post-base */ |
| 442 | mask = mask_array[BLWF] | mask_array[PSTF] | mask_array[CJCT]; |
Behdad Esfahbod | 3c2ea94 | 2012-05-11 16:23:38 +0200 | [diff] [blame] | 443 | for (unsigned int i = base + 1; i < end; i++) |
Behdad Esfahbod | 2816839 | 2011-07-31 16:00:35 -0400 | [diff] [blame] | 444 | info[i].mask |= mask; |
| 445 | } |
Behdad Esfahbod | 9da0487 | 2011-07-31 13:46:44 -0400 | [diff] [blame] | 446 | |
| 447 | /* Apply ZWJ/ZWNJ effects */ |
Behdad Esfahbod | 3c2ea94 | 2012-05-11 16:23:38 +0200 | [diff] [blame] | 448 | for (unsigned int i = start + 1; i < end; i++) |
Behdad Esfahbod | 9da0487 | 2011-07-31 13:46:44 -0400 | [diff] [blame] | 449 | if (is_joiner (info[i])) { |
| 450 | bool non_joiner = info[i].indic_category() == OT_ZWNJ; |
Behdad Esfahbod | 6b37bc8 | 2011-07-31 15:57:00 -0400 | [diff] [blame] | 451 | unsigned int j = i; |
Behdad Esfahbod | 9da0487 | 2011-07-31 13:46:44 -0400 | [diff] [blame] | 452 | |
| 453 | do { |
Behdad Esfahbod | 9da0487 | 2011-07-31 13:46:44 -0400 | [diff] [blame] | 454 | j--; |
Behdad Esfahbod | 6b37bc8 | 2011-07-31 15:57:00 -0400 | [diff] [blame] | 455 | |
Behdad Esfahbod | 3bf27a9 | 2012-05-11 11:17:23 +0200 | [diff] [blame] | 456 | info[j].mask &= ~mask_array[CJCT]; |
Behdad Esfahbod | 6b37bc8 | 2011-07-31 15:57:00 -0400 | [diff] [blame] | 457 | if (non_joiner) |
Behdad Esfahbod | c6d904d | 2012-05-11 11:07:40 +0200 | [diff] [blame] | 458 | info[j].mask &= ~mask_array[HALF]; |
Behdad Esfahbod | 6b37bc8 | 2011-07-31 15:57:00 -0400 | [diff] [blame] | 459 | |
Behdad Esfahbod | 9da0487 | 2011-07-31 13:46:44 -0400 | [diff] [blame] | 460 | } while (j > start && !is_consonant (info[j])); |
| 461 | } |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | |
| 465 | static void |
Behdad Esfahbod | ef24cc8 | 2012-05-09 17:56:03 +0200 | [diff] [blame] | 466 | initial_reordering_vowel_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array, |
| 467 | unsigned int start, unsigned int end) |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 468 | { |
Behdad Esfahbod | c5306b6 | 2012-05-10 12:07:33 +0200 | [diff] [blame] | 469 | /* We made the vowels look like consonants. So let's call the consonant logic! */ |
| 470 | initial_reordering_consonant_syllable (map, buffer, mask_array, start, end); |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static void |
Behdad Esfahbod | ef24cc8 | 2012-05-09 17:56:03 +0200 | [diff] [blame] | 474 | initial_reordering_standalone_cluster (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array, |
| 475 | unsigned int start, unsigned int end) |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 476 | { |
Behdad Esfahbod | 18c06e1 | 2012-05-11 20:02:14 +0200 | [diff] [blame^] | 477 | /* We treat NBSP/dotted-circle as if they are consonants, so we should just chain. |
| 478 | * Only if not in compatibility mode that is... */ |
| 479 | |
| 480 | if (options.uniscribe_bug_compatible) |
| 481 | { |
| 482 | /* For dotted-circle, this is what Uniscribe does: |
| 483 | * If dotted-circle is the last glyph, it just does nothing. |
| 484 | * Ie. It doesn't form Reph. */ |
| 485 | if (buffer->info[end - 1].indic_category() == OT_DOTTEDCIRCLE) |
| 486 | return; |
| 487 | } |
| 488 | |
Behdad Esfahbod | 1a1fa8c | 2012-05-10 12:20:21 +0200 | [diff] [blame] | 489 | initial_reordering_consonant_syllable (map, buffer, mask_array, start, end); |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | static void |
Behdad Esfahbod | ef24cc8 | 2012-05-09 17:56:03 +0200 | [diff] [blame] | 493 | initial_reordering_non_indic (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array, |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 494 | unsigned int start, unsigned int end) |
| 495 | { |
| 496 | /* Nothing to do right now. If we ever switch to using the output |
| 497 | * buffer in the reordering process, we'd need to next_glyph() here. */ |
| 498 | } |
| 499 | |
| 500 | #include "hb-ot-shape-complex-indic-machine.hh" |
| 501 | |
| 502 | static void |
| 503 | initial_reordering (const hb_ot_map_t *map, |
| 504 | hb_face_t *face, |
| 505 | hb_buffer_t *buffer, |
| 506 | void *user_data HB_UNUSED) |
| 507 | { |
Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 508 | hb_mask_t mask_array[ARRAY_LENGTH (indic_basic_features)] = {0}; |
| 509 | unsigned int num_masks = ARRAY_LENGTH (indic_basic_features); |
| 510 | for (unsigned int i = 0; i < num_masks; i++) |
Behdad Esfahbod | 76f7681 | 2011-07-07 22:25:25 -0400 | [diff] [blame] | 511 | mask_array[i] = map->get_1_mask (indic_basic_features[i].tag); |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 512 | |
| 513 | find_syllables (map, buffer, mask_array); |
Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 514 | } |
| 515 | |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 516 | static void |
Behdad Esfahbod | ef24cc8 | 2012-05-09 17:56:03 +0200 | [diff] [blame] | 517 | final_reordering_syllable (hb_buffer_t *buffer, |
| 518 | unsigned int start, unsigned int end) |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 519 | { |
Behdad Esfahbod | 4ac9e98 | 2012-05-10 12:53:53 +0200 | [diff] [blame] | 520 | hb_glyph_info_t *info = buffer->info; |
| 521 | |
Behdad Esfahbod | e7be057 | 2011-07-31 15:18:57 -0400 | [diff] [blame] | 522 | /* 4. Final reordering: |
| 523 | * |
| 524 | * After the localized forms and basic shaping forms GSUB features have been |
| 525 | * applied (see below), the shaping engine performs some final glyph |
| 526 | * reordering before applying all the remaining font features to the entire |
| 527 | * cluster. |
Behdad Esfahbod | 4ac9e98 | 2012-05-10 12:53:53 +0200 | [diff] [blame] | 528 | */ |
| 529 | |
| 530 | /* Find base again */ |
| 531 | unsigned int base = end; |
| 532 | for (unsigned int i = start; i < end; i++) |
| 533 | if (info[i].indic_position() == POS_BASE_C) { |
| 534 | base = i; |
| 535 | break; |
| 536 | } |
| 537 | |
| 538 | if (base == start) { |
| 539 | /* There's no Reph, and no left Matra to reposition. Just merge the cluster |
| 540 | * and go home. */ |
| 541 | buffer->merge_clusters (start, end); |
| 542 | return; |
| 543 | } |
| 544 | |
Behdad Esfahbod | 4705a70 | 2012-05-10 13:09:08 +0200 | [diff] [blame] | 545 | unsigned int start_of_last_cluster = base; |
| 546 | |
Behdad Esfahbod | 4ac9e98 | 2012-05-10 12:53:53 +0200 | [diff] [blame] | 547 | /* o Reorder matras: |
Behdad Esfahbod | e7be057 | 2011-07-31 15:18:57 -0400 | [diff] [blame] | 548 | * |
| 549 | * If a pre-base matra character had been reordered before applying basic |
| 550 | * features, the glyph can be moved closer to the main consonant based on |
| 551 | * whether half-forms had been formed. Actual position for the matra is |
| 552 | * defined as “after last standalone halant glyph, after initial matra |
| 553 | * position and before the main consonant”. If ZWJ or ZWNJ follow this |
| 554 | * halant, position is moved after it. |
Behdad Esfahbod | 4ac9e98 | 2012-05-10 12:53:53 +0200 | [diff] [blame] | 555 | */ |
| 556 | |
| 557 | unsigned int new_matra_pos = base - 1; |
| 558 | while (new_matra_pos > start && |
| 559 | !(FLAG (info[new_matra_pos].indic_category()) & (FLAG (OT_M) | FLAG (OT_H)))) |
| 560 | new_matra_pos--; |
Behdad Esfahbod | 6782bda | 2012-05-11 16:23:43 +0200 | [diff] [blame] | 561 | /* If we found no Halant we are done. Otherwise only proceed if the Halant does |
| 562 | * not belong to the Matra itself! */ |
| 563 | if (info[new_matra_pos].indic_category() == OT_H && |
| 564 | info[new_matra_pos].indic_position() != POS_LEFT_MATRA) { |
Behdad Esfahbod | 4ac9e98 | 2012-05-10 12:53:53 +0200 | [diff] [blame] | 565 | /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */ |
| 566 | if (new_matra_pos + 1 < end && is_joiner (info[new_matra_pos + 1])) |
| 567 | new_matra_pos++; |
| 568 | |
| 569 | /* Now go see if there's actually any matras... */ |
| 570 | for (unsigned int i = new_matra_pos; i > start; i--) |
Behdad Esfahbod | 7708ee2 | 2012-05-10 14:48:25 +0200 | [diff] [blame] | 571 | if (info[i - 1].indic_position () == POS_LEFT_MATRA) |
Behdad Esfahbod | 4ac9e98 | 2012-05-10 12:53:53 +0200 | [diff] [blame] | 572 | { |
| 573 | unsigned int old_matra_pos = i - 1; |
| 574 | hb_glyph_info_t matra = info[old_matra_pos]; |
| 575 | memmove (&info[old_matra_pos], &info[old_matra_pos + 1], (new_matra_pos - old_matra_pos) * sizeof (info[0])); |
| 576 | info[new_matra_pos] = matra; |
Behdad Esfahbod | 4705a70 | 2012-05-10 13:09:08 +0200 | [diff] [blame] | 577 | start_of_last_cluster = MIN (new_matra_pos, start_of_last_cluster); |
Behdad Esfahbod | 4ac9e98 | 2012-05-10 12:53:53 +0200 | [diff] [blame] | 578 | new_matra_pos--; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | |
| 583 | /* o Reorder reph: |
Behdad Esfahbod | e7be057 | 2011-07-31 15:18:57 -0400 | [diff] [blame] | 584 | * |
| 585 | * Reph’s original position is always at the beginning of the syllable, |
| 586 | * (i.e. it is not reordered at the character reordering stage). However, |
| 587 | * it will be reordered according to the basic-forms shaping results. |
| 588 | * Possible positions for reph, depending on the script, are; after main, |
| 589 | * before post-base consonant forms, and after post-base consonant forms. |
Behdad Esfahbod | dbb1058 | 2012-05-10 13:45:52 +0200 | [diff] [blame] | 590 | */ |
| 591 | |
| 592 | /* If there's anything after the Ra that has the REPH pos, it ought to be halant. |
| 593 | * Which means that the font has failed to ligate the Reph. In which case, we |
| 594 | * shouldn't move. */ |
| 595 | if (start + 1 < end && |
| 596 | info[start].indic_position() == POS_RA_TO_BECOME_REPH && |
| 597 | info[start + 1].indic_position() != POS_RA_TO_BECOME_REPH) |
| 598 | { |
Behdad Esfahbod | 02b2922 | 2012-05-10 21:44:50 +0200 | [diff] [blame] | 599 | unsigned int new_reph_pos; |
| 600 | |
| 601 | enum reph_position_t { |
| 602 | REPH_AFTER_MAIN, /* Malayalam, Oriya */ |
| 603 | REPH_BEFORE_SUBSCRIPT, /* Gurmukhi */ |
| 604 | REPH_AFTER_SUBSCRIPT, /* Bengali */ |
| 605 | REPH_BEFORE_POSTSCRIPT, /* Devanagari, Gujarati */ |
| 606 | REPH_AFTER_POSTSCRIPT, /* Kannada, Tamil, Telugu */ |
| 607 | } reph_pos = REPH_BEFORE_POSTSCRIPT; /* XXX */ /* XXX Figure out old behavior too */ |
| 608 | |
Behdad Esfahbod | dbb1058 | 2012-05-10 13:45:52 +0200 | [diff] [blame] | 609 | /* 1. If reph should be positioned after post-base consonant forms, |
| 610 | * proceed to step 5. |
Behdad Esfahbod | 02b2922 | 2012-05-10 21:44:50 +0200 | [diff] [blame] | 611 | */ |
| 612 | reph_step_1: |
| 613 | { |
| 614 | if (reph_pos == REPH_AFTER_POSTSCRIPT) |
| 615 | goto reph_step_5; |
| 616 | } |
| 617 | |
| 618 | /* 2. If the reph repositioning class is not after post-base: target |
Behdad Esfahbod | dbb1058 | 2012-05-10 13:45:52 +0200 | [diff] [blame] | 619 | * position is after the first explicit halant glyph between the |
| 620 | * first post-reph consonant and last main consonant. If ZWJ or ZWNJ |
| 621 | * are following this halant, position is moved after it. If such |
| 622 | * position is found, this is the target position. Otherwise, |
| 623 | * proceed to the next step. |
| 624 | * |
| 625 | * Note: in old-implementation fonts, where classifications were |
| 626 | * fixed in shaping engine, there was no case where reph position |
| 627 | * will be found on this step. |
Behdad Esfahbod | 02b2922 | 2012-05-10 21:44:50 +0200 | [diff] [blame] | 628 | */ |
| 629 | reph_step_2: |
| 630 | { |
| 631 | new_reph_pos = start + 1; |
| 632 | while (new_reph_pos < base && info[new_reph_pos].indic_category() != OT_H) |
| 633 | new_reph_pos++; |
| 634 | |
| 635 | if (new_reph_pos < base && info[new_reph_pos].indic_category() == OT_H) { |
| 636 | /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */ |
| 637 | if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1])) |
| 638 | new_reph_pos++; |
| 639 | goto reph_move; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | /* 3. If reph should be repositioned after the main consonant: find the |
Behdad Esfahbod | dbb1058 | 2012-05-10 13:45:52 +0200 | [diff] [blame] | 644 | * first consonant not ligated with main, or find the first |
| 645 | * consonant that is not a potential pre-base reordering Ra. |
Behdad Esfahbod | 02b2922 | 2012-05-10 21:44:50 +0200 | [diff] [blame] | 646 | */ |
| 647 | reph_step_3: |
| 648 | { |
| 649 | /* XXX */ |
| 650 | } |
| 651 | |
| 652 | /* 4. If reph should be positioned before post-base consonant, find |
Behdad Esfahbod | dbb1058 | 2012-05-10 13:45:52 +0200 | [diff] [blame] | 653 | * first post-base classified consonant not ligated with main. If no |
| 654 | * consonant is found, the target position should be before the |
| 655 | * first matra, syllable modifier sign or vedic sign. |
Behdad Esfahbod | 02b2922 | 2012-05-10 21:44:50 +0200 | [diff] [blame] | 656 | */ |
| 657 | reph_step_4: |
| 658 | { |
| 659 | /* XXX */ |
| 660 | } |
| 661 | |
| 662 | /* 5. If no consonant is found in steps 3 or 4, move reph to a position |
Behdad Esfahbod | dbb1058 | 2012-05-10 13:45:52 +0200 | [diff] [blame] | 663 | * immediately before the first post-base matra, syllable modifier |
| 664 | * sign or vedic sign that has a reordering class after the intended |
| 665 | * reph position. For example, if the reordering position for reph |
| 666 | * is post-main, it will skip above-base matras that also have a |
| 667 | * post-main position. |
Behdad Esfahbod | dbb1058 | 2012-05-10 13:45:52 +0200 | [diff] [blame] | 668 | */ |
Behdad Esfahbod | 02b2922 | 2012-05-10 21:44:50 +0200 | [diff] [blame] | 669 | reph_step_5: |
| 670 | { |
| 671 | /* XXX */ |
Behdad Esfahbod | 8df5636 | 2012-05-10 15:41:04 +0200 | [diff] [blame] | 672 | } |
| 673 | |
Behdad Esfahbod | 02b2922 | 2012-05-10 21:44:50 +0200 | [diff] [blame] | 674 | /* 6. Otherwise, reorder reph to the end of the syllable. |
| 675 | */ |
| 676 | reph_step_6: |
| 677 | { |
| 678 | new_reph_pos = end - 1; |
| 679 | while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD) |
| 680 | new_reph_pos--; |
| 681 | |
Behdad Esfahbod | 892eb78 | 2012-05-11 16:54:40 +0200 | [diff] [blame] | 682 | /* |
| 683 | * If the Reph is to be ending up after a Matra,Halant sequence, |
| 684 | * position it before that Halant so it can interact with the Matra. |
| 685 | * However, if it's a plain Consonant,Halant we shouldn't do that. |
| 686 | * Uniscribe doesn't do this. |
| 687 | * TEST: U+0930,U+094D,U+0915,U+094B,U+094D |
| 688 | */ |
| 689 | if (!options.uniscribe_bug_compatible && |
| 690 | unlikely (info[new_reph_pos].indic_category() == OT_H)) { |
Behdad Esfahbod | 02b2922 | 2012-05-10 21:44:50 +0200 | [diff] [blame] | 691 | for (unsigned int i = base + 1; i < new_reph_pos; i++) |
| 692 | if (info[i].indic_category() == OT_M) { |
| 693 | /* Ok, got it. */ |
| 694 | new_reph_pos--; |
| 695 | } |
| 696 | } |
| 697 | goto reph_move; |
| 698 | } |
| 699 | |
| 700 | reph_move: |
| 701 | { |
| 702 | /* Move */ |
| 703 | hb_glyph_info_t reph = info[start]; |
| 704 | memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0])); |
| 705 | info[new_reph_pos] = reph; |
| 706 | start_of_last_cluster = start; /* Yay, one big cluster! */ |
| 707 | } |
Behdad Esfahbod | dbb1058 | 2012-05-10 13:45:52 +0200 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | |
| 711 | /* o Reorder pre-base reordering consonants: |
Behdad Esfahbod | e7be057 | 2011-07-31 15:18:57 -0400 | [diff] [blame] | 712 | * |
| 713 | * If a pre-base reordering consonant is found, reorder it according to |
| 714 | * the following rules: |
| 715 | * |
| 716 | * 1. Only reorder a glyph produced by substitution during application |
| 717 | * of the feature. (Note that a font may shape a Ra consonant with |
| 718 | * the feature generally but block it in certain contexts.) |
| 719 | * |
| 720 | * 2. Try to find a target position the same way as for pre-base matra. |
| 721 | * If it is found, reorder pre-base consonant glyph. |
| 722 | * |
| 723 | * 3. If position is not found, reorder immediately before main |
| 724 | * consonant. |
| 725 | */ |
| 726 | |
Behdad Esfahbod | 21d2803 | 2012-05-10 18:34:34 +0200 | [diff] [blame] | 727 | |
| 728 | /* Finish off the clusters and go home! */ |
| 729 | |
Behdad Esfahbod | ebe2973 | 2012-05-11 16:43:12 +0200 | [diff] [blame] | 730 | if (!options.uniscribe_bug_compatible) |
| 731 | { |
Behdad Esfahbod | 21d2803 | 2012-05-10 18:34:34 +0200 | [diff] [blame] | 732 | /* This is what Uniscribe does. Ie. add cluster boundaries after Halant,ZWNJ. |
| 733 | * This means, half forms are submerged into the main consonants cluster. |
| 734 | * This is unnecessary, and makes cursor positioning harder, but that's what |
| 735 | * Uniscribe does. */ |
| 736 | unsigned int cluster_start = start; |
| 737 | for (unsigned int i = start + 1; i < start_of_last_cluster; i++) |
| 738 | if (info[i - 1].indic_category() == OT_H && info[i].indic_category() == OT_ZWNJ) { |
| 739 | i++; |
| 740 | buffer->merge_clusters (cluster_start, i); |
| 741 | cluster_start = i; |
| 742 | } |
| 743 | start_of_last_cluster = cluster_start; |
| 744 | } |
| 745 | |
| 746 | buffer->merge_clusters (start_of_last_cluster, end); |
Behdad Esfahbod | ef24cc8 | 2012-05-09 17:56:03 +0200 | [diff] [blame] | 747 | } |
Behdad Esfahbod | e7be057 | 2011-07-31 15:18:57 -0400 | [diff] [blame] | 748 | |
| 749 | |
Behdad Esfahbod | ef24cc8 | 2012-05-09 17:56:03 +0200 | [diff] [blame] | 750 | static void |
| 751 | final_reordering (const hb_ot_map_t *map, |
| 752 | hb_face_t *face, |
| 753 | hb_buffer_t *buffer, |
| 754 | void *user_data HB_UNUSED) |
| 755 | { |
| 756 | unsigned int count = buffer->len; |
| 757 | if (!count) return; |
Behdad Esfahbod | e7be057 | 2011-07-31 15:18:57 -0400 | [diff] [blame] | 758 | |
Behdad Esfahbod | ef24cc8 | 2012-05-09 17:56:03 +0200 | [diff] [blame] | 759 | hb_glyph_info_t *info = buffer->info; |
| 760 | unsigned int last = 0; |
Behdad Esfahbod | cee7187 | 2012-05-11 11:41:39 +0200 | [diff] [blame] | 761 | unsigned int last_syllable = info[0].syllable(); |
Behdad Esfahbod | ef24cc8 | 2012-05-09 17:56:03 +0200 | [diff] [blame] | 762 | for (unsigned int i = 1; i < count; i++) |
Behdad Esfahbod | cee7187 | 2012-05-11 11:41:39 +0200 | [diff] [blame] | 763 | if (last_syllable != info[i].syllable()) { |
Behdad Esfahbod | ef24cc8 | 2012-05-09 17:56:03 +0200 | [diff] [blame] | 764 | final_reordering_syllable (buffer, last, i); |
| 765 | last = i; |
Behdad Esfahbod | cee7187 | 2012-05-11 11:41:39 +0200 | [diff] [blame] | 766 | last_syllable = info[last].syllable(); |
Behdad Esfahbod | ef24cc8 | 2012-05-09 17:56:03 +0200 | [diff] [blame] | 767 | } |
| 768 | final_reordering_syllable (buffer, last, count); |
| 769 | |
Behdad Esfahbod | 743807a | 2011-07-29 16:37:02 -0400 | [diff] [blame] | 770 | HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category); |
| 771 | HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position); |
| 772 | } |
| 773 | |
| 774 | |
Behdad Esfahbod | b9ddbd5 | 2011-06-02 17:43:12 -0400 | [diff] [blame] | 775 | |