blob: 57f33409e2d9c05aef2f2b5cac80ac528d1e1853 [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 Esfahbod49c5ec52012-07-23 20:14:13 -040029#include "hb-ot-layout-private.hh"
Behdad Esfahbod352372a2011-07-30 19:04:02 -040030
Behdad Esfahbod1d002042012-08-02 05:01:11 -040031
Behdad Esfahboda3e04be2012-07-16 13:47:19 -040032#define OLD_INDIC_TAG(script) (((hb_tag_t) script) | 0x20000000)
33#define IS_OLD_INDIC_TAG(tag) ( \
Behdad Esfahbod1d002042012-08-02 05:01:11 -040034 (tag) == OLD_INDIC_TAG (HB_SCRIPT_BENGALI) || \
35 (tag) == OLD_INDIC_TAG (HB_SCRIPT_DEVANAGARI) || \
36 (tag) == OLD_INDIC_TAG (HB_SCRIPT_GUJARATI) || \
37 (tag) == OLD_INDIC_TAG (HB_SCRIPT_GURMUKHI) || \
38 (tag) == OLD_INDIC_TAG (HB_SCRIPT_KANNADA) || \
39 (tag) == OLD_INDIC_TAG (HB_SCRIPT_MALAYALAM) || \
40 (tag) == OLD_INDIC_TAG (HB_SCRIPT_ORIYA) || \
41 (tag) == OLD_INDIC_TAG (HB_SCRIPT_TAMIL) || \
42 (tag) == OLD_INDIC_TAG (HB_SCRIPT_TELUGU) || \
43 0)
44
45
Behdad Esfahboda2b471d2012-06-05 15:17:44 -040046struct indic_options_t
Behdad Esfahbodebe29732012-05-11 16:43:12 +020047{
Behdad Esfahboda2b471d2012-06-05 15:17:44 -040048 int initialized : 1;
49 int uniscribe_bug_compatible : 1;
50};
51
52union indic_options_union_t {
53 int i;
54 indic_options_t opts;
55};
56ASSERT_STATIC (sizeof (int) == sizeof (indic_options_union_t));
57
58static indic_options_union_t
59indic_options_init (void)
60{
61 indic_options_union_t u;
62 u.i = 0;
63 u.opts.initialized = 1;
64
65 char *c = getenv ("HB_OT_INDIC_OPTIONS");
66 u.opts.uniscribe_bug_compatible = c && strstr (c, "uniscribe-bug-compatible");
67
68 return u;
69}
70
71inline indic_options_t
72indic_options (void)
73{
74 static indic_options_union_t options;
75
76 if (unlikely (!options.i)) {
77 /* This is idempotent and threadsafe. */
78 options = indic_options_init ();
Behdad Esfahbodebe29732012-05-11 16:43:12 +020079 }
80
Behdad Esfahboda2b471d2012-06-05 15:17:44 -040081 return options.opts;
82}
83
Behdad Esfahbodebe29732012-05-11 16:43:12 +020084
Behdad Esfahbod9ccc6382012-07-19 12:32:16 -040085
Behdad Esfahbodeed903b2012-05-11 20:50:53 +020086struct feature_list_t {
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -040087 hb_tag_t tag;
88 hb_bool_t is_global;
Behdad Esfahbodeed903b2012-05-11 20:50:53 +020089};
90
Behdad Esfahbodd5c4edc2012-07-17 10:40:59 -040091/* These features are applied one at a time, given the order in this table. */
Behdad Esfahbodeed903b2012-05-11 20:50:53 +020092static const feature_list_t
93indic_basic_features[] =
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -040094{
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -040095 {HB_TAG('n','u','k','t'), true},
Behdad Esfahbode0475342012-07-19 20:24:14 -040096 {HB_TAG('a','k','h','n'), true},
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -040097 {HB_TAG('r','p','h','f'), false},
Behdad Esfahbod1ac075b2012-05-09 11:06:47 +020098 {HB_TAG('r','k','r','f'), true},
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -040099 {HB_TAG('p','r','e','f'), false},
100 {HB_TAG('b','l','w','f'), false},
101 {HB_TAG('h','a','l','f'), false},
Behdad Esfahbod29f106d2012-07-16 12:05:35 -0400102 {HB_TAG('a','b','v','f'), false},
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400103 {HB_TAG('p','s','t','f'), false},
Behdad Esfahbod0201e0a2012-07-17 13:55:10 -0400104 {HB_TAG('c','f','a','r'), false},
Behdad Esfahbod20b68e62012-07-20 10:47:46 -0400105 {HB_TAG('c','j','c','t'), true},
Behdad Esfahbod1d6846d2012-05-13 18:09:29 +0200106 {HB_TAG('v','a','t','u'), true},
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400107};
108
109/* Same order as the indic_basic_features array */
110enum {
111 _NUKT,
Behdad Esfahbode0475342012-07-19 20:24:14 -0400112 _AKHN,
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400113 RPHF,
Behdad Esfahboddf6d45c2012-05-09 11:38:31 +0200114 _RKRF,
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400115 PREF,
116 BLWF,
117 HALF,
Behdad Esfahbod29f106d2012-07-16 12:05:35 -0400118 ABVF,
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400119 PSTF,
Behdad Esfahbod0201e0a2012-07-17 13:55:10 -0400120 CFAR,
Behdad Esfahbod20b68e62012-07-20 10:47:46 -0400121 _CJCT,
Behdad Esfahbod1d6846d2012-05-13 18:09:29 +0200122 VATU
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400123};
124
Behdad Esfahbodd5c4edc2012-07-17 10:40:59 -0400125/* These features are applied all at once. */
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200126static const feature_list_t
127indic_other_features[] =
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400128{
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200129 {HB_TAG('i','n','i','t'), false},
130 {HB_TAG('p','r','e','s'), true},
131 {HB_TAG('a','b','v','s'), true},
132 {HB_TAG('b','l','w','s'), true},
133 {HB_TAG('p','s','t','s'), true},
134 {HB_TAG('h','a','l','n'), true},
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400135
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200136 {HB_TAG('d','i','s','t'), true},
137 {HB_TAG('a','b','v','m'), true},
138 {HB_TAG('b','l','w','m'), true},
139};
140
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400141
142static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400143initial_reordering (const hb_ot_shape_plan_t *plan,
Behdad Esfahbodafbcc242012-08-02 08:36:40 -0400144 hb_font_t *font,
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400145 hb_buffer_t *buffer);
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400146static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400147final_reordering (const hb_ot_shape_plan_t *plan,
Behdad Esfahbodafbcc242012-08-02 08:36:40 -0400148 hb_font_t *font,
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400149 hb_buffer_t *buffer);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400150
Behdad Esfahbod693918e2012-07-30 21:08:51 -0400151static void
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400152collect_features_indic (hb_ot_shape_planner_t *plan)
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400153{
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400154 hb_ot_map_builder_t *map = &plan->map;
155
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400156 map->add_bool_feature (HB_TAG('l','o','c','l'));
Behdad Esfahboda54a5502011-07-20 16:42:10 -0400157 /* The Indic specs do not require ccmp, but we apply it here since if
158 * there is a use of it, it's typically at the beginning. */
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400159 map->add_bool_feature (HB_TAG('c','c','m','p'));
160
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400161 map->add_gsub_pause (initial_reordering);
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400162
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200163 for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++) {
Behdad Esfahbod76f76812011-07-07 22:25:25 -0400164 map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400165 map->add_gsub_pause (NULL);
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200166 }
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400167
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400168 map->add_gsub_pause (final_reordering);
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400169
Behdad Esfahbodd5c4edc2012-07-17 10:40:59 -0400170 for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++)
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200171 map->add_bool_feature (indic_other_features[i].tag, indic_other_features[i].is_global);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400172}
173
Behdad Esfahbod693918e2012-07-30 21:08:51 -0400174static void
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400175override_features_indic (hb_ot_shape_planner_t *plan)
Behdad Esfahbodd96838e2012-07-16 20:26:57 -0400176{
Behdad Esfahbodaf92b4c2012-07-16 20:31:24 -0400177 /* Uniscribe does not apply 'kern'. */
178 if (indic_options ().uniscribe_bug_compatible)
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400179 plan->map.add_feature (HB_TAG('k','e','r','n'), 0, true);
Behdad Esfahbodd96838e2012-07-16 20:26:57 -0400180}
181
Behdad Esfahbod867361c2011-06-17 18:35:46 -0400182
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400183struct would_apply_feature_t
184{
185 would_apply_feature_t (const hb_ot_map_t *map, hb_tag_t feature_tag)
186 {
187 map->get_stage_lookups (0/*GSUB*/,
188 map->get_feature_stage (0/*GSUB*/, feature_tag),
189 &lookups, &count);
190 }
191
192 inline bool would_substitute (hb_codepoint_t *glyphs,
193 unsigned int glyphs_count,
194 hb_face_t *face) const
195 {
196 for (unsigned int i = 0; i < count; i++)
197 if (hb_ot_layout_would_substitute_lookup_fast (face, glyphs, glyphs_count, lookups[i].index))
198 return true;
199 return false;
200 }
201
202 private:
203 const hb_ot_map_t::lookup_map_t *lookups;
204 unsigned int count;
205};
206
207struct indic_shape_plan_t
208{
209 indic_shape_plan_t (const hb_ot_shape_plan_t *plan) :
210 pref (&plan->map, HB_TAG('p','r','e','f')),
211 blwf (&plan->map, HB_TAG('b','l','w','f')),
212 pstf (&plan->map, HB_TAG('p','s','t','f')),
Behdad Esfahbod914ffaa2012-08-02 11:03:39 -0400213 is_old_spec (IS_OLD_INDIC_TAG (plan->map.get_chosen_script (0))),
214 virama_glyph ((hb_codepoint_t) -1)
215 {
216 switch ((int) plan->props.script) {
217 case HB_SCRIPT_DEVANAGARI:virama = 0x094D; break;
218 case HB_SCRIPT_BENGALI: virama = 0x09CD; break;
219 case HB_SCRIPT_GURMUKHI: virama = 0x0A4D; break;
220 case HB_SCRIPT_GUJARATI: virama = 0x0ACD; break;
221 case HB_SCRIPT_ORIYA: virama = 0x0B4D; break;
222 case HB_SCRIPT_TAMIL: virama = 0x0BCD; break;
223 case HB_SCRIPT_TELUGU: virama = 0x0C4D; break;
224 case HB_SCRIPT_KANNADA: virama = 0x0CCD; break;
225 case HB_SCRIPT_MALAYALAM: virama = 0x0D4D; break;
226 case HB_SCRIPT_SINHALA: virama = 0x0DCA; break;
227 case HB_SCRIPT_KHMER: virama = 0x17D2; break;
228 default: virama = 0; break;
229 }
230 }
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400231
232 would_apply_feature_t pref;
233 would_apply_feature_t blwf;
234 would_apply_feature_t pstf;
235 bool is_old_spec;
Behdad Esfahbod914ffaa2012-08-02 11:03:39 -0400236 hb_codepoint_t virama;
237 hb_codepoint_t virama_glyph;
238
239 inline bool get_virama_glyph (hb_font_t *font, hb_codepoint_t *pglyph) const
240 {
241 hb_codepoint_t glyph = virama_glyph;
242 if (unlikely (virama_glyph == (hb_codepoint_t) -1))
243 {
244 if (!font->get_glyph (virama, 0, &glyph))
245 glyph = 0;
246 /* Technically speaking, the spec says we should apply 'locl' to virama too.
247 * Maybe one day... */
248
249 /* Our get_glyph() function needs a font, so we can't get the virama glyph
250 * during shape planning... Instead, overwrite it here. It's safe. Don't worry! */
251 (const_cast<indic_shape_plan_t *> (this))->virama_glyph = glyph;
252 }
253
254 *pglyph = glyph;
255 return glyph != 0;
256 }
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400257};
258
259static void *
260data_create_indic (const hb_ot_shape_plan_t *plan)
261{
262 indic_shape_plan_t *data = (indic_shape_plan_t *) calloc (1, sizeof (indic_shape_plan_t));
263 if (unlikely (!data))
264 return NULL;
265
266 /* Mixing C++ and C, oh well... */
267 indic_shape_plan_t indic_plan (plan);
268
269 *data = indic_plan;
270 return data;
271}
272
273static void
274data_destroy_indic (void *data)
275{
276 free (data);
277}
278
279static indic_position_t
280consonant_position_from_face (const indic_shape_plan_t *indic_plan,
281 hb_codepoint_t *glyphs, unsigned int glyphs_len,
282 hb_face_t *face)
283{
284 if (indic_plan->pref.would_substitute (glyphs, ARRAY_LENGTH (glyphs), face)) return POS_BELOW_C;
285 if (indic_plan->blwf.would_substitute (glyphs, ARRAY_LENGTH (glyphs), face)) return POS_BELOW_C;
286 if (indic_plan->pstf.would_substitute (glyphs, ARRAY_LENGTH (glyphs), face)) return POS_POST_C;
287 return POS_BASE_C;
288}
289
290
Behdad Esfahbod693918e2012-07-30 21:08:51 -0400291static void
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400292setup_masks_indic (const hb_ot_shape_plan_t *plan HB_UNUSED,
293 hb_buffer_t *buffer,
294 hb_font_t *font HB_UNUSED)
Behdad Esfahbod24eacf12012-08-02 08:42:11 -0400295{
296 HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
297 HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
298
299 /* We cannot setup masks here. We save information about characters
300 * and setup masks later on in a pause-callback. */
301
302 unsigned int count = buffer->len;
303 for (unsigned int i = 0; i < count; i++)
304 set_indic_properties (buffer->info[i]);
305}
306
307static int
308compare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
309{
310 int a = pa->indic_position();
311 int b = pb->indic_position();
312
313 return a < b ? -1 : a == b ? 0 : +1;
314}
315
316
317
318static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400319update_consonant_positions (const hb_ot_shape_plan_t *plan,
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400320 hb_buffer_t *buffer,
321 hb_font_t *font)
322{
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400323 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400324
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400325 unsigned int consonant_pos = indic_plan->is_old_spec ? 0 : 1;
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400326 hb_codepoint_t glyphs[2];
Behdad Esfahbod914ffaa2012-08-02 11:03:39 -0400327 if (indic_plan->get_virama_glyph (font, &glyphs[1 - consonant_pos]))
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400328 {
329 hb_face_t *face = font->face;
330 unsigned int count = buffer->len;
331 for (unsigned int i = 0; i < count; i++)
332 if (buffer->info[i].indic_position() == POS_BASE_C) {
Behdad Esfahbod24eacf12012-08-02 08:42:11 -0400333 glyphs[consonant_pos] = buffer->info[i].codepoint;
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400334 buffer->info[i].indic_position() = consonant_position_from_face (indic_plan, glyphs, 2, face);
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400335 }
336 }
337}
338
Behdad Esfahbod867361c2011-06-17 18:35:46 -0400339
Behdad Esfahbod7ea58db2012-05-11 18:58:57 +0200340/* Rules from:
341 * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
342
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400343static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400344initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan, hb_buffer_t *buffer, hb_mask_t *basic_mask_array,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200345 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400346{
Behdad Esfahbod914ffaa2012-08-02 11:03:39 -0400347 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
Behdad Esfahbodee58f3b2011-07-30 19:15:53 -0400348 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400349
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200350
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400351 /* 1. Find base consonant:
352 *
353 * The shaping engine finds the base consonant of the syllable, using the
354 * following algorithm: starting from the end of the syllable, move backwards
355 * until a consonant is found that does not have a below-base or post-base
356 * form (post-base forms have to follow below-base forms), or that is not a
357 * pre-base reordering Ra, or arrive at the first consonant. The consonant
358 * stopped at will be the base.
359 *
360 * o If the syllable starts with Ra + Halant (in a script that has Reph)
361 * and has more than one consonant, Ra is excluded from candidates for
362 * base consonants.
363 */
364
Behdad Esfahbod5e720712011-07-31 17:51:50 -0400365 unsigned int base = end;
Behdad Esfahbod76b34092012-05-09 11:43:43 +0200366 bool has_reph = false;
367
Behdad Esfahbod76b34092012-05-09 11:43:43 +0200368 {
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200369 /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
370 * and has more than one consonant, Ra is excluded from candidates for
371 * base consonants. */
372 unsigned int limit = start;
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400373 if (basic_mask_array[RPHF] &&
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200374 start + 3 <= end &&
375 info[start].indic_category() == OT_Ra &&
376 info[start + 1].indic_category() == OT_H &&
Behdad Esfahbodf31d97e2012-07-20 14:13:35 -0400377 (unlikely (buffer->props.script == HB_SCRIPT_SINHALA || buffer->props.script == HB_SCRIPT_TELUGU) ?
378 info[start + 2].indic_category() == OT_ZWJ /* In Sinhala & Telugu, form Reph only if ZWJ is present */:
Behdad Esfahbod3285e102012-07-18 17:22:14 -0400379 !is_joiner (info[start + 2] /* In other scripts, any joiner blocks Reph formation */ )
380 ))
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200381 {
382 limit += 2;
Behdad Esfahbod3285e102012-07-18 17:22:14 -0400383 while (limit < end && is_joiner (info[limit]))
384 limit++;
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200385 base = start;
386 has_reph = true;
387 };
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400388
Behdad Esfahbod14dbdd92012-07-18 13:13:03 -0400389 enum base_position_t {
390 BASE_FIRST,
391 BASE_LAST
392 } base_pos;
393
394 switch ((hb_tag_t) buffer->props.script)
395 {
Behdad Esfahbod34c21502012-07-23 23:51:29 -0400396 case HB_SCRIPT_SINHALA:
Behdad Esfahbod14dbdd92012-07-18 13:13:03 -0400397 case HB_SCRIPT_KHMER:
398 base_pos = BASE_FIRST;
399 break;
400
401 default:
402 base_pos = BASE_LAST;
403 break;
404 }
405
406 if (base_pos == BASE_LAST)
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400407 {
408 /* -> starting from the end of the syllable, move backwards */
409 unsigned int i = end;
Behdad Esfahbod92a1ad72012-07-20 18:38:27 -0400410 bool seen_below = false;
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400411 do {
412 i--;
413 /* -> until a consonant is found */
414 if (is_consonant (info[i]))
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200415 {
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400416 /* -> that does not have a below-base or post-base form
417 * (post-base forms have to follow below-base forms), */
418 if (info[i].indic_position() != POS_BELOW_C &&
Behdad Esfahbod92a1ad72012-07-20 18:38:27 -0400419 (info[i].indic_position() != POS_POST_C || seen_below))
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400420 {
421 base = i;
422 break;
423 }
Behdad Esfahbod92a1ad72012-07-20 18:38:27 -0400424 if (info[i].indic_position() == POS_BELOW_C)
425 seen_below = true;
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400426
427 /* -> or that is not a pre-base reordering Ra,
428 *
Behdad Esfahbod25e302d2012-07-17 14:25:14 -0400429 * IMPLEMENTATION NOTES:
430 *
431 * Our pre-base reordering Ra's are marked POS_BELOW, so will be skipped
432 * by the logic above already.
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400433 */
434
435 /* -> or arrive at the first consonant. The consonant stopped at will
436 * be the base. */
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200437 base = i;
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200438 }
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400439 else
Behdad Esfahboda9e45c32012-07-20 11:04:15 -0400440 {
Behdad Esfahbod88f413b2012-07-24 03:04:36 -0400441 /* A ZWJ after a Halant stops the base search, and requests an explicit
442 * half form.
443 * A ZWJ before a Halant, requests a subjoined form instead, and hence
444 * search continues. This is particularly important for Bengali
445 * sequence Ra,H,Ya that shouls form Ya-Phalaa by subjoining Ya. */
446 if (start < i &&
447 info[i].indic_category() == OT_ZWJ &&
448 info[i - 1].indic_category() == OT_H)
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400449 break;
Behdad Esfahboda9e45c32012-07-20 11:04:15 -0400450 }
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400451 } while (i > limit);
452 }
453 else
454 {
455 /* In scripts without half forms (eg. Khmer), the first consonant is always the base. */
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200456
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400457 if (!has_reph)
458 base = limit;
Behdad Esfahbod34c21502012-07-23 23:51:29 -0400459
460 /* Find the last base consonant that is not blocked by ZWJ. If there is
Behdad Esfahbod71fd5e82012-07-24 00:21:16 -0400461 * a ZWJ right before a base consonant, that would request a subjoined form. */
Behdad Esfahbod34c21502012-07-23 23:51:29 -0400462 for (unsigned int i = limit; i < end; i++)
463 if (is_consonant (info[i]) && info[i].indic_position() == POS_BASE_C)
Behdad Esfahbod71fd5e82012-07-24 00:21:16 -0400464 {
465 if (limit < i && info[i - 1].indic_category() == OT_ZWJ)
466 break;
467 else
468 base = i;
469 }
Behdad Esfahbod34c21502012-07-23 23:51:29 -0400470
471 /* Mark all subsequent consonants as below. */
472 for (unsigned int i = base + 1; i < end; i++)
473 if (is_consonant (info[i]) && info[i].indic_position() == POS_BASE_C)
474 info[i].indic_position() = POS_BELOW_C;
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400475 }
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200476
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200477 /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
478 * and has more than one consonant, Ra is excluded from candidates for
Behdad Esfahbod2278eef2012-07-24 00:26:43 -0400479 * base consonants.
480 *
481 * Only do this for unforced Reph. (ie. not for Ra,H,ZWJ. */
482 if (has_reph && base == start && start + 2 == limit) {
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200483 /* Have no other consonant, so Reph is not formed and Ra becomes base. */
484 has_reph = false;
485 }
Behdad Esfahbod5e4e21f2012-05-13 16:46:08 +0200486 }
Behdad Esfahbod2278eef2012-07-24 00:26:43 -0400487
Behdad Esfahbod34c21502012-07-23 23:51:29 -0400488 if (base < end)
489 info[base].indic_position() = POS_BASE_C;
Behdad Esfahbod3d250792012-05-10 11:37:42 +0200490
491
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400492 /* 2. Decompose and reorder Matras:
493 *
494 * Each matra and any syllable modifier sign in the cluster are moved to the
495 * appropriate position relative to the consonant(s) in the cluster. The
496 * shaping engine decomposes two- or three-part matras into their constituent
497 * parts before any repositioning. Matra characters are classified by which
498 * consonant in a conjunct they have affinity for and are reordered to the
499 * following positions:
500 *
501 * o Before first half form in the syllable
502 * o After subjoined consonants
503 * o After post-form consonant
504 * o After main consonant (for above marks)
505 *
506 * IMPLEMENTATION NOTES:
507 *
508 * The normalize() routine has already decomposed matras for us, so we don't
509 * need to worry about that.
510 */
511
512
513 /* 3. Reorder marks to canonical order:
514 *
515 * Adjacent nukta and halant or nukta and vedic sign are always repositioned
516 * if necessary, so that the nukta is first.
517 *
518 * IMPLEMENTATION NOTES:
519 *
520 * We don't need to do this: the normalize() routine already did this for us.
521 */
522
523
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400524 /* Reorder characters */
525
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200526 for (unsigned int i = start; i < base; i++)
Behdad Esfahbod900cf3d2012-07-20 10:18:23 -0400527 info[i].indic_position() = MIN (POS_PRE_C, (indic_position_t) info[i].indic_position());
Behdad Esfahbod55f70eb2012-07-17 12:50:13 -0400528
Behdad Esfahbod075d6712012-07-18 15:41:53 -0400529 if (base < end)
530 info[base].indic_position() = POS_BASE_C;
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400531
Behdad Esfahbod55f70eb2012-07-17 12:50:13 -0400532 /* Mark final consonants. A final consonant is one appearing after a matra,
533 * like in Khmer. */
534 for (unsigned int i = base + 1; i < end; i++)
535 if (info[i].indic_category() == OT_M) {
536 for (unsigned int j = i + 1; j < end; j++)
537 if (is_consonant (info[j])) {
538 info[j].indic_position() = POS_FINAL_C;
539 break;
540 }
541 break;
542 }
543
Behdad Esfahbodfd06bf52011-07-30 20:14:44 -0400544 /* Handle beginning Ra */
Behdad Esfahbod5e4e21f2012-05-13 16:46:08 +0200545 if (has_reph)
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200546 info[start].indic_position() = POS_RA_TO_BECOME_REPH;
Behdad Esfahbodfd06bf52011-07-30 20:14:44 -0400547
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400548 /* For old-style Indic script tags, move the first post-base Halant after
549 * last consonant. */
Behdad Esfahbod914ffaa2012-08-02 11:03:39 -0400550 if (indic_plan->is_old_spec) {
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200551 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400552 if (info[i].indic_category() == OT_H) {
553 unsigned int j;
554 for (j = end - 1; j > i; j--)
Behdad Esfahbod190eb312012-05-10 12:17:16 +0200555 if (is_consonant (info[j]))
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400556 break;
557 if (j > i) {
558 /* Move Halant to after last consonant. */
559 hb_glyph_info_t t = info[i];
560 memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
561 info[j] = t;
562 }
563 break;
564 }
565 }
566
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400567 /* Attach misc marks to previous char to move with them. */
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200568 {
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400569 indic_position_t last_pos = POS_START;
570 for (unsigned int i = start; i < end; i++)
571 {
572 if ((FLAG (info[i].indic_category()) & (JOINER_FLAGS | FLAG (OT_N) | FLAG (OT_RS) | HALANT_OR_COENG_FLAGS)))
573 {
574 info[i].indic_position() = last_pos;
575 if (unlikely (indic_options ().uniscribe_bug_compatible &&
576 info[i].indic_category() == OT_H &&
577 info[i].indic_position() == POS_PRE_M))
578 {
579 /*
580 * Uniscribe doesn't move the Halant with Left Matra.
581 * TEST: U+092B,U+093F,U+094DE
582 */
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200583 for (unsigned int j = i; j > start; j--)
Behdad Esfahbod6a091df2012-05-11 21:42:27 +0200584 if (info[j - 1].indic_position() != POS_PRE_M) {
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200585 info[i].indic_position() = info[j - 1].indic_position();
586 break;
587 }
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400588 }
589 } else if (info[i].indic_position() != POS_SMVD) {
590 last_pos = (indic_position_t) info[i].indic_position();
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200591 }
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400592 }
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200593 }
Behdad Esfahbod74ccc6a2012-07-17 11:16:19 -0400594 /* Re-attach ZWJ, ZWNJ, and halant to next char, for after-base consonants. */
595 {
596 unsigned int last_halant = end;
597 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahboddeb521d2012-07-17 11:37:32 -0400598 if (is_halant_or_coeng (info[i]))
Behdad Esfahbod74ccc6a2012-07-17 11:16:19 -0400599 last_halant = i;
600 else if (is_consonant (info[i])) {
601 for (unsigned int j = last_halant; j < i; j++)
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400602 if (info[j].indic_position() != POS_SMVD)
603 info[j].indic_position() = info[i].indic_position();
Behdad Esfahbod74ccc6a2012-07-17 11:16:19 -0400604 }
605 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400606
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200607 {
Behdad Esfahbod7b2a7da2012-07-22 23:58:55 -0400608 /* Things are out-of-control for post base positions, they may shuffle
609 * around like crazy, so merge clusters. For pre-base stuff, we handle
610 * cluster issues in final reordering. */
611 buffer->merge_clusters (base, end);
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200612 /* Sit tight, rock 'n roll! */
Behdad Esfahbodd3637ed2012-05-10 10:51:38 +0200613 hb_bubble_sort (info + start, end - start, compare_indic_order);
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200614 /* Find base again */
615 base = end;
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200616 for (unsigned int i = start; i < end; i++)
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200617 if (info[i].indic_position() == POS_BASE_C) {
618 base = i;
619 break;
620 }
621 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400622
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400623 /* Setup masks now */
624
Behdad Esfahbod28168392011-07-31 16:00:35 -0400625 {
626 hb_mask_t mask;
627
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200628 /* Reph */
Behdad Esfahbod668c6042012-05-11 15:34:13 +0200629 for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++)
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400630 info[i].mask |= basic_mask_array[RPHF];
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200631
Behdad Esfahbod28168392011-07-31 16:00:35 -0400632 /* Pre-base */
Behdad Esfahbod20b68e62012-07-20 10:47:46 -0400633 mask = basic_mask_array[HALF];
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200634 for (unsigned int i = start; i < base; i++)
Behdad Esfahbod28168392011-07-31 16:00:35 -0400635 info[i].mask |= mask;
636 /* Base */
Behdad Esfahbod20b68e62012-07-20 10:47:46 -0400637 mask = 0;
Behdad Esfahbod075d6712012-07-18 15:41:53 -0400638 if (base < end)
639 info[base].mask |= mask;
Behdad Esfahbod28168392011-07-31 16:00:35 -0400640 /* Post-base */
Behdad Esfahbod20b68e62012-07-20 10:47:46 -0400641 mask = basic_mask_array[BLWF] | basic_mask_array[ABVF] | basic_mask_array[PSTF];
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200642 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbod28168392011-07-31 16:00:35 -0400643 info[i].mask |= mask;
644 }
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400645
Behdad Esfahbod17d7de92012-07-16 15:20:15 -0400646 /* XXX This will not match for old-Indic spec since the Halant-Ra order is reversed already. */
Behdad Esfahbod5f0eaaa2012-07-20 15:47:24 -0400647 if (basic_mask_array[PREF] && base + 2 < end)
Behdad Esfahbod17d7de92012-07-16 15:20:15 -0400648 {
Behdad Esfahbod771a8f52012-07-23 20:07:50 -0400649 /* Find a Halant,Ra sequence and mark it for pre-base reordering processing. */
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -0400650 for (unsigned int i = base + 1; i + 1 < end; i++)
Behdad Esfahboddeb521d2012-07-17 11:37:32 -0400651 if (is_halant_or_coeng (info[i]) &&
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -0400652 info[i + 1].indic_category() == OT_Ra)
653 {
Behdad Esfahbod0201e0a2012-07-17 13:55:10 -0400654 info[i++].mask |= basic_mask_array[PREF];
655 info[i++].mask |= basic_mask_array[PREF];
656
657 /* Mark the subsequent stuff with 'cfar'. Used in Khmer.
658 * Read the feature spec.
659 * This allows distinguishing the following cases with MS Khmer fonts:
660 * U+1784,U+17D2,U+179A,U+17D2,U+1782
661 * U+1784,U+17D2,U+1782,U+17D2,U+179A
662 */
663 for (; i < end; i++)
664 info[i].mask |= basic_mask_array[CFAR];
665
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -0400666 break;
667 }
Behdad Esfahbod17d7de92012-07-16 15:20:15 -0400668 }
669
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400670 /* Apply ZWJ/ZWNJ effects */
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200671 for (unsigned int i = start + 1; i < end; i++)
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400672 if (is_joiner (info[i])) {
673 bool non_joiner = info[i].indic_category() == OT_ZWNJ;
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400674 unsigned int j = i;
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400675
676 do {
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400677 j--;
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400678
Behdad Esfahbod20b68e62012-07-20 10:47:46 -0400679 /* A ZWJ disables CJCT, however, it's mere presence is enough
680 * to disable ligation. No explicit action needed. */
681
682 /* A ZWNJ disables HALF. */
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400683 if (non_joiner)
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400684 info[j].mask &= ~basic_mask_array[HALF];
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400685
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400686 } while (j > start && !is_consonant (info[j]));
687 }
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400688}
689
690
691static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400692initial_reordering_vowel_syllable (const hb_ot_shape_plan_t *plan,
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +0200693 hb_buffer_t *buffer,
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400694 hb_mask_t *basic_mask_array,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200695 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400696{
Behdad Esfahbodc5306b62012-05-10 12:07:33 +0200697 /* We made the vowels look like consonants. So let's call the consonant logic! */
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400698 initial_reordering_consonant_syllable (plan, buffer, basic_mask_array, start, end);
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400699}
700
701static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400702initial_reordering_standalone_cluster (const hb_ot_shape_plan_t *plan,
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +0200703 hb_buffer_t *buffer,
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400704 hb_mask_t *basic_mask_array,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200705 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400706{
Behdad Esfahbod18c06e12012-05-11 20:02:14 +0200707 /* We treat NBSP/dotted-circle as if they are consonants, so we should just chain.
708 * Only if not in compatibility mode that is... */
709
Behdad Esfahboda2b471d2012-06-05 15:17:44 -0400710 if (indic_options ().uniscribe_bug_compatible)
Behdad Esfahbod18c06e12012-05-11 20:02:14 +0200711 {
712 /* For dotted-circle, this is what Uniscribe does:
713 * If dotted-circle is the last glyph, it just does nothing.
714 * Ie. It doesn't form Reph. */
715 if (buffer->info[end - 1].indic_category() == OT_DOTTEDCIRCLE)
716 return;
717 }
718
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400719 initial_reordering_consonant_syllable (plan, buffer, basic_mask_array, start, end);
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400720}
721
722static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400723initial_reordering_non_indic (const hb_ot_shape_plan_t *plan HB_UNUSED,
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +0200724 hb_buffer_t *buffer HB_UNUSED,
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400725 hb_mask_t *basic_mask_array HB_UNUSED,
Behdad Esfahbod3f182362012-05-13 16:20:10 +0200726 unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400727{
728 /* Nothing to do right now. If we ever switch to using the output
729 * buffer in the reordering process, we'd need to next_glyph() here. */
730}
731
732#include "hb-ot-shape-complex-indic-machine.hh"
733
734static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400735initial_reordering (const hb_ot_shape_plan_t *plan,
Behdad Esfahbod24eacf12012-08-02 08:42:11 -0400736 hb_font_t *font,
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400737 hb_buffer_t *buffer)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400738{
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400739 update_consonant_positions (plan, buffer, font);
Behdad Esfahbod24eacf12012-08-02 08:42:11 -0400740
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400741 hb_mask_t basic_mask_array[ARRAY_LENGTH (indic_basic_features)] = {0};
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400742 unsigned int num_masks = ARRAY_LENGTH (indic_basic_features);
743 for (unsigned int i = 0; i < num_masks; i++)
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400744 basic_mask_array[i] = plan->map.get_1_mask (indic_basic_features[i].tag);
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400745
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400746 find_syllables (plan, buffer, basic_mask_array);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400747}
748
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400749static void
Behdad Esfahbod46e645e2012-07-16 15:30:05 -0400750final_reordering_syllable (hb_buffer_t *buffer,
751 hb_mask_t init_mask, hb_mask_t pref_mask,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200752 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400753{
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200754 hb_glyph_info_t *info = buffer->info;
755
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400756 /* 4. Final reordering:
757 *
758 * After the localized forms and basic shaping forms GSUB features have been
759 * applied (see below), the shaping engine performs some final glyph
760 * reordering before applying all the remaining font features to the entire
761 * cluster.
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200762 */
763
764 /* Find base again */
Behdad Esfahbod5f0eaaa2012-07-20 15:47:24 -0400765 unsigned int base;
766 for (base = start; base < end; base++)
767 if (info[base].indic_position() >= POS_BASE_C) {
768 if (start < base && info[base].indic_position() > POS_BASE_C)
769 base--;
770 break;
771 }
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200772
Behdad Esfahbod4705a702012-05-10 13:09:08 +0200773
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200774 /* o Reorder matras:
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400775 *
776 * If a pre-base matra character had been reordered before applying basic
777 * features, the glyph can be moved closer to the main consonant based on
778 * whether half-forms had been formed. Actual position for the matra is
779 * defined as “after last standalone halant glyph, after initial matra
780 * position and before the main consonant”. If ZWJ or ZWNJ follow this
781 * halant, position is moved after it.
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200782 */
783
Behdad Esfahbod65c43ac2012-07-24 03:36:47 -0400784 if (start + 1 < end && start < base) /* Otherwise there can't be any pre-base matra characters. */
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200785 {
Behdad Esfahbod65c43ac2012-07-24 03:36:47 -0400786 /* If we lost track of base, alas, position before last thingy. */
787 unsigned int new_pos = base == end ? base - 2 : base - 1;
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200788
Behdad Esfahbod65c43ac2012-07-24 03:36:47 -0400789 /* Malayalam does not have "half" forms or explicit virama forms.
790 * The glyphs formed by 'half' are Chillus. We want to position
791 * matra after them all.
792 */
793 if (buffer->props.script != HB_SCRIPT_MALAYALAM)
794 {
795 while (new_pos > start &&
796 !(is_one_of (info[new_pos], (FLAG (OT_M) | FLAG (OT_H) | FLAG (OT_Coeng)))))
797 new_pos--;
798
799 /* If we found no Halant we are done.
800 * Otherwise only proceed if the Halant does
801 * not belong to the Matra itself! */
802 if (is_halant_or_coeng (info[new_pos]) &&
803 info[new_pos].indic_position() != POS_PRE_M)
804 {
805 /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
806 if (new_pos + 1 < end && is_joiner (info[new_pos + 1]))
807 new_pos++;
808 }
809 else
810 new_pos = start; /* No move. */
811 }
812
813 if (start < new_pos)
814 {
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200815 /* Now go see if there's actually any matras... */
Behdad Esfahbod921ce5b2012-07-16 15:26:56 -0400816 for (unsigned int i = new_pos; i > start; i--)
Behdad Esfahbod6a091df2012-05-11 21:42:27 +0200817 if (info[i - 1].indic_position () == POS_PRE_M)
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200818 {
Behdad Esfahbod1a1dbe92012-07-16 15:40:33 -0400819 unsigned int old_pos = i - 1;
820 hb_glyph_info_t tmp = info[old_pos];
821 memmove (&info[old_pos], &info[old_pos + 1], (new_pos - old_pos) * sizeof (info[0]));
822 info[new_pos] = tmp;
Behdad Esfahbod921ce5b2012-07-16 15:26:56 -0400823 new_pos--;
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200824 }
Behdad Esfahbod2cc933a2012-07-23 08:22:55 -0400825 buffer->merge_clusters (new_pos, MIN (end, base + 1));
Behdad Esfahbodabb32392012-07-22 23:55:19 -0400826 } else {
Behdad Esfahbode6b01a82012-07-23 00:11:26 -0400827 for (unsigned int i = start; i < base; i++)
Behdad Esfahbodabb32392012-07-22 23:55:19 -0400828 if (info[i].indic_position () == POS_PRE_M) {
Behdad Esfahbod2cc933a2012-07-23 08:22:55 -0400829 buffer->merge_clusters (i, MIN (end, base + 1));
Behdad Esfahbodabb32392012-07-22 23:55:19 -0400830 break;
831 }
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200832 }
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200833 }
834
835
836 /* o Reorder reph:
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400837 *
838 * Reph’s original position is always at the beginning of the syllable,
839 * (i.e. it is not reordered at the character reordering stage). However,
840 * it will be reordered according to the basic-forms shaping results.
841 * Possible positions for reph, depending on the script, are; after main,
842 * before post-base consonant forms, and after post-base consonant forms.
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200843 */
844
845 /* If there's anything after the Ra that has the REPH pos, it ought to be halant.
846 * Which means that the font has failed to ligate the Reph. In which case, we
847 * shouldn't move. */
848 if (start + 1 < end &&
849 info[start].indic_position() == POS_RA_TO_BECOME_REPH &&
850 info[start + 1].indic_position() != POS_RA_TO_BECOME_REPH)
851 {
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200852 unsigned int new_reph_pos;
853
854 enum reph_position_t {
Behdad Esfahbodf8936722012-05-11 21:10:03 +0200855 REPH_AFTER_MAIN,
856 REPH_BEFORE_SUBSCRIPT,
857 REPH_AFTER_SUBSCRIPT,
858 REPH_BEFORE_POSTSCRIPT,
Behdad Esfahbod9fc7a112012-06-04 08:28:19 -0400859 REPH_AFTER_POSTSCRIPT
Behdad Esfahbodf8936722012-05-11 21:10:03 +0200860 } reph_pos;
861
862 /* XXX Figure out old behavior too */
Behdad Esfahbod7f852b62012-05-11 23:10:31 +0200863 switch ((hb_tag_t) buffer->props.script)
Behdad Esfahbodf8936722012-05-11 21:10:03 +0200864 {
865 case HB_SCRIPT_MALAYALAM:
866 case HB_SCRIPT_ORIYA:
Behdad Esfahbod34c21502012-07-23 23:51:29 -0400867 case HB_SCRIPT_SINHALA:
Behdad Esfahbodf8936722012-05-11 21:10:03 +0200868 reph_pos = REPH_AFTER_MAIN;
869 break;
870
871 case HB_SCRIPT_GURMUKHI:
872 reph_pos = REPH_BEFORE_SUBSCRIPT;
873 break;
874
875 case HB_SCRIPT_BENGALI:
876 reph_pos = REPH_AFTER_SUBSCRIPT;
877 break;
878
879 default:
880 case HB_SCRIPT_DEVANAGARI:
881 case HB_SCRIPT_GUJARATI:
882 reph_pos = REPH_BEFORE_POSTSCRIPT;
883 break;
884
885 case HB_SCRIPT_KANNADA:
886 case HB_SCRIPT_TAMIL:
887 case HB_SCRIPT_TELUGU:
888 reph_pos = REPH_AFTER_POSTSCRIPT;
889 break;
890 }
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200891
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200892 /* 1. If reph should be positioned after post-base consonant forms,
893 * proceed to step 5.
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200894 */
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200895 if (reph_pos == REPH_AFTER_POSTSCRIPT)
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200896 {
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200897 goto reph_step_5;
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200898 }
899
900 /* 2. If the reph repositioning class is not after post-base: target
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200901 * position is after the first explicit halant glyph between the
902 * first post-reph consonant and last main consonant. If ZWJ or ZWNJ
903 * are following this halant, position is moved after it. If such
904 * position is found, this is the target position. Otherwise,
905 * proceed to the next step.
906 *
907 * Note: in old-implementation fonts, where classifications were
908 * fixed in shaping engine, there was no case where reph position
909 * will be found on this step.
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200910 */
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200911 {
912 new_reph_pos = start + 1;
Behdad Esfahboddeb521d2012-07-17 11:37:32 -0400913 while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos]))
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200914 new_reph_pos++;
915
Behdad Esfahboddeb521d2012-07-17 11:37:32 -0400916 if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos])) {
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200917 /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
918 if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
919 new_reph_pos++;
920 goto reph_move;
921 }
922 }
923
924 /* 3. If reph should be repositioned after the main consonant: find the
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200925 * first consonant not ligated with main, or find the first
926 * consonant that is not a potential pre-base reordering Ra.
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200927 */
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200928 if (reph_pos == REPH_AFTER_MAIN)
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200929 {
Behdad Esfahbodb504e062012-07-16 15:21:12 -0400930 new_reph_pos = base;
931 /* XXX Skip potential pre-base reordering Ra. */
Behdad Esfahbod34ae3362012-07-20 16:17:28 -0400932 while (new_reph_pos + 1 < end && info[new_reph_pos + 1].indic_position() <= POS_AFTER_MAIN)
Behdad Esfahbodb504e062012-07-16 15:21:12 -0400933 new_reph_pos++;
934 if (new_reph_pos < end)
935 goto reph_move;
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200936 }
937
938 /* 4. If reph should be positioned before post-base consonant, find
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200939 * first post-base classified consonant not ligated with main. If no
940 * consonant is found, the target position should be before the
941 * first matra, syllable modifier sign or vedic sign.
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200942 */
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200943 /* This is our take on what step 4 is trying to say (and failing, BADLY). */
944 if (reph_pos == REPH_AFTER_SUBSCRIPT)
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200945 {
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200946 new_reph_pos = base;
947 while (new_reph_pos < end &&
Behdad Esfahbodbe8b9f52012-07-19 12:11:12 -0400948 !( FLAG (info[new_reph_pos + 1].indic_position()) & (FLAG (POS_POST_C) | FLAG (POS_AFTER_POST) | FLAG (POS_SMVD))))
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200949 new_reph_pos++;
950 if (new_reph_pos < end)
951 goto reph_move;
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200952 }
953
954 /* 5. If no consonant is found in steps 3 or 4, move reph to a position
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200955 * immediately before the first post-base matra, syllable modifier
956 * sign or vedic sign that has a reordering class after the intended
957 * reph position. For example, if the reordering position for reph
958 * is post-main, it will skip above-base matras that also have a
959 * post-main position.
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200960 */
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200961 reph_step_5:
962 {
Behdad Esfahbodd0e68db2012-07-20 11:25:41 -0400963 /* Copied from step 2. */
964 new_reph_pos = start + 1;
965 while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos]))
966 new_reph_pos++;
967
968 if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos])) {
969 /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
970 if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
971 new_reph_pos++;
972 goto reph_move;
973 }
Behdad Esfahbod8df56362012-05-10 15:41:04 +0200974 }
975
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200976 /* 6. Otherwise, reorder reph to the end of the syllable.
977 */
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200978 {
979 new_reph_pos = end - 1;
980 while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD)
981 new_reph_pos--;
982
Behdad Esfahbod892eb782012-05-11 16:54:40 +0200983 /*
984 * If the Reph is to be ending up after a Matra,Halant sequence,
985 * position it before that Halant so it can interact with the Matra.
986 * However, if it's a plain Consonant,Halant we shouldn't do that.
987 * Uniscribe doesn't do this.
988 * TEST: U+0930,U+094D,U+0915,U+094B,U+094D
989 */
Behdad Esfahboda2b471d2012-06-05 15:17:44 -0400990 if (!indic_options ().uniscribe_bug_compatible &&
Behdad Esfahboddeb521d2012-07-17 11:37:32 -0400991 unlikely (is_halant_or_coeng (info[new_reph_pos]))) {
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200992 for (unsigned int i = base + 1; i < new_reph_pos; i++)
993 if (info[i].indic_category() == OT_M) {
994 /* Ok, got it. */
995 new_reph_pos--;
996 }
997 }
998 goto reph_move;
999 }
1000
1001 reph_move:
1002 {
Behdad Esfahbode6b01a82012-07-23 00:11:26 -04001003 /* Yay, one big cluster! Merge before moving. */
1004 buffer->merge_clusters (start, end);
1005
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001006 /* Move */
1007 hb_glyph_info_t reph = info[start];
1008 memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0]));
1009 info[new_reph_pos] = reph;
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001010 }
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001011 }
1012
1013
1014 /* o Reorder pre-base reordering consonants:
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001015 *
1016 * If a pre-base reordering consonant is found, reorder it according to
1017 * the following rules:
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001018 */
1019
Behdad Esfahbod46e645e2012-07-16 15:30:05 -04001020 if (pref_mask && base + 1 < end) /* Otherwise there can't be any pre-base reordering Ra. */
1021 {
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001022 for (unsigned int i = base + 1; i < end; i++)
1023 if ((info[i].mask & pref_mask) != 0)
Behdad Esfahbod78818122012-07-16 15:49:08 -04001024 {
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001025 /* 1. Only reorder a glyph produced by substitution during application
1026 * of the <pref> feature. (Note that a font may shape a Ra consonant with
1027 * the feature generally but block it in certain contexts.)
1028 */
1029 if (i + 1 == end || (info[i + 1].mask & pref_mask) == 0)
1030 {
1031 /*
1032 * 2. Try to find a target position the same way as for pre-base matra.
1033 * If it is found, reorder pre-base consonant glyph.
1034 *
1035 * 3. If position is not found, reorder immediately before main
1036 * consonant.
1037 */
1038
1039 unsigned int new_pos = base;
Behdad Esfahbod0afb84c2012-07-24 01:44:47 -04001040 while (new_pos > start &&
1041 !(is_one_of (info[new_pos - 1], FLAG(OT_M) | HALANT_OR_COENG_FLAGS)))
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001042 new_pos--;
1043
Behdad Esfahbodd90b8e82012-07-24 02:10:20 -04001044 /* In Khmer coeng model, a V,Ra can go *after* matras. If it goes after a
1045 * split matra, it should be reordered to *before* the left part of such matra. */
1046 if (new_pos > start && info[new_pos - 1].indic_category() == OT_M)
1047 {
1048 unsigned int old_pos = i;
1049 for (unsigned int i = base + 1; i < old_pos; i++)
1050 if (info[i].indic_category() == OT_M)
1051 {
1052 new_pos--;
1053 break;
1054 }
1055 }
1056
Behdad Esfahboddeb521d2012-07-17 11:37:32 -04001057 if (new_pos > start && is_halant_or_coeng (info[new_pos - 1]))
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001058 /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
1059 if (new_pos < end && is_joiner (info[new_pos]))
1060 new_pos++;
1061
1062 {
1063 unsigned int old_pos = i;
Behdad Esfahbode6b01a82012-07-23 00:11:26 -04001064 buffer->merge_clusters (new_pos, old_pos + 1);
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001065 hb_glyph_info_t tmp = info[old_pos];
1066 memmove (&info[new_pos + 1], &info[new_pos], (old_pos - new_pos) * sizeof (info[0]));
1067 info[new_pos] = tmp;
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001068 }
1069 }
1070
1071 break;
Behdad Esfahbod78818122012-07-16 15:49:08 -04001072 }
Behdad Esfahbod46e645e2012-07-16 15:30:05 -04001073 }
Behdad Esfahbodeed903b2012-05-11 20:50:53 +02001074
1075
Behdad Esfahboda913b022012-05-11 20:59:26 +02001076 /* Apply 'init' to the Left Matra if it's a word start. */
Behdad Esfahbod6a091df2012-05-11 21:42:27 +02001077 if (info[start].indic_position () == POS_PRE_M &&
Behdad Esfahboda913b022012-05-11 20:59:26 +02001078 (!start ||
Behdad Esfahbodeace47b2012-05-13 15:54:43 +02001079 !(FLAG (_hb_glyph_info_get_general_category (&info[start - 1])) &
Behdad Esfahbod2c372b82012-07-20 13:37:48 -04001080 FLAG_RANGE (HB_UNICODE_GENERAL_CATEGORY_FORMAT, HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK))))
Behdad Esfahbod46e645e2012-07-16 15:30:05 -04001081 info[start].mask |= init_mask;
Behdad Esfahboda913b022012-05-11 20:59:26 +02001082
Behdad Esfahbodeed903b2012-05-11 20:50:53 +02001083
Behdad Esfahbod8ed248d2012-07-20 11:42:24 -04001084 /*
1085 * Finish off the clusters and go home!
1086 */
Behdad Esfahboddecf6ff2012-07-20 13:51:31 -04001087 if (indic_options ().uniscribe_bug_compatible)
Behdad Esfahbodebe29732012-05-11 16:43:12 +02001088 {
Behdad Esfahbod30c3d5e2012-07-20 13:56:32 -04001089 /* Uniscribe merges the entire cluster.
Behdad Esfahbod21d28032012-05-10 18:34:34 +02001090 * This means, half forms are submerged into the main consonants cluster.
1091 * This is unnecessary, and makes cursor positioning harder, but that's what
1092 * Uniscribe does. */
Behdad Esfahbode6b01a82012-07-23 00:11:26 -04001093 buffer->merge_clusters (start, end);
Behdad Esfahbod21d28032012-05-10 18:34:34 +02001094 }
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001095}
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001096
1097
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001098static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -04001099final_reordering (const hb_ot_shape_plan_t *plan,
Behdad Esfahbodafbcc242012-08-02 08:36:40 -04001100 hb_font_t *font HB_UNUSED,
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -04001101 hb_buffer_t *buffer)
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001102{
1103 unsigned int count = buffer->len;
1104 if (!count) return;
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001105
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -04001106 hb_mask_t init_mask = plan->map.get_1_mask (HB_TAG('i','n','i','t'));
1107 hb_mask_t pref_mask = plan->map.get_1_mask (HB_TAG('p','r','e','f'));
Behdad Esfahbodeed903b2012-05-11 20:50:53 +02001108
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001109 hb_glyph_info_t *info = buffer->info;
1110 unsigned int last = 0;
Behdad Esfahbodcee71872012-05-11 11:41:39 +02001111 unsigned int last_syllable = info[0].syllable();
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001112 for (unsigned int i = 1; i < count; i++)
Behdad Esfahbodcee71872012-05-11 11:41:39 +02001113 if (last_syllable != info[i].syllable()) {
Behdad Esfahbod46e645e2012-07-16 15:30:05 -04001114 final_reordering_syllable (buffer, init_mask, pref_mask, last, i);
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001115 last = i;
Behdad Esfahbodcee71872012-05-11 11:41:39 +02001116 last_syllable = info[last].syllable();
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001117 }
Behdad Esfahbod46e645e2012-07-16 15:30:05 -04001118 final_reordering_syllable (buffer, init_mask, pref_mask, last, count);
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001119
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001120 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
1121 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
1122}
1123
1124
Behdad Esfahbod693918e2012-07-30 21:08:51 -04001125const hb_ot_complex_shaper_t _hb_ot_complex_shaper_indic =
1126{
1127 "indic",
1128 collect_features_indic,
1129 override_features_indic,
Behdad Esfahboda8c6da92012-08-02 10:46:34 -04001130 data_create_indic,
1131 data_destroy_indic,
Behdad Esfahbod693918e2012-07-30 21:08:51 -04001132 NULL, /* normalization_preference */
1133 setup_masks_indic,
Behdad Esfahbod1e7d8602012-07-31 23:41:06 -04001134 false, /* zero_width_attached_marks */
Behdad Esfahbod693918e2012-07-30 21:08:51 -04001135};