1b688343bcba577d08376fc8abbc926463a38146
[apps/home/video-player.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_codepoint_t glyph;
116   hb_position_t x, y;
117   hb_glyph_extents_t extents;
118
119   x = y = 13;
120   g_assert (!hb_font_get_contour_point (font, 17, 2, &x, &y));
121   g_assert_cmpint (x, ==, 0);
122   g_assert_cmpint (y, ==, 0);
123
124   x = y = 13;
125   hb_font_get_glyph_advance (font, 17, &x, &y);
126   g_assert_cmpint (x, ==, 0);
127   g_assert_cmpint (y, ==, 0);
128
129   extents.x_bearing = extents.y_bearing = 13;
130   extents.width = extents.height = 15;
131   hb_font_get_glyph_extents (font, 17, &extents);
132   g_assert_cmpint (extents.x_bearing, ==, 0);
133   g_assert_cmpint (extents.y_bearing, ==, 0);
134   g_assert_cmpint (extents.width, ==, 0);
135   g_assert_cmpint (extents.height, ==, 0);
136
137   glyph = 3;
138   g_assert (!hb_font_get_glyph (font, 17, 2, &glyph));
139   g_assert_cmpint (glyph, ==, 0);
140
141   x = y = 13;
142   hb_font_get_kerning (font, 17, 19, &x, &y);
143   g_assert_cmpint (x, ==, 0);
144   g_assert_cmpint (y, ==, 0);
145 }
146
147 static void
148 _test_fontfuncs_nil (hb_font_funcs_t *ffuncs)
149 {
150   hb_blob_t *blob;
151   hb_face_t *face;
152   hb_font_t *font;
153   hb_font_t *subfont;
154   int freed = 0;
155
156   blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
157   face = hb_face_create (blob, 0);
158   hb_blob_destroy (blob);
159   g_assert (!hb_face_is_immutable (face));
160   font = hb_font_create (face);
161   g_assert (hb_face_is_immutable (face));
162   hb_face_destroy (face);
163
164
165   hb_font_set_funcs (font, ffuncs, &freed, free_up);
166   g_assert_cmpint (freed, ==, 0);
167
168   _test_font_nil_funcs (font);
169
170   subfont = hb_font_create_sub_font (font);
171
172   g_assert_cmpint (freed, ==, 0);
173   hb_font_destroy (font);
174   g_assert_cmpint (freed, ==, 0);
175
176   _test_font_nil_funcs (subfont);
177
178   hb_font_destroy (subfont);
179   g_assert_cmpint (freed, ==, 1);
180 }
181
182 static void
183 test_fontfuncs_empty (void)
184 {
185   g_assert (hb_font_funcs_get_empty ());
186   g_assert (hb_font_funcs_is_immutable (hb_font_funcs_get_empty ()));
187   _test_fontfuncs_nil (hb_font_funcs_get_empty ());
188 }
189
190 static void
191 test_fontfuncs_nil (void)
192 {
193   hb_font_funcs_t *ffuncs;
194
195   ffuncs = hb_font_funcs_create ();
196
197   g_assert (!hb_font_funcs_is_immutable (ffuncs));
198   _test_fontfuncs_nil (hb_font_funcs_get_empty ());
199
200   hb_font_funcs_destroy (ffuncs);
201 }
202
203 static hb_bool_t
204 contour_point_func1 (hb_font_t *font, void *font_data,
205                      hb_codepoint_t glyph, unsigned int point_index,
206                      hb_position_t *x, hb_position_t *y,
207                      void *user_data)
208 {
209   if (glyph == 1) {
210     *x = 2;
211     *y = 3;
212     return TRUE;
213   }
214   if (glyph == 2) {
215     *x = 4;
216     *y = 5;
217     return TRUE;
218   }
219
220   return FALSE;
221 }
222
223 static hb_bool_t
224 contour_point_func2 (hb_font_t *font, void *font_data,
225                      hb_codepoint_t glyph, unsigned int point_index,
226                      hb_position_t *x, hb_position_t *y,
227                      void *user_data)
228 {
229   if (glyph == 1) {
230     *x = 6;
231     *y = 7;
232     return TRUE;
233   }
234
235   return hb_font_get_contour_point (hb_font_get_parent (font),
236                                     glyph, point_index, x, y);
237 }
238
239 static void
240 glyph_advance_func1 (hb_font_t *font, void *font_data,
241                      hb_codepoint_t glyph,
242                      hb_position_t *x_advance, hb_position_t *y_advance,
243                      void *user_data)
244 {
245   if (glyph == 1) {
246     *x_advance = 8;
247     *y_advance = 9;
248   }
249 }
250
251 static void
252 test_fontfuncs_subclassing (void)
253 {
254   hb_blob_t *blob;
255   hb_face_t *face;
256
257   hb_font_funcs_t *ffuncs1;
258   hb_font_funcs_t *ffuncs2;
259
260   hb_font_t *font1;
261   hb_font_t *font2;
262   hb_font_t *font3;
263
264   hb_position_t x;
265   hb_position_t y;
266
267   blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
268   face = hb_face_create (blob, 0);
269   hb_blob_destroy (blob);
270   font1 = hb_font_create (face);
271   hb_face_destroy (face);
272   hb_font_set_scale (font1, 10, 10);
273
274   /* setup font1 */
275   ffuncs1 = hb_font_funcs_create ();
276   hb_font_funcs_set_contour_point_func (ffuncs1, contour_point_func1, NULL, NULL);
277   hb_font_funcs_set_glyph_advance_func (ffuncs1, glyph_advance_func1, NULL, NULL);
278   hb_font_set_funcs (font1, ffuncs1, NULL, NULL);
279   hb_font_funcs_destroy (ffuncs1);
280
281   x = y = 1;
282   g_assert (hb_font_get_contour_point (font1, 1, 2, &x, &y));
283   g_assert_cmpint (x, ==, 2);
284   g_assert_cmpint (y, ==, 3);
285   g_assert (hb_font_get_contour_point (font1, 2, 5, &x, &y));
286   g_assert_cmpint (x, ==, 4);
287   g_assert_cmpint (y, ==, 5);
288   g_assert (!hb_font_get_contour_point (font1, 3, 7, &x, &y));
289   g_assert_cmpint (x, ==, 0);
290   g_assert_cmpint (y, ==, 0);
291   hb_font_get_glyph_advance (font1, 1, &x, &y);
292   g_assert_cmpint (x, ==, 8);
293   g_assert_cmpint (y, ==, 9);
294   hb_font_get_glyph_advance (font1, 2, &x, &y);
295   g_assert_cmpint (x, ==, 0);
296   g_assert_cmpint (y, ==, 0);
297
298
299   font2 = hb_font_create_sub_font (font1);
300   g_assert (hb_font_is_immutable (font1));
301   hb_font_destroy (font1);
302
303   /* setup font2 to override some funcs */
304   ffuncs2 = hb_font_funcs_create ();
305   hb_font_funcs_set_contour_point_func (ffuncs2, contour_point_func2, NULL, NULL);
306   hb_font_set_funcs (font2, ffuncs2, NULL, NULL);
307   hb_font_funcs_destroy (ffuncs2);
308
309   x = y = 1;
310   g_assert (hb_font_get_contour_point (font2, 1, 2, &x, &y));
311   g_assert_cmpint (x, ==, 6);
312   g_assert_cmpint (y, ==, 7);
313   g_assert (hb_font_get_contour_point (font2, 2, 5, &x, &y));
314   g_assert_cmpint (x, ==, 4);
315   g_assert_cmpint (y, ==, 5);
316   g_assert (!hb_font_get_contour_point (font2, 3, 7, &x, &y));
317   g_assert_cmpint (x, ==, 0);
318   g_assert_cmpint (y, ==, 0);
319   hb_font_get_glyph_advance (font2, 1, &x, &y);
320   g_assert_cmpint (x, ==, 8);
321   g_assert_cmpint (y, ==, 9);
322   hb_font_get_glyph_advance (font2, 2, &x, &y);
323   g_assert_cmpint (x, ==, 0);
324   g_assert_cmpint (y, ==, 0);
325
326
327   font3 = hb_font_create_sub_font (font2);
328   g_assert (hb_font_is_immutable (font2));
329   hb_font_destroy (font2);
330
331   /* setup font3 to override scale */
332   hb_font_set_scale (font3, 20, 30);
333
334   x = y = 1;
335   g_assert (hb_font_get_contour_point (font3, 1, 2, &x, &y));
336   g_assert_cmpint (x, ==, 6*2);
337   g_assert_cmpint (y, ==, 7*3);
338   g_assert (hb_font_get_contour_point (font3, 2, 5, &x, &y));
339   g_assert_cmpint (x, ==, 4*2);
340   g_assert_cmpint (y, ==, 5*3);
341   g_assert (!hb_font_get_contour_point (font3, 3, 7, &x, &y));
342   g_assert_cmpint (x, ==, 0*2);
343   g_assert_cmpint (y, ==, 0*3);
344   hb_font_get_glyph_advance (font3, 1, &x, &y);
345   g_assert_cmpint (x, ==, 8*2);
346   g_assert_cmpint (y, ==, 9*3);
347   hb_font_get_glyph_advance (font3, 2, &x, &y);
348   g_assert_cmpint (x, ==, 0*2);
349   g_assert_cmpint (y, ==, 0*3);
350
351
352   hb_font_destroy (font3);
353 }
354
355
356 static void
357 test_font_empty (void)
358 {
359   g_assert (hb_font_get_empty ());
360   g_assert (hb_font_get_empty () == hb_font_create (hb_face_get_empty ()));
361   g_assert (hb_font_get_empty () == hb_font_create (NULL));
362   g_assert (hb_font_get_empty () == hb_font_create_sub_font (NULL));
363   g_assert (hb_font_is_immutable (hb_font_get_empty ()));
364
365   g_assert (hb_font_get_face (hb_font_get_empty ()) == hb_face_get_empty ());
366   g_assert (hb_font_get_parent (hb_font_get_empty ()) == NULL);
367 }
368
369 static void
370 test_font_properties (void)
371 {
372   hb_blob_t *blob;
373   hb_face_t *face;
374   hb_font_t *font;
375   hb_font_t *subfont;
376   int x_scale, y_scale;
377   unsigned int x_ppem, y_ppem;
378
379   blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
380   face = hb_face_create (blob, 0);
381   hb_blob_destroy (blob);
382   font = hb_font_create (face);
383   hb_face_destroy (face);
384
385
386   g_assert (hb_font_get_face (font) == face);
387   g_assert (hb_font_get_parent (font) == NULL);
388
389
390   /* Check scale */
391
392   hb_font_get_scale (font, NULL, NULL);
393   x_scale = y_scale = 13;
394   hb_font_get_scale (font, &x_scale, NULL);
395   g_assert_cmpint (x_scale, ==, 0);
396   x_scale = y_scale = 13;
397   hb_font_get_scale (font, NULL, &y_scale);
398   g_assert_cmpint (y_scale, ==, 0);
399   x_scale = y_scale = 13;
400   hb_font_get_scale (font, &x_scale, &y_scale);
401   g_assert_cmpint (x_scale, ==, 0);
402   g_assert_cmpint (y_scale, ==, 0);
403
404   hb_font_set_scale (font, 17, 19);
405
406   x_scale = y_scale = 13;
407   hb_font_get_scale (font, &x_scale, &y_scale);
408   g_assert_cmpint (x_scale, ==, 17);
409   g_assert_cmpint (y_scale, ==, 19);
410
411
412   /* Check ppem */
413
414   hb_font_get_ppem (font, NULL, NULL);
415   x_ppem = y_ppem = 13;
416   hb_font_get_ppem (font, &x_ppem, NULL);
417   g_assert_cmpint (x_ppem, ==, 0);
418   x_ppem = y_ppem = 13;
419   hb_font_get_ppem (font, NULL, &y_ppem);
420   g_assert_cmpint (y_ppem, ==, 0);
421   x_ppem = y_ppem = 13;
422   hb_font_get_ppem (font, &x_ppem, &y_ppem);
423   g_assert_cmpint (x_ppem, ==, 0);
424   g_assert_cmpint (y_ppem, ==, 0);
425
426   hb_font_set_ppem (font, 17, 19);
427
428   x_ppem = y_ppem = 13;
429   hb_font_get_ppem (font, &x_ppem, &y_ppem);
430   g_assert_cmpint (x_ppem, ==, 17);
431   g_assert_cmpint (y_ppem, ==, 19);
432
433
434   /* Check immutable */
435
436   g_assert (!hb_font_is_immutable (font));
437   hb_font_make_immutable (font);
438   g_assert (hb_font_is_immutable (font));
439
440   hb_font_set_scale (font, 10, 12);
441   x_scale = y_scale = 13;
442   hb_font_get_scale (font, &x_scale, &y_scale);
443   g_assert_cmpint (x_scale, ==, 17);
444   g_assert_cmpint (y_scale, ==, 19);
445
446   hb_font_set_ppem (font, 10, 12);
447   x_ppem = y_ppem = 13;
448   hb_font_get_ppem (font, &x_ppem, &y_ppem);
449   g_assert_cmpint (x_ppem, ==, 17);
450   g_assert_cmpint (y_ppem, ==, 19);
451
452
453   /* sub_font now */
454   subfont = hb_font_create_sub_font (font);
455   hb_font_destroy (font);
456
457   g_assert (hb_font_get_parent (subfont) == font);
458   g_assert (hb_font_get_face (subfont) == face);
459
460   /* scale */
461   x_scale = y_scale = 13;
462   hb_font_get_scale (subfont, &x_scale, &y_scale);
463   g_assert_cmpint (x_scale, ==, 17);
464   g_assert_cmpint (y_scale, ==, 19);
465   hb_font_set_scale (subfont, 10, 12);
466   x_scale = y_scale = 13;
467   hb_font_get_scale (subfont, &x_scale, &y_scale);
468   g_assert_cmpint (x_scale, ==, 10);
469   g_assert_cmpint (y_scale, ==, 12);
470   x_scale = y_scale = 13;
471   hb_font_get_scale (font, &x_scale, &y_scale);
472   g_assert_cmpint (x_scale, ==, 17);
473   g_assert_cmpint (y_scale, ==, 19);
474
475   /* ppem */
476   x_ppem = y_ppem = 13;
477   hb_font_get_ppem (subfont, &x_ppem, &y_ppem);
478   g_assert_cmpint (x_ppem, ==, 17);
479   g_assert_cmpint (y_ppem, ==, 19);
480   hb_font_set_ppem (subfont, 10, 12);
481   x_ppem = y_ppem = 13;
482   hb_font_get_ppem (subfont, &x_ppem, &y_ppem);
483   g_assert_cmpint (x_ppem, ==, 10);
484   g_assert_cmpint (y_ppem, ==, 12);
485   x_ppem = y_ppem = 13;
486   hb_font_get_ppem (font, &x_ppem, &y_ppem);
487   g_assert_cmpint (x_ppem, ==, 17);
488   g_assert_cmpint (y_ppem, ==, 19);
489
490   hb_font_destroy (subfont);
491 }
492
493 int
494 main (int argc, char **argv)
495 {
496   hb_test_init (&argc, &argv);
497
498   hb_test_add (test_face_empty);
499   hb_test_add (test_face_create);
500   hb_test_add (test_face_createfortables);
501
502   hb_test_add (test_fontfuncs_empty);
503   hb_test_add (test_fontfuncs_nil);
504   hb_test_add (test_fontfuncs_subclassing);
505
506   hb_test_add (test_font_empty);
507   hb_test_add (test_font_properties);
508
509   return hb_test_run();
510 }