| 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.hh" |
| 28 | |
| 29 | #include "hb-ot.h" |
| 30 | |
| 31 | #include "hb-font.hh" |
| 32 | #include "hb-machinery.hh" |
| 33 | #include "hb-ot-face.hh" |
| 34 | |
| 35 | #include "hb-ot-cmap-table.hh" |
| 36 | #include "hb-ot-hmtx-table.hh" |
| 37 | #include "hb-ot-kern-table.hh" |
| 38 | #include "hb-ot-post-table.hh" |
| 39 | #include "hb-ot-glyf-table.hh" |
| 40 | #include "hb-ot-color-cbdt-table.hh" |
| 41 | |
| 42 | |
| 43 | static hb_bool_t |
| 44 | hb_ot_get_nominal_glyph (hb_font_t *font HB_UNUSED, |
| 45 | void *font_data, |
| 46 | hb_codepoint_t unicode, |
| 47 | hb_codepoint_t *glyph, |
| 48 | void *user_data HB_UNUSED) |
| 49 | |
| 50 | { |
| 51 | const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data; |
| 52 | return ot_font->cmap.get_relaxed()->get_nominal_glyph (unicode, glyph); |
| 53 | } |
| 54 | |
| 55 | static hb_bool_t |
| 56 | hb_ot_get_variation_glyph (hb_font_t *font HB_UNUSED, |
| 57 | void *font_data, |
| 58 | hb_codepoint_t unicode, |
| 59 | hb_codepoint_t variation_selector, |
| 60 | hb_codepoint_t *glyph, |
| 61 | void *user_data HB_UNUSED) |
| 62 | { |
| 63 | const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data; |
| 64 | return ot_font->cmap.get_relaxed ()->get_variation_glyph (unicode, variation_selector, glyph); |
| 65 | } |
| 66 | |
| 67 | static hb_position_t |
| 68 | hb_ot_get_glyph_h_advance (hb_font_t *font, |
| 69 | void *font_data, |
| 70 | hb_codepoint_t glyph, |
| 71 | void *user_data HB_UNUSED) |
| 72 | { |
| 73 | const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data; |
| 74 | return font->em_scale_x (ot_font->hmtx.get_relaxed ()->get_advance (glyph, font)); |
| 75 | } |
| 76 | |
| 77 | static hb_position_t |
| 78 | hb_ot_get_glyph_v_advance (hb_font_t *font, |
| 79 | void *font_data, |
| 80 | hb_codepoint_t glyph, |
| 81 | void *user_data HB_UNUSED) |
| 82 | { |
| 83 | const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data; |
| 84 | return font->em_scale_y (-(int) ot_font->vmtx.get_relaxed ()->get_advance (glyph, font)); |
| 85 | } |
| 86 | |
| 87 | static hb_position_t |
| 88 | hb_ot_get_glyph_h_kerning (hb_font_t *font, |
| 89 | void *font_data, |
| 90 | hb_codepoint_t left_glyph, |
| 91 | hb_codepoint_t right_glyph, |
| 92 | void *user_data HB_UNUSED) |
| 93 | { |
| 94 | const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data; |
| 95 | return font->em_scale_x (ot_font->kern->get_h_kerning (left_glyph, right_glyph)); |
| 96 | } |
| 97 | |
| 98 | static hb_bool_t |
| 99 | hb_ot_get_glyph_extents (hb_font_t *font, |
| 100 | void *font_data, |
| 101 | hb_codepoint_t glyph, |
| 102 | hb_glyph_extents_t *extents, |
| 103 | void *user_data HB_UNUSED) |
| 104 | { |
| 105 | const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data; |
| 106 | bool ret = ot_font->glyf->get_extents (glyph, extents); |
| 107 | if (!ret) |
| 108 | ret = ot_font->CBDT->get_extents (glyph, extents); |
| 109 | // TODO Hook up side-bearings variations. |
| 110 | extents->x_bearing = font->em_scale_x (extents->x_bearing); |
| 111 | extents->y_bearing = font->em_scale_y (extents->y_bearing); |
| 112 | extents->width = font->em_scale_x (extents->width); |
| 113 | extents->height = font->em_scale_y (extents->height); |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | static hb_bool_t |
| 118 | hb_ot_get_glyph_name (hb_font_t *font HB_UNUSED, |
| 119 | void *font_data, |
| 120 | hb_codepoint_t glyph, |
| 121 | char *name, unsigned int size, |
| 122 | void *user_data HB_UNUSED) |
| 123 | { |
| 124 | const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data; |
| 125 | return ot_font->post->get_glyph_name (glyph, name, size); |
| 126 | } |
| 127 | |
| 128 | static hb_bool_t |
| 129 | hb_ot_get_glyph_from_name (hb_font_t *font HB_UNUSED, |
| 130 | void *font_data, |
| 131 | const char *name, int len, |
| 132 | hb_codepoint_t *glyph, |
| 133 | void *user_data HB_UNUSED) |
| 134 | { |
| 135 | const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data; |
| 136 | return ot_font->post->get_glyph_from_name (name, len, glyph); |
| 137 | } |
| 138 | |
| 139 | static hb_bool_t |
| 140 | hb_ot_get_font_h_extents (hb_font_t *font, |
| 141 | void *font_data, |
| 142 | hb_font_extents_t *metrics, |
| 143 | void *user_data HB_UNUSED) |
| 144 | { |
| 145 | const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data; |
| 146 | metrics->ascender = font->em_scale_y (ot_font->hmtx.get_relaxed ()->ascender); |
| 147 | metrics->descender = font->em_scale_y (ot_font->hmtx.get_relaxed ()->descender); |
| 148 | metrics->line_gap = font->em_scale_y (ot_font->hmtx.get_relaxed ()->line_gap); |
| 149 | // TODO Hook up variations. |
| 150 | return ot_font->hmtx.get_relaxed ()->has_font_extents; |
| 151 | } |
| 152 | |
| 153 | static hb_bool_t |
| 154 | hb_ot_get_font_v_extents (hb_font_t *font, |
| 155 | void *font_data, |
| 156 | hb_font_extents_t *metrics, |
| 157 | void *user_data HB_UNUSED) |
| 158 | { |
| 159 | const hb_ot_face_data_t *ot_font = (const hb_ot_face_data_t *) font_data; |
| 160 | metrics->ascender = font->em_scale_x (ot_font->vmtx.get_relaxed ()->ascender); |
| 161 | metrics->descender = font->em_scale_x (ot_font->vmtx.get_relaxed ()->descender); |
| 162 | metrics->line_gap = font->em_scale_x (ot_font->vmtx.get_relaxed ()->line_gap); |
| 163 | // TODO Hook up variations. |
| 164 | return ot_font->vmtx.get_relaxed ()->has_font_extents; |
| 165 | } |
| 166 | |
| 167 | #ifdef HB_USE_ATEXIT |
| 168 | static void free_static_ot_funcs (void); |
| 169 | #endif |
| 170 | |
| 171 | static struct hb_ot_font_funcs_lazy_loader_t : hb_font_funcs_lazy_loader_t<hb_ot_font_funcs_lazy_loader_t> |
| 172 | { |
| 173 | static inline hb_font_funcs_t *create (void) |
| 174 | { |
| 175 | hb_font_funcs_t *funcs = hb_font_funcs_create (); |
| 176 | |
| 177 | hb_font_funcs_set_font_h_extents_func (funcs, hb_ot_get_font_h_extents, nullptr, nullptr); |
| 178 | hb_font_funcs_set_font_v_extents_func (funcs, hb_ot_get_font_v_extents, nullptr, nullptr); |
| 179 | hb_font_funcs_set_nominal_glyph_func (funcs, hb_ot_get_nominal_glyph, nullptr, nullptr); |
| 180 | hb_font_funcs_set_variation_glyph_func (funcs, hb_ot_get_variation_glyph, nullptr, nullptr); |
| 181 | hb_font_funcs_set_glyph_h_advance_func (funcs, hb_ot_get_glyph_h_advance, nullptr, nullptr); |
| 182 | hb_font_funcs_set_glyph_v_advance_func (funcs, hb_ot_get_glyph_v_advance, nullptr, nullptr); |
| 183 | //hb_font_funcs_set_glyph_h_origin_func (funcs, hb_ot_get_glyph_h_origin, nullptr, nullptr); |
| 184 | //hb_font_funcs_set_glyph_v_origin_func (funcs, hb_ot_get_glyph_v_origin, nullptr, nullptr); |
| 185 | hb_font_funcs_set_glyph_h_kerning_func (funcs, hb_ot_get_glyph_h_kerning, nullptr, nullptr); |
| 186 | //hb_font_funcs_set_glyph_v_kerning_func (funcs, hb_ot_get_glyph_v_kerning, nullptr, nullptr); |
| 187 | hb_font_funcs_set_glyph_extents_func (funcs, hb_ot_get_glyph_extents, nullptr, nullptr); |
| 188 | //hb_font_funcs_set_glyph_contour_point_func (funcs, hb_ot_get_glyph_contour_point, nullptr, nullptr); |
| 189 | hb_font_funcs_set_glyph_name_func (funcs, hb_ot_get_glyph_name, nullptr, nullptr); |
| 190 | hb_font_funcs_set_glyph_from_name_func (funcs, hb_ot_get_glyph_from_name, nullptr, nullptr); |
| 191 | |
| 192 | hb_font_funcs_make_immutable (funcs); |
| 193 | |
| 194 | #ifdef HB_USE_ATEXIT |
| 195 | atexit (free_static_ot_funcs); |
| 196 | #endif |
| 197 | |
| 198 | return funcs; |
| 199 | } |
| 200 | } static_ot_funcs; |
| 201 | |
| 202 | #ifdef HB_USE_ATEXIT |
| 203 | static |
| 204 | void free_static_ot_funcs (void) |
| 205 | { |
| 206 | static_ot_funcs.free_instance (); |
| 207 | } |
| 208 | #endif |
| 209 | |
| 210 | static hb_font_funcs_t * |
| 211 | _hb_ot_get_font_funcs (void) |
| 212 | { |
| 213 | return static_ot_funcs.get_unconst (); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | /** |
| 218 | * hb_ot_font_set_funcs: |
| 219 | * |
| 220 | * Since: 0.9.28 |
| 221 | **/ |
| 222 | void |
| 223 | hb_ot_font_set_funcs (hb_font_t *font) |
| 224 | { |
| 225 | if (unlikely (!hb_ot_shaper_face_data_ensure (font->face))) return; |
| 226 | hb_ot_face_data_t *ot_font = hb_ot_face_data (font->face); |
| 227 | |
| 228 | /* Load them lazies. We access them with get_relaxed() for performance. */ |
| 229 | ot_font->cmap.get (); |
| 230 | ot_font->hmtx.get (); |
| 231 | ot_font->vmtx.get (); |
| 232 | |
| 233 | hb_font_set_funcs (font, |
| 234 | _hb_ot_get_font_funcs (), |
| 235 | ot_font, |
| 236 | nullptr); |
| 237 | } |
| 238 | |