1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/harfbuzz/src/hb-ft.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,522 @@ 1.4 +/* 1.5 + * Copyright © 2009 Red Hat, Inc. 1.6 + * Copyright © 2009 Keith Stribley 1.7 + * 1.8 + * This is part of HarfBuzz, a text shaping library. 1.9 + * 1.10 + * Permission is hereby granted, without written agreement and without 1.11 + * license or royalty fees, to use, copy, modify, and distribute this 1.12 + * software and its documentation for any purpose, provided that the 1.13 + * above copyright notice and the following two paragraphs appear in 1.14 + * all copies of this software. 1.15 + * 1.16 + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 1.17 + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 1.18 + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 1.19 + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 1.20 + * DAMAGE. 1.21 + * 1.22 + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 1.23 + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 1.24 + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 1.25 + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 1.26 + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 1.27 + * 1.28 + * Red Hat Author(s): Behdad Esfahbod 1.29 + */ 1.30 + 1.31 +#include "hb-private.hh" 1.32 + 1.33 +#include "hb-ft.h" 1.34 + 1.35 +#include "hb-font-private.hh" 1.36 + 1.37 +#include FT_ADVANCES_H 1.38 +#include FT_TRUETYPE_TABLES_H 1.39 + 1.40 + 1.41 + 1.42 +#ifndef HB_DEBUG_FT 1.43 +#define HB_DEBUG_FT (HB_DEBUG+0) 1.44 +#endif 1.45 + 1.46 + 1.47 +/* TODO: 1.48 + * 1.49 + * In general, this file does a fine job of what it's supposed to do. 1.50 + * There are, however, things that need more work: 1.51 + * 1.52 + * - We don't handle any load_flags. That definitely has API implications. :( 1.53 + * I believe hb_ft_font_create() should take load_flags input. 1.54 + * In particular, FT_Get_Advance() without the NO_HINTING flag seems to be 1.55 + * buggy. 1.56 + * 1.57 + * - We don't handle / allow for emboldening / obliqueing. 1.58 + * 1.59 + * - In the future, we should add constructors to create fonts in font space? 1.60 + * 1.61 + * - FT_Load_Glyph() is exteremely costly. Do something about it? 1.62 + */ 1.63 + 1.64 + 1.65 +static hb_bool_t 1.66 +hb_ft_get_glyph (hb_font_t *font HB_UNUSED, 1.67 + void *font_data, 1.68 + hb_codepoint_t unicode, 1.69 + hb_codepoint_t variation_selector, 1.70 + hb_codepoint_t *glyph, 1.71 + void *user_data HB_UNUSED) 1.72 + 1.73 +{ 1.74 + FT_Face ft_face = (FT_Face) font_data; 1.75 + 1.76 +#ifdef HAVE_FT_FACE_GETCHARVARIANTINDEX 1.77 + if (unlikely (variation_selector)) { 1.78 + *glyph = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector); 1.79 + return *glyph != 0; 1.80 + } 1.81 +#endif 1.82 + 1.83 + *glyph = FT_Get_Char_Index (ft_face, unicode); 1.84 + return *glyph != 0; 1.85 +} 1.86 + 1.87 +static hb_position_t 1.88 +hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED, 1.89 + void *font_data, 1.90 + hb_codepoint_t glyph, 1.91 + void *user_data HB_UNUSED) 1.92 +{ 1.93 + FT_Face ft_face = (FT_Face) font_data; 1.94 + int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING; 1.95 + FT_Fixed v; 1.96 + 1.97 + if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v))) 1.98 + return 0; 1.99 + 1.100 + return (v + (1<<9)) >> 10; 1.101 +} 1.102 + 1.103 +static hb_position_t 1.104 +hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED, 1.105 + void *font_data, 1.106 + hb_codepoint_t glyph, 1.107 + void *user_data HB_UNUSED) 1.108 +{ 1.109 + FT_Face ft_face = (FT_Face) font_data; 1.110 + int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING | FT_LOAD_VERTICAL_LAYOUT; 1.111 + FT_Fixed v; 1.112 + 1.113 + if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v))) 1.114 + return 0; 1.115 + 1.116 + /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates 1.117 + * have a Y growing upward. Hence the extra negation. */ 1.118 + return (-v + (1<<9)) >> 10; 1.119 +} 1.120 + 1.121 +static hb_bool_t 1.122 +hb_ft_get_glyph_h_origin (hb_font_t *font HB_UNUSED, 1.123 + void *font_data HB_UNUSED, 1.124 + hb_codepoint_t glyph HB_UNUSED, 1.125 + hb_position_t *x HB_UNUSED, 1.126 + hb_position_t *y HB_UNUSED, 1.127 + void *user_data HB_UNUSED) 1.128 +{ 1.129 + /* We always work in the horizontal coordinates. */ 1.130 + return true; 1.131 +} 1.132 + 1.133 +static hb_bool_t 1.134 +hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED, 1.135 + void *font_data, 1.136 + hb_codepoint_t glyph, 1.137 + hb_position_t *x, 1.138 + hb_position_t *y, 1.139 + void *user_data HB_UNUSED) 1.140 +{ 1.141 + FT_Face ft_face = (FT_Face) font_data; 1.142 + int load_flags = FT_LOAD_DEFAULT; 1.143 + 1.144 + if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags))) 1.145 + return false; 1.146 + 1.147 + /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates 1.148 + * have a Y growing upward. Hence the extra negation. */ 1.149 + *x = ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBearingX; 1.150 + *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY); 1.151 + 1.152 + return true; 1.153 +} 1.154 + 1.155 +static hb_position_t 1.156 +hb_ft_get_glyph_h_kerning (hb_font_t *font, 1.157 + void *font_data, 1.158 + hb_codepoint_t left_glyph, 1.159 + hb_codepoint_t right_glyph, 1.160 + void *user_data HB_UNUSED) 1.161 +{ 1.162 + FT_Face ft_face = (FT_Face) font_data; 1.163 + FT_Vector kerningv; 1.164 + 1.165 + FT_Kerning_Mode mode = font->x_ppem ? FT_KERNING_DEFAULT : FT_KERNING_UNFITTED; 1.166 + if (FT_Get_Kerning (ft_face, left_glyph, right_glyph, mode, &kerningv)) 1.167 + return 0; 1.168 + 1.169 + return kerningv.x; 1.170 +} 1.171 + 1.172 +static hb_position_t 1.173 +hb_ft_get_glyph_v_kerning (hb_font_t *font HB_UNUSED, 1.174 + void *font_data HB_UNUSED, 1.175 + hb_codepoint_t top_glyph HB_UNUSED, 1.176 + hb_codepoint_t bottom_glyph HB_UNUSED, 1.177 + void *user_data HB_UNUSED) 1.178 +{ 1.179 + /* FreeType API doesn't support vertical kerning */ 1.180 + return 0; 1.181 +} 1.182 + 1.183 +static hb_bool_t 1.184 +hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED, 1.185 + void *font_data, 1.186 + hb_codepoint_t glyph, 1.187 + hb_glyph_extents_t *extents, 1.188 + void *user_data HB_UNUSED) 1.189 +{ 1.190 + FT_Face ft_face = (FT_Face) font_data; 1.191 + int load_flags = FT_LOAD_DEFAULT; 1.192 + 1.193 + if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags))) 1.194 + return false; 1.195 + 1.196 + extents->x_bearing = ft_face->glyph->metrics.horiBearingX; 1.197 + extents->y_bearing = ft_face->glyph->metrics.horiBearingY; 1.198 + extents->width = ft_face->glyph->metrics.width; 1.199 + extents->height = -ft_face->glyph->metrics.height; 1.200 + return true; 1.201 +} 1.202 + 1.203 +static hb_bool_t 1.204 +hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED, 1.205 + void *font_data, 1.206 + hb_codepoint_t glyph, 1.207 + unsigned int point_index, 1.208 + hb_position_t *x, 1.209 + hb_position_t *y, 1.210 + void *user_data HB_UNUSED) 1.211 +{ 1.212 + FT_Face ft_face = (FT_Face) font_data; 1.213 + int load_flags = FT_LOAD_DEFAULT; 1.214 + 1.215 + if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags))) 1.216 + return false; 1.217 + 1.218 + if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)) 1.219 + return false; 1.220 + 1.221 + if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points)) 1.222 + return false; 1.223 + 1.224 + *x = ft_face->glyph->outline.points[point_index].x; 1.225 + *y = ft_face->glyph->outline.points[point_index].y; 1.226 + 1.227 + return true; 1.228 +} 1.229 + 1.230 +static hb_bool_t 1.231 +hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED, 1.232 + void *font_data, 1.233 + hb_codepoint_t glyph, 1.234 + char *name, unsigned int size, 1.235 + void *user_data HB_UNUSED) 1.236 +{ 1.237 + FT_Face ft_face = (FT_Face) font_data; 1.238 + 1.239 + hb_bool_t ret = !FT_Get_Glyph_Name (ft_face, glyph, name, size); 1.240 + if (ret && (size && !*name)) 1.241 + ret = false; 1.242 + 1.243 + return ret; 1.244 +} 1.245 + 1.246 +static hb_bool_t 1.247 +hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED, 1.248 + void *font_data, 1.249 + const char *name, int len, /* -1 means nul-terminated */ 1.250 + hb_codepoint_t *glyph, 1.251 + void *user_data HB_UNUSED) 1.252 +{ 1.253 + FT_Face ft_face = (FT_Face) font_data; 1.254 + 1.255 + if (len < 0) 1.256 + *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name); 1.257 + else { 1.258 + /* Make a nul-terminated version. */ 1.259 + char buf[128]; 1.260 + len = MIN (len, (int) sizeof (buf) - 1); 1.261 + strncpy (buf, name, len); 1.262 + buf[len] = '\0'; 1.263 + *glyph = FT_Get_Name_Index (ft_face, buf); 1.264 + } 1.265 + 1.266 + if (*glyph == 0) 1.267 + { 1.268 + /* Check whether the given name was actually the name of glyph 0. */ 1.269 + char buf[128]; 1.270 + if (!FT_Get_Glyph_Name(ft_face, 0, buf, sizeof (buf)) && 1.271 + len < 0 ? !strcmp (buf, name) : !strncmp (buf, name, len)) 1.272 + return true; 1.273 + } 1.274 + 1.275 + return *glyph != 0; 1.276 +} 1.277 + 1.278 + 1.279 +static hb_font_funcs_t * 1.280 +_hb_ft_get_font_funcs (void) 1.281 +{ 1.282 + static const hb_font_funcs_t ft_ffuncs = { 1.283 + HB_OBJECT_HEADER_STATIC, 1.284 + 1.285 + true, /* immutable */ 1.286 + 1.287 + { 1.288 +#define HB_FONT_FUNC_IMPLEMENT(name) hb_ft_get_##name, 1.289 + HB_FONT_FUNCS_IMPLEMENT_CALLBACKS 1.290 +#undef HB_FONT_FUNC_IMPLEMENT 1.291 + } 1.292 + }; 1.293 + 1.294 + return const_cast<hb_font_funcs_t *> (&ft_ffuncs); 1.295 +} 1.296 + 1.297 + 1.298 +static hb_blob_t * 1.299 +reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data) 1.300 +{ 1.301 + FT_Face ft_face = (FT_Face) user_data; 1.302 + FT_Byte *buffer; 1.303 + FT_ULong length = 0; 1.304 + FT_Error error; 1.305 + 1.306 + /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob */ 1.307 + 1.308 + error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length); 1.309 + if (error) 1.310 + return NULL; 1.311 + 1.312 + buffer = (FT_Byte *) malloc (length); 1.313 + if (buffer == NULL) 1.314 + return NULL; 1.315 + 1.316 + error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length); 1.317 + if (error) 1.318 + return NULL; 1.319 + 1.320 + return hb_blob_create ((const char *) buffer, length, 1.321 + HB_MEMORY_MODE_WRITABLE, 1.322 + buffer, free); 1.323 +} 1.324 + 1.325 +/** 1.326 + * hb_ft_face_create: 1.327 + * @ft_face: (destroy destroy) (scope notified): 1.328 + * @destroy: 1.329 + * 1.330 + * 1.331 + * 1.332 + * Return value: (transfer full): 1.333 + * Since: 1.0 1.334 + **/ 1.335 +hb_face_t * 1.336 +hb_ft_face_create (FT_Face ft_face, 1.337 + hb_destroy_func_t destroy) 1.338 +{ 1.339 + hb_face_t *face; 1.340 + 1.341 + if (ft_face->stream->read == NULL) { 1.342 + hb_blob_t *blob; 1.343 + 1.344 + blob = hb_blob_create ((const char *) ft_face->stream->base, 1.345 + (unsigned int) ft_face->stream->size, 1.346 + /* TODO: We assume that it's mmap()'ed, but FreeType code 1.347 + * suggests that there are cases we reach here but font is 1.348 + * not mmapped. For example, when mmap() fails. No idea 1.349 + * how to deal with it better here. */ 1.350 + HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE, 1.351 + ft_face, destroy); 1.352 + face = hb_face_create (blob, ft_face->face_index); 1.353 + hb_blob_destroy (blob); 1.354 + } else { 1.355 + face = hb_face_create_for_tables (reference_table, ft_face, destroy); 1.356 + } 1.357 + 1.358 + hb_face_set_index (face, ft_face->face_index); 1.359 + hb_face_set_upem (face, ft_face->units_per_EM); 1.360 + 1.361 + return face; 1.362 +} 1.363 + 1.364 +static void 1.365 +hb_ft_face_finalize (FT_Face ft_face) 1.366 +{ 1.367 + hb_face_destroy ((hb_face_t *) ft_face->generic.data); 1.368 +} 1.369 + 1.370 +/** 1.371 + * hb_ft_face_create_cached: 1.372 + * @ft_face: 1.373 + * 1.374 + * 1.375 + * 1.376 + * Return value: (transfer full): 1.377 + * Since: 1.0 1.378 + **/ 1.379 +hb_face_t * 1.380 +hb_ft_face_create_cached (FT_Face ft_face) 1.381 +{ 1.382 + if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize)) 1.383 + { 1.384 + if (ft_face->generic.finalizer) 1.385 + ft_face->generic.finalizer (ft_face); 1.386 + 1.387 + ft_face->generic.data = hb_ft_face_create (ft_face, NULL); 1.388 + ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize; 1.389 + } 1.390 + 1.391 + return hb_face_reference ((hb_face_t *) ft_face->generic.data); 1.392 +} 1.393 + 1.394 +static void 1.395 +_do_nothing (void) 1.396 +{ 1.397 +} 1.398 + 1.399 + 1.400 +/** 1.401 + * hb_ft_font_create: 1.402 + * @ft_face: (destroy destroy) (scope notified): 1.403 + * @destroy: 1.404 + * 1.405 + * 1.406 + * 1.407 + * Return value: (transfer full): 1.408 + * Since: 1.0 1.409 + **/ 1.410 +hb_font_t * 1.411 +hb_ft_font_create (FT_Face ft_face, 1.412 + hb_destroy_func_t destroy) 1.413 +{ 1.414 + hb_font_t *font; 1.415 + hb_face_t *face; 1.416 + 1.417 + face = hb_ft_face_create (ft_face, destroy); 1.418 + font = hb_font_create (face); 1.419 + hb_face_destroy (face); 1.420 + hb_font_set_funcs (font, 1.421 + _hb_ft_get_font_funcs (), 1.422 + ft_face, (hb_destroy_func_t) _do_nothing); 1.423 + hb_font_set_scale (font, 1.424 + (int) (((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM + (1<<15)) >> 16), 1.425 + (int) (((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM + (1<<15)) >> 16)); 1.426 + hb_font_set_ppem (font, 1.427 + ft_face->size->metrics.x_ppem, 1.428 + ft_face->size->metrics.y_ppem); 1.429 + 1.430 + return font; 1.431 +} 1.432 + 1.433 + 1.434 +/* Thread-safe, lock-free, FT_Library */ 1.435 + 1.436 +static FT_Library ft_library; 1.437 + 1.438 +static inline 1.439 +void free_ft_library (void) 1.440 +{ 1.441 + FT_Done_FreeType (ft_library); 1.442 +} 1.443 + 1.444 +static FT_Library 1.445 +get_ft_library (void) 1.446 +{ 1.447 +retry: 1.448 + FT_Library library = (FT_Library) hb_atomic_ptr_get (&ft_library); 1.449 + 1.450 + if (unlikely (!library)) 1.451 + { 1.452 + /* Not found; allocate one. */ 1.453 + if (FT_Init_FreeType (&library)) 1.454 + return NULL; 1.455 + 1.456 + if (!hb_atomic_ptr_cmpexch (&ft_library, NULL, library)) { 1.457 + FT_Done_FreeType (library); 1.458 + goto retry; 1.459 + } 1.460 + 1.461 +#ifdef HAVE_ATEXIT 1.462 + atexit (free_ft_library); /* First person registers atexit() callback. */ 1.463 +#endif 1.464 + } 1.465 + 1.466 + return library; 1.467 +} 1.468 + 1.469 +static void 1.470 +_release_blob (FT_Face ft_face) 1.471 +{ 1.472 + hb_blob_destroy ((hb_blob_t *) ft_face->generic.data); 1.473 +} 1.474 + 1.475 +void 1.476 +hb_ft_font_set_funcs (hb_font_t *font) 1.477 +{ 1.478 + hb_blob_t *blob = hb_face_reference_blob (font->face); 1.479 + unsigned int blob_length; 1.480 + const char *blob_data = hb_blob_get_data (blob, &blob_length); 1.481 + if (unlikely (!blob_length)) 1.482 + DEBUG_MSG (FT, font, "Font face has empty blob"); 1.483 + 1.484 + FT_Face ft_face = NULL; 1.485 + FT_Error err = FT_New_Memory_Face (get_ft_library (), 1.486 + (const FT_Byte *) blob_data, 1.487 + blob_length, 1.488 + hb_face_get_index (font->face), 1.489 + &ft_face); 1.490 + 1.491 + if (unlikely (err)) { 1.492 + hb_blob_destroy (blob); 1.493 + DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed"); 1.494 + return; 1.495 + } 1.496 + 1.497 + FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE); 1.498 + 1.499 + assert (font->y_scale >= 0); 1.500 + FT_Set_Char_Size (ft_face, 1.501 + font->x_scale, font->y_scale, 1.502 + 0, 0); 1.503 +#if 0 1.504 + font->x_ppem * 72 * 64 / font->x_scale, 1.505 + font->y_ppem * 72 * 64 / font->y_scale); 1.506 +#endif 1.507 + 1.508 + ft_face->generic.data = blob; 1.509 + ft_face->generic.finalizer = (FT_Generic_Finalizer) _release_blob; 1.510 + 1.511 + hb_font_set_funcs (font, 1.512 + _hb_ft_get_font_funcs (), 1.513 + ft_face, 1.514 + (hb_destroy_func_t) FT_Done_Face); 1.515 +} 1.516 + 1.517 +FT_Face 1.518 +hb_ft_font_get_face (hb_font_t *font) 1.519 +{ 1.520 + if (font->destroy == (hb_destroy_func_t) FT_Done_Face || 1.521 + font->destroy == (hb_destroy_func_t) _do_nothing) 1.522 + return (FT_Face) font->user_data; 1.523 + 1.524 + return NULL; 1.525 +}