2 * Copyright © 2011 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
29 /* Unit tests for hb-font.h */
32 static const char test_data[] = "test\0data";
36 test_face_empty (void)
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));
42 g_assert (hb_face_reference_table (hb_face_get_empty (), HB_TAG ('h','e','a','d')) == hb_blob_get_empty ());
44 g_assert_cmpint (hb_face_get_upem (hb_face_get_empty ()), ==, 1000);
48 test_face_create (void)
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);
57 g_assert (hb_face_reference_table (face, HB_TAG ('h','e','a','d')) == hb_blob_get_empty ());
59 g_assert_cmpint (hb_face_get_upem (face), ==, 1000);
61 hb_face_destroy (face);
66 free_up (void *user_data)
68 int *freed = (int *) user_data;
76 get_table (hb_face_t *face, hb_tag_t tag, void *user_data)
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);
81 return hb_blob_get_empty ();
85 test_face_createfortables (void)
93 face = hb_face_create_for_tables (get_table, &freed, free_up);
96 g_assert (hb_face_reference_table (face, HB_TAG ('h','e','a','d')) == hb_blob_get_empty ());
98 blob = hb_face_reference_table (face, HB_TAG ('a','b','c','d'));
99 g_assert (blob != hb_blob_get_empty ());
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);
106 g_assert_cmpint (hb_face_get_upem (face), ==, 1000);
108 hb_face_destroy (face);
113 _test_font_nil_funcs (hb_font_t *font)
116 hb_glyph_extents_t extents;
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);
124 hb_font_get_glyph_advance (font, 17, &x, &y);
125 g_assert_cmpint (x, ==, 0);
126 g_assert_cmpint (y, ==, 0);
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);
136 g_assert (0 == hb_font_get_glyph (font, 17, 2));
139 hb_font_get_kerning (font, 17, 19, &x, &y);
140 g_assert_cmpint (x, ==, 0);
141 g_assert_cmpint (y, ==, 0);
145 _test_fontfuncs_nil (hb_font_funcs_t *ffuncs)
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);
160 hb_font_set_funcs (font, ffuncs, &freed, free_up);
161 g_assert_cmpint (freed, ==, 0);
163 _test_font_nil_funcs (font);
165 subfont = hb_font_create_sub_font (font);
167 g_assert_cmpint (freed, ==, 0);
168 hb_font_destroy (font);
169 g_assert_cmpint (freed, ==, 0);
171 _test_font_nil_funcs (subfont);
173 hb_font_destroy (subfont);
174 g_assert_cmpint (freed, ==, 1);
178 test_fontfuncs_empty (void)
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 ());
186 test_fontfuncs_custom (void)
188 hb_font_funcs_t *ffuncs;
190 ffuncs = hb_font_funcs_create ();
192 g_assert (!hb_font_funcs_is_immutable (ffuncs));
193 _test_fontfuncs_nil (hb_font_funcs_get_empty ());
195 hb_font_funcs_destroy (ffuncs);
200 test_font_empty (void)
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 ()));
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);
213 test_font_properties (void)
219 int x_scale, y_scale;
220 unsigned int x_ppem, y_ppem;
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);
229 g_assert (hb_font_get_face (font) == face);
230 g_assert (hb_font_get_parent (font) == NULL);
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);
247 hb_font_set_scale (font, 17, 19);
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);
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);
269 hb_font_set_ppem (font, 17, 19);
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);
277 /* Check immutable */
279 g_assert (!hb_font_is_immutable (font));
280 hb_font_make_immutable (font);
281 g_assert (hb_font_is_immutable (font));
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);
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);
297 subfont = hb_font_create_sub_font (font);
298 hb_font_destroy (font);
300 g_assert (hb_font_get_parent (subfont) == font);
301 g_assert (hb_font_get_face (subfont) == face);
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);
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);
333 hb_font_destroy (subfont);
337 main (int argc, char **argv)
339 hb_test_init (&argc, &argv);
341 hb_test_add (test_face_empty);
342 hb_test_add (test_face_create);
343 hb_test_add (test_face_createfortables);
345 hb_test_add (test_fontfuncs_empty);
346 hb_test_add (test_fontfuncs_custom);
348 hb_test_add (test_font_empty);
349 hb_test_add (test_font_properties);
352 return hb_test_run();