aef7e6f8079cc8b916a92e84f3d497c39aeb3b6d
[apps/home/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_bool_t
40 hb_ft_get_contour_point (hb_font_t *font HB_UNUSED,
41                          void *font_data,
42                          hb_codepoint_t glyph,
43                          unsigned int point_index,
44                          hb_position_t *x,
45                          hb_position_t *y,
46                          void *user_data HB_UNUSED)
47 {
48   FT_Face ft_face = (FT_Face) font_data;
49   int load_flags = FT_LOAD_DEFAULT;
50
51   /* TODO: load_flags, embolden, etc */
52
53   if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
54       return FALSE;
55
56   if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
57       return FALSE;
58
59   if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
60       return FALSE;
61
62   *x = ft_face->glyph->outline.points[point_index].x;
63   *y = ft_face->glyph->outline.points[point_index].y;
64
65   return TRUE;
66 }
67
68 static void
69 hb_ft_get_glyph_advance (hb_font_t *font HB_UNUSED,
70                          void *font_data,
71                          hb_codepoint_t glyph,
72                          hb_position_t *x_advance,
73                          hb_position_t *y_advance,
74                          void *user_data HB_UNUSED)
75 {
76   FT_Face ft_face = (FT_Face) font_data;
77   int load_flags = FT_LOAD_DEFAULT;
78
79   /* TODO: load_flags, embolden, etc */
80
81   if (likely (!FT_Load_Glyph (ft_face, glyph, load_flags)))
82   {
83     *x_advance = ft_face->glyph->advance.x;
84     *y_advance = ft_face->glyph->advance.y;
85   }
86 }
87
88 static void
89 hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
90                          void *font_data,
91                          hb_codepoint_t glyph,
92                          hb_glyph_extents_t *extents,
93                          void *user_data HB_UNUSED)
94 {
95   FT_Face ft_face = (FT_Face) font_data;
96   int load_flags = FT_LOAD_DEFAULT;
97
98   /* TODO: load_flags, embolden, etc */
99
100   if (likely (!FT_Load_Glyph (ft_face, glyph, load_flags)))
101   {
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;
107   }
108 }
109
110 static hb_bool_t
111 hb_ft_get_glyph (hb_font_t *font HB_UNUSED,
112                  void *font_data,
113                  hb_codepoint_t unicode,
114                  hb_codepoint_t variation_selector,
115                  hb_codepoint_t *glyph,
116                  void *user_data HB_UNUSED)
117
118 {
119   FT_Face ft_face = (FT_Face) font_data;
120
121 #ifdef HAVE_FT_FACE_GETCHARVARIANTINDEX
122   if (unlikely (variation_selector)) {
123     *glyph = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector);
124     if (*glyph)
125       return TRUE;
126   }
127 #endif
128
129   *glyph = FT_Get_Char_Index (ft_face, unicode);
130   return *glyph != 0;
131 }
132
133 static void
134 hb_ft_get_kerning (hb_font_t *font HB_UNUSED,
135                    void *font_data,
136                    hb_codepoint_t left_glyph,
137                    hb_codepoint_t right_glyph,
138                    hb_position_t *x_kern,
139                    hb_position_t *y_kern,
140                    void *user_data HB_UNUSED)
141 {
142   FT_Face ft_face = (FT_Face) font_data;
143   FT_Vector kerning;
144
145   if (FT_Get_Kerning (ft_face, left_glyph, right_glyph, FT_KERNING_DEFAULT, &kerning))
146     return;
147
148   *x_kern = kerning.x;
149   *y_kern = kerning.y;
150 }
151
152 static hb_font_funcs_t ft_ffuncs = {
153   HB_OBJECT_HEADER_STATIC,
154
155   TRUE, /* immutable */
156
157   {
158     hb_ft_get_contour_point,
159     hb_ft_get_glyph_advance,
160     hb_ft_get_glyph_extents,
161     hb_ft_get_glyph,
162     hb_ft_get_kerning
163   }
164 };
165
166 hb_font_funcs_t *
167 hb_ft_get_font_funcs (void)
168 {
169   return &ft_ffuncs;
170 }
171
172
173 static hb_blob_t *
174 get_table  (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
175 {
176   FT_Face ft_face = (FT_Face) user_data;
177   FT_Byte *buffer;
178   FT_ULong  length = 0;
179   FT_Error error;
180
181   if (unlikely (tag == HB_TAG_NONE))
182     return NULL;
183
184   error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
185   if (error)
186     return NULL;
187
188   buffer = (FT_Byte *) malloc (length);
189   if (buffer == NULL)
190     return NULL;
191
192   error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
193   if (error)
194     return NULL;
195
196   return hb_blob_create ((const char *) buffer, length,
197                          HB_MEMORY_MODE_WRITABLE,
198                          buffer, free);
199 }
200
201
202 hb_face_t *
203 hb_ft_face_create (FT_Face           ft_face,
204                    hb_destroy_func_t destroy)
205 {
206   hb_face_t *face;
207
208   if (ft_face->stream->read == NULL) {
209     hb_blob_t *blob;
210
211     blob = hb_blob_create ((const char *) ft_face->stream->base,
212                            (unsigned int) ft_face->stream->size,
213                            /* TODO: Check FT_FACE_FLAG_EXTERNAL_STREAM? */
214                            HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE,
215                            ft_face, destroy);
216     face = hb_face_create (blob, ft_face->face_index);
217     hb_blob_destroy (blob);
218   } else {
219     face = hb_face_create_for_tables (get_table, ft_face, destroy);
220   }
221
222   return face;
223 }
224
225 static void
226 hb_ft_face_finalize (FT_Face ft_face)
227 {
228   hb_face_destroy ((hb_face_t *) ft_face->generic.data);
229 }
230
231 hb_face_t *
232 hb_ft_face_create_cached (FT_Face ft_face)
233 {
234   if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
235   {
236     if (ft_face->generic.finalizer)
237       ft_face->generic.finalizer (ft_face);
238
239     ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
240     ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
241   }
242
243   return hb_face_reference ((hb_face_t *) ft_face->generic.data);
244 }
245
246
247 hb_font_t *
248 hb_ft_font_create (FT_Face           ft_face,
249                    hb_destroy_func_t destroy)
250 {
251   hb_font_t *font;
252   hb_face_t *face;
253
254   face = hb_ft_face_create (ft_face, destroy);
255   font = hb_font_create (face);
256   hb_face_destroy (face);
257   hb_font_set_funcs (font,
258                      hb_ft_get_font_funcs (),
259                      ft_face, NULL);
260   hb_font_set_scale (font,
261                      ((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM) >> 16,
262                      ((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM) >> 16);
263   hb_font_set_ppem (font,
264                     ft_face->size->metrics.x_ppem,
265                     ft_face->size->metrics.y_ppem);
266
267   return font;
268 }
269
270
271 HB_END_DECLS