blob: bc79025f73ff5bff27ca22c343ecd3b6e737f5ce [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 Esfahbod11b0e202012-08-02 14:21:40 -0400276 REPH_POS_AFTER_MAIN = POS_AFTER_MAIN,
277 REPH_POS_BEFORE_SUB = POS_BEFORE_SUB,
278 REPH_POS_AFTER_SUB = POS_AFTER_SUB,
279 REPH_POS_BEFORE_POST = POS_BEFORE_POST,
Behdad Esfahbod74f4bbf2013-10-17 19:07:53 +0200280 REPH_POS_AFTER_POST = POS_AFTER_POST,
281 REPH_POS_DONT_CARE = POS_RA_TO_BECOME_REPH
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400282};
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 Esfahbod74f4bbf2013-10-17 19:07:53 +0200293enum pref_len_t {
294 PREF_LEN_1 = 1,
295 PREF_LEN_2 = 2,
296 PREF_LEN_DONT_CARE = PREF_LEN_2
297};
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400298struct indic_config_t
299{
300 hb_script_t script;
301 bool has_old_spec;
302 hb_codepoint_t virama;
303 base_position_t base_pos;
304 reph_position_t reph_pos;
305 reph_mode_t reph_mode;
Behdad Esfahbod8acbb6b2013-10-15 12:15:49 +0200306 blwf_mode_t blwf_mode;
Behdad Esfahbod74f4bbf2013-10-17 19:07:53 +0200307 pref_len_t pref_len;
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400308};
309
310static const indic_config_t indic_configs[] =
311{
312 /* Default. Should be first. */
Behdad Esfahbod74f4bbf2013-10-17 19:07:53 +0200313 {HB_SCRIPT_INVALID, false, 0,BASE_POS_LAST, REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST, PREF_LEN_1},
314 {HB_SCRIPT_DEVANAGARI,true, 0x094D,BASE_POS_LAST, REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST, PREF_LEN_DONT_CARE},
315 {HB_SCRIPT_BENGALI, true, 0x09CD,BASE_POS_LAST, REPH_POS_AFTER_SUB, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST, PREF_LEN_DONT_CARE},
316 {HB_SCRIPT_GURMUKHI, true, 0x0A4D,BASE_POS_LAST, REPH_POS_BEFORE_SUB, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST, PREF_LEN_DONT_CARE},
317 {HB_SCRIPT_GUJARATI, true, 0x0ACD,BASE_POS_LAST, REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST, PREF_LEN_DONT_CARE},
318 {HB_SCRIPT_ORIYA, true, 0x0B4D,BASE_POS_LAST, REPH_POS_AFTER_MAIN, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST, PREF_LEN_DONT_CARE},
319 {HB_SCRIPT_TAMIL, true, 0x0BCD,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST, PREF_LEN_2},
320 {HB_SCRIPT_TELUGU, true, 0x0C4D,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_EXPLICIT, BLWF_MODE_POST_ONLY, PREF_LEN_2},
321 {HB_SCRIPT_KANNADA, true, 0x0CCD,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_IMPLICIT, BLWF_MODE_POST_ONLY, PREF_LEN_2},
322 {HB_SCRIPT_MALAYALAM, true, 0x0D4D,BASE_POS_LAST, REPH_POS_AFTER_MAIN, REPH_MODE_LOG_REPHA,BLWF_MODE_PRE_AND_POST, PREF_LEN_2},
Behdad Esfahbode10453e2013-10-17 16:49:06 +0200323 {HB_SCRIPT_SINHALA, false,0x0DCA,BASE_POS_LAST_SINHALA,
Behdad Esfahbod74f4bbf2013-10-17 19:07:53 +0200324 REPH_POS_AFTER_MAIN, REPH_MODE_EXPLICIT, BLWF_MODE_PRE_AND_POST, PREF_LEN_DONT_CARE},
325 {HB_SCRIPT_KHMER, false,0x17D2,BASE_POS_FIRST,REPH_POS_DONT_CARE, REPH_MODE_VIS_REPHA,BLWF_MODE_PRE_AND_POST, PREF_LEN_2},
Behdad Esfahbod8f9ec922013-10-17 19:52:47 +0200326 {HB_SCRIPT_JAVANESE, false,0xA9C0,BASE_POS_FIRST,REPH_POS_DONT_CARE, REPH_MODE_VIS_REPHA,BLWF_MODE_PRE_AND_POST, PREF_LEN_1},
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400327};
328
329
330
331/*
332 * Indic shaper.
333 */
Behdad Esfahbod9ccc6382012-07-19 12:32:16 -0400334
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200335struct feature_list_t {
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400336 hb_tag_t tag;
Behdad Esfahbodec544862013-02-14 11:25:10 -0500337 hb_ot_map_feature_flags_t flags;
Behdad Esfahbodeed903b2012-05-11 20:50:53 +0200338};
339
340static const feature_list_t
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400341indic_features[] =
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400342{
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400343 /*
344 * Basic features.
345 * These features are applied in order, one at a time, after initial_reordering.
346 */
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400347 {HB_TAG('n','u','k','t'), F_GLOBAL},
348 {HB_TAG('a','k','h','n'), F_GLOBAL},
349 {HB_TAG('r','p','h','f'), F_NONE},
350 {HB_TAG('r','k','r','f'), F_GLOBAL},
351 {HB_TAG('p','r','e','f'), F_NONE},
352 {HB_TAG('b','l','w','f'), F_NONE},
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400353 {HB_TAG('a','b','v','f'), F_NONE},
Behdad Esfahboda01cbf62013-10-15 16:37:53 +0200354 {HB_TAG('h','a','l','f'), F_NONE},
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400355 {HB_TAG('p','s','t','f'), F_NONE},
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400356 {HB_TAG('v','a','t','u'), F_GLOBAL},
357 {HB_TAG('c','j','c','t'), F_GLOBAL},
Behdad Esfahboda01cbf62013-10-15 16:37:53 +0200358 {HB_TAG('c','f','a','r'), F_NONE},
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400359 /*
360 * Other features.
361 * These features are applied all at once, after final_reordering.
Behdad Esfahboda01cbf62013-10-15 16:37:53 +0200362 * Default Bengali font in Windows for example has intermixed
363 * lookups for init,pres,abvs,blws features.
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400364 */
Behdad Esfahbodec544862013-02-14 11:25:10 -0500365 {HB_TAG('i','n','i','t'), F_NONE},
366 {HB_TAG('p','r','e','s'), F_GLOBAL},
367 {HB_TAG('a','b','v','s'), F_GLOBAL},
368 {HB_TAG('b','l','w','s'), F_GLOBAL},
369 {HB_TAG('p','s','t','s'), F_GLOBAL},
370 {HB_TAG('h','a','l','n'), F_GLOBAL},
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400371 /* Positioning features, though we don't care about the types. */
Behdad Esfahbodec544862013-02-14 11:25:10 -0500372 {HB_TAG('d','i','s','t'), F_GLOBAL},
373 {HB_TAG('a','b','v','m'), F_GLOBAL},
374 {HB_TAG('b','l','w','m'), F_GLOBAL},
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400375};
376
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400377/*
378 * Must be in the same order as the indic_features array.
379 */
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400380enum {
381 _NUKT,
Behdad Esfahbode0475342012-07-19 20:24:14 -0400382 _AKHN,
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400383 RPHF,
Behdad Esfahboddf6d45c2012-05-09 11:38:31 +0200384 _RKRF,
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400385 PREF,
Behdad Esfahbod167b6252012-08-05 21:16:26 -0700386 BLWF,
Behdad Esfahbod29f106d2012-07-16 12:05:35 -0400387 ABVF,
Behdad Esfahboda01cbf62013-10-15 16:37:53 +0200388 HALF,
Behdad Esfahbodc7fe56a2011-06-24 19:05:34 -0400389 PSTF,
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400390 _VATU,
Behdad Esfahbod70d65652013-02-12 18:01:21 -0500391 _CJCT,
Behdad Esfahboda01cbf62013-10-15 16:37:53 +0200392 CFAR,
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400393
394 INIT,
395 _PRES,
396 _ABVS,
397 _BLWS,
398 _PSTS,
399 _HALN,
400 _DIST,
401 _ABVM,
402 _BLWM,
403
404 INDIC_NUM_FEATURES,
405 INDIC_BASIC_FEATURES = INIT /* Don't forget to update this! */
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400406};
407
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400408static void
Behdad Esfahbod166b5cf2012-09-07 14:55:07 -0400409setup_syllables (const hb_ot_shape_plan_t *plan,
410 hb_font_t *font,
411 hb_buffer_t *buffer);
412static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400413initial_reordering (const hb_ot_shape_plan_t *plan,
Behdad Esfahbodafbcc242012-08-02 08:36:40 -0400414 hb_font_t *font,
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400415 hb_buffer_t *buffer);
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400416static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400417final_reordering (const hb_ot_shape_plan_t *plan,
Behdad Esfahbodafbcc242012-08-02 08:36:40 -0400418 hb_font_t *font,
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400419 hb_buffer_t *buffer);
Behdad Esfahbod30145272013-10-15 13:47:27 +0200420static void
421clear_syllables (const hb_ot_shape_plan_t *plan,
422 hb_font_t *font,
423 hb_buffer_t *buffer);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400424
Behdad Esfahbod693918e2012-07-30 21:08:51 -0400425static void
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400426collect_features_indic (hb_ot_shape_planner_t *plan)
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400427{
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400428 hb_ot_map_builder_t *map = &plan->map;
429
Behdad Esfahbod166b5cf2012-09-07 14:55:07 -0400430 /* Do this before any lookups have been applied. */
431 map->add_gsub_pause (setup_syllables);
432
Behdad Esfahbode7ffcfa2013-02-14 11:05:56 -0500433 map->add_global_bool_feature (HB_TAG('l','o','c','l'));
Behdad Esfahboda54a5502011-07-20 16:42:10 -0400434 /* The Indic specs do not require ccmp, but we apply it here since if
435 * there is a use of it, it's typically at the beginning. */
Behdad Esfahbode7ffcfa2013-02-14 11:05:56 -0500436 map->add_global_bool_feature (HB_TAG('c','c','m','p'));
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400437
Behdad Esfahbodf6fd3782011-07-08 00:22:40 -0400438
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400439 unsigned int i = 0;
440 map->add_gsub_pause (initial_reordering);
441 for (; i < INDIC_BASIC_FEATURES; i++) {
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400442 map->add_feature (indic_features[i].tag, 1, indic_features[i].flags | F_MANUAL_ZWJ);
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400443 map->add_gsub_pause (NULL);
Behdad Esfahbod412b9182012-05-09 11:07:18 +0200444 }
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400445 map->add_gsub_pause (final_reordering);
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400446 for (; i < INDIC_NUM_FEATURES; i++) {
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400447 map->add_feature (indic_features[i].tag, 1, indic_features[i].flags | F_MANUAL_ZWJ);
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400448 }
Behdad Esfahbod42d0f552013-10-17 13:05:05 +0200449
450 map->add_global_bool_feature (HB_TAG('c','a','l','t'));
451 map->add_global_bool_feature (HB_TAG('c','l','i','g'));
452
Behdad Esfahbod30145272013-10-15 13:47:27 +0200453 map->add_gsub_pause (clear_syllables);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -0400454}
455
Behdad Esfahbod693918e2012-07-30 21:08:51 -0400456static void
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400457override_features_indic (hb_ot_shape_planner_t *plan)
Behdad Esfahbodd96838e2012-07-16 20:26:57 -0400458{
Behdad Esfahbodeb102332013-10-15 15:26:44 +0200459 /* Uniscribe does not apply 'kern' in Khmer. */
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500460 if (hb_options ().uniscribe_bug_compatible)
Behdad Esfahbodeb102332013-10-15 15:26:44 +0200461 {
462 switch ((hb_tag_t) plan->props.script)
463 {
464 case HB_SCRIPT_KHMER:
Behdad Esfahbod9ac6b012013-10-17 16:27:38 +0200465 plan->map.add_feature (HB_TAG('k','e','r','n'), 0, F_GLOBAL);
Behdad Esfahbodeb102332013-10-15 15:26:44 +0200466 break;
467 }
468 }
Behdad Esfahbod6b389dd2012-11-12 11:02:56 -0800469
Behdad Esfahbodec544862013-02-14 11:25:10 -0500470 plan->map.add_feature (HB_TAG('l','i','g','a'), 0, F_GLOBAL);
Behdad Esfahbodd96838e2012-07-16 20:26:57 -0400471}
472
Behdad Esfahbod867361c2011-06-17 18:35:46 -0400473
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400474struct would_substitute_feature_t
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400475{
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +0200476 inline void init (const hb_ot_map_t *map, hb_tag_t feature_tag, bool zero_context_)
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400477 {
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +0200478 zero_context = zero_context_;
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400479 map->get_stage_lookups (0/*GSUB*/,
480 map->get_feature_stage (0/*GSUB*/, feature_tag),
481 &lookups, &count);
482 }
483
Behdad Esfahbod81449362013-03-05 20:08:59 -0500484 inline bool would_substitute (const hb_codepoint_t *glyphs,
485 unsigned int glyphs_count,
Behdad Esfahbod81449362013-03-05 20:08:59 -0500486 hb_face_t *face) const
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400487 {
488 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod362a9902012-11-15 14:57:31 -0800489 if (hb_ot_layout_lookup_would_substitute_fast (face, lookups[i].index, glyphs, glyphs_count, zero_context))
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400490 return true;
491 return false;
492 }
493
494 private:
495 const hb_ot_map_t::lookup_map_t *lookups;
496 unsigned int count;
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +0200497 bool zero_context;
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400498};
499
500struct indic_shape_plan_t
501{
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400502 ASSERT_POD ();
Behdad Esfahbod914ffaa2012-08-02 11:03:39 -0400503
504 inline bool get_virama_glyph (hb_font_t *font, hb_codepoint_t *pglyph) const
505 {
506 hb_codepoint_t glyph = virama_glyph;
507 if (unlikely (virama_glyph == (hb_codepoint_t) -1))
508 {
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400509 if (!config->virama || !font->get_glyph (config->virama, 0, &glyph))
Behdad Esfahbod914ffaa2012-08-02 11:03:39 -0400510 glyph = 0;
511 /* Technically speaking, the spec says we should apply 'locl' to virama too.
512 * Maybe one day... */
513
514 /* Our get_glyph() function needs a font, so we can't get the virama glyph
515 * during shape planning... Instead, overwrite it here. It's safe. Don't worry! */
516 (const_cast<indic_shape_plan_t *> (this))->virama_glyph = glyph;
517 }
518
519 *pglyph = glyph;
520 return glyph != 0;
521 }
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400522
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400523 const indic_config_t *config;
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400524
525 bool is_old_spec;
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400526 hb_codepoint_t virama_glyph;
527
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -0800528 would_substitute_feature_t rphf;
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400529 would_substitute_feature_t pref;
530 would_substitute_feature_t blwf;
531 would_substitute_feature_t pstf;
532
533 hb_mask_t mask_array[INDIC_NUM_FEATURES];
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400534};
535
536static void *
537data_create_indic (const hb_ot_shape_plan_t *plan)
538{
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400539 indic_shape_plan_t *indic_plan = (indic_shape_plan_t *) calloc (1, sizeof (indic_shape_plan_t));
540 if (unlikely (!indic_plan))
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400541 return NULL;
542
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400543 indic_plan->config = &indic_configs[0];
544 for (unsigned int i = 1; i < ARRAY_LENGTH (indic_configs); i++)
545 if (plan->props.script == indic_configs[i].script) {
546 indic_plan->config = &indic_configs[i];
547 break;
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400548 }
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400549
Behdad Esfahbod851784f2012-11-14 16:24:05 -0800550 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 -0400551 indic_plan->virama_glyph = (hb_codepoint_t) -1;
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400552
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +0200553 /* Use zero-context would_substitute() matching for new-spec of the main
554 * Indic scripts, but not for old-spec or scripts with one spec only. */
555 bool zero_context = indic_plan->config->has_old_spec || !indic_plan->is_old_spec;
556 indic_plan->rphf.init (&plan->map, HB_TAG('r','p','h','f'), zero_context);
557 indic_plan->pref.init (&plan->map, HB_TAG('p','r','e','f'), zero_context);
558 indic_plan->blwf.init (&plan->map, HB_TAG('b','l','w','f'), zero_context);
559 indic_plan->pstf.init (&plan->map, HB_TAG('p','s','t','f'), zero_context);
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400560
561 for (unsigned int i = 0; i < ARRAY_LENGTH (indic_plan->mask_array); i++)
Behdad Esfahbodec544862013-02-14 11:25:10 -0500562 indic_plan->mask_array[i] = (indic_features[i].flags & F_GLOBAL) ?
563 0 : plan->map.get_1_mask (indic_features[i].tag);
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400564
565 return indic_plan;
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400566}
567
568static void
569data_destroy_indic (void *data)
570{
571 free (data);
572}
573
574static indic_position_t
575consonant_position_from_face (const indic_shape_plan_t *indic_plan,
Behdad Esfahbod684fe592013-10-17 18:30:06 +0200576 const hb_codepoint_t consonant,
577 const hb_codepoint_t virama,
Behdad Esfahbod81449362013-03-05 20:08:59 -0500578 hb_face_t *face)
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400579{
Behdad Esfahbod81449362013-03-05 20:08:59 -0500580 /* For old-spec, the order of glyphs is Consonant,Virama,
581 * whereas for new-spec, it's Virama,Consonant. However,
582 * some broken fonts (like Free Sans) simply copied lookups
583 * from old-spec to new-spec without modification.
584 * And oddly enough, Uniscribe seems to respect those lookups.
585 * Eg. in the sequence U+0924,U+094D,U+0930, Uniscribe finds
586 * base at 0. The font however, only has lookups matching
587 * 930,94D in 'blwf', not the expected 94D,930 (with new-spec
588 * table). As such, we simply match both sequences. Seems
589 * to work. */
Behdad Esfahbod684fe592013-10-17 18:30:06 +0200590 hb_codepoint_t glyphs[3] = {virama, consonant, virama};
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +0200591 if (indic_plan->blwf.would_substitute (glyphs , 2, face) ||
Behdad Esfahbod684fe592013-10-17 18:30:06 +0200592 indic_plan->blwf.would_substitute (glyphs+1, 2, face))
Behdad Esfahbod81449362013-03-05 20:08:59 -0500593 return POS_BELOW_C;
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +0200594 if (indic_plan->pstf.would_substitute (glyphs , 2, face) ||
Behdad Esfahbod684fe592013-10-17 18:30:06 +0200595 indic_plan->pstf.would_substitute (glyphs+1, 2, face))
Behdad Esfahbod81449362013-03-05 20:08:59 -0500596 return POS_POST_C;
Behdad Esfahbod74f4bbf2013-10-17 19:07:53 +0200597 unsigned int pref_len = indic_plan->config->pref_len;
598 if ((pref_len == PREF_LEN_2 &&
599 (indic_plan->pref.would_substitute (glyphs , 2, face) ||
600 indic_plan->pref.would_substitute (glyphs+1, 2, face)))
601 || (pref_len == PREF_LEN_1 &&
602 indic_plan->pref.would_substitute (glyphs+1, 1, face)))
Behdad Esfahbodae9a5832013-10-17 12:24:55 +0200603 return POS_POST_C;
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400604 return POS_BASE_C;
605}
606
607
Behdad Esfahbod166b5cf2012-09-07 14:55:07 -0400608enum syllable_type_t {
609 consonant_syllable,
610 vowel_syllable,
611 standalone_cluster,
Behdad Esfahbod3c7b3642013-10-15 11:21:01 +0200612 avagraha_cluster,
Behdad Esfahbod166b5cf2012-09-07 14:55:07 -0400613 broken_cluster,
614 non_indic_cluster,
615};
616
617#include "hb-ot-shape-complex-indic-machine.hh"
618
619
Behdad Esfahbod693918e2012-07-30 21:08:51 -0400620static void
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400621setup_masks_indic (const hb_ot_shape_plan_t *plan HB_UNUSED,
622 hb_buffer_t *buffer,
623 hb_font_t *font HB_UNUSED)
Behdad Esfahbod24eacf12012-08-02 08:42:11 -0400624{
625 HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
626 HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
627
628 /* We cannot setup masks here. We save information about characters
629 * and setup masks later on in a pause-callback. */
630
631 unsigned int count = buffer->len;
632 for (unsigned int i = 0; i < count; i++)
633 set_indic_properties (buffer->info[i]);
634}
635
Behdad Esfahbod166b5cf2012-09-07 14:55:07 -0400636static void
637setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED,
638 hb_font_t *font HB_UNUSED,
639 hb_buffer_t *buffer)
640{
641 find_syllables (buffer);
642}
643
Behdad Esfahbod24eacf12012-08-02 08:42:11 -0400644static int
645compare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
646{
647 int a = pa->indic_position();
648 int b = pb->indic_position();
649
650 return a < b ? -1 : a == b ? 0 : +1;
651}
652
653
654
655static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400656update_consonant_positions (const hb_ot_shape_plan_t *plan,
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -0400657 hb_font_t *font,
658 hb_buffer_t *buffer)
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400659{
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400660 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400661
Behdad Esfahbodc4e71ff2013-10-17 17:04:47 +0200662 if (indic_plan->config->base_pos != BASE_POS_LAST)
663 return;
664
Behdad Esfahbod684fe592013-10-17 18:30:06 +0200665 hb_codepoint_t virama;
666 if (indic_plan->get_virama_glyph (font, &virama))
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400667 {
668 hb_face_t *face = font->face;
669 unsigned int count = buffer->len;
670 for (unsigned int i = 0; i < count; i++)
671 if (buffer->info[i].indic_position() == POS_BASE_C) {
Behdad Esfahbod684fe592013-10-17 18:30:06 +0200672 hb_codepoint_t consonant = buffer->info[i].codepoint;
673 buffer->info[i].indic_position() = consonant_position_from_face (indic_plan, consonant, virama, face);
Behdad Esfahbod8ef3d532012-08-02 07:53:18 -0400674 }
675 }
676}
677
Behdad Esfahbod867361c2011-06-17 18:35:46 -0400678
Behdad Esfahbod7ea58db2012-05-11 18:58:57 +0200679/* Rules from:
680 * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
681
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400682static void
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -0800683initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan,
684 hb_face_t *face,
685 hb_buffer_t *buffer,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +0200686 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400687{
Behdad Esfahbod914ffaa2012-08-02 11:03:39 -0400688 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
Behdad Esfahbodee58f3b2011-07-30 19:15:53 -0400689 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400690
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200691
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400692 /* 1. Find base consonant:
693 *
694 * The shaping engine finds the base consonant of the syllable, using the
695 * following algorithm: starting from the end of the syllable, move backwards
696 * until a consonant is found that does not have a below-base or post-base
697 * form (post-base forms have to follow below-base forms), or that is not a
698 * pre-base reordering Ra, or arrive at the first consonant. The consonant
699 * stopped at will be the base.
700 *
701 * o If the syllable starts with Ra + Halant (in a script that has Reph)
702 * and has more than one consonant, Ra is excluded from candidates for
703 * base consonants.
704 */
705
Behdad Esfahbod5e720712011-07-31 17:51:50 -0400706 unsigned int base = end;
Behdad Esfahbod76b34092012-05-09 11:43:43 +0200707 bool has_reph = false;
708
Behdad Esfahbod76b34092012-05-09 11:43:43 +0200709 {
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200710 /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
711 * and has more than one consonant, Ra is excluded from candidates for
712 * base consonants. */
713 unsigned int limit = start;
Behdad Esfahbodefed40b2013-10-17 18:50:11 +0200714 if (indic_plan->config->reph_pos != POS_RA_TO_BECOME_REPH &&
715 indic_plan->mask_array[RPHF] &&
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200716 start + 3 <= end &&
Behdad Esfahbod8b217f52012-12-21 15:48:32 -0500717 (
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400718 (indic_plan->config->reph_mode == REPH_MODE_IMPLICIT && !is_joiner (info[start + 2])) ||
719 (indic_plan->config->reph_mode == REPH_MODE_EXPLICIT && info[start + 2].indic_category() == OT_ZWJ)
Behdad Esfahbod3285e102012-07-18 17:22:14 -0400720 ))
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200721 {
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -0800722 /* See if it matches the 'rphf' feature. */
723 hb_codepoint_t glyphs[2] = {info[start].codepoint, info[start + 1].codepoint};
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +0200724 if (indic_plan->rphf.would_substitute (glyphs, ARRAY_LENGTH (glyphs), face))
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -0800725 {
726 limit += 2;
727 while (limit < end && is_joiner (info[limit]))
728 limit++;
729 base = start;
730 has_reph = true;
731 }
Behdad Esfahbod8b217f52012-12-21 15:48:32 -0500732 } else if (indic_plan->config->reph_mode == REPH_MODE_LOG_REPHA && info[start].indic_category() == OT_Repha)
733 {
734 limit += 1;
735 while (limit < end && is_joiner (info[limit]))
736 limit++;
737 base = start;
738 has_reph = true;
739 }
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400740
Behdad Esfahbod23b0e9d2012-08-26 14:30:18 -0400741 switch (indic_plan->config->base_pos)
Behdad Esfahbod14dbdd92012-07-18 13:13:03 -0400742 {
Behdad Esfahbodd469fad2012-11-14 15:07:36 -0800743 default:
744 assert (false);
745 /* fallthrough */
746
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400747 case BASE_POS_LAST:
748 {
749 /* -> starting from the end of the syllable, move backwards */
750 unsigned int i = end;
751 bool seen_below = false;
752 do {
753 i--;
754 /* -> until a consonant is found */
755 if (is_consonant (info[i]))
756 {
757 /* -> that does not have a below-base or post-base form
758 * (post-base forms have to follow below-base forms), */
759 if (info[i].indic_position() != POS_BELOW_C &&
760 (info[i].indic_position() != POS_POST_C || seen_below))
761 {
762 base = i;
763 break;
764 }
765 if (info[i].indic_position() == POS_BELOW_C)
766 seen_below = true;
767
768 /* -> or that is not a pre-base reordering Ra,
769 *
770 * IMPLEMENTATION NOTES:
771 *
Behdad Esfahbodfb7c1822013-03-06 00:53:24 -0500772 * Our pre-base reordering Ra's are marked POS_POST_C, so will be skipped
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400773 * by the logic above already.
774 */
775
776 /* -> or arrive at the first consonant. The consonant stopped at will
777 * be the base. */
778 base = i;
779 }
780 else
781 {
782 /* A ZWJ after a Halant stops the base search, and requests an explicit
783 * half form.
784 * A ZWJ before a Halant, requests a subjoined form instead, and hence
785 * search continues. This is particularly important for Bengali
Behdad Esfahbodc4be9912012-11-12 14:27:33 -0800786 * sequence Ra,H,Ya that should form Ya-Phalaa by subjoining Ya. */
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400787 if (start < i &&
788 info[i].indic_category() == OT_ZWJ &&
789 info[i - 1].indic_category() == OT_H)
790 break;
791 }
792 } while (i > limit);
793 }
794 break;
795
Behdad Esfahbode10453e2013-10-17 16:49:06 +0200796 case BASE_POS_LAST_SINHALA:
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400797 {
Behdad Esfahbodc4e71ff2013-10-17 17:04:47 +0200798 /* Sinhala base positioning is slightly different from main Indic, in that:
799 * 1. It's ZWJ behavior is different,
800 * 2. We don't need to look into the font for consonant positions.
801 */
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400802
803 if (!has_reph)
804 base = limit;
805
806 /* Find the last base consonant that is not blocked by ZWJ. If there is
807 * a ZWJ right before a base consonant, that would request a subjoined form. */
808 for (unsigned int i = limit; i < end; i++)
Behdad Esfahbodc4e71ff2013-10-17 17:04:47 +0200809 if (is_consonant (info[i]))
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400810 {
811 if (limit < i && info[i - 1].indic_category() == OT_ZWJ)
812 break;
813 else
814 base = i;
815 }
816
817 /* Mark all subsequent consonants as below. */
818 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbodc4e71ff2013-10-17 17:04:47 +0200819 if (is_consonant (info[i]))
Behdad Esfahbod11b0e202012-08-02 14:21:40 -0400820 info[i].indic_position() = POS_BELOW_C;
821 }
822 break;
Behdad Esfahbode10453e2013-10-17 16:49:06 +0200823
824 case BASE_POS_FIRST:
825 {
826 /* The first consonant is always the base. */
827
828 assert (indic_plan->config->reph_mode == REPH_MODE_VIS_REPHA);
Behdad Esfahbodefed40b2013-10-17 18:50:11 +0200829 assert (!has_reph);
Behdad Esfahbode10453e2013-10-17 16:49:06 +0200830
831 base = start;
832
833 /* Mark all subsequent consonants as below. */
834 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbodc4e71ff2013-10-17 17:04:47 +0200835 if (is_consonant (info[i]))
Behdad Esfahbode10453e2013-10-17 16:49:06 +0200836 info[i].indic_position() = POS_BELOW_C;
837 }
838 break;
Behdad Esfahbod5d326902012-07-17 14:23:28 -0400839 }
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200840
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200841 /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
842 * and has more than one consonant, Ra is excluded from candidates for
Behdad Esfahbod2278eef2012-07-24 00:26:43 -0400843 * base consonants.
844 *
845 * Only do this for unforced Reph. (ie. not for Ra,H,ZWJ. */
Behdad Esfahbod9621e0b2013-02-11 06:58:27 -0500846 if (has_reph && base == start && limit - base <= 2) {
Behdad Esfahbod617f4ac2012-05-13 16:48:03 +0200847 /* Have no other consonant, so Reph is not formed and Ra becomes base. */
848 has_reph = false;
849 }
Behdad Esfahbod5e4e21f2012-05-13 16:46:08 +0200850 }
Behdad Esfahbod2278eef2012-07-24 00:26:43 -0400851
Behdad Esfahbod3d250792012-05-10 11:37:42 +0200852
Behdad Esfahbod743807a2011-07-29 16:37:02 -0400853 /* 2. Decompose and reorder Matras:
854 *
855 * Each matra and any syllable modifier sign in the cluster are moved to the
856 * appropriate position relative to the consonant(s) in the cluster. The
857 * shaping engine decomposes two- or three-part matras into their constituent
858 * parts before any repositioning. Matra characters are classified by which
859 * consonant in a conjunct they have affinity for and are reordered to the
860 * following positions:
861 *
862 * o Before first half form in the syllable
863 * o After subjoined consonants
864 * o After post-form consonant
865 * o After main consonant (for above marks)
866 *
867 * IMPLEMENTATION NOTES:
868 *
869 * The normalize() routine has already decomposed matras for us, so we don't
870 * need to worry about that.
871 */
872
873
874 /* 3. Reorder marks to canonical order:
875 *
876 * Adjacent nukta and halant or nukta and vedic sign are always repositioned
877 * if necessary, so that the nukta is first.
878 *
879 * IMPLEMENTATION NOTES:
880 *
881 * We don't need to do this: the normalize() routine already did this for us.
882 */
883
884
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400885 /* Reorder characters */
886
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200887 for (unsigned int i = start; i < base; i++)
Behdad Esfahbod900cf3d2012-07-20 10:18:23 -0400888 info[i].indic_position() = MIN (POS_PRE_C, (indic_position_t) info[i].indic_position());
Behdad Esfahbod55f70eb2012-07-17 12:50:13 -0400889
Behdad Esfahbod075d6712012-07-18 15:41:53 -0400890 if (base < end)
891 info[base].indic_position() = POS_BASE_C;
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400892
Behdad Esfahbod55f70eb2012-07-17 12:50:13 -0400893 /* Mark final consonants. A final consonant is one appearing after a matra,
894 * like in Khmer. */
895 for (unsigned int i = base + 1; i < end; i++)
896 if (info[i].indic_category() == OT_M) {
897 for (unsigned int j = i + 1; j < end; j++)
898 if (is_consonant (info[j])) {
899 info[j].indic_position() = POS_FINAL_C;
900 break;
901 }
902 break;
903 }
904
Behdad Esfahbodfd06bf52011-07-30 20:14:44 -0400905 /* Handle beginning Ra */
Behdad Esfahbod5e4e21f2012-05-13 16:46:08 +0200906 if (has_reph)
Behdad Esfahboddbb10582012-05-10 13:45:52 +0200907 info[start].indic_position() = POS_RA_TO_BECOME_REPH;
Behdad Esfahbodfd06bf52011-07-30 20:14:44 -0400908
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400909 /* For old-style Indic script tags, move the first post-base Halant after
Behdad Esfahbodecd454b2013-01-08 18:09:46 -0600910 * last consonant. Only do this if there is *not* a Halant after last
911 * consonant. Otherwise it becomes messy. */
Behdad Esfahbod914ffaa2012-08-02 11:03:39 -0400912 if (indic_plan->is_old_spec) {
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200913 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400914 if (info[i].indic_category() == OT_H) {
915 unsigned int j;
916 for (j = end - 1; j > i; j--)
Behdad Esfahbodecd454b2013-01-08 18:09:46 -0600917 if (is_consonant (info[j]) || info[j].indic_category() == OT_H)
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400918 break;
Behdad Esfahbodecd454b2013-01-08 18:09:46 -0600919 if (info[j].indic_category() != OT_H && j > i) {
Behdad Esfahbodf5bc2722011-07-30 21:08:10 -0400920 /* Move Halant to after last consonant. */
921 hb_glyph_info_t t = info[i];
922 memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
923 info[j] = t;
924 }
925 break;
926 }
927 }
928
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400929 /* Attach misc marks to previous char to move with them. */
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200930 {
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400931 indic_position_t last_pos = POS_START;
932 for (unsigned int i = start; i < end; i++)
933 {
934 if ((FLAG (info[i].indic_category()) & (JOINER_FLAGS | FLAG (OT_N) | FLAG (OT_RS) | HALANT_OR_COENG_FLAGS)))
935 {
936 info[i].indic_position() = last_pos;
Behdad Esfahboddde55062012-11-14 11:37:04 -0800937 if (unlikely (info[i].indic_category() == OT_H &&
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400938 info[i].indic_position() == POS_PRE_M))
939 {
940 /*
941 * Uniscribe doesn't move the Halant with Left Matra.
942 * TEST: U+092B,U+093F,U+094DE
Behdad Esfahboddde55062012-11-14 11:37:04 -0800943 * We follow. This is important for the Sinhala
944 * U+0DDA split matra since it decomposes to U+0DD9,U+0DCA
945 * where U+0DD9 is a left matra and U+0DCA is the virama.
946 * We don't want to move the virama with the left matra.
947 * TEST: U+0D9A,U+0DDA
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400948 */
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200949 for (unsigned int j = i; j > start; j--)
Behdad Esfahbod6a091df2012-05-11 21:42:27 +0200950 if (info[j - 1].indic_position() != POS_PRE_M) {
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200951 info[i].indic_position() = info[j - 1].indic_position();
952 break;
953 }
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400954 }
955 } else if (info[i].indic_position() != POS_SMVD) {
956 last_pos = (indic_position_t) info[i].indic_position();
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200957 }
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400958 }
Behdad Esfahbodebe29732012-05-11 16:43:12 +0200959 }
Behdad Esfahbod74ccc6a2012-07-17 11:16:19 -0400960 /* Re-attach ZWJ, ZWNJ, and halant to next char, for after-base consonants. */
961 {
962 unsigned int last_halant = end;
963 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahboddeb521d2012-07-17 11:37:32 -0400964 if (is_halant_or_coeng (info[i]))
Behdad Esfahbod74ccc6a2012-07-17 11:16:19 -0400965 last_halant = i;
966 else if (is_consonant (info[i])) {
967 for (unsigned int j = last_halant; j < i; j++)
Behdad Esfahbod81202bd2012-07-20 15:10:02 -0400968 if (info[j].indic_position() != POS_SMVD)
969 info[j].indic_position() = info[i].indic_position();
Behdad Esfahbod3756efa2013-10-16 19:06:29 +0200970 last_halant = end;
Behdad Esfahbod74ccc6a2012-07-17 11:16:19 -0400971 }
972 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400973
Behdad Esfahbod28d5dae2013-10-16 12:32:12 +0200974
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200975 {
Behdad Esfahbod28d5dae2013-10-16 12:32:12 +0200976 /* Use syllable() for sort accounting temporarily. */
977 unsigned int syllable = info[start].syllable();
978 for (unsigned int i = start; i < end; i++)
979 info[i].syllable() = i - start;
980
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200981 /* Sit tight, rock 'n roll! */
Behdad Esfahbodd3637ed2012-05-10 10:51:38 +0200982 hb_bubble_sort (info + start, end - start, compare_indic_order);
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200983 /* Find base again */
984 base = end;
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +0200985 for (unsigned int i = start; i < end; i++)
Behdad Esfahbod28d5dae2013-10-16 12:32:12 +0200986 if (info[i].indic_position() == POS_BASE_C)
987 {
988 base = i;
Behdad Esfahboda391ff52012-05-10 11:31:20 +0200989 break;
990 }
Behdad Esfahbod28d5dae2013-10-16 12:32:12 +0200991 /* Things are out-of-control for post base positions, they may shuffle
992 * around like crazy. In old-spec mode, we move halants around, so in
993 * that case merge all clusters after base. Otherwise, check the sort
994 * order and merge as needed.
995 * For pre-base stuff, we handle cluster issues in final reordering. */
996 if (indic_plan->is_old_spec || end - base > 127)
997 buffer->merge_clusters (base, end);
998 else
999 {
1000 /* Note! syllable() is a one-byte field. */
1001 for (unsigned int i = base; i < end; i++)
1002 if (info[i].syllable() != 255)
1003 {
1004 unsigned int max = i;
1005 unsigned int j = start + info[i].syllable();
1006 while (j != i)
1007 {
1008 max = MAX (max, j);
1009 unsigned int next = start + info[j].syllable();
1010 info[j].syllable() = 255; /* So we don't process j later again. */
1011 j = next;
1012 }
1013 if (i != max)
1014 buffer->merge_clusters (i, max + 1);
1015 }
1016 }
1017
1018 /* Put syllable back in. */
1019 for (unsigned int i = start; i < end; i++)
1020 info[i].syllable() = syllable;
Behdad Esfahboda391ff52012-05-10 11:31:20 +02001021 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -04001022
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001023 /* Setup masks now */
1024
Behdad Esfahbod28168392011-07-31 16:00:35 -04001025 {
1026 hb_mask_t mask;
1027
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001028 /* Reph */
Behdad Esfahbod668c6042012-05-11 15:34:13 +02001029 for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++)
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001030 info[i].mask |= indic_plan->mask_array[RPHF];
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001031
Behdad Esfahbod28168392011-07-31 16:00:35 -04001032 /* Pre-base */
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001033 mask = indic_plan->mask_array[HALF];
Behdad Esfahbodc7dacac2013-10-17 12:20:24 +02001034 if (!indic_plan->is_old_spec &&
1035 indic_plan->config->blwf_mode == BLWF_MODE_PRE_AND_POST)
Behdad Esfahbod8acbb6b2013-10-15 12:15:49 +02001036 mask |= indic_plan->mask_array[BLWF];
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +02001037 for (unsigned int i = start; i < base; i++)
Behdad Esfahbod28168392011-07-31 16:00:35 -04001038 info[i].mask |= mask;
1039 /* Base */
Behdad Esfahbod20b68e62012-07-20 10:47:46 -04001040 mask = 0;
Behdad Esfahbod075d6712012-07-18 15:41:53 -04001041 if (base < end)
1042 info[base].mask |= mask;
Behdad Esfahbod28168392011-07-31 16:00:35 -04001043 /* Post-base */
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001044 mask = indic_plan->mask_array[BLWF] | indic_plan->mask_array[ABVF] | indic_plan->mask_array[PSTF];
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +02001045 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbod28168392011-07-31 16:00:35 -04001046 info[i].mask |= mask;
1047 }
Behdad Esfahbod9da04872011-07-31 13:46:44 -04001048
Behdad Esfahbod85c51ec2013-02-12 18:17:39 -05001049 if (indic_plan->is_old_spec &&
1050 buffer->props.script == HB_SCRIPT_DEVANAGARI)
1051 {
1052 /* Old-spec eye-lash Ra needs special handling. From the
1053 * spec:
1054 *
1055 * "The feature 'below-base form' is applied to consonants
1056 * having below-base forms and following the base consonant.
1057 * The exception is vattu, which may appear below half forms
1058 * as well as below the base glyph. The feature 'below-base
1059 * form' will be applied to all such occurrences of Ra as well."
1060 *
1061 * Test case: U+0924,U+094D,U+0930,U+094d,U+0915
1062 * with Sanskrit 2003 font.
1063 *
1064 * However, note that Ra,Halant,ZWJ is the correct way to
1065 * request eyelash form of Ra, so we wouldbn't inhibit it
1066 * in that sequence.
1067 *
1068 * Test case: U+0924,U+094D,U+0930,U+094d,U+200D,U+0915
1069 */
1070 for (unsigned int i = start; i + 1 < base; i++)
1071 if (info[i ].indic_category() == OT_Ra &&
1072 info[i+1].indic_category() == OT_H &&
1073 (i + 2 == base ||
1074 info[i+2].indic_category() != OT_ZWJ))
1075 {
1076 info[i ].mask |= indic_plan->mask_array[BLWF];
1077 info[i+1].mask |= indic_plan->mask_array[BLWF];
1078 }
1079 }
1080
Behdad Esfahbod74f4bbf2013-10-17 19:07:53 +02001081 unsigned int pref_len = indic_plan->config->pref_len;
1082 if (indic_plan->mask_array[PREF] && base + pref_len < end)
Behdad Esfahbod17d7de92012-07-16 15:20:15 -04001083 {
Behdad Esfahbod74f4bbf2013-10-17 19:07:53 +02001084 assert (1 <= pref_len && pref_len <= 2);
Behdad Esfahbod771a8f52012-07-23 20:07:50 -04001085 /* Find a Halant,Ra sequence and mark it for pre-base reordering processing. */
Behdad Esfahbod74f4bbf2013-10-17 19:07:53 +02001086 for (unsigned int i = base + 1; i + pref_len - 1 < end; i++) {
1087 hb_codepoint_t glyphs[2];
1088 for (unsigned int j = 0; j < pref_len; j++)
1089 glyphs[j] = info[i + j].codepoint;
1090 if (indic_plan->pref.would_substitute (glyphs, pref_len, face))
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001091 {
Behdad Esfahbod74f4bbf2013-10-17 19:07:53 +02001092 for (unsigned int j = 0; j < pref_len; j++)
1093 info[i++].mask |= indic_plan->mask_array[PREF];
Behdad Esfahbod0201e0a2012-07-17 13:55:10 -04001094
1095 /* Mark the subsequent stuff with 'cfar'. Used in Khmer.
1096 * Read the feature spec.
1097 * This allows distinguishing the following cases with MS Khmer fonts:
1098 * U+1784,U+17D2,U+179A,U+17D2,U+1782
1099 * U+1784,U+17D2,U+1782,U+17D2,U+179A
1100 */
Behdad Esfahbod74f4bbf2013-10-17 19:07:53 +02001101 if (indic_plan->mask_array[CFAR])
1102 for (; i < end; i++)
1103 info[i].mask |= indic_plan->mask_array[CFAR];
Behdad Esfahbod0201e0a2012-07-17 13:55:10 -04001104
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001105 break;
1106 }
Behdad Esfahbod56be6772012-11-12 14:09:40 -08001107 }
Behdad Esfahbod17d7de92012-07-16 15:20:15 -04001108 }
1109
Behdad Esfahbod9da04872011-07-31 13:46:44 -04001110 /* Apply ZWJ/ZWNJ effects */
Behdad Esfahbod3c2ea942012-05-11 16:23:38 +02001111 for (unsigned int i = start + 1; i < end; i++)
Behdad Esfahbod9da04872011-07-31 13:46:44 -04001112 if (is_joiner (info[i])) {
1113 bool non_joiner = info[i].indic_category() == OT_ZWNJ;
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -04001114 unsigned int j = i;
Behdad Esfahbod9da04872011-07-31 13:46:44 -04001115
1116 do {
Behdad Esfahbod9da04872011-07-31 13:46:44 -04001117 j--;
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -04001118
Behdad Esfahbodcfc507c2013-02-14 10:40:12 -05001119 /* ZWJ/ZWNJ should disable CJCT. They do that by simply
1120 * being there, since we don't skip them for the CJCT
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -04001121 * feature (ie. F_MANUAL_ZWJ) */
Behdad Esfahbod20b68e62012-07-20 10:47:46 -04001122
1123 /* A ZWNJ disables HALF. */
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -04001124 if (non_joiner)
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001125 info[j].mask &= ~indic_plan->mask_array[HALF];
Behdad Esfahbod6b37bc82011-07-31 15:57:00 -04001126
Behdad Esfahbod9da04872011-07-31 13:46:44 -04001127 } while (j > start && !is_consonant (info[j]));
1128 }
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001129}
1130
1131
1132static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -04001133initial_reordering_vowel_syllable (const hb_ot_shape_plan_t *plan,
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001134 hb_face_t *face,
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +02001135 hb_buffer_t *buffer,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001136 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001137{
Behdad Esfahbodc5306b62012-05-10 12:07:33 +02001138 /* We made the vowels look like consonants. So let's call the consonant logic! */
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001139 initial_reordering_consonant_syllable (plan, face, buffer, start, end);
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001140}
1141
1142static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -04001143initial_reordering_standalone_cluster (const hb_ot_shape_plan_t *plan,
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001144 hb_face_t *face,
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +02001145 hb_buffer_t *buffer,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001146 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001147{
Behdad Esfahbod18c06e12012-05-11 20:02:14 +02001148 /* We treat NBSP/dotted-circle as if they are consonants, so we should just chain.
1149 * Only if not in compatibility mode that is... */
1150
Behdad Esfahbodbab02d32013-02-12 15:26:45 -05001151 if (hb_options ().uniscribe_bug_compatible)
Behdad Esfahbod18c06e12012-05-11 20:02:14 +02001152 {
1153 /* For dotted-circle, this is what Uniscribe does:
1154 * If dotted-circle is the last glyph, it just does nothing.
1155 * Ie. It doesn't form Reph. */
1156 if (buffer->info[end - 1].indic_category() == OT_DOTTEDCIRCLE)
1157 return;
1158 }
1159
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001160 initial_reordering_consonant_syllable (plan, face, buffer, start, end);
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001161}
1162
1163static void
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001164initial_reordering_broken_cluster (const hb_ot_shape_plan_t *plan,
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001165 hb_face_t *face,
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001166 hb_buffer_t *buffer,
1167 unsigned int start, unsigned int end)
1168{
1169 /* We already inserted dotted-circles, so just call the standalone_cluster. */
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001170 initial_reordering_standalone_cluster (plan, face, buffer, start, end);
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001171}
1172
1173static void
Behdad Esfahbod3c7b3642013-10-15 11:21:01 +02001174initial_reordering_avagraha_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED,
1175 hb_face_t *face HB_UNUSED,
1176 hb_buffer_t *buffer HB_UNUSED,
1177 unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
1178{
1179 /* Nothing to do right now. If we ever switch to using the output
1180 * buffer in the reordering process, we'd need to next_glyph() here. */
1181}
1182
1183static void
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001184initial_reordering_non_indic_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED,
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001185 hb_face_t *face HB_UNUSED,
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001186 hb_buffer_t *buffer HB_UNUSED,
1187 unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001188{
1189 /* Nothing to do right now. If we ever switch to using the output
1190 * buffer in the reordering process, we'd need to next_glyph() here. */
1191}
1192
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001193
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001194static void
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001195initial_reordering_syllable (const hb_ot_shape_plan_t *plan,
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001196 hb_face_t *face,
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001197 hb_buffer_t *buffer,
1198 unsigned int start, unsigned int end)
1199{
1200 syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F);
1201 switch (syllable_type) {
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001202 case consonant_syllable: initial_reordering_consonant_syllable (plan, face, buffer, start, end); return;
1203 case vowel_syllable: initial_reordering_vowel_syllable (plan, face, buffer, start, end); return;
1204 case standalone_cluster: initial_reordering_standalone_cluster (plan, face, buffer, start, end); return;
Behdad Esfahbod3c7b3642013-10-15 11:21:01 +02001205 case avagraha_cluster: initial_reordering_avagraha_cluster (plan, face, buffer, start, end); return;
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001206 case broken_cluster: initial_reordering_broken_cluster (plan, face, buffer, start, end); return;
1207 case non_indic_cluster: initial_reordering_non_indic_cluster (plan, face, buffer, start, end); return;
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001208 }
1209}
1210
Behdad Esfahbod166b5cf2012-09-07 14:55:07 -04001211static inline void
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001212insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED,
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001213 hb_font_t *font,
1214 hb_buffer_t *buffer)
1215{
Behdad Esfahbod166b5cf2012-09-07 14:55:07 -04001216 /* Note: This loop is extra overhead, but should not be measurable. */
1217 bool has_broken_syllables = false;
1218 unsigned int count = buffer->len;
1219 for (unsigned int i = 0; i < count; i++)
1220 if ((buffer->info[i].syllable() & 0x0F) == broken_cluster) {
1221 has_broken_syllables = true;
1222 break;
1223 }
1224 if (likely (!has_broken_syllables))
1225 return;
1226
1227
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001228 hb_codepoint_t dottedcircle_glyph;
1229 if (!font->get_glyph (0x25CC, 0, &dottedcircle_glyph))
1230 return;
1231
Behdad Esfahbodf41dc2d2012-11-15 10:36:43 -08001232 hb_glyph_info_t dottedcircle = {0};
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001233 dottedcircle.codepoint = 0x25CC;
1234 set_indic_properties (dottedcircle);
1235 dottedcircle.codepoint = dottedcircle_glyph;
1236
1237 buffer->clear_output ();
1238
1239 buffer->idx = 0;
1240 unsigned int last_syllable = 0;
1241 while (buffer->idx < buffer->len)
1242 {
1243 unsigned int syllable = buffer->cur().syllable();
1244 syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F);
1245 if (unlikely (last_syllable != syllable && syllable_type == broken_cluster))
1246 {
Behdad Esfahbod596740d2012-12-21 19:41:04 -05001247 last_syllable = syllable;
1248
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001249 hb_glyph_info_t info = dottedcircle;
1250 info.cluster = buffer->cur().cluster;
1251 info.mask = buffer->cur().mask;
1252 info.syllable() = buffer->cur().syllable();
Behdad Esfahbod596740d2012-12-21 19:41:04 -05001253
1254 /* Insert dottedcircle after possible Repha. */
1255 while (buffer->idx < buffer->len &&
1256 last_syllable == buffer->cur().syllable() &&
1257 buffer->cur().indic_category() == OT_Repha)
1258 buffer->next_glyph ();
1259
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001260 buffer->output_info (info);
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001261 }
Behdad Esfahbod596740d2012-12-21 19:41:04 -05001262 else
1263 buffer->next_glyph ();
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001264 }
1265
1266 buffer->swap_buffers ();
1267}
1268
1269static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -04001270initial_reordering (const hb_ot_shape_plan_t *plan,
Behdad Esfahbod24eacf12012-08-02 08:42:11 -04001271 hb_font_t *font,
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -04001272 hb_buffer_t *buffer)
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001273{
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001274 update_consonant_positions (plan, font, buffer);
Behdad Esfahbod166b5cf2012-09-07 14:55:07 -04001275 insert_dotted_circles (plan, font, buffer);
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001276
1277 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001278 unsigned int count = buffer->len;
1279 if (unlikely (!count)) return;
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001280 unsigned int last = 0;
1281 unsigned int last_syllable = info[0].syllable();
1282 for (unsigned int i = 1; i < count; i++)
1283 if (last_syllable != info[i].syllable()) {
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001284 initial_reordering_syllable (plan, font->face, buffer, last, i);
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001285 last = i;
1286 last_syllable = info[last].syllable();
1287 }
Behdad Esfahbodf2c0f592012-11-12 14:02:02 -08001288 initial_reordering_syllable (plan, font->face, buffer, last, count);
Behdad Esfahbodb9ddbd52011-06-02 17:43:12 -04001289}
1290
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001291static void
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001292final_reordering_syllable (const hb_ot_shape_plan_t *plan,
1293 hb_buffer_t *buffer,
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001294 unsigned int start, unsigned int end)
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001295{
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001296 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +02001297 hb_glyph_info_t *info = buffer->info;
1298
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001299 /* 4. Final reordering:
1300 *
1301 * After the localized forms and basic shaping forms GSUB features have been
1302 * applied (see below), the shaping engine performs some final glyph
1303 * reordering before applying all the remaining font features to the entire
1304 * cluster.
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +02001305 */
1306
1307 /* Find base again */
Behdad Esfahbod5f0eaaa2012-07-20 15:47:24 -04001308 unsigned int base;
1309 for (base = start; base < end; base++)
1310 if (info[base].indic_position() >= POS_BASE_C) {
1311 if (start < base && info[base].indic_position() > POS_BASE_C)
1312 base--;
1313 break;
1314 }
Behdad Esfahboda0cb9f32013-02-13 09:26:55 -05001315 if (base == end && start < base &&
1316 info[base - 1].indic_category() != OT_ZWJ)
1317 base--;
1318 while (start < base &&
1319 (info[base].indic_category() == OT_H ||
1320 info[base].indic_category() == OT_N))
1321 base--;
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +02001322
Behdad Esfahbod4705a702012-05-10 13:09:08 +02001323
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +02001324 /* o Reorder matras:
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001325 *
1326 * If a pre-base matra character had been reordered before applying basic
1327 * features, the glyph can be moved closer to the main consonant based on
1328 * whether half-forms had been formed. Actual position for the matra is
1329 * defined as “after last standalone halant glyph, after initial matra
1330 * position and before the main consonant”. If ZWJ or ZWNJ follow this
1331 * halant, position is moved after it.
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +02001332 */
1333
Behdad Esfahbod65c43ac2012-07-24 03:36:47 -04001334 if (start + 1 < end && start < base) /* Otherwise there can't be any pre-base matra characters. */
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +02001335 {
Behdad Esfahbod65c43ac2012-07-24 03:36:47 -04001336 /* If we lost track of base, alas, position before last thingy. */
1337 unsigned int new_pos = base == end ? base - 2 : base - 1;
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +02001338
Behdad Esfahbod27bd55b2012-09-05 15:11:14 -04001339 /* Malayalam / Tamil do not have "half" forms or explicit virama forms.
1340 * The glyphs formed by 'half' are Chillus or ligated explicit viramas.
1341 * We want to position matra after them.
Behdad Esfahbod65c43ac2012-07-24 03:36:47 -04001342 */
Behdad Esfahbod27bd55b2012-09-05 15:11:14 -04001343 if (buffer->props.script != HB_SCRIPT_MALAYALAM && buffer->props.script != HB_SCRIPT_TAMIL)
Behdad Esfahbod65c43ac2012-07-24 03:36:47 -04001344 {
1345 while (new_pos > start &&
1346 !(is_one_of (info[new_pos], (FLAG (OT_M) | FLAG (OT_H) | FLAG (OT_Coeng)))))
1347 new_pos--;
1348
1349 /* If we found no Halant we are done.
1350 * Otherwise only proceed if the Halant does
1351 * not belong to the Matra itself! */
1352 if (is_halant_or_coeng (info[new_pos]) &&
1353 info[new_pos].indic_position() != POS_PRE_M)
1354 {
1355 /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
1356 if (new_pos + 1 < end && is_joiner (info[new_pos + 1]))
1357 new_pos++;
1358 }
1359 else
1360 new_pos = start; /* No move. */
1361 }
1362
Behdad Esfahbod27bd55b2012-09-05 15:11:14 -04001363 if (start < new_pos && info[new_pos].indic_position () != POS_PRE_M)
Behdad Esfahbod65c43ac2012-07-24 03:36:47 -04001364 {
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +02001365 /* Now go see if there's actually any matras... */
Behdad Esfahbod921ce5b2012-07-16 15:26:56 -04001366 for (unsigned int i = new_pos; i > start; i--)
Behdad Esfahbod6a091df2012-05-11 21:42:27 +02001367 if (info[i - 1].indic_position () == POS_PRE_M)
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +02001368 {
Behdad Esfahbod1a1dbe92012-07-16 15:40:33 -04001369 unsigned int old_pos = i - 1;
1370 hb_glyph_info_t tmp = info[old_pos];
1371 memmove (&info[old_pos], &info[old_pos + 1], (new_pos - old_pos) * sizeof (info[0]));
1372 info[new_pos] = tmp;
Behdad Esfahbodf22b7e72013-02-13 07:32:46 -05001373 if (old_pos < base && base <= new_pos) /* Shouldn't actually happen. */
1374 base--;
Behdad Esfahbod9cb59d42013-10-16 11:34:07 +02001375 buffer->merge_clusters (new_pos, MIN (end, base + 1));
Behdad Esfahbod921ce5b2012-07-16 15:26:56 -04001376 new_pos--;
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +02001377 }
Behdad Esfahbodabb32392012-07-22 23:55:19 -04001378 } else {
Behdad Esfahbode6b01a82012-07-23 00:11:26 -04001379 for (unsigned int i = start; i < base; i++)
Behdad Esfahbodabb32392012-07-22 23:55:19 -04001380 if (info[i].indic_position () == POS_PRE_M) {
Behdad Esfahbod2cc933a2012-07-23 08:22:55 -04001381 buffer->merge_clusters (i, MIN (end, base + 1));
Behdad Esfahbodabb32392012-07-22 23:55:19 -04001382 break;
1383 }
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +02001384 }
Behdad Esfahbod4ac9e982012-05-10 12:53:53 +02001385 }
1386
1387
1388 /* o Reorder reph:
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001389 *
1390 * Reph’s original position is always at the beginning of the syllable,
1391 * (i.e. it is not reordered at the character reordering stage). However,
1392 * it will be reordered according to the basic-forms shaping results.
1393 * Possible positions for reph, depending on the script, are; after main,
1394 * before post-base consonant forms, and after post-base consonant forms.
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001395 */
1396
Behdad Esfahbod65a929b2013-10-15 18:08:05 +02001397 /* Two cases:
1398 *
1399 * - If repha is encoded as a sequence of characters (Ra,H or Ra,H,ZWJ), then
1400 * we should only move it if the sequence ligated to the repha form.
1401 *
1402 * - If repha is encoded separately and in the logical position, we should only
1403 * move it if it did NOT ligate. If it ligated, it's probably the font trying
1404 * to make it work without the reordering.
1405 */
Behdad Esfahbodf5299ef2013-10-15 18:13:07 +02001406 if (start + 1 < end &&
1407 info[start].indic_position() == POS_RA_TO_BECOME_REPH &&
1408 ((info[start].indic_category() == OT_Repha) ^ is_a_ligature (info[start])))
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001409 {
Behdad Esfahbod11b0e202012-08-02 14:21:40 -04001410 unsigned int new_reph_pos;
1411 reph_position_t reph_pos = indic_plan->config->reph_pos;
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001412
Behdad Esfahbodefed40b2013-10-17 18:50:11 +02001413 assert (reph_pos != POS_RA_TO_BECOME_REPH);
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001414
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001415 /* 1. If reph should be positioned after post-base consonant forms,
1416 * proceed to step 5.
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001417 */
Behdad Esfahbod11b0e202012-08-02 14:21:40 -04001418 if (reph_pos == REPH_POS_AFTER_POST)
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001419 {
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +02001420 goto reph_step_5;
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001421 }
1422
1423 /* 2. If the reph repositioning class is not after post-base: target
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001424 * position is after the first explicit halant glyph between the
1425 * first post-reph consonant and last main consonant. If ZWJ or ZWNJ
1426 * are following this halant, position is moved after it. If such
1427 * position is found, this is the target position. Otherwise,
1428 * proceed to the next step.
1429 *
1430 * Note: in old-implementation fonts, where classifications were
1431 * fixed in shaping engine, there was no case where reph position
1432 * will be found on this step.
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001433 */
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001434 {
1435 new_reph_pos = start + 1;
Behdad Esfahboddeb521d2012-07-17 11:37:32 -04001436 while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos]))
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001437 new_reph_pos++;
1438
Behdad Esfahbod1f91c392013-02-13 09:38:40 -05001439 if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos]))
1440 {
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001441 /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
1442 if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
1443 new_reph_pos++;
1444 goto reph_move;
1445 }
1446 }
1447
1448 /* 3. If reph should be repositioned after the main consonant: find the
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001449 * first consonant not ligated with main, or find the first
1450 * consonant that is not a potential pre-base reordering Ra.
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001451 */
Behdad Esfahbod11b0e202012-08-02 14:21:40 -04001452 if (reph_pos == REPH_POS_AFTER_MAIN)
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001453 {
Behdad Esfahbodb504e062012-07-16 15:21:12 -04001454 new_reph_pos = base;
Behdad Esfahbod34ae3362012-07-20 16:17:28 -04001455 while (new_reph_pos + 1 < end && info[new_reph_pos + 1].indic_position() <= POS_AFTER_MAIN)
Behdad Esfahbodb504e062012-07-16 15:21:12 -04001456 new_reph_pos++;
1457 if (new_reph_pos < end)
1458 goto reph_move;
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001459 }
1460
1461 /* 4. If reph should be positioned before post-base consonant, find
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001462 * first post-base classified consonant not ligated with main. If no
1463 * consonant is found, the target position should be before the
1464 * first matra, syllable modifier sign or vedic sign.
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001465 */
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +02001466 /* This is our take on what step 4 is trying to say (and failing, BADLY). */
Behdad Esfahbod11b0e202012-08-02 14:21:40 -04001467 if (reph_pos == REPH_POS_AFTER_SUB)
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001468 {
Behdad Esfahbod9d0d3192012-05-11 21:36:32 +02001469 new_reph_pos = base;
1470 while (new_reph_pos < end &&
Behdad Esfahbodbe8b9f52012-07-19 12:11:12 -04001471 !( 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 +02001472 new_reph_pos++;
1473 if (new_reph_pos < end)
1474 goto reph_move;
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001475 }
1476
1477 /* 5. If no consonant is found in steps 3 or 4, move reph to a position
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001478 * immediately before the first post-base matra, syllable modifier
1479 * sign or vedic sign that has a reordering class after the intended
1480 * reph position. For example, if the reordering position for reph
1481 * is post-main, it will skip above-base matras that also have a
1482 * post-main position.
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001483 */
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001484 reph_step_5:
1485 {
Behdad Esfahbodd0e68db2012-07-20 11:25:41 -04001486 /* Copied from step 2. */
1487 new_reph_pos = start + 1;
1488 while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos]))
1489 new_reph_pos++;
1490
Behdad Esfahbod1f91c392013-02-13 09:38:40 -05001491 if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos]))
1492 {
Behdad Esfahbodd0e68db2012-07-20 11:25:41 -04001493 /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
1494 if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
1495 new_reph_pos++;
1496 goto reph_move;
1497 }
Behdad Esfahbod8df56362012-05-10 15:41:04 +02001498 }
1499
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001500 /* 6. Otherwise, reorder reph to the end of the syllable.
1501 */
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001502 {
1503 new_reph_pos = end - 1;
1504 while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD)
1505 new_reph_pos--;
1506
Behdad Esfahbod892eb782012-05-11 16:54:40 +02001507 /*
1508 * If the Reph is to be ending up after a Matra,Halant sequence,
1509 * position it before that Halant so it can interact with the Matra.
1510 * However, if it's a plain Consonant,Halant we shouldn't do that.
1511 * Uniscribe doesn't do this.
1512 * TEST: U+0930,U+094D,U+0915,U+094B,U+094D
1513 */
Behdad Esfahbodbab02d32013-02-12 15:26:45 -05001514 if (!hb_options ().uniscribe_bug_compatible &&
Behdad Esfahboddeb521d2012-07-17 11:37:32 -04001515 unlikely (is_halant_or_coeng (info[new_reph_pos]))) {
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001516 for (unsigned int i = base + 1; i < new_reph_pos; i++)
1517 if (info[i].indic_category() == OT_M) {
1518 /* Ok, got it. */
1519 new_reph_pos--;
1520 }
1521 }
1522 goto reph_move;
1523 }
1524
1525 reph_move:
1526 {
Behdad Esfahbod6b2abdc2013-10-17 13:15:43 +02001527 buffer->merge_clusters (start, new_reph_pos + 1);
Behdad Esfahbode6b01a82012-07-23 00:11:26 -04001528
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001529 /* Move */
1530 hb_glyph_info_t reph = info[start];
1531 memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0]));
1532 info[new_reph_pos] = reph;
Behdad Esfahbodf22b7e72013-02-13 07:32:46 -05001533 if (start < base && base <= new_reph_pos)
1534 base--;
Behdad Esfahbod02b29222012-05-10 21:44:50 +02001535 }
Behdad Esfahboddbb10582012-05-10 13:45:52 +02001536 }
1537
1538
1539 /* o Reorder pre-base reordering consonants:
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001540 *
1541 * If a pre-base reordering consonant is found, reorder it according to
1542 * the following rules:
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001543 */
1544
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001545 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 -04001546 {
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001547 for (unsigned int i = base + 1; i < end; i++)
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001548 if ((info[i].mask & indic_plan->mask_array[PREF]) != 0)
Behdad Esfahbod78818122012-07-16 15:49:08 -04001549 {
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001550 /* 1. Only reorder a glyph produced by substitution during application
1551 * of the <pref> feature. (Note that a font may shape a Ra consonant with
1552 * the feature generally but block it in certain contexts.)
1553 */
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001554 if (i + 1 == end || (info[i + 1].mask & indic_plan->mask_array[PREF]) == 0)
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001555 {
1556 /*
1557 * 2. Try to find a target position the same way as for pre-base matra.
1558 * If it is found, reorder pre-base consonant glyph.
1559 *
1560 * 3. If position is not found, reorder immediately before main
1561 * consonant.
1562 */
1563
1564 unsigned int new_pos = base;
Behdad Esfahbod88d3c982012-10-29 16:27:02 -07001565 /* Malayalam / Tamil do not have "half" forms or explicit virama forms.
1566 * The glyphs formed by 'half' are Chillus or ligated explicit viramas.
1567 * We want to position matra after them.
1568 */
1569 if (buffer->props.script != HB_SCRIPT_MALAYALAM && buffer->props.script != HB_SCRIPT_TAMIL)
Behdad Esfahbodd90b8e82012-07-24 02:10:20 -04001570 {
Behdad Esfahbod88d3c982012-10-29 16:27:02 -07001571 while (new_pos > start &&
1572 !(is_one_of (info[new_pos - 1], FLAG(OT_M) | HALANT_OR_COENG_FLAGS)))
1573 new_pos--;
1574
1575 /* In Khmer coeng model, a V,Ra can go *after* matras. If it goes after a
1576 * split matra, it should be reordered to *before* the left part of such matra. */
1577 if (new_pos > start && info[new_pos - 1].indic_category() == OT_M)
1578 {
1579 unsigned int old_pos = i;
1580 for (unsigned int i = base + 1; i < old_pos; i++)
1581 if (info[i].indic_category() == OT_M)
1582 {
1583 new_pos--;
1584 break;
1585 }
1586 }
Behdad Esfahbodd90b8e82012-07-24 02:10:20 -04001587 }
1588
Behdad Esfahboddeb521d2012-07-17 11:37:32 -04001589 if (new_pos > start && is_halant_or_coeng (info[new_pos - 1]))
Behdad Esfahbod1f91c392013-02-13 09:38:40 -05001590 {
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001591 /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
1592 if (new_pos < end && is_joiner (info[new_pos]))
1593 new_pos++;
Behdad Esfahbod1f91c392013-02-13 09:38:40 -05001594 }
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001595
1596 {
1597 unsigned int old_pos = i;
Behdad Esfahbode6b01a82012-07-23 00:11:26 -04001598 buffer->merge_clusters (new_pos, old_pos + 1);
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001599 hb_glyph_info_t tmp = info[old_pos];
1600 memmove (&info[new_pos + 1], &info[new_pos], (old_pos - new_pos) * sizeof (info[0]));
1601 info[new_pos] = tmp;
Behdad Esfahbodf22b7e72013-02-13 07:32:46 -05001602 if (new_pos <= base && base < old_pos)
1603 base++;
Behdad Esfahbod8e7b5882012-07-16 17:04:46 -04001604 }
1605 }
1606
1607 break;
Behdad Esfahbod78818122012-07-16 15:49:08 -04001608 }
Behdad Esfahbod46e645e2012-07-16 15:30:05 -04001609 }
Behdad Esfahbodeed903b2012-05-11 20:50:53 +02001610
1611
Behdad Esfahboda913b022012-05-11 20:59:26 +02001612 /* Apply 'init' to the Left Matra if it's a word start. */
Behdad Esfahbod6a091df2012-05-11 21:42:27 +02001613 if (info[start].indic_position () == POS_PRE_M &&
Behdad Esfahboda913b022012-05-11 20:59:26 +02001614 (!start ||
Behdad Esfahbodeace47b2012-05-13 15:54:43 +02001615 !(FLAG (_hb_glyph_info_get_general_category (&info[start - 1])) &
Behdad Esfahbod2c372b82012-07-20 13:37:48 -04001616 FLAG_RANGE (HB_UNICODE_GENERAL_CATEGORY_FORMAT, HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK))))
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001617 info[start].mask |= indic_plan->mask_array[INIT];
Behdad Esfahboda913b022012-05-11 20:59:26 +02001618
Behdad Esfahbodeed903b2012-05-11 20:50:53 +02001619
Behdad Esfahbod8ed248d2012-07-20 11:42:24 -04001620 /*
1621 * Finish off the clusters and go home!
1622 */
Behdad Esfahbod9ac6b012013-10-17 16:27:38 +02001623 if (hb_options ().uniscribe_bug_compatible)
Behdad Esfahbodebe29732012-05-11 16:43:12 +02001624 {
Behdad Esfahbod9ac6b012013-10-17 16:27:38 +02001625 switch ((hb_tag_t) plan->props.script)
1626 {
1627 case HB_SCRIPT_TAMIL:
1628 case HB_SCRIPT_SINHALA:
1629 break;
1630
1631 default:
1632 /* Uniscribe merges the entire cluster... Except for Tamil & Sinhala.
1633 * This means, half forms are submerged into the main consonants cluster.
1634 * This is unnecessary, and makes cursor positioning harder, but that's what
1635 * Uniscribe does. */
1636 buffer->merge_clusters (start, end);
1637 break;
1638 }
Behdad Esfahbod21d28032012-05-10 18:34:34 +02001639 }
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001640}
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001641
1642
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001643static void
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -04001644final_reordering (const hb_ot_shape_plan_t *plan,
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001645 hb_font_t *font HB_UNUSED,
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -04001646 hb_buffer_t *buffer)
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001647{
1648 unsigned int count = buffer->len;
Behdad Esfahbod327d14e2012-08-31 16:49:34 -04001649 if (unlikely (!count)) return;
Behdad Esfahbode7be0572011-07-31 15:18:57 -04001650
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001651 hb_glyph_info_t *info = buffer->info;
1652 unsigned int last = 0;
Behdad Esfahbodcee71872012-05-11 11:41:39 +02001653 unsigned int last_syllable = info[0].syllable();
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001654 for (unsigned int i = 1; i < count; i++)
Behdad Esfahbodcee71872012-05-11 11:41:39 +02001655 if (last_syllable != info[i].syllable()) {
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001656 final_reordering_syllable (plan, buffer, last, i);
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001657 last = i;
Behdad Esfahbodcee71872012-05-11 11:41:39 +02001658 last_syllable = info[last].syllable();
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001659 }
Behdad Esfahbod85fc6c42012-08-02 12:21:44 -04001660 final_reordering_syllable (plan, buffer, last, count);
Behdad Esfahbodef24cc82012-05-09 17:56:03 +02001661
Behdad Esfahbod743807a2011-07-29 16:37:02 -04001662 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
1663 HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
1664}
1665
1666
Behdad Esfahbod30145272013-10-15 13:47:27 +02001667static void
1668clear_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED,
1669 hb_font_t *font HB_UNUSED,
1670 hb_buffer_t *buffer)
1671{
1672 hb_glyph_info_t *info = buffer->info;
1673 unsigned int count = buffer->len;
1674 for (unsigned int i = 0; i < count; i++)
1675 info[i].syllable() = 0;
1676}
1677
1678
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001679static hb_ot_shape_normalization_mode_t
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001680normalization_preference_indic (const hb_segment_properties_t *props HB_UNUSED)
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001681{
1682 return HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT;
1683}
1684
Behdad Esfahbodeba312c2012-11-16 12:39:23 -08001685static bool
1686decompose_indic (const hb_ot_shape_normalize_context_t *c,
Behdad Esfahbod07369152012-11-13 12:35:35 -08001687 hb_codepoint_t ab,
1688 hb_codepoint_t *a,
1689 hb_codepoint_t *b)
1690{
1691 switch (ab)
1692 {
1693 /* Don't decompose these. */
1694 case 0x0931 : return false;
1695 case 0x0B94 : return false;
1696
1697
1698 /*
1699 * Decompose split matras that don't have Unicode decompositions.
1700 */
1701
1702 case 0x0F77 : *a = 0x0FB2; *b= 0x0F81; return true;
1703 case 0x0F79 : *a = 0x0FB3; *b= 0x0F81; return true;
1704 case 0x17BE : *a = 0x17C1; *b= 0x17BE; return true;
1705 case 0x17BF : *a = 0x17C1; *b= 0x17BF; return true;
1706 case 0x17C0 : *a = 0x17C1; *b= 0x17C0; return true;
1707 case 0x17C4 : *a = 0x17C1; *b= 0x17C4; return true;
1708 case 0x17C5 : *a = 0x17C1; *b= 0x17C5; return true;
1709 case 0x1925 : *a = 0x1920; *b= 0x1923; return true;
1710 case 0x1926 : *a = 0x1920; *b= 0x1924; return true;
1711 case 0x1B3C : *a = 0x1B42; *b= 0x1B3C; return true;
1712 case 0x1112E : *a = 0x11127; *b= 0x11131; return true;
1713 case 0x1112F : *a = 0x11127; *b= 0x11132; return true;
1714#if 0
1715 /* This one has no decomposition in Unicode, but needs no decomposition either. */
1716 /* case 0x0AC9 : return false; */
1717 case 0x0B57 : *a = no decomp, -> RIGHT; return true;
1718 case 0x1C29 : *a = no decomp, -> LEFT; return true;
1719 case 0xA9C0 : *a = no decomp, -> RIGHT; return true;
1720 case 0x111BF : *a = no decomp, -> ABOVE; return true;
1721#endif
1722 }
1723
Behdad Esfahbod43b65312012-11-16 13:12:35 -08001724 if ((ab == 0x0DDA || hb_in_range<hb_codepoint_t> (ab, 0x0DDC, 0x0DDE)))
Behdad Esfahbod07369152012-11-13 12:35:35 -08001725 {
Behdad Esfahbod43b65312012-11-16 13:12:35 -08001726 /*
1727 * Sinhala split matras... Let the fun begin.
1728 *
1729 * These four characters have Unicode decompositions. However, Uniscribe
1730 * decomposes them "Khmer-style", that is, it uses the character itself to
1731 * get the second half. The first half of all four decompositions is always
1732 * U+0DD9.
1733 *
1734 * Now, there are buggy fonts, namely, the widely used lklug.ttf, that are
1735 * broken with Uniscribe. But we need to support them. As such, we only
1736 * do the Uniscribe-style decomposition if the character is transformed into
1737 * its "sec.half" form by the 'pstf' feature. Otherwise, we fall back to
1738 * Unicode decomposition.
1739 *
1740 * Note that we can't unconditionally use Unicode decomposition. That would
1741 * break some other fonts, that are designed to work with Uniscribe, and
1742 * don't have positioning features for the Unicode-style decomposition.
1743 *
1744 * Argh...
Behdad Esfahbodb71b0bd2012-12-05 19:20:31 -05001745 *
1746 * The Uniscribe behavior is now documented in the newly published Sinhala
1747 * spec in 2012:
1748 *
1749 * http://www.microsoft.com/typography/OpenTypeDev/sinhala/intro.htm#shaping
Behdad Esfahbod43b65312012-11-16 13:12:35 -08001750 */
1751
1752 const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) c->plan->data;
1753
1754 hb_codepoint_t glyph;
1755
Behdad Esfahbodbab02d32013-02-12 15:26:45 -05001756 if (hb_options ().uniscribe_bug_compatible ||
Behdad Esfahbod43b65312012-11-16 13:12:35 -08001757 (c->font->get_glyph (ab, 0, &glyph) &&
Behdad Esfahbodb5a0f692013-10-17 18:04:23 +02001758 indic_plan->pstf.would_substitute (&glyph, 1, c->font->face)))
Behdad Esfahbod43b65312012-11-16 13:12:35 -08001759 {
1760 /* Ok, safe to use Uniscribe-style decomposition. */
1761 *a = 0x0DD9;
1762 *b = ab;
1763 return true;
1764 }
Behdad Esfahbod07369152012-11-13 12:35:35 -08001765 }
1766
Behdad Esfahbodeba312c2012-11-16 12:39:23 -08001767 return c->unicode->decompose (ab, a, b);
Behdad Esfahbod07369152012-11-13 12:35:35 -08001768}
1769
Behdad Esfahbodeba312c2012-11-16 12:39:23 -08001770static bool
1771compose_indic (const hb_ot_shape_normalize_context_t *c,
Behdad Esfahbod07369152012-11-13 12:35:35 -08001772 hb_codepoint_t a,
1773 hb_codepoint_t b,
1774 hb_codepoint_t *ab)
1775{
1776 /* Avoid recomposing split matras. */
Behdad Esfahbodeba312c2012-11-16 12:39:23 -08001777 if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (c->unicode->general_category (a)))
Behdad Esfahbod07369152012-11-13 12:35:35 -08001778 return false;
1779
1780 /* Composition-exclusion exceptions that we want to recompose. */
1781 if (a == 0x09AF && b == 0x09BC) { *ab = 0x09DF; return true; }
1782
Behdad Esfahbodeba312c2012-11-16 12:39:23 -08001783 return c->unicode->compose (a, b, ab);
Behdad Esfahbod07369152012-11-13 12:35:35 -08001784}
1785
1786
Behdad Esfahbod693918e2012-07-30 21:08:51 -04001787const hb_ot_complex_shaper_t _hb_ot_complex_shaper_indic =
1788{
1789 "indic",
1790 collect_features_indic,
1791 override_features_indic,
Behdad Esfahboda8c6da92012-08-02 10:46:34 -04001792 data_create_indic,
1793 data_destroy_indic,
Behdad Esfahbod9f9f04c2012-08-11 18:34:13 -04001794 NULL, /* preprocess_text */
Behdad Esfahbodb85800f2012-08-31 18:12:01 -04001795 normalization_preference_indic,
Behdad Esfahbod07369152012-11-13 12:35:35 -08001796 decompose_indic,
1797 compose_indic,
Behdad Esfahbod693918e2012-07-30 21:08:51 -04001798 setup_masks_indic,
Behdad Esfahbod56800022013-02-12 09:44:57 -05001799 HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
Behdad Esfahbod865745b2012-11-14 13:48:26 -08001800 false, /* fallback_position */
Behdad Esfahbod693918e2012-07-30 21:08:51 -04001801};