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
28 #include "hb-shape-plan.hh"
29 #include "hb-shaper.hh"
31 #include "hb-buffer.hh"
35 * SECTION:hb-shape-plan
36 * @title: hb-shape-plan
37 * @short_description: Object representing a shaping plan
40 * Shape plans are not used for shaping directly, but can be access to query
41 * certain information about how shaping will perform given a set of input
42 * parameters (script, language, direction, features, etc.)
43 * Most client would not need to deal with shape plans directly.
52 hb_shape_plan_key_t::init (bool copy,
54 const hb_segment_properties_t *props,
55 const hb_feature_t *user_features,
56 unsigned int num_user_features,
58 unsigned int num_coords,
59 const char * const *shaper_list)
61 hb_feature_t *features = nullptr;
62 if (copy && num_user_features && !(features = (hb_feature_t *) calloc (num_user_features, sizeof (hb_feature_t))))
66 this->num_user_features = num_user_features;
67 this->user_features = copy ? features : user_features;
68 if (copy && num_user_features)
70 memcpy (features, user_features, num_user_features * sizeof (hb_feature_t));
71 /* Make start/end uniform to easier catch bugs. */
72 for (unsigned int i = 0; i < num_user_features; i++)
74 if (features[0].start != HB_FEATURE_GLOBAL_START)
75 features[0].start = 1;
76 if (features[0].end != HB_FEATURE_GLOBAL_END)
80 this->shaper_func = nullptr;
81 this->shaper_name = nullptr;
82 #ifndef HB_NO_OT_SHAPE
83 this->ot.init (face, coords, num_coords);
90 #define HB_SHAPER_PLAN(shaper) \
92 if (face->data.shaper) \
94 this->shaper_func = _hb_##shaper##_shape; \
95 this->shaper_name = #shaper; \
100 if (unlikely (shaper_list))
102 for (; *shaper_list; shaper_list++)
105 #define HB_SHAPER_IMPLEMENT(shaper) \
106 else if (0 == strcmp (*shaper_list, #shaper)) \
107 HB_SHAPER_PLAN (shaper);
108 #include "hb-shaper-list.hh"
109 #undef HB_SHAPER_IMPLEMENT
113 const hb_shaper_entry_t *shapers = _hb_shapers_get ();
114 for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++)
117 #define HB_SHAPER_IMPLEMENT(shaper) \
118 else if (shapers[i].func == _hb_##shaper##_shape) \
119 HB_SHAPER_PLAN (shaper);
120 #include "hb-shaper-list.hh"
121 #undef HB_SHAPER_IMPLEMENT
123 #undef HB_SHAPER_PLAN
131 hb_shape_plan_key_t::user_features_match (const hb_shape_plan_key_t *other)
133 if (this->num_user_features != other->num_user_features)
135 for (unsigned int i = 0; i < num_user_features; i++)
137 if (this->user_features[i].tag != other->user_features[i].tag ||
138 this->user_features[i].value != other->user_features[i].value ||
139 (this->user_features[i].start == HB_FEATURE_GLOBAL_START &&
140 this->user_features[i].end == HB_FEATURE_GLOBAL_END) !=
141 (other->user_features[i].start == HB_FEATURE_GLOBAL_START &&
142 other->user_features[i].end == HB_FEATURE_GLOBAL_END))
149 hb_shape_plan_key_t::equal (const hb_shape_plan_key_t *other)
151 return hb_segment_properties_equal (&this->props, &other->props) &&
152 this->user_features_match (other) &&
153 #ifndef HB_NO_OT_SHAPE
154 this->ot.equal (&other->ot) &&
156 this->shaper_func == other->shaper_func;
166 * hb_shape_plan_create: (Xconstructor)
169 * @user_features: (array length=num_user_features):
170 * @num_user_features:
171 * @shaper_list: (array zero-terminated=1):
175 * Return value: (transfer full):
180 hb_shape_plan_create (hb_face_t *face,
181 const hb_segment_properties_t *props,
182 const hb_feature_t *user_features,
183 unsigned int num_user_features,
184 const char * const *shaper_list)
186 return hb_shape_plan_create2 (face, props,
187 user_features, num_user_features,
193 hb_shape_plan_create2 (hb_face_t *face,
194 const hb_segment_properties_t *props,
195 const hb_feature_t *user_features,
196 unsigned int num_user_features,
198 unsigned int num_coords,
199 const char * const *shaper_list)
201 DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr,
202 "face=%p num_features=%d num_coords=%d shaper_list=%p",
208 assert (props->direction != HB_DIRECTION_INVALID);
210 hb_shape_plan_t *shape_plan;
212 if (unlikely (!props))
214 if (!(shape_plan = hb_object_create<hb_shape_plan_t> ()))
217 if (unlikely (!face))
218 face = hb_face_get_empty ();
219 hb_face_make_immutable (face);
220 shape_plan->face_unsafe = face;
222 if (unlikely (!shape_plan->key.init (true,
231 #ifndef HB_NO_OT_SHAPE
232 if (unlikely (!shape_plan->ot.init0 (face, &shape_plan->key)))
238 #ifndef HB_NO_OT_SHAPE
241 shape_plan->key.free ();
245 return hb_shape_plan_get_empty ();
249 * hb_shape_plan_get_empty:
253 * Return value: (transfer full):
258 hb_shape_plan_get_empty ()
260 return const_cast<hb_shape_plan_t *> (&Null(hb_shape_plan_t));
264 * hb_shape_plan_reference: (skip)
265 * @shape_plan: a shape plan.
269 * Return value: (transfer full):
274 hb_shape_plan_reference (hb_shape_plan_t *shape_plan)
276 return hb_object_reference (shape_plan);
280 * hb_shape_plan_destroy: (skip)
281 * @shape_plan: a shape plan.
288 hb_shape_plan_destroy (hb_shape_plan_t *shape_plan)
290 if (!hb_object_destroy (shape_plan)) return;
292 #ifndef HB_NO_OT_SHAPE
293 shape_plan->ot.fini ();
295 shape_plan->key.free ();
300 * hb_shape_plan_set_user_data: (skip)
301 * @shape_plan: a shape plan.
314 hb_shape_plan_set_user_data (hb_shape_plan_t *shape_plan,
315 hb_user_data_key_t *key,
317 hb_destroy_func_t destroy,
320 return hb_object_set_user_data (shape_plan, key, data, destroy, replace);
324 * hb_shape_plan_get_user_data: (skip)
325 * @shape_plan: a shape plan.
330 * Return value: (transfer none):
335 hb_shape_plan_get_user_data (hb_shape_plan_t *shape_plan,
336 hb_user_data_key_t *key)
338 return hb_object_get_user_data (shape_plan, key);
342 * hb_shape_plan_get_shaper:
343 * @shape_plan: a shape plan.
347 * Return value: (transfer none):
352 hb_shape_plan_get_shaper (hb_shape_plan_t *shape_plan)
354 return shape_plan->key.shaper_name;
359 * hb_shape_plan_execute:
360 * @shape_plan: a shape plan.
363 * @features: (array length=num_features):
373 hb_shape_plan_execute (hb_shape_plan_t *shape_plan,
376 const hb_feature_t *features,
377 unsigned int num_features)
379 DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan,
380 "num_features=%d shaper_func=%p, shaper_name=%s",
382 shape_plan->key.shaper_func,
383 shape_plan->key.shaper_name);
385 if (unlikely (!buffer->len))
388 assert (!hb_object_is_immutable (buffer));
389 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE);
391 if (unlikely (hb_object_is_inert (shape_plan)))
394 assert (shape_plan->face_unsafe == font->face);
395 assert (hb_segment_properties_equal (&shape_plan->key.props, &buffer->props));
397 #define HB_SHAPER_EXECUTE(shaper) \
399 return font->data.shaper && \
400 _hb_##shaper##_shape (shape_plan, font, buffer, features, num_features); \
405 #define HB_SHAPER_IMPLEMENT(shaper) \
406 else if (shape_plan->key.shaper_func == _hb_##shaper##_shape) \
407 HB_SHAPER_EXECUTE (shaper);
408 #include "hb-shaper-list.hh"
409 #undef HB_SHAPER_IMPLEMENT
411 #undef HB_SHAPER_EXECUTE
422 * hb_shape_plan_create_cached:
425 * @user_features: (array length=num_user_features):
426 * @num_user_features:
427 * @shaper_list: (array zero-terminated=1):
431 * Return value: (transfer full):
436 hb_shape_plan_create_cached (hb_face_t *face,
437 const hb_segment_properties_t *props,
438 const hb_feature_t *user_features,
439 unsigned int num_user_features,
440 const char * const *shaper_list)
442 return hb_shape_plan_create_cached2 (face, props,
443 user_features, num_user_features,
449 hb_shape_plan_create_cached2 (hb_face_t *face,
450 const hb_segment_properties_t *props,
451 const hb_feature_t *user_features,
452 unsigned int num_user_features,
454 unsigned int num_coords,
455 const char * const *shaper_list)
457 DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr,
458 "face=%p num_features=%d shaper_list=%p",
464 hb_face_t::plan_node_t *cached_plan_nodes = face->shape_plans;
466 bool dont_cache = hb_object_is_inert (face);
468 if (likely (!dont_cache))
470 hb_shape_plan_key_t key;
471 if (!key.init (false,
479 return hb_shape_plan_get_empty ();
481 for (hb_face_t::plan_node_t *node = cached_plan_nodes; node; node = node->next)
482 if (node->shape_plan->key.equal (&key))
484 DEBUG_MSG_FUNC (SHAPE_PLAN, node->shape_plan, "fulfilled from cache");
485 return hb_shape_plan_reference (node->shape_plan);
489 hb_shape_plan_t *shape_plan = hb_shape_plan_create2 (face, props,
490 user_features, num_user_features,
494 if (unlikely (dont_cache))
497 hb_face_t::plan_node_t *node = (hb_face_t::plan_node_t *) calloc (1, sizeof (hb_face_t::plan_node_t));
498 if (unlikely (!node))
501 node->shape_plan = shape_plan;
502 node->next = cached_plan_nodes;
504 if (unlikely (!face->shape_plans.cmpexch (cached_plan_nodes, node)))
506 hb_shape_plan_destroy (shape_plan);
510 DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan, "inserted into cache");
512 return hb_shape_plan_reference (shape_plan);