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