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 myanmar_category() complex_var_u8_0() /* myanmar_category_t */ michael@0: #define myanmar_position() complex_var_u8_1() /* myanmar_position_t */ michael@0: michael@0: michael@0: /* michael@0: * Myanmar shaper. 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('r','p','h','f'), michael@0: HB_TAG('p','r','e','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: /* Pre-release version of Windows 8 Myanmar font had abvm,blwm michael@0: * features. The released Windows 8 version of the font (as well michael@0: * as the released spec) used 'mark' instead. The Windows 8 michael@0: * shaper however didn't apply 'mark' but did apply 'mkmk'. michael@0: * Perhaps it applied abvm/blwm. This was fixed in a Windows 8 michael@0: * update, so now it applies mark/mkmk. We are guessing that michael@0: * it still applies abvm/blwm too. michael@0: */ michael@0: HB_TAG('a','b','v','m'), michael@0: HB_TAG('b','l','w','m'), 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_myanmar (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: 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_myanmar (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: punctuation_cluster, michael@0: broken_cluster, michael@0: non_myanmar_cluster, michael@0: }; michael@0: michael@0: #include "hb-ot-shape-complex-myanmar-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 myanmar_category_t { michael@0: OT_As = 18, /* Asat */ michael@0: OT_D = 19, /* Digits except zero */ michael@0: OT_D0 = 20, /* Digit zero */ michael@0: OT_DB = OT_N, /* Dot below */ michael@0: OT_GB = OT_DOTTEDCIRCLE, michael@0: OT_MH = 21, /* Various consonant medial types */ michael@0: OT_MR = 22, /* Various consonant medial types */ michael@0: OT_MW = 23, /* Various consonant medial types */ michael@0: OT_MY = 24, /* Various consonant medial types */ michael@0: OT_PT = 25, /* Pwo and other tones */ michael@0: OT_VAbv = 26, michael@0: OT_VBlw = 27, michael@0: OT_VPre = 28, michael@0: OT_VPst = 29, michael@0: OT_VS = 30, /* Variation selectors */ michael@0: OT_P = 31 /* Punctuation */ michael@0: }; michael@0: michael@0: michael@0: static inline bool michael@0: is_one_of (const hb_glyph_info_t &info, unsigned int flags) michael@0: { michael@0: /* If it ligated, all bets are off. */ michael@0: if (_hb_glyph_info_ligated (&info)) return false; michael@0: return !!(FLAG (info.myanmar_category()) & flags); michael@0: } michael@0: michael@0: /* Note: michael@0: * michael@0: * We treat Vowels and placeholders as if they were consonants. This is safe because Vowels michael@0: * cannot happen in a consonant syllable. The plus side however is, we can call the michael@0: * consonant syllable logic from the vowel syllable function and get it all right! */ michael@0: #define CONSONANT_FLAGS (FLAG (OT_C) | FLAG (OT_CM) | FLAG (OT_Ra) | FLAG (OT_V) | FLAG (OT_NBSP) | FLAG (OT_GB)) michael@0: static inline bool michael@0: is_consonant (const hb_glyph_info_t &info) michael@0: { michael@0: return is_one_of (info, CONSONANT_FLAGS); michael@0: } michael@0: michael@0: michael@0: static inline void michael@0: set_myanmar_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: /* Myanmar michael@0: * http://www.microsoft.com/typography/OpenTypeDev/myanmar/intro.htm#analyze michael@0: */ michael@0: if (unlikely (hb_in_range (u, 0xFE00, 0xFE0F))) michael@0: cat = (indic_category_t) OT_VS; michael@0: else if (unlikely (u == 0x200C)) cat = (indic_category_t) OT_ZWNJ; michael@0: else if (unlikely (u == 0x200D)) cat = (indic_category_t) OT_ZWJ; michael@0: michael@0: switch (u) michael@0: { michael@0: case 0x104E: michael@0: cat = (indic_category_t) OT_C; /* The spec says C, IndicSyllableCategory doesn't have. */ michael@0: break; michael@0: michael@0: case 0x002D: case 0x00A0: case 0x00D7: case 0x2012: michael@0: case 0x2013: case 0x2014: case 0x2015: case 0x2022: michael@0: case 0x25CC: case 0x25FB: case 0x25FC: case 0x25FD: michael@0: case 0x25FE: michael@0: cat = (indic_category_t) OT_GB; michael@0: break; michael@0: michael@0: case 0x1004: case 0x101B: case 0x105A: michael@0: cat = (indic_category_t) OT_Ra; michael@0: break; michael@0: michael@0: case 0x1032: case 0x1036: michael@0: cat = (indic_category_t) OT_A; michael@0: break; michael@0: michael@0: case 0x103A: michael@0: cat = (indic_category_t) OT_As; michael@0: break; michael@0: michael@0: case 0x1041: case 0x1042: case 0x1043: case 0x1044: michael@0: case 0x1045: case 0x1046: case 0x1047: case 0x1048: michael@0: case 0x1049: case 0x1090: case 0x1091: case 0x1092: michael@0: case 0x1093: case 0x1094: case 0x1095: case 0x1096: michael@0: case 0x1097: case 0x1098: case 0x1099: michael@0: cat = (indic_category_t) OT_D; michael@0: break; michael@0: michael@0: case 0x1040: michael@0: cat = (indic_category_t) OT_D; /* XXX The spec says D0, but Uniscribe doesn't seem to do. */ michael@0: break; michael@0: michael@0: case 0x103E: case 0x1060: michael@0: cat = (indic_category_t) OT_MH; michael@0: break; michael@0: michael@0: case 0x103C: michael@0: cat = (indic_category_t) OT_MR; michael@0: break; michael@0: michael@0: case 0x103D: case 0x1082: michael@0: cat = (indic_category_t) OT_MW; michael@0: break; michael@0: michael@0: case 0x103B: case 0x105E: case 0x105F: michael@0: cat = (indic_category_t) OT_MY; michael@0: break; michael@0: michael@0: case 0x1063: case 0x1064: case 0x1069: case 0x106A: michael@0: case 0x106B: case 0x106C: case 0x106D: case 0xAA7B: michael@0: cat = (indic_category_t) OT_PT; michael@0: break; michael@0: michael@0: case 0x1038: case 0x1087: case 0x1088: case 0x1089: michael@0: case 0x108A: case 0x108B: case 0x108C: case 0x108D: michael@0: case 0x108F: case 0x109A: case 0x109B: case 0x109C: michael@0: cat = (indic_category_t) OT_SM; michael@0: break; michael@0: michael@0: case 0x104A: case 0x104B: michael@0: cat = (indic_category_t) OT_P; michael@0: break; michael@0: } 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; michael@0: pos = POS_PRE_M; 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.myanmar_category() = (myanmar_category_t) cat; michael@0: info.myanmar_position() = pos; michael@0: } michael@0: michael@0: michael@0: michael@0: static void michael@0: setup_masks_myanmar (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, myanmar_category); michael@0: HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_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_myanmar_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_myanmar_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb) michael@0: { michael@0: int a = pa->myanmar_position(); michael@0: int b = pb->myanmar_position(); michael@0: michael@0: return a < b ? -1 : a == b ? 0 : +1; michael@0: } michael@0: michael@0: michael@0: /* Rules from: michael@0: * http://www.microsoft.com/typography/OpenTypeDev/myanmar/intro.htm */ 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: michael@0: unsigned int base = end; michael@0: bool has_reph = false; michael@0: michael@0: { michael@0: unsigned int limit = start; michael@0: if (start + 3 <= end && michael@0: info[start ].myanmar_category() == OT_Ra && michael@0: info[start+1].myanmar_category() == OT_As && michael@0: info[start+2].myanmar_category() == OT_H) michael@0: { michael@0: limit += 3; michael@0: base = start; michael@0: has_reph = true; michael@0: } michael@0: michael@0: { michael@0: if (!has_reph) michael@0: base = limit; michael@0: michael@0: for (unsigned int i = limit; i < end; i++) michael@0: if (is_consonant (info[i])) michael@0: { michael@0: base = i; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Reorder! */ michael@0: { michael@0: unsigned int i = start; michael@0: for (; i < start + (has_reph ? 3 : 0); i++) michael@0: info[i].myanmar_position() = POS_AFTER_MAIN; michael@0: for (; i < base; i++) michael@0: info[i].myanmar_position() = POS_PRE_C; michael@0: if (i < end) michael@0: { michael@0: info[i].myanmar_position() = POS_BASE_C; michael@0: i++; michael@0: } michael@0: indic_position_t pos = POS_AFTER_MAIN; michael@0: /* The following loop may be ugly, but it implements all of michael@0: * Myanmar reordering! */ michael@0: for (; i < end; i++) michael@0: { michael@0: if (info[i].myanmar_category() == OT_MR) /* Pre-base reordering */ michael@0: { michael@0: info[i].myanmar_position() = POS_PRE_C; michael@0: continue; michael@0: } michael@0: if (info[i].myanmar_position() < POS_BASE_C) /* Left matra */ michael@0: { michael@0: continue; michael@0: } michael@0: michael@0: if (pos == POS_AFTER_MAIN && info[i].myanmar_category() == OT_VBlw) michael@0: { michael@0: pos = POS_BELOW_C; michael@0: info[i].myanmar_position() = pos; michael@0: continue; michael@0: } michael@0: michael@0: if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_A) michael@0: { michael@0: info[i].myanmar_position() = POS_BEFORE_SUB; michael@0: continue; michael@0: } michael@0: if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_VBlw) michael@0: { michael@0: info[i].myanmar_position() = pos; michael@0: continue; michael@0: } michael@0: if (pos == POS_BELOW_C && info[i].myanmar_category() != OT_A) michael@0: { michael@0: pos = POS_AFTER_SUB; michael@0: info[i].myanmar_position() = pos; michael@0: continue; michael@0: } michael@0: info[i].myanmar_position() = pos; michael@0: } 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_myanmar_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_punctuation_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: static void michael@0: initial_reordering_non_myanmar_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 punctuation_cluster: initial_reordering_punctuation_cluster (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_myanmar_cluster: initial_reordering_non_myanmar_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_myanmar_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, myanmar_category); michael@0: HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_position); michael@0: } michael@0: michael@0: michael@0: const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar = michael@0: { michael@0: "myanmar", michael@0: collect_features_myanmar, michael@0: override_features_myanmar, 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_myanmar, michael@0: HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY, michael@0: false, /* fallback_position */ michael@0: };