gfx/harfbuzz/src/hb-shaper.cc

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /*
michael@0 2 * Copyright © 2012 Google, Inc.
michael@0 3 *
michael@0 4 * This is part of HarfBuzz, a text shaping library.
michael@0 5 *
michael@0 6 * Permission is hereby granted, without written agreement and without
michael@0 7 * license or royalty fees, to use, copy, modify, and distribute this
michael@0 8 * software and its documentation for any purpose, provided that the
michael@0 9 * above copyright notice and the following two paragraphs appear in
michael@0 10 * all copies of this software.
michael@0 11 *
michael@0 12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
michael@0 13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
michael@0 14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
michael@0 15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
michael@0 16 * DAMAGE.
michael@0 17 *
michael@0 18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
michael@0 19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
michael@0 20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
michael@0 21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
michael@0 22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
michael@0 23 *
michael@0 24 * Google Author(s): Behdad Esfahbod
michael@0 25 */
michael@0 26
michael@0 27 #include "hb-private.hh"
michael@0 28 #include "hb-shaper-private.hh"
michael@0 29 #include "hb-atomic-private.hh"
michael@0 30
michael@0 31
michael@0 32 static const hb_shaper_pair_t all_shapers[] = {
michael@0 33 #define HB_SHAPER_IMPLEMENT(name) {#name, _hb_##name##_shape},
michael@0 34 #include "hb-shaper-list.hh"
michael@0 35 #undef HB_SHAPER_IMPLEMENT
michael@0 36 };
michael@0 37
michael@0 38
michael@0 39 /* Thread-safe, lock-free, shapers */
michael@0 40
michael@0 41 static const hb_shaper_pair_t *static_shapers;
michael@0 42
michael@0 43 static inline
michael@0 44 void free_static_shapers (void)
michael@0 45 {
michael@0 46 if (unlikely (static_shapers != all_shapers))
michael@0 47 free ((void *) static_shapers);
michael@0 48 }
michael@0 49
michael@0 50 const hb_shaper_pair_t *
michael@0 51 _hb_shapers_get (void)
michael@0 52 {
michael@0 53 retry:
michael@0 54 hb_shaper_pair_t *shapers = (hb_shaper_pair_t *) hb_atomic_ptr_get (&static_shapers);
michael@0 55
michael@0 56 if (unlikely (!shapers))
michael@0 57 {
michael@0 58 char *env = getenv ("HB_SHAPER_LIST");
michael@0 59 if (!env || !*env) {
michael@0 60 (void) hb_atomic_ptr_cmpexch (&static_shapers, NULL, &all_shapers[0]);
michael@0 61 return (const hb_shaper_pair_t *) all_shapers;
michael@0 62 }
michael@0 63
michael@0 64 /* Not found; allocate one. */
michael@0 65 shapers = (hb_shaper_pair_t *) malloc (sizeof (all_shapers));
michael@0 66 if (unlikely (!shapers)) {
michael@0 67 (void) hb_atomic_ptr_cmpexch (&static_shapers, NULL, &all_shapers[0]);
michael@0 68 return (const hb_shaper_pair_t *) all_shapers;
michael@0 69 }
michael@0 70
michael@0 71 memcpy (shapers, all_shapers, sizeof (all_shapers));
michael@0 72
michael@0 73 /* Reorder shaper list to prefer requested shapers. */
michael@0 74 unsigned int i = 0;
michael@0 75 char *end, *p = env;
michael@0 76 for (;;) {
michael@0 77 end = strchr (p, ',');
michael@0 78 if (!end)
michael@0 79 end = p + strlen (p);
michael@0 80
michael@0 81 for (unsigned int j = i; j < ARRAY_LENGTH (all_shapers); j++)
michael@0 82 if (end - p == (int) strlen (shapers[j].name) &&
michael@0 83 0 == strncmp (shapers[j].name, p, end - p))
michael@0 84 {
michael@0 85 /* Reorder this shaper to position i */
michael@0 86 struct hb_shaper_pair_t t = shapers[j];
michael@0 87 memmove (&shapers[i + 1], &shapers[i], sizeof (shapers[i]) * (j - i));
michael@0 88 shapers[i] = t;
michael@0 89 i++;
michael@0 90 }
michael@0 91
michael@0 92 if (!*end)
michael@0 93 break;
michael@0 94 else
michael@0 95 p = end + 1;
michael@0 96 }
michael@0 97
michael@0 98 if (!hb_atomic_ptr_cmpexch (&static_shapers, NULL, shapers)) {
michael@0 99 free (shapers);
michael@0 100 goto retry;
michael@0 101 }
michael@0 102
michael@0 103 #ifdef HAVE_ATEXIT
michael@0 104 atexit (free_static_shapers); /* First person registers atexit() callback. */
michael@0 105 #endif
michael@0 106 }
michael@0 107
michael@0 108 return shapers;
michael@0 109 }

mercurial