michael@0: /* michael@0: * Copyright © 2010,2012 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #include "hb-ot-shape-complex-private.hh" michael@0: michael@0: michael@0: /* Thai / Lao shaper */ michael@0: michael@0: michael@0: /* PUA shaping */ michael@0: michael@0: michael@0: enum thai_consonant_type_t michael@0: { michael@0: NC, michael@0: AC, michael@0: RC, michael@0: DC, michael@0: NOT_CONSONANT, michael@0: NUM_CONSONANT_TYPES = NOT_CONSONANT michael@0: }; michael@0: michael@0: static thai_consonant_type_t michael@0: get_consonant_type (hb_codepoint_t u) michael@0: { michael@0: if (u == 0x0E1B || u == 0x0E1D || u == 0x0E1F/* || u == 0x0E2C*/) michael@0: return AC; michael@0: if (u == 0x0E0D || u == 0x0E10) michael@0: return RC; michael@0: if (u == 0x0E0E || u == 0x0E0F) michael@0: return DC; michael@0: if (hb_in_range (u, 0x0E01, 0x0E2E)) michael@0: return NC; michael@0: return NOT_CONSONANT; michael@0: } michael@0: michael@0: michael@0: enum thai_mark_type_t michael@0: { michael@0: AV, michael@0: BV, michael@0: T, michael@0: NOT_MARK, michael@0: NUM_MARK_TYPES = NOT_MARK michael@0: }; michael@0: michael@0: static thai_mark_type_t michael@0: get_mark_type (hb_codepoint_t u) michael@0: { michael@0: if (u == 0x0E31 || hb_in_range (u, 0x0E34, 0x0E37) || michael@0: u == 0x0E47 || hb_in_range (u, 0x0E4D, 0x0E4E)) michael@0: return AV; michael@0: if (hb_in_range (u, 0x0E38, 0x0E3A)) michael@0: return BV; michael@0: if (hb_in_range (u, 0x0E48, 0x0E4C)) michael@0: return T; michael@0: return NOT_MARK; michael@0: } michael@0: michael@0: michael@0: enum thai_action_t michael@0: { michael@0: NOP, michael@0: SD, /* Shift combining-mark down */ michael@0: SL, /* Shift combining-mark left */ michael@0: SDL, /* Shift combining-mark down-left */ michael@0: RD /* Remove descender from base */ michael@0: }; michael@0: michael@0: static hb_codepoint_t michael@0: thai_pua_shape (hb_codepoint_t u, thai_action_t action, hb_font_t *font) michael@0: { michael@0: struct thai_pua_mapping_t { michael@0: hb_codepoint_t u; michael@0: hb_codepoint_t win_pua; michael@0: hb_codepoint_t mac_pua; michael@0: } const *pua_mappings = NULL; michael@0: static const thai_pua_mapping_t SD_mappings[] = { michael@0: {0x0E48, 0xF70A, 0xF88B}, /* MAI EK */ michael@0: {0x0E49, 0xF70B, 0xF88E}, /* MAI THO */ michael@0: {0x0E4A, 0xF70C, 0xF891}, /* MAI TRI */ michael@0: {0x0E4B, 0xF70D, 0xF894}, /* MAI CHATTAWA */ michael@0: {0x0E4C, 0xF70E, 0xF897}, /* THANTHAKHAT */ michael@0: {0x0E38, 0xF718, 0xF89B}, /* SARA U */ michael@0: {0x0E39, 0xF719, 0xF89C}, /* SARA UU */ michael@0: {0x0E3A, 0xF71A, 0xF89D}, /* PHINTHU */ michael@0: {0x0000, 0x0000, 0x0000} michael@0: }; michael@0: static const thai_pua_mapping_t SDL_mappings[] = { michael@0: {0x0E48, 0xF705, 0xF88C}, /* MAI EK */ michael@0: {0x0E49, 0xF706, 0xF88F}, /* MAI THO */ michael@0: {0x0E4A, 0xF707, 0xF892}, /* MAI TRI */ michael@0: {0x0E4B, 0xF708, 0xF895}, /* MAI CHATTAWA */ michael@0: {0x0E4C, 0xF709, 0xF898}, /* THANTHAKHAT */ michael@0: {0x0000, 0x0000, 0x0000} michael@0: }; michael@0: static const thai_pua_mapping_t SL_mappings[] = { michael@0: {0x0E48, 0xF713, 0xF88A}, /* MAI EK */ michael@0: {0x0E49, 0xF714, 0xF88D}, /* MAI THO */ michael@0: {0x0E4A, 0xF715, 0xF890}, /* MAI TRI */ michael@0: {0x0E4B, 0xF716, 0xF893}, /* MAI CHATTAWA */ michael@0: {0x0E4C, 0xF717, 0xF896}, /* THANTHAKHAT */ michael@0: {0x0E31, 0xF710, 0xF884}, /* MAI HAN-AKAT */ michael@0: {0x0E34, 0xF701, 0xF885}, /* SARA I */ michael@0: {0x0E35, 0xF702, 0xF886}, /* SARA II */ michael@0: {0x0E36, 0xF703, 0xF887}, /* SARA UE */ michael@0: {0x0E37, 0xF704, 0xF888}, /* SARA UEE */ michael@0: {0x0E47, 0xF712, 0xF889}, /* MAITAIKHU */ michael@0: {0x0E4D, 0xF711, 0xF899}, /* NIKHAHIT */ michael@0: {0x0000, 0x0000, 0x0000} michael@0: }; michael@0: static const thai_pua_mapping_t RD_mappings[] = { michael@0: {0x0E0D, 0xF70F, 0xF89A}, /* YO YING */ michael@0: {0x0E10, 0xF700, 0xF89E}, /* THO THAN */ michael@0: {0x0000, 0x0000, 0x0000} michael@0: }; michael@0: michael@0: switch (action) { michael@0: default: assert (false); /* Fallthrough */ michael@0: case NOP: return u; michael@0: case SD: pua_mappings = SD_mappings; break; michael@0: case SDL: pua_mappings = SDL_mappings; break; michael@0: case SL: pua_mappings = SL_mappings; break; michael@0: case RD: pua_mappings = RD_mappings; break; michael@0: } michael@0: for (; pua_mappings->u; pua_mappings++) michael@0: if (pua_mappings->u == u) michael@0: { michael@0: hb_codepoint_t glyph; michael@0: if (hb_font_get_glyph (font, pua_mappings->win_pua, 0, &glyph)) michael@0: return pua_mappings->win_pua; michael@0: if (hb_font_get_glyph (font, pua_mappings->mac_pua, 0, &glyph)) michael@0: return pua_mappings->mac_pua; michael@0: break; michael@0: } michael@0: return u; michael@0: } michael@0: michael@0: michael@0: static enum thai_above_state_t michael@0: { /* Cluster above looks like: */ michael@0: T0, /* ⣤ */ michael@0: T1, /* ⣼ */ michael@0: T2, /* ⣾ */ michael@0: T3, /* ⣿ */ michael@0: NUM_ABOVE_STATES michael@0: } thai_above_start_state[NUM_CONSONANT_TYPES + 1/* For NOT_CONSONANT */] = michael@0: { michael@0: T0, /* NC */ michael@0: T1, /* AC */ michael@0: T0, /* RC */ michael@0: T0, /* DC */ michael@0: T3, /* NOT_CONSONANT */ michael@0: }; michael@0: michael@0: static const struct thai_above_state_machine_edge_t { michael@0: thai_action_t action; michael@0: thai_above_state_t next_state; michael@0: } thai_above_state_machine[NUM_ABOVE_STATES][NUM_MARK_TYPES] = michael@0: { /*AV*/ /*BV*/ /*T*/ michael@0: /*T0*/ {{NOP,T3}, {NOP,T0}, {SD, T3}}, michael@0: /*T1*/ {{SL, T2}, {NOP,T1}, {SDL,T2}}, michael@0: /*T2*/ {{NOP,T3}, {NOP,T2}, {SL, T3}}, michael@0: /*T3*/ {{NOP,T3}, {NOP,T3}, {NOP,T3}}, michael@0: }; michael@0: michael@0: michael@0: static enum thai_below_state_t michael@0: { michael@0: B0, /* No descender */ michael@0: B1, /* Removable descender */ michael@0: B2, /* Strict descender */ michael@0: NUM_BELOW_STATES michael@0: } thai_below_start_state[NUM_CONSONANT_TYPES + 1/* For NOT_CONSONANT */] = michael@0: { michael@0: B0, /* NC */ michael@0: B0, /* AC */ michael@0: B1, /* RC */ michael@0: B2, /* DC */ michael@0: B2, /* NOT_CONSONANT */ michael@0: }; michael@0: michael@0: static const struct thai_below_state_machine_edge_t { michael@0: thai_action_t action; michael@0: thai_below_state_t next_state; michael@0: } thai_below_state_machine[NUM_BELOW_STATES][NUM_MARK_TYPES] = michael@0: { /*AV*/ /*BV*/ /*T*/ michael@0: /*B0*/ {{NOP,B0}, {NOP,B2}, {NOP, B0}}, michael@0: /*B1*/ {{NOP,B1}, {RD, B2}, {NOP, B1}}, michael@0: /*B2*/ {{NOP,B2}, {SD, B2}, {NOP, B2}}, michael@0: }; michael@0: michael@0: michael@0: static void michael@0: do_thai_pua_shaping (const hb_ot_shape_plan_t *plan HB_UNUSED, michael@0: hb_buffer_t *buffer, michael@0: hb_font_t *font) michael@0: { michael@0: thai_above_state_t above_state = thai_above_start_state[NOT_CONSONANT]; michael@0: thai_below_state_t below_state = thai_below_start_state[NOT_CONSONANT]; michael@0: unsigned int base = 0; michael@0: michael@0: hb_glyph_info_t *info = buffer->info; michael@0: unsigned int count = buffer->len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: { michael@0: thai_mark_type_t mt = get_mark_type (info[i].codepoint); michael@0: michael@0: if (mt == NOT_MARK) { michael@0: thai_consonant_type_t ct = get_consonant_type (info[i].codepoint); michael@0: above_state = thai_above_start_state[ct]; michael@0: below_state = thai_below_start_state[ct]; michael@0: base = i; michael@0: continue; michael@0: } michael@0: michael@0: const thai_above_state_machine_edge_t &above_edge = thai_above_state_machine[above_state][mt]; michael@0: const thai_below_state_machine_edge_t &below_edge = thai_below_state_machine[below_state][mt]; michael@0: above_state = above_edge.next_state; michael@0: below_state = below_edge.next_state; michael@0: michael@0: /* At least one of the above/below actions is NOP. */ michael@0: thai_action_t action = above_edge.action != NOP ? above_edge.action : below_edge.action; michael@0: michael@0: if (action == RD) michael@0: info[base].codepoint = thai_pua_shape (info[base].codepoint, action, font); michael@0: else michael@0: info[i].codepoint = thai_pua_shape (info[i].codepoint, action, font); michael@0: } michael@0: } michael@0: michael@0: michael@0: static void michael@0: preprocess_text_thai (const hb_ot_shape_plan_t *plan, michael@0: hb_buffer_t *buffer, michael@0: hb_font_t *font) michael@0: { michael@0: /* This function implements the shaping logic documented here: michael@0: * michael@0: * http://linux.thai.net/~thep/th-otf/shaping.html michael@0: * michael@0: * The first shaping rule listed there is needed even if the font has Thai michael@0: * OpenType tables. The rest do fallback positioning based on PUA codepoints. michael@0: * We implement that only if there exist no Thai GSUB in the font. michael@0: */ michael@0: michael@0: /* The following is NOT specified in the MS OT Thai spec, however, it seems michael@0: * to be what Uniscribe and other engines implement. According to Eric Muller: michael@0: * michael@0: * When you have a SARA AM, decompose it in NIKHAHIT + SARA AA, *and* move the michael@0: * NIKHAHIT backwards over any tone mark (0E48-0E4B). michael@0: * michael@0: * <0E14, 0E4B, 0E33> -> <0E14, 0E4D, 0E4B, 0E32> michael@0: * michael@0: * This reordering is legit only when the NIKHAHIT comes from a SARA AM, not michael@0: * when it's there to start with. The string <0E14, 0E4B, 0E4D> is probably michael@0: * not what a user wanted, but the rendering is nevertheless nikhahit above michael@0: * chattawa. michael@0: * michael@0: * Same for Lao. michael@0: * michael@0: * Note: michael@0: * michael@0: * Uniscribe also does some below-marks reordering. Namely, it positions U+0E3A michael@0: * after U+0E38 and U+0E39. We do that by modifying the ccc for U+0E3A. michael@0: * See unicode->modified_combining_class (). Lao does NOT have a U+0E3A michael@0: * equivalent. michael@0: */ michael@0: michael@0: michael@0: /* michael@0: * Here are the characters of significance: michael@0: * michael@0: * Thai Lao michael@0: * SARA AM: U+0E33 U+0EB3 michael@0: * SARA AA: U+0E32 U+0EB2 michael@0: * Nikhahit: U+0E4D U+0ECD michael@0: * michael@0: * Testing shows that Uniscribe reorder the following marks: michael@0: * Thai: <0E31,0E34..0E37,0E47..0E4E> michael@0: * Lao: <0EB1,0EB4..0EB7,0EC7..0ECE> michael@0: * michael@0: * Note how the Lao versions are the same as Thai + 0x80. michael@0: */ michael@0: michael@0: /* We only get one script at a time, so a script-agnostic implementation michael@0: * is adequate here. */ michael@0: #define IS_SARA_AM(x) (((x) & ~0x0080) == 0x0E33) michael@0: #define NIKHAHIT_FROM_SARA_AM(x) ((x) - 0xE33 + 0xE4D) michael@0: #define SARA_AA_FROM_SARA_AM(x) ((x) - 1) michael@0: #define IS_TONE_MARK(x) (hb_in_ranges ((x) & ~0x0080, 0x0E34, 0x0E37, 0x0E47, 0x0E4E, 0x0E31, 0x0E31)) michael@0: michael@0: buffer->clear_output (); michael@0: unsigned int count = buffer->len; michael@0: for (buffer->idx = 0; buffer->idx < count;) michael@0: { michael@0: hb_codepoint_t u = buffer->cur().codepoint; michael@0: if (likely (!IS_SARA_AM (u))) { michael@0: buffer->next_glyph (); michael@0: continue; michael@0: } michael@0: michael@0: /* Is SARA AM. Decompose and reorder. */ michael@0: hb_codepoint_t decomposed[2] = {hb_codepoint_t (NIKHAHIT_FROM_SARA_AM (u)), michael@0: hb_codepoint_t (SARA_AA_FROM_SARA_AM (u))}; michael@0: buffer->replace_glyphs (1, 2, decomposed); michael@0: if (unlikely (buffer->in_error)) michael@0: return; michael@0: michael@0: /* Ok, let's see... */ michael@0: unsigned int end = buffer->out_len; michael@0: unsigned int start = end - 2; michael@0: while (start > 0 && IS_TONE_MARK (buffer->out_info[start - 1].codepoint)) michael@0: start--; michael@0: michael@0: if (start + 2 < end) michael@0: { michael@0: /* Move Nikhahit (end-2) to the beginning */ michael@0: buffer->merge_out_clusters (start, end); michael@0: hb_glyph_info_t t = buffer->out_info[end - 2]; michael@0: memmove (buffer->out_info + start + 1, michael@0: buffer->out_info + start, michael@0: sizeof (buffer->out_info[0]) * (end - start - 2)); michael@0: buffer->out_info[start] = t; michael@0: } michael@0: else michael@0: { michael@0: /* Since we decomposed, and NIKHAHIT is combining, merge clusters with the michael@0: * previous cluster. */ michael@0: if (start) michael@0: buffer->merge_out_clusters (start - 1, end); michael@0: } michael@0: } michael@0: buffer->swap_buffers (); michael@0: michael@0: /* If font has Thai GSUB, we are done. */ michael@0: if (plan->props.script == HB_SCRIPT_THAI && !plan->map.found_script[0]) michael@0: do_thai_pua_shaping (plan, buffer, font); michael@0: } michael@0: michael@0: const hb_ot_complex_shaper_t _hb_ot_complex_shaper_thai = michael@0: { michael@0: "thai", michael@0: NULL, /* collect_features */ michael@0: NULL, /* override_features */ michael@0: NULL, /* data_create */ michael@0: NULL, /* data_destroy */ michael@0: preprocess_text_thai, michael@0: HB_OT_SHAPE_NORMALIZATION_MODE_DEFAULT, michael@0: NULL, /* decompose */ michael@0: NULL, /* compose */ michael@0: NULL, /* setup_masks */ michael@0: HB_OT_SHAPE_ZERO_WIDTH_MARKS_DEFAULT, michael@0: false,/* fallback_position */ michael@0: };