1 #include "benchmark/benchmark.h"
2
3 #include "hb.h"
4 #include "hb-ot.h"
5 #include "hb-ft.h"
6 #include FT_OUTLINE_H
7
8 #ifdef HAVE_TTFPARSER
9 #include "ttfparser.h"
10 #endif
11
12 #define HB_UNUSED __attribute__((unused))
13
14 static void
_hb_move_to(hb_position_t to_x HB_UNUSED,hb_position_t to_y HB_UNUSED,void * user_data HB_UNUSED)15 _hb_move_to (hb_position_t to_x HB_UNUSED, hb_position_t to_y HB_UNUSED, void *user_data HB_UNUSED) {}
16
17 static void
_hb_line_to(hb_position_t to_x HB_UNUSED,hb_position_t to_y HB_UNUSED,void * user_data HB_UNUSED)18 _hb_line_to (hb_position_t to_x HB_UNUSED, hb_position_t to_y HB_UNUSED, void *user_data HB_UNUSED) {}
19
20 static void
_hb_quadratic_to(hb_position_t control_x HB_UNUSED,hb_position_t control_y HB_UNUSED,hb_position_t to_x HB_UNUSED,hb_position_t to_y HB_UNUSED,void * user_data HB_UNUSED)21 _hb_quadratic_to (hb_position_t control_x HB_UNUSED, hb_position_t control_y HB_UNUSED,
22 hb_position_t to_x HB_UNUSED, hb_position_t to_y HB_UNUSED,
23 void *user_data HB_UNUSED) {}
24
25 static void
_hb_cubic_to(hb_position_t control1_x HB_UNUSED,hb_position_t control1_y HB_UNUSED,hb_position_t control2_x HB_UNUSED,hb_position_t control2_y HB_UNUSED,hb_position_t to_x HB_UNUSED,hb_position_t to_y HB_UNUSED,void * user_data HB_UNUSED)26 _hb_cubic_to (hb_position_t control1_x HB_UNUSED, hb_position_t control1_y HB_UNUSED,
27 hb_position_t control2_x HB_UNUSED, hb_position_t control2_y HB_UNUSED,
28 hb_position_t to_x HB_UNUSED, hb_position_t to_y HB_UNUSED,
29 void *user_data HB_UNUSED) {}
30
31 static void
_hb_close_path(void * user_data HB_UNUSED)32 _hb_close_path (void *user_data HB_UNUSED) {}
33
34 static void
_ft_move_to(const FT_Vector * to HB_UNUSED,void * user HB_UNUSED)35 _ft_move_to (const FT_Vector* to HB_UNUSED, void* user HB_UNUSED) {}
36
37 static void
_ft_line_to(const FT_Vector * to HB_UNUSED,void * user HB_UNUSED)38 _ft_line_to (const FT_Vector* to HB_UNUSED, void* user HB_UNUSED) {}
39
40 static void
_ft_conic_to(const FT_Vector * control HB_UNUSED,const FT_Vector * to HB_UNUSED,void * user HB_UNUSED)41 _ft_conic_to (const FT_Vector* control HB_UNUSED, const FT_Vector* to HB_UNUSED,
42 void* user HB_UNUSED) {}
43
44 static void
_ft_cubic_to(const FT_Vector * control1 HB_UNUSED,const FT_Vector * control2 HB_UNUSED,const FT_Vector * to HB_UNUSED,void * user HB_UNUSED)45 _ft_cubic_to (const FT_Vector* control1 HB_UNUSED, const FT_Vector* control2 HB_UNUSED,
46 const FT_Vector* to HB_UNUSED, void* user HB_UNUSED) {}
47
48 #ifdef HAVE_TTFPARSER
_tp_move_to(float x HB_UNUSED,float y HB_UNUSED,void * data HB_UNUSED)49 static void _tp_move_to (float x HB_UNUSED, float y HB_UNUSED, void *data HB_UNUSED) {}
_tp_line_to(float x,float y,void * data)50 static void _tp_line_to (float x, float y, void *data) {}
_tp_quad_to(float x1,float y1,float x,float y,void * data)51 static void _tp_quad_to (float x1, float y1, float x, float y, void *data) {}
_tp_curve_to(float x1,float y1,float x2,float y2,float x,float y,void * data)52 static void _tp_curve_to (float x1, float y1, float x2, float y2, float x, float y, void *data) {}
_tp_close_path(void * data)53 static void _tp_close_path (void *data) {}
54 #endif
55
draw(benchmark::State & state,const char * font_path,bool is_var,backend_t backend)56 static void draw (benchmark::State &state, const char *font_path, bool is_var, backend_t backend)
57 {
58 hb_font_t *font;
59 unsigned num_glyphs;
60 {
61 hb_blob_t *blob = hb_blob_create_from_file (font_path);
62 assert (hb_blob_get_length (blob));
63 hb_face_t *face = hb_face_create (blob, 0);
64 hb_blob_destroy (blob);
65 num_glyphs = hb_face_get_glyph_count (face);
66 font = hb_font_create (face);
67 hb_face_destroy (face);
68 }
69
70 if (backend == HARFBUZZ)
71 {
72 if (is_var)
73 {
74 hb_variation_t wght = {HB_TAG ('w','g','h','t'), 500};
75 hb_font_set_variations (font, &wght, 1);
76 }
77 hb_draw_funcs_t *draw_funcs = hb_draw_funcs_create ();
78 hb_draw_funcs_set_move_to_func (draw_funcs, _hb_move_to);
79 hb_draw_funcs_set_line_to_func (draw_funcs, _hb_line_to);
80 hb_draw_funcs_set_quadratic_to_func (draw_funcs, _hb_quadratic_to);
81 hb_draw_funcs_set_cubic_to_func (draw_funcs, _hb_cubic_to);
82 hb_draw_funcs_set_close_path_func (draw_funcs, _hb_close_path);
83
84 for (auto _ : state)
85 for (unsigned gid = 0; gid < num_glyphs; ++gid)
86 hb_font_draw_glyph (font, gid, draw_funcs, nullptr);
87
88 hb_draw_funcs_destroy (draw_funcs);
89 }
90 else if (backend == FREETYPE)
91 {
92 if (is_var)
93 {
94 hb_variation_t wght = {HB_TAG ('w','g','h','t'), 500};
95 hb_font_set_variations (font, &wght, 1);
96 }
97 hb_ft_font_set_funcs (font);
98 FT_Face ft_face = hb_ft_font_get_face (font);
99 hb_ft_font_set_load_flags (font, FT_LOAD_NO_HINTING | FT_LOAD_NO_SCALE);
100
101 FT_Outline_Funcs draw_funcs;
102 draw_funcs.move_to = (FT_Outline_MoveToFunc) _ft_move_to;
103 draw_funcs.line_to = (FT_Outline_LineToFunc) _ft_line_to;
104 draw_funcs.conic_to = (FT_Outline_ConicToFunc) _ft_conic_to;
105 draw_funcs.cubic_to = (FT_Outline_CubicToFunc) _ft_cubic_to;
106 draw_funcs.shift = 0;
107 draw_funcs.delta = 0;
108
109 for (auto _ : state)
110 for (unsigned gid = 0; gid < num_glyphs; ++gid)
111 {
112 FT_Load_Glyph (ft_face, gid, FT_LOAD_NO_HINTING | FT_LOAD_NO_SCALE);
113 FT_Outline_Decompose (&ft_face->glyph->outline, &draw_funcs, nullptr);
114 }
115 }
116 else if (backend == TTF_PARSER)
117 {
118 #ifdef HAVE_TTFPARSER
119 ttfp_face *tp_font = (ttfp_face *) malloc (ttfp_face_size_of ());
120 hb_blob_t *blob = hb_face_reference_blob (hb_font_get_face (font));
121 assert (ttfp_face_init (hb_blob_get_data (blob, nullptr), hb_blob_get_length (blob), 0, tp_font));
122 if (is_var) ttfp_set_variation (tp_font, TTFP_TAG('w','g','h','t'), 500);
123
124 ttfp_outline_builder builder;
125 builder.move_to = _tp_move_to;
126 builder.line_to = _tp_line_to;
127 builder.quad_to = _tp_quad_to;
128 builder.curve_to = _tp_curve_to;
129 builder.close_path = _tp_close_path;
130
131 ttfp_rect bbox;
132 for (auto _ : state)
133 for (unsigned gid = 0; gid < num_glyphs; ++gid)
134 ttfp_outline_glyph (tp_font, builder, &builder, gid, &bbox);
135
136 hb_blob_destroy (blob);
137 free (tp_font);
138 #endif
139 }
140 else abort ();
141
142 hb_font_destroy (font);
143 }
144
145 #define FONT_BASE_PATH "test/subset/data/fonts/"
146
147 BENCHMARK_CAPTURE (draw, cff - ot - SourceSansPro, FONT_BASE_PATH "SourceSansPro-Regular.otf", false, HARFBUZZ);
148 BENCHMARK_CAPTURE (draw, cff - ft - SourceSansPro, FONT_BASE_PATH "SourceSansPro-Regular.otf", false, FREETYPE);
149 BENCHMARK_CAPTURE (draw, cff - tp - SourceSansPro, FONT_BASE_PATH "SourceSansPro-Regular.otf", false, TTF_PARSER);
150
151 BENCHMARK_CAPTURE (draw, cff2 - ot - AdobeVFPrototype, FONT_BASE_PATH "AdobeVFPrototype.otf", false, HARFBUZZ);
152 BENCHMARK_CAPTURE (draw, cff2 - ft - AdobeVFPrototype, FONT_BASE_PATH "AdobeVFPrototype.otf", false, FREETYPE);
153 BENCHMARK_CAPTURE (draw, cff2 - tp - AdobeVFPrototype, FONT_BASE_PATH "AdobeVFPrototype.otf", false, TTF_PARSER);
154
155 BENCHMARK_CAPTURE (draw, cff2/vf - ot - AdobeVFPrototype, FONT_BASE_PATH "AdobeVFPrototype.otf", true, HARFBUZZ);
156 BENCHMARK_CAPTURE (draw, cff2/vf - ft - AdobeVFPrototype, FONT_BASE_PATH "AdobeVFPrototype.otf", true, FREETYPE);
157 BENCHMARK_CAPTURE (draw, cff2/vf - tp - AdobeVFPrototype, FONT_BASE_PATH "AdobeVFPrototype.otf", true, TTF_PARSER);
158
159 BENCHMARK_CAPTURE (draw, glyf - ot - SourceSerifVariable, FONT_BASE_PATH "SourceSerifVariable-Roman.ttf", false, HARFBUZZ);
160 BENCHMARK_CAPTURE (draw, glyf - ft - SourceSerifVariable, FONT_BASE_PATH "SourceSerifVariable-Roman.ttf", false, FREETYPE);
161 BENCHMARK_CAPTURE (draw, glyf - tp - SourceSerifVariable, FONT_BASE_PATH "SourceSerifVariable-Roman.ttf", false, TTF_PARSER);
162
163 BENCHMARK_CAPTURE (draw, glyf/vf - ot - SourceSerifVariable, FONT_BASE_PATH "SourceSerifVariable-Roman.ttf", true, HARFBUZZ);
164 BENCHMARK_CAPTURE (draw, glyf/vf - ft - SourceSerifVariable, FONT_BASE_PATH "SourceSerifVariable-Roman.ttf", true, FREETYPE);
165 BENCHMARK_CAPTURE (draw, glyf/vf - tp - SourceSerifVariable, FONT_BASE_PATH "SourceSerifVariable-Roman.ttf", true, TTF_PARSER);
166
167 BENCHMARK_CAPTURE (draw, glyf - ot - Comfortaa, FONT_BASE_PATH "Comfortaa-Regular-new.ttf", false, HARFBUZZ);
168 BENCHMARK_CAPTURE (draw, glyf - ft - Comfortaa, FONT_BASE_PATH "Comfortaa-Regular-new.ttf", false, FREETYPE);
169 BENCHMARK_CAPTURE (draw, glyf - tp - Comfortaa, FONT_BASE_PATH "Comfortaa-Regular-new.ttf", false, TTF_PARSER);
170
171 BENCHMARK_CAPTURE (draw, glyf/vf - ot - Comfortaa, FONT_BASE_PATH "Comfortaa-Regular-new.ttf", true, HARFBUZZ);
172 BENCHMARK_CAPTURE (draw, glyf/vf - ft - Comfortaa, FONT_BASE_PATH "Comfortaa-Regular-new.ttf", true, FREETYPE);
173 BENCHMARK_CAPTURE (draw, glyf/vf - tp - Comfortaa, FONT_BASE_PATH "Comfortaa-Regular-new.ttf", true, TTF_PARSER);
174
175 BENCHMARK_CAPTURE (draw, glyf - ot - Roboto, FONT_BASE_PATH "Roboto-Regular.ttf", false, HARFBUZZ);
176 BENCHMARK_CAPTURE (draw, glyf - ft - Roboto, FONT_BASE_PATH "Roboto-Regular.ttf", false, FREETYPE);
177 BENCHMARK_CAPTURE (draw, glyf - tp - Roboto, FONT_BASE_PATH "Roboto-Regular.ttf", false, TTF_PARSER);
178