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 to provide Unicode data.
45 **/
46
47
48#if !GLIB_CHECK_VERSION(2,29,14)
49static const hb_script_t
50glib_script_to_script[] =
51{
52 HB_SCRIPT_COMMON,
53 HB_SCRIPT_INHERITED,
54 HB_SCRIPT_ARABIC,
55 HB_SCRIPT_ARMENIAN,
56 HB_SCRIPT_BENGALI,
57 HB_SCRIPT_BOPOMOFO,
58 HB_SCRIPT_CHEROKEE,
59 HB_SCRIPT_COPTIC,
60 HB_SCRIPT_CYRILLIC,
61 HB_SCRIPT_DESERET,
62 HB_SCRIPT_DEVANAGARI,
63 HB_SCRIPT_ETHIOPIC,
64 HB_SCRIPT_GEORGIAN,
65 HB_SCRIPT_GOTHIC,
66 HB_SCRIPT_GREEK,
67 HB_SCRIPT_GUJARATI,
68 HB_SCRIPT_GURMUKHI,
69 HB_SCRIPT_HAN,
70 HB_SCRIPT_HANGUL,
71 HB_SCRIPT_HEBREW,
72 HB_SCRIPT_HIRAGANA,
73 HB_SCRIPT_KANNADA,
74 HB_SCRIPT_KATAKANA,
75 HB_SCRIPT_KHMER,
76 HB_SCRIPT_LAO,
77 HB_SCRIPT_LATIN,
78 HB_SCRIPT_MALAYALAM,
79 HB_SCRIPT_MONGOLIAN,
80 HB_SCRIPT_MYANMAR,
81 HB_SCRIPT_OGHAM,
82 HB_SCRIPT_OLD_ITALIC,
83 HB_SCRIPT_ORIYA,
84 HB_SCRIPT_RUNIC,
85 HB_SCRIPT_SINHALA,
86 HB_SCRIPT_SYRIAC,
87 HB_SCRIPT_TAMIL,
88 HB_SCRIPT_TELUGU,
89 HB_SCRIPT_THAANA,
90 HB_SCRIPT_THAI,
91 HB_SCRIPT_TIBETAN,
92 HB_SCRIPT_CANADIAN_SYLLABICS,
93 HB_SCRIPT_YI,
94 HB_SCRIPT_TAGALOG,
95 HB_SCRIPT_HANUNOO,
96 HB_SCRIPT_BUHID,
97 HB_SCRIPT_TAGBANWA,
98
99 /* Unicode-4.0 additions */
100 HB_SCRIPT_BRAILLE,
101 HB_SCRIPT_CYPRIOT,
102 HB_SCRIPT_LIMBU,
103 HB_SCRIPT_OSMANYA,
104 HB_SCRIPT_SHAVIAN,
105 HB_SCRIPT_LINEAR_B,
106 HB_SCRIPT_TAI_LE,
107 HB_SCRIPT_UGARITIC,
108
109 /* Unicode-4.1 additions */
110 HB_SCRIPT_NEW_TAI_LUE,
111 HB_SCRIPT_BUGINESE,
112 HB_SCRIPT_GLAGOLITIC,
113 HB_SCRIPT_TIFINAGH,
114 HB_SCRIPT_SYLOTI_NAGRI,
115 HB_SCRIPT_OLD_PERSIAN,
116 HB_SCRIPT_KHAROSHTHI,
117
118 /* Unicode-5.0 additions */
119 HB_SCRIPT_UNKNOWN,
120 HB_SCRIPT_BALINESE,
121 HB_SCRIPT_CUNEIFORM,
122 HB_SCRIPT_PHOENICIAN,
123 HB_SCRIPT_PHAGS_PA,
124 HB_SCRIPT_NKO,
125
126 /* Unicode-5.1 additions */
127 HB_SCRIPT_KAYAH_LI,
128 HB_SCRIPT_LEPCHA,
129 HB_SCRIPT_REJANG,
130 HB_SCRIPT_SUNDANESE,
131 HB_SCRIPT_SAURASHTRA,
132 HB_SCRIPT_CHAM,
133 HB_SCRIPT_OL_CHIKI,
134 HB_SCRIPT_VAI,
135 HB_SCRIPT_CARIAN,
136 HB_SCRIPT_LYCIAN,
137 HB_SCRIPT_LYDIAN,
138
139 /* Unicode-5.2 additions */
140 HB_SCRIPT_AVESTAN,
141 HB_SCRIPT_BAMUM,
142 HB_SCRIPT_EGYPTIAN_HIEROGLYPHS,
143 HB_SCRIPT_IMPERIAL_ARAMAIC,
144 HB_SCRIPT_INSCRIPTIONAL_PAHLAVI,
145 HB_SCRIPT_INSCRIPTIONAL_PARTHIAN,
146 HB_SCRIPT_JAVANESE,
147 HB_SCRIPT_KAITHI,
148 HB_SCRIPT_TAI_THAM,
149 HB_SCRIPT_LISU,
150 HB_SCRIPT_MEETEI_MAYEK,
151 HB_SCRIPT_OLD_SOUTH_ARABIAN,
152 HB_SCRIPT_OLD_TURKIC,
153 HB_SCRIPT_SAMARITAN,
154 HB_SCRIPT_TAI_VIET,
155
156 /* Unicode-6.0 additions */
157 HB_SCRIPT_BATAK,
158 HB_SCRIPT_BRAHMI,
159 HB_SCRIPT_MANDAIC,
160
161 /* Unicode-6.1 additions */
162 HB_SCRIPT_CHAKMA,
163 HB_SCRIPT_MEROITIC_CURSIVE,
164 HB_SCRIPT_MEROITIC_HIEROGLYPHS,
165 HB_SCRIPT_MIAO,
166 HB_SCRIPT_SHARADA,
167 HB_SCRIPT_SORA_SOMPENG,
168 HB_SCRIPT_TAKRI
169};
170#endif
171
172hb_script_t
173hb_glib_script_to_script (GUnicodeScript script)
174{
175#if GLIB_CHECK_VERSION(2,29,14)
176 return (hb_script_t) g_unicode_script_to_iso15924 (script);
177#else
178 if (likely ((unsigned int) script < ARRAY_LENGTH (glib_script_to_script)))
179 return glib_script_to_script[script];
180
181 if (unlikely (script == G_UNICODE_SCRIPT_INVALID_CODE))
182 return HB_SCRIPT_INVALID;
183
184 return HB_SCRIPT_UNKNOWN;
185#endif
186}
187
188GUnicodeScript
189hb_glib_script_from_script (hb_script_t script)
190{
191#if GLIB_CHECK_VERSION(2,29,14)
192 return g_unicode_script_from_iso15924 (script);
193#else
194 unsigned int count = ARRAY_LENGTH (glib_script_to_script);
195 for (unsigned int i = 0; i < count; i++)
196 if (glib_script_to_script[i] == script)
197 return (GUnicodeScript) i;
198
199 if (unlikely (script == HB_SCRIPT_INVALID))
200 return G_UNICODE_SCRIPT_INVALID_CODE;
201
202 return G_UNICODE_SCRIPT_UNKNOWN;
203#endif
204}
205
206
207static hb_unicode_combining_class_t
208hb_glib_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED,
209 hb_codepoint_t unicode,
210 void *user_data HB_UNUSED)
211
212{
213 return (hb_unicode_combining_class_t) g_unichar_combining_class (unicode);
214}
215
216static hb_unicode_general_category_t
217hb_glib_unicode_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED,
218 hb_codepoint_t unicode,
219 void *user_data HB_UNUSED)
220
221{
222 /* hb_unicode_general_category_t and GUnicodeType are identical */
223 return (hb_unicode_general_category_t) g_unichar_type (unicode);
224}
225
226static hb_codepoint_t
227hb_glib_unicode_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED,
228 hb_codepoint_t unicode,
229 void *user_data HB_UNUSED)
230{
231 g_unichar_get_mirror_char (unicode, &unicode);
232 return unicode;
233}
234
235static hb_script_t
236hb_glib_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
237 hb_codepoint_t unicode,
238 void *user_data HB_UNUSED)
239{
240 return hb_glib_script_to_script (g_unichar_get_script (unicode));
241}
242
243static hb_bool_t
244hb_glib_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
245 hb_codepoint_t a,
246 hb_codepoint_t b,
247 hb_codepoint_t *ab,
248 void *user_data HB_UNUSED)
249{
250#if GLIB_CHECK_VERSION(2,29,12)
251 return g_unichar_compose (a, b, ab);
252#endif
253
254 /* We don't ifdef-out the fallback code such that compiler always
255 * sees it and makes sure it's compilable. */
256
257 gchar utf8[12];
258 gchar *normalized;
259 int len;
260 hb_bool_t ret;
261
262 len = g_unichar_to_utf8 (a, utf8);
263 len += g_unichar_to_utf8 (b, utf8 + len);
264 normalized = g_utf8_normalize (utf8, len, G_NORMALIZE_NFC);
265 len = g_utf8_strlen (normalized, -1);
266 if (unlikely (!len))
267 return false;
268
269 if (len == 1) {
270 *ab = g_utf8_get_char (normalized);
271 ret = true;
272 } else {
273 ret = false;
274 }
275
276 g_free (normalized);
277 return ret;
278}
279
280static hb_bool_t
281hb_glib_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
282 hb_codepoint_t ab,
283 hb_codepoint_t *a,
284 hb_codepoint_t *b,
285 void *user_data HB_UNUSED)
286{
287#if GLIB_CHECK_VERSION(2,29,12)
288 return g_unichar_decompose (ab, a, b);
289#endif
290
291 /* We don't ifdef-out the fallback code such that compiler always
292 * sees it and makes sure it's compilable. */
293
294 gchar utf8[6];
295 gchar *normalized;
296 int len;
297 hb_bool_t ret;
298
299 len = g_unichar_to_utf8 (ab, utf8);
300 normalized = g_utf8_normalize (utf8, len, G_NORMALIZE_NFD);
301 len = g_utf8_strlen (normalized, -1);
302 if (unlikely (!len))
303 return false;
304
305 if (len == 1) {
306 *a = g_utf8_get_char (normalized);
307 *b = 0;
308 ret = *a != ab;
309 } else if (len == 2) {
310 *a = g_utf8_get_char (normalized);
311 *b = g_utf8_get_char (g_utf8_next_char (normalized));
312 /* Here's the ugly part: if ab decomposes to a single character and
313 * that character decomposes again, we have to detect that and undo
314 * the second part :-(. */
315 gchar *recomposed = g_utf8_normalize (normalized, -1, G_NORMALIZE_NFC);
316 hb_codepoint_t c = g_utf8_get_char (recomposed);
317 if (c != ab && c != *a) {
318 *a = c;
319 *b = 0;
320 }
321 g_free (recomposed);
322 ret = true;
323 } else {
324 /* If decomposed to more than two characters, take the last one,
325 * and recompose the rest to get the first component. */
326 gchar *end = g_utf8_offset_to_pointer (normalized, len - 1);
327 gchar *recomposed;
328 *b = g_utf8_get_char (end);
329 recomposed = g_utf8_normalize (normalized, end - normalized, G_NORMALIZE_NFC);
330 /* We expect that recomposed has exactly one character now. */
331 *a = g_utf8_get_char (recomposed);
332 g_free (recomposed);
333 ret = true;
334 }
335
336 g_free (normalized);
337 return ret;
338}
339
340
341#if HB_USE_ATEXIT
342static void free_static_glib_funcs ();
343#endif
344
345static struct hb_glib_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_glib_unicode_funcs_lazy_loader_t>
346{
347 static hb_unicode_funcs_t *create ()
348 {
349 hb_unicode_funcs_t *funcs = hb_unicode_funcs_create (nullptr);
350
351 hb_unicode_funcs_set_combining_class_func (funcs, hb_glib_unicode_combining_class, nullptr, nullptr);
352 hb_unicode_funcs_set_general_category_func (funcs, hb_glib_unicode_general_category, nullptr, nullptr);
353 hb_unicode_funcs_set_mirroring_func (funcs, hb_glib_unicode_mirroring, nullptr, nullptr);
354 hb_unicode_funcs_set_script_func (funcs, hb_glib_unicode_script, nullptr, nullptr);
355 hb_unicode_funcs_set_compose_func (funcs, hb_glib_unicode_compose, nullptr, nullptr);
356 hb_unicode_funcs_set_decompose_func (funcs, hb_glib_unicode_decompose, nullptr, nullptr);
357
358 hb_unicode_funcs_make_immutable (funcs);
359
360#if HB_USE_ATEXIT
361 atexit (free_static_glib_funcs);
362#endif
363
364 return funcs;
365 }
366} static_glib_funcs;
367
368#if HB_USE_ATEXIT
369static
370void free_static_glib_funcs ()
371{
372 static_glib_funcs.free_instance ();
373}
374#endif
375
376hb_unicode_funcs_t *
377hb_glib_get_unicode_funcs ()
378{
379 return static_glib_funcs.get_unconst ();
380}
381
382
383
384#if GLIB_CHECK_VERSION(2,31,10)
385
386static void
387_hb_g_bytes_unref (void *data)
388{
389 g_bytes_unref ((GBytes *) data);
390}
391
392/**
393 * hb_glib_blob_create:
394 *
395 * Since: 0.9.38
396 **/
397hb_blob_t *
398hb_glib_blob_create (GBytes *gbytes)
399{
400 gsize size = 0;
401 gconstpointer data = g_bytes_get_data (gbytes, &size);
402 return hb_blob_create ((const char *) data,
403 size,
404 HB_MEMORY_MODE_READONLY,
405 g_bytes_ref (gbytes),
406 _hb_g_bytes_unref);
407}
408#endif
409
410
411#endif
412