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_TRUETYPE_TABLES_H
41 * In general, this file does a fine job of what it's supposed to do.
42 * There are, however, things that need more work:
44 * - We don't handle any load_flags. That definitely has API implications. :(
45 * I believe hb_ft_font_create() should take load_flags input.
47 * - We don't handle / allow for emboldening / obliqueing.
49 * - In the future, we should add constructors to create fonts in font space.
51 * - I believe transforms are not correctly implemented. FreeType does not
52 * provide any API to get to the transform/delta set on the face. :(
54 * - Always use FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH?
59 hb_ft_get_glyph (hb_font_t *font HB_UNUSED,
61 hb_codepoint_t unicode,
62 hb_codepoint_t variation_selector,
63 hb_codepoint_t *glyph,
64 void *user_data HB_UNUSED)
67 FT_Face ft_face = (FT_Face) font_data;
69 #ifdef HAVE_FT_FACE_GETCHARVARIANTINDEX
70 if (unlikely (variation_selector)) {
71 *glyph = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector);
77 *glyph = FT_Get_Char_Index (ft_face, unicode);
82 hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
85 void *user_data HB_UNUSED)
87 FT_Face ft_face = (FT_Face) font_data;
88 int load_flags = FT_LOAD_DEFAULT;
90 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
93 return ft_face->glyph->metrics.horiAdvance;
97 hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
100 void *user_data HB_UNUSED)
102 FT_Face ft_face = (FT_Face) font_data;
103 int load_flags = FT_LOAD_DEFAULT;
105 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
108 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
109 * have a Y growing upward. Hence the extra negation. */
110 return -ft_face->glyph->metrics.vertAdvance;
114 hb_ft_get_glyph_h_origin (hb_font_t *font HB_UNUSED,
115 void *font_data HB_UNUSED,
116 hb_codepoint_t glyph HB_UNUSED,
117 hb_position_t *x HB_UNUSED,
118 hb_position_t *y HB_UNUSED,
119 void *user_data HB_UNUSED)
121 /* We always work in the horizontal coordinates. */
126 hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
128 hb_codepoint_t glyph,
131 void *user_data HB_UNUSED)
133 FT_Face ft_face = (FT_Face) font_data;
134 int load_flags = FT_LOAD_DEFAULT;
136 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
139 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
140 * have a Y growing upward. Hence the extra negation. */
141 *x = ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBearingX;
142 *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);
148 hb_ft_get_glyph_h_kerning (hb_font_t *font HB_UNUSED,
150 hb_codepoint_t left_glyph,
151 hb_codepoint_t right_glyph,
152 void *user_data HB_UNUSED)
154 FT_Face ft_face = (FT_Face) font_data;
157 if (FT_Get_Kerning (ft_face, left_glyph, right_glyph, FT_KERNING_DEFAULT, &kerningv))
164 hb_ft_get_glyph_v_kerning (hb_font_t *font HB_UNUSED,
165 void *font_data HB_UNUSED,
166 hb_codepoint_t top_glyph HB_UNUSED,
167 hb_codepoint_t bottom_glyph HB_UNUSED,
168 void *user_data HB_UNUSED)
170 /* FreeType API doesn't support vertical kerning */
175 hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
177 hb_codepoint_t glyph,
178 hb_glyph_extents_t *extents,
179 void *user_data HB_UNUSED)
181 FT_Face ft_face = (FT_Face) font_data;
182 int load_flags = FT_LOAD_DEFAULT;
184 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
187 extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
188 extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
189 extents->width = ft_face->glyph->metrics.width;
190 extents->height = ft_face->glyph->metrics.height;
195 hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
197 hb_codepoint_t glyph,
198 unsigned int point_index,
201 void *user_data HB_UNUSED)
203 FT_Face ft_face = (FT_Face) font_data;
204 int load_flags = FT_LOAD_DEFAULT;
206 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
209 if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
212 if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
215 *x = ft_face->glyph->outline.points[point_index].x;
216 *y = ft_face->glyph->outline.points[point_index].y;
221 static hb_font_funcs_t ft_ffuncs = {
222 HB_OBJECT_HEADER_STATIC,
224 TRUE, /* immutable */
228 hb_ft_get_glyph_h_advance,
229 hb_ft_get_glyph_v_advance,
230 hb_ft_get_glyph_h_origin,
231 hb_ft_get_glyph_v_origin,
232 hb_ft_get_glyph_h_kerning,
233 hb_ft_get_glyph_v_kerning,
234 hb_ft_get_glyph_extents,
235 hb_ft_get_glyph_contour_point,
240 hb_ft_get_font_funcs (void)
247 get_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
249 FT_Face ft_face = (FT_Face) user_data;
254 if (unlikely (tag == HB_TAG_NONE))
257 error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
261 buffer = (FT_Byte *) malloc (length);
265 error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
269 return hb_blob_create ((const char *) buffer, length,
270 HB_MEMORY_MODE_WRITABLE,
276 hb_ft_face_create (FT_Face ft_face,
277 hb_destroy_func_t destroy)
281 if (ft_face->stream->read == NULL) {
284 blob = hb_blob_create ((const char *) ft_face->stream->base,
285 (unsigned int) ft_face->stream->size,
286 /* TODO: We assume that it's mmap()'ed, but FreeType code
287 * suggests that there are cases we reach here but font is
288 * not mmapped. For example, when mmap() fails. No idea
289 * how to deal with it better here. */
290 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE,
292 face = hb_face_create (blob, ft_face->face_index);
293 hb_blob_destroy (blob);
295 face = hb_face_create_for_tables (get_table, ft_face, destroy);
302 hb_ft_face_finalize (FT_Face ft_face)
304 hb_face_destroy ((hb_face_t *) ft_face->generic.data);
308 hb_ft_face_create_cached (FT_Face ft_face)
310 if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
312 if (ft_face->generic.finalizer)
313 ft_face->generic.finalizer (ft_face);
315 ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
316 ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
319 return hb_face_reference ((hb_face_t *) ft_face->generic.data);
324 hb_ft_font_create (FT_Face ft_face,
325 hb_destroy_func_t destroy)
330 face = hb_ft_face_create (ft_face, destroy);
331 font = hb_font_create (face);
332 hb_face_destroy (face);
333 hb_font_set_funcs (font,
334 hb_ft_get_font_funcs (),
336 hb_font_set_scale (font,
337 ((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM) >> 16,
338 ((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM) >> 16);
339 hb_font_set_ppem (font,
340 ft_face->size->metrics.x_ppem,
341 ft_face->size->metrics.y_ppem);