hb-shape-plan.cc revision cfe9882610489e1b917e09a74dfbf6bbba2e4a57
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
40void
41hb_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_TYPE (shaper, shape_plan) *data= \
52	      HB_SHAPER_DATA_CREATE_FUNC (shaper, shape_plan) (shape_plan, user_features, num_user_features); \
53	    if (data) { \
54	      HB_SHAPER_DATA (shaper, shape_plan) = data; \
55	      shape_plan->shaper_func = _hb_##shaper##_shape; \
56	      return; \
57	    } \
58	  } \
59	} HB_STMT_END
60
61  if (likely (!shaper_list)) {
62    for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++)
63      if (0)
64	;
65#define HB_SHAPER_IMPLEMENT(shaper) \
66      else if (shapers[i].func == _hb_##shaper##_shape) \
67	HB_SHAPER_PLAN (shaper);
68#include "hb-shaper-list.hh"
69#undef HB_SHAPER_IMPLEMENT
70  } else {
71    for (; *shaper_list; shaper_list++)
72      if (0)
73	;
74#define HB_SHAPER_IMPLEMENT(shaper) \
75      else if (0 == strcmp (*shaper_list, #shaper)) \
76	HB_SHAPER_PLAN (shaper);
77#include "hb-shaper-list.hh"
78#undef HB_SHAPER_IMPLEMENT
79  }
80
81#undef HB_SHAPER_PLAN
82}
83
84
85/*
86 * hb_shape_plan_t
87 */
88
89hb_shape_plan_t *
90hb_shape_plan_create (hb_face_t                     *face,
91		      const hb_segment_properties_t *props,
92		      const hb_feature_t            *user_features,
93		      unsigned int                   num_user_features,
94		      const char * const            *shaper_list)
95{
96  hb_shape_plan_t *shape_plan;
97
98  if (unlikely (!face))
99    face = hb_face_get_empty ();
100  if (unlikely (!props || hb_object_is_inert (face)))
101    return hb_shape_plan_get_empty ();
102  if (!(shape_plan = hb_object_create<hb_shape_plan_t> ()))
103    return hb_shape_plan_get_empty ();
104
105  hb_face_make_immutable (face);
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
114hb_shape_plan_t *
115hb_shape_plan_get_empty (void)
116{
117  static const hb_shape_plan_t _hb_shape_plan_nil = {
118    HB_OBJECT_HEADER_STATIC,
119
120    NULL, /* face */
121    _HB_BUFFER_PROPS_DEFAULT, /* props */
122
123    NULL, /* shaper_func */
124
125    {
126#define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
127#include "hb-shaper-list.hh"
128#undef HB_SHAPER_IMPLEMENT
129    }
130  };
131
132  return const_cast<hb_shape_plan_t *> (&_hb_shape_plan_nil);
133}
134
135hb_shape_plan_t *
136hb_shape_plan_reference (hb_shape_plan_t *shape_plan)
137{
138  return hb_object_reference (shape_plan);
139}
140
141void
142hb_shape_plan_destroy (hb_shape_plan_t *shape_plan)
143{
144  if (!hb_object_destroy (shape_plan)) return;
145
146#define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, shape_plan);
147#include "hb-shaper-list.hh"
148#undef HB_SHAPER_IMPLEMENT
149
150  free (shape_plan);
151}
152
153
154hb_bool_t
155hb_shape_plan_execute (hb_shape_plan      *shape_plan,
156		       hb_font_t          *font,
157		       hb_buffer_t        *buffer,
158		       const hb_feature_t *features,
159		       unsigned int        num_features)
160{
161  if (unlikely (shape_plan->face != font->face))
162    return false;
163
164#define HB_SHAPER_EXECUTE(shaper) \
165	HB_STMT_START { \
166	  if (hb_##shaper##_shaper_font_data_ensure (font) && \
167	      _hb_##shaper##_shape (shape_plan, font, buffer, features, num_features)) \
168	    return true; \
169	  else \
170	    return false; \
171	} HB_STMT_END
172
173  if (0)
174    ;
175#define HB_SHAPER_IMPLEMENT(shaper) \
176  else if (shape_plan->shaper_func == _hb_##shaper##_shape) \
177    HB_SHAPER_EXECUTE (shaper);
178#include "hb-shaper-list.hh"
179#undef HB_SHAPER_IMPLEMENT
180
181#undef HB_SHAPER_EXECUTE
182
183  return false;
184}
185