• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 inline cairo_font_face_t *
helper_cairo_create_ft_font_face(const font_options_t * font_opts)47 helper_cairo_create_ft_font_face (const font_options_t *font_opts)
48 {
49   cairo_font_face_t *cairo_face;
50   /* We cannot use the FT_Face from hb_font_t, as doing so will confuse hb_font_t because
51    * cairo will reset the face size.  As such, create new face...
52    * TODO Perhaps add API to hb-ft to encapsulate this code. */
53   FT_Face ft_face = nullptr;//hb_ft_font_get_face (font);
54   if (!ft_face)
55   {
56     if (!ft_library)
57     {
58       FT_Init_FreeType (&ft_library);
59 #ifdef HAVE_ATEXIT
60       atexit (free_ft_library);
61 #endif
62     }
63 
64     unsigned int blob_length;
65     const char *blob_data = hb_blob_get_data (font_opts->blob, &blob_length);
66 
67     if (FT_New_Memory_Face (ft_library,
68 			    (const FT_Byte *) blob_data,
69 			    blob_length,
70 			    font_opts->face_index,
71 			    &ft_face))
72       fail (false, "FT_New_Memory_Face fail");
73   }
74   if (!ft_face)
75   {
76     /* This allows us to get some boxes at least... */
77     cairo_face = cairo_toy_font_face_create ("@cairo:sans",
78 					     CAIRO_FONT_SLANT_NORMAL,
79 					     CAIRO_FONT_WEIGHT_NORMAL);
80   }
81   else
82   {
83 #if !defined(HB_NO_VAR) && defined(HAVE_FT_SET_VAR_BLEND_COORDINATES)
84     unsigned int num_coords;
85     const float *coords = hb_font_get_var_coords_design (font_opts->font, &num_coords);
86     if (num_coords)
87     {
88       FT_Fixed *ft_coords = (FT_Fixed *) calloc (num_coords, sizeof (FT_Fixed));
89       if (ft_coords)
90       {
91 	for (unsigned int i = 0; i < num_coords; i++)
92 	  ft_coords[i] = coords[i] * 65536.f;
93 	FT_Set_Var_Design_Coordinates (ft_face, num_coords, ft_coords);
94 	free (ft_coords);
95       }
96     }
97 #endif
98 
99     cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, font_opts->ft_load_flags);
100   }
101   return cairo_face;
102 }
103 
104 static inline bool
helper_cairo_ft_scaled_font_has_color(cairo_scaled_font_t * scaled_font)105 helper_cairo_ft_scaled_font_has_color (cairo_scaled_font_t *scaled_font)
106 {
107   bool ret = false;
108 #ifdef FT_HAS_COLOR
109   FT_Face ft_face = cairo_ft_scaled_font_lock_face (scaled_font);
110   if (ft_face)
111   {
112     if (FT_HAS_COLOR (ft_face))
113       ret = true;
114     cairo_ft_scaled_font_unlock_face (scaled_font);
115   }
116 #endif
117   return ret;
118 }
119 
120 #endif
121