[test/shape] Add simplest test for hb_shape()
[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_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_nil (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 static hb_bool_t
199 contour_point_func1 (hb_font_t *font, void *font_data,
200                      hb_codepoint_t glyph, unsigned int point_index,
201                      hb_position_t *x, hb_position_t *y,
202                      void *user_data)
203 {
204   if (glyph == 1) {
205     *x = 2;
206     *y = 3;
207     return TRUE;
208   }
209   if (glyph == 2) {
210     *x = 4;
211     *y = 5;
212     return TRUE;
213   }
214
215   return FALSE;
216 }
217
218 static hb_bool_t
219 contour_point_func2 (hb_font_t *font, void *font_data,
220                      hb_codepoint_t glyph, unsigned int point_index,
221                      hb_position_t *x, hb_position_t *y,
222                      void *user_data)
223 {
224   if (glyph == 1) {
225     *x = 6;
226     *y = 7;
227     return TRUE;
228   }
229
230   return hb_font_get_contour_point (hb_font_get_parent (font),
231                                     glyph, point_index, x, y);
232 }
233
234 static void
235 glyph_advance_func1 (hb_font_t *font, void *font_data,
236                      hb_codepoint_t glyph,
237                      hb_position_t *x_advance, hb_position_t *y_advance,
238                      void *user_data)
239 {
240   if (glyph == 1) {
241     *x_advance = 8;
242     *y_advance = 9;
243   }
244 }
245
246 static void
247 test_fontfuncs_subclassing (void)
248 {
249   hb_blob_t *blob;
250   hb_face_t *face;
251
252   hb_font_funcs_t *ffuncs1;
253   hb_font_funcs_t *ffuncs2;
254
255   hb_font_t *font1;
256   hb_font_t *font2;
257   hb_font_t *font3;
258
259   hb_position_t x;
260   hb_position_t y;
261
262   blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
263   face = hb_face_create (blob, 0);
264   hb_blob_destroy (blob);
265   font1 = hb_font_create (face);
266   hb_face_destroy (face);
267   hb_font_set_scale (font1, 10, 10);
268
269   /* setup font1 */
270   ffuncs1 = hb_font_funcs_create ();
271   hb_font_funcs_set_contour_point_func (ffuncs1, contour_point_func1, NULL, NULL);
272   hb_font_funcs_set_glyph_advance_func (ffuncs1, glyph_advance_func1, NULL, NULL);
273   hb_font_set_funcs (font1, ffuncs1, NULL, NULL);
274   hb_font_funcs_destroy (ffuncs1);
275
276   x = y = 1;
277   g_assert (hb_font_get_contour_point (font1, 1, 2, &x, &y));
278   g_assert_cmpint (x, ==, 2);
279   g_assert_cmpint (y, ==, 3);
280   g_assert (hb_font_get_contour_point (font1, 2, 5, &x, &y));
281   g_assert_cmpint (x, ==, 4);
282   g_assert_cmpint (y, ==, 5);
283   g_assert (!hb_font_get_contour_point (font1, 3, 7, &x, &y));
284   g_assert_cmpint (x, ==, 0);
285   g_assert_cmpint (y, ==, 0);
286   hb_font_get_glyph_advance (font1, 1, &x, &y);
287   g_assert_cmpint (x, ==, 8);
288   g_assert_cmpint (y, ==, 9);
289   hb_font_get_glyph_advance (font1, 2, &x, &y);
290   g_assert_cmpint (x, ==, 0);
291   g_assert_cmpint (y, ==, 0);
292
293
294   font2 = hb_font_create_sub_font (font1);
295   g_assert (hb_font_is_immutable (font1));
296   hb_font_destroy (font1);
297
298   /* setup font2 to override some funcs */
299   ffuncs2 = hb_font_funcs_create ();
300   hb_font_funcs_set_contour_point_func (ffuncs2, contour_point_func2, NULL, NULL);
301   hb_font_set_funcs (font2, ffuncs2, NULL, NULL);
302   hb_font_funcs_destroy (ffuncs2);
303
304   x = y = 1;
305   g_assert (hb_font_get_contour_point (font2, 1, 2, &x, &y));
306   g_assert_cmpint (x, ==, 6);
307   g_assert_cmpint (y, ==, 7);
308   g_assert (hb_font_get_contour_point (font2, 2, 5, &x, &y));
309   g_assert_cmpint (x, ==, 4);
310   g_assert_cmpint (y, ==, 5);
311   g_assert (!hb_font_get_contour_point (font2, 3, 7, &x, &y));
312   g_assert_cmpint (x, ==, 0);
313   g_assert_cmpint (y, ==, 0);
314   hb_font_get_glyph_advance (font2, 1, &x, &y);
315   g_assert_cmpint (x, ==, 8);
316   g_assert_cmpint (y, ==, 9);
317   hb_font_get_glyph_advance (font2, 2, &x, &y);
318   g_assert_cmpint (x, ==, 0);
319   g_assert_cmpint (y, ==, 0);
320
321
322   font3 = hb_font_create_sub_font (font2);
323   g_assert (hb_font_is_immutable (font2));
324   hb_font_destroy (font2);
325
326   /* setup font3 to override scale */
327   hb_font_set_scale (font3, 20, 30);
328
329   x = y = 1;
330   g_assert (hb_font_get_contour_point (font3, 1, 2, &x, &y));
331   g_assert_cmpint (x, ==, 6*2);
332   g_assert_cmpint (y, ==, 7*3);
333   g_assert (hb_font_get_contour_point (font3, 2, 5, &x, &y));
334   g_assert_cmpint (x, ==, 4*2);
335   g_assert_cmpint (y, ==, 5*3);
336   g_assert (!hb_font_get_contour_point (font3, 3, 7, &x, &y));
337   g_assert_cmpint (x, ==, 0*2);
338   g_assert_cmpint (y, ==, 0*3);
339   hb_font_get_glyph_advance (font3, 1, &x, &y);
340   g_assert_cmpint (x, ==, 8*2);
341   g_assert_cmpint (y, ==, 9*3);
342   hb_font_get_glyph_advance (font3, 2, &x, &y);
343   g_assert_cmpint (x, ==, 0*2);
344   g_assert_cmpint (y, ==, 0*3);
345
346
347   hb_font_destroy (font3);
348 }
349
350
351 static void
352 test_font_empty (void)
353 {
354   g_assert (hb_font_get_empty ());
355   g_assert (hb_font_get_empty () == hb_font_create (hb_face_get_empty ()));
356   g_assert (hb_font_get_empty () == hb_font_create (NULL));
357   g_assert (hb_font_get_empty () == hb_font_create_sub_font (NULL));
358   g_assert (hb_font_is_immutable (hb_font_get_empty ()));
359
360   g_assert (hb_font_get_face (hb_font_get_empty ()) == hb_face_get_empty ());
361   g_assert (hb_font_get_parent (hb_font_get_empty ()) == NULL);
362 }
363
364 static void
365 test_font_properties (void)
366 {
367   hb_blob_t *blob;
368   hb_face_t *face;
369   hb_font_t *font;
370   hb_font_t *subfont;
371   int x_scale, y_scale;
372   unsigned int x_ppem, y_ppem;
373
374   blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
375   face = hb_face_create (blob, 0);
376   hb_blob_destroy (blob);
377   font = hb_font_create (face);
378   hb_face_destroy (face);
379
380
381   g_assert (hb_font_get_face (font) == face);
382   g_assert (hb_font_get_parent (font) == NULL);
383
384
385   /* Check scale */
386
387   hb_font_get_scale (font, NULL, NULL);
388   x_scale = y_scale = 13;
389   hb_font_get_scale (font, &x_scale, NULL);
390   g_assert_cmpint (x_scale, ==, 0);
391   x_scale = y_scale = 13;
392   hb_font_get_scale (font, NULL, &y_scale);
393   g_assert_cmpint (y_scale, ==, 0);
394   x_scale = y_scale = 13;
395   hb_font_get_scale (font, &x_scale, &y_scale);
396   g_assert_cmpint (x_scale, ==, 0);
397   g_assert_cmpint (y_scale, ==, 0);
398
399   hb_font_set_scale (font, 17, 19);
400
401   x_scale = y_scale = 13;
402   hb_font_get_scale (font, &x_scale, &y_scale);
403   g_assert_cmpint (x_scale, ==, 17);
404   g_assert_cmpint (y_scale, ==, 19);
405
406
407   /* Check ppem */
408
409   hb_font_get_ppem (font, NULL, NULL);
410   x_ppem = y_ppem = 13;
411   hb_font_get_ppem (font, &x_ppem, NULL);
412   g_assert_cmpint (x_ppem, ==, 0);
413   x_ppem = y_ppem = 13;
414   hb_font_get_ppem (font, NULL, &y_ppem);
415   g_assert_cmpint (y_ppem, ==, 0);
416   x_ppem = y_ppem = 13;
417   hb_font_get_ppem (font, &x_ppem, &y_ppem);
418   g_assert_cmpint (x_ppem, ==, 0);
419   g_assert_cmpint (y_ppem, ==, 0);
420
421   hb_font_set_ppem (font, 17, 19);
422
423   x_ppem = y_ppem = 13;
424   hb_font_get_ppem (font, &x_ppem, &y_ppem);
425   g_assert_cmpint (x_ppem, ==, 17);
426   g_assert_cmpint (y_ppem, ==, 19);
427
428
429   /* Check immutable */
430
431   g_assert (!hb_font_is_immutable (font));
432   hb_font_make_immutable (font);
433   g_assert (hb_font_is_immutable (font));
434
435   hb_font_set_scale (font, 10, 12);
436   x_scale = y_scale = 13;
437   hb_font_get_scale (font, &x_scale, &y_scale);
438   g_assert_cmpint (x_scale, ==, 17);
439   g_assert_cmpint (y_scale, ==, 19);
440
441   hb_font_set_ppem (font, 10, 12);
442   x_ppem = y_ppem = 13;
443   hb_font_get_ppem (font, &x_ppem, &y_ppem);
444   g_assert_cmpint (x_ppem, ==, 17);
445   g_assert_cmpint (y_ppem, ==, 19);
446
447
448   /* sub_font now */
449   subfont = hb_font_create_sub_font (font);
450   hb_font_destroy (font);
451
452   g_assert (hb_font_get_parent (subfont) == font);
453   g_assert (hb_font_get_face (subfont) == face);
454
455   /* scale */
456   x_scale = y_scale = 13;
457   hb_font_get_scale (subfont, &x_scale, &y_scale);
458   g_assert_cmpint (x_scale, ==, 17);
459   g_assert_cmpint (y_scale, ==, 19);
460   hb_font_set_scale (subfont, 10, 12);
461   x_scale = y_scale = 13;
462   hb_font_get_scale (subfont, &x_scale, &y_scale);
463   g_assert_cmpint (x_scale, ==, 10);
464   g_assert_cmpint (y_scale, ==, 12);
465   x_scale = y_scale = 13;
466   hb_font_get_scale (font, &x_scale, &y_scale);
467   g_assert_cmpint (x_scale, ==, 17);
468   g_assert_cmpint (y_scale, ==, 19);
469
470   /* ppem */
471   x_ppem = y_ppem = 13;
472   hb_font_get_ppem (subfont, &x_ppem, &y_ppem);
473   g_assert_cmpint (x_ppem, ==, 17);
474   g_assert_cmpint (y_ppem, ==, 19);
475   hb_font_set_ppem (subfont, 10, 12);
476   x_ppem = y_ppem = 13;
477   hb_font_get_ppem (subfont, &x_ppem, &y_ppem);
478   g_assert_cmpint (x_ppem, ==, 10);
479   g_assert_cmpint (y_ppem, ==, 12);
480   x_ppem = y_ppem = 13;
481   hb_font_get_ppem (font, &x_ppem, &y_ppem);
482   g_assert_cmpint (x_ppem, ==, 17);
483   g_assert_cmpint (y_ppem, ==, 19);
484
485   hb_font_destroy (subfont);
486 }
487
488 int
489 main (int argc, char **argv)
490 {
491   hb_test_init (&argc, &argv);
492
493   hb_test_add (test_face_empty);
494   hb_test_add (test_face_create);
495   hb_test_add (test_face_createfortables);
496
497   hb_test_add (test_fontfuncs_empty);
498   hb_test_add (test_fontfuncs_nil);
499   hb_test_add (test_fontfuncs_subclassing);
500
501   hb_test_add (test_font_empty);
502   hb_test_add (test_font_properties);
503
504   return hb_test_run();
505 }