1/*
2 * Copyright © 2011,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_MACHINE_HH
28#define HB_OT_SHAPE_COMPLEX_INDIC_MACHINE_HH
29
30#include "hb.hh"
31
32enum indic_syllable_type_t {
33  indic_consonant_syllable,
34  indic_vowel_syllable,
35  indic_standalone_cluster,
36  indic_symbol_cluster,
37  indic_broken_cluster,
38  indic_non_indic_cluster,
39};
40
41%%{
42  machine indic_syllable_machine;
43  alphtype unsigned char;
44  write exports;
45  write data;
46}%%
47
48%%{
49
50export C    = 1;
51export V    = 2;
52export N    = 3;
53export H    = 4;
54export ZWNJ = 5;
55export ZWJ  = 6;
56export M    = 7;
57export SM   = 8;
58export A    = 10;
59export PLACEHOLDER = 11;
60export DOTTEDCIRCLE = 12;
61export RS    = 13;
62export Repha = 15;
63export Ra    = 16;
64export CM    = 17;
65export Symbol= 18;
66export CS    = 19;
67
68c = (C | Ra);			# is_consonant
69n = ((ZWNJ?.RS)? (N.N?)?);	# is_consonant_modifier
70z = ZWJ|ZWNJ;			# is_joiner
71reph = (Ra H | Repha);		# possible reph
72
73cn = c.ZWJ?.n?;
74forced_rakar = ZWJ H ZWJ Ra;
75symbol = Symbol.N?;
76matra_group = z*.M.N?.(H | forced_rakar)?;
77syllable_tail = (z?.SM.SM?.ZWNJ?)? A*;
78halant_group = (z?.H.(ZWJ.N?)?);
79final_halant_group = halant_group | H.ZWNJ;
80medial_group = CM?;
81halant_or_matra_group = (final_halant_group | matra_group*);
82
83complex_syllable_tail = (halant_group.cn)* medial_group halant_or_matra_group syllable_tail;
84
85consonant_syllable =	(Repha|CS)? cn complex_syllable_tail;
86vowel_syllable =	reph? V.n? (ZWJ | complex_syllable_tail);
87standalone_cluster =	((Repha|CS)? PLACEHOLDER | reph? DOTTEDCIRCLE).n? complex_syllable_tail;
88symbol_cluster =	symbol syllable_tail;
89broken_cluster =	reph? n? complex_syllable_tail;
90other =			any;
91
92main := |*
93	consonant_syllable	=> { found_syllable (indic_consonant_syllable); };
94	vowel_syllable		=> { found_syllable (indic_vowel_syllable); };
95	standalone_cluster	=> { found_syllable (indic_standalone_cluster); };
96	symbol_cluster		=> { found_syllable (indic_symbol_cluster); };
97	broken_cluster		=> { found_syllable (indic_broken_cluster); };
98	other			=> { found_syllable (indic_non_indic_cluster); };
99*|;
100
101
102}%%
103
104#define found_syllable(syllable_type) \
105  HB_STMT_START { \
106    if (0) fprintf (stderr, "syllable %d..%d %s\n", ts, te, #syllable_type); \
107    for (unsigned int i = ts; i < te; i++) \
108      info[i].syllable() = (syllable_serial << 4) | syllable_type; \
109    syllable_serial++; \
110    if (unlikely (syllable_serial == 16)) syllable_serial = 1; \
111  } HB_STMT_END
112
113static void
114find_syllables_indic (hb_buffer_t *buffer)
115{
116  unsigned int p, pe, eof, ts, te, act;
117  int cs;
118  hb_glyph_info_t *info = buffer->info;
119  %%{
120    write init;
121    getkey info[p].indic_category();
122  }%%
123
124  p = 0;
125  pe = eof = buffer->len;
126
127  unsigned int syllable_serial = 1;
128  %%{
129    write exec;
130  }%%
131}
132
133#undef found_syllable
134
135#endif /* HB_OT_SHAPE_COMPLEX_INDIC_MACHINE_HH */
136