1 #include "benchmark/benchmark.h"
2 #include <cassert>
3 #include <cstring>
4
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
8
9 #include "hb-subset.h"
10
11
12 enum operation_t
13 {
14 subset_glyphs,
15 subset_unicodes,
16 instance,
17 };
18
19 struct axis_location_t
20 {
21 hb_tag_t axis_tag;
22 float axis_value;
23 };
24
25 static const axis_location_t
26 _roboto_flex_instance_opts[] =
27 {
28 {HB_TAG ('w', 'g', 'h', 't'), 600.f},
29 {HB_TAG ('w', 'd', 't', 'h'), 75.f},
30 {HB_TAG ('o', 'p', 's', 'z'), 90.f},
31 {HB_TAG ('G', 'R', 'A', 'D'), -100.f},
32 {HB_TAG ('s', 'l', 'n', 't'), -3.f},
33 {HB_TAG ('X', 'T', 'R', 'A'), 500.f},
34 {HB_TAG ('X', 'O', 'P', 'Q'), 150.f},
35 {HB_TAG ('Y', 'O', 'P', 'Q'), 100.f},
36 {HB_TAG ('Y', 'T', 'L', 'C'), 480.f},
37 {HB_TAG ('Y', 'T', 'U', 'C'), 600.f},
38 {HB_TAG ('Y', 'T', 'A', 'S'), 800.f},
39 {HB_TAG ('Y', 'T', 'D', 'E'), -50.f},
40 {HB_TAG ('Y', 'T', 'F', 'I'), 600.f},
41 };
42
43 static const axis_location_t
44 _mplus_instance_opts[] =
45 {
46 {HB_TAG ('w', 'g', 'h', 't'), 800.f},
47 };
48
49 template <typename Type, unsigned int n>
ARRAY_LEN(const Type (&)[n])50 static inline unsigned int ARRAY_LEN (const Type (&)[n]) { return n; }
51
52 #define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/"
53
54 struct test_input_t
55 {
56 const char *font_path;
57 unsigned max_subset_size;
58 const axis_location_t *instance_opts;
59 unsigned num_instance_opts;
60 } default_tests[] =
61 {
62 {SUBSET_FONT_BASE_PATH "Roboto-Regular.ttf", 1000, nullptr, 0},
63 {SUBSET_FONT_BASE_PATH "Amiri-Regular.ttf", 4096, nullptr, 0},
64 {SUBSET_FONT_BASE_PATH "NotoNastaliqUrdu-Regular.ttf", 1400, nullptr, 0},
65 {SUBSET_FONT_BASE_PATH "NotoSansDevanagari-Regular.ttf", 1000, nullptr, 0},
66 {SUBSET_FONT_BASE_PATH "Mplus1p-Regular.ttf", 10000, nullptr, 0},
67 {SUBSET_FONT_BASE_PATH "SourceHanSans-Regular_subset.otf", 10000, nullptr, 0},
68 {SUBSET_FONT_BASE_PATH "SourceSansPro-Regular.otf", 2000, nullptr, 0},
69 {SUBSET_FONT_BASE_PATH "AdobeVFPrototype.otf", 300, nullptr, 0},
70 {SUBSET_FONT_BASE_PATH "MPLUS1-Variable.ttf", 6000, _mplus_instance_opts, ARRAY_LEN (_mplus_instance_opts)},
71 {SUBSET_FONT_BASE_PATH "RobotoFlex-Variable.ttf", 900, _roboto_flex_instance_opts, ARRAY_LEN (_roboto_flex_instance_opts)},
72 #if 0
73 {"perf/fonts/NotoSansCJKsc-VF.ttf", 100000},
74 #endif
75 };
76
77 static test_input_t *tests = default_tests;
78 static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
79
80
AddCodepoints(const hb_set_t * codepoints_in_font,unsigned subset_size,hb_subset_input_t * input)81 void AddCodepoints(const hb_set_t* codepoints_in_font,
82 unsigned subset_size,
83 hb_subset_input_t* input)
84 {
85 auto *unicodes = hb_subset_input_unicode_set (input);
86 hb_codepoint_t cp = HB_SET_VALUE_INVALID;
87 for (unsigned i = 0; i < subset_size; i++) {
88 // TODO(garretrieger): pick randomly.
89 if (!hb_set_next (codepoints_in_font, &cp)) return;
90 hb_set_add (unicodes, cp);
91 }
92 }
93
AddGlyphs(unsigned num_glyphs_in_font,unsigned subset_size,hb_subset_input_t * input)94 void AddGlyphs(unsigned num_glyphs_in_font,
95 unsigned subset_size,
96 hb_subset_input_t* input)
97 {
98 auto *glyphs = hb_subset_input_glyph_set (input);
99 for (unsigned i = 0; i < subset_size && i < num_glyphs_in_font; i++) {
100 if (i + 1 == subset_size &&
101 hb_subset_input_get_flags (input) & HB_SUBSET_FLAGS_RETAIN_GIDS)
102 {
103 hb_set_add (glyphs, num_glyphs_in_font - 1);
104 continue;
105 }
106 hb_set_add (glyphs, i);
107 }
108 }
109
110 // Preprocess face and populate the subset accelerator on it to speed up
111 // the subsetting operations.
preprocess_face(hb_face_t * face)112 static hb_face_t* preprocess_face(hb_face_t* face)
113 {
114 hb_face_t* new_face = hb_subset_preprocess(face);
115 hb_face_destroy(face);
116 return new_face;
117 }
118
119 static hb_face_t *cached_face;
120
121 static void
free_cached_face(void)122 free_cached_face (void)
123 {
124 hb_face_destroy (cached_face);
125 cached_face = nullptr;
126 }
127
128
129 /* benchmark for subsetting a font */
BM_subset(benchmark::State & state,operation_t operation,const test_input_t & test_input,bool retain_gids)130 static void BM_subset (benchmark::State &state,
131 operation_t operation,
132 const test_input_t &test_input,
133 bool retain_gids)
134 {
135 unsigned subset_size = state.range(0);
136
137 hb_face_t *face = nullptr;
138
139 static const char *cached_font_path;
140
141 if (!cached_font_path || strcmp (cached_font_path, test_input.font_path))
142 {
143 hb_blob_t *blob = hb_blob_create_from_file_or_fail (test_input.font_path);
144 assert (blob);
145 face = hb_face_create (blob, 0);
146 hb_blob_destroy (blob);
147
148 face = preprocess_face (face);
149
150 if (cached_face)
151 hb_face_destroy (cached_face);
152
153 cached_face = hb_face_reference (face);
154 cached_font_path = test_input.font_path;
155 }
156 else
157 face = hb_face_reference (cached_face);
158
159 hb_subset_input_t* input = hb_subset_input_create_or_fail ();
160 assert (input);
161
162 if (retain_gids)
163 hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_RETAIN_GIDS);
164
165 switch (operation)
166 {
167 case subset_unicodes:
168 {
169 hb_set_t* all_codepoints = hb_set_create ();
170 hb_face_collect_unicodes (face, all_codepoints);
171 AddCodepoints(all_codepoints, subset_size, input);
172 hb_set_destroy (all_codepoints);
173 }
174 break;
175
176 case subset_glyphs:
177 {
178 unsigned num_glyphs = hb_face_get_glyph_count (face);
179 AddGlyphs(num_glyphs, subset_size, input);
180 }
181 break;
182
183 case instance:
184 {
185 hb_set_t* all_codepoints = hb_set_create ();
186 hb_face_collect_unicodes (face, all_codepoints);
187 AddCodepoints(all_codepoints, subset_size, input);
188 hb_set_destroy (all_codepoints);
189
190 for (unsigned i = 0; i < test_input.num_instance_opts; i++)
191 hb_subset_input_pin_axis_location (input, face,
192 test_input.instance_opts[i].axis_tag,
193 test_input.instance_opts[i].axis_value);
194 }
195 break;
196 }
197
198 for (auto _ : state)
199 {
200 hb_face_t* subset = hb_subset_or_fail (face, input);
201 assert (subset);
202 hb_face_destroy (subset);
203 }
204
205 hb_subset_input_destroy (input);
206 hb_face_destroy (face);
207 }
208
test_subset(operation_t op,const char * op_name,bool retain_gids,benchmark::TimeUnit time_unit,const test_input_t & test_input)209 static void test_subset (operation_t op,
210 const char *op_name,
211 bool retain_gids,
212 benchmark::TimeUnit time_unit,
213 const test_input_t &test_input)
214 {
215 if (op == instance && test_input.instance_opts == nullptr)
216 return;
217
218 char name[1024] = "BM_subset/";
219 strcat (name, op_name);
220 strcat (name, "/");
221 const char *p = strrchr (test_input.font_path, '/');
222 strcat (name, p ? p + 1 : test_input.font_path);
223 if (retain_gids)
224 strcat (name, "/retaingids");
225
226 benchmark::RegisterBenchmark (name, BM_subset, op, test_input, retain_gids)
227 ->Range(10, test_input.max_subset_size)
228 ->Unit(time_unit);
229 }
230
test_operation(operation_t op,const char * op_name,const test_input_t * tests,unsigned num_tests,benchmark::TimeUnit time_unit)231 static void test_operation (operation_t op,
232 const char *op_name,
233 const test_input_t *tests,
234 unsigned num_tests,
235 benchmark::TimeUnit time_unit)
236 {
237 for (unsigned i = 0; i < num_tests; i++)
238 {
239 auto& test_input = tests[i];
240 test_subset (op, op_name, true, time_unit, test_input);
241 test_subset (op, op_name, false, time_unit, test_input);
242 }
243 }
244
main(int argc,char ** argv)245 int main(int argc, char** argv)
246 {
247 benchmark::Initialize(&argc, argv);
248
249 #ifndef HB_NO_ATEXIT
250 atexit (free_cached_face);
251 #endif
252
253 if (argc > 1)
254 {
255 num_tests = (argc - 1) / 2;
256 tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
257 for (unsigned i = 0; i < num_tests; i++)
258 {
259 tests[i].font_path = argv[1 + i * 2];
260 tests[i].max_subset_size = atoi (argv[2 + i * 2]);
261 }
262 }
263
264 #define TEST_OPERATION(op, time_unit) test_operation (op, #op, tests, num_tests, time_unit)
265
266 TEST_OPERATION (subset_glyphs, benchmark::kMicrosecond);
267 TEST_OPERATION (subset_unicodes, benchmark::kMicrosecond);
268 TEST_OPERATION (instance, benchmark::kMicrosecond);
269
270 #undef TEST_OPERATION
271
272 benchmark::RunSpecifiedBenchmarks();
273 benchmark::Shutdown();
274
275 if (tests != default_tests)
276 free (tests);
277 }
278