blob: 6eeba2b3d1f5826406ea272f0fdede9c77a08a0b [file] [log] [blame]
Behdad Esfahbod027857d2012-07-26 17:34:25 -04001/*
2 * Copyright © 2012 Google, Inc.
3 *
4 * This is part of HarfBuzz, a text shaping library.
5 *
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.
11 *
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.
17 *
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.
23 *
24 * Google Author(s): Behdad Esfahbod
25 */
26
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -040027#include "hb-private.hh"
28#include "hb-debug.hh"
Behdad Esfahbod027857d2012-07-26 17:34:25 -040029#include "hb-shape-plan-private.hh"
Behdad Esfahbod5b95c142012-07-26 23:46:53 -040030#include "hb-shaper-private.hh"
Behdad Esfahbod027857d2012-07-26 17:34:25 -040031#include "hb-font-private.hh"
Behdad Esfahbod7abddbb2013-02-15 07:46:57 -050032#include "hb-buffer-private.hh"
Behdad Esfahbod027857d2012-07-26 17:34:25 -040033
Behdad Esfahbodbc3d0dc2014-08-12 16:49:18 -040034
Behdad Esfahbod4c8ac4f2012-08-08 17:44:19 -040035static void
Behdad Esfahbod5b95c142012-07-26 23:46:53 -040036hb_shape_plan_plan (hb_shape_plan_t *shape_plan,
37 const hb_feature_t *user_features,
38 unsigned int num_user_features,
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -070039 const int *coords,
40 unsigned int num_coords,
Behdad Esfahbod5b95c142012-07-26 23:46:53 -040041 const char * const *shaper_list)
42{
Behdad Esfahbodbc3d0dc2014-08-12 16:49:18 -040043 DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan,
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -070044 "num_features=%d num_coords=%d shaper_list=%p",
Behdad Esfahbodbc3d0dc2014-08-12 16:49:18 -040045 num_user_features,
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -070046 num_coords,
Behdad Esfahbodbc3d0dc2014-08-12 16:49:18 -040047 shaper_list);
48
Behdad Esfahbod5b95c142012-07-26 23:46:53 -040049 const hb_shaper_pair_t *shapers = _hb_shapers_get ();
Behdad Esfahbod5b95c142012-07-26 23:46:53 -040050
51#define HB_SHAPER_PLAN(shaper) \
52 HB_STMT_START { \
Behdad Esfahbodf47b9212013-12-02 05:57:27 -050053 if (hb_##shaper##_shaper_face_data_ensure (shape_plan->face_unsafe)) { \
Behdad Esfahbod3b7c4e22012-07-27 03:12:23 -040054 HB_SHAPER_DATA (shaper, shape_plan) = \
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -070055 HB_SHAPER_DATA_CREATE_FUNC (shaper, shape_plan) (shape_plan, \
56 user_features, num_user_features, \
57 coords, num_coords); \
Behdad Esfahbod3b7c4e22012-07-27 03:12:23 -040058 shape_plan->shaper_func = _hb_##shaper##_shape; \
Behdad Esfahbodf9edd5d2012-11-16 13:23:37 -080059 shape_plan->shaper_name = #shaper; \
Behdad Esfahbod3b7c4e22012-07-27 03:12:23 -040060 return; \
Behdad Esfahbod5b95c142012-07-26 23:46:53 -040061 } \
62 } HB_STMT_END
63
64 if (likely (!shaper_list)) {
65 for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++)
66 if (0)
67 ;
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
73 } else {
74 for (; *shaper_list; shaper_list++)
75 if (0)
76 ;
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
82 }
83
84#undef HB_SHAPER_PLAN
85}
86
Behdad Esfahbod027857d2012-07-26 17:34:25 -040087
88/*
89 * hb_shape_plan_t
90 */
91
Behdad Esfahbod288f2892013-09-06 15:40:22 -040092/**
Behdad Esfahbod085d4292013-09-12 17:14:33 -040093 * hb_shape_plan_create: (Xconstructor)
Behdad Esfahbod288f2892013-09-06 15:40:22 -040094 * @face:
95 * @props:
96 * @user_features: (array length=num_user_features):
97 * @num_user_features:
98 * @shaper_list: (array zero-terminated=1):
99 *
100 *
101 *
102 * Return value: (transfer full):
103 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200104 * Since: 0.9.7
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400105 **/
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400106hb_shape_plan_t *
107hb_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)
112{
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700113 return hb_shape_plan_create2 (face, props,
114 user_features, num_user_features,
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200115 nullptr, 0,
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700116 shaper_list);
117}
118
119hb_shape_plan_t *
120hb_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)
127{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200128 DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr,
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700129 "face=%p num_features=%d num_coords=%d shaper_list=%p",
Behdad Esfahboddc9aba62014-08-12 17:14:36 -0400130 face,
Behdad Esfahbodbc3d0dc2014-08-12 16:49:18 -0400131 num_user_features,
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700132 num_coords,
Behdad Esfahbodbc3d0dc2014-08-12 16:49:18 -0400133 shaper_list);
134
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400135 hb_shape_plan_t *shape_plan;
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200136 hb_feature_t *features = nullptr;
137 int *coords = nullptr;
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400138
139 if (unlikely (!face))
140 face = hb_face_get_empty ();
Behdad Esfahbodeb0bf3a2014-08-06 15:36:41 -0400141 if (unlikely (!props))
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400142 return hb_shape_plan_get_empty ();
Behdad Esfahboda5efaac2015-10-02 08:02:29 +0100143 if (num_user_features && !(features = (hb_feature_t *) calloc (num_user_features, sizeof (hb_feature_t))))
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400144 return hb_shape_plan_get_empty ();
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700145 if (num_coords && !(coords = (int *) calloc (num_coords, sizeof (int))))
146 {
147 free (features);
148 return hb_shape_plan_get_empty ();
149 }
150 if (!(shape_plan = hb_object_create<hb_shape_plan_t> ()))
151 {
152 free (coords);
Jonathan Kewca8d96c2013-12-02 05:22:00 -0500153 free (features);
154 return hb_shape_plan_get_empty ();
155 }
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400156
Behdad Esfahboddac86022014-06-03 17:57:00 -0400157 assert (props->direction != HB_DIRECTION_INVALID);
158
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400159 hb_face_make_immutable (face);
Behdad Esfahbod68af14d2017-08-09 17:09:21 -0700160 shape_plan->default_shaper_list = !shaper_list;
Behdad Esfahbodf47b9212013-12-02 05:57:27 -0500161 shape_plan->face_unsafe = face;
Behdad Esfahbod5b95c142012-07-26 23:46:53 -0400162 shape_plan->props = *props;
Jonathan Kewca8d96c2013-12-02 05:22:00 -0500163 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));
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700167 shape_plan->num_coords = num_coords;
168 shape_plan->coords = coords;
169 if (num_coords)
170 memcpy (coords, orig_coords, num_coords * sizeof (int));
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400171
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700172 hb_shape_plan_plan (shape_plan,
173 user_features, num_user_features,
174 coords, num_coords,
175 shaper_list);
Behdad Esfahbodbd26b4d2012-07-26 22:05:39 -0400176
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400177 return shape_plan;
178}
179
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400180/**
181 * hb_shape_plan_get_empty:
182 *
183 *
184 *
185 * Return value: (transfer full):
186 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200187 * Since: 0.9.7
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400188 **/
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400189hb_shape_plan_t *
190hb_shape_plan_get_empty (void)
191{
192 static const hb_shape_plan_t _hb_shape_plan_nil = {
193 HB_OBJECT_HEADER_STATIC,
Behdad Esfahbodea278d32012-07-27 02:12:28 -0400194
Behdad Esfahbodef6e9ce2012-07-29 21:35:22 -0400195 true, /* default_shaper_list */
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200196 nullptr, /* face */
Behdad Esfahbodf3064102012-11-15 18:39:46 -0800197 HB_SEGMENT_PROPERTIES_DEFAULT, /* props */
Behdad Esfahbodea278d32012-07-27 02:12:28 -0400198
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200199 nullptr, /* shaper_func */
200 nullptr, /* shaper_name */
Behdad Esfahbodea278d32012-07-27 02:12:28 -0400201
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200202 nullptr, /* user_features */
Jonathan Kewca8d96c2013-12-02 05:22:00 -0500203 0, /* num_user_featurs */
204
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200205 nullptr, /* coords */
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700206 0, /* num_coords */
207
Behdad Esfahbodea278d32012-07-27 02:12:28 -0400208 {
209#define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
210#include "hb-shaper-list.hh"
211#undef HB_SHAPER_IMPLEMENT
212 }
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400213 };
214
215 return const_cast<hb_shape_plan_t *> (&_hb_shape_plan_nil);
216}
217
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400218/**
219 * hb_shape_plan_reference: (skip)
220 * @shape_plan: a shape plan.
221 *
222 *
223 *
224 * Return value: (transfer full):
225 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200226 * Since: 0.9.7
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400227 **/
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400228hb_shape_plan_t *
229hb_shape_plan_reference (hb_shape_plan_t *shape_plan)
230{
231 return hb_object_reference (shape_plan);
232}
233
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400234/**
235 * hb_shape_plan_destroy: (skip)
236 * @shape_plan: a shape plan.
237 *
238 *
239 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200240 * Since: 0.9.7
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400241 **/
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400242void
243hb_shape_plan_destroy (hb_shape_plan_t *shape_plan)
244{
245 if (!hb_object_destroy (shape_plan)) return;
246
247#define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, shape_plan);
248#include "hb-shaper-list.hh"
249#undef HB_SHAPER_IMPLEMENT
250
Jonathan Kewca8d96c2013-12-02 05:22:00 -0500251 free (shape_plan->user_features);
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700252 free (shape_plan->coords);
Behdad Esfahbod46ee1082012-08-03 18:21:13 -0700253
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400254 free (shape_plan);
255}
Behdad Esfahbodc32c0962012-07-27 01:13:53 -0400256
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400257/**
258 * hb_shape_plan_set_user_data: (skip)
259 * @shape_plan: a shape plan.
260 * @key:
261 * @data:
262 * @destroy:
263 * @replace:
264 *
265 *
266 *
267 * Return value:
268 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200269 * Since: 0.9.7
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400270 **/
Behdad Esfahbodf3064102012-11-15 18:39:46 -0800271hb_bool_t
272hb_shape_plan_set_user_data (hb_shape_plan_t *shape_plan,
273 hb_user_data_key_t *key,
274 void * data,
275 hb_destroy_func_t destroy,
276 hb_bool_t replace)
277{
278 return hb_object_set_user_data (shape_plan, key, data, destroy, replace);
279}
280
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400281/**
282 * hb_shape_plan_get_user_data: (skip)
283 * @shape_plan: a shape plan.
284 * @key:
285 *
286 *
287 *
288 * Return value: (transfer none):
289 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200290 * Since: 0.9.7
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400291 **/
Behdad Esfahbodf3064102012-11-15 18:39:46 -0800292void *
293hb_shape_plan_get_user_data (hb_shape_plan_t *shape_plan,
294 hb_user_data_key_t *key)
295{
296 return hb_object_get_user_data (shape_plan, key);
297}
298
Behdad Esfahbodc32c0962012-07-27 01:13:53 -0400299
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400300/**
301 * hb_shape_plan_execute:
302 * @shape_plan: a shape plan.
303 * @font: a font.
304 * @buffer: a buffer.
305 * @features: (array length=num_features):
306 * @num_features:
307 *
308 *
309 *
310 * Return value:
311 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200312 * Since: 0.9.7
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400313 **/
Behdad Esfahbodc32c0962012-07-27 01:13:53 -0400314hb_bool_t
Behdad Esfahbodf3064102012-11-15 18:39:46 -0800315hb_shape_plan_execute (hb_shape_plan_t *shape_plan,
Behdad Esfahbodc32c0962012-07-27 01:13:53 -0400316 hb_font_t *font,
317 hb_buffer_t *buffer,
318 const hb_feature_t *features,
319 unsigned int num_features)
320{
Behdad Esfahbodbc3d0dc2014-08-12 16:49:18 -0400321 DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan,
Khaled Hosny3d976d22016-08-27 16:38:42 +0300322 "num_features=%d shaper_func=%p, shaper_name=%s",
Behdad Esfahbodbc3d0dc2014-08-12 16:49:18 -0400323 num_features,
Khaled Hosny3d976d22016-08-27 16:38:42 +0300324 shape_plan->shaper_func,
325 shape_plan->shaper_name);
Behdad Esfahbodbc3d0dc2014-08-12 16:49:18 -0400326
Behdad Esfahbod26ba4d12015-10-02 07:25:52 +0100327 if (unlikely (!buffer->len))
328 return true;
329
330 assert (!hb_object_is_inert (buffer));
331 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE);
332
333 if (unlikely (hb_object_is_inert (shape_plan)))
Behdad Esfahbodc32c0962012-07-27 01:13:53 -0400334 return false;
335
Behdad Esfahbodf47b9212013-12-02 05:57:27 -0500336 assert (shape_plan->face_unsafe == font->face);
Behdad Esfahbod7abddbb2013-02-15 07:46:57 -0500337 assert (hb_segment_properties_equal (&shape_plan->props, &buffer->props));
338
Behdad Esfahbodc32c0962012-07-27 01:13:53 -0400339#define HB_SHAPER_EXECUTE(shaper) \
340 HB_STMT_START { \
Behdad Esfahbod3b7c4e22012-07-27 03:12:23 -0400341 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); \
Behdad Esfahbodc32c0962012-07-27 01:13:53 -0400344 } HB_STMT_END
345
Behdad Esfahbodc5b668f2012-07-27 02:49:39 -0400346 if (0)
347 ;
Behdad Esfahbodc32c0962012-07-27 01:13:53 -0400348#define HB_SHAPER_IMPLEMENT(shaper) \
Behdad Esfahbodc5b668f2012-07-27 02:49:39 -0400349 else if (shape_plan->shaper_func == _hb_##shaper##_shape) \
350 HB_SHAPER_EXECUTE (shaper);
Behdad Esfahbodc32c0962012-07-27 01:13:53 -0400351#include "hb-shaper-list.hh"
352#undef HB_SHAPER_IMPLEMENT
353
354#undef HB_SHAPER_EXECUTE
355
356 return false;
357}
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400358
359
360/*
361 * caching
362 */
363
364#if 0
Behdad Esfahbod801298b2012-08-08 14:26:36 -0400365static unsigned int
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400366hb_shape_plan_hash (const hb_shape_plan_t *shape_plan)
367{
368 return hb_segment_properties_hash (&shape_plan->props) +
Behdad Esfahbod801298b2012-08-08 14:26:36 -0400369 shape_plan->default_shaper_list ? 0 : (intptr_t) shape_plan->shaper_func;
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400370}
371#endif
372
Jonathan Kewca8d96c2013-12-02 05:22:00 -0500373/* 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.
377 */
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400378struct hb_shape_plan_proposal_t
379{
380 const hb_segment_properties_t props;
381 const char * const *shaper_list;
Jonathan Kewca8d96c2013-12-02 05:22:00 -0500382 const hb_feature_t *user_features;
383 unsigned int num_user_features;
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700384 const int *coords;
385 unsigned int num_coords;
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400386 hb_shape_func_t *shaper_func;
387};
388
Jonathan Kewca8d96c2013-12-02 05:22:00 -0500389static inline hb_bool_t
390hb_shape_plan_user_features_match (const hb_shape_plan_t *shape_plan,
391 const hb_shape_plan_proposal_t *proposal)
392{
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700393 if (proposal->num_user_features != shape_plan->num_user_features)
394 return false;
Jonathan Kewca8d96c2013-12-02 05:22:00 -0500395 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 ||
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700399 proposal->user_features[i].end != shape_plan->user_features[i].end)
400 return false;
401 return true;
402}
403
404static inline hb_bool_t
405hb_shape_plan_coords_match (const hb_shape_plan_t *shape_plan,
406 const hb_shape_plan_proposal_t *proposal)
407{
408 if (proposal->num_coords != shape_plan->num_coords)
409 return false;
410 for (unsigned int i = 0, n = proposal->num_coords; i < n; i++)
411 if (proposal->coords[i] != shape_plan->coords[i])
412 return false;
Jonathan Kewca8d96c2013-12-02 05:22:00 -0500413 return true;
414}
415
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400416static hb_bool_t
417hb_shape_plan_matches (const hb_shape_plan_t *shape_plan,
418 const hb_shape_plan_proposal_t *proposal)
419{
420 return hb_segment_properties_equal (&shape_plan->props, &proposal->props) &&
Jonathan Kewca8d96c2013-12-02 05:22:00 -0500421 hb_shape_plan_user_features_match (shape_plan, proposal) &&
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700422 hb_shape_plan_coords_match (shape_plan, proposal) &&
Behdad Esfahbod68af14d2017-08-09 17:09:21 -0700423 ((shape_plan->default_shaper_list && !proposal->shaper_list) ||
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400424 (shape_plan->shaper_func == proposal->shaper_func));
425}
426
Jonathan Kewca8d96c2013-12-02 05:22:00 -0500427static inline hb_bool_t
428hb_non_global_user_features_present (const hb_feature_t *user_features,
429 unsigned int num_user_features)
430{
Chris Petersonaacca372017-04-17 23:25:24 -0700431 while (num_user_features) {
Jonathan Kewca8d96c2013-12-02 05:22:00 -0500432 if (user_features->start != 0 || user_features->end != (unsigned int) -1)
433 return true;
Chris Petersonaacca372017-04-17 23:25:24 -0700434 num_user_features--;
435 user_features++;
436 }
Jonathan Kewca8d96c2013-12-02 05:22:00 -0500437 return false;
438}
439
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700440static inline hb_bool_t
441hb_coords_present (const int *coords,
442 unsigned int num_coords)
443{
444 return num_coords != 0;
445}
446
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400447/**
448 * hb_shape_plan_create_cached:
449 * @face:
450 * @props:
451 * @user_features: (array length=num_user_features):
452 * @num_user_features:
453 * @shaper_list: (array zero-terminated=1):
454 *
455 *
456 *
457 * Return value: (transfer full):
458 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200459 * Since: 0.9.7
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400460 **/
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400461hb_shape_plan_t *
462hb_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)
467{
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700468 return hb_shape_plan_create_cached2 (face, props,
469 user_features, num_user_features,
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200470 nullptr, 0,
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700471 shaper_list);
472}
473
474hb_shape_plan_t *
475hb_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,
479 const int *coords,
480 unsigned int num_coords,
481 const char * const *shaper_list)
482{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200483 DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr,
Behdad Esfahboddc9aba62014-08-12 17:14:36 -0400484 "face=%p num_features=%d shaper_list=%p",
485 face,
Behdad Esfahbodbc3d0dc2014-08-12 16:49:18 -0400486 num_user_features,
487 shaper_list);
488
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400489 hb_shape_plan_proposal_t proposal = {
490 *props,
491 shaper_list,
Jonathan Kewca8d96c2013-12-02 05:22:00 -0500492 user_features,
493 num_user_features,
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200494 nullptr
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400495 };
496
497 if (shaper_list) {
Behdad Esfahbode956c652014-08-12 17:03:27 -0400498 /* Choose shaper. Adapted from hb_shape_plan_plan().
499 * Must choose shaper exactly the same way as that function. */
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400500 for (const char * const *shaper_item = shaper_list; *shaper_item; shaper_item++)
501 if (0)
502 ;
503#define HB_SHAPER_IMPLEMENT(shaper) \
Behdad Esfahbode956c652014-08-12 17:03:27 -0400504 else if (0 == strcmp (*shaper_item, #shaper) && \
505 hb_##shaper##_shaper_face_data_ensure (face)) \
506 { \
507 proposal.shaper_func = _hb_##shaper##_shape; \
508 break; \
509 }
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400510#include "hb-shaper-list.hh"
511#undef HB_SHAPER_IMPLEMENT
512
Behdad Esfahbod30eed752014-08-12 17:15:09 -0400513 if (unlikely (!proposal.shaper_func))
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400514 return hb_shape_plan_get_empty ();
515 }
516
517
518retry:
519 hb_face_t::plan_node_t *cached_plan_nodes = (hb_face_t::plan_node_t *) hb_atomic_ptr_get (&face->shape_plans);
jfkthame19e77e02017-10-02 13:08:49 +0100520
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))
525 {
526 DEBUG_MSG_FUNC (SHAPE_PLAN, node->shape_plan, "fulfilled from cache");
527 return hb_shape_plan_reference (node->shape_plan);
528 }
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400529
530 /* Not found. */
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700531 hb_shape_plan_t *shape_plan = hb_shape_plan_create2 (face, props,
532 user_features, num_user_features,
533 coords, num_coords,
534 shaper_list);
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400535
Behdad Esfahbodeb0bf3a2014-08-06 15:36:41 -0400536 /* Don't add to the cache if face is inert. */
537 if (unlikely (hb_object_is_inert (face)))
538 return shape_plan;
539
Jonathan Kewca8d96c2013-12-02 05:22:00 -0500540 /* Don't add the plan to the cache if there were user features with non-global ranges */
Jonathan Kewca8d96c2013-12-02 05:22:00 -0500541 if (hb_non_global_user_features_present (user_features, num_user_features))
542 return shape_plan;
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700543 /* Don't add the plan to the cache if there were variation coordinates XXX Fix me. */
544 if (hb_coords_present (coords, num_coords))
545 return shape_plan;
Jonathan Kewca8d96c2013-12-02 05:22:00 -0500546
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400547 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))
549 return shape_plan;
550
551 node->shape_plan = shape_plan;
552 node->next = cached_plan_nodes;
553
554 if (!hb_atomic_ptr_cmpexch (&face->shape_plans, cached_plan_nodes, node)) {
555 hb_shape_plan_destroy (shape_plan);
556 free (node);
557 goto retry;
558 }
Behdad Esfahboddc9aba62014-08-12 17:14:36 -0400559 DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan, "inserted into cache");
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400560
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -0400561 return hb_shape_plan_reference (shape_plan);
562}
Behdad Esfahbodf9edd5d2012-11-16 13:23:37 -0800563
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400564/**
565 * hb_shape_plan_get_shaper:
566 * @shape_plan: a shape plan.
567 *
568 *
569 *
570 * Return value: (transfer none):
571 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200572 * Since: 0.9.7
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400573 **/
Behdad Esfahbodf9edd5d2012-11-16 13:23:37 -0800574const char *
575hb_shape_plan_get_shaper (hb_shape_plan_t *shape_plan)
576{
Behdad Esfahbod7c5b7fe2012-11-22 14:15:08 -0500577 return shape_plan->shaper_name;
Behdad Esfahbodf9edd5d2012-11-16 13:23:37 -0800578}