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 hb_face_t *created_from_empty;
39 hb_face_t *created_from_null;
41 g_assert (hb_face_get_empty ());
43 created_from_empty = hb_face_create (hb_blob_get_empty (), 0);
44 g_assert (hb_face_get_empty () != created_from_empty);
46 created_from_null = hb_face_create (NULL, 0);
47 g_assert (hb_face_get_empty () != created_from_null);
49 g_assert (hb_face_reference_table (hb_face_get_empty (), HB_TAG ('h','e','a','d')) == hb_blob_get_empty ());
51 g_assert_cmpint (hb_face_get_upem (hb_face_get_empty ()), ==, 1000);
53 hb_face_destroy (created_from_null);
54 hb_face_destroy (created_from_empty);
58 test_face_create (void)
63 blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
64 face = hb_face_create (blob, 0);
65 hb_blob_destroy (blob);
67 g_assert (hb_face_reference_table (face, HB_TAG ('h','e','a','d')) == hb_blob_get_empty ());
69 g_assert_cmpint (hb_face_get_upem (face), ==, 1000);
71 hb_face_destroy (face);
76 free_up (void *user_data)
78 int *freed = (int *) user_data;
86 get_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data HB_UNUSED)
88 if (tag == HB_TAG ('a','b','c','d'))
89 return hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
91 return hb_blob_get_empty ();
95 test_face_createfortables (void)
103 face = hb_face_create_for_tables (get_table, &freed, free_up);
106 g_assert (hb_face_reference_table (face, HB_TAG ('h','e','a','d')) == hb_blob_get_empty ());
108 blob = hb_face_reference_table (face, HB_TAG ('a','b','c','d'));
109 g_assert (blob != hb_blob_get_empty ());
111 data = hb_blob_get_data (blob, &len);
112 g_assert_cmpint (len, ==, sizeof (test_data));
113 g_assert (0 == memcmp (data, test_data, sizeof (test_data)));
114 hb_blob_destroy (blob);
116 g_assert_cmpint (hb_face_get_upem (face), ==, 1000);
118 hb_face_destroy (face);
123 _test_font_nil_funcs (hb_font_t *font)
125 hb_codepoint_t glyph;
127 hb_glyph_extents_t extents;
128 unsigned int upem = hb_face_get_upem (hb_font_get_face (font));
131 g_assert (!hb_font_get_glyph_contour_point (font, 17, 2, &x, &y));
132 g_assert_cmpint (x, ==, 0);
133 g_assert_cmpint (y, ==, 0);
135 x = hb_font_get_glyph_h_advance (font, 17);
136 g_assert_cmpint (x, ==, upem);
138 extents.x_bearing = extents.y_bearing = 13;
139 extents.width = extents.height = 15;
140 hb_font_get_glyph_extents (font, 17, &extents);
141 g_assert_cmpint (extents.x_bearing, ==, 0);
142 g_assert_cmpint (extents.y_bearing, ==, 0);
143 g_assert_cmpint (extents.width, ==, 0);
144 g_assert_cmpint (extents.height, ==, 0);
147 g_assert (!hb_font_get_glyph (font, 17, 2, &glyph));
148 g_assert_cmpint (glyph, ==, 0);
150 x = hb_font_get_glyph_h_kerning (font, 17, 19);
151 g_assert_cmpint (x, ==, 0);
155 _test_fontfuncs_nil (hb_font_funcs_t *ffuncs)
163 blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
164 face = hb_face_create (blob, 0);
165 hb_blob_destroy (blob);
166 g_assert (!hb_face_is_immutable (face));
167 font = hb_font_create (face);
169 g_assert (hb_face_is_immutable (face));
170 hb_face_destroy (face);
173 hb_font_set_funcs (font, ffuncs, &freed, free_up);
174 g_assert_cmpint (freed, ==, 0);
176 _test_font_nil_funcs (font);
178 subfont = hb_font_create_sub_font (font);
181 g_assert_cmpint (freed, ==, 0);
182 hb_font_destroy (font);
183 g_assert_cmpint (freed, ==, 0);
185 _test_font_nil_funcs (subfont);
187 hb_font_destroy (subfont);
188 g_assert_cmpint (freed, ==, 1);
192 test_fontfuncs_empty (void)
194 g_assert (hb_font_funcs_get_empty ());
195 g_assert (hb_font_funcs_is_immutable (hb_font_funcs_get_empty ()));
196 _test_fontfuncs_nil (hb_font_funcs_get_empty ());
200 test_fontfuncs_nil (void)
202 hb_font_funcs_t *ffuncs;
204 ffuncs = hb_font_funcs_create ();
206 g_assert (!hb_font_funcs_is_immutable (ffuncs));
207 _test_fontfuncs_nil (hb_font_funcs_get_empty ());
209 hb_font_funcs_destroy (ffuncs);
213 contour_point_func1 (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED,
214 hb_codepoint_t glyph, unsigned int point_index HB_UNUSED,
215 hb_position_t *x, hb_position_t *y,
216 void *user_data HB_UNUSED)
233 contour_point_func2 (hb_font_t *font, void *font_data HB_UNUSED,
234 hb_codepoint_t glyph, unsigned int point_index,
235 hb_position_t *x, hb_position_t *y,
236 void *user_data HB_UNUSED)
244 return hb_font_get_glyph_contour_point (hb_font_get_parent (font),
245 glyph, point_index, x, y);
249 glyph_h_advance_func1 (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED,
250 hb_codepoint_t glyph,
251 void *user_data HB_UNUSED)
260 test_fontfuncs_subclassing (void)
265 hb_font_funcs_t *ffuncs1;
266 hb_font_funcs_t *ffuncs2;
275 blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
276 face = hb_face_create (blob, 0);
277 hb_blob_destroy (blob);
278 font1 = hb_font_create (face);
279 hb_face_destroy (face);
280 hb_font_set_scale (font1, 10, 10);
283 ffuncs1 = hb_font_funcs_create ();
284 hb_font_funcs_set_glyph_contour_point_func (ffuncs1, contour_point_func1, NULL, NULL);
285 hb_font_funcs_set_glyph_h_advance_func (ffuncs1, glyph_h_advance_func1, NULL, NULL);
286 hb_font_set_funcs (font1, ffuncs1, NULL, NULL);
287 hb_font_funcs_destroy (ffuncs1);
290 g_assert (hb_font_get_glyph_contour_point_for_origin (font1, 1, 2, HB_DIRECTION_LTR, &x, &y));
291 g_assert_cmpint (x, ==, 2);
292 g_assert_cmpint (y, ==, 3);
293 g_assert (hb_font_get_glyph_contour_point_for_origin (font1, 2, 5, HB_DIRECTION_LTR, &x, &y));
294 g_assert_cmpint (x, ==, 4);
295 g_assert_cmpint (y, ==, 5);
296 g_assert (!hb_font_get_glyph_contour_point_for_origin (font1, 3, 7, HB_DIRECTION_RTL, &x, &y));
297 g_assert_cmpint (x, ==, 0);
298 g_assert_cmpint (y, ==, 0);
299 x = hb_font_get_glyph_h_advance (font1, 1);
300 g_assert_cmpint (x, ==, 8);
301 x = hb_font_get_glyph_h_advance (font1, 2);
302 g_assert_cmpint (x, ==, 0);
304 /* creating sub-font doesn't make the parent font immutable;
305 * making a font immutable however makes it's lineage immutable.
307 font2 = hb_font_create_sub_font (font1);
308 font3 = hb_font_create_sub_font (font2);
309 g_assert (!hb_font_is_immutable (font1));
310 g_assert (!hb_font_is_immutable (font2));
311 g_assert (!hb_font_is_immutable (font3));
312 hb_font_make_immutable (font3);
313 g_assert (hb_font_is_immutable (font1));
314 g_assert (hb_font_is_immutable (font2));
315 g_assert (hb_font_is_immutable (font3));
316 hb_font_destroy (font2);
317 hb_font_destroy (font3);
319 font2 = hb_font_create_sub_font (font1);
320 hb_font_destroy (font1);
322 /* setup font2 to override some funcs */
323 ffuncs2 = hb_font_funcs_create ();
324 hb_font_funcs_set_glyph_contour_point_func (ffuncs2, contour_point_func2, NULL, NULL);
325 hb_font_set_funcs (font2, ffuncs2, NULL, NULL);
326 hb_font_funcs_destroy (ffuncs2);
329 g_assert (hb_font_get_glyph_contour_point_for_origin (font2, 1, 2, HB_DIRECTION_LTR, &x, &y));
330 g_assert_cmpint (x, ==, 6);
331 g_assert_cmpint (y, ==, 7);
332 g_assert (hb_font_get_glyph_contour_point_for_origin (font2, 2, 5, HB_DIRECTION_RTL, &x, &y));
333 g_assert_cmpint (x, ==, 4);
334 g_assert_cmpint (y, ==, 5);
335 g_assert (!hb_font_get_glyph_contour_point_for_origin (font2, 3, 7, HB_DIRECTION_LTR, &x, &y));
336 g_assert_cmpint (x, ==, 0);
337 g_assert_cmpint (y, ==, 0);
338 x = hb_font_get_glyph_h_advance (font2, 1);
339 g_assert_cmpint (x, ==, 8);
340 x = hb_font_get_glyph_h_advance (font2, 2);
341 g_assert_cmpint (x, ==, 0);
343 /* setup font3 to override scale */
344 font3 = hb_font_create_sub_font (font2);
345 hb_font_set_scale (font3, 20, 30);
348 g_assert (hb_font_get_glyph_contour_point_for_origin (font3, 1, 2, HB_DIRECTION_RTL, &x, &y));
349 g_assert_cmpint (x, ==, 6*2);
350 g_assert_cmpint (y, ==, 7*3);
351 g_assert (hb_font_get_glyph_contour_point_for_origin (font3, 2, 5, HB_DIRECTION_LTR, &x, &y));
352 g_assert_cmpint (x, ==, 4*2);
353 g_assert_cmpint (y, ==, 5*3);
354 g_assert (!hb_font_get_glyph_contour_point_for_origin (font3, 3, 7, HB_DIRECTION_LTR, &x, &y));
355 g_assert_cmpint (x, ==, 0*2);
356 g_assert_cmpint (y, ==, 0*3);
357 x = hb_font_get_glyph_h_advance (font3, 1);
358 g_assert_cmpint (x, ==, 8*2);
359 x = hb_font_get_glyph_h_advance (font3, 2);
360 g_assert_cmpint (x, ==, 0*2);
363 hb_font_destroy (font3);
364 hb_font_destroy (font2);
368 nominal_glyph_func (hb_font_t *font HB_UNUSED,
369 void *font_data HB_UNUSED,
370 hb_codepoint_t unicode HB_UNUSED,
371 hb_codepoint_t *glyph,
372 void *user_data HB_UNUSED)
379 nominal_glyphs_func (hb_font_t *font HB_UNUSED,
380 void *font_data HB_UNUSED,
381 unsigned int count HB_UNUSED,
382 const hb_codepoint_t *first_unicode HB_UNUSED,
383 unsigned int unicode_stride HB_UNUSED,
384 hb_codepoint_t *first_glyph HB_UNUSED,
385 unsigned int glyph_stride HB_UNUSED,
386 void *user_data HB_UNUSED)
392 test_fontfuncs_parallels (void)
397 hb_font_funcs_t *ffuncs1;
398 hb_font_funcs_t *ffuncs2;
403 hb_codepoint_t glyph;
405 blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
406 face = hb_face_create (blob, 0);
407 hb_blob_destroy (blob);
408 font0 = hb_font_create (face);
409 hb_face_destroy (face);
411 /* setup sub-font1 */
412 font1 = hb_font_create_sub_font (font0);
413 hb_font_destroy (font0);
414 ffuncs1 = hb_font_funcs_create ();
415 hb_font_funcs_set_nominal_glyph_func (ffuncs1, nominal_glyph_func, NULL, NULL);
416 hb_font_set_funcs (font1, ffuncs1, NULL, NULL);
417 hb_font_funcs_destroy (ffuncs1);
419 /* setup sub-font2 */
420 font2 = hb_font_create_sub_font (font1);
421 hb_font_destroy (font1);
422 ffuncs2 = hb_font_funcs_create ();
423 hb_font_funcs_set_nominal_glyphs_func (ffuncs1, nominal_glyphs_func, NULL, NULL);
424 hb_font_set_funcs (font2, ffuncs2, NULL, NULL);
425 hb_font_funcs_destroy (ffuncs2);
427 /* Just test that calling get_nominal_glyph doesn't infinite-loop. */
428 hb_font_get_nominal_glyph (font2, 0x0020u, &glyph);
430 hb_font_destroy (font2);
434 test_font_empty (void)
436 hb_font_t *created_from_empty;
437 hb_font_t *created_from_null;
438 hb_font_t *created_sub_from_null;
440 g_assert (hb_font_get_empty ());
442 created_from_empty = hb_font_create (hb_face_get_empty ());
443 g_assert (hb_font_get_empty () != created_from_empty);
445 created_from_null = hb_font_create (NULL);
446 g_assert (hb_font_get_empty () != created_from_null);
448 created_sub_from_null = hb_font_create_sub_font (NULL);
449 g_assert (hb_font_get_empty () != created_sub_from_null);
451 g_assert (hb_font_is_immutable (hb_font_get_empty ()));
453 g_assert (hb_font_get_face (hb_font_get_empty ()) == hb_face_get_empty ());
454 g_assert (hb_font_get_parent (hb_font_get_empty ()) == NULL);
456 hb_font_destroy (created_sub_from_null);
457 hb_font_destroy (created_from_null);
458 hb_font_destroy (created_from_empty);
462 test_font_properties (void)
468 int x_scale, y_scale;
469 unsigned int x_ppem, y_ppem;
472 blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
473 face = hb_face_create (blob, 0);
474 hb_blob_destroy (blob);
475 font = hb_font_create (face);
476 hb_face_destroy (face);
479 g_assert (hb_font_get_face (font) == face);
480 g_assert (hb_font_get_parent (font) == hb_font_get_empty ());
481 subfont = hb_font_create_sub_font (font);
482 g_assert (hb_font_get_parent (subfont) == font);
483 hb_font_set_parent(subfont, NULL);
484 g_assert (hb_font_get_parent (subfont) == hb_font_get_empty());
485 hb_font_set_parent(subfont, font);
486 g_assert (hb_font_get_parent (subfont) == font);
487 hb_font_set_parent(subfont, NULL);
488 hb_font_make_immutable (subfont);
489 g_assert (hb_font_get_parent (subfont) == hb_font_get_empty());
490 hb_font_set_parent(subfont, font);
491 g_assert (hb_font_get_parent (subfont) == hb_font_get_empty());
492 hb_font_destroy (subfont);
497 upem = hb_face_get_upem (hb_font_get_face (font));
498 hb_font_get_scale (font, NULL, NULL);
499 x_scale = y_scale = 13;
500 hb_font_get_scale (font, &x_scale, NULL);
501 g_assert_cmpint (x_scale, ==, upem);
502 x_scale = y_scale = 13;
503 hb_font_get_scale (font, NULL, &y_scale);
504 g_assert_cmpint (y_scale, ==, upem);
505 x_scale = y_scale = 13;
506 hb_font_get_scale (font, &x_scale, &y_scale);
507 g_assert_cmpint (x_scale, ==, upem);
508 g_assert_cmpint (y_scale, ==, upem);
510 hb_font_set_scale (font, 17, 19);
512 x_scale = y_scale = 13;
513 hb_font_get_scale (font, &x_scale, &y_scale);
514 g_assert_cmpint (x_scale, ==, 17);
515 g_assert_cmpint (y_scale, ==, 19);
520 hb_font_get_ppem (font, NULL, NULL);
521 x_ppem = y_ppem = 13;
522 hb_font_get_ppem (font, &x_ppem, NULL);
523 g_assert_cmpint (x_ppem, ==, 0);
524 x_ppem = y_ppem = 13;
525 hb_font_get_ppem (font, NULL, &y_ppem);
526 g_assert_cmpint (y_ppem, ==, 0);
527 x_ppem = y_ppem = 13;
528 hb_font_get_ppem (font, &x_ppem, &y_ppem);
529 g_assert_cmpint (x_ppem, ==, 0);
530 g_assert_cmpint (y_ppem, ==, 0);
532 hb_font_set_ppem (font, 17, 19);
534 x_ppem = y_ppem = 13;
535 hb_font_get_ppem (font, &x_ppem, &y_ppem);
536 g_assert_cmpint (x_ppem, ==, 17);
537 g_assert_cmpint (y_ppem, ==, 19);
540 g_assert_cmpint (hb_font_get_ptem (font), ==, 0);
541 hb_font_set_ptem (font, 42);
542 g_assert_cmpint (hb_font_get_ptem (font), ==, 42);
545 /* Check immutable */
547 g_assert (!hb_font_is_immutable (font));
548 hb_font_make_immutable (font);
549 g_assert (hb_font_is_immutable (font));
551 hb_font_set_scale (font, 10, 12);
552 x_scale = y_scale = 13;
553 hb_font_get_scale (font, &x_scale, &y_scale);
554 g_assert_cmpint (x_scale, ==, 17);
555 g_assert_cmpint (y_scale, ==, 19);
557 hb_font_set_ppem (font, 10, 12);
558 x_ppem = y_ppem = 13;
559 hb_font_get_ppem (font, &x_ppem, &y_ppem);
560 g_assert_cmpint (x_ppem, ==, 17);
561 g_assert_cmpint (y_ppem, ==, 19);
565 subfont = hb_font_create_sub_font (font);
566 hb_font_destroy (font);
568 g_assert (hb_font_get_parent (subfont) == font);
569 g_assert (hb_font_get_face (subfont) == face);
572 x_scale = y_scale = 13;
573 hb_font_get_scale (subfont, &x_scale, &y_scale);
574 g_assert_cmpint (x_scale, ==, 17);
575 g_assert_cmpint (y_scale, ==, 19);
576 hb_font_set_scale (subfont, 10, 12);
577 x_scale = y_scale = 13;
578 hb_font_get_scale (subfont, &x_scale, &y_scale);
579 g_assert_cmpint (x_scale, ==, 10);
580 g_assert_cmpint (y_scale, ==, 12);
581 x_scale = y_scale = 13;
582 hb_font_get_scale (font, &x_scale, &y_scale);
583 g_assert_cmpint (x_scale, ==, 17);
584 g_assert_cmpint (y_scale, ==, 19);
587 x_ppem = y_ppem = 13;
588 hb_font_get_ppem (subfont, &x_ppem, &y_ppem);
589 g_assert_cmpint (x_ppem, ==, 17);
590 g_assert_cmpint (y_ppem, ==, 19);
591 hb_font_set_ppem (subfont, 10, 12);
592 x_ppem = y_ppem = 13;
593 hb_font_get_ppem (subfont, &x_ppem, &y_ppem);
594 g_assert_cmpint (x_ppem, ==, 10);
595 g_assert_cmpint (y_ppem, ==, 12);
596 x_ppem = y_ppem = 13;
597 hb_font_get_ppem (font, &x_ppem, &y_ppem);
598 g_assert_cmpint (x_ppem, ==, 17);
599 g_assert_cmpint (y_ppem, ==, 19);
601 hb_font_destroy (subfont);
605 main (int argc, char **argv)
607 hb_test_init (&argc, &argv);
609 hb_test_add (test_face_empty);
610 hb_test_add (test_face_create);
611 hb_test_add (test_face_createfortables);
613 hb_test_add (test_fontfuncs_empty);
614 hb_test_add (test_fontfuncs_nil);
615 hb_test_add (test_fontfuncs_subclassing);
616 hb_test_add (test_fontfuncs_parallels);
618 hb_test_add (test_font_empty);
619 hb_test_add (test_font_properties);
621 return hb_test_run();