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
33 #include "hb-machinery.hh"
39 * @short_description: GLib integration
42 * Functions for using HarfBuzz with the GLib library to provide Unicode data.
46 #if !GLIB_CHECK_VERSION(2,29,14)
47 static const hb_script_t
48 glib_script_to_script[] =
90 HB_SCRIPT_CANADIAN_SYLLABICS,
97 /* Unicode-4.0 additions */
107 /* Unicode-4.1 additions */
108 HB_SCRIPT_NEW_TAI_LUE,
110 HB_SCRIPT_GLAGOLITIC,
112 HB_SCRIPT_SYLOTI_NAGRI,
113 HB_SCRIPT_OLD_PERSIAN,
114 HB_SCRIPT_KHAROSHTHI,
116 /* Unicode-5.0 additions */
120 HB_SCRIPT_PHOENICIAN,
124 /* Unicode-5.1 additions */
129 HB_SCRIPT_SAURASHTRA,
137 /* Unicode-5.2 additions */
140 HB_SCRIPT_EGYPTIAN_HIEROGLYPHS,
141 HB_SCRIPT_IMPERIAL_ARAMAIC,
142 HB_SCRIPT_INSCRIPTIONAL_PAHLAVI,
143 HB_SCRIPT_INSCRIPTIONAL_PARTHIAN,
148 HB_SCRIPT_MEETEI_MAYEK,
149 HB_SCRIPT_OLD_SOUTH_ARABIAN,
150 HB_SCRIPT_OLD_TURKIC,
154 /* Unicode-6.0 additions */
159 /* Unicode-6.1 additions */
161 HB_SCRIPT_MEROITIC_CURSIVE,
162 HB_SCRIPT_MEROITIC_HIEROGLYPHS,
165 HB_SCRIPT_SORA_SOMPENG,
171 hb_glib_script_to_script (GUnicodeScript script)
173 #if GLIB_CHECK_VERSION(2,29,14)
174 return (hb_script_t) g_unicode_script_to_iso15924 (script);
176 if (likely ((unsigned int) script < ARRAY_LENGTH (glib_script_to_script)))
177 return glib_script_to_script[script];
179 if (unlikely (script == G_UNICODE_SCRIPT_INVALID_CODE))
180 return HB_SCRIPT_INVALID;
182 return HB_SCRIPT_UNKNOWN;
187 hb_glib_script_from_script (hb_script_t script)
189 #if GLIB_CHECK_VERSION(2,29,14)
190 return g_unicode_script_from_iso15924 (script);
192 unsigned int count = ARRAY_LENGTH (glib_script_to_script);
193 for (unsigned int i = 0; i < count; i++)
194 if (glib_script_to_script[i] == script)
195 return (GUnicodeScript) i;
197 if (unlikely (script == HB_SCRIPT_INVALID))
198 return G_UNICODE_SCRIPT_INVALID_CODE;
200 return G_UNICODE_SCRIPT_UNKNOWN;
205 static hb_unicode_combining_class_t
206 hb_glib_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED,
207 hb_codepoint_t unicode,
208 void *user_data HB_UNUSED)
211 return (hb_unicode_combining_class_t) g_unichar_combining_class (unicode);
214 static hb_unicode_general_category_t
215 hb_glib_unicode_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED,
216 hb_codepoint_t unicode,
217 void *user_data HB_UNUSED)
220 /* hb_unicode_general_category_t and GUnicodeType are identical */
221 return (hb_unicode_general_category_t) g_unichar_type (unicode);
224 static hb_codepoint_t
225 hb_glib_unicode_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED,
226 hb_codepoint_t unicode,
227 void *user_data HB_UNUSED)
229 g_unichar_get_mirror_char (unicode, &unicode);
234 hb_glib_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
235 hb_codepoint_t unicode,
236 void *user_data HB_UNUSED)
238 return hb_glib_script_to_script (g_unichar_get_script (unicode));
242 hb_glib_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
246 void *user_data HB_UNUSED)
248 #if GLIB_CHECK_VERSION(2,29,12)
249 return g_unichar_compose (a, b, ab);
252 /* We don't ifdef-out the fallback code such that compiler always
253 * sees it and makes sure it's compilable. */
260 len = g_unichar_to_utf8 (a, utf8);
261 len += g_unichar_to_utf8 (b, utf8 + len);
262 normalized = g_utf8_normalize (utf8, len, G_NORMALIZE_NFC);
263 len = g_utf8_strlen (normalized, -1);
268 *ab = g_utf8_get_char (normalized);
279 hb_glib_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
283 void *user_data HB_UNUSED)
285 #if GLIB_CHECK_VERSION(2,29,12)
286 return g_unichar_decompose (ab, a, b);
289 /* We don't ifdef-out the fallback code such that compiler always
290 * sees it and makes sure it's compilable. */
297 len = g_unichar_to_utf8 (ab, utf8);
298 normalized = g_utf8_normalize (utf8, len, G_NORMALIZE_NFD);
299 len = g_utf8_strlen (normalized, -1);
304 *a = g_utf8_get_char (normalized);
307 } else if (len == 2) {
308 *a = g_utf8_get_char (normalized);
309 *b = g_utf8_get_char (g_utf8_next_char (normalized));
310 /* Here's the ugly part: if ab decomposes to a single character and
311 * that character decomposes again, we have to detect that and undo
312 * the second part :-(. */
313 gchar *recomposed = g_utf8_normalize (normalized, -1, G_NORMALIZE_NFC);
314 hb_codepoint_t c = g_utf8_get_char (recomposed);
315 if (c != ab && c != *a) {
322 /* If decomposed to more than two characters, take the last one,
323 * and recompose the rest to get the first component. */
324 gchar *end = g_utf8_offset_to_pointer (normalized, len - 1);
326 *b = g_utf8_get_char (end);
327 recomposed = g_utf8_normalize (normalized, end - normalized, G_NORMALIZE_NFC);
328 /* We expect that recomposed has exactly one character now. */
329 *a = g_utf8_get_char (recomposed);
340 static void free_static_glib_funcs ();
343 static struct hb_glib_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_glib_unicode_funcs_lazy_loader_t>
345 static hb_unicode_funcs_t *create ()
347 hb_unicode_funcs_t *funcs = hb_unicode_funcs_create (nullptr);
349 hb_unicode_funcs_set_combining_class_func (funcs, hb_glib_unicode_combining_class, nullptr, nullptr);
350 hb_unicode_funcs_set_general_category_func (funcs, hb_glib_unicode_general_category, nullptr, nullptr);
351 hb_unicode_funcs_set_mirroring_func (funcs, hb_glib_unicode_mirroring, nullptr, nullptr);
352 hb_unicode_funcs_set_script_func (funcs, hb_glib_unicode_script, nullptr, nullptr);
353 hb_unicode_funcs_set_compose_func (funcs, hb_glib_unicode_compose, nullptr, nullptr);
354 hb_unicode_funcs_set_decompose_func (funcs, hb_glib_unicode_decompose, nullptr, nullptr);
356 hb_unicode_funcs_make_immutable (funcs);
359 atexit (free_static_glib_funcs);
368 void free_static_glib_funcs ()
370 static_glib_funcs.free_instance ();
375 hb_glib_get_unicode_funcs ()
377 return static_glib_funcs.get_unconst ();
382 #if GLIB_CHECK_VERSION(2,31,10)
385 _hb_g_bytes_unref (void *data)
387 g_bytes_unref ((GBytes *) data);
391 * hb_glib_blob_create:
396 hb_glib_blob_create (GBytes *gbytes)
399 gconstpointer data = g_bytes_get_data (gbytes, &size);
400 return hb_blob_create ((const char *) data,
402 HB_MEMORY_MODE_READONLY,
403 g_bytes_ref (gbytes),