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