Imported Upstream version 2.3.1
[platform/upstream/harfbuzz.git] / src / test-ot-color.cc
1 /*
2  * Copyright © 2018  Ebrahim Byagowi
3  * Copyright © 2018  Khaled Hosny
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
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.
12  *
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
17  * DAMAGE.
18  *
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.
24  */
25
26 #include "hb.h"
27 #include "hb-ot.h"
28
29 #include "hb-ft.h"
30
31 #include <ft2build.h>
32 #include FT_FREETYPE_H
33 #include FT_GLYPH_H
34
35 #include <cairo.h>
36 #include <cairo-ft.h>
37 #include <cairo-svg.h>
38
39 #include <stdlib.h>
40 #include <stdio.h>
41
42 static void
43 svg_dump (hb_face_t *face, unsigned int face_index)
44 {
45   unsigned glyph_count = hb_face_get_glyph_count (face);
46
47   for (unsigned int glyph_id = 0; glyph_id < glyph_count; glyph_id++)
48   {
49     hb_blob_t *blob = hb_ot_color_glyph_reference_svg (face, glyph_id);
50
51     if (hb_blob_get_length (blob) == 0) continue;
52
53     unsigned int length;
54     const char *data = hb_blob_get_data (blob, &length);
55
56     char output_path[255];
57     sprintf (output_path, "out/svg-%u-%u.svg%s",
58              glyph_id,
59              face_index,
60              // append "z" if the content is gzipped, https://stackoverflow.com/a/6059405
61              (length > 2 && (data[0] == '\x1F') && (data[1] == '\x8B')) ? "z" : "");
62
63     FILE *f = fopen (output_path, "wb");
64     fwrite (data, 1, length, f);
65     fclose (f);
66
67     hb_blob_destroy (blob);
68   }
69 }
70
71 /* _png API is so easy to use unlike the below code, don't get confused */
72 static void
73 png_dump (hb_face_t *face, unsigned int face_index)
74 {
75   unsigned glyph_count = hb_face_get_glyph_count (face);
76   hb_font_t *font = hb_font_create (face);
77
78   /* scans the font for strikes */
79   unsigned int sample_glyph_id;
80   /* we don't care about different strikes for different glyphs at this point */
81   for (sample_glyph_id = 0; sample_glyph_id < glyph_count; sample_glyph_id++)
82   {
83     hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, sample_glyph_id);
84     unsigned int blob_length = hb_blob_get_length (blob);
85     hb_blob_destroy (blob);
86     if (blob_length != 0)
87       break;
88   }
89
90   unsigned int upem = hb_face_get_upem (face);
91   unsigned int blob_length = 0;
92   unsigned int strike = 0;
93   for (unsigned int ppem = 1; ppem < upem; ppem++)
94   {
95     hb_font_set_ppem (font, ppem, ppem);
96     hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, sample_glyph_id);
97     unsigned int new_blob_length = hb_blob_get_length (blob);
98     hb_blob_destroy (blob);
99     if (new_blob_length != blob_length)
100     {
101       for (unsigned int glyph_id = 0; glyph_id < glyph_count; glyph_id++)
102       {
103         hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, glyph_id);
104
105         if (hb_blob_get_length (blob) == 0) continue;
106
107         unsigned int length;
108         const char *data = hb_blob_get_data (blob, &length);
109
110         char output_path[255];
111         sprintf (output_path, "out/png-%u-%u-%u.png", glyph_id, strike, face_index);
112
113         FILE *f = fopen (output_path, "wb");
114         fwrite (data, 1, length, f);
115         fclose (f);
116
117         hb_blob_destroy (blob);
118       }
119
120       strike++;
121       blob_length = new_blob_length;
122     }
123   }
124
125   hb_font_destroy (font);
126 }
127
128 static void
129 layered_glyph_dump (hb_face_t *face, cairo_font_face_t *cairo_face, unsigned int face_index)
130 {
131   unsigned int upem = hb_face_get_upem (face);
132
133   unsigned glyph_count = hb_face_get_glyph_count (face);
134   for (hb_codepoint_t gid = 0; gid < glyph_count; ++gid)
135   {
136     unsigned int num_layers = hb_ot_color_glyph_get_layers (face, gid, 0, NULL, NULL);
137     if (!num_layers)
138       continue;
139
140     hb_ot_color_layer_t *layers = (hb_ot_color_layer_t*) malloc (num_layers * sizeof (hb_ot_color_layer_t));
141
142     hb_ot_color_glyph_get_layers (face, gid, 0, &num_layers, layers);
143     if (num_layers)
144     {
145       // Measure
146       cairo_text_extents_t extents;
147       {
148         cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
149         cairo_t *cr = cairo_create (surface);
150         cairo_set_font_face (cr, cairo_face);
151         cairo_set_font_size (cr, upem);
152
153         cairo_glyph_t *glyphs = (cairo_glyph_t *) calloc (num_layers, sizeof (cairo_glyph_t));
154         for (unsigned int j = 0; j < num_layers; ++j)
155           glyphs[j].index = layers[j].glyph;
156         cairo_glyph_extents (cr, glyphs, num_layers, &extents);
157         free (glyphs);
158         cairo_surface_destroy (surface);
159         cairo_destroy (cr);
160       }
161
162       // Add a slight margin
163       extents.width += extents.width / 10;
164       extents.height += extents.height / 10;
165       extents.x_bearing -= extents.width / 20;
166       extents.y_bearing -= extents.height / 20;
167
168       // Render
169       unsigned int palette_count = hb_ot_color_palette_get_count (face);
170       for (unsigned int palette = 0; palette < palette_count; palette++)
171       {
172         unsigned int num_colors = hb_ot_color_palette_get_colors (face, palette, 0, NULL, NULL);
173         if (!num_colors)
174           continue;
175
176         hb_color_t *colors = (hb_color_t*) calloc (num_colors, sizeof (hb_color_t));
177         hb_ot_color_palette_get_colors (face, palette, 0, &num_colors, colors);
178         if (num_colors)
179         {
180           char output_path[255];
181           sprintf (output_path, "out/colr-%u-%u-%u.svg", gid, palette, face_index);
182
183           cairo_surface_t *surface = cairo_svg_surface_create (output_path, extents.width, extents.height);
184           cairo_t *cr = cairo_create (surface);
185           cairo_set_font_face (cr, cairo_face);
186           cairo_set_font_size (cr, upem);
187
188           for (unsigned int layer = 0; layer < num_layers; ++layer)
189           {
190             hb_color_t color = 0x000000FF;
191             if (layers[layer].color_index != 0xFFFF)
192               color = colors[layers[layer].color_index];
193             cairo_set_source_rgba (cr,
194                                    hb_color_get_red (color) / 255.,
195                                    hb_color_get_green (color) / 255.,
196                                    hb_color_get_blue (color) / 255.,
197                                    hb_color_get_alpha (color) / 255.);
198
199             cairo_glyph_t glyph;
200             glyph.index = layers[layer].glyph;
201             glyph.x = -extents.x_bearing;
202             glyph.y = -extents.y_bearing;
203             cairo_show_glyphs (cr, &glyph, 1);
204           }
205
206           cairo_surface_destroy (surface);
207           cairo_destroy (cr);
208         }
209         free (colors);
210       }
211     }
212
213     free (layers);
214   }
215 }
216
217 static void
218 dump_glyphs (cairo_font_face_t *cairo_face, unsigned int upem,
219              unsigned int num_glyphs, unsigned int face_index)
220 {
221   for (unsigned int i = 0; i < num_glyphs; ++i)
222   {
223     cairo_text_extents_t extents;
224     cairo_glyph_t glyph = {0};
225     glyph.index = i;
226
227     // Measure
228     {
229       cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
230       cairo_t *cr = cairo_create (surface);
231       cairo_set_font_face (cr, cairo_face);
232       cairo_set_font_size (cr, upem);
233
234       cairo_glyph_extents (cr, &glyph, 1, &extents);
235       cairo_surface_destroy (surface);
236       cairo_destroy (cr);
237     }
238
239     // Add a slight margin
240     extents.width += extents.width / 10;
241     extents.height += extents.height / 10;
242     extents.x_bearing -= extents.width / 20;
243     extents.y_bearing -= extents.height / 20;
244
245     // Render
246     {
247       char output_path[255];
248       sprintf (output_path, "out/%u-%u.svg", face_index, i);
249       cairo_surface_t *surface = cairo_svg_surface_create (output_path, extents.width, extents.height);
250       cairo_t *cr = cairo_create (surface);
251       cairo_set_font_face (cr, cairo_face);
252       cairo_set_font_size (cr, upem);
253       glyph.x = -extents.x_bearing;
254       glyph.y = -extents.y_bearing;
255       cairo_show_glyphs (cr, &glyph, 1);
256       cairo_surface_destroy (surface);
257       cairo_destroy (cr);
258     }
259   }
260 }
261
262 int
263 main (int argc, char **argv)
264 {
265   if (argc != 2) {
266     fprintf (stderr, "usage: %s font-file.ttf\n"
267                      "run it like `rm -rf out && mkdir out && %s font-file.ttf`\n",
268                      argv[0], argv[0]);
269     exit (1);
270   }
271
272
273   FILE *font_name_file = fopen ("out/.dumped_font_name", "r");
274   if (font_name_file != NULL)
275   {
276     fprintf (stderr, "Purge or move ./out folder in order to run a new dump\n");
277     exit (1);
278   }
279
280   font_name_file = fopen ("out/.dumped_font_name", "w");
281   if (font_name_file == NULL)
282   {
283     fprintf (stderr, "./out is not accessible as a folder, create it please\n");
284     exit (1);
285   }
286   fwrite (argv[1], 1, strlen (argv[1]), font_name_file);
287   fclose (font_name_file);
288
289   hb_blob_t *blob = hb_blob_create_from_file (argv[1]);
290   unsigned int num_faces = hb_face_count (blob);
291   if (num_faces == 0)
292   {
293     fprintf (stderr, "error: The file (%s) was corrupted, empty or not found", argv[1]);
294     exit (1);
295   }
296
297   for (unsigned int face_index = 0; face_index < hb_face_count (blob); face_index++)
298   {
299     hb_face_t *face = hb_face_create (blob, face_index);
300     hb_font_t *font = hb_font_create (face);
301
302     if (hb_ot_color_has_png (face)) printf ("Dumping png (cbdt/sbix)...\n");
303     png_dump (face, face_index);
304
305     if (hb_ot_color_has_svg (face)) printf ("Dumping svg...\n");
306     svg_dump (face, face_index);
307
308     cairo_font_face_t *cairo_face;
309     {
310       FT_Library library;
311       FT_Init_FreeType (&library);
312       FT_Face ft_face;
313       FT_New_Face (library, argv[1], 0, &ft_face);
314       cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
315     }
316     if (hb_ot_color_has_layers (face) && hb_ot_color_has_palettes (face))
317       printf ("Dumping layered color glyphs...\n");
318     layered_glyph_dump (face, cairo_face, face_index);
319
320     unsigned int num_glyphs = hb_face_get_glyph_count (face);
321     unsigned int upem = hb_face_get_upem (face);
322
323     // disabled when color font as cairo rendering of NotoColorEmoji is soooo slow
324     if (!hb_ot_color_has_layers (face) &&
325         !hb_ot_color_has_png (face) &&
326         !hb_ot_color_has_svg (face))
327       dump_glyphs (cairo_face, upem, num_glyphs, face_index);
328
329     hb_font_destroy (font);
330     hb_face_destroy (face);
331     }
332
333   hb_blob_destroy (blob);
334
335   return 0;
336 }