blob: 20fb32e66fd31f6b1c7d4af96f46855ef94bd421 [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 Esfahbod0b7e4d92011-08-15 20:41:59 +020056 * - Rounding, etc?
57 *
Behdad Esfahbod744970a2011-05-16 18:15:37 -040058 * - In the future, we should add constructors to create fonts in font space.
59 *
60 * - I believe transforms are not correctly implemented. FreeType does not
61 * provide any API to get to the transform/delta set on the face. :(
62 *
63 * - Always use FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH?
Behdad Esfahbod323190c2012-04-12 12:29:10 -040064 *
65 * - 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
80#ifdef HAVE_FT_FACE_GETCHARVARIANTINDEX
81 if (unlikely (variation_selector)) {
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -040082 *glyph = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector);
83 if (*glyph)
84 return TRUE;
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040085 }
86#endif
87
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -040088 *glyph = FT_Get_Char_Index (ft_face, unicode);
89 return *glyph != 0;
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040090}
91
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -040092static hb_position_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -040093hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
94 void *font_data,
95 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -040096 void *user_data HB_UNUSED)
97{
98 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +020099 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
100 FT_Fixed v;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400101
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200102 if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v)))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400103 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400104
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200105 return v >> 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 Esfahbod8b38fae2011-05-19 13:08:00 -0400121 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
122 * have a Y growing upward. Hence the extra negation. */
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200123 return -v >> 10;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400124}
125
126static hb_bool_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400127hb_ft_get_glyph_h_origin (hb_font_t *font HB_UNUSED,
128 void *font_data HB_UNUSED,
129 hb_codepoint_t glyph HB_UNUSED,
Behdad Esfahbod19098182011-05-17 23:27:22 -0400130 hb_position_t *x HB_UNUSED,
131 hb_position_t *y HB_UNUSED,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400132 void *user_data HB_UNUSED)
133{
134 /* We always work in the horizontal coordinates. */
135 return TRUE;
136}
137
138static hb_bool_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400139hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
140 void *font_data,
141 hb_codepoint_t glyph,
Behdad Esfahbod19098182011-05-17 23:27:22 -0400142 hb_position_t *x,
143 hb_position_t *y,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400144 void *user_data HB_UNUSED)
145{
146 FT_Face ft_face = (FT_Face) font_data;
147 int load_flags = FT_LOAD_DEFAULT;
148
149 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
150 return FALSE;
151
Behdad Esfahbod8b38fae2011-05-19 13:08:00 -0400152 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
153 * have a Y growing upward. Hence the extra negation. */
154 *x = ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBearingX;
155 *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);
156
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400157 return TRUE;
158}
159
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400160static hb_position_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400161hb_ft_get_glyph_h_kerning (hb_font_t *font HB_UNUSED,
162 void *font_data,
163 hb_codepoint_t left_glyph,
164 hb_codepoint_t right_glyph,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400165 void *user_data HB_UNUSED)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500166{
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -0400167 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod60fbb362011-05-19 18:46:15 -0400168 FT_Vector kerningv;
Behdad Esfahbod3b593062009-11-04 15:48:32 -0500169
Behdad Esfahbod60fbb362011-05-19 18:46:15 -0400170 if (FT_Get_Kerning (ft_face, left_glyph, right_glyph, FT_KERNING_DEFAULT, &kerningv))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400171 return 0;
Behdad Esfahbod3b593062009-11-04 15:48:32 -0500172
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400173 return kerningv.x;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400174}
175
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400176static hb_position_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400177hb_ft_get_glyph_v_kerning (hb_font_t *font HB_UNUSED,
178 void *font_data HB_UNUSED,
179 hb_codepoint_t top_glyph HB_UNUSED,
180 hb_codepoint_t bottom_glyph HB_UNUSED,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400181 void *user_data HB_UNUSED)
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400182{
183 /* FreeType API doesn't support vertical kerning */
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400184 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400185}
186
187static hb_bool_t
188hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
189 void *font_data,
190 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400191 hb_glyph_extents_t *extents,
192 void *user_data HB_UNUSED)
193{
194 FT_Face ft_face = (FT_Face) font_data;
195 int load_flags = FT_LOAD_DEFAULT;
196
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400197 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
198 return FALSE;
199
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400200 extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
201 extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
202 extents->width = ft_face->glyph->metrics.width;
203 extents->height = ft_face->glyph->metrics.height;
204 return TRUE;
205}
206
207static hb_bool_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400208hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
209 void *font_data,
210 hb_codepoint_t glyph,
211 unsigned int point_index,
212 hb_position_t *x,
213 hb_position_t *y,
214 void *user_data HB_UNUSED)
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400215{
216 FT_Face ft_face = (FT_Face) font_data;
217 int load_flags = FT_LOAD_DEFAULT;
218
219 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
220 return FALSE;
221
222 if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
223 return FALSE;
224
225 if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
226 return FALSE;
227
228 *x = ft_face->glyph->outline.points[point_index].x;
229 *y = ft_face->glyph->outline.points[point_index].y;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400230
231 return TRUE;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500232}
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500233
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400234static hb_bool_t
235hb_ft_get_glyph_name (hb_font_t *font,
236 void *font_data,
237 hb_codepoint_t glyph,
238 char *name, unsigned int size,
239 void *user_data HB_UNUSED)
240{
241 FT_Face ft_face = (FT_Face) font_data;
242
243 hb_bool_t ret = !FT_Get_Glyph_Name (ft_face, glyph, name, size);
244 if (!ret)
245 snprintf (name, size, "gid%u", glyph);
246
247 return ret;
248}
249
250static hb_bool_t
251hb_ft_get_glyph_from_name (hb_font_t *font,
252 void *font_data,
253 const char *name, int len, /* -1 means nul-terminated */
254 hb_codepoint_t *glyph,
255 void *user_data HB_UNUSED)
256{
257 FT_Face ft_face = (FT_Face) font_data;
258
259 if (len < 0)
260 *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
261 else {
262 /* Make a nul-terminated version. */
263 char buf[128];
264 len = MIN (len, (int) sizeof (buf) - 1);
265 strncpy (buf, name, len);
266 buf[len] = '\0';
267 *glyph = FT_Get_Name_Index (ft_face, buf);
268 }
269
270 return *glyph != 0;
271}
272
273
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200274static hb_font_funcs_t *
275_hb_ft_get_font_funcs (void)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500276{
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400277 static const hb_font_funcs_t ft_ffuncs = {
278 HB_OBJECT_HEADER_STATIC,
279
280 TRUE, /* immutable */
281
282 {
283#define HB_FONT_FUNC_IMPLEMENT(name) hb_ft_get_##name,
284 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
285#undef HB_FONT_FUNC_IMPLEMENT
286 }
287 };
288
289 return const_cast<hb_font_funcs_t *> (&ft_ffuncs);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500290}
291
292
293static hb_blob_t *
Behdad Esfahbode7157842011-08-08 21:42:02 +0200294reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500295{
296 FT_Face ft_face = (FT_Face) user_data;
297 FT_Byte *buffer;
298 FT_ULong length = 0;
299 FT_Error error;
300
Behdad Esfahbod38973352011-08-08 23:37:41 +0200301 /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob */
Behdad Esfahbod2cd1ea42010-04-29 14:15:32 -0400302
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500303 error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
304 if (error)
Behdad Esfahbod750a2292010-05-20 16:23:27 +0100305 return NULL;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500306
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400307 buffer = (FT_Byte *) malloc (length);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500308 if (buffer == NULL)
Behdad Esfahbodd6b3c832010-04-23 19:59:53 -0400309 return NULL;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500310
311 error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
312 if (error)
Behdad Esfahbod750a2292010-05-20 16:23:27 +0100313 return NULL;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500314
315 return hb_blob_create ((const char *) buffer, length,
316 HB_MEMORY_MODE_WRITABLE,
Behdad Esfahbod56681892011-04-20 03:03:32 -0400317 buffer, free);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500318}
319
320
321hb_face_t *
322hb_ft_face_create (FT_Face ft_face,
323 hb_destroy_func_t destroy)
324{
325 hb_face_t *face;
326
Behdad Esfahbode48ed722010-03-01 22:33:45 -0500327 if (ft_face->stream->read == NULL) {
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500328 hb_blob_t *blob;
329
330 blob = hb_blob_create ((const char *) ft_face->stream->base,
331 (unsigned int) ft_face->stream->size,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400332 /* TODO: We assume that it's mmap()'ed, but FreeType code
333 * suggests that there are cases we reach here but font is
334 * not mmapped. For example, when mmap() fails. No idea
335 * how to deal with it better here. */
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500336 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE,
Behdad Esfahbod56681892011-04-20 03:03:32 -0400337 ft_face, destroy);
Behdad Esfahbod9a146882011-05-11 22:49:29 -0400338 face = hb_face_create (blob, ft_face->face_index);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500339 hb_blob_destroy (blob);
340 } else {
Behdad Esfahbode7157842011-08-08 21:42:02 +0200341 face = hb_face_create_for_tables (reference_table, ft_face, destroy);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500342 }
343
Behdad Esfahbodde1e1cf2011-08-09 00:19:38 +0200344 hb_face_set_index (face, ft_face->face_index);
345 hb_face_set_upem (face, ft_face->units_per_EM);
346
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500347 return face;
348}
349
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500350static void
351hb_ft_face_finalize (FT_Face ft_face)
352{
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400353 hb_face_destroy ((hb_face_t *) ft_face->generic.data);
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500354}
355
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500356hb_face_t *
357hb_ft_face_create_cached (FT_Face ft_face)
358{
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400359 if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500360 {
361 if (ft_face->generic.finalizer)
Behdad Esfahbodedb54e92009-11-06 15:19:22 -0500362 ft_face->generic.finalizer (ft_face);
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500363
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500364 ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500365 ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500366 }
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500367
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400368 return hb_face_reference ((hb_face_t *) ft_face->generic.data);
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500369}
370
Behdad Esfahbod553bc3d2011-08-15 16:21:06 +0200371static void
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200372_do_nothing (void)
373{
374}
375
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500376
377hb_font_t *
378hb_ft_font_create (FT_Face ft_face,
379 hb_destroy_func_t destroy)
380{
381 hb_font_t *font;
Behdad Esfahbodd2928852011-05-03 01:03:53 -0400382 hb_face_t *face;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500383
Behdad Esfahbodd2928852011-05-03 01:03:53 -0400384 face = hb_ft_face_create (ft_face, destroy);
385 font = hb_font_create (face);
386 hb_face_destroy (face);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500387 hb_font_set_funcs (font,
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200388 _hb_ft_get_font_funcs (),
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200389 ft_face, (hb_destroy_func_t) _do_nothing);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500390 hb_font_set_scale (font,
Behdad Esfahbod0a4399c2010-05-19 15:45:06 -0400391 ((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM) >> 16,
392 ((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM) >> 16);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500393 hb_font_set_ppem (font,
394 ft_face->size->metrics.x_ppem,
395 ft_face->size->metrics.y_ppem);
396
397 return font;
398}
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400399
400
Behdad Esfahbodf64b2eb2012-06-05 19:23:29 -0400401/* Thread-safe, lock-free, FT_Library */
402
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400403static FT_Library ft_library;
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200404
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400405static
406void free_ft_library (void)
407{
408 FT_Done_FreeType (ft_library);
409}
410
411static FT_Library
412get_ft_library (void)
413{
414retry:
415 FT_Library library = (FT_Library) hb_atomic_ptr_get (&ft_library);
416
417 if (unlikely (!library))
418 {
419 /* Not found; allocate one. */
420 if (FT_Init_FreeType (&library))
421 return NULL;
422
423 if (!hb_atomic_ptr_cmpexch (&ft_library, NULL, library)) {
424 FT_Done_FreeType (library);
425 goto retry;
426 }
427
428#ifdef HAVE_ATEXIT
429 atexit (free_ft_library); /* First person registers atexit() callback. */
430#endif
431 }
432
433 return library;
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200434}
435
436static void
437_release_blob (FT_Face ft_face)
438{
439 hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
440}
441
442void
443hb_ft_font_set_funcs (hb_font_t *font)
444{
445 hb_blob_t *blob = hb_face_reference_blob (font->face);
446 unsigned int blob_length;
447 const char *blob_data = hb_blob_get_data (blob, &blob_length);
448 if (unlikely (!blob_length))
449 DEBUG_MSG (FT, font, "Font face has empty blob");
450
451 FT_Face ft_face = NULL;
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400452 FT_Error err = FT_New_Memory_Face (get_ft_library (),
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200453 (const FT_Byte *) blob_data,
454 blob_length,
455 hb_face_get_index (font->face),
456 &ft_face);
457
458 if (unlikely (err)) {
459 hb_blob_destroy (blob);
460 DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed");
461 return;
462 }
463
Behdad Esfahbod254142b2011-08-15 16:15:44 +0200464 FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE);
465
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200466 FT_Set_Char_Size (ft_face,
467 font->x_scale, font->y_scale,
468 font->x_ppem * 72 * 64 / font->x_scale,
469 font->y_ppem * 72 * 64 / font->y_scale);
470
471 ft_face->generic.data = blob;
472 ft_face->generic.finalizer = (FT_Generic_Finalizer) _release_blob;
473
474 hb_font_set_funcs (font,
475 _hb_ft_get_font_funcs (),
476 ft_face,
477 (hb_destroy_func_t) FT_Done_Face);
478}
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200479
480FT_Face
481hb_ft_font_get_face (hb_font_t *font)
482{
483 if (font->destroy == (hb_destroy_func_t) FT_Done_Face ||
484 font->destroy == (hb_destroy_func_t) _do_nothing)
485 return (FT_Face) font->user_data;
Behdad Esfahbod9527fb22011-08-13 19:03:48 +0200486
487 return NULL;
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200488}