1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 // This file is available under and governed by the GNU General Public 26 // License version 2 only, as published by the Free Software Foundation. 27 // However, the following notice accompanied the original version of this 28 // file: 29 // 30 /* 31 * Copyright © 2010,2012 Google, Inc. 32 * 33 * This is part of HarfBuzz, a text shaping library. 34 * 35 * Permission is hereby granted, without written agreement and without 36 * license or royalty fees, to use, copy, modify, and distribute this 37 * software and its documentation for any purpose, provided that the 38 * above copyright notice and the following two paragraphs appear in 39 * all copies of this software. 40 * 41 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 42 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 43 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 44 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 45 * DAMAGE. 46 * 47 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 48 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 49 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 50 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 51 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 52 * 53 * Google Author(s): Behdad Esfahbod 54 */ 55 56 #include "hb-ot-shape-complex-private.hh" 57 58 59 /* Thai / Lao shaper */ 60 61 62 /* PUA shaping */ 63 64 65 enum thai_consonant_type_t 66 { 67 NC, 68 AC, 69 RC, 70 DC, 71 NOT_CONSONANT, 72 NUM_CONSONANT_TYPES = NOT_CONSONANT 73 }; 74 75 static thai_consonant_type_t 76 get_consonant_type (hb_codepoint_t u) 77 { 78 if (u == 0x0E1Bu || u == 0x0E1Du || u == 0x0E1Fu/* || u == 0x0E2Cu*/) 79 return AC; 80 if (u == 0x0E0Du || u == 0x0E10u) 81 return RC; 82 if (u == 0x0E0Eu || u == 0x0E0Fu) 83 return DC; 84 if (hb_in_range (u, 0x0E01u, 0x0E2Eu)) 85 return NC; 86 return NOT_CONSONANT; 87 } 88 89 90 enum thai_mark_type_t 91 { 92 AV, 93 BV, 94 T, 95 NOT_MARK, 96 NUM_MARK_TYPES = NOT_MARK 97 }; 98 99 static thai_mark_type_t 100 get_mark_type (hb_codepoint_t u) 101 { 102 if (u == 0x0E31u || hb_in_range (u, 0x0E34u, 0x0E37u) || 103 u == 0x0E47u || hb_in_range (u, 0x0E4Du, 0x0E4Eu)) 104 return AV; 105 if (hb_in_range (u, 0x0E38u, 0x0E3Au)) 106 return BV; 107 if (hb_in_range (u, 0x0E48u, 0x0E4Cu)) 108 return T; 109 return NOT_MARK; 110 } 111 112 113 enum thai_action_t 114 { 115 NOP, 116 SD, /* Shift combining-mark down */ 117 SL, /* Shift combining-mark left */ 118 SDL, /* Shift combining-mark down-left */ 119 RD /* Remove descender from base */ 120 }; 121 122 static hb_codepoint_t 123 thai_pua_shape (hb_codepoint_t u, thai_action_t action, hb_font_t *font) 124 { 125 struct thai_pua_mapping_t { 126 hb_codepoint_t u; 127 hb_codepoint_t win_pua; 128 hb_codepoint_t mac_pua; 129 } const *pua_mappings = NULL; 130 static const thai_pua_mapping_t SD_mappings[] = { 131 {0x0E48u, 0xF70Au, 0xF88Bu}, /* MAI EK */ 132 {0x0E49u, 0xF70Bu, 0xF88Eu}, /* MAI THO */ 133 {0x0E4Au, 0xF70Cu, 0xF891u}, /* MAI TRI */ 134 {0x0E4Bu, 0xF70Du, 0xF894u}, /* MAI CHATTAWA */ 135 {0x0E4Cu, 0xF70Eu, 0xF897u}, /* THANTHAKHAT */ 136 {0x0E38u, 0xF718u, 0xF89Bu}, /* SARA U */ 137 {0x0E39u, 0xF719u, 0xF89Cu}, /* SARA UU */ 138 {0x0E3Au, 0xF71Au, 0xF89Du}, /* PHINTHU */ 139 {0x0000u, 0x0000u, 0x0000u} 140 }; 141 static const thai_pua_mapping_t SDL_mappings[] = { 142 {0x0E48u, 0xF705u, 0xF88Cu}, /* MAI EK */ 143 {0x0E49u, 0xF706u, 0xF88Fu}, /* MAI THO */ 144 {0x0E4Au, 0xF707u, 0xF892u}, /* MAI TRI */ 145 {0x0E4Bu, 0xF708u, 0xF895u}, /* MAI CHATTAWA */ 146 {0x0E4Cu, 0xF709u, 0xF898u}, /* THANTHAKHAT */ 147 {0x0000u, 0x0000u, 0x0000u} 148 }; 149 static const thai_pua_mapping_t SL_mappings[] = { 150 {0x0E48u, 0xF713u, 0xF88Au}, /* MAI EK */ 151 {0x0E49u, 0xF714u, 0xF88Du}, /* MAI THO */ 152 {0x0E4Au, 0xF715u, 0xF890u}, /* MAI TRI */ 153 {0x0E4Bu, 0xF716u, 0xF893u}, /* MAI CHATTAWA */ 154 {0x0E4Cu, 0xF717u, 0xF896u}, /* THANTHAKHAT */ 155 {0x0E31u, 0xF710u, 0xF884u}, /* MAI HAN-AKAT */ 156 {0x0E34u, 0xF701u, 0xF885u}, /* SARA I */ 157 {0x0E35u, 0xF702u, 0xF886u}, /* SARA II */ 158 {0x0E36u, 0xF703u, 0xF887u}, /* SARA UE */ 159 {0x0E37u, 0xF704u, 0xF888u}, /* SARA UEE */ 160 {0x0E47u, 0xF712u, 0xF889u}, /* MAITAIKHU */ 161 {0x0E4Du, 0xF711u, 0xF899u}, /* NIKHAHIT */ 162 {0x0000u, 0x0000u, 0x0000u} 163 }; 164 static const thai_pua_mapping_t RD_mappings[] = { 165 {0x0E0Du, 0xF70Fu, 0xF89Au}, /* YO YING */ 166 {0x0E10u, 0xF700u, 0xF89Eu}, /* THO THAN */ 167 {0x0000u, 0x0000u, 0x0000u} 168 }; 169 170 switch (action) { 171 default: assert (false); /* Fallthrough */ 172 case NOP: return u; 173 case SD: pua_mappings = SD_mappings; break; 174 case SDL: pua_mappings = SDL_mappings; break; 175 case SL: pua_mappings = SL_mappings; break; 176 case RD: pua_mappings = RD_mappings; break; 177 } 178 for (; pua_mappings->u; pua_mappings++) 179 if (pua_mappings->u == u) 180 { 181 hb_codepoint_t glyph; 182 if (hb_font_get_glyph (font, pua_mappings->win_pua, 0, &glyph)) 183 return pua_mappings->win_pua; 184 if (hb_font_get_glyph (font, pua_mappings->mac_pua, 0, &glyph)) 185 return pua_mappings->mac_pua; 186 break; 187 } 188 return u; 189 } 190 191 192 static enum thai_above_state_t 193 { /* Cluster above looks like: */ 194 T0, /* ⣤ */ 195 T1, /* ⣼ */ 196 T2, /* ⣾ */ 197 T3, /* ⣿ */ 198 NUM_ABOVE_STATES 199 } thai_above_start_state[NUM_CONSONANT_TYPES + 1/* For NOT_CONSONANT */] = 200 { 201 T0, /* NC */ 202 T1, /* AC */ 203 T0, /* RC */ 204 T0, /* DC */ 205 T3, /* NOT_CONSONANT */ 206 }; 207 208 static const struct thai_above_state_machine_edge_t { 209 thai_action_t action; 210 thai_above_state_t next_state; 211 } thai_above_state_machine[NUM_ABOVE_STATES][NUM_MARK_TYPES] = 212 { /*AV*/ /*BV*/ /*T*/ 213 /*T0*/ {{NOP,T3}, {NOP,T0}, {SD, T3}}, 214 /*T1*/ {{SL, T2}, {NOP,T1}, {SDL,T2}}, 215 /*T2*/ {{NOP,T3}, {NOP,T2}, {SL, T3}}, 216 /*T3*/ {{NOP,T3}, {NOP,T3}, {NOP,T3}}, 217 }; 218 219 220 static enum thai_below_state_t 221 { 222 B0, /* No descender */ 223 B1, /* Removable descender */ 224 B2, /* Strict descender */ 225 NUM_BELOW_STATES 226 } thai_below_start_state[NUM_CONSONANT_TYPES + 1/* For NOT_CONSONANT */] = 227 { 228 B0, /* NC */ 229 B0, /* AC */ 230 B1, /* RC */ 231 B2, /* DC */ 232 B2, /* NOT_CONSONANT */ 233 }; 234 235 static const struct thai_below_state_machine_edge_t { 236 thai_action_t action; 237 thai_below_state_t next_state; 238 } thai_below_state_machine[NUM_BELOW_STATES][NUM_MARK_TYPES] = 239 { /*AV*/ /*BV*/ /*T*/ 240 /*B0*/ {{NOP,B0}, {NOP,B2}, {NOP, B0}}, 241 /*B1*/ {{NOP,B1}, {RD, B2}, {NOP, B1}}, 242 /*B2*/ {{NOP,B2}, {SD, B2}, {NOP, B2}}, 243 }; 244 245 246 static void 247 do_thai_pua_shaping (const hb_ot_shape_plan_t *plan HB_UNUSED, 248 hb_buffer_t *buffer, 249 hb_font_t *font) 250 { 251 thai_above_state_t above_state = thai_above_start_state[NOT_CONSONANT]; 252 thai_below_state_t below_state = thai_below_start_state[NOT_CONSONANT]; 253 unsigned int base = 0; 254 255 hb_glyph_info_t *info = buffer->info; 256 unsigned int count = buffer->len; 257 for (unsigned int i = 0; i < count; i++) 258 { 259 thai_mark_type_t mt = get_mark_type (info[i].codepoint); 260 261 if (mt == NOT_MARK) { 262 thai_consonant_type_t ct = get_consonant_type (info[i].codepoint); 263 above_state = thai_above_start_state[ct]; 264 below_state = thai_below_start_state[ct]; 265 base = i; 266 continue; 267 } 268 269 const thai_above_state_machine_edge_t &above_edge = thai_above_state_machine[above_state][mt]; 270 const thai_below_state_machine_edge_t &below_edge = thai_below_state_machine[below_state][mt]; 271 above_state = above_edge.next_state; 272 below_state = below_edge.next_state; 273 274 /* At least one of the above/below actions is NOP. */ 275 thai_action_t action = above_edge.action != NOP ? above_edge.action : below_edge.action; 276 277 if (action == RD) 278 info[base].codepoint = thai_pua_shape (info[base].codepoint, action, font); 279 else 280 info[i].codepoint = thai_pua_shape (info[i].codepoint, action, font); 281 } 282 } 283 284 285 static void 286 preprocess_text_thai (const hb_ot_shape_plan_t *plan, 287 hb_buffer_t *buffer, 288 hb_font_t *font) 289 { 290 /* This function implements the shaping logic documented here: 291 * 292 * http://linux.thai.net/~thep/th-otf/shaping.html 293 * 294 * The first shaping rule listed there is needed even if the font has Thai 295 * OpenType tables. The rest do fallback positioning based on PUA codepoints. 296 * We implement that only if there exist no Thai GSUB in the font. 297 */ 298 299 /* The following is NOT specified in the MS OT Thai spec, however, it seems 300 * to be what Uniscribe and other engines implement. According to Eric Muller: 301 * 302 * When you have a SARA AM, decompose it in NIKHAHIT + SARA AA, *and* move the 303 * NIKHAHIT backwards over any tone mark (0E48-0E4B). 304 * 305 * <0E14, 0E4B, 0E33> -> <0E14, 0E4D, 0E4B, 0E32> 306 * 307 * This reordering is legit only when the NIKHAHIT comes from a SARA AM, not 308 * when it's there to start with. The string <0E14, 0E4B, 0E4D> is probably 309 * not what a user wanted, but the rendering is nevertheless nikhahit above 310 * chattawa. 311 * 312 * Same for Lao. 313 * 314 * Note: 315 * 316 * Uniscribe also does some below-marks reordering. Namely, it positions U+0E3A 317 * after U+0E38 and U+0E39. We do that by modifying the ccc for U+0E3A. 318 * See unicode->modified_combining_class (). Lao does NOT have a U+0E3A 319 * equivalent. 320 */ 321 322 323 /* 324 * Here are the characters of significance: 325 * 326 * Thai Lao 327 * SARA AM: U+0E33 U+0EB3 328 * SARA AA: U+0E32 U+0EB2 329 * Nikhahit: U+0E4D U+0ECD 330 * 331 * Testing shows that Uniscribe reorder the following marks: 332 * Thai: <0E31,0E34..0E37,0E47..0E4E> 333 * Lao: <0EB1,0EB4..0EB7,0EC7..0ECE> 334 * 335 * Note how the Lao versions are the same as Thai + 0x80. 336 */ 337 338 /* We only get one script at a time, so a script-agnostic implementation 339 * is adequate here. */ 340 #define IS_SARA_AM(x) (((x) & ~0x0080u) == 0x0E33u) 341 #define NIKHAHIT_FROM_SARA_AM(x) ((x) - 0x0E33u + 0x0E4Du) 342 #define SARA_AA_FROM_SARA_AM(x) ((x) - 1) 343 #define IS_TONE_MARK(x) (hb_in_ranges ((x) & ~0x0080u, 0x0E34u, 0x0E37u, 0x0E47u, 0x0E4Eu, 0x0E31u, 0x0E31u)) 344 345 buffer->clear_output (); 346 unsigned int count = buffer->len; 347 for (buffer->idx = 0; buffer->idx < count;) 348 { 349 hb_codepoint_t u = buffer->cur().codepoint; 350 if (likely (!IS_SARA_AM (u))) { 351 buffer->next_glyph (); 352 continue; 353 } 354 355 /* Is SARA AM. Decompose and reorder. */ 356 hb_codepoint_t decomposed[2] = {hb_codepoint_t (NIKHAHIT_FROM_SARA_AM (u)), 357 hb_codepoint_t (SARA_AA_FROM_SARA_AM (u))}; 358 buffer->replace_glyphs (1, 2, decomposed); 359 if (unlikely (buffer->in_error)) 360 return; 361 362 /* Make Nikhahit be recognized as a mark when zeroing widths. */ 363 unsigned int end = buffer->out_len; 364 _hb_glyph_info_set_general_category (&buffer->out_info[end - 2], HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK); 365 366 /* Ok, let's see... */ 367 unsigned int start = end - 2; 368 while (start > 0 && IS_TONE_MARK (buffer->out_info[start - 1].codepoint)) 369 start--; 370 371 if (start + 2 < end) 372 { 373 /* Move Nikhahit (end-2) to the beginning */ 374 buffer->merge_out_clusters (start, end); 375 hb_glyph_info_t t = buffer->out_info[end - 2]; 376 memmove (buffer->out_info + start + 1, 377 buffer->out_info + start, 378 sizeof (buffer->out_info[0]) * (end - start - 2)); 379 buffer->out_info[start] = t; 380 } 381 else 382 { 383 /* Since we decomposed, and NIKHAHIT is combining, merge clusters with the 384 * previous cluster. */ 385 if (start && buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES) 386 buffer->merge_out_clusters (start - 1, end); 387 } 388 } 389 buffer->swap_buffers (); 390 391 /* If font has Thai GSUB, we are done. */ 392 if (plan->props.script == HB_SCRIPT_THAI && !plan->map.found_script[0]) 393 do_thai_pua_shaping (plan, buffer, font); 394 } 395 396 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_thai = 397 { 398 "thai", 399 NULL, /* collect_features */ 400 NULL, /* override_features */ 401 NULL, /* data_create */ 402 NULL, /* data_destroy */ 403 preprocess_text_thai, 404 HB_OT_SHAPE_NORMALIZATION_MODE_DEFAULT, 405 NULL, /* decompose */ 406 NULL, /* compose */ 407 NULL, /* setup_masks */ 408 HB_OT_SHAPE_ZERO_WIDTH_MARKS_DEFAULT, 409 false,/* fallback_position */ 410 };