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