[API] Pass face to get_table()
[framework/uifw/harfbuzz.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 void
33 test_face_empty (void)
34 {
35   g_assert (hb_face_get_empty ());
36   g_assert (hb_face_get_empty () == hb_face_create (hb_blob_get_empty (), 0));
37   g_assert (hb_face_get_empty () == hb_face_create (NULL, 0));
38
39   g_assert (hb_face_reference_table (hb_face_get_empty (), HB_TAG('h','e','a','d')) == hb_blob_get_empty ());
40 }
41
42 static void
43 free_up (void *user_data)
44 {
45   int *freed = (int *) user_data;
46
47   g_assert (!*freed);
48
49   (*freed)++;
50 }
51
52 static hb_blob_t *
53 get_table (hb_face_t *face, hb_tag_t tag, void *user_data)
54 {
55   return hb_blob_get_empty ();
56 }
57
58 static void
59 test_face_fortables (void)
60 {
61   hb_face_t *face;
62   int freed = 0;
63
64   face = hb_face_create_for_tables (get_table, &freed, free_up);
65   g_assert (!freed);
66
67   hb_face_destroy (face);
68   g_assert (freed);
69 }
70
71
72 static void
73 test_fontfuncs_empty (void)
74 {
75   g_assert (hb_font_funcs_get_empty ());
76   g_assert (hb_font_funcs_is_immutable (hb_font_funcs_get_empty ()));
77 }
78
79 static void
80 test_font_empty (void)
81 {
82   g_assert (hb_font_get_empty ());
83   g_assert (hb_font_get_empty () == hb_font_create (hb_face_get_empty ()));
84   g_assert (hb_font_get_empty () == hb_font_create (NULL));
85   g_assert (hb_font_get_empty () == hb_font_create_sub_font (NULL));
86   g_assert (hb_font_is_immutable (hb_font_get_empty ()));
87
88   g_assert (hb_font_get_face (hb_font_get_empty ()) == hb_face_get_empty ());
89   g_assert (hb_font_get_parent (hb_font_get_empty ()) == NULL);
90 }
91
92 static const char test_data[] = "test\0data";
93
94 static void
95 test_font_properties (void)
96 {
97   hb_blob_t *blob;
98   hb_face_t *face;
99   hb_font_t *font;
100   hb_font_t *subfont;
101   int x_scale, y_scale;
102   unsigned int x_ppem, y_ppem;
103
104   blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
105   face = hb_face_create (blob, 0);
106   hb_blob_destroy (blob);
107   font = hb_font_create (face);
108   hb_face_destroy (face);
109
110
111   g_assert (hb_font_get_face (font) == face);
112   g_assert (hb_font_get_parent (font) == NULL);
113
114
115   /* Check scale */
116
117   hb_font_get_scale (font, NULL, NULL);
118   x_scale = y_scale = 13;
119   hb_font_get_scale (font, &x_scale, NULL);
120   g_assert_cmpint (x_scale, ==, 0);
121   x_scale = y_scale = 13;
122   hb_font_get_scale (font, NULL, &y_scale);
123   g_assert_cmpint (y_scale, ==, 0);
124   x_scale = y_scale = 13;
125   hb_font_get_scale (font, &x_scale, &y_scale);
126   g_assert_cmpint (x_scale, ==, 0);
127   g_assert_cmpint (y_scale, ==, 0);
128
129   hb_font_set_scale (font, 17, 19);
130
131   x_scale = y_scale = 13;
132   hb_font_get_scale (font, &x_scale, &y_scale);
133   g_assert_cmpint (x_scale, ==, 17);
134   g_assert_cmpint (y_scale, ==, 19);
135
136
137   /* Check ppem */
138
139   hb_font_get_ppem (font, NULL, NULL);
140   x_ppem = y_ppem = 13;
141   hb_font_get_ppem (font, &x_ppem, NULL);
142   g_assert_cmpint (x_ppem, ==, 0);
143   x_ppem = y_ppem = 13;
144   hb_font_get_ppem (font, NULL, &y_ppem);
145   g_assert_cmpint (y_ppem, ==, 0);
146   x_ppem = y_ppem = 13;
147   hb_font_get_ppem (font, &x_ppem, &y_ppem);
148   g_assert_cmpint (x_ppem, ==, 0);
149   g_assert_cmpint (y_ppem, ==, 0);
150
151   hb_font_set_ppem (font, 17, 19);
152
153   x_ppem = y_ppem = 13;
154   hb_font_get_ppem (font, &x_ppem, &y_ppem);
155   g_assert_cmpint (x_ppem, ==, 17);
156   g_assert_cmpint (y_ppem, ==, 19);
157
158
159   /* Check immutable */
160
161   g_assert (!hb_font_is_immutable (font));
162   hb_font_make_immutable (font);
163   g_assert (hb_font_is_immutable (font));
164
165   hb_font_set_scale (font, 10, 12);
166   x_scale = y_scale = 13;
167   hb_font_get_scale (font, &x_scale, &y_scale);
168   g_assert_cmpint (x_scale, ==, 17);
169   g_assert_cmpint (y_scale, ==, 19);
170
171   hb_font_set_ppem (font, 10, 12);
172   x_ppem = y_ppem = 13;
173   hb_font_get_ppem (font, &x_ppem, &y_ppem);
174   g_assert_cmpint (x_ppem, ==, 17);
175   g_assert_cmpint (y_ppem, ==, 19);
176
177
178   /* sub_font now */
179   subfont = hb_font_create_sub_font (font);
180   hb_font_destroy (font);
181
182   g_assert (hb_font_get_parent (subfont) == font);
183   g_assert (hb_font_get_face (subfont) == face);
184
185   /* scale */
186   x_scale = y_scale = 13;
187   hb_font_get_scale (subfont, &x_scale, &y_scale);
188   g_assert_cmpint (x_scale, ==, 17);
189   g_assert_cmpint (y_scale, ==, 19);
190   hb_font_set_scale (subfont, 10, 12);
191   x_scale = y_scale = 13;
192   hb_font_get_scale (subfont, &x_scale, &y_scale);
193   g_assert_cmpint (x_scale, ==, 10);
194   g_assert_cmpint (y_scale, ==, 12);
195   x_scale = y_scale = 13;
196   hb_font_get_scale (font, &x_scale, &y_scale);
197   g_assert_cmpint (x_scale, ==, 17);
198   g_assert_cmpint (y_scale, ==, 19);
199
200   /* ppem */
201   x_ppem = y_ppem = 13;
202   hb_font_get_ppem (subfont, &x_ppem, &y_ppem);
203   g_assert_cmpint (x_ppem, ==, 17);
204   g_assert_cmpint (y_ppem, ==, 19);
205   hb_font_set_ppem (subfont, 10, 12);
206   x_ppem = y_ppem = 13;
207   hb_font_get_ppem (subfont, &x_ppem, &y_ppem);
208   g_assert_cmpint (x_ppem, ==, 10);
209   g_assert_cmpint (y_ppem, ==, 12);
210   x_ppem = y_ppem = 13;
211   hb_font_get_ppem (font, &x_ppem, &y_ppem);
212   g_assert_cmpint (x_ppem, ==, 17);
213   g_assert_cmpint (y_ppem, ==, 19);
214
215   hb_font_destroy (subfont);
216 }
217
218 int
219 main (int argc, char **argv)
220 {
221   hb_test_init (&argc, &argv);
222
223   hb_test_add (test_face_empty);
224   hb_test_add (test_face_fortables);
225
226   hb_test_add (test_fontfuncs_empty);
227
228   hb_test_add (test_font_empty);
229   hb_test_add (test_font_properties);
230
231   /*
232    * hb_font_set_funcs
233    * hb_font_funcs
234    */
235
236   return hb_test_run();
237 }