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