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.
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
52 #if !GLIB_CHECK_VERSION(2,29,14)
53 static const hb_script_t
54 glib_script_to_script[] =
96 HB_SCRIPT_CANADIAN_SYLLABICS,
103 /* Unicode-4.0 additions */
113 /* Unicode-4.1 additions */
114 HB_SCRIPT_NEW_TAI_LUE,
116 HB_SCRIPT_GLAGOLITIC,
118 HB_SCRIPT_SYLOTI_NAGRI,
119 HB_SCRIPT_OLD_PERSIAN,
120 HB_SCRIPT_KHAROSHTHI,
122 /* Unicode-5.0 additions */
126 HB_SCRIPT_PHOENICIAN,
130 /* Unicode-5.1 additions */
135 HB_SCRIPT_SAURASHTRA,
143 /* Unicode-5.2 additions */
146 HB_SCRIPT_EGYPTIAN_HIEROGLYPHS,
147 HB_SCRIPT_IMPERIAL_ARAMAIC,
148 HB_SCRIPT_INSCRIPTIONAL_PAHLAVI,
149 HB_SCRIPT_INSCRIPTIONAL_PARTHIAN,
154 HB_SCRIPT_MEETEI_MAYEK,
155 HB_SCRIPT_OLD_SOUTH_ARABIAN,
156 HB_SCRIPT_OLD_TURKIC,
160 /* Unicode-6.0 additions */
165 /* Unicode-6.1 additions */
167 HB_SCRIPT_MEROITIC_CURSIVE,
168 HB_SCRIPT_MEROITIC_HIEROGLYPHS,
171 HB_SCRIPT_SORA_SOMPENG,
177 * hb_glib_script_to_script:
178 * @script: The GUnicodeScript identifier to query
180 * Fetches the #hb_script_t script that corresponds to the
181 * specified GUnicodeScript identifier.
183 * Return value: the #hb_script_t script found
188 hb_glib_script_to_script (GUnicodeScript script)
190 #if GLIB_CHECK_VERSION(2,29,14)
191 return (hb_script_t) g_unicode_script_to_iso15924 (script);
193 if (likely ((unsigned int) script < ARRAY_LENGTH (glib_script_to_script)))
194 return glib_script_to_script[script];
196 if (unlikely (script == G_UNICODE_SCRIPT_INVALID_CODE))
197 return HB_SCRIPT_INVALID;
199 return HB_SCRIPT_UNKNOWN;
204 * hb_glib_script_from_script:
205 * @script: The #hb_script_t to query
207 * Fetches the GUnicodeScript identifier that corresponds to the
208 * specified #hb_script_t script.
210 * Return value: the GUnicodeScript identifier found
215 hb_glib_script_from_script (hb_script_t script)
217 #if GLIB_CHECK_VERSION(2,29,14)
218 return g_unicode_script_from_iso15924 (script);
220 unsigned int count = ARRAY_LENGTH (glib_script_to_script);
221 for (unsigned int i = 0; i < count; i++)
222 if (glib_script_to_script[i] == script)
223 return (GUnicodeScript) i;
225 if (unlikely (script == HB_SCRIPT_INVALID))
226 return G_UNICODE_SCRIPT_INVALID_CODE;
228 return G_UNICODE_SCRIPT_UNKNOWN;
233 static hb_unicode_combining_class_t
234 hb_glib_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED,
235 hb_codepoint_t unicode,
236 void *user_data HB_UNUSED)
239 return (hb_unicode_combining_class_t) g_unichar_combining_class (unicode);
242 static hb_unicode_general_category_t
243 hb_glib_unicode_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED,
244 hb_codepoint_t unicode,
245 void *user_data HB_UNUSED)
248 /* hb_unicode_general_category_t and GUnicodeType are identical */
249 return (hb_unicode_general_category_t) g_unichar_type (unicode);
252 static hb_codepoint_t
253 hb_glib_unicode_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED,
254 hb_codepoint_t unicode,
255 void *user_data HB_UNUSED)
257 g_unichar_get_mirror_char (unicode, &unicode);
262 hb_glib_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
263 hb_codepoint_t unicode,
264 void *user_data HB_UNUSED)
266 return hb_glib_script_to_script (g_unichar_get_script (unicode));
270 hb_glib_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
274 void *user_data HB_UNUSED)
276 #if GLIB_CHECK_VERSION(2,29,12)
277 return g_unichar_compose (a, b, ab);
280 /* We don't ifdef-out the fallback code such that compiler always
281 * sees it and makes sure it's compilable. */
288 len = g_unichar_to_utf8 (a, utf8);
289 len += g_unichar_to_utf8 (b, utf8 + len);
290 normalized = g_utf8_normalize (utf8, len, G_NORMALIZE_NFC);
291 len = g_utf8_strlen (normalized, -1);
296 *ab = g_utf8_get_char (normalized);
307 hb_glib_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
311 void *user_data HB_UNUSED)
313 #if GLIB_CHECK_VERSION(2,29,12)
314 return g_unichar_decompose (ab, a, b);
317 /* We don't ifdef-out the fallback code such that compiler always
318 * sees it and makes sure it's compilable. */
325 len = g_unichar_to_utf8 (ab, utf8);
326 normalized = g_utf8_normalize (utf8, len, G_NORMALIZE_NFD);
327 len = g_utf8_strlen (normalized, -1);
332 *a = g_utf8_get_char (normalized);
335 } else if (len == 2) {
336 *a = g_utf8_get_char (normalized);
337 *b = g_utf8_get_char (g_utf8_next_char (normalized));
338 /* Here's the ugly part: if ab decomposes to a single character and
339 * that character decomposes again, we have to detect that and undo
340 * the second part :-(. */
341 gchar *recomposed = g_utf8_normalize (normalized, -1, G_NORMALIZE_NFC);
342 hb_codepoint_t c = g_utf8_get_char (recomposed);
343 if (c != ab && c != *a) {
350 /* If decomposed to more than two characters, take the last one,
351 * and recompose the rest to get the first component. */
352 gchar *end = g_utf8_offset_to_pointer (normalized, len - 1);
354 *b = g_utf8_get_char (end);
355 recomposed = g_utf8_normalize (normalized, end - normalized, G_NORMALIZE_NFC);
356 /* We expect that recomposed has exactly one character now. */
357 *a = g_utf8_get_char (recomposed);
368 static void free_static_glib_funcs ();
371 static struct hb_glib_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_glib_unicode_funcs_lazy_loader_t>
373 static hb_unicode_funcs_t *create ()
375 hb_unicode_funcs_t *funcs = hb_unicode_funcs_create (nullptr);
377 hb_unicode_funcs_set_combining_class_func (funcs, hb_glib_unicode_combining_class, nullptr, nullptr);
378 hb_unicode_funcs_set_general_category_func (funcs, hb_glib_unicode_general_category, nullptr, nullptr);
379 hb_unicode_funcs_set_mirroring_func (funcs, hb_glib_unicode_mirroring, nullptr, nullptr);
380 hb_unicode_funcs_set_script_func (funcs, hb_glib_unicode_script, nullptr, nullptr);
381 hb_unicode_funcs_set_compose_func (funcs, hb_glib_unicode_compose, nullptr, nullptr);
382 hb_unicode_funcs_set_decompose_func (funcs, hb_glib_unicode_decompose, nullptr, nullptr);
384 hb_unicode_funcs_make_immutable (funcs);
387 atexit (free_static_glib_funcs);
396 void free_static_glib_funcs ()
398 static_glib_funcs.free_instance ();
403 * hb_glib_get_unicode_funcs:
405 * Fetches a Unicode-functions structure that is populated
406 * with the appropriate GLib function for each method.
408 * Return value: (transfer none): a pointer to the #hb_unicode_funcs_t Unicode-functions structure
413 hb_glib_get_unicode_funcs ()
415 return static_glib_funcs.get_unconst ();
420 #if GLIB_CHECK_VERSION(2,31,10)
423 _hb_g_bytes_unref (void *data)
425 g_bytes_unref ((GBytes *) data);
429 * hb_glib_blob_create:
430 * @gbytes: the GBytes structure to work upon
432 * Creates an #hb_blob_t blob from the specified
433 * GBytes data structure.
435 * Return value: (transfer full): the new #hb_blob_t blob object
440 hb_glib_blob_create (GBytes *gbytes)
443 gconstpointer data = g_bytes_get_data (gbytes, &size);
444 return hb_blob_create ((const char *) data,
446 HB_MEMORY_MODE_READONLY,
447 g_bytes_ref (gbytes),