6 #include <condition_variable>
13 #include "hb-subset.h"
21 #define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/"
25 const char *font_path;
26 const unsigned max_subset_size;
29 {SUBSET_FONT_BASE_PATH "Roboto-Regular.ttf", 4000},
30 {SUBSET_FONT_BASE_PATH "Amiri-Regular.ttf", 4000},
31 {SUBSET_FONT_BASE_PATH "NotoNastaliqUrdu-Regular.ttf", 1000},
32 {SUBSET_FONT_BASE_PATH "NotoSansDevanagari-Regular.ttf", 1000},
33 {SUBSET_FONT_BASE_PATH "Mplus1p-Regular.ttf", 10000},
34 {SUBSET_FONT_BASE_PATH "SourceHanSans-Regular_subset.otf", 10000},
35 {SUBSET_FONT_BASE_PATH "SourceSansPro-Regular.otf", 2000},
39 static test_input_t *tests = default_tests;
40 static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
43 // https://en.cppreference.com/w/cpp/thread/condition_variable/wait
44 static std::condition_variable cv;
45 static std::mutex cv_m;
46 static bool ready = false;
48 static unsigned num_repetitions = 1;
49 static unsigned num_threads = 3;
51 static void AddCodepoints(const hb_set_t* codepoints_in_font,
53 hb_subset_input_t* input)
55 auto *unicodes = hb_subset_input_unicode_set (input);
56 hb_codepoint_t cp = HB_SET_VALUE_INVALID;
57 for (unsigned i = 0; i < subset_size; i++) {
58 if (!hb_set_next (codepoints_in_font, &cp)) return;
59 hb_set_add (unicodes, cp);
63 static void AddGlyphs(unsigned num_glyphs_in_font,
65 hb_subset_input_t* input)
67 auto *glyphs = hb_subset_input_glyph_set (input);
68 for (unsigned i = 0; i < subset_size && i < num_glyphs_in_font; i++) {
69 hb_set_add (glyphs, i);
73 static void subset (operation_t operation,
74 const test_input_t &test_input,
77 // Wait till all threads are ready.
79 std::unique_lock<std::mutex> lk (cv_m);
80 cv.wait(lk, [] {return ready;});
83 unsigned subset_size = test_input.max_subset_size;
85 hb_subset_input_t* input = hb_subset_input_create_or_fail ();
90 case subset_codepoints:
92 hb_set_t* all_codepoints = hb_set_create ();
93 hb_face_collect_unicodes (face, all_codepoints);
94 AddCodepoints(all_codepoints, subset_size, input);
95 hb_set_destroy (all_codepoints);
101 unsigned num_glyphs = hb_face_get_glyph_count (face);
102 AddGlyphs(num_glyphs, subset_size, input);
107 for (unsigned i = 0; i < num_repetitions; i++)
109 hb_face_t* subset = hb_subset_or_fail (face, input);
111 hb_face_destroy (subset);
114 hb_subset_input_destroy (input);
117 static void test_operation (operation_t operation,
118 const char *operation_name,
119 const test_input_t &test_input)
121 char name[1024] = "subset";
124 p = strrchr (test_input.font_path, '/');
125 strcat (name, p ? p + 1 : test_input.font_path);
127 strcat (name, operation_name);
129 printf ("Testing %s\n", name);
133 hb_blob_t *blob = hb_blob_create_from_file_or_fail (test_input.font_path);
135 face = hb_face_create (blob, 0);
136 hb_blob_destroy (blob);
139 std::vector<std::thread> threads;
140 for (unsigned i = 0; i < num_threads; i++)
141 threads.push_back (std::thread (subset, operation, test_input, face));
144 std::unique_lock<std::mutex> lk (cv_m);
149 for (unsigned i = 0; i < num_threads; i++)
152 hb_face_destroy (face);
155 int main(int argc, char** argv)
158 num_threads = atoi (argv[1]);
160 num_repetitions = atoi (argv[2]);
164 num_tests = argc - 3;
165 tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
166 for (unsigned i = 0; i < num_tests; i++)
168 tests[i].font_path = argv[3 + i];
172 printf ("Num threads %u; num repetitions %u\n", num_threads, num_repetitions);
173 for (unsigned i = 0; i < num_tests; i++)
175 auto& test_input = tests[i];
176 test_operation (subset_codepoints, "codepoints", test_input);
177 test_operation (subset_glyphs, "glyphs", test_input);
180 if (tests != default_tests)