blob: 992295540bd969727a75060a1d903651e091eada [file] [log] [blame]
Behdad Esfahbodfb194b82011-04-20 02:00:47 -04001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 2009 Red Hat, Inc.
Behdad Esfahbodfb194b82011-04-20 02:00:47 -04003 * Copyright © 2011 Codethink Limited
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04004 * Copyright © 2010,2011 Google, Inc.
Behdad Esfahbodfb194b82011-04-20 02:00:47 -04005 *
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
Behdad Esfahbodc57d4542011-04-20 18:50:27 -040031#include "hb-private.hh"
Behdad Esfahbodfb194b82011-04-20 02:00:47 -040032
33#include "hb-unicode-private.hh"
34
35HB_BEGIN_DECLS
36
37
38/*
39 * hb_unicode_funcs_t
40 */
41
42static unsigned int
43hb_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
50static unsigned int
51hb_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
58static hb_unicode_general_category_t
59hb_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
66static hb_codepoint_t
67hb_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
74static hb_script_t
75hb_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
Behdad Esfahbodd4bee9f2011-04-27 09:24:37 -040083extern HB_INTERNAL hb_unicode_funcs_t _hb_unicode_funcs_nil;
Behdad Esfahbodfb194b82011-04-20 02:00:47 -040084hb_unicode_funcs_t _hb_unicode_funcs_nil = {
Behdad Esfahbodfca368c2011-04-21 18:24:02 -040085 HB_OBJECT_HEADER_STATIC,
86
Behdad Esfahbodfb194b82011-04-20 02:00:47 -040087 NULL, /* parent */
88 TRUE, /* immutable */
89 {
90 hb_unicode_get_combining_class_nil,
91 hb_unicode_get_eastasian_width_nil,
92 hb_unicode_get_general_category_nil,
93 hb_unicode_get_mirroring_nil,
94 hb_unicode_get_script_nil,
95 }
96};
97
98
99hb_unicode_funcs_t *
Behdad Esfahbodd4bee9f2011-04-27 09:24:37 -0400100hb_unicode_funcs_get_default (void)
101{
102 return &_hb_unicode_funcs_default;
103}
104
105hb_unicode_funcs_t *
Behdad Esfahbodfb194b82011-04-20 02:00:47 -0400106hb_unicode_funcs_create (hb_unicode_funcs_t *parent)
107{
108 hb_unicode_funcs_t *ufuncs;
109
Behdad Esfahbod47e71d92011-04-27 16:38:03 -0400110 if (!(ufuncs = hb_object_create<hb_unicode_funcs_t> ()))
Behdad Esfahbodfb194b82011-04-20 02:00:47 -0400111 return &_hb_unicode_funcs_nil;
112
113 if (parent != NULL)
114 {
115 hb_unicode_funcs_make_immutable (parent);
116 ufuncs->parent = hb_unicode_funcs_reference (parent);
117
118 ufuncs->get = parent->get;
119
120 /* We can safely copy user_data from parent since we hold a reference
121 * onto it and it's immutable. We should not copy the destroy notifiers
122 * though. */
123 ufuncs->user_data = parent->user_data;
124 }
125 else
126 {
127 ufuncs->get = _hb_unicode_funcs_nil.get;
128 }
129
130 return ufuncs;
131}
132
133hb_unicode_funcs_t *
134hb_unicode_funcs_reference (hb_unicode_funcs_t *ufuncs)
135{
Behdad Esfahbod47e71d92011-04-27 16:38:03 -0400136 return hb_object_reference (ufuncs);
Behdad Esfahbodfb194b82011-04-20 02:00:47 -0400137}
138
Behdad Esfahbodfb194b82011-04-20 02:00:47 -0400139void
140hb_unicode_funcs_destroy (hb_unicode_funcs_t *ufuncs)
141{
Behdad Esfahbod47e71d92011-04-27 16:38:03 -0400142 if (!hb_object_destroy (ufuncs)) return;
Behdad Esfahbodfb194b82011-04-20 02:00:47 -0400143
144#define DESTROY(name) if (ufuncs->destroy.name) ufuncs->destroy.name (ufuncs->user_data.name)
145 DESTROY (combining_class);
146 DESTROY (eastasian_width);
147 DESTROY (general_category);
148 DESTROY (mirroring);
149 DESTROY (script);
150#undef DESTROY
151
152 if (ufuncs->parent != NULL)
153 hb_unicode_funcs_destroy (ufuncs->parent);
154
155 free (ufuncs);
156}
157
158void
159hb_unicode_funcs_make_immutable (hb_unicode_funcs_t *ufuncs)
160{
Behdad Esfahbod47e71d92011-04-27 16:38:03 -0400161 if (hb_object_is_inert (ufuncs))
Behdad Esfahbodfb194b82011-04-20 02:00:47 -0400162 return;
163
164 ufuncs->immutable = TRUE;
165}
166
167hb_bool_t
168hb_unicode_funcs_is_immutable (hb_unicode_funcs_t *ufuncs)
169{
170 return ufuncs->immutable;
171}
172
173hb_unicode_funcs_t *
174hb_unicode_funcs_get_parent (hb_unicode_funcs_t *ufuncs)
175{
176 return ufuncs->parent;
177}
178
179
180#define IMPLEMENT(return_type, name) \
181 \
182void \
183hb_unicode_funcs_set_##name##_func (hb_unicode_funcs_t *ufuncs, \
184 hb_unicode_get_##name##_func_t func, \
185 void *user_data, \
186 hb_destroy_func_t destroy) \
187{ \
188 if (ufuncs->immutable) \
189 return; \
190 \
191 if (ufuncs->destroy.name) \
192 ufuncs->destroy.name (ufuncs->user_data.name); \
193 \
194 if (func) { \
195 ufuncs->get.name = func; \
196 ufuncs->user_data.name = user_data; \
197 ufuncs->destroy.name = destroy; \
198 } else if (ufuncs->parent != NULL) { \
199 ufuncs->get.name = ufuncs->parent->get.name; \
200 ufuncs->user_data.name = ufuncs->parent->user_data.name; \
201 ufuncs->destroy.name = NULL; \
202 } else { \
203 ufuncs->get.name = hb_unicode_get_##name##_nil; \
204 ufuncs->user_data.name = NULL; \
205 ufuncs->destroy.name = NULL; \
206 } \
207} \
208 \
209return_type \
210hb_unicode_get_##name (hb_unicode_funcs_t *ufuncs, \
211 hb_codepoint_t unicode) \
212{ \
213 return ufuncs->get.name (ufuncs, unicode, ufuncs->user_data.name); \
214}
215
216IMPLEMENT (unsigned int, combining_class)
217IMPLEMENT (unsigned int, eastasian_width)
218IMPLEMENT (hb_unicode_general_category_t, general_category)
219IMPLEMENT (hb_codepoint_t, mirroring)
220IMPLEMENT (hb_script_t, script)
221
222#undef IMPLEMENT
223
224
225HB_END_DECLS