More code shuffling
[profile/ivi/org.tizen.video-player.git] / src / hb-unicode.cc
1 /*
2  * Copyright © 2009  Red Hat, Inc.
3  * Copyright © 2011 Codethink Limited
4  * Copyright © 2010,2011  Google, Inc.
5  *
6  *  This is part of HarfBuzz, a text shaping library.
7  *
8  * Permission is hereby granted, without written agreement and without
9  * license or royalty fees, to use, copy, modify, and distribute this
10  * software and its documentation for any purpose, provided that the
11  * above copyright notice and the following two paragraphs appear in
12  * all copies of this software.
13  *
14  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
17  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18  * DAMAGE.
19  *
20  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
21  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25  *
26  * Red Hat Author(s): Behdad Esfahbod
27  * Codethink Author(s): Ryan Lortie
28  * Google Author(s): Behdad Esfahbod
29  */
30
31 #include "hb-private.hh"
32
33 #include "hb-unicode-private.hh"
34
35 HB_BEGIN_DECLS
36
37
38 /*
39  * hb_unicode_funcs_t
40  */
41
42 #define HB_UNICODE_FUNC_IMPLEMENT(return_type, name, default_value)             \
43                                                                                 \
44                                                                                 \
45 static return_type                                                              \
46 hb_unicode_get_##name##_nil (hb_unicode_funcs_t *ufuncs    HB_UNUSED,           \
47                              hb_codepoint_t      unicode   HB_UNUSED,           \
48                              void               *user_data HB_UNUSED)           \
49 {                                                                               \
50   return default_value;                                                         \
51 }
52
53   HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
54 #undef HB_UNICODE_FUNC_IMPLEMENT
55
56
57 hb_unicode_funcs_t _hb_unicode_funcs_nil = {
58   HB_OBJECT_HEADER_STATIC,
59
60   NULL, /* parent */
61   TRUE, /* immutable */
62   {
63 #define HB_UNICODE_FUNC_IMPLEMENT(return_type, name, default_value) hb_unicode_get_##name##_nil,
64     HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
65 #undef HB_UNICODE_FUNC_IMPLEMENT
66   }
67 };
68
69
70 hb_unicode_funcs_t *
71 hb_unicode_funcs_get_default (void)
72 {
73   return &_hb_unicode_funcs_default;
74 }
75
76 hb_unicode_funcs_t *
77 hb_unicode_funcs_create (hb_unicode_funcs_t *parent)
78 {
79   hb_unicode_funcs_t *ufuncs;
80
81   if (!(ufuncs = hb_object_create<hb_unicode_funcs_t> ()))
82     return &_hb_unicode_funcs_nil;
83
84   if (!parent)
85     parent = &_hb_unicode_funcs_nil;
86
87   hb_unicode_funcs_make_immutable (parent);
88   ufuncs->parent = hb_unicode_funcs_reference (parent);
89
90   ufuncs->get = parent->get;
91
92   /* We can safely copy user_data from parent since we hold a reference
93    * onto it and it's immutable.  We should not copy the destroy notifiers
94    * though. */
95   ufuncs->user_data = parent->user_data;
96
97   return ufuncs;
98 }
99
100 hb_unicode_funcs_t *
101 hb_unicode_funcs_get_empty (void)
102 {
103   return &_hb_unicode_funcs_nil;
104 }
105
106 hb_unicode_funcs_t *
107 hb_unicode_funcs_reference (hb_unicode_funcs_t *ufuncs)
108 {
109   return hb_object_reference (ufuncs);
110 }
111
112 void
113 hb_unicode_funcs_destroy (hb_unicode_funcs_t *ufuncs)
114 {
115   if (!hb_object_destroy (ufuncs)) return;
116
117 #define HB_UNICODE_FUNC_IMPLEMENT(return_type, name, default_value) \
118   if (ufuncs->destroy.name) ufuncs->destroy.name (ufuncs->user_data.name);
119     HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
120 #undef HB_UNICODE_FUNC_IMPLEMENT
121
122   hb_unicode_funcs_destroy (ufuncs->parent);
123
124   free (ufuncs);
125 }
126
127 hb_bool_t
128 hb_unicode_funcs_set_user_data (hb_unicode_funcs_t *ufuncs,
129                                 hb_user_data_key_t *key,
130                                 void *              data,
131                                 hb_destroy_func_t   destroy)
132 {
133   return hb_object_set_user_data (ufuncs, key, data, destroy);
134 }
135
136 void *
137 hb_unicode_funcs_get_user_data (hb_unicode_funcs_t *ufuncs,
138                                 hb_user_data_key_t *key)
139 {
140   return hb_object_get_user_data (ufuncs, key);
141 }
142
143
144 void
145 hb_unicode_funcs_make_immutable (hb_unicode_funcs_t *ufuncs)
146 {
147   if (hb_object_is_inert (ufuncs))
148     return;
149
150   ufuncs->immutable = TRUE;
151 }
152
153 hb_bool_t
154 hb_unicode_funcs_is_immutable (hb_unicode_funcs_t *ufuncs)
155 {
156   return ufuncs->immutable;
157 }
158
159 hb_unicode_funcs_t *
160 hb_unicode_funcs_get_parent (hb_unicode_funcs_t *ufuncs)
161 {
162   return ufuncs->parent ? ufuncs->parent : &_hb_unicode_funcs_nil;
163 }
164
165
166 #define HB_UNICODE_FUNC_IMPLEMENT(return_type, name, default_value)             \
167                                                                                 \
168 void                                                                            \
169 hb_unicode_funcs_set_##name##_func (hb_unicode_funcs_t             *ufuncs,     \
170                                     hb_unicode_get_##name##_func_t  func,       \
171                                     void                           *user_data,  \
172                                     hb_destroy_func_t               destroy)    \
173 {                                                                               \
174   if (ufuncs->immutable)                                                        \
175     return;                                                                     \
176                                                                                 \
177   if (ufuncs->destroy.name)                                                     \
178     ufuncs->destroy.name (ufuncs->user_data.name);                              \
179                                                                                 \
180   if (func) {                                                                   \
181     ufuncs->get.name = func;                                                    \
182     ufuncs->user_data.name = user_data;                                         \
183     ufuncs->destroy.name = destroy;                                             \
184   } else {                                                                      \
185     ufuncs->get.name = ufuncs->parent->get.name;                                \
186     ufuncs->user_data.name = ufuncs->parent->user_data.name;                    \
187     ufuncs->destroy.name = NULL;                                                \
188   }                                                                             \
189 }                                                                               \
190                                                                                 \
191 return_type                                                                     \
192 hb_unicode_get_##name (hb_unicode_funcs_t *ufuncs,                              \
193                        hb_codepoint_t      unicode)                             \
194 {                                                                               \
195   return ufuncs->get.name (ufuncs, unicode, ufuncs->user_data.name);            \
196 }
197
198     HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
199 #undef HB_UNICODE_FUNC_IMPLEMENT
200
201
202 HB_END_DECLS