[API] Add support for vertical text
[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 hb_bool_t
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 *x_advance,
86                            hb_position_t *y_advance,
87                            void *user_data HB_UNUSED)
88 {
89   FT_Face ft_face = (FT_Face) font_data;
90   int load_flags = FT_LOAD_DEFAULT;
91
92   if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
93     return FALSE;
94
95   *x_advance = ft_face->glyph->metrics.horiAdvance;
96   return TRUE;
97 }
98
99 static hb_bool_t
100 hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
101                            void *font_data,
102                            hb_codepoint_t glyph,
103                            hb_position_t *x_advance,
104                            hb_position_t *y_advance,
105                            void *user_data HB_UNUSED)
106 {
107   FT_Face ft_face = (FT_Face) font_data;
108   int load_flags = FT_LOAD_DEFAULT;
109
110   if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
111     return FALSE;
112
113   *y_advance = -ft_face->glyph->metrics.vertAdvance;
114   return TRUE;
115 }
116
117 static hb_bool_t
118 hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
119                           void *font_data,
120                           hb_codepoint_t glyph,
121                           hb_position_t *x_origin,
122                           hb_position_t *y_origin,
123                           void *user_data HB_UNUSED)
124 {
125   FT_Face ft_face = (FT_Face) font_data;
126   int load_flags = FT_LOAD_DEFAULT;
127
128   if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
129     return FALSE;
130
131   *y_origin = ft_face->glyph->metrics.vertAdvance;
132   return TRUE;
133 }
134
135 static hb_bool_t
136 hb_ft_get_h_kerning (hb_font_t *font HB_UNUSED,
137                      void *font_data,
138                      hb_codepoint_t left_glyph,
139                      hb_codepoint_t right_glyph,
140                      hb_position_t *x_kern,
141                      hb_position_t *y_kern,
142                      void *user_data HB_UNUSED)
143 {
144   FT_Face ft_face = (FT_Face) font_data;
145   FT_Vector kerning;
146
147   if (FT_Get_Kerning (ft_face, left_glyph, right_glyph, FT_KERNING_DEFAULT, &kerning))
148     return FALSE;
149
150   *x_kern = kerning.x;
151   *y_kern = kerning.y;
152   return TRUE;
153 }
154
155 static hb_bool_t
156 hb_ft_get_v_kerning (hb_font_t *font HB_UNUSED,
157                      void *font_data,
158                      hb_codepoint_t top_glyph,
159                      hb_codepoint_t bottom_glyph,
160                      hb_position_t *x_kern,
161                      hb_position_t *y_kern,
162                      void *user_data HB_UNUSED)
163 {
164   /* FreeType API doesn't support vertical kerning */
165   return FALSE;
166 }
167
168 static hb_bool_t
169 hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
170                          void *font_data,
171                          hb_codepoint_t glyph,
172                          hb_bool_t *vertical,
173                          hb_glyph_extents_t *extents,
174                          void *user_data HB_UNUSED)
175 {
176   FT_Face ft_face = (FT_Face) font_data;
177   int load_flags = FT_LOAD_DEFAULT;
178
179   /* TODO: load_flags, embolden, etc, shape/transform */
180
181   if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
182     return FALSE;
183
184   /* XXX: A few negations should be in order here, not sure. */
185   extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
186   extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
187   extents->width = ft_face->glyph->metrics.width;
188   extents->height = ft_face->glyph->metrics.height;
189   return TRUE;
190 }
191
192 static hb_bool_t
193 hb_ft_get_contour_point (hb_font_t *font HB_UNUSED,
194                          void *font_data,
195                          hb_codepoint_t glyph,
196                          unsigned int point_index,
197                          hb_bool_t *vertical,
198                          hb_position_t *x,
199                          hb_position_t *y,
200                          void *user_data HB_UNUSED)
201 {
202   FT_Face ft_face = (FT_Face) font_data;
203   int load_flags = FT_LOAD_DEFAULT;
204
205   if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
206       return FALSE;
207
208   if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
209       return FALSE;
210
211   if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
212       return FALSE;
213
214   *x = ft_face->glyph->outline.points[point_index].x;
215   *y = ft_face->glyph->outline.points[point_index].y;
216   *vertical = FALSE; /* We always return position in horizontal coordinates */
217
218   return TRUE;
219 }
220
221 static hb_font_funcs_t ft_ffuncs = {
222   HB_OBJECT_HEADER_STATIC,
223
224   TRUE, /* immutable */
225
226   {
227     hb_ft_get_glyph,
228     hb_ft_get_glyph_h_advance,
229     hb_ft_get_glyph_v_advance,
230     hb_ft_get_glyph_v_origin,
231     hb_ft_get_h_kerning,
232     hb_ft_get_v_kerning,
233     hb_ft_get_glyph_extents,
234     hb_ft_get_contour_point,
235   }
236 };
237
238 hb_font_funcs_t *
239 hb_ft_get_font_funcs (void)
240 {
241   return &ft_ffuncs;
242 }
243
244
245 static hb_blob_t *
246 get_table  (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
247 {
248   FT_Face ft_face = (FT_Face) user_data;
249   FT_Byte *buffer;
250   FT_ULong  length = 0;
251   FT_Error error;
252
253   if (unlikely (tag == HB_TAG_NONE))
254     return NULL;
255
256   error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
257   if (error)
258     return NULL;
259
260   buffer = (FT_Byte *) malloc (length);
261   if (buffer == NULL)
262     return NULL;
263
264   error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
265   if (error)
266     return NULL;
267
268   return hb_blob_create ((const char *) buffer, length,
269                          HB_MEMORY_MODE_WRITABLE,
270                          buffer, free);
271 }
272
273
274 hb_face_t *
275 hb_ft_face_create (FT_Face           ft_face,
276                    hb_destroy_func_t destroy)
277 {
278   hb_face_t *face;
279
280   if (ft_face->stream->read == NULL) {
281     hb_blob_t *blob;
282
283     blob = hb_blob_create ((const char *) ft_face->stream->base,
284                            (unsigned int) ft_face->stream->size,
285                            /* TODO: We assume that it's mmap()'ed, but FreeType code
286                             * suggests that there are cases we reach here but font is
287                             * not mmapped.  For example, when mmap() fails.  No idea
288                             * how to deal with it better here. */
289                            HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE,
290                            ft_face, destroy);
291     face = hb_face_create (blob, ft_face->face_index);
292     hb_blob_destroy (blob);
293   } else {
294     face = hb_face_create_for_tables (get_table, ft_face, destroy);
295   }
296
297   return face;
298 }
299
300 static void
301 hb_ft_face_finalize (FT_Face ft_face)
302 {
303   hb_face_destroy ((hb_face_t *) ft_face->generic.data);
304 }
305
306 hb_face_t *
307 hb_ft_face_create_cached (FT_Face ft_face)
308 {
309   if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
310   {
311     if (ft_face->generic.finalizer)
312       ft_face->generic.finalizer (ft_face);
313
314     ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
315     ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
316   }
317
318   return hb_face_reference ((hb_face_t *) ft_face->generic.data);
319 }
320
321
322 hb_font_t *
323 hb_ft_font_create (FT_Face           ft_face,
324                    hb_destroy_func_t destroy)
325 {
326   hb_font_t *font;
327   hb_face_t *face;
328
329   face = hb_ft_face_create (ft_face, destroy);
330   font = hb_font_create (face);
331   hb_face_destroy (face);
332   hb_font_set_funcs (font,
333                      hb_ft_get_font_funcs (),
334                      ft_face, NULL);
335   hb_font_set_scale (font,
336                      ((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM) >> 16,
337                      ((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM) >> 16);
338   hb_font_set_ppem (font,
339                     ft_face->size->metrics.x_ppem,
340                     ft_face->size->metrics.y_ppem);
341
342   return font;
343 }
344
345
346 HB_END_DECLS