1 /*
2 * Copyright © 2022 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
25 */
26
27 #ifndef HELPER_CAIRO_FT_HH
28 #define HELPER_CAIRO_FT_HH
29
30 #include "font-options.hh"
31
32 #include <cairo-ft.h>
33 #include <hb-ft.h>
34 #include FT_MULTIPLE_MASTERS_H
35
36 static FT_Library ft_library;
37
38 #ifdef HAVE_ATEXIT
39 static inline
free_ft_library()40 void free_ft_library ()
41 {
42 FT_Done_FreeType (ft_library);
43 }
44 #endif
45
46 static void
_release_blob(void * arg)47 _release_blob (void *arg)
48 {
49 FT_Face ft_face = (FT_Face) arg;
50 hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
51 }
52
53 static inline cairo_font_face_t *
helper_cairo_create_ft_font_face(const font_options_t * font_opts)54 helper_cairo_create_ft_font_face (const font_options_t *font_opts)
55 {
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);
61 if (!ft_face)
62 {
63 if (!ft_library)
64 {
65 FT_Init_FreeType (&ft_library);
66 #ifdef HAVE_ATEXIT
67 atexit (free_ft_library);
68 #endif
69 }
70
71 unsigned int blob_length;
72 const char *blob_data = hb_blob_get_data (font_opts->blob, &blob_length);
73
74 if (FT_New_Memory_Face (ft_library,
75 (const FT_Byte *) blob_data,
76 blob_length,
77 font_opts->face_index,
78 &ft_face))
79 fail (false, "FT_New_Memory_Face fail");
80 }
81 if (!ft_face)
82 {
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);
87 }
88 else
89 {
90 ft_face->generic.data = hb_blob_reference (font_opts->blob);
91 ft_face->generic.finalizer = _release_blob;
92
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);
96 if (num_coords)
97 {
98 FT_Fixed *ft_coords = (FT_Fixed *) calloc (num_coords, sizeof (FT_Fixed));
99 if (ft_coords)
100 {
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);
104 free (ft_coords);
105 }
106 }
107 #endif
108
109 cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, font_opts->ft_load_flags);
110 }
111 return cairo_face;
112 }
113
114 static inline bool
helper_cairo_ft_scaled_font_has_color(cairo_scaled_font_t * scaled_font)115 helper_cairo_ft_scaled_font_has_color (cairo_scaled_font_t *scaled_font)
116 {
117 bool ret = false;
118 #ifdef FT_HAS_COLOR
119 FT_Face ft_face = cairo_ft_scaled_font_lock_face (scaled_font);
120 if (ft_face)
121 {
122 if (FT_HAS_COLOR (ft_face))
123 ret = true;
124 cairo_ft_scaled_font_unlock_face (scaled_font);
125 }
126 #endif
127 return ret;
128 }
129
130 #endif
131