2 * Copyright © 2009 Red Hat, Inc.
3 * Copyright © 2011 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
35 #include "hb-machinery.hh"
41 * @short_description: GLib integration
44 * Functions for using HarfBuzz with the GLib library to provide Unicode data.
48 #if !GLIB_CHECK_VERSION(2,29,14)
49 static const hb_script_t
50 glib_script_to_script[] =
92 HB_SCRIPT_CANADIAN_SYLLABICS,
99 /* Unicode-4.0 additions */
109 /* Unicode-4.1 additions */
110 HB_SCRIPT_NEW_TAI_LUE,
112 HB_SCRIPT_GLAGOLITIC,
114 HB_SCRIPT_SYLOTI_NAGRI,
115 HB_SCRIPT_OLD_PERSIAN,
116 HB_SCRIPT_KHAROSHTHI,
118 /* Unicode-5.0 additions */
122 HB_SCRIPT_PHOENICIAN,
126 /* Unicode-5.1 additions */
131 HB_SCRIPT_SAURASHTRA,
139 /* Unicode-5.2 additions */
142 HB_SCRIPT_EGYPTIAN_HIEROGLYPHS,
143 HB_SCRIPT_IMPERIAL_ARAMAIC,
144 HB_SCRIPT_INSCRIPTIONAL_PAHLAVI,
145 HB_SCRIPT_INSCRIPTIONAL_PARTHIAN,
150 HB_SCRIPT_MEETEI_MAYEK,
151 HB_SCRIPT_OLD_SOUTH_ARABIAN,
152 HB_SCRIPT_OLD_TURKIC,
156 /* Unicode-6.0 additions */
161 /* Unicode-6.1 additions */
163 HB_SCRIPT_MEROITIC_CURSIVE,
164 HB_SCRIPT_MEROITIC_HIEROGLYPHS,
167 HB_SCRIPT_SORA_SOMPENG,
173 hb_glib_script_to_script (GUnicodeScript script)
175 #if GLIB_CHECK_VERSION(2,29,14)
176 return (hb_script_t) g_unicode_script_to_iso15924 (script);
178 if (likely ((unsigned int) script < ARRAY_LENGTH (glib_script_to_script)))
179 return glib_script_to_script[script];
181 if (unlikely (script == G_UNICODE_SCRIPT_INVALID_CODE))
182 return HB_SCRIPT_INVALID;
184 return HB_SCRIPT_UNKNOWN;
189 hb_glib_script_from_script (hb_script_t script)
191 #if GLIB_CHECK_VERSION(2,29,14)
192 return g_unicode_script_from_iso15924 (script);
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;
199 if (unlikely (script == HB_SCRIPT_INVALID))
200 return G_UNICODE_SCRIPT_INVALID_CODE;
202 return G_UNICODE_SCRIPT_UNKNOWN;
207 static hb_unicode_combining_class_t
208 hb_glib_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED,
209 hb_codepoint_t unicode,
210 void *user_data HB_UNUSED)
213 return (hb_unicode_combining_class_t) g_unichar_combining_class (unicode);
216 static hb_unicode_general_category_t
217 hb_glib_unicode_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED,
218 hb_codepoint_t unicode,
219 void *user_data HB_UNUSED)
222 /* hb_unicode_general_category_t and GUnicodeType are identical */
223 return (hb_unicode_general_category_t) g_unichar_type (unicode);
226 static hb_codepoint_t
227 hb_glib_unicode_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED,
228 hb_codepoint_t unicode,
229 void *user_data HB_UNUSED)
231 g_unichar_get_mirror_char (unicode, &unicode);
236 hb_glib_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
237 hb_codepoint_t unicode,
238 void *user_data HB_UNUSED)
240 return hb_glib_script_to_script (g_unichar_get_script (unicode));
244 hb_glib_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
248 void *user_data HB_UNUSED)
250 #if GLIB_CHECK_VERSION(2,29,12)
251 return g_unichar_compose (a, b, ab);
254 /* We don't ifdef-out the fallback code such that compiler always
255 * sees it and makes sure it's compilable. */
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);
270 *ab = g_utf8_get_char (normalized);
281 hb_glib_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
285 void *user_data HB_UNUSED)
287 #if GLIB_CHECK_VERSION(2,29,12)
288 return g_unichar_decompose (ab, a, b);
291 /* We don't ifdef-out the fallback code such that compiler always
292 * sees it and makes sure it's compilable. */
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);
306 *a = g_utf8_get_char (normalized);
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) {
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);
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);
342 static void free_static_glib_funcs ();
345 static struct hb_glib_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_glib_unicode_funcs_lazy_loader_t>
347 static hb_unicode_funcs_t *create ()
349 hb_unicode_funcs_t *funcs = hb_unicode_funcs_create (nullptr);
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);
358 hb_unicode_funcs_make_immutable (funcs);
361 atexit (free_static_glib_funcs);
370 void free_static_glib_funcs ()
372 static_glib_funcs.free_instance ();
377 hb_glib_get_unicode_funcs ()
379 return static_glib_funcs.get_unconst ();
384 #if GLIB_CHECK_VERSION(2,31,10)
387 _hb_g_bytes_unref (void *data)
389 g_bytes_unref ((GBytes *) data);
393 * hb_glib_blob_create:
398 hb_glib_blob_create (GBytes *gbytes)
401 gconstpointer data = g_bytes_get_data (gbytes, &size);
402 return hb_blob_create ((const char *) data,
404 HB_MEMORY_MODE_READONLY,
405 g_bytes_ref (gbytes),