xref: /aosp_15_r20/external/harfbuzz_ng/util/helper-cairo-ft.hh (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
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     if (FT_New_Face (ft_library,
65 		     font_opts->font_file,
66 		     font_opts->face_index,
67 		     &ft_face))
68       fail (false, "FT_New_Face fail");
69   }
70   if (!ft_face)
71   {
72     /* This allows us to get some boxes at least... */
73     cairo_face = cairo_toy_font_face_create ("@cairo:sans",
74 					     CAIRO_FONT_SLANT_NORMAL,
75 					     CAIRO_FONT_WEIGHT_NORMAL);
76   }
77   else
78   {
79 #if !defined(HB_NO_VAR) && defined(HAVE_FT_SET_VAR_BLEND_COORDINATES)
80     unsigned int num_coords;
81     const float *coords = hb_font_get_var_coords_design (font_opts->font, &num_coords);
82     if (num_coords)
83     {
84       FT_Fixed *ft_coords = (FT_Fixed *) calloc (num_coords, sizeof (FT_Fixed));
85       if (ft_coords)
86       {
87 	for (unsigned int i = 0; i < num_coords; i++)
88 	  ft_coords[i] = coords[i] * 65536.f;
89 	FT_Set_Var_Design_Coordinates (ft_face, num_coords, ft_coords);
90 	free (ft_coords);
91       }
92     }
93 #endif
94 
95     cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, font_opts->ft_load_flags);
96   }
97   return cairo_face;
98 }
99 
100 static inline bool
helper_cairo_ft_scaled_font_has_color(cairo_scaled_font_t * scaled_font)101 helper_cairo_ft_scaled_font_has_color (cairo_scaled_font_t *scaled_font)
102 {
103   bool ret = false;
104 #ifdef FT_HAS_COLOR
105   FT_Face ft_face = cairo_ft_scaled_font_lock_face (scaled_font);
106   if (ft_face)
107   {
108     if (FT_HAS_COLOR (ft_face))
109       ret = true;
110     cairo_ft_scaled_font_unlock_face (scaled_font);
111   }
112 #endif
113   return ret;
114 }
115 
116 #endif
117