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