Add a Notification Settings Button to all web notifications behind the web platform...
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-shape-plan.cc
blobd2f293d69dedc5a3a34239b0166e2fb15bd57345
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"
33 #ifndef HB_DEBUG_SHAPE_PLAN
34 #define HB_DEBUG_SHAPE_PLAN (HB_DEBUG+0)
35 #endif
38 #define HB_SHAPER_IMPLEMENT(shaper) \
39 HB_SHAPER_DATA_ENSURE_DECLARE(shaper, face) \
40 HB_SHAPER_DATA_ENSURE_DECLARE(shaper, font)
41 #include "hb-shaper-list.hh"
42 #undef HB_SHAPER_IMPLEMENT
45 static void
46 hb_shape_plan_plan (hb_shape_plan_t *shape_plan,
47 const hb_feature_t *user_features,
48 unsigned int num_user_features,
49 const char * const *shaper_list)
51 DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan,
52 "num_features=%d shaper_list=%p",
53 num_user_features,
54 shaper_list);
56 const hb_shaper_pair_t *shapers = _hb_shapers_get ();
58 #define HB_SHAPER_PLAN(shaper) \
59 HB_STMT_START { \
60 if (hb_##shaper##_shaper_face_data_ensure (shape_plan->face_unsafe)) { \
61 HB_SHAPER_DATA (shaper, shape_plan) = \
62 HB_SHAPER_DATA_CREATE_FUNC (shaper, shape_plan) (shape_plan, user_features, num_user_features); \
63 shape_plan->shaper_func = _hb_##shaper##_shape; \
64 shape_plan->shaper_name = #shaper; \
65 return; \
66 } \
67 } HB_STMT_END
69 if (likely (!shaper_list)) {
70 for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++)
71 if (0)
73 #define HB_SHAPER_IMPLEMENT(shaper) \
74 else if (shapers[i].func == _hb_##shaper##_shape) \
75 HB_SHAPER_PLAN (shaper);
76 #include "hb-shaper-list.hh"
77 #undef HB_SHAPER_IMPLEMENT
78 } else {
79 for (; *shaper_list; shaper_list++)
80 if (0)
82 #define HB_SHAPER_IMPLEMENT(shaper) \
83 else if (0 == strcmp (*shaper_list, #shaper)) \
84 HB_SHAPER_PLAN (shaper);
85 #include "hb-shaper-list.hh"
86 #undef HB_SHAPER_IMPLEMENT
89 #undef HB_SHAPER_PLAN
94 * hb_shape_plan_t
97 /**
98 * hb_shape_plan_create: (Xconstructor)
99 * @face:
100 * @props:
101 * @user_features: (array length=num_user_features):
102 * @num_user_features:
103 * @shaper_list: (array zero-terminated=1):
107 * Return value: (transfer full):
109 * Since: 0.9.7
111 hb_shape_plan_t *
112 hb_shape_plan_create (hb_face_t *face,
113 const hb_segment_properties_t *props,
114 const hb_feature_t *user_features,
115 unsigned int num_user_features,
116 const char * const *shaper_list)
118 DEBUG_MSG_FUNC (SHAPE_PLAN, NULL,
119 "face=%p num_features=%d shaper_list=%p",
120 face,
121 num_user_features,
122 shaper_list);
124 hb_shape_plan_t *shape_plan;
125 hb_feature_t *features = NULL;
127 if (unlikely (!face))
128 face = hb_face_get_empty ();
129 if (unlikely (!props))
130 return hb_shape_plan_get_empty ();
131 if (num_user_features && !(features = (hb_feature_t *) malloc (num_user_features * sizeof (hb_feature_t))))
132 return hb_shape_plan_get_empty ();
133 if (!(shape_plan = hb_object_create<hb_shape_plan_t> ())) {
134 free (features);
135 return hb_shape_plan_get_empty ();
138 assert (props->direction != HB_DIRECTION_INVALID);
140 hb_face_make_immutable (face);
141 shape_plan->default_shaper_list = shaper_list == NULL;
142 shape_plan->face_unsafe = face;
143 shape_plan->props = *props;
144 shape_plan->num_user_features = num_user_features;
145 shape_plan->user_features = features;
146 if (num_user_features)
147 memcpy (features, user_features, num_user_features * sizeof (hb_feature_t));
149 hb_shape_plan_plan (shape_plan, user_features, num_user_features, shaper_list);
151 return shape_plan;
155 * hb_shape_plan_get_empty:
159 * Return value: (transfer full):
161 * Since: 0.9.7
163 hb_shape_plan_t *
164 hb_shape_plan_get_empty (void)
166 static const hb_shape_plan_t _hb_shape_plan_nil = {
167 HB_OBJECT_HEADER_STATIC,
169 true, /* default_shaper_list */
170 NULL, /* face */
171 HB_SEGMENT_PROPERTIES_DEFAULT, /* props */
173 NULL, /* shaper_func */
174 NULL, /* shaper_name */
176 NULL, /* user_features */
177 0, /* num_user_featurs */
180 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
181 #include "hb-shaper-list.hh"
182 #undef HB_SHAPER_IMPLEMENT
186 return const_cast<hb_shape_plan_t *> (&_hb_shape_plan_nil);
190 * hb_shape_plan_reference: (skip)
191 * @shape_plan: a shape plan.
195 * Return value: (transfer full):
197 * Since: 0.9.7
199 hb_shape_plan_t *
200 hb_shape_plan_reference (hb_shape_plan_t *shape_plan)
202 return hb_object_reference (shape_plan);
206 * hb_shape_plan_destroy: (skip)
207 * @shape_plan: a shape plan.
211 * Since: 0.9.7
213 void
214 hb_shape_plan_destroy (hb_shape_plan_t *shape_plan)
216 if (!hb_object_destroy (shape_plan)) return;
218 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, shape_plan);
219 #include "hb-shaper-list.hh"
220 #undef HB_SHAPER_IMPLEMENT
222 free (shape_plan->user_features);
224 free (shape_plan);
228 * hb_shape_plan_set_user_data: (skip)
229 * @shape_plan: a shape plan.
230 * @key:
231 * @data:
232 * @destroy:
233 * @replace:
237 * Return value:
239 * Since: 0.9.7
241 hb_bool_t
242 hb_shape_plan_set_user_data (hb_shape_plan_t *shape_plan,
243 hb_user_data_key_t *key,
244 void * data,
245 hb_destroy_func_t destroy,
246 hb_bool_t replace)
248 return hb_object_set_user_data (shape_plan, key, data, destroy, replace);
252 * hb_shape_plan_get_user_data: (skip)
253 * @shape_plan: a shape plan.
254 * @key:
258 * Return value: (transfer none):
260 * Since: 0.9.7
262 void *
263 hb_shape_plan_get_user_data (hb_shape_plan_t *shape_plan,
264 hb_user_data_key_t *key)
266 return hb_object_get_user_data (shape_plan, key);
271 * hb_shape_plan_execute:
272 * @shape_plan: a shape plan.
273 * @font: a font.
274 * @buffer: a buffer.
275 * @features: (array length=num_features):
276 * @num_features:
280 * Return value:
282 * Since: 0.9.7
284 hb_bool_t
285 hb_shape_plan_execute (hb_shape_plan_t *shape_plan,
286 hb_font_t *font,
287 hb_buffer_t *buffer,
288 const hb_feature_t *features,
289 unsigned int num_features)
291 DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan,
292 "num_features=%d shaper_func=%p",
293 num_features,
294 shape_plan->shaper_func);
296 if (unlikely (hb_object_is_inert (shape_plan) ||
297 hb_object_is_inert (buffer)))
298 return false;
300 assert (shape_plan->face_unsafe == font->face);
301 assert (hb_segment_properties_equal (&shape_plan->props, &buffer->props));
303 #define HB_SHAPER_EXECUTE(shaper) \
304 HB_STMT_START { \
305 return HB_SHAPER_DATA (shaper, shape_plan) && \
306 hb_##shaper##_shaper_font_data_ensure (font) && \
307 _hb_##shaper##_shape (shape_plan, font, buffer, features, num_features); \
308 } HB_STMT_END
310 if (0)
312 #define HB_SHAPER_IMPLEMENT(shaper) \
313 else if (shape_plan->shaper_func == _hb_##shaper##_shape) \
314 HB_SHAPER_EXECUTE (shaper);
315 #include "hb-shaper-list.hh"
316 #undef HB_SHAPER_IMPLEMENT
318 #undef HB_SHAPER_EXECUTE
320 return false;
325 * caching
328 #if 0
329 static unsigned int
330 hb_shape_plan_hash (const hb_shape_plan_t *shape_plan)
332 return hb_segment_properties_hash (&shape_plan->props) +
333 shape_plan->default_shaper_list ? 0 : (intptr_t) shape_plan->shaper_func;
335 #endif
337 /* User-feature caching is currently somewhat dumb:
338 * it only finds matches where the feature array is identical,
339 * not cases where the feature lists would be compatible for plan purposes
340 * but have different ranges, for example.
342 struct hb_shape_plan_proposal_t
344 const hb_segment_properties_t props;
345 const char * const *shaper_list;
346 const hb_feature_t *user_features;
347 unsigned int num_user_features;
348 hb_shape_func_t *shaper_func;
351 static inline hb_bool_t
352 hb_shape_plan_user_features_match (const hb_shape_plan_t *shape_plan,
353 const hb_shape_plan_proposal_t *proposal)
355 if (proposal->num_user_features != shape_plan->num_user_features) return false;
356 for (unsigned int i = 0, n = proposal->num_user_features; i < n; i++)
357 if (proposal->user_features[i].tag != shape_plan->user_features[i].tag ||
358 proposal->user_features[i].value != shape_plan->user_features[i].value ||
359 proposal->user_features[i].start != shape_plan->user_features[i].start ||
360 proposal->user_features[i].end != shape_plan->user_features[i].end) return false;
361 return true;
364 static hb_bool_t
365 hb_shape_plan_matches (const hb_shape_plan_t *shape_plan,
366 const hb_shape_plan_proposal_t *proposal)
368 return hb_segment_properties_equal (&shape_plan->props, &proposal->props) &&
369 hb_shape_plan_user_features_match (shape_plan, proposal) &&
370 ((shape_plan->default_shaper_list && proposal->shaper_list == NULL) ||
371 (shape_plan->shaper_func == proposal->shaper_func));
374 static inline hb_bool_t
375 hb_non_global_user_features_present (const hb_feature_t *user_features,
376 unsigned int num_user_features)
378 while (num_user_features)
379 if (user_features->start != 0 || user_features->end != (unsigned int) -1)
380 return true;
381 else
382 num_user_features--, user_features++;
383 return false;
387 * hb_shape_plan_create_cached:
388 * @face:
389 * @props:
390 * @user_features: (array length=num_user_features):
391 * @num_user_features:
392 * @shaper_list: (array zero-terminated=1):
396 * Return value: (transfer full):
398 * Since: 0.9.7
400 hb_shape_plan_t *
401 hb_shape_plan_create_cached (hb_face_t *face,
402 const hb_segment_properties_t *props,
403 const hb_feature_t *user_features,
404 unsigned int num_user_features,
405 const char * const *shaper_list)
407 DEBUG_MSG_FUNC (SHAPE_PLAN, NULL,
408 "face=%p num_features=%d shaper_list=%p",
409 face,
410 num_user_features,
411 shaper_list);
413 hb_shape_plan_proposal_t proposal = {
414 *props,
415 shaper_list,
416 user_features,
417 num_user_features,
418 NULL
421 if (shaper_list) {
422 /* Choose shaper. Adapted from hb_shape_plan_plan().
423 * Must choose shaper exactly the same way as that function. */
424 for (const char * const *shaper_item = shaper_list; *shaper_item; shaper_item++)
425 if (0)
427 #define HB_SHAPER_IMPLEMENT(shaper) \
428 else if (0 == strcmp (*shaper_item, #shaper) && \
429 hb_##shaper##_shaper_face_data_ensure (face)) \
431 proposal.shaper_func = _hb_##shaper##_shape; \
432 break; \
434 #include "hb-shaper-list.hh"
435 #undef HB_SHAPER_IMPLEMENT
437 if (unlikely (!proposal.shaper_func))
438 return hb_shape_plan_get_empty ();
442 retry:
443 hb_face_t::plan_node_t *cached_plan_nodes = (hb_face_t::plan_node_t *) hb_atomic_ptr_get (&face->shape_plans);
444 for (hb_face_t::plan_node_t *node = cached_plan_nodes; node; node = node->next)
445 if (hb_shape_plan_matches (node->shape_plan, &proposal))
447 DEBUG_MSG_FUNC (SHAPE_PLAN, node->shape_plan, "fulfilled from cache");
448 return hb_shape_plan_reference (node->shape_plan);
451 /* Not found. */
453 hb_shape_plan_t *shape_plan = hb_shape_plan_create (face, props, user_features, num_user_features, shaper_list);
455 /* Don't add to the cache if face is inert. */
456 if (unlikely (hb_object_is_inert (face)))
457 return shape_plan;
459 /* Don't add the plan to the cache if there were user features with non-global ranges */
461 if (hb_non_global_user_features_present (user_features, num_user_features))
462 return shape_plan;
464 hb_face_t::plan_node_t *node = (hb_face_t::plan_node_t *) calloc (1, sizeof (hb_face_t::plan_node_t));
465 if (unlikely (!node))
466 return shape_plan;
468 node->shape_plan = shape_plan;
469 node->next = cached_plan_nodes;
471 if (!hb_atomic_ptr_cmpexch (&face->shape_plans, cached_plan_nodes, node)) {
472 hb_shape_plan_destroy (shape_plan);
473 free (node);
474 goto retry;
476 DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan, "inserted into cache");
478 return hb_shape_plan_reference (shape_plan);
482 * hb_shape_plan_get_shaper:
483 * @shape_plan: a shape plan.
487 * Return value: (transfer none):
489 * Since: 0.9.7
491 const char *
492 hb_shape_plan_get_shaper (hb_shape_plan_t *shape_plan)
494 return shape_plan->shaper_name;