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