2 * Copyright © 2018 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Garret Rieger
30 #include "hb-subset-test.h"
32 /* Unit tests for hb-subset-glyf.h */
34 static void check_maxp_field (uint8_t *raw_maxp, unsigned int offset, uint16_t expected_value)
36 uint16_t actual_value = (raw_maxp[offset] << 8) + raw_maxp[offset + 1];
37 g_assert_cmpuint(expected_value, ==, actual_value);
40 static void check_maxp_num_glyphs (hb_face_t *face, uint16_t expected_num_glyphs, bool hints)
42 hb_blob_t *maxp_blob = hb_face_reference_table (face, HB_TAG ('m','a','x', 'p'));
44 unsigned int maxp_len;
45 uint8_t *raw_maxp = (uint8_t *) hb_blob_get_data(maxp_blob, &maxp_len);
47 check_maxp_field (raw_maxp, 4, expected_num_glyphs); // numGlyphs
50 check_maxp_field (raw_maxp, 14, 1); // maxZones
51 check_maxp_field (raw_maxp, 16, 0); // maxTwilightPoints
52 check_maxp_field (raw_maxp, 18, 0); // maxStorage
53 check_maxp_field (raw_maxp, 20, 0); // maxFunctionDefs
54 check_maxp_field (raw_maxp, 22, 0); // maxInstructionDefs
55 check_maxp_field (raw_maxp, 24, 0); // maxStackElements
56 check_maxp_field (raw_maxp, 26, 0); // maxSizeOfInstructions
59 hb_blob_destroy (maxp_blob);
63 test_subset_glyf (void)
65 hb_face_t *face_abc = hb_subset_test_open_font ("fonts/Roboto-Regular.abc.ttf");
66 hb_face_t *face_ac = hb_subset_test_open_font ("fonts/Roboto-Regular.ac.ttf");
68 hb_set_t *codepoints = hb_set_create();
69 hb_set_add (codepoints, 97);
70 hb_set_add (codepoints, 99);
71 hb_face_t *face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
72 hb_set_destroy (codepoints);
74 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('g','l','y','f'));
75 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('l','o','c', 'a'));
76 check_maxp_num_glyphs(face_abc_subset, 3, true);
78 hb_face_destroy (face_abc_subset);
79 hb_face_destroy (face_abc);
80 hb_face_destroy (face_ac);
84 test_subset_glyf_with_components (void)
86 hb_face_t *face_components = hb_subset_test_open_font ("fonts/Roboto-Regular.components.ttf");
87 hb_face_t *face_subset = hb_subset_test_open_font ("fonts/Roboto-Regular.components.subset.ttf");
89 hb_set_t *codepoints = hb_set_create();
90 hb_set_add (codepoints, 0x1fc);
91 hb_face_t *face_generated_subset = hb_subset_test_create_subset (face_components, hb_subset_test_create_input (codepoints));
92 hb_set_destroy (codepoints);
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);
98 hb_face_destroy (face_generated_subset);
99 hb_face_destroy (face_subset);
100 hb_face_destroy (face_components);
104 test_subset_glyf_noop (void)
106 hb_face_t *face_abc = hb_subset_test_open_font("fonts/Roboto-Regular.abc.ttf");
108 hb_set_t *codepoints = hb_set_create();
109 hb_set_add (codepoints, 97);
110 hb_set_add (codepoints, 98);
111 hb_set_add (codepoints, 99);
112 hb_face_t *face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
113 hb_set_destroy (codepoints);
115 hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('g','l','y','f'));
116 hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('l','o','c', 'a'));
117 check_maxp_num_glyphs(face_abc_subset, 4, true);
119 hb_face_destroy (face_abc_subset);
120 hb_face_destroy (face_abc);
124 test_subset_glyf_strip_hints_simple (void)
126 hb_face_t *face_abc = hb_subset_test_open_font ("fonts/Roboto-Regular.abc.ttf");
127 hb_face_t *face_ac = hb_subset_test_open_font ("fonts/Roboto-Regular.ac.nohints.ttf");
129 hb_set_t *codepoints = hb_set_create();
130 hb_set_add (codepoints, 'a');
131 hb_set_add (codepoints, 'c');
132 hb_subset_input_t *input = hb_subset_test_create_input (codepoints);
133 *hb_subset_input_drop_hints(input) = true;
134 hb_face_t *face_abc_subset = hb_subset_test_create_subset (face_abc, input);
135 hb_set_destroy (codepoints);
137 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('l','o','c', 'a'));
138 hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('g','l','y','f'));
139 check_maxp_num_glyphs(face_abc_subset, 3, false);
141 hb_face_destroy (face_abc_subset);
142 hb_face_destroy (face_abc);
143 hb_face_destroy (face_ac);
147 test_subset_glyf_strip_hints_composite (void)
149 hb_face_t *face_components = hb_subset_test_open_font ("fonts/Roboto-Regular.components.ttf");
150 hb_face_t *face_subset = hb_subset_test_open_font ("fonts/Roboto-Regular.components.1fc.nohints.ttf");
152 hb_set_t *codepoints = hb_set_create();
153 hb_set_add (codepoints, 0x1fc);
154 hb_subset_input_t *input = hb_subset_test_create_input (codepoints);
155 *hb_subset_input_drop_hints(input) = true;
157 hb_face_t *face_generated_subset = hb_subset_test_create_subset (face_components, input);
158 hb_set_destroy (codepoints);
160 hb_subset_test_check (face_subset, face_generated_subset, HB_TAG ('g','l','y','f'));
161 hb_subset_test_check (face_subset, face_generated_subset, HB_TAG ('l','o','c', 'a'));
162 check_maxp_num_glyphs(face_generated_subset, 4, false);
164 hb_face_destroy (face_generated_subset);
165 hb_face_destroy (face_subset);
166 hb_face_destroy (face_components);
169 // TODO(grieger): test for long loca generation.
172 main (int argc, char **argv)
174 hb_test_init (&argc, &argv);
176 hb_test_add (test_subset_glyf_noop);
177 hb_test_add (test_subset_glyf);
178 hb_test_add (test_subset_glyf_strip_hints_simple);
179 hb_test_add (test_subset_glyf_strip_hints_composite);
180 hb_test_add (test_subset_glyf_with_components);
182 return hb_test_run();