michael@0: /* michael@0: * Copyright © 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-shape-plan-private.hh" michael@0: #include "hb-shaper-private.hh" michael@0: #include "hb-font-private.hh" michael@0: #include "hb-buffer-private.hh" michael@0: michael@0: #define HB_SHAPER_IMPLEMENT(shaper) \ michael@0: HB_SHAPER_DATA_ENSURE_DECLARE(shaper, face) \ michael@0: HB_SHAPER_DATA_ENSURE_DECLARE(shaper, font) michael@0: #include "hb-shaper-list.hh" michael@0: #undef HB_SHAPER_IMPLEMENT michael@0: michael@0: michael@0: static void michael@0: hb_shape_plan_plan (hb_shape_plan_t *shape_plan, michael@0: const hb_feature_t *user_features, michael@0: unsigned int num_user_features, michael@0: const char * const *shaper_list) michael@0: { michael@0: const hb_shaper_pair_t *shapers = _hb_shapers_get (); michael@0: michael@0: #define HB_SHAPER_PLAN(shaper) \ michael@0: HB_STMT_START { \ michael@0: if (hb_##shaper##_shaper_face_data_ensure (shape_plan->face_unsafe)) { \ michael@0: HB_SHAPER_DATA (shaper, shape_plan) = \ michael@0: HB_SHAPER_DATA_CREATE_FUNC (shaper, shape_plan) (shape_plan, user_features, num_user_features); \ michael@0: shape_plan->shaper_func = _hb_##shaper##_shape; \ michael@0: shape_plan->shaper_name = #shaper; \ michael@0: return; \ michael@0: } \ michael@0: } HB_STMT_END michael@0: michael@0: if (likely (!shaper_list)) { michael@0: for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++) michael@0: if (0) michael@0: ; michael@0: #define HB_SHAPER_IMPLEMENT(shaper) \ michael@0: else if (shapers[i].func == _hb_##shaper##_shape) \ michael@0: HB_SHAPER_PLAN (shaper); michael@0: #include "hb-shaper-list.hh" michael@0: #undef HB_SHAPER_IMPLEMENT michael@0: } else { michael@0: for (; *shaper_list; shaper_list++) michael@0: if (0) michael@0: ; michael@0: #define HB_SHAPER_IMPLEMENT(shaper) \ michael@0: else if (0 == strcmp (*shaper_list, #shaper)) \ michael@0: HB_SHAPER_PLAN (shaper); michael@0: #include "hb-shaper-list.hh" michael@0: #undef HB_SHAPER_IMPLEMENT michael@0: } michael@0: michael@0: #undef HB_SHAPER_PLAN michael@0: } michael@0: michael@0: michael@0: /* michael@0: * hb_shape_plan_t michael@0: */ michael@0: michael@0: /** michael@0: * hb_shape_plan_create: (Xconstructor) michael@0: * @face: michael@0: * @props: michael@0: * @user_features: (array length=num_user_features): michael@0: * @num_user_features: michael@0: * @shaper_list: (array zero-terminated=1): michael@0: * michael@0: * michael@0: * michael@0: * Return value: (transfer full): michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_shape_plan_t * michael@0: hb_shape_plan_create (hb_face_t *face, 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: const char * const *shaper_list) michael@0: { michael@0: assert (props->direction != HB_DIRECTION_INVALID); michael@0: michael@0: hb_shape_plan_t *shape_plan; michael@0: hb_feature_t *features = NULL; michael@0: michael@0: if (unlikely (!face)) michael@0: face = hb_face_get_empty (); michael@0: if (unlikely (!props || hb_object_is_inert (face))) michael@0: return hb_shape_plan_get_empty (); michael@0: if (num_user_features && !(features = (hb_feature_t *) malloc (num_user_features * sizeof (hb_feature_t)))) michael@0: return hb_shape_plan_get_empty (); michael@0: if (!(shape_plan = hb_object_create ())) { michael@0: free (features); michael@0: return hb_shape_plan_get_empty (); michael@0: } michael@0: michael@0: hb_face_make_immutable (face); michael@0: shape_plan->default_shaper_list = shaper_list == NULL; michael@0: shape_plan->face_unsafe = face; michael@0: shape_plan->props = *props; michael@0: shape_plan->num_user_features = num_user_features; michael@0: shape_plan->user_features = features; michael@0: if (num_user_features) michael@0: memcpy (features, user_features, num_user_features * sizeof (hb_feature_t)); michael@0: michael@0: hb_shape_plan_plan (shape_plan, user_features, num_user_features, shaper_list); michael@0: michael@0: return shape_plan; michael@0: } michael@0: michael@0: /** michael@0: * hb_shape_plan_get_empty: michael@0: * michael@0: * michael@0: * michael@0: * Return value: (transfer full): michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_shape_plan_t * michael@0: hb_shape_plan_get_empty (void) michael@0: { michael@0: static const hb_shape_plan_t _hb_shape_plan_nil = { michael@0: HB_OBJECT_HEADER_STATIC, michael@0: michael@0: true, /* default_shaper_list */ michael@0: NULL, /* face */ michael@0: HB_SEGMENT_PROPERTIES_DEFAULT, /* props */ michael@0: michael@0: NULL, /* shaper_func */ michael@0: NULL, /* shaper_name */ michael@0: michael@0: NULL, /* user_features */ michael@0: 0, /* num_user_featurs */ michael@0: michael@0: { michael@0: #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID, michael@0: #include "hb-shaper-list.hh" michael@0: #undef HB_SHAPER_IMPLEMENT michael@0: } michael@0: }; michael@0: michael@0: return const_cast (&_hb_shape_plan_nil); michael@0: } michael@0: michael@0: /** michael@0: * hb_shape_plan_reference: (skip) michael@0: * @shape_plan: a shape plan. michael@0: * michael@0: * michael@0: * michael@0: * Return value: (transfer full): michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_shape_plan_t * michael@0: hb_shape_plan_reference (hb_shape_plan_t *shape_plan) michael@0: { michael@0: return hb_object_reference (shape_plan); michael@0: } michael@0: michael@0: /** michael@0: * hb_shape_plan_destroy: (skip) michael@0: * @shape_plan: a shape plan. michael@0: * michael@0: * michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: void michael@0: hb_shape_plan_destroy (hb_shape_plan_t *shape_plan) michael@0: { michael@0: if (!hb_object_destroy (shape_plan)) return; michael@0: michael@0: #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, shape_plan); michael@0: #include "hb-shaper-list.hh" michael@0: #undef HB_SHAPER_IMPLEMENT michael@0: michael@0: free (shape_plan->user_features); michael@0: michael@0: free (shape_plan); michael@0: } michael@0: michael@0: /** michael@0: * hb_shape_plan_set_user_data: (skip) michael@0: * @shape_plan: a shape plan. michael@0: * @key: michael@0: * @data: michael@0: * @destroy: michael@0: * @replace: michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_bool_t michael@0: hb_shape_plan_set_user_data (hb_shape_plan_t *shape_plan, michael@0: hb_user_data_key_t *key, michael@0: void * data, michael@0: hb_destroy_func_t destroy, michael@0: hb_bool_t replace) michael@0: { michael@0: return hb_object_set_user_data (shape_plan, key, data, destroy, replace); michael@0: } michael@0: michael@0: /** michael@0: * hb_shape_plan_get_user_data: (skip) michael@0: * @shape_plan: a shape plan. michael@0: * @key: michael@0: * michael@0: * michael@0: * michael@0: * Return value: (transfer none): michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: void * michael@0: hb_shape_plan_get_user_data (hb_shape_plan_t *shape_plan, michael@0: hb_user_data_key_t *key) michael@0: { michael@0: return hb_object_get_user_data (shape_plan, key); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * hb_shape_plan_execute: michael@0: * @shape_plan: a shape plan. michael@0: * @font: a font. michael@0: * @buffer: a buffer. michael@0: * @features: (array length=num_features): michael@0: * @num_features: michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_bool_t michael@0: hb_shape_plan_execute (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: if (unlikely (hb_object_is_inert (shape_plan) || michael@0: hb_object_is_inert (font) || michael@0: hb_object_is_inert (buffer))) michael@0: return false; michael@0: michael@0: assert (shape_plan->face_unsafe == font->face); michael@0: assert (hb_segment_properties_equal (&shape_plan->props, &buffer->props)); michael@0: michael@0: #define HB_SHAPER_EXECUTE(shaper) \ michael@0: HB_STMT_START { \ michael@0: return HB_SHAPER_DATA (shaper, shape_plan) && \ michael@0: hb_##shaper##_shaper_font_data_ensure (font) && \ michael@0: _hb_##shaper##_shape (shape_plan, font, buffer, features, num_features); \ michael@0: } HB_STMT_END michael@0: michael@0: if (0) michael@0: ; michael@0: #define HB_SHAPER_IMPLEMENT(shaper) \ michael@0: else if (shape_plan->shaper_func == _hb_##shaper##_shape) \ michael@0: HB_SHAPER_EXECUTE (shaper); michael@0: #include "hb-shaper-list.hh" michael@0: #undef HB_SHAPER_IMPLEMENT michael@0: michael@0: #undef HB_SHAPER_EXECUTE michael@0: michael@0: return false; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * caching michael@0: */ michael@0: michael@0: #if 0 michael@0: static unsigned int michael@0: hb_shape_plan_hash (const hb_shape_plan_t *shape_plan) michael@0: { michael@0: return hb_segment_properties_hash (&shape_plan->props) + michael@0: shape_plan->default_shaper_list ? 0 : (intptr_t) shape_plan->shaper_func; michael@0: } michael@0: #endif michael@0: michael@0: /* User-feature caching is currently somewhat dumb: michael@0: * it only finds matches where the feature array is identical, michael@0: * not cases where the feature lists would be compatible for plan purposes michael@0: * but have different ranges, for example. michael@0: */ michael@0: struct hb_shape_plan_proposal_t michael@0: { michael@0: const hb_segment_properties_t props; michael@0: const char * const *shaper_list; michael@0: const hb_feature_t *user_features; michael@0: unsigned int num_user_features; michael@0: hb_shape_func_t *shaper_func; michael@0: }; michael@0: michael@0: static inline hb_bool_t michael@0: hb_shape_plan_user_features_match (const hb_shape_plan_t *shape_plan, michael@0: const hb_shape_plan_proposal_t *proposal) michael@0: { michael@0: if (proposal->num_user_features != shape_plan->num_user_features) return false; michael@0: for (unsigned int i = 0, n = proposal->num_user_features; i < n; i++) michael@0: if (proposal->user_features[i].tag != shape_plan->user_features[i].tag || michael@0: proposal->user_features[i].value != shape_plan->user_features[i].value || michael@0: proposal->user_features[i].start != shape_plan->user_features[i].start || michael@0: proposal->user_features[i].end != shape_plan->user_features[i].end) return false; michael@0: return true; michael@0: } michael@0: michael@0: static hb_bool_t michael@0: hb_shape_plan_matches (const hb_shape_plan_t *shape_plan, michael@0: const hb_shape_plan_proposal_t *proposal) michael@0: { michael@0: return hb_segment_properties_equal (&shape_plan->props, &proposal->props) && michael@0: hb_shape_plan_user_features_match (shape_plan, proposal) && michael@0: ((shape_plan->default_shaper_list && proposal->shaper_list == NULL) || michael@0: (shape_plan->shaper_func == proposal->shaper_func)); michael@0: } michael@0: michael@0: static inline hb_bool_t michael@0: hb_non_global_user_features_present (const hb_feature_t *user_features, michael@0: unsigned int num_user_features) michael@0: { michael@0: while (num_user_features) michael@0: if (user_features->start != 0 || user_features->end != (unsigned int) -1) michael@0: return true; michael@0: else michael@0: num_user_features--, user_features++; michael@0: return false; michael@0: } michael@0: michael@0: /** michael@0: * hb_shape_plan_create_cached: michael@0: * @face: michael@0: * @props: michael@0: * @user_features: (array length=num_user_features): michael@0: * @num_user_features: michael@0: * @shaper_list: (array zero-terminated=1): michael@0: * michael@0: * michael@0: * michael@0: * Return value: (transfer full): michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_shape_plan_t * michael@0: hb_shape_plan_create_cached (hb_face_t *face, 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: const char * const *shaper_list) michael@0: { michael@0: hb_shape_plan_proposal_t proposal = { michael@0: *props, michael@0: shaper_list, michael@0: user_features, michael@0: num_user_features, michael@0: NULL michael@0: }; michael@0: michael@0: if (shaper_list) { michael@0: /* Choose shaper. Adapted from hb_shape_plan_plan(). */ michael@0: #define HB_SHAPER_PLAN(shaper) \ michael@0: HB_STMT_START { \ michael@0: if (hb_##shaper##_shaper_face_data_ensure (face)) \ michael@0: proposal.shaper_func = _hb_##shaper##_shape; \ michael@0: } HB_STMT_END michael@0: michael@0: for (const char * const *shaper_item = shaper_list; *shaper_item; shaper_item++) michael@0: if (0) michael@0: ; michael@0: #define HB_SHAPER_IMPLEMENT(shaper) \ michael@0: else if (0 == strcmp (*shaper_item, #shaper)) \ michael@0: HB_SHAPER_PLAN (shaper); michael@0: #include "hb-shaper-list.hh" michael@0: #undef HB_SHAPER_IMPLEMENT michael@0: michael@0: #undef HB_SHAPER_PLAN michael@0: michael@0: if (unlikely (!proposal.shaper_list)) michael@0: return hb_shape_plan_get_empty (); michael@0: } michael@0: michael@0: michael@0: retry: michael@0: hb_face_t::plan_node_t *cached_plan_nodes = (hb_face_t::plan_node_t *) hb_atomic_ptr_get (&face->shape_plans); michael@0: for (hb_face_t::plan_node_t *node = cached_plan_nodes; node; node = node->next) michael@0: if (hb_shape_plan_matches (node->shape_plan, &proposal)) michael@0: return hb_shape_plan_reference (node->shape_plan); michael@0: michael@0: /* Not found. */ michael@0: michael@0: hb_shape_plan_t *shape_plan = hb_shape_plan_create (face, props, user_features, num_user_features, shaper_list); michael@0: michael@0: /* Don't add the plan to the cache if there were user features with non-global ranges */ michael@0: michael@0: if (hb_non_global_user_features_present (user_features, num_user_features)) michael@0: return shape_plan; michael@0: michael@0: hb_face_t::plan_node_t *node = (hb_face_t::plan_node_t *) calloc (1, sizeof (hb_face_t::plan_node_t)); michael@0: if (unlikely (!node)) michael@0: return shape_plan; michael@0: michael@0: node->shape_plan = shape_plan; michael@0: node->next = cached_plan_nodes; michael@0: michael@0: if (!hb_atomic_ptr_cmpexch (&face->shape_plans, cached_plan_nodes, node)) { michael@0: hb_shape_plan_destroy (shape_plan); michael@0: free (node); michael@0: goto retry; michael@0: } michael@0: michael@0: return hb_shape_plan_reference (shape_plan); michael@0: } michael@0: michael@0: /** michael@0: * hb_shape_plan_get_shaper: michael@0: * @shape_plan: a shape plan. michael@0: * michael@0: * michael@0: * michael@0: * Return value: (transfer none): michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: const char * michael@0: hb_shape_plan_get_shaper (hb_shape_plan_t *shape_plan) michael@0: { michael@0: return shape_plan->shaper_name; michael@0: }