blob: 322f93a8efcc406a3b733f0913dce0f7059f214a [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 Esfahbod8fb3d1a2009-11-03 18:34:20 -05004 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04005 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -05006 *
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
12 *
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 *
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 *
25 * Red Hat Author(s): Behdad Esfahbod
26 */
27
Behdad Esfahbodc57d4542011-04-20 18:50:27 -040028#include "hb-private.hh"
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -050029
30#include "hb-ft.h"
31
Behdad Esfahbodc57d4542011-04-20 18:50:27 -040032#include "hb-font-private.hh"
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -050033
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +020034#include FT_ADVANCES_H
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -050035#include FT_TRUETYPE_TABLES_H
36
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040037
38
Behdad Esfahbod38b21182011-08-09 10:51:24 +020039#ifndef HB_DEBUG_FT
40#define HB_DEBUG_FT (HB_DEBUG+0)
41#endif
42
43
Behdad Esfahbod744970a2011-05-16 18:15:37 -040044/* TODO:
45 *
46 * In general, this file does a fine job of what it's supposed to do.
47 * There are, however, things that need more work:
48 *
49 * - We don't handle any load_flags. That definitely has API implications. :(
50 * I believe hb_ft_font_create() should take load_flags input.
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +020051 * In particular, FT_Get_Advance() without the NO_HINTING flag seems to be
52 * buggy.
Behdad Esfahbod744970a2011-05-16 18:15:37 -040053 *
Behdad Esfahbodf34aaba2014-12-28 18:56:15 -080054 * FreeType works in 26.6 mode. Clients can decide to use that mode, and everything
55 * 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
58 * pass NO_HINTING around. This seems to work best, until we go ahead and add a full
59 * load_flags API.
60 *
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 Esfahbod0fd8c2f2011-05-12 15:14:13 -040069static hb_bool_t
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040070hb_ft_get_glyph (hb_font_t *font HB_UNUSED,
Behdad Esfahbodb4678272011-05-11 23:25:28 -040071 void *font_data,
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040072 hb_codepoint_t unicode,
73 hb_codepoint_t variation_selector,
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -040074 hb_codepoint_t *glyph,
Behdad Esfahbodb4678272011-05-11 23:25:28 -040075 void *user_data HB_UNUSED)
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040076
77{
78 FT_Face ft_face = (FT_Face) font_data;
79
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040080 if (unlikely (variation_selector)) {
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -040081 *glyph = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector);
Behdad Esfahbod79d10072013-06-13 19:01:07 -040082 return *glyph != 0;
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040083 }
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040084
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -040085 *glyph = FT_Get_Char_Index (ft_face, unicode);
86 return *glyph != 0;
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040087}
88
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -040089static hb_position_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -040090hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
91 void *font_data,
92 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -040093 void *user_data HB_UNUSED)
94{
95 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +020096 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
97 FT_Fixed v;
Behdad Esfahbod744970a2011-05-16 18:15:37 -040098
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +020099 if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v)))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400100 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400101
Behdad Esfahboda319d072015-01-23 12:44:24 -0800102 if (font->x_scale < 0)
103 v = -v;
104
Behdad Esfahbod755b44c2013-10-18 11:17:42 +0200105 return (v + (1<<9)) >> 10;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400106}
107
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400108static hb_position_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400109hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
110 void *font_data,
111 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400112 void *user_data HB_UNUSED)
113{
114 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200115 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING | FT_LOAD_VERTICAL_LAYOUT;
116 FT_Fixed v;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400117
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200118 if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v)))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400119 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400120
Behdad Esfahbod7888a6b2015-01-28 12:40:40 -0800121 if (font->y_scale < 0)
122 v = -v;
123
Behdad Esfahbod8b38fae2011-05-19 13:08:00 -0400124 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
125 * have a Y growing upward. Hence the extra negation. */
Behdad Esfahbod755b44c2013-10-18 11:17:42 +0200126 return (-v + (1<<9)) >> 10;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400127}
128
129static hb_bool_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400130hb_ft_get_glyph_h_origin (hb_font_t *font HB_UNUSED,
131 void *font_data HB_UNUSED,
132 hb_codepoint_t glyph HB_UNUSED,
Behdad Esfahbod19098182011-05-17 23:27:22 -0400133 hb_position_t *x HB_UNUSED,
134 hb_position_t *y HB_UNUSED,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400135 void *user_data HB_UNUSED)
136{
137 /* We always work in the horizontal coordinates. */
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400138 return true;
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400139}
140
141static hb_bool_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400142hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
143 void *font_data,
144 hb_codepoint_t glyph,
Behdad Esfahbod19098182011-05-17 23:27:22 -0400145 hb_position_t *x,
146 hb_position_t *y,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400147 void *user_data HB_UNUSED)
148{
149 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod8afaf092014-10-02 16:40:41 -0400150 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400151
152 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400153 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400154
Behdad Esfahbod8b38fae2011-05-19 13:08:00 -0400155 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
156 * have a Y growing upward. Hence the extra negation. */
157 *x = ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBearingX;
158 *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);
159
Behdad Esfahbod7888a6b2015-01-28 12:40:40 -0800160 if (font->x_scale < 0)
161 *x = -*x;
162 if (font->y_scale < 0)
163 *y = -*y;
164
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400165 return true;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400166}
167
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400168static hb_position_t
Behdad Esfahbodcdf74442012-07-11 18:52:39 -0400169hb_ft_get_glyph_h_kerning (hb_font_t *font,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400170 void *font_data,
171 hb_codepoint_t left_glyph,
172 hb_codepoint_t right_glyph,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400173 void *user_data HB_UNUSED)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500174{
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -0400175 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod60fbb362011-05-19 18:46:15 -0400176 FT_Vector kerningv;
Behdad Esfahbod3b593062009-11-04 15:48:32 -0500177
Behdad Esfahbodcdf74442012-07-11 18:52:39 -0400178 FT_Kerning_Mode mode = font->x_ppem ? FT_KERNING_DEFAULT : FT_KERNING_UNFITTED;
179 if (FT_Get_Kerning (ft_face, left_glyph, right_glyph, mode, &kerningv))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400180 return 0;
Behdad Esfahbod3b593062009-11-04 15:48:32 -0500181
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400182 return kerningv.x;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400183}
184
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400185static hb_position_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400186hb_ft_get_glyph_v_kerning (hb_font_t *font HB_UNUSED,
187 void *font_data HB_UNUSED,
188 hb_codepoint_t top_glyph HB_UNUSED,
189 hb_codepoint_t bottom_glyph HB_UNUSED,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400190 void *user_data HB_UNUSED)
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400191{
192 /* FreeType API doesn't support vertical kerning */
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400193 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400194}
195
196static hb_bool_t
197hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
198 void *font_data,
199 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400200 hb_glyph_extents_t *extents,
201 void *user_data HB_UNUSED)
202{
203 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod8afaf092014-10-02 16:40:41 -0400204 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400205
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400206 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400207 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400208
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400209 extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
210 extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
211 extents->width = ft_face->glyph->metrics.width;
Behdad Esfahbod21756932012-08-08 01:20:45 -0400212 extents->height = -ft_face->glyph->metrics.height;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400213 return true;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400214}
215
216static hb_bool_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400217hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
218 void *font_data,
219 hb_codepoint_t glyph,
220 unsigned int point_index,
221 hb_position_t *x,
222 hb_position_t *y,
223 void *user_data HB_UNUSED)
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400224{
225 FT_Face ft_face = (FT_Face) font_data;
226 int load_flags = FT_LOAD_DEFAULT;
227
228 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400229 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400230
231 if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400232 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400233
234 if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400235 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400236
237 *x = ft_face->glyph->outline.points[point_index].x;
238 *y = ft_face->glyph->outline.points[point_index].y;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400239
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400240 return true;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500241}
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500242
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400243static hb_bool_t
Behdad Esfahbod271c8f82012-07-13 09:32:30 -0400244hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED,
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400245 void *font_data,
246 hb_codepoint_t glyph,
247 char *name, unsigned int size,
248 void *user_data HB_UNUSED)
249{
250 FT_Face ft_face = (FT_Face) font_data;
251
252 hb_bool_t ret = !FT_Get_Glyph_Name (ft_face, glyph, name, size);
Behdad Esfahbod5594c2d2013-03-06 19:37:31 -0500253 if (ret && (size && !*name))
254 ret = false;
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400255
256 return ret;
257}
258
259static hb_bool_t
Behdad Esfahbod271c8f82012-07-13 09:32:30 -0400260hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400261 void *font_data,
262 const char *name, int len, /* -1 means nul-terminated */
263 hb_codepoint_t *glyph,
264 void *user_data HB_UNUSED)
265{
266 FT_Face ft_face = (FT_Face) font_data;
267
268 if (len < 0)
269 *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
270 else {
271 /* Make a nul-terminated version. */
272 char buf[128];
273 len = MIN (len, (int) sizeof (buf) - 1);
274 strncpy (buf, name, len);
275 buf[len] = '\0';
276 *glyph = FT_Get_Name_Index (ft_face, buf);
277 }
278
Behdad Esfahbode509d352013-07-11 14:56:45 -0400279 if (*glyph == 0)
280 {
281 /* Check whether the given name was actually the name of glyph 0. */
282 char buf[128];
283 if (!FT_Get_Glyph_Name(ft_face, 0, buf, sizeof (buf)) &&
284 len < 0 ? !strcmp (buf, name) : !strncmp (buf, name, len))
285 return true;
286 }
287
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400288 return *glyph != 0;
289}
290
291
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200292static hb_font_funcs_t *
293_hb_ft_get_font_funcs (void)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500294{
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400295 static const hb_font_funcs_t ft_ffuncs = {
296 HB_OBJECT_HEADER_STATIC,
297
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400298 true, /* immutable */
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400299
300 {
301#define HB_FONT_FUNC_IMPLEMENT(name) hb_ft_get_##name,
302 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
303#undef HB_FONT_FUNC_IMPLEMENT
304 }
305 };
306
307 return const_cast<hb_font_funcs_t *> (&ft_ffuncs);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500308}
309
310
311static hb_blob_t *
Behdad Esfahbode7157842011-08-08 21:42:02 +0200312reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500313{
314 FT_Face ft_face = (FT_Face) user_data;
315 FT_Byte *buffer;
316 FT_ULong length = 0;
317 FT_Error error;
318
Behdad Esfahbod38973352011-08-08 23:37:41 +0200319 /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob */
Behdad Esfahbod2cd1ea42010-04-29 14:15:32 -0400320
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500321 error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
322 if (error)
Behdad Esfahbod750a2292010-05-20 16:23:27 +0100323 return NULL;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500324
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400325 buffer = (FT_Byte *) malloc (length);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500326 if (buffer == NULL)
Behdad Esfahbodd6b3c832010-04-23 19:59:53 -0400327 return NULL;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500328
329 error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
330 if (error)
Behdad Esfahbod750a2292010-05-20 16:23:27 +0100331 return NULL;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500332
333 return hb_blob_create ((const char *) buffer, length,
334 HB_MEMORY_MODE_WRITABLE,
Behdad Esfahbod56681892011-04-20 03:03:32 -0400335 buffer, free);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500336}
337
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400338/**
339 * hb_ft_face_create:
340 * @ft_face: (destroy destroy) (scope notified):
341 * @destroy:
342 *
343 *
344 *
345 * Return value: (transfer full):
346 * Since: 1.0
347 **/
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500348hb_face_t *
349hb_ft_face_create (FT_Face ft_face,
350 hb_destroy_func_t destroy)
351{
352 hb_face_t *face;
353
Behdad Esfahbode48ed722010-03-01 22:33:45 -0500354 if (ft_face->stream->read == NULL) {
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500355 hb_blob_t *blob;
356
357 blob = hb_blob_create ((const char *) ft_face->stream->base,
358 (unsigned int) ft_face->stream->size,
Behdad Esfahbodaffacf22014-12-28 16:20:31 -0800359 HB_MEMORY_MODE_READONLY,
Behdad Esfahbod56681892011-04-20 03:03:32 -0400360 ft_face, destroy);
Behdad Esfahbod9a146882011-05-11 22:49:29 -0400361 face = hb_face_create (blob, ft_face->face_index);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500362 hb_blob_destroy (blob);
363 } else {
Behdad Esfahbode7157842011-08-08 21:42:02 +0200364 face = hb_face_create_for_tables (reference_table, ft_face, destroy);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500365 }
366
Behdad Esfahbodde1e1cf2011-08-09 00:19:38 +0200367 hb_face_set_index (face, ft_face->face_index);
368 hb_face_set_upem (face, ft_face->units_per_EM);
369
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500370 return face;
371}
372
Behdad Esfahbod350f3a02014-12-28 17:44:26 -0800373/**
374 * hb_ft_face_create_referenced:
375 * @ft_face:
376 *
377 *
378 *
379 * Return value: (transfer full):
380 * Since: 1.0
381 **/
382hb_face_t *
383hb_ft_face_create_referenced (FT_Face ft_face)
384{
385 FT_Reference_Face (ft_face);
386 return hb_ft_face_create (ft_face, (hb_destroy_func_t) FT_Done_Face);
387}
388
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500389static void
390hb_ft_face_finalize (FT_Face ft_face)
391{
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400392 hb_face_destroy ((hb_face_t *) ft_face->generic.data);
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500393}
394
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400395/**
396 * hb_ft_face_create_cached:
397 * @ft_face:
398 *
399 *
400 *
401 * Return value: (transfer full):
402 * Since: 1.0
403 **/
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500404hb_face_t *
405hb_ft_face_create_cached (FT_Face ft_face)
406{
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400407 if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500408 {
409 if (ft_face->generic.finalizer)
Behdad Esfahbodedb54e92009-11-06 15:19:22 -0500410 ft_face->generic.finalizer (ft_face);
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500411
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500412 ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500413 ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500414 }
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500415
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400416 return hb_face_reference ((hb_face_t *) ft_face->generic.data);
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500417}
418
Behdad Esfahbod553bc3d2011-08-15 16:21:06 +0200419static void
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200420_do_nothing (void)
421{
422}
423
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500424
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400425/**
426 * hb_ft_font_create:
427 * @ft_face: (destroy destroy) (scope notified):
428 * @destroy:
429 *
430 *
431 *
432 * Return value: (transfer full):
433 * Since: 1.0
434 **/
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500435hb_font_t *
436hb_ft_font_create (FT_Face ft_face,
437 hb_destroy_func_t destroy)
438{
439 hb_font_t *font;
Behdad Esfahbodd2928852011-05-03 01:03:53 -0400440 hb_face_t *face;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500441
Behdad Esfahbodd2928852011-05-03 01:03:53 -0400442 face = hb_ft_face_create (ft_face, destroy);
443 font = hb_font_create (face);
444 hb_face_destroy (face);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500445 hb_font_set_funcs (font,
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200446 _hb_ft_get_font_funcs (),
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200447 ft_face, (hb_destroy_func_t) _do_nothing);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500448 hb_font_set_scale (font,
Behdad Esfahbod83408cf2013-11-06 14:46:04 -0500449 (int) (((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM + (1<<15)) >> 16),
450 (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 -0800451#if 0 /* hb-ft works in no-hinting model */
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500452 hb_font_set_ppem (font,
453 ft_face->size->metrics.x_ppem,
454 ft_face->size->metrics.y_ppem);
Behdad Esfahbodf34aaba2014-12-28 18:56:15 -0800455#endif
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500456
457 return font;
458}
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400459
Behdad Esfahbod350f3a02014-12-28 17:44:26 -0800460/**
461 * hb_ft_font_create_referenced:
462 * @ft_face:
463 *
464 *
465 *
466 * Return value: (transfer full):
467 * Since: 1.0
468 **/
469hb_font_t *
470hb_ft_font_create_referenced (FT_Face ft_face)
471{
472 FT_Reference_Face (ft_face);
473 return hb_ft_font_create (ft_face, (hb_destroy_func_t) FT_Done_Face);
474}
475
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400476
Behdad Esfahbodf64b2eb2012-06-05 19:23:29 -0400477/* Thread-safe, lock-free, FT_Library */
478
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400479static FT_Library ft_library;
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200480
Chris Petersonfb85d612015-01-04 19:31:10 -0800481#ifdef HB_USE_ATEXIT
482static
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400483void free_ft_library (void)
484{
485 FT_Done_FreeType (ft_library);
486}
Chris Petersonfb85d612015-01-04 19:31:10 -0800487#endif
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400488
489static FT_Library
490get_ft_library (void)
491{
492retry:
493 FT_Library library = (FT_Library) hb_atomic_ptr_get (&ft_library);
494
495 if (unlikely (!library))
496 {
497 /* Not found; allocate one. */
498 if (FT_Init_FreeType (&library))
499 return NULL;
500
501 if (!hb_atomic_ptr_cmpexch (&ft_library, NULL, library)) {
502 FT_Done_FreeType (library);
503 goto retry;
504 }
505
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400506#ifdef HB_USE_ATEXIT
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400507 atexit (free_ft_library); /* First person registers atexit() callback. */
508#endif
509 }
510
511 return library;
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200512}
513
514static void
515_release_blob (FT_Face ft_face)
516{
517 hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
518}
519
520void
521hb_ft_font_set_funcs (hb_font_t *font)
522{
523 hb_blob_t *blob = hb_face_reference_blob (font->face);
524 unsigned int blob_length;
525 const char *blob_data = hb_blob_get_data (blob, &blob_length);
526 if (unlikely (!blob_length))
527 DEBUG_MSG (FT, font, "Font face has empty blob");
528
529 FT_Face ft_face = NULL;
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400530 FT_Error err = FT_New_Memory_Face (get_ft_library (),
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200531 (const FT_Byte *) blob_data,
532 blob_length,
533 hb_face_get_index (font->face),
534 &ft_face);
535
536 if (unlikely (err)) {
537 hb_blob_destroy (blob);
538 DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed");
539 return;
540 }
541
Behdad Esfahbod254142b2011-08-15 16:15:44 +0200542 FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE);
543
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200544 FT_Set_Char_Size (ft_face,
Behdad Esfahboda319d072015-01-23 12:44:24 -0800545 abs (font->x_scale), abs (font->y_scale),
Behdad Esfahbod2023e2b2012-07-11 19:00:30 -0400546 0, 0);
547#if 0
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200548 font->x_ppem * 72 * 64 / font->x_scale,
549 font->y_ppem * 72 * 64 / font->y_scale);
Behdad Esfahbod2023e2b2012-07-11 19:00:30 -0400550#endif
Behdad Esfahboda319d072015-01-23 12:44:24 -0800551 if (font->x_scale < 0 || font->y_scale < 0)
552 {
553 FT_Matrix matrix = { font->x_scale < 0 ? -1 : +1, 0,
554 0, font->y_scale < 0 ? -1 : +1};
555 FT_Set_Transform (ft_face, &matrix, NULL);
556 }
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200557
558 ft_face->generic.data = blob;
559 ft_face->generic.finalizer = (FT_Generic_Finalizer) _release_blob;
560
561 hb_font_set_funcs (font,
562 _hb_ft_get_font_funcs (),
563 ft_face,
564 (hb_destroy_func_t) FT_Done_Face);
565}
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200566
567FT_Face
568hb_ft_font_get_face (hb_font_t *font)
569{
570 if (font->destroy == (hb_destroy_func_t) FT_Done_Face ||
571 font->destroy == (hb_destroy_func_t) _do_nothing)
572 return (FT_Face) font->user_data;
Behdad Esfahbod9527fb22011-08-13 19:03:48 +0200573
574 return NULL;
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200575}