michael@0: /* michael@0: * Copyright © 2013 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: /* Hangul shaper */ michael@0: michael@0: michael@0: /* Same order as the feature array below */ michael@0: enum { michael@0: NONE, michael@0: michael@0: LJMO, michael@0: VJMO, michael@0: TJMO, michael@0: michael@0: FIRST_HANGUL_FEATURE = LJMO, michael@0: HANGUL_FEATURE_COUNT = TJMO + 1 michael@0: }; michael@0: michael@0: static const hb_tag_t hangul_features[HANGUL_FEATURE_COUNT] = michael@0: { michael@0: HB_TAG_NONE, michael@0: HB_TAG('l','j','m','o'), michael@0: HB_TAG('v','j','m','o'), michael@0: HB_TAG('t','j','m','o') michael@0: }; michael@0: michael@0: static void michael@0: collect_features_hangul (hb_ot_shape_planner_t *plan) michael@0: { michael@0: hb_ot_map_builder_t *map = &plan->map; michael@0: michael@0: for (unsigned int i = FIRST_HANGUL_FEATURE; i < HANGUL_FEATURE_COUNT; i++) michael@0: map->add_feature (hangul_features[i], 1, F_NONE); michael@0: } michael@0: michael@0: struct hangul_shape_plan_t michael@0: { michael@0: ASSERT_POD (); michael@0: michael@0: hb_mask_t mask_array[HANGUL_FEATURE_COUNT]; michael@0: }; michael@0: michael@0: static void * michael@0: data_create_hangul (const hb_ot_shape_plan_t *plan) michael@0: { michael@0: hangul_shape_plan_t *hangul_plan = (hangul_shape_plan_t *) calloc (1, sizeof (hangul_shape_plan_t)); michael@0: if (unlikely (!hangul_plan)) michael@0: return NULL; michael@0: michael@0: for (unsigned int i = 0; i < HANGUL_FEATURE_COUNT; i++) michael@0: hangul_plan->mask_array[i] = plan->map.get_1_mask (hangul_features[i]); michael@0: michael@0: return hangul_plan; michael@0: } michael@0: michael@0: static void michael@0: data_destroy_hangul (void *data) michael@0: { michael@0: free (data); michael@0: } michael@0: michael@0: /* Constants for algorithmic hangul syllable [de]composition. */ michael@0: #define LBase 0x1100 michael@0: #define VBase 0x1161 michael@0: #define TBase 0x11A7 michael@0: #define LCount 19 michael@0: #define VCount 21 michael@0: #define TCount 28 michael@0: #define SBase 0xAC00 michael@0: #define NCount (VCount * TCount) michael@0: #define SCount (LCount * NCount) michael@0: michael@0: #define isCombiningL(u) (hb_in_range ((u), LBase, LBase+LCount-1)) michael@0: #define isCombiningV(u) (hb_in_range ((u), VBase, VBase+VCount-1)) michael@0: #define isCombiningT(u) (hb_in_range ((u), TBase+1, TBase+TCount-1)) michael@0: #define isCombinedS(u) (hb_in_range ((u), SBase, SBase+SCount-1)) michael@0: michael@0: #define isL(u) (hb_in_ranges ((u), 0x1100, 0x115F, 0xA960, 0xA97C)) michael@0: #define isV(u) (hb_in_ranges ((u), 0x1160, 0x11A7, 0xD7B0, 0xD7C6)) michael@0: #define isT(u) (hb_in_ranges ((u), 0x11A8, 0x11FF, 0xD7CB, 0xD7FB)) michael@0: michael@0: #define isHangulTone(u) (hb_in_range ((u), 0x302e, 0x302f)) michael@0: michael@0: /* buffer var allocations */ michael@0: #define hangul_shaping_feature() complex_var_u8_0() /* hangul jamo shaping feature */ michael@0: michael@0: static bool michael@0: is_zero_width_char (hb_font_t *font, michael@0: hb_codepoint_t unicode) michael@0: { michael@0: hb_codepoint_t glyph; michael@0: return hb_font_get_glyph (font, unicode, 0, &glyph) && hb_font_get_glyph_h_advance (font, glyph) == 0; michael@0: } michael@0: michael@0: static void michael@0: preprocess_text_hangul (const hb_ot_shape_plan_t *plan, michael@0: hb_buffer_t *buffer, michael@0: hb_font_t *font) michael@0: { michael@0: HB_BUFFER_ALLOCATE_VAR (buffer, hangul_shaping_feature); michael@0: michael@0: /* Hangul syllables come in two shapes: LV, and LVT. Of those: michael@0: * michael@0: * - LV can be precomposed, or decomposed. Lets call those michael@0: * and , michael@0: * - LVT can be fully precomposed, partically precomposed, or michael@0: * fully decomposed. Ie. , , or . michael@0: * michael@0: * The composition / decomposition is mechanical. However, not michael@0: * all sequences compose, and not all sequences michael@0: * compose. michael@0: * michael@0: * Here are the specifics: michael@0: * michael@0: * - : U+1100..115F, U+A960..A97F michael@0: * - : U+1160..11A7, U+D7B0..D7C7 michael@0: * - : U+11A8..11FF, U+D7CB..D7FB michael@0: * michael@0: * - Only the sequences for the 11xx ranges combine. michael@0: * - Only sequences for T in U+11A8..11C3 combine. michael@0: * michael@0: * Here is what we want to accomplish in this shaper: michael@0: * michael@0: * - If the whole syllable can be precomposed, do that, michael@0: * - Otherwise, fully decompose and apply ljmo/vjmo/tjmo features. michael@0: * - If a valid syllable is followed by a Hangul tone mark, reorder the tone michael@0: * mark to precede the whole syllable - unless it is a zero-width glyph, in michael@0: * which case we leave it untouched, assuming it's designed to overstrike. michael@0: * michael@0: * That is, of the different possible syllables: michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * - needs no work. michael@0: * michael@0: * - and can stay the way they are if the font supports them, otherwise we michael@0: * should fully decompose them if font supports. michael@0: * michael@0: * - and we should compose if the whole thing can be composed. michael@0: * michael@0: * - we should compose if the whole thing can be composed, otherwise we should michael@0: * decompose. michael@0: */ michael@0: michael@0: buffer->clear_output (); michael@0: unsigned int start = 0, end = 0; /* Extent of most recently seen syllable; michael@0: * valid only if start < end michael@0: */ michael@0: unsigned int count = buffer->len; michael@0: michael@0: for (buffer->idx = 0; buffer->idx < count;) michael@0: { michael@0: hb_codepoint_t u = buffer->cur().codepoint; michael@0: michael@0: if (isHangulTone (u)) michael@0: { michael@0: /* michael@0: * We could cache the width of the tone marks and the existence of dotted-circle, michael@0: * but the use of the Hangul tone mark characters seems to be rare enough that michael@0: * I didn't bother for now. michael@0: */ michael@0: if (start < end && end == buffer->out_len) michael@0: { michael@0: /* Tone mark follows a valid syllable; move it in front, unless it's zero width. */ michael@0: buffer->next_glyph (); michael@0: if (!is_zero_width_char (font, u)) michael@0: { michael@0: hb_glyph_info_t *info = buffer->out_info; michael@0: hb_glyph_info_t tone = info[end]; michael@0: memmove (&info[start + 1], &info[start], (end - start) * sizeof (hb_glyph_info_t)); michael@0: info[start] = tone; michael@0: } michael@0: /* Merge clusters across the (possibly reordered) syllable+tone. michael@0: * We want to merge even in the zero-width tone mark case here, michael@0: * so that clustering behavior isn't dependent on how the tone mark michael@0: * is handled by the font. michael@0: */ michael@0: buffer->merge_out_clusters (start, end + 1); michael@0: } michael@0: else michael@0: { michael@0: /* No valid syllable as base for tone mark; try to insert dotted circle. */ michael@0: if (font->has_glyph (0x25cc)) michael@0: { michael@0: hb_codepoint_t chars[2]; michael@0: if (!is_zero_width_char (font, u)) { michael@0: chars[0] = u; michael@0: chars[1] = 0x25cc; michael@0: } else { michael@0: chars[0] = 0x25cc; michael@0: chars[1] = u; michael@0: } michael@0: buffer->replace_glyphs (1, 2, chars); michael@0: } michael@0: else michael@0: { michael@0: /* No dotted circle available in the font; just leave tone mark untouched. */ michael@0: buffer->next_glyph (); michael@0: } michael@0: } michael@0: start = end = buffer->out_len; michael@0: continue; michael@0: } michael@0: michael@0: start = buffer->out_len; /* Remember current position as a potential syllable start; michael@0: * will only be used if we set end to a later position. michael@0: */ michael@0: michael@0: if (isL (u) && buffer->idx + 1 < count) michael@0: { michael@0: hb_codepoint_t l = u; michael@0: hb_codepoint_t v = buffer->cur(+1).codepoint; michael@0: if (isV (v)) michael@0: { michael@0: /* Have or . */ michael@0: hb_codepoint_t t = 0; michael@0: unsigned int tindex = 0; michael@0: if (buffer->idx + 2 < count) michael@0: { michael@0: t = buffer->cur(+2).codepoint; michael@0: if (isT (t)) michael@0: tindex = t - TBase; /* Only used if isCombiningT (t); otherwise invalid. */ michael@0: else michael@0: t = 0; /* The next character was not a trailing jamo. */ michael@0: } michael@0: michael@0: /* We've got a syllable ; see if it can potentially be composed. */ michael@0: if (isCombiningL (l) && isCombiningV (v) && (t == 0 || isCombiningT (t))) michael@0: { michael@0: /* Try to compose; if this succeeds, end is set to start+1. */ michael@0: hb_codepoint_t s = SBase + (l - LBase) * NCount + (v - VBase) * TCount + tindex; michael@0: if (font->has_glyph (s)) michael@0: { michael@0: buffer->replace_glyphs (t ? 3 : 2, 1, &s); michael@0: if (unlikely (buffer->in_error)) michael@0: return; michael@0: end = start + 1; michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: /* We didn't compose, either because it's an Old Hangul syllable without a michael@0: * precomposed character in Unicode, or because the font didn't support the michael@0: * necessary precomposed glyph. michael@0: * Set jamo features on the individual glyphs, and advance past them. michael@0: */ michael@0: buffer->cur().hangul_shaping_feature() = LJMO; michael@0: buffer->next_glyph (); michael@0: buffer->cur().hangul_shaping_feature() = VJMO; michael@0: buffer->next_glyph (); michael@0: if (t) michael@0: { michael@0: buffer->cur().hangul_shaping_feature() = TJMO; michael@0: buffer->next_glyph (); michael@0: end = start + 3; michael@0: } michael@0: else michael@0: end = start + 2; michael@0: buffer->merge_out_clusters (start, end); michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: else if (isCombinedS (u)) michael@0: { michael@0: /* Have , , or */ michael@0: hb_codepoint_t s = u; michael@0: bool has_glyph = font->has_glyph (s); michael@0: unsigned int lindex = (s - SBase) / NCount; michael@0: unsigned int nindex = (s - SBase) % NCount; michael@0: unsigned int vindex = nindex / TCount; michael@0: unsigned int tindex = nindex % TCount; michael@0: michael@0: if (!tindex && michael@0: buffer->idx + 1 < count && michael@0: isCombiningT (buffer->cur(+1).codepoint)) michael@0: { michael@0: /* , try to combine. */ michael@0: unsigned int new_tindex = buffer->cur(+1).codepoint - TBase; michael@0: hb_codepoint_t new_s = s + new_tindex; michael@0: if (font->has_glyph (new_s)) michael@0: { michael@0: buffer->replace_glyphs (2, 1, &new_s); michael@0: if (unlikely (buffer->in_error)) michael@0: return; michael@0: end = start + 1; michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: /* Otherwise, decompose if font doesn't support or , michael@0: * or if having non-combining . Note that we already handled michael@0: * combining above. */ michael@0: if (!has_glyph || michael@0: (!tindex && michael@0: buffer->idx + 1 < count && michael@0: isT (buffer->cur(+1).codepoint))) michael@0: { michael@0: hb_codepoint_t decomposed[3] = {LBase + lindex, michael@0: VBase + vindex, michael@0: TBase + tindex}; michael@0: if (font->has_glyph (decomposed[0]) && michael@0: font->has_glyph (decomposed[1]) && michael@0: (!tindex || font->has_glyph (decomposed[2]))) michael@0: { michael@0: unsigned int s_len = tindex ? 3 : 2; michael@0: buffer->replace_glyphs (1, s_len, decomposed); michael@0: if (unlikely (buffer->in_error)) michael@0: return; michael@0: michael@0: /* We decomposed S: apply jamo features to the individual glyphs michael@0: * that are now in buffer->out_info. michael@0: */ michael@0: hb_glyph_info_t *info = buffer->out_info; michael@0: michael@0: /* If we decomposed an LV because of a non-combining T following, michael@0: * we want to include this T in the syllable. michael@0: */ michael@0: if (has_glyph && !tindex) michael@0: { michael@0: buffer->next_glyph (); michael@0: s_len++; michael@0: } michael@0: end = start + s_len; michael@0: michael@0: unsigned int i = start; michael@0: info[i++].hangul_shaping_feature() = LJMO; michael@0: info[i++].hangul_shaping_feature() = VJMO; michael@0: if (i < end) michael@0: info[i++].hangul_shaping_feature() = TJMO; michael@0: buffer->merge_out_clusters (start, end); michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: if (has_glyph) michael@0: { michael@0: /* We didn't decompose the S, so just advance past it. */ michael@0: end = start + 1; michael@0: buffer->next_glyph (); michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: /* Didn't find a recognizable syllable, so we leave end <= start; michael@0: * this will prevent tone-mark reordering happening. michael@0: */ michael@0: buffer->next_glyph (); michael@0: } michael@0: buffer->swap_buffers (); michael@0: } michael@0: michael@0: static void michael@0: setup_masks_hangul (const hb_ot_shape_plan_t *plan, michael@0: hb_buffer_t *buffer, michael@0: hb_font_t *font HB_UNUSED) michael@0: { michael@0: const hangul_shape_plan_t *hangul_plan = (const hangul_shape_plan_t *) plan->data; michael@0: michael@0: if (likely (hangul_plan)) michael@0: { michael@0: unsigned int count = buffer->len; michael@0: hb_glyph_info_t *info = buffer->info; michael@0: for (unsigned int i = 0; i < count; i++, info++) michael@0: info->mask |= hangul_plan->mask_array[info->hangul_shaping_feature()]; michael@0: } michael@0: michael@0: HB_BUFFER_DEALLOCATE_VAR (buffer, hangul_shaping_feature); michael@0: } michael@0: michael@0: michael@0: const hb_ot_complex_shaper_t _hb_ot_complex_shaper_hangul = michael@0: { michael@0: "hangul", michael@0: collect_features_hangul, michael@0: NULL, /* override_features */ michael@0: data_create_hangul, /* data_create */ michael@0: data_destroy_hangul, /* data_destroy */ michael@0: preprocess_text_hangul, michael@0: HB_OT_SHAPE_NORMALIZATION_MODE_NONE, michael@0: NULL, /* decompose */ michael@0: NULL, /* compose */ michael@0: setup_masks_hangul, /* setup_masks */ michael@0: HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE, michael@0: false, /* fallback_position */ michael@0: };