1ab013cd3a87aa4f2934df265f12e327af78bd70
[platform/upstream/harfbuzz.git] / src / main.cc
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 <cassert>
37 #include <cstdlib>
38 #include <cstdio>
39 #include <cstring>
40
41 #ifdef HB_NO_OPEN
42 #define hb_blob_create_from_file_or_fail(x)  hb_blob_get_empty ()
43 #endif
44
45 #if !defined(HB_NO_COLOR) && !defined(HB_NO_DRAW) && defined(HB_EXPERIMENTAL_API)
46 static void
47 svg_dump (hb_face_t *face, unsigned face_index)
48 {
49   unsigned glyph_count = hb_face_get_glyph_count (face);
50
51   for (unsigned glyph_id = 0; glyph_id < glyph_count; ++glyph_id)
52   {
53     hb_blob_t *blob = hb_ot_color_glyph_reference_svg (face, glyph_id);
54
55     if (hb_blob_get_length (blob) == 0) continue;
56
57     unsigned length;
58     const char *data = hb_blob_get_data (blob, &length);
59
60     char output_path[255];
61     sprintf (output_path, "out/svg-%u-%u.svg%s",
62              glyph_id,
63              face_index,
64              // append "z" if the content is gzipped, https://stackoverflow.com/a/6059405
65              (length > 2 && (data[0] == '\x1F') && (data[1] == '\x8B')) ? "z" : "");
66
67     FILE *f = fopen (output_path, "wb");
68     fwrite (data, 1, length, f);
69     fclose (f);
70
71     hb_blob_destroy (blob);
72   }
73 }
74
75 /* _png API is so easy to use unlike the below code, don't get confused */
76 static void
77 png_dump (hb_face_t *face, unsigned face_index)
78 {
79   unsigned glyph_count = hb_face_get_glyph_count (face);
80   hb_font_t *font = hb_font_create (face);
81
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)
86   {
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);
90     if (blob_length != 0)
91       break;
92   }
93
94   unsigned upem = hb_face_get_upem (face);
95   unsigned blob_length = 0;
96   unsigned strike = 0;
97   for (unsigned ppem = 1; ppem < upem; ++ppem)
98   {
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)
104     {
105       for (unsigned glyph_id = 0; glyph_id < glyph_count; ++glyph_id)
106       {
107         hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, glyph_id);
108
109         if (hb_blob_get_length (blob) == 0) continue;
110
111         unsigned length;
112         const char *data = hb_blob_get_data (blob, &length);
113
114         char output_path[255];
115         sprintf (output_path, "out/png-%u-%u-%u.png", glyph_id, strike, face_index);
116
117         FILE *f = fopen (output_path, "wb");
118         fwrite (data, 1, length, f);
119         fclose (f);
120
121         hb_blob_destroy (blob);
122       }
123
124       strike++;
125       blob_length = new_blob_length;
126     }
127   }
128
129   hb_font_destroy (font);
130 }
131
132 struct user_data_t
133 {
134   FILE *f;
135   hb_position_t ascender;
136 };
137
138 static void
139 move_to (hb_position_t to_x, hb_position_t to_y, user_data_t &user_data)
140 {
141   fprintf (user_data.f, "M%d,%d", to_x, user_data.ascender - to_y);
142 }
143
144 static void
145 line_to (hb_position_t to_x, hb_position_t to_y, user_data_t &user_data)
146 {
147   fprintf (user_data.f, "L%d,%d", to_x, user_data.ascender - to_y);
148 }
149
150 static void
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)
154 {
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);
157 }
158
159 static void
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)
164 {
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);
168 }
169
170 static void
171 close_path (user_data_t &user_data)
172 {
173   fprintf (user_data.f, "Z");
174 }
175
176 static void
177 layered_glyph_dump (hb_font_t *font, hb_draw_funcs_t *funcs, unsigned face_index)
178 {
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)
182   {
183     unsigned num_colors = hb_ot_color_palette_get_colors (face, palette, 0, nullptr, nullptr);
184     if (!num_colors) continue;
185
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);
188     if (!num_colors)
189     {
190       free (colors);
191       continue;
192     }
193
194     unsigned num_glyphs = hb_face_get_glyph_count (face);
195     for (hb_codepoint_t gid = 0; gid < num_glyphs; ++gid)
196     {
197       unsigned num_layers = hb_ot_color_glyph_get_layers (face, gid, 0, nullptr, nullptr);
198       if (!num_layers) continue;
199
200       hb_ot_color_layer_t *layers = (hb_ot_color_layer_t*) malloc (num_layers * sizeof (hb_ot_color_layer_t));
201
202       hb_ot_color_glyph_get_layers (face, gid, 0, &num_layers, layers);
203       if (num_layers)
204       {
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))
209         {
210           printf ("Skip gid: %d\n", gid);
211           continue;
212         }
213
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;
223         user_data.f = f;
224
225         for (unsigned layer = 0; layer < num_layers; ++layer)
226         {
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.);
234           fprintf (f, "d=\"");
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");
238         }
239
240         fprintf (f, "</svg>");
241         fclose (f);
242       }
243       free (layers);
244     }
245
246     free (colors);
247   }
248 }
249
250 static void
251 dump_glyphs (hb_font_t *font, hb_draw_funcs_t *funcs, unsigned face_index)
252 {
253   unsigned num_glyphs = hb_face_get_glyph_count (hb_font_get_face (font));
254   for (unsigned gid = 0; gid < num_glyphs; ++gid)
255   {
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))
260     {
261       printf ("Skip gid: %d\n", gid);
262       continue;
263     }
264
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;
274     user_data.f = f;
275     if (!hb_font_draw_glyph (font, gid, funcs, &user_data))
276       printf ("Failed to decompose gid: %d\n", gid);
277     fprintf (f, "\"/></svg>");
278     fclose (f);
279   }
280 }
281
282 static void
283 dump_glyphs (hb_blob_t *blob, const char *font_name)
284 {
285   FILE *font_name_file = fopen ("out/.dumped_font_name", "r");
286   if (font_name_file)
287   {
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");
290     return;
291   }
292
293   font_name_file = fopen ("out/.dumped_font_name", "w");
294   if (!font_name_file)
295   {
296     fprintf (stderr, "./out is not accessible as a folder, create it please\n");
297     return;
298   }
299   fwrite (font_name, 1, strlen (font_name), font_name_file);
300   fclose (font_name_file);
301
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);
308
309   unsigned num_faces = hb_face_count (blob);
310   for (unsigned face_index = 0; face_index < num_faces; ++face_index)
311   {
312     hb_face_t *face = hb_face_create (blob, face_index);
313     hb_font_t *font = hb_font_create (face);
314
315     if (hb_ot_color_has_png (face))
316       printf ("Dumping png (CBDT/sbix)...\n");
317     png_dump (face, face_index);
318
319     if (hb_ot_color_has_svg (face))
320       printf ("Dumping svg (SVG )...\n");
321     svg_dump (face, face_index);
322
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);
326
327     dump_glyphs (font, funcs, face_index);
328
329     hb_font_destroy (font);
330     hb_face_destroy (face);
331   }
332
333   hb_draw_funcs_destroy (funcs);
334 }
335 #endif
336
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"
343
344 using namespace OT;
345
346 static void
347 print_layout_info_using_private_api (hb_blob_t *blob)
348 {
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)
353   {
354     printf ("Sanitization of the file wasn't successful. Exit");
355     exit (1);
356   }
357   const OpenTypeFontFile& ot = *sanitized;
358
359   switch (ot.get_tag ())
360   {
361   case OpenTypeFontFile::TrueTypeTag:
362     printf ("OpenType font with TrueType outlines\n");
363     break;
364   case OpenTypeFontFile::CFFTag:
365     printf ("OpenType font with CFF (Type1) outlines\n");
366     break;
367   case OpenTypeFontFile::TTCTag:
368     printf ("TrueType Collection of OpenType fonts\n");
369     break;
370   case OpenTypeFontFile::TrueTag:
371     printf ("Obsolete Apple TrueType font\n");
372     break;
373   case OpenTypeFontFile::Typ1Tag:
374     printf ("Obsolete Apple Type1 font in SFNT container\n");
375     break;
376   case OpenTypeFontFile::DFontTag:
377     printf ("DFont Mac Resource Fork\n");
378     break;
379   default:
380     printf ("Unknown font format\n");
381     break;
382   }
383
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)
387   {
388     const OpenTypeFontFace &font = ot.get_face (n_font);
389     printf ("Font %d of %d:\n", n_font, num_faces);
390
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)
394     {
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);
400
401       switch (table.tag)
402       {
403
404       case HB_OT_TAG_GSUB:
405       case HB_OT_TAG_GPOS:
406         {
407
408         const GSUBGPOS &g = *reinterpret_cast<const GSUBGPOS *> (font_data + table.offset);
409
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)
413         {
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));
417
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)
423           {
424             const LangSys &langsys = n_langsys == -1
425                                    ? script.get_default_lang_sys ()
426                                    : script.get_lang_sys (n_langsys);
427             if (n_langsys == -1)
428               printf ("      Default Language System\n");
429             else
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");
434             else
435               printf ("        Required feature index: %d\n",
436                       langsys.get_required_feature_index ());
437
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)
441             {
442               printf ("        Feature index %2d of %2d: %d\n", n_feature, num_features,
443                       langsys.get_feature_index (n_feature));
444             }
445           }
446         }
447
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)
451         {
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)));
456
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));
461           }
462         }
463
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)
467         {
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 ());
471         }
472
473         }
474         break;
475
476       case GDEF::tableTag:
477         {
478
479         const GDEF &gdef = *reinterpret_cast<const GDEF *> (font_data + table.offset);
480
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 ");
491         break;
492         }
493       }
494     }
495   }
496 }
497 /* end of private API use */
498 #endif
499
500 int
501 main (int argc, char **argv)
502 {
503   if (argc != 2)
504   {
505     fprintf (stderr, "usage: %s font-file.ttf\n", argv[0]);
506     exit (1);
507   }
508
509   hb_blob_t *blob = hb_blob_create_from_file_or_fail (argv[1]);
510   assert (blob);
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);
514 #endif
515 #if !defined(HB_NO_COLOR) && !defined(HB_NO_DRAW) && defined(HB_EXPERIMENTAL_API)
516   dump_glyphs (blob, argv[1]);
517 #endif
518   hb_blob_destroy (blob);
519
520   return 0;
521 }