Imported Upstream version 8.2.2
[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 /**
53  * hb_glib_script_to_script:
54  * @script: The GUnicodeScript identifier to query
55  *
56  * Fetches the #hb_script_t script that corresponds to the
57  * specified GUnicodeScript identifier.
58  *
59  * Return value: the #hb_script_t script found
60  *
61  * Since: 0.9.38
62  **/
63 hb_script_t
64 hb_glib_script_to_script (GUnicodeScript script)
65 {
66   return (hb_script_t) g_unicode_script_to_iso15924 (script);
67 }
68
69 /**
70  * hb_glib_script_from_script:
71  * @script: The #hb_script_t to query
72  *
73  * Fetches the GUnicodeScript identifier that corresponds to the
74  * specified #hb_script_t script.
75  *
76  * Return value: the GUnicodeScript identifier found
77  *
78  * Since: 0.9.38
79  **/
80 GUnicodeScript
81 hb_glib_script_from_script (hb_script_t script)
82 {
83   return g_unicode_script_from_iso15924 (script);
84 }
85
86
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)
91
92 {
93   return (hb_unicode_combining_class_t) g_unichar_combining_class (unicode);
94 }
95
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)
100
101 {
102   /* hb_unicode_general_category_t and GUnicodeType are identical */
103   return (hb_unicode_general_category_t) g_unichar_type (unicode);
104 }
105
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)
110 {
111   g_unichar_get_mirror_char (unicode, &unicode);
112   return unicode;
113 }
114
115 static hb_script_t
116 hb_glib_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
117                         hb_codepoint_t      unicode,
118                         void               *user_data HB_UNUSED)
119 {
120   return hb_glib_script_to_script (g_unichar_get_script (unicode));
121 }
122
123 static hb_bool_t
124 hb_glib_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
125                          hb_codepoint_t      a,
126                          hb_codepoint_t      b,
127                          hb_codepoint_t     *ab,
128                          void               *user_data HB_UNUSED)
129 {
130 #if GLIB_CHECK_VERSION(2,29,12)
131   return g_unichar_compose (a, b, ab);
132 #else
133   return false;
134 #endif
135 }
136
137 static hb_bool_t
138 hb_glib_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
139                            hb_codepoint_t      ab,
140                            hb_codepoint_t     *a,
141                            hb_codepoint_t     *b,
142                            void               *user_data HB_UNUSED)
143 {
144 #if GLIB_CHECK_VERSION(2,29,12)
145   return g_unichar_decompose (ab, a, b);
146 #else
147   return false;
148 #endif
149 }
150
151
152 static inline void free_static_glib_funcs ();
153
154 static struct hb_glib_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_glib_unicode_funcs_lazy_loader_t>
155 {
156   static hb_unicode_funcs_t *create ()
157   {
158     hb_unicode_funcs_t *funcs = hb_unicode_funcs_create (nullptr);
159
160     hb_unicode_funcs_set_combining_class_func (funcs, hb_glib_unicode_combining_class, nullptr, nullptr);
161     hb_unicode_funcs_set_general_category_func (funcs, hb_glib_unicode_general_category, nullptr, nullptr);
162     hb_unicode_funcs_set_mirroring_func (funcs, hb_glib_unicode_mirroring, nullptr, nullptr);
163     hb_unicode_funcs_set_script_func (funcs, hb_glib_unicode_script, nullptr, nullptr);
164     hb_unicode_funcs_set_compose_func (funcs, hb_glib_unicode_compose, nullptr, nullptr);
165     hb_unicode_funcs_set_decompose_func (funcs, hb_glib_unicode_decompose, nullptr, nullptr);
166
167     hb_unicode_funcs_make_immutable (funcs);
168
169     hb_atexit (free_static_glib_funcs);
170
171     return funcs;
172   }
173 } static_glib_funcs;
174
175 static inline
176 void free_static_glib_funcs ()
177 {
178   static_glib_funcs.free_instance ();
179 }
180
181 /**
182  * hb_glib_get_unicode_funcs:
183  *
184  * Fetches a Unicode-functions structure that is populated
185  * with the appropriate GLib function for each method.
186  *
187  * Return value: (transfer none): a pointer to the #hb_unicode_funcs_t Unicode-functions structure
188  *
189  * Since: 0.9.38
190  **/
191 hb_unicode_funcs_t *
192 hb_glib_get_unicode_funcs ()
193 {
194   return static_glib_funcs.get_unconst ();
195 }
196
197
198
199 #if GLIB_CHECK_VERSION(2,31,10)
200
201 static void
202 _hb_g_bytes_unref (void *data)
203 {
204   g_bytes_unref ((GBytes *) data);
205 }
206
207 /**
208  * hb_glib_blob_create:
209  * @gbytes: the GBytes structure to work upon
210  *
211  * Creates an #hb_blob_t blob from the specified
212  * GBytes data structure.
213  *
214  * Return value: (transfer full): the new #hb_blob_t blob object
215  *
216  * Since: 0.9.38
217  **/
218 hb_blob_t *
219 hb_glib_blob_create (GBytes *gbytes)
220 {
221   gsize size = 0;
222   gconstpointer data = g_bytes_get_data (gbytes, &size);
223   return hb_blob_create ((const char *) data,
224                          size,
225                          HB_MEMORY_MODE_READONLY,
226                          g_bytes_ref (gbytes),
227                          _hb_g_bytes_unref);
228 }
229 #endif
230
231
232 #endif