[Vertical] GPOS is always done with horizontal origin
[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 /* TODO:
40  *
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:
43  *
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.
46  *
47  *   - We don't handle / allow for emboldening / obliqueing.
48  *
49  *   - In the future, we should add constructors to create fonts in font space.
50  *
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. :(
53  *
54  *   - Always use FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH?
55  */
56
57
58 static hb_bool_t
59 hb_ft_get_glyph (hb_font_t *font HB_UNUSED,
60                  void *font_data,
61                  hb_codepoint_t unicode,
62                  hb_codepoint_t variation_selector,
63                  hb_codepoint_t *glyph,
64                  void *user_data HB_UNUSED)
65
66 {
67   FT_Face ft_face = (FT_Face) font_data;
68
69 #ifdef HAVE_FT_FACE_GETCHARVARIANTINDEX
70   if (unlikely (variation_selector)) {
71     *glyph = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector);
72     if (*glyph)
73       return TRUE;
74   }
75 #endif
76
77   *glyph = FT_Get_Char_Index (ft_face, unicode);
78   return *glyph != 0;
79 }
80
81 static void
82 hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
83                            void *font_data,
84                            hb_codepoint_t glyph,
85                            hb_position_t *advance,
86                            void *user_data HB_UNUSED)
87 {
88   FT_Face ft_face = (FT_Face) font_data;
89   int load_flags = FT_LOAD_DEFAULT;
90
91   if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
92     return;
93
94   *advance = ft_face->glyph->metrics.horiAdvance;
95 }
96
97 static void
98 hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
99                            void *font_data,
100                            hb_codepoint_t glyph,
101                            hb_position_t *advance,
102                            void *user_data HB_UNUSED)
103 {
104   FT_Face ft_face = (FT_Face) font_data;
105   int load_flags = FT_LOAD_DEFAULT;
106
107   if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
108     return;
109
110   /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
111    * have a Y growing upward.  Hence the extra negation. */
112   *advance = -ft_face->glyph->metrics.vertAdvance;
113 }
114
115 static hb_bool_t
116 hb_ft_get_glyph_h_origin (hb_font_t *font HB_UNUSED,
117                           void *font_data HB_UNUSED,
118                           hb_codepoint_t glyph HB_UNUSED,
119                           hb_position_t *x HB_UNUSED,
120                           hb_position_t *y HB_UNUSED,
121                           void *user_data HB_UNUSED)
122 {
123   /* We always work in the horizontal coordinates. */
124   return TRUE;
125 }
126
127 static hb_bool_t
128 hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
129                           void *font_data,
130                           hb_codepoint_t glyph,
131                           hb_position_t *x,
132                           hb_position_t *y,
133                           void *user_data HB_UNUSED)
134 {
135   FT_Face ft_face = (FT_Face) font_data;
136   int load_flags = FT_LOAD_DEFAULT;
137
138   if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
139     return FALSE;
140
141   /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
142    * have a Y growing upward.  Hence the extra negation. */
143   *x = ft_face->glyph->metrics.horiBearingX -   ft_face->glyph->metrics.vertBearingX;
144   *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);
145
146   return TRUE;
147 }
148
149 static void
150 hb_ft_get_glyph_h_kerning (hb_font_t *font HB_UNUSED,
151                            void *font_data,
152                            hb_codepoint_t left_glyph,
153                            hb_codepoint_t right_glyph,
154                            hb_position_t *kerning,
155                            void *user_data HB_UNUSED)
156 {
157   FT_Face ft_face = (FT_Face) font_data;
158   FT_Vector kerningv;
159
160   if (FT_Get_Kerning (ft_face, left_glyph, right_glyph, FT_KERNING_DEFAULT, &kerningv))
161     return;
162
163   *kerning = kerningv.x;
164 }
165
166 static void
167 hb_ft_get_glyph_v_kerning (hb_font_t *font HB_UNUSED,
168                            void *font_data HB_UNUSED,
169                            hb_codepoint_t top_glyph HB_UNUSED,
170                            hb_codepoint_t bottom_glyph HB_UNUSED,
171                            hb_position_t *kerning HB_UNUSED,
172                            void *user_data HB_UNUSED)
173 {
174   /* FreeType API doesn't support vertical kerning */
175   return;
176 }
177
178 static hb_bool_t
179 hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
180                          void *font_data,
181                          hb_codepoint_t glyph,
182                          hb_glyph_extents_t *extents,
183                          void *user_data HB_UNUSED)
184 {
185   FT_Face ft_face = (FT_Face) font_data;
186   int load_flags = FT_LOAD_DEFAULT;
187
188   if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
189     return FALSE;
190
191   extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
192   extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
193   extents->width = ft_face->glyph->metrics.width;
194   extents->height = ft_face->glyph->metrics.height;
195   return TRUE;
196 }
197
198 static hb_bool_t
199 hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
200                                void *font_data,
201                                hb_codepoint_t glyph,
202                                unsigned int point_index,
203                                hb_position_t *x,
204                                hb_position_t *y,
205                                void *user_data HB_UNUSED)
206 {
207   FT_Face ft_face = (FT_Face) font_data;
208   int load_flags = FT_LOAD_DEFAULT;
209
210   if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
211       return FALSE;
212
213   if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
214       return FALSE;
215
216   if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
217       return FALSE;
218
219   *x = ft_face->glyph->outline.points[point_index].x;
220   *y = ft_face->glyph->outline.points[point_index].y;
221
222   return TRUE;
223 }
224
225 static hb_font_funcs_t ft_ffuncs = {
226   HB_OBJECT_HEADER_STATIC,
227
228   TRUE, /* immutable */
229
230   {
231     hb_ft_get_glyph,
232     hb_ft_get_glyph_h_advance,
233     hb_ft_get_glyph_v_advance,
234     hb_ft_get_glyph_h_origin,
235     hb_ft_get_glyph_v_origin,
236     hb_ft_get_glyph_h_kerning,
237     hb_ft_get_glyph_v_kerning,
238     hb_ft_get_glyph_extents,
239     hb_ft_get_glyph_contour_point,
240   }
241 };
242
243 hb_font_funcs_t *
244 hb_ft_get_font_funcs (void)
245 {
246   return &ft_ffuncs;
247 }
248
249
250 static hb_blob_t *
251 get_table  (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
252 {
253   FT_Face ft_face = (FT_Face) user_data;
254   FT_Byte *buffer;
255   FT_ULong  length = 0;
256   FT_Error error;
257
258   if (unlikely (tag == HB_TAG_NONE))
259     return NULL;
260
261   error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
262   if (error)
263     return NULL;
264
265   buffer = (FT_Byte *) malloc (length);
266   if (buffer == NULL)
267     return NULL;
268
269   error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
270   if (error)
271     return NULL;
272
273   return hb_blob_create ((const char *) buffer, length,
274                          HB_MEMORY_MODE_WRITABLE,
275                          buffer, free);
276 }
277
278
279 hb_face_t *
280 hb_ft_face_create (FT_Face           ft_face,
281                    hb_destroy_func_t destroy)
282 {
283   hb_face_t *face;
284
285   if (ft_face->stream->read == NULL) {
286     hb_blob_t *blob;
287
288     blob = hb_blob_create ((const char *) ft_face->stream->base,
289                            (unsigned int) ft_face->stream->size,
290                            /* TODO: We assume that it's mmap()'ed, but FreeType code
291                             * suggests that there are cases we reach here but font is
292                             * not mmapped.  For example, when mmap() fails.  No idea
293                             * how to deal with it better here. */
294                            HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE,
295                            ft_face, destroy);
296     face = hb_face_create (blob, ft_face->face_index);
297     hb_blob_destroy (blob);
298   } else {
299     face = hb_face_create_for_tables (get_table, ft_face, destroy);
300   }
301
302   return face;
303 }
304
305 static void
306 hb_ft_face_finalize (FT_Face ft_face)
307 {
308   hb_face_destroy ((hb_face_t *) ft_face->generic.data);
309 }
310
311 hb_face_t *
312 hb_ft_face_create_cached (FT_Face ft_face)
313 {
314   if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
315   {
316     if (ft_face->generic.finalizer)
317       ft_face->generic.finalizer (ft_face);
318
319     ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
320     ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
321   }
322
323   return hb_face_reference ((hb_face_t *) ft_face->generic.data);
324 }
325
326
327 hb_font_t *
328 hb_ft_font_create (FT_Face           ft_face,
329                    hb_destroy_func_t destroy)
330 {
331   hb_font_t *font;
332   hb_face_t *face;
333
334   face = hb_ft_face_create (ft_face, destroy);
335   font = hb_font_create (face);
336   hb_face_destroy (face);
337   hb_font_set_funcs (font,
338                      hb_ft_get_font_funcs (),
339                      ft_face, NULL);
340   hb_font_set_scale (font,
341                      ((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM) >> 16,
342                      ((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM) >> 16);
343   hb_font_set_ppem (font,
344                     ft_face->size->metrics.x_ppem,
345                     ft_face->size->metrics.y_ppem);
346
347   return font;
348 }
349
350
351 HB_END_DECLS