blob: c6738b4006e1c894919473976a82b61ce719d278 [file] [log] [blame]
Behdad Esfahbodda603e82011-05-11 22:52:35 -04001/*
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
Behdad Esfahbod2ca0b5a2011-05-11 23:57:36 -040032static const char test_data[] = "test\0data";
33
34
Behdad Esfahbodda603e82011-05-11 22:52:35 -040035static void
36test_face_empty (void)
37{
Philip Withnalla6ced902017-02-08 02:18:33 +000038 hb_face_t *created_from_empty;
39 hb_face_t *created_from_null;
40
Behdad Esfahbodda603e82011-05-11 22:52:35 -040041 g_assert (hb_face_get_empty ());
Philip Withnalla6ced902017-02-08 02:18:33 +000042
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);
Behdad Esfahbod74d9fa32011-05-11 23:07:47 -040048
Behdad Esfahbod2ca0b5a2011-05-11 23:57:36 -040049 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);
Philip Withnalla6ced902017-02-08 02:18:33 +000052
53 hb_face_destroy (created_from_null);
54 hb_face_destroy (created_from_empty);
Behdad Esfahbodda603e82011-05-11 22:52:35 -040055}
56
57static void
Behdad Esfahbod2ca0b5a2011-05-11 23:57:36 -040058test_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
75static void
Behdad Esfahbodea93e7b2011-05-11 23:22:55 -040076free_up (void *user_data)
77{
78 int *freed = (int *) user_data;
79
80 g_assert (!*freed);
81
82 (*freed)++;
83}
84
85static hb_blob_t *
Behdad Esfahboddcfcb952018-09-30 18:14:50 +020086get_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data HB_UNUSED)
Behdad Esfahbodea93e7b2011-05-11 23:22:55 -040087{
Behdad Esfahbod2ca0b5a2011-05-11 23:57:36 -040088 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
Behdad Esfahbodea93e7b2011-05-11 23:22:55 -040091 return hb_blob_get_empty ();
92}
93
94static void
Behdad Esfahbod2ca0b5a2011-05-11 23:57:36 -040095test_face_createfortables (void)
Behdad Esfahbodea93e7b2011-05-11 23:22:55 -040096{
97 hb_face_t *face;
Behdad Esfahbod2ca0b5a2011-05-11 23:57:36 -040098 hb_blob_t *blob;
99 const char *data;
100 unsigned int len;
Behdad Esfahbodea93e7b2011-05-11 23:22:55 -0400101 int freed = 0;
102
103 face = hb_face_create_for_tables (get_table, &freed, free_up);
104 g_assert (!freed);
105
Behdad Esfahbod2ca0b5a2011-05-11 23:57:36 -0400106 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
Behdad Esfahbodea93e7b2011-05-11 23:22:55 -0400118 hb_face_destroy (face);
119 g_assert (freed);
120}
121
Behdad Esfahbod14f1e812011-05-12 00:18:28 -0400122static void
123_test_font_nil_funcs (hb_font_t *font)
124{
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -0400125 hb_codepoint_t glyph;
Behdad Esfahbod14f1e812011-05-12 00:18:28 -0400126 hb_position_t x, y;
127 hb_glyph_extents_t extents;
Behdad Esfahbod88da7bb2015-10-02 14:38:20 +0100128 unsigned int upem = hb_face_get_upem (hb_font_get_face (font));
Behdad Esfahbod14f1e812011-05-12 00:18:28 -0400129
130 x = y = 13;
Behdad Esfahbodd3169122011-05-25 11:01:32 -0400131 g_assert (!hb_font_get_glyph_contour_point (font, 17, 2, &x, &y));
Behdad Esfahbod14f1e812011-05-12 00:18:28 -0400132 g_assert_cmpint (x, ==, 0);
133 g_assert_cmpint (y, ==, 0);
134
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400135 x = hb_font_get_glyph_h_advance (font, 17);
Behdad Esfahbod88da7bb2015-10-02 14:38:20 +0100136 g_assert_cmpint (x, ==, upem);
Behdad Esfahbod14f1e812011-05-12 00:18:28 -0400137
138 extents.x_bearing = extents.y_bearing = 13;
139 extents.width = extents.height = 15;
Behdad Esfahbodd3169122011-05-25 11:01:32 -0400140 hb_font_get_glyph_extents (font, 17, &extents);
Behdad Esfahbod14f1e812011-05-12 00:18:28 -0400141 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
Behdad Esfahbod0fd8c2f2011-05-12 15:14:13 -0400146 glyph = 3;
147 g_assert (!hb_font_get_glyph (font, 17, 2, &glyph));
148 g_assert_cmpint (glyph, ==, 0);
Behdad Esfahbod14f1e812011-05-12 00:18:28 -0400149}
150
151static void
152_test_fontfuncs_nil (hb_font_funcs_t *ffuncs)
153{
154 hb_blob_t *blob;
155 hb_face_t *face;
156 hb_font_t *font;
157 hb_font_t *subfont;
158 int freed = 0;
159
160 blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
161 face = hb_face_create (blob, 0);
162 hb_blob_destroy (blob);
Behdad Esfahbod7fc5a302011-05-12 17:48:20 -0400163 g_assert (!hb_face_is_immutable (face));
Behdad Esfahbod14f1e812011-05-12 00:18:28 -0400164 font = hb_font_create (face);
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400165 g_assert (font);
Behdad Esfahbod7fc5a302011-05-12 17:48:20 -0400166 g_assert (hb_face_is_immutable (face));
Behdad Esfahbod14f1e812011-05-12 00:18:28 -0400167 hb_face_destroy (face);
168
169
170 hb_font_set_funcs (font, ffuncs, &freed, free_up);
171 g_assert_cmpint (freed, ==, 0);
172
173 _test_font_nil_funcs (font);
174
175 subfont = hb_font_create_sub_font (font);
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400176 g_assert (subfont);
Behdad Esfahbod14f1e812011-05-12 00:18:28 -0400177
178 g_assert_cmpint (freed, ==, 0);
179 hb_font_destroy (font);
180 g_assert_cmpint (freed, ==, 0);
181
182 _test_font_nil_funcs (subfont);
183
184 hb_font_destroy (subfont);
185 g_assert_cmpint (freed, ==, 1);
186}
Behdad Esfahbodea93e7b2011-05-11 23:22:55 -0400187
188static void
189test_fontfuncs_empty (void)
Behdad Esfahbodda603e82011-05-11 22:52:35 -0400190{
191 g_assert (hb_font_funcs_get_empty ());
192 g_assert (hb_font_funcs_is_immutable (hb_font_funcs_get_empty ()));
Behdad Esfahbod14f1e812011-05-12 00:18:28 -0400193 _test_fontfuncs_nil (hb_font_funcs_get_empty ());
Behdad Esfahbodda603e82011-05-11 22:52:35 -0400194}
195
196static void
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400197test_fontfuncs_nil (void)
Behdad Esfahbod2ca0b5a2011-05-11 23:57:36 -0400198{
Behdad Esfahbod14f1e812011-05-12 00:18:28 -0400199 hb_font_funcs_t *ffuncs;
200
201 ffuncs = hb_font_funcs_create ();
202
203 g_assert (!hb_font_funcs_is_immutable (ffuncs));
204 _test_fontfuncs_nil (hb_font_funcs_get_empty ());
205
206 hb_font_funcs_destroy (ffuncs);
Behdad Esfahbod2ca0b5a2011-05-11 23:57:36 -0400207}
208
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400209static hb_bool_t
Behdad Esfahboddcfcb952018-09-30 18:14:50 +0200210contour_point_func1 (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED,
211 hb_codepoint_t glyph, unsigned int point_index HB_UNUSED,
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400212 hb_position_t *x, hb_position_t *y,
Behdad Esfahboddcfcb952018-09-30 18:14:50 +0200213 void *user_data HB_UNUSED)
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400214{
215 if (glyph == 1) {
216 *x = 2;
217 *y = 3;
218 return TRUE;
219 }
220 if (glyph == 2) {
221 *x = 4;
222 *y = 5;
223 return TRUE;
224 }
225
226 return FALSE;
227}
228
229static hb_bool_t
Behdad Esfahboddcfcb952018-09-30 18:14:50 +0200230contour_point_func2 (hb_font_t *font, void *font_data HB_UNUSED,
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400231 hb_codepoint_t glyph, unsigned int point_index,
232 hb_position_t *x, hb_position_t *y,
Behdad Esfahboddcfcb952018-09-30 18:14:50 +0200233 void *user_data HB_UNUSED)
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400234{
235 if (glyph == 1) {
236 *x = 6;
237 *y = 7;
238 return TRUE;
239 }
240
Behdad Esfahbodd3169122011-05-25 11:01:32 -0400241 return hb_font_get_glyph_contour_point (hb_font_get_parent (font),
242 glyph, point_index, x, y);
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400243}
244
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400245static hb_position_t
Behdad Esfahboddcfcb952018-09-30 18:14:50 +0200246glyph_h_advance_func1 (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED,
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400247 hb_codepoint_t glyph,
Behdad Esfahboddcfcb952018-09-30 18:14:50 +0200248 void *user_data HB_UNUSED)
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400249{
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400250 if (glyph == 1)
251 return 8;
252
253 return 0;
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400254}
255
256static void
257test_fontfuncs_subclassing (void)
258{
259 hb_blob_t *blob;
260 hb_face_t *face;
261
262 hb_font_funcs_t *ffuncs1;
263 hb_font_funcs_t *ffuncs2;
264
265 hb_font_t *font1;
266 hb_font_t *font2;
267 hb_font_t *font3;
268
269 hb_position_t x;
270 hb_position_t y;
271
272 blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
273 face = hb_face_create (blob, 0);
274 hb_blob_destroy (blob);
275 font1 = hb_font_create (face);
276 hb_face_destroy (face);
277 hb_font_set_scale (font1, 10, 10);
278
279 /* setup font1 */
280 ffuncs1 = hb_font_funcs_create ();
Behdad Esfahbodd3169122011-05-25 11:01:32 -0400281 hb_font_funcs_set_glyph_contour_point_func (ffuncs1, contour_point_func1, NULL, NULL);
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400282 hb_font_funcs_set_glyph_h_advance_func (ffuncs1, glyph_h_advance_func1, NULL, NULL);
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400283 hb_font_set_funcs (font1, ffuncs1, NULL, NULL);
284 hb_font_funcs_destroy (ffuncs1);
285
286 x = y = 1;
Behdad Esfahbodd3169122011-05-25 11:01:32 -0400287 g_assert (hb_font_get_glyph_contour_point_for_origin (font1, 1, 2, HB_DIRECTION_LTR, &x, &y));
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400288 g_assert_cmpint (x, ==, 2);
289 g_assert_cmpint (y, ==, 3);
Behdad Esfahbodd3169122011-05-25 11:01:32 -0400290 g_assert (hb_font_get_glyph_contour_point_for_origin (font1, 2, 5, HB_DIRECTION_LTR, &x, &y));
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400291 g_assert_cmpint (x, ==, 4);
292 g_assert_cmpint (y, ==, 5);
Behdad Esfahbodd3169122011-05-25 11:01:32 -0400293 g_assert (!hb_font_get_glyph_contour_point_for_origin (font1, 3, 7, HB_DIRECTION_RTL, &x, &y));
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400294 g_assert_cmpint (x, ==, 0);
295 g_assert_cmpint (y, ==, 0);
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400296 x = hb_font_get_glyph_h_advance (font1, 1);
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400297 g_assert_cmpint (x, ==, 8);
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400298 x = hb_font_get_glyph_h_advance (font1, 2);
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400299 g_assert_cmpint (x, ==, 0);
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400300
Behdad Esfahbod1866e172015-10-02 14:21:29 +0100301 /* creating sub-font doesn't make the parent font immutable;
302 * making a font immutable however makes it's lineage immutable.
303 */
304 font2 = hb_font_create_sub_font (font1);
305 font3 = hb_font_create_sub_font (font2);
306 g_assert (!hb_font_is_immutable (font1));
307 g_assert (!hb_font_is_immutable (font2));
308 g_assert (!hb_font_is_immutable (font3));
309 hb_font_make_immutable (font3);
310 g_assert (hb_font_is_immutable (font1));
311 g_assert (hb_font_is_immutable (font2));
312 g_assert (hb_font_is_immutable (font3));
313 hb_font_destroy (font2);
314 hb_font_destroy (font3);
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400315
316 font2 = hb_font_create_sub_font (font1);
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400317 hb_font_destroy (font1);
318
319 /* setup font2 to override some funcs */
320 ffuncs2 = hb_font_funcs_create ();
Behdad Esfahbodd3169122011-05-25 11:01:32 -0400321 hb_font_funcs_set_glyph_contour_point_func (ffuncs2, contour_point_func2, NULL, NULL);
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400322 hb_font_set_funcs (font2, ffuncs2, NULL, NULL);
323 hb_font_funcs_destroy (ffuncs2);
324
325 x = y = 1;
Behdad Esfahbodd3169122011-05-25 11:01:32 -0400326 g_assert (hb_font_get_glyph_contour_point_for_origin (font2, 1, 2, HB_DIRECTION_LTR, &x, &y));
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400327 g_assert_cmpint (x, ==, 6);
328 g_assert_cmpint (y, ==, 7);
Behdad Esfahbodd3169122011-05-25 11:01:32 -0400329 g_assert (hb_font_get_glyph_contour_point_for_origin (font2, 2, 5, HB_DIRECTION_RTL, &x, &y));
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400330 g_assert_cmpint (x, ==, 4);
331 g_assert_cmpint (y, ==, 5);
Behdad Esfahbodd3169122011-05-25 11:01:32 -0400332 g_assert (!hb_font_get_glyph_contour_point_for_origin (font2, 3, 7, HB_DIRECTION_LTR, &x, &y));
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400333 g_assert_cmpint (x, ==, 0);
334 g_assert_cmpint (y, ==, 0);
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400335 x = hb_font_get_glyph_h_advance (font2, 1);
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400336 g_assert_cmpint (x, ==, 8);
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400337 x = hb_font_get_glyph_h_advance (font2, 2);
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400338 g_assert_cmpint (x, ==, 0);
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400339
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400340 /* setup font3 to override scale */
Behdad Esfahbod1866e172015-10-02 14:21:29 +0100341 font3 = hb_font_create_sub_font (font2);
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400342 hb_font_set_scale (font3, 20, 30);
343
344 x = y = 1;
Behdad Esfahbodd3169122011-05-25 11:01:32 -0400345 g_assert (hb_font_get_glyph_contour_point_for_origin (font3, 1, 2, HB_DIRECTION_RTL, &x, &y));
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400346 g_assert_cmpint (x, ==, 6*2);
347 g_assert_cmpint (y, ==, 7*3);
Behdad Esfahbodd3169122011-05-25 11:01:32 -0400348 g_assert (hb_font_get_glyph_contour_point_for_origin (font3, 2, 5, HB_DIRECTION_LTR, &x, &y));
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400349 g_assert_cmpint (x, ==, 4*2);
350 g_assert_cmpint (y, ==, 5*3);
Behdad Esfahbodd3169122011-05-25 11:01:32 -0400351 g_assert (!hb_font_get_glyph_contour_point_for_origin (font3, 3, 7, HB_DIRECTION_LTR, &x, &y));
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400352 g_assert_cmpint (x, ==, 0*2);
353 g_assert_cmpint (y, ==, 0*3);
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400354 x = hb_font_get_glyph_h_advance (font3, 1);
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400355 g_assert_cmpint (x, ==, 8*2);
Behdad Esfahbod2d8ebcb2011-05-25 11:27:33 -0400356 x = hb_font_get_glyph_h_advance (font3, 2);
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400357 g_assert_cmpint (x, ==, 0*2);
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400358
359
360 hb_font_destroy (font3);
Ebrahim Byagowief9307f2018-09-22 16:45:31 +0330361 hb_font_destroy (font2);
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400362}
363
Behdad Esfahbod77d5c3d2018-10-19 19:01:01 -0700364static hb_bool_t
365nominal_glyph_func (hb_font_t *font HB_UNUSED,
366 void *font_data HB_UNUSED,
Ebrahim Byagowi8931bc42018-10-20 23:23:32 +0330367 hb_codepoint_t unicode HB_UNUSED,
Behdad Esfahbod77d5c3d2018-10-19 19:01:01 -0700368 hb_codepoint_t *glyph,
369 void *user_data HB_UNUSED)
370{
371 *glyph = 0;
372 return FALSE;
373}
374
375static unsigned int
376nominal_glyphs_func (hb_font_t *font HB_UNUSED,
377 void *font_data HB_UNUSED,
378 unsigned int count HB_UNUSED,
379 const hb_codepoint_t *first_unicode HB_UNUSED,
380 unsigned int unicode_stride HB_UNUSED,
381 hb_codepoint_t *first_glyph HB_UNUSED,
382 unsigned int glyph_stride HB_UNUSED,
383 void *user_data HB_UNUSED)
384{
385 return 0;
386}
387
388static void
389test_fontfuncs_parallels (void)
390{
391 hb_blob_t *blob;
392 hb_face_t *face;
393
394 hb_font_funcs_t *ffuncs1;
395 hb_font_funcs_t *ffuncs2;
396
397 hb_font_t *font0;
398 hb_font_t *font1;
399 hb_font_t *font2;
Chun-wei Fan1e09add2018-12-12 01:32:01 +0800400 hb_codepoint_t glyph;
Behdad Esfahbod77d5c3d2018-10-19 19:01:01 -0700401
402 blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
403 face = hb_face_create (blob, 0);
404 hb_blob_destroy (blob);
405 font0 = hb_font_create (face);
406 hb_face_destroy (face);
407
408 /* setup sub-font1 */
409 font1 = hb_font_create_sub_font (font0);
410 hb_font_destroy (font0);
411 ffuncs1 = hb_font_funcs_create ();
412 hb_font_funcs_set_nominal_glyph_func (ffuncs1, nominal_glyph_func, NULL, NULL);
413 hb_font_set_funcs (font1, ffuncs1, NULL, NULL);
414 hb_font_funcs_destroy (ffuncs1);
415
416 /* setup sub-font2 */
417 font2 = hb_font_create_sub_font (font1);
418 hb_font_destroy (font1);
419 ffuncs2 = hb_font_funcs_create ();
420 hb_font_funcs_set_nominal_glyphs_func (ffuncs1, nominal_glyphs_func, NULL, NULL);
421 hb_font_set_funcs (font2, ffuncs2, NULL, NULL);
422 hb_font_funcs_destroy (ffuncs2);
423
424 /* Just test that calling get_nominal_glyph doesn't infinite-loop. */
Behdad Esfahbod77d5c3d2018-10-19 19:01:01 -0700425 hb_font_get_nominal_glyph (font2, 0x0020u, &glyph);
Behdad Esfahbodf11c5572018-10-20 11:56:30 -0700426
427 hb_font_destroy (font2);
Behdad Esfahbod77d5c3d2018-10-19 19:01:01 -0700428}
Behdad Esfahbod2ca0b5a2011-05-11 23:57:36 -0400429
430static void
Behdad Esfahbodda603e82011-05-11 22:52:35 -0400431test_font_empty (void)
432{
Philip Withnalla6ced902017-02-08 02:18:33 +0000433 hb_font_t *created_from_empty;
434 hb_font_t *created_from_null;
435 hb_font_t *created_sub_from_null;
436
Behdad Esfahbodda603e82011-05-11 22:52:35 -0400437 g_assert (hb_font_get_empty ());
Philip Withnalla6ced902017-02-08 02:18:33 +0000438
439 created_from_empty = hb_font_create (hb_face_get_empty ());
440 g_assert (hb_font_get_empty () != created_from_empty);
441
442 created_from_null = hb_font_create (NULL);
443 g_assert (hb_font_get_empty () != created_from_null);
444
445 created_sub_from_null = hb_font_create_sub_font (NULL);
446 g_assert (hb_font_get_empty () != created_sub_from_null);
447
Behdad Esfahbodda603e82011-05-11 22:52:35 -0400448 g_assert (hb_font_is_immutable (hb_font_get_empty ()));
Behdad Esfahbod74d9fa32011-05-11 23:07:47 -0400449
450 g_assert (hb_font_get_face (hb_font_get_empty ()) == hb_face_get_empty ());
451 g_assert (hb_font_get_parent (hb_font_get_empty ()) == NULL);
Philip Withnalla6ced902017-02-08 02:18:33 +0000452
453 hb_font_destroy (created_sub_from_null);
454 hb_font_destroy (created_from_null);
455 hb_font_destroy (created_from_empty);
Behdad Esfahbodda603e82011-05-11 22:52:35 -0400456}
457
Behdad Esfahbod606923b2011-05-11 23:05:02 -0400458static void
459test_font_properties (void)
460{
461 hb_blob_t *blob;
462 hb_face_t *face;
463 hb_font_t *font;
Behdad Esfahbodcdb15312011-05-11 23:12:58 -0400464 hb_font_t *subfont;
Behdad Esfahbod606923b2011-05-11 23:05:02 -0400465 int x_scale, y_scale;
466 unsigned int x_ppem, y_ppem;
Behdad Esfahbod88da7bb2015-10-02 14:38:20 +0100467 unsigned int upem;
Behdad Esfahbod606923b2011-05-11 23:05:02 -0400468
469 blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
470 face = hb_face_create (blob, 0);
471 hb_blob_destroy (blob);
472 font = hb_font_create (face);
473 hb_face_destroy (face);
474
475
Behdad Esfahboddb9f4eb2011-05-11 23:06:02 -0400476 g_assert (hb_font_get_face (font) == face);
Behdad Esfahbodda29b432015-11-04 20:22:44 -0800477 g_assert (hb_font_get_parent (font) == hb_font_get_empty ());
Behdad Esfahbod3e905e32015-10-08 12:51:02 -0400478 subfont = hb_font_create_sub_font (font);
479 g_assert (hb_font_get_parent (subfont) == font);
480 hb_font_set_parent(subfont, NULL);
481 g_assert (hb_font_get_parent (subfont) == hb_font_get_empty());
482 hb_font_set_parent(subfont, font);
483 g_assert (hb_font_get_parent (subfont) == font);
484 hb_font_set_parent(subfont, NULL);
485 hb_font_make_immutable (subfont);
486 g_assert (hb_font_get_parent (subfont) == hb_font_get_empty());
487 hb_font_set_parent(subfont, font);
488 g_assert (hb_font_get_parent (subfont) == hb_font_get_empty());
489 hb_font_destroy (subfont);
Behdad Esfahboddb9f4eb2011-05-11 23:06:02 -0400490
491
Behdad Esfahbod606923b2011-05-11 23:05:02 -0400492 /* Check scale */
493
Behdad Esfahbod88da7bb2015-10-02 14:38:20 +0100494 upem = hb_face_get_upem (hb_font_get_face (font));
Behdad Esfahbod606923b2011-05-11 23:05:02 -0400495 hb_font_get_scale (font, NULL, NULL);
496 x_scale = y_scale = 13;
497 hb_font_get_scale (font, &x_scale, NULL);
Behdad Esfahbod88da7bb2015-10-02 14:38:20 +0100498 g_assert_cmpint (x_scale, ==, upem);
Behdad Esfahbod606923b2011-05-11 23:05:02 -0400499 x_scale = y_scale = 13;
500 hb_font_get_scale (font, NULL, &y_scale);
Behdad Esfahbod88da7bb2015-10-02 14:38:20 +0100501 g_assert_cmpint (y_scale, ==, upem);
Behdad Esfahbod606923b2011-05-11 23:05:02 -0400502 x_scale = y_scale = 13;
503 hb_font_get_scale (font, &x_scale, &y_scale);
Behdad Esfahbod88da7bb2015-10-02 14:38:20 +0100504 g_assert_cmpint (x_scale, ==, upem);
505 g_assert_cmpint (y_scale, ==, upem);
Behdad Esfahbod606923b2011-05-11 23:05:02 -0400506
507 hb_font_set_scale (font, 17, 19);
508
509 x_scale = y_scale = 13;
510 hb_font_get_scale (font, &x_scale, &y_scale);
511 g_assert_cmpint (x_scale, ==, 17);
512 g_assert_cmpint (y_scale, ==, 19);
513
514
515 /* Check ppem */
516
517 hb_font_get_ppem (font, NULL, NULL);
518 x_ppem = y_ppem = 13;
519 hb_font_get_ppem (font, &x_ppem, NULL);
520 g_assert_cmpint (x_ppem, ==, 0);
521 x_ppem = y_ppem = 13;
522 hb_font_get_ppem (font, NULL, &y_ppem);
523 g_assert_cmpint (y_ppem, ==, 0);
524 x_ppem = y_ppem = 13;
525 hb_font_get_ppem (font, &x_ppem, &y_ppem);
526 g_assert_cmpint (x_ppem, ==, 0);
527 g_assert_cmpint (y_ppem, ==, 0);
528
529 hb_font_set_ppem (font, 17, 19);
530
531 x_ppem = y_ppem = 13;
532 hb_font_get_ppem (font, &x_ppem, &y_ppem);
533 g_assert_cmpint (x_ppem, ==, 17);
534 g_assert_cmpint (y_ppem, ==, 19);
535
Behdad Esfahbod777c2242018-11-04 02:40:20 -0500536 /* Check ptem */
537 g_assert_cmpint (hb_font_get_ptem (font), ==, 0);
538 hb_font_set_ptem (font, 42);
539 g_assert_cmpint (hb_font_get_ptem (font), ==, 42);
540
Behdad Esfahbod606923b2011-05-11 23:05:02 -0400541
542 /* Check immutable */
543
544 g_assert (!hb_font_is_immutable (font));
545 hb_font_make_immutable (font);
546 g_assert (hb_font_is_immutable (font));
547
548 hb_font_set_scale (font, 10, 12);
549 x_scale = y_scale = 13;
550 hb_font_get_scale (font, &x_scale, &y_scale);
551 g_assert_cmpint (x_scale, ==, 17);
552 g_assert_cmpint (y_scale, ==, 19);
553
554 hb_font_set_ppem (font, 10, 12);
555 x_ppem = y_ppem = 13;
556 hb_font_get_ppem (font, &x_ppem, &y_ppem);
557 g_assert_cmpint (x_ppem, ==, 17);
558 g_assert_cmpint (y_ppem, ==, 19);
559
560
Behdad Esfahbodcdb15312011-05-11 23:12:58 -0400561 /* sub_font now */
562 subfont = hb_font_create_sub_font (font);
Behdad Esfahbod606923b2011-05-11 23:05:02 -0400563 hb_font_destroy (font);
Behdad Esfahbodcdb15312011-05-11 23:12:58 -0400564
565 g_assert (hb_font_get_parent (subfont) == font);
566 g_assert (hb_font_get_face (subfont) == face);
567
568 /* scale */
569 x_scale = y_scale = 13;
570 hb_font_get_scale (subfont, &x_scale, &y_scale);
571 g_assert_cmpint (x_scale, ==, 17);
572 g_assert_cmpint (y_scale, ==, 19);
573 hb_font_set_scale (subfont, 10, 12);
574 x_scale = y_scale = 13;
575 hb_font_get_scale (subfont, &x_scale, &y_scale);
576 g_assert_cmpint (x_scale, ==, 10);
577 g_assert_cmpint (y_scale, ==, 12);
578 x_scale = y_scale = 13;
579 hb_font_get_scale (font, &x_scale, &y_scale);
580 g_assert_cmpint (x_scale, ==, 17);
581 g_assert_cmpint (y_scale, ==, 19);
582
583 /* ppem */
584 x_ppem = y_ppem = 13;
585 hb_font_get_ppem (subfont, &x_ppem, &y_ppem);
586 g_assert_cmpint (x_ppem, ==, 17);
587 g_assert_cmpint (y_ppem, ==, 19);
588 hb_font_set_ppem (subfont, 10, 12);
589 x_ppem = y_ppem = 13;
590 hb_font_get_ppem (subfont, &x_ppem, &y_ppem);
591 g_assert_cmpint (x_ppem, ==, 10);
592 g_assert_cmpint (y_ppem, ==, 12);
593 x_ppem = y_ppem = 13;
594 hb_font_get_ppem (font, &x_ppem, &y_ppem);
595 g_assert_cmpint (x_ppem, ==, 17);
596 g_assert_cmpint (y_ppem, ==, 19);
597
598 hb_font_destroy (subfont);
Behdad Esfahbod606923b2011-05-11 23:05:02 -0400599}
600
Behdad Esfahbodda603e82011-05-11 22:52:35 -0400601int
602main (int argc, char **argv)
603{
604 hb_test_init (&argc, &argv);
605
606 hb_test_add (test_face_empty);
Behdad Esfahbod2ca0b5a2011-05-11 23:57:36 -0400607 hb_test_add (test_face_create);
608 hb_test_add (test_face_createfortables);
Behdad Esfahbodda603e82011-05-11 22:52:35 -0400609
Behdad Esfahbodea93e7b2011-05-11 23:22:55 -0400610 hb_test_add (test_fontfuncs_empty);
Behdad Esfahbodf2c1dd42011-05-12 00:35:12 -0400611 hb_test_add (test_fontfuncs_nil);
612 hb_test_add (test_fontfuncs_subclassing);
Behdad Esfahbod77d5c3d2018-10-19 19:01:01 -0700613 hb_test_add (test_fontfuncs_parallels);
Behdad Esfahbodda603e82011-05-11 22:52:35 -0400614
615 hb_test_add (test_font_empty);
Behdad Esfahbod606923b2011-05-11 23:05:02 -0400616 hb_test_add (test_font_properties);
Behdad Esfahbodda603e82011-05-11 22:52:35 -0400617
Behdad Esfahbodda603e82011-05-11 22:52:35 -0400618 return hb_test_run();
619}