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