blob: c063e5bd0f6a3991af7bd054aa9007ab12c2f7d6 [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 Esfahbod3ed46342012-04-19 22:34:06 -040027#include "hb-ot-shape-complex-indic-private.hh"
Behdad Esfahbod352372a2011-07-30 19:04:02 -040028
Behdad Esfahbodebe29732012-05-11 16:43:12 +020029static 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 Esfahbod743807a2011-07-29 16:37:02 -040040static int
41compare_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
49static indic_position_t
50consonant_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 Esfahboddbccf872012-05-09 17:24:39 +020059 return record ? record->position : POS_BASE_C;
Behdad Esfahbod743807a2011-07-29 16:37:02 -040060}
61
Behdad Esfahbod352372a2011-07-30 19:04:02 -040062static bool
63is_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 Esfahbod9ee27a92011-07-31 11:10:14 -040071static bool
Behdad Esfahbod9da04872011-07-31 13:46:44 -040072is_joiner (const hb_glyph_info_t &info)
Behdad Esfahbod9ee27a92011-07-31 11:10:14 -040073{
Behdad Esfahbod9da04872011-07-31 13:46:44 -040074 return !!(FLAG (info.indic_category()) & (FLAG (OT_ZWJ) | FLAG (OT_ZWNJ)));
75}
76
77static bool
78is_consonant (const hb_glyph_info_t &info)
79{
Behdad Esfahbod1a1fa8c2012-05-10 12:20:21 +020080 /* Note:
81 *
Behdad Esfahbod18c06e12012-05-11 20:02:14 +020082 * We treat Vowels and placeholders as if they were consonants. This is safe because Vowels
Behdad Esfahbodc5306b62012-05-10 12:07:33 +020083 * 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 Esfahbod18c06e12012-05-11 20:02:14 +020085 return !!(FLAG (info.indic_category()) & (FLAG (OT_C) | FLAG (OT_Ra) | FLAG (OT_V) | FLAG (OT_NBSP) | FLAG (OT_DOTTEDCIRCLE)));
Behdad Esfahbod9ee27a92011-07-31 11:10:14 -040086}
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -040087
88static const struct {
89 hb_tag_t tag;
90 hb_bool_t is_global;
91} indic_basic_features[] =
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -040092{
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -040093 {HB_TAG('n','u','k','t'), true},
94 {HB_TAG('a','k','h','n'), false},
95 {HB_TAG('r','p','h','f'), false},
Behdad Esfahbod1ac075b2012-05-09 11:06:47 +020096 {HB_TAG('r','k','r','f'), true},
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -040097 {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 Esfahbod9da04872011-07-31 13:46:44 -0400102 {HB_TAG('c','j','c','t'), false},
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400103};
104
105/* Same order as the indic_basic_features array */
106enum {
107 _NUKT,
108 AKHN,
109 RPHF,
Behdad Esfahboddf6d45c2012-05-09 11:38:31 +0200110 _RKRF,
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400111 PREF,
112 BLWF,
113 HALF,
114 _VATU,
115 PSTF,
Behdad Esfahbode8eedf22012-01-16 16:39:40 -0500116 CJCT
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400117};
118
119static 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 Esfahbod743807a2011-07-29 16:37:02 -0400132
133static void
134initial_reordering (const hb_ot_map_t *map,
135 hb_face_t *face,
136 hb_buffer_t *buffer,
137 void *user_data HB_UNUSED);
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400138static void
139final_reordering (const hb_ot_map_t *map,
140 hb_face_t *face,
141 hb_buffer_t *buffer,
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400142 void *user_data HB_UNUSED);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400143
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400144void
Behdad Esfahbod76f76812011-07-07 22:25:25 -0400145_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 -0400146{
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400147 map->add_bool_feature (HB_TAG('l','o','c','l'));
Behdad Esfahboda54a5502011-07-20 16:42:10 -0400148 /* 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 Esfahbodf6fd3782011-07-08 00:22:40 -0400150 map->add_bool_feature (HB_TAG('c','c','m','p'));
151
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400152 map->add_gsub_pause (initial_reordering, NULL);
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400153
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200154 for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++) {
Behdad Esfahbod76f76812011-07-07 22:25:25 -0400155 map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200156 map->add_gsub_pause (NULL, NULL);
157 }
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400158
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400159 map->add_gsub_pause (final_reordering, NULL);
160
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200161 for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++) {
Behdad Esfahbod76f76812011-07-07 22:25:25 -0400162 map->add_bool_feature (indic_other_features[i], true);
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200163 map->add_gsub_pause (NULL, NULL);
164 }
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400165}
166
Behdad Esfahbod867361c2011-06-17 18:35:46 -0400167
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400168hb_ot_shape_normalization_mode_t
169_hb_ot_shape_complex_normalization_preference_indic (void)
Behdad Esfahbod02cdf742011-07-21 12:23:12 -0400170{
171 /* We want split matras decomposed by the common shaping logic. */
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400172 return HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED;
Behdad Esfahbod02cdf742011-07-21 12:23:12 -0400173}
174
Behdad Esfahbod867361c2011-06-17 18:35:46 -0400175
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400176void
Behdad Esfahbodacd88e62012-04-10 18:02:20 -0400177_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 -0400178{
Behdad Esfahbod9f9bcce2011-07-28 17:06:46 -0400179 HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
180 HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
181
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400182 /* 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 Esfahbodb9ddbd52011-06-02 17:43:12 -0400186 for (unsigned int i = 0; i < count; i++)
187 {
Behdad Esfahbod92332e52012-05-09 17:40:00 +0200188 hb_glyph_info_t &info = buffer->info[i];
189 unsigned int type = get_indic_categories (info.codepoint);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400190
Behdad Esfahbod92332e52012-05-09 17:40:00 +0200191 info.indic_category() = type & 0x0F;
192 info.indic_position() = type >> 4;
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400193
Behdad Esfahbod3399a062012-05-11 17:54:26 +0200194 /* 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 Esfahbod92332e52012-05-09 17:40:00 +0200205 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 Esfahbod18c06e12012-05-11 20:02:14 +0200216 else if (unlikely (info.codepoint == 0x25CC))
217 info.indic_category() = OT_DOTTEDCIRCLE;
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400218 }
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400219}
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400220
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400221static int
222compare_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 Esfahbod867361c2011-06-17 18:35:46 -0400229
Behdad Esfahbod7ea58db2012-05-11 18:58:57 +0200230/* Rules from:
231 * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
232
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400233static void
Behdad Esfahbod08310612012-05-11 19:07:58 +0200234initial_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
237static void
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200238initial_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 Esfahbod743807a2011-07-29 16:37:02 -0400240{
Behdad Esfahbodee58f3b2011-07-30 19:15:53 -0400241 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400242
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400243 /* 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 Esfahbod5e720712011-07-31 17:51:50 -0400257 unsigned int base = end;
Behdad Esfahbod76b34092012-05-09 11:43:43 +0200258 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 Esfahbod6d8e0cb2012-05-10 11:41:51 +0200265 start + 3 <= end &&
Behdad Esfahbod76b34092012-05-09 11:43:43 +0200266 info[start].indic_category() == OT_Ra &&
Behdad Esfahbod6d8e0cb2012-05-10 11:41:51 +0200267 info[start + 1].indic_category() == OT_H &&
268 !is_joiner (info[start + 2]))
Behdad Esfahbod76b34092012-05-09 11:43:43 +0200269 {
270 limit += 2;
271 base = start;
272 has_reph = true;
273 };
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400274
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200275 {
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 Esfahbod743807a2011-07-29 16:37:02 -0400282 {
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200283 /* -> 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 Esfahbod743807a2011-07-29 16:37:02 -0400300 }
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200301 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 Esfahbod743807a2011-07-29 16:37:02 -0400308
Behdad Esfahbod08310612012-05-11 19:07:58 +0200309 /* Continue reading below ------v */
310 initial_reordering_syllable (map, buffer, mask_array, start, end, base);
311}
Behdad Esfahbod3d250792012-05-10 11:37:42 +0200312
Behdad Esfahbod08310612012-05-11 19:07:58 +0200313static void
314initial_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 Esfahbod3d250792012-05-10 11:37:42 +0200318
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400319 /* 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 Esfahbod45d6f292011-07-30 14:44:30 -0400351 /* Reorder characters */
352
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200353 for (unsigned int i = start; i < base; i++)
Behdad Esfahboddbccf872012-05-09 17:24:39 +0200354 info[i].indic_position() = POS_PRE_C;
355 info[base].indic_position() = POS_BASE_C;
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400356
Behdad Esfahbodfd06bf52011-07-30 20:14:44 -0400357 /* Handle beginning Ra */
Behdad Esfahbod08310612012-05-11 19:07:58 +0200358 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 Esfahboddbb10582012-05-10 13:45:52 +0200364 info[start].indic_position() = POS_RA_TO_BECOME_REPH;
Behdad Esfahbodfd06bf52011-07-30 20:14:44 -0400365
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400366 /* 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 Esfahbod3c2ea942012-05-11 16:23:38 +0200370 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400371 if (info[i].indic_category() == OT_H) {
372 unsigned int j;
373 for (j = end - 1; j > i; j--)
Behdad Esfahbod190eb312012-05-10 12:17:16 +0200374 if (is_consonant (info[j]))
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400375 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 Esfahbod45d6f292011-07-30 14:44:30 -0400386 /* Attach ZWJ, ZWNJ, nukta, and halant to previous char to move with them. */
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200387 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 Esfahbod67ea29a2012-05-11 16:51:23 +0200394 /*
395 * Uniscribe doesn't move the Halant with Left Matra.
396 * TEST: U+092B,U+093F,U+094DE
397 */
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200398 /* 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 Esfahbodebe29732012-05-11 16:43:12 +0200405 info[i].indic_position() = info[j - 1].indic_position();
406 break;
407 }
408 }
409 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400410
411 /* We do bubble-sort, skip malicious clusters attempts */
Behdad Esfahbodb99d63a2012-05-10 11:32:52 +0200412 if (end - start < 64)
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200413 {
414 /* Sit tight, rock 'n roll! */
Behdad Esfahbodd3637ed2012-05-10 10:51:38 +0200415 hb_bubble_sort (info + start, end - start, compare_indic_order);
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200416 /* Find base again */
417 base = end;
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200418 for (unsigned int i = start; i < end; i++)
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200419 if (info[i].indic_position() == POS_BASE_C) {
420 base = i;
421 break;
422 }
423 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400424
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400425 /* Setup masks now */
426
Behdad Esfahbod28168392011-07-31 16:00:35 -0400427 {
428 hb_mask_t mask;
429
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200430 /* Reph */
Behdad Esfahbod668c6042012-05-11 15:34:13 +0200431 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 Esfahboddbb10582012-05-10 13:45:52 +0200433
Behdad Esfahbod28168392011-07-31 16:00:35 -0400434 /* Pre-base */
435 mask = mask_array[HALF] | mask_array[AKHN] | mask_array[CJCT];
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200436 for (unsigned int i = start; i < base; i++)
Behdad Esfahbod28168392011-07-31 16:00:35 -0400437 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 Esfahbod3c2ea942012-05-11 16:23:38 +0200443 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbod28168392011-07-31 16:00:35 -0400444 info[i].mask |= mask;
445 }
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400446
447 /* Apply ZWJ/ZWNJ effects */
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200448 for (unsigned int i = start + 1; i < end; i++)
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400449 if (is_joiner (info[i])) {
450 bool non_joiner = info[i].indic_category() == OT_ZWNJ;
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400451 unsigned int j = i;
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400452
453 do {
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400454 j--;
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400455
Behdad Esfahbod3bf27a92012-05-11 11:17:23 +0200456 info[j].mask &= ~mask_array[CJCT];
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400457 if (non_joiner)
Behdad Esfahbodc6d904d2012-05-11 11:07:40 +0200458 info[j].mask &= ~mask_array[HALF];
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400459
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400460 } while (j > start && !is_consonant (info[j]));
461 }
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400462}
463
464
465static void
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200466initial_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 Esfahbod743807a2011-07-29 16:37:02 -0400468{
Behdad Esfahbodc5306b62012-05-10 12:07:33 +0200469 /* 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 Esfahbod743807a2011-07-29 16:37:02 -0400471}
472
473static void
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200474initial_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 Esfahbod743807a2011-07-29 16:37:02 -0400476{
Behdad Esfahbod18c06e12012-05-11 20:02:14 +0200477 /* 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 Esfahbod1a1fa8c2012-05-10 12:20:21 +0200489 initial_reordering_consonant_syllable (map, buffer, mask_array, start, end);
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400490}
491
492static void
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200493initial_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 -0400494 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
502static void
503initial_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 Esfahbodb9ddbd52011-06-02 17:43:12 -0400508 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 Esfahbod76f76812011-07-07 22:25:25 -0400511 mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400512
513 find_syllables (map, buffer, mask_array);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400514}
515
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400516static void
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200517final_reordering_syllable (hb_buffer_t *buffer,
518 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400519{
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200520 hb_glyph_info_t *info = buffer->info;
521
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400522 /* 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 Esfahbod4ac9e982012-05-10 12:53:53 +0200528 */
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 Esfahbod4705a702012-05-10 13:09:08 +0200545 unsigned int start_of_last_cluster = base;
546
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200547 /* o Reorder matras:
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400548 *
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 Esfahbod4ac9e982012-05-10 12:53:53 +0200555 */
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 Esfahbod6782bda2012-05-11 16:23:43 +0200561 /* 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 Esfahbod4ac9e982012-05-10 12:53:53 +0200565 /* -> 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 Esfahbod7708ee22012-05-10 14:48:25 +0200571 if (info[i - 1].indic_position () == POS_LEFT_MATRA)
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200572 {
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 Esfahbod4705a702012-05-10 13:09:08 +0200577 start_of_last_cluster = MIN (new_matra_pos, start_of_last_cluster);
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200578 new_matra_pos--;
579 }
580 }
581
582
583 /* o Reorder reph:
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400584 *
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 Esfahboddbb10582012-05-10 13:45:52 +0200590 */
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 Esfahbod02b29222012-05-10 21:44:50 +0200599 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 Esfahboddbb10582012-05-10 13:45:52 +0200609 /* 1. If reph should be positioned after post-base consonant forms,
610 * proceed to step 5.
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200611 */
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 Esfahboddbb10582012-05-10 13:45:52 +0200619 * 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 Esfahbod02b29222012-05-10 21:44:50 +0200628 */
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 Esfahboddbb10582012-05-10 13:45:52 +0200644 * first consonant not ligated with main, or find the first
645 * consonant that is not a potential pre-base reordering Ra.
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200646 */
647 reph_step_3:
648 {
649 /* XXX */
650 }
651
652 /* 4. If reph should be positioned before post-base consonant, find
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200653 * 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 Esfahbod02b29222012-05-10 21:44:50 +0200656 */
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 Esfahboddbb10582012-05-10 13:45:52 +0200663 * 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 Esfahboddbb10582012-05-10 13:45:52 +0200668 */
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200669 reph_step_5:
670 {
671 /* XXX */
Behdad Esfahbod8df56362012-05-10 15:41:04 +0200672 }
673
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200674 /* 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 Esfahbod892eb782012-05-11 16:54:40 +0200682 /*
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 Esfahbod02b29222012-05-10 21:44:50 +0200691 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 Esfahboddbb10582012-05-10 13:45:52 +0200708 }
709
710
711 /* o Reorder pre-base reordering consonants:
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400712 *
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 Esfahbod21d28032012-05-10 18:34:34 +0200727
728 /* Finish off the clusters and go home! */
729
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200730 if (!options.uniscribe_bug_compatible)
731 {
Behdad Esfahbod21d28032012-05-10 18:34:34 +0200732 /* 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 Esfahbodef24cc82012-05-09 17:56:03 +0200747}
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400748
749
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200750static void
751final_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 Esfahbode7be0572011-07-31 15:18:57 -0400758
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200759 hb_glyph_info_t *info = buffer->info;
760 unsigned int last = 0;
Behdad Esfahbodcee71872012-05-11 11:41:39 +0200761 unsigned int last_syllable = info[0].syllable();
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200762 for (unsigned int i = 1; i < count; i++)
Behdad Esfahbodcee71872012-05-11 11:41:39 +0200763 if (last_syllable != info[i].syllable()) {
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200764 final_reordering_syllable (buffer, last, i);
765 last = i;
Behdad Esfahbodcee71872012-05-11 11:41:39 +0200766 last_syllable = info[last].syllable();
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200767 }
768 final_reordering_syllable (buffer, last, count);
769
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400770 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
771 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
772}
773
774
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400775