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