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
)
41 #define ISSPACE(c) ((c)==' '||(c)=='\f'||(c)=='\n'||(c)=='\r'||(c)=='\t'||(c)=='\v')
42 while (*pp
< end
&& (c
= **pp
, ISSPACE (c
)))
48 parse_char (const char **pp
, const char *end
, char c
)
50 parse_space (pp
, end
);
52 if (*pp
== end
|| **pp
!= c
)
60 parse_uint (const char **pp
, const char *end
, unsigned int *pv
)
63 strncpy (buf
, *pp
, end
- *pp
);
64 buf
[ARRAY_LENGTH (buf
) - 1] = '\0';
70 v
= strtol (p
, &pend
, 0);
81 parse_feature_value_prefix (const char **pp
, const char *end
, hb_feature_t
*feature
)
83 if (parse_char (pp
, end
, '-'))
86 parse_char (pp
, end
, '+');
94 parse_feature_tag (const char **pp
, const char *end
, hb_feature_t
*feature
)
99 parse_space (pp
, end
);
101 #define ISALNUM(c) (('a' <= (c) && (c) <= 'z') || ('A' <= (c) && (c) <= 'Z') || ('0' <= (c) && (c) <= '9'))
102 while (*pp
< end
&& (c
= **pp
, ISALNUM(c
)))
109 feature
->tag
= hb_tag_from_string (p
, *pp
- p
);
114 parse_feature_indices (const char **pp
, const char *end
, hb_feature_t
*feature
)
116 parse_space (pp
, end
);
121 feature
->end
= (unsigned int) -1;
123 if (!parse_char (pp
, end
, '['))
126 has_start
= parse_uint (pp
, end
, &feature
->start
);
128 if (parse_char (pp
, end
, ':')) {
129 parse_uint (pp
, end
, &feature
->end
);
132 feature
->end
= feature
->start
+ 1;
135 return parse_char (pp
, end
, ']');
139 parse_feature_value_postfix (const char **pp
, const char *end
, hb_feature_t
*feature
)
141 return !parse_char (pp
, end
, '=') || parse_uint (pp
, end
, &feature
->value
);
146 parse_one_feature (const char **pp
, const char *end
, hb_feature_t
*feature
)
148 return parse_feature_value_prefix (pp
, end
, feature
) &&
149 parse_feature_tag (pp
, end
, feature
) &&
150 parse_feature_indices (pp
, end
, feature
) &&
151 parse_feature_value_postfix (pp
, end
, feature
) &&
156 hb_feature_from_string (const char *str
, int len
,
157 hb_feature_t
*feature
)
162 return parse_one_feature (&str
, str
+ len
, feature
);
166 hb_feature_to_string (hb_feature_t
*feature
,
167 char *buf
, unsigned int size
)
169 if (unlikely (!size
)) return;
172 unsigned int len
= 0;
173 if (feature
->value
== 0)
175 hb_tag_to_string (feature
->tag
, s
+ len
);
177 while (len
&& s
[len
- 1] == ' ')
179 if (feature
->start
!= 0 || feature
->start
!= (unsigned int) -1)
183 len
+= snprintf (s
+ len
, ARRAY_LENGTH (s
) - len
, "%d", feature
->start
);
184 if (feature
->end
!= feature
->start
+ 1) {
186 if (feature
->end
!= (unsigned int) -1)
187 len
+= snprintf (s
+ len
, ARRAY_LENGTH (s
) - len
, "%d", feature
->end
);
191 if (feature
->value
> 1)
194 len
+= snprintf (s
+ len
, ARRAY_LENGTH (s
) - len
, "%d", feature
->value
);
196 assert (len
< ARRAY_LENGTH (s
));
197 len
= MIN (len
, size
- 1);
198 memcpy (buf
, s
, len
);
203 static const char **static_shaper_list
;
206 void free_static_shaper_list (void)
208 free (static_shaper_list
);
212 hb_shape_list_shapers (void)
215 const char **shaper_list
= (const char **) hb_atomic_ptr_get (&static_shaper_list
);
217 if (unlikely (!shaper_list
))
219 /* Not found; allocate one. */
220 shaper_list
= (const char **) calloc (1 + HB_SHAPERS_COUNT
, sizeof (const char *));
221 if (unlikely (!shaper_list
)) {
222 static const char *nil_shaper_list
[] = {NULL
};
223 return nil_shaper_list
;
226 const hb_shaper_pair_t
*shapers
= _hb_shapers_get ();
228 for (i
= 0; i
< HB_SHAPERS_COUNT
; i
++)
229 shaper_list
[i
] = shapers
[i
].name
;
230 shaper_list
[i
] = NULL
;
232 if (!hb_atomic_ptr_cmpexch (&static_shaper_list
, NULL
, shaper_list
)) {
238 atexit (free_static_shaper_list
); /* First person registers atexit() callback. */
247 hb_shape_full (hb_font_t
*font
,
249 const hb_feature_t
*features
,
250 unsigned int num_features
,
251 const char * const *shaper_list
)
253 if (unlikely (!buffer
->len
))
256 assert (buffer
->content_type
== HB_BUFFER_CONTENT_TYPE_UNICODE
);
258 buffer
->guess_properties ();
260 hb_shape_plan_t
*shape_plan
= hb_shape_plan_create_cached (font
->face
, &buffer
->props
, features
, num_features
, shaper_list
);
261 hb_bool_t res
= hb_shape_plan_execute (shape_plan
, font
, buffer
, features
, num_features
);
262 hb_shape_plan_destroy (shape_plan
);
265 buffer
->content_type
= HB_BUFFER_CONTENT_TYPE_GLYPHS
;
270 hb_shape (hb_font_t
*font
,
272 const hb_feature_t
*features
,
273 unsigned int num_features
)
275 hb_shape_full (font
, buffer
, features
, num_features
, NULL
);