2 * Copyright © 2018 Ebrahim Byagowi
3 * Copyright © 2018 Khaled Hosny
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
32 #include FT_FREETYPE_H
37 #include <cairo-svg.h>
43 svg_dump (hb_face_t *face, unsigned int face_index)
45 unsigned glyph_count = hb_face_get_glyph_count (face);
47 for (unsigned int glyph_id = 0; glyph_id < glyph_count; glyph_id++)
49 hb_blob_t *blob = hb_ot_color_glyph_reference_svg (face, glyph_id);
51 if (hb_blob_get_length (blob) == 0) continue;
54 const char *data = hb_blob_get_data (blob, &length);
56 char output_path[255];
57 sprintf (output_path, "out/svg-%u-%u.svg%s",
60 // append "z" if the content is gzipped, https://stackoverflow.com/a/6059405
61 (length > 2 && (data[0] == '\x1F') && (data[1] == '\x8B')) ? "z" : "");
63 FILE *f = fopen (output_path, "wb");
64 fwrite (data, 1, length, f);
67 hb_blob_destroy (blob);
71 /* _png API is so easy to use unlike the below code, don't get confused */
73 png_dump (hb_face_t *face, unsigned int face_index)
75 unsigned glyph_count = hb_face_get_glyph_count (face);
76 hb_font_t *font = hb_font_create (face);
78 /* scans the font for strikes */
79 unsigned int sample_glyph_id;
80 /* we don't care about different strikes for different glyphs at this point */
81 for (sample_glyph_id = 0; sample_glyph_id < glyph_count; sample_glyph_id++)
83 hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, sample_glyph_id);
84 unsigned int blob_length = hb_blob_get_length (blob);
85 hb_blob_destroy (blob);
90 unsigned int upem = hb_face_get_upem (face);
91 unsigned int blob_length = 0;
92 unsigned int strike = 0;
93 for (unsigned int ppem = 1; ppem < upem; ppem++)
95 hb_font_set_ppem (font, ppem, ppem);
96 hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, sample_glyph_id);
97 unsigned int new_blob_length = hb_blob_get_length (blob);
98 hb_blob_destroy (blob);
99 if (new_blob_length != blob_length)
101 for (unsigned int glyph_id = 0; glyph_id < glyph_count; glyph_id++)
103 hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, glyph_id);
105 if (hb_blob_get_length (blob) == 0) continue;
108 const char *data = hb_blob_get_data (blob, &length);
110 char output_path[255];
111 sprintf (output_path, "out/png-%u-%u-%u.png", glyph_id, strike, face_index);
113 FILE *f = fopen (output_path, "wb");
114 fwrite (data, 1, length, f);
117 hb_blob_destroy (blob);
121 blob_length = new_blob_length;
125 hb_font_destroy (font);
129 layered_glyph_dump (hb_face_t *face, cairo_font_face_t *cairo_face, unsigned int face_index)
131 unsigned int upem = hb_face_get_upem (face);
133 unsigned glyph_count = hb_face_get_glyph_count (face);
134 for (hb_codepoint_t gid = 0; gid < glyph_count; ++gid)
136 unsigned int num_layers = hb_ot_color_glyph_get_layers (face, gid, 0, NULL, NULL);
140 hb_ot_color_layer_t *layers = (hb_ot_color_layer_t*) malloc (num_layers * sizeof (hb_ot_color_layer_t));
142 hb_ot_color_glyph_get_layers (face, gid, 0, &num_layers, layers);
146 cairo_text_extents_t extents;
148 cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
149 cairo_t *cr = cairo_create (surface);
150 cairo_set_font_face (cr, cairo_face);
151 cairo_set_font_size (cr, upem);
153 cairo_glyph_t *glyphs = (cairo_glyph_t *) calloc (num_layers, sizeof (cairo_glyph_t));
154 for (unsigned int j = 0; j < num_layers; ++j)
155 glyphs[j].index = layers[j].glyph;
156 cairo_glyph_extents (cr, glyphs, num_layers, &extents);
158 cairo_surface_destroy (surface);
162 // Add a slight margin
163 extents.width += extents.width / 10;
164 extents.height += extents.height / 10;
165 extents.x_bearing -= extents.width / 20;
166 extents.y_bearing -= extents.height / 20;
169 unsigned int palette_count = hb_ot_color_palette_get_count (face);
170 for (unsigned int palette = 0; palette < palette_count; palette++)
172 unsigned int num_colors = hb_ot_color_palette_get_colors (face, palette, 0, NULL, NULL);
176 hb_color_t *colors = (hb_color_t*) calloc (num_colors, sizeof (hb_color_t));
177 hb_ot_color_palette_get_colors (face, palette, 0, &num_colors, colors);
180 char output_path[255];
181 sprintf (output_path, "out/colr-%u-%u-%u.svg", gid, palette, face_index);
183 cairo_surface_t *surface = cairo_svg_surface_create (output_path, extents.width, extents.height);
184 cairo_t *cr = cairo_create (surface);
185 cairo_set_font_face (cr, cairo_face);
186 cairo_set_font_size (cr, upem);
188 for (unsigned int layer = 0; layer < num_layers; ++layer)
190 hb_color_t color = 0x000000FF;
191 if (layers[layer].color_index != 0xFFFF)
192 color = colors[layers[layer].color_index];
193 cairo_set_source_rgba (cr,
194 hb_color_get_red (color) / 255.,
195 hb_color_get_green (color) / 255.,
196 hb_color_get_blue (color) / 255.,
197 hb_color_get_alpha (color) / 255.);
200 glyph.index = layers[layer].glyph;
201 glyph.x = -extents.x_bearing;
202 glyph.y = -extents.y_bearing;
203 cairo_show_glyphs (cr, &glyph, 1);
206 cairo_surface_destroy (surface);
218 dump_glyphs (cairo_font_face_t *cairo_face, unsigned int upem,
219 unsigned int num_glyphs, unsigned int face_index)
221 for (unsigned int i = 0; i < num_glyphs; ++i)
223 cairo_text_extents_t extents;
224 cairo_glyph_t glyph = {0};
229 cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
230 cairo_t *cr = cairo_create (surface);
231 cairo_set_font_face (cr, cairo_face);
232 cairo_set_font_size (cr, upem);
234 cairo_glyph_extents (cr, &glyph, 1, &extents);
235 cairo_surface_destroy (surface);
239 // Add a slight margin
240 extents.width += extents.width / 10;
241 extents.height += extents.height / 10;
242 extents.x_bearing -= extents.width / 20;
243 extents.y_bearing -= extents.height / 20;
247 char output_path[255];
248 sprintf (output_path, "out/%u-%u.svg", face_index, i);
249 cairo_surface_t *surface = cairo_svg_surface_create (output_path, extents.width, extents.height);
250 cairo_t *cr = cairo_create (surface);
251 cairo_set_font_face (cr, cairo_face);
252 cairo_set_font_size (cr, upem);
253 glyph.x = -extents.x_bearing;
254 glyph.y = -extents.y_bearing;
255 cairo_show_glyphs (cr, &glyph, 1);
256 cairo_surface_destroy (surface);
263 main (int argc, char **argv)
266 fprintf (stderr, "usage: %s font-file.ttf\n"
267 "run it like `rm -rf out && mkdir out && %s font-file.ttf`\n",
273 FILE *font_name_file = fopen ("out/.dumped_font_name", "r");
274 if (font_name_file != NULL)
276 fprintf (stderr, "Purge or move ./out folder in order to run a new dump\n");
280 font_name_file = fopen ("out/.dumped_font_name", "w");
281 if (font_name_file == NULL)
283 fprintf (stderr, "./out is not accessible as a folder, create it please\n");
286 fwrite (argv[1], 1, strlen (argv[1]), font_name_file);
287 fclose (font_name_file);
289 hb_blob_t *blob = hb_blob_create_from_file (argv[1]);
290 unsigned int num_faces = hb_face_count (blob);
293 fprintf (stderr, "error: The file (%s) was corrupted, empty or not found", argv[1]);
297 for (unsigned int face_index = 0; face_index < hb_face_count (blob); face_index++)
299 hb_face_t *face = hb_face_create (blob, face_index);
300 hb_font_t *font = hb_font_create (face);
302 if (hb_ot_color_has_png (face)) printf ("Dumping png (cbdt/sbix)...\n");
303 png_dump (face, face_index);
305 if (hb_ot_color_has_svg (face)) printf ("Dumping svg...\n");
306 svg_dump (face, face_index);
308 cairo_font_face_t *cairo_face;
311 FT_Init_FreeType (&library);
313 FT_New_Face (library, argv[1], 0, &ft_face);
314 cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
316 if (hb_ot_color_has_layers (face) && hb_ot_color_has_palettes (face))
317 printf ("Dumping layered color glyphs...\n");
318 layered_glyph_dump (face, cairo_face, face_index);
320 unsigned int num_glyphs = hb_face_get_glyph_count (face);
321 unsigned int upem = hb_face_get_upem (face);
323 // disabled when color font as cairo rendering of NotoColorEmoji is soooo slow
324 if (!hb_ot_color_has_layers (face) &&
325 !hb_ot_color_has_png (face) &&
326 !hb_ot_color_has_svg (face))
327 dump_glyphs (cairo_face, upem, num_glyphs, face_index);
329 hb_font_destroy (font);
330 hb_face_destroy (face);
333 hb_blob_destroy (blob);