blob: 9b2a908445f8ee71bdfe08d2bef96b7134bb8840 [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
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040073 if (unlikely (variation_selector)) {
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -040074 *glyph = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector);
Behdad Esfahbod79d10072013-06-13 19:01:07 -040075 return *glyph != 0;
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040076 }
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040077
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -040078 *glyph = FT_Get_Char_Index (ft_face, unicode);
79 return *glyph != 0;
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -040080}
81
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -040082static hb_position_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -040083hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
84 void *font_data,
85 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -040086 void *user_data HB_UNUSED)
87{
88 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +020089 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
90 FT_Fixed v;
Behdad Esfahbod744970a2011-05-16 18:15:37 -040091
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +020092 if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v)))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -040093 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -040094
Behdad Esfahbod755b44c2013-10-18 11:17:42 +020095 return (v + (1<<9)) >> 10;
Behdad Esfahbod744970a2011-05-16 18:15:37 -040096}
97
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -040098static hb_position_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -040099hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
100 void *font_data,
101 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400102 void *user_data HB_UNUSED)
103{
104 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200105 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING | FT_LOAD_VERTICAL_LAYOUT;
106 FT_Fixed v;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400107
Behdad Esfahbod0b7e4d92011-08-15 20:41:59 +0200108 if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v)))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400109 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400110
Behdad Esfahbod8b38fae2011-05-19 13:08:00 -0400111 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
112 * have a Y growing upward. Hence the extra negation. */
Behdad Esfahbod755b44c2013-10-18 11:17:42 +0200113 return (-v + (1<<9)) >> 10;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400114}
115
116static hb_bool_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400117hb_ft_get_glyph_h_origin (hb_font_t *font HB_UNUSED,
118 void *font_data HB_UNUSED,
119 hb_codepoint_t glyph HB_UNUSED,
Behdad Esfahbod19098182011-05-17 23:27:22 -0400120 hb_position_t *x HB_UNUSED,
121 hb_position_t *y HB_UNUSED,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400122 void *user_data HB_UNUSED)
123{
124 /* We always work in the horizontal coordinates. */
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400125 return true;
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400126}
127
128static hb_bool_t
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400129hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
130 void *font_data,
131 hb_codepoint_t glyph,
Behdad Esfahbod19098182011-05-17 23:27:22 -0400132 hb_position_t *x,
133 hb_position_t *y,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400134 void *user_data HB_UNUSED)
135{
136 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod8afaf092014-10-02 16:40:41 -0400137 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400138
139 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400140 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400141
Behdad Esfahbod8b38fae2011-05-19 13:08:00 -0400142 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
143 * have a Y growing upward. Hence the extra negation. */
144 *x = ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBearingX;
145 *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);
146
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400147 return true;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400148}
149
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400150static hb_position_t
Behdad Esfahbodcdf74442012-07-11 18:52:39 -0400151hb_ft_get_glyph_h_kerning (hb_font_t *font,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400152 void *font_data,
153 hb_codepoint_t left_glyph,
154 hb_codepoint_t right_glyph,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400155 void *user_data HB_UNUSED)
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500156{
Behdad Esfahbodb9d975b2011-05-10 20:41:13 -0400157 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod60fbb362011-05-19 18:46:15 -0400158 FT_Vector kerningv;
Behdad Esfahbod3b593062009-11-04 15:48:32 -0500159
Behdad Esfahbodcdf74442012-07-11 18:52:39 -0400160 FT_Kerning_Mode mode = font->x_ppem ? FT_KERNING_DEFAULT : FT_KERNING_UNFITTED;
161 if (FT_Get_Kerning (ft_face, left_glyph, right_glyph, mode, &kerningv))
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400162 return 0;
Behdad Esfahbod3b593062009-11-04 15:48:32 -0500163
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400164 return kerningv.x;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400165}
166
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400167static hb_position_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400168hb_ft_get_glyph_v_kerning (hb_font_t *font HB_UNUSED,
169 void *font_data HB_UNUSED,
170 hb_codepoint_t top_glyph HB_UNUSED,
171 hb_codepoint_t bottom_glyph HB_UNUSED,
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400172 void *user_data HB_UNUSED)
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400173{
174 /* FreeType API doesn't support vertical kerning */
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400175 return 0;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400176}
177
178static hb_bool_t
179hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
180 void *font_data,
181 hb_codepoint_t glyph,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400182 hb_glyph_extents_t *extents,
183 void *user_data HB_UNUSED)
184{
185 FT_Face ft_face = (FT_Face) font_data;
Behdad Esfahbod8afaf092014-10-02 16:40:41 -0400186 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400187
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400188 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400189 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400190
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400191 extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
192 extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
193 extents->width = ft_face->glyph->metrics.width;
Behdad Esfahbod21756932012-08-08 01:20:45 -0400194 extents->height = -ft_face->glyph->metrics.height;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400195 return true;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400196}
197
198static hb_bool_t
Behdad Esfahbod7e2c85d2011-05-17 17:55:03 -0400199hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
200 void *font_data,
201 hb_codepoint_t glyph,
202 unsigned int point_index,
203 hb_position_t *x,
204 hb_position_t *y,
205 void *user_data HB_UNUSED)
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400206{
207 FT_Face ft_face = (FT_Face) font_data;
208 int load_flags = FT_LOAD_DEFAULT;
209
210 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400211 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400212
213 if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400214 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400215
216 if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400217 return false;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400218
219 *x = ft_face->glyph->outline.points[point_index].x;
220 *y = ft_face->glyph->outline.points[point_index].y;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400221
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400222 return true;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500223}
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500224
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400225static hb_bool_t
Behdad Esfahbod271c8f82012-07-13 09:32:30 -0400226hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED,
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400227 void *font_data,
228 hb_codepoint_t glyph,
229 char *name, unsigned int size,
230 void *user_data HB_UNUSED)
231{
232 FT_Face ft_face = (FT_Face) font_data;
233
234 hb_bool_t ret = !FT_Get_Glyph_Name (ft_face, glyph, name, size);
Behdad Esfahbod5594c2d2013-03-06 19:37:31 -0500235 if (ret && (size && !*name))
236 ret = false;
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400237
238 return ret;
239}
240
241static hb_bool_t
Behdad Esfahbod271c8f82012-07-13 09:32:30 -0400242hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400243 void *font_data,
244 const char *name, int len, /* -1 means nul-terminated */
245 hb_codepoint_t *glyph,
246 void *user_data HB_UNUSED)
247{
248 FT_Face ft_face = (FT_Face) font_data;
249
250 if (len < 0)
251 *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
252 else {
253 /* Make a nul-terminated version. */
254 char buf[128];
255 len = MIN (len, (int) sizeof (buf) - 1);
256 strncpy (buf, name, len);
257 buf[len] = '\0';
258 *glyph = FT_Get_Name_Index (ft_face, buf);
259 }
260
Behdad Esfahbode509d352013-07-11 14:56:45 -0400261 if (*glyph == 0)
262 {
263 /* Check whether the given name was actually the name of glyph 0. */
264 char buf[128];
265 if (!FT_Get_Glyph_Name(ft_face, 0, buf, sizeof (buf)) &&
266 len < 0 ? !strcmp (buf, name) : !strncmp (buf, name, len))
267 return true;
268 }
269
Behdad Esfahbodbce09552012-05-27 11:29:21 -0400270 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
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400280 true, /* immutable */
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400281
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
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400320/**
321 * hb_ft_face_create:
322 * @ft_face: (destroy destroy) (scope notified):
323 * @destroy:
324 *
325 *
326 *
327 * Return value: (transfer full):
328 * Since: 1.0
329 **/
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500330hb_face_t *
331hb_ft_face_create (FT_Face ft_face,
332 hb_destroy_func_t destroy)
333{
334 hb_face_t *face;
335
Behdad Esfahbode48ed722010-03-01 22:33:45 -0500336 if (ft_face->stream->read == NULL) {
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500337 hb_blob_t *blob;
338
339 blob = hb_blob_create ((const char *) ft_face->stream->base,
340 (unsigned int) ft_face->stream->size,
Behdad Esfahbodaffacf22014-12-28 16:20:31 -0800341 HB_MEMORY_MODE_READONLY,
Behdad Esfahbod56681892011-04-20 03:03:32 -0400342 ft_face, destroy);
Behdad Esfahbod9a146882011-05-11 22:49:29 -0400343 face = hb_face_create (blob, ft_face->face_index);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500344 hb_blob_destroy (blob);
345 } else {
Behdad Esfahbode7157842011-08-08 21:42:02 +0200346 face = hb_face_create_for_tables (reference_table, ft_face, destroy);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500347 }
348
Behdad Esfahbodde1e1cf2011-08-09 00:19:38 +0200349 hb_face_set_index (face, ft_face->face_index);
350 hb_face_set_upem (face, ft_face->units_per_EM);
351
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500352 return face;
353}
354
Behdad Esfahbod350f3a02014-12-28 17:44:26 -0800355/**
356 * hb_ft_face_create_referenced:
357 * @ft_face:
358 *
359 *
360 *
361 * Return value: (transfer full):
362 * Since: 1.0
363 **/
364hb_face_t *
365hb_ft_face_create_referenced (FT_Face ft_face)
366{
367 FT_Reference_Face (ft_face);
368 return hb_ft_face_create (ft_face, (hb_destroy_func_t) FT_Done_Face);
369}
370
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500371static void
372hb_ft_face_finalize (FT_Face ft_face)
373{
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400374 hb_face_destroy ((hb_face_t *) ft_face->generic.data);
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500375}
376
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400377/**
378 * hb_ft_face_create_cached:
379 * @ft_face:
380 *
381 *
382 *
383 * Return value: (transfer full):
384 * Since: 1.0
385 **/
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500386hb_face_t *
387hb_ft_face_create_cached (FT_Face ft_face)
388{
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400389 if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500390 {
391 if (ft_face->generic.finalizer)
Behdad Esfahbodedb54e92009-11-06 15:19:22 -0500392 ft_face->generic.finalizer (ft_face);
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500393
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500394 ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
Behdad Esfahbod3648bdf2009-11-05 20:17:53 -0500395 ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
Behdad Esfahbodf4281e02009-11-05 17:58:41 -0500396 }
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500397
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -0400398 return hb_face_reference ((hb_face_t *) ft_face->generic.data);
Behdad Esfahbod6358ff42009-11-05 17:39:16 -0500399}
400
Behdad Esfahbod553bc3d2011-08-15 16:21:06 +0200401static void
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200402_do_nothing (void)
403{
404}
405
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500406
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -0400407/**
408 * hb_ft_font_create:
409 * @ft_face: (destroy destroy) (scope notified):
410 * @destroy:
411 *
412 *
413 *
414 * Return value: (transfer full):
415 * Since: 1.0
416 **/
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500417hb_font_t *
418hb_ft_font_create (FT_Face ft_face,
419 hb_destroy_func_t destroy)
420{
421 hb_font_t *font;
Behdad Esfahbodd2928852011-05-03 01:03:53 -0400422 hb_face_t *face;
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500423
Behdad Esfahbodd2928852011-05-03 01:03:53 -0400424 face = hb_ft_face_create (ft_face, destroy);
425 font = hb_font_create (face);
426 hb_face_destroy (face);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500427 hb_font_set_funcs (font,
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200428 _hb_ft_get_font_funcs (),
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200429 ft_face, (hb_destroy_func_t) _do_nothing);
Behdad Esfahbod8fb3d1a2009-11-03 18:34:20 -0500430 hb_font_set_scale (font,
Behdad Esfahbod83408cf2013-11-06 14:46:04 -0500431 (int) (((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM + (1<<15)) >> 16),
432 (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 -0500433 hb_font_set_ppem (font,
434 ft_face->size->metrics.x_ppem,
435 ft_face->size->metrics.y_ppem);
436
437 return font;
438}
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400439
Behdad Esfahbod350f3a02014-12-28 17:44:26 -0800440/**
441 * hb_ft_font_create_referenced:
442 * @ft_face:
443 *
444 *
445 *
446 * Return value: (transfer full):
447 * Since: 1.0
448 **/
449hb_font_t *
450hb_ft_font_create_referenced (FT_Face ft_face)
451{
452 FT_Reference_Face (ft_face);
453 return hb_ft_font_create (ft_face, (hb_destroy_func_t) FT_Done_Face);
454}
455
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400456
Behdad Esfahbodf64b2eb2012-06-05 19:23:29 -0400457/* Thread-safe, lock-free, FT_Library */
458
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400459static FT_Library ft_library;
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200460
Behdad Esfahbode9171af2013-01-29 22:45:00 -0500461static inline
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400462void free_ft_library (void)
463{
464 FT_Done_FreeType (ft_library);
465}
466
467static FT_Library
468get_ft_library (void)
469{
470retry:
471 FT_Library library = (FT_Library) hb_atomic_ptr_get (&ft_library);
472
473 if (unlikely (!library))
474 {
475 /* Not found; allocate one. */
476 if (FT_Init_FreeType (&library))
477 return NULL;
478
479 if (!hb_atomic_ptr_cmpexch (&ft_library, NULL, library)) {
480 FT_Done_FreeType (library);
481 goto retry;
482 }
483
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400484#ifdef HB_USE_ATEXIT
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400485 atexit (free_ft_library); /* First person registers atexit() callback. */
486#endif
487 }
488
489 return library;
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200490}
491
492static void
493_release_blob (FT_Face ft_face)
494{
495 hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
496}
497
498void
499hb_ft_font_set_funcs (hb_font_t *font)
500{
501 hb_blob_t *blob = hb_face_reference_blob (font->face);
502 unsigned int blob_length;
503 const char *blob_data = hb_blob_get_data (blob, &blob_length);
504 if (unlikely (!blob_length))
505 DEBUG_MSG (FT, font, "Font face has empty blob");
506
507 FT_Face ft_face = NULL;
Behdad Esfahbod04aed572012-06-05 18:30:19 -0400508 FT_Error err = FT_New_Memory_Face (get_ft_library (),
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200509 (const FT_Byte *) blob_data,
510 blob_length,
511 hb_face_get_index (font->face),
512 &ft_face);
513
514 if (unlikely (err)) {
515 hb_blob_destroy (blob);
516 DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed");
517 return;
518 }
519
Behdad Esfahbod254142b2011-08-15 16:15:44 +0200520 FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE);
521
Behdad Esfahbod21756932012-08-08 01:20:45 -0400522 assert (font->y_scale >= 0);
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200523 FT_Set_Char_Size (ft_face,
524 font->x_scale, font->y_scale,
Behdad Esfahbod2023e2b2012-07-11 19:00:30 -0400525 0, 0);
526#if 0
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200527 font->x_ppem * 72 * 64 / font->x_scale,
528 font->y_ppem * 72 * 64 / font->y_scale);
Behdad Esfahbod2023e2b2012-07-11 19:00:30 -0400529#endif
Behdad Esfahbod38b21182011-08-09 10:51:24 +0200530
531 ft_face->generic.data = blob;
532 ft_face->generic.finalizer = (FT_Generic_Finalizer) _release_blob;
533
534 hb_font_set_funcs (font,
535 _hb_ft_get_font_funcs (),
536 ft_face,
537 (hb_destroy_func_t) FT_Done_Face);
538}
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200539
540FT_Face
541hb_ft_font_get_face (hb_font_t *font)
542{
543 if (font->destroy == (hb_destroy_func_t) FT_Done_Face ||
544 font->destroy == (hb_destroy_func_t) _do_nothing)
545 return (FT_Face) font->user_data;
Behdad Esfahbod9527fb22011-08-13 19:03:48 +0200546
547 return NULL;
Behdad Esfahbod01ec13a2011-08-10 22:00:35 +0200548}