2 * Copyright © 2022 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
27 #ifndef HELPER_CAIRO_FT_HH
28 #define HELPER_CAIRO_FT_HH
30 #include "font-options.hh"
34 #include FT_MULTIPLE_MASTERS_H
36 static FT_Library ft_library;
40 void free_ft_library ()
42 FT_Done_FreeType (ft_library);
47 _release_blob (void *arg)
49 FT_Face ft_face = (FT_Face) arg;
50 hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
53 static inline cairo_font_face_t *
54 helper_cairo_create_ft_font_face (const font_options_t *font_opts)
56 cairo_font_face_t *cairo_face;
57 /* We cannot use the FT_Face from hb_font_t, as doing so will confuse hb_font_t because
58 * cairo will reset the face size. As such, create new face...
59 * TODO Perhaps add API to hb-ft to encapsulate this code. */
60 FT_Face ft_face = nullptr;//hb_ft_font_get_face (font);
65 FT_Init_FreeType (&ft_library);
67 atexit (free_ft_library);
71 unsigned int blob_length;
72 const char *blob_data = hb_blob_get_data (font_opts->blob, &blob_length);
74 if (FT_New_Memory_Face (ft_library,
75 (const FT_Byte *) blob_data,
77 font_opts->face_index,
79 fail (false, "FT_New_Memory_Face fail");
83 /* This allows us to get some boxes at least... */
84 cairo_face = cairo_toy_font_face_create ("@cairo:sans",
85 CAIRO_FONT_SLANT_NORMAL,
86 CAIRO_FONT_WEIGHT_NORMAL);
90 ft_face->generic.data = hb_blob_reference (font_opts->blob);
91 ft_face->generic.finalizer = _release_blob;
93 #if !defined(HB_NO_VAR) && defined(HAVE_FT_SET_VAR_BLEND_COORDINATES)
94 unsigned int num_coords;
95 const float *coords = hb_font_get_var_coords_design (font_opts->font, &num_coords);
98 FT_Fixed *ft_coords = (FT_Fixed *) calloc (num_coords, sizeof (FT_Fixed));
101 for (unsigned int i = 0; i < num_coords; i++)
102 ft_coords[i] = coords[i] * 65536.f;
103 FT_Set_Var_Design_Coordinates (ft_face, num_coords, ft_coords);
109 cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, font_opts->ft_load_flags);
115 helper_cairo_ft_scaled_font_has_color (cairo_scaled_font_t *scaled_font)
119 FT_Face ft_face = cairo_ft_scaled_font_lock_face (scaled_font);
122 if (FT_HAS_COLOR (ft_face))
124 cairo_ft_scaled_font_unlock_face (scaled_font);