1/*
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
27#include "hb.hh"
28#include "hb-shape-plan.hh"
29#include "hb-shaper.hh"
30#include "hb-font.hh"
31#include "hb-buffer.hh"
32
33
34static void
35hb_shape_plan_plan (hb_shape_plan_t *shape_plan,
36 const hb_feature_t *user_features,
37 unsigned int num_user_features,
38 const int *coords,
39 unsigned int num_coords,
40 const char * const *shaper_list)
41{
42 DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan,
43 "num_features=%d num_coords=%d shaper_list=%p",
44 num_user_features,
45 num_coords,
46 shaper_list);
47
48 const hb_shaper_pair_t *shapers = _hb_shapers_get ();
49
50#define HB_SHAPER_PLAN(shaper) \
51 HB_STMT_START { \
52 if (hb_##shaper##_shaper_face_data_ensure (shape_plan->face_unsafe)) \
53 { \
54 /* XXX-MT-bug What happened to *ensure*ing this?!!!! */ \
55 HB_SHAPER_DATA (shaper, shape_plan).set_relaxed ( \
56 HB_SHAPER_DATA_CREATE_FUNC (shaper, shape_plan) (shape_plan, \
57 user_features, num_user_features, \
58 coords, num_coords)); \
59 shape_plan->shaper_func = _hb_##shaper##_shape; \
60 shape_plan->shaper_name = #shaper; \
61 return; \
62 } \
63 } HB_STMT_END
64
65 if (likely (!shaper_list)) {
66 for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++)
67 if (0)
68 ;
69#define HB_SHAPER_IMPLEMENT(shaper) \
70 else if (shapers[i].func == _hb_##shaper##_shape) \
71 HB_SHAPER_PLAN (shaper);
72#include "hb-shaper-list.hh"
73#undef HB_SHAPER_IMPLEMENT
74 } else {
75 for (; *shaper_list; shaper_list++)
76 if (0)
77 ;
78#define HB_SHAPER_IMPLEMENT(shaper) \
79 else if (0 == strcmp (*shaper_list, #shaper)) \
80 HB_SHAPER_PLAN (shaper);
81#include "hb-shaper-list.hh"
82#undef HB_SHAPER_IMPLEMENT
83 }
84
85#undef HB_SHAPER_PLAN
86}
87
88
89/*
90 * hb_shape_plan_t
91 */
92
93DEFINE_NULL_INSTANCE (hb_shape_plan_t) =
94{
95 HB_OBJECT_HEADER_STATIC,
96
97 true, /* default_shaper_list */
98 nullptr, /* face */
99 HB_SEGMENT_PROPERTIES_DEFAULT, /* props */
100
101 nullptr, /* shaper_func */
102 nullptr, /* shaper_name */
103
104 nullptr, /* user_features */
105 0, /* num_user_featurs */
106
107 nullptr, /* coords */
108 0, /* num_coords */
109
110 {
111#define HB_SHAPER_IMPLEMENT(shaper) HB_ATOMIC_PTR_INIT (HB_SHAPER_DATA_INVALID),
112#include "hb-shaper-list.hh"
113#undef HB_SHAPER_IMPLEMENT
114 },
115};
116
117
118/**
119 * hb_shape_plan_create: (Xconstructor)
120 * @face:
121 * @props:
122 * @user_features: (array length=num_user_features):
123 * @num_user_features:
124 * @shaper_list: (array zero-terminated=1):
125 *
126 *
127 *
128 * Return value: (transfer full):
129 *
130 * Since: 0.9.7
131 **/
132hb_shape_plan_t *
133hb_shape_plan_create (hb_face_t *face,
134 const hb_segment_properties_t *props,
135 const hb_feature_t *user_features,
136 unsigned int num_user_features,
137 const char * const *shaper_list)
138{
139 return hb_shape_plan_create2 (face, props,
140 user_features, num_user_features,
141 nullptr, 0,
142 shaper_list);
143}
144
145hb_shape_plan_t *
146hb_shape_plan_create2 (hb_face_t *face,
147 const hb_segment_properties_t *props,
148 const hb_feature_t *user_features,
149 unsigned int num_user_features,
150 const int *orig_coords,
151 unsigned int num_coords,
152 const char * const *shaper_list)
153{
154 DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr,
155 "face=%p num_features=%d num_coords=%d shaper_list=%p",
156 face,
157 num_user_features,
158 num_coords,
159 shaper_list);
160
161 hb_shape_plan_t *shape_plan;
162 hb_feature_t *features = nullptr;
163 int *coords = nullptr;
164
165 if (unlikely (!face))
166 face = hb_face_get_empty ();
167 if (unlikely (!props))
168 return hb_shape_plan_get_empty ();
169 if (num_user_features && !(features = (hb_feature_t *) calloc (num_user_features, sizeof (hb_feature_t))))
170 return hb_shape_plan_get_empty ();
171 if (num_coords && !(coords = (int *) calloc (num_coords, sizeof (int))))
172 {
173 free (features);
174 return hb_shape_plan_get_empty ();
175 }
176 if (!(shape_plan = hb_object_create<hb_shape_plan_t> ()))
177 {
178 free (coords);
179 free (features);
180 return hb_shape_plan_get_empty ();
181 }
182
183 assert (props->direction != HB_DIRECTION_INVALID);
184
185 hb_face_make_immutable (face);
186 shape_plan->default_shaper_list = !shaper_list;
187 shape_plan->face_unsafe = face;
188 shape_plan->props = *props;
189 shape_plan->num_user_features = num_user_features;
190 shape_plan->user_features = features;
191 if (num_user_features)
192 memcpy (features, user_features, num_user_features * sizeof (hb_feature_t));
193 shape_plan->num_coords = num_coords;
194 shape_plan->coords = coords;
195 if (num_coords)
196 memcpy (coords, orig_coords, num_coords * sizeof (int));
197
198 hb_shape_plan_plan (shape_plan,
199 user_features, num_user_features,
200 coords, num_coords,
201 shaper_list);
202
203 return shape_plan;
204}
205
206/**
207 * hb_shape_plan_get_empty:
208 *
209 *
210 *
211 * Return value: (transfer full):
212 *
213 * Since: 0.9.7
214 **/
215hb_shape_plan_t *
216hb_shape_plan_get_empty (void)
217{
218 return const_cast<hb_shape_plan_t *> (&Null(hb_shape_plan_t));
219}
220
221/**
222 * hb_shape_plan_reference: (skip)
223 * @shape_plan: a shape plan.
224 *
225 *
226 *
227 * Return value: (transfer full):
228 *
229 * Since: 0.9.7
230 **/
231hb_shape_plan_t *
232hb_shape_plan_reference (hb_shape_plan_t *shape_plan)
233{
234 return hb_object_reference (shape_plan);
235}
236
237/**
238 * hb_shape_plan_destroy: (skip)
239 * @shape_plan: a shape plan.
240 *
241 *
242 *
243 * Since: 0.9.7
244 **/
245void
246hb_shape_plan_destroy (hb_shape_plan_t *shape_plan)
247{
248 if (!hb_object_destroy (shape_plan)) return;
249
250#define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, shape_plan);
251#include "hb-shaper-list.hh"
252#undef HB_SHAPER_IMPLEMENT
253
254 free (shape_plan->user_features);
255 free (shape_plan->coords);
256
257 free (shape_plan);
258}
259
260/**
261 * hb_shape_plan_set_user_data: (skip)
262 * @shape_plan: a shape plan.
263 * @key:
264 * @data:
265 * @destroy:
266 * @replace:
267 *
268 *
269 *
270 * Return value:
271 *
272 * Since: 0.9.7
273 **/
274hb_bool_t
275hb_shape_plan_set_user_data (hb_shape_plan_t *shape_plan,
276 hb_user_data_key_t *key,
277 void * data,
278 hb_destroy_func_t destroy,
279 hb_bool_t replace)
280{
281 return hb_object_set_user_data (shape_plan, key, data, destroy, replace);
282}
283
284/**
285 * hb_shape_plan_get_user_data: (skip)
286 * @shape_plan: a shape plan.
287 * @key:
288 *
289 *
290 *
291 * Return value: (transfer none):
292 *
293 * Since: 0.9.7
294 **/
295void *
296hb_shape_plan_get_user_data (hb_shape_plan_t *shape_plan,
297 hb_user_data_key_t *key)
298{
299 return hb_object_get_user_data (shape_plan, key);
300}
301
302
303/**
304 * hb_shape_plan_execute:
305 * @shape_plan: a shape plan.
306 * @font: a font.
307 * @buffer: a buffer.
308 * @features: (array length=num_features):
309 * @num_features:
310 *
311 *
312 *
313 * Return value:
314 *
315 * Since: 0.9.7
316 **/
317hb_bool_t
318hb_shape_plan_execute (hb_shape_plan_t *shape_plan,
319 hb_font_t *font,
320 hb_buffer_t *buffer,
321 const hb_feature_t *features,
322 unsigned int num_features)
323{
324 DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan,
325 "num_features=%d shaper_func=%p, shaper_name=%s",
326 num_features,
327 shape_plan->shaper_func,
328 shape_plan->shaper_name);
329
330 if (unlikely (!buffer->len))
331 return true;
332
333 assert (!hb_object_is_inert (buffer));
334 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE);
335
336 if (unlikely (hb_object_is_inert (shape_plan)))
337 return false;
338
339 assert (shape_plan->face_unsafe == font->face);
340 assert (hb_segment_properties_equal (&shape_plan->props, &buffer->props));
341
342#define HB_SHAPER_EXECUTE(shaper) \
343 HB_STMT_START { \
344 return HB_SHAPER_DATA (shaper, shape_plan).get () && \
345 hb_##shaper##_shaper_font_data_ensure (font) && \
346 _hb_##shaper##_shape (shape_plan, font, buffer, features, num_features); \
347 } HB_STMT_END
348
349 if (0)
350 ;
351#define HB_SHAPER_IMPLEMENT(shaper) \
352 else if (shape_plan->shaper_func == _hb_##shaper##_shape) \
353 HB_SHAPER_EXECUTE (shaper);
354#include "hb-shaper-list.hh"
355#undef HB_SHAPER_IMPLEMENT
356
357#undef HB_SHAPER_EXECUTE
358
359 return false;
360}
361
362
363/*
364 * caching
365 */
366
367#if 0
368static unsigned int
369hb_shape_plan_hash (const hb_shape_plan_t *shape_plan)
370{
371 return hb_segment_properties_hash (&shape_plan->props) +
372 shape_plan->default_shaper_list ? 0 : (intptr_t) shape_plan->shaper_func;
373}
374#endif
375
376/* User-feature caching is currently somewhat dumb:
377 * it only finds matches where the feature array is identical,
378 * not cases where the feature lists would be compatible for plan purposes
379 * but have different ranges, for example.
380 */
381struct hb_shape_plan_proposal_t
382{
383 const hb_segment_properties_t props;
384 const char * const *shaper_list;
385 const hb_feature_t *user_features;
386 unsigned int num_user_features;
387 const int *coords;
388 unsigned int num_coords;
389 hb_shape_func_t *shaper_func;
390};
391
392static inline hb_bool_t
393hb_shape_plan_user_features_match (const hb_shape_plan_t *shape_plan,
394 const hb_shape_plan_proposal_t *proposal)
395{
396 if (proposal->num_user_features != shape_plan->num_user_features)
397 return false;
398 for (unsigned int i = 0, n = proposal->num_user_features; i < n; i++)
399 if (proposal->user_features[i].tag != shape_plan->user_features[i].tag ||
400 proposal->user_features[i].value != shape_plan->user_features[i].value ||
401 proposal->user_features[i].start != shape_plan->user_features[i].start ||
402 proposal->user_features[i].end != shape_plan->user_features[i].end)
403 return false;
404 return true;
405}
406
407static inline hb_bool_t
408hb_shape_plan_coords_match (const hb_shape_plan_t *shape_plan,
409 const hb_shape_plan_proposal_t *proposal)
410{
411 if (proposal->num_coords != shape_plan->num_coords)
412 return false;
413 for (unsigned int i = 0, n = proposal->num_coords; i < n; i++)
414 if (proposal->coords[i] != shape_plan->coords[i])
415 return false;
416 return true;
417}
418
419static hb_bool_t
420hb_shape_plan_matches (const hb_shape_plan_t *shape_plan,
421 const hb_shape_plan_proposal_t *proposal)
422{
423 return hb_segment_properties_equal (&shape_plan->props, &proposal->props) &&
424 hb_shape_plan_user_features_match (shape_plan, proposal) &&
425 hb_shape_plan_coords_match (shape_plan, proposal) &&
426 ((shape_plan->default_shaper_list && !proposal->shaper_list) ||
427 (shape_plan->shaper_func == proposal->shaper_func));
428}
429
430static inline hb_bool_t
431hb_non_global_user_features_present (const hb_feature_t *user_features,
432 unsigned int num_user_features)
433{
434 while (num_user_features) {
435 if (user_features->start != 0 || user_features->end != (unsigned int) -1)
436 return true;
437 num_user_features--;
438 user_features++;
439 }
440 return false;
441}
442
443static inline hb_bool_t
444hb_coords_present (const int *coords,
445 unsigned int num_coords)
446{
447 return num_coords != 0;
448}
449
450/**
451 * hb_shape_plan_create_cached:
452 * @face:
453 * @props:
454 * @user_features: (array length=num_user_features):
455 * @num_user_features:
456 * @shaper_list: (array zero-terminated=1):
457 *
458 *
459 *
460 * Return value: (transfer full):
461 *
462 * Since: 0.9.7
463 **/
464hb_shape_plan_t *
465hb_shape_plan_create_cached (hb_face_t *face,
466 const hb_segment_properties_t *props,
467 const hb_feature_t *user_features,
468 unsigned int num_user_features,
469 const char * const *shaper_list)
470{
471 return hb_shape_plan_create_cached2 (face, props,
472 user_features, num_user_features,
473 nullptr, 0,
474 shaper_list);
475}
476
477hb_shape_plan_t *
478hb_shape_plan_create_cached2 (hb_face_t *face,
479 const hb_segment_properties_t *props,
480 const hb_feature_t *user_features,
481 unsigned int num_user_features,
482 const int *coords,
483 unsigned int num_coords,
484 const char * const *shaper_list)
485{
486 DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr,
487 "face=%p num_features=%d shaper_list=%p",
488 face,
489 num_user_features,
490 shaper_list);
491
492 hb_shape_plan_proposal_t proposal = {
493 *props,
494 shaper_list,
495 user_features,
496 num_user_features,
497 nullptr
498 };
499
500 if (shaper_list) {
501 /* Choose shaper. Adapted from hb_shape_plan_plan().
502 * Must choose shaper exactly the same way as that function. */
503 for (const char * const *shaper_item = shaper_list; *shaper_item; shaper_item++)
504 if (0)
505 ;
506#define HB_SHAPER_IMPLEMENT(shaper) \
507 else if (0 == strcmp (*shaper_item, #shaper) && \
508 hb_##shaper##_shaper_face_data_ensure (face)) \
509 { \
510 proposal.shaper_func = _hb_##shaper##_shape; \
511 break; \
512 }
513#include "hb-shaper-list.hh"
514#undef HB_SHAPER_IMPLEMENT
515
516 if (unlikely (!proposal.shaper_func))
517 return hb_shape_plan_get_empty ();
518 }
519
520
521retry:
522 hb_face_t::plan_node_t *cached_plan_nodes = face->shape_plans.get ();
523
524 /* Don't look for plan in the cache if there were variation coordinates XXX Fix me. */
525 if (!hb_coords_present (coords, num_coords))
526 for (hb_face_t::plan_node_t *node = cached_plan_nodes; node; node = node->next)
527 if (hb_shape_plan_matches (node->shape_plan, &proposal))
528 {
529 DEBUG_MSG_FUNC (SHAPE_PLAN, node->shape_plan, "fulfilled from cache");
530 return hb_shape_plan_reference (node->shape_plan);
531 }
532
533 /* Not found. */
534 hb_shape_plan_t *shape_plan = hb_shape_plan_create2 (face, props,
535 user_features, num_user_features,
536 coords, num_coords,
537 shaper_list);
538
539 /* Don't add to the cache if face is inert. */
540 if (unlikely (hb_object_is_inert (face)))
541 return shape_plan;
542
543 /* Don't add the plan to the cache if there were user features with non-global ranges */
544 if (hb_non_global_user_features_present (user_features, num_user_features))
545 return shape_plan;
546 /* Don't add the plan to the cache if there were variation coordinates XXX Fix me. */
547 if (hb_coords_present (coords, num_coords))
548 return shape_plan;
549
550 hb_face_t::plan_node_t *node = (hb_face_t::plan_node_t *) calloc (1, sizeof (hb_face_t::plan_node_t));
551 if (unlikely (!node))
552 return shape_plan;
553
554 node->shape_plan = shape_plan;
555 node->next = cached_plan_nodes;
556
557 if (unlikely (!face->shape_plans.cmpexch (cached_plan_nodes, node)))
558 {
559 hb_shape_plan_destroy (shape_plan);
560 free (node);
561 goto retry;
562 }
563 DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan, "inserted into cache");
564
565 return hb_shape_plan_reference (shape_plan);
566}
567
568/**
569 * hb_shape_plan_get_shaper:
570 * @shape_plan: a shape plan.
571 *
572 *
573 *
574 * Return value: (transfer none):
575 *
576 * Since: 0.9.7
577 **/
578const char *
579hb_shape_plan_get_shaper (hb_shape_plan_t *shape_plan)
580{
581 return shape_plan->shaper_name;
582}
583