2 * Copyright © 2009 Red Hat, Inc.
3 * Copyright © 2009 Keith Stribley
4 * Copyright © 2015 Google, Inc.
6 * This is part of HarfBuzz, a text shaping library.
8 * Permission is hereby granted, without written agreement and without
9 * license or royalty fees, to use, copy, modify, and distribute this
10 * software and its documentation for any purpose, provided that the
11 * above copyright notice and the following two paragraphs appear in
12 * all copies of this software.
14 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
17 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
20 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
21 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
26 * Red Hat Author(s): Behdad Esfahbod
27 * Google Author(s): Behdad Esfahbod
30 #include "hb-private.hh"
34 #include "hb-font-private.hh"
36 #include "hb-cache-private.hh" // Maybe use in the future?
38 #include FT_ADVANCES_H
39 #include FT_MULTIPLE_MASTERS_H
40 #include FT_TRUETYPE_TABLES_H
45 #define HB_DEBUG_FT (HB_DEBUG+0)
51 * In general, this file does a fine job of what it's supposed to do.
52 * There are, however, things that need more work:
54 * - I remember seeing FT_Get_Advance() without the NO_HINTING flag to be buggy.
55 * Have not investigated.
57 * - FreeType works in 26.6 mode. Clients can decide to use that mode, and everything
58 * would work fine. However, we also abuse this API for performing in font-space,
59 * but don't pass the correct flags to FreeType. We just abuse the no-hinting mode
60 * for that, such that no rounding etc happens. As such, we don't set ppem, and
61 * pass NO_HINTING as load_flags. Would be much better to use NO_SCALE, and scale
62 * ourselves, like we do in uniscribe, etc.
64 * - We don't handle / allow for emboldening / obliqueing.
66 * - In the future, we should add constructors to create fonts in font space?
68 * - FT_Load_Glyph() is exteremely costly. Do something about it?
76 bool symbol; /* Whether selected cmap is symbol cmap. */
77 bool unref; /* Whether to destroy ft_face when done. */
81 _hb_ft_font_create (FT_Face ft_face, bool symbol, bool unref)
83 hb_ft_font_t *ft_font = (hb_ft_font_t *) calloc (1, sizeof (hb_ft_font_t));
85 if (unlikely (!ft_font))
88 ft_font->ft_face = ft_face;
89 ft_font->symbol = symbol;
90 ft_font->unref = unref;
92 ft_font->load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
98 _hb_ft_face_destroy (FT_Face ft_face)
100 FT_Done_Face (ft_face);
104 _hb_ft_font_destroy (hb_ft_font_t *ft_font)
107 _hb_ft_face_destroy (ft_font->ft_face);
113 * hb_ft_font_set_load_flags:
122 hb_ft_font_set_load_flags (hb_font_t *font, int load_flags)
127 if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
130 hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
132 ft_font->load_flags = load_flags;
136 * hb_ft_font_get_load_flags:
145 hb_ft_font_get_load_flags (hb_font_t *font)
147 if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
150 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
152 return ft_font->load_flags;
156 hb_ft_font_get_face (hb_font_t *font)
158 if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
161 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
163 return ft_font->ft_face;
169 hb_ft_get_nominal_glyph (hb_font_t *font HB_UNUSED,
171 hb_codepoint_t unicode,
172 hb_codepoint_t *glyph,
173 void *user_data HB_UNUSED)
175 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
176 unsigned int g = FT_Get_Char_Index (ft_font->ft_face, unicode);
180 if (unlikely (ft_font->symbol) && unicode <= 0x00FFu)
182 /* For symbol-encoded OpenType fonts, we duplicate the
183 * U+F000..F0FF range at U+0000..U+00FF. That's what
184 * Windows seems to do, and that's hinted about at:
185 * http://www.microsoft.com/typography/otspec/recom.htm
186 * under "Non-Standard (Symbol) Fonts". */
187 g = FT_Get_Char_Index (ft_font->ft_face, 0xF000u + unicode);
200 hb_ft_get_variation_glyph (hb_font_t *font HB_UNUSED,
202 hb_codepoint_t unicode,
203 hb_codepoint_t variation_selector,
204 hb_codepoint_t *glyph,
205 void *user_data HB_UNUSED)
207 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
208 unsigned int g = FT_Face_GetCharVariantIndex (ft_font->ft_face, unicode, variation_selector);
218 hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
220 hb_codepoint_t glyph,
221 void *user_data HB_UNUSED)
223 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
226 if (unlikely (FT_Get_Advance (ft_font->ft_face, glyph, ft_font->load_flags, &v)))
229 if (font->x_scale < 0)
232 return (v + (1<<9)) >> 10;
236 hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
238 hb_codepoint_t glyph,
239 void *user_data HB_UNUSED)
241 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
244 if (unlikely (FT_Get_Advance (ft_font->ft_face, glyph, ft_font->load_flags | FT_LOAD_VERTICAL_LAYOUT, &v)))
247 if (font->y_scale < 0)
250 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
251 * have a Y growing upward. Hence the extra negation. */
252 return (-v + (1<<9)) >> 10;
256 hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
258 hb_codepoint_t glyph,
261 void *user_data HB_UNUSED)
263 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
264 FT_Face ft_face = ft_font->ft_face;
266 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
269 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
270 * have a Y growing upward. Hence the extra negation. */
271 *x = ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBearingX;
272 *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);
274 if (font->x_scale < 0)
276 if (font->y_scale < 0)
283 hb_ft_get_glyph_h_kerning (hb_font_t *font,
285 hb_codepoint_t left_glyph,
286 hb_codepoint_t right_glyph,
287 void *user_data HB_UNUSED)
289 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
292 FT_Kerning_Mode mode = font->x_ppem ? FT_KERNING_DEFAULT : FT_KERNING_UNFITTED;
293 if (FT_Get_Kerning (ft_font->ft_face, left_glyph, right_glyph, mode, &kerningv))
300 hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
302 hb_codepoint_t glyph,
303 hb_glyph_extents_t *extents,
304 void *user_data HB_UNUSED)
306 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
307 FT_Face ft_face = ft_font->ft_face;
309 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
312 extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
313 extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
314 extents->width = ft_face->glyph->metrics.width;
315 extents->height = -ft_face->glyph->metrics.height;
316 if (font->x_scale < 0)
318 extents->x_bearing = -extents->x_bearing;
319 extents->width = -extents->width;
321 if (font->y_scale < 0)
323 extents->y_bearing = -extents->y_bearing;
324 extents->height = -extents->height;
330 hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
332 hb_codepoint_t glyph,
333 unsigned int point_index,
336 void *user_data HB_UNUSED)
338 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
339 FT_Face ft_face = ft_font->ft_face;
341 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
344 if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
347 if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
350 *x = ft_face->glyph->outline.points[point_index].x;
351 *y = ft_face->glyph->outline.points[point_index].y;
357 hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED,
359 hb_codepoint_t glyph,
360 char *name, unsigned int size,
361 void *user_data HB_UNUSED)
363 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
365 hb_bool_t ret = !FT_Get_Glyph_Name (ft_font->ft_face, glyph, name, size);
366 if (ret && (size && !*name))
373 hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
375 const char *name, int len, /* -1 means nul-terminated */
376 hb_codepoint_t *glyph,
377 void *user_data HB_UNUSED)
379 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
380 FT_Face ft_face = ft_font->ft_face;
383 *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
385 /* Make a nul-terminated version. */
387 len = MIN (len, (int) sizeof (buf) - 1);
388 strncpy (buf, name, len);
390 *glyph = FT_Get_Name_Index (ft_face, buf);
395 /* Check whether the given name was actually the name of glyph 0. */
397 if (!FT_Get_Glyph_Name(ft_face, 0, buf, sizeof (buf)) &&
398 len < 0 ? !strcmp (buf, name) : !strncmp (buf, name, len))
406 hb_ft_get_font_h_extents (hb_font_t *font HB_UNUSED,
408 hb_font_extents_t *metrics,
409 void *user_data HB_UNUSED)
411 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
412 FT_Face ft_face = ft_font->ft_face;
413 metrics->ascender = ft_face->size->metrics.ascender;
414 metrics->descender = ft_face->size->metrics.descender;
415 metrics->line_gap = ft_face->size->metrics.height - (ft_face->size->metrics.ascender - ft_face->size->metrics.descender);
416 if (font->y_scale < 0)
418 metrics->ascender = -metrics->ascender;
419 metrics->descender = -metrics->descender;
420 metrics->line_gap = -metrics->line_gap;
425 static hb_font_funcs_t *static_ft_funcs = NULL;
429 void free_static_ft_funcs (void)
431 hb_font_funcs_destroy (static_ft_funcs);
436 _hb_ft_font_set_funcs (hb_font_t *font, FT_Face ft_face, bool unref)
439 hb_font_funcs_t *funcs = (hb_font_funcs_t *) hb_atomic_ptr_get (&static_ft_funcs);
441 if (unlikely (!funcs))
443 funcs = hb_font_funcs_create ();
445 hb_font_funcs_set_font_h_extents_func (funcs, hb_ft_get_font_h_extents, NULL, NULL);
446 //hb_font_funcs_set_font_v_extents_func (funcs, hb_ft_get_font_v_extents, NULL, NULL);
447 hb_font_funcs_set_nominal_glyph_func (funcs, hb_ft_get_nominal_glyph, NULL, NULL);
448 hb_font_funcs_set_variation_glyph_func (funcs, hb_ft_get_variation_glyph, NULL, NULL);
449 hb_font_funcs_set_glyph_h_advance_func (funcs, hb_ft_get_glyph_h_advance, NULL, NULL);
450 hb_font_funcs_set_glyph_v_advance_func (funcs, hb_ft_get_glyph_v_advance, NULL, NULL);
451 //hb_font_funcs_set_glyph_h_origin_func (funcs, hb_ft_get_glyph_h_origin, NULL, NULL);
452 hb_font_funcs_set_glyph_v_origin_func (funcs, hb_ft_get_glyph_v_origin, NULL, NULL);
453 hb_font_funcs_set_glyph_h_kerning_func (funcs, hb_ft_get_glyph_h_kerning, NULL, NULL);
454 //hb_font_funcs_set_glyph_v_kerning_func (funcs, hb_ft_get_glyph_v_kerning, NULL, NULL);
455 hb_font_funcs_set_glyph_extents_func (funcs, hb_ft_get_glyph_extents, NULL, NULL);
456 hb_font_funcs_set_glyph_contour_point_func (funcs, hb_ft_get_glyph_contour_point, NULL, NULL);
457 hb_font_funcs_set_glyph_name_func (funcs, hb_ft_get_glyph_name, NULL, NULL);
458 hb_font_funcs_set_glyph_from_name_func (funcs, hb_ft_get_glyph_from_name, NULL, NULL);
460 hb_font_funcs_make_immutable (funcs);
462 if (!hb_atomic_ptr_cmpexch (&static_ft_funcs, NULL, funcs)) {
463 hb_font_funcs_destroy (funcs);
468 atexit (free_static_ft_funcs); /* First person registers atexit() callback. */
472 bool symbol = ft_face->charmap && ft_face->charmap->encoding == FT_ENCODING_MS_SYMBOL;
474 hb_font_set_funcs (font,
476 _hb_ft_font_create (ft_face, symbol, unref),
477 (hb_destroy_func_t) _hb_ft_font_destroy);
482 reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
484 FT_Face ft_face = (FT_Face) user_data;
489 /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob */
491 error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
495 buffer = (FT_Byte *) malloc (length);
499 error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
503 return hb_blob_create ((const char *) buffer, length,
504 HB_MEMORY_MODE_WRITABLE,
510 * @ft_face: (destroy destroy) (scope notified):
515 * Return value: (transfer full):
519 hb_ft_face_create (FT_Face ft_face,
520 hb_destroy_func_t destroy)
524 if (ft_face->stream->read == NULL) {
527 blob = hb_blob_create ((const char *) ft_face->stream->base,
528 (unsigned int) ft_face->stream->size,
529 HB_MEMORY_MODE_READONLY,
531 face = hb_face_create (blob, ft_face->face_index);
532 hb_blob_destroy (blob);
534 face = hb_face_create_for_tables (reference_table, ft_face, destroy);
537 hb_face_set_index (face, ft_face->face_index);
538 hb_face_set_upem (face, ft_face->units_per_EM);
544 * hb_ft_face_create_referenced:
549 * Return value: (transfer full):
553 hb_ft_face_create_referenced (FT_Face ft_face)
555 FT_Reference_Face (ft_face);
556 return hb_ft_face_create (ft_face, (hb_destroy_func_t) _hb_ft_face_destroy);
560 hb_ft_face_finalize (FT_Face ft_face)
562 hb_face_destroy ((hb_face_t *) ft_face->generic.data);
566 * hb_ft_face_create_cached:
571 * Return value: (transfer full):
575 hb_ft_face_create_cached (FT_Face ft_face)
577 if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
579 if (ft_face->generic.finalizer)
580 ft_face->generic.finalizer (ft_face);
582 ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
583 ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
586 return hb_face_reference ((hb_face_t *) ft_face->generic.data);
592 * @ft_face: (destroy destroy) (scope notified):
597 * Return value: (transfer full):
601 hb_ft_font_create (FT_Face ft_face,
602 hb_destroy_func_t destroy)
607 face = hb_ft_face_create (ft_face, destroy);
608 font = hb_font_create (face);
609 hb_face_destroy (face);
610 _hb_ft_font_set_funcs (font, ft_face, false);
611 hb_font_set_scale (font,
612 (int) (((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM + (1u<<15)) >> 16),
613 (int) (((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM + (1u<<15)) >> 16));
614 #if 0 /* hb-ft works in no-hinting model */
615 hb_font_set_ppem (font,
616 ft_face->size->metrics.x_ppem,
617 ft_face->size->metrics.y_ppem);
620 #ifdef HAVE_FT_GET_VAR_BLEND_COORDINATES
621 FT_MM_Var *mm_var = NULL;
622 if (!FT_Get_MM_Var (ft_face, &mm_var))
624 FT_Fixed *ft_coords = (FT_Fixed *) calloc (mm_var->num_axis, sizeof (FT_Fixed));
625 int *coords = (int *) calloc (mm_var->num_axis, sizeof (int));
626 if (coords && ft_coords)
628 if (!FT_Get_Var_Blend_Coordinates (ft_face, mm_var->num_axis, ft_coords))
630 for (unsigned int i = 0; i < mm_var->num_axis; ++i)
631 coords[i] = ft_coords[i] >>= 2;
633 hb_font_set_var_coords_normalized (font, coords, mm_var->num_axis);
646 * hb_ft_font_create_referenced:
651 * Return value: (transfer full):
655 hb_ft_font_create_referenced (FT_Face ft_face)
657 FT_Reference_Face (ft_face);
658 return hb_ft_font_create (ft_face, (hb_destroy_func_t) _hb_ft_face_destroy);
662 /* Thread-safe, lock-free, FT_Library */
664 static FT_Library ft_library;
668 void free_ft_library (void)
670 FT_Done_FreeType (ft_library);
675 get_ft_library (void)
678 FT_Library library = (FT_Library) hb_atomic_ptr_get (&ft_library);
680 if (unlikely (!library))
682 /* Not found; allocate one. */
683 if (FT_Init_FreeType (&library))
686 if (!hb_atomic_ptr_cmpexch (&ft_library, NULL, library)) {
687 FT_Done_FreeType (library);
692 atexit (free_ft_library); /* First person registers atexit() callback. */
700 _release_blob (FT_Face ft_face)
702 hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
706 hb_ft_font_set_funcs (hb_font_t *font)
708 hb_blob_t *blob = hb_face_reference_blob (font->face);
709 unsigned int blob_length;
710 const char *blob_data = hb_blob_get_data (blob, &blob_length);
711 if (unlikely (!blob_length))
712 DEBUG_MSG (FT, font, "Font face has empty blob");
714 FT_Face ft_face = NULL;
715 FT_Error err = FT_New_Memory_Face (get_ft_library (),
716 (const FT_Byte *) blob_data,
718 hb_face_get_index (font->face),
721 if (unlikely (err)) {
722 hb_blob_destroy (blob);
723 DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed");
727 if (FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE))
728 FT_Select_Charmap (ft_face, FT_ENCODING_MS_SYMBOL);
730 FT_Set_Char_Size (ft_face,
731 abs (font->x_scale), abs (font->y_scale),
734 font->x_ppem * 72 * 64 / font->x_scale,
735 font->y_ppem * 72 * 64 / font->y_scale);
737 if (font->x_scale < 0 || font->y_scale < 0)
739 FT_Matrix matrix = { font->x_scale < 0 ? -1 : +1, 0,
740 0, font->y_scale < 0 ? -1 : +1};
741 FT_Set_Transform (ft_face, &matrix, NULL);
744 unsigned int num_coords;
745 const int *coords = hb_font_get_var_coords_normalized (font, &num_coords);
748 FT_Fixed *ft_coords = (FT_Fixed *) calloc (num_coords, sizeof (FT_Fixed));
751 for (unsigned int i = 0; i < num_coords; i++)
752 ft_coords[i] = coords[i] << 2;
753 FT_Set_Var_Blend_Coordinates (ft_face, num_coords, ft_coords);
758 ft_face->generic.data = blob;
759 ft_face->generic.finalizer = (FT_Generic_Finalizer) _release_blob;
761 _hb_ft_font_set_funcs (font, ft_face, true);
762 hb_ft_font_set_load_flags (font, FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING);