1 /*
2 * Copyright © 2007,2008,2009 Red Hat, Inc.
3 * Copyright © 2018,2019,2020 Ebrahim Byagowi
4 * Copyright © 2018 Khaled Hosny
5 *
6 * This is part of HarfBuzz, a text shaping library.
7 *
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.
13 *
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
18 * DAMAGE.
19 *
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.
25 *
26 * Red Hat Author(s): Behdad Esfahbod
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "hb.h"
34 #include "hb-ot.h"
35
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39
40 #ifdef HB_NO_OPEN
41 #define hb_blob_create_from_file(x) hb_blob_get_empty ()
42 #endif
43
44 #if !defined(HB_NO_COLOR) && !defined(HB_NO_DRAW) && defined(HB_EXPERIMENTAL_API)
45 static void
svg_dump(hb_face_t * face,unsigned face_index)46 svg_dump (hb_face_t *face, unsigned face_index)
47 {
48 unsigned glyph_count = hb_face_get_glyph_count (face);
49
50 for (unsigned glyph_id = 0; glyph_id < glyph_count; ++glyph_id)
51 {
52 hb_blob_t *blob = hb_ot_color_glyph_reference_svg (face, glyph_id);
53
54 if (hb_blob_get_length (blob) == 0) continue;
55
56 unsigned length;
57 const char *data = hb_blob_get_data (blob, &length);
58
59 char output_path[255];
60 sprintf (output_path, "out/svg-%u-%u.svg%s",
61 glyph_id,
62 face_index,
63 // append "z" if the content is gzipped, https://stackoverflow.com/a/6059405
64 (length > 2 && (data[0] == '\x1F') && (data[1] == '\x8B')) ? "z" : "");
65
66 FILE *f = fopen (output_path, "wb");
67 fwrite (data, 1, length, f);
68 fclose (f);
69
70 hb_blob_destroy (blob);
71 }
72 }
73
74 /* _png API is so easy to use unlike the below code, don't get confused */
75 static void
png_dump(hb_face_t * face,unsigned face_index)76 png_dump (hb_face_t *face, unsigned face_index)
77 {
78 unsigned glyph_count = hb_face_get_glyph_count (face);
79 hb_font_t *font = hb_font_create (face);
80
81 /* scans the font for strikes */
82 unsigned sample_glyph_id;
83 /* we don't care about different strikes for different glyphs at this point */
84 for (sample_glyph_id = 0; sample_glyph_id < glyph_count; ++sample_glyph_id)
85 {
86 hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, sample_glyph_id);
87 unsigned blob_length = hb_blob_get_length (blob);
88 hb_blob_destroy (blob);
89 if (blob_length != 0)
90 break;
91 }
92
93 unsigned upem = hb_face_get_upem (face);
94 unsigned blob_length = 0;
95 unsigned strike = 0;
96 for (unsigned ppem = 1; ppem < upem; ++ppem)
97 {
98 hb_font_set_ppem (font, ppem, ppem);
99 hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, sample_glyph_id);
100 unsigned new_blob_length = hb_blob_get_length (blob);
101 hb_blob_destroy (blob);
102 if (new_blob_length != blob_length)
103 {
104 for (unsigned glyph_id = 0; glyph_id < glyph_count; ++glyph_id)
105 {
106 hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, glyph_id);
107
108 if (hb_blob_get_length (blob) == 0) continue;
109
110 unsigned length;
111 const char *data = hb_blob_get_data (blob, &length);
112
113 char output_path[255];
114 sprintf (output_path, "out/png-%u-%u-%u.png", glyph_id, strike, face_index);
115
116 FILE *f = fopen (output_path, "wb");
117 fwrite (data, 1, length, f);
118 fclose (f);
119
120 hb_blob_destroy (blob);
121 }
122
123 strike++;
124 blob_length = new_blob_length;
125 }
126 }
127
128 hb_font_destroy (font);
129 }
130
131 struct user_data_t
132 {
133 FILE *f;
134 hb_position_t ascender;
135 };
136
137 static void
move_to(hb_position_t to_x,hb_position_t to_y,user_data_t & user_data)138 move_to (hb_position_t to_x, hb_position_t to_y, user_data_t &user_data)
139 {
140 fprintf (user_data.f, "M%d,%d", to_x, user_data.ascender - to_y);
141 }
142
143 static void
line_to(hb_position_t to_x,hb_position_t to_y,user_data_t & user_data)144 line_to (hb_position_t to_x, hb_position_t to_y, user_data_t &user_data)
145 {
146 fprintf (user_data.f, "L%d,%d", to_x, user_data.ascender - to_y);
147 }
148
149 static void
quadratic_to(hb_position_t control_x,hb_position_t control_y,hb_position_t to_x,hb_position_t to_y,user_data_t & user_data)150 quadratic_to (hb_position_t control_x, hb_position_t control_y,
151 hb_position_t to_x, hb_position_t to_y,
152 user_data_t &user_data)
153 {
154 fprintf (user_data.f, "Q%d,%d %d,%d", control_x, user_data.ascender - control_y,
155 to_x, user_data.ascender - to_y);
156 }
157
158 static void
cubic_to(hb_position_t control1_x,hb_position_t control1_y,hb_position_t control2_x,hb_position_t control2_y,hb_position_t to_x,hb_position_t to_y,user_data_t & user_data)159 cubic_to (hb_position_t control1_x, hb_position_t control1_y,
160 hb_position_t control2_x, hb_position_t control2_y,
161 hb_position_t to_x, hb_position_t to_y,
162 user_data_t &user_data)
163 {
164 fprintf (user_data.f, "C%d,%d %d,%d %d,%d", control1_x, user_data.ascender - control1_y,
165 control2_x, user_data.ascender - control2_y,
166 to_x, user_data.ascender - to_y);
167 }
168
169 static void
close_path(user_data_t & user_data)170 close_path (user_data_t &user_data)
171 {
172 fprintf (user_data.f, "Z");
173 }
174
175 static void
layered_glyph_dump(hb_font_t * font,hb_draw_funcs_t * funcs,unsigned face_index)176 layered_glyph_dump (hb_font_t *font, hb_draw_funcs_t *funcs, unsigned face_index)
177 {
178 hb_face_t *face = hb_font_get_face (font);
179 unsigned palette_count = hb_ot_color_palette_get_count (face);
180 for (unsigned palette = 0; palette < palette_count; ++palette)
181 {
182 unsigned num_colors = hb_ot_color_palette_get_colors (face, palette, 0, nullptr, nullptr);
183 if (!num_colors) continue;
184
185 hb_color_t *colors = (hb_color_t*) calloc (num_colors, sizeof (hb_color_t));
186 hb_ot_color_palette_get_colors (face, palette, 0, &num_colors, colors);
187 if (!num_colors)
188 {
189 free (colors);
190 continue;
191 }
192
193 unsigned num_glyphs = hb_face_get_glyph_count (face);
194 for (hb_codepoint_t gid = 0; gid < num_glyphs; ++gid)
195 {
196 unsigned num_layers = hb_ot_color_glyph_get_layers (face, gid, 0, nullptr, nullptr);
197 if (!num_layers) continue;
198
199 hb_ot_color_layer_t *layers = (hb_ot_color_layer_t*) malloc (num_layers * sizeof (hb_ot_color_layer_t));
200
201 hb_ot_color_glyph_get_layers (face, gid, 0, &num_layers, layers);
202 if (num_layers)
203 {
204 hb_font_extents_t font_extents;
205 hb_font_get_extents_for_direction (font, HB_DIRECTION_LTR, &font_extents);
206 hb_glyph_extents_t extents = {0};
207 if (!hb_font_get_glyph_extents (font, gid, &extents))
208 {
209 printf ("Skip gid: %d\n", gid);
210 continue;
211 }
212
213 char output_path[255];
214 sprintf (output_path, "out/colr-%u-%u-%u.svg", gid, palette, face_index);
215 FILE *f = fopen (output_path, "wb");
216 fprintf (f, "<svg xmlns=\"http://www.w3.org/2000/svg\""
217 " viewBox=\"%d %d %d %d\">\n",
218 extents.x_bearing, 0,
219 extents.x_bearing + extents.width, -extents.height);
220 user_data_t user_data;
221 user_data.ascender = extents.y_bearing;
222 user_data.f = f;
223
224 for (unsigned layer = 0; layer < num_layers; ++layer)
225 {
226 hb_color_t color = 0x000000FF;
227 if (layers[layer].color_index != 0xFFFF)
228 color = colors[layers[layer].color_index];
229 fprintf (f, "<path fill=\"#%02X%02X%02X\" ",
230 hb_color_get_red (color), hb_color_get_green (color), hb_color_get_green (color));
231 if (hb_color_get_alpha (color) != 255)
232 fprintf (f, "fill-opacity=\"%.3f\"", (double) hb_color_get_alpha (color) / 255.);
233 fprintf (f, "d=\"");
234 if (!hb_font_draw_glyph (font, layers[layer].glyph, funcs, &user_data))
235 printf ("Failed to decompose layer %d while %d\n", layers[layer].glyph, gid);
236 fprintf (f, "\"/>\n");
237 }
238
239 fprintf (f, "</svg>");
240 fclose (f);
241 }
242 free (layers);
243 }
244
245 free (colors);
246 }
247 }
248
249 static void
dump_glyphs(hb_font_t * font,hb_draw_funcs_t * funcs,unsigned face_index)250 dump_glyphs (hb_font_t *font, hb_draw_funcs_t *funcs, unsigned face_index)
251 {
252 unsigned num_glyphs = hb_face_get_glyph_count (hb_font_get_face (font));
253 for (unsigned gid = 0; gid < num_glyphs; ++gid)
254 {
255 hb_font_extents_t font_extents;
256 hb_font_get_extents_for_direction (font, HB_DIRECTION_LTR, &font_extents);
257 hb_glyph_extents_t extents = {0};
258 if (!hb_font_get_glyph_extents (font, gid, &extents))
259 {
260 printf ("Skip gid: %d\n", gid);
261 continue;
262 }
263
264 char output_path[255];
265 sprintf (output_path, "out/%u-%u.svg", face_index, gid);
266 FILE *f = fopen (output_path, "wb");
267 fprintf (f, "<svg xmlns=\"http://www.w3.org/2000/svg\""
268 " viewBox=\"%d %d %d %d\"><path d=\"",
269 extents.x_bearing, 0,
270 extents.x_bearing + extents.width, font_extents.ascender - font_extents.descender);
271 user_data_t user_data;
272 user_data.ascender = font_extents.ascender;
273 user_data.f = f;
274 if (!hb_font_draw_glyph (font, gid, funcs, &user_data))
275 printf ("Failed to decompose gid: %d\n", gid);
276 fprintf (f, "\"/></svg>");
277 fclose (f);
278 }
279 }
280
281 static void
dump_glyphs(hb_blob_t * blob,const char * font_name)282 dump_glyphs (hb_blob_t *blob, const char *font_name)
283 {
284 FILE *font_name_file = fopen ("out/.dumped_font_name", "r");
285 if (font_name_file)
286 {
287 fprintf (stderr, "Purge or rename ./out folder if you like to run a glyph dump,\n"
288 "run it like `rm -rf out && mkdir out && src/main font-file.ttf`\n");
289 return;
290 }
291
292 font_name_file = fopen ("out/.dumped_font_name", "w");
293 if (!font_name_file)
294 {
295 fprintf (stderr, "./out is not accessible as a folder, create it please\n");
296 return;
297 }
298 fwrite (font_name, 1, strlen (font_name), font_name_file);
299 fclose (font_name_file);
300
301 hb_draw_funcs_t *funcs = hb_draw_funcs_create ();
302 hb_draw_funcs_set_move_to_func (funcs, (hb_draw_move_to_func_t) move_to);
303 hb_draw_funcs_set_line_to_func (funcs, (hb_draw_line_to_func_t) line_to);
304 hb_draw_funcs_set_quadratic_to_func (funcs, (hb_draw_quadratic_to_func_t) quadratic_to);
305 hb_draw_funcs_set_cubic_to_func (funcs, (hb_draw_cubic_to_func_t) cubic_to);
306 hb_draw_funcs_set_close_path_func (funcs, (hb_draw_close_path_func_t) close_path);
307
308 unsigned num_faces = hb_face_count (blob);
309 for (unsigned face_index = 0; face_index < num_faces; ++face_index)
310 {
311 hb_face_t *face = hb_face_create (blob, face_index);
312 hb_font_t *font = hb_font_create (face);
313
314 if (hb_ot_color_has_png (face))
315 printf ("Dumping png (CBDT/sbix)...\n");
316 png_dump (face, face_index);
317
318 if (hb_ot_color_has_svg (face))
319 printf ("Dumping svg (SVG )...\n");
320 svg_dump (face, face_index);
321
322 if (hb_ot_color_has_layers (face) && hb_ot_color_has_palettes (face))
323 printf ("Dumping layered color glyphs (COLR/CPAL)...\n");
324 layered_glyph_dump (font, funcs, face_index);
325
326 dump_glyphs (font, funcs, face_index);
327
328 hb_font_destroy (font);
329 hb_face_destroy (face);
330 }
331
332 hb_draw_funcs_destroy (funcs);
333 }
334 #endif
335
336 #ifndef MAIN_CC_NO_PRIVATE_API
337 /* Only this part of this mini app uses private API */
338 #include "hb-static.cc"
339 #include "hb-open-file.hh"
340 #include "hb-ot-layout-gdef-table.hh"
341 #include "hb-ot-layout-gsubgpos.hh"
342
343 using namespace OT;
344
345 static void
print_layout_info_using_private_api(hb_blob_t * blob)346 print_layout_info_using_private_api (hb_blob_t *blob)
347 {
348 const char *font_data = hb_blob_get_data (blob, nullptr);
349 hb_blob_t *font_blob = hb_sanitize_context_t ().sanitize_blob<OpenTypeFontFile> (blob);
350 const OpenTypeFontFile* sanitized = font_blob->as<OpenTypeFontFile> ();
351 if (!font_blob->data)
352 {
353 printf ("Sanitization of the file wasn't successful. Exit");
354 exit (1);
355 }
356 const OpenTypeFontFile& ot = *sanitized;
357
358 switch (ot.get_tag ())
359 {
360 case OpenTypeFontFile::TrueTypeTag:
361 printf ("OpenType font with TrueType outlines\n");
362 break;
363 case OpenTypeFontFile::CFFTag:
364 printf ("OpenType font with CFF (Type1) outlines\n");
365 break;
366 case OpenTypeFontFile::TTCTag:
367 printf ("TrueType Collection of OpenType fonts\n");
368 break;
369 case OpenTypeFontFile::TrueTag:
370 printf ("Obsolete Apple TrueType font\n");
371 break;
372 case OpenTypeFontFile::Typ1Tag:
373 printf ("Obsolete Apple Type1 font in SFNT container\n");
374 break;
375 case OpenTypeFontFile::DFontTag:
376 printf ("DFont Mac Resource Fork\n");
377 break;
378 default:
379 printf ("Unknown font format\n");
380 break;
381 }
382
383 unsigned num_faces = hb_face_count (blob);
384 printf ("%d font(s) found in file\n", num_faces);
385 for (unsigned n_font = 0; n_font < num_faces; ++n_font)
386 {
387 const OpenTypeFontFace &font = ot.get_face (n_font);
388 printf ("Font %d of %d:\n", n_font, num_faces);
389
390 unsigned num_tables = font.get_table_count ();
391 printf (" %d table(s) found in font\n", num_tables);
392 for (unsigned n_table = 0; n_table < num_tables; ++n_table)
393 {
394 const OpenTypeTable &table = font.get_table (n_table);
395 printf (" Table %2d of %2d: %.4s (0x%08x+0x%08x)\n", n_table, num_tables,
396 (const char *) table.tag,
397 (unsigned) table.offset,
398 (unsigned) table.length);
399
400 switch (table.tag)
401 {
402
403 case HB_OT_TAG_GSUB:
404 case HB_OT_TAG_GPOS:
405 {
406
407 const GSUBGPOS &g = *reinterpret_cast<const GSUBGPOS *> (font_data + table.offset);
408
409 unsigned num_scripts = g.get_script_count ();
410 printf (" %d script(s) found in table\n", num_scripts);
411 for (unsigned n_script = 0; n_script < num_scripts; ++n_script)
412 {
413 const Script &script = g.get_script (n_script);
414 printf (" Script %2d of %2d: %.4s\n", n_script, num_scripts,
415 (const char *) g.get_script_tag (n_script));
416
417 if (!script.has_default_lang_sys ())
418 printf (" No default language system\n");
419 int num_langsys = script.get_lang_sys_count ();
420 printf (" %d language system(s) found in script\n", num_langsys);
421 for (int n_langsys = script.has_default_lang_sys () ? -1 : 0; n_langsys < num_langsys; ++n_langsys)
422 {
423 const LangSys &langsys = n_langsys == -1
424 ? script.get_default_lang_sys ()
425 : script.get_lang_sys (n_langsys);
426 if (n_langsys == -1)
427 printf (" Default Language System\n");
428 else
429 printf (" Language System %2d of %2d: %.4s\n", n_langsys, num_langsys,
430 (const char *) script.get_lang_sys_tag (n_langsys));
431 if (!langsys.has_required_feature ())
432 printf (" No required feature\n");
433 else
434 printf (" Required feature index: %d\n",
435 langsys.get_required_feature_index ());
436
437 unsigned num_features = langsys.get_feature_count ();
438 printf (" %d feature(s) found in language system\n", num_features);
439 for (unsigned n_feature = 0; n_feature < num_features; ++n_feature)
440 {
441 printf (" Feature index %2d of %2d: %d\n", n_feature, num_features,
442 langsys.get_feature_index (n_feature));
443 }
444 }
445 }
446
447 unsigned num_features = g.get_feature_count ();
448 printf (" %d feature(s) found in table\n", num_features);
449 for (unsigned n_feature = 0; n_feature < num_features; ++n_feature)
450 {
451 const Feature &feature = g.get_feature (n_feature);
452 unsigned num_lookups = feature.get_lookup_count ();
453 printf (" Feature %2d of %2d: %c%c%c%c\n", n_feature, num_features,
454 HB_UNTAG (g.get_feature_tag (n_feature)));
455
456 printf (" %d lookup(s) found in feature\n", num_lookups);
457 for (unsigned n_lookup = 0; n_lookup < num_lookups; ++n_lookup) {
458 printf (" Lookup index %2d of %2d: %d\n", n_lookup, num_lookups,
459 feature.get_lookup_index (n_lookup));
460 }
461 }
462
463 unsigned num_lookups = g.get_lookup_count ();
464 printf (" %d lookup(s) found in table\n", num_lookups);
465 for (unsigned n_lookup = 0; n_lookup < num_lookups; ++n_lookup)
466 {
467 const Lookup &lookup = g.get_lookup (n_lookup);
468 printf (" Lookup %2d of %2d: type %d, props 0x%04X\n", n_lookup, num_lookups,
469 lookup.get_type (), lookup.get_props ());
470 }
471
472 }
473 break;
474
475 case GDEF::tableTag:
476 {
477
478 const GDEF &gdef = *reinterpret_cast<const GDEF *> (font_data + table.offset);
479
480 printf (" Has %sglyph classes\n",
481 gdef.has_glyph_classes () ? "" : "no ");
482 printf (" Has %smark attachment types\n",
483 gdef.has_mark_attachment_types () ? "" : "no ");
484 printf (" Has %sattach points\n",
485 gdef.has_attach_points () ? "" : "no ");
486 printf (" Has %slig carets\n",
487 gdef.has_lig_carets () ? "" : "no ");
488 printf (" Has %smark sets\n",
489 gdef.has_mark_sets () ? "" : "no ");
490 break;
491 }
492 }
493 }
494 }
495 }
496 /* end of private API use */
497 #endif
498
499 int
main(int argc,char ** argv)500 main (int argc, char **argv)
501 {
502 if (argc != 2)
503 {
504 fprintf (stderr, "usage: %s font-file.ttf\n", argv[0]);
505 exit (1);
506 }
507
508 hb_blob_t *blob = hb_blob_create_from_file (argv[1]);
509 printf ("Opened font file %s: %d bytes long\n", argv[1], hb_blob_get_length (blob));
510 #ifndef MAIN_CC_NO_PRIVATE_API
511 print_layout_info_using_private_api (blob);
512 #endif
513 #if !defined(HB_NO_COLOR) && !defined(HB_NO_DRAW) && defined(HB_EXPERIMENTAL_API)
514 dump_glyphs (blob, argv[1]);
515 #endif
516 hb_blob_destroy (blob);
517
518 return 0;
519 }
520