michael@0: /* michael@0: * Copyright © 2011,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-indic-private.hh" michael@0: #include "hb-ot-layout-private.hh" michael@0: michael@0: /* buffer var allocations */ michael@0: #define indic_category() complex_var_u8_0() /* indic_category_t */ michael@0: #define indic_position() complex_var_u8_1() /* indic_position_t */ michael@0: michael@0: michael@0: /* michael@0: * Indic shaper. michael@0: */ michael@0: michael@0: michael@0: #define IN_HALF_BLOCK(u, Base) (((u) & ~0x7F) == (Base)) michael@0: michael@0: #define IS_DEVA(u) (IN_HALF_BLOCK (u, 0x0900)) michael@0: #define IS_BENG(u) (IN_HALF_BLOCK (u, 0x0980)) michael@0: #define IS_GURU(u) (IN_HALF_BLOCK (u, 0x0A00)) michael@0: #define IS_GUJR(u) (IN_HALF_BLOCK (u, 0x0A80)) michael@0: #define IS_ORYA(u) (IN_HALF_BLOCK (u, 0x0B00)) michael@0: #define IS_TAML(u) (IN_HALF_BLOCK (u, 0x0B80)) michael@0: #define IS_TELU(u) (IN_HALF_BLOCK (u, 0x0C00)) michael@0: #define IS_KNDA(u) (IN_HALF_BLOCK (u, 0x0C80)) michael@0: #define IS_MLYM(u) (IN_HALF_BLOCK (u, 0x0D00)) michael@0: #define IS_SINH(u) (IN_HALF_BLOCK (u, 0x0D80)) michael@0: #define IS_KHMR(u) (IN_HALF_BLOCK (u, 0x1780)) michael@0: michael@0: michael@0: #define MATRA_POS_LEFT(u) POS_PRE_M michael@0: #define MATRA_POS_RIGHT(u) ( \ michael@0: IS_DEVA(u) ? POS_AFTER_SUB : \ michael@0: IS_BENG(u) ? POS_AFTER_POST : \ michael@0: IS_GURU(u) ? POS_AFTER_POST : \ michael@0: IS_GUJR(u) ? POS_AFTER_POST : \ michael@0: IS_ORYA(u) ? POS_AFTER_POST : \ michael@0: IS_TAML(u) ? POS_AFTER_POST : \ michael@0: IS_TELU(u) ? (u <= 0x0C42 ? POS_BEFORE_SUB : POS_AFTER_SUB) : \ michael@0: IS_KNDA(u) ? (u < 0x0CC3 || u > 0xCD6 ? POS_BEFORE_SUB : POS_AFTER_SUB) : \ michael@0: IS_MLYM(u) ? POS_AFTER_POST : \ michael@0: IS_SINH(u) ? POS_AFTER_SUB : \ michael@0: IS_KHMR(u) ? POS_AFTER_POST : \ michael@0: /*default*/ POS_AFTER_SUB \ michael@0: ) michael@0: #define MATRA_POS_TOP(u) ( /* BENG and MLYM don't have top matras. */ \ michael@0: IS_DEVA(u) ? POS_AFTER_SUB : \ michael@0: IS_GURU(u) ? POS_AFTER_POST : /* Deviate from spec */ \ michael@0: IS_GUJR(u) ? POS_AFTER_SUB : \ michael@0: IS_ORYA(u) ? POS_AFTER_MAIN : \ michael@0: IS_TAML(u) ? POS_AFTER_SUB : \ michael@0: IS_TELU(u) ? POS_BEFORE_SUB : \ michael@0: IS_KNDA(u) ? POS_BEFORE_SUB : \ michael@0: IS_SINH(u) ? POS_AFTER_SUB : \ michael@0: IS_KHMR(u) ? POS_AFTER_POST : \ michael@0: /*default*/ POS_AFTER_SUB \ michael@0: ) michael@0: #define MATRA_POS_BOTTOM(u) ( \ michael@0: IS_DEVA(u) ? POS_AFTER_SUB : \ michael@0: IS_BENG(u) ? POS_AFTER_SUB : \ michael@0: IS_GURU(u) ? POS_AFTER_POST : \ michael@0: IS_GUJR(u) ? POS_AFTER_POST : \ michael@0: IS_ORYA(u) ? POS_AFTER_SUB : \ michael@0: IS_TAML(u) ? POS_AFTER_POST : \ michael@0: IS_TELU(u) ? POS_BEFORE_SUB : \ michael@0: IS_KNDA(u) ? POS_BEFORE_SUB : \ michael@0: IS_MLYM(u) ? POS_AFTER_POST : \ michael@0: IS_SINH(u) ? POS_AFTER_SUB : \ michael@0: IS_KHMR(u) ? POS_AFTER_POST : \ michael@0: /*default*/ POS_AFTER_SUB \ michael@0: ) michael@0: michael@0: static inline indic_position_t michael@0: matra_position (hb_codepoint_t u, indic_position_t side) michael@0: { michael@0: switch ((int) side) michael@0: { michael@0: case POS_PRE_C: return MATRA_POS_LEFT (u); michael@0: case POS_POST_C: return MATRA_POS_RIGHT (u); michael@0: case POS_ABOVE_C: return MATRA_POS_TOP (u); michael@0: case POS_BELOW_C: return MATRA_POS_BOTTOM (u); michael@0: }; michael@0: return side; michael@0: } michael@0: michael@0: /* XXX michael@0: * This is a hack for now. We should move this data into the main Indic table. michael@0: * Or completely remove it and just check in the tables. michael@0: */ michael@0: static const hb_codepoint_t ra_chars[] = { michael@0: 0x0930, /* Devanagari */ michael@0: 0x09B0, /* Bengali */ michael@0: 0x09F0, /* Bengali */ michael@0: 0x0A30, /* Gurmukhi */ /* No Reph */ michael@0: 0x0AB0, /* Gujarati */ michael@0: 0x0B30, /* Oriya */ michael@0: 0x0BB0, /* Tamil */ /* No Reph */ michael@0: 0x0C30, /* Telugu */ /* Reph formed only with ZWJ */ michael@0: 0x0CB0, /* Kannada */ michael@0: 0x0D30, /* Malayalam */ /* No Reph, Logical Repha */ michael@0: michael@0: 0x0DBB, /* Sinhala */ /* Reph formed only with ZWJ */ michael@0: michael@0: 0x179A, /* Khmer */ /* No Reph, Visual Repha */ michael@0: }; michael@0: michael@0: static inline bool michael@0: is_ra (hb_codepoint_t u) michael@0: { michael@0: for (unsigned int i = 0; i < ARRAY_LENGTH (ra_chars); i++) michael@0: if (u == ra_chars[i]) michael@0: return true; michael@0: return false; 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.indic_category()) & flags); michael@0: } michael@0: michael@0: #define JOINER_FLAGS (FLAG (OT_ZWJ) | FLAG (OT_ZWNJ)) michael@0: static inline bool michael@0: is_joiner (const hb_glyph_info_t &info) michael@0: { michael@0: return is_one_of (info, JOINER_FLAGS); michael@0: } michael@0: michael@0: #define MEDIAL_FLAGS (FLAG (OT_CM) | FLAG (OT_CM2)) 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_Ra) | MEDIAL_FLAGS | FLAG (OT_V) | FLAG (OT_NBSP) | FLAG (OT_DOTTEDCIRCLE)) 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: #define HALANT_OR_COENG_FLAGS (FLAG (OT_H) | FLAG (OT_Coeng)) michael@0: static inline bool michael@0: is_halant_or_coeng (const hb_glyph_info_t &info) michael@0: { michael@0: return is_one_of (info, HALANT_OR_COENG_FLAGS); michael@0: } michael@0: michael@0: static inline void michael@0: set_indic_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: michael@0: /* michael@0: * Re-assign category michael@0: */ michael@0: michael@0: michael@0: /* The spec says U+0952 is OT_A. However, testing shows that Uniscribe michael@0: * treats U+0951..U+0954 all behave similarly. michael@0: * TESTS: michael@0: * U+092E,U+0947,U+0952 michael@0: * U+092E,U+0952,U+0947 michael@0: * U+092E,U+0947,U+0951 michael@0: * U+092E,U+0951,U+0947 michael@0: */ michael@0: if (unlikely (hb_in_range (u, 0x0951, 0x0954))) michael@0: cat = OT_A; michael@0: michael@0: if (unlikely (u == 0x17D1)) michael@0: cat = OT_X; michael@0: if (cat == OT_X && michael@0: unlikely (hb_in_range (u, 0x17CB, 0x17D3))) /* Khmer Various signs */ michael@0: { michael@0: /* These are like Top Matras. */ michael@0: cat = OT_M; michael@0: pos = POS_ABOVE_C; michael@0: } michael@0: if (u == 0x17C6) /* Khmer Bindu doesn't like to be repositioned. */ michael@0: cat = OT_N; michael@0: michael@0: if (unlikely (u == 0x17D2)) cat = OT_Coeng; /* Khmer coeng */ michael@0: else if (unlikely (u == 0x200C)) cat = OT_ZWNJ; michael@0: else if (unlikely (u == 0x200D)) cat = OT_ZWJ; michael@0: else if (unlikely (u == 0x25CC)) cat = OT_DOTTEDCIRCLE; michael@0: else if (unlikely (u == 0x0A71)) cat = OT_SM; /* GURMUKHI ADDAK. Move it to the end. */ michael@0: else if (unlikely (u == 0xA982)) cat = OT_SM; /* Javanese repha. */ michael@0: else if (unlikely (u == 0xA9BE)) cat = OT_CM2; /* Javanese medial ya. */ michael@0: else if (unlikely (u == 0xA9BD)) { cat = OT_M; pos = POS_POST_C; } /* Javanese vocalic r. */ michael@0: michael@0: if (cat == OT_Repha) { michael@0: /* There are two kinds of characters marked as Repha: michael@0: * - The ones that are GenCat=Mn are already positioned visually, ie. after base. (eg. Khmer) michael@0: * - The ones that are GenCat=Lo is encoded logically, ie. beginning of syllable. (eg. Malayalam) michael@0: * michael@0: * We recategorize the first kind to look like a Nukta and attached to the base directly. michael@0: */ michael@0: if (_hb_glyph_info_get_general_category (&info) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) michael@0: cat = OT_N; michael@0: } michael@0: michael@0: michael@0: michael@0: /* michael@0: * Re-assign position. michael@0: */ michael@0: michael@0: if ((FLAG (cat) & CONSONANT_FLAGS)) michael@0: { michael@0: pos = POS_BASE_C; michael@0: if (is_ra (u)) michael@0: cat = OT_Ra; michael@0: } michael@0: else if (cat == OT_M) michael@0: { michael@0: pos = matra_position (u, pos); michael@0: } michael@0: else if ((FLAG (cat) & (FLAG (OT_SM) | FLAG (OT_VD) | FLAG (OT_A) | FLAG (OT_Avag)))) michael@0: { michael@0: pos = POS_SMVD; michael@0: } michael@0: michael@0: if (unlikely (u == 0x0B01)) pos = POS_BEFORE_SUB; /* Oriya Bindu is BeforeSub in the spec. */ michael@0: michael@0: michael@0: michael@0: info.indic_category() = cat; michael@0: info.indic_position() = pos; michael@0: } michael@0: michael@0: /* michael@0: * Things above this line should ideally be moved to the Indic table itself. michael@0: */ michael@0: michael@0: michael@0: /* michael@0: * Indic configurations. Note that we do not want to keep every single script-specific michael@0: * behavior in these tables necessarily. This should mainly be used for per-script michael@0: * properties that are cheaper keeping here, than in the code. Ie. if, say, one and michael@0: * only one script has an exception, that one script can be if'ed directly in the code, michael@0: * instead of adding a new flag in these structs. michael@0: */ michael@0: michael@0: enum base_position_t { michael@0: BASE_POS_FIRST, michael@0: BASE_POS_LAST_SINHALA, michael@0: BASE_POS_LAST michael@0: }; michael@0: enum reph_position_t { michael@0: REPH_POS_AFTER_MAIN = POS_AFTER_MAIN, michael@0: REPH_POS_BEFORE_SUB = POS_BEFORE_SUB, michael@0: REPH_POS_AFTER_SUB = POS_AFTER_SUB, michael@0: REPH_POS_BEFORE_POST = POS_BEFORE_POST, michael@0: REPH_POS_AFTER_POST = POS_AFTER_POST, michael@0: REPH_POS_DONT_CARE = POS_RA_TO_BECOME_REPH michael@0: }; michael@0: enum reph_mode_t { michael@0: REPH_MODE_IMPLICIT, /* Reph formed out of initial Ra,H sequence. */ michael@0: REPH_MODE_EXPLICIT, /* Reph formed out of initial Ra,H,ZWJ sequence. */ michael@0: REPH_MODE_VIS_REPHA, /* Encoded Repha character, no reordering needed. */ michael@0: REPH_MODE_LOG_REPHA /* Encoded Repha character, needs reordering. */ michael@0: }; michael@0: enum blwf_mode_t { michael@0: BLWF_MODE_PRE_AND_POST, /* Below-forms feature applied to pre-base and post-base. */ michael@0: BLWF_MODE_POST_ONLY /* Below-forms feature applied to post-base only. */ michael@0: }; michael@0: enum pref_len_t { michael@0: PREF_LEN_1 = 1, michael@0: PREF_LEN_2 = 2, michael@0: PREF_LEN_DONT_CARE = PREF_LEN_2 michael@0: }; michael@0: struct indic_config_t michael@0: { michael@0: hb_script_t script; michael@0: bool has_old_spec; michael@0: hb_codepoint_t virama; michael@0: base_position_t base_pos; michael@0: reph_position_t reph_pos; michael@0: reph_mode_t reph_mode; michael@0: blwf_mode_t blwf_mode; michael@0: pref_len_t pref_len; michael@0: }; michael@0: michael@0: static const indic_config_t indic_configs[] = michael@0: { michael@0: /* Default. Should be first. */ michael@0: {HB_SCRIPT_INVALID, false, 0,BASE_POS_LAST, REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST, PREF_LEN_1}, michael@0: {HB_SCRIPT_DEVANAGARI,true, 0x094D,BASE_POS_LAST, REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST, PREF_LEN_DONT_CARE}, michael@0: {HB_SCRIPT_BENGALI, true, 0x09CD,BASE_POS_LAST, REPH_POS_AFTER_SUB, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST, PREF_LEN_DONT_CARE}, michael@0: {HB_SCRIPT_GURMUKHI, true, 0x0A4D,BASE_POS_LAST, REPH_POS_BEFORE_SUB, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST, PREF_LEN_DONT_CARE}, michael@0: {HB_SCRIPT_GUJARATI, true, 0x0ACD,BASE_POS_LAST, REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST, PREF_LEN_DONT_CARE}, michael@0: {HB_SCRIPT_ORIYA, true, 0x0B4D,BASE_POS_LAST, REPH_POS_AFTER_MAIN, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST, PREF_LEN_DONT_CARE}, michael@0: {HB_SCRIPT_TAMIL, true, 0x0BCD,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST, PREF_LEN_2}, michael@0: {HB_SCRIPT_TELUGU, true, 0x0C4D,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_EXPLICIT, BLWF_MODE_POST_ONLY, PREF_LEN_2}, michael@0: {HB_SCRIPT_KANNADA, true, 0x0CCD,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_IMPLICIT, BLWF_MODE_POST_ONLY, PREF_LEN_2}, michael@0: {HB_SCRIPT_MALAYALAM, true, 0x0D4D,BASE_POS_LAST, REPH_POS_AFTER_MAIN, REPH_MODE_LOG_REPHA,BLWF_MODE_PRE_AND_POST, PREF_LEN_2}, michael@0: {HB_SCRIPT_SINHALA, false,0x0DCA,BASE_POS_LAST_SINHALA, michael@0: REPH_POS_AFTER_MAIN, REPH_MODE_EXPLICIT, BLWF_MODE_PRE_AND_POST, PREF_LEN_DONT_CARE}, michael@0: {HB_SCRIPT_KHMER, false,0x17D2,BASE_POS_FIRST,REPH_POS_DONT_CARE, REPH_MODE_VIS_REPHA,BLWF_MODE_PRE_AND_POST, PREF_LEN_2}, michael@0: {HB_SCRIPT_JAVANESE, false,0xA9C0,BASE_POS_FIRST,REPH_POS_DONT_CARE, REPH_MODE_VIS_REPHA,BLWF_MODE_PRE_AND_POST, PREF_LEN_1}, michael@0: }; michael@0: michael@0: michael@0: michael@0: /* michael@0: * Indic shaper. michael@0: */ michael@0: michael@0: struct feature_list_t { michael@0: hb_tag_t tag; michael@0: hb_ot_map_feature_flags_t flags; michael@0: }; michael@0: michael@0: static const feature_list_t michael@0: indic_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('n','u','k','t'), F_GLOBAL}, michael@0: {HB_TAG('a','k','h','n'), F_GLOBAL}, michael@0: {HB_TAG('r','p','h','f'), F_NONE}, michael@0: {HB_TAG('r','k','r','f'), F_GLOBAL}, michael@0: {HB_TAG('p','r','e','f'), F_NONE}, michael@0: {HB_TAG('b','l','w','f'), F_NONE}, michael@0: {HB_TAG('a','b','v','f'), F_NONE}, michael@0: {HB_TAG('h','a','l','f'), F_NONE}, michael@0: {HB_TAG('p','s','t','f'), F_NONE}, michael@0: {HB_TAG('v','a','t','u'), F_GLOBAL}, michael@0: {HB_TAG('c','j','c','t'), F_GLOBAL}, michael@0: {HB_TAG('c','f','a','r'), F_NONE}, michael@0: /* michael@0: * Other features. michael@0: * These features are applied all at once, after final_reordering. michael@0: * Default Bengali font in Windows for example has intermixed michael@0: * lookups for init,pres,abvs,blws features. michael@0: */ michael@0: {HB_TAG('i','n','i','t'), F_NONE}, michael@0: {HB_TAG('p','r','e','s'), F_GLOBAL}, michael@0: {HB_TAG('a','b','v','s'), F_GLOBAL}, michael@0: {HB_TAG('b','l','w','s'), F_GLOBAL}, michael@0: {HB_TAG('p','s','t','s'), F_GLOBAL}, michael@0: {HB_TAG('h','a','l','n'), F_GLOBAL}, michael@0: /* Positioning features, though we don't care about the types. */ michael@0: {HB_TAG('d','i','s','t'), F_GLOBAL}, michael@0: {HB_TAG('a','b','v','m'), F_GLOBAL}, michael@0: {HB_TAG('b','l','w','m'), F_GLOBAL}, michael@0: }; michael@0: michael@0: /* michael@0: * Must be in the same order as the indic_features array. michael@0: */ michael@0: enum { michael@0: _NUKT, michael@0: _AKHN, michael@0: RPHF, michael@0: _RKRF, michael@0: PREF, michael@0: BLWF, michael@0: ABVF, michael@0: HALF, michael@0: PSTF, michael@0: _VATU, michael@0: _CJCT, michael@0: CFAR, michael@0: michael@0: INIT, michael@0: _PRES, michael@0: _ABVS, michael@0: _BLWS, michael@0: _PSTS, michael@0: _HALN, michael@0: _DIST, michael@0: _ABVM, michael@0: _BLWM, michael@0: michael@0: INDIC_NUM_FEATURES, michael@0: INDIC_BASIC_FEATURES = INIT /* Don't forget to update this! */ 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: static void michael@0: clear_syllables (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_indic (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: unsigned int i = 0; michael@0: map->add_gsub_pause (initial_reordering); michael@0: for (; i < INDIC_BASIC_FEATURES; i++) { michael@0: map->add_feature (indic_features[i].tag, 1, indic_features[i].flags | F_MANUAL_ZWJ); michael@0: map->add_gsub_pause (NULL); michael@0: } michael@0: map->add_gsub_pause (final_reordering); michael@0: for (; i < INDIC_NUM_FEATURES; i++) { michael@0: map->add_feature (indic_features[i].tag, 1, indic_features[i].flags | F_MANUAL_ZWJ); michael@0: } michael@0: michael@0: map->add_global_bool_feature (HB_TAG('c','a','l','t')); michael@0: map->add_global_bool_feature (HB_TAG('c','l','i','g')); michael@0: michael@0: map->add_gsub_pause (clear_syllables); michael@0: } michael@0: michael@0: static void michael@0: override_features_indic (hb_ot_shape_planner_t *plan) michael@0: { michael@0: /* Uniscribe does not apply 'kern' in Khmer. */ michael@0: if (hb_options ().uniscribe_bug_compatible) michael@0: { michael@0: switch ((hb_tag_t) plan->props.script) michael@0: { michael@0: case HB_SCRIPT_KHMER: michael@0: plan->map.add_feature (HB_TAG('k','e','r','n'), 0, F_GLOBAL); michael@0: break; michael@0: } michael@0: } 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: struct would_substitute_feature_t michael@0: { michael@0: inline void init (const hb_ot_map_t *map, hb_tag_t feature_tag, bool zero_context_) michael@0: { michael@0: zero_context = zero_context_; michael@0: map->get_stage_lookups (0/*GSUB*/, michael@0: map->get_feature_stage (0/*GSUB*/, feature_tag), michael@0: &lookups, &count); michael@0: } michael@0: michael@0: inline bool would_substitute (const hb_codepoint_t *glyphs, michael@0: unsigned int glyphs_count, michael@0: hb_face_t *face) const michael@0: { michael@0: for (unsigned int i = 0; i < count; i++) michael@0: if (hb_ot_layout_lookup_would_substitute_fast (face, lookups[i].index, glyphs, glyphs_count, zero_context)) michael@0: return true; michael@0: return false; michael@0: } michael@0: michael@0: private: michael@0: const hb_ot_map_t::lookup_map_t *lookups; michael@0: unsigned int count; michael@0: bool zero_context; michael@0: }; michael@0: michael@0: struct indic_shape_plan_t michael@0: { michael@0: ASSERT_POD (); michael@0: michael@0: inline bool get_virama_glyph (hb_font_t *font, hb_codepoint_t *pglyph) const michael@0: { michael@0: hb_codepoint_t glyph = virama_glyph; michael@0: if (unlikely (virama_glyph == (hb_codepoint_t) -1)) michael@0: { michael@0: if (!config->virama || !font->get_glyph (config->virama, 0, &glyph)) michael@0: glyph = 0; michael@0: /* Technically speaking, the spec says we should apply 'locl' to virama too. michael@0: * Maybe one day... */ michael@0: michael@0: /* Our get_glyph() function needs a font, so we can't get the virama glyph michael@0: * during shape planning... Instead, overwrite it here. It's safe. Don't worry! */ michael@0: (const_cast (this))->virama_glyph = glyph; michael@0: } michael@0: michael@0: *pglyph = glyph; michael@0: return glyph != 0; michael@0: } michael@0: michael@0: const indic_config_t *config; michael@0: michael@0: bool is_old_spec; michael@0: hb_codepoint_t virama_glyph; michael@0: michael@0: would_substitute_feature_t rphf; michael@0: would_substitute_feature_t pref; michael@0: would_substitute_feature_t blwf; michael@0: would_substitute_feature_t pstf; michael@0: michael@0: hb_mask_t mask_array[INDIC_NUM_FEATURES]; michael@0: }; michael@0: michael@0: static void * michael@0: data_create_indic (const hb_ot_shape_plan_t *plan) michael@0: { michael@0: indic_shape_plan_t *indic_plan = (indic_shape_plan_t *) calloc (1, sizeof (indic_shape_plan_t)); michael@0: if (unlikely (!indic_plan)) michael@0: return NULL; michael@0: michael@0: indic_plan->config = &indic_configs[0]; michael@0: for (unsigned int i = 1; i < ARRAY_LENGTH (indic_configs); i++) michael@0: if (plan->props.script == indic_configs[i].script) { michael@0: indic_plan->config = &indic_configs[i]; michael@0: break; michael@0: } michael@0: michael@0: indic_plan->is_old_spec = indic_plan->config->has_old_spec && ((plan->map.chosen_script[0] & 0x000000FF) != '2'); michael@0: indic_plan->virama_glyph = (hb_codepoint_t) -1; michael@0: michael@0: /* Use zero-context would_substitute() matching for new-spec of the main michael@0: * Indic scripts, but not for old-spec or scripts with one spec only. */ michael@0: bool zero_context = indic_plan->config->has_old_spec || !indic_plan->is_old_spec; michael@0: indic_plan->rphf.init (&plan->map, HB_TAG('r','p','h','f'), zero_context); michael@0: indic_plan->pref.init (&plan->map, HB_TAG('p','r','e','f'), zero_context); michael@0: indic_plan->blwf.init (&plan->map, HB_TAG('b','l','w','f'), zero_context); michael@0: indic_plan->pstf.init (&plan->map, HB_TAG('p','s','t','f'), zero_context); michael@0: michael@0: for (unsigned int i = 0; i < ARRAY_LENGTH (indic_plan->mask_array); i++) michael@0: indic_plan->mask_array[i] = (indic_features[i].flags & F_GLOBAL) ? michael@0: 0 : plan->map.get_1_mask (indic_features[i].tag); michael@0: michael@0: return indic_plan; michael@0: } michael@0: michael@0: static void michael@0: data_destroy_indic (void *data) michael@0: { michael@0: free (data); michael@0: } michael@0: michael@0: static indic_position_t michael@0: consonant_position_from_face (const indic_shape_plan_t *indic_plan, michael@0: const hb_codepoint_t consonant, michael@0: const hb_codepoint_t virama, michael@0: hb_face_t *face) michael@0: { michael@0: /* For old-spec, the order of glyphs is Consonant,Virama, michael@0: * whereas for new-spec, it's Virama,Consonant. However, michael@0: * some broken fonts (like Free Sans) simply copied lookups michael@0: * from old-spec to new-spec without modification. michael@0: * And oddly enough, Uniscribe seems to respect those lookups. michael@0: * Eg. in the sequence U+0924,U+094D,U+0930, Uniscribe finds michael@0: * base at 0. The font however, only has lookups matching michael@0: * 930,94D in 'blwf', not the expected 94D,930 (with new-spec michael@0: * table). As such, we simply match both sequences. Seems michael@0: * to work. */ michael@0: hb_codepoint_t glyphs[3] = {virama, consonant, virama}; michael@0: if (indic_plan->blwf.would_substitute (glyphs , 2, face) || michael@0: indic_plan->blwf.would_substitute (glyphs+1, 2, face)) michael@0: return POS_BELOW_C; michael@0: if (indic_plan->pstf.would_substitute (glyphs , 2, face) || michael@0: indic_plan->pstf.would_substitute (glyphs+1, 2, face)) michael@0: return POS_POST_C; michael@0: unsigned int pref_len = indic_plan->config->pref_len; michael@0: if ((pref_len == PREF_LEN_2 && michael@0: (indic_plan->pref.would_substitute (glyphs , 2, face) || michael@0: indic_plan->pref.would_substitute (glyphs+1, 2, face))) michael@0: || (pref_len == PREF_LEN_1 && michael@0: indic_plan->pref.would_substitute (glyphs+1, 1, face))) michael@0: return POS_POST_C; michael@0: return POS_BASE_C; michael@0: } michael@0: michael@0: michael@0: enum syllable_type_t { michael@0: consonant_syllable, michael@0: vowel_syllable, michael@0: standalone_cluster, michael@0: avagraha_cluster, michael@0: broken_cluster, michael@0: non_indic_cluster, michael@0: }; michael@0: michael@0: #include "hb-ot-shape-complex-indic-machine.hh" michael@0: michael@0: michael@0: static void michael@0: setup_masks_indic (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, indic_category); michael@0: HB_BUFFER_ALLOCATE_VAR (buffer, indic_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_indic_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_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb) michael@0: { michael@0: int a = pa->indic_position(); michael@0: int b = pb->indic_position(); michael@0: michael@0: return a < b ? -1 : a == b ? 0 : +1; michael@0: } michael@0: michael@0: michael@0: michael@0: static void michael@0: update_consonant_positions (const hb_ot_shape_plan_t *plan, michael@0: hb_font_t *font, michael@0: hb_buffer_t *buffer) michael@0: { michael@0: const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data; michael@0: michael@0: if (indic_plan->config->base_pos != BASE_POS_LAST) michael@0: return; michael@0: michael@0: hb_codepoint_t virama; michael@0: if (indic_plan->get_virama_glyph (font, &virama)) michael@0: { michael@0: hb_face_t *face = font->face; michael@0: unsigned int count = buffer->len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: if (buffer->info[i].indic_position() == POS_BASE_C) { michael@0: hb_codepoint_t consonant = buffer->info[i].codepoint; michael@0: buffer->info[i].indic_position() = consonant_position_from_face (indic_plan, consonant, virama, face); michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: /* Rules from: michael@0: * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */ 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: const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data; michael@0: hb_glyph_info_t *info = buffer->info; michael@0: michael@0: michael@0: /* 1. Find base consonant: michael@0: * michael@0: * The shaping engine finds the base consonant of the syllable, using the michael@0: * following algorithm: starting from the end of the syllable, move backwards michael@0: * until a consonant is found that does not have a below-base or post-base michael@0: * form (post-base forms have to follow below-base forms), or that is not a michael@0: * pre-base reordering Ra, or arrive at the first consonant. The consonant michael@0: * stopped at will be the base. michael@0: * michael@0: * o If the syllable starts with Ra + Halant (in a script that has Reph) michael@0: * and has more than one consonant, Ra is excluded from candidates for michael@0: * base consonants. michael@0: */ michael@0: michael@0: unsigned int base = end; michael@0: bool has_reph = false; michael@0: michael@0: { michael@0: /* -> If the syllable starts with Ra + Halant (in a script that has Reph) michael@0: * and has more than one consonant, Ra is excluded from candidates for michael@0: * base consonants. */ michael@0: unsigned int limit = start; michael@0: if (indic_plan->config->reph_pos != REPH_POS_DONT_CARE && michael@0: indic_plan->mask_array[RPHF] && michael@0: start + 3 <= end && michael@0: ( michael@0: (indic_plan->config->reph_mode == REPH_MODE_IMPLICIT && !is_joiner (info[start + 2])) || michael@0: (indic_plan->config->reph_mode == REPH_MODE_EXPLICIT && info[start + 2].indic_category() == OT_ZWJ) michael@0: )) michael@0: { michael@0: /* See if it matches the 'rphf' feature. */ michael@0: hb_codepoint_t glyphs[2] = {info[start].codepoint, info[start + 1].codepoint}; michael@0: if (indic_plan->rphf.would_substitute (glyphs, ARRAY_LENGTH (glyphs), face)) michael@0: { michael@0: limit += 2; michael@0: while (limit < end && is_joiner (info[limit])) michael@0: limit++; michael@0: base = start; michael@0: has_reph = true; michael@0: } michael@0: } else if (indic_plan->config->reph_mode == REPH_MODE_LOG_REPHA && info[start].indic_category() == OT_Repha) michael@0: { michael@0: limit += 1; michael@0: while (limit < end && is_joiner (info[limit])) michael@0: limit++; michael@0: base = start; michael@0: has_reph = true; michael@0: } michael@0: michael@0: switch (indic_plan->config->base_pos) michael@0: { michael@0: default: michael@0: assert (false); michael@0: /* fallthrough */ michael@0: michael@0: case BASE_POS_LAST: michael@0: { michael@0: /* -> starting from the end of the syllable, move backwards */ michael@0: unsigned int i = end; michael@0: bool seen_below = false; michael@0: do { michael@0: i--; michael@0: /* -> until a consonant is found */ michael@0: if (is_consonant (info[i])) michael@0: { michael@0: /* -> that does not have a below-base or post-base form michael@0: * (post-base forms have to follow below-base forms), */ michael@0: if (info[i].indic_position() != POS_BELOW_C && michael@0: (info[i].indic_position() != POS_POST_C || seen_below)) michael@0: { michael@0: base = i; michael@0: break; michael@0: } michael@0: if (info[i].indic_position() == POS_BELOW_C) michael@0: seen_below = true; michael@0: michael@0: /* -> or that is not a pre-base reordering Ra, michael@0: * michael@0: * IMPLEMENTATION NOTES: michael@0: * michael@0: * Our pre-base reordering Ra's are marked POS_POST_C, so will be skipped michael@0: * by the logic above already. michael@0: */ michael@0: michael@0: /* -> or arrive at the first consonant. The consonant stopped at will michael@0: * be the base. */ michael@0: base = i; michael@0: } michael@0: else michael@0: { michael@0: /* A ZWJ after a Halant stops the base search, and requests an explicit michael@0: * half form. michael@0: * A ZWJ before a Halant, requests a subjoined form instead, and hence michael@0: * search continues. This is particularly important for Bengali michael@0: * sequence Ra,H,Ya that should form Ya-Phalaa by subjoining Ya. */ michael@0: if (start < i && michael@0: info[i].indic_category() == OT_ZWJ && michael@0: info[i - 1].indic_category() == OT_H) michael@0: break; michael@0: } michael@0: } while (i > limit); michael@0: } michael@0: break; michael@0: michael@0: case BASE_POS_LAST_SINHALA: michael@0: { michael@0: /* Sinhala base positioning is slightly different from main Indic, in that: michael@0: * 1. It's ZWJ behavior is different, michael@0: * 2. We don't need to look into the font for consonant positions. michael@0: */ michael@0: michael@0: if (!has_reph) michael@0: base = limit; michael@0: michael@0: /* Find the last base consonant that is not blocked by ZWJ. If there is michael@0: * a ZWJ right before a base consonant, that would request a subjoined form. */ michael@0: for (unsigned int i = limit; i < end; i++) michael@0: if (is_consonant (info[i])) michael@0: { michael@0: if (limit < i && info[i - 1].indic_category() == OT_ZWJ) michael@0: break; michael@0: else michael@0: base = i; michael@0: } michael@0: michael@0: /* Mark all subsequent consonants as below. */ michael@0: for (unsigned int i = base + 1; i < end; i++) michael@0: if (is_consonant (info[i])) michael@0: info[i].indic_position() = POS_BELOW_C; michael@0: } michael@0: break; michael@0: michael@0: case BASE_POS_FIRST: michael@0: { michael@0: /* The first consonant is always the base. */ michael@0: michael@0: assert (indic_plan->config->reph_mode == REPH_MODE_VIS_REPHA); michael@0: assert (!has_reph); michael@0: michael@0: base = start; michael@0: michael@0: /* Mark all subsequent consonants as below. */ michael@0: for (unsigned int i = base + 1; i < end; i++) michael@0: if (is_consonant (info[i])) michael@0: info[i].indic_position() = POS_BELOW_C; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: /* -> If the syllable starts with Ra + Halant (in a script that has Reph) michael@0: * and has more than one consonant, Ra is excluded from candidates for michael@0: * base consonants. michael@0: * michael@0: * Only do this for unforced Reph. (ie. not for Ra,H,ZWJ. */ michael@0: if (has_reph && base == start && limit - base <= 2) { michael@0: /* Have no other consonant, so Reph is not formed and Ra becomes base. */ michael@0: has_reph = false; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* 2. Decompose and reorder Matras: michael@0: * michael@0: * Each matra and any syllable modifier sign in the cluster are moved to the michael@0: * appropriate position relative to the consonant(s) in the cluster. The michael@0: * shaping engine decomposes two- or three-part matras into their constituent michael@0: * parts before any repositioning. Matra characters are classified by which michael@0: * consonant in a conjunct they have affinity for and are reordered to the michael@0: * following positions: michael@0: * michael@0: * o Before first half form in the syllable michael@0: * o After subjoined consonants michael@0: * o After post-form consonant michael@0: * o After main consonant (for above marks) michael@0: * michael@0: * IMPLEMENTATION NOTES: michael@0: * michael@0: * The normalize() routine has already decomposed matras for us, so we don't michael@0: * need to worry about that. michael@0: */ michael@0: michael@0: michael@0: /* 3. Reorder marks to canonical order: michael@0: * michael@0: * Adjacent nukta and halant or nukta and vedic sign are always repositioned michael@0: * if necessary, so that the nukta is first. michael@0: * michael@0: * IMPLEMENTATION NOTES: michael@0: * michael@0: * We don't need to do this: the normalize() routine already did this for us. michael@0: */ michael@0: michael@0: michael@0: /* Reorder characters */ michael@0: michael@0: for (unsigned int i = start; i < base; i++) michael@0: info[i].indic_position() = MIN (POS_PRE_C, (indic_position_t) info[i].indic_position()); michael@0: michael@0: if (base < end) michael@0: info[base].indic_position() = POS_BASE_C; michael@0: michael@0: /* Mark final consonants. A final consonant is one appearing after a matra, michael@0: * like in Khmer. */ michael@0: for (unsigned int i = base + 1; i < end; i++) michael@0: if (info[i].indic_category() == OT_M) { michael@0: for (unsigned int j = i + 1; j < end; j++) michael@0: if (is_consonant (info[j])) { michael@0: info[j].indic_position() = POS_FINAL_C; michael@0: break; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: /* Handle beginning Ra */ michael@0: if (has_reph) michael@0: info[start].indic_position() = POS_RA_TO_BECOME_REPH; michael@0: michael@0: /* For old-style Indic script tags, move the first post-base Halant after michael@0: * last consonant. Only do this if there is *not* a Halant after last michael@0: * consonant. Otherwise it becomes messy. */ michael@0: if (indic_plan->is_old_spec) { michael@0: for (unsigned int i = base + 1; i < end; i++) michael@0: if (info[i].indic_category() == OT_H) { michael@0: unsigned int j; michael@0: for (j = end - 1; j > i; j--) michael@0: if (is_consonant (info[j]) || info[j].indic_category() == OT_H) michael@0: break; michael@0: if (info[j].indic_category() != OT_H && j > i) { michael@0: /* Move Halant to after last consonant. */ michael@0: hb_glyph_info_t t = info[i]; michael@0: memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0])); michael@0: info[j] = t; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: michael@0: /* Attach misc marks to previous char to move with them. */ michael@0: { michael@0: indic_position_t last_pos = POS_START; michael@0: for (unsigned int i = start; i < end; i++) michael@0: { michael@0: if ((FLAG (info[i].indic_category()) & (JOINER_FLAGS | FLAG (OT_N) | FLAG (OT_RS) | MEDIAL_FLAGS | HALANT_OR_COENG_FLAGS))) michael@0: { michael@0: info[i].indic_position() = last_pos; michael@0: if (unlikely (info[i].indic_category() == OT_H && michael@0: info[i].indic_position() == POS_PRE_M)) michael@0: { michael@0: /* michael@0: * Uniscribe doesn't move the Halant with Left Matra. michael@0: * TEST: U+092B,U+093F,U+094DE michael@0: * We follow. This is important for the Sinhala michael@0: * U+0DDA split matra since it decomposes to U+0DD9,U+0DCA michael@0: * where U+0DD9 is a left matra and U+0DCA is the virama. michael@0: * We don't want to move the virama with the left matra. michael@0: * TEST: U+0D9A,U+0DDA michael@0: */ michael@0: for (unsigned int j = i; j > start; j--) michael@0: if (info[j - 1].indic_position() != POS_PRE_M) { michael@0: info[i].indic_position() = info[j - 1].indic_position(); michael@0: break; michael@0: } michael@0: } michael@0: } else if (info[i].indic_position() != POS_SMVD) { michael@0: last_pos = (indic_position_t) info[i].indic_position(); michael@0: } michael@0: } michael@0: } michael@0: /* For post-base consonants let them own anything before them michael@0: * since the last consonant or matra. */ michael@0: { michael@0: unsigned int last = base; michael@0: for (unsigned int i = base + 1; i < end; i++) michael@0: if (is_consonant (info[i])) michael@0: { michael@0: for (unsigned int j = last + 1; j < i; j++) michael@0: if (info[j].indic_position() < POS_SMVD) michael@0: info[j].indic_position() = info[i].indic_position(); michael@0: last = i; michael@0: } else if (info[i].indic_category() == OT_M) michael@0: last = i; michael@0: } michael@0: michael@0: michael@0: { michael@0: /* Use syllable() for sort accounting temporarily. */ michael@0: unsigned int syllable = info[start].syllable(); michael@0: for (unsigned int i = start; i < end; i++) michael@0: info[i].syllable() = i - start; michael@0: michael@0: /* Sit tight, rock 'n roll! */ michael@0: hb_bubble_sort (info + start, end - start, compare_indic_order); michael@0: /* Find base again */ michael@0: base = end; michael@0: for (unsigned int i = start; i < end; i++) michael@0: if (info[i].indic_position() == POS_BASE_C) michael@0: { michael@0: base = i; michael@0: break; michael@0: } michael@0: /* Things are out-of-control for post base positions, they may shuffle michael@0: * around like crazy. In old-spec mode, we move halants around, so in michael@0: * that case merge all clusters after base. Otherwise, check the sort michael@0: * order and merge as needed. michael@0: * For pre-base stuff, we handle cluster issues in final reordering. */ michael@0: if (indic_plan->is_old_spec || end - base > 127) michael@0: buffer->merge_clusters (base, end); michael@0: else michael@0: { michael@0: /* Note! syllable() is a one-byte field. */ michael@0: for (unsigned int i = base; i < end; i++) michael@0: if (info[i].syllable() != 255) michael@0: { michael@0: unsigned int max = i; michael@0: unsigned int j = start + info[i].syllable(); michael@0: while (j != i) michael@0: { michael@0: max = MAX (max, j); michael@0: unsigned int next = start + info[j].syllable(); michael@0: info[j].syllable() = 255; /* So we don't process j later again. */ michael@0: j = next; michael@0: } michael@0: if (i != max) michael@0: buffer->merge_clusters (i, max + 1); michael@0: } michael@0: } michael@0: michael@0: /* Put syllable back in. */ michael@0: for (unsigned int i = start; i < end; i++) michael@0: info[i].syllable() = syllable; michael@0: } michael@0: michael@0: /* Setup masks now */ michael@0: michael@0: { michael@0: hb_mask_t mask; michael@0: michael@0: /* Reph */ michael@0: for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++) michael@0: info[i].mask |= indic_plan->mask_array[RPHF]; michael@0: michael@0: /* Pre-base */ michael@0: mask = indic_plan->mask_array[HALF]; michael@0: if (!indic_plan->is_old_spec && michael@0: indic_plan->config->blwf_mode == BLWF_MODE_PRE_AND_POST) michael@0: mask |= indic_plan->mask_array[BLWF]; michael@0: for (unsigned int i = start; i < base; i++) michael@0: info[i].mask |= mask; michael@0: /* Base */ michael@0: mask = 0; michael@0: if (base < end) michael@0: info[base].mask |= mask; michael@0: /* Post-base */ michael@0: mask = indic_plan->mask_array[BLWF] | indic_plan->mask_array[ABVF] | indic_plan->mask_array[PSTF]; michael@0: for (unsigned int i = base + 1; i < end; i++) michael@0: info[i].mask |= mask; michael@0: } michael@0: michael@0: if (indic_plan->is_old_spec && michael@0: buffer->props.script == HB_SCRIPT_DEVANAGARI) michael@0: { michael@0: /* Old-spec eye-lash Ra needs special handling. From the michael@0: * spec: michael@0: * michael@0: * "The feature 'below-base form' is applied to consonants michael@0: * having below-base forms and following the base consonant. michael@0: * The exception is vattu, which may appear below half forms michael@0: * as well as below the base glyph. The feature 'below-base michael@0: * form' will be applied to all such occurrences of Ra as well." michael@0: * michael@0: * Test case: U+0924,U+094D,U+0930,U+094d,U+0915 michael@0: * with Sanskrit 2003 font. michael@0: * michael@0: * However, note that Ra,Halant,ZWJ is the correct way to michael@0: * request eyelash form of Ra, so we wouldbn't inhibit it michael@0: * in that sequence. michael@0: * michael@0: * Test case: U+0924,U+094D,U+0930,U+094d,U+200D,U+0915 michael@0: */ michael@0: for (unsigned int i = start; i + 1 < base; i++) michael@0: if (info[i ].indic_category() == OT_Ra && michael@0: info[i+1].indic_category() == OT_H && michael@0: (i + 2 == base || michael@0: info[i+2].indic_category() != OT_ZWJ)) michael@0: { michael@0: info[i ].mask |= indic_plan->mask_array[BLWF]; michael@0: info[i+1].mask |= indic_plan->mask_array[BLWF]; michael@0: } michael@0: } michael@0: michael@0: unsigned int pref_len = indic_plan->config->pref_len; michael@0: if (indic_plan->mask_array[PREF] && base + pref_len < end) michael@0: { michael@0: assert (1 <= pref_len && pref_len <= 2); michael@0: /* Find a Halant,Ra sequence and mark it for pre-base reordering processing. */ michael@0: for (unsigned int i = base + 1; i + pref_len - 1 < end; i++) { michael@0: hb_codepoint_t glyphs[2]; michael@0: for (unsigned int j = 0; j < pref_len; j++) michael@0: glyphs[j] = info[i + j].codepoint; michael@0: if (indic_plan->pref.would_substitute (glyphs, pref_len, face)) michael@0: { michael@0: for (unsigned int j = 0; j < pref_len; j++) michael@0: info[i++].mask |= indic_plan->mask_array[PREF]; michael@0: michael@0: /* Mark the subsequent stuff with 'cfar'. Used in Khmer. michael@0: * Read the feature spec. michael@0: * This allows distinguishing the following cases with MS Khmer fonts: michael@0: * U+1784,U+17D2,U+179A,U+17D2,U+1782 michael@0: * U+1784,U+17D2,U+1782,U+17D2,U+179A michael@0: */ michael@0: if (indic_plan->mask_array[CFAR]) michael@0: for (; i < end; i++) michael@0: info[i].mask |= indic_plan->mask_array[CFAR]; michael@0: michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Apply ZWJ/ZWNJ effects */ michael@0: for (unsigned int i = start + 1; i < end; i++) michael@0: if (is_joiner (info[i])) { michael@0: bool non_joiner = info[i].indic_category() == OT_ZWNJ; michael@0: unsigned int j = i; michael@0: michael@0: do { michael@0: j--; michael@0: michael@0: /* ZWJ/ZWNJ should disable CJCT. They do that by simply michael@0: * being there, since we don't skip them for the CJCT michael@0: * feature (ie. F_MANUAL_ZWJ) */ michael@0: michael@0: /* A ZWNJ disables HALF. */ michael@0: if (non_joiner) michael@0: info[j].mask &= ~indic_plan->mask_array[HALF]; michael@0: michael@0: } while (j > start && !is_consonant (info[j])); michael@0: } michael@0: } michael@0: michael@0: michael@0: static void michael@0: initial_reordering_vowel_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: /* We made the vowels look like consonants. So let's call the consonant logic! */ michael@0: initial_reordering_consonant_syllable (plan, face, buffer, start, end); michael@0: } michael@0: michael@0: static void michael@0: initial_reordering_standalone_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 treat NBSP/dotted-circle as if they are consonants, so we should just chain. michael@0: * Only if not in compatibility mode that is... */ michael@0: michael@0: if (hb_options ().uniscribe_bug_compatible) michael@0: { michael@0: /* For dotted-circle, this is what Uniscribe does: michael@0: * If dotted-circle is the last glyph, it just does nothing. michael@0: * Ie. It doesn't form Reph. */ michael@0: if (buffer->info[end - 1].indic_category() == OT_DOTTEDCIRCLE) michael@0: return; michael@0: } michael@0: michael@0: initial_reordering_consonant_syllable (plan, face, buffer, start, end); 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 standalone_cluster. */ michael@0: initial_reordering_standalone_cluster (plan, face, buffer, start, end); michael@0: } michael@0: michael@0: static void michael@0: initial_reordering_avagraha_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_indic_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 vowel_syllable: initial_reordering_vowel_syllable (plan, face, buffer, start, end); return; michael@0: case standalone_cluster: initial_reordering_standalone_cluster (plan, face, buffer, start, end); return; michael@0: case avagraha_cluster: initial_reordering_avagraha_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_indic_cluster: initial_reordering_non_indic_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_indic_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: /* Insert dottedcircle after possible Repha. */ michael@0: while (buffer->idx < buffer->len && michael@0: last_syllable == buffer->cur().syllable() && michael@0: buffer->cur().indic_category() == OT_Repha) michael@0: buffer->next_glyph (); 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: update_consonant_positions (plan, font, buffer); 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_syllable (const hb_ot_shape_plan_t *plan, michael@0: hb_buffer_t *buffer, michael@0: unsigned int start, unsigned int end) michael@0: { michael@0: const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data; michael@0: hb_glyph_info_t *info = buffer->info; michael@0: michael@0: /* 4. Final reordering: michael@0: * michael@0: * After the localized forms and basic shaping forms GSUB features have been michael@0: * applied (see below), the shaping engine performs some final glyph michael@0: * reordering before applying all the remaining font features to the entire michael@0: * cluster. michael@0: */ michael@0: michael@0: /* Find base again */ michael@0: unsigned int base; michael@0: for (base = start; base < end; base++) michael@0: if (info[base].indic_position() >= POS_BASE_C) { michael@0: if (start < base && info[base].indic_position() > POS_BASE_C) michael@0: base--; michael@0: break; michael@0: } michael@0: if (base == end && start < base && michael@0: info[base - 1].indic_category() != OT_ZWJ) michael@0: base--; michael@0: while (start < base && michael@0: (info[base].indic_category() == OT_H || michael@0: info[base].indic_category() == OT_N)) michael@0: base--; michael@0: michael@0: michael@0: /* o Reorder matras: michael@0: * michael@0: * If a pre-base matra character had been reordered before applying basic michael@0: * features, the glyph can be moved closer to the main consonant based on michael@0: * whether half-forms had been formed. Actual position for the matra is michael@0: * defined as “after last standalone halant glyph, after initial matra michael@0: * position and before the main consonant”. If ZWJ or ZWNJ follow this michael@0: * halant, position is moved after it. michael@0: */ michael@0: michael@0: if (start + 1 < end && start < base) /* Otherwise there can't be any pre-base matra characters. */ michael@0: { michael@0: /* If we lost track of base, alas, position before last thingy. */ michael@0: unsigned int new_pos = base == end ? base - 2 : base - 1; michael@0: michael@0: /* Malayalam / Tamil do not have "half" forms or explicit virama forms. michael@0: * The glyphs formed by 'half' are Chillus or ligated explicit viramas. michael@0: * We want to position matra after them. michael@0: */ michael@0: if (buffer->props.script != HB_SCRIPT_MALAYALAM && buffer->props.script != HB_SCRIPT_TAMIL) michael@0: { michael@0: while (new_pos > start && michael@0: !(is_one_of (info[new_pos], (FLAG (OT_M) | FLAG (OT_H) | FLAG (OT_Coeng))))) michael@0: new_pos--; michael@0: michael@0: /* If we found no Halant we are done. michael@0: * Otherwise only proceed if the Halant does michael@0: * not belong to the Matra itself! */ michael@0: if (is_halant_or_coeng (info[new_pos]) && michael@0: info[new_pos].indic_position() != POS_PRE_M) michael@0: { michael@0: /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */ michael@0: if (new_pos + 1 < end && is_joiner (info[new_pos + 1])) michael@0: new_pos++; michael@0: } michael@0: else michael@0: new_pos = start; /* No move. */ michael@0: } michael@0: michael@0: if (start < new_pos && info[new_pos].indic_position () != POS_PRE_M) michael@0: { michael@0: /* Now go see if there's actually any matras... */ michael@0: for (unsigned int i = new_pos; i > start; i--) michael@0: if (info[i - 1].indic_position () == POS_PRE_M) michael@0: { michael@0: unsigned int old_pos = i - 1; michael@0: hb_glyph_info_t tmp = info[old_pos]; michael@0: memmove (&info[old_pos], &info[old_pos + 1], (new_pos - old_pos) * sizeof (info[0])); michael@0: info[new_pos] = tmp; michael@0: if (old_pos < base && base <= new_pos) /* Shouldn't actually happen. */ michael@0: base--; michael@0: buffer->merge_clusters (new_pos, MIN (end, base + 1)); michael@0: new_pos--; michael@0: } michael@0: } else { michael@0: for (unsigned int i = start; i < base; i++) michael@0: if (info[i].indic_position () == POS_PRE_M) { michael@0: buffer->merge_clusters (i, MIN (end, base + 1)); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: /* o Reorder reph: michael@0: * michael@0: * Reph’s original position is always at the beginning of the syllable, michael@0: * (i.e. it is not reordered at the character reordering stage). However, michael@0: * it will be reordered according to the basic-forms shaping results. michael@0: * Possible positions for reph, depending on the script, are; after main, michael@0: * before post-base consonant forms, and after post-base consonant forms. michael@0: */ michael@0: michael@0: /* Two cases: michael@0: * michael@0: * - If repha is encoded as a sequence of characters (Ra,H or Ra,H,ZWJ), then michael@0: * we should only move it if the sequence ligated to the repha form. michael@0: * michael@0: * - If repha is encoded separately and in the logical position, we should only michael@0: * move it if it did NOT ligate. If it ligated, it's probably the font trying michael@0: * to make it work without the reordering. michael@0: */ michael@0: if (start + 1 < end && michael@0: info[start].indic_position() == POS_RA_TO_BECOME_REPH && michael@0: ((info[start].indic_category() == OT_Repha) ^ michael@0: _hb_glyph_info_ligated (&info[start]))) michael@0: { michael@0: unsigned int new_reph_pos; michael@0: reph_position_t reph_pos = indic_plan->config->reph_pos; michael@0: michael@0: assert (reph_pos != REPH_POS_DONT_CARE); michael@0: michael@0: /* 1. If reph should be positioned after post-base consonant forms, michael@0: * proceed to step 5. michael@0: */ michael@0: if (reph_pos == REPH_POS_AFTER_POST) michael@0: { michael@0: goto reph_step_5; michael@0: } michael@0: michael@0: /* 2. If the reph repositioning class is not after post-base: target michael@0: * position is after the first explicit halant glyph between the michael@0: * first post-reph consonant and last main consonant. If ZWJ or ZWNJ michael@0: * are following this halant, position is moved after it. If such michael@0: * position is found, this is the target position. Otherwise, michael@0: * proceed to the next step. michael@0: * michael@0: * Note: in old-implementation fonts, where classifications were michael@0: * fixed in shaping engine, there was no case where reph position michael@0: * will be found on this step. michael@0: */ michael@0: { michael@0: new_reph_pos = start + 1; michael@0: while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos])) michael@0: new_reph_pos++; michael@0: michael@0: if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos])) michael@0: { michael@0: /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */ michael@0: if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1])) michael@0: new_reph_pos++; michael@0: goto reph_move; michael@0: } michael@0: } michael@0: michael@0: /* 3. If reph should be repositioned after the main consonant: find the michael@0: * first consonant not ligated with main, or find the first michael@0: * consonant that is not a potential pre-base reordering Ra. michael@0: */ michael@0: if (reph_pos == REPH_POS_AFTER_MAIN) michael@0: { michael@0: new_reph_pos = base; michael@0: while (new_reph_pos + 1 < end && info[new_reph_pos + 1].indic_position() <= POS_AFTER_MAIN) michael@0: new_reph_pos++; michael@0: if (new_reph_pos < end) michael@0: goto reph_move; michael@0: } michael@0: michael@0: /* 4. If reph should be positioned before post-base consonant, find michael@0: * first post-base classified consonant not ligated with main. If no michael@0: * consonant is found, the target position should be before the michael@0: * first matra, syllable modifier sign or vedic sign. michael@0: */ michael@0: /* This is our take on what step 4 is trying to say (and failing, BADLY). */ michael@0: if (reph_pos == REPH_POS_AFTER_SUB) michael@0: { michael@0: new_reph_pos = base; michael@0: while (new_reph_pos < end && michael@0: !( FLAG (info[new_reph_pos + 1].indic_position()) & (FLAG (POS_POST_C) | FLAG (POS_AFTER_POST) | FLAG (POS_SMVD)))) michael@0: new_reph_pos++; michael@0: if (new_reph_pos < end) michael@0: goto reph_move; michael@0: } michael@0: michael@0: /* 5. If no consonant is found in steps 3 or 4, move reph to a position michael@0: * immediately before the first post-base matra, syllable modifier michael@0: * sign or vedic sign that has a reordering class after the intended michael@0: * reph position. For example, if the reordering position for reph michael@0: * is post-main, it will skip above-base matras that also have a michael@0: * post-main position. michael@0: */ michael@0: reph_step_5: michael@0: { michael@0: /* Copied from step 2. */ michael@0: new_reph_pos = start + 1; michael@0: while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos])) michael@0: new_reph_pos++; michael@0: michael@0: if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos])) michael@0: { michael@0: /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */ michael@0: if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1])) michael@0: new_reph_pos++; michael@0: goto reph_move; michael@0: } michael@0: } michael@0: michael@0: /* 6. Otherwise, reorder reph to the end of the syllable. michael@0: */ michael@0: { michael@0: new_reph_pos = end - 1; michael@0: while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD) michael@0: new_reph_pos--; michael@0: michael@0: /* michael@0: * If the Reph is to be ending up after a Matra,Halant sequence, michael@0: * position it before that Halant so it can interact with the Matra. michael@0: * However, if it's a plain Consonant,Halant we shouldn't do that. michael@0: * Uniscribe doesn't do this. michael@0: * TEST: U+0930,U+094D,U+0915,U+094B,U+094D michael@0: */ michael@0: if (!hb_options ().uniscribe_bug_compatible && michael@0: unlikely (is_halant_or_coeng (info[new_reph_pos]))) { michael@0: for (unsigned int i = base + 1; i < new_reph_pos; i++) michael@0: if (info[i].indic_category() == OT_M) { michael@0: /* Ok, got it. */ michael@0: new_reph_pos--; michael@0: } michael@0: } michael@0: goto reph_move; michael@0: } michael@0: michael@0: reph_move: michael@0: { michael@0: buffer->merge_clusters (start, new_reph_pos + 1); michael@0: michael@0: /* Move */ michael@0: hb_glyph_info_t reph = info[start]; michael@0: memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0])); michael@0: info[new_reph_pos] = reph; michael@0: if (start < base && base <= new_reph_pos) michael@0: base--; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* o Reorder pre-base reordering consonants: michael@0: * michael@0: * If a pre-base reordering consonant is found, reorder it according to michael@0: * the following rules: michael@0: */ michael@0: michael@0: if (indic_plan->mask_array[PREF] && base + 1 < end) /* Otherwise there can't be any pre-base reordering Ra. */ michael@0: { michael@0: unsigned int pref_len = indic_plan->config->pref_len; michael@0: for (unsigned int i = base + 1; i < end; i++) michael@0: if ((info[i].mask & indic_plan->mask_array[PREF]) != 0) michael@0: { michael@0: /* 1. Only reorder a glyph produced by substitution during application michael@0: * of the feature. (Note that a font may shape a Ra consonant with michael@0: * the feature generally but block it in certain contexts.) michael@0: */ michael@0: /* Note: We just check that something got substituted. We don't check that michael@0: * the feature actually did it... michael@0: * michael@0: * If pref len is longer than one, then only reorder if it ligated. If michael@0: * pref len is one, only reorder if it didn't ligate with other things. */ michael@0: if (_hb_glyph_info_substituted (&info[i]) && michael@0: ((pref_len == 1) ^ _hb_glyph_info_ligated (&info[i]))) michael@0: { michael@0: /* michael@0: * 2. Try to find a target position the same way as for pre-base matra. michael@0: * If it is found, reorder pre-base consonant glyph. michael@0: * michael@0: * 3. If position is not found, reorder immediately before main michael@0: * consonant. michael@0: */ michael@0: michael@0: unsigned int new_pos = base; michael@0: /* Malayalam / Tamil do not have "half" forms or explicit virama forms. michael@0: * The glyphs formed by 'half' are Chillus or ligated explicit viramas. michael@0: * We want to position matra after them. michael@0: */ michael@0: if (buffer->props.script != HB_SCRIPT_MALAYALAM && buffer->props.script != HB_SCRIPT_TAMIL) michael@0: { michael@0: while (new_pos > start && michael@0: !(is_one_of (info[new_pos - 1], FLAG(OT_M) | HALANT_OR_COENG_FLAGS))) michael@0: new_pos--; michael@0: michael@0: /* In Khmer coeng model, a H,Ra can go *after* matras. If it goes after a michael@0: * split matra, it should be reordered to *before* the left part of such matra. */ michael@0: if (new_pos > start && info[new_pos - 1].indic_category() == OT_M) michael@0: { michael@0: unsigned int old_pos = i; michael@0: for (unsigned int i = base + 1; i < old_pos; i++) michael@0: if (info[i].indic_category() == OT_M) michael@0: { michael@0: new_pos--; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (new_pos > start && is_halant_or_coeng (info[new_pos - 1])) michael@0: { michael@0: /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */ michael@0: if (new_pos < end && is_joiner (info[new_pos])) michael@0: new_pos++; michael@0: } michael@0: michael@0: { michael@0: unsigned int old_pos = i; michael@0: buffer->merge_clusters (new_pos, old_pos + 1); michael@0: hb_glyph_info_t tmp = info[old_pos]; michael@0: memmove (&info[new_pos + 1], &info[new_pos], (old_pos - new_pos) * sizeof (info[0])); michael@0: info[new_pos] = tmp; michael@0: if (new_pos <= base && base < old_pos) michael@0: base++; michael@0: } michael@0: } michael@0: michael@0: break; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* Apply 'init' to the Left Matra if it's a word start. */ michael@0: if (info[start].indic_position () == POS_PRE_M && michael@0: (!start || michael@0: !(FLAG (_hb_glyph_info_get_general_category (&info[start - 1])) & michael@0: FLAG_RANGE (HB_UNICODE_GENERAL_CATEGORY_FORMAT, HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))) michael@0: info[start].mask |= indic_plan->mask_array[INIT]; michael@0: michael@0: michael@0: /* michael@0: * Finish off the clusters and go home! michael@0: */ michael@0: if (hb_options ().uniscribe_bug_compatible) michael@0: { michael@0: switch ((hb_tag_t) plan->props.script) michael@0: { michael@0: case HB_SCRIPT_TAMIL: michael@0: case HB_SCRIPT_SINHALA: michael@0: break; michael@0: michael@0: default: michael@0: /* Uniscribe merges the entire cluster... Except for Tamil & Sinhala. michael@0: * This means, half forms are submerged into the main consonants cluster. michael@0: * This is unnecessary, and makes cursor positioning harder, but that's what michael@0: * Uniscribe does. */ michael@0: buffer->merge_clusters (start, end); michael@0: break; michael@0: } michael@0: } michael@0: } 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: unsigned int count = buffer->len; michael@0: if (unlikely (!count)) return; michael@0: michael@0: hb_glyph_info_t *info = buffer->info; 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: final_reordering_syllable (plan, buffer, last, i); michael@0: last = i; michael@0: last_syllable = info[last].syllable(); michael@0: } michael@0: final_reordering_syllable (plan, buffer, last, count); michael@0: michael@0: HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category); michael@0: HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position); michael@0: } michael@0: michael@0: michael@0: static void michael@0: clear_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: 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: info[i].syllable() = 0; michael@0: } michael@0: michael@0: michael@0: static bool michael@0: decompose_indic (const hb_ot_shape_normalize_context_t *c, michael@0: hb_codepoint_t ab, michael@0: hb_codepoint_t *a, michael@0: hb_codepoint_t *b) michael@0: { michael@0: switch (ab) michael@0: { michael@0: /* Don't decompose these. */ michael@0: case 0x0931 : return false; michael@0: case 0x0B94 : return false; michael@0: michael@0: michael@0: /* michael@0: * Decompose split matras that don't have Unicode decompositions. michael@0: */ michael@0: michael@0: case 0x0F77 : *a = 0x0FB2; *b= 0x0F81; return true; michael@0: case 0x0F79 : *a = 0x0FB3; *b= 0x0F81; return true; michael@0: case 0x17BE : *a = 0x17C1; *b= 0x17BE; return true; michael@0: case 0x17BF : *a = 0x17C1; *b= 0x17BF; return true; michael@0: case 0x17C0 : *a = 0x17C1; *b= 0x17C0; return true; michael@0: case 0x17C4 : *a = 0x17C1; *b= 0x17C4; return true; michael@0: case 0x17C5 : *a = 0x17C1; *b= 0x17C5; return true; michael@0: case 0x1925 : *a = 0x1920; *b= 0x1923; return true; michael@0: case 0x1926 : *a = 0x1920; *b= 0x1924; return true; michael@0: case 0x1B3C : *a = 0x1B42; *b= 0x1B3C; return true; michael@0: case 0x1112E : *a = 0x11127; *b= 0x11131; return true; michael@0: case 0x1112F : *a = 0x11127; *b= 0x11132; return true; michael@0: #if 0 michael@0: /* This one has no decomposition in Unicode, but needs no decomposition either. */ michael@0: /* case 0x0AC9 : return false; */ michael@0: case 0x0B57 : *a = no decomp, -> RIGHT; return true; michael@0: case 0x1C29 : *a = no decomp, -> LEFT; return true; michael@0: case 0xA9C0 : *a = no decomp, -> RIGHT; return true; michael@0: case 0x111BF : *a = no decomp, -> ABOVE; return true; michael@0: #endif michael@0: } michael@0: michael@0: if ((ab == 0x0DDA || hb_in_range (ab, 0x0DDC, 0x0DDE))) michael@0: { michael@0: /* michael@0: * Sinhala split matras... Let the fun begin. michael@0: * michael@0: * These four characters have Unicode decompositions. However, Uniscribe michael@0: * decomposes them "Khmer-style", that is, it uses the character itself to michael@0: * get the second half. The first half of all four decompositions is always michael@0: * U+0DD9. michael@0: * michael@0: * Now, there are buggy fonts, namely, the widely used lklug.ttf, that are michael@0: * broken with Uniscribe. But we need to support them. As such, we only michael@0: * do the Uniscribe-style decomposition if the character is transformed into michael@0: * its "sec.half" form by the 'pstf' feature. Otherwise, we fall back to michael@0: * Unicode decomposition. michael@0: * michael@0: * Note that we can't unconditionally use Unicode decomposition. That would michael@0: * break some other fonts, that are designed to work with Uniscribe, and michael@0: * don't have positioning features for the Unicode-style decomposition. michael@0: * michael@0: * Argh... michael@0: * michael@0: * The Uniscribe behavior is now documented in the newly published Sinhala michael@0: * spec in 2012: michael@0: * michael@0: * http://www.microsoft.com/typography/OpenTypeDev/sinhala/intro.htm#shaping michael@0: */ michael@0: michael@0: const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) c->plan->data; michael@0: michael@0: hb_codepoint_t glyph; michael@0: michael@0: if (hb_options ().uniscribe_bug_compatible || michael@0: (c->font->get_glyph (ab, 0, &glyph) && michael@0: indic_plan->pstf.would_substitute (&glyph, 1, c->font->face))) michael@0: { michael@0: /* Ok, safe to use Uniscribe-style decomposition. */ michael@0: *a = 0x0DD9; michael@0: *b = ab; michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return c->unicode->decompose (ab, a, b); michael@0: } michael@0: michael@0: static bool michael@0: compose_indic (const hb_ot_shape_normalize_context_t *c, michael@0: hb_codepoint_t a, michael@0: hb_codepoint_t b, michael@0: hb_codepoint_t *ab) michael@0: { michael@0: /* Avoid recomposing split matras. */ michael@0: if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (c->unicode->general_category (a))) michael@0: return false; michael@0: michael@0: /* Composition-exclusion exceptions that we want to recompose. */ michael@0: if (a == 0x09AF && b == 0x09BC) { *ab = 0x09DF; return true; } michael@0: michael@0: return c->unicode->compose (a, b, ab); michael@0: } michael@0: michael@0: michael@0: const hb_ot_complex_shaper_t _hb_ot_complex_shaper_indic = michael@0: { michael@0: "indic", michael@0: collect_features_indic, michael@0: override_features_indic, michael@0: data_create_indic, michael@0: data_destroy_indic, michael@0: NULL, /* preprocess_text */ michael@0: HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT, michael@0: decompose_indic, michael@0: compose_indic, michael@0: setup_masks_indic, michael@0: HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE, michael@0: false, /* fallback_position */ michael@0: };