Humm, undo some 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 static unsigned int
43 hb_unicode_get_combining_class_nil (hb_unicode_funcs_t *ufuncs    HB_UNUSED,
44                                     hb_codepoint_t      unicode   HB_UNUSED,
45                                     void               *user_data HB_UNUSED)
46 {
47   return 0;
48 }
49
50 static unsigned int
51 hb_unicode_get_eastasian_width_nil (hb_unicode_funcs_t *ufuncs    HB_UNUSED,
52                                     hb_codepoint_t      unicode   HB_UNUSED,
53                                     void               *user_data HB_UNUSED)
54 {
55   return 1;
56 }
57
58 static hb_unicode_general_category_t
59 hb_unicode_get_general_category_nil (hb_unicode_funcs_t *ufuncs    HB_UNUSED,
60                                      hb_codepoint_t      unicode   HB_UNUSED,
61                                      void               *user_data HB_UNUSED)
62 {
63   return HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER;
64 }
65
66 static hb_codepoint_t
67 hb_unicode_get_mirroring_nil (hb_unicode_funcs_t *ufuncs    HB_UNUSED,
68                               hb_codepoint_t      unicode   HB_UNUSED,
69                               void               *user_data HB_UNUSED)
70 {
71   return unicode;
72 }
73
74 static hb_script_t
75 hb_unicode_get_script_nil (hb_unicode_funcs_t *ufuncs    HB_UNUSED,
76                            hb_codepoint_t      unicode   HB_UNUSED,
77                            void               *user_data HB_UNUSED)
78 {
79   return HB_SCRIPT_UNKNOWN;
80 }
81
82
83 hb_unicode_funcs_t _hb_unicode_funcs_nil = {
84   HB_OBJECT_HEADER_STATIC,
85
86   NULL, /* parent */
87   TRUE, /* immutable */
88   {
89 #define HB_UNICODE_FUNC_IMPLEMENT(name) hb_unicode_get_##name##_nil,
90     HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
91 #undef HB_UNICODE_FUNC_IMPLEMENT
92   }
93 };
94
95
96 hb_unicode_funcs_t *
97 hb_unicode_funcs_get_default (void)
98 {
99   return &_hb_unicode_funcs_default;
100 }
101
102 hb_unicode_funcs_t *
103 hb_unicode_funcs_create (hb_unicode_funcs_t *parent)
104 {
105   hb_unicode_funcs_t *ufuncs;
106
107   if (!(ufuncs = hb_object_create<hb_unicode_funcs_t> ()))
108     return &_hb_unicode_funcs_nil;
109
110   if (!parent)
111     parent = &_hb_unicode_funcs_nil;
112
113   hb_unicode_funcs_make_immutable (parent);
114   ufuncs->parent = hb_unicode_funcs_reference (parent);
115
116   ufuncs->get = parent->get;
117
118   /* We can safely copy user_data from parent since we hold a reference
119    * onto it and it's immutable.  We should not copy the destroy notifiers
120    * though. */
121   ufuncs->user_data = parent->user_data;
122
123   return ufuncs;
124 }
125
126 hb_unicode_funcs_t *
127 hb_unicode_funcs_get_empty (void)
128 {
129   return &_hb_unicode_funcs_nil;
130 }
131
132 hb_unicode_funcs_t *
133 hb_unicode_funcs_reference (hb_unicode_funcs_t *ufuncs)
134 {
135   return hb_object_reference (ufuncs);
136 }
137
138 void
139 hb_unicode_funcs_destroy (hb_unicode_funcs_t *ufuncs)
140 {
141   if (!hb_object_destroy (ufuncs)) return;
142
143 #define HB_UNICODE_FUNC_IMPLEMENT(name) \
144   if (ufuncs->destroy.name) ufuncs->destroy.name (ufuncs->user_data.name);
145     HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
146 #undef HB_UNICODE_FUNC_IMPLEMENT
147
148   hb_unicode_funcs_destroy (ufuncs->parent);
149
150   free (ufuncs);
151 }
152
153 hb_bool_t
154 hb_unicode_funcs_set_user_data (hb_unicode_funcs_t *ufuncs,
155                                 hb_user_data_key_t *key,
156                                 void *              data,
157                                 hb_destroy_func_t   destroy)
158 {
159   return hb_object_set_user_data (ufuncs, key, data, destroy);
160 }
161
162 void *
163 hb_unicode_funcs_get_user_data (hb_unicode_funcs_t *ufuncs,
164                                 hb_user_data_key_t *key)
165 {
166   return hb_object_get_user_data (ufuncs, key);
167 }
168
169
170 void
171 hb_unicode_funcs_make_immutable (hb_unicode_funcs_t *ufuncs)
172 {
173   if (hb_object_is_inert (ufuncs))
174     return;
175
176   ufuncs->immutable = TRUE;
177 }
178
179 hb_bool_t
180 hb_unicode_funcs_is_immutable (hb_unicode_funcs_t *ufuncs)
181 {
182   return ufuncs->immutable;
183 }
184
185 hb_unicode_funcs_t *
186 hb_unicode_funcs_get_parent (hb_unicode_funcs_t *ufuncs)
187 {
188   return ufuncs->parent ? ufuncs->parent : &_hb_unicode_funcs_nil;
189 }
190
191
192 #define HB_UNICODE_FUNC_IMPLEMENT(name)                                         \
193                                                                                 \
194 void                                                                            \
195 hb_unicode_funcs_set_##name##_func (hb_unicode_funcs_t             *ufuncs,     \
196                                     hb_unicode_get_##name##_func_t  func,       \
197                                     void                           *user_data,  \
198                                     hb_destroy_func_t               destroy)    \
199 {                                                                               \
200   if (ufuncs->immutable)                                                        \
201     return;                                                                     \
202                                                                                 \
203   if (ufuncs->destroy.name)                                                     \
204     ufuncs->destroy.name (ufuncs->user_data.name);                              \
205                                                                                 \
206   if (func) {                                                                   \
207     ufuncs->get.name = func;                                                    \
208     ufuncs->user_data.name = user_data;                                         \
209     ufuncs->destroy.name = destroy;                                             \
210   } else {                                                                      \
211     ufuncs->get.name = ufuncs->parent->get.name;                                \
212     ufuncs->user_data.name = ufuncs->parent->user_data.name;                    \
213     ufuncs->destroy.name = NULL;                                                \
214   }                                                                             \
215 }
216
217     HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
218 #undef HB_UNICODE_FUNC_IMPLEMENT
219
220
221 #define HB_UNICODE_FUNC_IMPLEMENT(return_type, name)                            \
222                                                                                 \
223 return_type                                                                     \
224 hb_unicode_get_##name (hb_unicode_funcs_t *ufuncs,                              \
225                        hb_codepoint_t      unicode)                             \
226 {                                                                               \
227   return ufuncs->get.name (ufuncs, unicode, ufuncs->user_data.name);            \
228 }
229     HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS_SIMPLE
230 #undef HB_UNICODE_FUNC_IMPLEMENT
231
232
233 HB_END_DECLS