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 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('g','l','y','f'));
74 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('l','o','c', 'a'));
75 check_maxp_num_glyphs(face_abc_subset, 3, true);
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_components(void)83 test_subset_glyf_with_components (void)
84 {
85 hb_face_t *face_components = hb_test_open_font_file ("fonts/Roboto-Regular.components.ttf");
86 hb_face_t *face_subset = hb_test_open_font_file ("fonts/Roboto-Regular.components.subset.ttf");
87
88 hb_set_t *codepoints = hb_set_create();
89 hb_face_t *face_generated_subset;
90 hb_set_add (codepoints, 0x1fc);
91 face_generated_subset = hb_subset_test_create_subset (face_components, hb_subset_test_create_input (codepoints));
92 hb_set_destroy (codepoints);
93
94 hb_subset_test_check (face_subset, face_generated_subset, HB_TAG ('g','l','y','f'));
95 hb_subset_test_check (face_subset, face_generated_subset, HB_TAG ('l','o','c', 'a'));
96 check_maxp_num_glyphs(face_generated_subset, 4, true);
97
98 hb_face_destroy (face_generated_subset);
99 hb_face_destroy (face_subset);
100 hb_face_destroy (face_components);
101 }
102
103 static void
test_subset_glyf_with_gsub(void)104 test_subset_glyf_with_gsub (void)
105 {
106 hb_face_t *face_fil = hb_test_open_font_file ("fonts/Roboto-Regular.gsub.fil.ttf");
107 hb_face_t *face_fi = hb_test_open_font_file ("fonts/Roboto-Regular.gsub.fi.ttf");
108 hb_subset_input_t *input;
109 hb_face_t *face_subset;
110
111 hb_set_t *codepoints = hb_set_create();
112 hb_set_add (codepoints, 102); // f
113 hb_set_add (codepoints, 105); // i
114
115 input = hb_subset_test_create_input (codepoints);
116 hb_set_destroy (codepoints);
117 hb_subset_input_set_drop_layout (input, false);
118
119 face_subset = hb_subset_test_create_subset (face_fil, input);
120
121 hb_subset_test_check (face_fi, face_subset, HB_TAG ('g','l','y','f'));
122 hb_subset_test_check (face_fi, face_subset, HB_TAG ('l','o','c', 'a'));
123 check_maxp_num_glyphs(face_subset, 5, true);
124
125 hb_face_destroy (face_subset);
126 hb_face_destroy (face_fi);
127 hb_face_destroy (face_fil);
128 }
129
130 static void
test_subset_glyf_without_gsub(void)131 test_subset_glyf_without_gsub (void)
132 {
133 hb_face_t *face_fil = hb_test_open_font_file ("fonts/Roboto-Regular.gsub.fil.ttf");
134 hb_face_t *face_fi = hb_test_open_font_file ("fonts/Roboto-Regular.nogsub.fi.ttf");
135 hb_subset_input_t *input;
136 hb_face_t *face_subset;
137
138 hb_set_t *codepoints = hb_set_create();
139 hb_set_add (codepoints, 102); // f
140 hb_set_add (codepoints, 105); // i
141
142 input = hb_subset_test_create_input (codepoints);
143 hb_set_destroy (codepoints);
144 hb_subset_input_set_drop_layout (input, true);
145
146 face_subset = hb_subset_test_create_subset (face_fil, input);
147
148 hb_subset_test_check (face_fi, face_subset, HB_TAG ('g','l','y','f'));
149 hb_subset_test_check (face_fi, face_subset, HB_TAG ('l','o','c', 'a'));
150 check_maxp_num_glyphs(face_subset, 3, true);
151
152 hb_face_destroy (face_subset);
153 hb_face_destroy (face_fi);
154 hb_face_destroy (face_fil);
155 }
156
157 static void
test_subset_glyf_noop(void)158 test_subset_glyf_noop (void)
159 {
160 hb_face_t *face_abc = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
161
162 hb_set_t *codepoints = hb_set_create();
163 hb_face_t *face_abc_subset;
164 hb_set_add (codepoints, 97);
165 hb_set_add (codepoints, 98);
166 hb_set_add (codepoints, 99);
167 face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
168 hb_set_destroy (codepoints);
169
170 hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('g','l','y','f'));
171 hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('l','o','c', 'a'));
172 check_maxp_num_glyphs(face_abc_subset, 4, true);
173
174 hb_face_destroy (face_abc_subset);
175 hb_face_destroy (face_abc);
176 }
177
178 static void
test_subset_glyf_strip_hints_simple(void)179 test_subset_glyf_strip_hints_simple (void)
180 {
181 hb_face_t *face_abc = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
182 hb_face_t *face_ac = hb_test_open_font_file ("fonts/Roboto-Regular.ac.nohints.ttf");
183
184 hb_set_t *codepoints = hb_set_create();
185 hb_subset_input_t *input;
186 hb_face_t *face_abc_subset;
187 hb_set_add (codepoints, 'a');
188 hb_set_add (codepoints, 'c');
189 input = hb_subset_test_create_input (codepoints);
190 hb_subset_input_set_drop_hints (input, true);
191 face_abc_subset = hb_subset_test_create_subset (face_abc, input);
192 hb_set_destroy (codepoints);
193
194 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('l','o','c', 'a'));
195 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('g','l','y','f'));
196 check_maxp_num_glyphs(face_abc_subset, 3, false);
197
198 hb_face_destroy (face_abc_subset);
199 hb_face_destroy (face_abc);
200 hb_face_destroy (face_ac);
201 }
202
203 static void
test_subset_glyf_strip_hints_composite(void)204 test_subset_glyf_strip_hints_composite (void)
205 {
206 hb_face_t *face_components = hb_test_open_font_file ("fonts/Roboto-Regular.components.ttf");
207 hb_face_t *face_subset = hb_test_open_font_file ("fonts/Roboto-Regular.components.1fc.nohints.ttf");
208
209 hb_set_t *codepoints = hb_set_create();
210 hb_subset_input_t *input;
211 hb_face_t *face_generated_subset;
212 hb_set_add (codepoints, 0x1fc);
213 input = hb_subset_test_create_input (codepoints);
214 hb_subset_input_set_drop_hints (input, true);
215
216 face_generated_subset = hb_subset_test_create_subset (face_components, input);
217 hb_set_destroy (codepoints);
218
219 hb_subset_test_check (face_subset, face_generated_subset, HB_TAG ('g','l','y','f'));
220 hb_subset_test_check (face_subset, face_generated_subset, HB_TAG ('l','o','c', 'a'));
221 check_maxp_num_glyphs(face_generated_subset, 4, false);
222
223 hb_face_destroy (face_generated_subset);
224 hb_face_destroy (face_subset);
225 hb_face_destroy (face_components);
226 }
227
228 static void
test_subset_glyf_strip_hints_invalid(void)229 test_subset_glyf_strip_hints_invalid (void)
230 {
231 hb_face_t *face = hb_test_open_font_file ("../fuzzing/fonts/oom-ccc61c92d589f895174cdef6ff2e3b20e9999a1a");
232
233 hb_set_t *codepoints = hb_set_create();
234 const hb_codepoint_t text[] =
235 {
236 'A', 'B', 'C', 'D', 'E', 'X', 'Y', 'Z', '1', '2',
237 '3', '@', '_', '%', '&', ')', '*', '$', '!'
238 };
239 unsigned int i;
240 hb_subset_input_t *input;
241 hb_face_t *face_subset;
242
243 for (i = 0; i < sizeof (text) / sizeof (hb_codepoint_t); i++)
244 {
245 hb_set_add (codepoints, text[i]);
246 }
247
248 input = hb_subset_test_create_input (codepoints);
249 hb_subset_input_set_drop_hints (input, true);
250 hb_set_destroy (codepoints);
251
252 face_subset = hb_subset_test_create_subset (face, input);
253 g_assert (face_subset);
254 g_assert (face_subset == hb_face_get_empty ());
255
256 hb_face_destroy (face_subset);
257 hb_face_destroy (face);
258 }
259
260 // TODO(grieger): test for long loca generation.
261
262 int
main(int argc,char ** argv)263 main (int argc, char **argv)
264 {
265 hb_test_init (&argc, &argv);
266
267 hb_test_add (test_subset_glyf_noop);
268 hb_test_add (test_subset_glyf);
269 hb_test_add (test_subset_glyf_strip_hints_simple);
270 hb_test_add (test_subset_glyf_strip_hints_composite);
271 hb_test_add (test_subset_glyf_strip_hints_invalid);
272 hb_test_add (test_subset_glyf_with_components);
273 hb_test_add (test_subset_glyf_with_gsub);
274 hb_test_add (test_subset_glyf_without_gsub);
275
276 return hb_test_run();
277 }
278