1 /*
2 * Copyright © 2019 Adobe 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 * Adobe Author(s): Michiharu Ariza
25 */
26
27 #include "hb-test.h"
28 #include <hb-ot.h>
29
30 /* Unit tests for glyph advance widths and extents of TrueType variable fonts */
31
32 static void
test_extents_tt_var(void)33 test_extents_tt_var (void)
34 {
35 hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman-nohvar-41,C1.ttf");
36 g_assert (face);
37 hb_font_t *font = hb_font_create (face);
38 hb_face_destroy (face);
39 g_assert (font);
40 hb_ot_font_set_funcs (font);
41
42 hb_glyph_extents_t extents;
43 hb_bool_t result = hb_font_get_glyph_extents (font, 2, &extents);
44 g_assert (result);
45
46 g_assert_cmpint (extents.x_bearing, ==, 10);
47 g_assert_cmpint (extents.y_bearing, ==, 846);
48 g_assert_cmpint (extents.width, ==, 500);
49 g_assert_cmpint (extents.height, ==, -846);
50
51 float coords[1] = { 500.0f };
52 hb_font_set_var_coords_design (font, coords, 1);
53 result = hb_font_get_glyph_extents (font, 2, &extents);
54 g_assert (result);
55
56 g_assert_cmpint (extents.x_bearing, ==, 0);
57 g_assert_cmpint (extents.y_bearing, ==, 874);
58 g_assert_cmpint (extents.width, ==, 551);
59 g_assert_cmpint (extents.height, ==, -874);
60
61 hb_font_destroy (font);
62 }
63
64 static void
test_advance_tt_var_nohvar(void)65 test_advance_tt_var_nohvar (void)
66 {
67 hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman-nohvar-41,C1.ttf");
68 g_assert (face);
69 hb_font_t *font = hb_font_create (face);
70 hb_face_destroy (face);
71 g_assert (font);
72 hb_ot_font_set_funcs (font);
73
74 hb_position_t x, y;
75 hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_LTR, &x, &y);
76
77 g_assert_cmpint (x, ==, 520);
78 g_assert_cmpint (y, ==, 0);
79
80 hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y);
81
82 g_assert_cmpint (x, ==, 0);
83 g_assert_cmpint (y, ==, -1000);
84
85 float coords[1] = { 500.0f };
86 hb_font_set_var_coords_design (font, coords, 1);
87 hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_LTR, &x, &y);
88
89 g_assert_cmpint (x, ==, 551);
90 g_assert_cmpint (y, ==, 0);
91
92 hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y);
93
94 g_assert_cmpint (x, ==, 0);
95 g_assert_cmpint (y, ==, -1000);
96
97 hb_font_destroy (font);
98 }
99
100 static void
test_advance_tt_var_hvarvvar(void)101 test_advance_tt_var_hvarvvar (void)
102 {
103 hb_face_t *face = hb_test_open_font_file ("fonts/SourceSerifVariable-Roman-VVAR.abc.ttf");
104 g_assert (face);
105 hb_font_t *font = hb_font_create (face);
106 hb_face_destroy (face);
107 g_assert (font);
108 hb_ot_font_set_funcs (font);
109
110 hb_position_t x, y;
111 hb_font_get_glyph_advance_for_direction(font, 1, HB_DIRECTION_LTR, &x, &y);
112
113 g_assert_cmpint (x, ==, 508);
114 g_assert_cmpint (y, ==, 0);
115
116 hb_font_get_glyph_advance_for_direction(font, 1, HB_DIRECTION_TTB, &x, &y);
117
118 g_assert_cmpint (x, ==, 0);
119 g_assert_cmpint (y, ==, -1000);
120
121 float coords[1] = { 700.0f };
122 hb_font_set_var_coords_design (font, coords, 1);
123 hb_font_get_glyph_advance_for_direction(font, 1, HB_DIRECTION_LTR, &x, &y);
124
125 g_assert_cmpint (x, ==, 531);
126 g_assert_cmpint (y, ==, 0);
127
128 hb_font_get_glyph_advance_for_direction(font, 1, HB_DIRECTION_TTB, &x, &y);
129
130 g_assert_cmpint (x, ==, 0);
131 g_assert_cmpint (y, ==, -1012);
132
133 hb_font_destroy (font);
134 }
135
136 static void
test_advance_tt_var_anchor(void)137 test_advance_tt_var_anchor (void)
138 {
139 hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.anchor.ttf");
140 g_assert (face);
141 hb_font_t *font = hb_font_create (face);
142 hb_face_destroy (face);
143 g_assert (font);
144 hb_ot_font_set_funcs (font);
145
146 hb_glyph_extents_t extents;
147 hb_bool_t result = hb_font_get_glyph_extents (font, 2, &extents);
148 g_assert (result);
149
150 g_assert_cmpint (extents.x_bearing, ==, 56);
151 g_assert_cmpint (extents.y_bearing, ==, 672);
152 g_assert_cmpint (extents.width, ==, 556);
153 g_assert_cmpint (extents.height, ==, -684);
154
155 float coords[1] = { 500.0f };
156 hb_font_set_var_coords_design (font, coords, 1);
157 result = hb_font_get_glyph_extents (font, 2, &extents);
158 g_assert (result);
159
160 g_assert_cmpint (extents.x_bearing, ==, 50);
161 g_assert_cmpint (extents.y_bearing, ==, 667);
162 g_assert_cmpint (extents.width, ==, 592);
163 g_assert_cmpint (extents.height, ==, -679);
164
165 hb_font_destroy (font);
166 }
167
168 static void
test_extents_tt_var_comp(void)169 test_extents_tt_var_comp (void)
170 {
171 hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.modcomp.ttf");
172 g_assert (face);
173 hb_font_t *font = hb_font_create (face);
174 hb_face_destroy (face);
175 g_assert (font);
176 hb_ot_font_set_funcs (font);
177
178 hb_glyph_extents_t extents;
179 float coords[1] = { 800.0f };
180 hb_font_set_var_coords_design (font, coords, 1);
181
182 hb_bool_t result;
183 result = hb_font_get_glyph_extents (font, 2, &extents); /* Ccedilla, cedilla y-scaled by 0.8, with unscaled component offset */
184 g_assert (result);
185
186 g_assert_cmpint (extents.x_bearing, ==, 19);
187 g_assert_cmpint (extents.y_bearing, ==, 663);
188 g_assert_cmpint (extents.width, ==, 519);
189 g_assert_cmpint (extents.height, ==, -895);
190
191 result = hb_font_get_glyph_extents (font, 3, &extents); /* Cacute, acute y-scaled by 0.8, with unscaled component offset (default) */
192 g_assert (result);
193
194 g_assert_cmpint (extents.x_bearing, ==, 19);
195 g_assert_cmpint (extents.y_bearing, ==, 909);
196 g_assert_cmpint (extents.width, ==, 519);
197 g_assert_cmpint (extents.height, ==, -921);
198
199 result = hb_font_get_glyph_extents (font, 4, &extents); /* Ccaron, caron y-scaled by 0.8, with scaled component offset */
200 g_assert (result);
201
202 g_assert_cmpint (extents.x_bearing, ==, 19);
203 g_assert_cmpint (extents.y_bearing, ==, 866);
204 g_assert_cmpint (extents.width, ==, 519);
205 g_assert_cmpint (extents.height, ==, -878);
206
207 hb_font_destroy (font);
208 }
209
210 static void
test_advance_tt_var_comp_v(void)211 test_advance_tt_var_comp_v (void)
212 {
213 hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.modcomp.ttf");
214 g_assert (face);
215 hb_font_t *font = hb_font_create (face);
216 hb_face_destroy (face);
217 g_assert (font);
218 hb_ot_font_set_funcs (font);
219
220 float coords[1] = { 800.0f };
221 hb_font_set_var_coords_design (font, coords, 1);
222
223 hb_position_t x, y;
224 hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y); /* No VVAR; 'C' in composite Ccedilla determines metrics */
225
226 g_assert_cmpint (x, ==, 0);
227 g_assert_cmpint (y, ==, -991);
228
229 hb_font_get_glyph_origin_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y);
230
231 g_assert_cmpint (x, ==, 292);
232 g_assert_cmpint (y, ==, 1013);
233
234 hb_font_destroy (font);
235 }
236
237 static void
test_advance_tt_var_gvar_infer(void)238 test_advance_tt_var_gvar_infer (void)
239 {
240 hb_face_t *face = hb_test_open_font_file ("fonts/TestGVAREight.ttf");
241 hb_font_t *font = hb_font_create (face);
242 hb_ot_font_set_funcs (font);
243 hb_face_destroy (face);
244
245 int coords[6] = {100};
246 hb_font_set_var_coords_normalized (font, coords, 6);
247
248 hb_glyph_extents_t extents = {0};
249 g_assert (hb_font_get_glyph_extents (font, 4, &extents));
250
251 hb_font_destroy (font);
252 }
253
254 int
main(int argc,char ** argv)255 main (int argc, char **argv)
256 {
257 hb_test_init (&argc, &argv);
258
259 hb_test_add (test_extents_tt_var);
260 hb_test_add (test_advance_tt_var_nohvar);
261 hb_test_add (test_advance_tt_var_hvarvvar);
262 hb_test_add (test_advance_tt_var_anchor);
263 hb_test_add (test_extents_tt_var_comp);
264 hb_test_add (test_advance_tt_var_comp_v);
265 hb_test_add (test_advance_tt_var_gvar_infer);
266
267 return hb_test_run ();
268 }
269