1 /*
2 * Copyright © 2018 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 CFF/CFF2 glyph extents */
31
32 static void
test_extents_cff1(void)33 test_extents_cff1 (void)
34 {
35 hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansPro-Regular.abc.otf");
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, 1, &extents);
44 g_assert (result);
45
46 g_assert_cmpint (extents.x_bearing, ==, 52);
47 g_assert_cmpint (extents.y_bearing, ==, 498);
48 g_assert_cmpint (extents.width, ==, 381);
49 g_assert_cmpint (extents.height, ==, -510);
50
51 hb_font_destroy (font);
52
53 hb_face_t *face_j = hb_test_open_font_file ("fonts/SourceHanSans-Regular.41,3041,4C2E.otf");
54 g_assert (face_j);
55 hb_font_t *font_j = hb_font_create (face_j);
56 hb_face_destroy (face_j);
57 g_assert (font_j);
58 hb_ot_font_set_funcs (font_j);
59
60 hb_bool_t result_j = hb_font_get_glyph_extents (font_j, 3, &extents);
61 g_assert (result_j);
62
63 g_assert_cmpint (extents.x_bearing, ==, 34);
64 g_assert_cmpint (extents.y_bearing, ==, 840);
65 g_assert_cmpint (extents.width, ==, 920);
66 g_assert_cmpint (extents.height, ==, -907);
67
68 hb_font_destroy (font_j);
69 }
70
71 static void
test_extents_cff1_flex(void)72 test_extents_cff1_flex (void)
73 {
74 hb_face_t *face = hb_test_open_font_file ("fonts/cff1_flex.otf");
75 g_assert (face);
76 hb_font_t *font = hb_font_create (face);
77 hb_face_destroy (face);
78 g_assert (font);
79 hb_ot_font_set_funcs (font);
80
81 hb_glyph_extents_t extents;
82 hb_bool_t result = hb_font_get_glyph_extents (font, 1, &extents);
83 g_assert (result);
84
85 g_assert_cmpint (extents.x_bearing, ==, -20);
86 g_assert_cmpint (extents.y_bearing, ==, 520);
87 g_assert_cmpint (extents.width, ==, 540);
88 g_assert_cmpint (extents.height, ==, -540);
89
90 hb_font_destroy (font);
91 }
92
93 static void
test_extents_cff1_seac(void)94 test_extents_cff1_seac (void)
95 {
96 hb_face_t *face = hb_test_open_font_file ("fonts/cff1_seac.otf");
97 g_assert (face);
98 hb_font_t *font = hb_font_create (face);
99 hb_face_destroy (face);
100 g_assert (font);
101 hb_ot_font_set_funcs (font);
102
103 hb_glyph_extents_t extents;
104 hb_bool_t result = hb_font_get_glyph_extents (font, 3, &extents); /* Agrave */
105 g_assert (result);
106
107 g_assert_cmpint (extents.x_bearing, ==, 3);
108 g_assert_cmpint (extents.y_bearing, ==, 861);
109 g_assert_cmpint (extents.width, ==, 538);
110 g_assert_cmpint (extents.height, ==, -861);
111
112 result = hb_font_get_glyph_extents (font, 4, &extents); /* Udieresis */
113 g_assert (result);
114
115 g_assert_cmpint (extents.x_bearing, ==, 87);
116 g_assert_cmpint (extents.y_bearing, ==, 827);
117 g_assert_cmpint (extents.width, ==, 471);
118 g_assert_cmpint (extents.height, ==, -839);
119
120 hb_font_destroy (font);
121 }
122
123 static void
test_extents_cff2(void)124 test_extents_cff2 (void)
125 {
126 hb_face_t *face = hb_test_open_font_file ("fonts/AdobeVFPrototype.abc.otf");
127 g_assert (face);
128 hb_font_t *font = hb_font_create (face);
129 hb_face_destroy (face);
130 g_assert (font);
131 hb_ot_font_set_funcs (font);
132
133 hb_glyph_extents_t extents;
134 hb_bool_t result = hb_font_get_glyph_extents (font, 1, &extents);
135 g_assert (result);
136
137 g_assert_cmpint (extents.x_bearing, ==, 46);
138 g_assert_cmpint (extents.y_bearing, ==, 487);
139 g_assert_cmpint (extents.width, ==, 455);
140 g_assert_cmpint (extents.height, ==, -500);
141
142 float coords[2] = { 600.0f, 50.0f };
143 hb_font_set_var_coords_design (font, coords, 2);
144 result = hb_font_get_glyph_extents (font, 1, &extents);
145 g_assert (result);
146
147 g_assert_cmpint (extents.x_bearing, ==, 38);
148 g_assert_cmpint (extents.y_bearing, ==, 493);
149 g_assert_cmpint (extents.width, ==, 480);
150 g_assert_cmpint (extents.height, ==, -507);
151
152 hb_font_destroy (font);
153 }
154
155 static void
test_extents_cff2_vsindex(void)156 test_extents_cff2_vsindex (void)
157 {
158 hb_face_t *face = hb_test_open_font_file ("fonts/AdobeVFPrototype_vsindex.otf");
159 g_assert (face);
160 hb_font_t *font = hb_font_create (face);
161 hb_face_destroy (face);
162 g_assert (font);
163 hb_ot_font_set_funcs (font);
164
165 hb_glyph_extents_t extents;
166 float coords[2] = { 800.0f, 50.0f };
167 hb_font_set_var_coords_design (font, coords, 2);
168 hb_bool_t result = hb_font_get_glyph_extents (font, 1, &extents);
169 g_assert (result);
170
171 g_assert_cmpint (extents.x_bearing, ==, 12);
172 g_assert_cmpint (extents.y_bearing, ==, 655);
173 g_assert_cmpint (extents.width, ==, 652);
174 g_assert_cmpint (extents.height, ==, -655);
175
176 result = hb_font_get_glyph_extents (font, 2, &extents);
177 g_assert (result);
178
179 g_assert_cmpint (extents.x_bearing, ==, 8);
180 g_assert_cmpint (extents.y_bearing, ==, 669);
181 g_assert_cmpint (extents.width, ==, 649);
182 g_assert_cmpint (extents.height, ==, -669);
183
184 hb_font_destroy (font);
185 }
186
187 static void
test_extents_cff2_vsindex_named_instance(void)188 test_extents_cff2_vsindex_named_instance (void)
189 {
190 hb_face_t *face = hb_test_open_font_file ("fonts/AdobeVFPrototype_vsindex.otf");
191 g_assert (face);
192 hb_font_t *font = hb_font_create (face);
193 hb_face_destroy (face);
194 g_assert (font);
195 hb_ot_font_set_funcs (font);
196
197 hb_font_set_var_named_instance (font, 6); // 6 (BlackMediumContrast): 900, 50
198 hb_glyph_extents_t extents;
199 hb_bool_t result = hb_font_get_glyph_extents (font, 1, &extents);
200 g_assert (result);
201
202 g_assert_cmpint (extents.x_bearing, ==, 13);
203 g_assert_cmpint (extents.y_bearing, ==, 652);
204 g_assert_cmpint (extents.width, ==, 653);
205 g_assert_cmpint (extents.height, ==, -652);
206
207 result = hb_font_get_glyph_extents (font, 2, &extents);
208 g_assert (result);
209
210 g_assert_cmpint (extents.x_bearing, ==, 6);
211 g_assert_cmpint (extents.y_bearing, ==, 675);
212 g_assert_cmpint (extents.width, ==, 647);
213 g_assert_cmpint (extents.height, ==, -675);
214
215 hb_font_destroy (font);
216 }
217
218 int
main(int argc,char ** argv)219 main (int argc, char **argv)
220 {
221 hb_test_init (&argc, &argv);
222
223 hb_test_add (test_extents_cff1);
224 hb_test_add (test_extents_cff1_flex);
225 hb_test_add (test_extents_cff1_seac);
226 hb_test_add (test_extents_cff2);
227 hb_test_add (test_extents_cff2_vsindex);
228 hb_test_add (test_extents_cff2_vsindex_named_instance);
229
230 return hb_test_run ();
231 }
232