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.
31 #define hb_blob_create_from_file(x) hb_blob_get_empty ()
34 #if !defined(HB_NO_COLOR) && defined(CAIRO_HAS_SVG_SURFACE)
41 #include FT_FREETYPE_H
45 #include <cairo-svg.h>
51 svg_dump (hb_face_t *face, unsigned int face_index)
53 unsigned glyph_count = hb_face_get_glyph_count (face);
55 for (unsigned int glyph_id = 0; glyph_id < glyph_count; glyph_id++)
57 hb_blob_t *blob = hb_ot_color_glyph_reference_svg (face, glyph_id);
59 if (hb_blob_get_length (blob) == 0) continue;
62 const char *data = hb_blob_get_data (blob, &length);
64 char output_path[255];
65 sprintf (output_path, "out/svg-%u-%u.svg%s",
68 // append "z" if the content is gzipped, https://stackoverflow.com/a/6059405
69 (length > 2 && (data[0] == '\x1F') && (data[1] == '\x8B')) ? "z" : "");
71 FILE *f = fopen (output_path, "wb");
72 fwrite (data, 1, length, f);
75 hb_blob_destroy (blob);
79 /* _png API is so easy to use unlike the below code, don't get confused */
81 png_dump (hb_face_t *face, unsigned int face_index)
83 unsigned glyph_count = hb_face_get_glyph_count (face);
84 hb_font_t *font = hb_font_create (face);
86 /* scans the font for strikes */
87 unsigned int sample_glyph_id;
88 /* we don't care about different strikes for different glyphs at this point */
89 for (sample_glyph_id = 0; sample_glyph_id < glyph_count; sample_glyph_id++)
91 hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, sample_glyph_id);
92 unsigned int blob_length = hb_blob_get_length (blob);
93 hb_blob_destroy (blob);
98 unsigned int upem = hb_face_get_upem (face);
99 unsigned int blob_length = 0;
100 unsigned int strike = 0;
101 for (unsigned int ppem = 1; ppem < upem; ppem++)
103 hb_font_set_ppem (font, ppem, ppem);
104 hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, sample_glyph_id);
105 unsigned int new_blob_length = hb_blob_get_length (blob);
106 hb_blob_destroy (blob);
107 if (new_blob_length != blob_length)
109 for (unsigned int glyph_id = 0; glyph_id < glyph_count; glyph_id++)
111 hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, glyph_id);
113 if (hb_blob_get_length (blob) == 0) continue;
116 const char *data = hb_blob_get_data (blob, &length);
118 char output_path[255];
119 sprintf (output_path, "out/png-%u-%u-%u.png", glyph_id, strike, face_index);
121 FILE *f = fopen (output_path, "wb");
122 fwrite (data, 1, length, f);
125 hb_blob_destroy (blob);
129 blob_length = new_blob_length;
133 hb_font_destroy (font);
137 layered_glyph_dump (hb_face_t *face, cairo_font_face_t *cairo_face, unsigned int face_index)
139 unsigned int upem = hb_face_get_upem (face);
141 unsigned glyph_count = hb_face_get_glyph_count (face);
142 for (hb_codepoint_t gid = 0; gid < glyph_count; ++gid)
144 unsigned int num_layers = hb_ot_color_glyph_get_layers (face, gid, 0, nullptr, nullptr);
148 hb_ot_color_layer_t *layers = (hb_ot_color_layer_t*) malloc (num_layers * sizeof (hb_ot_color_layer_t));
150 hb_ot_color_glyph_get_layers (face, gid, 0, &num_layers, layers);
154 cairo_text_extents_t extents;
156 cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
157 cairo_t *cr = cairo_create (surface);
158 cairo_set_font_face (cr, cairo_face);
159 cairo_set_font_size (cr, upem);
161 cairo_glyph_t *glyphs = (cairo_glyph_t *) calloc (num_layers, sizeof (cairo_glyph_t));
162 for (unsigned int j = 0; j < num_layers; ++j)
163 glyphs[j].index = layers[j].glyph;
164 cairo_glyph_extents (cr, glyphs, num_layers, &extents);
166 cairo_surface_destroy (surface);
170 // Add a slight margin
171 extents.width += extents.width / 10;
172 extents.height += extents.height / 10;
173 extents.x_bearing -= extents.width / 20;
174 extents.y_bearing -= extents.height / 20;
177 unsigned int palette_count = hb_ot_color_palette_get_count (face);
178 for (unsigned int palette = 0; palette < palette_count; palette++)
180 unsigned int num_colors = hb_ot_color_palette_get_colors (face, palette, 0, nullptr, nullptr);
184 hb_color_t *colors = (hb_color_t*) calloc (num_colors, sizeof (hb_color_t));
185 hb_ot_color_palette_get_colors (face, palette, 0, &num_colors, colors);
188 char output_path[255];
189 sprintf (output_path, "out/colr-%u-%u-%u.svg", gid, palette, face_index);
191 cairo_surface_t *surface = cairo_svg_surface_create (output_path, extents.width, extents.height);
192 cairo_t *cr = cairo_create (surface);
193 cairo_set_font_face (cr, cairo_face);
194 cairo_set_font_size (cr, upem);
196 for (unsigned int layer = 0; layer < num_layers; ++layer)
198 hb_color_t color = 0x000000FF;
199 if (layers[layer].color_index != 0xFFFF)
200 color = colors[layers[layer].color_index];
201 cairo_set_source_rgba (cr,
202 hb_color_get_red (color) / 255.,
203 hb_color_get_green (color) / 255.,
204 hb_color_get_blue (color) / 255.,
205 hb_color_get_alpha (color) / 255.);
208 glyph.index = layers[layer].glyph;
209 glyph.x = -extents.x_bearing;
210 glyph.y = -extents.y_bearing;
211 cairo_show_glyphs (cr, &glyph, 1);
214 cairo_surface_destroy (surface);
226 dump_glyphs (cairo_font_face_t *cairo_face, unsigned int upem,
227 unsigned int num_glyphs, unsigned int face_index)
229 for (unsigned int i = 0; i < num_glyphs; ++i)
231 cairo_text_extents_t extents;
232 cairo_glyph_t glyph = {0};
237 cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
238 cairo_t *cr = cairo_create (surface);
239 cairo_set_font_face (cr, cairo_face);
240 cairo_set_font_size (cr, upem);
242 cairo_glyph_extents (cr, &glyph, 1, &extents);
243 cairo_surface_destroy (surface);
247 // Add a slight margin
248 extents.width += extents.width / 10;
249 extents.height += extents.height / 10;
250 extents.x_bearing -= extents.width / 20;
251 extents.y_bearing -= extents.height / 20;
255 char output_path[255];
256 sprintf (output_path, "out/%u-%u.svg", face_index, i);
257 cairo_surface_t *surface = cairo_svg_surface_create (output_path, extents.width, extents.height);
258 cairo_t *cr = cairo_create (surface);
259 cairo_set_font_face (cr, cairo_face);
260 cairo_set_font_size (cr, upem);
261 glyph.x = -extents.x_bearing;
262 glyph.y = -extents.y_bearing;
263 cairo_show_glyphs (cr, &glyph, 1);
264 cairo_surface_destroy (surface);
271 main (int argc, char **argv)
274 fprintf (stderr, "usage: %s font-file.ttf\n"
275 "run it like `rm -rf out && mkdir out && %s font-file.ttf`\n",
281 FILE *font_name_file = fopen ("out/.dumped_font_name", "r");
282 if (font_name_file != nullptr)
284 fprintf (stderr, "Purge or move ./out folder in order to run a new dump\n");
288 font_name_file = fopen ("out/.dumped_font_name", "w");
289 if (font_name_file == nullptr)
291 fprintf (stderr, "./out is not accessible as a folder, create it please\n");
294 fwrite (argv[1], 1, strlen (argv[1]), font_name_file);
295 fclose (font_name_file);
297 hb_blob_t *blob = hb_blob_create_from_file (argv[1]);
298 unsigned int num_faces = hb_face_count (blob);
301 fprintf (stderr, "error: The file (%s) was corrupted, empty or not found", argv[1]);
305 for (unsigned int face_index = 0; face_index < hb_face_count (blob); face_index++)
307 hb_face_t *face = hb_face_create (blob, face_index);
308 hb_font_t *font = hb_font_create (face);
310 if (hb_ot_color_has_png (face)) printf ("Dumping png (cbdt/sbix)...\n");
311 png_dump (face, face_index);
313 if (hb_ot_color_has_svg (face)) printf ("Dumping svg...\n");
314 svg_dump (face, face_index);
316 cairo_font_face_t *cairo_face;
319 FT_Init_FreeType (&library);
321 FT_New_Face (library, argv[1], 0, &ft_face);
322 cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
324 if (hb_ot_color_has_layers (face) && hb_ot_color_has_palettes (face))
325 printf ("Dumping layered color glyphs...\n");
326 layered_glyph_dump (face, cairo_face, face_index);
328 unsigned int num_glyphs = hb_face_get_glyph_count (face);
329 unsigned int upem = hb_face_get_upem (face);
331 // disabled when color font as cairo rendering of NotoColorEmoji is soooo slow
332 if (!hb_ot_color_has_layers (face) &&
333 !hb_ot_color_has_png (face) &&
334 !hb_ot_color_has_svg (face))
335 dump_glyphs (cairo_face, upem, num_glyphs, face_index);
337 hb_font_destroy (font);
338 hb_face_destroy (face);
341 hb_blob_destroy (blob);
347 int main (int argc, char **argv) { return 0; }