michael@0: /* michael@0: * Copyright © 2009 Red Hat, Inc. michael@0: * Copyright © 2012 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Red Hat Author(s): Behdad Esfahbod michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #include "hb-private.hh" michael@0: michael@0: #include "hb-shaper-private.hh" michael@0: #include "hb-shape-plan-private.hh" michael@0: #include "hb-buffer-private.hh" michael@0: #include "hb-font-private.hh" michael@0: michael@0: michael@0: static void michael@0: parse_space (const char **pp, const char *end) michael@0: { michael@0: char c; michael@0: while (*pp < end && (c = **pp, ISSPACE (c))) michael@0: (*pp)++; michael@0: } michael@0: michael@0: static hb_bool_t michael@0: parse_char (const char **pp, const char *end, char c) michael@0: { michael@0: parse_space (pp, end); michael@0: michael@0: if (*pp == end || **pp != c) michael@0: return false; michael@0: michael@0: (*pp)++; michael@0: return true; michael@0: } michael@0: michael@0: static hb_bool_t michael@0: parse_uint (const char **pp, const char *end, unsigned int *pv) michael@0: { michael@0: char buf[32]; michael@0: unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - *pp)); michael@0: strncpy (buf, *pp, len); michael@0: buf[len] = '\0'; michael@0: michael@0: char *p = buf; michael@0: char *pend = p; michael@0: unsigned int v; michael@0: michael@0: /* Intentionally use strtol instead of strtoul, such that michael@0: * -1 turns into "big number"... */ michael@0: errno = 0; michael@0: v = strtol (p, &pend, 0); michael@0: if (errno || p == pend) michael@0: return false; michael@0: michael@0: *pv = v; michael@0: *pp += pend - p; michael@0: return true; michael@0: } michael@0: michael@0: static hb_bool_t michael@0: parse_feature_value_prefix (const char **pp, const char *end, hb_feature_t *feature) michael@0: { michael@0: if (parse_char (pp, end, '-')) michael@0: feature->value = 0; michael@0: else { michael@0: parse_char (pp, end, '+'); michael@0: feature->value = 1; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static hb_bool_t michael@0: parse_feature_tag (const char **pp, const char *end, hb_feature_t *feature) michael@0: { michael@0: const char *p = *pp; michael@0: char c; michael@0: michael@0: parse_space (pp, end); michael@0: michael@0: #define ISALNUM(c) (('a' <= (c) && (c) <= 'z') || ('A' <= (c) && (c) <= 'Z') || ('0' <= (c) && (c) <= '9')) michael@0: while (*pp < end && (c = **pp, ISALNUM(c))) michael@0: (*pp)++; michael@0: #undef ISALNUM michael@0: michael@0: if (p == *pp) michael@0: return false; michael@0: michael@0: feature->tag = hb_tag_from_string (p, *pp - p); michael@0: return true; michael@0: } michael@0: michael@0: static hb_bool_t michael@0: parse_feature_indices (const char **pp, const char *end, hb_feature_t *feature) michael@0: { michael@0: parse_space (pp, end); michael@0: michael@0: hb_bool_t has_start; michael@0: michael@0: feature->start = 0; michael@0: feature->end = (unsigned int) -1; michael@0: michael@0: if (!parse_char (pp, end, '[')) michael@0: return true; michael@0: michael@0: has_start = parse_uint (pp, end, &feature->start); michael@0: michael@0: if (parse_char (pp, end, ':')) { michael@0: parse_uint (pp, end, &feature->end); michael@0: } else { michael@0: if (has_start) michael@0: feature->end = feature->start + 1; michael@0: } michael@0: michael@0: return parse_char (pp, end, ']'); michael@0: } michael@0: michael@0: static hb_bool_t michael@0: parse_feature_value_postfix (const char **pp, const char *end, hb_feature_t *feature) michael@0: { michael@0: return !parse_char (pp, end, '=') || parse_uint (pp, end, &feature->value); michael@0: } michael@0: michael@0: michael@0: static hb_bool_t michael@0: parse_one_feature (const char **pp, const char *end, hb_feature_t *feature) michael@0: { michael@0: return parse_feature_value_prefix (pp, end, feature) && michael@0: parse_feature_tag (pp, end, feature) && michael@0: parse_feature_indices (pp, end, feature) && michael@0: parse_feature_value_postfix (pp, end, feature) && michael@0: *pp == end; michael@0: } michael@0: michael@0: /** michael@0: * hb_feature_from_string: michael@0: * @str: (array length=len): michael@0: * @len: michael@0: * @feature: (out): michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_bool_t michael@0: hb_feature_from_string (const char *str, int len, michael@0: hb_feature_t *feature) michael@0: { michael@0: if (len < 0) michael@0: len = strlen (str); michael@0: michael@0: return parse_one_feature (&str, str + len, feature); michael@0: } michael@0: michael@0: /** michael@0: * hb_feature_to_string: michael@0: * @feature: michael@0: * @buf: (array length=size): michael@0: * @size: michael@0: * michael@0: * michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: void michael@0: hb_feature_to_string (hb_feature_t *feature, michael@0: char *buf, unsigned int size) michael@0: { michael@0: if (unlikely (!size)) return; michael@0: michael@0: char s[128]; michael@0: unsigned int len = 0; michael@0: if (feature->value == 0) michael@0: s[len++] = '-'; michael@0: hb_tag_to_string (feature->tag, s + len); michael@0: len += 4; michael@0: while (len && s[len - 1] == ' ') michael@0: len--; michael@0: if (feature->start != 0 || feature->end != (unsigned int) -1) michael@0: { michael@0: s[len++] = '['; michael@0: if (feature->start) michael@0: len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%d", feature->start)); michael@0: if (feature->end != feature->start + 1) { michael@0: s[len++] = ':'; michael@0: if (feature->end != (unsigned int) -1) michael@0: len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%d", feature->end)); michael@0: } michael@0: s[len++] = ']'; michael@0: } michael@0: if (feature->value > 1) michael@0: { michael@0: s[len++] = '='; michael@0: len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%d", feature->value)); michael@0: } michael@0: assert (len < ARRAY_LENGTH (s)); michael@0: len = MIN (len, size - 1); michael@0: memcpy (buf, s, len); michael@0: buf[len] = '\0'; michael@0: } michael@0: michael@0: michael@0: static const char **static_shaper_list; michael@0: michael@0: static inline michael@0: void free_static_shaper_list (void) michael@0: { michael@0: free (static_shaper_list); michael@0: } michael@0: michael@0: /** michael@0: * hb_shape_list_shapers: michael@0: * michael@0: * michael@0: * michael@0: * Return value: (transfer none): michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: const char ** michael@0: hb_shape_list_shapers (void) michael@0: { michael@0: retry: michael@0: const char **shaper_list = (const char **) hb_atomic_ptr_get (&static_shaper_list); michael@0: michael@0: if (unlikely (!shaper_list)) michael@0: { michael@0: /* Not found; allocate one. */ michael@0: shaper_list = (const char **) calloc (1 + HB_SHAPERS_COUNT, sizeof (const char *)); michael@0: if (unlikely (!shaper_list)) { michael@0: static const char *nil_shaper_list[] = {NULL}; michael@0: return nil_shaper_list; michael@0: } michael@0: michael@0: const hb_shaper_pair_t *shapers = _hb_shapers_get (); michael@0: unsigned int i; michael@0: for (i = 0; i < HB_SHAPERS_COUNT; i++) michael@0: shaper_list[i] = shapers[i].name; michael@0: shaper_list[i] = NULL; michael@0: michael@0: if (!hb_atomic_ptr_cmpexch (&static_shaper_list, NULL, shaper_list)) { michael@0: free (shaper_list); michael@0: goto retry; michael@0: } michael@0: michael@0: #ifdef HAVE_ATEXIT michael@0: atexit (free_static_shaper_list); /* First person registers atexit() callback. */ michael@0: #endif michael@0: } michael@0: michael@0: return shaper_list; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * hb_shape_full: michael@0: * @font: a font. michael@0: * @buffer: a buffer. michael@0: * @features: (array length=num_features): michael@0: * @num_features: michael@0: * @shaper_list: (array zero-terminated=1): michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_bool_t michael@0: hb_shape_full (hb_font_t *font, michael@0: hb_buffer_t *buffer, michael@0: const hb_feature_t *features, michael@0: unsigned int num_features, michael@0: const char * const *shaper_list) michael@0: { michael@0: if (unlikely (!buffer->len)) michael@0: return true; michael@0: michael@0: assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE); michael@0: michael@0: hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props, features, num_features, shaper_list); michael@0: hb_bool_t res = hb_shape_plan_execute (shape_plan, font, buffer, features, num_features); michael@0: hb_shape_plan_destroy (shape_plan); michael@0: michael@0: if (res) michael@0: buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS; michael@0: return res; michael@0: } michael@0: michael@0: /** michael@0: * hb_shape: michael@0: * @font: a font. michael@0: * @buffer: a buffer. michael@0: * @features: (array length=num_features): michael@0: * @num_features: michael@0: * michael@0: * michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: void michael@0: hb_shape (hb_font_t *font, michael@0: hb_buffer_t *buffer, michael@0: const hb_feature_t *features, michael@0: unsigned int num_features) michael@0: { michael@0: hb_shape_full (font, buffer, features, num_features, NULL); michael@0: }