527dfcdc1998a8237f5fef9ba6af11ead4d6e539
[platform/upstream/harfbuzz.git] / test / api / 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   hb_face_t *created_from_empty;
39   hb_face_t *created_from_null;
40
41   g_assert (hb_face_get_empty ());
42
43   created_from_empty = hb_face_create (hb_blob_get_empty (), 0);
44   g_assert (hb_face_get_empty () != created_from_empty);
45
46   created_from_null = hb_face_create (NULL, 0);
47   g_assert (hb_face_get_empty () != created_from_null);
48
49   g_assert (hb_face_reference_table (hb_face_get_empty (), HB_TAG ('h','e','a','d')) == hb_blob_get_empty ());
50
51   g_assert_cmpint (hb_face_get_upem (hb_face_get_empty ()), ==, 1000);
52
53   hb_face_destroy (created_from_null);
54   hb_face_destroy (created_from_empty);
55 }
56
57 static void
58 test_face_create (void)
59 {
60   hb_face_t *face;
61   hb_blob_t *blob;
62
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);
66
67   g_assert (hb_face_reference_table (face, HB_TAG ('h','e','a','d')) == hb_blob_get_empty ());
68
69   g_assert_cmpint (hb_face_get_upem (face), ==, 1000);
70
71   hb_face_destroy (face);
72 }
73
74
75 static void
76 free_up (void *user_data)
77 {
78   int *freed = (int *) user_data;
79
80   g_assert (!*freed);
81
82   (*freed)++;
83 }
84
85 static hb_blob_t *
86 get_table (hb_face_t *face, hb_tag_t tag, void *user_data)
87 {
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);
90
91   return hb_blob_get_empty ();
92 }
93
94 static void
95 test_face_createfortables (void)
96 {
97   hb_face_t *face;
98   hb_blob_t *blob;
99   const char *data;
100   unsigned int len;
101   int freed = 0;
102
103   face = hb_face_create_for_tables (get_table, &freed, free_up);
104   g_assert (!freed);
105
106   g_assert (hb_face_reference_table (face, HB_TAG ('h','e','a','d')) == hb_blob_get_empty ());
107
108   blob = hb_face_reference_table (face, HB_TAG ('a','b','c','d'));
109   g_assert (blob != hb_blob_get_empty ());
110
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);
115
116   g_assert_cmpint (hb_face_get_upem (face), ==, 1000);
117
118   hb_face_destroy (face);
119   g_assert (freed);
120 }
121
122 static void
123 _test_font_nil_funcs (hb_font_t *font)
124 {
125   hb_codepoint_t glyph;
126   hb_position_t x, y;
127   hb_glyph_extents_t extents;
128   unsigned int upem = hb_face_get_upem (hb_font_get_face (font));
129
130   x = y = 13;
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);
134
135   x = hb_font_get_glyph_h_advance (font, 17);
136   g_assert_cmpint (x, ==, upem);
137
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);
145
146   glyph = 3;
147   g_assert (!hb_font_get_glyph (font, 17, 2, &glyph));
148   g_assert_cmpint (glyph, ==, 0);
149
150   x = hb_font_get_glyph_h_kerning (font, 17, 19);
151   g_assert_cmpint (x, ==, 0);
152 }
153
154 static void
155 _test_fontfuncs_nil (hb_font_funcs_t *ffuncs)
156 {
157   hb_blob_t *blob;
158   hb_face_t *face;
159   hb_font_t *font;
160   hb_font_t *subfont;
161   int freed = 0;
162
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);
168   g_assert (font);
169   g_assert (hb_face_is_immutable (face));
170   hb_face_destroy (face);
171
172
173   hb_font_set_funcs (font, ffuncs, &freed, free_up);
174   g_assert_cmpint (freed, ==, 0);
175
176   _test_font_nil_funcs (font);
177
178   subfont = hb_font_create_sub_font (font);
179   g_assert (subfont);
180
181   g_assert_cmpint (freed, ==, 0);
182   hb_font_destroy (font);
183   g_assert_cmpint (freed, ==, 0);
184
185   _test_font_nil_funcs (subfont);
186
187   hb_font_destroy (subfont);
188   g_assert_cmpint (freed, ==, 1);
189 }
190
191 static void
192 test_fontfuncs_empty (void)
193 {
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 ());
197 }
198
199 static void
200 test_fontfuncs_nil (void)
201 {
202   hb_font_funcs_t *ffuncs;
203
204   ffuncs = hb_font_funcs_create ();
205
206   g_assert (!hb_font_funcs_is_immutable (ffuncs));
207   _test_fontfuncs_nil (hb_font_funcs_get_empty ());
208
209   hb_font_funcs_destroy (ffuncs);
210 }
211
212 static hb_bool_t
213 contour_point_func1 (hb_font_t *font, void *font_data,
214                      hb_codepoint_t glyph, unsigned int point_index,
215                      hb_position_t *x, hb_position_t *y,
216                      void *user_data)
217 {
218   if (glyph == 1) {
219     *x = 2;
220     *y = 3;
221     return TRUE;
222   }
223   if (glyph == 2) {
224     *x = 4;
225     *y = 5;
226     return TRUE;
227   }
228
229   return FALSE;
230 }
231
232 static hb_bool_t
233 contour_point_func2 (hb_font_t *font, void *font_data,
234                      hb_codepoint_t glyph, unsigned int point_index,
235                      hb_position_t *x, hb_position_t *y,
236                      void *user_data)
237 {
238   if (glyph == 1) {
239     *x = 6;
240     *y = 7;
241     return TRUE;
242   }
243
244   return hb_font_get_glyph_contour_point (hb_font_get_parent (font),
245                                           glyph, point_index, x, y);
246 }
247
248 static hb_position_t
249 glyph_h_advance_func1 (hb_font_t *font, void *font_data,
250                        hb_codepoint_t glyph,
251                        void *user_data)
252 {
253   if (glyph == 1)
254     return 8;
255
256   return 0;
257 }
258
259 static void
260 test_fontfuncs_subclassing (void)
261 {
262   hb_blob_t *blob;
263   hb_face_t *face;
264
265   hb_font_funcs_t *ffuncs1;
266   hb_font_funcs_t *ffuncs2;
267
268   hb_font_t *font1;
269   hb_font_t *font2;
270   hb_font_t *font3;
271
272   hb_position_t x;
273   hb_position_t y;
274
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);
281
282   /* setup font1 */
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);
288
289   x = y = 1;
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);
303
304   /* creating sub-font doesn't make the parent font immutable;
305    * making a font immutable however makes it's lineage immutable.
306    */
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);
318
319   font2 = hb_font_create_sub_font (font1);
320   hb_font_destroy (font1);
321
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);
327
328   x = y = 1;
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);
342
343   /* setup font3 to override scale */
344   font3 = hb_font_create_sub_font (font2);
345   hb_font_set_scale (font3, 20, 30);
346
347   x = y = 1;
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);
361
362
363   hb_font_destroy (font3);
364 }
365
366
367 static void
368 test_font_empty (void)
369 {
370   hb_font_t *created_from_empty;
371   hb_font_t *created_from_null;
372   hb_font_t *created_sub_from_null;
373
374   g_assert (hb_font_get_empty ());
375
376   created_from_empty = hb_font_create (hb_face_get_empty ());
377   g_assert (hb_font_get_empty () != created_from_empty);
378
379   created_from_null = hb_font_create (NULL);
380   g_assert (hb_font_get_empty () != created_from_null);
381
382   created_sub_from_null = hb_font_create_sub_font (NULL);
383   g_assert (hb_font_get_empty () != created_sub_from_null);
384
385   g_assert (hb_font_is_immutable (hb_font_get_empty ()));
386
387   g_assert (hb_font_get_face (hb_font_get_empty ()) == hb_face_get_empty ());
388   g_assert (hb_font_get_parent (hb_font_get_empty ()) == NULL);
389
390   hb_font_destroy (created_sub_from_null);
391   hb_font_destroy (created_from_null);
392   hb_font_destroy (created_from_empty);
393 }
394
395 static void
396 test_font_properties (void)
397 {
398   hb_blob_t *blob;
399   hb_face_t *face;
400   hb_font_t *font;
401   hb_font_t *subfont;
402   int x_scale, y_scale;
403   unsigned int x_ppem, y_ppem;
404   unsigned int upem;
405
406   blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
407   face = hb_face_create (blob, 0);
408   hb_blob_destroy (blob);
409   font = hb_font_create (face);
410   hb_face_destroy (face);
411
412
413   g_assert (hb_font_get_face (font) == face);
414   g_assert (hb_font_get_parent (font) == hb_font_get_empty ());
415   subfont = hb_font_create_sub_font (font);
416   g_assert (hb_font_get_parent (subfont) == font);
417   hb_font_set_parent(subfont, NULL);
418   g_assert (hb_font_get_parent (subfont) == hb_font_get_empty());
419   hb_font_set_parent(subfont, font);
420   g_assert (hb_font_get_parent (subfont) == font);
421   hb_font_set_parent(subfont, NULL);
422   hb_font_make_immutable (subfont);
423   g_assert (hb_font_get_parent (subfont) == hb_font_get_empty());
424   hb_font_set_parent(subfont, font);
425   g_assert (hb_font_get_parent (subfont) == hb_font_get_empty());
426   hb_font_destroy (subfont);
427
428
429   /* Check scale */
430
431   upem = hb_face_get_upem (hb_font_get_face (font));
432   hb_font_get_scale (font, NULL, NULL);
433   x_scale = y_scale = 13;
434   hb_font_get_scale (font, &x_scale, NULL);
435   g_assert_cmpint (x_scale, ==, upem);
436   x_scale = y_scale = 13;
437   hb_font_get_scale (font, NULL, &y_scale);
438   g_assert_cmpint (y_scale, ==, upem);
439   x_scale = y_scale = 13;
440   hb_font_get_scale (font, &x_scale, &y_scale);
441   g_assert_cmpint (x_scale, ==, upem);
442   g_assert_cmpint (y_scale, ==, upem);
443
444   hb_font_set_scale (font, 17, 19);
445
446   x_scale = y_scale = 13;
447   hb_font_get_scale (font, &x_scale, &y_scale);
448   g_assert_cmpint (x_scale, ==, 17);
449   g_assert_cmpint (y_scale, ==, 19);
450
451
452   /* Check ppem */
453
454   hb_font_get_ppem (font, NULL, NULL);
455   x_ppem = y_ppem = 13;
456   hb_font_get_ppem (font, &x_ppem, NULL);
457   g_assert_cmpint (x_ppem, ==, 0);
458   x_ppem = y_ppem = 13;
459   hb_font_get_ppem (font, NULL, &y_ppem);
460   g_assert_cmpint (y_ppem, ==, 0);
461   x_ppem = y_ppem = 13;
462   hb_font_get_ppem (font, &x_ppem, &y_ppem);
463   g_assert_cmpint (x_ppem, ==, 0);
464   g_assert_cmpint (y_ppem, ==, 0);
465
466   hb_font_set_ppem (font, 17, 19);
467
468   x_ppem = y_ppem = 13;
469   hb_font_get_ppem (font, &x_ppem, &y_ppem);
470   g_assert_cmpint (x_ppem, ==, 17);
471   g_assert_cmpint (y_ppem, ==, 19);
472
473
474   /* Check immutable */
475
476   g_assert (!hb_font_is_immutable (font));
477   hb_font_make_immutable (font);
478   g_assert (hb_font_is_immutable (font));
479
480   hb_font_set_scale (font, 10, 12);
481   x_scale = y_scale = 13;
482   hb_font_get_scale (font, &x_scale, &y_scale);
483   g_assert_cmpint (x_scale, ==, 17);
484   g_assert_cmpint (y_scale, ==, 19);
485
486   hb_font_set_ppem (font, 10, 12);
487   x_ppem = y_ppem = 13;
488   hb_font_get_ppem (font, &x_ppem, &y_ppem);
489   g_assert_cmpint (x_ppem, ==, 17);
490   g_assert_cmpint (y_ppem, ==, 19);
491
492
493   /* sub_font now */
494   subfont = hb_font_create_sub_font (font);
495   hb_font_destroy (font);
496
497   g_assert (hb_font_get_parent (subfont) == font);
498   g_assert (hb_font_get_face (subfont) == face);
499
500   /* scale */
501   x_scale = y_scale = 13;
502   hb_font_get_scale (subfont, &x_scale, &y_scale);
503   g_assert_cmpint (x_scale, ==, 17);
504   g_assert_cmpint (y_scale, ==, 19);
505   hb_font_set_scale (subfont, 10, 12);
506   x_scale = y_scale = 13;
507   hb_font_get_scale (subfont, &x_scale, &y_scale);
508   g_assert_cmpint (x_scale, ==, 10);
509   g_assert_cmpint (y_scale, ==, 12);
510   x_scale = y_scale = 13;
511   hb_font_get_scale (font, &x_scale, &y_scale);
512   g_assert_cmpint (x_scale, ==, 17);
513   g_assert_cmpint (y_scale, ==, 19);
514
515   /* ppem */
516   x_ppem = y_ppem = 13;
517   hb_font_get_ppem (subfont, &x_ppem, &y_ppem);
518   g_assert_cmpint (x_ppem, ==, 17);
519   g_assert_cmpint (y_ppem, ==, 19);
520   hb_font_set_ppem (subfont, 10, 12);
521   x_ppem = y_ppem = 13;
522   hb_font_get_ppem (subfont, &x_ppem, &y_ppem);
523   g_assert_cmpint (x_ppem, ==, 10);
524   g_assert_cmpint (y_ppem, ==, 12);
525   x_ppem = y_ppem = 13;
526   hb_font_get_ppem (font, &x_ppem, &y_ppem);
527   g_assert_cmpint (x_ppem, ==, 17);
528   g_assert_cmpint (y_ppem, ==, 19);
529
530   hb_font_destroy (subfont);
531 }
532
533 int
534 main (int argc, char **argv)
535 {
536   hb_test_init (&argc, &argv);
537
538   hb_test_add (test_face_empty);
539   hb_test_add (test_face_create);
540   hb_test_add (test_face_createfortables);
541
542   hb_test_add (test_fontfuncs_empty);
543   hb_test_add (test_fontfuncs_nil);
544   hb_test_add (test_fontfuncs_subclassing);
545
546   hb_test_add (test_font_empty);
547   hb_test_add (test_font_properties);
548
549   return hb_test_run();
550 }