michael@0: /* michael@0: * Copyright © 2011,2012,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-indic-private.hh" michael@0: michael@0: /* buffer var allocations */ michael@0: #define sea_category() complex_var_u8_0() /* indic_category_t */ michael@0: #define sea_position() complex_var_u8_1() /* indic_position_t */ michael@0: michael@0: michael@0: /* michael@0: * South-East Asian shaper. michael@0: * Loosely based on the Myanmar spec / shaper. michael@0: * There is no OpenType spec for this. michael@0: */ michael@0: michael@0: static const hb_tag_t michael@0: basic_features[] = michael@0: { michael@0: /* michael@0: * Basic features. michael@0: * These features are applied in order, one at a time, after initial_reordering. michael@0: */ michael@0: HB_TAG('p','r','e','f'), michael@0: HB_TAG('a','b','v','f'), michael@0: HB_TAG('b','l','w','f'), michael@0: HB_TAG('p','s','t','f'), michael@0: }; michael@0: static const hb_tag_t michael@0: other_features[] = michael@0: { michael@0: /* michael@0: * Other features. michael@0: * These features are applied all at once, after final_reordering. michael@0: */ michael@0: HB_TAG('p','r','e','s'), michael@0: HB_TAG('a','b','v','s'), michael@0: HB_TAG('b','l','w','s'), michael@0: HB_TAG('p','s','t','s'), michael@0: /* Positioning features, though we don't care about the types. */ michael@0: HB_TAG('d','i','s','t'), michael@0: }; michael@0: michael@0: static void michael@0: setup_syllables (const hb_ot_shape_plan_t *plan, michael@0: hb_font_t *font, michael@0: hb_buffer_t *buffer); michael@0: static void michael@0: initial_reordering (const hb_ot_shape_plan_t *plan, michael@0: hb_font_t *font, michael@0: hb_buffer_t *buffer); michael@0: static void michael@0: final_reordering (const hb_ot_shape_plan_t *plan, michael@0: hb_font_t *font, michael@0: hb_buffer_t *buffer); michael@0: michael@0: static void michael@0: collect_features_sea (hb_ot_shape_planner_t *plan) michael@0: { michael@0: hb_ot_map_builder_t *map = &plan->map; michael@0: michael@0: /* Do this before any lookups have been applied. */ michael@0: map->add_gsub_pause (setup_syllables); michael@0: michael@0: map->add_global_bool_feature (HB_TAG('l','o','c','l')); michael@0: /* The Indic specs do not require ccmp, but we apply it here since if michael@0: * there is a use of it, it's typically at the beginning. */ michael@0: map->add_global_bool_feature (HB_TAG('c','c','m','p')); michael@0: michael@0: map->add_gsub_pause (initial_reordering); michael@0: for (unsigned int i = 0; i < ARRAY_LENGTH (basic_features); i++) michael@0: { michael@0: map->add_feature (basic_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ); michael@0: map->add_gsub_pause (NULL); michael@0: } michael@0: map->add_gsub_pause (final_reordering); michael@0: for (unsigned int i = 0; i < ARRAY_LENGTH (other_features); i++) michael@0: map->add_feature (other_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ); michael@0: } michael@0: michael@0: static void michael@0: override_features_sea (hb_ot_shape_planner_t *plan) michael@0: { michael@0: plan->map.add_feature (HB_TAG('l','i','g','a'), 0, F_GLOBAL); michael@0: } michael@0: michael@0: michael@0: enum syllable_type_t { michael@0: consonant_syllable, michael@0: broken_cluster, michael@0: non_sea_cluster, michael@0: }; michael@0: michael@0: #include "hb-ot-shape-complex-sea-machine.hh" michael@0: michael@0: michael@0: /* Note: This enum is duplicated in the -machine.rl source file. michael@0: * Not sure how to avoid duplication. */ michael@0: enum sea_category_t { michael@0: // OT_C = 1, michael@0: OT_GB = 12, /* Generic Base XXX DOTTED CIRCLE only for now */ michael@0: // OT_H = 4, /* Halant */ michael@0: OT_IV = 2, /* Independent Vowel */ michael@0: OT_MR = 22, /* Medial Ra */ michael@0: // OT_CM = 17, /* Consonant Medial */ michael@0: OT_VAbv = 26, michael@0: OT_VBlw = 27, michael@0: OT_VPre = 28, michael@0: OT_VPst = 29, michael@0: OT_T = 3, /* Tone Marks */ michael@0: // OT_A = 10, /* Anusvara */ michael@0: }; michael@0: michael@0: static inline void michael@0: set_sea_properties (hb_glyph_info_t &info) michael@0: { michael@0: hb_codepoint_t u = info.codepoint; michael@0: unsigned int type = hb_indic_get_categories (u); michael@0: indic_category_t cat = (indic_category_t) (type & 0x7F); michael@0: indic_position_t pos = (indic_position_t) (type >> 8); michael@0: michael@0: /* Medial Ra */ michael@0: if (u == 0x1A55 || u == 0xAA34) michael@0: cat = (indic_category_t) OT_MR; michael@0: michael@0: if (cat == OT_M) michael@0: { michael@0: switch ((int) pos) michael@0: { michael@0: case POS_PRE_C: cat = (indic_category_t) OT_VPre; break; michael@0: case POS_ABOVE_C: cat = (indic_category_t) OT_VAbv; break; michael@0: case POS_BELOW_C: cat = (indic_category_t) OT_VBlw; break; michael@0: case POS_POST_C: cat = (indic_category_t) OT_VPst; break; michael@0: } michael@0: } michael@0: michael@0: info.sea_category() = (sea_category_t) cat; michael@0: info.sea_position() = pos; michael@0: } michael@0: michael@0: michael@0: static void michael@0: setup_masks_sea (const hb_ot_shape_plan_t *plan HB_UNUSED, michael@0: hb_buffer_t *buffer, michael@0: hb_font_t *font HB_UNUSED) michael@0: { michael@0: HB_BUFFER_ALLOCATE_VAR (buffer, sea_category); michael@0: HB_BUFFER_ALLOCATE_VAR (buffer, sea_position); michael@0: michael@0: /* We cannot setup masks here. We save information about characters michael@0: * and setup masks later on in a pause-callback. */ michael@0: michael@0: unsigned int count = buffer->len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: set_sea_properties (buffer->info[i]); michael@0: } michael@0: michael@0: static void michael@0: setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED, michael@0: hb_font_t *font HB_UNUSED, michael@0: hb_buffer_t *buffer) michael@0: { michael@0: find_syllables (buffer); michael@0: } michael@0: michael@0: static int michael@0: compare_sea_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb) michael@0: { michael@0: int a = pa->sea_position(); michael@0: int b = pb->sea_position(); michael@0: michael@0: return a < b ? -1 : a == b ? 0 : +1; michael@0: } michael@0: michael@0: michael@0: static void michael@0: initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan, michael@0: hb_face_t *face, michael@0: hb_buffer_t *buffer, michael@0: unsigned int start, unsigned int end) michael@0: { michael@0: hb_glyph_info_t *info = buffer->info; michael@0: unsigned int base = start; michael@0: michael@0: /* Reorder! */ michael@0: unsigned int i = start; michael@0: for (; i < base; i++) michael@0: info[i].sea_position() = POS_PRE_C; michael@0: if (i < end) michael@0: { michael@0: info[i].sea_position() = POS_BASE_C; michael@0: i++; michael@0: } michael@0: for (; i < end; i++) michael@0: { michael@0: if (info[i].sea_category() == OT_MR) /* Pre-base reordering */ michael@0: { michael@0: info[i].sea_position() = POS_PRE_C; michael@0: continue; michael@0: } michael@0: if (info[i].sea_category() == OT_VPre) /* Left matra */ michael@0: { michael@0: info[i].sea_position() = POS_PRE_M; michael@0: continue; michael@0: } michael@0: michael@0: info[i].sea_position() = POS_AFTER_MAIN; michael@0: } michael@0: michael@0: buffer->merge_clusters (start, end); michael@0: /* Sit tight, rock 'n roll! */ michael@0: hb_bubble_sort (info + start, end - start, compare_sea_order); michael@0: } michael@0: michael@0: static void michael@0: initial_reordering_broken_cluster (const hb_ot_shape_plan_t *plan, michael@0: hb_face_t *face, michael@0: hb_buffer_t *buffer, michael@0: unsigned int start, unsigned int end) michael@0: { michael@0: /* We already inserted dotted-circles, so just call the consonant_syllable. */ michael@0: initial_reordering_consonant_syllable (plan, face, buffer, start, end); michael@0: } michael@0: michael@0: static void michael@0: initial_reordering_non_sea_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED, michael@0: hb_face_t *face HB_UNUSED, michael@0: hb_buffer_t *buffer HB_UNUSED, michael@0: unsigned int start HB_UNUSED, unsigned int end HB_UNUSED) michael@0: { michael@0: /* Nothing to do right now. If we ever switch to using the output michael@0: * buffer in the reordering process, we'd need to next_glyph() here. */ michael@0: } michael@0: michael@0: michael@0: static void michael@0: initial_reordering_syllable (const hb_ot_shape_plan_t *plan, michael@0: hb_face_t *face, michael@0: hb_buffer_t *buffer, michael@0: unsigned int start, unsigned int end) michael@0: { michael@0: syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F); michael@0: switch (syllable_type) { michael@0: case consonant_syllable: initial_reordering_consonant_syllable (plan, face, buffer, start, end); return; michael@0: case broken_cluster: initial_reordering_broken_cluster (plan, face, buffer, start, end); return; michael@0: case non_sea_cluster: initial_reordering_non_sea_cluster (plan, face, buffer, start, end); return; michael@0: } michael@0: } michael@0: michael@0: static inline void michael@0: insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED, michael@0: hb_font_t *font, michael@0: hb_buffer_t *buffer) michael@0: { michael@0: /* Note: This loop is extra overhead, but should not be measurable. */ michael@0: bool has_broken_syllables = false; michael@0: unsigned int count = buffer->len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: if ((buffer->info[i].syllable() & 0x0F) == broken_cluster) { michael@0: has_broken_syllables = true; michael@0: break; michael@0: } michael@0: if (likely (!has_broken_syllables)) michael@0: return; michael@0: michael@0: michael@0: hb_codepoint_t dottedcircle_glyph; michael@0: if (!font->get_glyph (0x25CC, 0, &dottedcircle_glyph)) michael@0: return; michael@0: michael@0: hb_glyph_info_t dottedcircle = {0}; michael@0: dottedcircle.codepoint = 0x25CC; michael@0: set_sea_properties (dottedcircle); michael@0: dottedcircle.codepoint = dottedcircle_glyph; michael@0: michael@0: buffer->clear_output (); michael@0: michael@0: buffer->idx = 0; michael@0: unsigned int last_syllable = 0; michael@0: while (buffer->idx < buffer->len) michael@0: { michael@0: unsigned int syllable = buffer->cur().syllable(); michael@0: syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F); michael@0: if (unlikely (last_syllable != syllable && syllable_type == broken_cluster)) michael@0: { michael@0: last_syllable = syllable; michael@0: michael@0: hb_glyph_info_t info = dottedcircle; michael@0: info.cluster = buffer->cur().cluster; michael@0: info.mask = buffer->cur().mask; michael@0: info.syllable() = buffer->cur().syllable(); michael@0: michael@0: buffer->output_info (info); michael@0: } michael@0: else michael@0: buffer->next_glyph (); michael@0: } michael@0: michael@0: buffer->swap_buffers (); michael@0: } michael@0: michael@0: static void michael@0: initial_reordering (const hb_ot_shape_plan_t *plan, michael@0: hb_font_t *font, michael@0: hb_buffer_t *buffer) michael@0: { michael@0: insert_dotted_circles (plan, font, buffer); michael@0: michael@0: hb_glyph_info_t *info = buffer->info; michael@0: unsigned int count = buffer->len; michael@0: if (unlikely (!count)) return; michael@0: unsigned int last = 0; michael@0: unsigned int last_syllable = info[0].syllable(); michael@0: for (unsigned int i = 1; i < count; i++) michael@0: if (last_syllable != info[i].syllable()) { michael@0: initial_reordering_syllable (plan, font->face, buffer, last, i); michael@0: last = i; michael@0: last_syllable = info[last].syllable(); michael@0: } michael@0: initial_reordering_syllable (plan, font->face, buffer, last, count); michael@0: } michael@0: michael@0: static void michael@0: final_reordering (const hb_ot_shape_plan_t *plan, michael@0: hb_font_t *font HB_UNUSED, michael@0: hb_buffer_t *buffer) michael@0: { michael@0: hb_glyph_info_t *info = buffer->info; michael@0: unsigned int count = buffer->len; michael@0: michael@0: /* Zero syllables now... */ michael@0: for (unsigned int i = 0; i < count; i++) michael@0: info[i].syllable() = 0; michael@0: michael@0: HB_BUFFER_DEALLOCATE_VAR (buffer, sea_category); michael@0: HB_BUFFER_DEALLOCATE_VAR (buffer, sea_position); michael@0: } michael@0: michael@0: michael@0: const hb_ot_complex_shaper_t _hb_ot_complex_shaper_sea = michael@0: { michael@0: "sea", michael@0: collect_features_sea, michael@0: override_features_sea, michael@0: NULL, /* data_create */ michael@0: NULL, /* data_destroy */ michael@0: NULL, /* preprocess_text */ michael@0: HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT, michael@0: NULL, /* decompose */ michael@0: NULL, /* compose */ michael@0: setup_masks_sea, michael@0: HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE, michael@0: false, /* fallback_position */ michael@0: };