blob: 682acb7eda185dd68e5631dee3b1e283c216196f [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 Esfahbod8fb3d1a2009-11-03 18:34:20 -050031
32#include "hb-ft.h"
33
Behdad Esfahbodc57d4542011-04-20 18:50:27 -040034#include "hb-font-private.hh"
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -050035
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +020036#include FT_ADVANCES_H
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -050037#include FT_TRUETYPE_TABLES_H
38
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040039
40
Behdad Esfahbod38b21182011-08-09 10:51:24 +020041#ifndef HB_DEBUG_FT
42#define HB_DEBUG_FT (HB_DEBUG+0)
43#endif
44
45
Behdad Esfahbod744970a2011-05-16 18:15:37 -040046/* TODO:
47 *
48 * In general, this file does a fine job of what it's supposed to do.
49 * There are, however, things that need more work:
50 *
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -040051 * - I remember seeing FT_Get_Advance() without the NO_HINTING flag to be buggy.
52 * Have not investigated.
Behdad Esfahbod744970a2011-05-16 18:15:37 -040053 *
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -040054 * - FreeType works in 26.6 mode. Clients can decide to use that mode, and everything
Behdad Esfahbodf34aaba2014-12-28 18:56:15 -080055 * would work fine. However, we also abuse this API for performing in font-space,
56 * but don't pass the correct flags to FreeType. We just abuse the no-hinting mode
57 * for that, such that no rounding etc happens. As such, we don't set ppem, and
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -040058 * pass NO_HINTING as load_flags. Would be much better to use NO_SCALE, and scale
59 * ourselves, like we do in uniscribe, etc.
Behdad Esfahbodf34aaba2014-12-28 18:56:15 -080060 *
Behdad Esfahbod744970a2011-05-16 18:15:37 -040061 * - We don't handle / allow for emboldening / obliqueing.
62 *
Behdad Esfahbod190e19e2013-03-09 20:30:22 -050063 * - In the future, we should add constructors to create fonts in font space?
Behdad Esfahbod744970a2011-05-16 18:15:37 -040064 *
Behdad Esfahbod323190c2012-04-12 12:29:10 -040065 * - FT_Load_Glyph() is exteremely costly. Do something about it?
Behdad Esfahbod744970a2011-05-16 18:15:37 -040066 */
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -050067
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040068
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -040069struct hb_ft_font_t
70{
71 FT_Face ft_face;
72 int load_flags;
73 bool unref; /* Whether to destroy ft_face when done. */
74};
75
76static hb_ft_font_t *
77_hb_ft_font_create (FT_Face ft_face, bool unref)
78{
79 hb_ft_font_t *ft_font = (hb_ft_font_t *) calloc (1, sizeof (hb_ft_font_t));
80
81 if (unlikely (!ft_font))
82 return NULL;
83
84 ft_font->ft_face = ft_face;
85 ft_font->unref = unref;
86
Behdad Esfahbodca97ea72015-10-15 20:20:22 -030087 ft_font->load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -040088
89 return ft_font;
90}
91
92static void
93_hb_ft_font_destroy (hb_ft_font_t *ft_font)
94{
95 if (ft_font->unref)
96 FT_Done_Face (ft_font->ft_face);
97
98 free (ft_font);
99}
100
Behdad Esfahbodedeb3da2015-10-08 12:47:15 -0400101/**
102 * hb_ft_font_set_load_flags:
103 * @font:
104 * @load_flags:
105 *
106 *
107 *
108 * Since: 1.0.5
109 **/
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400110void
111hb_ft_font_set_load_flags (hb_font_t *font, int load_flags)
112{
113 if (font->immutable)
114 return;
115
116 if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
117 return;
118
119 hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
120
121 ft_font->load_flags = load_flags;
122}
123
Behdad Esfahbodedeb3da2015-10-08 12:47:15 -0400124/**
125 * hb_ft_font_get_load_flags:
126 * @font:
127 *
128 *
129 *
130 * Return value:
131 * Since: 1.0.5
132 **/
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400133int
134hb_ft_font_get_load_flags (hb_font_t *font)
135{
136 if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
137 return 0;
138
139 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
140
141 return ft_font->load_flags;
142}
143
144FT_Face
145hb_ft_font_get_face (hb_font_t *font)
146{
147 if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
148 return NULL;
149
150 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
151
152 return ft_font->ft_face;
153}
154
155
156
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -0400157static hb_bool_t
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -0400158hb_ft_get_glyph (hb_font_t *font HB_UNUSED,
Behdad Esfahbodb4678272011-05-11 23:25:28 -0400159 void *font_data,
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -0400160 hb_codepoint_t unicode,
161 hb_codepoint_t variation_selector,
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -0400162 hb_codepoint_t *glyph,
Behdad Esfahbodb4678272011-05-11 23:25:28 -0400163 void *user_data HB_UNUSED)
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -0400164
165{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400166 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
Behdad Esfahbod9df099b2015-05-18 18:37:06 -0700167 unsigned int g;
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -0400168
Behdad Esfahbod9df099b2015-05-18 18:37:06 -0700169 if (likely (!variation_selector))
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400170 g = FT_Get_Char_Index (ft_font->ft_face, unicode);
Behdad Esfahbod9df099b2015-05-18 18:37:06 -0700171 else
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400172 g = FT_Face_GetCharVariantIndex (ft_font->ft_face, unicode, variation_selector);
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -0400173
Behdad Esfahbod9df099b2015-05-18 18:37:06 -0700174 if (unlikely (!g))
175 return false;
176
177 *glyph = g;
178 return true;
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -0400179}
180
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400181static hb_position_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400182hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
183 void *font_data,
184 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400185 void *user_data HB_UNUSED)
186{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400187 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200188 FT_Fixed v;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400189
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400190 if (unlikely (FT_Get_Advance (ft_font->ft_face, glyph, ft_font->load_flags, &v)))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400191 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400192
Behdad Esfahboda319d072015-01-23 12:44:24 -0800193 if (font->x_scale < 0)
194 v = -v;
195
Behdad Esfahbod755b44c2013-10-18 11:17:42 +0200196 return (v + (1<<9)) >> 10;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400197}
198
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400199static hb_position_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400200hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
201 void *font_data,
202 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400203 void *user_data HB_UNUSED)
204{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400205 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200206 FT_Fixed v;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400207
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400208 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 -0400209 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400210
Behdad Esfahbod7888a6b2015-01-28 12:40:40 -0800211 if (font->y_scale < 0)
212 v = -v;
213
Behdad Esfahbod8b38fae2011-05-19 13:08:00 -0400214 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
215 * have a Y growing upward. Hence the extra negation. */
Behdad Esfahbod755b44c2013-10-18 11:17:42 +0200216 return (-v + (1<<9)) >> 10;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400217}
218
219static hb_bool_t
220hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
221 void *font_data,
222 hb_codepoint_t glyph,
Behdad Esfahbod19098182011-05-17 23:27:22 -0400223 hb_position_t *x,
224 hb_position_t *y,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400225 void *user_data HB_UNUSED)
226{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400227 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
228 FT_Face ft_face = ft_font->ft_face;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400229
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400230 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400231 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400232
Behdad Esfahbod8b38fae2011-05-19 13:08:00 -0400233 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
234 * have a Y growing upward. Hence the extra negation. */
235 *x = ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBearingX;
236 *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);
237
Behdad Esfahbod7888a6b2015-01-28 12:40:40 -0800238 if (font->x_scale < 0)
239 *x = -*x;
240 if (font->y_scale < 0)
241 *y = -*y;
242
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400243 return true;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400244}
245
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400246static hb_position_t
Behdad Esfahbodcdf74442012-07-11 18:52:39 -0400247hb_ft_get_glyph_h_kerning (hb_font_t *font,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400248 void *font_data,
249 hb_codepoint_t left_glyph,
250 hb_codepoint_t right_glyph,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400251 void *user_data HB_UNUSED)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500252{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400253 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
Behdad Esfahbod60fbb362011-05-19 18:46:15 -0400254 FT_Vector kerningv;
Behdad Esfahbod3b593062009-11-04 15:48:32 -0500255
Behdad Esfahbodcdf74442012-07-11 18:52:39 -0400256 FT_Kerning_Mode mode = font->x_ppem ? FT_KERNING_DEFAULT : FT_KERNING_UNFITTED;
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400257 if (FT_Get_Kerning (ft_font->ft_face, left_glyph, right_glyph, mode, &kerningv))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400258 return 0;
Behdad Esfahbod3b593062009-11-04 15:48:32 -0500259
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400260 return kerningv.x;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400261}
262
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400263static hb_bool_t
264hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
265 void *font_data,
266 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400267 hb_glyph_extents_t *extents,
268 void *user_data HB_UNUSED)
269{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400270 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
271 FT_Face ft_face = ft_font->ft_face;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400272
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400273 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400274 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400275
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400276 extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
277 extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
278 extents->width = ft_face->glyph->metrics.width;
Behdad Esfahbod21756932012-08-08 01:20:45 -0400279 extents->height = -ft_face->glyph->metrics.height;
Behdad Esfahbodc743ec52015-11-05 17:33:57 -0800280 if (font->x_scale < 0)
281 {
282 extents->x_bearing = -extents->x_bearing;
283 extents->width = -extents->width;
284 }
285 if (font->y_scale < 0)
286 {
287 extents->y_bearing = -extents->y_bearing;
288 extents->height = -extents->height;
289 }
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400290 return true;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400291}
292
293static hb_bool_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400294hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
295 void *font_data,
296 hb_codepoint_t glyph,
297 unsigned int point_index,
298 hb_position_t *x,
299 hb_position_t *y,
300 void *user_data HB_UNUSED)
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400301{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400302 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
303 FT_Face ft_face = ft_font->ft_face;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400304
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400305 if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400306 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400307
308 if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400309 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400310
311 if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400312 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400313
314 *x = ft_face->glyph->outline.points[point_index].x;
315 *y = ft_face->glyph->outline.points[point_index].y;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400316
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400317 return true;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500318}
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500319
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400320static hb_bool_t
Behdad Esfahbod271c8f82012-07-13 09:32:30 -0400321hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED,
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400322 void *font_data,
323 hb_codepoint_t glyph,
324 char *name, unsigned int size,
325 void *user_data HB_UNUSED)
326{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400327 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400328
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400329 hb_bool_t ret = !FT_Get_Glyph_Name (ft_font->ft_face, glyph, name, size);
Behdad Esfahbod5594c2d2013-03-06 19:37:31 -0500330 if (ret && (size && !*name))
331 ret = false;
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400332
333 return ret;
334}
335
336static hb_bool_t
Behdad Esfahbod271c8f82012-07-13 09:32:30 -0400337hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400338 void *font_data,
339 const char *name, int len, /* -1 means nul-terminated */
340 hb_codepoint_t *glyph,
341 void *user_data HB_UNUSED)
342{
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400343 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
344 FT_Face ft_face = ft_font->ft_face;
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400345
346 if (len < 0)
347 *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
348 else {
349 /* Make a nul-terminated version. */
350 char buf[128];
351 len = MIN (len, (int) sizeof (buf) - 1);
352 strncpy (buf, name, len);
353 buf[len] = '\0';
354 *glyph = FT_Get_Name_Index (ft_face, buf);
355 }
356
Behdad Esfahbode509d352013-07-11 14:56:45 -0400357 if (*glyph == 0)
358 {
359 /* Check whether the given name was actually the name of glyph 0. */
360 char buf[128];
361 if (!FT_Get_Glyph_Name(ft_face, 0, buf, sizeof (buf)) &&
362 len < 0 ? !strcmp (buf, name) : !strncmp (buf, name, len))
363 return true;
364 }
365
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400366 return *glyph != 0;
367}
368
Simon Cozens6f2e6de2015-10-26 16:23:22 +0900369static hb_bool_t
370hb_ft_get_font_h_extents (hb_font_t *font HB_UNUSED,
371 void *font_data,
372 hb_font_extents_t *metrics,
373 void *user_data HB_UNUSED)
374{
375 const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
376 FT_Face ft_face = ft_font->ft_face;
377 metrics->ascender = ft_face->ascender;
378 metrics->descender = ft_face->descender;
379 metrics->line_gap = ft_face->height - (ft_face->ascender - ft_face->descender);
380 if (font->y_scale < 0)
381 {
382 metrics->ascender = -metrics->ascender;
383 metrics->descender = -metrics->descender;
384 metrics->line_gap = -metrics->line_gap;
385 }
386 return true;
387}
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400388
Behdad Esfahbod7918c262015-11-04 20:37:49 -0800389static hb_font_funcs_t *static_ft_funcs = NULL;
390
391#ifdef HB_USE_ATEXIT
392static
393void free_static_ft_funcs (void)
394{
395 hb_font_funcs_destroy (static_ft_funcs);
396}
397#endif
398
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400399static void
400_hb_ft_font_set_funcs (hb_font_t *font, FT_Face ft_face, bool unref)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500401{
Behdad Esfahbod7918c262015-11-04 20:37:49 -0800402retry:
403 hb_font_funcs_t *funcs = (hb_font_funcs_t *) hb_atomic_ptr_get (&static_ft_funcs);
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400404
Behdad Esfahbod7918c262015-11-04 20:37:49 -0800405 if (unlikely (!funcs))
406 {
407 funcs = hb_font_funcs_create ();
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400408
Simon Cozens6f2e6de2015-10-26 16:23:22 +0900409 hb_font_funcs_set_font_h_extents_func (funcs, hb_ft_get_font_h_extents, NULL, NULL);
410 //hb_font_funcs_set_font_v_extents_func (funcs, hb_ft_get_font_v_extents, NULL, NULL);
Behdad Esfahbod7918c262015-11-04 20:37:49 -0800411 hb_font_funcs_set_glyph_func (funcs, hb_ft_get_glyph, NULL, NULL);
412 hb_font_funcs_set_glyph_h_advance_func (funcs, hb_ft_get_glyph_h_advance, NULL, NULL);
413 hb_font_funcs_set_glyph_v_advance_func (funcs, hb_ft_get_glyph_v_advance, NULL, NULL);
Behdad Esfahbod44f82752015-11-04 20:40:05 -0800414 //hb_font_funcs_set_glyph_h_origin_func (funcs, hb_ft_get_glyph_h_origin, NULL, NULL);
Behdad Esfahbod7918c262015-11-04 20:37:49 -0800415 hb_font_funcs_set_glyph_v_origin_func (funcs, hb_ft_get_glyph_v_origin, NULL, NULL);
416 hb_font_funcs_set_glyph_h_kerning_func (funcs, hb_ft_get_glyph_h_kerning, NULL, NULL);
Behdad Esfahbod44f82752015-11-04 20:40:05 -0800417 //hb_font_funcs_set_glyph_v_kerning_func (funcs, hb_ft_get_glyph_v_kerning, NULL, NULL);
Behdad Esfahbod7918c262015-11-04 20:37:49 -0800418 hb_font_funcs_set_glyph_extents_func (funcs, hb_ft_get_glyph_extents, NULL, NULL);
419 hb_font_funcs_set_glyph_contour_point_func (funcs, hb_ft_get_glyph_contour_point, NULL, NULL);
420 hb_font_funcs_set_glyph_name_func (funcs, hb_ft_get_glyph_name, NULL, NULL);
421 hb_font_funcs_set_glyph_from_name_func (funcs, hb_ft_get_glyph_from_name, NULL, NULL);
422
Behdad Esfahbod44f82752015-11-04 20:40:05 -0800423 hb_font_funcs_make_immutable (funcs);
424
Behdad Esfahbod7918c262015-11-04 20:37:49 -0800425 if (!hb_atomic_ptr_cmpexch (&static_ft_funcs, NULL, funcs)) {
426 hb_font_funcs_destroy (funcs);
427 goto retry;
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400428 }
Behdad Esfahbod7918c262015-11-04 20:37:49 -0800429
430#ifdef HB_USE_ATEXIT
431 atexit (free_static_ft_funcs); /* First person registers atexit() callback. */
432#endif
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400433 };
434
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400435 hb_font_set_funcs (font,
Behdad Esfahbod7918c262015-11-04 20:37:49 -0800436 funcs,
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400437 _hb_ft_font_create (ft_face, unref),
438 (hb_destroy_func_t) _hb_ft_font_destroy);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500439}
440
441
442static hb_blob_t *
Behdad Esfahbode7157842011-08-08 21:42:02 +0200443reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500444{
445 FT_Face ft_face = (FT_Face) user_data;
446 FT_Byte *buffer;
447 FT_ULong length = 0;
448 FT_Error error;
449
Behdad Esfahbod38973352011-08-08 23:37:41 +0200450 /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob */
Behdad Esfahbod2cd1ea42010-04-29 14:15:32 -0400451
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500452 error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
453 if (error)
Behdad Esfahbod750a2292010-05-20 16:23:27 +0100454 return NULL;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500455
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400456 buffer = (FT_Byte *) malloc (length);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500457 if (buffer == NULL)
Behdad Esfahbodd6b3c832010-04-23 19:59:53 -0400458 return NULL;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500459
460 error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
461 if (error)
Behdad Esfahbod750a2292010-05-20 16:23:27 +0100462 return NULL;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500463
464 return hb_blob_create ((const char *) buffer, length,
465 HB_MEMORY_MODE_WRITABLE,
Behdad Esfahbod56681892011-04-20 03:03:32 -0400466 buffer, free);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500467}
468
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400469/**
470 * hb_ft_face_create:
471 * @ft_face: (destroy destroy) (scope notified):
472 * @destroy:
473 *
474 *
475 *
476 * Return value: (transfer full):
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430477 * Since: 0.9.2
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400478 **/
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500479hb_face_t *
480hb_ft_face_create (FT_Face ft_face,
481 hb_destroy_func_t destroy)
482{
483 hb_face_t *face;
484
Behdad Esfahbode48ed722010-03-01 22:33:45 -0500485 if (ft_face->stream->read == NULL) {
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500486 hb_blob_t *blob;
487
488 blob = hb_blob_create ((const char *) ft_face->stream->base,
489 (unsigned int) ft_face->stream->size,
Behdad Esfahbodaffacf22014-12-28 16:20:31 -0800490 HB_MEMORY_MODE_READONLY,
Behdad Esfahbod56681892011-04-20 03:03:32 -0400491 ft_face, destroy);
Behdad Esfahbod9a146882011-05-11 22:49:29 -0400492 face = hb_face_create (blob, ft_face->face_index);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500493 hb_blob_destroy (blob);
494 } else {
Behdad Esfahbode7157842011-08-08 21:42:02 +0200495 face = hb_face_create_for_tables (reference_table, ft_face, destroy);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500496 }
497
Behdad Esfahbodde1e1cf2011-08-09 00:19:38 +0200498 hb_face_set_index (face, ft_face->face_index);
499 hb_face_set_upem (face, ft_face->units_per_EM);
500
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500501 return face;
502}
503
Behdad Esfahbod350f3a02014-12-28 17:44:26 -0800504/**
505 * hb_ft_face_create_referenced:
506 * @ft_face:
507 *
508 *
509 *
510 * Return value: (transfer full):
Sascha Brawer01c3a882015-06-01 13:22:01 +0200511 * Since: 0.9.38
Behdad Esfahbod350f3a02014-12-28 17:44:26 -0800512 **/
513hb_face_t *
514hb_ft_face_create_referenced (FT_Face ft_face)
515{
516 FT_Reference_Face (ft_face);
517 return hb_ft_face_create (ft_face, (hb_destroy_func_t) FT_Done_Face);
518}
519
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500520static void
521hb_ft_face_finalize (FT_Face ft_face)
522{
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400523 hb_face_destroy ((hb_face_t *) ft_face->generic.data);
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500524}
525
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400526/**
527 * hb_ft_face_create_cached:
528 * @ft_face:
529 *
530 *
531 *
532 * Return value: (transfer full):
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430533 * Since: 0.9.2
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400534 **/
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500535hb_face_t *
536hb_ft_face_create_cached (FT_Face ft_face)
537{
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400538 if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500539 {
540 if (ft_face->generic.finalizer)
Behdad Esfahbodedb54e92009-11-06 15:19:22 -0500541 ft_face->generic.finalizer (ft_face);
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500542
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500543 ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500544 ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500545 }
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500546
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400547 return hb_face_reference ((hb_face_t *) ft_face->generic.data);
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500548}
549
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500550
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400551/**
552 * hb_ft_font_create:
553 * @ft_face: (destroy destroy) (scope notified):
554 * @destroy:
555 *
556 *
557 *
558 * Return value: (transfer full):
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430559 * Since: 0.9.2
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400560 **/
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500561hb_font_t *
562hb_ft_font_create (FT_Face ft_face,
563 hb_destroy_func_t destroy)
564{
565 hb_font_t *font;
Behdad Esfahbodd2928852011-05-03 01:03:53 -0400566 hb_face_t *face;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500567
Behdad Esfahbodd2928852011-05-03 01:03:53 -0400568 face = hb_ft_face_create (ft_face, destroy);
569 font = hb_font_create (face);
570 hb_face_destroy (face);
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400571 _hb_ft_font_set_funcs (font, ft_face, false);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500572 hb_font_set_scale (font,
Behdad Esfahbod83408cf2013-11-06 14:46:04 -0500573 (int) (((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM + (1<<15)) >> 16),
574 (int) (((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM + (1<<15)) >> 16));
Behdad Esfahbodf34aaba2014-12-28 18:56:15 -0800575#if 0 /* hb-ft works in no-hinting model */
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500576 hb_font_set_ppem (font,
577 ft_face->size->metrics.x_ppem,
578 ft_face->size->metrics.y_ppem);
Behdad Esfahbodf34aaba2014-12-28 18:56:15 -0800579#endif
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500580
581 return font;
582}
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400583
Behdad Esfahbod350f3a02014-12-28 17:44:26 -0800584/**
585 * hb_ft_font_create_referenced:
586 * @ft_face:
587 *
588 *
589 *
590 * Return value: (transfer full):
Sascha Brawer01c3a882015-06-01 13:22:01 +0200591 * Since: 0.9.38
Behdad Esfahbod350f3a02014-12-28 17:44:26 -0800592 **/
593hb_font_t *
594hb_ft_font_create_referenced (FT_Face ft_face)
595{
596 FT_Reference_Face (ft_face);
597 return hb_ft_font_create (ft_face, (hb_destroy_func_t) FT_Done_Face);
598}
599
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400600
Behdad Esfahbodf64b2eb2012-06-05 19:23:29 -0400601/* Thread-safe, lock-free, FT_Library */
602
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400603static FT_Library ft_library;
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200604
Chris Petersonfb85d612015-01-04 19:31:10 -0800605#ifdef HB_USE_ATEXIT
606static
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400607void free_ft_library (void)
608{
609 FT_Done_FreeType (ft_library);
610}
Chris Petersonfb85d612015-01-04 19:31:10 -0800611#endif
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400612
613static FT_Library
614get_ft_library (void)
615{
616retry:
617 FT_Library library = (FT_Library) hb_atomic_ptr_get (&ft_library);
618
619 if (unlikely (!library))
620 {
621 /* Not found; allocate one. */
622 if (FT_Init_FreeType (&library))
623 return NULL;
624
625 if (!hb_atomic_ptr_cmpexch (&ft_library, NULL, library)) {
626 FT_Done_FreeType (library);
627 goto retry;
628 }
629
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400630#ifdef HB_USE_ATEXIT
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400631 atexit (free_ft_library); /* First person registers atexit() callback. */
632#endif
633 }
634
635 return library;
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200636}
637
638static void
639_release_blob (FT_Face ft_face)
640{
641 hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
642}
643
644void
645hb_ft_font_set_funcs (hb_font_t *font)
646{
647 hb_blob_t *blob = hb_face_reference_blob (font->face);
648 unsigned int blob_length;
649 const char *blob_data = hb_blob_get_data (blob, &blob_length);
650 if (unlikely (!blob_length))
651 DEBUG_MSG (FT, font, "Font face has empty blob");
652
653 FT_Face ft_face = NULL;
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400654 FT_Error err = FT_New_Memory_Face (get_ft_library (),
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200655 (const FT_Byte *) blob_data,
656 blob_length,
657 hb_face_get_index (font->face),
658 &ft_face);
659
660 if (unlikely (err)) {
661 hb_blob_destroy (blob);
662 DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed");
663 return;
664 }
665
Behdad Esfahbod254142b2011-08-15 16:15:44 +0200666 FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE);
667
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200668 FT_Set_Char_Size (ft_face,
Behdad Esfahboda319d072015-01-23 12:44:24 -0800669 abs (font->x_scale), abs (font->y_scale),
Behdad Esfahbod2023e2b2012-07-11 19:00:30 -0400670 0, 0);
671#if 0
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200672 font->x_ppem * 72 * 64 / font->x_scale,
673 font->y_ppem * 72 * 64 / font->y_scale);
Behdad Esfahbod2023e2b2012-07-11 19:00:30 -0400674#endif
Behdad Esfahboda319d072015-01-23 12:44:24 -0800675 if (font->x_scale < 0 || font->y_scale < 0)
676 {
677 FT_Matrix matrix = { font->x_scale < 0 ? -1 : +1, 0,
678 0, font->y_scale < 0 ? -1 : +1};
679 FT_Set_Transform (ft_face, &matrix, NULL);
680 }
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200681
682 ft_face->generic.data = blob;
683 ft_face->generic.finalizer = (FT_Generic_Finalizer) _release_blob;
684
Behdad Esfahbod2a9627c2015-10-07 17:33:20 -0400685 _hb_ft_font_set_funcs (font, ft_face, true);
686 hb_ft_font_set_load_flags (font, FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING);
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200687}