Add git cl format presubmit warning for extension and apps.
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-shape-plan.cc
blobb44a9e2ab77ed4d6958ce2c4bfb09ded5f992183
1 /*
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
16 * DAMAGE.
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-shape-plan-private.hh"
28 #include "hb-shaper-private.hh"
29 #include "hb-font-private.hh"
30 #include "hb-buffer-private.hh"
32 #define HB_SHAPER_IMPLEMENT(shaper) \
33 HB_SHAPER_DATA_ENSURE_DECLARE(shaper, face) \
34 HB_SHAPER_DATA_ENSURE_DECLARE(shaper, font)
35 #include "hb-shaper-list.hh"
36 #undef HB_SHAPER_IMPLEMENT
39 static void
40 hb_shape_plan_plan (hb_shape_plan_t *shape_plan,
41 const hb_feature_t *user_features,
42 unsigned int num_user_features,
43 const char * const *shaper_list)
45 const hb_shaper_pair_t *shapers = _hb_shapers_get ();
47 #define HB_SHAPER_PLAN(shaper) \
48 HB_STMT_START { \
49 if (hb_##shaper##_shaper_face_data_ensure (shape_plan->face)) { \
50 HB_SHAPER_DATA (shaper, shape_plan) = \
51 HB_SHAPER_DATA_CREATE_FUNC (shaper, shape_plan) (shape_plan, user_features, num_user_features); \
52 shape_plan->shaper_func = _hb_##shaper##_shape; \
53 shape_plan->shaper_name = #shaper; \
54 return; \
55 } \
56 } HB_STMT_END
58 if (likely (!shaper_list)) {
59 for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++)
60 if (0)
62 #define HB_SHAPER_IMPLEMENT(shaper) \
63 else if (shapers[i].func == _hb_##shaper##_shape) \
64 HB_SHAPER_PLAN (shaper);
65 #include "hb-shaper-list.hh"
66 #undef HB_SHAPER_IMPLEMENT
67 } else {
68 for (; *shaper_list; shaper_list++)
69 if (0)
71 #define HB_SHAPER_IMPLEMENT(shaper) \
72 else if (0 == strcmp (*shaper_list, #shaper)) \
73 HB_SHAPER_PLAN (shaper);
74 #include "hb-shaper-list.hh"
75 #undef HB_SHAPER_IMPLEMENT
78 #undef HB_SHAPER_PLAN
83 * hb_shape_plan_t
86 /**
87 * hb_shape_plan_create: (Xconstructor)
88 * @face:
89 * @props:
90 * @user_features: (array length=num_user_features):
91 * @num_user_features:
92 * @shaper_list: (array zero-terminated=1):
96 * Return value: (transfer full):
98 * Since: 1.0
99 **/
100 hb_shape_plan_t *
101 hb_shape_plan_create (hb_face_t *face,
102 const hb_segment_properties_t *props,
103 const hb_feature_t *user_features,
104 unsigned int num_user_features,
105 const char * const *shaper_list)
107 assert (props->direction != HB_DIRECTION_INVALID);
109 hb_shape_plan_t *shape_plan;
111 if (unlikely (!face))
112 face = hb_face_get_empty ();
113 if (unlikely (!props || hb_object_is_inert (face)))
114 return hb_shape_plan_get_empty ();
115 if (!(shape_plan = hb_object_create<hb_shape_plan_t> ()))
116 return hb_shape_plan_get_empty ();
118 hb_face_make_immutable (face);
119 shape_plan->default_shaper_list = shaper_list == NULL;
120 shape_plan->face = hb_face_reference (face);
121 shape_plan->props = *props;
123 hb_shape_plan_plan (shape_plan, user_features, num_user_features, shaper_list);
125 return shape_plan;
129 * hb_shape_plan_get_empty:
133 * Return value: (transfer full):
135 * Since: 1.0
137 hb_shape_plan_t *
138 hb_shape_plan_get_empty (void)
140 static const hb_shape_plan_t _hb_shape_plan_nil = {
141 HB_OBJECT_HEADER_STATIC,
143 true, /* default_shaper_list */
144 NULL, /* face */
145 HB_SEGMENT_PROPERTIES_DEFAULT, /* props */
147 NULL, /* shaper_func */
148 NULL, /* shaper_name */
151 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
152 #include "hb-shaper-list.hh"
153 #undef HB_SHAPER_IMPLEMENT
157 return const_cast<hb_shape_plan_t *> (&_hb_shape_plan_nil);
161 * hb_shape_plan_reference: (skip)
162 * @shape_plan: a shape plan.
166 * Return value: (transfer full):
168 * Since: 1.0
170 hb_shape_plan_t *
171 hb_shape_plan_reference (hb_shape_plan_t *shape_plan)
173 return hb_object_reference (shape_plan);
177 * hb_shape_plan_destroy: (skip)
178 * @shape_plan: a shape plan.
182 * Since: 1.0
184 void
185 hb_shape_plan_destroy (hb_shape_plan_t *shape_plan)
187 if (!hb_object_destroy (shape_plan)) return;
189 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, shape_plan);
190 #include "hb-shaper-list.hh"
191 #undef HB_SHAPER_IMPLEMENT
193 hb_face_destroy (shape_plan->face);
195 free (shape_plan);
199 * hb_shape_plan_set_user_data: (skip)
200 * @shape_plan: a shape plan.
201 * @key:
202 * @data:
203 * @destroy:
204 * @replace:
208 * Return value:
210 * Since: 1.0
212 hb_bool_t
213 hb_shape_plan_set_user_data (hb_shape_plan_t *shape_plan,
214 hb_user_data_key_t *key,
215 void * data,
216 hb_destroy_func_t destroy,
217 hb_bool_t replace)
219 return hb_object_set_user_data (shape_plan, key, data, destroy, replace);
223 * hb_shape_plan_get_user_data: (skip)
224 * @shape_plan: a shape plan.
225 * @key:
229 * Return value: (transfer none):
231 * Since: 1.0
233 void *
234 hb_shape_plan_get_user_data (hb_shape_plan_t *shape_plan,
235 hb_user_data_key_t *key)
237 return hb_object_get_user_data (shape_plan, key);
242 * hb_shape_plan_execute:
243 * @shape_plan: a shape plan.
244 * @font: a font.
245 * @buffer: a buffer.
246 * @features: (array length=num_features):
247 * @num_features:
251 * Return value:
253 * Since: 1.0
255 hb_bool_t
256 hb_shape_plan_execute (hb_shape_plan_t *shape_plan,
257 hb_font_t *font,
258 hb_buffer_t *buffer,
259 const hb_feature_t *features,
260 unsigned int num_features)
262 if (unlikely (hb_object_is_inert (shape_plan) ||
263 hb_object_is_inert (font) ||
264 hb_object_is_inert (buffer)))
265 return false;
267 assert (shape_plan->face == font->face);
268 assert (hb_segment_properties_equal (&shape_plan->props, &buffer->props));
270 #define HB_SHAPER_EXECUTE(shaper) \
271 HB_STMT_START { \
272 return HB_SHAPER_DATA (shaper, shape_plan) && \
273 hb_##shaper##_shaper_font_data_ensure (font) && \
274 _hb_##shaper##_shape (shape_plan, font, buffer, features, num_features); \
275 } HB_STMT_END
277 if (0)
279 #define HB_SHAPER_IMPLEMENT(shaper) \
280 else if (shape_plan->shaper_func == _hb_##shaper##_shape) \
281 HB_SHAPER_EXECUTE (shaper);
282 #include "hb-shaper-list.hh"
283 #undef HB_SHAPER_IMPLEMENT
285 #undef HB_SHAPER_EXECUTE
287 return false;
292 * caching
295 #if 0
296 static unsigned int
297 hb_shape_plan_hash (const hb_shape_plan_t *shape_plan)
299 return hb_segment_properties_hash (&shape_plan->props) +
300 shape_plan->default_shaper_list ? 0 : (intptr_t) shape_plan->shaper_func;
302 #endif
304 /* TODO no user-feature caching for now. */
305 struct hb_shape_plan_proposal_t
307 const hb_segment_properties_t props;
308 const char * const *shaper_list;
309 hb_shape_func_t *shaper_func;
312 static hb_bool_t
313 hb_shape_plan_matches (const hb_shape_plan_t *shape_plan,
314 const hb_shape_plan_proposal_t *proposal)
316 return hb_segment_properties_equal (&shape_plan->props, &proposal->props) &&
317 ((shape_plan->default_shaper_list && proposal->shaper_list == NULL) ||
318 (shape_plan->shaper_func == proposal->shaper_func));
322 * hb_shape_plan_create_cached:
323 * @face:
324 * @props:
325 * @user_features: (array length=num_user_features):
326 * @num_user_features:
327 * @shaper_list: (array zero-terminated=1):
331 * Return value: (transfer full):
333 * Since: 1.0
335 hb_shape_plan_t *
336 hb_shape_plan_create_cached (hb_face_t *face,
337 const hb_segment_properties_t *props,
338 const hb_feature_t *user_features,
339 unsigned int num_user_features,
340 const char * const *shaper_list)
342 if (num_user_features)
343 return hb_shape_plan_create (face, props, user_features, num_user_features, shaper_list);
345 hb_shape_plan_proposal_t proposal = {
346 *props,
347 shaper_list,
348 NULL
351 if (shaper_list) {
352 /* Choose shaper. Adapted from hb_shape_plan_plan(). */
353 #define HB_SHAPER_PLAN(shaper) \
354 HB_STMT_START { \
355 if (hb_##shaper##_shaper_face_data_ensure (face)) \
356 proposal.shaper_func = _hb_##shaper##_shape; \
357 } HB_STMT_END
359 for (const char * const *shaper_item = shaper_list; *shaper_item; shaper_item++)
360 if (0)
362 #define HB_SHAPER_IMPLEMENT(shaper) \
363 else if (0 == strcmp (*shaper_item, #shaper)) \
364 HB_SHAPER_PLAN (shaper);
365 #include "hb-shaper-list.hh"
366 #undef HB_SHAPER_IMPLEMENT
368 #undef HB_SHAPER_PLAN
370 if (unlikely (!proposal.shaper_list))
371 return hb_shape_plan_get_empty ();
375 retry:
376 hb_face_t::plan_node_t *cached_plan_nodes = (hb_face_t::plan_node_t *) hb_atomic_ptr_get (&face->shape_plans);
377 for (hb_face_t::plan_node_t *node = cached_plan_nodes; node; node = node->next)
378 if (hb_shape_plan_matches (node->shape_plan, &proposal))
379 return hb_shape_plan_reference (node->shape_plan);
381 /* Not found. */
383 hb_shape_plan_t *shape_plan = hb_shape_plan_create (face, props, user_features, num_user_features, shaper_list);
385 hb_face_t::plan_node_t *node = (hb_face_t::plan_node_t *) calloc (1, sizeof (hb_face_t::plan_node_t));
386 if (unlikely (!node))
387 return shape_plan;
389 node->shape_plan = shape_plan;
390 node->next = cached_plan_nodes;
392 if (!hb_atomic_ptr_cmpexch (&face->shape_plans, cached_plan_nodes, node)) {
393 hb_shape_plan_destroy (shape_plan);
394 free (node);
395 goto retry;
398 /* Release our reference on face. */
399 hb_face_destroy (face);
401 return hb_shape_plan_reference (shape_plan);
405 * hb_shape_plan_get_shaper:
406 * @shape_plan: a shape plan.
410 * Return value: (transfer none):
412 * Since: 1.0
414 const char *
415 hb_shape_plan_get_shaper (hb_shape_plan_t *shape_plan)
417 return shape_plan->shaper_name;