[test/font] Test empty funcs
[framework/uifw/harfbuzz.git] / test / test-font.c
1 /*
2  * Copyright © 2011  Google, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Google Author(s): Behdad Esfahbod
25  */
26
27 #include "hb-test.h"
28
29 /* Unit tests for hb-font.h */
30
31
32 static const char test_data[] = "test\0data";
33
34
35 static void
36 test_face_empty (void)
37 {
38   g_assert (hb_face_get_empty ());
39   g_assert (hb_face_get_empty () == hb_face_create (hb_blob_get_empty (), 0));
40   g_assert (hb_face_get_empty () == hb_face_create (NULL, 0));
41
42   g_assert (hb_face_reference_table (hb_face_get_empty (), HB_TAG ('h','e','a','d')) == hb_blob_get_empty ());
43
44   g_assert_cmpint (hb_face_get_upem (hb_face_get_empty ()), ==, 1000);
45 }
46
47 static void
48 test_face_create (void)
49 {
50   hb_face_t *face;
51   hb_blob_t *blob;
52
53   blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
54   face = hb_face_create (blob, 0);
55   hb_blob_destroy (blob);
56
57   g_assert (hb_face_reference_table (face, HB_TAG ('h','e','a','d')) == hb_blob_get_empty ());
58
59   g_assert_cmpint (hb_face_get_upem (face), ==, 1000);
60
61   hb_face_destroy (face);
62 }
63
64
65 static void
66 free_up (void *user_data)
67 {
68   int *freed = (int *) user_data;
69
70   g_assert (!*freed);
71
72   (*freed)++;
73 }
74
75 static hb_blob_t *
76 get_table (hb_face_t *face, hb_tag_t tag, void *user_data)
77 {
78   if (tag == HB_TAG ('a','b','c','d'))
79     return hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
80
81   return hb_blob_get_empty ();
82 }
83
84 static void
85 test_face_createfortables (void)
86 {
87   hb_face_t *face;
88   hb_blob_t *blob;
89   const char *data;
90   unsigned int len;
91   int freed = 0;
92
93   face = hb_face_create_for_tables (get_table, &freed, free_up);
94   g_assert (!freed);
95
96   g_assert (hb_face_reference_table (face, HB_TAG ('h','e','a','d')) == hb_blob_get_empty ());
97
98   blob = hb_face_reference_table (face, HB_TAG ('a','b','c','d'));
99   g_assert (blob != hb_blob_get_empty ());
100
101   data = hb_blob_get_data (blob, &len);
102   g_assert_cmpint (len, ==, sizeof (test_data));
103   g_assert (0 == memcmp (data, test_data, sizeof (test_data)));
104   hb_blob_destroy (blob);
105
106   g_assert_cmpint (hb_face_get_upem (face), ==, 1000);
107
108   hb_face_destroy (face);
109   g_assert (freed);
110 }
111
112 static void
113 _test_font_nil_funcs (hb_font_t *font)
114 {
115   hb_position_t x, y;
116   hb_glyph_extents_t extents;
117
118   x = y = 13;
119   g_assert (!hb_font_get_contour_point (font, 17, 2, &x, &y));
120   g_assert_cmpint (x, ==, 0);
121   g_assert_cmpint (y, ==, 0);
122
123   x = y = 13;
124   hb_font_get_glyph_advance (font, 17, &x, &y);
125   g_assert_cmpint (x, ==, 0);
126   g_assert_cmpint (y, ==, 0);
127
128   extents.x_bearing = extents.y_bearing = 13;
129   extents.width = extents.height = 15;
130   hb_font_get_glyph_extents (font, 17, &extents);
131   g_assert_cmpint (extents.x_bearing, ==, 0);
132   g_assert_cmpint (extents.y_bearing, ==, 0);
133   g_assert_cmpint (extents.width, ==, 0);
134   g_assert_cmpint (extents.height, ==, 0);
135
136   g_assert (0 == hb_font_get_glyph (font, 17, 2));
137
138   x = y = 13;
139   hb_font_get_kerning (font, 17, 19, &x, &y);
140   g_assert_cmpint (x, ==, 0);
141   g_assert_cmpint (y, ==, 0);
142 }
143
144 static void
145 _test_fontfuncs_nil (hb_font_funcs_t *ffuncs)
146 {
147   hb_blob_t *blob;
148   hb_face_t *face;
149   hb_font_t *font;
150   hb_font_t *subfont;
151   int freed = 0;
152
153   blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
154   face = hb_face_create (blob, 0);
155   hb_blob_destroy (blob);
156   font = hb_font_create (face);
157   hb_face_destroy (face);
158
159
160   hb_font_set_funcs (font, ffuncs, &freed, free_up);
161   g_assert_cmpint (freed, ==, 0);
162
163   _test_font_nil_funcs (font);
164
165   subfont = hb_font_create_sub_font (font);
166
167   g_assert_cmpint (freed, ==, 0);
168   hb_font_destroy (font);
169   g_assert_cmpint (freed, ==, 0);
170
171   _test_font_nil_funcs (subfont);
172
173   hb_font_destroy (subfont);
174   g_assert_cmpint (freed, ==, 1);
175 }
176
177 static void
178 test_fontfuncs_empty (void)
179 {
180   g_assert (hb_font_funcs_get_empty ());
181   g_assert (hb_font_funcs_is_immutable (hb_font_funcs_get_empty ()));
182   _test_fontfuncs_nil (hb_font_funcs_get_empty ());
183 }
184
185 static void
186 test_fontfuncs_custom (void)
187 {
188   hb_font_funcs_t *ffuncs;
189
190   ffuncs = hb_font_funcs_create ();
191
192   g_assert (!hb_font_funcs_is_immutable (ffuncs));
193   _test_fontfuncs_nil (hb_font_funcs_get_empty ());
194
195   hb_font_funcs_destroy (ffuncs);
196 }
197
198
199 static void
200 test_font_empty (void)
201 {
202   g_assert (hb_font_get_empty ());
203   g_assert (hb_font_get_empty () == hb_font_create (hb_face_get_empty ()));
204   g_assert (hb_font_get_empty () == hb_font_create (NULL));
205   g_assert (hb_font_get_empty () == hb_font_create_sub_font (NULL));
206   g_assert (hb_font_is_immutable (hb_font_get_empty ()));
207
208   g_assert (hb_font_get_face (hb_font_get_empty ()) == hb_face_get_empty ());
209   g_assert (hb_font_get_parent (hb_font_get_empty ()) == NULL);
210 }
211
212 static void
213 test_font_properties (void)
214 {
215   hb_blob_t *blob;
216   hb_face_t *face;
217   hb_font_t *font;
218   hb_font_t *subfont;
219   int x_scale, y_scale;
220   unsigned int x_ppem, y_ppem;
221
222   blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
223   face = hb_face_create (blob, 0);
224   hb_blob_destroy (blob);
225   font = hb_font_create (face);
226   hb_face_destroy (face);
227
228
229   g_assert (hb_font_get_face (font) == face);
230   g_assert (hb_font_get_parent (font) == NULL);
231
232
233   /* Check scale */
234
235   hb_font_get_scale (font, NULL, NULL);
236   x_scale = y_scale = 13;
237   hb_font_get_scale (font, &x_scale, NULL);
238   g_assert_cmpint (x_scale, ==, 0);
239   x_scale = y_scale = 13;
240   hb_font_get_scale (font, NULL, &y_scale);
241   g_assert_cmpint (y_scale, ==, 0);
242   x_scale = y_scale = 13;
243   hb_font_get_scale (font, &x_scale, &y_scale);
244   g_assert_cmpint (x_scale, ==, 0);
245   g_assert_cmpint (y_scale, ==, 0);
246
247   hb_font_set_scale (font, 17, 19);
248
249   x_scale = y_scale = 13;
250   hb_font_get_scale (font, &x_scale, &y_scale);
251   g_assert_cmpint (x_scale, ==, 17);
252   g_assert_cmpint (y_scale, ==, 19);
253
254
255   /* Check ppem */
256
257   hb_font_get_ppem (font, NULL, NULL);
258   x_ppem = y_ppem = 13;
259   hb_font_get_ppem (font, &x_ppem, NULL);
260   g_assert_cmpint (x_ppem, ==, 0);
261   x_ppem = y_ppem = 13;
262   hb_font_get_ppem (font, NULL, &y_ppem);
263   g_assert_cmpint (y_ppem, ==, 0);
264   x_ppem = y_ppem = 13;
265   hb_font_get_ppem (font, &x_ppem, &y_ppem);
266   g_assert_cmpint (x_ppem, ==, 0);
267   g_assert_cmpint (y_ppem, ==, 0);
268
269   hb_font_set_ppem (font, 17, 19);
270
271   x_ppem = y_ppem = 13;
272   hb_font_get_ppem (font, &x_ppem, &y_ppem);
273   g_assert_cmpint (x_ppem, ==, 17);
274   g_assert_cmpint (y_ppem, ==, 19);
275
276
277   /* Check immutable */
278
279   g_assert (!hb_font_is_immutable (font));
280   hb_font_make_immutable (font);
281   g_assert (hb_font_is_immutable (font));
282
283   hb_font_set_scale (font, 10, 12);
284   x_scale = y_scale = 13;
285   hb_font_get_scale (font, &x_scale, &y_scale);
286   g_assert_cmpint (x_scale, ==, 17);
287   g_assert_cmpint (y_scale, ==, 19);
288
289   hb_font_set_ppem (font, 10, 12);
290   x_ppem = y_ppem = 13;
291   hb_font_get_ppem (font, &x_ppem, &y_ppem);
292   g_assert_cmpint (x_ppem, ==, 17);
293   g_assert_cmpint (y_ppem, ==, 19);
294
295
296   /* sub_font now */
297   subfont = hb_font_create_sub_font (font);
298   hb_font_destroy (font);
299
300   g_assert (hb_font_get_parent (subfont) == font);
301   g_assert (hb_font_get_face (subfont) == face);
302
303   /* scale */
304   x_scale = y_scale = 13;
305   hb_font_get_scale (subfont, &x_scale, &y_scale);
306   g_assert_cmpint (x_scale, ==, 17);
307   g_assert_cmpint (y_scale, ==, 19);
308   hb_font_set_scale (subfont, 10, 12);
309   x_scale = y_scale = 13;
310   hb_font_get_scale (subfont, &x_scale, &y_scale);
311   g_assert_cmpint (x_scale, ==, 10);
312   g_assert_cmpint (y_scale, ==, 12);
313   x_scale = y_scale = 13;
314   hb_font_get_scale (font, &x_scale, &y_scale);
315   g_assert_cmpint (x_scale, ==, 17);
316   g_assert_cmpint (y_scale, ==, 19);
317
318   /* ppem */
319   x_ppem = y_ppem = 13;
320   hb_font_get_ppem (subfont, &x_ppem, &y_ppem);
321   g_assert_cmpint (x_ppem, ==, 17);
322   g_assert_cmpint (y_ppem, ==, 19);
323   hb_font_set_ppem (subfont, 10, 12);
324   x_ppem = y_ppem = 13;
325   hb_font_get_ppem (subfont, &x_ppem, &y_ppem);
326   g_assert_cmpint (x_ppem, ==, 10);
327   g_assert_cmpint (y_ppem, ==, 12);
328   x_ppem = y_ppem = 13;
329   hb_font_get_ppem (font, &x_ppem, &y_ppem);
330   g_assert_cmpint (x_ppem, ==, 17);
331   g_assert_cmpint (y_ppem, ==, 19);
332
333   hb_font_destroy (subfont);
334 }
335
336 int
337 main (int argc, char **argv)
338 {
339   hb_test_init (&argc, &argv);
340
341   hb_test_add (test_face_empty);
342   hb_test_add (test_face_create);
343   hb_test_add (test_face_createfortables);
344
345   hb_test_add (test_fontfuncs_empty);
346   hb_test_add (test_fontfuncs_custom);
347
348   hb_test_add (test_font_empty);
349   hb_test_add (test_font_properties);
350
351
352   return hb_test_run();
353 }