blob: 99c22683f757a1895b8a01ea3bb0a040831a99fe [file] [log] [blame]
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -04001/*
Behdad Esfahbod20503cc2011-06-07 17:02:48 -04002 * Copyright © 2011 Google, Inc.
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -04003 *
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 Esfahbodb9ddbd52011-06-02 17:43:12 -040027
Behdad Esfahbod3ed46342012-04-19 22:34:06 -040028#include "hb-ot-shape-complex-indic-private.hh"
Behdad Esfahbod352372a2011-07-30 19:04:02 -040029
Behdad Esfahbod743807a2011-07-29 16:37:02 -040030static int
31compare_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
39static indic_position_t
40consonant_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 Esfahboddbccf872012-05-09 17:24:39 +020049 return record ? record->position : POS_BASE_C;
Behdad Esfahbod743807a2011-07-29 16:37:02 -040050}
51
Behdad Esfahbod352372a2011-07-30 19:04:02 -040052static bool
53is_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 Esfahbod9ee27a92011-07-31 11:10:14 -040061static bool
Behdad Esfahbod9da04872011-07-31 13:46:44 -040062is_joiner (const hb_glyph_info_t &info)
Behdad Esfahbod9ee27a92011-07-31 11:10:14 -040063{
Behdad Esfahbod9da04872011-07-31 13:46:44 -040064 return !!(FLAG (info.indic_category()) & (FLAG (OT_ZWJ) | FLAG (OT_ZWNJ)));
65}
66
67static bool
68is_consonant (const hb_glyph_info_t &info)
69{
Behdad Esfahbod1a1fa8c2012-05-10 12:20:21 +020070 /* Note:
71 *
72 * We treat Vowels and NBSP as if they were consonants. This is safe because Vowels
Behdad Esfahbodc5306b62012-05-10 12:07:33 +020073 * 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 Esfahbod1a1fa8c2012-05-10 12:20:21 +020075 return !!(FLAG (info.indic_category()) & (FLAG (OT_C) | FLAG (OT_Ra) | FLAG (OT_V) | FLAG (OT_NBSP)));
Behdad Esfahbod9ee27a92011-07-31 11:10:14 -040076}
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -040077
78static const struct {
79 hb_tag_t tag;
80 hb_bool_t is_global;
81} indic_basic_features[] =
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -040082{
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -040083 {HB_TAG('n','u','k','t'), true},
84 {HB_TAG('a','k','h','n'), false},
85 {HB_TAG('r','p','h','f'), false},
Behdad Esfahbod1ac075b2012-05-09 11:06:47 +020086 {HB_TAG('r','k','r','f'), true},
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -040087 {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 Esfahbod9da04872011-07-31 13:46:44 -040092 {HB_TAG('c','j','c','t'), false},
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -040093};
94
95/* Same order as the indic_basic_features array */
96enum {
97 _NUKT,
98 AKHN,
99 RPHF,
Behdad Esfahboddf6d45c2012-05-09 11:38:31 +0200100 _RKRF,
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400101 PREF,
102 BLWF,
103 HALF,
104 _VATU,
105 PSTF,
Behdad Esfahbode8eedf22012-01-16 16:39:40 -0500106 CJCT
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400107};
108
109static 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 Esfahbod743807a2011-07-29 16:37:02 -0400122
123static void
124initial_reordering (const hb_ot_map_t *map,
125 hb_face_t *face,
126 hb_buffer_t *buffer,
127 void *user_data HB_UNUSED);
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400128static void
129final_reordering (const hb_ot_map_t *map,
130 hb_face_t *face,
131 hb_buffer_t *buffer,
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400132 void *user_data HB_UNUSED);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400133
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400134void
Behdad Esfahbod76f76812011-07-07 22:25:25 -0400135_hb_ot_shape_complex_collect_features_indic (hb_ot_map_builder_t *map, const hb_segment_properties_t *props)
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400136{
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400137 map->add_bool_feature (HB_TAG('l','o','c','l'));
Behdad Esfahboda54a5502011-07-20 16:42:10 -0400138 /* 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 Esfahbodf6fd3782011-07-08 00:22:40 -0400140 map->add_bool_feature (HB_TAG('c','c','m','p'));
141
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400142 map->add_gsub_pause (initial_reordering, NULL);
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400143
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200144 for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++) {
Behdad Esfahbod76f76812011-07-07 22:25:25 -0400145 map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200146 map->add_gsub_pause (NULL, NULL);
147 }
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400148
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400149 map->add_gsub_pause (final_reordering, NULL);
150
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200151 for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++) {
Behdad Esfahbod76f76812011-07-07 22:25:25 -0400152 map->add_bool_feature (indic_other_features[i], true);
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200153 map->add_gsub_pause (NULL, NULL);
154 }
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400155}
156
Behdad Esfahbod867361c2011-06-17 18:35:46 -0400157
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400158hb_ot_shape_normalization_mode_t
159_hb_ot_shape_complex_normalization_preference_indic (void)
Behdad Esfahbod02cdf742011-07-21 12:23:12 -0400160{
161 /* We want split matras decomposed by the common shaping logic. */
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400162 return HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED;
Behdad Esfahbod02cdf742011-07-21 12:23:12 -0400163}
164
Behdad Esfahbod867361c2011-06-17 18:35:46 -0400165
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400166void
Behdad Esfahbodacd88e62012-04-10 18:02:20 -0400167_hb_ot_shape_complex_setup_masks_indic (hb_ot_map_t *map, hb_buffer_t *buffer, hb_font_t *font)
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400168{
Behdad Esfahbod9f9bcce2011-07-28 17:06:46 -0400169 HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
170 HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
171
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400172 /* 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 Esfahbodb9ddbd52011-06-02 17:43:12 -0400176 for (unsigned int i = 0; i < count; i++)
177 {
Behdad Esfahbod92332e52012-05-09 17:40:00 +0200178 hb_glyph_info_t &info = buffer->info[i];
179 unsigned int type = get_indic_categories (info.codepoint);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400180
Behdad Esfahbod92332e52012-05-09 17:40:00 +0200181 info.indic_category() = type & 0x0F;
182 info.indic_position() = type >> 4;
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400183
Behdad Esfahbod92332e52012-05-09 17:40:00 +0200184 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 Esfahbod33c92e72012-05-09 15:41:51 +0200195
Behdad Esfahbod92332e52012-05-09 17:40:00 +0200196 if (unlikely (info.codepoint == 0x0952)) {
197 info.indic_category() = OT_A;
198 info.indic_position() = POS_SMVD;
Behdad Esfahbod33c92e72012-05-09 15:41:51 +0200199 }
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400200 }
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400201}
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400202
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400203static int
204compare_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 Esfahbod867361c2011-06-17 18:35:46 -0400211
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400212static void
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200213initial_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 Esfahbod743807a2011-07-29 16:37:02 -0400215{
Behdad Esfahbodee58f3b2011-07-30 19:15:53 -0400216 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400217
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 Esfahbod5e720712011-07-31 17:51:50 -0400235 unsigned int base = end;
Behdad Esfahbod76b34092012-05-09 11:43:43 +0200236 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 Esfahbod6d8e0cb2012-05-10 11:41:51 +0200243 start + 3 <= end &&
Behdad Esfahbod76b34092012-05-09 11:43:43 +0200244 info[start].indic_category() == OT_Ra &&
Behdad Esfahbod6d8e0cb2012-05-10 11:41:51 +0200245 info[start + 1].indic_category() == OT_H &&
246 !is_joiner (info[start + 2]))
Behdad Esfahbod76b34092012-05-09 11:43:43 +0200247 {
248 limit += 2;
249 base = start;
250 has_reph = true;
251 };
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400252
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200253 {
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 Esfahbod743807a2011-07-29 16:37:02 -0400260 {
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200261 /* -> 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 Esfahbod743807a2011-07-29 16:37:02 -0400278 }
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200279 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 Esfahbod743807a2011-07-29 16:37:02 -0400286
Behdad Esfahbod3d250792012-05-10 11:37:42 +0200287 /* -> 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 Esfahbod743807a2011-07-29 16:37:02 -0400296 /* 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 Esfahbod45d6f292011-07-30 14:44:30 -0400328 /* Reorder characters */
329
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200330 for (unsigned int i = start; i < base; i++)
Behdad Esfahboddbccf872012-05-09 17:24:39 +0200331 info[i].indic_position() = POS_PRE_C;
332 info[base].indic_position() = POS_BASE_C;
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400333
Behdad Esfahbodfd06bf52011-07-30 20:14:44 -0400334 /* Handle beginning Ra */
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200335 if (has_reph)
336 info[start].indic_position() = POS_RA_TO_BECOME_REPH;
Behdad Esfahbodfd06bf52011-07-30 20:14:44 -0400337
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400338 /* 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 Esfahbod3c2ea942012-05-11 16:23:38 +0200342 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400343 if (info[i].indic_category() == OT_H) {
344 unsigned int j;
345 for (j = end - 1; j > i; j--)
Behdad Esfahbod190eb312012-05-10 12:17:16 +0200346 if (is_consonant (info[j]))
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400347 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 Esfahbod45d6f292011-07-30 14:44:30 -0400358 /* Attach ZWJ, ZWNJ, nukta, and halant to previous char to move with them. */
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200359 for (unsigned int i = start + 1; i < end; i++)
Behdad Esfahbodee58f3b2011-07-30 19:15:53 -0400360 if ((FLAG (info[i].indic_category()) &
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400361 (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H))))
Behdad Esfahbodee58f3b2011-07-30 19:15:53 -0400362 info[i].indic_position() = info[i - 1].indic_position();
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400363
364 /* We do bubble-sort, skip malicious clusters attempts */
Behdad Esfahbodb99d63a2012-05-10 11:32:52 +0200365 if (end - start < 64)
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200366 {
367 /* Sit tight, rock 'n roll! */
Behdad Esfahbodd3637ed2012-05-10 10:51:38 +0200368 hb_bubble_sort (info + start, end - start, compare_indic_order);
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200369 /* Find base again */
370 base = end;
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200371 for (unsigned int i = start; i < end; i++)
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200372 if (info[i].indic_position() == POS_BASE_C) {
373 base = i;
374 break;
375 }
376 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400377
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400378 /* Setup masks now */
379
Behdad Esfahbod28168392011-07-31 16:00:35 -0400380 {
381 hb_mask_t mask;
382
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200383 /* Reph */
Behdad Esfahbod668c6042012-05-11 15:34:13 +0200384 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 Esfahboddbb10582012-05-10 13:45:52 +0200386
Behdad Esfahbod28168392011-07-31 16:00:35 -0400387 /* Pre-base */
388 mask = mask_array[HALF] | mask_array[AKHN] | mask_array[CJCT];
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200389 for (unsigned int i = start; i < base; i++)
Behdad Esfahbod28168392011-07-31 16:00:35 -0400390 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 Esfahbod3c2ea942012-05-11 16:23:38 +0200396 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbod28168392011-07-31 16:00:35 -0400397 info[i].mask |= mask;
398 }
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400399
400 /* Apply ZWJ/ZWNJ effects */
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200401 for (unsigned int i = start + 1; i < end; i++)
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400402 if (is_joiner (info[i])) {
403 bool non_joiner = info[i].indic_category() == OT_ZWNJ;
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400404 unsigned int j = i;
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400405
406 do {
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400407 j--;
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400408
Behdad Esfahbod3bf27a92012-05-11 11:17:23 +0200409 info[j].mask &= ~mask_array[CJCT];
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400410 if (non_joiner)
Behdad Esfahbodc6d904d2012-05-11 11:07:40 +0200411 info[j].mask &= ~mask_array[HALF];
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400412
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400413 } while (j > start && !is_consonant (info[j]));
414 }
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400415}
416
417
418static void
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200419initial_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 Esfahbod743807a2011-07-29 16:37:02 -0400421{
Behdad Esfahbodc5306b62012-05-10 12:07:33 +0200422 /* 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 Esfahbod743807a2011-07-29 16:37:02 -0400424}
425
426static void
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200427initial_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 Esfahbod743807a2011-07-29 16:37:02 -0400429{
Behdad Esfahbod1a1fa8c2012-05-10 12:20:21 +0200430 /* 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 Esfahbod743807a2011-07-29 16:37:02 -0400432}
433
434static void
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200435initial_reordering_non_indic (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400436 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
444static void
445initial_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 Esfahbodb9ddbd52011-06-02 17:43:12 -0400450 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 Esfahbod76f76812011-07-07 22:25:25 -0400453 mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400454
455 find_syllables (map, buffer, mask_array);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400456}
457
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400458static void
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200459final_reordering_syllable (hb_buffer_t *buffer,
460 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400461{
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200462 hb_glyph_info_t *info = buffer->info;
463
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400464 /* 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 Esfahbod4ac9e982012-05-10 12:53:53 +0200470 */
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 Esfahbod4705a702012-05-10 13:09:08 +0200487 unsigned int start_of_last_cluster = base;
488
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200489 /* o Reorder matras:
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400490 *
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 Esfahbod4ac9e982012-05-10 12:53:53 +0200497 */
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 Esfahbod7708ee22012-05-10 14:48:25 +0200511 if (info[i - 1].indic_position () == POS_LEFT_MATRA)
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200512 {
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 Esfahbod4705a702012-05-10 13:09:08 +0200517 start_of_last_cluster = MIN (new_matra_pos, start_of_last_cluster);
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200518 new_matra_pos--;
519 }
520 }
521
522
523 /* o Reorder reph:
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400524 *
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 Esfahboddbb10582012-05-10 13:45:52 +0200530 */
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 Esfahbod02b29222012-05-10 21:44:50 +0200539 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 Esfahboddbb10582012-05-10 13:45:52 +0200549 /* 1. If reph should be positioned after post-base consonant forms,
550 * proceed to step 5.
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200551 */
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 Esfahboddbb10582012-05-10 13:45:52 +0200559 * 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 Esfahbod02b29222012-05-10 21:44:50 +0200568 */
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 Esfahboddbb10582012-05-10 13:45:52 +0200584 * first consonant not ligated with main, or find the first
585 * consonant that is not a potential pre-base reordering Ra.
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200586 */
587 reph_step_3:
588 {
589 /* XXX */
590 }
591
592 /* 4. If reph should be positioned before post-base consonant, find
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200593 * 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 Esfahbod02b29222012-05-10 21:44:50 +0200596 */
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 Esfahboddbb10582012-05-10 13:45:52 +0200603 * 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 Esfahboddbb10582012-05-10 13:45:52 +0200608 */
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200609 reph_step_5:
610 {
611 /* XXX */
Behdad Esfahbod8df56362012-05-10 15:41:04 +0200612 }
613
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200614 /* 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 Esfahboddbb10582012-05-10 13:45:52 +0200644 }
645
646
647 /* o Reorder pre-base reordering consonants:
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400648 *
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 Esfahbod21d28032012-05-10 18:34:34 +0200663
664 /* Finish off the clusters and go home! */
665
Behdad Esfahbod2b70df52012-05-10 18:38:22 +0200666 /* 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 Esfahbod21d28032012-05-10 18:34:34 +0200670 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 Esfahbodef24cc82012-05-09 17:56:03 +0200686}
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400687
688
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200689static void
690final_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 Esfahbode7be0572011-07-31 15:18:57 -0400697
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200698 hb_glyph_info_t *info = buffer->info;
699 unsigned int last = 0;
Behdad Esfahbodcee71872012-05-11 11:41:39 +0200700 unsigned int last_syllable = info[0].syllable();
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200701 for (unsigned int i = 1; i < count; i++)
Behdad Esfahbodcee71872012-05-11 11:41:39 +0200702 if (last_syllable != info[i].syllable()) {
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200703 final_reordering_syllable (buffer, last, i);
704 last = i;
Behdad Esfahbodcee71872012-05-11 11:41:39 +0200705 last_syllable = info[last].syllable();
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200706 }
707 final_reordering_syllable (buffer, last, count);
708
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400709 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
710 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
711}
712
713
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400714