Cosmetic
[framework/uifw/harfbuzz.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, an OpenType Layout engine 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_face_t *face, const void *user_data,
38                  hb_codepoint_t unicode, hb_codepoint_t variation_selector)
39 {
40   FT_Face ft_face = (FT_Face) user_data;
41
42   if (HB_UNLIKELY (variation_selector)) {
43     hb_codepoint_t glyph = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector);
44     if (glyph)
45       return glyph;
46   }
47
48   return FT_Get_Char_Index (ft_face, unicode);
49 }
50
51 static hb_bool_t
52 hb_ft_get_contour_point (hb_font_t *font, hb_face_t *face, const void *user_data,
53                          unsigned int point_index,
54                          hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y)
55 {
56   FT_Face ft_face = (FT_Face) user_data;
57   int load_flags = FT_LOAD_DEFAULT;
58
59   /* TODO: load_flags, embolden, etc */
60
61   if (HB_UNLIKELY (FT_Load_Glyph (ft_face, glyph, load_flags)))
62       return FALSE;
63
64   if (HB_UNLIKELY (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
65       return FALSE;
66
67   if (HB_UNLIKELY (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
68       return FALSE;
69
70   *x = ft_face->glyph->outline.points[point_index].x;
71   *y = ft_face->glyph->outline.points[point_index].y;
72
73   return TRUE;
74 }
75
76 static void
77 hb_ft_get_glyph_metrics (hb_font_t *font, hb_face_t *face, const void *user_data,
78                          hb_codepoint_t glyph, hb_glyph_metrics_t *metrics)
79 {
80   FT_Face ft_face = (FT_Face) user_data;
81   int load_flags = FT_LOAD_DEFAULT;
82
83   /* TODO: load_flags, embolden, etc */
84
85   metrics->x_advance = metrics->y_advance = 0;
86   metrics->x_offset = metrics->y_offset = 0;
87   metrics->width = metrics->height = 0;
88   if (HB_LIKELY (!FT_Load_Glyph (ft_face, glyph, load_flags)))
89   {
90     /* TODO: A few negations should be in order here, not sure. */
91     metrics->x_advance = ft_face->glyph->advance.x;
92     metrics->y_advance = ft_face->glyph->advance.y;
93     metrics->x_offset = ft_face->glyph->metrics.horiBearingX;
94     metrics->y_offset = ft_face->glyph->metrics.horiBearingY;
95     metrics->width = ft_face->glyph->metrics.width;
96     metrics->height = ft_face->glyph->metrics.height;
97   }
98 }
99
100 static hb_position_t
101 hb_ft_get_kerning (hb_font_t *font, hb_face_t *face, const void *user_data,
102                    hb_codepoint_t first_glyph, hb_codepoint_t second_glyph)
103 {
104   FT_Face ft_face = (FT_Face) user_data;
105   FT_Vector kerning;
106
107   /* TODO: Kern type? */
108   if (FT_Get_Kerning (ft_face, first_glyph, second_glyph, FT_KERNING_DEFAULT, &kerning))
109       return 0;
110
111   return kerning.x;
112 }
113
114 static hb_font_funcs_t ft_ffuncs = {
115   HB_REFERENCE_COUNT_INVALID, /* ref_count */
116
117   TRUE, /* immutable */
118
119   hb_ft_get_glyph,
120   hb_ft_get_contour_point,
121   hb_ft_get_glyph_metrics,
122   hb_ft_get_kerning
123 };
124
125 hb_font_funcs_t *
126 hb_ft_get_font_funcs (void)
127 {
128   return &ft_ffuncs;
129 }
130
131
132 static hb_blob_t *
133 _get_table  (hb_tag_t tag, void *user_data)
134 {
135   FT_Face ft_face = (FT_Face) user_data;
136   FT_Byte *buffer;
137   FT_ULong  length = 0;
138   FT_Error error;
139
140   error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
141   if (error)
142     return hb_blob_create_empty ();
143
144   /* TODO Use FT_Memory? */
145   buffer = malloc (length);
146   if (buffer == NULL)
147     return hb_blob_create_empty ();
148
149   error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
150   if (error)
151     return hb_blob_create_empty ();
152
153   return hb_blob_create ((const char *) buffer, length,
154                          HB_MEMORY_MODE_WRITABLE,
155                          free, buffer);
156 }
157
158
159 hb_face_t *
160 hb_ft_face_create (FT_Face           ft_face,
161                    hb_destroy_func_t destroy)
162 {
163   hb_face_t *face;
164
165   /* TODO: Handle NULL ft_face (in other places too */
166   if (ft_face->stream->base != NULL) {
167     hb_blob_t *blob;
168
169     blob = hb_blob_create ((const char *) ft_face->stream->base,
170                            (unsigned int) ft_face->stream->size,
171                            HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE,
172                            destroy, ft_face);
173     face = hb_face_create_for_data (blob, ft_face->face_index);
174     hb_blob_destroy (blob);
175   } else {
176     face = hb_face_create_for_tables (_get_table, destroy, ft_face);
177   }
178
179   return face;
180 }
181
182 hb_face_t *
183 hb_ft_face_create_cached (FT_Face ft_face)
184 {
185   /* TODO: Not thread-safe */
186   if (HB_UNLIKELY (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_face_destroy))
187   {
188     if (ft_face->generic.finalizer)
189       ft_face->generic.finalizer (ft_face->generic.data);
190
191     ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
192     ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_face_destroy;
193   }
194
195   return hb_face_reference (ft_face->generic.data);
196 }
197
198
199 hb_font_t *
200 hb_ft_font_create (FT_Face           ft_face,
201                    hb_destroy_func_t destroy)
202 {
203   hb_font_t *font;
204
205   font = hb_font_create ();
206   hb_font_set_funcs (font,
207                      hb_ft_get_font_funcs (),
208                      destroy, ft_face);
209   hb_font_set_scale (font,
210                      ft_face->size->metrics.x_scale,
211                      ft_face->size->metrics.y_scale);
212   hb_font_set_ppem (font,
213                     ft_face->size->metrics.x_ppem,
214                     ft_face->size->metrics.y_ppem);
215
216   return font;
217 }