diff --git a/src/Makefile.am b/src/Makefile.am index 847c9e77f..9b583fdbc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -47,6 +47,7 @@ HBSOURCES = \ hb-shaper-list.hh \ hb-shaper-impl-private.hh \ hb-shaper-private.hh \ + hb-shaper.cc \ hb-tt-font.cc \ hb-unicode-private.hh \ hb-unicode.cc \ diff --git a/src/hb-fallback-shape.cc b/src/hb-fallback-shape.cc index 4c9a3b365..48aba7312 100644 --- a/src/hb-fallback-shape.cc +++ b/src/hb-fallback-shape.cc @@ -87,7 +87,8 @@ _hb_fallback_shaper_shape_plan_data_destroy (hb_fallback_shaper_shape_plan_data_ */ hb_bool_t -_hb_fallback_shape (hb_font_t *font, +_hb_fallback_shape (hb_shape_plan_t *shape_plan, + hb_font_t *font, hb_buffer_t *buffer, const hb_feature_t *features HB_UNUSED, unsigned int num_features HB_UNUSED) diff --git a/src/hb-old.cc b/src/hb-old.cc index b898b3f6e..879ee70b1 100644 --- a/src/hb-old.cc +++ b/src/hb-old.cc @@ -302,7 +302,8 @@ _hb_old_font_get (hb_font_t *font) } hb_bool_t -_hb_old_shape (hb_font_t *font, +_hb_old_shape (hb_shape_plan_t *shape_plan, + hb_font_t *font, hb_buffer_t *buffer, const hb_feature_t *features, unsigned int num_features) diff --git a/src/hb-ot-shape.cc b/src/hb-ot-shape.cc index 512ffa5ef..a3c4153d2 100644 --- a/src/hb-ot-shape.cc +++ b/src/hb-ot-shape.cc @@ -541,7 +541,8 @@ hb_ot_shape_execute (hb_ot_shape_plan_t *plan, } hb_bool_t -_hb_ot_shape (hb_font_t *font, +_hb_ot_shape (hb_shape_plan_t *shape_plan, + hb_font_t *font, hb_buffer_t *buffer, const hb_feature_t *features, unsigned int num_features) diff --git a/src/hb-shape-plan-private.hh b/src/hb-shape-plan-private.hh index 1f54608ed..68246f47b 100644 --- a/src/hb-shape-plan-private.hh +++ b/src/hb-shape-plan-private.hh @@ -39,7 +39,7 @@ struct hb_shape_plan_t hb_object_header_t header; ASSERT_POD (); - hb_shape_func_t *shapers[HB_NUM_SHAPERS]; + hb_shape_func_t *shapers[HB_SHAPERS_COUNT]; struct hb_shaper_data_t shaper_data; }; diff --git a/src/hb-shape-plan.cc b/src/hb-shape-plan.cc index b96d5f227..e76847d71 100644 --- a/src/hb-shape-plan.cc +++ b/src/hb-shape-plan.cc @@ -53,6 +53,8 @@ hb_shape_plan_create (hb_face_t *face, hb_face_make_immutable (face); + /* Plan! */ + return shape_plan; } diff --git a/src/hb-shape.cc b/src/hb-shape.cc index a1f340249..3915de786 100644 --- a/src/hb-shape.cc +++ b/src/hb-shape.cc @@ -30,85 +30,6 @@ #include "hb-buffer-private.hh" -static const struct hb_shaper_pair_t { - char name[16]; - hb_shape_func_t *func; -} all_shapers[] = { -#define HB_SHAPER_IMPLEMENT(name) {#name, _hb_##name##_shape}, -#include "hb-shaper-list.hh" -#undef HB_SHAPER_IMPLEMENT -}; - - -/* Thread-safe, lock-free, shapers */ - -static const hb_shaper_pair_t *static_shapers; - -static -void free_static_shapers (void) -{ - if (unlikely (static_shapers != all_shapers)) - free ((void *) static_shapers); -} - -static const hb_shaper_pair_t * -get_shapers (void) -{ -retry: - hb_shaper_pair_t *shapers = (hb_shaper_pair_t *) hb_atomic_ptr_get (&static_shapers); - - if (unlikely (!shapers)) - { - char *env = getenv ("HB_SHAPER_LIST"); - if (!env || !*env) { - (void) hb_atomic_ptr_cmpexch (&static_shapers, NULL, &all_shapers[0]); - return (const hb_shaper_pair_t *) all_shapers; - } - - /* Not found; allocate one. */ - shapers = (hb_shaper_pair_t *) malloc (sizeof (all_shapers)); - if (unlikely (!shapers)) - return (const hb_shaper_pair_t *) all_shapers; - memcpy (shapers, all_shapers, sizeof (all_shapers)); - - /* Reorder shaper list to prefer requested shapers. */ - unsigned int i = 0; - char *end, *p = env; - for (;;) { - end = strchr (p, ','); - if (!end) - end = p + strlen (p); - - for (unsigned int j = i; j < ARRAY_LENGTH (all_shapers); j++) - if (end - p == (int) strlen (shapers[j].name) && - 0 == strncmp (shapers[j].name, p, end - p)) - { - /* Reorder this shaper to position i */ - struct hb_shaper_pair_t t = shapers[j]; - memmove (&shapers[i + 1], &shapers[i], sizeof (shapers[i]) * (j - i)); - shapers[i] = t; - i++; - } - - if (!*end) - break; - else - p = end + 1; - } - - if (!hb_atomic_ptr_cmpexch (&static_shapers, NULL, shapers)) { - free (shapers); - goto retry; - } - -#ifdef HAVE_ATEXIT - atexit (free_static_shapers); /* First person registers atexit() callback. */ -#endif - } - - return shapers; -} - static const char **static_shaper_list; @@ -127,15 +48,15 @@ retry: if (unlikely (!shaper_list)) { /* Not found; allocate one. */ - shaper_list = (const char **) calloc (1 + ARRAY_LENGTH (all_shapers), sizeof (const char *)); + shaper_list = (const char **) calloc (1 + HB_SHAPERS_COUNT, sizeof (const char *)); if (unlikely (!shaper_list)) { static const char *nil_shaper_list[] = {NULL}; return nil_shaper_list; } - const hb_shaper_pair_t *shapers = get_shapers (); + const hb_shaper_pair_t *shapers = _hb_shapers_get (); unsigned int i; - for (i = 0; i < ARRAY_LENGTH (all_shapers); i++) + for (i = 0; i < HB_SHAPERS_COUNT; i++) shaper_list[i] = shapers[i].name; shaper_list[i] = NULL; @@ -160,18 +81,19 @@ hb_shape_full (hb_font_t *font, unsigned int num_features, const char * const *shaper_list) { + const hb_shaper_pair_t *shapers = _hb_shapers_get (); + hb_font_make_immutable (font); /* So we can safely cache stuff on it */ if (likely (!shaper_list)) { - const hb_shaper_pair_t *shapers = get_shapers (); - for (unsigned int i = 0; i < ARRAY_LENGTH (all_shapers); i++) - if (likely (shapers[i].func (font, buffer, features, num_features))) + for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++) + if (likely (shapers[i].func (NULL, font, buffer, features, num_features))) return true; } else { while (*shaper_list) { - for (unsigned int i = 0; i < ARRAY_LENGTH (all_shapers); i++) - if (0 == strcmp (*shaper_list, all_shapers[i].name)) { - if (likely (all_shapers[i].func (font, buffer, features, num_features))) + for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++) + if (0 == strcmp (*shaper_list, shapers[i].name)) { + if (likely (shapers[i].func (NULL, font, buffer, features, num_features))) return true; break; } diff --git a/src/hb-shaper-private.hh b/src/hb-shaper-private.hh index 88be96cdb..fa279bc0e 100644 --- a/src/hb-shaper-private.hh +++ b/src/hb-shaper-private.hh @@ -29,7 +29,10 @@ #include "hb-private.hh" -typedef hb_bool_t hb_shape_func_t (hb_font_t *font, +#include "hb-shape-plan.h" /* TODO remove */ + +typedef hb_bool_t hb_shape_func_t (hb_shape_plan_t *shape_plan, + hb_font_t *font, hb_buffer_t *buffer, const hb_feature_t *features, unsigned int num_features); @@ -39,6 +42,15 @@ typedef hb_bool_t hb_shape_func_t (hb_font_t *font, #include "hb-shaper-list.hh" #undef HB_SHAPER_IMPLEMENT +struct hb_shaper_pair_t { + char name[16]; + hb_shape_func_t *func; +}; + +HB_INTERNAL const hb_shaper_pair_t * +_hb_shapers_get (void); + + /* For embedding in face / font / ... */ struct hb_shaper_data_t { #define HB_SHAPER_IMPLEMENT(shaper) void *shaper; @@ -46,7 +58,7 @@ struct hb_shaper_data_t { #undef HB_SHAPER_IMPLEMENT }; -#define HB_NUM_SHAPERS (sizeof (hb_shaper_data_t) / sizeof (void *)) +#define HB_SHAPERS_COUNT (sizeof (hb_shaper_data_t) / sizeof (void *)) /* Means: succeeded, but don't need to keep any data. */ #define HB_SHAPER_DATA_SUCCEEDED ((void *) +1) diff --git a/src/hb-shaper.cc b/src/hb-shaper.cc new file mode 100644 index 000000000..a16ffc849 --- /dev/null +++ b/src/hb-shaper.cc @@ -0,0 +1,109 @@ +/* + * Copyright © 2012 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Google Author(s): Behdad Esfahbod + */ + +#include "hb-private.hh" + +#include "hb-shaper-private.hh" + + +static const hb_shaper_pair_t all_shapers[] = { +#define HB_SHAPER_IMPLEMENT(name) {#name, _hb_##name##_shape}, +#include "hb-shaper-list.hh" +#undef HB_SHAPER_IMPLEMENT +}; + + +/* Thread-safe, lock-free, shapers */ + +static const hb_shaper_pair_t *static_shapers; + +static +void free_static_shapers (void) +{ + if (unlikely (static_shapers != all_shapers)) + free ((void *) static_shapers); +} + +const hb_shaper_pair_t * +_hb_shapers_get (void) +{ +retry: + hb_shaper_pair_t *shapers = (hb_shaper_pair_t *) hb_atomic_ptr_get (&static_shapers); + + if (unlikely (!shapers)) + { + char *env = getenv ("HB_SHAPER_LIST"); + if (!env || !*env) { + (void) hb_atomic_ptr_cmpexch (&static_shapers, NULL, &all_shapers[0]); + return (const hb_shaper_pair_t *) all_shapers; + } + + /* Not found; allocate one. */ + shapers = (hb_shaper_pair_t *) malloc (sizeof (all_shapers)); + if (unlikely (!shapers)) { + (void) hb_atomic_ptr_cmpexch (&static_shapers, NULL, &all_shapers[0]); + return (const hb_shaper_pair_t *) all_shapers; + } + + memcpy (shapers, all_shapers, sizeof (all_shapers)); + + /* Reorder shaper list to prefer requested shapers. */ + unsigned int i = 0; + char *end, *p = env; + for (;;) { + end = strchr (p, ','); + if (!end) + end = p + strlen (p); + + for (unsigned int j = i; j < ARRAY_LENGTH (all_shapers); j++) + if (end - p == (int) strlen (shapers[j].name) && + 0 == strncmp (shapers[j].name, p, end - p)) + { + /* Reorder this shaper to position i */ + struct hb_shaper_pair_t t = shapers[j]; + memmove (&shapers[i + 1], &shapers[i], sizeof (shapers[i]) * (j - i)); + shapers[i] = t; + i++; + } + + if (!*end) + break; + else + p = end + 1; + } + + if (!hb_atomic_ptr_cmpexch (&static_shapers, NULL, shapers)) { + free (shapers); + goto retry; + } + +#ifdef HAVE_ATEXIT + atexit (free_static_shapers); /* First person registers atexit() callback. */ +#endif + } + + return shapers; +} diff --git a/src/hb-uniscribe.cc b/src/hb-uniscribe.cc index fe7be7082..faba5870c 100644 --- a/src/hb-uniscribe.cc +++ b/src/hb-uniscribe.cc @@ -270,7 +270,8 @@ hb_uniscribe_font_get_hfont (hb_font_t *font) hb_bool_t -_hb_uniscribe_shape (hb_font_t *font, +_hb_uniscribe_shape (hb_shape_plan_t *shape_plan, + hb_font_t *font, hb_buffer_t *buffer, const hb_feature_t *features, unsigned int num_features)