2 * Copyright © 2012 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Behdad Esfahbod
27 #include "hb-private.hh"
28 #include "hb-debug.hh"
29 #include "hb-shape-plan-private.hh"
30 #include "hb-shaper-private.hh"
31 #include "hb-font-private.hh"
32 #include "hb-buffer-private.hh"
36 hb_shape_plan_plan (hb_shape_plan_t *shape_plan,
37 const hb_feature_t *user_features,
38 unsigned int num_user_features,
40 unsigned int num_coords,
41 const char * const *shaper_list)
43 DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan,
44 "num_features=%d num_coords=%d shaper_list=%p",
49 const hb_shaper_pair_t *shapers = _hb_shapers_get ();
51 #define HB_SHAPER_PLAN(shaper) \
53 if (hb_##shaper##_shaper_face_data_ensure (shape_plan->face_unsafe)) { \
54 HB_SHAPER_DATA (shaper, shape_plan) = \
55 HB_SHAPER_DATA_CREATE_FUNC (shaper, shape_plan) (shape_plan, \
56 user_features, num_user_features, \
57 coords, num_coords); \
58 shape_plan->shaper_func = _hb_##shaper##_shape; \
59 shape_plan->shaper_name = #shaper; \
64 if (likely (!shaper_list)) {
65 for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++)
68 #define HB_SHAPER_IMPLEMENT(shaper) \
69 else if (shapers[i].func == _hb_##shaper##_shape) \
70 HB_SHAPER_PLAN (shaper);
71 #include "hb-shaper-list.hh"
72 #undef HB_SHAPER_IMPLEMENT
74 for (; *shaper_list; shaper_list++)
77 #define HB_SHAPER_IMPLEMENT(shaper) \
78 else if (0 == strcmp (*shaper_list, #shaper)) \
79 HB_SHAPER_PLAN (shaper);
80 #include "hb-shaper-list.hh"
81 #undef HB_SHAPER_IMPLEMENT
93 * hb_shape_plan_create: (Xconstructor)
96 * @user_features: (array length=num_user_features):
98 * @shaper_list: (array zero-terminated=1):
102 * Return value: (transfer full):
107 hb_shape_plan_create (hb_face_t *face,
108 const hb_segment_properties_t *props,
109 const hb_feature_t *user_features,
110 unsigned int num_user_features,
111 const char * const *shaper_list)
113 return hb_shape_plan_create2 (face, props,
114 user_features, num_user_features,
120 hb_shape_plan_create2 (hb_face_t *face,
121 const hb_segment_properties_t *props,
122 const hb_feature_t *user_features,
123 unsigned int num_user_features,
124 const int *orig_coords,
125 unsigned int num_coords,
126 const char * const *shaper_list)
128 DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr,
129 "face=%p num_features=%d num_coords=%d shaper_list=%p",
135 hb_shape_plan_t *shape_plan;
136 hb_feature_t *features = nullptr;
137 int *coords = nullptr;
139 if (unlikely (!face))
140 face = hb_face_get_empty ();
141 if (unlikely (!props))
142 return hb_shape_plan_get_empty ();
143 if (num_user_features && !(features = (hb_feature_t *) calloc (num_user_features, sizeof (hb_feature_t))))
144 return hb_shape_plan_get_empty ();
145 if (num_coords && !(coords = (int *) calloc (num_coords, sizeof (int))))
148 return hb_shape_plan_get_empty ();
150 if (!(shape_plan = hb_object_create<hb_shape_plan_t> ()))
154 return hb_shape_plan_get_empty ();
157 assert (props->direction != HB_DIRECTION_INVALID);
159 hb_face_make_immutable (face);
160 shape_plan->default_shaper_list = !shaper_list;
161 shape_plan->face_unsafe = face;
162 shape_plan->props = *props;
163 shape_plan->num_user_features = num_user_features;
164 shape_plan->user_features = features;
165 if (num_user_features)
166 memcpy (features, user_features, num_user_features * sizeof (hb_feature_t));
167 shape_plan->num_coords = num_coords;
168 shape_plan->coords = coords;
170 memcpy (coords, orig_coords, num_coords * sizeof (int));
172 hb_shape_plan_plan (shape_plan,
173 user_features, num_user_features,
181 * hb_shape_plan_get_empty:
185 * Return value: (transfer full):
190 hb_shape_plan_get_empty (void)
192 static const hb_shape_plan_t _hb_shape_plan_nil = {
193 HB_OBJECT_HEADER_STATIC,
195 true, /* default_shaper_list */
197 HB_SEGMENT_PROPERTIES_DEFAULT, /* props */
199 nullptr, /* shaper_func */
200 nullptr, /* shaper_name */
202 nullptr, /* user_features */
203 0, /* num_user_featurs */
205 nullptr, /* coords */
209 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
210 #include "hb-shaper-list.hh"
211 #undef HB_SHAPER_IMPLEMENT
215 return const_cast<hb_shape_plan_t *> (&_hb_shape_plan_nil);
219 * hb_shape_plan_reference: (skip)
220 * @shape_plan: a shape plan.
224 * Return value: (transfer full):
229 hb_shape_plan_reference (hb_shape_plan_t *shape_plan)
231 return hb_object_reference (shape_plan);
235 * hb_shape_plan_destroy: (skip)
236 * @shape_plan: a shape plan.
243 hb_shape_plan_destroy (hb_shape_plan_t *shape_plan)
245 if (!hb_object_destroy (shape_plan)) return;
247 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, shape_plan);
248 #include "hb-shaper-list.hh"
249 #undef HB_SHAPER_IMPLEMENT
251 free (shape_plan->user_features);
252 free (shape_plan->coords);
258 * hb_shape_plan_set_user_data: (skip)
259 * @shape_plan: a shape plan.
272 hb_shape_plan_set_user_data (hb_shape_plan_t *shape_plan,
273 hb_user_data_key_t *key,
275 hb_destroy_func_t destroy,
278 return hb_object_set_user_data (shape_plan, key, data, destroy, replace);
282 * hb_shape_plan_get_user_data: (skip)
283 * @shape_plan: a shape plan.
288 * Return value: (transfer none):
293 hb_shape_plan_get_user_data (hb_shape_plan_t *shape_plan,
294 hb_user_data_key_t *key)
296 return hb_object_get_user_data (shape_plan, key);
301 * hb_shape_plan_execute:
302 * @shape_plan: a shape plan.
305 * @features: (array length=num_features):
315 hb_shape_plan_execute (hb_shape_plan_t *shape_plan,
318 const hb_feature_t *features,
319 unsigned int num_features)
321 DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan,
322 "num_features=%d shaper_func=%p, shaper_name=%s",
324 shape_plan->shaper_func,
325 shape_plan->shaper_name);
327 if (unlikely (!buffer->len))
330 assert (!hb_object_is_inert (buffer));
331 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE);
333 if (unlikely (hb_object_is_inert (shape_plan)))
336 assert (shape_plan->face_unsafe == font->face);
337 assert (hb_segment_properties_equal (&shape_plan->props, &buffer->props));
339 #define HB_SHAPER_EXECUTE(shaper) \
341 return HB_SHAPER_DATA (shaper, shape_plan) && \
342 hb_##shaper##_shaper_font_data_ensure (font) && \
343 _hb_##shaper##_shape (shape_plan, font, buffer, features, num_features); \
348 #define HB_SHAPER_IMPLEMENT(shaper) \
349 else if (shape_plan->shaper_func == _hb_##shaper##_shape) \
350 HB_SHAPER_EXECUTE (shaper);
351 #include "hb-shaper-list.hh"
352 #undef HB_SHAPER_IMPLEMENT
354 #undef HB_SHAPER_EXECUTE
366 hb_shape_plan_hash (const hb_shape_plan_t *shape_plan)
368 return hb_segment_properties_hash (&shape_plan->props) +
369 shape_plan->default_shaper_list ? 0 : (intptr_t) shape_plan->shaper_func;
373 /* User-feature caching is currently somewhat dumb:
374 * it only finds matches where the feature array is identical,
375 * not cases where the feature lists would be compatible for plan purposes
376 * but have different ranges, for example.
378 struct hb_shape_plan_proposal_t
380 const hb_segment_properties_t props;
381 const char * const *shaper_list;
382 const hb_feature_t *user_features;
383 unsigned int num_user_features;
385 unsigned int num_coords;
386 hb_shape_func_t *shaper_func;
389 static inline hb_bool_t
390 hb_shape_plan_user_features_match (const hb_shape_plan_t *shape_plan,
391 const hb_shape_plan_proposal_t *proposal)
393 if (proposal->num_user_features != shape_plan->num_user_features)
395 for (unsigned int i = 0, n = proposal->num_user_features; i < n; i++)
396 if (proposal->user_features[i].tag != shape_plan->user_features[i].tag ||
397 proposal->user_features[i].value != shape_plan->user_features[i].value ||
398 proposal->user_features[i].start != shape_plan->user_features[i].start ||
399 proposal->user_features[i].end != shape_plan->user_features[i].end)
404 static inline hb_bool_t
405 hb_shape_plan_coords_match (const hb_shape_plan_t *shape_plan,
406 const hb_shape_plan_proposal_t *proposal)
408 if (proposal->num_coords != shape_plan->num_coords)
410 for (unsigned int i = 0, n = proposal->num_coords; i < n; i++)
411 if (proposal->coords[i] != shape_plan->coords[i])
417 hb_shape_plan_matches (const hb_shape_plan_t *shape_plan,
418 const hb_shape_plan_proposal_t *proposal)
420 return hb_segment_properties_equal (&shape_plan->props, &proposal->props) &&
421 hb_shape_plan_user_features_match (shape_plan, proposal) &&
422 hb_shape_plan_coords_match (shape_plan, proposal) &&
423 ((shape_plan->default_shaper_list && !proposal->shaper_list) ||
424 (shape_plan->shaper_func == proposal->shaper_func));
427 static inline hb_bool_t
428 hb_non_global_user_features_present (const hb_feature_t *user_features,
429 unsigned int num_user_features)
431 while (num_user_features) {
432 if (user_features->start != 0 || user_features->end != (unsigned int) -1)
440 static inline hb_bool_t
441 hb_coords_present (const int *coords,
442 unsigned int num_coords)
444 return num_coords != 0;
448 * hb_shape_plan_create_cached:
451 * @user_features: (array length=num_user_features):
452 * @num_user_features:
453 * @shaper_list: (array zero-terminated=1):
457 * Return value: (transfer full):
462 hb_shape_plan_create_cached (hb_face_t *face,
463 const hb_segment_properties_t *props,
464 const hb_feature_t *user_features,
465 unsigned int num_user_features,
466 const char * const *shaper_list)
468 return hb_shape_plan_create_cached2 (face, props,
469 user_features, num_user_features,
475 hb_shape_plan_create_cached2 (hb_face_t *face,
476 const hb_segment_properties_t *props,
477 const hb_feature_t *user_features,
478 unsigned int num_user_features,
480 unsigned int num_coords,
481 const char * const *shaper_list)
483 DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr,
484 "face=%p num_features=%d shaper_list=%p",
489 hb_shape_plan_proposal_t proposal = {
498 /* Choose shaper. Adapted from hb_shape_plan_plan().
499 * Must choose shaper exactly the same way as that function. */
500 for (const char * const *shaper_item = shaper_list; *shaper_item; shaper_item++)
503 #define HB_SHAPER_IMPLEMENT(shaper) \
504 else if (0 == strcmp (*shaper_item, #shaper) && \
505 hb_##shaper##_shaper_face_data_ensure (face)) \
507 proposal.shaper_func = _hb_##shaper##_shape; \
510 #include "hb-shaper-list.hh"
511 #undef HB_SHAPER_IMPLEMENT
513 if (unlikely (!proposal.shaper_func))
514 return hb_shape_plan_get_empty ();
519 hb_face_t::plan_node_t *cached_plan_nodes = (hb_face_t::plan_node_t *) hb_atomic_ptr_get (&face->shape_plans);
521 /* Don't look for plan in the cache if there were variation coordinates XXX Fix me. */
522 if (!hb_coords_present (coords, num_coords))
523 for (hb_face_t::plan_node_t *node = cached_plan_nodes; node; node = node->next)
524 if (hb_shape_plan_matches (node->shape_plan, &proposal))
526 DEBUG_MSG_FUNC (SHAPE_PLAN, node->shape_plan, "fulfilled from cache");
527 return hb_shape_plan_reference (node->shape_plan);
531 hb_shape_plan_t *shape_plan = hb_shape_plan_create2 (face, props,
532 user_features, num_user_features,
536 /* Don't add to the cache if face is inert. */
537 if (unlikely (hb_object_is_inert (face)))
540 /* Don't add the plan to the cache if there were user features with non-global ranges */
541 if (hb_non_global_user_features_present (user_features, num_user_features))
543 /* Don't add the plan to the cache if there were variation coordinates XXX Fix me. */
544 if (hb_coords_present (coords, num_coords))
547 hb_face_t::plan_node_t *node = (hb_face_t::plan_node_t *) calloc (1, sizeof (hb_face_t::plan_node_t));
548 if (unlikely (!node))
551 node->shape_plan = shape_plan;
552 node->next = cached_plan_nodes;
554 if (!hb_atomic_ptr_cmpexch (&face->shape_plans, cached_plan_nodes, node)) {
555 hb_shape_plan_destroy (shape_plan);
559 DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan, "inserted into cache");
561 return hb_shape_plan_reference (shape_plan);
565 * hb_shape_plan_get_shaper:
566 * @shape_plan: a shape plan.
570 * Return value: (transfer none):
575 hb_shape_plan_get_shaper (hb_shape_plan_t *shape_plan)
577 return shape_plan->shaper_name;