2009-08-13 23:13:25 +02:00
|
|
|
/*
|
2011-04-21 23:14:28 +02:00
|
|
|
* Copyright © 2009 Red Hat, Inc.
|
2012-09-07 04:09:06 +02:00
|
|
|
* Copyright © 2012 Google, Inc.
|
2009-08-13 23:13:25 +02:00
|
|
|
*
|
2010-04-22 06:11:43 +02:00
|
|
|
* This is part of HarfBuzz, a text shaping library.
|
2009-08-13 23:13:25 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, without written agreement and without
|
|
|
|
* license or royalty fees, to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose, provided that the
|
|
|
|
* above copyright notice and the following two paragraphs appear in
|
|
|
|
* all copies of this software.
|
|
|
|
*
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
|
|
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
|
|
|
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|
|
|
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
|
|
|
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
*
|
|
|
|
* Red Hat Author(s): Behdad Esfahbod
|
2012-09-07 04:09:06 +02:00
|
|
|
* Google Author(s): Behdad Esfahbod
|
2009-08-13 23:13:25 +02:00
|
|
|
*/
|
|
|
|
|
2011-04-21 00:50:27 +02:00
|
|
|
#include "hb-private.hh"
|
2009-08-13 23:13:25 +02:00
|
|
|
|
2012-07-26 23:34:25 +02:00
|
|
|
#include "hb-shaper-private.hh"
|
2012-07-27 05:46:53 +02:00
|
|
|
#include "hb-shape-plan-private.hh"
|
2010-05-13 00:23:21 +02:00
|
|
|
#include "hb-buffer-private.hh"
|
2012-07-27 05:46:53 +02:00
|
|
|
#include "hb-font-private.hh"
|
2009-11-05 03:07:03 +01:00
|
|
|
|
2011-08-05 04:31:05 +02:00
|
|
|
|
2012-09-07 04:09:06 +02:00
|
|
|
static void
|
|
|
|
parse_space (const char **pp, const char *end)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
#define ISSPACE(c) ((c)==' '||(c)=='\f'||(c)=='\n'||(c)=='\r'||(c)=='\t'||(c)=='\v')
|
|
|
|
while (*pp < end && (c = **pp, ISSPACE (c)))
|
|
|
|
(*pp)++;
|
|
|
|
#undef ISSPACE
|
|
|
|
}
|
|
|
|
|
|
|
|
static hb_bool_t
|
|
|
|
parse_char (const char **pp, const char *end, char c)
|
|
|
|
{
|
|
|
|
parse_space (pp, end);
|
|
|
|
|
|
|
|
if (*pp == end || **pp != c)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
(*pp)++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static hb_bool_t
|
|
|
|
parse_uint (const char **pp, const char *end, unsigned int *pv)
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
strncpy (buf, *pp, end - *pp);
|
|
|
|
buf[ARRAY_LENGTH (buf) - 1] = '\0';
|
|
|
|
|
|
|
|
char *p = buf;
|
|
|
|
char *pend = p;
|
|
|
|
unsigned int v;
|
|
|
|
|
|
|
|
v = strtol (p, &pend, 0);
|
|
|
|
|
|
|
|
if (p == pend)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*pv = v;
|
|
|
|
*pp += pend - p;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static hb_bool_t
|
|
|
|
parse_feature_value_prefix (const char **pp, const char *end, hb_feature_t *feature)
|
|
|
|
{
|
|
|
|
if (parse_char (pp, end, '-'))
|
|
|
|
feature->value = 0;
|
|
|
|
else {
|
|
|
|
parse_char (pp, end, '+');
|
|
|
|
feature->value = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static hb_bool_t
|
|
|
|
parse_feature_tag (const char **pp, const char *end, hb_feature_t *feature)
|
|
|
|
{
|
|
|
|
const char *p = *pp;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
parse_space (pp, end);
|
|
|
|
|
|
|
|
#define ISALNUM(c) (('a' <= (c) && (c) <= 'z') || ('A' <= (c) && (c) <= 'Z') || ('0' <= (c) && (c) <= '9'))
|
|
|
|
while (*pp < end && (c = **pp, ISALNUM(c)))
|
|
|
|
(*pp)++;
|
|
|
|
#undef ISALNUM
|
|
|
|
|
|
|
|
if (p == *pp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
feature->tag = hb_tag_from_string (p, *pp - p);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static hb_bool_t
|
|
|
|
parse_feature_indices (const char **pp, const char *end, hb_feature_t *feature)
|
|
|
|
{
|
|
|
|
parse_space (pp, end);
|
|
|
|
|
|
|
|
hb_bool_t has_start;
|
|
|
|
|
|
|
|
feature->start = 0;
|
|
|
|
feature->end = (unsigned int) -1;
|
|
|
|
|
|
|
|
if (!parse_char (pp, end, '['))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
has_start = parse_uint (pp, end, &feature->start);
|
|
|
|
|
|
|
|
if (parse_char (pp, end, ':')) {
|
|
|
|
parse_uint (pp, end, &feature->end);
|
|
|
|
} else {
|
|
|
|
if (has_start)
|
|
|
|
feature->end = feature->start + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parse_char (pp, end, ']');
|
|
|
|
}
|
|
|
|
|
|
|
|
static hb_bool_t
|
|
|
|
parse_feature_value_postfix (const char **pp, const char *end, hb_feature_t *feature)
|
|
|
|
{
|
|
|
|
return !parse_char (pp, end, '=') || parse_uint (pp, end, &feature->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static hb_bool_t
|
|
|
|
parse_one_feature (const char **pp, const char *end, hb_feature_t *feature)
|
|
|
|
{
|
|
|
|
return parse_feature_value_prefix (pp, end, feature) &&
|
|
|
|
parse_feature_tag (pp, end, feature) &&
|
|
|
|
parse_feature_indices (pp, end, feature) &&
|
|
|
|
parse_feature_value_postfix (pp, end, feature) &&
|
|
|
|
*pp == end;
|
|
|
|
}
|
|
|
|
|
|
|
|
hb_bool_t
|
|
|
|
hb_feature_from_string (const char *str, int len,
|
|
|
|
hb_feature_t *feature)
|
|
|
|
{
|
|
|
|
if (len < 0)
|
|
|
|
len = strlen (str);
|
|
|
|
|
|
|
|
return parse_one_feature (&str, str + len, feature);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
hb_feature_to_string (hb_feature_t *feature,
|
|
|
|
char *buf, unsigned int size)
|
|
|
|
{
|
|
|
|
if (unlikely (!size)) return;
|
|
|
|
|
|
|
|
char s[128];
|
|
|
|
unsigned int len = 0;
|
|
|
|
if (feature->value == 0)
|
|
|
|
s[len++] = '-';
|
|
|
|
hb_tag_to_string (feature->tag, s + len);
|
|
|
|
len += 4;
|
|
|
|
while (len && s[len - 1] == ' ')
|
|
|
|
len--;
|
2012-11-13 23:42:22 +01:00
|
|
|
if (feature->start != 0 || feature->end != (unsigned int) -1)
|
2012-09-07 04:09:06 +02:00
|
|
|
{
|
|
|
|
s[len++] = '[';
|
|
|
|
if (feature->start)
|
|
|
|
len += snprintf (s + len, ARRAY_LENGTH (s) - len, "%d", feature->start);
|
|
|
|
if (feature->end != feature->start + 1) {
|
|
|
|
s[len++] = ':';
|
|
|
|
if (feature->end != (unsigned int) -1)
|
|
|
|
len += snprintf (s + len, ARRAY_LENGTH (s) - len, "%d", feature->end);
|
|
|
|
}
|
|
|
|
s[len++] = ']';
|
|
|
|
}
|
|
|
|
if (feature->value > 1)
|
|
|
|
{
|
|
|
|
s[len++] = '=';
|
|
|
|
len += snprintf (s + len, ARRAY_LENGTH (s) - len, "%d", feature->value);
|
|
|
|
}
|
|
|
|
assert (len < ARRAY_LENGTH (s));
|
|
|
|
len = MIN (len, size - 1);
|
|
|
|
memcpy (buf, s, len);
|
|
|
|
s[len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-06 01:23:29 +02:00
|
|
|
static const char **static_shaper_list;
|
|
|
|
|
2013-01-30 04:45:00 +01:00
|
|
|
static inline
|
2012-06-06 01:23:29 +02:00
|
|
|
void free_static_shaper_list (void)
|
|
|
|
{
|
|
|
|
free (static_shaper_list);
|
|
|
|
}
|
2011-08-06 01:48:49 +02:00
|
|
|
|
|
|
|
const char **
|
|
|
|
hb_shape_list_shapers (void)
|
|
|
|
{
|
2012-06-06 01:23:29 +02:00
|
|
|
retry:
|
|
|
|
const char **shaper_list = (const char **) hb_atomic_ptr_get (&static_shaper_list);
|
|
|
|
|
|
|
|
if (unlikely (!shaper_list))
|
|
|
|
{
|
|
|
|
/* Not found; allocate one. */
|
2012-07-27 04:05:39 +02:00
|
|
|
shaper_list = (const char **) calloc (1 + HB_SHAPERS_COUNT, sizeof (const char *));
|
2012-06-06 01:23:29 +02:00
|
|
|
if (unlikely (!shaper_list)) {
|
|
|
|
static const char *nil_shaper_list[] = {NULL};
|
|
|
|
return nil_shaper_list;
|
|
|
|
}
|
|
|
|
|
2012-07-27 04:05:39 +02:00
|
|
|
const hb_shaper_pair_t *shapers = _hb_shapers_get ();
|
2012-06-06 01:23:29 +02:00
|
|
|
unsigned int i;
|
2012-07-27 04:05:39 +02:00
|
|
|
for (i = 0; i < HB_SHAPERS_COUNT; i++)
|
2012-06-06 01:23:29 +02:00
|
|
|
shaper_list[i] = shapers[i].name;
|
|
|
|
shaper_list[i] = NULL;
|
|
|
|
|
|
|
|
if (!hb_atomic_ptr_cmpexch (&static_shaper_list, NULL, shaper_list)) {
|
|
|
|
free (shaper_list);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_ATEXIT
|
|
|
|
atexit (free_static_shaper_list); /* First person registers atexit() callback. */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return shaper_list;
|
2011-08-06 01:48:49 +02:00
|
|
|
}
|
2011-08-05 04:31:05 +02:00
|
|
|
|
2012-06-06 01:23:29 +02:00
|
|
|
|
2011-08-05 04:31:05 +02:00
|
|
|
hb_bool_t
|
2011-08-10 16:25:56 +02:00
|
|
|
hb_shape_full (hb_font_t *font,
|
|
|
|
hb_buffer_t *buffer,
|
|
|
|
const hb_feature_t *features,
|
|
|
|
unsigned int num_features,
|
|
|
|
const char * const *shaper_list)
|
2011-03-16 18:53:32 +01:00
|
|
|
{
|
2012-07-27 07:26:11 +02:00
|
|
|
if (unlikely (!buffer->len))
|
|
|
|
return true;
|
|
|
|
|
2012-09-07 04:26:16 +02:00
|
|
|
assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE);
|
|
|
|
|
2012-07-27 10:02:38 +02:00
|
|
|
hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props, features, num_features, shaper_list);
|
2012-07-27 07:13:53 +02:00
|
|
|
hb_bool_t res = hb_shape_plan_execute (shape_plan, font, buffer, features, num_features);
|
|
|
|
hb_shape_plan_destroy (shape_plan);
|
2012-09-07 04:26:16 +02:00
|
|
|
|
|
|
|
if (res)
|
|
|
|
buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
|
2012-07-27 07:13:53 +02:00
|
|
|
return res;
|
2011-03-16 18:53:32 +01:00
|
|
|
}
|
2011-08-05 23:22:19 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
hb_shape (hb_font_t *font,
|
|
|
|
hb_buffer_t *buffer,
|
|
|
|
const hb_feature_t *features,
|
|
|
|
unsigned int num_features)
|
|
|
|
{
|
2012-04-12 20:53:53 +02:00
|
|
|
hb_shape_full (font, buffer, features, num_features, NULL);
|
2011-08-05 23:22:19 +02:00
|
|
|
}
|