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