• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <cassert>
2 #include <cstring>
3 #include <thread>
4 #include <condition_variable>
5 #include <vector>
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include "hb-subset.h"
12 
13 enum operation_t
14 {
15   subset_codepoints,
16   subset_glyphs
17 };
18 
19 #define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/"
20 
21 struct test_input_t
22 {
23   const char *font_path;
24   const unsigned max_subset_size;
25 } default_tests[] =
26 {
27   {SUBSET_FONT_BASE_PATH "Roboto-Regular.ttf", 4000},
28   {SUBSET_FONT_BASE_PATH "Amiri-Regular.ttf", 4000},
29   {SUBSET_FONT_BASE_PATH "NotoNastaliqUrdu-Regular.ttf", 1000},
30   {SUBSET_FONT_BASE_PATH "NotoSansDevanagari-Regular.ttf", 1000},
31   {SUBSET_FONT_BASE_PATH "Mplus1p-Regular.ttf", 10000},
32   {SUBSET_FONT_BASE_PATH "SourceHanSans-Regular_subset.otf", 10000},
33   {SUBSET_FONT_BASE_PATH "SourceSansPro-Regular.otf", 2000},
34 };
35 
36 
37 static test_input_t *tests = default_tests;
38 static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
39 
40 
41 // https://en.cppreference.com/w/cpp/thread/condition_variable/wait
42 static std::condition_variable cv;
43 static std::mutex cv_m;
44 static bool ready = false;
45 
46 static unsigned num_repetitions = 1;
47 static unsigned num_threads = 3;
48 
AddCodepoints(const hb_set_t * codepoints_in_font,unsigned subset_size,hb_subset_input_t * input)49 static void AddCodepoints(const hb_set_t* codepoints_in_font,
50                           unsigned subset_size,
51                           hb_subset_input_t* input)
52 {
53   auto *unicodes = hb_subset_input_unicode_set (input);
54   hb_codepoint_t cp = HB_SET_VALUE_INVALID;
55   for (unsigned i = 0; i < subset_size; i++) {
56     if (!hb_set_next (codepoints_in_font, &cp)) return;
57     hb_set_add (unicodes, cp);
58   }
59 }
60 
AddGlyphs(unsigned num_glyphs_in_font,unsigned subset_size,hb_subset_input_t * input)61 static void AddGlyphs(unsigned num_glyphs_in_font,
62                unsigned subset_size,
63                hb_subset_input_t* input)
64 {
65   auto *glyphs = hb_subset_input_glyph_set (input);
66   for (unsigned i = 0; i < subset_size && i < num_glyphs_in_font; i++) {
67     hb_set_add (glyphs, i);
68   }
69 }
70 
subset(operation_t operation,const test_input_t & test_input,hb_face_t * face)71 static void subset (operation_t operation,
72                     const test_input_t &test_input,
73                     hb_face_t *face)
74 {
75   // Wait till all threads are ready.
76   {
77     std::unique_lock<std::mutex> lk (cv_m);
78     cv.wait(lk, [] {return ready;});
79   }
80 
81   unsigned subset_size = test_input.max_subset_size;
82 
83   hb_subset_input_t* input = hb_subset_input_create_or_fail ();
84   assert (input);
85 
86   switch (operation)
87   {
88     case subset_codepoints:
89     {
90       hb_set_t* all_codepoints = hb_set_create ();
91       hb_face_collect_unicodes (face, all_codepoints);
92       AddCodepoints(all_codepoints, subset_size, input);
93       hb_set_destroy (all_codepoints);
94     }
95     break;
96 
97     case subset_glyphs:
98     {
99       unsigned num_glyphs = hb_face_get_glyph_count (face);
100       AddGlyphs(num_glyphs, subset_size, input);
101     }
102     break;
103   }
104 
105   for (unsigned i = 0; i < num_repetitions; i++)
106   {
107     hb_face_t* subset = hb_subset_or_fail (face, input);
108     assert (subset);
109     hb_face_destroy (subset);
110   }
111 
112   hb_subset_input_destroy (input);
113 }
114 
test_operation(operation_t operation,const char * operation_name,const test_input_t & test_input)115 static void test_operation (operation_t operation,
116                             const char *operation_name,
117                             const test_input_t &test_input)
118 {
119   char name[1024] = "subset";
120   const char *p;
121   strcat (name, "/");
122   p = strrchr (test_input.font_path, '/');
123   strcat (name, p ? p + 1 : test_input.font_path);
124   strcat (name, "/");
125   strcat (name, operation_name);
126 
127   printf ("Testing %s\n", name);
128 
129   hb_face_t *face;
130   {
131     hb_blob_t *blob = hb_blob_create_from_file_or_fail (test_input.font_path);
132     assert (blob);
133     face = hb_face_create (blob, 0);
134     hb_blob_destroy (blob);
135   }
136 
137   std::vector<std::thread> threads;
138   for (unsigned i = 0; i < num_threads; i++)
139     threads.push_back (std::thread (subset, operation, test_input, face));
140 
141   {
142     std::unique_lock<std::mutex> lk (cv_m);
143     ready = true;
144   }
145   cv.notify_all();
146 
147   for (unsigned i = 0; i < num_threads; i++)
148     threads[i].join ();
149 
150   hb_face_destroy (face);
151 }
152 
main(int argc,char ** argv)153 int main(int argc, char** argv)
154 {
155   if (argc > 1)
156     num_threads = atoi (argv[1]);
157   if (argc > 2)
158     num_repetitions = atoi (argv[2]);
159 
160   if (argc > 4)
161   {
162     num_tests = argc - 3;
163     tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
164     for (unsigned i = 0; i < num_tests; i++)
165     {
166       tests[i].font_path = argv[3 + i];
167     }
168   }
169 
170   printf ("Num threads %u; num repetitions %u\n", num_threads, num_repetitions);
171   for (unsigned i = 0; i < num_tests; i++)
172   {
173     auto& test_input = tests[i];
174     test_operation (subset_codepoints, "codepoints", test_input);
175     test_operation (subset_glyphs, "glyphs", test_input);
176   }
177 
178   if (tests != default_tests)
179     free (tests);
180 }
181