22a226f3a6c1c532e0338d65414f769af1d2e351
[platform/upstream/harfbuzz.git] / src / hb-shape-plan.cc
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-shape-plan-private.hh"
28 #include "hb-shaper-private.hh"
29 #include "hb-font-private.hh"
30
31 #define HB_SHAPER_IMPLEMENT(shaper) \
32         HB_SHAPER_DATA_ENSURE_DECLARE(shaper, face) \
33         HB_SHAPER_DATA_ENSURE_DECLARE(shaper, font)
34 #include "hb-shaper-list.hh"
35 #undef HB_SHAPER_IMPLEMENT
36
37
38 static void
39 hb_shape_plan_plan (hb_shape_plan_t    *shape_plan,
40                     const hb_feature_t *user_features,
41                     unsigned int        num_user_features,
42                     const char * const *shaper_list)
43 {
44   const hb_shaper_pair_t *shapers = _hb_shapers_get ();
45
46 #define HB_SHAPER_PLAN(shaper) \
47         HB_STMT_START { \
48           if (hb_##shaper##_shaper_face_data_ensure (shape_plan->face)) { \
49             HB_SHAPER_DATA (shaper, shape_plan) = \
50               HB_SHAPER_DATA_CREATE_FUNC (shaper, shape_plan) (shape_plan, user_features, num_user_features); \
51             shape_plan->shaper_func = _hb_##shaper##_shape; \
52             shape_plan->shaper_name = #shaper; \
53             return; \
54           } \
55         } HB_STMT_END
56
57   if (likely (!shaper_list)) {
58     for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++)
59       if (0)
60         ;
61 #define HB_SHAPER_IMPLEMENT(shaper) \
62       else if (shapers[i].func == _hb_##shaper##_shape) \
63         HB_SHAPER_PLAN (shaper);
64 #include "hb-shaper-list.hh"
65 #undef HB_SHAPER_IMPLEMENT
66   } else {
67     for (; *shaper_list; shaper_list++)
68       if (0)
69         ;
70 #define HB_SHAPER_IMPLEMENT(shaper) \
71       else if (0 == strcmp (*shaper_list, #shaper)) \
72         HB_SHAPER_PLAN (shaper);
73 #include "hb-shaper-list.hh"
74 #undef HB_SHAPER_IMPLEMENT
75   }
76
77 #undef HB_SHAPER_PLAN
78 }
79
80
81 /*
82  * hb_shape_plan_t
83  */
84
85 hb_shape_plan_t *
86 hb_shape_plan_create (hb_face_t                     *face,
87                       const hb_segment_properties_t *props,
88                       const hb_feature_t            *user_features,
89                       unsigned int                   num_user_features,
90                       const char * const            *shaper_list)
91 {
92   assert (props->direction != HB_DIRECTION_INVALID);
93
94   hb_shape_plan_t *shape_plan;
95
96   if (unlikely (!face))
97     face = hb_face_get_empty ();
98   if (unlikely (!props || hb_object_is_inert (face)))
99     return hb_shape_plan_get_empty ();
100   if (!(shape_plan = hb_object_create<hb_shape_plan_t> ()))
101     return hb_shape_plan_get_empty ();
102
103   hb_face_make_immutable (face);
104   shape_plan->default_shaper_list = shaper_list == NULL;
105   shape_plan->face = hb_face_reference (face);
106   shape_plan->props = *props;
107
108   hb_shape_plan_plan (shape_plan, user_features, num_user_features, shaper_list);
109
110   return shape_plan;
111 }
112
113 hb_shape_plan_t *
114 hb_shape_plan_get_empty (void)
115 {
116   static const hb_shape_plan_t _hb_shape_plan_nil = {
117     HB_OBJECT_HEADER_STATIC,
118
119     true, /* default_shaper_list */
120     NULL, /* face */
121     HB_SEGMENT_PROPERTIES_DEFAULT, /* props */
122
123     NULL, /* shaper_func */
124     NULL, /* shaper_name */
125
126     {
127 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
128 #include "hb-shaper-list.hh"
129 #undef HB_SHAPER_IMPLEMENT
130     }
131   };
132
133   return const_cast<hb_shape_plan_t *> (&_hb_shape_plan_nil);
134 }
135
136 hb_shape_plan_t *
137 hb_shape_plan_reference (hb_shape_plan_t *shape_plan)
138 {
139   return hb_object_reference (shape_plan);
140 }
141
142 void
143 hb_shape_plan_destroy (hb_shape_plan_t *shape_plan)
144 {
145   if (!hb_object_destroy (shape_plan)) return;
146
147 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, shape_plan);
148 #include "hb-shaper-list.hh"
149 #undef HB_SHAPER_IMPLEMENT
150
151   hb_face_destroy (shape_plan->face);
152
153   free (shape_plan);
154 }
155
156 hb_bool_t
157 hb_shape_plan_set_user_data (hb_shape_plan_t    *shape_plan,
158                              hb_user_data_key_t *key,
159                              void *              data,
160                              hb_destroy_func_t   destroy,
161                              hb_bool_t           replace)
162 {
163   return hb_object_set_user_data (shape_plan, key, data, destroy, replace);
164 }
165
166 void *
167 hb_shape_plan_get_user_data (hb_shape_plan_t    *shape_plan,
168                              hb_user_data_key_t *key)
169 {
170   return hb_object_get_user_data (shape_plan, key);
171 }
172
173
174 hb_bool_t
175 hb_shape_plan_execute (hb_shape_plan_t    *shape_plan,
176                        hb_font_t          *font,
177                        hb_buffer_t        *buffer,
178                        const hb_feature_t *features,
179                        unsigned int        num_features)
180 {
181   if (unlikely (shape_plan->face != font->face))
182     return false;
183
184 #define HB_SHAPER_EXECUTE(shaper) \
185         HB_STMT_START { \
186           return HB_SHAPER_DATA (shaper, shape_plan) && \
187                  hb_##shaper##_shaper_font_data_ensure (font) && \
188                  _hb_##shaper##_shape (shape_plan, font, buffer, features, num_features); \
189         } HB_STMT_END
190
191   if (0)
192     ;
193 #define HB_SHAPER_IMPLEMENT(shaper) \
194   else if (shape_plan->shaper_func == _hb_##shaper##_shape) \
195     HB_SHAPER_EXECUTE (shaper);
196 #include "hb-shaper-list.hh"
197 #undef HB_SHAPER_IMPLEMENT
198
199 #undef HB_SHAPER_EXECUTE
200
201   return false;
202 }
203
204
205 /*
206  * caching
207  */
208
209 #if 0
210 static unsigned int
211 hb_shape_plan_hash (const hb_shape_plan_t *shape_plan)
212 {
213   return hb_segment_properties_hash (&shape_plan->props) +
214          shape_plan->default_shaper_list ? 0 : (intptr_t) shape_plan->shaper_func;
215 }
216 #endif
217
218 /* TODO no user-feature caching for now. */
219 struct hb_shape_plan_proposal_t
220 {
221   const hb_segment_properties_t  props;
222   const char * const            *shaper_list;
223   hb_shape_func_t               *shaper_func;
224 };
225
226 static hb_bool_t
227 hb_shape_plan_matches (const hb_shape_plan_t          *shape_plan,
228                        const hb_shape_plan_proposal_t *proposal)
229 {
230   return hb_segment_properties_equal (&shape_plan->props, &proposal->props) &&
231          ((shape_plan->default_shaper_list && proposal->shaper_list == NULL) ||
232           (shape_plan->shaper_func == proposal->shaper_func));
233 }
234
235 hb_shape_plan_t *
236 hb_shape_plan_create_cached (hb_face_t                     *face,
237                              const hb_segment_properties_t *props,
238                              const hb_feature_t            *user_features,
239                              unsigned int                   num_user_features,
240                              const char * const            *shaper_list)
241 {
242   if (num_user_features)
243     return hb_shape_plan_create (face, props, user_features, num_user_features, shaper_list);
244
245   hb_shape_plan_proposal_t proposal = {
246     *props,
247     shaper_list,
248     NULL
249   };
250
251   if (shaper_list) {
252     /* Choose shaper.  Adapted from hb_shape_plan_plan(). */
253 #define HB_SHAPER_PLAN(shaper) \
254           HB_STMT_START { \
255             if (hb_##shaper##_shaper_face_data_ensure (face)) \
256               proposal.shaper_func = _hb_##shaper##_shape; \
257           } HB_STMT_END
258
259     for (const char * const *shaper_item = shaper_list; *shaper_item; shaper_item++)
260       if (0)
261         ;
262 #define HB_SHAPER_IMPLEMENT(shaper) \
263       else if (0 == strcmp (*shaper_item, #shaper)) \
264         HB_SHAPER_PLAN (shaper);
265 #include "hb-shaper-list.hh"
266 #undef HB_SHAPER_IMPLEMENT
267
268 #undef HB_SHAPER_PLAN
269
270     if (unlikely (!proposal.shaper_list))
271       return hb_shape_plan_get_empty ();
272   }
273
274
275 retry:
276   hb_face_t::plan_node_t *cached_plan_nodes = (hb_face_t::plan_node_t *) hb_atomic_ptr_get (&face->shape_plans);
277   for (hb_face_t::plan_node_t *node = cached_plan_nodes; node; node = node->next)
278     if (hb_shape_plan_matches (node->shape_plan, &proposal))
279       return hb_shape_plan_reference (node->shape_plan);
280
281   /* Not found. */
282
283   hb_shape_plan_t *shape_plan = hb_shape_plan_create (face, props, user_features, num_user_features, shaper_list);
284
285   hb_face_t::plan_node_t *node = (hb_face_t::plan_node_t *) calloc (1, sizeof (hb_face_t::plan_node_t));
286   if (unlikely (!node))
287     return shape_plan;
288
289   node->shape_plan = shape_plan;
290   node->next = cached_plan_nodes;
291
292   if (!hb_atomic_ptr_cmpexch (&face->shape_plans, cached_plan_nodes, node)) {
293     hb_shape_plan_destroy (shape_plan);
294     free (node);
295     goto retry;
296   }
297
298   /* Release our reference on face. */
299   hb_face_destroy (face);
300
301   return hb_shape_plan_reference (shape_plan);
302 }
303
304 const char *
305 hb_shape_plan_get_shaper (hb_shape_plan_t *shape_plan)
306 {
307   return shape_plan->shaper_name;
308 }