blob: 6b55fb76044ddb4994283eece432b4614d43b4a6 [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 Esfahbod3614ba22012-08-02 07:13:55 -040085struct indic_shape_plan_t
Behdad Esfahbod743807a2011-07-29 16:37:02 -040086{
Behdad Esfahbod3614ba22012-08-02 07:13:55 -040087 struct would_apply_feature_t
Behdad Esfahbod610e5e82012-08-02 05:27:46 -040088 {
Behdad Esfahbod3614ba22012-08-02 07:13:55 -040089 would_apply_feature_t (const hb_ot_map_t *map, hb_tag_t feature_tag)
Behdad Esfahbod610e5e82012-08-02 05:27:46 -040090 {
91 map->get_stage_lookups (0/*GSUB*/,
92 map->get_feature_stage (0/*GSUB*/, feature_tag),
93 &lookups, &count);
94 }
Behdad Esfahbod743807a2011-07-29 16:37:02 -040095
Behdad Esfahbod610e5e82012-08-02 05:27:46 -040096 inline bool would_substitute (hb_codepoint_t *glyphs,
97 unsigned int glyphs_count,
98 hb_face_t *face) const
99 {
100 for (unsigned int i = 0; i < count; i++)
101 if (hb_ot_layout_would_substitute_lookup_fast (face, glyphs, glyphs_count, lookups[i].index))
102 return true;
103 return false;
104 }
Behdad Esfahbod0de771b2012-07-16 13:39:36 -0400105
Behdad Esfahbod610e5e82012-08-02 05:27:46 -0400106 private:
107 const hb_ot_map_t::lookup_map_t *lookups;
108 unsigned int count;
109 };
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400110
Behdad Esfahbod3614ba22012-08-02 07:13:55 -0400111 indic_shape_plan_t (const hb_ot_map_t *map_) :
112 map (map_),
113 pref (map_, HB_TAG('p','r','e','f')),
114 blwf (map_, HB_TAG('b','l','w','f')),
115 pstf (map_, HB_TAG('p','s','t','f')),
116 is_old_spec (IS_OLD_INDIC_TAG (map->get_chosen_script (0))) {}
Behdad Esfahbodf0554422012-07-19 16:20:21 -0400117
Behdad Esfahbod610e5e82012-08-02 05:27:46 -0400118 const hb_ot_map_t *map;
Behdad Esfahbod3614ba22012-08-02 07:13:55 -0400119 would_apply_feature_t pref;
120 would_apply_feature_t blwf;
121 would_apply_feature_t pstf;
122 bool is_old_spec;
Behdad Esfahbod610e5e82012-08-02 05:27:46 -0400123};
Behdad Esfahbodf0554422012-07-19 16:20:21 -0400124
125static indic_position_t
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400126consonant_position_from_font (const indic_shape_plan_t *indic_plan,
127 hb_codepoint_t *glyphs, unsigned int glyphs_len,
128 hb_face_t *face)
Behdad Esfahbodf0554422012-07-19 16:20:21 -0400129{
Behdad Esfahbod3614ba22012-08-02 07:13:55 -0400130 if (indic_plan->pref.would_substitute (glyphs, ARRAY_LENGTH (glyphs), face)) return POS_BELOW_C;
131 if (indic_plan->blwf.would_substitute (glyphs, ARRAY_LENGTH (glyphs), face)) return POS_BELOW_C;
132 if (indic_plan->pstf.would_substitute (glyphs, ARRAY_LENGTH (glyphs), face)) return POS_POST_C;
Behdad Esfahbodf0554422012-07-19 16:20:21 -0400133 return POS_BASE_C;
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400134}
135
Behdad Esfahbod9ccc6382012-07-19 12:32:16 -0400136
137
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200138struct feature_list_t {
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400139 hb_tag_t tag;
140 hb_bool_t is_global;
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200141};
142
Behdad Esfahbodd5c4edc2012-07-17 10:40:59 -0400143/* These features are applied one at a time, given the order in this table. */
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200144static const feature_list_t
145indic_basic_features[] =
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400146{
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400147 {HB_TAG('n','u','k','t'), true},
Behdad Esfahbode0475342012-07-19 20:24:14 -0400148 {HB_TAG('a','k','h','n'), true},
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400149 {HB_TAG('r','p','h','f'), false},
Behdad Esfahbod1ac075b2012-05-09 11:06:47 +0200150 {HB_TAG('r','k','r','f'), true},
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400151 {HB_TAG('p','r','e','f'), false},
152 {HB_TAG('b','l','w','f'), false},
153 {HB_TAG('h','a','l','f'), false},
Behdad Esfahbod29f106d2012-07-16 12:05:35 -0400154 {HB_TAG('a','b','v','f'), false},
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400155 {HB_TAG('p','s','t','f'), false},
Behdad Esfahbod0201e0a2012-07-17 13:55:10 -0400156 {HB_TAG('c','f','a','r'), false},
Behdad Esfahbod20b68e62012-07-20 10:47:46 -0400157 {HB_TAG('c','j','c','t'), true},
Behdad Esfahbod1d6846d2012-05-13 18:09:29 +0200158 {HB_TAG('v','a','t','u'), true},
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400159};
160
161/* Same order as the indic_basic_features array */
162enum {
163 _NUKT,
Behdad Esfahbode0475342012-07-19 20:24:14 -0400164 _AKHN,
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400165 RPHF,
Behdad Esfahboddf6d45c2012-05-09 11:38:31 +0200166 _RKRF,
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400167 PREF,
168 BLWF,
169 HALF,
Behdad Esfahbod29f106d2012-07-16 12:05:35 -0400170 ABVF,
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400171 PSTF,
Behdad Esfahbod0201e0a2012-07-17 13:55:10 -0400172 CFAR,
Behdad Esfahbod20b68e62012-07-20 10:47:46 -0400173 _CJCT,
Behdad Esfahbod1d6846d2012-05-13 18:09:29 +0200174 VATU
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400175};
176
Behdad Esfahbodd5c4edc2012-07-17 10:40:59 -0400177/* These features are applied all at once. */
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200178static const feature_list_t
179indic_other_features[] =
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400180{
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200181 {HB_TAG('i','n','i','t'), false},
182 {HB_TAG('p','r','e','s'), true},
183 {HB_TAG('a','b','v','s'), true},
184 {HB_TAG('b','l','w','s'), true},
185 {HB_TAG('p','s','t','s'), true},
186 {HB_TAG('h','a','l','n'), true},
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400187
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200188 {HB_TAG('d','i','s','t'), true},
189 {HB_TAG('a','b','v','m'), true},
190 {HB_TAG('b','l','w','m'), true},
191};
192
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400193
194static void
195initial_reordering (const hb_ot_map_t *map,
Behdad Esfahbodafbcc242012-08-02 08:36:40 -0400196 hb_font_t *font,
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400197 hb_buffer_t *buffer);
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400198static void
199final_reordering (const hb_ot_map_t *map,
Behdad Esfahbodafbcc242012-08-02 08:36:40 -0400200 hb_font_t *font,
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400201 hb_buffer_t *buffer);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400202
Behdad Esfahbod693918e2012-07-30 21:08:51 -0400203static void
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400204collect_features_indic (hb_ot_shape_planner_t *plan)
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400205{
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400206 hb_ot_map_builder_t *map = &plan->map;
207
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400208 map->add_bool_feature (HB_TAG('l','o','c','l'));
Behdad Esfahboda54a5502011-07-20 16:42:10 -0400209 /* The Indic specs do not require ccmp, but we apply it here since if
210 * there is a use of it, it's typically at the beginning. */
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400211 map->add_bool_feature (HB_TAG('c','c','m','p'));
212
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400213 map->add_gsub_pause (initial_reordering);
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400214
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200215 for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++) {
Behdad Esfahbod76f76812011-07-07 22:25:25 -0400216 map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400217 map->add_gsub_pause (NULL);
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200218 }
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400219
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400220 map->add_gsub_pause (final_reordering);
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400221
Behdad Esfahbodd5c4edc2012-07-17 10:40:59 -0400222 for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++)
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200223 map->add_bool_feature (indic_other_features[i].tag, indic_other_features[i].is_global);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400224}
225
Behdad Esfahbod693918e2012-07-30 21:08:51 -0400226static void
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400227override_features_indic (hb_ot_shape_planner_t *plan)
Behdad Esfahbodd96838e2012-07-16 20:26:57 -0400228{
Behdad Esfahbodaf92b4c2012-07-16 20:31:24 -0400229 /* Uniscribe does not apply 'kern'. */
230 if (indic_options ().uniscribe_bug_compatible)
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400231 plan->map.add_feature (HB_TAG('k','e','r','n'), 0, true);
Behdad Esfahbodd96838e2012-07-16 20:26:57 -0400232}
233
Behdad Esfahbod867361c2011-06-17 18:35:46 -0400234
Behdad Esfahbod693918e2012-07-30 21:08:51 -0400235static void
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400236setup_masks_indic (const hb_ot_shape_plan_t *plan HB_UNUSED,
237 hb_buffer_t *buffer,
238 hb_font_t *font HB_UNUSED)
Behdad Esfahbod24eacf12012-08-02 08:42:11 -0400239{
240 HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
241 HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
242
243 /* We cannot setup masks here. We save information about characters
244 * and setup masks later on in a pause-callback. */
245
246 unsigned int count = buffer->len;
247 for (unsigned int i = 0; i < count; i++)
248 set_indic_properties (buffer->info[i]);
249}
250
251static int
252compare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
253{
254 int a = pa->indic_position();
255 int b = pb->indic_position();
256
257 return a < b ? -1 : a == b ? 0 : +1;
258}
259
260
261
262static void
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400263update_consonant_positions (const hb_ot_map_t *map,
264 hb_buffer_t *buffer,
265 hb_font_t *font)
266{
267 hb_codepoint_t virama;
268 switch ((int) buffer->props.script) {
269 case HB_SCRIPT_DEVANAGARI: virama = 0x094D; break;
270 case HB_SCRIPT_BENGALI: virama = 0x09CD; break;
271 case HB_SCRIPT_GURMUKHI: virama = 0x0A4D; break;
272 case HB_SCRIPT_GUJARATI: virama = 0x0ACD; break;
273 case HB_SCRIPT_ORIYA: virama = 0x0B4D; break;
274 case HB_SCRIPT_TAMIL: virama = 0x0BCD; break;
275 case HB_SCRIPT_TELUGU: virama = 0x0C4D; break;
276 case HB_SCRIPT_KANNADA: virama = 0x0CCD; break;
277 case HB_SCRIPT_MALAYALAM: virama = 0x0D4D; break;
278 case HB_SCRIPT_SINHALA: virama = 0x0DCA; break;
279 case HB_SCRIPT_KHMER: virama = 0x17D2; break;
Behdad Esfahbod24eacf12012-08-02 08:42:11 -0400280 default: virama = 0; break;
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400281 }
282
283 indic_shape_plan_t indic_plan (map);
284
285 unsigned int consonant_pos = indic_plan.is_old_spec ? 0 : 1;
286 hb_codepoint_t glyphs[2];
287 if (virama && font->get_glyph (virama, 0, &glyphs[1 - consonant_pos]))
288 {
Behdad Esfahbod24eacf12012-08-02 08:42:11 -0400289 /* Technically speaking, the spec says we should apply 'locl' to virama too.
290 * Maybe one day... */
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400291 hb_face_t *face = font->face;
292 unsigned int count = buffer->len;
293 for (unsigned int i = 0; i < count; i++)
294 if (buffer->info[i].indic_position() == POS_BASE_C) {
Behdad Esfahbod24eacf12012-08-02 08:42:11 -0400295 glyphs[consonant_pos] = buffer->info[i].codepoint;
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400296 buffer->info[i].indic_position() = consonant_position_from_font (&indic_plan, glyphs, 2, face);
297 }
298 }
299}
300
Behdad Esfahbod867361c2011-06-17 18:35:46 -0400301
Behdad Esfahbod7ea58db2012-05-11 18:58:57 +0200302/* Rules from:
303 * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
304
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400305static void
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400306initial_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 +0200307 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400308{
Behdad Esfahbodee58f3b2011-07-30 19:15:53 -0400309 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400310
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200311
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400312 /* 1. Find base consonant:
313 *
314 * The shaping engine finds the base consonant of the syllable, using the
315 * following algorithm: starting from the end of the syllable, move backwards
316 * until a consonant is found that does not have a below-base or post-base
317 * form (post-base forms have to follow below-base forms), or that is not a
318 * pre-base reordering Ra, or arrive at the first consonant. The consonant
319 * stopped at will be the base.
320 *
321 * o If the syllable starts with Ra + Halant (in a script that has Reph)
322 * and has more than one consonant, Ra is excluded from candidates for
323 * base consonants.
324 */
325
Behdad Esfahbod5e720712011-07-31 17:51:50 -0400326 unsigned int base = end;
Behdad Esfahbod76b34092012-05-09 11:43:43 +0200327 bool has_reph = false;
328
Behdad Esfahbod76b34092012-05-09 11:43:43 +0200329 {
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200330 /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
331 * and has more than one consonant, Ra is excluded from candidates for
332 * base consonants. */
333 unsigned int limit = start;
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400334 if (basic_mask_array[RPHF] &&
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200335 start + 3 <= end &&
336 info[start].indic_category() == OT_Ra &&
337 info[start + 1].indic_category() == OT_H &&
Behdad Esfahbodf31d97e2012-07-20 14:13:35 -0400338 (unlikely (buffer->props.script == HB_SCRIPT_SINHALA || buffer->props.script == HB_SCRIPT_TELUGU) ?
339 info[start + 2].indic_category() == OT_ZWJ /* In Sinhala & Telugu, form Reph only if ZWJ is present */:
Behdad Esfahbod3285e102012-07-18 17:22:14 -0400340 !is_joiner (info[start + 2] /* In other scripts, any joiner blocks Reph formation */ )
341 ))
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200342 {
343 limit += 2;
Behdad Esfahbod3285e102012-07-18 17:22:14 -0400344 while (limit < end && is_joiner (info[limit]))
345 limit++;
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200346 base = start;
347 has_reph = true;
348 };
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400349
Behdad Esfahbod14dbdd92012-07-18 13:13:03 -0400350 enum base_position_t {
351 BASE_FIRST,
352 BASE_LAST
353 } base_pos;
354
355 switch ((hb_tag_t) buffer->props.script)
356 {
Behdad Esfahbod34c21502012-07-23 23:51:29 -0400357 case HB_SCRIPT_SINHALA:
Behdad Esfahbod14dbdd92012-07-18 13:13:03 -0400358 case HB_SCRIPT_KHMER:
359 base_pos = BASE_FIRST;
360 break;
361
362 default:
363 base_pos = BASE_LAST;
364 break;
365 }
366
367 if (base_pos == BASE_LAST)
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400368 {
369 /* -> starting from the end of the syllable, move backwards */
370 unsigned int i = end;
Behdad Esfahbod92a1ad72012-07-20 18:38:27 -0400371 bool seen_below = false;
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400372 do {
373 i--;
374 /* -> until a consonant is found */
375 if (is_consonant (info[i]))
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200376 {
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400377 /* -> that does not have a below-base or post-base form
378 * (post-base forms have to follow below-base forms), */
379 if (info[i].indic_position() != POS_BELOW_C &&
Behdad Esfahbod92a1ad72012-07-20 18:38:27 -0400380 (info[i].indic_position() != POS_POST_C || seen_below))
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400381 {
382 base = i;
383 break;
384 }
Behdad Esfahbod92a1ad72012-07-20 18:38:27 -0400385 if (info[i].indic_position() == POS_BELOW_C)
386 seen_below = true;
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400387
388 /* -> or that is not a pre-base reordering Ra,
389 *
Behdad Esfahbod25e302d2012-07-17 14:25:14 -0400390 * IMPLEMENTATION NOTES:
391 *
392 * Our pre-base reordering Ra's are marked POS_BELOW, so will be skipped
393 * by the logic above already.
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400394 */
395
396 /* -> or arrive at the first consonant. The consonant stopped at will
397 * be the base. */
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200398 base = i;
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200399 }
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400400 else
Behdad Esfahboda9e45c32012-07-20 11:04:15 -0400401 {
Behdad Esfahbod88f413b2012-07-24 03:04:36 -0400402 /* A ZWJ after a Halant stops the base search, and requests an explicit
403 * half form.
404 * A ZWJ before a Halant, requests a subjoined form instead, and hence
405 * search continues. This is particularly important for Bengali
406 * sequence Ra,H,Ya that shouls form Ya-Phalaa by subjoining Ya. */
407 if (start < i &&
408 info[i].indic_category() == OT_ZWJ &&
409 info[i - 1].indic_category() == OT_H)
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400410 break;
Behdad Esfahboda9e45c32012-07-20 11:04:15 -0400411 }
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400412 } while (i > limit);
413 }
414 else
415 {
416 /* In scripts without half forms (eg. Khmer), the first consonant is always the base. */
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200417
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400418 if (!has_reph)
419 base = limit;
Behdad Esfahbod34c21502012-07-23 23:51:29 -0400420
421 /* Find the last base consonant that is not blocked by ZWJ. If there is
Behdad Esfahbod71fd5e82012-07-24 00:21:16 -0400422 * a ZWJ right before a base consonant, that would request a subjoined form. */
Behdad Esfahbod34c21502012-07-23 23:51:29 -0400423 for (unsigned int i = limit; i < end; i++)
424 if (is_consonant (info[i]) && info[i].indic_position() == POS_BASE_C)
Behdad Esfahbod71fd5e82012-07-24 00:21:16 -0400425 {
426 if (limit < i && info[i - 1].indic_category() == OT_ZWJ)
427 break;
428 else
429 base = i;
430 }
Behdad Esfahbod34c21502012-07-23 23:51:29 -0400431
432 /* Mark all subsequent consonants as below. */
433 for (unsigned int i = base + 1; i < end; i++)
434 if (is_consonant (info[i]) && info[i].indic_position() == POS_BASE_C)
435 info[i].indic_position() = POS_BELOW_C;
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400436 }
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200437
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200438 /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
439 * and has more than one consonant, Ra is excluded from candidates for
Behdad Esfahbod2278eef2012-07-24 00:26:43 -0400440 * base consonants.
441 *
442 * Only do this for unforced Reph. (ie. not for Ra,H,ZWJ. */
443 if (has_reph && base == start && start + 2 == limit) {
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200444 /* Have no other consonant, so Reph is not formed and Ra becomes base. */
445 has_reph = false;
446 }
Behdad Esfahbod5e4e21f2012-05-13 16:46:08 +0200447 }
Behdad Esfahbod2278eef2012-07-24 00:26:43 -0400448
Behdad Esfahbod34c21502012-07-23 23:51:29 -0400449 if (base < end)
450 info[base].indic_position() = POS_BASE_C;
Behdad Esfahbod3d250792012-05-10 11:37:42 +0200451
452
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400453 /* 2. Decompose and reorder Matras:
454 *
455 * Each matra and any syllable modifier sign in the cluster are moved to the
456 * appropriate position relative to the consonant(s) in the cluster. The
457 * shaping engine decomposes two- or three-part matras into their constituent
458 * parts before any repositioning. Matra characters are classified by which
459 * consonant in a conjunct they have affinity for and are reordered to the
460 * following positions:
461 *
462 * o Before first half form in the syllable
463 * o After subjoined consonants
464 * o After post-form consonant
465 * o After main consonant (for above marks)
466 *
467 * IMPLEMENTATION NOTES:
468 *
469 * The normalize() routine has already decomposed matras for us, so we don't
470 * need to worry about that.
471 */
472
473
474 /* 3. Reorder marks to canonical order:
475 *
476 * Adjacent nukta and halant or nukta and vedic sign are always repositioned
477 * if necessary, so that the nukta is first.
478 *
479 * IMPLEMENTATION NOTES:
480 *
481 * We don't need to do this: the normalize() routine already did this for us.
482 */
483
484
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400485 /* Reorder characters */
486
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200487 for (unsigned int i = start; i < base; i++)
Behdad Esfahbod900cf3d2012-07-20 10:18:23 -0400488 info[i].indic_position() = MIN (POS_PRE_C, (indic_position_t) info[i].indic_position());
Behdad Esfahbod55f70eb2012-07-17 12:50:13 -0400489
Behdad Esfahbod075d6712012-07-18 15:41:53 -0400490 if (base < end)
491 info[base].indic_position() = POS_BASE_C;
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400492
Behdad Esfahbod55f70eb2012-07-17 12:50:13 -0400493 /* Mark final consonants. A final consonant is one appearing after a matra,
494 * like in Khmer. */
495 for (unsigned int i = base + 1; i < end; i++)
496 if (info[i].indic_category() == OT_M) {
497 for (unsigned int j = i + 1; j < end; j++)
498 if (is_consonant (info[j])) {
499 info[j].indic_position() = POS_FINAL_C;
500 break;
501 }
502 break;
503 }
504
Behdad Esfahbodfd06bf52011-07-30 20:14:44 -0400505 /* Handle beginning Ra */
Behdad Esfahbod5e4e21f2012-05-13 16:46:08 +0200506 if (has_reph)
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200507 info[start].indic_position() = POS_RA_TO_BECOME_REPH;
Behdad Esfahbodfd06bf52011-07-30 20:14:44 -0400508
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400509 /* For old-style Indic script tags, move the first post-base Halant after
510 * last consonant. */
Behdad Esfahboda3e04be2012-07-16 13:47:19 -0400511 if (IS_OLD_INDIC_TAG (map->get_chosen_script (0))) {
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200512 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400513 if (info[i].indic_category() == OT_H) {
514 unsigned int j;
515 for (j = end - 1; j > i; j--)
Behdad Esfahbod190eb312012-05-10 12:17:16 +0200516 if (is_consonant (info[j]))
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400517 break;
518 if (j > i) {
519 /* Move Halant to after last consonant. */
520 hb_glyph_info_t t = info[i];
521 memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
522 info[j] = t;
523 }
524 break;
525 }
526 }
527
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400528 /* Attach misc marks to previous char to move with them. */
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200529 {
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400530 indic_position_t last_pos = POS_START;
531 for (unsigned int i = start; i < end; i++)
532 {
533 if ((FLAG (info[i].indic_category()) & (JOINER_FLAGS | FLAG (OT_N) | FLAG (OT_RS) | HALANT_OR_COENG_FLAGS)))
534 {
535 info[i].indic_position() = last_pos;
536 if (unlikely (indic_options ().uniscribe_bug_compatible &&
537 info[i].indic_category() == OT_H &&
538 info[i].indic_position() == POS_PRE_M))
539 {
540 /*
541 * Uniscribe doesn't move the Halant with Left Matra.
542 * TEST: U+092B,U+093F,U+094DE
543 */
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200544 for (unsigned int j = i; j > start; j--)
Behdad Esfahbod6a091df2012-05-11 21:42:27 +0200545 if (info[j - 1].indic_position() != POS_PRE_M) {
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200546 info[i].indic_position() = info[j - 1].indic_position();
547 break;
548 }
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400549 }
550 } else if (info[i].indic_position() != POS_SMVD) {
551 last_pos = (indic_position_t) info[i].indic_position();
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200552 }
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400553 }
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200554 }
Behdad Esfahbod74ccc6a2012-07-17 11:16:19 -0400555 /* Re-attach ZWJ, ZWNJ, and halant to next char, for after-base consonants. */
556 {
557 unsigned int last_halant = end;
558 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahboddeb521d2012-07-17 11:37:32 -0400559 if (is_halant_or_coeng (info[i]))
Behdad Esfahbod74ccc6a2012-07-17 11:16:19 -0400560 last_halant = i;
561 else if (is_consonant (info[i])) {
562 for (unsigned int j = last_halant; j < i; j++)
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400563 if (info[j].indic_position() != POS_SMVD)
564 info[j].indic_position() = info[i].indic_position();
Behdad Esfahbod74ccc6a2012-07-17 11:16:19 -0400565 }
566 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400567
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200568 {
Behdad Esfahbod7b2a7da2012-07-22 23:58:55 -0400569 /* Things are out-of-control for post base positions, they may shuffle
570 * around like crazy, so merge clusters. For pre-base stuff, we handle
571 * cluster issues in final reordering. */
572 buffer->merge_clusters (base, end);
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200573 /* Sit tight, rock 'n roll! */
Behdad Esfahbodd3637ed2012-05-10 10:51:38 +0200574 hb_bubble_sort (info + start, end - start, compare_indic_order);
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200575 /* Find base again */
576 base = end;
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200577 for (unsigned int i = start; i < end; i++)
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200578 if (info[i].indic_position() == POS_BASE_C) {
579 base = i;
580 break;
581 }
582 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400583
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400584 /* Setup masks now */
585
Behdad Esfahbod28168392011-07-31 16:00:35 -0400586 {
587 hb_mask_t mask;
588
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200589 /* Reph */
Behdad Esfahbod668c6042012-05-11 15:34:13 +0200590 for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++)
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400591 info[i].mask |= basic_mask_array[RPHF];
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200592
Behdad Esfahbod28168392011-07-31 16:00:35 -0400593 /* Pre-base */
Behdad Esfahbod20b68e62012-07-20 10:47:46 -0400594 mask = basic_mask_array[HALF];
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200595 for (unsigned int i = start; i < base; i++)
Behdad Esfahbod28168392011-07-31 16:00:35 -0400596 info[i].mask |= mask;
597 /* Base */
Behdad Esfahbod20b68e62012-07-20 10:47:46 -0400598 mask = 0;
Behdad Esfahbod075d6712012-07-18 15:41:53 -0400599 if (base < end)
600 info[base].mask |= mask;
Behdad Esfahbod28168392011-07-31 16:00:35 -0400601 /* Post-base */
Behdad Esfahbod20b68e62012-07-20 10:47:46 -0400602 mask = basic_mask_array[BLWF] | basic_mask_array[ABVF] | basic_mask_array[PSTF];
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200603 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbod28168392011-07-31 16:00:35 -0400604 info[i].mask |= mask;
605 }
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400606
Behdad Esfahbod17d7de92012-07-16 15:20:15 -0400607 /* XXX This will not match for old-Indic spec since the Halant-Ra order is reversed already. */
Behdad Esfahbod5f0eaaa2012-07-20 15:47:24 -0400608 if (basic_mask_array[PREF] && base + 2 < end)
Behdad Esfahbod17d7de92012-07-16 15:20:15 -0400609 {
Behdad Esfahbod771a8f52012-07-23 20:07:50 -0400610 /* Find a Halant,Ra sequence and mark it for pre-base reordering processing. */
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -0400611 for (unsigned int i = base + 1; i + 1 < end; i++)
Behdad Esfahboddeb521d2012-07-17 11:37:32 -0400612 if (is_halant_or_coeng (info[i]) &&
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -0400613 info[i + 1].indic_category() == OT_Ra)
614 {
Behdad Esfahbod0201e0a2012-07-17 13:55:10 -0400615 info[i++].mask |= basic_mask_array[PREF];
616 info[i++].mask |= basic_mask_array[PREF];
617
618 /* Mark the subsequent stuff with 'cfar'. Used in Khmer.
619 * Read the feature spec.
620 * This allows distinguishing the following cases with MS Khmer fonts:
621 * U+1784,U+17D2,U+179A,U+17D2,U+1782
622 * U+1784,U+17D2,U+1782,U+17D2,U+179A
623 */
624 for (; i < end; i++)
625 info[i].mask |= basic_mask_array[CFAR];
626
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -0400627 break;
628 }
Behdad Esfahbod17d7de92012-07-16 15:20:15 -0400629 }
630
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400631 /* Apply ZWJ/ZWNJ effects */
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200632 for (unsigned int i = start + 1; i < end; i++)
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400633 if (is_joiner (info[i])) {
634 bool non_joiner = info[i].indic_category() == OT_ZWNJ;
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400635 unsigned int j = i;
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400636
637 do {
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400638 j--;
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400639
Behdad Esfahbod20b68e62012-07-20 10:47:46 -0400640 /* A ZWJ disables CJCT, however, it's mere presence is enough
641 * to disable ligation. No explicit action needed. */
642
643 /* A ZWNJ disables HALF. */
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400644 if (non_joiner)
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400645 info[j].mask &= ~basic_mask_array[HALF];
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -0400646
Behdad Esfahbod9da04872011-07-31 13:46:44 -0400647 } while (j > start && !is_consonant (info[j]));
648 }
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400649}
650
651
652static void
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +0200653initial_reordering_vowel_syllable (const hb_ot_map_t *map,
654 hb_buffer_t *buffer,
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400655 hb_mask_t *basic_mask_array,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200656 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400657{
Behdad Esfahbodc5306b62012-05-10 12:07:33 +0200658 /* We made the vowels look like consonants. So let's call the consonant logic! */
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400659 initial_reordering_consonant_syllable (map, buffer, basic_mask_array, start, end);
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400660}
661
662static void
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +0200663initial_reordering_standalone_cluster (const hb_ot_map_t *map,
664 hb_buffer_t *buffer,
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400665 hb_mask_t *basic_mask_array,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200666 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400667{
Behdad Esfahbod18c06e12012-05-11 20:02:14 +0200668 /* We treat NBSP/dotted-circle as if they are consonants, so we should just chain.
669 * Only if not in compatibility mode that is... */
670
Behdad Esfahboda2b471d2012-06-05 15:17:44 -0400671 if (indic_options ().uniscribe_bug_compatible)
Behdad Esfahbod18c06e12012-05-11 20:02:14 +0200672 {
673 /* For dotted-circle, this is what Uniscribe does:
674 * If dotted-circle is the last glyph, it just does nothing.
675 * Ie. It doesn't form Reph. */
676 if (buffer->info[end - 1].indic_category() == OT_DOTTEDCIRCLE)
677 return;
678 }
679
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400680 initial_reordering_consonant_syllable (map, buffer, basic_mask_array, start, end);
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400681}
682
683static void
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +0200684initial_reordering_non_indic (const hb_ot_map_t *map HB_UNUSED,
685 hb_buffer_t *buffer HB_UNUSED,
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400686 hb_mask_t *basic_mask_array HB_UNUSED,
Behdad Esfahbod3f182362012-05-13 16:20:10 +0200687 unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400688{
689 /* Nothing to do right now. If we ever switch to using the output
690 * buffer in the reordering process, we'd need to next_glyph() here. */
691}
692
693#include "hb-ot-shape-complex-indic-machine.hh"
694
695static void
696initial_reordering (const hb_ot_map_t *map,
Behdad Esfahbod24eacf12012-08-02 08:42:11 -0400697 hb_font_t *font,
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400698 hb_buffer_t *buffer)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400699{
Behdad Esfahbod24eacf12012-08-02 08:42:11 -0400700 update_consonant_positions (map, buffer, font);
701
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400702 hb_mask_t basic_mask_array[ARRAY_LENGTH (indic_basic_features)] = {0};
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400703 unsigned int num_masks = ARRAY_LENGTH (indic_basic_features);
704 for (unsigned int i = 0; i < num_masks; i++)
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400705 basic_mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400706
Behdad Esfahbod70fe77b2012-07-16 14:52:18 -0400707 find_syllables (map, buffer, basic_mask_array);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400708}
709
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400710static void
Behdad Esfahbod46e645e2012-07-16 15:30:05 -0400711final_reordering_syllable (hb_buffer_t *buffer,
712 hb_mask_t init_mask, hb_mask_t pref_mask,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200713 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400714{
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200715 hb_glyph_info_t *info = buffer->info;
716
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400717 /* 4. Final reordering:
718 *
719 * After the localized forms and basic shaping forms GSUB features have been
720 * applied (see below), the shaping engine performs some final glyph
721 * reordering before applying all the remaining font features to the entire
722 * cluster.
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200723 */
724
725 /* Find base again */
Behdad Esfahbod5f0eaaa2012-07-20 15:47:24 -0400726 unsigned int base;
727 for (base = start; base < end; base++)
728 if (info[base].indic_position() >= POS_BASE_C) {
729 if (start < base && info[base].indic_position() > POS_BASE_C)
730 base--;
731 break;
732 }
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200733
Behdad Esfahbod4705a702012-05-10 13:09:08 +0200734
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200735 /* o Reorder matras:
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400736 *
737 * If a pre-base matra character had been reordered before applying basic
738 * features, the glyph can be moved closer to the main consonant based on
739 * whether half-forms had been formed. Actual position for the matra is
740 * defined as “after last standalone halant glyph, after initial matra
741 * position and before the main consonant”. If ZWJ or ZWNJ follow this
742 * halant, position is moved after it.
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200743 */
744
Behdad Esfahbod65c43ac2012-07-24 03:36:47 -0400745 if (start + 1 < end && start < base) /* Otherwise there can't be any pre-base matra characters. */
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200746 {
Behdad Esfahbod65c43ac2012-07-24 03:36:47 -0400747 /* If we lost track of base, alas, position before last thingy. */
748 unsigned int new_pos = base == end ? base - 2 : base - 1;
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200749
Behdad Esfahbod65c43ac2012-07-24 03:36:47 -0400750 /* Malayalam does not have "half" forms or explicit virama forms.
751 * The glyphs formed by 'half' are Chillus. We want to position
752 * matra after them all.
753 */
754 if (buffer->props.script != HB_SCRIPT_MALAYALAM)
755 {
756 while (new_pos > start &&
757 !(is_one_of (info[new_pos], (FLAG (OT_M) | FLAG (OT_H) | FLAG (OT_Coeng)))))
758 new_pos--;
759
760 /* If we found no Halant we are done.
761 * Otherwise only proceed if the Halant does
762 * not belong to the Matra itself! */
763 if (is_halant_or_coeng (info[new_pos]) &&
764 info[new_pos].indic_position() != POS_PRE_M)
765 {
766 /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
767 if (new_pos + 1 < end && is_joiner (info[new_pos + 1]))
768 new_pos++;
769 }
770 else
771 new_pos = start; /* No move. */
772 }
773
774 if (start < new_pos)
775 {
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200776 /* Now go see if there's actually any matras... */
Behdad Esfahbod921ce5b2012-07-16 15:26:56 -0400777 for (unsigned int i = new_pos; i > start; i--)
Behdad Esfahbod6a091df2012-05-11 21:42:27 +0200778 if (info[i - 1].indic_position () == POS_PRE_M)
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200779 {
Behdad Esfahbod1a1dbe92012-07-16 15:40:33 -0400780 unsigned int old_pos = i - 1;
781 hb_glyph_info_t tmp = info[old_pos];
782 memmove (&info[old_pos], &info[old_pos + 1], (new_pos - old_pos) * sizeof (info[0]));
783 info[new_pos] = tmp;
Behdad Esfahbod921ce5b2012-07-16 15:26:56 -0400784 new_pos--;
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200785 }
Behdad Esfahbod2cc933a2012-07-23 08:22:55 -0400786 buffer->merge_clusters (new_pos, MIN (end, base + 1));
Behdad Esfahbodabb32392012-07-22 23:55:19 -0400787 } else {
Behdad Esfahbode6b01a82012-07-23 00:11:26 -0400788 for (unsigned int i = start; i < base; i++)
Behdad Esfahbodabb32392012-07-22 23:55:19 -0400789 if (info[i].indic_position () == POS_PRE_M) {
Behdad Esfahbod2cc933a2012-07-23 08:22:55 -0400790 buffer->merge_clusters (i, MIN (end, base + 1));
Behdad Esfahbodabb32392012-07-22 23:55:19 -0400791 break;
792 }
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200793 }
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +0200794 }
795
796
797 /* o Reorder reph:
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400798 *
799 * Reph’s original position is always at the beginning of the syllable,
800 * (i.e. it is not reordered at the character reordering stage). However,
801 * it will be reordered according to the basic-forms shaping results.
802 * Possible positions for reph, depending on the script, are; after main,
803 * before post-base consonant forms, and after post-base consonant forms.
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200804 */
805
806 /* If there's anything after the Ra that has the REPH pos, it ought to be halant.
807 * Which means that the font has failed to ligate the Reph. In which case, we
808 * shouldn't move. */
809 if (start + 1 < end &&
810 info[start].indic_position() == POS_RA_TO_BECOME_REPH &&
811 info[start + 1].indic_position() != POS_RA_TO_BECOME_REPH)
812 {
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200813 unsigned int new_reph_pos;
814
815 enum reph_position_t {
Behdad Esfahbodf8936722012-05-11 21:10:03 +0200816 REPH_AFTER_MAIN,
817 REPH_BEFORE_SUBSCRIPT,
818 REPH_AFTER_SUBSCRIPT,
819 REPH_BEFORE_POSTSCRIPT,
Behdad Esfahbod9fc7a112012-06-04 08:28:19 -0400820 REPH_AFTER_POSTSCRIPT
Behdad Esfahbodf8936722012-05-11 21:10:03 +0200821 } reph_pos;
822
823 /* XXX Figure out old behavior too */
Behdad Esfahbod7f852b62012-05-11 23:10:31 +0200824 switch ((hb_tag_t) buffer->props.script)
Behdad Esfahbodf8936722012-05-11 21:10:03 +0200825 {
826 case HB_SCRIPT_MALAYALAM:
827 case HB_SCRIPT_ORIYA:
Behdad Esfahbod34c21502012-07-23 23:51:29 -0400828 case HB_SCRIPT_SINHALA:
Behdad Esfahbodf8936722012-05-11 21:10:03 +0200829 reph_pos = REPH_AFTER_MAIN;
830 break;
831
832 case HB_SCRIPT_GURMUKHI:
833 reph_pos = REPH_BEFORE_SUBSCRIPT;
834 break;
835
836 case HB_SCRIPT_BENGALI:
837 reph_pos = REPH_AFTER_SUBSCRIPT;
838 break;
839
840 default:
841 case HB_SCRIPT_DEVANAGARI:
842 case HB_SCRIPT_GUJARATI:
843 reph_pos = REPH_BEFORE_POSTSCRIPT;
844 break;
845
846 case HB_SCRIPT_KANNADA:
847 case HB_SCRIPT_TAMIL:
848 case HB_SCRIPT_TELUGU:
849 reph_pos = REPH_AFTER_POSTSCRIPT;
850 break;
851 }
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200852
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200853 /* 1. If reph should be positioned after post-base consonant forms,
854 * proceed to step 5.
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200855 */
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200856 if (reph_pos == REPH_AFTER_POSTSCRIPT)
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200857 {
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200858 goto reph_step_5;
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200859 }
860
861 /* 2. If the reph repositioning class is not after post-base: target
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200862 * position is after the first explicit halant glyph between the
863 * first post-reph consonant and last main consonant. If ZWJ or ZWNJ
864 * are following this halant, position is moved after it. If such
865 * position is found, this is the target position. Otherwise,
866 * proceed to the next step.
867 *
868 * Note: in old-implementation fonts, where classifications were
869 * fixed in shaping engine, there was no case where reph position
870 * will be found on this step.
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200871 */
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200872 {
873 new_reph_pos = start + 1;
Behdad Esfahboddeb521d2012-07-17 11:37:32 -0400874 while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos]))
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200875 new_reph_pos++;
876
Behdad Esfahboddeb521d2012-07-17 11:37:32 -0400877 if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos])) {
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200878 /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
879 if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
880 new_reph_pos++;
881 goto reph_move;
882 }
883 }
884
885 /* 3. If reph should be repositioned after the main consonant: find the
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200886 * first consonant not ligated with main, or find the first
887 * consonant that is not a potential pre-base reordering Ra.
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200888 */
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200889 if (reph_pos == REPH_AFTER_MAIN)
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200890 {
Behdad Esfahbodb504e062012-07-16 15:21:12 -0400891 new_reph_pos = base;
892 /* XXX Skip potential pre-base reordering Ra. */
Behdad Esfahbod34ae3362012-07-20 16:17:28 -0400893 while (new_reph_pos + 1 < end && info[new_reph_pos + 1].indic_position() <= POS_AFTER_MAIN)
Behdad Esfahbodb504e062012-07-16 15:21:12 -0400894 new_reph_pos++;
895 if (new_reph_pos < end)
896 goto reph_move;
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200897 }
898
899 /* 4. If reph should be positioned before post-base consonant, find
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200900 * first post-base classified consonant not ligated with main. If no
901 * consonant is found, the target position should be before the
902 * first matra, syllable modifier sign or vedic sign.
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200903 */
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200904 /* This is our take on what step 4 is trying to say (and failing, BADLY). */
905 if (reph_pos == REPH_AFTER_SUBSCRIPT)
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200906 {
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +0200907 new_reph_pos = base;
908 while (new_reph_pos < end &&
Behdad Esfahbodbe8b9f52012-07-19 12:11:12 -0400909 !( 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 +0200910 new_reph_pos++;
911 if (new_reph_pos < end)
912 goto reph_move;
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200913 }
914
915 /* 5. If no consonant is found in steps 3 or 4, move reph to a position
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200916 * immediately before the first post-base matra, syllable modifier
917 * sign or vedic sign that has a reordering class after the intended
918 * reph position. For example, if the reordering position for reph
919 * is post-main, it will skip above-base matras that also have a
920 * post-main position.
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200921 */
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200922 reph_step_5:
923 {
Behdad Esfahbodd0e68db2012-07-20 11:25:41 -0400924 /* Copied from step 2. */
925 new_reph_pos = start + 1;
926 while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos]))
927 new_reph_pos++;
928
929 if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos])) {
930 /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
931 if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
932 new_reph_pos++;
933 goto reph_move;
934 }
Behdad Esfahbod8df56362012-05-10 15:41:04 +0200935 }
936
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200937 /* 6. Otherwise, reorder reph to the end of the syllable.
938 */
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200939 {
940 new_reph_pos = end - 1;
941 while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD)
942 new_reph_pos--;
943
Behdad Esfahbod892eb782012-05-11 16:54:40 +0200944 /*
945 * If the Reph is to be ending up after a Matra,Halant sequence,
946 * position it before that Halant so it can interact with the Matra.
947 * However, if it's a plain Consonant,Halant we shouldn't do that.
948 * Uniscribe doesn't do this.
949 * TEST: U+0930,U+094D,U+0915,U+094B,U+094D
950 */
Behdad Esfahboda2b471d2012-06-05 15:17:44 -0400951 if (!indic_options ().uniscribe_bug_compatible &&
Behdad Esfahboddeb521d2012-07-17 11:37:32 -0400952 unlikely (is_halant_or_coeng (info[new_reph_pos]))) {
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200953 for (unsigned int i = base + 1; i < new_reph_pos; i++)
954 if (info[i].indic_category() == OT_M) {
955 /* Ok, got it. */
956 new_reph_pos--;
957 }
958 }
959 goto reph_move;
960 }
961
962 reph_move:
963 {
Behdad Esfahbode6b01a82012-07-23 00:11:26 -0400964 /* Yay, one big cluster! Merge before moving. */
965 buffer->merge_clusters (start, end);
966
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200967 /* Move */
968 hb_glyph_info_t reph = info[start];
969 memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0]));
970 info[new_reph_pos] = reph;
Behdad Esfahbod02b29222012-05-10 21:44:50 +0200971 }
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200972 }
973
974
975 /* o Reorder pre-base reordering consonants:
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400976 *
977 * If a pre-base reordering consonant is found, reorder it according to
978 * the following rules:
Behdad Esfahbode7be0572011-07-31 15:18:57 -0400979 */
980
Behdad Esfahbod46e645e2012-07-16 15:30:05 -0400981 if (pref_mask && base + 1 < end) /* Otherwise there can't be any pre-base reordering Ra. */
982 {
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -0400983 for (unsigned int i = base + 1; i < end; i++)
984 if ((info[i].mask & pref_mask) != 0)
Behdad Esfahbod78818122012-07-16 15:49:08 -0400985 {
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -0400986 /* 1. Only reorder a glyph produced by substitution during application
987 * of the <pref> feature. (Note that a font may shape a Ra consonant with
988 * the feature generally but block it in certain contexts.)
989 */
990 if (i + 1 == end || (info[i + 1].mask & pref_mask) == 0)
991 {
992 /*
993 * 2. Try to find a target position the same way as for pre-base matra.
994 * If it is found, reorder pre-base consonant glyph.
995 *
996 * 3. If position is not found, reorder immediately before main
997 * consonant.
998 */
999
1000 unsigned int new_pos = base;
Behdad Esfahbod0afb84c2012-07-24 01:44:47 -04001001 while (new_pos > start &&
1002 !(is_one_of (info[new_pos - 1], FLAG(OT_M) | HALANT_OR_COENG_FLAGS)))
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001003 new_pos--;
1004
Behdad Esfahbodd90b8e82012-07-24 02:10:20 -04001005 /* In Khmer coeng model, a V,Ra can go *after* matras. If it goes after a
1006 * split matra, it should be reordered to *before* the left part of such matra. */
1007 if (new_pos > start && info[new_pos - 1].indic_category() == OT_M)
1008 {
1009 unsigned int old_pos = i;
1010 for (unsigned int i = base + 1; i < old_pos; i++)
1011 if (info[i].indic_category() == OT_M)
1012 {
1013 new_pos--;
1014 break;
1015 }
1016 }
1017
Behdad Esfahboddeb521d2012-07-17 11:37:32 -04001018 if (new_pos > start && is_halant_or_coeng (info[new_pos - 1]))
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001019 /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
1020 if (new_pos < end && is_joiner (info[new_pos]))
1021 new_pos++;
1022
1023 {
1024 unsigned int old_pos = i;
Behdad Esfahbode6b01a82012-07-23 00:11:26 -04001025 buffer->merge_clusters (new_pos, old_pos + 1);
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001026 hb_glyph_info_t tmp = info[old_pos];
1027 memmove (&info[new_pos + 1], &info[new_pos], (old_pos - new_pos) * sizeof (info[0]));
1028 info[new_pos] = tmp;
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001029 }
1030 }
1031
1032 break;
Behdad Esfahbod78818122012-07-16 15:49:08 -04001033 }
Behdad Esfahbod46e645e2012-07-16 15:30:05 -04001034 }
Behdad Esfahbodeed903b2012-05-11 20:50:53 +02001035
1036
Behdad Esfahboda913b022012-05-11 20:59:26 +02001037 /* Apply 'init' to the Left Matra if it's a word start. */
Behdad Esfahbod6a091df2012-05-11 21:42:27 +02001038 if (info[start].indic_position () == POS_PRE_M &&
Behdad Esfahboda913b022012-05-11 20:59:26 +02001039 (!start ||
Behdad Esfahbodeace47b2012-05-13 15:54:43 +02001040 !(FLAG (_hb_glyph_info_get_general_category (&info[start - 1])) &
Behdad Esfahbod2c372b82012-07-20 13:37:48 -04001041 FLAG_RANGE (HB_UNICODE_GENERAL_CATEGORY_FORMAT, HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK))))
Behdad Esfahbod46e645e2012-07-16 15:30:05 -04001042 info[start].mask |= init_mask;
Behdad Esfahboda913b022012-05-11 20:59:26 +02001043
Behdad Esfahbodeed903b2012-05-11 20:50:53 +02001044
Behdad Esfahbod8ed248d2012-07-20 11:42:24 -04001045 /*
1046 * Finish off the clusters and go home!
1047 */
Behdad Esfahboddecf6ff2012-07-20 13:51:31 -04001048 if (indic_options ().uniscribe_bug_compatible)
Behdad Esfahbodebe29732012-05-11 16:43:12 +02001049 {
Behdad Esfahbod30c3d5e2012-07-20 13:56:32 -04001050 /* Uniscribe merges the entire cluster.
Behdad Esfahbod21d28032012-05-10 18:34:34 +02001051 * This means, half forms are submerged into the main consonants cluster.
1052 * This is unnecessary, and makes cursor positioning harder, but that's what
1053 * Uniscribe does. */
Behdad Esfahbode6b01a82012-07-23 00:11:26 -04001054 buffer->merge_clusters (start, end);
Behdad Esfahbod21d28032012-05-10 18:34:34 +02001055 }
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001056}
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001057
1058
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001059static void
1060final_reordering (const hb_ot_map_t *map,
Behdad Esfahbodafbcc242012-08-02 08:36:40 -04001061 hb_font_t *font HB_UNUSED,
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -04001062 hb_buffer_t *buffer)
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001063{
1064 unsigned int count = buffer->len;
1065 if (!count) return;
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001066
Behdad Esfahbod46e645e2012-07-16 15:30:05 -04001067 hb_mask_t init_mask = map->get_1_mask (HB_TAG('i','n','i','t'));
1068 hb_mask_t pref_mask = map->get_1_mask (HB_TAG('p','r','e','f'));
Behdad Esfahbodeed903b2012-05-11 20:50:53 +02001069
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001070 hb_glyph_info_t *info = buffer->info;
1071 unsigned int last = 0;
Behdad Esfahbodcee71872012-05-11 11:41:39 +02001072 unsigned int last_syllable = info[0].syllable();
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001073 for (unsigned int i = 1; i < count; i++)
Behdad Esfahbodcee71872012-05-11 11:41:39 +02001074 if (last_syllable != info[i].syllable()) {
Behdad Esfahbod46e645e2012-07-16 15:30:05 -04001075 final_reordering_syllable (buffer, init_mask, pref_mask, last, i);
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001076 last = i;
Behdad Esfahbodcee71872012-05-11 11:41:39 +02001077 last_syllable = info[last].syllable();
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001078 }
Behdad Esfahbod46e645e2012-07-16 15:30:05 -04001079 final_reordering_syllable (buffer, init_mask, pref_mask, last, count);
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001080
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001081 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
1082 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
1083}
1084
1085
Behdad Esfahbod693918e2012-07-30 21:08:51 -04001086const hb_ot_complex_shaper_t _hb_ot_complex_shaper_indic =
1087{
1088 "indic",
1089 collect_features_indic,
1090 override_features_indic,
1091 NULL, /* normalization_preference */
1092 setup_masks_indic,
Behdad Esfahbod1e7d8602012-07-31 23:41:06 -04001093 false, /* zero_width_attached_marks */
Behdad Esfahbod693918e2012-07-30 21:08:51 -04001094};