michael@0: /* michael@0: * Copyright © 2009,2010 Red Hat, Inc. michael@0: * Copyright © 2010,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: * Red Hat Author(s): Behdad Esfahbod michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #define HB_SHAPER ot michael@0: #define hb_ot_shaper_face_data_t hb_ot_layout_t michael@0: #define hb_ot_shaper_shape_plan_data_t hb_ot_shape_plan_t michael@0: #include "hb-shaper-impl-private.hh" michael@0: michael@0: #include "hb-ot-shape-private.hh" michael@0: #include "hb-ot-shape-complex-private.hh" michael@0: #include "hb-ot-shape-fallback-private.hh" michael@0: #include "hb-ot-shape-normalize-private.hh" michael@0: michael@0: #include "hb-ot-layout-private.hh" michael@0: #include "hb-set-private.hh" michael@0: michael@0: michael@0: static hb_tag_t common_features[] = { michael@0: HB_TAG('c','c','m','p'), michael@0: HB_TAG('l','i','g','a'), michael@0: HB_TAG('l','o','c','l'), michael@0: HB_TAG('m','a','r','k'), michael@0: HB_TAG('m','k','m','k'), michael@0: HB_TAG('r','l','i','g'), michael@0: }; michael@0: michael@0: michael@0: static hb_tag_t horizontal_features[] = { michael@0: HB_TAG('c','a','l','t'), michael@0: HB_TAG('c','l','i','g'), michael@0: HB_TAG('c','u','r','s'), michael@0: HB_TAG('k','e','r','n'), michael@0: HB_TAG('r','c','l','t'), michael@0: }; michael@0: michael@0: static hb_tag_t vertical_features[] = { michael@0: HB_TAG('v','e','r','t'), michael@0: }; michael@0: michael@0: michael@0: michael@0: static void michael@0: hb_ot_shape_collect_features (hb_ot_shape_planner_t *planner, michael@0: const hb_segment_properties_t *props, michael@0: const hb_feature_t *user_features, michael@0: unsigned int num_user_features) michael@0: { michael@0: hb_ot_map_builder_t *map = &planner->map; michael@0: michael@0: switch (props->direction) { michael@0: case HB_DIRECTION_LTR: michael@0: map->add_global_bool_feature (HB_TAG ('l','t','r','a')); michael@0: map->add_global_bool_feature (HB_TAG ('l','t','r','m')); michael@0: break; michael@0: case HB_DIRECTION_RTL: michael@0: map->add_global_bool_feature (HB_TAG ('r','t','l','a')); michael@0: map->add_feature (HB_TAG ('r','t','l','m'), 1, F_NONE); michael@0: break; michael@0: case HB_DIRECTION_TTB: michael@0: case HB_DIRECTION_BTT: michael@0: case HB_DIRECTION_INVALID: michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: map->add_feature (HB_TAG ('f','r','a','c'), 1, F_NONE); michael@0: map->add_feature (HB_TAG ('n','u','m','r'), 1, F_NONE); michael@0: map->add_feature (HB_TAG ('d','n','o','m'), 1, F_NONE); michael@0: michael@0: if (planner->shaper->collect_features) michael@0: planner->shaper->collect_features (planner); michael@0: michael@0: for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++) michael@0: map->add_global_bool_feature (common_features[i]); michael@0: michael@0: if (HB_DIRECTION_IS_HORIZONTAL (props->direction)) michael@0: for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++) michael@0: map->add_feature (horizontal_features[i], 1, F_GLOBAL | michael@0: (horizontal_features[i] == HB_TAG('k','e','r','n') ? michael@0: F_HAS_FALLBACK : F_NONE)); michael@0: else michael@0: for (unsigned int i = 0; i < ARRAY_LENGTH (vertical_features); i++) michael@0: map->add_feature (vertical_features[i], 1, F_GLOBAL | michael@0: (vertical_features[i] == HB_TAG('v','k','r','n') ? michael@0: F_HAS_FALLBACK : F_NONE)); michael@0: michael@0: if (planner->shaper->override_features) michael@0: planner->shaper->override_features (planner); michael@0: michael@0: for (unsigned int i = 0; i < num_user_features; i++) { michael@0: const hb_feature_t *feature = &user_features[i]; michael@0: map->add_feature (feature->tag, feature->value, michael@0: (feature->start == 0 && feature->end == (unsigned int) -1) ? michael@0: F_GLOBAL : F_NONE); michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * shaper face data michael@0: */ michael@0: michael@0: hb_ot_shaper_face_data_t * michael@0: _hb_ot_shaper_face_data_create (hb_face_t *face) michael@0: { michael@0: return _hb_ot_layout_create (face); michael@0: } michael@0: michael@0: void michael@0: _hb_ot_shaper_face_data_destroy (hb_ot_shaper_face_data_t *data) michael@0: { michael@0: _hb_ot_layout_destroy (data); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * shaper font data michael@0: */ michael@0: michael@0: struct hb_ot_shaper_font_data_t {}; michael@0: michael@0: hb_ot_shaper_font_data_t * michael@0: _hb_ot_shaper_font_data_create (hb_font_t *font) michael@0: { michael@0: return (hb_ot_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED; michael@0: } michael@0: michael@0: void michael@0: _hb_ot_shaper_font_data_destroy (hb_ot_shaper_font_data_t *data) michael@0: { michael@0: } michael@0: michael@0: michael@0: /* michael@0: * shaper shape_plan data michael@0: */ michael@0: michael@0: hb_ot_shaper_shape_plan_data_t * michael@0: _hb_ot_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan, michael@0: const hb_feature_t *user_features, michael@0: unsigned int num_user_features) michael@0: { michael@0: hb_ot_shape_plan_t *plan = (hb_ot_shape_plan_t *) calloc (1, sizeof (hb_ot_shape_plan_t)); michael@0: if (unlikely (!plan)) michael@0: return NULL; michael@0: michael@0: hb_ot_shape_planner_t planner (shape_plan); michael@0: michael@0: planner.shaper = hb_ot_shape_complex_categorize (&planner); michael@0: michael@0: hb_ot_shape_collect_features (&planner, &shape_plan->props, user_features, num_user_features); michael@0: michael@0: planner.compile (*plan); michael@0: michael@0: if (plan->shaper->data_create) { michael@0: plan->data = plan->shaper->data_create (plan); michael@0: if (unlikely (!plan->data)) michael@0: return NULL; michael@0: } michael@0: michael@0: return plan; michael@0: } michael@0: michael@0: void michael@0: _hb_ot_shaper_shape_plan_data_destroy (hb_ot_shaper_shape_plan_data_t *plan) michael@0: { michael@0: if (plan->shaper->data_destroy) michael@0: plan->shaper->data_destroy (const_cast (plan->data)); michael@0: michael@0: plan->finish (); michael@0: michael@0: free (plan); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * shaper michael@0: */ michael@0: michael@0: struct hb_ot_shape_context_t michael@0: { michael@0: hb_ot_shape_plan_t *plan; michael@0: hb_font_t *font; michael@0: hb_face_t *face; michael@0: hb_buffer_t *buffer; michael@0: const hb_feature_t *user_features; michael@0: unsigned int num_user_features; michael@0: michael@0: /* Transient stuff */ michael@0: hb_direction_t target_direction; michael@0: }; michael@0: michael@0: michael@0: michael@0: /* Main shaper */ michael@0: michael@0: michael@0: /* Prepare */ michael@0: michael@0: static void michael@0: hb_set_unicode_props (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: _hb_glyph_info_set_unicode_props (&buffer->info[i], buffer->unicode); michael@0: } michael@0: michael@0: static void michael@0: hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font) michael@0: { michael@0: if (!(buffer->flags & HB_BUFFER_FLAG_BOT) || michael@0: _hb_glyph_info_get_general_category (&buffer->info[0]) != michael@0: HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) michael@0: return; michael@0: michael@0: if (!font->has_glyph (0x25CC)) michael@0: return; michael@0: michael@0: hb_glyph_info_t dottedcircle; michael@0: dottedcircle.codepoint = 0x25CC; michael@0: _hb_glyph_info_set_unicode_props (&dottedcircle, buffer->unicode); michael@0: michael@0: buffer->clear_output (); michael@0: michael@0: buffer->idx = 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: buffer->output_info (info); michael@0: while (buffer->idx < buffer->len) michael@0: buffer->next_glyph (); michael@0: michael@0: buffer->swap_buffers (); michael@0: } michael@0: michael@0: static void michael@0: hb_form_clusters (hb_buffer_t *buffer) michael@0: { michael@0: unsigned int count = buffer->len; michael@0: for (unsigned int i = 1; i < count; i++) michael@0: if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&buffer->info[i]))) michael@0: buffer->merge_clusters (i - 1, i + 1); michael@0: } michael@0: michael@0: static void michael@0: hb_ensure_native_direction (hb_buffer_t *buffer) michael@0: { michael@0: hb_direction_t direction = buffer->props.direction; michael@0: michael@0: /* TODO vertical: michael@0: * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType michael@0: * Ogham fonts are supposed to be implemented BTT or not. Need to research that michael@0: * first. */ michael@0: if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) || michael@0: (HB_DIRECTION_IS_VERTICAL (direction) && direction != HB_DIRECTION_TTB)) michael@0: { michael@0: hb_buffer_reverse_clusters (buffer); michael@0: buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction); michael@0: } michael@0: } michael@0: michael@0: michael@0: /* Substitute */ michael@0: michael@0: static inline void michael@0: hb_ot_mirror_chars (hb_ot_shape_context_t *c) michael@0: { michael@0: if (HB_DIRECTION_IS_FORWARD (c->target_direction)) michael@0: return; michael@0: michael@0: hb_buffer_t *buffer = c->buffer; michael@0: hb_unicode_funcs_t *unicode = buffer->unicode; michael@0: hb_mask_t rtlm_mask = c->plan->rtlm_mask; michael@0: michael@0: unsigned int count = buffer->len; michael@0: hb_glyph_info_t *info = buffer->info; michael@0: for (unsigned int i = 0; i < count; i++) { michael@0: hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint); michael@0: if (likely (codepoint == info[i].codepoint)) michael@0: info[i].mask |= rtlm_mask; michael@0: else michael@0: info[i].codepoint = codepoint; michael@0: } michael@0: } michael@0: michael@0: static inline void michael@0: hb_ot_shape_setup_masks_fraction (hb_ot_shape_context_t *c) michael@0: { michael@0: if (!c->plan->has_frac) michael@0: return; michael@0: michael@0: hb_buffer_t *buffer = c->buffer; michael@0: michael@0: /* TODO look in pre/post context text also. */ michael@0: unsigned int count = buffer->len; michael@0: hb_glyph_info_t *info = buffer->info; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: { michael@0: if (info[i].codepoint == 0x2044) /* FRACTION SLASH */ michael@0: { michael@0: unsigned int start = i, end = i + 1; michael@0: while (start && michael@0: _hb_glyph_info_get_general_category (&info[start - 1]) == michael@0: HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER) michael@0: start--; michael@0: while (end < count && michael@0: _hb_glyph_info_get_general_category (&info[end]) == michael@0: HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER) michael@0: end++; michael@0: michael@0: for (unsigned int j = start; j < i; j++) michael@0: info[j].mask |= c->plan->numr_mask | c->plan->frac_mask; michael@0: info[i].mask |= c->plan->frac_mask; michael@0: for (unsigned int j = i + 1; j < end; j++) michael@0: info[j].mask |= c->plan->frac_mask | c->plan->dnom_mask; michael@0: michael@0: i = end - 1; michael@0: } michael@0: } michael@0: } michael@0: michael@0: static inline void michael@0: hb_ot_shape_initialize_masks (hb_ot_shape_context_t *c) michael@0: { michael@0: hb_ot_map_t *map = &c->plan->map; michael@0: hb_buffer_t *buffer = c->buffer; michael@0: michael@0: hb_mask_t global_mask = map->get_global_mask (); michael@0: buffer->reset_masks (global_mask); michael@0: } michael@0: michael@0: static inline void michael@0: hb_ot_shape_setup_masks (hb_ot_shape_context_t *c) michael@0: { michael@0: hb_ot_map_t *map = &c->plan->map; michael@0: hb_buffer_t *buffer = c->buffer; michael@0: michael@0: hb_ot_shape_setup_masks_fraction (c); michael@0: michael@0: if (c->plan->shaper->setup_masks) michael@0: c->plan->shaper->setup_masks (c->plan, buffer, c->font); michael@0: michael@0: for (unsigned int i = 0; i < c->num_user_features; i++) michael@0: { michael@0: const hb_feature_t *feature = &c->user_features[i]; michael@0: if (!(feature->start == 0 && feature->end == (unsigned int)-1)) { michael@0: unsigned int shift; michael@0: hb_mask_t mask = map->get_mask (feature->tag, &shift); michael@0: buffer->set_masks (feature->value << shift, mask, feature->start, feature->end); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static inline void michael@0: hb_ot_map_glyphs_fast (hb_buffer_t *buffer) michael@0: { michael@0: /* Normalization process sets up glyph_index(), we just copy it. */ michael@0: unsigned int count = buffer->len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: buffer->info[i].codepoint = buffer->info[i].glyph_index(); michael@0: } michael@0: michael@0: static inline void michael@0: hb_synthesize_glyph_classes (hb_ot_shape_context_t *c) michael@0: { michael@0: unsigned int count = c->buffer->len; michael@0: hb_glyph_info_t *info = c->buffer->info; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: _hb_glyph_info_set_glyph_props (&info[i], michael@0: _hb_glyph_info_get_general_category (&info[i]) michael@0: == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ? michael@0: HB_OT_LAYOUT_GLYPH_PROPS_MARK : michael@0: HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH); michael@0: } michael@0: michael@0: static inline void michael@0: hb_ot_substitute_default (hb_ot_shape_context_t *c) michael@0: { michael@0: hb_buffer_t *buffer = c->buffer; michael@0: michael@0: if (c->plan->shaper->preprocess_text) michael@0: c->plan->shaper->preprocess_text (c->plan, buffer, c->font); michael@0: michael@0: hb_ot_shape_initialize_masks (c); michael@0: michael@0: hb_ot_mirror_chars (c); michael@0: michael@0: HB_BUFFER_ALLOCATE_VAR (buffer, glyph_index); michael@0: michael@0: _hb_ot_shape_normalize (c->plan, buffer, c->font); michael@0: michael@0: hb_ot_shape_setup_masks (c); michael@0: michael@0: /* This is unfortunate to go here, but necessary... */ michael@0: if (!hb_ot_layout_has_positioning (c->face)) michael@0: _hb_ot_shape_fallback_position_recategorize_marks (c->plan, c->font, buffer); michael@0: michael@0: hb_ot_map_glyphs_fast (buffer); michael@0: michael@0: HB_BUFFER_DEALLOCATE_VAR (buffer, glyph_index); michael@0: } michael@0: michael@0: static inline void michael@0: hb_ot_substitute_complex (hb_ot_shape_context_t *c) michael@0: { michael@0: hb_buffer_t *buffer = c->buffer; michael@0: michael@0: hb_ot_layout_substitute_start (c->font, buffer); michael@0: michael@0: if (!hb_ot_layout_has_glyph_classes (c->face)) michael@0: hb_synthesize_glyph_classes (c); michael@0: michael@0: c->plan->substitute (c->font, buffer); michael@0: michael@0: hb_ot_layout_substitute_finish (c->font, buffer); michael@0: michael@0: return; michael@0: } michael@0: michael@0: static inline void michael@0: hb_ot_substitute (hb_ot_shape_context_t *c) michael@0: { michael@0: hb_ot_substitute_default (c); michael@0: hb_ot_substitute_complex (c); michael@0: } michael@0: michael@0: /* Position */ michael@0: michael@0: static inline void michael@0: zero_mark_widths_by_unicode (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: { 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: zero_mark_widths_by_gdef (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_is_mark (&buffer->info[i])) 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: hb_ot_position_default (hb_ot_shape_context_t *c) michael@0: { michael@0: hb_direction_t direction = c->buffer->props.direction; michael@0: unsigned int count = c->buffer->len; michael@0: hb_glyph_info_t *info = c->buffer->info; michael@0: hb_glyph_position_t *pos = c->buffer->pos; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: { michael@0: c->font->get_glyph_advance_for_direction (info[i].codepoint, michael@0: direction, michael@0: &pos[i].x_advance, michael@0: &pos[i].y_advance); michael@0: c->font->subtract_glyph_origin_for_direction (info[i].codepoint, michael@0: direction, michael@0: &pos[i].x_offset, michael@0: &pos[i].y_offset); michael@0: michael@0: } michael@0: } michael@0: michael@0: static inline bool michael@0: hb_ot_position_complex (hb_ot_shape_context_t *c) michael@0: { michael@0: bool ret = false; michael@0: unsigned int count = c->buffer->len; michael@0: michael@0: switch (c->plan->shaper->zero_width_marks) michael@0: { michael@0: case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY: michael@0: zero_mark_widths_by_gdef (c->buffer); michael@0: break; michael@0: michael@0: /* Not currently used for any shaper: michael@0: case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_EARLY: michael@0: zero_mark_widths_by_unicode (c->buffer); michael@0: break; michael@0: */ michael@0: michael@0: default: michael@0: case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE: michael@0: case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_LATE: michael@0: case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE: michael@0: break; michael@0: } michael@0: michael@0: if (hb_ot_layout_has_positioning (c->face)) michael@0: { michael@0: hb_glyph_info_t *info = c->buffer->info; michael@0: hb_glyph_position_t *pos = c->buffer->pos; michael@0: michael@0: /* Change glyph origin to what GPOS expects, apply GPOS, change it back. */ michael@0: michael@0: for (unsigned int i = 0; i < count; i++) { michael@0: c->font->add_glyph_origin_for_direction (info[i].codepoint, michael@0: HB_DIRECTION_LTR, michael@0: &pos[i].x_offset, michael@0: &pos[i].y_offset); michael@0: } michael@0: michael@0: c->plan->position (c->font, c->buffer); michael@0: michael@0: for (unsigned int i = 0; i < count; i++) { michael@0: c->font->subtract_glyph_origin_for_direction (info[i].codepoint, michael@0: HB_DIRECTION_LTR, michael@0: &pos[i].x_offset, michael@0: &pos[i].y_offset); michael@0: } michael@0: michael@0: ret = true; michael@0: } michael@0: michael@0: switch (c->plan->shaper->zero_width_marks) michael@0: { michael@0: case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_LATE: michael@0: zero_mark_widths_by_unicode (c->buffer); michael@0: break; michael@0: michael@0: case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE: michael@0: zero_mark_widths_by_gdef (c->buffer); michael@0: break; michael@0: michael@0: default: michael@0: case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE: michael@0: //case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_EARLY: michael@0: case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY: michael@0: break; michael@0: } michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: static inline void michael@0: hb_ot_position (hb_ot_shape_context_t *c) michael@0: { michael@0: hb_ot_layout_position_start (c->font, c->buffer); michael@0: michael@0: hb_ot_position_default (c); michael@0: michael@0: hb_bool_t fallback = !hb_ot_position_complex (c); michael@0: michael@0: hb_ot_layout_position_finish (c->font, c->buffer); michael@0: michael@0: if (fallback && c->plan->shaper->fallback_position) michael@0: _hb_ot_shape_fallback_position (c->plan, c->font, c->buffer); michael@0: michael@0: if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction)) michael@0: hb_buffer_reverse (c->buffer); michael@0: michael@0: /* Visual fallback goes here. */ michael@0: michael@0: if (fallback) michael@0: _hb_ot_shape_fallback_kern (c->plan, c->font, c->buffer); michael@0: } michael@0: michael@0: michael@0: /* Post-process */ michael@0: michael@0: static void michael@0: hb_ot_hide_default_ignorables (hb_ot_shape_context_t *c) michael@0: { michael@0: if (c->buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES) michael@0: return; michael@0: michael@0: hb_codepoint_t space; michael@0: enum { michael@0: SPACE_DONT_KNOW, michael@0: SPACE_AVAILABLE, michael@0: SPACE_UNAVAILABLE michael@0: } space_status = SPACE_DONT_KNOW; michael@0: michael@0: unsigned int count = c->buffer->len; michael@0: hb_glyph_info_t *info = c->buffer->info; michael@0: hb_glyph_position_t *pos = c->buffer->pos; michael@0: unsigned int j = 0; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: { michael@0: if (unlikely (!_hb_glyph_info_ligated (&info[i]) && michael@0: _hb_glyph_info_is_default_ignorable (&info[i]))) michael@0: { michael@0: if (space_status == SPACE_DONT_KNOW) michael@0: space_status = c->font->get_glyph (' ', 0, &space) ? SPACE_AVAILABLE : SPACE_UNAVAILABLE; michael@0: michael@0: if (space_status == SPACE_AVAILABLE) michael@0: { michael@0: info[i].codepoint = space; michael@0: pos[i].x_advance = 0; michael@0: pos[i].y_advance = 0; michael@0: } michael@0: else michael@0: continue; /* Delete it. */ michael@0: } michael@0: if (j != i) michael@0: { michael@0: info[j] = info[i]; michael@0: pos[j] = pos[i]; michael@0: } michael@0: j++; michael@0: } michael@0: c->buffer->len = j; michael@0: } michael@0: michael@0: michael@0: /* Pull it all together! */ michael@0: michael@0: static void michael@0: hb_ot_shape_internal (hb_ot_shape_context_t *c) michael@0: { michael@0: c->buffer->deallocate_var_all (); michael@0: michael@0: /* Save the original direction, we use it later. */ michael@0: c->target_direction = c->buffer->props.direction; michael@0: michael@0: _hb_buffer_allocate_unicode_vars (c->buffer); michael@0: michael@0: c->buffer->clear_output (); michael@0: michael@0: hb_set_unicode_props (c->buffer); michael@0: hb_insert_dotted_circle (c->buffer, c->font); michael@0: hb_form_clusters (c->buffer); michael@0: michael@0: hb_ensure_native_direction (c->buffer); michael@0: michael@0: hb_ot_substitute (c); michael@0: hb_ot_position (c); michael@0: michael@0: hb_ot_hide_default_ignorables (c); michael@0: michael@0: _hb_buffer_deallocate_unicode_vars (c->buffer); michael@0: michael@0: c->buffer->props.direction = c->target_direction; michael@0: michael@0: c->buffer->deallocate_var_all (); michael@0: } michael@0: michael@0: michael@0: hb_bool_t michael@0: _hb_ot_shape (hb_shape_plan_t *shape_plan, michael@0: hb_font_t *font, michael@0: hb_buffer_t *buffer, michael@0: const hb_feature_t *features, michael@0: unsigned int num_features) michael@0: { michael@0: hb_ot_shape_context_t c = {HB_SHAPER_DATA_GET (shape_plan), font, font->face, buffer, features, num_features}; michael@0: hb_ot_shape_internal (&c); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: michael@0: void michael@0: hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan, michael@0: hb_tag_t table_tag, michael@0: hb_set_t *lookup_indexes /* OUT */) michael@0: { michael@0: /* XXX Does the first part always succeed? */ michael@0: HB_SHAPER_DATA_GET (shape_plan)->collect_lookups (table_tag, lookup_indexes); michael@0: } michael@0: michael@0: michael@0: /* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */ michael@0: static void michael@0: add_char (hb_font_t *font, michael@0: hb_unicode_funcs_t *unicode, michael@0: hb_bool_t mirror, michael@0: hb_codepoint_t u, michael@0: hb_set_t *glyphs) michael@0: { michael@0: hb_codepoint_t glyph; michael@0: if (font->get_glyph (u, 0, &glyph)) michael@0: glyphs->add (glyph); michael@0: if (mirror) michael@0: { michael@0: hb_codepoint_t m = unicode->mirroring (u); michael@0: if (m != u && font->get_glyph (m, 0, &glyph)) michael@0: glyphs->add (glyph); michael@0: } michael@0: } michael@0: michael@0: michael@0: void michael@0: hb_ot_shape_glyphs_closure (hb_font_t *font, michael@0: hb_buffer_t *buffer, michael@0: const hb_feature_t *features, michael@0: unsigned int num_features, michael@0: hb_set_t *glyphs) michael@0: { michael@0: hb_ot_shape_plan_t plan; michael@0: michael@0: const char *shapers[] = {"ot", NULL}; michael@0: hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props, michael@0: features, num_features, shapers); michael@0: michael@0: bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL; michael@0: michael@0: unsigned int count = buffer->len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: add_char (font, buffer->unicode, mirror, buffer->info[i].codepoint, glyphs); michael@0: michael@0: hb_set_t lookups; michael@0: lookups.init (); michael@0: hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, &lookups); michael@0: michael@0: /* And find transitive closure. */ michael@0: hb_set_t copy; michael@0: copy.init (); michael@0: do { michael@0: copy.set (glyphs); michael@0: for (hb_codepoint_t lookup_index = -1; hb_set_next (&lookups, &lookup_index);) michael@0: hb_ot_layout_lookup_substitute_closure (font->face, lookup_index, glyphs); michael@0: } while (!copy.is_equal (glyphs)); michael@0: michael@0: hb_shape_plan_destroy (shape_plan); michael@0: }