1/*
2 * Copyright © 2012 Google, Inc.
3 *
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
27#ifndef HB_OT_SHAPE_COMPLEX_INDIC_HH
28#define HB_OT_SHAPE_COMPLEX_INDIC_HH
29
30#include "hb.hh"
31
32#include "hb-ot-shape-complex.hh"
33
34
35/* buffer var allocations */
36#define indic_category() complex_var_u8_0() /* indic_category_t */
37#define indic_position() complex_var_u8_1() /* indic_position_t */
38
39
40#define INDIC_TABLE_ELEMENT_TYPE uint16_t
41
42/* Cateories used in the OpenType spec:
43 * https://docs.microsoft.com/en-us/typography/script-development/devanagari
44 */
45/* Note: This enum is duplicated in the -machine.rl source file.
46 * Not sure how to avoid duplication. */
47enum indic_category_t {
48 OT_X = 0,
49 OT_C = 1,
50 OT_V = 2,
51 OT_N = 3,
52 OT_H = 4,
53 OT_ZWNJ = 5,
54 OT_ZWJ = 6,
55 OT_M = 7,
56 OT_SM = 8,
57 /* OT_VD = 9, UNUSED; we use OT_A instead. */
58 OT_A = 10,
59 OT_PLACEHOLDER = 11,
60 OT_DOTTEDCIRCLE = 12,
61 OT_RS = 13, /* Register Shifter, used in Khmer OT spec. */
62 OT_Coeng = 14, /* Khmer-style Virama. */
63 OT_Repha = 15, /* Atomically-encoded logical or visual repha. */
64 OT_Ra = 16,
65 OT_CM = 17, /* Consonant-Medial. */
66 OT_Symbol = 18, /* Avagraha, etc that take marks (SM,A,VD). */
67 OT_CS = 19,
68
69 /* The following are used by Khmer & Myanmar shapers. Defined
70 * here for them to share. */
71 OT_VAbv = 26,
72 OT_VBlw = 27,
73 OT_VPre = 28,
74 OT_VPst = 29,
75};
76
77#define MEDIAL_FLAGS (FLAG (OT_CM))
78
79/* Note:
80 *
81 * We treat Vowels and placeholders as if they were consonants. This is safe because Vowels
82 * cannot happen in a consonant syllable. The plus side however is, we can call the
83 * consonant syllable logic from the vowel syllable function and get it all right! */
84#define CONSONANT_FLAGS (FLAG (OT_C) | FLAG (OT_CS) | FLAG (OT_Ra) | MEDIAL_FLAGS | FLAG (OT_V) | FLAG (OT_PLACEHOLDER) | FLAG (OT_DOTTEDCIRCLE))
85#define JOINER_FLAGS (FLAG (OT_ZWJ) | FLAG (OT_ZWNJ))
86
87
88/* Visual positions in a syllable from left to right. */
89enum indic_position_t {
90 POS_START = 0,
91
92 POS_RA_TO_BECOME_REPH = 1,
93 POS_PRE_M = 2,
94 POS_PRE_C = 3,
95
96 POS_BASE_C = 4,
97 POS_AFTER_MAIN = 5,
98
99 POS_ABOVE_C = 6,
100
101 POS_BEFORE_SUB = 7,
102 POS_BELOW_C = 8,
103 POS_AFTER_SUB = 9,
104
105 POS_BEFORE_POST = 10,
106 POS_POST_C = 11,
107 POS_AFTER_POST = 12,
108
109 POS_FINAL_C = 13,
110 POS_SMVD = 14,
111
112 POS_END = 15
113};
114
115/* Categories used in IndicSyllabicCategory.txt from UCD. */
116enum indic_syllabic_category_t {
117 INDIC_SYLLABIC_CATEGORY_OTHER = OT_X,
118
119 INDIC_SYLLABIC_CATEGORY_AVAGRAHA = OT_Symbol,
120 INDIC_SYLLABIC_CATEGORY_BINDU = OT_SM,
121 INDIC_SYLLABIC_CATEGORY_BRAHMI_JOINING_NUMBER = OT_PLACEHOLDER, /* Don't care. */
122 INDIC_SYLLABIC_CATEGORY_CANTILLATION_MARK = OT_A,
123 INDIC_SYLLABIC_CATEGORY_CONSONANT = OT_C,
124 INDIC_SYLLABIC_CATEGORY_CONSONANT_DEAD = OT_C,
125 INDIC_SYLLABIC_CATEGORY_CONSONANT_FINAL = OT_CM,
126 INDIC_SYLLABIC_CATEGORY_CONSONANT_HEAD_LETTER = OT_C,
127 INDIC_SYLLABIC_CATEGORY_CONSONANT_KILLER = OT_M, /* U+17CD only. */
128 INDIC_SYLLABIC_CATEGORY_CONSONANT_MEDIAL = OT_CM,
129 INDIC_SYLLABIC_CATEGORY_CONSONANT_PLACEHOLDER = OT_PLACEHOLDER,
130 INDIC_SYLLABIC_CATEGORY_CONSONANT_PRECEDING_REPHA = OT_Repha,
131 INDIC_SYLLABIC_CATEGORY_CONSONANT_PREFIXED = OT_X, /* Don't care. */
132 INDIC_SYLLABIC_CATEGORY_CONSONANT_SUBJOINED = OT_CM,
133 INDIC_SYLLABIC_CATEGORY_CONSONANT_SUCCEEDING_REPHA = OT_CM,
134 INDIC_SYLLABIC_CATEGORY_CONSONANT_WITH_STACKER = OT_CS,
135 INDIC_SYLLABIC_CATEGORY_GEMINATION_MARK = OT_SM, /* https://github.com/harfbuzz/harfbuzz/issues/552 */
136 INDIC_SYLLABIC_CATEGORY_INVISIBLE_STACKER = OT_Coeng,
137 INDIC_SYLLABIC_CATEGORY_JOINER = OT_ZWJ,
138 INDIC_SYLLABIC_CATEGORY_MODIFYING_LETTER = OT_X,
139 INDIC_SYLLABIC_CATEGORY_NON_JOINER = OT_ZWNJ,
140 INDIC_SYLLABIC_CATEGORY_NUKTA = OT_N,
141 INDIC_SYLLABIC_CATEGORY_NUMBER = OT_PLACEHOLDER,
142 INDIC_SYLLABIC_CATEGORY_NUMBER_JOINER = OT_PLACEHOLDER, /* Don't care. */
143 INDIC_SYLLABIC_CATEGORY_PURE_KILLER = OT_M, /* Is like a vowel matra. */
144 INDIC_SYLLABIC_CATEGORY_REGISTER_SHIFTER = OT_RS,
145 INDIC_SYLLABIC_CATEGORY_SYLLABLE_MODIFIER = OT_SM,
146 INDIC_SYLLABIC_CATEGORY_TONE_LETTER = OT_X,
147 INDIC_SYLLABIC_CATEGORY_TONE_MARK = OT_N,
148 INDIC_SYLLABIC_CATEGORY_VIRAMA = OT_H,
149 INDIC_SYLLABIC_CATEGORY_VISARGA = OT_SM,
150 INDIC_SYLLABIC_CATEGORY_VOWEL = OT_V,
151 INDIC_SYLLABIC_CATEGORY_VOWEL_DEPENDENT = OT_M,
152 INDIC_SYLLABIC_CATEGORY_VOWEL_INDEPENDENT = OT_V
153};
154
155/* Categories used in IndicSMatraCategory.txt from UCD */
156enum indic_matra_category_t {
157 INDIC_MATRA_CATEGORY_NOT_APPLICABLE = POS_END,
158
159 INDIC_MATRA_CATEGORY_LEFT = POS_PRE_C,
160 INDIC_MATRA_CATEGORY_TOP = POS_ABOVE_C,
161 INDIC_MATRA_CATEGORY_BOTTOM = POS_BELOW_C,
162 INDIC_MATRA_CATEGORY_RIGHT = POS_POST_C,
163
164 /* These should resolve to the position of the last part of the split sequence. */
165 INDIC_MATRA_CATEGORY_BOTTOM_AND_RIGHT = INDIC_MATRA_CATEGORY_RIGHT,
166 INDIC_MATRA_CATEGORY_LEFT_AND_RIGHT = INDIC_MATRA_CATEGORY_RIGHT,
167 INDIC_MATRA_CATEGORY_TOP_AND_BOTTOM = INDIC_MATRA_CATEGORY_BOTTOM,
168 INDIC_MATRA_CATEGORY_TOP_AND_BOTTOM_AND_LEFT = INDIC_MATRA_CATEGORY_BOTTOM,
169 INDIC_MATRA_CATEGORY_TOP_AND_BOTTOM_AND_RIGHT = INDIC_MATRA_CATEGORY_RIGHT,
170 INDIC_MATRA_CATEGORY_TOP_AND_LEFT = INDIC_MATRA_CATEGORY_TOP,
171 INDIC_MATRA_CATEGORY_TOP_AND_LEFT_AND_RIGHT = INDIC_MATRA_CATEGORY_RIGHT,
172 INDIC_MATRA_CATEGORY_TOP_AND_RIGHT = INDIC_MATRA_CATEGORY_RIGHT,
173
174 INDIC_MATRA_CATEGORY_OVERSTRUCK = POS_AFTER_MAIN,
175 INDIC_MATRA_CATEGORY_VISUAL_ORDER_LEFT = POS_PRE_M
176};
177
178#define INDIC_COMBINE_CATEGORIES(S,M) \
179 ( \
180 ASSERT_STATIC_EXPR_ZERO (S < 255 && M < 255) + \
181 ( S | \
182 ( \
183 ( \
184 S == INDIC_SYLLABIC_CATEGORY_CONSONANT_MEDIAL || \
185 S == INDIC_SYLLABIC_CATEGORY_GEMINATION_MARK || \
186 S == INDIC_SYLLABIC_CATEGORY_REGISTER_SHIFTER || \
187 S == INDIC_SYLLABIC_CATEGORY_CONSONANT_SUCCEEDING_REPHA || \
188 S == INDIC_SYLLABIC_CATEGORY_VIRAMA || \
189 S == INDIC_SYLLABIC_CATEGORY_VOWEL_DEPENDENT || \
190 false \
191 ? M : INDIC_MATRA_CATEGORY_NOT_APPLICABLE \
192 ) << 8 \
193 ) \
194 ) \
195 )
196
197HB_INTERNAL INDIC_TABLE_ELEMENT_TYPE
198hb_indic_get_categories (hb_codepoint_t u);
199
200
201static inline bool
202is_one_of (const hb_glyph_info_t &info, unsigned int flags)
203{
204 /* If it ligated, all bets are off. */
205 if (_hb_glyph_info_ligated (&info)) return false;
206 return !!(FLAG_UNSAFE (info.indic_category()) & flags);
207}
208
209static inline bool
210is_joiner (const hb_glyph_info_t &info)
211{
212 return is_one_of (info, JOINER_FLAGS);
213}
214
215static inline bool
216is_consonant (const hb_glyph_info_t &info)
217{
218 return is_one_of (info, CONSONANT_FLAGS);
219}
220
221static inline bool
222is_halant (const hb_glyph_info_t &info)
223{
224 return is_one_of (info, FLAG (OT_H));
225}
226
227#define IN_HALF_BLOCK(u, Base) (((u) & ~0x7Fu) == (Base))
228
229#define IS_DEVA(u) (IN_HALF_BLOCK (u, 0x0900u))
230#define IS_BENG(u) (IN_HALF_BLOCK (u, 0x0980u))
231#define IS_GURU(u) (IN_HALF_BLOCK (u, 0x0A00u))
232#define IS_GUJR(u) (IN_HALF_BLOCK (u, 0x0A80u))
233#define IS_ORYA(u) (IN_HALF_BLOCK (u, 0x0B00u))
234#define IS_TAML(u) (IN_HALF_BLOCK (u, 0x0B80u))
235#define IS_TELU(u) (IN_HALF_BLOCK (u, 0x0C00u))
236#define IS_KNDA(u) (IN_HALF_BLOCK (u, 0x0C80u))
237#define IS_MLYM(u) (IN_HALF_BLOCK (u, 0x0D00u))
238#define IS_SINH(u) (IN_HALF_BLOCK (u, 0x0D80u))
239
240
241#define MATRA_POS_LEFT(u) POS_PRE_M
242#define MATRA_POS_RIGHT(u) ( \
243 IS_DEVA(u) ? POS_AFTER_SUB : \
244 IS_BENG(u) ? POS_AFTER_POST : \
245 IS_GURU(u) ? POS_AFTER_POST : \
246 IS_GUJR(u) ? POS_AFTER_POST : \
247 IS_ORYA(u) ? POS_AFTER_POST : \
248 IS_TAML(u) ? POS_AFTER_POST : \
249 IS_TELU(u) ? (u <= 0x0C42u ? POS_BEFORE_SUB : POS_AFTER_SUB) : \
250 IS_KNDA(u) ? (u < 0x0CC3u || u > 0xCD6u ? POS_BEFORE_SUB : POS_AFTER_SUB) : \
251 IS_MLYM(u) ? POS_AFTER_POST : \
252 IS_SINH(u) ? POS_AFTER_SUB : \
253 /*default*/ POS_AFTER_SUB \
254 )
255#define MATRA_POS_TOP(u) ( /* BENG and MLYM don't have top matras. */ \
256 IS_DEVA(u) ? POS_AFTER_SUB : \
257 IS_GURU(u) ? POS_AFTER_POST : /* Deviate from spec */ \
258 IS_GUJR(u) ? POS_AFTER_SUB : \
259 IS_ORYA(u) ? POS_AFTER_MAIN : \
260 IS_TAML(u) ? POS_AFTER_SUB : \
261 IS_TELU(u) ? POS_BEFORE_SUB : \
262 IS_KNDA(u) ? POS_BEFORE_SUB : \
263 IS_SINH(u) ? POS_AFTER_SUB : \
264 /*default*/ POS_AFTER_SUB \
265 )
266#define MATRA_POS_BOTTOM(u) ( \
267 IS_DEVA(u) ? POS_AFTER_SUB : \
268 IS_BENG(u) ? POS_AFTER_SUB : \
269 IS_GURU(u) ? POS_AFTER_POST : \
270 IS_GUJR(u) ? POS_AFTER_POST : \
271 IS_ORYA(u) ? POS_AFTER_SUB : \
272 IS_TAML(u) ? POS_AFTER_POST : \
273 IS_TELU(u) ? POS_BEFORE_SUB : \
274 IS_KNDA(u) ? POS_BEFORE_SUB : \
275 IS_MLYM(u) ? POS_AFTER_POST : \
276 IS_SINH(u) ? POS_AFTER_SUB : \
277 /*default*/ POS_AFTER_SUB \
278 )
279
280static inline indic_position_t
281matra_position_indic (hb_codepoint_t u, indic_position_t side)
282{
283 switch ((int) side)
284 {
285 case POS_PRE_C: return MATRA_POS_LEFT (u);
286 case POS_POST_C: return MATRA_POS_RIGHT (u);
287 case POS_ABOVE_C: return MATRA_POS_TOP (u);
288 case POS_BELOW_C: return MATRA_POS_BOTTOM (u);
289 }
290 return side;
291}
292
293/* XXX
294 * This is a hack for now. We should move this data into the main Indic table.
295 * Or completely remove it and just check in the tables.
296 */
297static const hb_codepoint_t ra_chars[] = {
298 0x0930u, /* Devanagari */
299 0x09B0u, /* Bengali */
300 0x09F0u, /* Bengali */
301 0x0A30u, /* Gurmukhi */ /* No Reph */
302 0x0AB0u, /* Gujarati */
303 0x0B30u, /* Oriya */
304 0x0BB0u, /* Tamil */ /* No Reph */
305 0x0C30u, /* Telugu */ /* Reph formed only with ZWJ */
306 0x0CB0u, /* Kannada */
307 0x0D30u, /* Malayalam */ /* No Reph, Logical Repha */
308
309 0x0DBBu, /* Sinhala */ /* Reph formed only with ZWJ */
310
311 0x179Au, /* Khmer */
312};
313
314static inline bool
315is_ra (hb_codepoint_t u)
316{
317 for (unsigned int i = 0; i < ARRAY_LENGTH (ra_chars); i++)
318 if (u == ra_chars[i])
319 return true;
320 return false;
321}
322
323static inline void
324set_indic_properties (hb_glyph_info_t &info)
325{
326 hb_codepoint_t u = info.codepoint;
327 unsigned int type = hb_indic_get_categories (u);
328 indic_category_t cat = (indic_category_t) (type & 0x7Fu);
329 indic_position_t pos = (indic_position_t) (type >> 8);
330
331
332 /*
333 * Re-assign category
334 */
335
336 /* The following act more like the Bindus. */
337 if (unlikely (hb_in_range<hb_codepoint_t> (u, 0x0953u, 0x0954u)))
338 cat = OT_SM;
339 /* The following act like consonants. */
340 else if (unlikely (hb_in_ranges<hb_codepoint_t> (u, 0x0A72u, 0x0A73u,
341 0x1CF5u, 0x1CF6u)))
342 cat = OT_C;
343 /* TODO: The following should only be allowed after a Visarga.
344 * For now, just treat them like regular tone marks. */
345 else if (unlikely (hb_in_range<hb_codepoint_t> (u, 0x1CE2u, 0x1CE8u)))
346 cat = OT_A;
347 /* TODO: The following should only be allowed after some of
348 * the nasalization marks, maybe only for U+1CE9..U+1CF1.
349 * For now, just treat them like tone marks. */
350 else if (unlikely (u == 0x1CEDu))
351 cat = OT_A;
352 /* The following take marks in standalone clusters, similar to Avagraha. */
353 else if (unlikely (hb_in_ranges<hb_codepoint_t> (u, 0xA8F2u, 0xA8F7u,
354 0x1CE9u, 0x1CECu,
355 0x1CEEu, 0x1CF1u)))
356 {
357 cat = OT_Symbol;
358 static_assert (((int) INDIC_SYLLABIC_CATEGORY_AVAGRAHA == OT_Symbol), "");
359 }
360 else if (unlikely (u == 0x0A51u))
361 {
362 /* https://github.com/harfbuzz/harfbuzz/issues/524 */
363 cat = OT_M;
364 pos = POS_BELOW_C;
365 }
366
367 /* According to ScriptExtensions.txt, these Grantha marks may also be used in Tamil,
368 * so the Indic shaper needs to know their categories. */
369 else if (unlikely (u == 0x11301u || u == 0x11303u)) cat = OT_SM;
370 else if (unlikely (u == 0x1133Bu || u == 0x1133Cu)) cat = OT_N;
371
372 else if (unlikely (u == 0x0AFBu)) cat = OT_N; /* https://github.com/harfbuzz/harfbuzz/issues/552 */
373
374 else if (unlikely (u == 0x0980u)) cat = OT_PLACEHOLDER; /* https://github.com/harfbuzz/harfbuzz/issues/538 */
375 else if (unlikely (u == 0x09FCu)) cat = OT_PLACEHOLDER; /* https://github.com/harfbuzz/harfbuzz/pull/1613 */
376 else if (unlikely (u == 0x0C80u)) cat = OT_PLACEHOLDER; /* https://github.com/harfbuzz/harfbuzz/pull/623 */
377 else if (unlikely (hb_in_range<hb_codepoint_t> (u, 0x2010u, 0x2011u)))
378 cat = OT_PLACEHOLDER;
379 else if (unlikely (u == 0x25CCu)) cat = OT_DOTTEDCIRCLE;
380
381
382 /*
383 * Re-assign position.
384 */
385
386 if ((FLAG_UNSAFE (cat) & CONSONANT_FLAGS))
387 {
388 pos = POS_BASE_C;
389 if (is_ra (u))
390 cat = OT_Ra;
391 }
392 else if (cat == OT_M)
393 {
394 pos = matra_position_indic (u, pos);
395 }
396 else if ((FLAG_UNSAFE (cat) & (FLAG (OT_SM) /* | FLAG (OT_VD) */ | FLAG (OT_A) | FLAG (OT_Symbol))))
397 {
398 pos = POS_SMVD;
399 }
400
401 if (unlikely (u == 0x0B01u)) pos = POS_BEFORE_SUB; /* Oriya Bindu is BeforeSub in the spec. */
402
403
404
405 info.indic_category() = cat;
406 info.indic_position() = pos;
407}
408
409struct hb_indic_would_substitute_feature_t
410{
411 void init (const hb_ot_map_t *map, hb_tag_t feature_tag, bool zero_context_)
412 {
413 zero_context = zero_context_;
414 map->get_stage_lookups (0/*GSUB*/,
415 map->get_feature_stage (0/*GSUB*/, feature_tag),
416 &lookups, &count);
417 }
418
419 bool would_substitute (const hb_codepoint_t *glyphs,
420 unsigned int glyphs_count,
421 hb_face_t *face) const
422 {
423 for (unsigned int i = 0; i < count; i++)
424 if (hb_ot_layout_lookup_would_substitute (face, lookups[i].index, glyphs, glyphs_count, zero_context))
425 return true;
426 return false;
427 }
428
429 private:
430 const hb_ot_map_t::lookup_map_t *lookups;
431 unsigned int count;
432 bool zero_context;
433};
434
435
436#endif /* HB_OT_SHAPE_COMPLEX_INDIC_HH */
437