1 /* 2 * Copyright 2009 Red Hat, Inc. 3 * Copyright 2012 Google, Inc. 4 * 5 * This is part of HarfBuzz, a text shaping library. 6 * 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. 12 * 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. 18 * 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. 24 * 25 * Red Hat Author(s): Behdad Esfahbod 26 * Google Author(s): Behdad Esfahbod 27 */ 28 29 #include "hb-private.hh" 30 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" 35 36 37 static bool 38 parse_space (const char **pp, const char *end) 39 { 40 while (*pp < end && ISSPACE (**pp)) 41 (*pp)++; 42 return true; 43 } 44 45 static bool 46 parse_char (const char **pp, const char *end, char c) 47 { 48 parse_space (pp, end); 49 50 if (*pp == end || **pp != c) 51 return false; 52 53 (*pp)++; 54 return true; 55 } 56 57 static bool 58 parse_uint (const char **pp, const char *end, unsigned int *pv) 59 { 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'; 64 65 char *p = buf; 66 char *pend = p; 67 unsigned int v; 68 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; 75 76 *pv = v; 77 *pp += pend - p; 78 return true; 79 } 80 81 static bool 82 parse_bool (const char **pp, const char *end, unsigned int *pv) 83 { 84 parse_space (pp, end); 85 86 const char *p = *pp; 87 while (*pp < end && ISALPHA(**pp)) 88 (*pp)++; 89 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; 97 98 return true; 99 } 100 101 static bool 102 parse_feature_value_prefix (const char **pp, const char *end, hb_feature_t *feature) 103 { 104 if (parse_char (pp, end, '-')) 105 feature->value = 0; 106 else { 107 parse_char (pp, end, '+'); 108 feature->value = 1; 109 } 110 111 return true; 112 } 113 114 static bool 115 parse_feature_tag (const char **pp, const char *end, hb_feature_t *feature) 116 { 117 parse_space (pp, end); 118 119 char quote = 0; 120 121 if (*pp < end && (**pp == '\'' || **pp == '"')) 122 { 123 quote = **pp; 124 (*pp)++; 125 } 126 127 const char *p = *pp; 128 while (*pp < end && ISALNUM(**pp)) 129 (*pp)++; 130 131 if (p == *pp || *pp - p > 4) 132 return false; 133 134 feature->tag = hb_tag_from_string (p, *pp - p); 135 136 if (quote) 137 { 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)++; 145 } 146 147 return true; 148 } 149 150 static bool 151 parse_feature_indices (const char **pp, const char *end, hb_feature_t *feature) 152 { 153 parse_space (pp, end); 154 155 bool has_start; 156 157 feature->start = 0; 158 feature->end = (unsigned int) -1; 159 160 if (!parse_char (pp, end, '[')) 161 return true; 162 163 has_start = parse_uint (pp, end, &feature->start); 164 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; 170 } 171 172 return parse_char (pp, end, ']'); 173 } 174 175 static bool 176 parse_feature_value_postfix (const char **pp, const char *end, hb_feature_t *feature) 177 { 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; 185 } 186 187 188 static bool 189 parse_one_feature (const char **pp, const char *end, hb_feature_t *feature) 190 { 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; 197 } 198 199 /** 200 * hb_feature_from_string: 201 * @str: (array length=len): 202 * @len: 203 * @feature: (out) (allow-none): 204 * 205 * 206 * 207 * Return value: 208 * 209 * Since: 1.0 210 **/ 211 hb_bool_t 212 hb_feature_from_string (const char *str, int len, 213 hb_feature_t *feature) 214 { 215 hb_feature_t feat; 216 217 if (len < 0) 218 len = strlen (str); 219 220 if (likely (parse_one_feature (&str, str + len, &feat))) 221 { 222 if (feature) 223 *feature = feat; 224 return true; 225 } 226 227 if (feature) 228 memset (feature, 0, sizeof (*feature)); 229 return false; 230 } 231 232 /** 233 * hb_feature_to_string: 234 * @feature: 235 * @buf: (array length=size): 236 * @size: 237 * 238 * 239 * 240 * Since: 1.0 241 **/ 242 void 243 hb_feature_to_string (hb_feature_t *feature, 244 char *buf, unsigned int size) 245 { 246 if (unlikely (!size)) return; 247 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) 257 { 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)); 265 } 266 s[len++] = ']'; 267 } 268 if (feature->value > 1) 269 { 270 s[len++] = '='; 271 len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->value)); 272 } 273 assert (len < ARRAY_LENGTH (s)); 274 len = MIN (len, size - 1); 275 memcpy (buf, s, len); 276 buf[len] = '\0'; 277 } 278 279 280 static const char **static_shaper_list; 281 282 static inline 283 void free_static_shaper_list (void) 284 { 285 free (static_shaper_list); 286 } 287 288 /** 289 * hb_shape_list_shapers: 290 * 291 * 292 * 293 * Return value: (transfer none): 294 * 295 * Since: 1.0 296 **/ 297 const char ** 298 hb_shape_list_shapers (void) 299 { 300 retry: 301 const char **shaper_list = (const char **) hb_atomic_ptr_get (&static_shaper_list); 302 303 if (unlikely (!shaper_list)) 304 { 305 /* Not found; allocate one. */ 306 shaper_list = (const char **) calloc (1 + HB_SHAPERS_COUNT, sizeof (const char *)); 307 if (unlikely (!shaper_list)) { 308 static const char *nil_shaper_list[] = {NULL}; 309 return nil_shaper_list; 310 } 311 312 const hb_shaper_pair_t *shapers = _hb_shapers_get (); 313 unsigned int i; 314 for (i = 0; i < HB_SHAPERS_COUNT; i++) 315 shaper_list[i] = shapers[i].name; 316 shaper_list[i] = NULL; 317 318 if (!hb_atomic_ptr_cmpexch (&static_shaper_list, NULL, shaper_list)) { 319 free (shaper_list); 320 goto retry; 321 } 322 323 #ifdef HB_USE_ATEXIT 324 atexit (free_static_shaper_list); /* First person registers atexit() callback. */ 325 #endif 326 } 327 328 return shaper_list; 329 } 330 331 332 /** 333 * hb_shape_full: 334 * @font: a font. 335 * @buffer: a buffer. 336 * @features: (array length=num_features): 337 * @num_features: 338 * @shaper_list: (array zero-terminated=1): 339 * 340 * 341 * 342 * Return value: 343 * 344 * Since: 1.0 345 **/ 346 hb_bool_t 347 hb_shape_full (hb_font_t *font, 348 hb_buffer_t *buffer, 349 const hb_feature_t *features, 350 unsigned int num_features, 351 const char * const *shaper_list) 352 { 353 if (unlikely (!buffer->len)) 354 return true; 355 356 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE); 357 358 hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props, features, num_features, shaper_list); 359 hb_bool_t res = hb_shape_plan_execute (shape_plan, font, buffer, features, num_features); 360 hb_shape_plan_destroy (shape_plan); 361 362 if (res) 363 buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS; 364 return res; 365 } 366 367 /** 368 * hb_shape: 369 * @font: a font. 370 * @buffer: a buffer. 371 * @features: (array length=num_features): 372 * @num_features: 373 * 374 * 375 * 376 * Since: 1.0 377 **/ 378 void 379 hb_shape (hb_font_t *font, 380 hb_buffer_t *buffer, 381 const hb_feature_t *features, 382 unsigned int num_features) 383 { 384 hb_shape_full (font, buffer, features, num_features, NULL); 385 } 386