Add hb_object_header_t which is the common part of all objects
[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     hb_unicode_get_combining_class_nil,
90     hb_unicode_get_eastasian_width_nil,
91     hb_unicode_get_general_category_nil,
92     hb_unicode_get_mirroring_nil,
93     hb_unicode_get_script_nil,
94   }
95 };
96
97
98 hb_unicode_funcs_t *
99 hb_unicode_funcs_create (hb_unicode_funcs_t *parent)
100 {
101   hb_unicode_funcs_t *ufuncs;
102
103   if (!HB_OBJECT_DO_CREATE (hb_unicode_funcs_t, ufuncs))
104     return &_hb_unicode_funcs_nil;
105
106   if (parent != NULL)
107   {
108     hb_unicode_funcs_make_immutable (parent);
109     ufuncs->parent = hb_unicode_funcs_reference (parent);
110
111     ufuncs->get = parent->get;
112
113     /* We can safely copy user_data from parent since we hold a reference
114      * onto it and it's immutable.  We should not copy the destroy notifiers
115      * though. */
116     ufuncs->user_data = parent->user_data;
117   }
118   else
119   {
120     ufuncs->get = _hb_unicode_funcs_nil.get;
121   }
122
123   return ufuncs;
124 }
125
126 hb_unicode_funcs_t *
127 hb_unicode_funcs_reference (hb_unicode_funcs_t *ufuncs)
128 {
129   HB_OBJECT_DO_REFERENCE (ufuncs);
130 }
131
132 void
133 hb_unicode_funcs_destroy (hb_unicode_funcs_t *ufuncs)
134 {
135   HB_OBJECT_DO_DESTROY (ufuncs);
136
137 #define DESTROY(name) if (ufuncs->destroy.name) ufuncs->destroy.name (ufuncs->user_data.name)
138   DESTROY (combining_class);
139   DESTROY (eastasian_width);
140   DESTROY (general_category);
141   DESTROY (mirroring);
142   DESTROY (script);
143 #undef DESTROY
144
145   if (ufuncs->parent != NULL)
146     hb_unicode_funcs_destroy (ufuncs->parent);
147
148   free (ufuncs);
149 }
150
151 void
152 hb_unicode_funcs_make_immutable (hb_unicode_funcs_t *ufuncs)
153 {
154   if (HB_OBJECT_IS_INERT (ufuncs))
155     return;
156
157   ufuncs->immutable = TRUE;
158 }
159
160 hb_bool_t
161 hb_unicode_funcs_is_immutable (hb_unicode_funcs_t *ufuncs)
162 {
163   return ufuncs->immutable;
164 }
165
166 hb_unicode_funcs_t *
167 hb_unicode_funcs_get_parent (hb_unicode_funcs_t *ufuncs)
168 {
169   return ufuncs->parent;
170 }
171
172
173 #define IMPLEMENT(return_type, name)                                           \
174                                                                                \
175 void                                                                           \
176 hb_unicode_funcs_set_##name##_func (hb_unicode_funcs_t             *ufuncs,    \
177                                     hb_unicode_get_##name##_func_t  func,      \
178                                     void                           *user_data, \
179                                     hb_destroy_func_t               destroy)   \
180 {                                                                              \
181   if (ufuncs->immutable)                                                       \
182     return;                                                                    \
183                                                                                \
184   if (ufuncs->destroy.name)                                                    \
185     ufuncs->destroy.name (ufuncs->user_data.name);                             \
186                                                                                \
187   if (func) {                                                                  \
188     ufuncs->get.name = func;                                                   \
189     ufuncs->user_data.name = user_data;                                        \
190     ufuncs->destroy.name = destroy;                                            \
191   } else if (ufuncs->parent != NULL) {                                         \
192     ufuncs->get.name = ufuncs->parent->get.name;                               \
193     ufuncs->user_data.name = ufuncs->parent->user_data.name;                   \
194     ufuncs->destroy.name = NULL;                                               \
195   } else {                                                                     \
196     ufuncs->get.name = hb_unicode_get_##name##_nil;                            \
197     ufuncs->user_data.name = NULL;                                             \
198     ufuncs->destroy.name = NULL;                                               \
199   }                                                                            \
200 }                                                                              \
201                                                                                \
202 return_type                                                                    \
203 hb_unicode_get_##name (hb_unicode_funcs_t *ufuncs,                             \
204                        hb_codepoint_t      unicode)                            \
205 {                                                                              \
206   return ufuncs->get.name (ufuncs, unicode, ufuncs->user_data.name);           \
207 }
208
209 IMPLEMENT (unsigned int, combining_class)
210 IMPLEMENT (unsigned int, eastasian_width)
211 IMPLEMENT (hb_unicode_general_category_t, general_category)
212 IMPLEMENT (hb_codepoint_t, mirroring)
213 IMPLEMENT (hb_script_t, script)
214
215 #undef IMPLEMENT
216
217
218 HB_END_DECLS