| 1 | /* |
| 2 | * Copyright © 2011,2014 Google, Inc. |
| 3 | * |
| 4 | * This is part of HarfBuzz, a text shaping library. |
| 5 | * |
| 6 | * Permission is hereby granted, without written agreement and without |
| 7 | * license or royalty fees, to use, copy, modify, and distribute this |
| 8 | * software and its documentation for any purpose, provided that the |
| 9 | * above copyright notice and the following two paragraphs appear in |
| 10 | * all copies of this software. |
| 11 | * |
| 12 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 13 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 14 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 15 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 16 | * DAMAGE. |
| 17 | * |
| 18 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 19 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 21 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 22 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 23 | * |
| 24 | * Google Author(s): Behdad Esfahbod, Roozbeh Pournader |
| 25 | */ |
| 26 | |
| 27 | #include "hb-private.hh" |
| 28 | |
| 29 | #include "hb-ot.h" |
| 30 | |
| 31 | #include "hb-font-private.hh" |
| 32 | |
| 33 | #include "hb-ot-cmap-table.hh" |
| 34 | #include "hb-ot-cbdt-table.hh" |
| 35 | #include "hb-ot-glyf-table.hh" |
| 36 | #include "hb-ot-hmtx-table.hh" |
| 37 | #include "hb-ot-kern-table.hh" |
| 38 | #include "hb-ot-post-table.hh" |
| 39 | |
| 40 | |
| 41 | struct hb_ot_font_t |
| 42 | { |
| 43 | OT::cmap::accelerator_t cmap; |
| 44 | OT::hmtx::accelerator_t h_metrics; |
| 45 | OT::vmtx::accelerator_t v_metrics; |
| 46 | OT::hb_lazy_loader_t<OT::glyf::accelerator_t> glyf; |
| 47 | OT::hb_lazy_loader_t<OT::CBDT::accelerator_t> cbdt; |
| 48 | OT::hb_lazy_loader_t<OT::post::accelerator_t> post; |
| 49 | OT::hb_lazy_loader_t<OT::kern::accelerator_t> kern; |
| 50 | }; |
| 51 | |
| 52 | |
| 53 | static hb_ot_font_t * |
| 54 | _hb_ot_font_create (hb_face_t *face) |
| 55 | { |
| 56 | hb_ot_font_t *ot_font = (hb_ot_font_t *) calloc (nmemb: 1, size: sizeof (hb_ot_font_t)); |
| 57 | |
| 58 | if (unlikely (!ot_font)) |
| 59 | return nullptr; |
| 60 | |
| 61 | ot_font->cmap.init (face); |
| 62 | ot_font->h_metrics.init (face); |
| 63 | ot_font->v_metrics.init (face, default_advance_: ot_font->h_metrics.ascender - ot_font->h_metrics.descender); /* TODO Can we do this lazily? */ |
| 64 | ot_font->glyf.init (face_: face); |
| 65 | ot_font->cbdt.init (face_: face); |
| 66 | ot_font->post.init (face_: face); |
| 67 | ot_font->kern.init (face_: face); |
| 68 | |
| 69 | return ot_font; |
| 70 | } |
| 71 | |
| 72 | static void |
| 73 | _hb_ot_font_destroy (void *data) |
| 74 | { |
| 75 | hb_ot_font_t *ot_font = (hb_ot_font_t *) data; |
| 76 | |
| 77 | ot_font->cmap.fini (); |
| 78 | ot_font->h_metrics.fini (); |
| 79 | ot_font->v_metrics.fini (); |
| 80 | ot_font->glyf.fini (); |
| 81 | ot_font->cbdt.fini (); |
| 82 | ot_font->post.fini (); |
| 83 | ot_font->kern.fini (); |
| 84 | |
| 85 | free (ptr: ot_font); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | static hb_bool_t |
| 90 | hb_ot_get_nominal_glyph (hb_font_t *font HB_UNUSED, |
| 91 | void *font_data, |
| 92 | hb_codepoint_t unicode, |
| 93 | hb_codepoint_t *glyph, |
| 94 | void *user_data HB_UNUSED) |
| 95 | |
| 96 | { |
| 97 | const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data; |
| 98 | return ot_font->cmap.get_nominal_glyph (unicode, glyph); |
| 99 | } |
| 100 | |
| 101 | static hb_bool_t |
| 102 | hb_ot_get_variation_glyph (hb_font_t *font HB_UNUSED, |
| 103 | void *font_data, |
| 104 | hb_codepoint_t unicode, |
| 105 | hb_codepoint_t variation_selector, |
| 106 | hb_codepoint_t *glyph, |
| 107 | void *user_data HB_UNUSED) |
| 108 | { |
| 109 | const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data; |
| 110 | return ot_font->cmap.get_variation_glyph (unicode, variation_selector, glyph); |
| 111 | } |
| 112 | |
| 113 | static hb_position_t |
| 114 | hb_ot_get_glyph_h_advance (hb_font_t *font, |
| 115 | void *font_data, |
| 116 | hb_codepoint_t glyph, |
| 117 | void *user_data HB_UNUSED) |
| 118 | { |
| 119 | const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data; |
| 120 | return font->em_scale_x (v: ot_font->h_metrics.get_advance (glyph, font)); |
| 121 | } |
| 122 | |
| 123 | static hb_position_t |
| 124 | hb_ot_get_glyph_v_advance (hb_font_t *font, |
| 125 | void *font_data, |
| 126 | hb_codepoint_t glyph, |
| 127 | void *user_data HB_UNUSED) |
| 128 | { |
| 129 | const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data; |
| 130 | return font->em_scale_y (v: -(int) ot_font->v_metrics.get_advance (glyph, font)); |
| 131 | } |
| 132 | |
| 133 | static hb_position_t |
| 134 | hb_ot_get_glyph_h_kerning (hb_font_t *font, |
| 135 | void *font_data, |
| 136 | hb_codepoint_t left_glyph, |
| 137 | hb_codepoint_t right_glyph, |
| 138 | void *user_data HB_UNUSED) |
| 139 | { |
| 140 | const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data; |
| 141 | return font->em_scale_x (v: ot_font->kern->get_h_kerning (left: left_glyph, right: right_glyph)); |
| 142 | } |
| 143 | |
| 144 | static hb_bool_t |
| 145 | hb_ot_get_glyph_extents (hb_font_t *font HB_UNUSED, |
| 146 | void *font_data, |
| 147 | hb_codepoint_t glyph, |
| 148 | hb_glyph_extents_t *extents, |
| 149 | void *user_data HB_UNUSED) |
| 150 | { |
| 151 | const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data; |
| 152 | bool ret = ot_font->glyf->get_extents (glyph, extents); |
| 153 | if (!ret) |
| 154 | ret = ot_font->cbdt->get_extents (glyph, extents); |
| 155 | // TODO Hook up side-bearings variations. |
| 156 | extents->x_bearing = font->em_scale_x (v: extents->x_bearing); |
| 157 | extents->y_bearing = font->em_scale_y (v: extents->y_bearing); |
| 158 | extents->width = font->em_scale_x (v: extents->width); |
| 159 | extents->height = font->em_scale_y (v: extents->height); |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | static hb_bool_t |
| 164 | hb_ot_get_glyph_name (hb_font_t *font HB_UNUSED, |
| 165 | void *font_data, |
| 166 | hb_codepoint_t glyph, |
| 167 | char *name, unsigned int size, |
| 168 | void *user_data HB_UNUSED) |
| 169 | { |
| 170 | const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data; |
| 171 | return ot_font->post->get_glyph_name (glyph, buf: name, buf_len: size); |
| 172 | } |
| 173 | |
| 174 | static hb_bool_t |
| 175 | hb_ot_get_glyph_from_name (hb_font_t *font HB_UNUSED, |
| 176 | void *font_data, |
| 177 | const char *name, int len, |
| 178 | hb_codepoint_t *glyph, |
| 179 | void *user_data HB_UNUSED) |
| 180 | { |
| 181 | const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data; |
| 182 | return ot_font->post->get_glyph_from_name (name, len, glyph); |
| 183 | } |
| 184 | |
| 185 | static hb_bool_t |
| 186 | hb_ot_get_font_h_extents (hb_font_t *font HB_UNUSED, |
| 187 | void *font_data, |
| 188 | hb_font_extents_t *metrics, |
| 189 | void *user_data HB_UNUSED) |
| 190 | { |
| 191 | const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data; |
| 192 | metrics->ascender = font->em_scale_y (v: ot_font->h_metrics.ascender); |
| 193 | metrics->descender = font->em_scale_y (v: ot_font->h_metrics.descender); |
| 194 | metrics->line_gap = font->em_scale_y (v: ot_font->h_metrics.line_gap); |
| 195 | // TODO Hook up variations. |
| 196 | return ot_font->h_metrics.has_font_extents; |
| 197 | } |
| 198 | |
| 199 | static hb_bool_t |
| 200 | hb_ot_get_font_v_extents (hb_font_t *font HB_UNUSED, |
| 201 | void *font_data, |
| 202 | hb_font_extents_t *metrics, |
| 203 | void *user_data HB_UNUSED) |
| 204 | { |
| 205 | const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data; |
| 206 | metrics->ascender = font->em_scale_x (v: ot_font->v_metrics.ascender); |
| 207 | metrics->descender = font->em_scale_x (v: ot_font->v_metrics.descender); |
| 208 | metrics->line_gap = font->em_scale_x (v: ot_font->v_metrics.line_gap); |
| 209 | // TODO Hook up variations. |
| 210 | return ot_font->v_metrics.has_font_extents; |
| 211 | } |
| 212 | |
| 213 | static hb_font_funcs_t *static_ot_funcs = nullptr; |
| 214 | |
| 215 | #ifdef HB_USE_ATEXIT |
| 216 | static |
| 217 | void free_static_ot_funcs (void) |
| 218 | { |
| 219 | hb_font_funcs_destroy (ffuncs: static_ot_funcs); |
| 220 | } |
| 221 | #endif |
| 222 | |
| 223 | static hb_font_funcs_t * |
| 224 | _hb_ot_get_font_funcs (void) |
| 225 | { |
| 226 | retry: |
| 227 | hb_font_funcs_t *funcs = (hb_font_funcs_t *) hb_atomic_ptr_get (&static_ot_funcs); |
| 228 | |
| 229 | if (unlikely (!funcs)) |
| 230 | { |
| 231 | funcs = hb_font_funcs_create (); |
| 232 | |
| 233 | hb_font_funcs_set_font_h_extents_func (ffuncs: funcs, func: hb_ot_get_font_h_extents, user_data: nullptr, destroy: nullptr); |
| 234 | hb_font_funcs_set_font_v_extents_func (ffuncs: funcs, func: hb_ot_get_font_v_extents, user_data: nullptr, destroy: nullptr); |
| 235 | hb_font_funcs_set_nominal_glyph_func (ffuncs: funcs, func: hb_ot_get_nominal_glyph, user_data: nullptr, destroy: nullptr); |
| 236 | hb_font_funcs_set_variation_glyph_func (ffuncs: funcs, func: hb_ot_get_variation_glyph, user_data: nullptr, destroy: nullptr); |
| 237 | hb_font_funcs_set_glyph_h_advance_func (ffuncs: funcs, func: hb_ot_get_glyph_h_advance, user_data: nullptr, destroy: nullptr); |
| 238 | hb_font_funcs_set_glyph_v_advance_func (ffuncs: funcs, func: hb_ot_get_glyph_v_advance, user_data: nullptr, destroy: nullptr); |
| 239 | //hb_font_funcs_set_glyph_h_origin_func (funcs, hb_ot_get_glyph_h_origin, nullptr, nullptr); |
| 240 | //hb_font_funcs_set_glyph_v_origin_func (funcs, hb_ot_get_glyph_v_origin, nullptr, nullptr); |
| 241 | hb_font_funcs_set_glyph_h_kerning_func (ffuncs: funcs, func: hb_ot_get_glyph_h_kerning, user_data: nullptr, destroy: nullptr); |
| 242 | //hb_font_funcs_set_glyph_v_kerning_func (funcs, hb_ot_get_glyph_v_kerning, nullptr, nullptr); |
| 243 | hb_font_funcs_set_glyph_extents_func (ffuncs: funcs, func: hb_ot_get_glyph_extents, user_data: nullptr, destroy: nullptr); |
| 244 | //hb_font_funcs_set_glyph_contour_point_func (funcs, hb_ot_get_glyph_contour_point, nullptr, nullptr); |
| 245 | hb_font_funcs_set_glyph_name_func (ffuncs: funcs, func: hb_ot_get_glyph_name, user_data: nullptr, destroy: nullptr); |
| 246 | hb_font_funcs_set_glyph_from_name_func (ffuncs: funcs, func: hb_ot_get_glyph_from_name, user_data: nullptr, destroy: nullptr); |
| 247 | |
| 248 | hb_font_funcs_make_immutable (ffuncs: funcs); |
| 249 | |
| 250 | if (!hb_atomic_ptr_cmpexch (&static_ot_funcs, nullptr, funcs)) { |
| 251 | hb_font_funcs_destroy (ffuncs: funcs); |
| 252 | goto retry; |
| 253 | } |
| 254 | |
| 255 | #ifdef HB_USE_ATEXIT |
| 256 | atexit (func: free_static_ot_funcs); /* First person registers atexit() callback. */ |
| 257 | #endif |
| 258 | }; |
| 259 | |
| 260 | return funcs; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | /** |
| 265 | * hb_ot_font_set_funcs: |
| 266 | * |
| 267 | * Since: 0.9.28 |
| 268 | **/ |
| 269 | void |
| 270 | hb_ot_font_set_funcs (hb_font_t *font) |
| 271 | { |
| 272 | hb_ot_font_t *ot_font = _hb_ot_font_create (face: font->face); |
| 273 | if (unlikely (!ot_font)) |
| 274 | return; |
| 275 | |
| 276 | hb_font_set_funcs (font, |
| 277 | klass: _hb_ot_get_font_funcs (), |
| 278 | font_data: ot_font, |
| 279 | destroy: _hb_ot_font_destroy); |
| 280 | } |
| 281 | |