hb-glib.cc (6053B)
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 return g_unichar_compose (a, b, ab); 131 } 132 133 static hb_bool_t 134 hb_glib_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED, 135 hb_codepoint_t ab, 136 hb_codepoint_t *a, 137 hb_codepoint_t *b, 138 void *user_data HB_UNUSED) 139 { 140 return g_unichar_decompose (ab, a, b); 141 } 142 143 144 static inline void free_static_glib_funcs (); 145 146 static struct hb_glib_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_glib_unicode_funcs_lazy_loader_t> 147 { 148 static hb_unicode_funcs_t *create () 149 { 150 hb_unicode_funcs_t *funcs = hb_unicode_funcs_create (nullptr); 151 152 hb_unicode_funcs_set_combining_class_func (funcs, hb_glib_unicode_combining_class, nullptr, nullptr); 153 hb_unicode_funcs_set_general_category_func (funcs, hb_glib_unicode_general_category, nullptr, nullptr); 154 hb_unicode_funcs_set_mirroring_func (funcs, hb_glib_unicode_mirroring, nullptr, nullptr); 155 hb_unicode_funcs_set_script_func (funcs, hb_glib_unicode_script, nullptr, nullptr); 156 hb_unicode_funcs_set_compose_func (funcs, hb_glib_unicode_compose, nullptr, nullptr); 157 hb_unicode_funcs_set_decompose_func (funcs, hb_glib_unicode_decompose, nullptr, nullptr); 158 159 hb_unicode_funcs_make_immutable (funcs); 160 161 hb_atexit (free_static_glib_funcs); 162 163 return funcs; 164 } 165 } static_glib_funcs; 166 167 static inline 168 void free_static_glib_funcs () 169 { 170 static_glib_funcs.free_instance (); 171 } 172 173 /** 174 * hb_glib_get_unicode_funcs: 175 * 176 * Fetches a Unicode-functions structure that is populated 177 * with the appropriate GLib function for each method. 178 * 179 * Return value: (transfer none): a pointer to the #hb_unicode_funcs_t Unicode-functions structure 180 * 181 * Since: 0.9.38 182 **/ 183 hb_unicode_funcs_t * 184 hb_glib_get_unicode_funcs () 185 { 186 return static_glib_funcs.get_unconst (); 187 } 188 189 190 191 #if GLIB_CHECK_VERSION(2,31,10) 192 193 static void 194 _hb_g_bytes_unref (void *data) 195 { 196 g_bytes_unref ((GBytes *) data); 197 } 198 199 /** 200 * hb_glib_blob_create: 201 * @gbytes: the GBytes structure to work upon 202 * 203 * Creates an #hb_blob_t blob from the specified 204 * GBytes data structure. 205 * 206 * Return value: (transfer full): the new #hb_blob_t blob object 207 * 208 * Since: 0.9.38 209 **/ 210 hb_blob_t * 211 hb_glib_blob_create (GBytes *gbytes) 212 { 213 gsize size = 0; 214 gconstpointer data = g_bytes_get_data (gbytes, &size); 215 return hb_blob_create ((const char *) data, 216 size, 217 HB_MEMORY_MODE_READONLY, 218 g_bytes_ref (gbytes), 219 _hb_g_bytes_unref); 220 } 221 #endif 222 223 224 #endif