Imported Upstream version 0.9.3
[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-private.hh"
28
29 #include "hb-shape-plan-private.hh"
30 #include "hb-shaper-private.hh"
31 #include "hb-font-private.hh"
32
33 #define HB_SHAPER_IMPLEMENT(shaper) \
34         HB_SHAPER_DATA_ENSURE_DECLARE(shaper, face) \
35         HB_SHAPER_DATA_ENSURE_DECLARE(shaper, font)
36 #include "hb-shaper-list.hh"
37 #undef HB_SHAPER_IMPLEMENT
38
39
40 static void
41 hb_shape_plan_plan (hb_shape_plan_t    *shape_plan,
42                     const hb_feature_t *user_features,
43                     unsigned int        num_user_features,
44                     const char * const *shaper_list)
45 {
46   const hb_shaper_pair_t *shapers = _hb_shapers_get ();
47
48 #define HB_SHAPER_PLAN(shaper) \
49         HB_STMT_START { \
50           if (hb_##shaper##_shaper_face_data_ensure (shape_plan->face)) { \
51             HB_SHAPER_DATA (shaper, shape_plan) = \
52               HB_SHAPER_DATA_CREATE_FUNC (shaper, shape_plan) (shape_plan, user_features, num_user_features); \
53             shape_plan->shaper_func = _hb_##shaper##_shape; \
54             return; \
55           } \
56         } HB_STMT_END
57
58   if (likely (!shaper_list)) {
59     for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++)
60       if (0)
61         ;
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)
70         ;
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
76   }
77
78 #undef HB_SHAPER_PLAN
79 }
80
81
82 /*
83  * hb_shape_plan_t
84  */
85
86 hb_shape_plan_t *
87 hb_shape_plan_create (hb_face_t                     *face,
88                       const hb_segment_properties_t *props,
89                       const hb_feature_t            *user_features,
90                       unsigned int                   num_user_features,
91                       const char * const            *shaper_list)
92 {
93   assert (props->direction != HB_DIRECTION_INVALID);
94
95   hb_shape_plan_t *shape_plan;
96
97   if (unlikely (!face))
98     face = hb_face_get_empty ();
99   if (unlikely (!props || hb_object_is_inert (face)))
100     return hb_shape_plan_get_empty ();
101   if (!(shape_plan = hb_object_create<hb_shape_plan_t> ()))
102     return hb_shape_plan_get_empty ();
103
104   hb_face_make_immutable (face);
105   shape_plan->default_shaper_list = shaper_list == NULL;
106   shape_plan->face = hb_face_reference (face);
107   shape_plan->props = *props;
108
109   hb_shape_plan_plan (shape_plan, user_features, num_user_features, shaper_list);
110
111   return shape_plan;
112 }
113
114 hb_shape_plan_t *
115 hb_shape_plan_get_empty (void)
116 {
117   static const hb_shape_plan_t _hb_shape_plan_nil = {
118     HB_OBJECT_HEADER_STATIC,
119
120     true, /* default_shaper_list */
121     NULL, /* face */
122     _HB_BUFFER_PROPS_DEFAULT, /* props */
123
124     NULL, /* shaper_func */
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
157 hb_bool_t
158 hb_shape_plan_execute (hb_shape_plan      *shape_plan,
159                        hb_font_t          *font,
160                        hb_buffer_t        *buffer,
161                        const hb_feature_t *features,
162                        unsigned int        num_features)
163 {
164   if (unlikely (shape_plan->face != font->face))
165     return false;
166
167 #define HB_SHAPER_EXECUTE(shaper) \
168         HB_STMT_START { \
169           return HB_SHAPER_DATA (shaper, shape_plan) && \
170                  hb_##shaper##_shaper_font_data_ensure (font) && \
171                  _hb_##shaper##_shape (shape_plan, font, buffer, features, num_features); \
172         } HB_STMT_END
173
174   if (0)
175     ;
176 #define HB_SHAPER_IMPLEMENT(shaper) \
177   else if (shape_plan->shaper_func == _hb_##shaper##_shape) \
178     HB_SHAPER_EXECUTE (shaper);
179 #include "hb-shaper-list.hh"
180 #undef HB_SHAPER_IMPLEMENT
181
182 #undef HB_SHAPER_EXECUTE
183
184   return false;
185 }
186
187
188 /*
189  * caching
190  */
191
192 #if 0
193 static unsigned int
194 hb_shape_plan_hash (const hb_shape_plan_t *shape_plan)
195 {
196   return hb_segment_properties_hash (&shape_plan->props) +
197          shape_plan->default_shaper_list ? 0 : (intptr_t) shape_plan->shaper_func;
198 }
199 #endif
200
201 /* TODO no user-feature caching for now. */
202 struct hb_shape_plan_proposal_t
203 {
204   const hb_segment_properties_t  props;
205   const char * const            *shaper_list;
206   hb_shape_func_t               *shaper_func;
207 };
208
209 static hb_bool_t
210 hb_shape_plan_matches (const hb_shape_plan_t          *shape_plan,
211                        const hb_shape_plan_proposal_t *proposal)
212 {
213   return hb_segment_properties_equal (&shape_plan->props, &proposal->props) &&
214          ((shape_plan->default_shaper_list && proposal->shaper_list == NULL) ||
215           (shape_plan->shaper_func == proposal->shaper_func));
216 }
217
218 hb_shape_plan_t *
219 hb_shape_plan_create_cached (hb_face_t                     *face,
220                              const hb_segment_properties_t *props,
221                              const hb_feature_t            *user_features,
222                              unsigned int                   num_user_features,
223                              const char * const            *shaper_list)
224 {
225   if (num_user_features)
226     return hb_shape_plan_create (face, props, user_features, num_user_features, shaper_list);
227
228   hb_shape_plan_proposal_t proposal = {
229     *props,
230     shaper_list,
231     NULL
232   };
233
234   if (shaper_list) {
235     /* Choose shaper.  Adapted from hb_shape_plan_plan(). */
236 #define HB_SHAPER_PLAN(shaper) \
237           HB_STMT_START { \
238             if (hb_##shaper##_shaper_face_data_ensure (face)) \
239               proposal.shaper_func = _hb_##shaper##_shape; \
240           } HB_STMT_END
241
242     for (const char * const *shaper_item = shaper_list; *shaper_item; shaper_item++)
243       if (0)
244         ;
245 #define HB_SHAPER_IMPLEMENT(shaper) \
246       else if (0 == strcmp (*shaper_item, #shaper)) \
247         HB_SHAPER_PLAN (shaper);
248 #include "hb-shaper-list.hh"
249 #undef HB_SHAPER_IMPLEMENT
250
251 #undef HB_SHAPER_PLAN
252
253     if (unlikely (!proposal.shaper_list))
254       return hb_shape_plan_get_empty ();
255   }
256
257
258 retry:
259   hb_face_t::plan_node_t *cached_plan_nodes = (hb_face_t::plan_node_t *) hb_atomic_ptr_get (&face->shape_plans);
260   for (hb_face_t::plan_node_t *node = cached_plan_nodes; node; node = node->next)
261     if (hb_shape_plan_matches (node->shape_plan, &proposal))
262       return hb_shape_plan_reference (node->shape_plan);
263
264   /* Not found. */
265
266   hb_shape_plan_t *shape_plan = hb_shape_plan_create (face, props, user_features, num_user_features, shaper_list);
267
268   hb_face_t::plan_node_t *node = (hb_face_t::plan_node_t *) calloc (1, sizeof (hb_face_t::plan_node_t));
269   if (unlikely (!node))
270     return shape_plan;
271
272   node->shape_plan = shape_plan;
273   node->next = cached_plan_nodes;
274
275   if (!hb_atomic_ptr_cmpexch (&face->shape_plans, cached_plan_nodes, node)) {
276     hb_shape_plan_destroy (shape_plan);
277     free (node);
278     goto retry;
279   }
280
281   /* Release our reference on face. */
282   hb_face_destroy (face);
283
284   return hb_shape_plan_reference (shape_plan);
285 }