blob: 23c2cc0324ec7766b43f926db1e9d7b963e6d43e [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?
64 */
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -050065
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040066
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -040067static hb_bool_t
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040068hb_ft_get_glyph (hb_font_t *font HB_UNUSED,
Behdad Esfahbodb4678272011-05-11 23:25:28 -040069 void *font_data,
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040070 hb_codepoint_t unicode,
71 hb_codepoint_t variation_selector,
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -040072 hb_codepoint_t *glyph,
Behdad Esfahbodb4678272011-05-11 23:25:28 -040073 void *user_data HB_UNUSED)
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040074
75{
76 FT_Face ft_face = (FT_Face) font_data;
77
78#ifdef HAVE_FT_FACE_GETCHARVARIANTINDEX
79 if (unlikely (variation_selector)) {
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -040080 *glyph = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector);
81 if (*glyph)
82 return TRUE;
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040083 }
84#endif
85
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -040086 *glyph = FT_Get_Char_Index (ft_face, unicode);
87 return *glyph != 0;
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040088}
89
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -040090static hb_position_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -040091hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
92 void *font_data,
93 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -040094 void *user_data HB_UNUSED)
95{
96 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +020097 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
98 FT_Fixed v;
Behdad Esfahbod744970a2011-05-16 18:15:37 -040099
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200100 if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v)))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400101 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400102
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200103 return v >> 10;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400104}
105
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400106static hb_position_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400107hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
108 void *font_data,
109 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400110 void *user_data HB_UNUSED)
111{
112 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200113 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING | FT_LOAD_VERTICAL_LAYOUT;
114 FT_Fixed v;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400115
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200116 if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v)))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400117 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400118
Behdad Esfahbod8b38fae2011-05-19 13:08:00 -0400119 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
120 * have a Y growing upward. Hence the extra negation. */
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200121 return -v >> 10;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400122}
123
124static hb_bool_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400125hb_ft_get_glyph_h_origin (hb_font_t *font HB_UNUSED,
126 void *font_data HB_UNUSED,
127 hb_codepoint_t glyph HB_UNUSED,
Behdad Esfahbod19098182011-05-17 23:27:22 -0400128 hb_position_t *x HB_UNUSED,
129 hb_position_t *y HB_UNUSED,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400130 void *user_data HB_UNUSED)
131{
132 /* We always work in the horizontal coordinates. */
133 return TRUE;
134}
135
136static hb_bool_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400137hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
138 void *font_data,
139 hb_codepoint_t glyph,
Behdad Esfahbod19098182011-05-17 23:27:22 -0400140 hb_position_t *x,
141 hb_position_t *y,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400142 void *user_data HB_UNUSED)
143{
144 FT_Face ft_face = (FT_Face) font_data;
145 int load_flags = FT_LOAD_DEFAULT;
146
147 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
148 return FALSE;
149
Behdad Esfahbod8b38fae2011-05-19 13:08:00 -0400150 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
151 * have a Y growing upward. Hence the extra negation. */
152 *x = ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBearingX;
153 *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);
154
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400155 return TRUE;
156}
157
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400158static hb_position_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400159hb_ft_get_glyph_h_kerning (hb_font_t *font HB_UNUSED,
160 void *font_data,
161 hb_codepoint_t left_glyph,
162 hb_codepoint_t right_glyph,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400163 void *user_data HB_UNUSED)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500164{
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -0400165 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod60fbb362011-05-19 18:46:15 -0400166 FT_Vector kerningv;
Behdad Esfahbod3b593062009-11-04 15:48:32 -0500167
Behdad Esfahbod60fbb362011-05-19 18:46:15 -0400168 if (FT_Get_Kerning (ft_face, left_glyph, right_glyph, FT_KERNING_DEFAULT, &kerningv))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400169 return 0;
Behdad Esfahbod3b593062009-11-04 15:48:32 -0500170
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400171 return kerningv.x;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400172}
173
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400174static hb_position_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400175hb_ft_get_glyph_v_kerning (hb_font_t *font HB_UNUSED,
176 void *font_data HB_UNUSED,
177 hb_codepoint_t top_glyph HB_UNUSED,
178 hb_codepoint_t bottom_glyph HB_UNUSED,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400179 void *user_data HB_UNUSED)
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400180{
181 /* FreeType API doesn't support vertical kerning */
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400182 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400183}
184
185static hb_bool_t
186hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
187 void *font_data,
188 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400189 hb_glyph_extents_t *extents,
190 void *user_data HB_UNUSED)
191{
192 FT_Face ft_face = (FT_Face) font_data;
193 int load_flags = FT_LOAD_DEFAULT;
194
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400195 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
196 return FALSE;
197
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400198 extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
199 extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
200 extents->width = ft_face->glyph->metrics.width;
201 extents->height = ft_face->glyph->metrics.height;
202 return TRUE;
203}
204
205static hb_bool_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400206hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
207 void *font_data,
208 hb_codepoint_t glyph,
209 unsigned int point_index,
210 hb_position_t *x,
211 hb_position_t *y,
212 void *user_data HB_UNUSED)
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400213{
214 FT_Face ft_face = (FT_Face) font_data;
215 int load_flags = FT_LOAD_DEFAULT;
216
217 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
218 return FALSE;
219
220 if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
221 return FALSE;
222
223 if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
224 return FALSE;
225
226 *x = ft_face->glyph->outline.points[point_index].x;
227 *y = ft_face->glyph->outline.points[point_index].y;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400228
229 return TRUE;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500230}
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500231
232static hb_font_funcs_t ft_ffuncs = {
Behdad Esfahbodfca368c2011-04-21 18:24:02 -0400233 HB_OBJECT_HEADER_STATIC,
234
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500235 TRUE, /* immutable */
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -0400236
Behdad Esfahbodc4426722010-05-24 18:02:32 +0100237 {
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -0400238 hb_ft_get_glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400239 hb_ft_get_glyph_h_advance,
240 hb_ft_get_glyph_v_advance,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400241 hb_ft_get_glyph_h_origin,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400242 hb_ft_get_glyph_v_origin,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400243 hb_ft_get_glyph_h_kerning,
244 hb_ft_get_glyph_v_kerning,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400245 hb_ft_get_glyph_extents,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400246 hb_ft_get_glyph_contour_point,
Behdad Esfahbodc4426722010-05-24 18:02:32 +0100247 }
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500248};
249
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200250static hb_font_funcs_t *
251_hb_ft_get_font_funcs (void)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500252{
253 return &ft_ffuncs;
254}
255
256
257static hb_blob_t *
Behdad Esfahbode7157842011-08-08 21:42:02 +0200258reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500259{
260 FT_Face ft_face = (FT_Face) user_data;
261 FT_Byte *buffer;
262 FT_ULong length = 0;
263 FT_Error error;
264
Behdad Esfahbod38973352011-08-08 23:37:41 +0200265 /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob */
Behdad Esfahbod2cd1ea42010-04-29 14:15:32 -0400266
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500267 error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
268 if (error)
Behdad Esfahbod750a2292010-05-20 16:23:27 +0100269 return NULL;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500270
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400271 buffer = (FT_Byte *) malloc (length);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500272 if (buffer == NULL)
Behdad Esfahbodd6b3c832010-04-23 19:59:53 -0400273 return NULL;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500274
275 error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
276 if (error)
Behdad Esfahbod750a2292010-05-20 16:23:27 +0100277 return NULL;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500278
279 return hb_blob_create ((const char *) buffer, length,
280 HB_MEMORY_MODE_WRITABLE,
Behdad Esfahbod56681892011-04-20 03:03:32 -0400281 buffer, free);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500282}
283
284
285hb_face_t *
286hb_ft_face_create (FT_Face ft_face,
287 hb_destroy_func_t destroy)
288{
289 hb_face_t *face;
290
Behdad Esfahbode48ed722010-03-01 22:33:45 -0500291 if (ft_face->stream->read == NULL) {
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500292 hb_blob_t *blob;
293
294 blob = hb_blob_create ((const char *) ft_face->stream->base,
295 (unsigned int) ft_face->stream->size,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400296 /* TODO: We assume that it's mmap()'ed, but FreeType code
297 * suggests that there are cases we reach here but font is
298 * not mmapped. For example, when mmap() fails. No idea
299 * how to deal with it better here. */
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500300 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE,
Behdad Esfahbod56681892011-04-20 03:03:32 -0400301 ft_face, destroy);
Behdad Esfahbod9a146882011-05-11 22:49:29 -0400302 face = hb_face_create (blob, ft_face->face_index);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500303 hb_blob_destroy (blob);
304 } else {
Behdad Esfahbode7157842011-08-08 21:42:02 +0200305 face = hb_face_create_for_tables (reference_table, ft_face, destroy);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500306 }
307
Behdad Esfahbodde1e1cf2011-08-09 00:19:38 +0200308 hb_face_set_index (face, ft_face->face_index);
309 hb_face_set_upem (face, ft_face->units_per_EM);
310
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500311 return face;
312}
313
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500314static void
315hb_ft_face_finalize (FT_Face ft_face)
316{
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400317 hb_face_destroy ((hb_face_t *) ft_face->generic.data);
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500318}
319
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500320hb_face_t *
321hb_ft_face_create_cached (FT_Face ft_face)
322{
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400323 if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500324 {
325 if (ft_face->generic.finalizer)
Behdad Esfahbodedb54e92009-11-06 15:19:22 -0500326 ft_face->generic.finalizer (ft_face);
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500327
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500328 ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500329 ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500330 }
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500331
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400332 return hb_face_reference ((hb_face_t *) ft_face->generic.data);
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500333}
334
Behdad Esfahbod553bc3d2011-08-15 16:21:06 +0200335static void
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200336_do_nothing (void)
337{
338}
339
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500340
341hb_font_t *
342hb_ft_font_create (FT_Face ft_face,
343 hb_destroy_func_t destroy)
344{
345 hb_font_t *font;
Behdad Esfahbodd2928852011-05-03 01:03:53 -0400346 hb_face_t *face;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500347
Behdad Esfahbodd2928852011-05-03 01:03:53 -0400348 face = hb_ft_face_create (ft_face, destroy);
349 font = hb_font_create (face);
350 hb_face_destroy (face);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500351 hb_font_set_funcs (font,
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200352 _hb_ft_get_font_funcs (),
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200353 ft_face, (hb_destroy_func_t) _do_nothing);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500354 hb_font_set_scale (font,
Behdad Esfahbod0a4399c2010-05-19 15:45:06 -0400355 ((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM) >> 16,
356 ((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM) >> 16);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500357 hb_font_set_ppem (font,
358 ft_face->size->metrics.x_ppem,
359 ft_face->size->metrics.y_ppem);
360
361 return font;
362}
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400363
364
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200365
366
367static FT_Library ft_library;
368static hb_bool_t ft_library_initialized;
369static struct ft_library_destructor {
370 ~ft_library_destructor (void) {
371 if (ft_library)
372 FT_Done_FreeType (ft_library);
373 }
374} static_ft_library_destructor;
375
376static FT_Library
377_get_ft_library (void)
378{
379 if (unlikely (!ft_library_initialized)) {
380 FT_Init_FreeType (&ft_library);
381 ft_library_initialized = TRUE;
382 }
383
384 return ft_library;
385}
386
387static void
388_release_blob (FT_Face ft_face)
389{
390 hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
391}
392
393void
394hb_ft_font_set_funcs (hb_font_t *font)
395{
396 hb_blob_t *blob = hb_face_reference_blob (font->face);
397 unsigned int blob_length;
398 const char *blob_data = hb_blob_get_data (blob, &blob_length);
399 if (unlikely (!blob_length))
400 DEBUG_MSG (FT, font, "Font face has empty blob");
401
402 FT_Face ft_face = NULL;
403 FT_Error err = FT_New_Memory_Face (_get_ft_library (),
404 (const FT_Byte *) blob_data,
405 blob_length,
406 hb_face_get_index (font->face),
407 &ft_face);
408
409 if (unlikely (err)) {
410 hb_blob_destroy (blob);
411 DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed");
412 return;
413 }
414
Behdad Esfahbod254142b2011-08-15 16:15:44 +0200415 FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE);
416
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200417 FT_Set_Char_Size (ft_face,
418 font->x_scale, font->y_scale,
419 font->x_ppem * 72 * 64 / font->x_scale,
420 font->y_ppem * 72 * 64 / font->y_scale);
421
422 ft_face->generic.data = blob;
423 ft_face->generic.finalizer = (FT_Generic_Finalizer) _release_blob;
424
425 hb_font_set_funcs (font,
426 _hb_ft_get_font_funcs (),
427 ft_face,
428 (hb_destroy_func_t) FT_Done_Face);
429}
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200430
431FT_Face
432hb_ft_font_get_face (hb_font_t *font)
433{
434 if (font->destroy == (hb_destroy_func_t) FT_Done_Face ||
435 font->destroy == (hb_destroy_func_t) _do_nothing)
436 return (FT_Face) font->user_data;
Behdad Esfahbod9527fb22011-08-13 19:03:48 +0200437
438 return NULL;
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200439}