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
32 static void
33 test_subset_32_tables (void)
34 {
35 hb_face_t *face = hb_test_open_font_file ("../fuzzing/fonts/oom-6ef8c96d3710262511bcc730dce9c00e722cb653");
36
37 hb_subset_input_t *input = hb_subset_input_create_or_fail ();
38 hb_set_t *codepoints = hb_subset_input_unicode_set (input);
39 hb_face_t *subset;
40
41 hb_set_add (codepoints, 'a');
42 hb_set_add (codepoints, 'b');
43 hb_set_add (codepoints, 'c');
44
45 subset = hb_subset_or_fail (face, input);
46 g_assert (subset);
47 g_assert (subset != hb_face_get_empty ());
48
49 hb_subset_input_destroy (input);
50 hb_face_destroy (subset);
51 hb_face_destroy (face);
52 }
53
54 static void
55 test_subset_no_inf_loop (void)
56 {
57 hb_face_t *face = hb_test_open_font_file ("../fuzzing/fonts/clusterfuzz-testcase-minimized-hb-subset-fuzzer-5521982557782016");
58
59 hb_subset_input_t *input = hb_subset_input_create_or_fail ();
60 hb_set_t *codepoints = hb_subset_input_unicode_set (input);
61 hb_face_t *subset;
62
63 hb_set_add (codepoints, 'a');
64 hb_set_add (codepoints, 'b');
65 hb_set_add (codepoints, 'c');
66
67 subset = hb_subset_or_fail (face, input);
68 g_assert (!subset);
69
70 hb_subset_input_destroy (input);
71 hb_face_destroy (subset);
72 hb_face_destroy (face);
73 }
74
75 static void
76 test_subset_crash (void)
77 {
78 hb_face_t *face = hb_test_open_font_file ("../fuzzing/fonts/crash-4b60576767ee4d9fe1cc10959d89baf73d4e8249");
79
80 hb_subset_input_t *input = hb_subset_input_create_or_fail ();
81 hb_set_t *codepoints = hb_subset_input_unicode_set (input);
82 hb_face_t *subset;
83
84 hb_set_add (codepoints, 'a');
85 hb_set_add (codepoints, 'b');
86 hb_set_add (codepoints, 'c');
87
88 subset = hb_subset_or_fail (face, input);
89 g_assert (!subset);
90
91 hb_subset_input_destroy (input);
92 hb_face_destroy (subset);
93 hb_face_destroy (face);
94 }
95
96 static void
97 test_subset_set_flags (void)
98 {
99 hb_subset_input_t *input = hb_subset_input_create_or_fail ();
100
101 g_assert (hb_subset_input_get_flags (input) == HB_SUBSET_FLAGS_DEFAULT);
102
103 hb_subset_input_set_flags (input,
104 HB_SUBSET_FLAGS_NAME_LEGACY |
105 HB_SUBSET_FLAGS_NOTDEF_OUTLINE |
106 HB_SUBSET_FLAGS_GLYPH_NAMES);
107
108 g_assert (hb_subset_input_get_flags (input) ==
109 (hb_subset_flags_t) (
110 HB_SUBSET_FLAGS_NAME_LEGACY |
111 HB_SUBSET_FLAGS_NOTDEF_OUTLINE |
112 HB_SUBSET_FLAGS_GLYPH_NAMES));
113
114 hb_subset_input_set_flags (input,
115 HB_SUBSET_FLAGS_NAME_LEGACY |
116 HB_SUBSET_FLAGS_NOTDEF_OUTLINE |
117 HB_SUBSET_FLAGS_NO_PRUNE_UNICODE_RANGES);
118
119 g_assert (hb_subset_input_get_flags (input) ==
120 (hb_subset_flags_t) (
121 HB_SUBSET_FLAGS_NAME_LEGACY |
122 HB_SUBSET_FLAGS_NOTDEF_OUTLINE |
123 HB_SUBSET_FLAGS_NO_PRUNE_UNICODE_RANGES));
124
125
126 hb_subset_input_destroy (input);
127 }
128
129
130 static void
131 test_subset_sets (void)
132 {
133 hb_subset_input_t *input = hb_subset_input_create_or_fail ();
134 hb_set_t* set = hb_set_create ();
135
136 hb_set_add (hb_subset_input_set (input, HB_SUBSET_SETS_GLYPH_INDEX), 83);
137 hb_set_add (hb_subset_input_set (input, HB_SUBSET_SETS_UNICODE), 85);
138
139 hb_set_clear (hb_subset_input_set (input, HB_SUBSET_SETS_LAYOUT_FEATURE_TAG));
140 hb_set_add (hb_subset_input_set (input, HB_SUBSET_SETS_LAYOUT_FEATURE_TAG), 87);
141
142 hb_set_add (set, 83);
143 g_assert (hb_set_is_equal (hb_subset_input_glyph_set (input), set));
144 hb_set_clear (set);
145
146 hb_set_add (set, 85);
147 g_assert (hb_set_is_equal (hb_subset_input_unicode_set (input), set));
148 hb_set_clear (set);
149
150 hb_set_add (set, 87);
151 g_assert (hb_set_is_equal (hb_subset_input_set (input, HB_SUBSET_SETS_LAYOUT_FEATURE_TAG), set));
152 hb_set_clear (set);
153
154 hb_set_destroy (set);
155 hb_subset_input_destroy (input);
156 }
157
158 static void
159 test_subset_plan (void)
160 {
161 hb_face_t *face_abc = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
162 hb_face_t *face_ac = hb_test_open_font_file ("fonts/Roboto-Regular.ac.ttf");
163
164 hb_set_t *codepoints = hb_set_create();
165 hb_set_add (codepoints, 97);
166 hb_set_add (codepoints, 99);
167 hb_subset_input_t* input = hb_subset_test_create_input (codepoints);
168 hb_set_destroy (codepoints);
169
170 hb_subset_plan_t* plan = hb_subset_plan_create_or_fail (face_abc, input);
171 g_assert (plan);
172
173 const hb_map_t* mapping = hb_subset_plan_old_to_new_glyph_mapping (plan);
174 g_assert (hb_map_get (mapping, 1) == 1);
175 g_assert (hb_map_get (mapping, 3) == 2);
176
177 mapping = hb_subset_plan_new_to_old_glyph_mapping (plan);
178 g_assert (hb_map_get (mapping, 1) == 1);
179 g_assert (hb_map_get (mapping, 2) == 3);
180
181 mapping = hb_subset_plan_unicode_to_old_glyph_mapping (plan);
182 g_assert (hb_map_get (mapping, 0x63) == 3);
183
184 hb_face_t* face_abc_subset = hb_subset_plan_execute_or_fail (plan);
185
186 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('l','o','c', 'a'));
187 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('g','l','y','f'));
188
189 hb_subset_input_destroy (input);
190 hb_subset_plan_destroy (plan);
191 hb_face_destroy (face_abc_subset);
192 hb_face_destroy (face_abc);
193 hb_face_destroy (face_ac);
194 }
195
196 static hb_blob_t*
197 _ref_table (hb_face_t *face, hb_tag_t tag, void *user_data)
198 {
199 return hb_face_reference_table ((hb_face_t*) user_data, tag);
200 }
201
202 static void
203 test_subset_create_for_tables_face (void)
204 {
205 hb_face_t *face_abc = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
206 hb_face_t *face_ac = hb_test_open_font_file ("fonts/Roboto-Regular.ac.ttf");
207 hb_face_t *face_create_for_tables = hb_face_create_for_tables (
208 _ref_table,
209 face_abc,
210 NULL);
211
212 hb_set_t *codepoints = hb_set_create();
213 hb_set_add (codepoints, 97);
214 hb_set_add (codepoints, 99);
215
216 hb_subset_input_t* input = hb_subset_test_create_input (codepoints);
217 hb_set_destroy (codepoints);
218
219 hb_face_t* face_abc_subset = hb_subset_or_fail (face_create_for_tables, input);
220
221 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('l','o','c', 'a'));
222 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('g','l','y','f'));
223 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('g','a','s','p'));
224
225 hb_subset_input_destroy (input);
226 hb_face_destroy (face_abc_subset);
227 hb_face_destroy (face_create_for_tables);
228 hb_face_destroy (face_abc);
229 hb_face_destroy (face_ac);
230 }
231
232 int
233 main (int argc, char **argv)
234 {
235 hb_test_init (&argc, &argv);
236
237 hb_test_add (test_subset_32_tables);
238 hb_test_add (test_subset_no_inf_loop);
239 hb_test_add (test_subset_crash);
240 hb_test_add (test_subset_set_flags);
241 hb_test_add (test_subset_sets);
242 hb_test_add (test_subset_plan);
243 hb_test_add (test_subset_create_for_tables_face);
244
245 return hb_test_run();
246 }