Blame src/hb-shaper.cc

Packit 874993
/*
Packit 874993
 * Copyright © 2012  Google, Inc.
Packit 874993
 *
Packit 874993
 *  This is part of HarfBuzz, a text shaping library.
Packit 874993
 *
Packit 874993
 * Permission is hereby granted, without written agreement and without
Packit 874993
 * license or royalty fees, to use, copy, modify, and distribute this
Packit 874993
 * software and its documentation for any purpose, provided that the
Packit 874993
 * above copyright notice and the following two paragraphs appear in
Packit 874993
 * all copies of this software.
Packit 874993
 *
Packit 874993
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
Packit 874993
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
Packit 874993
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
Packit 874993
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
Packit 874993
 * DAMAGE.
Packit 874993
 *
Packit 874993
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
Packit 874993
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
Packit 874993
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
Packit 874993
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
Packit 874993
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
Packit 874993
 *
Packit 874993
 * Google Author(s): Behdad Esfahbod
Packit 874993
 */
Packit 874993
Packit 874993
#include "hb-private.hh"
Packit 874993
#include "hb-shaper-private.hh"
Packit 874993
#include "hb-atomic-private.hh"
Packit 874993
Packit 874993
Packit 874993
static const hb_shaper_pair_t all_shapers[] = {
Packit 874993
#define HB_SHAPER_IMPLEMENT(name) {#name, _hb_##name##_shape},
Packit 874993
#include "hb-shaper-list.hh"
Packit 874993
#undef HB_SHAPER_IMPLEMENT
Packit 874993
};
Packit 874993
Packit 874993
Packit 874993
/* Thread-safe, lock-free, shapers */
Packit 874993
Packit 874993
static const hb_shaper_pair_t *static_shapers;
Packit 874993
Packit 874993
#ifdef HB_USE_ATEXIT
Packit 874993
static
Packit 874993
void free_static_shapers (void)
Packit 874993
{
Packit 874993
  if (unlikely (static_shapers != all_shapers))
Packit 874993
    free ((void *) static_shapers);
Packit 874993
}
Packit 874993
#endif
Packit 874993
Packit 874993
const hb_shaper_pair_t *
Packit 874993
_hb_shapers_get (void)
Packit 874993
{
Packit 874993
retry:
Packit 874993
  hb_shaper_pair_t *shapers = (hb_shaper_pair_t *) hb_atomic_ptr_get (&static_shapers);
Packit 874993
Packit 874993
  if (unlikely (!shapers))
Packit 874993
  {
Packit 874993
    char *env = getenv ("HB_SHAPER_LIST");
Packit 874993
    if (!env || !*env) {
Packit 874993
      (void) hb_atomic_ptr_cmpexch (&static_shapers, NULL, &all_shapers[0]);
Packit 874993
      return (const hb_shaper_pair_t *) all_shapers;
Packit 874993
    }
Packit 874993
Packit 874993
    /* Not found; allocate one. */
Packit 874993
    shapers = (hb_shaper_pair_t *) calloc (1, sizeof (all_shapers));
Packit 874993
    if (unlikely (!shapers)) {
Packit 874993
      (void) hb_atomic_ptr_cmpexch (&static_shapers, NULL, &all_shapers[0]);
Packit 874993
      return (const hb_shaper_pair_t *) all_shapers;
Packit 874993
    }
Packit 874993
Packit 874993
    memcpy (shapers, all_shapers, sizeof (all_shapers));
Packit 874993
Packit 874993
     /* Reorder shaper list to prefer requested shapers. */
Packit 874993
    unsigned int i = 0;
Packit 874993
    char *end, *p = env;
Packit 874993
    for (;;) {
Packit 874993
      end = strchr (p, ',');
Packit 874993
      if (!end)
Packit 874993
	end = p + strlen (p);
Packit 874993
Packit 874993
      for (unsigned int j = i; j < ARRAY_LENGTH (all_shapers); j++)
Packit 874993
	if (end - p == (int) strlen (shapers[j].name) &&
Packit 874993
	    0 == strncmp (shapers[j].name, p, end - p))
Packit 874993
	{
Packit 874993
	  /* Reorder this shaper to position i */
Packit 874993
	 struct hb_shaper_pair_t t = shapers[j];
Packit 874993
	 memmove (&shapers[i + 1], &shapers[i], sizeof (shapers[i]) * (j - i));
Packit 874993
	 shapers[i] = t;
Packit 874993
	 i++;
Packit 874993
	}
Packit 874993
Packit 874993
      if (!*end)
Packit 874993
	break;
Packit 874993
      else
Packit 874993
	p = end + 1;
Packit 874993
    }
Packit 874993
Packit 874993
    if (!hb_atomic_ptr_cmpexch (&static_shapers, NULL, shapers)) {
Packit 874993
      free (shapers);
Packit 874993
      goto retry;
Packit 874993
    }
Packit 874993
Packit 874993
#ifdef HB_USE_ATEXIT
Packit 874993
    atexit (free_static_shapers); /* First person registers atexit() callback. */
Packit 874993
#endif
Packit 874993
  }
Packit 874993
Packit 874993
  return shapers;
Packit 874993
}