blob: e1f64d1cef31656c01f83e161e8d09fd1d70071d [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 Esfahbod49c5ec52012-07-23 20:14:13 -040028#include "hb-ot-layout-private.hh"
Behdad Esfahbod352372a2011-07-30 19:04:02 -040029
Behdad Esfahbod3a83d332013-02-12 12:14:10 -050030/* buffer var allocations */
31#define indic_category() complex_var_u8_0() /* indic_category_t */
32#define indic_position() complex_var_u8_1() /* indic_position_t */
33
34
35/*
36 * Indic shaper.
37 */
38
39
40#define IN_HALF_BLOCK(u, Base) (((u) & ~0x7F) == (Base))
41
42#define IS_DEVA(u) (IN_HALF_BLOCK (u, 0x0900))
43#define IS_BENG(u) (IN_HALF_BLOCK (u, 0x0980))
44#define IS_GURU(u) (IN_HALF_BLOCK (u, 0x0A00))
45#define IS_GUJR(u) (IN_HALF_BLOCK (u, 0x0A80))
46#define IS_ORYA(u) (IN_HALF_BLOCK (u, 0x0B00))
47#define IS_TAML(u) (IN_HALF_BLOCK (u, 0x0B80))
48#define IS_TELU(u) (IN_HALF_BLOCK (u, 0x0C00))
49#define IS_KNDA(u) (IN_HALF_BLOCK (u, 0x0C80))
50#define IS_MLYM(u) (IN_HALF_BLOCK (u, 0x0D00))
51#define IS_SINH(u) (IN_HALF_BLOCK (u, 0x0D80))
52#define IS_KHMR(u) (IN_HALF_BLOCK (u, 0x1780))
53
54
55#define MATRA_POS_LEFT(u) POS_PRE_M
56#define MATRA_POS_RIGHT(u) ( \
57 IS_DEVA(u) ? POS_AFTER_SUB : \
58 IS_BENG(u) ? POS_AFTER_POST : \
59 IS_GURU(u) ? POS_AFTER_POST : \
60 IS_GUJR(u) ? POS_AFTER_POST : \
61 IS_ORYA(u) ? POS_AFTER_POST : \
62 IS_TAML(u) ? POS_AFTER_POST : \
63 IS_TELU(u) ? (u <= 0x0C42 ? POS_BEFORE_SUB : POS_AFTER_SUB) : \
64 IS_KNDA(u) ? (u < 0x0CC3 || u > 0xCD6 ? POS_BEFORE_SUB : POS_AFTER_SUB) : \
65 IS_MLYM(u) ? POS_AFTER_POST : \
66 IS_SINH(u) ? POS_AFTER_SUB : \
67 IS_KHMR(u) ? POS_AFTER_POST : \
68 /*default*/ POS_AFTER_SUB \
69 )
70#define MATRA_POS_TOP(u) ( /* BENG and MLYM don't have top matras. */ \
71 IS_DEVA(u) ? POS_AFTER_SUB : \
72 IS_GURU(u) ? POS_AFTER_POST : /* Deviate from spec */ \
73 IS_GUJR(u) ? POS_AFTER_SUB : \
74 IS_ORYA(u) ? POS_AFTER_MAIN : \
75 IS_TAML(u) ? POS_AFTER_SUB : \
76 IS_TELU(u) ? POS_BEFORE_SUB : \
77 IS_KNDA(u) ? POS_BEFORE_SUB : \
78 IS_SINH(u) ? POS_AFTER_SUB : \
79 IS_KHMR(u) ? POS_AFTER_POST : \
80 /*default*/ POS_AFTER_SUB \
81 )
82#define MATRA_POS_BOTTOM(u) ( \
83 IS_DEVA(u) ? POS_AFTER_SUB : \
84 IS_BENG(u) ? POS_AFTER_SUB : \
85 IS_GURU(u) ? POS_AFTER_POST : \
86 IS_GUJR(u) ? POS_AFTER_POST : \
87 IS_ORYA(u) ? POS_AFTER_SUB : \
88 IS_TAML(u) ? POS_AFTER_POST : \
89 IS_TELU(u) ? POS_BEFORE_SUB : \
90 IS_KNDA(u) ? POS_BEFORE_SUB : \
91 IS_MLYM(u) ? POS_AFTER_POST : \
92 IS_SINH(u) ? POS_AFTER_SUB : \
93 IS_KHMR(u) ? POS_AFTER_POST : \
94 /*default*/ POS_AFTER_SUB \
95 )
96
97static inline indic_position_t
98matra_position (hb_codepoint_t u, indic_position_t side)
99{
100 switch ((int) side)
101 {
102 case POS_PRE_C: return MATRA_POS_LEFT (u);
103 case POS_POST_C: return MATRA_POS_RIGHT (u);
104 case POS_ABOVE_C: return MATRA_POS_TOP (u);
105 case POS_BELOW_C: return MATRA_POS_BOTTOM (u);
106 };
107 return side;
108}
109
110/* XXX
111 * This is a hack for now. We should move this data into the main Indic table.
112 * Or completely remove it and just check in the tables.
113 */
114static const hb_codepoint_t ra_chars[] = {
115 0x0930, /* Devanagari */
116 0x09B0, /* Bengali */
117 0x09F0, /* Bengali */
118 0x0A30, /* Gurmukhi */ /* No Reph */
119 0x0AB0, /* Gujarati */
120 0x0B30, /* Oriya */
121 0x0BB0, /* Tamil */ /* No Reph */
122 0x0C30, /* Telugu */ /* Reph formed only with ZWJ */
123 0x0CB0, /* Kannada */
124 0x0D30, /* Malayalam */ /* No Reph, Logical Repha */
125
126 0x0DBB, /* Sinhala */ /* Reph formed only with ZWJ */
127
128 0x179A, /* Khmer */ /* No Reph, Visual Repha */
129};
130
Behdad Esfahbod3a83d332013-02-12 12:14:10 -0500131static inline bool
132is_ra (hb_codepoint_t u)
133{
134 for (unsigned int i = 0; i < ARRAY_LENGTH (ra_chars); i++)
135 if (u == ra_chars[i])
136 return true;
137 return false;
138}
139
140static inline bool
141is_one_of (const hb_glyph_info_t &info, unsigned int flags)
142{
143 /* If it ligated, all bets are off. */
144 if (is_a_ligature (info)) return false;
145 return !!(FLAG (info.indic_category()) & flags);
146}
147
148#define JOINER_FLAGS (FLAG (OT_ZWJ) | FLAG (OT_ZWNJ))
149static inline bool
150is_joiner (const hb_glyph_info_t &info)
151{
152 return is_one_of (info, JOINER_FLAGS);
153}
154
155/* Note:
156 *
157 * We treat Vowels and placeholders as if they were consonants. This is safe because Vowels
158 * cannot happen in a consonant syllable. The plus side however is, we can call the
159 * consonant syllable logic from the vowel syllable function and get it all right! */
160#define CONSONANT_FLAGS (FLAG (OT_C) | FLAG (OT_CM) | FLAG (OT_Ra) | FLAG (OT_V) | FLAG (OT_NBSP) | FLAG (OT_DOTTEDCIRCLE))
161static inline bool
162is_consonant (const hb_glyph_info_t &info)
163{
164 return is_one_of (info, CONSONANT_FLAGS);
165}
166
167#define HALANT_OR_COENG_FLAGS (FLAG (OT_H) | FLAG (OT_Coeng))
168static inline bool
169is_halant_or_coeng (const hb_glyph_info_t &info)
170{
171 return is_one_of (info, HALANT_OR_COENG_FLAGS);
172}
173
174static inline void
175set_indic_properties (hb_glyph_info_t &info)
176{
177 hb_codepoint_t u = info.codepoint;
178 unsigned int type = hb_indic_get_categories (u);
179 indic_category_t cat = (indic_category_t) (type & 0x7F);
180 indic_position_t pos = (indic_position_t) (type >> 8);
181
182
183 /*
184 * Re-assign category
185 */
186
187
188 /* The spec says U+0952 is OT_A. However, testing shows that Uniscribe
Behdad Esfahbod3756efa2013-10-16 19:06:29 +0200189 * treats U+0951..U+0954 all behave similarly.
Behdad Esfahbod3a83d332013-02-12 12:14:10 -0500190 * TESTS:
191 * U+092E,U+0947,U+0952
192 * U+092E,U+0952,U+0947
193 * U+092E,U+0947,U+0951
194 * U+092E,U+0951,U+0947
Behdad Esfahbod3756efa2013-10-16 19:06:29 +0200195 */
Behdad Esfahbod3a83d332013-02-12 12:14:10 -0500196 if (unlikely (hb_in_range<hb_codepoint_t> (u, 0x0951, 0x0954)))
Behdad Esfahbod3756efa2013-10-16 19:06:29 +0200197 cat = OT_A;
Behdad Esfahbod3a83d332013-02-12 12:14:10 -0500198
199 if (unlikely (u == 0x17D1))
200 cat = OT_X;
201 if (cat == OT_X &&
202 unlikely (hb_in_range<hb_codepoint_t> (u, 0x17CB, 0x17D3))) /* Khmer Various signs */
203 {
204 /* These are like Top Matras. */
205 cat = OT_M;
206 pos = POS_ABOVE_C;
207 }
208 if (u == 0x17C6) /* Khmer Bindu doesn't like to be repositioned. */
209 cat = OT_N;
210
211 if (unlikely (u == 0x17D2)) cat = OT_Coeng; /* Khmer coeng */
212 else if (unlikely (u == 0x200C)) cat = OT_ZWNJ;
213 else if (unlikely (u == 0x200D)) cat = OT_ZWJ;
214 else if (unlikely (u == 0x25CC)) cat = OT_DOTTEDCIRCLE;
Behdad Esfahbod3756efa2013-10-16 19:06:29 +0200215 else if (unlikely (u == 0x0A71)) cat = OT_SM; /* GURMUKHI ADDAK. Move it to the end. */
Behdad Esfahbod3a83d332013-02-12 12:14:10 -0500216
217 if (cat == OT_Repha) {
218 /* There are two kinds of characters marked as Repha:
219 * - The ones that are GenCat=Mn are already positioned visually, ie. after base. (eg. Khmer)
220 * - The ones that are GenCat=Lo is encoded logically, ie. beginning of syllable. (eg. Malayalam)
221 *
222 * We recategorize the first kind to look like a Nukta and attached to the base directly.
223 */
224 if (_hb_glyph_info_get_general_category (&info) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
225 cat = OT_N;
226 }
227
228
229
230 /*
231 * Re-assign position.
232 */
233
234 if ((FLAG (cat) & CONSONANT_FLAGS))
235 {
Behdad Esfahbodc4e71ff2013-10-17 17:04:47 +0200236 pos = POS_BASE_C;
Behdad Esfahbod3a83d332013-02-12 12:14:10 -0500237 if (is_ra (u))
238 cat = OT_Ra;
239 }
240 else if (cat == OT_M)
241 {
242 pos = matra_position (u, pos);
243 }
Behdad Esfahbod3756efa2013-10-16 19:06:29 +0200244 else if ((FLAG (cat) & (FLAG (OT_SM) | FLAG (OT_VD) | FLAG (OT_A) | FLAG (OT_Avag))))
Behdad Esfahbod3a83d332013-02-12 12:14:10 -0500245 {
246 pos = POS_SMVD;
247 }
248
249 if (unlikely (u == 0x0B01)) pos = POS_BEFORE_SUB; /* Oriya Bindu is BeforeSub in the spec. */
250
251
252
253 info.indic_category() = cat;
254 info.indic_position() = pos;
255}
256
257/*
258 * Things above this line should ideally be moved to the Indic table itself.
259 */
260
Behdad Esfahbod1d002042012-08-02 05:01:11 -0400261
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400262/*
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400263 * Indic configurations. Note that we do not want to keep every single script-specific
264 * behavior in these tables necessarily. This should mainly be used for per-script
265 * properties that are cheaper keeping here, than in the code. Ie. if, say, one and
266 * only one script has an exception, that one script can be if'ed directly in the code,
267 * instead of adding a new flag in these structs.
268 */
269
270enum base_position_t {
271 BASE_POS_FIRST,
Behdad Esfahbode10453e2013-10-17 16:49:06 +0200272 BASE_POS_LAST_SINHALA,
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400273 BASE_POS_LAST
274};
275enum reph_position_t {
Behdad Esfahbodefed40b2013-10-17 18:50:11 +0200276 REPH_POS_DONT_CARE = POS_RA_TO_BECOME_REPH,
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400277 REPH_POS_AFTER_MAIN = POS_AFTER_MAIN,
278 REPH_POS_BEFORE_SUB = POS_BEFORE_SUB,
279 REPH_POS_AFTER_SUB = POS_AFTER_SUB,
280 REPH_POS_BEFORE_POST = POS_BEFORE_POST,
281 REPH_POS_AFTER_POST = POS_AFTER_POST
282};
283enum reph_mode_t {
284 REPH_MODE_IMPLICIT, /* Reph formed out of initial Ra,H sequence. */
285 REPH_MODE_EXPLICIT, /* Reph formed out of initial Ra,H,ZWJ sequence. */
286 REPH_MODE_VIS_REPHA, /* Encoded Repha character, no reordering needed. */
287 REPH_MODE_LOG_REPHA /* Encoded Repha character, needs reordering. */
288};
Behdad Esfahbod8acbb6b2013-10-15 12:15:49 +0200289enum blwf_mode_t {
290 BLWF_MODE_PRE_AND_POST, /* Below-forms feature applied to pre-base and post-base. */
291 BLWF_MODE_POST_ONLY /* Below-forms feature applied to post-base only. */
292};
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400293struct indic_config_t
294{
295 hb_script_t script;
296 bool has_old_spec;
297 hb_codepoint_t virama;
298 base_position_t base_pos;
299 reph_position_t reph_pos;
300 reph_mode_t reph_mode;
Behdad Esfahbod8acbb6b2013-10-15 12:15:49 +0200301 blwf_mode_t blwf_mode;
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400302};
303
304static const indic_config_t indic_configs[] =
305{
306 /* Default. Should be first. */
Behdad Esfahbodefed40b2013-10-17 18:50:11 +0200307 {HB_SCRIPT_INVALID, false, 0,BASE_POS_LAST, REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
Behdad Esfahbod8acbb6b2013-10-15 12:15:49 +0200308 {HB_SCRIPT_DEVANAGARI,true, 0x094D,BASE_POS_LAST, REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
309 {HB_SCRIPT_BENGALI, true, 0x09CD,BASE_POS_LAST, REPH_POS_AFTER_SUB, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
310 {HB_SCRIPT_GURMUKHI, true, 0x0A4D,BASE_POS_LAST, REPH_POS_BEFORE_SUB, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
311 {HB_SCRIPT_GUJARATI, true, 0x0ACD,BASE_POS_LAST, REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
312 {HB_SCRIPT_ORIYA, true, 0x0B4D,BASE_POS_LAST, REPH_POS_AFTER_MAIN, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
313 {HB_SCRIPT_TAMIL, true, 0x0BCD,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
314 {HB_SCRIPT_TELUGU, true, 0x0C4D,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_EXPLICIT, BLWF_MODE_POST_ONLY},
315 {HB_SCRIPT_KANNADA, true, 0x0CCD,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_IMPLICIT, BLWF_MODE_POST_ONLY},
316 {HB_SCRIPT_MALAYALAM, true, 0x0D4D,BASE_POS_LAST, REPH_POS_AFTER_MAIN, REPH_MODE_LOG_REPHA,BLWF_MODE_PRE_AND_POST},
Behdad Esfahbode10453e2013-10-17 16:49:06 +0200317 {HB_SCRIPT_SINHALA, false,0x0DCA,BASE_POS_LAST_SINHALA,
318 REPH_POS_AFTER_MAIN, REPH_MODE_EXPLICIT, BLWF_MODE_PRE_AND_POST},
Behdad Esfahbodefed40b2013-10-17 18:50:11 +0200319 {HB_SCRIPT_KHMER, false,0x17D2,BASE_POS_FIRST,REPH_POS_DONT_CARE, REPH_MODE_VIS_REPHA,BLWF_MODE_PRE_AND_POST},
320 {HB_SCRIPT_JAVANESE, false,0xA9C0,BASE_POS_LAST, REPH_POS_DONT_CARE, REPH_MODE_VIS_REPHA,BLWF_MODE_PRE_AND_POST},
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400321};
322
323
324
325/*
326 * Indic shaper.
327 */
Behdad Esfahbod9ccc6382012-07-19 12:32:16 -0400328
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200329struct feature_list_t {
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400330 hb_tag_t tag;
Behdad Esfahbodec544862013-02-14 11:25:10 -0500331 hb_ot_map_feature_flags_t flags;
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200332};
333
334static const feature_list_t
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400335indic_features[] =
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400336{
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400337 /*
338 * Basic features.
339 * These features are applied in order, one at a time, after initial_reordering.
340 */
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400341 {HB_TAG('n','u','k','t'), F_GLOBAL},
342 {HB_TAG('a','k','h','n'), F_GLOBAL},
343 {HB_TAG('r','p','h','f'), F_NONE},
344 {HB_TAG('r','k','r','f'), F_GLOBAL},
345 {HB_TAG('p','r','e','f'), F_NONE},
346 {HB_TAG('b','l','w','f'), F_NONE},
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400347 {HB_TAG('a','b','v','f'), F_NONE},
Behdad Esfahboda01cbf62013-10-15 16:37:53 +0200348 {HB_TAG('h','a','l','f'), F_NONE},
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400349 {HB_TAG('p','s','t','f'), F_NONE},
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400350 {HB_TAG('v','a','t','u'), F_GLOBAL},
351 {HB_TAG('c','j','c','t'), F_GLOBAL},
Behdad Esfahboda01cbf62013-10-15 16:37:53 +0200352 {HB_TAG('c','f','a','r'), F_NONE},
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400353 /*
354 * Other features.
355 * These features are applied all at once, after final_reordering.
Behdad Esfahboda01cbf62013-10-15 16:37:53 +0200356 * Default Bengali font in Windows for example has intermixed
357 * lookups for init,pres,abvs,blws features.
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400358 */
Behdad Esfahbodec544862013-02-14 11:25:10 -0500359 {HB_TAG('i','n','i','t'), F_NONE},
360 {HB_TAG('p','r','e','s'), F_GLOBAL},
361 {HB_TAG('a','b','v','s'), F_GLOBAL},
362 {HB_TAG('b','l','w','s'), F_GLOBAL},
363 {HB_TAG('p','s','t','s'), F_GLOBAL},
364 {HB_TAG('h','a','l','n'), F_GLOBAL},
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400365 /* Positioning features, though we don't care about the types. */
Behdad Esfahbodec544862013-02-14 11:25:10 -0500366 {HB_TAG('d','i','s','t'), F_GLOBAL},
367 {HB_TAG('a','b','v','m'), F_GLOBAL},
368 {HB_TAG('b','l','w','m'), F_GLOBAL},
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400369};
370
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400371/*
372 * Must be in the same order as the indic_features array.
373 */
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400374enum {
375 _NUKT,
Behdad Esfahbode0475342012-07-19 20:24:14 -0400376 _AKHN,
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400377 RPHF,
Behdad Esfahboddf6d45c2012-05-09 11:38:31 +0200378 _RKRF,
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400379 PREF,
Behdad Esfahbod167b6252012-08-05 21:16:26 -0700380 BLWF,
Behdad Esfahbod29f106d2012-07-16 12:05:35 -0400381 ABVF,
Behdad Esfahboda01cbf62013-10-15 16:37:53 +0200382 HALF,
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400383 PSTF,
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400384 _VATU,
Behdad Esfahbod70d65652013-02-12 18:01:21 -0500385 _CJCT,
Behdad Esfahboda01cbf62013-10-15 16:37:53 +0200386 CFAR,
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400387
388 INIT,
389 _PRES,
390 _ABVS,
391 _BLWS,
392 _PSTS,
393 _HALN,
394 _DIST,
395 _ABVM,
396 _BLWM,
397
398 INDIC_NUM_FEATURES,
399 INDIC_BASIC_FEATURES = INIT /* Don't forget to update this! */
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400400};
401
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400402static void
Behdad Esfahbod166b5cf2012-09-07 14:55:07 -0400403setup_syllables (const hb_ot_shape_plan_t *plan,
404 hb_font_t *font,
405 hb_buffer_t *buffer);
406static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400407initial_reordering (const hb_ot_shape_plan_t *plan,
Behdad Esfahbodafbcc242012-08-02 08:36:40 -0400408 hb_font_t *font,
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400409 hb_buffer_t *buffer);
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400410static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400411final_reordering (const hb_ot_shape_plan_t *plan,
Behdad Esfahbodafbcc242012-08-02 08:36:40 -0400412 hb_font_t *font,
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400413 hb_buffer_t *buffer);
Behdad Esfahbod30145272013-10-15 13:47:27 +0200414static void
415clear_syllables (const hb_ot_shape_plan_t *plan,
416 hb_font_t *font,
417 hb_buffer_t *buffer);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400418
Behdad Esfahbod693918e2012-07-30 21:08:51 -0400419static void
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400420collect_features_indic (hb_ot_shape_planner_t *plan)
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400421{
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400422 hb_ot_map_builder_t *map = &plan->map;
423
Behdad Esfahbod166b5cf2012-09-07 14:55:07 -0400424 /* Do this before any lookups have been applied. */
425 map->add_gsub_pause (setup_syllables);
426
Behdad Esfahbode7ffcfa2013-02-14 11:05:56 -0500427 map->add_global_bool_feature (HB_TAG('l','o','c','l'));
Behdad Esfahboda54a5502011-07-20 16:42:10 -0400428 /* The Indic specs do not require ccmp, but we apply it here since if
429 * there is a use of it, it's typically at the beginning. */
Behdad Esfahbode7ffcfa2013-02-14 11:05:56 -0500430 map->add_global_bool_feature (HB_TAG('c','c','m','p'));
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400431
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400432
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400433 unsigned int i = 0;
434 map->add_gsub_pause (initial_reordering);
435 for (; i < INDIC_BASIC_FEATURES; i++) {
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400436 map->add_feature (indic_features[i].tag, 1, indic_features[i].flags | F_MANUAL_ZWJ);
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400437 map->add_gsub_pause (NULL);
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200438 }
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400439 map->add_gsub_pause (final_reordering);
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400440 for (; i < INDIC_NUM_FEATURES; i++) {
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400441 map->add_feature (indic_features[i].tag, 1, indic_features[i].flags | F_MANUAL_ZWJ);
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400442 }
Behdad Esfahbod42d0f552013-10-17 13:05:05 +0200443
444 map->add_global_bool_feature (HB_TAG('c','a','l','t'));
445 map->add_global_bool_feature (HB_TAG('c','l','i','g'));
446
Behdad Esfahbod30145272013-10-15 13:47:27 +0200447 map->add_gsub_pause (clear_syllables);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400448}
449
Behdad Esfahbod693918e2012-07-30 21:08:51 -0400450static void
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400451override_features_indic (hb_ot_shape_planner_t *plan)
Behdad Esfahbodd96838e2012-07-16 20:26:57 -0400452{
Behdad Esfahbodeb102332013-10-15 15:26:44 +0200453 /* Uniscribe does not apply 'kern' in Khmer. */
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500454 if (hb_options ().uniscribe_bug_compatible)
Behdad Esfahbodeb102332013-10-15 15:26:44 +0200455 {
456 switch ((hb_tag_t) plan->props.script)
457 {
458 case HB_SCRIPT_KHMER:
Behdad Esfahbod9ac6b012013-10-17 16:27:38 +0200459 plan->map.add_feature (HB_TAG('k','e','r','n'), 0, F_GLOBAL);
Behdad Esfahbodeb102332013-10-15 15:26:44 +0200460 break;
461 }
462 }
Behdad Esfahbod6b389dd2012-11-12 11:02:56 -0800463
Behdad Esfahbodec544862013-02-14 11:25:10 -0500464 plan->map.add_feature (HB_TAG('l','i','g','a'), 0, F_GLOBAL);
Behdad Esfahbodd96838e2012-07-16 20:26:57 -0400465}
466
Behdad Esfahbod867361c2011-06-17 18:35:46 -0400467
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400468struct would_substitute_feature_t
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400469{
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +0200470 inline void init (const hb_ot_map_t *map, hb_tag_t feature_tag, bool zero_context_)
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400471 {
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +0200472 zero_context = zero_context_;
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400473 map->get_stage_lookups (0/*GSUB*/,
474 map->get_feature_stage (0/*GSUB*/, feature_tag),
475 &lookups, &count);
476 }
477
Behdad Esfahbod81449362013-03-05 20:08:59 -0500478 inline bool would_substitute (const hb_codepoint_t *glyphs,
479 unsigned int glyphs_count,
Behdad Esfahbod81449362013-03-05 20:08:59 -0500480 hb_face_t *face) const
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400481 {
482 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod362a9902012-11-15 14:57:31 -0800483 if (hb_ot_layout_lookup_would_substitute_fast (face, lookups[i].index, glyphs, glyphs_count, zero_context))
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400484 return true;
485 return false;
486 }
487
488 private:
489 const hb_ot_map_t::lookup_map_t *lookups;
490 unsigned int count;
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +0200491 bool zero_context;
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400492};
493
494struct indic_shape_plan_t
495{
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400496 ASSERT_POD ();
Behdad Esfahbod914ffaa2012-08-02 11:03:39 -0400497
498 inline bool get_virama_glyph (hb_font_t *font, hb_codepoint_t *pglyph) const
499 {
500 hb_codepoint_t glyph = virama_glyph;
501 if (unlikely (virama_glyph == (hb_codepoint_t) -1))
502 {
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400503 if (!config->virama || !font->get_glyph (config->virama, 0, &glyph))
Behdad Esfahbod914ffaa2012-08-02 11:03:39 -0400504 glyph = 0;
505 /* Technically speaking, the spec says we should apply 'locl' to virama too.
506 * Maybe one day... */
507
508 /* Our get_glyph() function needs a font, so we can't get the virama glyph
509 * during shape planning... Instead, overwrite it here. It's safe. Don't worry! */
510 (const_cast<indic_shape_plan_t *> (this))->virama_glyph = glyph;
511 }
512
513 *pglyph = glyph;
514 return glyph != 0;
515 }
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400516
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400517 const indic_config_t *config;
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400518
519 bool is_old_spec;
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400520 hb_codepoint_t virama_glyph;
521
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -0800522 would_substitute_feature_t rphf;
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400523 would_substitute_feature_t pref;
524 would_substitute_feature_t blwf;
525 would_substitute_feature_t pstf;
526
527 hb_mask_t mask_array[INDIC_NUM_FEATURES];
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400528};
529
530static void *
531data_create_indic (const hb_ot_shape_plan_t *plan)
532{
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400533 indic_shape_plan_t *indic_plan = (indic_shape_plan_t *) calloc (1, sizeof (indic_shape_plan_t));
534 if (unlikely (!indic_plan))
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400535 return NULL;
536
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400537 indic_plan->config = &indic_configs[0];
538 for (unsigned int i = 1; i < ARRAY_LENGTH (indic_configs); i++)
539 if (plan->props.script == indic_configs[i].script) {
540 indic_plan->config = &indic_configs[i];
541 break;
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400542 }
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400543
Behdad Esfahbod851784f2012-11-14 16:24:05 -0800544 indic_plan->is_old_spec = indic_plan->config->has_old_spec && ((plan->map.chosen_script[0] & 0x000000FF) != '2');
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400545 indic_plan->virama_glyph = (hb_codepoint_t) -1;
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400546
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +0200547 /* Use zero-context would_substitute() matching for new-spec of the main
548 * Indic scripts, but not for old-spec or scripts with one spec only. */
549 bool zero_context = indic_plan->config->has_old_spec || !indic_plan->is_old_spec;
550 indic_plan->rphf.init (&plan->map, HB_TAG('r','p','h','f'), zero_context);
551 indic_plan->pref.init (&plan->map, HB_TAG('p','r','e','f'), zero_context);
552 indic_plan->blwf.init (&plan->map, HB_TAG('b','l','w','f'), zero_context);
553 indic_plan->pstf.init (&plan->map, HB_TAG('p','s','t','f'), zero_context);
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400554
555 for (unsigned int i = 0; i < ARRAY_LENGTH (indic_plan->mask_array); i++)
Behdad Esfahbodec544862013-02-14 11:25:10 -0500556 indic_plan->mask_array[i] = (indic_features[i].flags & F_GLOBAL) ?
557 0 : plan->map.get_1_mask (indic_features[i].tag);
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400558
559 return indic_plan;
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400560}
561
562static void
563data_destroy_indic (void *data)
564{
565 free (data);
566}
567
568static indic_position_t
569consonant_position_from_face (const indic_shape_plan_t *indic_plan,
Behdad Esfahbod684fe592013-10-17 18:30:06 +0200570 const hb_codepoint_t consonant,
571 const hb_codepoint_t virama,
Behdad Esfahbod81449362013-03-05 20:08:59 -0500572 hb_face_t *face)
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400573{
Behdad Esfahbod81449362013-03-05 20:08:59 -0500574 /* For old-spec, the order of glyphs is Consonant,Virama,
575 * whereas for new-spec, it's Virama,Consonant. However,
576 * some broken fonts (like Free Sans) simply copied lookups
577 * from old-spec to new-spec without modification.
578 * And oddly enough, Uniscribe seems to respect those lookups.
579 * Eg. in the sequence U+0924,U+094D,U+0930, Uniscribe finds
580 * base at 0. The font however, only has lookups matching
581 * 930,94D in 'blwf', not the expected 94D,930 (with new-spec
582 * table). As such, we simply match both sequences. Seems
583 * to work. */
Behdad Esfahbod684fe592013-10-17 18:30:06 +0200584 hb_codepoint_t glyphs[3] = {virama, consonant, virama};
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +0200585 if (indic_plan->blwf.would_substitute (glyphs , 2, face) ||
Behdad Esfahbod684fe592013-10-17 18:30:06 +0200586 indic_plan->blwf.would_substitute (glyphs+1, 2, face))
Behdad Esfahbod81449362013-03-05 20:08:59 -0500587 return POS_BELOW_C;
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +0200588 if (indic_plan->pstf.would_substitute (glyphs , 2, face) ||
Behdad Esfahbod684fe592013-10-17 18:30:06 +0200589 indic_plan->pstf.would_substitute (glyphs+1, 2, face))
Behdad Esfahbod81449362013-03-05 20:08:59 -0500590 return POS_POST_C;
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +0200591 if (indic_plan->pref.would_substitute (glyphs , 2, face) ||
Behdad Esfahbod684fe592013-10-17 18:30:06 +0200592 indic_plan->pref.would_substitute (glyphs+1, 2, face))
Behdad Esfahbodae9a5832013-10-17 12:24:55 +0200593 return POS_POST_C;
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400594 return POS_BASE_C;
595}
596
597
Behdad Esfahbod166b5cf2012-09-07 14:55:07 -0400598enum syllable_type_t {
599 consonant_syllable,
600 vowel_syllable,
601 standalone_cluster,
Behdad Esfahbod3c7b3642013-10-15 11:21:01 +0200602 avagraha_cluster,
Behdad Esfahbod166b5cf2012-09-07 14:55:07 -0400603 broken_cluster,
604 non_indic_cluster,
605};
606
607#include "hb-ot-shape-complex-indic-machine.hh"
608
609
Behdad Esfahbod693918e2012-07-30 21:08:51 -0400610static void
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400611setup_masks_indic (const hb_ot_shape_plan_t *plan HB_UNUSED,
612 hb_buffer_t *buffer,
613 hb_font_t *font HB_UNUSED)
Behdad Esfahbod24eacf12012-08-02 08:42:11 -0400614{
615 HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
616 HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
617
618 /* We cannot setup masks here. We save information about characters
619 * and setup masks later on in a pause-callback. */
620
621 unsigned int count = buffer->len;
622 for (unsigned int i = 0; i < count; i++)
623 set_indic_properties (buffer->info[i]);
624}
625
Behdad Esfahbod166b5cf2012-09-07 14:55:07 -0400626static void
627setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED,
628 hb_font_t *font HB_UNUSED,
629 hb_buffer_t *buffer)
630{
631 find_syllables (buffer);
632}
633
Behdad Esfahbod24eacf12012-08-02 08:42:11 -0400634static int
635compare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
636{
637 int a = pa->indic_position();
638 int b = pb->indic_position();
639
640 return a < b ? -1 : a == b ? 0 : +1;
641}
642
643
644
645static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400646update_consonant_positions (const hb_ot_shape_plan_t *plan,
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400647 hb_font_t *font,
648 hb_buffer_t *buffer)
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400649{
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400650 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400651
Behdad Esfahbodc4e71ff2013-10-17 17:04:47 +0200652 if (indic_plan->config->base_pos != BASE_POS_LAST)
653 return;
654
Behdad Esfahbod684fe592013-10-17 18:30:06 +0200655 hb_codepoint_t virama;
656 if (indic_plan->get_virama_glyph (font, &virama))
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400657 {
658 hb_face_t *face = font->face;
659 unsigned int count = buffer->len;
660 for (unsigned int i = 0; i < count; i++)
661 if (buffer->info[i].indic_position() == POS_BASE_C) {
Behdad Esfahbod684fe592013-10-17 18:30:06 +0200662 hb_codepoint_t consonant = buffer->info[i].codepoint;
663 buffer->info[i].indic_position() = consonant_position_from_face (indic_plan, consonant, virama, face);
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400664 }
665 }
666}
667
Behdad Esfahbod867361c2011-06-17 18:35:46 -0400668
Behdad Esfahbod7ea58db2012-05-11 18:58:57 +0200669/* Rules from:
670 * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
671
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400672static void
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -0800673initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan,
674 hb_face_t *face,
675 hb_buffer_t *buffer,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200676 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400677{
Behdad Esfahbod914ffaa2012-08-02 11:03:39 -0400678 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
Behdad Esfahbodee58f3b2011-07-30 19:15:53 -0400679 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400680
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200681
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400682 /* 1. Find base consonant:
683 *
684 * The shaping engine finds the base consonant of the syllable, using the
685 * following algorithm: starting from the end of the syllable, move backwards
686 * until a consonant is found that does not have a below-base or post-base
687 * form (post-base forms have to follow below-base forms), or that is not a
688 * pre-base reordering Ra, or arrive at the first consonant. The consonant
689 * stopped at will be the base.
690 *
691 * o If the syllable starts with Ra + Halant (in a script that has Reph)
692 * and has more than one consonant, Ra is excluded from candidates for
693 * base consonants.
694 */
695
Behdad Esfahbod5e720712011-07-31 17:51:50 -0400696 unsigned int base = end;
Behdad Esfahbod76b34092012-05-09 11:43:43 +0200697 bool has_reph = false;
698
Behdad Esfahbod76b34092012-05-09 11:43:43 +0200699 {
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200700 /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
701 * and has more than one consonant, Ra is excluded from candidates for
702 * base consonants. */
703 unsigned int limit = start;
Behdad Esfahbodefed40b2013-10-17 18:50:11 +0200704 if (indic_plan->config->reph_pos != POS_RA_TO_BECOME_REPH &&
705 indic_plan->mask_array[RPHF] &&
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200706 start + 3 <= end &&
Behdad Esfahbod8b217f52012-12-21 15:48:32 -0500707 (
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400708 (indic_plan->config->reph_mode == REPH_MODE_IMPLICIT && !is_joiner (info[start + 2])) ||
709 (indic_plan->config->reph_mode == REPH_MODE_EXPLICIT && info[start + 2].indic_category() == OT_ZWJ)
Behdad Esfahbod3285e102012-07-18 17:22:14 -0400710 ))
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200711 {
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -0800712 /* See if it matches the 'rphf' feature. */
713 hb_codepoint_t glyphs[2] = {info[start].codepoint, info[start + 1].codepoint};
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +0200714 if (indic_plan->rphf.would_substitute (glyphs, ARRAY_LENGTH (glyphs), face))
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -0800715 {
716 limit += 2;
717 while (limit < end && is_joiner (info[limit]))
718 limit++;
719 base = start;
720 has_reph = true;
721 }
Behdad Esfahbod8b217f52012-12-21 15:48:32 -0500722 } else if (indic_plan->config->reph_mode == REPH_MODE_LOG_REPHA && info[start].indic_category() == OT_Repha)
723 {
724 limit += 1;
725 while (limit < end && is_joiner (info[limit]))
726 limit++;
727 base = start;
728 has_reph = true;
729 }
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400730
Behdad Esfahbod23b0e9d2012-08-26 14:30:18 -0400731 switch (indic_plan->config->base_pos)
Behdad Esfahbod14dbdd92012-07-18 13:13:03 -0400732 {
Behdad Esfahbodd469fad2012-11-14 15:07:36 -0800733 default:
734 assert (false);
735 /* fallthrough */
736
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400737 case BASE_POS_LAST:
738 {
739 /* -> starting from the end of the syllable, move backwards */
740 unsigned int i = end;
741 bool seen_below = false;
742 do {
743 i--;
744 /* -> until a consonant is found */
745 if (is_consonant (info[i]))
746 {
747 /* -> that does not have a below-base or post-base form
748 * (post-base forms have to follow below-base forms), */
749 if (info[i].indic_position() != POS_BELOW_C &&
750 (info[i].indic_position() != POS_POST_C || seen_below))
751 {
752 base = i;
753 break;
754 }
755 if (info[i].indic_position() == POS_BELOW_C)
756 seen_below = true;
757
758 /* -> or that is not a pre-base reordering Ra,
759 *
760 * IMPLEMENTATION NOTES:
761 *
Behdad Esfahbodfb7c1822013-03-06 00:53:24 -0500762 * Our pre-base reordering Ra's are marked POS_POST_C, so will be skipped
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400763 * by the logic above already.
764 */
765
766 /* -> or arrive at the first consonant. The consonant stopped at will
767 * be the base. */
768 base = i;
769 }
770 else
771 {
772 /* A ZWJ after a Halant stops the base search, and requests an explicit
773 * half form.
774 * A ZWJ before a Halant, requests a subjoined form instead, and hence
775 * search continues. This is particularly important for Bengali
Behdad Esfahbodc4be9912012-11-12 14:27:33 -0800776 * sequence Ra,H,Ya that should form Ya-Phalaa by subjoining Ya. */
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400777 if (start < i &&
778 info[i].indic_category() == OT_ZWJ &&
779 info[i - 1].indic_category() == OT_H)
780 break;
781 }
782 } while (i > limit);
783 }
784 break;
785
Behdad Esfahbode10453e2013-10-17 16:49:06 +0200786 case BASE_POS_LAST_SINHALA:
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400787 {
Behdad Esfahbodc4e71ff2013-10-17 17:04:47 +0200788 /* Sinhala base positioning is slightly different from main Indic, in that:
789 * 1. It's ZWJ behavior is different,
790 * 2. We don't need to look into the font for consonant positions.
791 */
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400792
793 if (!has_reph)
794 base = limit;
795
796 /* Find the last base consonant that is not blocked by ZWJ. If there is
797 * a ZWJ right before a base consonant, that would request a subjoined form. */
798 for (unsigned int i = limit; i < end; i++)
Behdad Esfahbodc4e71ff2013-10-17 17:04:47 +0200799 if (is_consonant (info[i]))
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400800 {
801 if (limit < i && info[i - 1].indic_category() == OT_ZWJ)
802 break;
803 else
804 base = i;
805 }
806
807 /* Mark all subsequent consonants as below. */
808 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbodc4e71ff2013-10-17 17:04:47 +0200809 if (is_consonant (info[i]))
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400810 info[i].indic_position() = POS_BELOW_C;
811 }
812 break;
Behdad Esfahbode10453e2013-10-17 16:49:06 +0200813
814 case BASE_POS_FIRST:
815 {
816 /* The first consonant is always the base. */
817
818 assert (indic_plan->config->reph_mode == REPH_MODE_VIS_REPHA);
Behdad Esfahbodefed40b2013-10-17 18:50:11 +0200819 assert (!has_reph);
Behdad Esfahbode10453e2013-10-17 16:49:06 +0200820
821 base = start;
822
823 /* Mark all subsequent consonants as below. */
824 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbodc4e71ff2013-10-17 17:04:47 +0200825 if (is_consonant (info[i]))
Behdad Esfahbode10453e2013-10-17 16:49:06 +0200826 info[i].indic_position() = POS_BELOW_C;
827 }
828 break;
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400829 }
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200830
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200831 /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
832 * and has more than one consonant, Ra is excluded from candidates for
Behdad Esfahbod2278eef2012-07-24 00:26:43 -0400833 * base consonants.
834 *
835 * Only do this for unforced Reph. (ie. not for Ra,H,ZWJ. */
Behdad Esfahbod9621e0b2013-02-11 06:58:27 -0500836 if (has_reph && base == start && limit - base <= 2) {
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200837 /* Have no other consonant, so Reph is not formed and Ra becomes base. */
838 has_reph = false;
839 }
Behdad Esfahbod5e4e21f2012-05-13 16:46:08 +0200840 }
Behdad Esfahbod2278eef2012-07-24 00:26:43 -0400841
Behdad Esfahbod3d250792012-05-10 11:37:42 +0200842
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400843 /* 2. Decompose and reorder Matras:
844 *
845 * Each matra and any syllable modifier sign in the cluster are moved to the
846 * appropriate position relative to the consonant(s) in the cluster. The
847 * shaping engine decomposes two- or three-part matras into their constituent
848 * parts before any repositioning. Matra characters are classified by which
849 * consonant in a conjunct they have affinity for and are reordered to the
850 * following positions:
851 *
852 * o Before first half form in the syllable
853 * o After subjoined consonants
854 * o After post-form consonant
855 * o After main consonant (for above marks)
856 *
857 * IMPLEMENTATION NOTES:
858 *
859 * The normalize() routine has already decomposed matras for us, so we don't
860 * need to worry about that.
861 */
862
863
864 /* 3. Reorder marks to canonical order:
865 *
866 * Adjacent nukta and halant or nukta and vedic sign are always repositioned
867 * if necessary, so that the nukta is first.
868 *
869 * IMPLEMENTATION NOTES:
870 *
871 * We don't need to do this: the normalize() routine already did this for us.
872 */
873
874
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400875 /* Reorder characters */
876
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200877 for (unsigned int i = start; i < base; i++)
Behdad Esfahbod900cf3d2012-07-20 10:18:23 -0400878 info[i].indic_position() = MIN (POS_PRE_C, (indic_position_t) info[i].indic_position());
Behdad Esfahbod55f70eb2012-07-17 12:50:13 -0400879
Behdad Esfahbod075d6712012-07-18 15:41:53 -0400880 if (base < end)
881 info[base].indic_position() = POS_BASE_C;
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400882
Behdad Esfahbod55f70eb2012-07-17 12:50:13 -0400883 /* Mark final consonants. A final consonant is one appearing after a matra,
884 * like in Khmer. */
885 for (unsigned int i = base + 1; i < end; i++)
886 if (info[i].indic_category() == OT_M) {
887 for (unsigned int j = i + 1; j < end; j++)
888 if (is_consonant (info[j])) {
889 info[j].indic_position() = POS_FINAL_C;
890 break;
891 }
892 break;
893 }
894
Behdad Esfahbodfd06bf52011-07-30 20:14:44 -0400895 /* Handle beginning Ra */
Behdad Esfahbod5e4e21f2012-05-13 16:46:08 +0200896 if (has_reph)
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200897 info[start].indic_position() = POS_RA_TO_BECOME_REPH;
Behdad Esfahbodfd06bf52011-07-30 20:14:44 -0400898
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400899 /* For old-style Indic script tags, move the first post-base Halant after
Behdad Esfahbodecd454b2013-01-08 18:09:46 -0600900 * last consonant. Only do this if there is *not* a Halant after last
901 * consonant. Otherwise it becomes messy. */
Behdad Esfahbod914ffaa2012-08-02 11:03:39 -0400902 if (indic_plan->is_old_spec) {
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200903 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400904 if (info[i].indic_category() == OT_H) {
905 unsigned int j;
906 for (j = end - 1; j > i; j--)
Behdad Esfahbodecd454b2013-01-08 18:09:46 -0600907 if (is_consonant (info[j]) || info[j].indic_category() == OT_H)
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400908 break;
Behdad Esfahbodecd454b2013-01-08 18:09:46 -0600909 if (info[j].indic_category() != OT_H && j > i) {
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400910 /* Move Halant to after last consonant. */
911 hb_glyph_info_t t = info[i];
912 memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
913 info[j] = t;
914 }
915 break;
916 }
917 }
918
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400919 /* Attach misc marks to previous char to move with them. */
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200920 {
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400921 indic_position_t last_pos = POS_START;
922 for (unsigned int i = start; i < end; i++)
923 {
924 if ((FLAG (info[i].indic_category()) & (JOINER_FLAGS | FLAG (OT_N) | FLAG (OT_RS) | HALANT_OR_COENG_FLAGS)))
925 {
926 info[i].indic_position() = last_pos;
Behdad Esfahboddde55062012-11-14 11:37:04 -0800927 if (unlikely (info[i].indic_category() == OT_H &&
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400928 info[i].indic_position() == POS_PRE_M))
929 {
930 /*
931 * Uniscribe doesn't move the Halant with Left Matra.
932 * TEST: U+092B,U+093F,U+094DE
Behdad Esfahboddde55062012-11-14 11:37:04 -0800933 * We follow. This is important for the Sinhala
934 * U+0DDA split matra since it decomposes to U+0DD9,U+0DCA
935 * where U+0DD9 is a left matra and U+0DCA is the virama.
936 * We don't want to move the virama with the left matra.
937 * TEST: U+0D9A,U+0DDA
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400938 */
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200939 for (unsigned int j = i; j > start; j--)
Behdad Esfahbod6a091df2012-05-11 21:42:27 +0200940 if (info[j - 1].indic_position() != POS_PRE_M) {
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200941 info[i].indic_position() = info[j - 1].indic_position();
942 break;
943 }
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400944 }
945 } else if (info[i].indic_position() != POS_SMVD) {
946 last_pos = (indic_position_t) info[i].indic_position();
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200947 }
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400948 }
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200949 }
Behdad Esfahbod74ccc6a2012-07-17 11:16:19 -0400950 /* Re-attach ZWJ, ZWNJ, and halant to next char, for after-base consonants. */
951 {
952 unsigned int last_halant = end;
953 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahboddeb521d2012-07-17 11:37:32 -0400954 if (is_halant_or_coeng (info[i]))
Behdad Esfahbod74ccc6a2012-07-17 11:16:19 -0400955 last_halant = i;
956 else if (is_consonant (info[i])) {
957 for (unsigned int j = last_halant; j < i; j++)
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400958 if (info[j].indic_position() != POS_SMVD)
959 info[j].indic_position() = info[i].indic_position();
Behdad Esfahbod3756efa2013-10-16 19:06:29 +0200960 last_halant = end;
Behdad Esfahbod74ccc6a2012-07-17 11:16:19 -0400961 }
962 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400963
Behdad Esfahbod28d5dae2013-10-16 12:32:12 +0200964
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200965 {
Behdad Esfahbod28d5dae2013-10-16 12:32:12 +0200966 /* Use syllable() for sort accounting temporarily. */
967 unsigned int syllable = info[start].syllable();
968 for (unsigned int i = start; i < end; i++)
969 info[i].syllable() = i - start;
970
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200971 /* Sit tight, rock 'n roll! */
Behdad Esfahbodd3637ed2012-05-10 10:51:38 +0200972 hb_bubble_sort (info + start, end - start, compare_indic_order);
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200973 /* Find base again */
974 base = end;
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200975 for (unsigned int i = start; i < end; i++)
Behdad Esfahbod28d5dae2013-10-16 12:32:12 +0200976 if (info[i].indic_position() == POS_BASE_C)
977 {
978 base = i;
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200979 break;
980 }
Behdad Esfahbod28d5dae2013-10-16 12:32:12 +0200981 /* Things are out-of-control for post base positions, they may shuffle
982 * around like crazy. In old-spec mode, we move halants around, so in
983 * that case merge all clusters after base. Otherwise, check the sort
984 * order and merge as needed.
985 * For pre-base stuff, we handle cluster issues in final reordering. */
986 if (indic_plan->is_old_spec || end - base > 127)
987 buffer->merge_clusters (base, end);
988 else
989 {
990 /* Note! syllable() is a one-byte field. */
991 for (unsigned int i = base; i < end; i++)
992 if (info[i].syllable() != 255)
993 {
994 unsigned int max = i;
995 unsigned int j = start + info[i].syllable();
996 while (j != i)
997 {
998 max = MAX (max, j);
999 unsigned int next = start + info[j].syllable();
1000 info[j].syllable() = 255; /* So we don't process j later again. */
1001 j = next;
1002 }
1003 if (i != max)
1004 buffer->merge_clusters (i, max + 1);
1005 }
1006 }
1007
1008 /* Put syllable back in. */
1009 for (unsigned int i = start; i < end; i++)
1010 info[i].syllable() = syllable;
Behdad Esfahboda391ff52012-05-10 11:31:20 +02001011 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -04001012
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001013 /* Setup masks now */
1014
Behdad Esfahbod28168392011-07-31 16:00:35 -04001015 {
1016 hb_mask_t mask;
1017
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001018 /* Reph */
Behdad Esfahbod668c6042012-05-11 15:34:13 +02001019 for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++)
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001020 info[i].mask |= indic_plan->mask_array[RPHF];
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001021
Behdad Esfahbod28168392011-07-31 16:00:35 -04001022 /* Pre-base */
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001023 mask = indic_plan->mask_array[HALF];
Behdad Esfahbodc7dacac2013-10-17 12:20:24 +02001024 if (!indic_plan->is_old_spec &&
1025 indic_plan->config->blwf_mode == BLWF_MODE_PRE_AND_POST)
Behdad Esfahbod8acbb6b2013-10-15 12:15:49 +02001026 mask |= indic_plan->mask_array[BLWF];
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +02001027 for (unsigned int i = start; i < base; i++)
Behdad Esfahbod28168392011-07-31 16:00:35 -04001028 info[i].mask |= mask;
1029 /* Base */
Behdad Esfahbod20b68e62012-07-20 10:47:46 -04001030 mask = 0;
Behdad Esfahbod075d6712012-07-18 15:41:53 -04001031 if (base < end)
1032 info[base].mask |= mask;
Behdad Esfahbod28168392011-07-31 16:00:35 -04001033 /* Post-base */
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001034 mask = indic_plan->mask_array[BLWF] | indic_plan->mask_array[ABVF] | indic_plan->mask_array[PSTF];
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +02001035 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbod28168392011-07-31 16:00:35 -04001036 info[i].mask |= mask;
1037 }
Behdad Esfahbod9da04872011-07-31 13:46:44 -04001038
Behdad Esfahbod85c51ec2013-02-12 18:17:39 -05001039 if (indic_plan->is_old_spec &&
1040 buffer->props.script == HB_SCRIPT_DEVANAGARI)
1041 {
1042 /* Old-spec eye-lash Ra needs special handling. From the
1043 * spec:
1044 *
1045 * "The feature 'below-base form' is applied to consonants
1046 * having below-base forms and following the base consonant.
1047 * The exception is vattu, which may appear below half forms
1048 * as well as below the base glyph. The feature 'below-base
1049 * form' will be applied to all such occurrences of Ra as well."
1050 *
1051 * Test case: U+0924,U+094D,U+0930,U+094d,U+0915
1052 * with Sanskrit 2003 font.
1053 *
1054 * However, note that Ra,Halant,ZWJ is the correct way to
1055 * request eyelash form of Ra, so we wouldbn't inhibit it
1056 * in that sequence.
1057 *
1058 * Test case: U+0924,U+094D,U+0930,U+094d,U+200D,U+0915
1059 */
1060 for (unsigned int i = start; i + 1 < base; i++)
1061 if (info[i ].indic_category() == OT_Ra &&
1062 info[i+1].indic_category() == OT_H &&
1063 (i + 2 == base ||
1064 info[i+2].indic_category() != OT_ZWJ))
1065 {
1066 info[i ].mask |= indic_plan->mask_array[BLWF];
1067 info[i+1].mask |= indic_plan->mask_array[BLWF];
1068 }
1069 }
1070
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001071 if (indic_plan->mask_array[PREF] && base + 2 < end)
Behdad Esfahbod17d7de92012-07-16 15:20:15 -04001072 {
Behdad Esfahbod771a8f52012-07-23 20:07:50 -04001073 /* Find a Halant,Ra sequence and mark it for pre-base reordering processing. */
Behdad Esfahbod56be6772012-11-12 14:09:40 -08001074 for (unsigned int i = base + 1; i + 1 < end; i++) {
1075 hb_codepoint_t glyphs[2] = {info[i].codepoint, info[i + 1].codepoint};
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +02001076 if (indic_plan->pref.would_substitute (glyphs, ARRAY_LENGTH (glyphs), face))
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001077 {
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001078 info[i++].mask |= indic_plan->mask_array[PREF];
1079 info[i++].mask |= indic_plan->mask_array[PREF];
Behdad Esfahbod0201e0a2012-07-17 13:55:10 -04001080
1081 /* Mark the subsequent stuff with 'cfar'. Used in Khmer.
1082 * Read the feature spec.
1083 * This allows distinguishing the following cases with MS Khmer fonts:
1084 * U+1784,U+17D2,U+179A,U+17D2,U+1782
1085 * U+1784,U+17D2,U+1782,U+17D2,U+179A
1086 */
1087 for (; i < end; i++)
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001088 info[i].mask |= indic_plan->mask_array[CFAR];
Behdad Esfahbod0201e0a2012-07-17 13:55:10 -04001089
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001090 break;
1091 }
Behdad Esfahbod56be6772012-11-12 14:09:40 -08001092 }
Behdad Esfahbod17d7de92012-07-16 15:20:15 -04001093 }
1094
Behdad Esfahbod9da04872011-07-31 13:46:44 -04001095 /* Apply ZWJ/ZWNJ effects */
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +02001096 for (unsigned int i = start + 1; i < end; i++)
Behdad Esfahbod9da04872011-07-31 13:46:44 -04001097 if (is_joiner (info[i])) {
1098 bool non_joiner = info[i].indic_category() == OT_ZWNJ;
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -04001099 unsigned int j = i;
Behdad Esfahbod9da04872011-07-31 13:46:44 -04001100
1101 do {
Behdad Esfahbod9da04872011-07-31 13:46:44 -04001102 j--;
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -04001103
Behdad Esfahbodcfc507c2013-02-14 10:40:12 -05001104 /* ZWJ/ZWNJ should disable CJCT. They do that by simply
1105 * being there, since we don't skip them for the CJCT
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -04001106 * feature (ie. F_MANUAL_ZWJ) */
Behdad Esfahbod20b68e62012-07-20 10:47:46 -04001107
1108 /* A ZWNJ disables HALF. */
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -04001109 if (non_joiner)
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001110 info[j].mask &= ~indic_plan->mask_array[HALF];
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -04001111
Behdad Esfahbod9da04872011-07-31 13:46:44 -04001112 } while (j > start && !is_consonant (info[j]));
1113 }
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001114}
1115
1116
1117static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -04001118initial_reordering_vowel_syllable (const hb_ot_shape_plan_t *plan,
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001119 hb_face_t *face,
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +02001120 hb_buffer_t *buffer,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001121 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001122{
Behdad Esfahbodc5306b62012-05-10 12:07:33 +02001123 /* We made the vowels look like consonants. So let's call the consonant logic! */
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001124 initial_reordering_consonant_syllable (plan, face, buffer, start, end);
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001125}
1126
1127static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -04001128initial_reordering_standalone_cluster (const hb_ot_shape_plan_t *plan,
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001129 hb_face_t *face,
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +02001130 hb_buffer_t *buffer,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001131 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001132{
Behdad Esfahbod18c06e12012-05-11 20:02:14 +02001133 /* We treat NBSP/dotted-circle as if they are consonants, so we should just chain.
1134 * Only if not in compatibility mode that is... */
1135
Behdad Esfahbodbab02d32013-02-12 15:26:45 -05001136 if (hb_options ().uniscribe_bug_compatible)
Behdad Esfahbod18c06e12012-05-11 20:02:14 +02001137 {
1138 /* For dotted-circle, this is what Uniscribe does:
1139 * If dotted-circle is the last glyph, it just does nothing.
1140 * Ie. It doesn't form Reph. */
1141 if (buffer->info[end - 1].indic_category() == OT_DOTTEDCIRCLE)
1142 return;
1143 }
1144
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001145 initial_reordering_consonant_syllable (plan, face, buffer, start, end);
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001146}
1147
1148static void
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001149initial_reordering_broken_cluster (const hb_ot_shape_plan_t *plan,
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001150 hb_face_t *face,
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001151 hb_buffer_t *buffer,
1152 unsigned int start, unsigned int end)
1153{
1154 /* We already inserted dotted-circles, so just call the standalone_cluster. */
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001155 initial_reordering_standalone_cluster (plan, face, buffer, start, end);
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001156}
1157
1158static void
Behdad Esfahbod3c7b3642013-10-15 11:21:01 +02001159initial_reordering_avagraha_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED,
1160 hb_face_t *face HB_UNUSED,
1161 hb_buffer_t *buffer HB_UNUSED,
1162 unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
1163{
1164 /* Nothing to do right now. If we ever switch to using the output
1165 * buffer in the reordering process, we'd need to next_glyph() here. */
1166}
1167
1168static void
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001169initial_reordering_non_indic_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED,
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001170 hb_face_t *face HB_UNUSED,
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001171 hb_buffer_t *buffer HB_UNUSED,
1172 unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001173{
1174 /* Nothing to do right now. If we ever switch to using the output
1175 * buffer in the reordering process, we'd need to next_glyph() here. */
1176}
1177
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001178
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001179static void
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001180initial_reordering_syllable (const hb_ot_shape_plan_t *plan,
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001181 hb_face_t *face,
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001182 hb_buffer_t *buffer,
1183 unsigned int start, unsigned int end)
1184{
1185 syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F);
1186 switch (syllable_type) {
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001187 case consonant_syllable: initial_reordering_consonant_syllable (plan, face, buffer, start, end); return;
1188 case vowel_syllable: initial_reordering_vowel_syllable (plan, face, buffer, start, end); return;
1189 case standalone_cluster: initial_reordering_standalone_cluster (plan, face, buffer, start, end); return;
Behdad Esfahbod3c7b3642013-10-15 11:21:01 +02001190 case avagraha_cluster: initial_reordering_avagraha_cluster (plan, face, buffer, start, end); return;
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001191 case broken_cluster: initial_reordering_broken_cluster (plan, face, buffer, start, end); return;
1192 case non_indic_cluster: initial_reordering_non_indic_cluster (plan, face, buffer, start, end); return;
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001193 }
1194}
1195
Behdad Esfahbod166b5cf2012-09-07 14:55:07 -04001196static inline void
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001197insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED,
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001198 hb_font_t *font,
1199 hb_buffer_t *buffer)
1200{
Behdad Esfahbod166b5cf2012-09-07 14:55:07 -04001201 /* Note: This loop is extra overhead, but should not be measurable. */
1202 bool has_broken_syllables = false;
1203 unsigned int count = buffer->len;
1204 for (unsigned int i = 0; i < count; i++)
1205 if ((buffer->info[i].syllable() & 0x0F) == broken_cluster) {
1206 has_broken_syllables = true;
1207 break;
1208 }
1209 if (likely (!has_broken_syllables))
1210 return;
1211
1212
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001213 hb_codepoint_t dottedcircle_glyph;
1214 if (!font->get_glyph (0x25CC, 0, &dottedcircle_glyph))
1215 return;
1216
Behdad Esfahbodf41dc2d2012-11-15 10:36:43 -08001217 hb_glyph_info_t dottedcircle = {0};
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001218 dottedcircle.codepoint = 0x25CC;
1219 set_indic_properties (dottedcircle);
1220 dottedcircle.codepoint = dottedcircle_glyph;
1221
1222 buffer->clear_output ();
1223
1224 buffer->idx = 0;
1225 unsigned int last_syllable = 0;
1226 while (buffer->idx < buffer->len)
1227 {
1228 unsigned int syllable = buffer->cur().syllable();
1229 syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F);
1230 if (unlikely (last_syllable != syllable && syllable_type == broken_cluster))
1231 {
Behdad Esfahbod596740d2012-12-21 19:41:04 -05001232 last_syllable = syllable;
1233
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001234 hb_glyph_info_t info = dottedcircle;
1235 info.cluster = buffer->cur().cluster;
1236 info.mask = buffer->cur().mask;
1237 info.syllable() = buffer->cur().syllable();
Behdad Esfahbod596740d2012-12-21 19:41:04 -05001238
1239 /* Insert dottedcircle after possible Repha. */
1240 while (buffer->idx < buffer->len &&
1241 last_syllable == buffer->cur().syllable() &&
1242 buffer->cur().indic_category() == OT_Repha)
1243 buffer->next_glyph ();
1244
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001245 buffer->output_info (info);
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001246 }
Behdad Esfahbod596740d2012-12-21 19:41:04 -05001247 else
1248 buffer->next_glyph ();
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001249 }
1250
1251 buffer->swap_buffers ();
1252}
1253
1254static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -04001255initial_reordering (const hb_ot_shape_plan_t *plan,
Behdad Esfahbod24eacf12012-08-02 08:42:11 -04001256 hb_font_t *font,
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -04001257 hb_buffer_t *buffer)
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001258{
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001259 update_consonant_positions (plan, font, buffer);
Behdad Esfahbod166b5cf2012-09-07 14:55:07 -04001260 insert_dotted_circles (plan, font, buffer);
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001261
1262 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001263 unsigned int count = buffer->len;
1264 if (unlikely (!count)) return;
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001265 unsigned int last = 0;
1266 unsigned int last_syllable = info[0].syllable();
1267 for (unsigned int i = 1; i < count; i++)
1268 if (last_syllable != info[i].syllable()) {
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001269 initial_reordering_syllable (plan, font->face, buffer, last, i);
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001270 last = i;
1271 last_syllable = info[last].syllable();
1272 }
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001273 initial_reordering_syllable (plan, font->face, buffer, last, count);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -04001274}
1275
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001276static void
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001277final_reordering_syllable (const hb_ot_shape_plan_t *plan,
1278 hb_buffer_t *buffer,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001279 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001280{
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001281 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +02001282 hb_glyph_info_t *info = buffer->info;
1283
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001284 /* 4. Final reordering:
1285 *
1286 * After the localized forms and basic shaping forms GSUB features have been
1287 * applied (see below), the shaping engine performs some final glyph
1288 * reordering before applying all the remaining font features to the entire
1289 * cluster.
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +02001290 */
1291
1292 /* Find base again */
Behdad Esfahbod5f0eaaa2012-07-20 15:47:24 -04001293 unsigned int base;
1294 for (base = start; base < end; base++)
1295 if (info[base].indic_position() >= POS_BASE_C) {
1296 if (start < base && info[base].indic_position() > POS_BASE_C)
1297 base--;
1298 break;
1299 }
Behdad Esfahboda0cb9f32013-02-13 09:26:55 -05001300 if (base == end && start < base &&
1301 info[base - 1].indic_category() != OT_ZWJ)
1302 base--;
1303 while (start < base &&
1304 (info[base].indic_category() == OT_H ||
1305 info[base].indic_category() == OT_N))
1306 base--;
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +02001307
Behdad Esfahbod4705a702012-05-10 13:09:08 +02001308
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +02001309 /* o Reorder matras:
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001310 *
1311 * If a pre-base matra character had been reordered before applying basic
1312 * features, the glyph can be moved closer to the main consonant based on
1313 * whether half-forms had been formed. Actual position for the matra is
1314 * defined as “after last standalone halant glyph, after initial matra
1315 * position and before the main consonant”. If ZWJ or ZWNJ follow this
1316 * halant, position is moved after it.
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +02001317 */
1318
Behdad Esfahbod65c43ac2012-07-24 03:36:47 -04001319 if (start + 1 < end && start < base) /* Otherwise there can't be any pre-base matra characters. */
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +02001320 {
Behdad Esfahbod65c43ac2012-07-24 03:36:47 -04001321 /* If we lost track of base, alas, position before last thingy. */
1322 unsigned int new_pos = base == end ? base - 2 : base - 1;
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +02001323
Behdad Esfahbod27bd55b2012-09-05 15:11:14 -04001324 /* Malayalam / Tamil do not have "half" forms or explicit virama forms.
1325 * The glyphs formed by 'half' are Chillus or ligated explicit viramas.
1326 * We want to position matra after them.
Behdad Esfahbod65c43ac2012-07-24 03:36:47 -04001327 */
Behdad Esfahbod27bd55b2012-09-05 15:11:14 -04001328 if (buffer->props.script != HB_SCRIPT_MALAYALAM && buffer->props.script != HB_SCRIPT_TAMIL)
Behdad Esfahbod65c43ac2012-07-24 03:36:47 -04001329 {
1330 while (new_pos > start &&
1331 !(is_one_of (info[new_pos], (FLAG (OT_M) | FLAG (OT_H) | FLAG (OT_Coeng)))))
1332 new_pos--;
1333
1334 /* If we found no Halant we are done.
1335 * Otherwise only proceed if the Halant does
1336 * not belong to the Matra itself! */
1337 if (is_halant_or_coeng (info[new_pos]) &&
1338 info[new_pos].indic_position() != POS_PRE_M)
1339 {
1340 /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
1341 if (new_pos + 1 < end && is_joiner (info[new_pos + 1]))
1342 new_pos++;
1343 }
1344 else
1345 new_pos = start; /* No move. */
1346 }
1347
Behdad Esfahbod27bd55b2012-09-05 15:11:14 -04001348 if (start < new_pos && info[new_pos].indic_position () != POS_PRE_M)
Behdad Esfahbod65c43ac2012-07-24 03:36:47 -04001349 {
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +02001350 /* Now go see if there's actually any matras... */
Behdad Esfahbod921ce5b2012-07-16 15:26:56 -04001351 for (unsigned int i = new_pos; i > start; i--)
Behdad Esfahbod6a091df2012-05-11 21:42:27 +02001352 if (info[i - 1].indic_position () == POS_PRE_M)
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +02001353 {
Behdad Esfahbod1a1dbe92012-07-16 15:40:33 -04001354 unsigned int old_pos = i - 1;
1355 hb_glyph_info_t tmp = info[old_pos];
1356 memmove (&info[old_pos], &info[old_pos + 1], (new_pos - old_pos) * sizeof (info[0]));
1357 info[new_pos] = tmp;
Behdad Esfahbodf22b7e72013-02-13 07:32:46 -05001358 if (old_pos < base && base <= new_pos) /* Shouldn't actually happen. */
1359 base--;
Behdad Esfahbod9cb59d42013-10-16 11:34:07 +02001360 buffer->merge_clusters (new_pos, MIN (end, base + 1));
Behdad Esfahbod921ce5b2012-07-16 15:26:56 -04001361 new_pos--;
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +02001362 }
Behdad Esfahbodabb32392012-07-22 23:55:19 -04001363 } else {
Behdad Esfahbode6b01a82012-07-23 00:11:26 -04001364 for (unsigned int i = start; i < base; i++)
Behdad Esfahbodabb32392012-07-22 23:55:19 -04001365 if (info[i].indic_position () == POS_PRE_M) {
Behdad Esfahbod2cc933a2012-07-23 08:22:55 -04001366 buffer->merge_clusters (i, MIN (end, base + 1));
Behdad Esfahbodabb32392012-07-22 23:55:19 -04001367 break;
1368 }
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +02001369 }
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +02001370 }
1371
1372
1373 /* o Reorder reph:
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001374 *
1375 * Reph’s original position is always at the beginning of the syllable,
1376 * (i.e. it is not reordered at the character reordering stage). However,
1377 * it will be reordered according to the basic-forms shaping results.
1378 * Possible positions for reph, depending on the script, are; after main,
1379 * before post-base consonant forms, and after post-base consonant forms.
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001380 */
1381
Behdad Esfahbod65a929b2013-10-15 18:08:05 +02001382 /* Two cases:
1383 *
1384 * - If repha is encoded as a sequence of characters (Ra,H or Ra,H,ZWJ), then
1385 * we should only move it if the sequence ligated to the repha form.
1386 *
1387 * - If repha is encoded separately and in the logical position, we should only
1388 * move it if it did NOT ligate. If it ligated, it's probably the font trying
1389 * to make it work without the reordering.
1390 */
Behdad Esfahbodf5299ef2013-10-15 18:13:07 +02001391 if (start + 1 < end &&
1392 info[start].indic_position() == POS_RA_TO_BECOME_REPH &&
1393 ((info[start].indic_category() == OT_Repha) ^ is_a_ligature (info[start])))
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001394 {
Behdad Esfahbod11b0e202012-08-02 14:21:40 -04001395 unsigned int new_reph_pos;
1396 reph_position_t reph_pos = indic_plan->config->reph_pos;
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001397
Behdad Esfahbodefed40b2013-10-17 18:50:11 +02001398 assert (reph_pos != POS_RA_TO_BECOME_REPH);
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001399
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001400 /* 1. If reph should be positioned after post-base consonant forms,
1401 * proceed to step 5.
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001402 */
Behdad Esfahbod11b0e202012-08-02 14:21:40 -04001403 if (reph_pos == REPH_POS_AFTER_POST)
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001404 {
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +02001405 goto reph_step_5;
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001406 }
1407
1408 /* 2. If the reph repositioning class is not after post-base: target
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001409 * position is after the first explicit halant glyph between the
1410 * first post-reph consonant and last main consonant. If ZWJ or ZWNJ
1411 * are following this halant, position is moved after it. If such
1412 * position is found, this is the target position. Otherwise,
1413 * proceed to the next step.
1414 *
1415 * Note: in old-implementation fonts, where classifications were
1416 * fixed in shaping engine, there was no case where reph position
1417 * will be found on this step.
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001418 */
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001419 {
1420 new_reph_pos = start + 1;
Behdad Esfahboddeb521d2012-07-17 11:37:32 -04001421 while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos]))
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001422 new_reph_pos++;
1423
Behdad Esfahbod1f91c392013-02-13 09:38:40 -05001424 if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos]))
1425 {
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001426 /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
1427 if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
1428 new_reph_pos++;
1429 goto reph_move;
1430 }
1431 }
1432
1433 /* 3. If reph should be repositioned after the main consonant: find the
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001434 * first consonant not ligated with main, or find the first
1435 * consonant that is not a potential pre-base reordering Ra.
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001436 */
Behdad Esfahbod11b0e202012-08-02 14:21:40 -04001437 if (reph_pos == REPH_POS_AFTER_MAIN)
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001438 {
Behdad Esfahbodb504e062012-07-16 15:21:12 -04001439 new_reph_pos = base;
Behdad Esfahbod34ae3362012-07-20 16:17:28 -04001440 while (new_reph_pos + 1 < end && info[new_reph_pos + 1].indic_position() <= POS_AFTER_MAIN)
Behdad Esfahbodb504e062012-07-16 15:21:12 -04001441 new_reph_pos++;
1442 if (new_reph_pos < end)
1443 goto reph_move;
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001444 }
1445
1446 /* 4. If reph should be positioned before post-base consonant, find
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001447 * first post-base classified consonant not ligated with main. If no
1448 * consonant is found, the target position should be before the
1449 * first matra, syllable modifier sign or vedic sign.
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001450 */
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +02001451 /* This is our take on what step 4 is trying to say (and failing, BADLY). */
Behdad Esfahbod11b0e202012-08-02 14:21:40 -04001452 if (reph_pos == REPH_POS_AFTER_SUB)
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001453 {
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +02001454 new_reph_pos = base;
1455 while (new_reph_pos < end &&
Behdad Esfahbodbe8b9f52012-07-19 12:11:12 -04001456 !( 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 +02001457 new_reph_pos++;
1458 if (new_reph_pos < end)
1459 goto reph_move;
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001460 }
1461
1462 /* 5. If no consonant is found in steps 3 or 4, move reph to a position
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001463 * immediately before the first post-base matra, syllable modifier
1464 * sign or vedic sign that has a reordering class after the intended
1465 * reph position. For example, if the reordering position for reph
1466 * is post-main, it will skip above-base matras that also have a
1467 * post-main position.
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001468 */
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001469 reph_step_5:
1470 {
Behdad Esfahbodd0e68db2012-07-20 11:25:41 -04001471 /* Copied from step 2. */
1472 new_reph_pos = start + 1;
1473 while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos]))
1474 new_reph_pos++;
1475
Behdad Esfahbod1f91c392013-02-13 09:38:40 -05001476 if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos]))
1477 {
Behdad Esfahbodd0e68db2012-07-20 11:25:41 -04001478 /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
1479 if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
1480 new_reph_pos++;
1481 goto reph_move;
1482 }
Behdad Esfahbod8df56362012-05-10 15:41:04 +02001483 }
1484
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001485 /* 6. Otherwise, reorder reph to the end of the syllable.
1486 */
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001487 {
1488 new_reph_pos = end - 1;
1489 while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD)
1490 new_reph_pos--;
1491
Behdad Esfahbod892eb782012-05-11 16:54:40 +02001492 /*
1493 * If the Reph is to be ending up after a Matra,Halant sequence,
1494 * position it before that Halant so it can interact with the Matra.
1495 * However, if it's a plain Consonant,Halant we shouldn't do that.
1496 * Uniscribe doesn't do this.
1497 * TEST: U+0930,U+094D,U+0915,U+094B,U+094D
1498 */
Behdad Esfahbodbab02d32013-02-12 15:26:45 -05001499 if (!hb_options ().uniscribe_bug_compatible &&
Behdad Esfahboddeb521d2012-07-17 11:37:32 -04001500 unlikely (is_halant_or_coeng (info[new_reph_pos]))) {
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001501 for (unsigned int i = base + 1; i < new_reph_pos; i++)
1502 if (info[i].indic_category() == OT_M) {
1503 /* Ok, got it. */
1504 new_reph_pos--;
1505 }
1506 }
1507 goto reph_move;
1508 }
1509
1510 reph_move:
1511 {
Behdad Esfahbod6b2abdc2013-10-17 13:15:43 +02001512 buffer->merge_clusters (start, new_reph_pos + 1);
Behdad Esfahbode6b01a82012-07-23 00:11:26 -04001513
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001514 /* Move */
1515 hb_glyph_info_t reph = info[start];
1516 memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0]));
1517 info[new_reph_pos] = reph;
Behdad Esfahbodf22b7e72013-02-13 07:32:46 -05001518 if (start < base && base <= new_reph_pos)
1519 base--;
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001520 }
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001521 }
1522
1523
1524 /* o Reorder pre-base reordering consonants:
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001525 *
1526 * If a pre-base reordering consonant is found, reorder it according to
1527 * the following rules:
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001528 */
1529
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001530 if (indic_plan->mask_array[PREF] && base + 1 < end) /* Otherwise there can't be any pre-base reordering Ra. */
Behdad Esfahbod46e645e2012-07-16 15:30:05 -04001531 {
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001532 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001533 if ((info[i].mask & indic_plan->mask_array[PREF]) != 0)
Behdad Esfahbod78818122012-07-16 15:49:08 -04001534 {
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001535 /* 1. Only reorder a glyph produced by substitution during application
1536 * of the <pref> feature. (Note that a font may shape a Ra consonant with
1537 * the feature generally but block it in certain contexts.)
1538 */
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001539 if (i + 1 == end || (info[i + 1].mask & indic_plan->mask_array[PREF]) == 0)
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001540 {
1541 /*
1542 * 2. Try to find a target position the same way as for pre-base matra.
1543 * If it is found, reorder pre-base consonant glyph.
1544 *
1545 * 3. If position is not found, reorder immediately before main
1546 * consonant.
1547 */
1548
1549 unsigned int new_pos = base;
Behdad Esfahbod88d3c982012-10-29 16:27:02 -07001550 /* Malayalam / Tamil do not have "half" forms or explicit virama forms.
1551 * The glyphs formed by 'half' are Chillus or ligated explicit viramas.
1552 * We want to position matra after them.
1553 */
1554 if (buffer->props.script != HB_SCRIPT_MALAYALAM && buffer->props.script != HB_SCRIPT_TAMIL)
Behdad Esfahbodd90b8e82012-07-24 02:10:20 -04001555 {
Behdad Esfahbod88d3c982012-10-29 16:27:02 -07001556 while (new_pos > start &&
1557 !(is_one_of (info[new_pos - 1], FLAG(OT_M) | HALANT_OR_COENG_FLAGS)))
1558 new_pos--;
1559
1560 /* In Khmer coeng model, a V,Ra can go *after* matras. If it goes after a
1561 * split matra, it should be reordered to *before* the left part of such matra. */
1562 if (new_pos > start && info[new_pos - 1].indic_category() == OT_M)
1563 {
1564 unsigned int old_pos = i;
1565 for (unsigned int i = base + 1; i < old_pos; i++)
1566 if (info[i].indic_category() == OT_M)
1567 {
1568 new_pos--;
1569 break;
1570 }
1571 }
Behdad Esfahbodd90b8e82012-07-24 02:10:20 -04001572 }
1573
Behdad Esfahboddeb521d2012-07-17 11:37:32 -04001574 if (new_pos > start && is_halant_or_coeng (info[new_pos - 1]))
Behdad Esfahbod1f91c392013-02-13 09:38:40 -05001575 {
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001576 /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
1577 if (new_pos < end && is_joiner (info[new_pos]))
1578 new_pos++;
Behdad Esfahbod1f91c392013-02-13 09:38:40 -05001579 }
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001580
1581 {
1582 unsigned int old_pos = i;
Behdad Esfahbode6b01a82012-07-23 00:11:26 -04001583 buffer->merge_clusters (new_pos, old_pos + 1);
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001584 hb_glyph_info_t tmp = info[old_pos];
1585 memmove (&info[new_pos + 1], &info[new_pos], (old_pos - new_pos) * sizeof (info[0]));
1586 info[new_pos] = tmp;
Behdad Esfahbodf22b7e72013-02-13 07:32:46 -05001587 if (new_pos <= base && base < old_pos)
1588 base++;
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001589 }
1590 }
1591
1592 break;
Behdad Esfahbod78818122012-07-16 15:49:08 -04001593 }
Behdad Esfahbod46e645e2012-07-16 15:30:05 -04001594 }
Behdad Esfahbodeed903b2012-05-11 20:50:53 +02001595
1596
Behdad Esfahboda913b022012-05-11 20:59:26 +02001597 /* Apply 'init' to the Left Matra if it's a word start. */
Behdad Esfahbod6a091df2012-05-11 21:42:27 +02001598 if (info[start].indic_position () == POS_PRE_M &&
Behdad Esfahboda913b022012-05-11 20:59:26 +02001599 (!start ||
Behdad Esfahbodeace47b2012-05-13 15:54:43 +02001600 !(FLAG (_hb_glyph_info_get_general_category (&info[start - 1])) &
Behdad Esfahbod2c372b82012-07-20 13:37:48 -04001601 FLAG_RANGE (HB_UNICODE_GENERAL_CATEGORY_FORMAT, HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK))))
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001602 info[start].mask |= indic_plan->mask_array[INIT];
Behdad Esfahboda913b022012-05-11 20:59:26 +02001603
Behdad Esfahbodeed903b2012-05-11 20:50:53 +02001604
Behdad Esfahbod8ed248d2012-07-20 11:42:24 -04001605 /*
1606 * Finish off the clusters and go home!
1607 */
Behdad Esfahbod9ac6b012013-10-17 16:27:38 +02001608 if (hb_options ().uniscribe_bug_compatible)
Behdad Esfahbodebe29732012-05-11 16:43:12 +02001609 {
Behdad Esfahbod9ac6b012013-10-17 16:27:38 +02001610 switch ((hb_tag_t) plan->props.script)
1611 {
1612 case HB_SCRIPT_TAMIL:
1613 case HB_SCRIPT_SINHALA:
1614 break;
1615
1616 default:
1617 /* Uniscribe merges the entire cluster... Except for Tamil & Sinhala.
1618 * This means, half forms are submerged into the main consonants cluster.
1619 * This is unnecessary, and makes cursor positioning harder, but that's what
1620 * Uniscribe does. */
1621 buffer->merge_clusters (start, end);
1622 break;
1623 }
Behdad Esfahbod21d28032012-05-10 18:34:34 +02001624 }
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001625}
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001626
1627
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001628static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -04001629final_reordering (const hb_ot_shape_plan_t *plan,
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001630 hb_font_t *font HB_UNUSED,
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -04001631 hb_buffer_t *buffer)
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001632{
1633 unsigned int count = buffer->len;
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001634 if (unlikely (!count)) return;
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001635
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001636 hb_glyph_info_t *info = buffer->info;
1637 unsigned int last = 0;
Behdad Esfahbodcee71872012-05-11 11:41:39 +02001638 unsigned int last_syllable = info[0].syllable();
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001639 for (unsigned int i = 1; i < count; i++)
Behdad Esfahbodcee71872012-05-11 11:41:39 +02001640 if (last_syllable != info[i].syllable()) {
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001641 final_reordering_syllable (plan, buffer, last, i);
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001642 last = i;
Behdad Esfahbodcee71872012-05-11 11:41:39 +02001643 last_syllable = info[last].syllable();
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001644 }
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001645 final_reordering_syllable (plan, buffer, last, count);
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001646
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001647 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
1648 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
1649}
1650
1651
Behdad Esfahbod30145272013-10-15 13:47:27 +02001652static void
1653clear_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED,
1654 hb_font_t *font HB_UNUSED,
1655 hb_buffer_t *buffer)
1656{
1657 hb_glyph_info_t *info = buffer->info;
1658 unsigned int count = buffer->len;
1659 for (unsigned int i = 0; i < count; i++)
1660 info[i].syllable() = 0;
1661}
1662
1663
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001664static hb_ot_shape_normalization_mode_t
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001665normalization_preference_indic (const hb_segment_properties_t *props HB_UNUSED)
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001666{
1667 return HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT;
1668}
1669
Behdad Esfahbodeba312c2012-11-16 12:39:23 -08001670static bool
1671decompose_indic (const hb_ot_shape_normalize_context_t *c,
Behdad Esfahbod07369152012-11-13 12:35:35 -08001672 hb_codepoint_t ab,
1673 hb_codepoint_t *a,
1674 hb_codepoint_t *b)
1675{
1676 switch (ab)
1677 {
1678 /* Don't decompose these. */
1679 case 0x0931 : return false;
1680 case 0x0B94 : return false;
1681
1682
1683 /*
1684 * Decompose split matras that don't have Unicode decompositions.
1685 */
1686
1687 case 0x0F77 : *a = 0x0FB2; *b= 0x0F81; return true;
1688 case 0x0F79 : *a = 0x0FB3; *b= 0x0F81; return true;
1689 case 0x17BE : *a = 0x17C1; *b= 0x17BE; return true;
1690 case 0x17BF : *a = 0x17C1; *b= 0x17BF; return true;
1691 case 0x17C0 : *a = 0x17C1; *b= 0x17C0; return true;
1692 case 0x17C4 : *a = 0x17C1; *b= 0x17C4; return true;
1693 case 0x17C5 : *a = 0x17C1; *b= 0x17C5; return true;
1694 case 0x1925 : *a = 0x1920; *b= 0x1923; return true;
1695 case 0x1926 : *a = 0x1920; *b= 0x1924; return true;
1696 case 0x1B3C : *a = 0x1B42; *b= 0x1B3C; return true;
1697 case 0x1112E : *a = 0x11127; *b= 0x11131; return true;
1698 case 0x1112F : *a = 0x11127; *b= 0x11132; return true;
1699#if 0
1700 /* This one has no decomposition in Unicode, but needs no decomposition either. */
1701 /* case 0x0AC9 : return false; */
1702 case 0x0B57 : *a = no decomp, -> RIGHT; return true;
1703 case 0x1C29 : *a = no decomp, -> LEFT; return true;
1704 case 0xA9C0 : *a = no decomp, -> RIGHT; return true;
1705 case 0x111BF : *a = no decomp, -> ABOVE; return true;
1706#endif
1707 }
1708
Behdad Esfahbod43b65312012-11-16 13:12:35 -08001709 if ((ab == 0x0DDA || hb_in_range<hb_codepoint_t> (ab, 0x0DDC, 0x0DDE)))
Behdad Esfahbod07369152012-11-13 12:35:35 -08001710 {
Behdad Esfahbod43b65312012-11-16 13:12:35 -08001711 /*
1712 * Sinhala split matras... Let the fun begin.
1713 *
1714 * These four characters have Unicode decompositions. However, Uniscribe
1715 * decomposes them "Khmer-style", that is, it uses the character itself to
1716 * get the second half. The first half of all four decompositions is always
1717 * U+0DD9.
1718 *
1719 * Now, there are buggy fonts, namely, the widely used lklug.ttf, that are
1720 * broken with Uniscribe. But we need to support them. As such, we only
1721 * do the Uniscribe-style decomposition if the character is transformed into
1722 * its "sec.half" form by the 'pstf' feature. Otherwise, we fall back to
1723 * Unicode decomposition.
1724 *
1725 * Note that we can't unconditionally use Unicode decomposition. That would
1726 * break some other fonts, that are designed to work with Uniscribe, and
1727 * don't have positioning features for the Unicode-style decomposition.
1728 *
1729 * Argh...
Behdad Esfahbodb71b0bd2012-12-05 19:20:31 -05001730 *
1731 * The Uniscribe behavior is now documented in the newly published Sinhala
1732 * spec in 2012:
1733 *
1734 * http://www.microsoft.com/typography/OpenTypeDev/sinhala/intro.htm#shaping
Behdad Esfahbod43b65312012-11-16 13:12:35 -08001735 */
1736
1737 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) c->plan->data;
1738
1739 hb_codepoint_t glyph;
1740
Behdad Esfahbodbab02d32013-02-12 15:26:45 -05001741 if (hb_options ().uniscribe_bug_compatible ||
Behdad Esfahbod43b65312012-11-16 13:12:35 -08001742 (c->font->get_glyph (ab, 0, &glyph) &&
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +02001743 indic_plan->pstf.would_substitute (&glyph, 1, c->font->face)))
Behdad Esfahbod43b65312012-11-16 13:12:35 -08001744 {
1745 /* Ok, safe to use Uniscribe-style decomposition. */
1746 *a = 0x0DD9;
1747 *b = ab;
1748 return true;
1749 }
Behdad Esfahbod07369152012-11-13 12:35:35 -08001750 }
1751
Behdad Esfahbodeba312c2012-11-16 12:39:23 -08001752 return c->unicode->decompose (ab, a, b);
Behdad Esfahbod07369152012-11-13 12:35:35 -08001753}
1754
Behdad Esfahbodeba312c2012-11-16 12:39:23 -08001755static bool
1756compose_indic (const hb_ot_shape_normalize_context_t *c,
Behdad Esfahbod07369152012-11-13 12:35:35 -08001757 hb_codepoint_t a,
1758 hb_codepoint_t b,
1759 hb_codepoint_t *ab)
1760{
1761 /* Avoid recomposing split matras. */
Behdad Esfahbodeba312c2012-11-16 12:39:23 -08001762 if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (c->unicode->general_category (a)))
Behdad Esfahbod07369152012-11-13 12:35:35 -08001763 return false;
1764
1765 /* Composition-exclusion exceptions that we want to recompose. */
1766 if (a == 0x09AF && b == 0x09BC) { *ab = 0x09DF; return true; }
1767
Behdad Esfahbodeba312c2012-11-16 12:39:23 -08001768 return c->unicode->compose (a, b, ab);
Behdad Esfahbod07369152012-11-13 12:35:35 -08001769}
1770
1771
Behdad Esfahbod693918e2012-07-30 21:08:51 -04001772const hb_ot_complex_shaper_t _hb_ot_complex_shaper_indic =
1773{
1774 "indic",
1775 collect_features_indic,
1776 override_features_indic,
Behdad Esfahboda8c6da92012-08-02 10:46:34 -04001777 data_create_indic,
1778 data_destroy_indic,
Behdad Esfahbod9f9f04c2012-08-11 18:34:13 -04001779 NULL, /* preprocess_text */
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001780 normalization_preference_indic,
Behdad Esfahbod07369152012-11-13 12:35:35 -08001781 decompose_indic,
1782 compose_indic,
Behdad Esfahbod693918e2012-07-30 21:08:51 -04001783 setup_masks_indic,
Behdad Esfahbod56800022013-02-12 09:44:57 -05001784 HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
Behdad Esfahbod865745b2012-11-14 13:48:26 -08001785 false, /* fallback_position */
Behdad Esfahbod693918e2012-07-30 21:08:51 -04001786};