gfx/harfbuzz/src/hb-ot-shape-complex-thai.cc

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /*
michael@0 2 * Copyright © 2010,2012 Google, Inc.
michael@0 3 *
michael@0 4 * This is part of HarfBuzz, a text shaping library.
michael@0 5 *
michael@0 6 * Permission is hereby granted, without written agreement and without
michael@0 7 * license or royalty fees, to use, copy, modify, and distribute this
michael@0 8 * software and its documentation for any purpose, provided that the
michael@0 9 * above copyright notice and the following two paragraphs appear in
michael@0 10 * all copies of this software.
michael@0 11 *
michael@0 12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
michael@0 13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
michael@0 14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
michael@0 15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
michael@0 16 * DAMAGE.
michael@0 17 *
michael@0 18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
michael@0 19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
michael@0 20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
michael@0 21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
michael@0 22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
michael@0 23 *
michael@0 24 * Google Author(s): Behdad Esfahbod
michael@0 25 */
michael@0 26
michael@0 27 #include "hb-ot-shape-complex-private.hh"
michael@0 28
michael@0 29
michael@0 30 /* Thai / Lao shaper */
michael@0 31
michael@0 32
michael@0 33 /* PUA shaping */
michael@0 34
michael@0 35
michael@0 36 enum thai_consonant_type_t
michael@0 37 {
michael@0 38 NC,
michael@0 39 AC,
michael@0 40 RC,
michael@0 41 DC,
michael@0 42 NOT_CONSONANT,
michael@0 43 NUM_CONSONANT_TYPES = NOT_CONSONANT
michael@0 44 };
michael@0 45
michael@0 46 static thai_consonant_type_t
michael@0 47 get_consonant_type (hb_codepoint_t u)
michael@0 48 {
michael@0 49 if (u == 0x0E1B || u == 0x0E1D || u == 0x0E1F/* || u == 0x0E2C*/)
michael@0 50 return AC;
michael@0 51 if (u == 0x0E0D || u == 0x0E10)
michael@0 52 return RC;
michael@0 53 if (u == 0x0E0E || u == 0x0E0F)
michael@0 54 return DC;
michael@0 55 if (hb_in_range<hb_codepoint_t> (u, 0x0E01, 0x0E2E))
michael@0 56 return NC;
michael@0 57 return NOT_CONSONANT;
michael@0 58 }
michael@0 59
michael@0 60
michael@0 61 enum thai_mark_type_t
michael@0 62 {
michael@0 63 AV,
michael@0 64 BV,
michael@0 65 T,
michael@0 66 NOT_MARK,
michael@0 67 NUM_MARK_TYPES = NOT_MARK
michael@0 68 };
michael@0 69
michael@0 70 static thai_mark_type_t
michael@0 71 get_mark_type (hb_codepoint_t u)
michael@0 72 {
michael@0 73 if (u == 0x0E31 || hb_in_range<hb_codepoint_t> (u, 0x0E34, 0x0E37) ||
michael@0 74 u == 0x0E47 || hb_in_range<hb_codepoint_t> (u, 0x0E4D, 0x0E4E))
michael@0 75 return AV;
michael@0 76 if (hb_in_range<hb_codepoint_t> (u, 0x0E38, 0x0E3A))
michael@0 77 return BV;
michael@0 78 if (hb_in_range<hb_codepoint_t> (u, 0x0E48, 0x0E4C))
michael@0 79 return T;
michael@0 80 return NOT_MARK;
michael@0 81 }
michael@0 82
michael@0 83
michael@0 84 enum thai_action_t
michael@0 85 {
michael@0 86 NOP,
michael@0 87 SD, /* Shift combining-mark down */
michael@0 88 SL, /* Shift combining-mark left */
michael@0 89 SDL, /* Shift combining-mark down-left */
michael@0 90 RD /* Remove descender from base */
michael@0 91 };
michael@0 92
michael@0 93 static hb_codepoint_t
michael@0 94 thai_pua_shape (hb_codepoint_t u, thai_action_t action, hb_font_t *font)
michael@0 95 {
michael@0 96 struct thai_pua_mapping_t {
michael@0 97 hb_codepoint_t u;
michael@0 98 hb_codepoint_t win_pua;
michael@0 99 hb_codepoint_t mac_pua;
michael@0 100 } const *pua_mappings = NULL;
michael@0 101 static const thai_pua_mapping_t SD_mappings[] = {
michael@0 102 {0x0E48, 0xF70A, 0xF88B}, /* MAI EK */
michael@0 103 {0x0E49, 0xF70B, 0xF88E}, /* MAI THO */
michael@0 104 {0x0E4A, 0xF70C, 0xF891}, /* MAI TRI */
michael@0 105 {0x0E4B, 0xF70D, 0xF894}, /* MAI CHATTAWA */
michael@0 106 {0x0E4C, 0xF70E, 0xF897}, /* THANTHAKHAT */
michael@0 107 {0x0E38, 0xF718, 0xF89B}, /* SARA U */
michael@0 108 {0x0E39, 0xF719, 0xF89C}, /* SARA UU */
michael@0 109 {0x0E3A, 0xF71A, 0xF89D}, /* PHINTHU */
michael@0 110 {0x0000, 0x0000, 0x0000}
michael@0 111 };
michael@0 112 static const thai_pua_mapping_t SDL_mappings[] = {
michael@0 113 {0x0E48, 0xF705, 0xF88C}, /* MAI EK */
michael@0 114 {0x0E49, 0xF706, 0xF88F}, /* MAI THO */
michael@0 115 {0x0E4A, 0xF707, 0xF892}, /* MAI TRI */
michael@0 116 {0x0E4B, 0xF708, 0xF895}, /* MAI CHATTAWA */
michael@0 117 {0x0E4C, 0xF709, 0xF898}, /* THANTHAKHAT */
michael@0 118 {0x0000, 0x0000, 0x0000}
michael@0 119 };
michael@0 120 static const thai_pua_mapping_t SL_mappings[] = {
michael@0 121 {0x0E48, 0xF713, 0xF88A}, /* MAI EK */
michael@0 122 {0x0E49, 0xF714, 0xF88D}, /* MAI THO */
michael@0 123 {0x0E4A, 0xF715, 0xF890}, /* MAI TRI */
michael@0 124 {0x0E4B, 0xF716, 0xF893}, /* MAI CHATTAWA */
michael@0 125 {0x0E4C, 0xF717, 0xF896}, /* THANTHAKHAT */
michael@0 126 {0x0E31, 0xF710, 0xF884}, /* MAI HAN-AKAT */
michael@0 127 {0x0E34, 0xF701, 0xF885}, /* SARA I */
michael@0 128 {0x0E35, 0xF702, 0xF886}, /* SARA II */
michael@0 129 {0x0E36, 0xF703, 0xF887}, /* SARA UE */
michael@0 130 {0x0E37, 0xF704, 0xF888}, /* SARA UEE */
michael@0 131 {0x0E47, 0xF712, 0xF889}, /* MAITAIKHU */
michael@0 132 {0x0E4D, 0xF711, 0xF899}, /* NIKHAHIT */
michael@0 133 {0x0000, 0x0000, 0x0000}
michael@0 134 };
michael@0 135 static const thai_pua_mapping_t RD_mappings[] = {
michael@0 136 {0x0E0D, 0xF70F, 0xF89A}, /* YO YING */
michael@0 137 {0x0E10, 0xF700, 0xF89E}, /* THO THAN */
michael@0 138 {0x0000, 0x0000, 0x0000}
michael@0 139 };
michael@0 140
michael@0 141 switch (action) {
michael@0 142 default: assert (false); /* Fallthrough */
michael@0 143 case NOP: return u;
michael@0 144 case SD: pua_mappings = SD_mappings; break;
michael@0 145 case SDL: pua_mappings = SDL_mappings; break;
michael@0 146 case SL: pua_mappings = SL_mappings; break;
michael@0 147 case RD: pua_mappings = RD_mappings; break;
michael@0 148 }
michael@0 149 for (; pua_mappings->u; pua_mappings++)
michael@0 150 if (pua_mappings->u == u)
michael@0 151 {
michael@0 152 hb_codepoint_t glyph;
michael@0 153 if (hb_font_get_glyph (font, pua_mappings->win_pua, 0, &glyph))
michael@0 154 return pua_mappings->win_pua;
michael@0 155 if (hb_font_get_glyph (font, pua_mappings->mac_pua, 0, &glyph))
michael@0 156 return pua_mappings->mac_pua;
michael@0 157 break;
michael@0 158 }
michael@0 159 return u;
michael@0 160 }
michael@0 161
michael@0 162
michael@0 163 static enum thai_above_state_t
michael@0 164 { /* Cluster above looks like: */
michael@0 165 T0, /* ⣤ */
michael@0 166 T1, /* ⣼ */
michael@0 167 T2, /* ⣾ */
michael@0 168 T3, /* ⣿ */
michael@0 169 NUM_ABOVE_STATES
michael@0 170 } thai_above_start_state[NUM_CONSONANT_TYPES + 1/* For NOT_CONSONANT */] =
michael@0 171 {
michael@0 172 T0, /* NC */
michael@0 173 T1, /* AC */
michael@0 174 T0, /* RC */
michael@0 175 T0, /* DC */
michael@0 176 T3, /* NOT_CONSONANT */
michael@0 177 };
michael@0 178
michael@0 179 static const struct thai_above_state_machine_edge_t {
michael@0 180 thai_action_t action;
michael@0 181 thai_above_state_t next_state;
michael@0 182 } thai_above_state_machine[NUM_ABOVE_STATES][NUM_MARK_TYPES] =
michael@0 183 { /*AV*/ /*BV*/ /*T*/
michael@0 184 /*T0*/ {{NOP,T3}, {NOP,T0}, {SD, T3}},
michael@0 185 /*T1*/ {{SL, T2}, {NOP,T1}, {SDL,T2}},
michael@0 186 /*T2*/ {{NOP,T3}, {NOP,T2}, {SL, T3}},
michael@0 187 /*T3*/ {{NOP,T3}, {NOP,T3}, {NOP,T3}},
michael@0 188 };
michael@0 189
michael@0 190
michael@0 191 static enum thai_below_state_t
michael@0 192 {
michael@0 193 B0, /* No descender */
michael@0 194 B1, /* Removable descender */
michael@0 195 B2, /* Strict descender */
michael@0 196 NUM_BELOW_STATES
michael@0 197 } thai_below_start_state[NUM_CONSONANT_TYPES + 1/* For NOT_CONSONANT */] =
michael@0 198 {
michael@0 199 B0, /* NC */
michael@0 200 B0, /* AC */
michael@0 201 B1, /* RC */
michael@0 202 B2, /* DC */
michael@0 203 B2, /* NOT_CONSONANT */
michael@0 204 };
michael@0 205
michael@0 206 static const struct thai_below_state_machine_edge_t {
michael@0 207 thai_action_t action;
michael@0 208 thai_below_state_t next_state;
michael@0 209 } thai_below_state_machine[NUM_BELOW_STATES][NUM_MARK_TYPES] =
michael@0 210 { /*AV*/ /*BV*/ /*T*/
michael@0 211 /*B0*/ {{NOP,B0}, {NOP,B2}, {NOP, B0}},
michael@0 212 /*B1*/ {{NOP,B1}, {RD, B2}, {NOP, B1}},
michael@0 213 /*B2*/ {{NOP,B2}, {SD, B2}, {NOP, B2}},
michael@0 214 };
michael@0 215
michael@0 216
michael@0 217 static void
michael@0 218 do_thai_pua_shaping (const hb_ot_shape_plan_t *plan HB_UNUSED,
michael@0 219 hb_buffer_t *buffer,
michael@0 220 hb_font_t *font)
michael@0 221 {
michael@0 222 thai_above_state_t above_state = thai_above_start_state[NOT_CONSONANT];
michael@0 223 thai_below_state_t below_state = thai_below_start_state[NOT_CONSONANT];
michael@0 224 unsigned int base = 0;
michael@0 225
michael@0 226 hb_glyph_info_t *info = buffer->info;
michael@0 227 unsigned int count = buffer->len;
michael@0 228 for (unsigned int i = 0; i < count; i++)
michael@0 229 {
michael@0 230 thai_mark_type_t mt = get_mark_type (info[i].codepoint);
michael@0 231
michael@0 232 if (mt == NOT_MARK) {
michael@0 233 thai_consonant_type_t ct = get_consonant_type (info[i].codepoint);
michael@0 234 above_state = thai_above_start_state[ct];
michael@0 235 below_state = thai_below_start_state[ct];
michael@0 236 base = i;
michael@0 237 continue;
michael@0 238 }
michael@0 239
michael@0 240 const thai_above_state_machine_edge_t &above_edge = thai_above_state_machine[above_state][mt];
michael@0 241 const thai_below_state_machine_edge_t &below_edge = thai_below_state_machine[below_state][mt];
michael@0 242 above_state = above_edge.next_state;
michael@0 243 below_state = below_edge.next_state;
michael@0 244
michael@0 245 /* At least one of the above/below actions is NOP. */
michael@0 246 thai_action_t action = above_edge.action != NOP ? above_edge.action : below_edge.action;
michael@0 247
michael@0 248 if (action == RD)
michael@0 249 info[base].codepoint = thai_pua_shape (info[base].codepoint, action, font);
michael@0 250 else
michael@0 251 info[i].codepoint = thai_pua_shape (info[i].codepoint, action, font);
michael@0 252 }
michael@0 253 }
michael@0 254
michael@0 255
michael@0 256 static void
michael@0 257 preprocess_text_thai (const hb_ot_shape_plan_t *plan,
michael@0 258 hb_buffer_t *buffer,
michael@0 259 hb_font_t *font)
michael@0 260 {
michael@0 261 /* This function implements the shaping logic documented here:
michael@0 262 *
michael@0 263 * http://linux.thai.net/~thep/th-otf/shaping.html
michael@0 264 *
michael@0 265 * The first shaping rule listed there is needed even if the font has Thai
michael@0 266 * OpenType tables. The rest do fallback positioning based on PUA codepoints.
michael@0 267 * We implement that only if there exist no Thai GSUB in the font.
michael@0 268 */
michael@0 269
michael@0 270 /* The following is NOT specified in the MS OT Thai spec, however, it seems
michael@0 271 * to be what Uniscribe and other engines implement. According to Eric Muller:
michael@0 272 *
michael@0 273 * When you have a SARA AM, decompose it in NIKHAHIT + SARA AA, *and* move the
michael@0 274 * NIKHAHIT backwards over any tone mark (0E48-0E4B).
michael@0 275 *
michael@0 276 * <0E14, 0E4B, 0E33> -> <0E14, 0E4D, 0E4B, 0E32>
michael@0 277 *
michael@0 278 * This reordering is legit only when the NIKHAHIT comes from a SARA AM, not
michael@0 279 * when it's there to start with. The string <0E14, 0E4B, 0E4D> is probably
michael@0 280 * not what a user wanted, but the rendering is nevertheless nikhahit above
michael@0 281 * chattawa.
michael@0 282 *
michael@0 283 * Same for Lao.
michael@0 284 *
michael@0 285 * Note:
michael@0 286 *
michael@0 287 * Uniscribe also does some below-marks reordering. Namely, it positions U+0E3A
michael@0 288 * after U+0E38 and U+0E39. We do that by modifying the ccc for U+0E3A.
michael@0 289 * See unicode->modified_combining_class (). Lao does NOT have a U+0E3A
michael@0 290 * equivalent.
michael@0 291 */
michael@0 292
michael@0 293
michael@0 294 /*
michael@0 295 * Here are the characters of significance:
michael@0 296 *
michael@0 297 * Thai Lao
michael@0 298 * SARA AM: U+0E33 U+0EB3
michael@0 299 * SARA AA: U+0E32 U+0EB2
michael@0 300 * Nikhahit: U+0E4D U+0ECD
michael@0 301 *
michael@0 302 * Testing shows that Uniscribe reorder the following marks:
michael@0 303 * Thai: <0E31,0E34..0E37,0E47..0E4E>
michael@0 304 * Lao: <0EB1,0EB4..0EB7,0EC7..0ECE>
michael@0 305 *
michael@0 306 * Note how the Lao versions are the same as Thai + 0x80.
michael@0 307 */
michael@0 308
michael@0 309 /* We only get one script at a time, so a script-agnostic implementation
michael@0 310 * is adequate here. */
michael@0 311 #define IS_SARA_AM(x) (((x) & ~0x0080) == 0x0E33)
michael@0 312 #define NIKHAHIT_FROM_SARA_AM(x) ((x) - 0xE33 + 0xE4D)
michael@0 313 #define SARA_AA_FROM_SARA_AM(x) ((x) - 1)
michael@0 314 #define IS_TONE_MARK(x) (hb_in_ranges<hb_codepoint_t> ((x) & ~0x0080, 0x0E34, 0x0E37, 0x0E47, 0x0E4E, 0x0E31, 0x0E31))
michael@0 315
michael@0 316 buffer->clear_output ();
michael@0 317 unsigned int count = buffer->len;
michael@0 318 for (buffer->idx = 0; buffer->idx < count;)
michael@0 319 {
michael@0 320 hb_codepoint_t u = buffer->cur().codepoint;
michael@0 321 if (likely (!IS_SARA_AM (u))) {
michael@0 322 buffer->next_glyph ();
michael@0 323 continue;
michael@0 324 }
michael@0 325
michael@0 326 /* Is SARA AM. Decompose and reorder. */
michael@0 327 hb_codepoint_t decomposed[2] = {hb_codepoint_t (NIKHAHIT_FROM_SARA_AM (u)),
michael@0 328 hb_codepoint_t (SARA_AA_FROM_SARA_AM (u))};
michael@0 329 buffer->replace_glyphs (1, 2, decomposed);
michael@0 330 if (unlikely (buffer->in_error))
michael@0 331 return;
michael@0 332
michael@0 333 /* Ok, let's see... */
michael@0 334 unsigned int end = buffer->out_len;
michael@0 335 unsigned int start = end - 2;
michael@0 336 while (start > 0 && IS_TONE_MARK (buffer->out_info[start - 1].codepoint))
michael@0 337 start--;
michael@0 338
michael@0 339 if (start + 2 < end)
michael@0 340 {
michael@0 341 /* Move Nikhahit (end-2) to the beginning */
michael@0 342 buffer->merge_out_clusters (start, end);
michael@0 343 hb_glyph_info_t t = buffer->out_info[end - 2];
michael@0 344 memmove (buffer->out_info + start + 1,
michael@0 345 buffer->out_info + start,
michael@0 346 sizeof (buffer->out_info[0]) * (end - start - 2));
michael@0 347 buffer->out_info[start] = t;
michael@0 348 }
michael@0 349 else
michael@0 350 {
michael@0 351 /* Since we decomposed, and NIKHAHIT is combining, merge clusters with the
michael@0 352 * previous cluster. */
michael@0 353 if (start)
michael@0 354 buffer->merge_out_clusters (start - 1, end);
michael@0 355 }
michael@0 356 }
michael@0 357 buffer->swap_buffers ();
michael@0 358
michael@0 359 /* If font has Thai GSUB, we are done. */
michael@0 360 if (plan->props.script == HB_SCRIPT_THAI && !plan->map.found_script[0])
michael@0 361 do_thai_pua_shaping (plan, buffer, font);
michael@0 362 }
michael@0 363
michael@0 364 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_thai =
michael@0 365 {
michael@0 366 "thai",
michael@0 367 NULL, /* collect_features */
michael@0 368 NULL, /* override_features */
michael@0 369 NULL, /* data_create */
michael@0 370 NULL, /* data_destroy */
michael@0 371 preprocess_text_thai,
michael@0 372 HB_OT_SHAPE_NORMALIZATION_MODE_DEFAULT,
michael@0 373 NULL, /* decompose */
michael@0 374 NULL, /* compose */
michael@0 375 NULL, /* setup_masks */
michael@0 376 HB_OT_SHAPE_ZERO_WIDTH_MARKS_DEFAULT,
michael@0 377 false,/* fallback_position */
michael@0 378 };

mercurial