blob: feb5136abff3ddf06904fcd9eda54608438f0b18 [file] [log] [blame]
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -04001/*
Behdad Esfahbod27aba592012-05-24 15:00:01 -04002 * Copyright © 2011,2012 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 Esfahboda913b022012-05-11 20:59:26 +020028#include "hb-ot-shape-private.hh"
Behdad Esfahbod352372a2011-07-30 19:04:02 -040029
Behdad Esfahboda3e04be2012-07-16 13:47:19 -040030#define OLD_INDIC_TAG(script) (((hb_tag_t) script) | 0x20000000)
31#define IS_OLD_INDIC_TAG(tag) ( \
32 (tag) == OLD_INDIC_TAG (HB_SCRIPT_BENGALI) || \
33 (tag) == OLD_INDIC_TAG (HB_SCRIPT_DEVANAGARI) || \
34 (tag) == OLD_INDIC_TAG (HB_SCRIPT_GUJARATI) || \
35 (tag) == OLD_INDIC_TAG (HB_SCRIPT_GURMUKHI) || \
36 (tag) == OLD_INDIC_TAG (HB_SCRIPT_KANNADA) || \
37 (tag) == OLD_INDIC_TAG (HB_SCRIPT_MALAYALAM) || \
38 (tag) == OLD_INDIC_TAG (HB_SCRIPT_ORIYA) || \
39 (tag) == OLD_INDIC_TAG (HB_SCRIPT_TAMIL) || \
40 (tag) == OLD_INDIC_TAG (HB_SCRIPT_TELUGU) \
41 )
Behdad Esfahboda2b471d2012-06-05 15:17:44 -040042struct indic_options_t
Behdad Esfahbodebe29732012-05-11 16:43:12 +020043{
Behdad Esfahboda2b471d2012-06-05 15:17:44 -040044 int initialized : 1;
45 int uniscribe_bug_compatible : 1;
46};
47
48union indic_options_union_t {
49 int i;
50 indic_options_t opts;
51};
52ASSERT_STATIC (sizeof (int) == sizeof (indic_options_union_t));
53
54static indic_options_union_t
55indic_options_init (void)
56{
57 indic_options_union_t u;
58 u.i = 0;
59 u.opts.initialized = 1;
60
61 char *c = getenv ("HB_OT_INDIC_OPTIONS");
62 u.opts.uniscribe_bug_compatible = c && strstr (c, "uniscribe-bug-compatible");
63
64 return u;
65}
66
67inline indic_options_t
68indic_options (void)
69{
70 static indic_options_union_t options;
71
72 if (unlikely (!options.i)) {
73 /* This is idempotent and threadsafe. */
74 options = indic_options_init ();
Behdad Esfahbodebe29732012-05-11 16:43:12 +020075 }
76
Behdad Esfahboda2b471d2012-06-05 15:17:44 -040077 return options.opts;
78}
79
Behdad Esfahbodebe29732012-05-11 16:43:12 +020080
Behdad Esfahbod743807a2011-07-29 16:37:02 -040081static int
82compare_codepoint (const void *pa, const void *pb)
83{
84 hb_codepoint_t a = * (hb_codepoint_t *) pa;
85 hb_codepoint_t b = * (hb_codepoint_t *) pb;
86
87 return a < b ? -1 : a == b ? 0 : +1;
88}
89
90static indic_position_t
91consonant_position (hb_codepoint_t u)
92{
93 consonant_position_t *record;
94
Behdad Esfahbod0de771b2012-07-16 13:39:36 -040095 /* Khmer does not have pre-base half forms. */
96 if (0x1780 <= u && u <= 0x17FF)
97 return POS_BELOW_C;
98
Behdad Esfahbod743807a2011-07-29 16:37:02 -040099 record = (consonant_position_t *) bsearch (&u, consonant_positions,
100 ARRAY_LENGTH (consonant_positions),
101 sizeof (consonant_positions[0]),
102 compare_codepoint);
103
Behdad Esfahboddbccf872012-05-09 17:24:39 +0200104 return record ? record->position : POS_BASE_C;
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400105}
106
Behdad Esfahbod352372a2011-07-30 19:04:02 -0400107static bool
108is_ra (hb_codepoint_t u)
109{
110 return !!bsearch (&u, ra_chars,
111 ARRAY_LENGTH (ra_chars),
112 sizeof (ra_chars[0]),
113 compare_codepoint);
114}
115
Behdad Esfahbod9ee27a92011-07-31 11:10:14 -0400116static bool
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400117is_joiner (const hb_glyph_info_t &info)
Behdad Esfahbod9ee27a92011-07-31 11:10:14 -0400118{
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400119 return !!(FLAG (info.indic_category()) & (FLAG (OT_ZWJ) | FLAG (OT_ZWNJ)));
120}
121
122static bool
123is_consonant (const hb_glyph_info_t &info)
124{
Behdad Esfahbod1a1fa8c2012-05-10 12:20:21 +0200125 /* Note:
126 *
Behdad Esfahbod18c06e12012-05-11 20:02:14 +0200127 * We treat Vowels and placeholders as if they were consonants. This is safe because Vowels
Behdad Esfahbodc5306b62012-05-10 12:07:33 +0200128 * cannot happen in a consonant syllable. The plus side however is, we can call the
129 * consonant syllable logic from the vowel syllable function and get it all right! */
Behdad Esfahbod18c06e12012-05-11 20:02:14 +0200130 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 -0400131}
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400132
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200133struct feature_list_t {
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400134 hb_tag_t tag;
135 hb_bool_t is_global;
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200136};
137
138static const feature_list_t
139indic_basic_features[] =
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400140{
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400141 {HB_TAG('n','u','k','t'), true},
142 {HB_TAG('a','k','h','n'), false},
143 {HB_TAG('r','p','h','f'), false},
Behdad Esfahbod1ac075b2012-05-09 11:06:47 +0200144 {HB_TAG('r','k','r','f'), true},
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400145 {HB_TAG('p','r','e','f'), false},
146 {HB_TAG('b','l','w','f'), false},
147 {HB_TAG('h','a','l','f'), false},
Behdad Esfahbod29f106d2012-07-16 12:05:35 -0400148 {HB_TAG('a','b','v','f'), false},
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400149 {HB_TAG('p','s','t','f'), false},
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400150 {HB_TAG('c','j','c','t'), false},
Behdad Esfahbod1d6846d2012-05-13 18:09:29 +0200151 {HB_TAG('v','a','t','u'), true},
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400152};
153
154/* Same order as the indic_basic_features array */
155enum {
156 _NUKT,
157 AKHN,
158 RPHF,
Behdad Esfahboddf6d45c2012-05-09 11:38:31 +0200159 _RKRF,
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400160 PREF,
161 BLWF,
162 HALF,
Behdad Esfahbod29f106d2012-07-16 12:05:35 -0400163 ABVF,
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400164 PSTF,
Behdad Esfahbod1d6846d2012-05-13 18:09:29 +0200165 CJCT,
166 VATU
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400167};
168
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200169static const feature_list_t
170indic_other_features[] =
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400171{
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200172 {HB_TAG('i','n','i','t'), false},
173 {HB_TAG('p','r','e','s'), true},
174 {HB_TAG('a','b','v','s'), true},
175 {HB_TAG('b','l','w','s'), true},
176 {HB_TAG('p','s','t','s'), true},
177 {HB_TAG('h','a','l','n'), true},
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400178
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200179 {HB_TAG('d','i','s','t'), true},
180 {HB_TAG('a','b','v','m'), true},
181 {HB_TAG('b','l','w','m'), true},
182};
183
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400184
185static void
186initial_reordering (const hb_ot_map_t *map,
187 hb_face_t *face,
188 hb_buffer_t *buffer,
189 void *user_data HB_UNUSED);
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400190static void
191final_reordering (const hb_ot_map_t *map,
192 hb_face_t *face,
193 hb_buffer_t *buffer,
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400194 void *user_data HB_UNUSED);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400195
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400196void
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +0200197_hb_ot_shape_complex_collect_features_indic (hb_ot_map_builder_t *map,
198 const hb_segment_properties_t *props HB_UNUSED)
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400199{
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400200 map->add_bool_feature (HB_TAG('l','o','c','l'));
Behdad Esfahboda54a5502011-07-20 16:42:10 -0400201 /* The Indic specs do not require ccmp, but we apply it here since if
202 * there is a use of it, it's typically at the beginning. */
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400203 map->add_bool_feature (HB_TAG('c','c','m','p'));
204
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400205 map->add_gsub_pause (initial_reordering, NULL);
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400206
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200207 for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++) {
Behdad Esfahbod76f76812011-07-07 22:25:25 -0400208 map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200209 map->add_gsub_pause (NULL, NULL);
210 }
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400211
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400212 map->add_gsub_pause (final_reordering, NULL);
213
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200214 for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++) {
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200215 map->add_bool_feature (indic_other_features[i].tag, indic_other_features[i].is_global);
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200216 map->add_gsub_pause (NULL, NULL);
217 }
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400218}
219
Behdad Esfahbod867361c2011-06-17 18:35:46 -0400220
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400221hb_ot_shape_normalization_mode_t
222_hb_ot_shape_complex_normalization_preference_indic (void)
Behdad Esfahbod02cdf742011-07-21 12:23:12 -0400223{
224 /* We want split matras decomposed by the common shaping logic. */
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400225 return HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED;
Behdad Esfahbod02cdf742011-07-21 12:23:12 -0400226}
227
Behdad Esfahbod867361c2011-06-17 18:35:46 -0400228
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400229void
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +0200230_hb_ot_shape_complex_setup_masks_indic (hb_ot_map_t *map HB_UNUSED,
231 hb_buffer_t *buffer,
Behdad Esfahbod3f182362012-05-13 16:20:10 +0200232 hb_font_t *font HB_UNUSED)
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400233{
Behdad Esfahbod9f9bcce2011-07-28 17:06:46 -0400234 HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
235 HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
236
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400237 /* We cannot setup masks here. We save information about characters
238 * and setup masks later on in a pause-callback. */
239
240 unsigned int count = buffer->len;
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400241 for (unsigned int i = 0; i < count; i++)
242 {
Behdad Esfahbod92332e52012-05-09 17:40:00 +0200243 hb_glyph_info_t &info = buffer->info[i];
244 unsigned int type = get_indic_categories (info.codepoint);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400245
Behdad Esfahbod92332e52012-05-09 17:40:00 +0200246 info.indic_category() = type & 0x0F;
247 info.indic_position() = type >> 4;
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400248
Behdad Esfahbod3399a062012-05-11 17:54:26 +0200249 /* The spec says U+0952 is OT_A. However, testing shows that Uniscribe
250 * treats U+0951..U+0952 all as OT_VD.
251 * TESTS:
252 * U+092E,U+0947,U+0952
253 * U+092E,U+0952,U+0947
254 * U+092E,U+0947,U+0951
255 * U+092E,U+0951,U+0947
256 * */
257 if (unlikely (hb_in_range<hb_codepoint_t> (info.codepoint, 0x0951, 0x0954)))
258 info.indic_category() = OT_VD;
259
Behdad Esfahbod92332e52012-05-09 17:40:00 +0200260 if (info.indic_category() == OT_C) {
261 info.indic_position() = consonant_position (info.codepoint);
262 if (is_ra (info.codepoint))
263 info.indic_category() = OT_Ra;
Behdad Esfahbod7d09c982012-07-16 16:45:22 -0400264 } else if (info.indic_category() == OT_RS) {
265 info.indic_position() = POS_ABOVE_M;
Behdad Esfahbod92332e52012-05-09 17:40:00 +0200266 } else if (info.indic_category() == OT_SM ||
267 info.indic_category() == OT_VD) {
268 info.indic_position() = POS_SMVD;
269 } else if (unlikely (info.codepoint == 0x200C))
270 info.indic_category() = OT_ZWNJ;
271 else if (unlikely (info.codepoint == 0x200D))
272 info.indic_category() = OT_ZWJ;
Behdad Esfahbod18c06e12012-05-11 20:02:14 +0200273 else if (unlikely (info.codepoint == 0x25CC))
274 info.indic_category() = OT_DOTTEDCIRCLE;
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400275 }
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400276}
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400277
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400278static int
279compare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
280{
281 int a = pa->indic_position();
282 int b = pb->indic_position();
283
284 return a < b ? -1 : a == b ? 0 : +1;
285}
Behdad Esfahbod867361c2011-06-17 18:35:46 -0400286
Behdad Esfahbod7ea58db2012-05-11 18:58:57 +0200287/* Rules from:
288 * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
289
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400290static void
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400291initial_reordering_consonant_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *basic_mask_array,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200292 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400293{
Behdad Esfahbodee58f3b2011-07-30 19:15:53 -0400294 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400295
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200296
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400297 /* 1. Find base consonant:
298 *
299 * The shaping engine finds the base consonant of the syllable, using the
300 * following algorithm: starting from the end of the syllable, move backwards
301 * until a consonant is found that does not have a below-base or post-base
302 * form (post-base forms have to follow below-base forms), or that is not a
303 * pre-base reordering Ra, or arrive at the first consonant. The consonant
304 * stopped at will be the base.
305 *
306 * o If the syllable starts with Ra + Halant (in a script that has Reph)
307 * and has more than one consonant, Ra is excluded from candidates for
308 * base consonants.
309 */
310
Behdad Esfahbod5e720712011-07-31 17:51:50 -0400311 unsigned int base = end;
Behdad Esfahbod76b34092012-05-09 11:43:43 +0200312 bool has_reph = false;
313
Behdad Esfahbod76b34092012-05-09 11:43:43 +0200314 {
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200315 /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
316 * and has more than one consonant, Ra is excluded from candidates for
317 * base consonants. */
318 unsigned int limit = start;
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400319 if (basic_mask_array[RPHF] &&
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200320 start + 3 <= end &&
321 info[start].indic_category() == OT_Ra &&
322 info[start + 1].indic_category() == OT_H &&
323 !is_joiner (info[start + 2]))
324 {
325 limit += 2;
326 base = start;
327 has_reph = true;
328 };
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400329
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200330 /* -> starting from the end of the syllable, move backwards */
331 unsigned int i = end;
332 do {
333 i--;
334 /* -> until a consonant is found */
335 if (is_consonant (info[i]))
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400336 {
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200337 /* -> that does not have a below-base or post-base form
338 * (post-base forms have to follow below-base forms), */
339 if (info[i].indic_position() != POS_BELOW_C &&
340 info[i].indic_position() != POS_POST_C)
341 {
342 base = i;
343 break;
344 }
345
346 /* -> or that is not a pre-base reordering Ra,
347 *
348 * TODO
349 */
350
351 /* -> or arrive at the first consonant. The consonant stopped at will
352 * be the base. */
353 base = i;
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400354 }
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200355 else
356 if (is_joiner (info[i]))
357 break;
358 } while (i > limit);
359 if (base < start)
360 base = start; /* Just in case... */
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400361
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200362
363 /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
364 * and has more than one consonant, Ra is excluded from candidates for
365 * base consonants. */
366 if (has_reph && base == start) {
367 /* Have no other consonant, so Reph is not formed and Ra becomes base. */
368 has_reph = false;
369 }
Behdad Esfahbod5e4e21f2012-05-13 16:46:08 +0200370 }
Behdad Esfahbod3d250792012-05-10 11:37:42 +0200371
372
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400373 /* 2. Decompose and reorder Matras:
374 *
375 * Each matra and any syllable modifier sign in the cluster are moved to the
376 * appropriate position relative to the consonant(s) in the cluster. The
377 * shaping engine decomposes two- or three-part matras into their constituent
378 * parts before any repositioning. Matra characters are classified by which
379 * consonant in a conjunct they have affinity for and are reordered to the
380 * following positions:
381 *
382 * o Before first half form in the syllable
383 * o After subjoined consonants
384 * o After post-form consonant
385 * o After main consonant (for above marks)
386 *
387 * IMPLEMENTATION NOTES:
388 *
389 * The normalize() routine has already decomposed matras for us, so we don't
390 * need to worry about that.
391 */
392
393
394 /* 3. Reorder marks to canonical order:
395 *
396 * Adjacent nukta and halant or nukta and vedic sign are always repositioned
397 * if necessary, so that the nukta is first.
398 *
399 * IMPLEMENTATION NOTES:
400 *
401 * We don't need to do this: the normalize() routine already did this for us.
402 */
403
404
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400405 /* Reorder characters */
406
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200407 for (unsigned int i = start; i < base; i++)
Behdad Esfahboddbccf872012-05-09 17:24:39 +0200408 info[i].indic_position() = POS_PRE_C;
409 info[base].indic_position() = POS_BASE_C;
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400410
Behdad Esfahbodfd06bf52011-07-30 20:14:44 -0400411 /* Handle beginning Ra */
Behdad Esfahbod5e4e21f2012-05-13 16:46:08 +0200412 if (has_reph)
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200413 info[start].indic_position() = POS_RA_TO_BECOME_REPH;
Behdad Esfahbodfd06bf52011-07-30 20:14:44 -0400414
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400415 /* For old-style Indic script tags, move the first post-base Halant after
416 * last consonant. */
Behdad Esfahboda3e04be2012-07-16 13:47:19 -0400417 if (IS_OLD_INDIC_TAG (map->get_chosen_script (0))) {
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200418 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400419 if (info[i].indic_category() == OT_H) {
420 unsigned int j;
421 for (j = end - 1; j > i; j--)
Behdad Esfahbod190eb312012-05-10 12:17:16 +0200422 if (is_consonant (info[j]))
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400423 break;
424 if (j > i) {
425 /* Move Halant to after last consonant. */
426 hb_glyph_info_t t = info[i];
427 memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
428 info[j] = t;
429 }
430 break;
431 }
432 }
433
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400434 /* Attach ZWJ, ZWNJ, nukta, and halant to previous char to move with them. */
Behdad Esfahboda2b471d2012-06-05 15:17:44 -0400435 if (!indic_options ().uniscribe_bug_compatible)
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200436 {
437 /* Please update the Uniscribe branch when touching this! */
438 for (unsigned int i = start + 1; i < end; i++)
439 if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H))))
440 info[i].indic_position() = info[i - 1].indic_position();
441 } else {
Behdad Esfahbod67ea29a2012-05-11 16:51:23 +0200442 /*
443 * Uniscribe doesn't move the Halant with Left Matra.
444 * TEST: U+092B,U+093F,U+094DE
445 */
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200446 /* Please update the non-Uniscribe branch when touching this! */
447 for (unsigned int i = start + 1; i < end; i++)
448 if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H)))) {
449 info[i].indic_position() = info[i - 1].indic_position();
Behdad Esfahbod6a091df2012-05-11 21:42:27 +0200450 if (info[i].indic_category() == OT_H && info[i].indic_position() == POS_PRE_M)
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200451 for (unsigned int j = i; j > start; j--)
Behdad Esfahbod6a091df2012-05-11 21:42:27 +0200452 if (info[j - 1].indic_position() != POS_PRE_M) {
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200453 info[i].indic_position() = info[j - 1].indic_position();
454 break;
455 }
456 }
457 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400458
459 /* We do bubble-sort, skip malicious clusters attempts */
Behdad Esfahbodb99d63a2012-05-10 11:32:52 +0200460 if (end - start < 64)
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200461 {
462 /* Sit tight, rock 'n roll! */
Behdad Esfahbodd3637ed2012-05-10 10:51:38 +0200463 hb_bubble_sort (info + start, end - start, compare_indic_order);
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200464 /* Find base again */
465 base = end;
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200466 for (unsigned int i = start; i < end; i++)
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200467 if (info[i].indic_position() == POS_BASE_C) {
468 base = i;
469 break;
470 }
471 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400472
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400473 /* Setup masks now */
474
Behdad Esfahbod28168392011-07-31 16:00:35 -0400475 {
476 hb_mask_t mask;
477
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200478 /* Reph */
Behdad Esfahbod668c6042012-05-11 15:34:13 +0200479 for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++)
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400480 info[i].mask |= basic_mask_array[RPHF];
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200481
Behdad Esfahbod28168392011-07-31 16:00:35 -0400482 /* Pre-base */
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400483 mask = basic_mask_array[HALF] | basic_mask_array[AKHN] | basic_mask_array[CJCT];
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200484 for (unsigned int i = start; i < base; i++)
Behdad Esfahbod28168392011-07-31 16:00:35 -0400485 info[i].mask |= mask;
486 /* Base */
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400487 mask = basic_mask_array[AKHN] | basic_mask_array[CJCT];
Behdad Esfahbod28168392011-07-31 16:00:35 -0400488 info[base].mask |= mask;
489 /* Post-base */
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400490 mask = basic_mask_array[BLWF] | basic_mask_array[ABVF] | basic_mask_array[PSTF] | basic_mask_array[CJCT];
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200491 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbod28168392011-07-31 16:00:35 -0400492 info[i].mask |= mask;
493 }
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400494
Behdad Esfahbod17d7de92012-07-16 15:20:15 -0400495 /* XXX This will not match for old-Indic spec since the Halant-Ra order is reversed already. */
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -0400496 if (basic_mask_array[PREF] && base + 3 <= end)
Behdad Esfahbod17d7de92012-07-16 15:20:15 -0400497 {
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -0400498 /* Find a Halant,Ra sequence and mark it fore pre-base reordering processing. */
499 for (unsigned int i = base + 1; i + 1 < end; i++)
500 if (info[i].indic_category() == OT_H &&
501 info[i + 1].indic_category() == OT_Ra)
502 {
503 info[i].mask |= basic_mask_array[PREF];
504 info[i + 1].mask |= basic_mask_array[PREF];
505 break;
506 }
Behdad Esfahbod17d7de92012-07-16 15:20:15 -0400507 }
508
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400509 /* Apply ZWJ/ZWNJ effects */
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200510 for (unsigned int i = start + 1; i < end; i++)
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400511 if (is_joiner (info[i])) {
512 bool non_joiner = info[i].indic_category() == OT_ZWNJ;
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400513 unsigned int j = i;
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400514
515 do {
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400516 j--;
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400517
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400518 info[j].mask &= ~basic_mask_array[CJCT];
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400519 if (non_joiner)
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400520 info[j].mask &= ~basic_mask_array[HALF];
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400521
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400522 } while (j > start && !is_consonant (info[j]));
523 }
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400524}
525
526
527static void
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +0200528initial_reordering_vowel_syllable (const hb_ot_map_t *map,
529 hb_buffer_t *buffer,
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400530 hb_mask_t *basic_mask_array,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200531 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400532{
Behdad Esfahbodc5306b62012-05-10 12:07:33 +0200533 /* We made the vowels look like consonants. So let's call the consonant logic! */
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400534 initial_reordering_consonant_syllable (map, buffer, basic_mask_array, start, end);
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400535}
536
537static void
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +0200538initial_reordering_standalone_cluster (const hb_ot_map_t *map,
539 hb_buffer_t *buffer,
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400540 hb_mask_t *basic_mask_array,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200541 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400542{
Behdad Esfahbod18c06e12012-05-11 20:02:14 +0200543 /* We treat NBSP/dotted-circle as if they are consonants, so we should just chain.
544 * Only if not in compatibility mode that is... */
545
Behdad Esfahboda2b471d2012-06-05 15:17:44 -0400546 if (indic_options ().uniscribe_bug_compatible)
Behdad Esfahbod18c06e12012-05-11 20:02:14 +0200547 {
548 /* For dotted-circle, this is what Uniscribe does:
549 * If dotted-circle is the last glyph, it just does nothing.
550 * Ie. It doesn't form Reph. */
551 if (buffer->info[end - 1].indic_category() == OT_DOTTEDCIRCLE)
552 return;
553 }
554
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400555 initial_reordering_consonant_syllable (map, buffer, basic_mask_array, start, end);
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400556}
557
558static void
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +0200559initial_reordering_non_indic (const hb_ot_map_t *map HB_UNUSED,
560 hb_buffer_t *buffer HB_UNUSED,
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400561 hb_mask_t *basic_mask_array HB_UNUSED,
Behdad Esfahbod3f182362012-05-13 16:20:10 +0200562 unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400563{
564 /* Nothing to do right now. If we ever switch to using the output
565 * buffer in the reordering process, we'd need to next_glyph() here. */
566}
567
568#include "hb-ot-shape-complex-indic-machine.hh"
569
570static void
571initial_reordering (const hb_ot_map_t *map,
Behdad Esfahbod3f182362012-05-13 16:20:10 +0200572 hb_face_t *face HB_UNUSED,
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400573 hb_buffer_t *buffer,
574 void *user_data HB_UNUSED)
575{
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400576 hb_mask_t basic_mask_array[ARRAY_LENGTH (indic_basic_features)] = {0};
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400577 unsigned int num_masks = ARRAY_LENGTH (indic_basic_features);
578 for (unsigned int i = 0; i < num_masks; i++)
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400579 basic_mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400580
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400581 find_syllables (map, buffer, basic_mask_array);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400582}
583
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400584static void
Behdad Esfahbod46e645e2012-07-16 15:30:05 -0400585final_reordering_syllable (hb_buffer_t *buffer,
586 hb_mask_t init_mask, hb_mask_t pref_mask,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200587 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400588{
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200589 hb_glyph_info_t *info = buffer->info;
590
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400591 /* 4. Final reordering:
592 *
593 * After the localized forms and basic shaping forms GSUB features have been
594 * applied (see below), the shaping engine performs some final glyph
595 * reordering before applying all the remaining font features to the entire
596 * cluster.
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200597 */
598
599 /* Find base again */
600 unsigned int base = end;
601 for (unsigned int i = start; i < end; i++)
602 if (info[i].indic_position() == POS_BASE_C) {
603 base = i;
604 break;
605 }
606
Behdad Esfahbod4705a702012-05-10 13:09:08 +0200607 unsigned int start_of_last_cluster = base;
608
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200609 /* o Reorder matras:
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400610 *
611 * If a pre-base matra character had been reordered before applying basic
612 * features, the glyph can be moved closer to the main consonant based on
613 * whether half-forms had been formed. Actual position for the matra is
614 * defined as “after last standalone halant glyph, after initial matra
615 * position and before the main consonant”. If ZWJ or ZWNJ follow this
616 * halant, position is moved after it.
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200617 */
618
Behdad Esfahbod362d3db2012-07-16 15:15:28 -0400619 if (start < base) /* Otherwise there can't be any pre-base matra characters. */
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200620 {
Behdad Esfahbod921ce5b2012-07-16 15:26:56 -0400621 unsigned int new_pos = base - 1;
622 while (new_pos > start &&
623 !(FLAG (info[new_pos].indic_category()) & (FLAG (OT_M) | FLAG (OT_H))))
624 new_pos--;
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200625 /* If we found no Halant we are done. Otherwise only proceed if the Halant does
626 * not belong to the Matra itself! */
Behdad Esfahbod921ce5b2012-07-16 15:26:56 -0400627 if (info[new_pos].indic_category() == OT_H &&
628 info[new_pos].indic_position() != POS_PRE_M) {
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200629 /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
Behdad Esfahbod921ce5b2012-07-16 15:26:56 -0400630 if (new_pos + 1 < end && is_joiner (info[new_pos + 1]))
631 new_pos++;
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200632
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200633 /* Now go see if there's actually any matras... */
Behdad Esfahbod921ce5b2012-07-16 15:26:56 -0400634 for (unsigned int i = new_pos; i > start; i--)
Behdad Esfahbod6a091df2012-05-11 21:42:27 +0200635 if (info[i - 1].indic_position () == POS_PRE_M)
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200636 {
Behdad Esfahbod1a1dbe92012-07-16 15:40:33 -0400637 unsigned int old_pos = i - 1;
638 hb_glyph_info_t tmp = info[old_pos];
639 memmove (&info[old_pos], &info[old_pos + 1], (new_pos - old_pos) * sizeof (info[0]));
640 info[new_pos] = tmp;
Behdad Esfahbod921ce5b2012-07-16 15:26:56 -0400641 start_of_last_cluster = MIN (new_pos, start_of_last_cluster);
642 new_pos--;
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200643 }
644 }
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200645 }
646
647
648 /* o Reorder reph:
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400649 *
650 * Reph’s original position is always at the beginning of the syllable,
651 * (i.e. it is not reordered at the character reordering stage). However,
652 * it will be reordered according to the basic-forms shaping results.
653 * Possible positions for reph, depending on the script, are; after main,
654 * before post-base consonant forms, and after post-base consonant forms.
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200655 */
656
657 /* If there's anything after the Ra that has the REPH pos, it ought to be halant.
658 * Which means that the font has failed to ligate the Reph. In which case, we
659 * shouldn't move. */
660 if (start + 1 < end &&
661 info[start].indic_position() == POS_RA_TO_BECOME_REPH &&
662 info[start + 1].indic_position() != POS_RA_TO_BECOME_REPH)
663 {
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200664 unsigned int new_reph_pos;
665
666 enum reph_position_t {
Behdad Esfahbodf8936722012-05-11 21:10:03 +0200667 REPH_AFTER_MAIN,
668 REPH_BEFORE_SUBSCRIPT,
669 REPH_AFTER_SUBSCRIPT,
670 REPH_BEFORE_POSTSCRIPT,
Behdad Esfahbod9fc7a112012-06-04 08:28:19 -0400671 REPH_AFTER_POSTSCRIPT
Behdad Esfahbodf8936722012-05-11 21:10:03 +0200672 } reph_pos;
673
674 /* XXX Figure out old behavior too */
Behdad Esfahbod7f852b62012-05-11 23:10:31 +0200675 switch ((hb_tag_t) buffer->props.script)
Behdad Esfahbodf8936722012-05-11 21:10:03 +0200676 {
677 case HB_SCRIPT_MALAYALAM:
678 case HB_SCRIPT_ORIYA:
679 reph_pos = REPH_AFTER_MAIN;
680 break;
681
682 case HB_SCRIPT_GURMUKHI:
683 reph_pos = REPH_BEFORE_SUBSCRIPT;
684 break;
685
686 case HB_SCRIPT_BENGALI:
687 reph_pos = REPH_AFTER_SUBSCRIPT;
688 break;
689
690 default:
691 case HB_SCRIPT_DEVANAGARI:
692 case HB_SCRIPT_GUJARATI:
693 reph_pos = REPH_BEFORE_POSTSCRIPT;
694 break;
695
696 case HB_SCRIPT_KANNADA:
697 case HB_SCRIPT_TAMIL:
698 case HB_SCRIPT_TELUGU:
699 reph_pos = REPH_AFTER_POSTSCRIPT;
700 break;
701 }
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200702
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200703 /* 1. If reph should be positioned after post-base consonant forms,
704 * proceed to step 5.
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200705 */
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200706 if (reph_pos == REPH_AFTER_POSTSCRIPT)
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200707 {
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200708 goto reph_step_5;
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200709 }
710
711 /* 2. If the reph repositioning class is not after post-base: target
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200712 * position is after the first explicit halant glyph between the
713 * first post-reph consonant and last main consonant. If ZWJ or ZWNJ
714 * are following this halant, position is moved after it. If such
715 * position is found, this is the target position. Otherwise,
716 * proceed to the next step.
717 *
718 * Note: in old-implementation fonts, where classifications were
719 * fixed in shaping engine, there was no case where reph position
720 * will be found on this step.
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200721 */
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200722 {
723 new_reph_pos = start + 1;
724 while (new_reph_pos < base && info[new_reph_pos].indic_category() != OT_H)
725 new_reph_pos++;
726
727 if (new_reph_pos < base && info[new_reph_pos].indic_category() == OT_H) {
728 /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
729 if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
730 new_reph_pos++;
731 goto reph_move;
732 }
733 }
734
735 /* 3. If reph should be repositioned after the main consonant: find the
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200736 * first consonant not ligated with main, or find the first
737 * consonant that is not a potential pre-base reordering Ra.
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200738 */
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200739 if (reph_pos == REPH_AFTER_MAIN)
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200740 {
Behdad Esfahbodb504e062012-07-16 15:21:12 -0400741 new_reph_pos = base;
742 /* XXX Skip potential pre-base reordering Ra. */
743 while (new_reph_pos < end &&
744 !( FLAG (info[new_reph_pos + 1].indic_position()) & (FLAG (POS_BELOW_C) | FLAG (POS_POST_C) | FLAG (POS_POST_M) | FLAG (POS_SMVD))))
745 new_reph_pos++;
746 if (new_reph_pos < end)
747 goto reph_move;
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200748 }
749
750 /* 4. If reph should be positioned before post-base consonant, find
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200751 * first post-base classified consonant not ligated with main. If no
752 * consonant is found, the target position should be before the
753 * first matra, syllable modifier sign or vedic sign.
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200754 */
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200755 /* This is our take on what step 4 is trying to say (and failing, BADLY). */
756 if (reph_pos == REPH_AFTER_SUBSCRIPT)
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200757 {
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200758 new_reph_pos = base;
759 while (new_reph_pos < end &&
Behdad Esfahbod6a091df2012-05-11 21:42:27 +0200760 !( FLAG (info[new_reph_pos + 1].indic_position()) & (FLAG (POS_POST_C) | FLAG (POS_POST_M) | FLAG (POS_SMVD))))
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200761 new_reph_pos++;
762 if (new_reph_pos < end)
763 goto reph_move;
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200764 }
765
766 /* 5. If no consonant is found in steps 3 or 4, move reph to a position
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200767 * immediately before the first post-base matra, syllable modifier
768 * sign or vedic sign that has a reordering class after the intended
769 * reph position. For example, if the reordering position for reph
770 * is post-main, it will skip above-base matras that also have a
771 * post-main position.
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200772 */
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200773 reph_step_5:
774 {
775 /* XXX */
Behdad Esfahbod8df56362012-05-10 15:41:04 +0200776 }
777
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200778 /* 6. Otherwise, reorder reph to the end of the syllable.
779 */
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200780 {
781 new_reph_pos = end - 1;
782 while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD)
783 new_reph_pos--;
784
Behdad Esfahbod892eb782012-05-11 16:54:40 +0200785 /*
786 * If the Reph is to be ending up after a Matra,Halant sequence,
787 * position it before that Halant so it can interact with the Matra.
788 * However, if it's a plain Consonant,Halant we shouldn't do that.
789 * Uniscribe doesn't do this.
790 * TEST: U+0930,U+094D,U+0915,U+094B,U+094D
791 */
Behdad Esfahboda2b471d2012-06-05 15:17:44 -0400792 if (!indic_options ().uniscribe_bug_compatible &&
Behdad Esfahbod892eb782012-05-11 16:54:40 +0200793 unlikely (info[new_reph_pos].indic_category() == OT_H)) {
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200794 for (unsigned int i = base + 1; i < new_reph_pos; i++)
795 if (info[i].indic_category() == OT_M) {
796 /* Ok, got it. */
797 new_reph_pos--;
798 }
799 }
800 goto reph_move;
801 }
802
803 reph_move:
804 {
805 /* Move */
806 hb_glyph_info_t reph = info[start];
807 memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0]));
808 info[new_reph_pos] = reph;
809 start_of_last_cluster = start; /* Yay, one big cluster! */
810 }
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200811 }
812
813
814 /* o Reorder pre-base reordering consonants:
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400815 *
816 * If a pre-base reordering consonant is found, reorder it according to
817 * the following rules:
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400818 */
819
Behdad Esfahbod46e645e2012-07-16 15:30:05 -0400820 if (pref_mask && base + 1 < end) /* Otherwise there can't be any pre-base reordering Ra. */
821 {
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -0400822 for (unsigned int i = base + 1; i < end; i++)
823 if ((info[i].mask & pref_mask) != 0)
Behdad Esfahbod78818122012-07-16 15:49:08 -0400824 {
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -0400825 /* 1. Only reorder a glyph produced by substitution during application
826 * of the <pref> feature. (Note that a font may shape a Ra consonant with
827 * the feature generally but block it in certain contexts.)
828 */
829 if (i + 1 == end || (info[i + 1].mask & pref_mask) == 0)
830 {
831 /*
832 * 2. Try to find a target position the same way as for pre-base matra.
833 * If it is found, reorder pre-base consonant glyph.
834 *
835 * 3. If position is not found, reorder immediately before main
836 * consonant.
837 */
838
839 unsigned int new_pos = base;
840 while (new_pos > start + 1 &&
841 !(FLAG (info[new_pos - 1].indic_category()) & (FLAG (OT_M) | FLAG (OT_H))))
842 new_pos--;
843
844 if (new_pos > start && info[new_pos - 1].indic_category() == OT_H)
845 /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
846 if (new_pos < end && is_joiner (info[new_pos]))
847 new_pos++;
848
849 {
850 unsigned int old_pos = i;
851 hb_glyph_info_t tmp = info[old_pos];
852 memmove (&info[new_pos + 1], &info[new_pos], (old_pos - new_pos) * sizeof (info[0]));
853 info[new_pos] = tmp;
854 start_of_last_cluster = MIN (new_pos, start_of_last_cluster);
855 }
856 }
857
858 break;
Behdad Esfahbod78818122012-07-16 15:49:08 -0400859 }
Behdad Esfahbod46e645e2012-07-16 15:30:05 -0400860 }
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200861
862
Behdad Esfahboda913b022012-05-11 20:59:26 +0200863 /* Apply 'init' to the Left Matra if it's a word start. */
Behdad Esfahbod6a091df2012-05-11 21:42:27 +0200864 if (info[start].indic_position () == POS_PRE_M &&
Behdad Esfahboda913b022012-05-11 20:59:26 +0200865 (!start ||
Behdad Esfahbodeace47b2012-05-13 15:54:43 +0200866 !(FLAG (_hb_glyph_info_get_general_category (&info[start - 1])) &
Behdad Esfahboda913b022012-05-11 20:59:26 +0200867 (FLAG (HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER) |
868 FLAG (HB_UNICODE_GENERAL_CATEGORY_MODIFIER_LETTER) |
869 FLAG (HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER) |
870 FLAG (HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER) |
871 FLAG (HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER) |
872 FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) |
873 FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) |
874 FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))))
Behdad Esfahbod46e645e2012-07-16 15:30:05 -0400875 info[start].mask |= init_mask;
Behdad Esfahboda913b022012-05-11 20:59:26 +0200876
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200877
Behdad Esfahbod21d28032012-05-10 18:34:34 +0200878
879 /* Finish off the clusters and go home! */
880
Behdad Esfahboda2b471d2012-06-05 15:17:44 -0400881 if (!indic_options ().uniscribe_bug_compatible)
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200882 {
Behdad Esfahbod21d28032012-05-10 18:34:34 +0200883 /* This is what Uniscribe does. Ie. add cluster boundaries after Halant,ZWNJ.
884 * This means, half forms are submerged into the main consonants cluster.
885 * This is unnecessary, and makes cursor positioning harder, but that's what
886 * Uniscribe does. */
887 unsigned int cluster_start = start;
888 for (unsigned int i = start + 1; i < start_of_last_cluster; i++)
889 if (info[i - 1].indic_category() == OT_H && info[i].indic_category() == OT_ZWNJ) {
890 i++;
891 buffer->merge_clusters (cluster_start, i);
892 cluster_start = i;
893 }
894 start_of_last_cluster = cluster_start;
895 }
896
897 buffer->merge_clusters (start_of_last_cluster, end);
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200898}
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400899
900
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200901static void
902final_reordering (const hb_ot_map_t *map,
Behdad Esfahbod3f182362012-05-13 16:20:10 +0200903 hb_face_t *face HB_UNUSED,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200904 hb_buffer_t *buffer,
905 void *user_data HB_UNUSED)
906{
907 unsigned int count = buffer->len;
908 if (!count) return;
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400909
Behdad Esfahbod46e645e2012-07-16 15:30:05 -0400910 hb_mask_t init_mask = map->get_1_mask (HB_TAG('i','n','i','t'));
911 hb_mask_t pref_mask = map->get_1_mask (HB_TAG('p','r','e','f'));
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200912
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200913 hb_glyph_info_t *info = buffer->info;
914 unsigned int last = 0;
Behdad Esfahbodcee71872012-05-11 11:41:39 +0200915 unsigned int last_syllable = info[0].syllable();
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200916 for (unsigned int i = 1; i < count; i++)
Behdad Esfahbodcee71872012-05-11 11:41:39 +0200917 if (last_syllable != info[i].syllable()) {
Behdad Esfahbod46e645e2012-07-16 15:30:05 -0400918 final_reordering_syllable (buffer, init_mask, pref_mask, last, i);
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200919 last = i;
Behdad Esfahbodcee71872012-05-11 11:41:39 +0200920 last_syllable = info[last].syllable();
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200921 }
Behdad Esfahbod46e645e2012-07-16 15:30:05 -0400922 final_reordering_syllable (buffer, init_mask, pref_mask, last, count);
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200923
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400924 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
925 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
926}
927
928
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400929