blob: 44e0b0bc2a560885d910477efa4634efe0e8f082 [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 *
54 * - We don't handle / allow for emboldening / obliqueing.
55 *
Behdad Esfahbod190e19e2013-03-09 20:30:22 -050056 * - In the future, we should add constructors to create fonts in font space?
Behdad Esfahbod744970a2011-05-16 18:15:37 -040057 *
Behdad Esfahbod323190c2012-04-12 12:29:10 -040058 * - FT_Load_Glyph() is exteremely costly. Do something about it?
Behdad Esfahbod744970a2011-05-16 18:15:37 -040059 */
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -050060
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040061
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -040062static hb_bool_t
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040063hb_ft_get_glyph (hb_font_t *font HB_UNUSED,
Behdad Esfahbodb4678272011-05-11 23:25:28 -040064 void *font_data,
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040065 hb_codepoint_t unicode,
66 hb_codepoint_t variation_selector,
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -040067 hb_codepoint_t *glyph,
Behdad Esfahbodb4678272011-05-11 23:25:28 -040068 void *user_data HB_UNUSED)
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040069
70{
71 FT_Face ft_face = (FT_Face) font_data;
72
73#ifdef HAVE_FT_FACE_GETCHARVARIANTINDEX
74 if (unlikely (variation_selector)) {
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -040075 *glyph = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector);
Behdad Esfahbod79d10072013-06-13 19:01:07 -040076 return *glyph != 0;
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040077 }
78#endif
79
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -040080 *glyph = FT_Get_Char_Index (ft_face, unicode);
81 return *glyph != 0;
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040082}
83
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -040084static hb_position_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -040085hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
86 void *font_data,
87 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -040088 void *user_data HB_UNUSED)
89{
90 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +020091 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
92 FT_Fixed v;
Behdad Esfahbod744970a2011-05-16 18:15:37 -040093
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +020094 if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v)))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -040095 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -040096
Behdad Esfahbod755b44c2013-10-18 11:17:42 +020097 return (v + (1<<9)) >> 10;
Behdad Esfahbod744970a2011-05-16 18:15:37 -040098}
99
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400100static hb_position_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400101hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
102 void *font_data,
103 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400104 void *user_data HB_UNUSED)
105{
106 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200107 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING | FT_LOAD_VERTICAL_LAYOUT;
108 FT_Fixed v;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400109
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200110 if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v)))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400111 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400112
Behdad Esfahbod8b38fae2011-05-19 13:08:00 -0400113 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
114 * have a Y growing upward. Hence the extra negation. */
Behdad Esfahbod755b44c2013-10-18 11:17:42 +0200115 return (-v + (1<<9)) >> 10;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400116}
117
118static hb_bool_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400119hb_ft_get_glyph_h_origin (hb_font_t *font HB_UNUSED,
120 void *font_data HB_UNUSED,
121 hb_codepoint_t glyph HB_UNUSED,
Behdad Esfahbod19098182011-05-17 23:27:22 -0400122 hb_position_t *x HB_UNUSED,
123 hb_position_t *y HB_UNUSED,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400124 void *user_data HB_UNUSED)
125{
126 /* We always work in the horizontal coordinates. */
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400127 return true;
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400128}
129
130static hb_bool_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400131hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
132 void *font_data,
133 hb_codepoint_t glyph,
Behdad Esfahbod19098182011-05-17 23:27:22 -0400134 hb_position_t *x,
135 hb_position_t *y,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400136 void *user_data HB_UNUSED)
137{
138 FT_Face ft_face = (FT_Face) font_data;
139 int load_flags = FT_LOAD_DEFAULT;
140
141 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400142 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400143
Behdad Esfahbod8b38fae2011-05-19 13:08:00 -0400144 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
145 * have a Y growing upward. Hence the extra negation. */
146 *x = ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBearingX;
147 *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);
148
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400149 return true;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400150}
151
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400152static hb_position_t
Behdad Esfahbodcdf74442012-07-11 18:52:39 -0400153hb_ft_get_glyph_h_kerning (hb_font_t *font,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400154 void *font_data,
155 hb_codepoint_t left_glyph,
156 hb_codepoint_t right_glyph,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400157 void *user_data HB_UNUSED)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500158{
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -0400159 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod60fbb362011-05-19 18:46:15 -0400160 FT_Vector kerningv;
Behdad Esfahbod3b593062009-11-04 15:48:32 -0500161
Behdad Esfahbodcdf74442012-07-11 18:52:39 -0400162 FT_Kerning_Mode mode = font->x_ppem ? FT_KERNING_DEFAULT : FT_KERNING_UNFITTED;
163 if (FT_Get_Kerning (ft_face, left_glyph, right_glyph, mode, &kerningv))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400164 return 0;
Behdad Esfahbod3b593062009-11-04 15:48:32 -0500165
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400166 return kerningv.x;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400167}
168
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400169static hb_position_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400170hb_ft_get_glyph_v_kerning (hb_font_t *font HB_UNUSED,
171 void *font_data HB_UNUSED,
172 hb_codepoint_t top_glyph HB_UNUSED,
173 hb_codepoint_t bottom_glyph HB_UNUSED,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400174 void *user_data HB_UNUSED)
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400175{
176 /* FreeType API doesn't support vertical kerning */
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400177 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400178}
179
180static hb_bool_t
181hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
182 void *font_data,
183 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400184 hb_glyph_extents_t *extents,
185 void *user_data HB_UNUSED)
186{
187 FT_Face ft_face = (FT_Face) font_data;
188 int load_flags = FT_LOAD_DEFAULT;
189
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400190 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400191 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400192
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400193 extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
194 extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
195 extents->width = ft_face->glyph->metrics.width;
Behdad Esfahbod21756932012-08-08 01:20:45 -0400196 extents->height = -ft_face->glyph->metrics.height;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400197 return true;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400198}
199
200static hb_bool_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400201hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
202 void *font_data,
203 hb_codepoint_t glyph,
204 unsigned int point_index,
205 hb_position_t *x,
206 hb_position_t *y,
207 void *user_data HB_UNUSED)
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400208{
209 FT_Face ft_face = (FT_Face) font_data;
210 int load_flags = FT_LOAD_DEFAULT;
211
212 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400213 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400214
215 if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400216 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400217
218 if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400219 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400220
221 *x = ft_face->glyph->outline.points[point_index].x;
222 *y = ft_face->glyph->outline.points[point_index].y;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400223
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400224 return true;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500225}
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500226
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400227static hb_bool_t
Behdad Esfahbod271c8f82012-07-13 09:32:30 -0400228hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED,
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400229 void *font_data,
230 hb_codepoint_t glyph,
231 char *name, unsigned int size,
232 void *user_data HB_UNUSED)
233{
234 FT_Face ft_face = (FT_Face) font_data;
235
236 hb_bool_t ret = !FT_Get_Glyph_Name (ft_face, glyph, name, size);
Behdad Esfahbod5594c2d2013-03-06 19:37:31 -0500237 if (ret && (size && !*name))
238 ret = false;
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400239
240 return ret;
241}
242
243static hb_bool_t
Behdad Esfahbod271c8f82012-07-13 09:32:30 -0400244hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400245 void *font_data,
246 const char *name, int len, /* -1 means nul-terminated */
247 hb_codepoint_t *glyph,
248 void *user_data HB_UNUSED)
249{
250 FT_Face ft_face = (FT_Face) font_data;
251
252 if (len < 0)
253 *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
254 else {
255 /* Make a nul-terminated version. */
256 char buf[128];
257 len = MIN (len, (int) sizeof (buf) - 1);
258 strncpy (buf, name, len);
259 buf[len] = '\0';
260 *glyph = FT_Get_Name_Index (ft_face, buf);
261 }
262
Behdad Esfahbode509d352013-07-11 14:56:45 -0400263 if (*glyph == 0)
264 {
265 /* Check whether the given name was actually the name of glyph 0. */
266 char buf[128];
267 if (!FT_Get_Glyph_Name(ft_face, 0, buf, sizeof (buf)) &&
268 len < 0 ? !strcmp (buf, name) : !strncmp (buf, name, len))
269 return true;
270 }
271
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400272 return *glyph != 0;
273}
274
275
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200276static hb_font_funcs_t *
277_hb_ft_get_font_funcs (void)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500278{
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400279 static const hb_font_funcs_t ft_ffuncs = {
280 HB_OBJECT_HEADER_STATIC,
281
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400282 true, /* immutable */
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400283
284 {
285#define HB_FONT_FUNC_IMPLEMENT(name) hb_ft_get_##name,
286 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
287#undef HB_FONT_FUNC_IMPLEMENT
288 }
289 };
290
291 return const_cast<hb_font_funcs_t *> (&ft_ffuncs);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500292}
293
294
295static hb_blob_t *
Behdad Esfahbode7157842011-08-08 21:42:02 +0200296reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500297{
298 FT_Face ft_face = (FT_Face) user_data;
299 FT_Byte *buffer;
300 FT_ULong length = 0;
301 FT_Error error;
302
Behdad Esfahbod38973352011-08-08 23:37:41 +0200303 /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob */
Behdad Esfahbod2cd1ea42010-04-29 14:15:32 -0400304
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500305 error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
306 if (error)
Behdad Esfahbod750a2292010-05-20 16:23:27 +0100307 return NULL;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500308
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400309 buffer = (FT_Byte *) malloc (length);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500310 if (buffer == NULL)
Behdad Esfahbodd6b3c832010-04-23 19:59:53 -0400311 return NULL;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500312
313 error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
314 if (error)
Behdad Esfahbod750a2292010-05-20 16:23:27 +0100315 return NULL;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500316
317 return hb_blob_create ((const char *) buffer, length,
318 HB_MEMORY_MODE_WRITABLE,
Behdad Esfahbod56681892011-04-20 03:03:32 -0400319 buffer, free);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500320}
321
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400322/**
323 * hb_ft_face_create:
324 * @ft_face: (destroy destroy) (scope notified):
325 * @destroy:
326 *
327 *
328 *
329 * Return value: (transfer full):
330 * Since: 1.0
331 **/
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500332hb_face_t *
333hb_ft_face_create (FT_Face ft_face,
334 hb_destroy_func_t destroy)
335{
336 hb_face_t *face;
337
Behdad Esfahbode48ed722010-03-01 22:33:45 -0500338 if (ft_face->stream->read == NULL) {
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500339 hb_blob_t *blob;
340
341 blob = hb_blob_create ((const char *) ft_face->stream->base,
342 (unsigned int) ft_face->stream->size,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400343 /* TODO: We assume that it's mmap()'ed, but FreeType code
344 * suggests that there are cases we reach here but font is
345 * not mmapped. For example, when mmap() fails. No idea
346 * how to deal with it better here. */
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500347 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE,
Behdad Esfahbod56681892011-04-20 03:03:32 -0400348 ft_face, destroy);
Behdad Esfahbod9a146882011-05-11 22:49:29 -0400349 face = hb_face_create (blob, ft_face->face_index);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500350 hb_blob_destroy (blob);
351 } else {
Behdad Esfahbode7157842011-08-08 21:42:02 +0200352 face = hb_face_create_for_tables (reference_table, ft_face, destroy);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500353 }
354
Behdad Esfahbodde1e1cf2011-08-09 00:19:38 +0200355 hb_face_set_index (face, ft_face->face_index);
356 hb_face_set_upem (face, ft_face->units_per_EM);
357
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500358 return face;
359}
360
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500361static void
362hb_ft_face_finalize (FT_Face ft_face)
363{
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400364 hb_face_destroy ((hb_face_t *) ft_face->generic.data);
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500365}
366
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400367/**
368 * hb_ft_face_create_cached:
369 * @ft_face:
370 *
371 *
372 *
373 * Return value: (transfer full):
374 * Since: 1.0
375 **/
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500376hb_face_t *
377hb_ft_face_create_cached (FT_Face ft_face)
378{
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400379 if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500380 {
381 if (ft_face->generic.finalizer)
Behdad Esfahbodedb54e92009-11-06 15:19:22 -0500382 ft_face->generic.finalizer (ft_face);
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500383
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500384 ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500385 ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500386 }
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500387
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400388 return hb_face_reference ((hb_face_t *) ft_face->generic.data);
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500389}
390
Behdad Esfahbod553bc3d2011-08-15 16:21:06 +0200391static void
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200392_do_nothing (void)
393{
394}
395
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500396
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400397/**
398 * hb_ft_font_create:
399 * @ft_face: (destroy destroy) (scope notified):
400 * @destroy:
401 *
402 *
403 *
404 * Return value: (transfer full):
405 * Since: 1.0
406 **/
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500407hb_font_t *
408hb_ft_font_create (FT_Face ft_face,
409 hb_destroy_func_t destroy)
410{
411 hb_font_t *font;
Behdad Esfahbodd2928852011-05-03 01:03:53 -0400412 hb_face_t *face;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500413
Behdad Esfahbodd2928852011-05-03 01:03:53 -0400414 face = hb_ft_face_create (ft_face, destroy);
415 font = hb_font_create (face);
416 hb_face_destroy (face);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500417 hb_font_set_funcs (font,
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200418 _hb_ft_get_font_funcs (),
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200419 ft_face, (hb_destroy_func_t) _do_nothing);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500420 hb_font_set_scale (font,
Behdad Esfahbod83408cf2013-11-06 14:46:04 -0500421 (int) (((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM + (1<<15)) >> 16),
422 (int) (((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM + (1<<15)) >> 16));
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500423 hb_font_set_ppem (font,
424 ft_face->size->metrics.x_ppem,
425 ft_face->size->metrics.y_ppem);
426
427 return font;
428}
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400429
430
Behdad Esfahbodf64b2eb2012-06-05 19:23:29 -0400431/* Thread-safe, lock-free, FT_Library */
432
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400433static FT_Library ft_library;
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200434
Behdad Esfahbode9171af2013-01-29 22:45:00 -0500435static inline
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400436void free_ft_library (void)
437{
438 FT_Done_FreeType (ft_library);
439}
440
441static FT_Library
442get_ft_library (void)
443{
444retry:
445 FT_Library library = (FT_Library) hb_atomic_ptr_get (&ft_library);
446
447 if (unlikely (!library))
448 {
449 /* Not found; allocate one. */
450 if (FT_Init_FreeType (&library))
451 return NULL;
452
453 if (!hb_atomic_ptr_cmpexch (&ft_library, NULL, library)) {
454 FT_Done_FreeType (library);
455 goto retry;
456 }
457
458#ifdef HAVE_ATEXIT
459 atexit (free_ft_library); /* First person registers atexit() callback. */
460#endif
461 }
462
463 return library;
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200464}
465
466static void
467_release_blob (FT_Face ft_face)
468{
469 hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
470}
471
472void
473hb_ft_font_set_funcs (hb_font_t *font)
474{
475 hb_blob_t *blob = hb_face_reference_blob (font->face);
476 unsigned int blob_length;
477 const char *blob_data = hb_blob_get_data (blob, &blob_length);
478 if (unlikely (!blob_length))
479 DEBUG_MSG (FT, font, "Font face has empty blob");
480
481 FT_Face ft_face = NULL;
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400482 FT_Error err = FT_New_Memory_Face (get_ft_library (),
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200483 (const FT_Byte *) blob_data,
484 blob_length,
485 hb_face_get_index (font->face),
486 &ft_face);
487
488 if (unlikely (err)) {
489 hb_blob_destroy (blob);
490 DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed");
491 return;
492 }
493
Behdad Esfahbod254142b2011-08-15 16:15:44 +0200494 FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE);
495
Behdad Esfahbod21756932012-08-08 01:20:45 -0400496 assert (font->y_scale >= 0);
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200497 FT_Set_Char_Size (ft_face,
498 font->x_scale, font->y_scale,
Behdad Esfahbod2023e2b2012-07-11 19:00:30 -0400499 0, 0);
500#if 0
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200501 font->x_ppem * 72 * 64 / font->x_scale,
502 font->y_ppem * 72 * 64 / font->y_scale);
Behdad Esfahbod2023e2b2012-07-11 19:00:30 -0400503#endif
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200504
505 ft_face->generic.data = blob;
506 ft_face->generic.finalizer = (FT_Generic_Finalizer) _release_blob;
507
508 hb_font_set_funcs (font,
509 _hb_ft_get_font_funcs (),
510 ft_face,
511 (hb_destroy_func_t) FT_Done_Face);
512}
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200513
514FT_Face
515hb_ft_font_get_face (hb_font_t *font)
516{
517 if (font->destroy == (hb_destroy_func_t) FT_Done_Face ||
518 font->destroy == (hb_destroy_func_t) _do_nothing)
519 return (FT_Face) font->user_data;
Behdad Esfahbod9527fb22011-08-13 19:03:48 +0200520
521 return NULL;
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200522}