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
53 * hb_glib_script_to_script:
54 * @script: The GUnicodeScript identifier to query
56 * Fetches the #hb_script_t script that corresponds to the
57 * specified GUnicodeScript identifier.
59 * Return value: the #hb_script_t script found
64 hb_glib_script_to_script (GUnicodeScript script)
66 return (hb_script_t) g_unicode_script_to_iso15924 (script);
70 * hb_glib_script_from_script:
71 * @script: The #hb_script_t to query
73 * Fetches the GUnicodeScript identifier that corresponds to the
74 * specified #hb_script_t script.
76 * Return value: the GUnicodeScript identifier found
81 hb_glib_script_from_script (hb_script_t script)
83 return g_unicode_script_from_iso15924 (script);
87 static hb_unicode_combining_class_t
88 hb_glib_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED,
89 hb_codepoint_t unicode,
90 void *user_data HB_UNUSED)
93 return (hb_unicode_combining_class_t) g_unichar_combining_class (unicode);
96 static hb_unicode_general_category_t
97 hb_glib_unicode_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED,
98 hb_codepoint_t unicode,
99 void *user_data HB_UNUSED)
102 /* hb_unicode_general_category_t and GUnicodeType are identical */
103 return (hb_unicode_general_category_t) g_unichar_type (unicode);
106 static hb_codepoint_t
107 hb_glib_unicode_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED,
108 hb_codepoint_t unicode,
109 void *user_data HB_UNUSED)
111 g_unichar_get_mirror_char (unicode, &unicode);
116 hb_glib_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
117 hb_codepoint_t unicode,
118 void *user_data HB_UNUSED)
120 return hb_glib_script_to_script (g_unichar_get_script (unicode));
124 hb_glib_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
128 void *user_data HB_UNUSED)
130 #if GLIB_CHECK_VERSION(2,29,12)
131 return g_unichar_compose (a, b, ab);
134 /* We don't ifdef-out the fallback code such that compiler always
135 * sees it and makes sure it's compilable. */
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);
150 *ab = g_utf8_get_char (normalized);
161 hb_glib_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
165 void *user_data HB_UNUSED)
167 #if GLIB_CHECK_VERSION(2,29,12)
168 return g_unichar_decompose (ab, a, b);
171 /* We don't ifdef-out the fallback code such that compiler always
172 * sees it and makes sure it's compilable. */
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);
186 *a = g_utf8_get_char (normalized);
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) {
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);
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);
221 static inline void free_static_glib_funcs ();
223 static struct hb_glib_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_glib_unicode_funcs_lazy_loader_t>
225 static hb_unicode_funcs_t *create ()
227 hb_unicode_funcs_t *funcs = hb_unicode_funcs_create (nullptr);
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);
236 hb_unicode_funcs_make_immutable (funcs);
238 hb_atexit (free_static_glib_funcs);
245 void free_static_glib_funcs ()
247 static_glib_funcs.free_instance ();
251 * hb_glib_get_unicode_funcs:
253 * Fetches a Unicode-functions structure that is populated
254 * with the appropriate GLib function for each method.
256 * Return value: (transfer none): a pointer to the #hb_unicode_funcs_t Unicode-functions structure
261 hb_glib_get_unicode_funcs ()
263 return static_glib_funcs.get_unconst ();
268 #if GLIB_CHECK_VERSION(2,31,10)
271 _hb_g_bytes_unref (void *data)
273 g_bytes_unref ((GBytes *) data);
277 * hb_glib_blob_create:
278 * @gbytes: the GBytes structure to work upon
280 * Creates an #hb_blob_t blob from the specified
281 * GBytes data structure.
283 * Return value: (transfer full): the new #hb_blob_t blob object
288 hb_glib_blob_create (GBytes *gbytes)
291 gconstpointer data = g_bytes_get_data (gbytes, &size);
292 return hb_blob_create ((const char *) data,
294 HB_MEMORY_MODE_READONLY,
295 g_bytes_ref (gbytes),