1 /*
2 * Copyright © 2018 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): Garret Rieger
25 */
26
27 #include "hb-test.h"
28 #include "hb-subset-test.h"
29
30 /* Unit tests for hb-subset-glyf.h */
31
check_maxp_field(uint8_t * raw_maxp,unsigned int offset,uint16_t expected_value)32 static void check_maxp_field (uint8_t *raw_maxp, unsigned int offset, uint16_t expected_value)
33 {
34 uint16_t actual_value = (raw_maxp[offset] << 8) + raw_maxp[offset + 1];
35 g_assert_cmpuint(expected_value, ==, actual_value);
36 }
37
check_maxp_num_glyphs(hb_face_t * face,uint16_t expected_num_glyphs,bool hints)38 static void check_maxp_num_glyphs (hb_face_t *face, uint16_t expected_num_glyphs, bool hints)
39 {
40 hb_blob_t *maxp_blob = hb_face_reference_table (face, HB_TAG ('m','a','x', 'p'));
41
42 unsigned int maxp_len;
43 uint8_t *raw_maxp = (uint8_t *) hb_blob_get_data(maxp_blob, &maxp_len);
44
45 check_maxp_field (raw_maxp, 4, expected_num_glyphs); // numGlyphs
46 if (!hints)
47 {
48 check_maxp_field (raw_maxp, 14, 1); // maxZones
49 check_maxp_field (raw_maxp, 16, 0); // maxTwilightPoints
50 check_maxp_field (raw_maxp, 18, 0); // maxStorage
51 check_maxp_field (raw_maxp, 20, 0); // maxFunctionDefs
52 check_maxp_field (raw_maxp, 22, 0); // maxInstructionDefs
53 check_maxp_field (raw_maxp, 24, 0); // maxStackElements
54 check_maxp_field (raw_maxp, 26, 0); // maxSizeOfInstructions
55 }
56
57 hb_blob_destroy (maxp_blob);
58 }
59
60 static void
test_subset_glyf(void)61 test_subset_glyf (void)
62 {
63 hb_face_t *face_abc = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
64 hb_face_t *face_ac = hb_test_open_font_file ("fonts/Roboto-Regular.ac.ttf");
65
66 hb_set_t *codepoints = hb_set_create();
67 hb_face_t *face_abc_subset;
68 hb_set_add (codepoints, 97);
69 hb_set_add (codepoints, 99);
70 face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
71 hb_set_destroy (codepoints);
72
73 check_maxp_num_glyphs(face_abc_subset, 3, true);
74 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('l','o','c', 'a'));
75 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('g','l','y','f'));
76
77 hb_face_destroy (face_abc_subset);
78 hb_face_destroy (face_abc);
79 hb_face_destroy (face_ac);
80 }
81
82 static void
test_subset_glyf_with_input_glyphs(void)83 test_subset_glyf_with_input_glyphs (void)
84 {
85 hb_face_t *face_abc = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
86 hb_face_t *face_ac = hb_test_open_font_file ("fonts/Roboto-Regular.ac.ttf");
87
88 hb_set_t *glyphs = hb_set_create();
89 hb_face_t *face_abc_subset;
90 hb_set_add (glyphs, 1);
91 hb_set_add (glyphs, 3);
92 face_abc_subset =
93 hb_subset_test_create_subset (face_abc, hb_subset_test_create_input_from_glyphs (glyphs));
94 hb_set_destroy (glyphs);
95
96 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('g','l','y','f'));
97 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('l','o','c', 'a'));
98 check_maxp_num_glyphs(face_abc_subset, 3, true);
99
100 hb_face_destroy (face_abc_subset);
101 hb_face_destroy (face_abc);
102 hb_face_destroy (face_ac);
103 }
104
105 static void
test_subset_glyf_with_components(void)106 test_subset_glyf_with_components (void)
107 {
108 hb_face_t *face_components = hb_test_open_font_file ("fonts/Roboto-Regular.components.ttf");
109 hb_face_t *face_subset = hb_test_open_font_file ("fonts/Roboto-Regular.components.subset.ttf");
110
111 hb_set_t *codepoints = hb_set_create();
112 hb_face_t *face_generated_subset;
113 hb_set_add (codepoints, 0x1fc);
114 face_generated_subset = hb_subset_test_create_subset (face_components, hb_subset_test_create_input (codepoints));
115 hb_set_destroy (codepoints);
116
117 hb_subset_test_check (face_subset, face_generated_subset, HB_TAG ('g','l','y','f'));
118 hb_subset_test_check (face_subset, face_generated_subset, HB_TAG ('l','o','c', 'a'));
119 check_maxp_num_glyphs(face_generated_subset, 4, true);
120
121 hb_face_destroy (face_generated_subset);
122 hb_face_destroy (face_subset);
123 hb_face_destroy (face_components);
124 }
125
126 static void
test_subset_glyf_with_gsub(void)127 test_subset_glyf_with_gsub (void)
128 {
129 hb_face_t *face_fil = hb_test_open_font_file ("fonts/Roboto-Regular.gsub.fil.ttf");
130 hb_face_t *face_fi = hb_test_open_font_file ("fonts/Roboto-Regular.gsub.fi.ttf");
131 hb_subset_input_t *input;
132 hb_face_t *face_subset;
133
134 hb_set_t *codepoints = hb_set_create();
135 hb_set_add (codepoints, 102); // f
136 hb_set_add (codepoints, 105); // i
137
138 input = hb_subset_test_create_input (codepoints);
139 hb_set_destroy (codepoints);
140 hb_set_del (hb_subset_input_drop_tables_set (input), HB_TAG('G', 'S', 'U', 'B'));
141 hb_set_del (hb_subset_input_drop_tables_set (input), HB_TAG('G', 'P', 'O', 'S'));
142 hb_set_del (hb_subset_input_drop_tables_set (input), HB_TAG('G', 'D', 'E', 'F'));
143
144 face_subset = hb_subset_test_create_subset (face_fil, input);
145
146 hb_subset_test_check (face_fi, face_subset, HB_TAG ('g','l','y','f'));
147 hb_subset_test_check (face_fi, face_subset, HB_TAG ('l','o','c', 'a'));
148 check_maxp_num_glyphs(face_subset, 5, true);
149
150 hb_face_destroy (face_subset);
151 hb_face_destroy (face_fi);
152 hb_face_destroy (face_fil);
153 }
154
155 static void
test_subset_glyf_without_gsub(void)156 test_subset_glyf_without_gsub (void)
157 {
158 hb_face_t *face_fil = hb_test_open_font_file ("fonts/Roboto-Regular.gsub.fil.ttf");
159 hb_face_t *face_fi = hb_test_open_font_file ("fonts/Roboto-Regular.nogsub.fi.ttf");
160 hb_subset_input_t *input;
161 hb_face_t *face_subset;
162
163 hb_set_t *codepoints = hb_set_create();
164 hb_set_add (codepoints, 102); // f
165 hb_set_add (codepoints, 105); // i
166
167 input = hb_subset_test_create_input (codepoints);
168 hb_set_destroy (codepoints);
169 hb_set_add (hb_subset_input_drop_tables_set (input), HB_TAG('G', 'S', 'U', 'B'));
170 hb_set_add (hb_subset_input_drop_tables_set (input), HB_TAG('G', 'P', 'O', 'S'));
171 hb_set_add (hb_subset_input_drop_tables_set (input), HB_TAG('G', 'D', 'E', 'F'));
172
173 face_subset = hb_subset_test_create_subset (face_fil, input);
174
175 hb_subset_test_check (face_fi, face_subset, HB_TAG ('g','l','y','f'));
176 hb_subset_test_check (face_fi, face_subset, HB_TAG ('l','o','c', 'a'));
177 check_maxp_num_glyphs(face_subset, 3, true);
178
179 hb_face_destroy (face_subset);
180 hb_face_destroy (face_fi);
181 hb_face_destroy (face_fil);
182 }
183
184 static void
test_subset_glyf_noop(void)185 test_subset_glyf_noop (void)
186 {
187 hb_face_t *face_abc = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
188
189 hb_set_t *codepoints = hb_set_create();
190 hb_face_t *face_abc_subset;
191 hb_set_add (codepoints, 97);
192 hb_set_add (codepoints, 98);
193 hb_set_add (codepoints, 99);
194 face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
195 hb_set_destroy (codepoints);
196
197 check_maxp_num_glyphs(face_abc_subset, 4, true);
198 hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('l','o','c', 'a'));
199 hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('g','l','y','f'));
200
201 hb_face_destroy (face_abc_subset);
202 hb_face_destroy (face_abc);
203 }
204
205 static void
test_subset_glyf_strip_hints_simple(void)206 test_subset_glyf_strip_hints_simple (void)
207 {
208 hb_face_t *face_abc = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
209 hb_face_t *face_ac = hb_test_open_font_file ("fonts/Roboto-Regular.ac.nohints.ttf");
210
211 hb_set_t *codepoints = hb_set_create();
212 hb_subset_input_t *input;
213 hb_face_t *face_abc_subset;
214 hb_set_add (codepoints, 'a');
215 hb_set_add (codepoints, 'c');
216 input = hb_subset_test_create_input (codepoints);
217 hb_subset_input_set_drop_hints (input, true);
218 face_abc_subset = hb_subset_test_create_subset (face_abc, input);
219 hb_set_destroy (codepoints);
220
221 check_maxp_num_glyphs(face_abc_subset, 3, false);
222 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('l','o','c', 'a'));
223 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('g','l','y','f'));
224
225 hb_face_destroy (face_abc_subset);
226 hb_face_destroy (face_abc);
227 hb_face_destroy (face_ac);
228 }
229
230 static void
test_subset_glyf_strip_hints_composite(void)231 test_subset_glyf_strip_hints_composite (void)
232 {
233 hb_face_t *face_components = hb_test_open_font_file ("fonts/Roboto-Regular.components.ttf");
234 hb_face_t *face_subset = hb_test_open_font_file ("fonts/Roboto-Regular.components.1fc.nohints.ttf");
235
236 hb_set_t *codepoints = hb_set_create();
237 hb_subset_input_t *input;
238 hb_face_t *face_generated_subset;
239 hb_set_add (codepoints, 0x1fc);
240 input = hb_subset_test_create_input (codepoints);
241 hb_subset_input_set_drop_hints (input, true);
242
243 face_generated_subset = hb_subset_test_create_subset (face_components, input);
244 hb_set_destroy (codepoints);
245
246 check_maxp_num_glyphs(face_generated_subset, 4, false);
247 hb_subset_test_check (face_subset, face_generated_subset, HB_TAG ('l','o','c', 'a'));
248 hb_subset_test_check (face_subset, face_generated_subset, HB_TAG ('g','l','y','f'));
249
250 hb_face_destroy (face_generated_subset);
251 hb_face_destroy (face_subset);
252 hb_face_destroy (face_components);
253 }
254
255 static void
test_subset_glyf_strip_hints_invalid(void)256 test_subset_glyf_strip_hints_invalid (void)
257 {
258 hb_face_t *face = hb_test_open_font_file ("../fuzzing/fonts/oom-ccc61c92d589f895174cdef6ff2e3b20e9999a1a");
259
260 hb_set_t *codepoints = hb_set_create();
261 const hb_codepoint_t text[] =
262 {
263 'A', 'B', 'C', 'D', 'E', 'X', 'Y', 'Z', '1', '2',
264 '3', '@', '_', '%', '&', ')', '*', '$', '!'
265 };
266 unsigned int i;
267 hb_subset_input_t *input;
268 hb_face_t *face_subset;
269
270 for (i = 0; i < sizeof (text) / sizeof (hb_codepoint_t); i++)
271 {
272 hb_set_add (codepoints, text[i]);
273 }
274
275 input = hb_subset_test_create_input (codepoints);
276 hb_subset_input_set_drop_hints (input, true);
277 hb_set_destroy (codepoints);
278
279 face_subset = hb_subset_test_create_subset (face, input);
280 g_assert (face_subset);
281 g_assert (face_subset == hb_face_get_empty ());
282
283 hb_face_destroy (face_subset);
284 hb_face_destroy (face);
285 }
286
287 static void
test_subset_glyf_retain_gids(void)288 test_subset_glyf_retain_gids (void)
289 {
290 hb_face_t *face_abc = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
291 hb_face_t *face_ac = hb_test_open_font_file ("fonts/Roboto-Regular.ac.retaingids.ttf");
292
293 hb_set_t *codepoints = hb_set_create();
294 hb_face_t *face_abc_subset;
295 hb_set_add (codepoints, 97);
296 hb_set_add (codepoints, 99);
297
298 hb_subset_input_t *input = hb_subset_test_create_input (codepoints);
299 hb_subset_input_set_retain_gids (input, true);
300 face_abc_subset = hb_subset_test_create_subset (face_abc, input);
301 hb_set_destroy (codepoints);
302
303 check_maxp_num_glyphs(face_abc_subset, 4, true);
304 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('l','o','c', 'a'));
305 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('g','l','y','f'));
306
307 hb_face_destroy (face_abc_subset);
308 hb_face_destroy (face_abc);
309 hb_face_destroy (face_ac);
310 }
311
312 static void
test_subset_glyf_retain_gids_truncates(void)313 test_subset_glyf_retain_gids_truncates (void)
314 {
315 hb_face_t *face_abc = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
316 hb_face_t *face_a = hb_test_open_font_file ("fonts/Roboto-Regular.a.retaingids.ttf");
317
318 hb_set_t *codepoints = hb_set_create();
319 hb_face_t *face_abc_subset;
320 hb_set_add (codepoints, 97);
321
322 hb_subset_input_t *input = hb_subset_test_create_input (codepoints);
323 hb_subset_input_set_retain_gids (input, true);
324 face_abc_subset = hb_subset_test_create_subset (face_abc, input);
325 hb_set_destroy (codepoints);
326
327 check_maxp_num_glyphs(face_abc_subset, 2, true);
328 hb_subset_test_check (face_a, face_abc_subset, HB_TAG ('l','o','c', 'a'));
329 hb_subset_test_check (face_a, face_abc_subset, HB_TAG ('g','l','y','f'));
330
331 hb_face_destroy (face_abc_subset);
332 hb_face_destroy (face_abc);
333 hb_face_destroy (face_a);
334 }
335
336 // TODO(grieger): test for long loca generation.
337
338 int
main(int argc,char ** argv)339 main (int argc, char **argv)
340 {
341 hb_test_init (&argc, &argv);
342
343 hb_test_add (test_subset_glyf_noop);
344 hb_test_add (test_subset_glyf);
345 hb_test_add (test_subset_glyf_with_input_glyphs);
346 hb_test_add (test_subset_glyf_strip_hints_simple);
347 hb_test_add (test_subset_glyf_strip_hints_composite);
348 hb_test_add (test_subset_glyf_strip_hints_invalid);
349 hb_test_add (test_subset_glyf_with_components);
350 hb_test_add (test_subset_glyf_with_gsub);
351 hb_test_add (test_subset_glyf_without_gsub);
352 hb_test_add (test_subset_glyf_retain_gids);
353 hb_test_add (test_subset_glyf_retain_gids_truncates);
354
355 return hb_test_run();
356 }
357