Imported Upstream version 2.6.7
[platform/upstream/harfbuzz.git] / src / hb-glib.cc
1 /*
2  * Copyright © 2009  Red Hat, Inc.
3  * Copyright © 2011  Google, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
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.
12  *
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
17  * DAMAGE.
18  *
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.
24  *
25  * Red Hat Author(s): Behdad Esfahbod
26  * Google Author(s): Behdad Esfahbod
27  */
28
29 #include "hb.hh"
30
31 #ifdef HAVE_GLIB
32
33 #include "hb-glib.h"
34
35 #include "hb-machinery.hh"
36
37
38 /**
39  * SECTION:hb-glib
40  * @title: hb-glib
41  * @short_description: GLib integration
42  * @include: hb-glib.h
43  *
44  * Functions for using HarfBuzz with the GLib library. 
45  *
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
48  * structure.
49  **/
50
51
52 #if !GLIB_CHECK_VERSION(2,29,14)
53 static const hb_script_t
54 glib_script_to_script[] =
55 {
56   HB_SCRIPT_COMMON,
57   HB_SCRIPT_INHERITED,
58   HB_SCRIPT_ARABIC,
59   HB_SCRIPT_ARMENIAN,
60   HB_SCRIPT_BENGALI,
61   HB_SCRIPT_BOPOMOFO,
62   HB_SCRIPT_CHEROKEE,
63   HB_SCRIPT_COPTIC,
64   HB_SCRIPT_CYRILLIC,
65   HB_SCRIPT_DESERET,
66   HB_SCRIPT_DEVANAGARI,
67   HB_SCRIPT_ETHIOPIC,
68   HB_SCRIPT_GEORGIAN,
69   HB_SCRIPT_GOTHIC,
70   HB_SCRIPT_GREEK,
71   HB_SCRIPT_GUJARATI,
72   HB_SCRIPT_GURMUKHI,
73   HB_SCRIPT_HAN,
74   HB_SCRIPT_HANGUL,
75   HB_SCRIPT_HEBREW,
76   HB_SCRIPT_HIRAGANA,
77   HB_SCRIPT_KANNADA,
78   HB_SCRIPT_KATAKANA,
79   HB_SCRIPT_KHMER,
80   HB_SCRIPT_LAO,
81   HB_SCRIPT_LATIN,
82   HB_SCRIPT_MALAYALAM,
83   HB_SCRIPT_MONGOLIAN,
84   HB_SCRIPT_MYANMAR,
85   HB_SCRIPT_OGHAM,
86   HB_SCRIPT_OLD_ITALIC,
87   HB_SCRIPT_ORIYA,
88   HB_SCRIPT_RUNIC,
89   HB_SCRIPT_SINHALA,
90   HB_SCRIPT_SYRIAC,
91   HB_SCRIPT_TAMIL,
92   HB_SCRIPT_TELUGU,
93   HB_SCRIPT_THAANA,
94   HB_SCRIPT_THAI,
95   HB_SCRIPT_TIBETAN,
96   HB_SCRIPT_CANADIAN_SYLLABICS,
97   HB_SCRIPT_YI,
98   HB_SCRIPT_TAGALOG,
99   HB_SCRIPT_HANUNOO,
100   HB_SCRIPT_BUHID,
101   HB_SCRIPT_TAGBANWA,
102
103   /* Unicode-4.0 additions */
104   HB_SCRIPT_BRAILLE,
105   HB_SCRIPT_CYPRIOT,
106   HB_SCRIPT_LIMBU,
107   HB_SCRIPT_OSMANYA,
108   HB_SCRIPT_SHAVIAN,
109   HB_SCRIPT_LINEAR_B,
110   HB_SCRIPT_TAI_LE,
111   HB_SCRIPT_UGARITIC,
112
113   /* Unicode-4.1 additions */
114   HB_SCRIPT_NEW_TAI_LUE,
115   HB_SCRIPT_BUGINESE,
116   HB_SCRIPT_GLAGOLITIC,
117   HB_SCRIPT_TIFINAGH,
118   HB_SCRIPT_SYLOTI_NAGRI,
119   HB_SCRIPT_OLD_PERSIAN,
120   HB_SCRIPT_KHAROSHTHI,
121
122   /* Unicode-5.0 additions */
123   HB_SCRIPT_UNKNOWN,
124   HB_SCRIPT_BALINESE,
125   HB_SCRIPT_CUNEIFORM,
126   HB_SCRIPT_PHOENICIAN,
127   HB_SCRIPT_PHAGS_PA,
128   HB_SCRIPT_NKO,
129
130   /* Unicode-5.1 additions */
131   HB_SCRIPT_KAYAH_LI,
132   HB_SCRIPT_LEPCHA,
133   HB_SCRIPT_REJANG,
134   HB_SCRIPT_SUNDANESE,
135   HB_SCRIPT_SAURASHTRA,
136   HB_SCRIPT_CHAM,
137   HB_SCRIPT_OL_CHIKI,
138   HB_SCRIPT_VAI,
139   HB_SCRIPT_CARIAN,
140   HB_SCRIPT_LYCIAN,
141   HB_SCRIPT_LYDIAN,
142
143   /* Unicode-5.2 additions */
144   HB_SCRIPT_AVESTAN,
145   HB_SCRIPT_BAMUM,
146   HB_SCRIPT_EGYPTIAN_HIEROGLYPHS,
147   HB_SCRIPT_IMPERIAL_ARAMAIC,
148   HB_SCRIPT_INSCRIPTIONAL_PAHLAVI,
149   HB_SCRIPT_INSCRIPTIONAL_PARTHIAN,
150   HB_SCRIPT_JAVANESE,
151   HB_SCRIPT_KAITHI,
152   HB_SCRIPT_TAI_THAM,
153   HB_SCRIPT_LISU,
154   HB_SCRIPT_MEETEI_MAYEK,
155   HB_SCRIPT_OLD_SOUTH_ARABIAN,
156   HB_SCRIPT_OLD_TURKIC,
157   HB_SCRIPT_SAMARITAN,
158   HB_SCRIPT_TAI_VIET,
159
160   /* Unicode-6.0 additions */
161   HB_SCRIPT_BATAK,
162   HB_SCRIPT_BRAHMI,
163   HB_SCRIPT_MANDAIC,
164
165   /* Unicode-6.1 additions */
166   HB_SCRIPT_CHAKMA,
167   HB_SCRIPT_MEROITIC_CURSIVE,
168   HB_SCRIPT_MEROITIC_HIEROGLYPHS,
169   HB_SCRIPT_MIAO,
170   HB_SCRIPT_SHARADA,
171   HB_SCRIPT_SORA_SOMPENG,
172   HB_SCRIPT_TAKRI
173 };
174 #endif
175
176 /**
177  * hb_glib_script_to_script:
178  * @script: The GUnicodeScript identifier to query
179  *
180  * Fetches the #hb_script_t script that corresponds to the
181  * specified GUnicodeScript identifier.
182  *
183  * Return value: the #hb_script_t script found
184  *
185  * Since: 0.9.38
186  **/
187 hb_script_t
188 hb_glib_script_to_script (GUnicodeScript script)
189 {
190 #if GLIB_CHECK_VERSION(2,29,14)
191   return (hb_script_t) g_unicode_script_to_iso15924 (script);
192 #else
193   if (likely ((unsigned int) script < ARRAY_LENGTH (glib_script_to_script)))
194     return glib_script_to_script[script];
195
196   if (unlikely (script == G_UNICODE_SCRIPT_INVALID_CODE))
197     return HB_SCRIPT_INVALID;
198
199   return HB_SCRIPT_UNKNOWN;
200 #endif
201 }
202
203 /**
204  * hb_glib_script_from_script:
205  * @script: The #hb_script_t to query
206  *
207  * Fetches the GUnicodeScript identifier that corresponds to the
208  * specified #hb_script_t script.
209  *
210  * Return value: the GUnicodeScript identifier found
211  *
212  * Since: 0.9.38
213  **/
214 GUnicodeScript
215 hb_glib_script_from_script (hb_script_t script)
216 {
217 #if GLIB_CHECK_VERSION(2,29,14)
218   return g_unicode_script_from_iso15924 (script);
219 #else
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;
224
225   if (unlikely (script == HB_SCRIPT_INVALID))
226     return G_UNICODE_SCRIPT_INVALID_CODE;
227
228   return G_UNICODE_SCRIPT_UNKNOWN;
229 #endif
230 }
231
232
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)
237
238 {
239   return (hb_unicode_combining_class_t) g_unichar_combining_class (unicode);
240 }
241
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)
246
247 {
248   /* hb_unicode_general_category_t and GUnicodeType are identical */
249   return (hb_unicode_general_category_t) g_unichar_type (unicode);
250 }
251
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)
256 {
257   g_unichar_get_mirror_char (unicode, &unicode);
258   return unicode;
259 }
260
261 static hb_script_t
262 hb_glib_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
263                         hb_codepoint_t      unicode,
264                         void               *user_data HB_UNUSED)
265 {
266   return hb_glib_script_to_script (g_unichar_get_script (unicode));
267 }
268
269 static hb_bool_t
270 hb_glib_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
271                          hb_codepoint_t      a,
272                          hb_codepoint_t      b,
273                          hb_codepoint_t     *ab,
274                          void               *user_data HB_UNUSED)
275 {
276 #if GLIB_CHECK_VERSION(2,29,12)
277   return g_unichar_compose (a, b, ab);
278 #endif
279
280   /* We don't ifdef-out the fallback code such that compiler always
281    * sees it and makes sure it's compilable. */
282
283   gchar utf8[12];
284   gchar *normalized;
285   int len;
286   hb_bool_t ret;
287
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);
292   if (unlikely (!len))
293     return false;
294
295   if (len == 1) {
296     *ab = g_utf8_get_char (normalized);
297     ret = true;
298   } else {
299     ret = false;
300   }
301
302   g_free (normalized);
303   return ret;
304 }
305
306 static hb_bool_t
307 hb_glib_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
308                            hb_codepoint_t      ab,
309                            hb_codepoint_t     *a,
310                            hb_codepoint_t     *b,
311                            void               *user_data HB_UNUSED)
312 {
313 #if GLIB_CHECK_VERSION(2,29,12)
314   return g_unichar_decompose (ab, a, b);
315 #endif
316
317   /* We don't ifdef-out the fallback code such that compiler always
318    * sees it and makes sure it's compilable. */
319
320   gchar utf8[6];
321   gchar *normalized;
322   int len;
323   hb_bool_t ret;
324
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);
328   if (unlikely (!len))
329     return false;
330
331   if (len == 1) {
332     *a = g_utf8_get_char (normalized);
333     *b = 0;
334     ret = *a != ab;
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) {
344       *a = c;
345       *b = 0;
346     }
347     g_free (recomposed);
348     ret = true;
349   } else {
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);
353     gchar *recomposed;
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);
358     g_free (recomposed);
359     ret = true;
360   }
361
362   g_free (normalized);
363   return ret;
364 }
365
366
367 #if HB_USE_ATEXIT
368 static void free_static_glib_funcs ();
369 #endif
370
371 static struct hb_glib_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_glib_unicode_funcs_lazy_loader_t>
372 {
373   static hb_unicode_funcs_t *create ()
374   {
375     hb_unicode_funcs_t *funcs = hb_unicode_funcs_create (nullptr);
376
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);
383
384     hb_unicode_funcs_make_immutable (funcs);
385
386 #if HB_USE_ATEXIT
387     atexit (free_static_glib_funcs);
388 #endif
389
390     return funcs;
391   }
392 } static_glib_funcs;
393
394 #if HB_USE_ATEXIT
395 static
396 void free_static_glib_funcs ()
397 {
398   static_glib_funcs.free_instance ();
399 }
400 #endif
401
402 /**
403  * hb_glib_get_unicode_funcs:
404  *
405  * Fetches a Unicode-functions structure that is populated
406  * with the appropriate GLib function for each method.
407  *
408  * Return value: (transfer none): a pointer to the #hb_unicode_funcs_t Unicode-functions structure
409  *
410  * Since: 0.9.38
411  **/
412 hb_unicode_funcs_t *
413 hb_glib_get_unicode_funcs ()
414 {
415   return static_glib_funcs.get_unconst ();
416 }
417
418
419
420 #if GLIB_CHECK_VERSION(2,31,10)
421
422 static void
423 _hb_g_bytes_unref (void *data)
424 {
425   g_bytes_unref ((GBytes *) data);
426 }
427
428 /**
429  * hb_glib_blob_create:
430  * @gbytes: the GBytes structure to work upon
431  *
432  * Creates an #hb_blob_t blob from the specified
433  * GBytes data structure.
434  *
435  * Return value: (transfer full): the new #hb_blob_t blob object
436  *
437  * Since: 0.9.38
438  **/
439 hb_blob_t *
440 hb_glib_blob_create (GBytes *gbytes)
441 {
442   gsize size = 0;
443   gconstpointer data = g_bytes_get_data (gbytes, &size);
444   return hb_blob_create ((const char *) data,
445                          size,
446                          HB_MEMORY_MODE_READONLY,
447                          g_bytes_ref (gbytes),
448                          _hb_g_bytes_unref);
449 }
450 #endif
451
452
453 #endif