blob: fc4b11220a94ca2010468b0ac61650901edf02e3 [file] [log] [blame]
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -05001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 2009 Red Hat, Inc.
3 * Copyright © 2009 Keith Stribley
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -04004 * Copyright © 2015 Google, Inc.
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -05005 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04006 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -05007 *
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.
13 *
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
18 * DAMAGE.
19 *
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.
25 *
26 * Red Hat Author(s): Behdad Esfahbod
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -040027 * Google Author(s): Behdad Esfahbod
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -050028 */
29
Behdad Esfahbodc57d4542011-04-20 18:50:27 -040030#include "hb-private.hh"
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -040031#include "hb-debug.hh"
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -050032
33#include "hb-ft.h"
34
Behdad Esfahbodc57d4542011-04-20 18:50:27 -040035#include "hb-font-private.hh"
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -050036
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +020037#include FT_ADVANCES_H
Sascha Brawer72873cf2016-09-13 18:40:07 +020038#include FT_MULTIPLE_MASTERS_H
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -050039#include FT_TRUETYPE_TABLES_H
40
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040041
Behdad Esfahbod744970a2011-05-16 18:15:37 -040042/* TODO:
43 *
44 * In general, this file does a fine job of what it's supposed to do.
45 * There are, however, things that need more work:
46 *
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -040047 * - I remember seeing FT_Get_Advance() without the NO_HINTING flag to be buggy.
48 * Have not investigated.
Behdad Esfahbod744970a2011-05-16 18:15:37 -040049 *
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -040050 * - FreeType works in 26.6 mode. Clients can decide to use that mode, and everything
Behdad Esfahbodf34aaba2014-12-28 18:56:15 -080051 * would work fine. However, we also abuse this API for performing in font-space,
52 * but don't pass the correct flags to FreeType. We just abuse the no-hinting mode
53 * for that, such that no rounding etc happens. As such, we don't set ppem, and
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -040054 * pass NO_HINTING as load_flags. Would be much better to use NO_SCALE, and scale
55 * ourselves, like we do in uniscribe, etc.
Behdad Esfahbodf34aaba2014-12-28 18:56:15 -080056 *
Behdad Esfahbod744970a2011-05-16 18:15:37 -040057 * - We don't handle / allow for emboldening / obliqueing.
58 *
Behdad Esfahbod190e19e2013-03-09 20:30:22 -050059 * - In the future, we should add constructors to create fonts in font space?
Behdad Esfahbod744970a2011-05-16 18:15:37 -040060 *
Bruce Mitchener90218fa2018-01-31 20:44:45 +070061 * - FT_Load_Glyph() is extremely costly. Do something about it?
Behdad Esfahbod744970a2011-05-16 18:15:37 -040062 */
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -050063
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040064
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -040065struct hb_ft_font_t
66{
67 FT_Face ft_face;
68 int load_flags;
Behdad Esfahbod34f9aa52016-07-20 02:35:54 -070069 bool symbol; /* Whether selected cmap is symbol cmap. */
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -040070 bool unref; /* Whether to destroy ft_face when done. */
71};
72
73static hb_ft_font_t *
Behdad Esfahbod34f9aa52016-07-20 02:35:54 -070074_hb_ft_font_create (FT_Face ft_face, bool symbol, bool unref)
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -040075{
76 hb_ft_font_t *ft_font = (hb_ft_font_t *) calloc (1, sizeof (hb_ft_font_t));
77
78 if (unlikely (!ft_font))
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +020079 return nullptr;
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -040080
81 ft_font->ft_face = ft_face;
Behdad Esfahbod34f9aa52016-07-20 02:35:54 -070082 ft_font->symbol = symbol;
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -040083 ft_font->unref = unref;
84
Behdad Esfahbodca97ea72015-10-15 20:20:22 -030085 ft_font->load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -040086
87 return ft_font;
88}
89
90static void
Behdad Esfahbode1b6d922017-10-11 15:51:31 +020091_hb_ft_face_destroy (void *data)
Birunthan Mohanathas6bf9db42016-07-11 13:38:23 -070092{
Behdad Esfahbode1b6d922017-10-11 15:51:31 +020093 FT_Done_Face ((FT_Face) data);
Birunthan Mohanathas6bf9db42016-07-11 13:38:23 -070094}
95
96static void
Behdad Esfahbode1b6d922017-10-11 15:51:31 +020097_hb_ft_font_destroy (void *data)
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -040098{
Behdad Esfahbode1b6d922017-10-11 15:51:31 +020099 hb_ft_font_t *ft_font = (hb_ft_font_t *) data;
100
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400101 if (ft_font->unref)
Birunthan Mohanathas6bf9db42016-07-11 13:38:23 -0700102 _hb_ft_face_destroy (ft_font->ft_face);
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400103
104 free (ft_font);
105}
106
Behdad Esfahbodedeb3da2015-10-08 12:47:15 -0400107/**
108 * hb_ft_font_set_load_flags:
109 * @font:
110 * @load_flags:
111 *
112 *
113 *
114 * Since: 1.0.5
115 **/
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400116void
117hb_ft_font_set_load_flags (hb_font_t *font, int load_flags)
118{
119 if (font->immutable)
120 return;
121
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200122 if (font->destroy != _hb_ft_font_destroy)
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400123 return;
124
125 hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
126
127 ft_font->load_flags = load_flags;
128}
129
Behdad Esfahbodedeb3da2015-10-08 12:47:15 -0400130/**
131 * hb_ft_font_get_load_flags:
132 * @font:
133 *
134 *
135 *
136 * Return value:
137 * Since: 1.0.5
138 **/
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400139int
140hb_ft_font_get_load_flags (hb_font_t *font)
141{
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200142 if (font->destroy != _hb_ft_font_destroy)
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400143 return 0;
144
145 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
146
147 return ft_font->load_flags;
148}
149
150FT_Face
151hb_ft_font_get_face (hb_font_t *font)
152{
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200153 if (font->destroy != _hb_ft_font_destroy)
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200154 return nullptr;
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400155
156 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
157
158 return ft_font->ft_face;
159}
160
161
162
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -0400163static hb_bool_t
Behdad Esfahbod8b5bc142016-02-24 19:05:23 +0900164hb_ft_get_nominal_glyph (hb_font_t *font HB_UNUSED,
165 void *font_data,
166 hb_codepoint_t unicode,
167 hb_codepoint_t *glyph,
168 void *user_data HB_UNUSED)
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -0400169{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400170 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
Behdad Esfahbod8b5bc142016-02-24 19:05:23 +0900171 unsigned int g = FT_Get_Char_Index (ft_font->ft_face, unicode);
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -0400172
Behdad Esfahbod8b5bc142016-02-24 19:05:23 +0900173 if (unlikely (!g))
Behdad Esfahbod34f9aa52016-07-20 02:35:54 -0700174 {
175 if (unlikely (ft_font->symbol) && unicode <= 0x00FFu)
176 {
177 /* For symbol-encoded OpenType fonts, we duplicate the
178 * U+F000..F0FF range at U+0000..U+00FF. That's what
179 * Windows seems to do, and that's hinted about at:
180 * http://www.microsoft.com/typography/otspec/recom.htm
181 * under "Non-Standard (Symbol) Fonts". */
182 g = FT_Get_Char_Index (ft_font->ft_face, 0xF000u + unicode);
183 if (!g)
184 return false;
185 }
186 else
187 return false;
188 }
Behdad Esfahbod8b5bc142016-02-24 19:05:23 +0900189
190 *glyph = g;
191 return true;
192}
193
194static hb_bool_t
195hb_ft_get_variation_glyph (hb_font_t *font HB_UNUSED,
196 void *font_data,
197 hb_codepoint_t unicode,
198 hb_codepoint_t variation_selector,
199 hb_codepoint_t *glyph,
200 void *user_data HB_UNUSED)
201{
202 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
203 unsigned int g = FT_Face_GetCharVariantIndex (ft_font->ft_face, unicode, variation_selector);
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -0400204
Behdad Esfahbod9df099b2015-05-18 18:37:06 -0700205 if (unlikely (!g))
206 return false;
207
208 *glyph = g;
209 return true;
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -0400210}
211
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400212static hb_position_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400213hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
214 void *font_data,
215 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400216 void *user_data HB_UNUSED)
217{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400218 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200219 FT_Fixed v;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400220
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400221 if (unlikely (FT_Get_Advance (ft_font->ft_face, glyph, ft_font->load_flags, &v)))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400222 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400223
Behdad Esfahboda319d072015-01-23 12:44:24 -0800224 if (font->x_scale < 0)
225 v = -v;
226
Behdad Esfahbod755b44c2013-10-18 11:17:42 +0200227 return (v + (1<<9)) >> 10;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400228}
229
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400230static hb_position_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400231hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
232 void *font_data,
233 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400234 void *user_data HB_UNUSED)
235{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400236 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200237 FT_Fixed v;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400238
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400239 if (unlikely (FT_Get_Advance (ft_font->ft_face, glyph, ft_font->load_flags | FT_LOAD_VERTICAL_LAYOUT, &v)))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400240 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400241
Behdad Esfahbod7888a6b2015-01-28 12:40:40 -0800242 if (font->y_scale < 0)
243 v = -v;
244
Behdad Esfahbod8b38fae2011-05-19 13:08:00 -0400245 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
246 * have a Y growing upward. Hence the extra negation. */
Behdad Esfahbod755b44c2013-10-18 11:17:42 +0200247 return (-v + (1<<9)) >> 10;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400248}
249
250static hb_bool_t
251hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
252 void *font_data,
253 hb_codepoint_t glyph,
Behdad Esfahbod19098182011-05-17 23:27:22 -0400254 hb_position_t *x,
255 hb_position_t *y,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400256 void *user_data HB_UNUSED)
257{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400258 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
259 FT_Face ft_face = ft_font->ft_face;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400260
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400261 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400262 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400263
Behdad Esfahbod8b38fae2011-05-19 13:08:00 -0400264 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
265 * have a Y growing upward. Hence the extra negation. */
266 *x = ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBearingX;
267 *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);
268
Behdad Esfahbod7888a6b2015-01-28 12:40:40 -0800269 if (font->x_scale < 0)
270 *x = -*x;
271 if (font->y_scale < 0)
272 *y = -*y;
273
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400274 return true;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400275}
276
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400277static hb_position_t
Behdad Esfahbodcdf74442012-07-11 18:52:39 -0400278hb_ft_get_glyph_h_kerning (hb_font_t *font,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400279 void *font_data,
280 hb_codepoint_t left_glyph,
281 hb_codepoint_t right_glyph,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400282 void *user_data HB_UNUSED)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500283{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400284 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
Behdad Esfahbod60fbb362011-05-19 18:46:15 -0400285 FT_Vector kerningv;
Behdad Esfahbod3b593062009-11-04 15:48:32 -0500286
Behdad Esfahbodcdf74442012-07-11 18:52:39 -0400287 FT_Kerning_Mode mode = font->x_ppem ? FT_KERNING_DEFAULT : FT_KERNING_UNFITTED;
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400288 if (FT_Get_Kerning (ft_font->ft_face, left_glyph, right_glyph, mode, &kerningv))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400289 return 0;
Behdad Esfahbod3b593062009-11-04 15:48:32 -0500290
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400291 return kerningv.x;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400292}
293
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400294static hb_bool_t
295hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
296 void *font_data,
297 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400298 hb_glyph_extents_t *extents,
299 void *user_data HB_UNUSED)
300{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400301 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
302 FT_Face ft_face = ft_font->ft_face;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400303
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400304 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400305 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400306
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400307 extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
308 extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
309 extents->width = ft_face->glyph->metrics.width;
Behdad Esfahbod21756932012-08-08 01:20:45 -0400310 extents->height = -ft_face->glyph->metrics.height;
Behdad Esfahbodc743ec52015-11-05 17:33:57 -0800311 if (font->x_scale < 0)
312 {
313 extents->x_bearing = -extents->x_bearing;
314 extents->width = -extents->width;
315 }
316 if (font->y_scale < 0)
317 {
318 extents->y_bearing = -extents->y_bearing;
319 extents->height = -extents->height;
320 }
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400321 return true;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400322}
323
324static hb_bool_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400325hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
326 void *font_data,
327 hb_codepoint_t glyph,
328 unsigned int point_index,
329 hb_position_t *x,
330 hb_position_t *y,
331 void *user_data HB_UNUSED)
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400332{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400333 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
334 FT_Face ft_face = ft_font->ft_face;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400335
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400336 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400337 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400338
339 if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400340 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400341
342 if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400343 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400344
345 *x = ft_face->glyph->outline.points[point_index].x;
346 *y = ft_face->glyph->outline.points[point_index].y;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400347
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400348 return true;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500349}
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500350
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400351static hb_bool_t
Behdad Esfahbod271c8f82012-07-13 09:32:30 -0400352hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED,
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400353 void *font_data,
354 hb_codepoint_t glyph,
355 char *name, unsigned int size,
356 void *user_data HB_UNUSED)
357{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400358 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400359
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400360 hb_bool_t ret = !FT_Get_Glyph_Name (ft_font->ft_face, glyph, name, size);
Behdad Esfahbod5594c2d2013-03-06 19:37:31 -0500361 if (ret && (size && !*name))
362 ret = false;
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400363
364 return ret;
365}
366
367static hb_bool_t
Behdad Esfahbod271c8f82012-07-13 09:32:30 -0400368hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400369 void *font_data,
370 const char *name, int len, /* -1 means nul-terminated */
371 hb_codepoint_t *glyph,
372 void *user_data HB_UNUSED)
373{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400374 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
375 FT_Face ft_face = ft_font->ft_face;
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400376
377 if (len < 0)
378 *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
379 else {
380 /* Make a nul-terminated version. */
381 char buf[128];
382 len = MIN (len, (int) sizeof (buf) - 1);
383 strncpy (buf, name, len);
384 buf[len] = '\0';
385 *glyph = FT_Get_Name_Index (ft_face, buf);
386 }
387
Behdad Esfahbode509d352013-07-11 14:56:45 -0400388 if (*glyph == 0)
389 {
390 /* Check whether the given name was actually the name of glyph 0. */
391 char buf[128];
392 if (!FT_Get_Glyph_Name(ft_face, 0, buf, sizeof (buf)) &&
393 len < 0 ? !strcmp (buf, name) : !strncmp (buf, name, len))
394 return true;
395 }
396
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400397 return *glyph != 0;
398}
399
Simon Cozens6f2e6de2015-10-26 16:23:22 +0900400static hb_bool_t
401hb_ft_get_font_h_extents (hb_font_t *font HB_UNUSED,
402 void *font_data,
403 hb_font_extents_t *metrics,
404 void *user_data HB_UNUSED)
405{
406 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
407 FT_Face ft_face = ft_font->ft_face;
Behdad Esfahbod31fa3892015-12-10 16:38:29 +0100408 metrics->ascender = ft_face->size->metrics.ascender;
409 metrics->descender = ft_face->size->metrics.descender;
410 metrics->line_gap = ft_face->size->metrics.height - (ft_face->size->metrics.ascender - ft_face->size->metrics.descender);
Simon Cozens6f2e6de2015-10-26 16:23:22 +0900411 if (font->y_scale < 0)
412 {
413 metrics->ascender = -metrics->ascender;
414 metrics->descender = -metrics->descender;
415 metrics->line_gap = -metrics->line_gap;
416 }
417 return true;
418}
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400419
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200420static hb_font_funcs_t *static_ft_funcs = nullptr;
Behdad Esfahbod7918c262015-11-04 20:37:49 -0800421
422#ifdef HB_USE_ATEXIT
423static
424void free_static_ft_funcs (void)
425{
426 hb_font_funcs_destroy (static_ft_funcs);
427}
428#endif
429
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400430static void
431_hb_ft_font_set_funcs (hb_font_t *font, FT_Face ft_face, bool unref)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500432{
Behdad Esfahbod7918c262015-11-04 20:37:49 -0800433retry:
434 hb_font_funcs_t *funcs = (hb_font_funcs_t *) hb_atomic_ptr_get (&static_ft_funcs);
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400435
Behdad Esfahbod7918c262015-11-04 20:37:49 -0800436 if (unlikely (!funcs))
437 {
438 funcs = hb_font_funcs_create ();
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400439
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200440 hb_font_funcs_set_font_h_extents_func (funcs, hb_ft_get_font_h_extents, nullptr, nullptr);
441 //hb_font_funcs_set_font_v_extents_func (funcs, hb_ft_get_font_v_extents, nullptr, nullptr);
442 hb_font_funcs_set_nominal_glyph_func (funcs, hb_ft_get_nominal_glyph, nullptr, nullptr);
443 hb_font_funcs_set_variation_glyph_func (funcs, hb_ft_get_variation_glyph, nullptr, nullptr);
444 hb_font_funcs_set_glyph_h_advance_func (funcs, hb_ft_get_glyph_h_advance, nullptr, nullptr);
445 hb_font_funcs_set_glyph_v_advance_func (funcs, hb_ft_get_glyph_v_advance, nullptr, nullptr);
446 //hb_font_funcs_set_glyph_h_origin_func (funcs, hb_ft_get_glyph_h_origin, nullptr, nullptr);
447 hb_font_funcs_set_glyph_v_origin_func (funcs, hb_ft_get_glyph_v_origin, nullptr, nullptr);
448 hb_font_funcs_set_glyph_h_kerning_func (funcs, hb_ft_get_glyph_h_kerning, nullptr, nullptr);
449 //hb_font_funcs_set_glyph_v_kerning_func (funcs, hb_ft_get_glyph_v_kerning, nullptr, nullptr);
450 hb_font_funcs_set_glyph_extents_func (funcs, hb_ft_get_glyph_extents, nullptr, nullptr);
451 hb_font_funcs_set_glyph_contour_point_func (funcs, hb_ft_get_glyph_contour_point, nullptr, nullptr);
452 hb_font_funcs_set_glyph_name_func (funcs, hb_ft_get_glyph_name, nullptr, nullptr);
453 hb_font_funcs_set_glyph_from_name_func (funcs, hb_ft_get_glyph_from_name, nullptr, nullptr);
Behdad Esfahbod7918c262015-11-04 20:37:49 -0800454
Behdad Esfahbod44f82752015-11-04 20:40:05 -0800455 hb_font_funcs_make_immutable (funcs);
456
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200457 if (!hb_atomic_ptr_cmpexch (&static_ft_funcs, nullptr, funcs)) {
Behdad Esfahbod7918c262015-11-04 20:37:49 -0800458 hb_font_funcs_destroy (funcs);
459 goto retry;
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400460 }
Behdad Esfahbod7918c262015-11-04 20:37:49 -0800461
462#ifdef HB_USE_ATEXIT
463 atexit (free_static_ft_funcs); /* First person registers atexit() callback. */
464#endif
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400465 };
466
Behdad Esfahbod34f9aa52016-07-20 02:35:54 -0700467 bool symbol = ft_face->charmap && ft_face->charmap->encoding == FT_ENCODING_MS_SYMBOL;
468
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400469 hb_font_set_funcs (font,
Behdad Esfahbod7918c262015-11-04 20:37:49 -0800470 funcs,
Behdad Esfahbod34f9aa52016-07-20 02:35:54 -0700471 _hb_ft_font_create (ft_face, symbol, unref),
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200472 _hb_ft_font_destroy);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500473}
474
475
476static hb_blob_t *
Behdad Esfahbode7157842011-08-08 21:42:02 +0200477reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500478{
479 FT_Face ft_face = (FT_Face) user_data;
480 FT_Byte *buffer;
481 FT_ULong length = 0;
482 FT_Error error;
483
Behdad Esfahbod38973352011-08-08 23:37:41 +0200484 /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob */
Behdad Esfahbod2cd1ea42010-04-29 14:15:32 -0400485
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200486 error = FT_Load_Sfnt_Table (ft_face, tag, 0, nullptr, &length);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500487 if (error)
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200488 return nullptr;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500489
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400490 buffer = (FT_Byte *) malloc (length);
Behdad Esfahbod68af14d2017-08-09 17:09:21 -0700491 if (!buffer)
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200492 return nullptr;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500493
494 error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
495 if (error)
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200496 return nullptr;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500497
498 return hb_blob_create ((const char *) buffer, length,
499 HB_MEMORY_MODE_WRITABLE,
Behdad Esfahbod56681892011-04-20 03:03:32 -0400500 buffer, free);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500501}
502
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400503/**
504 * hb_ft_face_create:
505 * @ft_face: (destroy destroy) (scope notified):
506 * @destroy:
507 *
508 *
509 *
510 * Return value: (transfer full):
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430511 * Since: 0.9.2
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400512 **/
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500513hb_face_t *
514hb_ft_face_create (FT_Face ft_face,
515 hb_destroy_func_t destroy)
516{
517 hb_face_t *face;
518
Behdad Esfahbod68af14d2017-08-09 17:09:21 -0700519 if (!ft_face->stream->read) {
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500520 hb_blob_t *blob;
521
522 blob = hb_blob_create ((const char *) ft_face->stream->base,
523 (unsigned int) ft_face->stream->size,
Behdad Esfahbodaffacf22014-12-28 16:20:31 -0800524 HB_MEMORY_MODE_READONLY,
Behdad Esfahbod56681892011-04-20 03:03:32 -0400525 ft_face, destroy);
Behdad Esfahbod9a146882011-05-11 22:49:29 -0400526 face = hb_face_create (blob, ft_face->face_index);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500527 hb_blob_destroy (blob);
528 } else {
Behdad Esfahbode7157842011-08-08 21:42:02 +0200529 face = hb_face_create_for_tables (reference_table, ft_face, destroy);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500530 }
531
Behdad Esfahbodde1e1cf2011-08-09 00:19:38 +0200532 hb_face_set_index (face, ft_face->face_index);
533 hb_face_set_upem (face, ft_face->units_per_EM);
534
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500535 return face;
536}
537
Behdad Esfahbod350f3a02014-12-28 17:44:26 -0800538/**
539 * hb_ft_face_create_referenced:
540 * @ft_face:
541 *
542 *
543 *
544 * Return value: (transfer full):
Sascha Brawer01c3a882015-06-01 13:22:01 +0200545 * Since: 0.9.38
Behdad Esfahbod350f3a02014-12-28 17:44:26 -0800546 **/
547hb_face_t *
548hb_ft_face_create_referenced (FT_Face ft_face)
549{
550 FT_Reference_Face (ft_face);
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200551 return hb_ft_face_create (ft_face, _hb_ft_face_destroy);
Behdad Esfahbod350f3a02014-12-28 17:44:26 -0800552}
553
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500554static void
555hb_ft_face_finalize (FT_Face ft_face)
556{
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400557 hb_face_destroy ((hb_face_t *) ft_face->generic.data);
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500558}
559
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400560/**
561 * hb_ft_face_create_cached:
562 * @ft_face:
563 *
564 *
565 *
566 * Return value: (transfer full):
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430567 * Since: 0.9.2
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400568 **/
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500569hb_face_t *
570hb_ft_face_create_cached (FT_Face ft_face)
571{
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400572 if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500573 {
574 if (ft_face->generic.finalizer)
Behdad Esfahbodedb54e92009-11-06 15:19:22 -0500575 ft_face->generic.finalizer (ft_face);
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500576
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200577 ft_face->generic.data = hb_ft_face_create (ft_face, nullptr);
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500578 ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500579 }
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500580
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400581 return hb_face_reference ((hb_face_t *) ft_face->generic.data);
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500582}
583
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500584
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400585/**
586 * hb_ft_font_create:
587 * @ft_face: (destroy destroy) (scope notified):
588 * @destroy:
589 *
590 *
591 *
592 * Return value: (transfer full):
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430593 * Since: 0.9.2
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400594 **/
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500595hb_font_t *
596hb_ft_font_create (FT_Face ft_face,
597 hb_destroy_func_t destroy)
598{
599 hb_font_t *font;
Behdad Esfahbodd2928852011-05-03 01:03:53 -0400600 hb_face_t *face;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500601
Behdad Esfahbodd2928852011-05-03 01:03:53 -0400602 face = hb_ft_face_create (ft_face, destroy);
603 font = hb_font_create (face);
604 hb_face_destroy (face);
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400605 _hb_ft_font_set_funcs (font, ft_face, false);
Behdad Esfahbod4e478132017-10-12 10:33:16 +0200606 hb_ft_font_changed (font);
607 return font;
608}
609
610void
611hb_ft_font_changed (hb_font_t *font)
612{
613 if (font->destroy != _hb_ft_font_destroy)
614 return;
615
616 hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
617 FT_Face ft_face = ft_font->ft_face;
618
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500619 hb_font_set_scale (font,
Behdad Esfahbod33317312016-08-08 17:24:04 -0700620 (int) (((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM + (1u<<15)) >> 16),
621 (int) (((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM + (1u<<15)) >> 16));
Behdad Esfahbodf34aaba2014-12-28 18:56:15 -0800622#if 0 /* hb-ft works in no-hinting model */
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500623 hb_font_set_ppem (font,
624 ft_face->size->metrics.x_ppem,
625 ft_face->size->metrics.y_ppem);
Behdad Esfahbodf34aaba2014-12-28 18:56:15 -0800626#endif
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500627
Behdad Esfahbod42c81422016-09-13 23:49:46 +0200628#ifdef HAVE_FT_GET_VAR_BLEND_COORDINATES
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200629 FT_MM_Var *mm_var = nullptr;
Behdad Esfahbod42c81422016-09-13 23:49:46 +0200630 if (!FT_Get_MM_Var (ft_face, &mm_var))
631 {
Behdad Esfahbod825e4042017-01-22 16:41:45 -0800632 FT_Fixed *ft_coords = (FT_Fixed *) calloc (mm_var->num_axis, sizeof (FT_Fixed));
633 int *coords = (int *) calloc (mm_var->num_axis, sizeof (int));
634 if (coords && ft_coords)
Behdad Esfahbod42c81422016-09-13 23:49:46 +0200635 {
Behdad Esfahbod825e4042017-01-22 16:41:45 -0800636 if (!FT_Get_Var_Blend_Coordinates (ft_face, mm_var->num_axis, ft_coords))
637 {
Behdad Esfahbodcb43bdb2018-01-05 13:06:25 +0000638 bool nonzero = false;
Behdad Esfahbod42c81422016-09-13 23:49:46 +0200639
Behdad Esfahbodcb43bdb2018-01-05 13:06:25 +0000640 for (unsigned int i = 0; i < mm_var->num_axis; ++i)
641 {
642 coords[i] = ft_coords[i] >>= 2;
643 nonzero = nonzero || coords[i];
644 }
645
646 if (nonzero)
647 hb_font_set_var_coords_normalized (font, coords, mm_var->num_axis);
648 else
649 hb_font_set_var_coords_normalized (font, nullptr, 0);
Behdad Esfahbod825e4042017-01-22 16:41:45 -0800650 }
Sascha Brawer72873cf2016-09-13 18:40:07 +0200651 }
Behdad Esfahbodac8c4e52017-08-09 22:05:08 -0700652 free (coords);
653 free (ft_coords);
Behdad Esfahbod0473d952018-01-08 10:07:46 +0000654#ifdef HAVE_FT_DONE_MM_VAR
655 FT_Done_MM_Var (ft_face->glyph->library, mm_var);
656#else
Behdad Esfahbod825e4042017-01-22 16:41:45 -0800657 free (mm_var);
Behdad Esfahbod0473d952018-01-08 10:07:46 +0000658#endif
Sascha Brawer72873cf2016-09-13 18:40:07 +0200659 }
Behdad Esfahbod42c81422016-09-13 23:49:46 +0200660#endif
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500661}
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400662
Behdad Esfahbod350f3a02014-12-28 17:44:26 -0800663/**
664 * hb_ft_font_create_referenced:
665 * @ft_face:
666 *
667 *
668 *
669 * Return value: (transfer full):
Sascha Brawer01c3a882015-06-01 13:22:01 +0200670 * Since: 0.9.38
Behdad Esfahbod350f3a02014-12-28 17:44:26 -0800671 **/
672hb_font_t *
673hb_ft_font_create_referenced (FT_Face ft_face)
674{
675 FT_Reference_Face (ft_face);
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200676 return hb_ft_font_create (ft_face, _hb_ft_face_destroy);
Behdad Esfahbod350f3a02014-12-28 17:44:26 -0800677}
678
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400679
Behdad Esfahbodf64b2eb2012-06-05 19:23:29 -0400680/* Thread-safe, lock-free, FT_Library */
681
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400682static FT_Library ft_library;
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200683
Chris Petersonfb85d612015-01-04 19:31:10 -0800684#ifdef HB_USE_ATEXIT
685static
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400686void free_ft_library (void)
687{
688 FT_Done_FreeType (ft_library);
689}
Chris Petersonfb85d612015-01-04 19:31:10 -0800690#endif
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400691
692static FT_Library
693get_ft_library (void)
694{
695retry:
696 FT_Library library = (FT_Library) hb_atomic_ptr_get (&ft_library);
697
698 if (unlikely (!library))
699 {
700 /* Not found; allocate one. */
701 if (FT_Init_FreeType (&library))
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200702 return nullptr;
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400703
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200704 if (!hb_atomic_ptr_cmpexch (&ft_library, nullptr, library)) {
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400705 FT_Done_FreeType (library);
706 goto retry;
707 }
708
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400709#ifdef HB_USE_ATEXIT
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400710 atexit (free_ft_library); /* First person registers atexit() callback. */
711#endif
712 }
713
714 return library;
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200715}
716
717static void
718_release_blob (FT_Face ft_face)
719{
720 hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
721}
722
723void
724hb_ft_font_set_funcs (hb_font_t *font)
725{
726 hb_blob_t *blob = hb_face_reference_blob (font->face);
727 unsigned int blob_length;
728 const char *blob_data = hb_blob_get_data (blob, &blob_length);
729 if (unlikely (!blob_length))
730 DEBUG_MSG (FT, font, "Font face has empty blob");
731
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200732 FT_Face ft_face = nullptr;
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400733 FT_Error err = FT_New_Memory_Face (get_ft_library (),
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200734 (const FT_Byte *) blob_data,
735 blob_length,
736 hb_face_get_index (font->face),
737 &ft_face);
738
739 if (unlikely (err)) {
740 hb_blob_destroy (blob);
741 DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed");
742 return;
743 }
744
Behdad Esfahbod34f9aa52016-07-20 02:35:54 -0700745 if (FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE))
746 FT_Select_Charmap (ft_face, FT_ENCODING_MS_SYMBOL);
Behdad Esfahbod254142b2011-08-15 16:15:44 +0200747
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200748 FT_Set_Char_Size (ft_face,
Behdad Esfahboda319d072015-01-23 12:44:24 -0800749 abs (font->x_scale), abs (font->y_scale),
Behdad Esfahbod2023e2b2012-07-11 19:00:30 -0400750 0, 0);
751#if 0
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200752 font->x_ppem * 72 * 64 / font->x_scale,
753 font->y_ppem * 72 * 64 / font->y_scale);
Behdad Esfahbod2023e2b2012-07-11 19:00:30 -0400754#endif
Behdad Esfahboda319d072015-01-23 12:44:24 -0800755 if (font->x_scale < 0 || font->y_scale < 0)
756 {
757 FT_Matrix matrix = { font->x_scale < 0 ? -1 : +1, 0,
758 0, font->y_scale < 0 ? -1 : +1};
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200759 FT_Set_Transform (ft_face, &matrix, nullptr);
Behdad Esfahboda319d072015-01-23 12:44:24 -0800760 }
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200761
Olivier Blin0fd89dc2017-12-04 18:58:41 +0100762#ifdef HAVE_FT_SET_VAR_BLEND_COORDINATES
Behdad Esfahbod47ee34e2017-01-21 18:10:08 -0800763 unsigned int num_coords;
Behdad Esfahbod7647a052017-01-22 17:47:49 -0800764 const int *coords = hb_font_get_var_coords_normalized (font, &num_coords);
Behdad Esfahbod47ee34e2017-01-21 18:10:08 -0800765 if (num_coords)
766 {
Behdad Esfahbod825e4042017-01-22 16:41:45 -0800767 FT_Fixed *ft_coords = (FT_Fixed *) calloc (num_coords, sizeof (FT_Fixed));
768 if (ft_coords)
769 {
770 for (unsigned int i = 0; i < num_coords; i++)
771 ft_coords[i] = coords[i] << 2;
772 FT_Set_Var_Blend_Coordinates (ft_face, num_coords, ft_coords);
773 free (ft_coords);
774 }
Behdad Esfahbod47ee34e2017-01-21 18:10:08 -0800775 }
Olivier Blin0fd89dc2017-12-04 18:58:41 +0100776#endif
Behdad Esfahbod47ee34e2017-01-21 18:10:08 -0800777
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200778 ft_face->generic.data = blob;
779 ft_face->generic.finalizer = (FT_Generic_Finalizer) _release_blob;
780
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400781 _hb_ft_font_set_funcs (font, ft_face, true);
782 hb_ft_font_set_load_flags (font, FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING);
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200783}