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-fallback-private.hh" michael@0: #include "hb-ot-layout-gsubgpos-private.hh" michael@0: michael@0: static unsigned int michael@0: recategorize_combining_class (hb_codepoint_t u, michael@0: unsigned int klass) michael@0: { michael@0: if (klass >= 200) michael@0: return klass; michael@0: michael@0: /* Thai / Lao need some per-character work. */ michael@0: if ((u & ~0xFF) == 0x0E00) michael@0: { michael@0: if (unlikely (klass == 0)) michael@0: { michael@0: switch (u) michael@0: { michael@0: case 0x0E31: michael@0: case 0x0E34: michael@0: case 0x0E35: michael@0: case 0x0E36: michael@0: case 0x0E37: michael@0: case 0x0E47: michael@0: case 0x0E4C: michael@0: case 0x0E4D: michael@0: case 0x0E4E: michael@0: klass = HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT; michael@0: break; michael@0: michael@0: case 0x0EB1: michael@0: case 0x0EB4: michael@0: case 0x0EB5: michael@0: case 0x0EB6: michael@0: case 0x0EB7: michael@0: case 0x0EBB: michael@0: case 0x0ECC: michael@0: case 0x0ECD: michael@0: klass = HB_UNICODE_COMBINING_CLASS_ABOVE; michael@0: break; michael@0: michael@0: case 0x0EBC: michael@0: klass = HB_UNICODE_COMBINING_CLASS_BELOW; michael@0: break; michael@0: } michael@0: } else { michael@0: /* Thai virama is below-right */ michael@0: if (u == 0x0E3A) michael@0: klass = HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT; michael@0: } michael@0: } michael@0: michael@0: switch (klass) michael@0: { michael@0: michael@0: /* Hebrew */ michael@0: michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC10: /* sheva */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC11: /* hataf segol */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC12: /* hataf patah */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC13: /* hataf qamats */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC14: /* hiriq */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC15: /* tsere */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC16: /* segol */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC17: /* patah */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC18: /* qamats */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC20: /* qubuts */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC22: /* meteg */ michael@0: return HB_UNICODE_COMBINING_CLASS_BELOW; michael@0: michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC23: /* rafe */ michael@0: return HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE; michael@0: michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC24: /* shin dot */ michael@0: return HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT; michael@0: michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC25: /* sin dot */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC19: /* holam */ michael@0: return HB_UNICODE_COMBINING_CLASS_ABOVE_LEFT; michael@0: michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC26: /* point varika */ michael@0: return HB_UNICODE_COMBINING_CLASS_ABOVE; michael@0: michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC21: /* dagesh */ michael@0: break; michael@0: michael@0: michael@0: /* Arabic and Syriac */ michael@0: michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC27: /* fathatan */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC28: /* dammatan */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC30: /* fatha */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC31: /* damma */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC33: /* shadda */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC34: /* sukun */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC35: /* superscript alef */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC36: /* superscript alaph */ michael@0: return HB_UNICODE_COMBINING_CLASS_ABOVE; michael@0: michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC29: /* kasratan */ michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC32: /* kasra */ michael@0: return HB_UNICODE_COMBINING_CLASS_BELOW; michael@0: michael@0: michael@0: /* Thai */ michael@0: michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC103: /* sara u / sara uu */ michael@0: return HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT; michael@0: michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC107: /* mai */ michael@0: return HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT; michael@0: michael@0: michael@0: /* Lao */ michael@0: michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC118: /* sign u / sign uu */ michael@0: return HB_UNICODE_COMBINING_CLASS_BELOW; michael@0: michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC122: /* mai */ michael@0: return HB_UNICODE_COMBINING_CLASS_ABOVE; michael@0: michael@0: michael@0: /* Tibetan */ michael@0: michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC129: /* sign aa */ michael@0: return HB_UNICODE_COMBINING_CLASS_BELOW; michael@0: michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC130: /* sign i*/ michael@0: return HB_UNICODE_COMBINING_CLASS_ABOVE; michael@0: michael@0: case HB_MODIFIED_COMBINING_CLASS_CCC132: /* sign u */ michael@0: return HB_UNICODE_COMBINING_CLASS_BELOW; michael@0: michael@0: } michael@0: michael@0: return klass; michael@0: } michael@0: michael@0: void michael@0: _hb_ot_shape_fallback_position_recategorize_marks (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: unsigned int count = buffer->len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: if (_hb_glyph_info_get_general_category (&buffer->info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) { michael@0: unsigned int combining_class = _hb_glyph_info_get_modified_combining_class (&buffer->info[i]); michael@0: combining_class = recategorize_combining_class (buffer->info[i].codepoint, combining_class); michael@0: _hb_glyph_info_set_modified_combining_class (&buffer->info[i], combining_class); michael@0: } michael@0: } michael@0: michael@0: michael@0: static void michael@0: zero_mark_advances (hb_buffer_t *buffer, michael@0: unsigned int start, michael@0: unsigned int end) michael@0: { michael@0: for (unsigned int i = start; i < end; i++) michael@0: if (_hb_glyph_info_get_general_category (&buffer->info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) michael@0: { michael@0: buffer->pos[i].x_advance = 0; michael@0: buffer->pos[i].y_advance = 0; michael@0: } michael@0: } michael@0: michael@0: static inline void michael@0: position_mark (const hb_ot_shape_plan_t *plan, michael@0: hb_font_t *font, michael@0: hb_buffer_t *buffer, michael@0: hb_glyph_extents_t &base_extents, michael@0: unsigned int i, michael@0: unsigned int combining_class) michael@0: { michael@0: hb_glyph_extents_t mark_extents; michael@0: if (!font->get_glyph_extents (buffer->info[i].codepoint, michael@0: &mark_extents)) michael@0: return; michael@0: michael@0: hb_position_t y_gap = font->y_scale / 16; michael@0: michael@0: hb_glyph_position_t &pos = buffer->pos[i]; michael@0: pos.x_offset = pos.y_offset = 0; michael@0: michael@0: michael@0: /* We dont position LEFT and RIGHT marks. */ michael@0: michael@0: /* X positioning */ michael@0: switch (combining_class) michael@0: { michael@0: case HB_UNICODE_COMBINING_CLASS_DOUBLE_BELOW: michael@0: case HB_UNICODE_COMBINING_CLASS_DOUBLE_ABOVE: michael@0: if (buffer->props.direction == HB_DIRECTION_LTR) { michael@0: pos.x_offset += base_extents.x_bearing - mark_extents.width / 2 - mark_extents.x_bearing; michael@0: break; michael@0: } else if (buffer->props.direction == HB_DIRECTION_RTL) { michael@0: pos.x_offset += base_extents.x_bearing + base_extents.width - mark_extents.width / 2 - mark_extents.x_bearing; michael@0: break; michael@0: } michael@0: /* Fall through */ michael@0: michael@0: default: michael@0: case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW: michael@0: case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE: michael@0: case HB_UNICODE_COMBINING_CLASS_BELOW: michael@0: case HB_UNICODE_COMBINING_CLASS_ABOVE: michael@0: /* Center align. */ michael@0: pos.x_offset += base_extents.x_bearing + (base_extents.width - mark_extents.width) / 2 - mark_extents.x_bearing; michael@0: break; michael@0: michael@0: case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW_LEFT: michael@0: case HB_UNICODE_COMBINING_CLASS_BELOW_LEFT: michael@0: case HB_UNICODE_COMBINING_CLASS_ABOVE_LEFT: michael@0: /* Left align. */ michael@0: pos.x_offset += base_extents.x_bearing - mark_extents.x_bearing; michael@0: break; michael@0: michael@0: case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE_RIGHT: michael@0: case HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT: michael@0: case HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT: michael@0: /* Right align. */ michael@0: pos.x_offset += base_extents.x_bearing + base_extents.width - mark_extents.width - mark_extents.x_bearing; michael@0: break; michael@0: } michael@0: michael@0: /* Y positioning */ michael@0: switch (combining_class) michael@0: { michael@0: case HB_UNICODE_COMBINING_CLASS_DOUBLE_BELOW: michael@0: case HB_UNICODE_COMBINING_CLASS_BELOW_LEFT: michael@0: case HB_UNICODE_COMBINING_CLASS_BELOW: michael@0: case HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT: michael@0: /* Add gap, fall-through. */ michael@0: base_extents.height -= y_gap; michael@0: michael@0: case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW_LEFT: michael@0: case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW: michael@0: pos.y_offset = base_extents.y_bearing + base_extents.height - mark_extents.y_bearing; michael@0: /* Never shift up "below" marks. */ michael@0: if ((y_gap > 0) == (pos.y_offset > 0)) michael@0: { michael@0: base_extents.height -= pos.y_offset; michael@0: pos.y_offset = 0; michael@0: } michael@0: base_extents.height += mark_extents.height; michael@0: break; michael@0: michael@0: case HB_UNICODE_COMBINING_CLASS_DOUBLE_ABOVE: michael@0: case HB_UNICODE_COMBINING_CLASS_ABOVE_LEFT: michael@0: case HB_UNICODE_COMBINING_CLASS_ABOVE: michael@0: case HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT: michael@0: /* Add gap, fall-through. */ michael@0: base_extents.y_bearing += y_gap; michael@0: base_extents.height -= y_gap; michael@0: michael@0: case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE: michael@0: case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE_RIGHT: michael@0: pos.y_offset = base_extents.y_bearing - (mark_extents.y_bearing + mark_extents.height); michael@0: /* Don't shift down "above" marks too much. */ michael@0: if ((y_gap > 0) != (pos.y_offset > 0)) michael@0: { michael@0: unsigned int correction = -pos.y_offset / 2; michael@0: base_extents.y_bearing += correction; michael@0: base_extents.height -= correction; michael@0: pos.y_offset += correction; michael@0: } michael@0: base_extents.y_bearing -= mark_extents.height; michael@0: base_extents.height += mark_extents.height; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: static inline void michael@0: position_around_base (const hb_ot_shape_plan_t *plan, michael@0: hb_font_t *font, michael@0: hb_buffer_t *buffer, michael@0: unsigned int base, michael@0: unsigned int end) michael@0: { michael@0: hb_direction_t horiz_dir = HB_DIRECTION_INVALID; michael@0: hb_glyph_extents_t base_extents; michael@0: if (!font->get_glyph_extents (buffer->info[base].codepoint, michael@0: &base_extents)) michael@0: { michael@0: /* If extents don't work, zero marks and go home. */ michael@0: zero_mark_advances (buffer, base + 1, end); michael@0: return; michael@0: } michael@0: base_extents.x_bearing += buffer->pos[base].x_offset; michael@0: base_extents.y_bearing += buffer->pos[base].y_offset; michael@0: michael@0: unsigned int lig_id = _hb_glyph_info_get_lig_id (&buffer->info[base]); michael@0: unsigned int num_lig_components = _hb_glyph_info_get_lig_num_comps (&buffer->info[base]); michael@0: michael@0: hb_position_t x_offset = 0, y_offset = 0; michael@0: if (HB_DIRECTION_IS_FORWARD (buffer->props.direction)) { michael@0: x_offset -= buffer->pos[base].x_advance; michael@0: y_offset -= buffer->pos[base].y_advance; michael@0: } michael@0: michael@0: hb_glyph_extents_t component_extents = base_extents; michael@0: unsigned int last_lig_component = (unsigned int) -1; michael@0: unsigned int last_combining_class = 255; michael@0: hb_glyph_extents_t cluster_extents = base_extents; /* Initialization is just to shut gcc up. */ michael@0: for (unsigned int i = base + 1; i < end; i++) michael@0: if (_hb_glyph_info_get_modified_combining_class (&buffer->info[i])) michael@0: { michael@0: if (num_lig_components > 1) { michael@0: unsigned int this_lig_id = _hb_glyph_info_get_lig_id (&buffer->info[i]); michael@0: unsigned int this_lig_component = _hb_glyph_info_get_lig_comp (&buffer->info[i]) - 1; michael@0: /* Conditions for attaching to the last component. */ michael@0: if (!lig_id || lig_id != this_lig_id || this_lig_component >= num_lig_components) michael@0: this_lig_component = num_lig_components - 1; michael@0: if (last_lig_component != this_lig_component) michael@0: { michael@0: last_lig_component = this_lig_component; michael@0: last_combining_class = 255; michael@0: component_extents = base_extents; michael@0: if (unlikely (horiz_dir == HB_DIRECTION_INVALID)) { michael@0: if (HB_DIRECTION_IS_HORIZONTAL (plan->props.direction)) michael@0: horiz_dir = plan->props.direction; michael@0: else michael@0: horiz_dir = hb_script_get_horizontal_direction (plan->props.script); michael@0: } michael@0: if (horiz_dir == HB_DIRECTION_LTR) michael@0: component_extents.x_bearing += (this_lig_component * component_extents.width) / num_lig_components; michael@0: else michael@0: component_extents.x_bearing += ((num_lig_components - 1 - this_lig_component) * component_extents.width) / num_lig_components; michael@0: component_extents.width /= num_lig_components; michael@0: } michael@0: } michael@0: michael@0: unsigned int this_combining_class = _hb_glyph_info_get_modified_combining_class (&buffer->info[i]); michael@0: if (last_combining_class != this_combining_class) michael@0: { michael@0: last_combining_class = this_combining_class; michael@0: cluster_extents = component_extents; michael@0: } michael@0: michael@0: position_mark (plan, font, buffer, cluster_extents, i, this_combining_class); michael@0: michael@0: buffer->pos[i].x_advance = 0; michael@0: buffer->pos[i].y_advance = 0; michael@0: buffer->pos[i].x_offset += x_offset; michael@0: buffer->pos[i].y_offset += y_offset; michael@0: michael@0: } else { michael@0: if (HB_DIRECTION_IS_FORWARD (buffer->props.direction)) { michael@0: x_offset -= buffer->pos[i].x_advance; michael@0: y_offset -= buffer->pos[i].y_advance; michael@0: } else { michael@0: x_offset += buffer->pos[i].x_advance; michael@0: y_offset += buffer->pos[i].y_advance; michael@0: } michael@0: } michael@0: } michael@0: michael@0: static inline void michael@0: position_cluster (const hb_ot_shape_plan_t *plan, michael@0: hb_font_t *font, michael@0: hb_buffer_t *buffer, michael@0: unsigned int start, michael@0: unsigned int end) michael@0: { michael@0: if (end - start < 2) michael@0: return; michael@0: michael@0: /* Find the base glyph */ michael@0: for (unsigned int i = start; i < end; i++) michael@0: if (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&buffer->info[i]))) michael@0: { michael@0: /* Find mark glyphs */ michael@0: unsigned int j; michael@0: for (j = i + 1; j < end; j++) michael@0: if (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&buffer->info[j]))) michael@0: break; michael@0: michael@0: position_around_base (plan, font, buffer, i, j); michael@0: michael@0: i = j - 1; michael@0: } michael@0: } michael@0: michael@0: void michael@0: _hb_ot_shape_fallback_position (const hb_ot_shape_plan_t *plan, michael@0: hb_font_t *font, michael@0: hb_buffer_t *buffer) michael@0: { michael@0: unsigned int start = 0; michael@0: unsigned int last_cluster = buffer->info[0].cluster; michael@0: unsigned int count = buffer->len; michael@0: for (unsigned int i = 1; i < count; i++) michael@0: if (buffer->info[i].cluster != last_cluster) { michael@0: position_cluster (plan, font, buffer, start, i); michael@0: start = i; michael@0: last_cluster = buffer->info[i].cluster; michael@0: } michael@0: position_cluster (plan, font, buffer, start, count); michael@0: } michael@0: michael@0: michael@0: /* Performs old-style TrueType kerning. */ michael@0: void michael@0: _hb_ot_shape_fallback_kern (const hb_ot_shape_plan_t *plan, michael@0: hb_font_t *font, michael@0: hb_buffer_t *buffer) michael@0: { michael@0: if (!plan->has_kern) return; michael@0: michael@0: unsigned int count = buffer->len; michael@0: michael@0: OT::hb_apply_context_t c (1, font, buffer); michael@0: c.set_lookup_mask (plan->kern_mask); michael@0: c.set_lookup_props (OT::LookupFlag::IgnoreMarks); michael@0: michael@0: hb_glyph_info_t *info = buffer->info; michael@0: hb_glyph_position_t *pos = buffer->pos; michael@0: michael@0: for (unsigned int idx = 0; idx < count;) michael@0: { michael@0: OT::hb_apply_context_t::skipping_forward_iterator_t skippy_iter (&c, idx, 1); michael@0: if (!skippy_iter.next ()) michael@0: { michael@0: idx++; michael@0: continue; michael@0: } michael@0: michael@0: hb_position_t x_kern, y_kern; michael@0: font->get_glyph_kerning_for_direction (info[idx].codepoint, michael@0: info[skippy_iter.idx].codepoint, michael@0: buffer->props.direction, michael@0: &x_kern, &y_kern); michael@0: michael@0: if (x_kern) michael@0: { michael@0: hb_position_t kern1 = x_kern >> 1; michael@0: hb_position_t kern2 = x_kern - kern1; michael@0: pos[idx].x_advance += kern1; michael@0: pos[skippy_iter.idx].x_advance += kern2; michael@0: pos[skippy_iter.idx].x_offset += kern2; michael@0: } michael@0: michael@0: if (y_kern) michael@0: { michael@0: hb_position_t kern1 = y_kern >> 1; michael@0: hb_position_t kern2 = y_kern - kern1; michael@0: pos[idx].y_advance += kern1; michael@0: pos[skippy_iter.idx].y_advance += kern2; michael@0: pos[skippy_iter.idx].y_offset += kern2; michael@0: } michael@0: michael@0: idx = skippy_iter.idx; michael@0: } michael@0: }