1/* 2 * Copyright © 2015 Mozilla Foundation. 3 * Copyright © 2015 Google, Inc. 4 * 5 * This is part of HarfBuzz, a text shaping library. 6 * 7 * Permission is hereby granted, without written agreement and without 8 * license or royalty fees, to use, copy, modify, and distribute this 9 * software and its documentation for any purpose, provided that the 10 * above copyright notice and the following two paragraphs appear in 11 * all copies of this software. 12 * 13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 17 * DAMAGE. 18 * 19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 24 * 25 * Mozilla Author(s): Jonathan Kew 26 * Google Author(s): Behdad Esfahbod 27 */ 28 29#ifndef HB_OT_SHAPER_USE_MACHINE_HH 30#define HB_OT_SHAPER_USE_MACHINE_HH 31 32#include "hb.hh" 33 34#include "hb-ot-shaper-syllabic.hh" 35 36/* buffer var allocations */ 37#define use_category() ot_shaper_var_u8_category() 38 39#define USE(Cat) use_syllable_machine_ex_##Cat 40 41enum use_syllable_type_t { 42 use_virama_terminated_cluster, 43 use_sakot_terminated_cluster, 44 use_standard_cluster, 45 use_number_joiner_terminated_cluster, 46 use_numeral_cluster, 47 use_symbol_cluster, 48 use_hieroglyph_cluster, 49 use_broken_cluster, 50 use_non_cluster, 51}; 52 53%%{ 54 machine use_syllable_machine; 55 alphtype unsigned char; 56 write exports; 57 write data; 58}%% 59 60%%{ 61 62# Categories used in the Universal Shaping Engine spec: 63# https://docs.microsoft.com/en-us/typography/script-development/use 64 65export O = 0; # OTHER 66 67export B = 1; # BASE 68export N = 4; # BASE_NUM 69export GB = 5; # BASE_OTHER 70export CGJ = 6; # CGJ 71export SUB = 11; # CONS_SUB 72export H = 12; # HALANT 73 74export HN = 13; # HALANT_NUM 75export ZWNJ = 14; # Zero width non-joiner 76export WJ = 16; # Word joiner 77export R = 18; # REPHA 78export CS = 43; # CONS_WITH_STACKER 79export IS = 44; # INVISIBLE_STACKER 80export Sk = 48; # SAKOT 81export G = 49; # HIEROGLYPH 82export J = 50; # HIEROGLYPH_JOINER 83export SB = 51; # HIEROGLYPH_SEGMENT_BEGIN 84export SE = 52; # HIEROGLYPH_SEGMENT_END 85export HVM = 53; # HALANT_OR_VOWEL_MODIFIER 86export HM = 54; # HIEROGLYPH_MOD 87export HR = 55; # HIEROGLYPH_MIRROR 88export RK = 56; # REORDERING_KILLER 89 90export FAbv = 24; # CONS_FINAL_ABOVE 91export FBlw = 25; # CONS_FINAL_BELOW 92export FPst = 26; # CONS_FINAL_POST 93export MAbv = 27; # CONS_MED_ABOVE 94export MBlw = 28; # CONS_MED_BELOW 95export MPst = 29; # CONS_MED_POST 96export MPre = 30; # CONS_MED_PRE 97export CMAbv = 31; # CONS_MOD_ABOVE 98export CMBlw = 32; # CONS_MOD_BELOW 99export VAbv = 33; # VOWEL_ABOVE / VOWEL_ABOVE_BELOW / VOWEL_ABOVE_BELOW_POST / VOWEL_ABOVE_POST 100export VBlw = 34; # VOWEL_BELOW / VOWEL_BELOW_POST 101export VPst = 35; # VOWEL_POST UIPC = Right 102export VPre = 22; # VOWEL_PRE / VOWEL_PRE_ABOVE / VOWEL_PRE_ABOVE_POST / VOWEL_PRE_POST 103export VMAbv = 37; # VOWEL_MOD_ABOVE 104export VMBlw = 38; # VOWEL_MOD_BELOW 105export VMPst = 39; # VOWEL_MOD_POST 106export VMPre = 23; # VOWEL_MOD_PRE 107export SMAbv = 41; # SYM_MOD_ABOVE 108export SMBlw = 42; # SYM_MOD_BELOW 109export FMAbv = 45; # CONS_FINAL_MOD UIPC = Top 110export FMBlw = 46; # CONS_FINAL_MOD UIPC = Bottom 111export FMPst = 47; # CONS_FINAL_MOD UIPC = Not_Applicable 112 113 114h = H | HVM | IS | Sk; 115 116consonant_modifiers = CMAbv* CMBlw* ((h B | SUB) CMAbv* CMBlw*)*; 117medial_consonants = MPre? MAbv? MBlw? MPst?; 118dependent_vowels = VPre* VAbv* VBlw* VPst* | H; 119vowel_modifiers = HVM? VMPre* VMAbv* VMBlw* VMPst*; 120final_consonants = FAbv* FBlw* FPst*; 121final_modifiers = FMAbv* FMBlw* | FMPst?; 122 123complex_syllable_start = (R | CS)? (B | GB); 124complex_syllable_middle = 125 consonant_modifiers 126 medial_consonants 127 dependent_vowels 128 vowel_modifiers 129 (Sk B)* 130; 131complex_syllable_tail = 132 complex_syllable_middle 133 final_consonants 134 final_modifiers 135; 136number_joiner_terminated_cluster_tail = (HN N)* HN; 137numeral_cluster_tail = (HN N)+; 138symbol_cluster_tail = SMAbv+ SMBlw* | SMBlw+; 139 140virama_terminated_cluster_tail = 141 consonant_modifiers 142 (IS | RK) 143; 144virama_terminated_cluster = 145 complex_syllable_start 146 virama_terminated_cluster_tail 147; 148sakot_terminated_cluster_tail = 149 complex_syllable_middle 150 Sk 151; 152sakot_terminated_cluster = 153 complex_syllable_start 154 sakot_terminated_cluster_tail 155; 156standard_cluster = 157 complex_syllable_start 158 complex_syllable_tail 159; 160tail = complex_syllable_tail | sakot_terminated_cluster_tail | symbol_cluster_tail | virama_terminated_cluster_tail; 161broken_cluster = 162 R? 163 (tail | number_joiner_terminated_cluster_tail | numeral_cluster_tail) 164; 165 166number_joiner_terminated_cluster = N number_joiner_terminated_cluster_tail; 167numeral_cluster = N numeral_cluster_tail?; 168symbol_cluster = (O | GB | SB) tail?; 169hieroglyph_cluster = SB* G HR? HM? SE* (J SB* (G HR? HM? SE*)?)*; 170other = any; 171 172main := |* 173 virama_terminated_cluster ZWNJ? => { found_syllable (use_virama_terminated_cluster); }; 174 sakot_terminated_cluster ZWNJ? => { found_syllable (use_sakot_terminated_cluster); }; 175 standard_cluster ZWNJ? => { found_syllable (use_standard_cluster); }; 176 number_joiner_terminated_cluster ZWNJ? => { found_syllable (use_number_joiner_terminated_cluster); }; 177 numeral_cluster ZWNJ? => { found_syllable (use_numeral_cluster); }; 178 symbol_cluster ZWNJ? => { found_syllable (use_symbol_cluster); }; 179 hieroglyph_cluster ZWNJ? => { found_syllable (use_hieroglyph_cluster); }; 180 broken_cluster ZWNJ? => { found_syllable (use_broken_cluster); buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_BROKEN_SYLLABLE; }; 181 other => { found_syllable (use_non_cluster); }; 182*|; 183 184 185}%% 186 187#define found_syllable(syllable_type) \ 188 HB_STMT_START { \ 189 if (0) fprintf (stderr, "syllable %u..%u %s\n", (*ts).second.first, (*te).second.first, #syllable_type); \ 190 for (unsigned i = (*ts).second.first; i < (*te).second.first; ++i) \ 191 info[i].syllable() = (syllable_serial << 4) | syllable_type; \ 192 syllable_serial++; \ 193 if (syllable_serial == 16) syllable_serial = 1; \ 194 } HB_STMT_END 195 196 197template <typename Iter> 198struct machine_index_t : 199 hb_iter_with_fallback_t<machine_index_t<Iter>, 200 typename Iter::item_t> 201{ 202 machine_index_t (const Iter& it) : it (it) {} 203 machine_index_t (const machine_index_t& o) : hb_iter_with_fallback_t<machine_index_t<Iter>, 204 typename Iter::item_t> (), 205 it (o.it), is_null (o.is_null) {} 206 207 static constexpr bool is_random_access_iterator = Iter::is_random_access_iterator; 208 static constexpr bool is_sorted_iterator = Iter::is_sorted_iterator; 209 210 typename Iter::item_t __item__ () const { return *it; } 211 typename Iter::item_t __item_at__ (unsigned i) const { return it[i]; } 212 unsigned __len__ () const { return it.len (); } 213 void __next__ () { ++it; } 214 void __forward__ (unsigned n) { it += n; } 215 void __prev__ () { --it; } 216 void __rewind__ (unsigned n) { it -= n; } 217 218 void operator = (unsigned n) 219 { 220 assert (n == 0); 221 is_null = true; 222 } 223 explicit operator bool () { return !is_null; } 224 225 void operator = (const machine_index_t& o) 226 { 227 is_null = o.is_null; 228 unsigned index = (*it).first; 229 unsigned n = (*o.it).first; 230 if (index < n) it += n - index; else if (index > n) it -= index - n; 231 } 232 bool operator == (const machine_index_t& o) const 233 { return is_null ? o.is_null : !o.is_null && (*it).first == (*o.it).first; } 234 bool operator != (const machine_index_t& o) const { return !(*this == o); } 235 236 private: 237 Iter it; 238 bool is_null = false; 239}; 240struct 241{ 242 template <typename Iter, 243 hb_requires (hb_is_iterable (Iter))> 244 machine_index_t<hb_iter_type<Iter>> 245 operator () (Iter&& it) const 246 { return machine_index_t<hb_iter_type<Iter>> (hb_iter (it)); } 247} 248HB_FUNCOBJ (machine_index); 249 250 251 252static bool 253not_ccs_default_ignorable (const hb_glyph_info_t &i) 254{ return i.use_category() != USE(CGJ); } 255 256static inline void 257find_syllables_use (hb_buffer_t *buffer) 258{ 259 hb_glyph_info_t *info = buffer->info; 260 auto p = 261 + hb_iter (info, buffer->len) 262 | hb_enumerate 263 | hb_filter ([] (const hb_glyph_info_t &i) { return not_ccs_default_ignorable (i); }, 264 hb_second) 265 | hb_filter ([&] (const hb_pair_t<unsigned, const hb_glyph_info_t &> p) 266 { 267 if (p.second.use_category() == USE(ZWNJ)) 268 for (unsigned i = p.first + 1; i < buffer->len; ++i) 269 if (not_ccs_default_ignorable (info[i])) 270 return !_hb_glyph_info_is_unicode_mark (&info[i]); 271 return true; 272 }) 273 | hb_enumerate 274 | machine_index 275 ; 276 auto pe = p + p.len (); 277 auto eof = +pe; 278 auto ts = +p; 279 auto te = +p; 280 unsigned int act HB_UNUSED; 281 int cs; 282 %%{ 283 write init; 284 getkey (*p).second.second.use_category(); 285 }%% 286 287 unsigned int syllable_serial = 1; 288 %%{ 289 write exec; 290 }%% 291} 292 293#undef found_syllable 294 295#endif /* HB_OT_SHAPER_USE_MACHINE_HH */ 296