1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/harfbuzz/src/hb-shaper.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,109 @@ 1.4 +/* 1.5 + * Copyright © 2012 Google, Inc. 1.6 + * 1.7 + * This is part of HarfBuzz, a text shaping library. 1.8 + * 1.9 + * Permission is hereby granted, without written agreement and without 1.10 + * license or royalty fees, to use, copy, modify, and distribute this 1.11 + * software and its documentation for any purpose, provided that the 1.12 + * above copyright notice and the following two paragraphs appear in 1.13 + * all copies of this software. 1.14 + * 1.15 + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 1.16 + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 1.17 + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 1.18 + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 1.19 + * DAMAGE. 1.20 + * 1.21 + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 1.22 + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 1.23 + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 1.24 + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 1.25 + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 1.26 + * 1.27 + * Google Author(s): Behdad Esfahbod 1.28 + */ 1.29 + 1.30 +#include "hb-private.hh" 1.31 +#include "hb-shaper-private.hh" 1.32 +#include "hb-atomic-private.hh" 1.33 + 1.34 + 1.35 +static const hb_shaper_pair_t all_shapers[] = { 1.36 +#define HB_SHAPER_IMPLEMENT(name) {#name, _hb_##name##_shape}, 1.37 +#include "hb-shaper-list.hh" 1.38 +#undef HB_SHAPER_IMPLEMENT 1.39 +}; 1.40 + 1.41 + 1.42 +/* Thread-safe, lock-free, shapers */ 1.43 + 1.44 +static const hb_shaper_pair_t *static_shapers; 1.45 + 1.46 +static inline 1.47 +void free_static_shapers (void) 1.48 +{ 1.49 + if (unlikely (static_shapers != all_shapers)) 1.50 + free ((void *) static_shapers); 1.51 +} 1.52 + 1.53 +const hb_shaper_pair_t * 1.54 +_hb_shapers_get (void) 1.55 +{ 1.56 +retry: 1.57 + hb_shaper_pair_t *shapers = (hb_shaper_pair_t *) hb_atomic_ptr_get (&static_shapers); 1.58 + 1.59 + if (unlikely (!shapers)) 1.60 + { 1.61 + char *env = getenv ("HB_SHAPER_LIST"); 1.62 + if (!env || !*env) { 1.63 + (void) hb_atomic_ptr_cmpexch (&static_shapers, NULL, &all_shapers[0]); 1.64 + return (const hb_shaper_pair_t *) all_shapers; 1.65 + } 1.66 + 1.67 + /* Not found; allocate one. */ 1.68 + shapers = (hb_shaper_pair_t *) malloc (sizeof (all_shapers)); 1.69 + if (unlikely (!shapers)) { 1.70 + (void) hb_atomic_ptr_cmpexch (&static_shapers, NULL, &all_shapers[0]); 1.71 + return (const hb_shaper_pair_t *) all_shapers; 1.72 + } 1.73 + 1.74 + memcpy (shapers, all_shapers, sizeof (all_shapers)); 1.75 + 1.76 + /* Reorder shaper list to prefer requested shapers. */ 1.77 + unsigned int i = 0; 1.78 + char *end, *p = env; 1.79 + for (;;) { 1.80 + end = strchr (p, ','); 1.81 + if (!end) 1.82 + end = p + strlen (p); 1.83 + 1.84 + for (unsigned int j = i; j < ARRAY_LENGTH (all_shapers); j++) 1.85 + if (end - p == (int) strlen (shapers[j].name) && 1.86 + 0 == strncmp (shapers[j].name, p, end - p)) 1.87 + { 1.88 + /* Reorder this shaper to position i */ 1.89 + struct hb_shaper_pair_t t = shapers[j]; 1.90 + memmove (&shapers[i + 1], &shapers[i], sizeof (shapers[i]) * (j - i)); 1.91 + shapers[i] = t; 1.92 + i++; 1.93 + } 1.94 + 1.95 + if (!*end) 1.96 + break; 1.97 + else 1.98 + p = end + 1; 1.99 + } 1.100 + 1.101 + if (!hb_atomic_ptr_cmpexch (&static_shapers, NULL, shapers)) { 1.102 + free (shapers); 1.103 + goto retry; 1.104 + } 1.105 + 1.106 +#ifdef HAVE_ATEXIT 1.107 + atexit (free_static_shapers); /* First person registers atexit() callback. */ 1.108 +#endif 1.109 + } 1.110 + 1.111 + return shapers; 1.112 +}