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
29 #include "hb-private.hh"
33 #include "hb-unicode-private.hh"
36 #if !GLIB_CHECK_VERSION(2,29,14)
37 static const hb_script_t
38 glib_script_to_script
[] =
80 HB_SCRIPT_CANADIAN_SYLLABICS
,
87 /* Unicode-4.0 additions */
97 /* Unicode-4.1 additions */
98 HB_SCRIPT_NEW_TAI_LUE
,
100 HB_SCRIPT_GLAGOLITIC
,
102 HB_SCRIPT_SYLOTI_NAGRI
,
103 HB_SCRIPT_OLD_PERSIAN
,
104 HB_SCRIPT_KHAROSHTHI
,
106 /* Unicode-5.0 additions */
110 HB_SCRIPT_PHOENICIAN
,
114 /* Unicode-5.1 additions */
119 HB_SCRIPT_SAURASHTRA
,
127 /* Unicode-5.2 additions */
130 HB_SCRIPT_EGYPTIAN_HIEROGLYPHS
,
131 HB_SCRIPT_IMPERIAL_ARAMAIC
,
132 HB_SCRIPT_INSCRIPTIONAL_PAHLAVI
,
133 HB_SCRIPT_INSCRIPTIONAL_PARTHIAN
,
138 HB_SCRIPT_MEETEI_MAYEK
,
139 HB_SCRIPT_OLD_SOUTH_ARABIAN
,
140 HB_SCRIPT_OLD_TURKIC
,
144 /* Unicode-6.0 additions */
149 /* Unicode-6.1 additions */
151 HB_SCRIPT_MEROITIC_CURSIVE
,
152 HB_SCRIPT_MEROITIC_HIEROGLYPHS
,
155 HB_SCRIPT_SORA_SOMPENG
,
161 hb_glib_script_to_script (GUnicodeScript script
)
163 #if GLIB_CHECK_VERSION(2,29,14)
164 return (hb_script_t
) g_unicode_script_to_iso15924 (script
);
166 if (likely ((unsigned int) script
< ARRAY_LENGTH (glib_script_to_script
)))
167 return glib_script_to_script
[script
];
169 if (unlikely (script
== G_UNICODE_SCRIPT_INVALID_CODE
))
170 return HB_SCRIPT_INVALID
;
172 return HB_SCRIPT_UNKNOWN
;
177 hb_glib_script_from_script (hb_script_t script
)
179 #if GLIB_CHECK_VERSION(2,29,14)
180 return g_unicode_script_from_iso15924 (script
);
182 unsigned int count
= ARRAY_LENGTH (glib_script_to_script
);
183 for (unsigned int i
= 0; i
< count
; i
++)
184 if (glib_script_to_script
[i
] == script
)
185 return (GUnicodeScript
) i
;
187 if (unlikely (script
== HB_SCRIPT_INVALID
))
188 return G_UNICODE_SCRIPT_INVALID_CODE
;
190 return G_UNICODE_SCRIPT_UNKNOWN
;
195 static hb_unicode_combining_class_t
196 hb_glib_unicode_combining_class (hb_unicode_funcs_t
*ufuncs HB_UNUSED
,
197 hb_codepoint_t unicode
,
198 void *user_data HB_UNUSED
)
201 return (hb_unicode_combining_class_t
) g_unichar_combining_class (unicode
);
205 hb_glib_unicode_eastasian_width (hb_unicode_funcs_t
*ufuncs HB_UNUSED
,
206 hb_codepoint_t unicode
,
207 void *user_data HB_UNUSED
)
209 return g_unichar_iswide (unicode
) ? 2 : 1;
212 static hb_unicode_general_category_t
213 hb_glib_unicode_general_category (hb_unicode_funcs_t
*ufuncs HB_UNUSED
,
214 hb_codepoint_t unicode
,
215 void *user_data HB_UNUSED
)
218 /* hb_unicode_general_category_t and GUnicodeType are identical */
219 return (hb_unicode_general_category_t
) g_unichar_type (unicode
);
222 static hb_codepoint_t
223 hb_glib_unicode_mirroring (hb_unicode_funcs_t
*ufuncs HB_UNUSED
,
224 hb_codepoint_t unicode
,
225 void *user_data HB_UNUSED
)
227 g_unichar_get_mirror_char (unicode
, &unicode
);
232 hb_glib_unicode_script (hb_unicode_funcs_t
*ufuncs HB_UNUSED
,
233 hb_codepoint_t unicode
,
234 void *user_data HB_UNUSED
)
236 return hb_glib_script_to_script (g_unichar_get_script (unicode
));
240 hb_glib_unicode_compose (hb_unicode_funcs_t
*ufuncs HB_UNUSED
,
244 void *user_data HB_UNUSED
)
246 #if GLIB_CHECK_VERSION(2,29,12)
247 return g_unichar_compose (a
, b
, ab
);
250 /* We don't ifdef-out the fallback code such that compiler always
251 * sees it and makes sure it's compilable. */
258 len
= g_unichar_to_utf8 (a
, utf8
);
259 len
+= g_unichar_to_utf8 (b
, utf8
+ len
);
260 normalized
= g_utf8_normalize (utf8
, len
, G_NORMALIZE_NFC
);
261 len
= g_utf8_strlen (normalized
, -1);
266 *ab
= g_utf8_get_char (normalized
);
277 hb_glib_unicode_decompose (hb_unicode_funcs_t
*ufuncs HB_UNUSED
,
281 void *user_data HB_UNUSED
)
283 #if GLIB_CHECK_VERSION(2,29,12)
284 return g_unichar_decompose (ab
, a
, b
);
287 /* We don't ifdef-out the fallback code such that compiler always
288 * sees it and makes sure it's compilable. */
295 len
= g_unichar_to_utf8 (ab
, utf8
);
296 normalized
= g_utf8_normalize (utf8
, len
, G_NORMALIZE_NFD
);
297 len
= g_utf8_strlen (normalized
, -1);
302 *a
= g_utf8_get_char (normalized
);
305 } else if (len
== 2) {
306 *a
= g_utf8_get_char (normalized
);
307 *b
= g_utf8_get_char (g_utf8_next_char (normalized
));
308 /* Here's the ugly part: if ab decomposes to a single character and
309 * that character decomposes again, we have to detect that and undo
310 * the second part :-(. */
311 gchar
*recomposed
= g_utf8_normalize (normalized
, -1, G_NORMALIZE_NFC
);
312 hb_codepoint_t c
= g_utf8_get_char (recomposed
);
313 if (c
!= ab
&& c
!= *a
) {
320 /* If decomposed to more than two characters, take the last one,
321 * and recompose the rest to get the first component. */
322 gchar
*end
= g_utf8_offset_to_pointer (normalized
, len
- 1);
324 *b
= g_utf8_get_char (end
);
325 recomposed
= g_utf8_normalize (normalized
, end
- normalized
, G_NORMALIZE_NFC
);
326 /* We expect that recomposed has exactly one character now. */
327 *a
= g_utf8_get_char (recomposed
);
337 hb_glib_unicode_decompose_compatibility (hb_unicode_funcs_t
*ufuncs HB_UNUSED
,
339 hb_codepoint_t
*decomposed
,
340 void *user_data HB_UNUSED
)
342 #if GLIB_CHECK_VERSION(2,29,12)
343 return g_unichar_fully_decompose (u
, true, decomposed
, HB_UNICODE_MAX_DECOMPOSITION_LEN
);
346 /* If the user doesn't have GLib >= 2.29.12 we have to perform
347 * a round trip to UTF-8 and the associated memory management dance. */
349 gchar
*utf8_decomposed
, *c
;
350 gsize utf8_len
, utf8_decomposed_len
, i
;
352 /* Convert @u to UTF-8 and normalise it in NFKD mode. This performs the compatibility decomposition. */
353 utf8_len
= g_unichar_to_utf8 (u
, utf8
);
354 utf8_decomposed
= g_utf8_normalize (utf8
, utf8_len
, G_NORMALIZE_NFKD
);
355 utf8_decomposed_len
= g_utf8_strlen (utf8_decomposed
, -1);
357 assert (utf8_decomposed_len
<= HB_UNICODE_MAX_DECOMPOSITION_LEN
);
359 for (i
= 0, c
= utf8_decomposed
; i
< utf8_decomposed_len
; i
++, c
= g_utf8_next_char (c
))
360 *decomposed
++ = g_utf8_get_char (c
);
362 g_free (utf8_decomposed
);
364 return utf8_decomposed_len
;
368 hb_glib_get_unicode_funcs (void)
370 static const hb_unicode_funcs_t _hb_glib_unicode_funcs
= {
371 HB_OBJECT_HEADER_STATIC
,
374 true, /* immutable */
376 #define HB_UNICODE_FUNC_IMPLEMENT(name) hb_glib_unicode_##name,
377 HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
378 #undef HB_UNICODE_FUNC_IMPLEMENT
382 return const_cast<hb_unicode_funcs_t
*> (&_hb_glib_unicode_funcs
);
386 hb_glib_blob_create (GBytes
*gbytes
)
389 gconstpointer data
= g_bytes_get_data (gbytes
, &size
);
390 return hb_blob_create ((const char *) data
,
392 HB_MEMORY_MODE_READONLY
,
393 g_bytes_ref (gbytes
),
394 (hb_destroy_func_t
) g_bytes_unref
);