cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-shape.cc
blob67ef7e6a8f56a09714582f4b17b9dc8b4745b21b
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 void
38 parse_space (const char **pp, const char *end)
40 char c;
41 while (*pp < end && (c = **pp, ISSPACE (c)))
42 (*pp)++;
45 static hb_bool_t
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 hb_bool_t
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 hb_bool_t
82 parse_feature_value_prefix (const char **pp, const char *end, hb_feature_t *feature)
84 if (parse_char (pp, end, '-'))
85 feature->value = 0;
86 else {
87 parse_char (pp, end, '+');
88 feature->value = 1;
91 return true;
94 static hb_bool_t
95 parse_feature_tag (const char **pp, const char *end, hb_feature_t *feature)
97 const char *p = *pp;
98 char c;
100 parse_space (pp, end);
102 #define ISALNUM(c) (('a' <= (c) && (c) <= 'z') || ('A' <= (c) && (c) <= 'Z') || ('0' <= (c) && (c) <= '9'))
103 while (*pp < end && (c = **pp, ISALNUM(c)))
104 (*pp)++;
105 #undef ISALNUM
107 if (p == *pp)
108 return false;
110 feature->tag = hb_tag_from_string (p, *pp - p);
111 return true;
114 static hb_bool_t
115 parse_feature_indices (const char **pp, const char *end, hb_feature_t *feature)
117 parse_space (pp, end);
119 hb_bool_t has_start;
121 feature->start = 0;
122 feature->end = (unsigned int) -1;
124 if (!parse_char (pp, end, '['))
125 return true;
127 has_start = parse_uint (pp, end, &feature->start);
129 if (parse_char (pp, end, ':')) {
130 parse_uint (pp, end, &feature->end);
131 } else {
132 if (has_start)
133 feature->end = feature->start + 1;
136 return parse_char (pp, end, ']');
139 static hb_bool_t
140 parse_feature_value_postfix (const char **pp, const char *end, hb_feature_t *feature)
142 return !parse_char (pp, end, '=') || parse_uint (pp, end, &feature->value);
146 static hb_bool_t
147 parse_one_feature (const char **pp, const char *end, hb_feature_t *feature)
149 return parse_feature_value_prefix (pp, end, feature) &&
150 parse_feature_tag (pp, end, feature) &&
151 parse_feature_indices (pp, end, feature) &&
152 parse_feature_value_postfix (pp, end, feature) &&
153 *pp == end;
156 hb_bool_t
157 hb_feature_from_string (const char *str, int len,
158 hb_feature_t *feature)
160 if (len < 0)
161 len = strlen (str);
163 return parse_one_feature (&str, str + len, feature);
166 void
167 hb_feature_to_string (hb_feature_t *feature,
168 char *buf, unsigned int size)
170 if (unlikely (!size)) return;
172 char s[128];
173 unsigned int len = 0;
174 if (feature->value == 0)
175 s[len++] = '-';
176 hb_tag_to_string (feature->tag, s + len);
177 len += 4;
178 while (len && s[len - 1] == ' ')
179 len--;
180 if (feature->start != 0 || feature->end != (unsigned int) -1)
182 s[len++] = '[';
183 if (feature->start)
184 len += snprintf (s + len, ARRAY_LENGTH (s) - len, "%d", feature->start);
185 if (feature->end != feature->start + 1) {
186 s[len++] = ':';
187 if (feature->end != (unsigned int) -1)
188 len += snprintf (s + len, ARRAY_LENGTH (s) - len, "%d", feature->end);
190 s[len++] = ']';
192 if (feature->value > 1)
194 s[len++] = '=';
195 len += snprintf (s + len, ARRAY_LENGTH (s) - len, "%d", feature->value);
197 assert (len < ARRAY_LENGTH (s));
198 len = MIN (len, size - 1);
199 memcpy (buf, s, len);
200 s[len] = '\0';
204 static const char **static_shaper_list;
206 static inline
207 void free_static_shaper_list (void)
209 free (static_shaper_list);
212 const char **
213 hb_shape_list_shapers (void)
215 retry:
216 const char **shaper_list = (const char **) hb_atomic_ptr_get (&static_shaper_list);
218 if (unlikely (!shaper_list))
220 /* Not found; allocate one. */
221 shaper_list = (const char **) calloc (1 + HB_SHAPERS_COUNT, sizeof (const char *));
222 if (unlikely (!shaper_list)) {
223 static const char *nil_shaper_list[] = {NULL};
224 return nil_shaper_list;
227 const hb_shaper_pair_t *shapers = _hb_shapers_get ();
228 unsigned int i;
229 for (i = 0; i < HB_SHAPERS_COUNT; i++)
230 shaper_list[i] = shapers[i].name;
231 shaper_list[i] = NULL;
233 if (!hb_atomic_ptr_cmpexch (&static_shaper_list, NULL, shaper_list)) {
234 free (shaper_list);
235 goto retry;
238 #ifdef HAVE_ATEXIT
239 atexit (free_static_shaper_list); /* First person registers atexit() callback. */
240 #endif
243 return shaper_list;
247 hb_bool_t
248 hb_shape_full (hb_font_t *font,
249 hb_buffer_t *buffer,
250 const hb_feature_t *features,
251 unsigned int num_features,
252 const char * const *shaper_list)
254 if (unlikely (!buffer->len))
255 return true;
257 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE);
259 hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props, features, num_features, shaper_list);
260 hb_bool_t res = hb_shape_plan_execute (shape_plan, font, buffer, features, num_features);
261 hb_shape_plan_destroy (shape_plan);
263 if (res)
264 buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
265 return res;
268 void
269 hb_shape (hb_font_t *font,
270 hb_buffer_t *buffer,
271 const hb_feature_t *features,
272 unsigned int num_features)
274 hb_shape_full (font, buffer, features, num_features, NULL);