2 * Copyright © 2009 Red Hat, Inc.
3 * Copyright © 2009 Keith Stribley
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Red Hat Author(s): Behdad Esfahbod
28 #include "hb-private.hh"
32 #include "hb-font-private.hh"
34 #include FT_ADVANCES_H
35 #include FT_TRUETYPE_TABLES_H
40 #define HB_DEBUG_FT (HB_DEBUG+0)
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:
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.
51 * In particular, FT_Get_Advance() without the NO_HINTING flag seems to be
54 * - We don't handle / allow for emboldening / obliqueing.
58 * - In the future, we should add constructors to create fonts in font space.
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. :(
63 * - Always use FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH?
65 * - FT_Load_Glyph() is exteremely costly. Do something about it?
70 hb_ft_get_glyph (hb_font_t *font HB_UNUSED,
72 hb_codepoint_t unicode,
73 hb_codepoint_t variation_selector,
74 hb_codepoint_t *glyph,
75 void *user_data HB_UNUSED)
78 FT_Face ft_face = (FT_Face) font_data;
80 #ifdef HAVE_FT_FACE_GETCHARVARIANTINDEX
81 if (unlikely (variation_selector)) {
82 *glyph = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector);
88 *glyph = FT_Get_Char_Index (ft_face, unicode);
93 hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
96 void *user_data HB_UNUSED)
98 FT_Face ft_face = (FT_Face) font_data;
99 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
102 if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v)))
109 hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
111 hb_codepoint_t glyph,
112 void *user_data HB_UNUSED)
114 FT_Face ft_face = (FT_Face) font_data;
115 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING | FT_LOAD_VERTICAL_LAYOUT;
118 if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v)))
121 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
122 * have a Y growing upward. Hence the extra negation. */
127 hb_ft_get_glyph_h_origin (hb_font_t *font HB_UNUSED,
128 void *font_data HB_UNUSED,
129 hb_codepoint_t glyph HB_UNUSED,
130 hb_position_t *x HB_UNUSED,
131 hb_position_t *y HB_UNUSED,
132 void *user_data HB_UNUSED)
134 /* We always work in the horizontal coordinates. */
139 hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
141 hb_codepoint_t glyph,
144 void *user_data HB_UNUSED)
146 FT_Face ft_face = (FT_Face) font_data;
147 int load_flags = FT_LOAD_DEFAULT;
149 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
152 /* 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);
161 hb_ft_get_glyph_h_kerning (hb_font_t *font,
163 hb_codepoint_t left_glyph,
164 hb_codepoint_t right_glyph,
165 void *user_data HB_UNUSED)
167 FT_Face ft_face = (FT_Face) font_data;
170 FT_Kerning_Mode mode = font->x_ppem ? FT_KERNING_DEFAULT : FT_KERNING_UNFITTED;
171 if (FT_Get_Kerning (ft_face, left_glyph, right_glyph, mode, &kerningv))
178 hb_ft_get_glyph_v_kerning (hb_font_t *font HB_UNUSED,
179 void *font_data HB_UNUSED,
180 hb_codepoint_t top_glyph HB_UNUSED,
181 hb_codepoint_t bottom_glyph HB_UNUSED,
182 void *user_data HB_UNUSED)
184 /* FreeType API doesn't support vertical kerning */
189 hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
191 hb_codepoint_t glyph,
192 hb_glyph_extents_t *extents,
193 void *user_data HB_UNUSED)
195 FT_Face ft_face = (FT_Face) font_data;
196 int load_flags = FT_LOAD_DEFAULT;
198 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
201 extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
202 extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
203 extents->width = ft_face->glyph->metrics.width;
204 extents->height = -ft_face->glyph->metrics.height;
209 hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
211 hb_codepoint_t glyph,
212 unsigned int point_index,
215 void *user_data HB_UNUSED)
217 FT_Face ft_face = (FT_Face) font_data;
218 int load_flags = FT_LOAD_DEFAULT;
220 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
223 if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
226 if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
229 *x = ft_face->glyph->outline.points[point_index].x;
230 *y = ft_face->glyph->outline.points[point_index].y;
236 hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED,
238 hb_codepoint_t glyph,
239 char *name, unsigned int size,
240 void *user_data HB_UNUSED)
242 FT_Face ft_face = (FT_Face) font_data;
244 hb_bool_t ret = !FT_Get_Glyph_Name (ft_face, glyph, name, size);
246 snprintf (name, size, "gid%u", glyph);
252 hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
254 const char *name, int len, /* -1 means nul-terminated */
255 hb_codepoint_t *glyph,
256 void *user_data HB_UNUSED)
258 FT_Face ft_face = (FT_Face) font_data;
261 *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
263 /* Make a nul-terminated version. */
265 len = MIN (len, (int) sizeof (buf) - 1);
266 strncpy (buf, name, len);
268 *glyph = FT_Get_Name_Index (ft_face, buf);
275 static hb_font_funcs_t *
276 _hb_ft_get_font_funcs (void)
278 static const hb_font_funcs_t ft_ffuncs = {
279 HB_OBJECT_HEADER_STATIC,
281 true, /* immutable */
284 #define HB_FONT_FUNC_IMPLEMENT(name) hb_ft_get_##name,
285 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
286 #undef HB_FONT_FUNC_IMPLEMENT
290 return const_cast<hb_font_funcs_t *> (&ft_ffuncs);
295 reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
297 FT_Face ft_face = (FT_Face) user_data;
302 /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob */
304 error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
308 buffer = (FT_Byte *) malloc (length);
312 error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
316 return hb_blob_create ((const char *) buffer, length,
317 HB_MEMORY_MODE_WRITABLE,
323 hb_ft_face_create (FT_Face ft_face,
324 hb_destroy_func_t destroy)
328 if (ft_face->stream->read == NULL) {
331 blob = hb_blob_create ((const char *) ft_face->stream->base,
332 (unsigned int) ft_face->stream->size,
333 /* TODO: We assume that it's mmap()'ed, but FreeType code
334 * suggests that there are cases we reach here but font is
335 * not mmapped. For example, when mmap() fails. No idea
336 * how to deal with it better here. */
337 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE,
339 face = hb_face_create (blob, ft_face->face_index);
340 hb_blob_destroy (blob);
342 face = hb_face_create_for_tables (reference_table, ft_face, destroy);
345 hb_face_set_index (face, ft_face->face_index);
346 hb_face_set_upem (face, ft_face->units_per_EM);
352 hb_ft_face_finalize (FT_Face ft_face)
354 hb_face_destroy ((hb_face_t *) ft_face->generic.data);
358 hb_ft_face_create_cached (FT_Face ft_face)
360 if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
362 if (ft_face->generic.finalizer)
363 ft_face->generic.finalizer (ft_face);
365 ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
366 ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
369 return hb_face_reference ((hb_face_t *) ft_face->generic.data);
379 hb_ft_font_create (FT_Face ft_face,
380 hb_destroy_func_t destroy)
385 face = hb_ft_face_create (ft_face, destroy);
386 font = hb_font_create (face);
387 hb_face_destroy (face);
388 hb_font_set_funcs (font,
389 _hb_ft_get_font_funcs (),
390 ft_face, (hb_destroy_func_t) _do_nothing);
391 hb_font_set_scale (font,
392 ((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM) >> 16,
393 ((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM) >> 16);
394 hb_font_set_ppem (font,
395 ft_face->size->metrics.x_ppem,
396 ft_face->size->metrics.y_ppem);
402 /* Thread-safe, lock-free, FT_Library */
404 static FT_Library ft_library;
407 void free_ft_library (void)
409 FT_Done_FreeType (ft_library);
413 get_ft_library (void)
416 FT_Library library = (FT_Library) hb_atomic_ptr_get (&ft_library);
418 if (unlikely (!library))
420 /* Not found; allocate one. */
421 if (FT_Init_FreeType (&library))
424 if (!hb_atomic_ptr_cmpexch (&ft_library, NULL, library)) {
425 FT_Done_FreeType (library);
430 atexit (free_ft_library); /* First person registers atexit() callback. */
438 _release_blob (FT_Face ft_face)
440 hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
444 hb_ft_font_set_funcs (hb_font_t *font)
446 hb_blob_t *blob = hb_face_reference_blob (font->face);
447 unsigned int blob_length;
448 const char *blob_data = hb_blob_get_data (blob, &blob_length);
449 if (unlikely (!blob_length))
450 DEBUG_MSG (FT, font, "Font face has empty blob");
452 FT_Face ft_face = NULL;
453 FT_Error err = FT_New_Memory_Face (get_ft_library (),
454 (const FT_Byte *) blob_data,
456 hb_face_get_index (font->face),
459 if (unlikely (err)) {
460 hb_blob_destroy (blob);
461 DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed");
465 FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE);
467 assert (font->y_scale >= 0);
468 FT_Set_Char_Size (ft_face,
469 font->x_scale, font->y_scale,
472 font->x_ppem * 72 * 64 / font->x_scale,
473 font->y_ppem * 72 * 64 / font->y_scale);
476 ft_face->generic.data = blob;
477 ft_face->generic.finalizer = (FT_Generic_Finalizer) _release_blob;
479 hb_font_set_funcs (font,
480 _hb_ft_get_font_funcs (),
482 (hb_destroy_func_t) FT_Done_Face);
486 hb_ft_font_get_face (hb_font_t *font)
488 if (font->destroy == (hb_destroy_func_t) FT_Done_Face ||
489 font->destroy == (hb_destroy_func_t) _do_nothing)
490 return (FT_Face) font->user_data;