2 * Copyright © 2007,2008,2009 Red Hat, Inc.
3 * Copyright © 2018,2019,2020 Ebrahim Byagowi
4 * Copyright © 2018 Khaled Hosny
6 * This is part of HarfBuzz, a text shaping library.
8 * Permission is hereby granted, without written agreement and without
9 * license or royalty fees, to use, copy, modify, and distribute this
10 * software and its documentation for any purpose, provided that the
11 * above copyright notice and the following two paragraphs appear in
12 * all copies of this software.
14 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
17 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
20 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
21 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
26 * Red Hat Author(s): Behdad Esfahbod
42 #define hb_blob_create_from_file_or_fail(x) hb_blob_get_empty ()
45 #if !defined(HB_NO_COLOR) && !defined(HB_NO_DRAW) && defined(HB_EXPERIMENTAL_API)
47 svg_dump (hb_face_t *face, unsigned face_index)
49 unsigned glyph_count = hb_face_get_glyph_count (face);
51 for (unsigned glyph_id = 0; glyph_id < glyph_count; ++glyph_id)
53 hb_blob_t *blob = hb_ot_color_glyph_reference_svg (face, glyph_id);
55 if (hb_blob_get_length (blob) == 0) continue;
58 const char *data = hb_blob_get_data (blob, &length);
60 char output_path[255];
61 sprintf (output_path, "out/svg-%u-%u.svg%s",
64 // append "z" if the content is gzipped, https://stackoverflow.com/a/6059405
65 (length > 2 && (data[0] == '\x1F') && (data[1] == '\x8B')) ? "z" : "");
67 FILE *f = fopen (output_path, "wb");
68 fwrite (data, 1, length, f);
71 hb_blob_destroy (blob);
75 /* _png API is so easy to use unlike the below code, don't get confused */
77 png_dump (hb_face_t *face, unsigned face_index)
79 unsigned glyph_count = hb_face_get_glyph_count (face);
80 hb_font_t *font = hb_font_create (face);
82 /* scans the font for strikes */
83 unsigned sample_glyph_id;
84 /* we don't care about different strikes for different glyphs at this point */
85 for (sample_glyph_id = 0; sample_glyph_id < glyph_count; ++sample_glyph_id)
87 hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, sample_glyph_id);
88 unsigned blob_length = hb_blob_get_length (blob);
89 hb_blob_destroy (blob);
94 unsigned upem = hb_face_get_upem (face);
95 unsigned blob_length = 0;
97 for (unsigned ppem = 1; ppem < upem; ++ppem)
99 hb_font_set_ppem (font, ppem, ppem);
100 hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, sample_glyph_id);
101 unsigned new_blob_length = hb_blob_get_length (blob);
102 hb_blob_destroy (blob);
103 if (new_blob_length != blob_length)
105 for (unsigned glyph_id = 0; glyph_id < glyph_count; ++glyph_id)
107 hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, glyph_id);
109 if (hb_blob_get_length (blob) == 0) continue;
112 const char *data = hb_blob_get_data (blob, &length);
114 char output_path[255];
115 sprintf (output_path, "out/png-%u-%u-%u.png", glyph_id, strike, face_index);
117 FILE *f = fopen (output_path, "wb");
118 fwrite (data, 1, length, f);
121 hb_blob_destroy (blob);
125 blob_length = new_blob_length;
129 hb_font_destroy (font);
135 hb_position_t ascender;
139 move_to (hb_position_t to_x, hb_position_t to_y, user_data_t &user_data)
141 fprintf (user_data.f, "M%d,%d", to_x, user_data.ascender - to_y);
145 line_to (hb_position_t to_x, hb_position_t to_y, user_data_t &user_data)
147 fprintf (user_data.f, "L%d,%d", to_x, user_data.ascender - to_y);
151 quadratic_to (hb_position_t control_x, hb_position_t control_y,
152 hb_position_t to_x, hb_position_t to_y,
153 user_data_t &user_data)
155 fprintf (user_data.f, "Q%d,%d %d,%d", control_x, user_data.ascender - control_y,
156 to_x, user_data.ascender - to_y);
160 cubic_to (hb_position_t control1_x, hb_position_t control1_y,
161 hb_position_t control2_x, hb_position_t control2_y,
162 hb_position_t to_x, hb_position_t to_y,
163 user_data_t &user_data)
165 fprintf (user_data.f, "C%d,%d %d,%d %d,%d", control1_x, user_data.ascender - control1_y,
166 control2_x, user_data.ascender - control2_y,
167 to_x, user_data.ascender - to_y);
171 close_path (user_data_t &user_data)
173 fprintf (user_data.f, "Z");
177 layered_glyph_dump (hb_font_t *font, hb_draw_funcs_t *funcs, unsigned face_index)
179 hb_face_t *face = hb_font_get_face (font);
180 unsigned palette_count = hb_ot_color_palette_get_count (face);
181 for (unsigned palette = 0; palette < palette_count; ++palette)
183 unsigned num_colors = hb_ot_color_palette_get_colors (face, palette, 0, nullptr, nullptr);
184 if (!num_colors) continue;
186 hb_color_t *colors = (hb_color_t*) calloc (num_colors, sizeof (hb_color_t));
187 hb_ot_color_palette_get_colors (face, palette, 0, &num_colors, colors);
194 unsigned num_glyphs = hb_face_get_glyph_count (face);
195 for (hb_codepoint_t gid = 0; gid < num_glyphs; ++gid)
197 unsigned num_layers = hb_ot_color_glyph_get_layers (face, gid, 0, nullptr, nullptr);
198 if (!num_layers) continue;
200 hb_ot_color_layer_t *layers = (hb_ot_color_layer_t*) malloc (num_layers * sizeof (hb_ot_color_layer_t));
202 hb_ot_color_glyph_get_layers (face, gid, 0, &num_layers, layers);
205 hb_font_extents_t font_extents;
206 hb_font_get_extents_for_direction (font, HB_DIRECTION_LTR, &font_extents);
207 hb_glyph_extents_t extents = {0};
208 if (!hb_font_get_glyph_extents (font, gid, &extents))
210 printf ("Skip gid: %d\n", gid);
214 char output_path[255];
215 sprintf (output_path, "out/colr-%u-%u-%u.svg", gid, palette, face_index);
216 FILE *f = fopen (output_path, "wb");
217 fprintf (f, "<svg xmlns=\"http://www.w3.org/2000/svg\""
218 " viewBox=\"%d %d %d %d\">\n",
219 extents.x_bearing, 0,
220 extents.x_bearing + extents.width, -extents.height);
221 user_data_t user_data;
222 user_data.ascender = extents.y_bearing;
225 for (unsigned layer = 0; layer < num_layers; ++layer)
227 hb_color_t color = 0x000000FF;
228 if (layers[layer].color_index != 0xFFFF)
229 color = colors[layers[layer].color_index];
230 fprintf (f, "<path fill=\"#%02X%02X%02X\" ",
231 hb_color_get_red (color), hb_color_get_green (color), hb_color_get_green (color));
232 if (hb_color_get_alpha (color) != 255)
233 fprintf (f, "fill-opacity=\"%.3f\"", (double) hb_color_get_alpha (color) / 255.);
235 if (!hb_font_draw_glyph (font, layers[layer].glyph, funcs, &user_data))
236 printf ("Failed to decompose layer %d while %d\n", layers[layer].glyph, gid);
237 fprintf (f, "\"/>\n");
240 fprintf (f, "</svg>");
251 dump_glyphs (hb_font_t *font, hb_draw_funcs_t *funcs, unsigned face_index)
253 unsigned num_glyphs = hb_face_get_glyph_count (hb_font_get_face (font));
254 for (unsigned gid = 0; gid < num_glyphs; ++gid)
256 hb_font_extents_t font_extents;
257 hb_font_get_extents_for_direction (font, HB_DIRECTION_LTR, &font_extents);
258 hb_glyph_extents_t extents = {0};
259 if (!hb_font_get_glyph_extents (font, gid, &extents))
261 printf ("Skip gid: %d\n", gid);
265 char output_path[255];
266 sprintf (output_path, "out/%u-%u.svg", face_index, gid);
267 FILE *f = fopen (output_path, "wb");
268 fprintf (f, "<svg xmlns=\"http://www.w3.org/2000/svg\""
269 " viewBox=\"%d %d %d %d\"><path d=\"",
270 extents.x_bearing, 0,
271 extents.x_bearing + extents.width, font_extents.ascender - font_extents.descender);
272 user_data_t user_data;
273 user_data.ascender = font_extents.ascender;
275 if (!hb_font_draw_glyph (font, gid, funcs, &user_data))
276 printf ("Failed to decompose gid: %d\n", gid);
277 fprintf (f, "\"/></svg>");
283 dump_glyphs (hb_blob_t *blob, const char *font_name)
285 FILE *font_name_file = fopen ("out/.dumped_font_name", "r");
288 fprintf (stderr, "Purge or rename ./out folder if you like to run a glyph dump,\n"
289 "run it like `rm -rf out && mkdir out && src/main font-file.ttf`\n");
293 font_name_file = fopen ("out/.dumped_font_name", "w");
296 fprintf (stderr, "./out is not accessible as a folder, create it please\n");
299 fwrite (font_name, 1, strlen (font_name), font_name_file);
300 fclose (font_name_file);
302 hb_draw_funcs_t *funcs = hb_draw_funcs_create ();
303 hb_draw_funcs_set_move_to_func (funcs, (hb_draw_move_to_func_t) move_to);
304 hb_draw_funcs_set_line_to_func (funcs, (hb_draw_line_to_func_t) line_to);
305 hb_draw_funcs_set_quadratic_to_func (funcs, (hb_draw_quadratic_to_func_t) quadratic_to);
306 hb_draw_funcs_set_cubic_to_func (funcs, (hb_draw_cubic_to_func_t) cubic_to);
307 hb_draw_funcs_set_close_path_func (funcs, (hb_draw_close_path_func_t) close_path);
309 unsigned num_faces = hb_face_count (blob);
310 for (unsigned face_index = 0; face_index < num_faces; ++face_index)
312 hb_face_t *face = hb_face_create (blob, face_index);
313 hb_font_t *font = hb_font_create (face);
315 if (hb_ot_color_has_png (face))
316 printf ("Dumping png (CBDT/sbix)...\n");
317 png_dump (face, face_index);
319 if (hb_ot_color_has_svg (face))
320 printf ("Dumping svg (SVG )...\n");
321 svg_dump (face, face_index);
323 if (hb_ot_color_has_layers (face) && hb_ot_color_has_palettes (face))
324 printf ("Dumping layered color glyphs (COLR/CPAL)...\n");
325 layered_glyph_dump (font, funcs, face_index);
327 dump_glyphs (font, funcs, face_index);
329 hb_font_destroy (font);
330 hb_face_destroy (face);
333 hb_draw_funcs_destroy (funcs);
337 #ifndef MAIN_CC_NO_PRIVATE_API
338 /* Only this part of this mini app uses private API */
339 #include "hb-static.cc"
340 #include "hb-open-file.hh"
341 #include "hb-ot-layout-gdef-table.hh"
342 #include "hb-ot-layout-gsubgpos.hh"
347 print_layout_info_using_private_api (hb_blob_t *blob)
349 const char *font_data = hb_blob_get_data (blob, nullptr);
350 hb_blob_t *font_blob = hb_sanitize_context_t ().sanitize_blob<OpenTypeFontFile> (blob);
351 const OpenTypeFontFile* sanitized = font_blob->as<OpenTypeFontFile> ();
352 if (!font_blob->data)
354 printf ("Sanitization of the file wasn't successful. Exit");
357 const OpenTypeFontFile& ot = *sanitized;
359 switch (ot.get_tag ())
361 case OpenTypeFontFile::TrueTypeTag:
362 printf ("OpenType font with TrueType outlines\n");
364 case OpenTypeFontFile::CFFTag:
365 printf ("OpenType font with CFF (Type1) outlines\n");
367 case OpenTypeFontFile::TTCTag:
368 printf ("TrueType Collection of OpenType fonts\n");
370 case OpenTypeFontFile::TrueTag:
371 printf ("Obsolete Apple TrueType font\n");
373 case OpenTypeFontFile::Typ1Tag:
374 printf ("Obsolete Apple Type1 font in SFNT container\n");
376 case OpenTypeFontFile::DFontTag:
377 printf ("DFont Mac Resource Fork\n");
380 printf ("Unknown font format\n");
384 unsigned num_faces = hb_face_count (blob);
385 printf ("%d font(s) found in file\n", num_faces);
386 for (unsigned n_font = 0; n_font < num_faces; ++n_font)
388 const OpenTypeFontFace &font = ot.get_face (n_font);
389 printf ("Font %d of %d:\n", n_font, num_faces);
391 unsigned num_tables = font.get_table_count ();
392 printf (" %d table(s) found in font\n", num_tables);
393 for (unsigned n_table = 0; n_table < num_tables; ++n_table)
395 const OpenTypeTable &table = font.get_table (n_table);
396 printf (" Table %2d of %2d: %.4s (0x%08x+0x%08x)\n", n_table, num_tables,
397 (const char *) table.tag,
398 (unsigned) table.offset,
399 (unsigned) table.length);
408 const GSUBGPOS &g = *reinterpret_cast<const GSUBGPOS *> (font_data + table.offset);
410 unsigned num_scripts = g.get_script_count ();
411 printf (" %d script(s) found in table\n", num_scripts);
412 for (unsigned n_script = 0; n_script < num_scripts; ++n_script)
414 const Script &script = g.get_script (n_script);
415 printf (" Script %2d of %2d: %.4s\n", n_script, num_scripts,
416 (const char *) g.get_script_tag (n_script));
418 if (!script.has_default_lang_sys ())
419 printf (" No default language system\n");
420 int num_langsys = script.get_lang_sys_count ();
421 printf (" %d language system(s) found in script\n", num_langsys);
422 for (int n_langsys = script.has_default_lang_sys () ? -1 : 0; n_langsys < num_langsys; ++n_langsys)
424 const LangSys &langsys = n_langsys == -1
425 ? script.get_default_lang_sys ()
426 : script.get_lang_sys (n_langsys);
428 printf (" Default Language System\n");
430 printf (" Language System %2d of %2d: %.4s\n", n_langsys, num_langsys,
431 (const char *) script.get_lang_sys_tag (n_langsys));
432 if (!langsys.has_required_feature ())
433 printf (" No required feature\n");
435 printf (" Required feature index: %d\n",
436 langsys.get_required_feature_index ());
438 unsigned num_features = langsys.get_feature_count ();
439 printf (" %d feature(s) found in language system\n", num_features);
440 for (unsigned n_feature = 0; n_feature < num_features; ++n_feature)
442 printf (" Feature index %2d of %2d: %d\n", n_feature, num_features,
443 langsys.get_feature_index (n_feature));
448 unsigned num_features = g.get_feature_count ();
449 printf (" %d feature(s) found in table\n", num_features);
450 for (unsigned n_feature = 0; n_feature < num_features; ++n_feature)
452 const Feature &feature = g.get_feature (n_feature);
453 unsigned num_lookups = feature.get_lookup_count ();
454 printf (" Feature %2d of %2d: %c%c%c%c\n", n_feature, num_features,
455 HB_UNTAG (g.get_feature_tag (n_feature)));
457 printf (" %d lookup(s) found in feature\n", num_lookups);
458 for (unsigned n_lookup = 0; n_lookup < num_lookups; ++n_lookup) {
459 printf (" Lookup index %2d of %2d: %d\n", n_lookup, num_lookups,
460 feature.get_lookup_index (n_lookup));
464 unsigned num_lookups = g.get_lookup_count ();
465 printf (" %d lookup(s) found in table\n", num_lookups);
466 for (unsigned n_lookup = 0; n_lookup < num_lookups; ++n_lookup)
468 const Lookup &lookup = g.get_lookup (n_lookup);
469 printf (" Lookup %2d of %2d: type %d, props 0x%04X\n", n_lookup, num_lookups,
470 lookup.get_type (), lookup.get_props ());
479 const GDEF &gdef = *reinterpret_cast<const GDEF *> (font_data + table.offset);
481 printf (" Has %sglyph classes\n",
482 gdef.has_glyph_classes () ? "" : "no ");
483 printf (" Has %smark attachment types\n",
484 gdef.has_mark_attachment_types () ? "" : "no ");
485 printf (" Has %sattach points\n",
486 gdef.has_attach_points () ? "" : "no ");
487 printf (" Has %slig carets\n",
488 gdef.has_lig_carets () ? "" : "no ");
489 printf (" Has %smark sets\n",
490 gdef.has_mark_sets () ? "" : "no ");
497 /* end of private API use */
501 main (int argc, char **argv)
505 fprintf (stderr, "usage: %s font-file.ttf\n", argv[0]);
509 hb_blob_t *blob = hb_blob_create_from_file_or_fail (argv[1]);
511 printf ("Opened font file %s: %d bytes long\n", argv[1], hb_blob_get_length (blob));
512 #ifndef MAIN_CC_NO_PRIVATE_API
513 print_layout_info_using_private_api (blob);
515 #if !defined(HB_NO_COLOR) && !defined(HB_NO_DRAW) && defined(HB_EXPERIMENTAL_API)
516 dump_glyphs (blob, argv[1]);
518 hb_blob_destroy (blob);