OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright © 2009 Red Hat, Inc. |
| 3 * Copyright © 2009 Keith Stribley |
| 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 */ |
| 27 |
| 28 #include "hb-private.hh" |
| 29 |
| 30 #include "hb-ft.h" |
| 31 |
| 32 #include "hb-font-private.hh" |
| 33 |
| 34 #include FT_ADVANCES_H |
| 35 #include FT_TRUETYPE_TABLES_H |
| 36 |
| 37 |
| 38 |
| 39 #ifndef HB_DEBUG_FT |
| 40 #define HB_DEBUG_FT (HB_DEBUG+0) |
| 41 #endif |
| 42 |
| 43 |
| 44 /* TODO: |
| 45 * |
| 46 * In general, this file does a fine job of what it's supposed to do. |
| 47 * There are, however, things that need more work: |
| 48 * |
| 49 * - We don't handle any load_flags. That definitely has API implications. :( |
| 50 * I believe hb_ft_font_create() should take load_flags input. |
| 51 * In particular, FT_Get_Advance() without the NO_HINTING flag seems to be |
| 52 * buggy. |
| 53 * |
| 54 * - We don't handle / allow for emboldening / obliqueing. |
| 55 * |
| 56 * - Rounding, etc? |
| 57 * |
| 58 * - In the future, we should add constructors to create fonts in font space. |
| 59 * |
| 60 * - I believe transforms are not correctly implemented. FreeType does not |
| 61 * provide any API to get to the transform/delta set on the face. :( |
| 62 * |
| 63 * - Always use FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH? |
| 64 */ |
| 65 |
| 66 |
| 67 static hb_bool_t |
| 68 hb_ft_get_glyph (hb_font_t *font HB_UNUSED, |
| 69 void *font_data, |
| 70 hb_codepoint_t unicode, |
| 71 hb_codepoint_t variation_selector, |
| 72 hb_codepoint_t *glyph, |
| 73 void *user_data HB_UNUSED) |
| 74 |
| 75 { |
| 76 FT_Face ft_face = (FT_Face) font_data; |
| 77 |
| 78 #ifdef HAVE_FT_FACE_GETCHARVARIANTINDEX |
| 79 if (unlikely (variation_selector)) { |
| 80 *glyph = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector); |
| 81 if (*glyph) |
| 82 return TRUE; |
| 83 } |
| 84 #endif |
| 85 |
| 86 *glyph = FT_Get_Char_Index (ft_face, unicode); |
| 87 return *glyph != 0; |
| 88 } |
| 89 |
| 90 static hb_position_t |
| 91 hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED, |
| 92 void *font_data, |
| 93 hb_codepoint_t glyph, |
| 94 void *user_data HB_UNUSED) |
| 95 { |
| 96 FT_Face ft_face = (FT_Face) font_data; |
| 97 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING; |
| 98 FT_Fixed v; |
| 99 |
| 100 if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v))) |
| 101 return 0; |
| 102 |
| 103 return v >> 10; |
| 104 } |
| 105 |
| 106 static hb_position_t |
| 107 hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED, |
| 108 void *font_data, |
| 109 hb_codepoint_t glyph, |
| 110 void *user_data HB_UNUSED) |
| 111 { |
| 112 FT_Face ft_face = (FT_Face) font_data; |
| 113 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING | FT_LOAD_VERTICAL_LAYOU
T; |
| 114 FT_Fixed v; |
| 115 |
| 116 if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v))) |
| 117 return 0; |
| 118 |
| 119 /* Note: FreeType's vertical metrics grows downward while other FreeType coord
inates |
| 120 * have a Y growing upward. Hence the extra negation. */ |
| 121 return -v >> 10; |
| 122 } |
| 123 |
| 124 static hb_bool_t |
| 125 hb_ft_get_glyph_h_origin (hb_font_t *font HB_UNUSED, |
| 126 void *font_data HB_UNUSED, |
| 127 hb_codepoint_t glyph HB_UNUSED, |
| 128 hb_position_t *x HB_UNUSED, |
| 129 hb_position_t *y HB_UNUSED, |
| 130 void *user_data HB_UNUSED) |
| 131 { |
| 132 /* We always work in the horizontal coordinates. */ |
| 133 return TRUE; |
| 134 } |
| 135 |
| 136 static hb_bool_t |
| 137 hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED, |
| 138 void *font_data, |
| 139 hb_codepoint_t glyph, |
| 140 hb_position_t *x, |
| 141 hb_position_t *y, |
| 142 void *user_data HB_UNUSED) |
| 143 { |
| 144 FT_Face ft_face = (FT_Face) font_data; |
| 145 int load_flags = FT_LOAD_DEFAULT; |
| 146 |
| 147 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags))) |
| 148 return FALSE; |
| 149 |
| 150 /* Note: FreeType's vertical metrics grows downward while other FreeType coord
inates |
| 151 * have a Y growing upward. Hence the extra negation. */ |
| 152 *x = ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBear
ingX; |
| 153 *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBear
ingY); |
| 154 |
| 155 return TRUE; |
| 156 } |
| 157 |
| 158 static hb_position_t |
| 159 hb_ft_get_glyph_h_kerning (hb_font_t *font HB_UNUSED, |
| 160 void *font_data, |
| 161 hb_codepoint_t left_glyph, |
| 162 hb_codepoint_t right_glyph, |
| 163 void *user_data HB_UNUSED) |
| 164 { |
| 165 FT_Face ft_face = (FT_Face) font_data; |
| 166 FT_Vector kerningv; |
| 167 |
| 168 if (FT_Get_Kerning (ft_face, left_glyph, right_glyph, FT_KERNING_DEFAULT, &ker
ningv)) |
| 169 return 0; |
| 170 |
| 171 return kerningv.x; |
| 172 } |
| 173 |
| 174 static hb_position_t |
| 175 hb_ft_get_glyph_v_kerning (hb_font_t *font HB_UNUSED, |
| 176 void *font_data HB_UNUSED, |
| 177 hb_codepoint_t top_glyph HB_UNUSED, |
| 178 hb_codepoint_t bottom_glyph HB_UNUSED, |
| 179 void *user_data HB_UNUSED) |
| 180 { |
| 181 /* FreeType API doesn't support vertical kerning */ |
| 182 return 0; |
| 183 } |
| 184 |
| 185 static hb_bool_t |
| 186 hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED, |
| 187 void *font_data, |
| 188 hb_codepoint_t glyph, |
| 189 hb_glyph_extents_t *extents, |
| 190 void *user_data HB_UNUSED) |
| 191 { |
| 192 FT_Face ft_face = (FT_Face) font_data; |
| 193 int load_flags = FT_LOAD_DEFAULT; |
| 194 |
| 195 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags))) |
| 196 return FALSE; |
| 197 |
| 198 extents->x_bearing = ft_face->glyph->metrics.horiBearingX; |
| 199 extents->y_bearing = ft_face->glyph->metrics.horiBearingY; |
| 200 extents->width = ft_face->glyph->metrics.width; |
| 201 extents->height = ft_face->glyph->metrics.height; |
| 202 return TRUE; |
| 203 } |
| 204 |
| 205 static hb_bool_t |
| 206 hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED, |
| 207 void *font_data, |
| 208 hb_codepoint_t glyph, |
| 209 unsigned int point_index, |
| 210 hb_position_t *x, |
| 211 hb_position_t *y, |
| 212 void *user_data HB_UNUSED) |
| 213 { |
| 214 FT_Face ft_face = (FT_Face) font_data; |
| 215 int load_flags = FT_LOAD_DEFAULT; |
| 216 |
| 217 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags))) |
| 218 return FALSE; |
| 219 |
| 220 if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)) |
| 221 return FALSE; |
| 222 |
| 223 if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points)) |
| 224 return FALSE; |
| 225 |
| 226 *x = ft_face->glyph->outline.points[point_index].x; |
| 227 *y = ft_face->glyph->outline.points[point_index].y; |
| 228 |
| 229 return TRUE; |
| 230 } |
| 231 |
| 232 static hb_font_funcs_t ft_ffuncs = { |
| 233 HB_OBJECT_HEADER_STATIC, |
| 234 |
| 235 TRUE, /* immutable */ |
| 236 |
| 237 { |
| 238 hb_ft_get_glyph, |
| 239 hb_ft_get_glyph_h_advance, |
| 240 hb_ft_get_glyph_v_advance, |
| 241 hb_ft_get_glyph_h_origin, |
| 242 hb_ft_get_glyph_v_origin, |
| 243 hb_ft_get_glyph_h_kerning, |
| 244 hb_ft_get_glyph_v_kerning, |
| 245 hb_ft_get_glyph_extents, |
| 246 hb_ft_get_glyph_contour_point, |
| 247 } |
| 248 }; |
| 249 |
| 250 static hb_font_funcs_t * |
| 251 _hb_ft_get_font_funcs (void) |
| 252 { |
| 253 return &ft_ffuncs; |
| 254 } |
| 255 |
| 256 |
| 257 static hb_blob_t * |
| 258 reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data) |
| 259 { |
| 260 FT_Face ft_face = (FT_Face) user_data; |
| 261 FT_Byte *buffer; |
| 262 FT_ULong length = 0; |
| 263 FT_Error error; |
| 264 |
| 265 /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob
*/ |
| 266 |
| 267 error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length); |
| 268 if (error) |
| 269 return NULL; |
| 270 |
| 271 buffer = (FT_Byte *) malloc (length); |
| 272 if (buffer == NULL) |
| 273 return NULL; |
| 274 |
| 275 error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length); |
| 276 if (error) |
| 277 return NULL; |
| 278 |
| 279 return hb_blob_create ((const char *) buffer, length, |
| 280 HB_MEMORY_MODE_WRITABLE, |
| 281 buffer, free); |
| 282 } |
| 283 |
| 284 |
| 285 hb_face_t * |
| 286 hb_ft_face_create (FT_Face ft_face, |
| 287 hb_destroy_func_t destroy) |
| 288 { |
| 289 hb_face_t *face; |
| 290 |
| 291 if (ft_face->stream->read == NULL) { |
| 292 hb_blob_t *blob; |
| 293 |
| 294 blob = hb_blob_create ((const char *) ft_face->stream->base, |
| 295 (unsigned int) ft_face->stream->size, |
| 296 /* TODO: We assume that it's mmap()'ed, but FreeType
code |
| 297 * suggests that there are cases we reach here but fo
nt is |
| 298 * not mmapped. For example, when mmap() fails. No
idea |
| 299 * how to deal with it better here. */ |
| 300 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE, |
| 301 ft_face, destroy); |
| 302 face = hb_face_create (blob, ft_face->face_index); |
| 303 hb_blob_destroy (blob); |
| 304 } else { |
| 305 face = hb_face_create_for_tables (reference_table, ft_face, destroy); |
| 306 } |
| 307 |
| 308 hb_face_set_index (face, ft_face->face_index); |
| 309 hb_face_set_upem (face, ft_face->units_per_EM); |
| 310 |
| 311 return face; |
| 312 } |
| 313 |
| 314 static void |
| 315 hb_ft_face_finalize (FT_Face ft_face) |
| 316 { |
| 317 hb_face_destroy ((hb_face_t *) ft_face->generic.data); |
| 318 } |
| 319 |
| 320 hb_face_t * |
| 321 hb_ft_face_create_cached (FT_Face ft_face) |
| 322 { |
| 323 if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Gene
ric_Finalizer) hb_ft_face_finalize)) |
| 324 { |
| 325 if (ft_face->generic.finalizer) |
| 326 ft_face->generic.finalizer (ft_face); |
| 327 |
| 328 ft_face->generic.data = hb_ft_face_create (ft_face, NULL); |
| 329 ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize; |
| 330 } |
| 331 |
| 332 return hb_face_reference ((hb_face_t *) ft_face->generic.data); |
| 333 } |
| 334 |
| 335 static void |
| 336 _do_nothing (void) |
| 337 { |
| 338 } |
| 339 |
| 340 |
| 341 hb_font_t * |
| 342 hb_ft_font_create (FT_Face ft_face, |
| 343 hb_destroy_func_t destroy) |
| 344 { |
| 345 hb_font_t *font; |
| 346 hb_face_t *face; |
| 347 |
| 348 face = hb_ft_face_create (ft_face, destroy); |
| 349 font = hb_font_create (face); |
| 350 hb_face_destroy (face); |
| 351 hb_font_set_funcs (font, |
| 352 _hb_ft_get_font_funcs (), |
| 353 ft_face, (hb_destroy_func_t) _do_nothing); |
| 354 hb_font_set_scale (font, |
| 355 ((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_
face->units_per_EM) >> 16, |
| 356 ((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_
face->units_per_EM) >> 16); |
| 357 hb_font_set_ppem (font, |
| 358 ft_face->size->metrics.x_ppem, |
| 359 ft_face->size->metrics.y_ppem); |
| 360 |
| 361 return font; |
| 362 } |
| 363 |
| 364 |
| 365 |
| 366 |
| 367 static FT_Library ft_library; |
| 368 static hb_bool_t ft_library_initialized; |
| 369 static struct ft_library_destructor { |
| 370 ~ft_library_destructor (void) { |
| 371 if (ft_library) |
| 372 FT_Done_FreeType (ft_library); |
| 373 } |
| 374 } static_ft_library_destructor; |
| 375 |
| 376 static FT_Library |
| 377 _get_ft_library (void) |
| 378 { |
| 379 if (unlikely (!ft_library_initialized)) { |
| 380 FT_Init_FreeType (&ft_library); |
| 381 ft_library_initialized = TRUE; |
| 382 } |
| 383 |
| 384 return ft_library; |
| 385 } |
| 386 |
| 387 static void |
| 388 _release_blob (FT_Face ft_face) |
| 389 { |
| 390 hb_blob_destroy ((hb_blob_t *) ft_face->generic.data); |
| 391 } |
| 392 |
| 393 void |
| 394 hb_ft_font_set_funcs (hb_font_t *font) |
| 395 { |
| 396 hb_blob_t *blob = hb_face_reference_blob (font->face); |
| 397 unsigned int blob_length; |
| 398 const char *blob_data = hb_blob_get_data (blob, &blob_length); |
| 399 if (unlikely (!blob_length)) |
| 400 DEBUG_MSG (FT, font, "Font face has empty blob"); |
| 401 |
| 402 FT_Face ft_face = NULL; |
| 403 FT_Error err = FT_New_Memory_Face (_get_ft_library (), |
| 404 (const FT_Byte *) blob_data, |
| 405 blob_length, |
| 406 hb_face_get_index (font->face), |
| 407 &ft_face); |
| 408 |
| 409 if (unlikely (err)) { |
| 410 hb_blob_destroy (blob); |
| 411 DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed"); |
| 412 return; |
| 413 } |
| 414 |
| 415 FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE); |
| 416 |
| 417 FT_Set_Char_Size (ft_face, |
| 418 font->x_scale, font->y_scale, |
| 419 font->x_ppem * 72 * 64 / font->x_scale, |
| 420 font->y_ppem * 72 * 64 / font->y_scale); |
| 421 |
| 422 ft_face->generic.data = blob; |
| 423 ft_face->generic.finalizer = (FT_Generic_Finalizer) _release_blob; |
| 424 |
| 425 hb_font_set_funcs (font, |
| 426 _hb_ft_get_font_funcs (), |
| 427 ft_face, |
| 428 (hb_destroy_func_t) FT_Done_Face); |
| 429 } |
| 430 |
| 431 FT_Face |
| 432 hb_ft_font_get_face (hb_font_t *font) |
| 433 { |
| 434 if (font->destroy == (hb_destroy_func_t) FT_Done_Face || |
| 435 font->destroy == (hb_destroy_func_t) _do_nothing) |
| 436 return (FT_Face) font->user_data; |
| 437 |
| 438 return NULL; |
| 439 } |
OLD | NEW |