2 * Copyright © 2009 Red Hat, Inc.
3 * Copyright © 2012 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
29 #include "hb-private.hh"
31 #include "hb-shaper-private.hh"
32 #include "hb-shape-plan-private.hh"
33 #include "hb-buffer-private.hh"
34 #include "hb-font-private.hh"
38 parse_space (const char **pp, const char *end)
40 while (*pp < end && ISSPACE (**pp))
46 parse_char (const char **pp, const char *end, char c)
48 parse_space (pp, end);
50 if (*pp == end || **pp != c)
58 parse_uint (const char **pp, const char *end, unsigned int *pv)
61 unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - *pp));
62 strncpy (buf, *pp, len);
69 /* Intentionally use strtol instead of strtoul, such that
70 * -1 turns into "big number"... */
72 v = strtol (p, &pend, 0);
73 if (errno || p == pend)
82 parse_bool (const char **pp, const char *end, unsigned int *pv)
84 parse_space (pp, end);
87 while (*pp < end && ISALPHA(**pp))
90 /* CSS allows on/off as aliases 1/0. */
91 if (*pp - p == 2 || 0 == strncmp (p, "on", 2))
93 else if (*pp - p == 3 || 0 == strncmp (p, "off", 2))
102 parse_feature_value_prefix (const char **pp, const char *end, hb_feature_t *feature)
104 if (parse_char (pp, end, '-'))
107 parse_char (pp, end, '+');
115 parse_feature_tag (const char **pp, const char *end, hb_feature_t *feature)
117 parse_space (pp, end);
121 if (*pp < end && (**pp == '\'' || **pp == '"'))
128 while (*pp < end && ISALNUM(**pp))
131 if (p == *pp || *pp - p > 4)
134 feature->tag = hb_tag_from_string (p, *pp - p);
138 /* CSS expects exactly four bytes. And we only allow quotations for
139 * CSS compatibility. So, enforce the length. */
142 if (*pp == end || **pp != quote)
151 parse_feature_indices (const char **pp, const char *end, hb_feature_t *feature)
153 parse_space (pp, end);
158 feature->end = (unsigned int) -1;
160 if (!parse_char (pp, end, '['))
163 has_start = parse_uint (pp, end, &feature->start);
165 if (parse_char (pp, end, ':')) {
166 parse_uint (pp, end, &feature->end);
169 feature->end = feature->start + 1;
172 return parse_char (pp, end, ']');
176 parse_feature_value_postfix (const char **pp, const char *end, hb_feature_t *feature)
178 bool had_equal = parse_char (pp, end, '=');
179 bool had_value = parse_uint (pp, end, &feature->value) ||
180 parse_bool (pp, end, &feature->value);
181 /* CSS doesn't use equal-sign between tag and value.
182 * If there was an equal-sign, then there *must* be a value.
183 * A value without an eqaul-sign is ok, but not required. */
184 return !had_equal || had_value;
189 parse_one_feature (const char **pp, const char *end, hb_feature_t *feature)
191 return parse_feature_value_prefix (pp, end, feature) &&
192 parse_feature_tag (pp, end, feature) &&
193 parse_feature_indices (pp, end, feature) &&
194 parse_feature_value_postfix (pp, end, feature) &&
195 parse_space (pp, end) &&
200 * hb_feature_from_string:
201 * @str: (array length=len):
203 * @feature: (out) (optional):
212 hb_feature_from_string (const char *str, int len,
213 hb_feature_t *feature)
220 if (likely (parse_one_feature (&str, str + len, &feat)))
228 memset (feature, 0, sizeof (*feature));
233 * hb_feature_to_string:
235 * @buf: (array length=size):
243 hb_feature_to_string (hb_feature_t *feature,
244 char *buf, unsigned int size)
246 if (unlikely (!size)) return;
249 unsigned int len = 0;
250 if (feature->value == 0)
252 hb_tag_to_string (feature->tag, s + len);
254 while (len && s[len - 1] == ' ')
256 if (feature->start != 0 || feature->end != (unsigned int) -1)
260 len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->start));
261 if (feature->end != feature->start + 1) {
263 if (feature->end != (unsigned int) -1)
264 len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->end));
268 if (feature->value > 1)
271 len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->value));
273 assert (len < ARRAY_LENGTH (s));
274 len = MIN (len, size - 1);
275 memcpy (buf, s, len);
280 static const char **static_shaper_list;
284 void free_static_shaper_list (void)
286 free (static_shaper_list);
291 * hb_shape_list_shapers:
295 * Return value: (transfer none):
300 hb_shape_list_shapers (void)
303 const char **shaper_list = (const char **) hb_atomic_ptr_get (&static_shaper_list);
305 if (unlikely (!shaper_list))
307 /* Not found; allocate one. */
308 shaper_list = (const char **) calloc (1 + HB_SHAPERS_COUNT, sizeof (const char *));
309 if (unlikely (!shaper_list)) {
310 static const char *nil_shaper_list[] = {NULL};
311 return nil_shaper_list;
314 const hb_shaper_pair_t *shapers = _hb_shapers_get ();
316 for (i = 0; i < HB_SHAPERS_COUNT; i++)
317 shaper_list[i] = shapers[i].name;
318 shaper_list[i] = NULL;
320 if (!hb_atomic_ptr_cmpexch (&static_shaper_list, NULL, shaper_list)) {
326 atexit (free_static_shaper_list); /* First person registers atexit() callback. */
338 * @features: (array length=num_features):
340 * @shaper_list: (array zero-terminated=1):
349 hb_shape_full (hb_font_t *font,
351 const hb_feature_t *features,
352 unsigned int num_features,
353 const char * const *shaper_list)
355 if (unlikely (!buffer->len))
358 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE);
360 hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props, features, num_features, shaper_list);
361 hb_bool_t res = hb_shape_plan_execute (shape_plan, font, buffer, features, num_features);
362 hb_shape_plan_destroy (shape_plan);
365 buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
373 * @features: (array length=num_features):
381 hb_shape (hb_font_t *font,
383 const hb_feature_t *features,
384 unsigned int num_features)
386 hb_shape_full (font, buffer, features, num_features, NULL);