[API] Make hb_font_create() take a face and reference it
[apps/core/preloaded/video-player.git] / src / hb-ft.cc
1 /*
2  * Copyright © 2009  Red Hat, Inc.
3  * Copyright © 2009  Keith Stribley
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
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
28 #include "hb-private.hh"
29
30 #include "hb-ft.h"
31
32 #include "hb-font-private.hh"
33
34 #include FT_TRUETYPE_TABLES_H
35
36 HB_BEGIN_DECLS
37
38
39 static hb_codepoint_t
40 hb_ft_get_glyph (hb_font_t *font HB_UNUSED,
41                  const void *user_data,
42                  hb_codepoint_t unicode,
43                  hb_codepoint_t variation_selector)
44 {
45   FT_Face ft_face = (FT_Face) user_data;
46
47 #ifdef HAVE_FT_FACE_GETCHARVARIANTINDEX
48   if (unlikely (variation_selector)) {
49     hb_codepoint_t glyph = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector);
50     if (glyph)
51       return glyph;
52   }
53 #endif
54
55   return FT_Get_Char_Index (ft_face, unicode);
56 }
57
58 static void
59 hb_ft_get_glyph_advance (hb_font_t *font HB_UNUSED,
60                          const void *user_data,
61                          hb_codepoint_t glyph,
62                          hb_position_t *x_advance,
63                          hb_position_t *y_advance)
64 {
65   FT_Face ft_face = (FT_Face) user_data;
66   int load_flags = FT_LOAD_DEFAULT;
67
68   /* TODO: load_flags, embolden, etc */
69
70   if (likely (!FT_Load_Glyph (ft_face, glyph, load_flags)))
71   {
72     *x_advance = ft_face->glyph->advance.x;
73     *y_advance = ft_face->glyph->advance.y;
74   }
75 }
76
77 static void
78 hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
79                          const void *user_data,
80                          hb_codepoint_t glyph,
81                          hb_glyph_extents_t *extents)
82 {
83   FT_Face ft_face = (FT_Face) user_data;
84   int load_flags = FT_LOAD_DEFAULT;
85
86   /* TODO: load_flags, embolden, etc */
87
88   if (likely (!FT_Load_Glyph (ft_face, glyph, load_flags)))
89   {
90     /* XXX: A few negations should be in order here, not sure. */
91     extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
92     extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
93     extents->width = ft_face->glyph->metrics.width;
94     extents->height = ft_face->glyph->metrics.height;
95   }
96 }
97
98 static hb_bool_t
99 hb_ft_get_contour_point (hb_font_t *font HB_UNUSED,
100                          const void *user_data,
101                          unsigned int point_index,
102                          hb_codepoint_t glyph,
103                          hb_position_t *x,
104                          hb_position_t *y)
105 {
106   FT_Face ft_face = (FT_Face) user_data;
107   int load_flags = FT_LOAD_DEFAULT;
108
109   /* TODO: load_flags, embolden, etc */
110
111   if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
112       return FALSE;
113
114   if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
115       return FALSE;
116
117   if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
118       return FALSE;
119
120   *x = ft_face->glyph->outline.points[point_index].x;
121   *y = ft_face->glyph->outline.points[point_index].y;
122
123   return TRUE;
124 }
125
126 static hb_position_t
127 hb_ft_get_kerning (hb_font_t *font HB_UNUSED,
128                    const void *user_data,
129                    hb_codepoint_t first_glyph,
130                    hb_codepoint_t second_glyph)
131 {
132   FT_Face ft_face = (FT_Face) user_data;
133   FT_Vector kerning;
134
135   /* TODO: Kern type? */
136   if (FT_Get_Kerning (ft_face, first_glyph, second_glyph, FT_KERNING_DEFAULT, &kerning))
137       return 0;
138
139   return kerning.x;
140 }
141
142 static hb_font_funcs_t ft_ffuncs = {
143   HB_OBJECT_HEADER_STATIC,
144
145   TRUE, /* immutable */
146   {
147     hb_ft_get_glyph,
148     hb_ft_get_glyph_advance,
149     hb_ft_get_glyph_extents,
150     hb_ft_get_contour_point,
151     hb_ft_get_kerning
152   }
153 };
154
155 hb_font_funcs_t *
156 hb_ft_get_font_funcs (void)
157 {
158   return &ft_ffuncs;
159 }
160
161
162 static hb_blob_t *
163 get_table  (hb_tag_t tag, void *user_data)
164 {
165   FT_Face ft_face = (FT_Face) user_data;
166   FT_Byte *buffer;
167   FT_ULong  length = 0;
168   FT_Error error;
169
170   if (unlikely (tag == HB_TAG_NONE))
171     return NULL;
172
173   error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
174   if (error)
175     return NULL;
176
177   buffer = (FT_Byte *) malloc (length);
178   if (buffer == NULL)
179     return NULL;
180
181   error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
182   if (error)
183     return NULL;
184
185   return hb_blob_create ((const char *) buffer, length,
186                          HB_MEMORY_MODE_WRITABLE,
187                          buffer, free);
188 }
189
190
191 hb_face_t *
192 hb_ft_face_create (FT_Face           ft_face,
193                    hb_destroy_func_t destroy)
194 {
195   hb_face_t *face;
196
197   if (ft_face->stream->read == NULL) {
198     hb_blob_t *blob;
199
200     blob = hb_blob_create ((const char *) ft_face->stream->base,
201                            (unsigned int) ft_face->stream->size,
202                            /* TODO: Check FT_FACE_FLAG_EXTERNAL_STREAM? */
203                            HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE,
204                            ft_face, destroy);
205     face = hb_face_create_for_data (blob, ft_face->face_index);
206     hb_blob_destroy (blob);
207   } else {
208     face = hb_face_create_for_tables (get_table, ft_face, destroy);
209   }
210
211   return face;
212 }
213
214 static void
215 hb_ft_face_finalize (FT_Face ft_face)
216 {
217   hb_face_destroy ((hb_face_t *) ft_face->generic.data);
218 }
219
220 hb_face_t *
221 hb_ft_face_create_cached (FT_Face ft_face)
222 {
223   if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
224   {
225     if (ft_face->generic.finalizer)
226       ft_face->generic.finalizer (ft_face);
227
228     ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
229     ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
230   }
231
232   return hb_face_reference ((hb_face_t *) ft_face->generic.data);
233 }
234
235
236 hb_font_t *
237 hb_ft_font_create (FT_Face           ft_face,
238                    hb_destroy_func_t destroy)
239 {
240   hb_font_t *font;
241
242   font = hb_font_create (hb_ft_face_create_cached (ft_face));
243   hb_font_set_funcs (font,
244                      hb_ft_get_font_funcs (),
245                      ft_face, destroy);
246   hb_font_set_scale (font,
247                      ((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM) >> 16,
248                      ((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM) >> 16);
249   hb_font_set_ppem (font,
250                     ft_face->size->metrics.x_ppem,
251                     ft_face->size->metrics.y_ppem);
252
253   return font;
254 }
255
256
257 HB_END_DECLS