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