Fixes for Android GN build input/outputs
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-shape.cc
blob9a59c0855689564753a91206729f5e163016453e
1 /*
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
17 * DAMAGE.
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"
37 static bool
38 parse_space (const char **pp, const char *end)
40 while (*pp < end && ISSPACE (**pp))
41 (*pp)++;
42 return true;
45 static bool
46 parse_char (const char **pp, const char *end, char c)
48 parse_space (pp, end);
50 if (*pp == end || **pp != c)
51 return false;
53 (*pp)++;
54 return true;
57 static bool
58 parse_uint (const char **pp, const char *end, unsigned int *pv)
60 char buf[32];
61 unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - *pp));
62 strncpy (buf, *pp, len);
63 buf[len] = '\0';
65 char *p = buf;
66 char *pend = p;
67 unsigned int v;
69 /* Intentionally use strtol instead of strtoul, such that
70 * -1 turns into "big number"... */
71 errno = 0;
72 v = strtol (p, &pend, 0);
73 if (errno || p == pend)
74 return false;
76 *pv = v;
77 *pp += pend - p;
78 return true;
81 static bool
82 parse_bool (const char **pp, const char *end, unsigned int *pv)
84 parse_space (pp, end);
86 const char *p = *pp;
87 while (*pp < end && ISALPHA(**pp))
88 (*pp)++;
90 /* CSS allows on/off as aliases 1/0. */
91 if (*pp - p == 2 || 0 == strncmp (p, "on", 2))
92 *pv = 1;
93 else if (*pp - p == 3 || 0 == strncmp (p, "off", 2))
94 *pv = 0;
95 else
96 return false;
98 return true;
101 static bool
102 parse_feature_value_prefix (const char **pp, const char *end, hb_feature_t *feature)
104 if (parse_char (pp, end, '-'))
105 feature->value = 0;
106 else {
107 parse_char (pp, end, '+');
108 feature->value = 1;
111 return true;
114 static bool
115 parse_feature_tag (const char **pp, const char *end, hb_feature_t *feature)
117 parse_space (pp, end);
119 char quote = 0;
121 if (*pp < end && (**pp == '\'' || **pp == '"'))
123 quote = **pp;
124 (*pp)++;
127 const char *p = *pp;
128 while (*pp < end && ISALNUM(**pp))
129 (*pp)++;
131 if (p == *pp || *pp - p > 4)
132 return false;
134 feature->tag = hb_tag_from_string (p, *pp - p);
136 if (quote)
138 /* CSS expects exactly four bytes. And we only allow quotations for
139 * CSS compatibility. So, enforce the length. */
140 if (*pp - p != 4)
141 return false;
142 if (*pp == end || **pp != quote)
143 return false;
144 (*pp)++;
147 return true;
150 static bool
151 parse_feature_indices (const char **pp, const char *end, hb_feature_t *feature)
153 parse_space (pp, end);
155 bool has_start;
157 feature->start = 0;
158 feature->end = (unsigned int) -1;
160 if (!parse_char (pp, end, '['))
161 return true;
163 has_start = parse_uint (pp, end, &feature->start);
165 if (parse_char (pp, end, ':')) {
166 parse_uint (pp, end, &feature->end);
167 } else {
168 if (has_start)
169 feature->end = feature->start + 1;
172 return parse_char (pp, end, ']');
175 static bool
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;
188 static bool
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) &&
196 *pp == end;
200 * hb_feature_from_string:
201 * @str: (array length=len):
202 * @len:
203 * @feature: (out) (optional):
207 * Return value:
209 * Since: 1.0
211 hb_bool_t
212 hb_feature_from_string (const char *str, int len,
213 hb_feature_t *feature)
215 hb_feature_t feat;
217 if (len < 0)
218 len = strlen (str);
220 if (likely (parse_one_feature (&str, str + len, &feat)))
222 if (feature)
223 *feature = feat;
224 return true;
227 if (feature)
228 memset (feature, 0, sizeof (*feature));
229 return false;
233 * hb_feature_to_string:
234 * @feature:
235 * @buf: (array length=size):
236 * @size:
240 * Since: 1.0
242 void
243 hb_feature_to_string (hb_feature_t *feature,
244 char *buf, unsigned int size)
246 if (unlikely (!size)) return;
248 char s[128];
249 unsigned int len = 0;
250 if (feature->value == 0)
251 s[len++] = '-';
252 hb_tag_to_string (feature->tag, s + len);
253 len += 4;
254 while (len && s[len - 1] == ' ')
255 len--;
256 if (feature->start != 0 || feature->end != (unsigned int) -1)
258 s[len++] = '[';
259 if (feature->start)
260 len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->start));
261 if (feature->end != feature->start + 1) {
262 s[len++] = ':';
263 if (feature->end != (unsigned int) -1)
264 len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->end));
266 s[len++] = ']';
268 if (feature->value > 1)
270 s[len++] = '=';
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);
276 buf[len] = '\0';
280 static const char **static_shaper_list;
282 #ifdef HB_USE_ATEXIT
283 static
284 void free_static_shaper_list (void)
286 free (static_shaper_list);
288 #endif
291 * hb_shape_list_shapers:
295 * Return value: (transfer none):
297 * Since: 1.0
299 const char **
300 hb_shape_list_shapers (void)
302 retry:
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 ();
315 unsigned int i;
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)) {
321 free (shaper_list);
322 goto retry;
325 #ifdef HB_USE_ATEXIT
326 atexit (free_static_shaper_list); /* First person registers atexit() callback. */
327 #endif
330 return shaper_list;
335 * hb_shape_full:
336 * @font: a font.
337 * @buffer: a buffer.
338 * @features: (array length=num_features):
339 * @num_features:
340 * @shaper_list: (array zero-terminated=1):
344 * Return value:
346 * Since: 1.0
348 hb_bool_t
349 hb_shape_full (hb_font_t *font,
350 hb_buffer_t *buffer,
351 const hb_feature_t *features,
352 unsigned int num_features,
353 const char * const *shaper_list)
355 if (unlikely (!buffer->len))
356 return true;
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);
364 if (res)
365 buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
366 return res;
370 * hb_shape:
371 * @font: a font.
372 * @buffer: a buffer.
373 * @features: (array length=num_features):
374 * @num_features:
378 * Since: 1.0
380 void
381 hb_shape (hb_font_t *font,
382 hb_buffer_t *buffer,
383 const hb_feature_t *features,
384 unsigned int num_features)
386 hb_shape_full (font, buffer, features, num_features, NULL);