1 /*
2 * Copyright © 2009 Red Hat, Inc.
3 * Copyright © 2011 Google, Inc.
4 *
5 * This is part of HarfBuzz, a text shaping library.
6 *
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
12 *
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 *
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 *
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
27 */
28
29 #include "hb.hh"
30
31 #ifdef HAVE_GLIB
32
33 #include "hb-glib.h"
34
35 #include "hb-machinery.hh"
36
37
38 /**
39 * SECTION:hb-glib
40 * @title: hb-glib
41 * @short_description: GLib integration
42 * @include: hb-glib.h
43 *
44 * Functions for using HarfBuzz with the GLib library.
45 *
46 * HarfBuzz supports using GLib to provide Unicode data, by attaching
47 * GLib functions to the virtual methods in a #hb_unicode_funcs_t function
48 * structure.
49 **/
50
51
52 /**
53 * hb_glib_script_to_script:
54 * @script: The GUnicodeScript identifier to query
55 *
56 * Fetches the #hb_script_t script that corresponds to the
57 * specified GUnicodeScript identifier.
58 *
59 * Return value: the #hb_script_t script found
60 *
61 * Since: 0.9.38
62 **/
63 hb_script_t
hb_glib_script_to_script(GUnicodeScript script)64 hb_glib_script_to_script (GUnicodeScript script)
65 {
66 return (hb_script_t) g_unicode_script_to_iso15924 (script);
67 }
68
69 /**
70 * hb_glib_script_from_script:
71 * @script: The #hb_script_t to query
72 *
73 * Fetches the GUnicodeScript identifier that corresponds to the
74 * specified #hb_script_t script.
75 *
76 * Return value: the GUnicodeScript identifier found
77 *
78 * Since: 0.9.38
79 **/
80 GUnicodeScript
hb_glib_script_from_script(hb_script_t script)81 hb_glib_script_from_script (hb_script_t script)
82 {
83 return g_unicode_script_from_iso15924 (script);
84 }
85
86
87 static hb_unicode_combining_class_t
hb_glib_unicode_combining_class(hb_unicode_funcs_t * ufuncs HB_UNUSED,hb_codepoint_t unicode,void * user_data HB_UNUSED)88 hb_glib_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED,
89 hb_codepoint_t unicode,
90 void *user_data HB_UNUSED)
91
92 {
93 return (hb_unicode_combining_class_t) g_unichar_combining_class (unicode);
94 }
95
96 static hb_unicode_general_category_t
hb_glib_unicode_general_category(hb_unicode_funcs_t * ufuncs HB_UNUSED,hb_codepoint_t unicode,void * user_data HB_UNUSED)97 hb_glib_unicode_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED,
98 hb_codepoint_t unicode,
99 void *user_data HB_UNUSED)
100
101 {
102 /* hb_unicode_general_category_t and GUnicodeType are identical */
103 return (hb_unicode_general_category_t) g_unichar_type (unicode);
104 }
105
106 static hb_codepoint_t
hb_glib_unicode_mirroring(hb_unicode_funcs_t * ufuncs HB_UNUSED,hb_codepoint_t unicode,void * user_data HB_UNUSED)107 hb_glib_unicode_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED,
108 hb_codepoint_t unicode,
109 void *user_data HB_UNUSED)
110 {
111 g_unichar_get_mirror_char (unicode, &unicode);
112 return unicode;
113 }
114
115 static hb_script_t
hb_glib_unicode_script(hb_unicode_funcs_t * ufuncs HB_UNUSED,hb_codepoint_t unicode,void * user_data HB_UNUSED)116 hb_glib_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
117 hb_codepoint_t unicode,
118 void *user_data HB_UNUSED)
119 {
120 return hb_glib_script_to_script (g_unichar_get_script (unicode));
121 }
122
123 static hb_bool_t
hb_glib_unicode_compose(hb_unicode_funcs_t * ufuncs HB_UNUSED,hb_codepoint_t a,hb_codepoint_t b,hb_codepoint_t * ab,void * user_data HB_UNUSED)124 hb_glib_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
125 hb_codepoint_t a,
126 hb_codepoint_t b,
127 hb_codepoint_t *ab,
128 void *user_data HB_UNUSED)
129 {
130 #if GLIB_CHECK_VERSION(2,29,12)
131 return g_unichar_compose (a, b, ab);
132 #endif
133
134 /* We don't ifdef-out the fallback code such that compiler always
135 * sees it and makes sure it's compilable. */
136
137 gchar utf8[12];
138 gchar *normalized;
139 int len;
140 hb_bool_t ret;
141
142 len = g_unichar_to_utf8 (a, utf8);
143 len += g_unichar_to_utf8 (b, utf8 + len);
144 normalized = g_utf8_normalize (utf8, len, G_NORMALIZE_NFC);
145 len = g_utf8_strlen (normalized, -1);
146 if (unlikely (!len))
147 return false;
148
149 if (len == 1) {
150 *ab = g_utf8_get_char (normalized);
151 ret = true;
152 } else {
153 ret = false;
154 }
155
156 g_free (normalized);
157 return ret;
158 }
159
160 static hb_bool_t
hb_glib_unicode_decompose(hb_unicode_funcs_t * ufuncs HB_UNUSED,hb_codepoint_t ab,hb_codepoint_t * a,hb_codepoint_t * b,void * user_data HB_UNUSED)161 hb_glib_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
162 hb_codepoint_t ab,
163 hb_codepoint_t *a,
164 hb_codepoint_t *b,
165 void *user_data HB_UNUSED)
166 {
167 #if GLIB_CHECK_VERSION(2,29,12)
168 return g_unichar_decompose (ab, a, b);
169 #endif
170
171 /* We don't ifdef-out the fallback code such that compiler always
172 * sees it and makes sure it's compilable. */
173
174 gchar utf8[6];
175 gchar *normalized;
176 int len;
177 hb_bool_t ret;
178
179 len = g_unichar_to_utf8 (ab, utf8);
180 normalized = g_utf8_normalize (utf8, len, G_NORMALIZE_NFD);
181 len = g_utf8_strlen (normalized, -1);
182 if (unlikely (!len))
183 return false;
184
185 if (len == 1) {
186 *a = g_utf8_get_char (normalized);
187 *b = 0;
188 ret = *a != ab;
189 } else if (len == 2) {
190 *a = g_utf8_get_char (normalized);
191 *b = g_utf8_get_char (g_utf8_next_char (normalized));
192 /* Here's the ugly part: if ab decomposes to a single character and
193 * that character decomposes again, we have to detect that and undo
194 * the second part :-(. */
195 gchar *recomposed = g_utf8_normalize (normalized, -1, G_NORMALIZE_NFC);
196 hb_codepoint_t c = g_utf8_get_char (recomposed);
197 if (c != ab && c != *a) {
198 *a = c;
199 *b = 0;
200 }
201 g_free (recomposed);
202 ret = true;
203 } else {
204 /* If decomposed to more than two characters, take the last one,
205 * and recompose the rest to get the first component. */
206 gchar *end = g_utf8_offset_to_pointer (normalized, len - 1);
207 gchar *recomposed;
208 *b = g_utf8_get_char (end);
209 recomposed = g_utf8_normalize (normalized, end - normalized, G_NORMALIZE_NFC);
210 /* We expect that recomposed has exactly one character now. */
211 *a = g_utf8_get_char (recomposed);
212 g_free (recomposed);
213 ret = true;
214 }
215
216 g_free (normalized);
217 return ret;
218 }
219
220
221 static inline void free_static_glib_funcs ();
222
223 static struct hb_glib_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_glib_unicode_funcs_lazy_loader_t>
224 {
createhb_glib_unicode_funcs_lazy_loader_t225 static hb_unicode_funcs_t *create ()
226 {
227 hb_unicode_funcs_t *funcs = hb_unicode_funcs_create (nullptr);
228
229 hb_unicode_funcs_set_combining_class_func (funcs, hb_glib_unicode_combining_class, nullptr, nullptr);
230 hb_unicode_funcs_set_general_category_func (funcs, hb_glib_unicode_general_category, nullptr, nullptr);
231 hb_unicode_funcs_set_mirroring_func (funcs, hb_glib_unicode_mirroring, nullptr, nullptr);
232 hb_unicode_funcs_set_script_func (funcs, hb_glib_unicode_script, nullptr, nullptr);
233 hb_unicode_funcs_set_compose_func (funcs, hb_glib_unicode_compose, nullptr, nullptr);
234 hb_unicode_funcs_set_decompose_func (funcs, hb_glib_unicode_decompose, nullptr, nullptr);
235
236 hb_unicode_funcs_make_immutable (funcs);
237
238 hb_atexit (free_static_glib_funcs);
239
240 return funcs;
241 }
242 } static_glib_funcs;
243
244 static inline
free_static_glib_funcs()245 void free_static_glib_funcs ()
246 {
247 static_glib_funcs.free_instance ();
248 }
249
250 /**
251 * hb_glib_get_unicode_funcs:
252 *
253 * Fetches a Unicode-functions structure that is populated
254 * with the appropriate GLib function for each method.
255 *
256 * Return value: (transfer none): a pointer to the #hb_unicode_funcs_t Unicode-functions structure
257 *
258 * Since: 0.9.38
259 **/
260 hb_unicode_funcs_t *
hb_glib_get_unicode_funcs()261 hb_glib_get_unicode_funcs ()
262 {
263 return static_glib_funcs.get_unconst ();
264 }
265
266
267
268 #if GLIB_CHECK_VERSION(2,31,10)
269
270 static void
_hb_g_bytes_unref(void * data)271 _hb_g_bytes_unref (void *data)
272 {
273 g_bytes_unref ((GBytes *) data);
274 }
275
276 /**
277 * hb_glib_blob_create:
278 * @gbytes: the GBytes structure to work upon
279 *
280 * Creates an #hb_blob_t blob from the specified
281 * GBytes data structure.
282 *
283 * Return value: (transfer full): the new #hb_blob_t blob object
284 *
285 * Since: 0.9.38
286 **/
287 hb_blob_t *
hb_glib_blob_create(GBytes * gbytes)288 hb_glib_blob_create (GBytes *gbytes)
289 {
290 gsize size = 0;
291 gconstpointer data = g_bytes_get_data (gbytes, &size);
292 return hb_blob_create ((const char *) data,
293 size,
294 HB_MEMORY_MODE_READONLY,
295 g_bytes_ref (gbytes),
296 _hb_g_bytes_unref);
297 }
298 #endif
299
300
301 #endif
302