< prev index next >

src/java.desktop/share/native/libfontmanager/harfbuzz/hb-shaper.cc

Print this page




   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 #ifdef HB_USE_ATEXIT
  44 static
  45 void free_static_shapers (void)
  46 {
  47 retry:
  48   hb_shaper_pair_t *shapers = (hb_shaper_pair_t *) hb_atomic_ptr_get (&static_shapers);
  49   if (!hb_atomic_ptr_cmpexch (&static_shapers, shapers, nullptr))
  50     goto retry;
  51 
  52   if (unlikely (shapers != all_shapers))
  53     free ((void *) shapers);
  54 }
  55 #endif
  56 
  57 const hb_shaper_pair_t *
  58 _hb_shapers_get (void)
  59 {
  60 retry:
  61   hb_shaper_pair_t *shapers = (hb_shaper_pair_t *) hb_atomic_ptr_get (&static_shapers);
  62 
  63   if (unlikely (!shapers))
  64   {
  65     char *env = getenv ("HB_SHAPER_LIST");
  66     if (!env || !*env) {
  67       (void) hb_atomic_ptr_cmpexch (&static_shapers, nullptr, &all_shapers[0]);
  68       return (const hb_shaper_pair_t *) all_shapers;
  69     }
  70 
  71     /* Not found; allocate one. */
  72     shapers = (hb_shaper_pair_t *) calloc (1, sizeof (all_shapers));
  73     if (unlikely (!shapers)) {
  74       (void) hb_atomic_ptr_cmpexch (&static_shapers, nullptr, &all_shapers[0]);
  75       return (const hb_shaper_pair_t *) all_shapers;
  76     }
  77 
  78     memcpy (shapers, all_shapers, sizeof (all_shapers));
  79 
  80      /* Reorder shaper list to prefer requested shapers. */
  81     unsigned int i = 0;
  82     char *end, *p = env;
  83     for (;;) {

  84       end = strchr (p, ',');
  85       if (!end)
  86         end = p + strlen (p);
  87 
  88       for (unsigned int j = i; j < ARRAY_LENGTH (all_shapers); j++)
  89         if (end - p == (int) strlen (shapers[j].name) &&
  90             0 == strncmp (shapers[j].name, p, end - p))
  91         {
  92           /* Reorder this shaper to position i */
  93          struct hb_shaper_pair_t t = shapers[j];
  94          memmove (&shapers[i + 1], &shapers[i], sizeof (shapers[i]) * (j - i));
  95          shapers[i] = t;
  96          i++;
  97         }
  98 
  99       if (!*end)
 100         break;
 101       else
 102         p = end + 1;
 103     }
 104 
 105     if (!hb_atomic_ptr_cmpexch (&static_shapers, nullptr, shapers)) {
 106       free (shapers);
 107       goto retry;


 108     }



 109 
 110 #ifdef HB_USE_ATEXIT
 111     atexit (free_static_shapers); /* First person registers atexit() callback. */




 112 #endif
 113   }
 114 
 115   return shapers;



 116 }


   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.hh"
  28 #include "hb-shaper.hh"
  29 #include "hb-machinery.hh"
  30 
  31 
  32 static const hb_shaper_entry_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 #if HB_USE_ATEXIT
  39 static void free_static_shapers ();















  40 #endif
  41 
  42 static struct hb_shapers_lazy_loader_t : hb_lazy_loader_t<const hb_shaper_entry_t,
  43                                                           hb_shapers_lazy_loader_t>
  44 {
  45   static hb_shaper_entry_t *create ()



  46   {
  47     char *env = getenv ("HB_SHAPER_LIST");
  48     if (!env || !*env)
  49       return nullptr;


  50 
  51     hb_shaper_entry_t *shapers = (hb_shaper_entry_t *) calloc (1, sizeof (all_shapers));
  52     if (unlikely (!shapers))
  53       return nullptr;



  54 
  55     memcpy (shapers, all_shapers, sizeof (all_shapers));
  56 
  57      /* Reorder shaper list to prefer requested shapers. */
  58     unsigned int i = 0;
  59     char *end, *p = env;
  60     for (;;)
  61     {
  62       end = strchr (p, ',');
  63       if (!end)
  64         end = p + strlen (p);
  65 
  66       for (unsigned int j = i; j < ARRAY_LENGTH (all_shapers); j++)
  67         if (end - p == (int) strlen (shapers[j].name) &&
  68             0 == strncmp (shapers[j].name, p, end - p))
  69         {
  70           /* Reorder this shaper to position i */
  71          struct hb_shaper_entry_t t = shapers[j];
  72          memmove (&shapers[i + 1], &shapers[i], sizeof (shapers[i]) * (j - i));
  73          shapers[i] = t;
  74          i++;
  75         }
  76 
  77       if (!*end)
  78         break;
  79       else
  80         p = end + 1;
  81     }
  82 
  83 #if HB_USE_ATEXIT
  84     atexit (free_static_shapers);
  85 #endif
  86 
  87     return shapers;
  88   }
  89   static void destroy (const hb_shaper_entry_t *p) { free ((void *) p); }
  90   static const hb_shaper_entry_t *get_null ()      { return all_shapers; }
  91 } static_shapers;
  92 
  93 #if HB_USE_ATEXIT
  94 static
  95 void free_static_shapers ()
  96 {
  97   static_shapers.free_instance ();
  98 }
  99 #endif

 100 
 101 const hb_shaper_entry_t *
 102 _hb_shapers_get ()
 103 {
 104   return static_shapers.get_unconst ();
 105 }
< prev index next >