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
40 hb_ft_get_contour_point (hb_font_t *font HB_UNUSED,
41 const void *font_data,
43 unsigned int point_index,
46 const void *user_data HB_UNUSED)
48 FT_Face ft_face = (FT_Face) font_data;
49 int load_flags = FT_LOAD_DEFAULT;
51 /* TODO: load_flags, embolden, etc */
53 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
56 if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
59 if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
62 *x = ft_face->glyph->outline.points[point_index].x;
63 *y = ft_face->glyph->outline.points[point_index].y;
69 hb_ft_get_glyph_advance (hb_font_t *font HB_UNUSED,
70 const void *font_data,
72 hb_position_t *x_advance,
73 hb_position_t *y_advance,
74 const void *user_data HB_UNUSED)
76 FT_Face ft_face = (FT_Face) font_data;
77 int load_flags = FT_LOAD_DEFAULT;
79 /* TODO: load_flags, embolden, etc */
81 if (likely (!FT_Load_Glyph (ft_face, glyph, load_flags)))
83 *x_advance = ft_face->glyph->advance.x;
84 *y_advance = ft_face->glyph->advance.y;
89 hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
90 const void *font_data,
92 hb_glyph_extents_t *extents,
93 const void *user_data HB_UNUSED)
95 FT_Face ft_face = (FT_Face) font_data;
96 int load_flags = FT_LOAD_DEFAULT;
98 /* TODO: load_flags, embolden, etc */
100 if (likely (!FT_Load_Glyph (ft_face, glyph, load_flags)))
102 /* XXX: A few negations should be in order here, not sure. */
103 extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
104 extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
105 extents->width = ft_face->glyph->metrics.width;
106 extents->height = ft_face->glyph->metrics.height;
110 static hb_codepoint_t
111 hb_ft_get_glyph (hb_font_t *font HB_UNUSED,
112 const void *font_data,
113 hb_codepoint_t unicode,
114 hb_codepoint_t variation_selector,
115 const void *user_data HB_UNUSED)
118 FT_Face ft_face = (FT_Face) font_data;
120 #ifdef HAVE_FT_FACE_GETCHARVARIANTINDEX
121 if (unlikely (variation_selector)) {
122 hb_codepoint_t glyph = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector);
128 return FT_Get_Char_Index (ft_face, unicode);
132 hb_ft_get_kerning (hb_font_t *font HB_UNUSED,
133 const void *font_data,
134 hb_codepoint_t first_glyph,
135 hb_codepoint_t second_glyph,
136 hb_position_t *x_kern,
137 hb_position_t *y_kern,
138 const void *user_data HB_UNUSED)
140 FT_Face ft_face = (FT_Face) font_data;
143 /* TODO: Kern type? */
144 if (FT_Get_Kerning (ft_face, first_glyph, second_glyph, FT_KERNING_DEFAULT, &kerning))
151 static hb_font_funcs_t ft_ffuncs = {
152 HB_OBJECT_HEADER_STATIC,
154 TRUE, /* immutable */
157 hb_ft_get_contour_point,
158 hb_ft_get_glyph_advance,
159 hb_ft_get_glyph_extents,
166 hb_ft_get_font_funcs (void)
173 get_table (hb_tag_t tag, void *user_data)
175 FT_Face ft_face = (FT_Face) user_data;
180 if (unlikely (tag == HB_TAG_NONE))
183 error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
187 buffer = (FT_Byte *) malloc (length);
191 error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
195 return hb_blob_create ((const char *) buffer, length,
196 HB_MEMORY_MODE_WRITABLE,
202 hb_ft_face_create (FT_Face ft_face,
203 hb_destroy_func_t destroy)
207 if (ft_face->stream->read == NULL) {
210 blob = hb_blob_create ((const char *) ft_face->stream->base,
211 (unsigned int) ft_face->stream->size,
212 /* TODO: Check FT_FACE_FLAG_EXTERNAL_STREAM? */
213 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE,
215 face = hb_face_create_for_data (blob, ft_face->face_index);
216 hb_blob_destroy (blob);
218 face = hb_face_create_for_tables (get_table, ft_face, destroy);
225 hb_ft_face_finalize (FT_Face ft_face)
227 hb_face_destroy ((hb_face_t *) ft_face->generic.data);
231 hb_ft_face_create_cached (FT_Face ft_face)
233 if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
235 if (ft_face->generic.finalizer)
236 ft_face->generic.finalizer (ft_face);
238 ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
239 ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
242 return hb_face_reference ((hb_face_t *) ft_face->generic.data);
247 hb_ft_font_create (FT_Face ft_face,
248 hb_destroy_func_t destroy)
253 face = hb_ft_face_create (ft_face, destroy);
254 font = hb_font_create (face);
255 hb_face_destroy (face);
256 hb_font_set_funcs (font,
257 hb_ft_get_font_funcs (),
259 hb_font_set_scale (font,
260 ((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM) >> 16,
261 ((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM) >> 16);
262 hb_font_set_ppem (font,
263 ft_face->size->metrics.x_ppem,
264 ft_face->size->metrics.y_ppem);