99e8ef9f3933f6841ae8b4679306e05d06ea462e
[platform/upstream/harfbuzz.git] / src / dump-emoji.cc
1 /*
2  * Copyright © 2018  Ebrahim Byagowi
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  */
24
25 #include "hb-static.cc"
26 #include "hb-ot-color-cbdt-table.hh"
27 #include "hb-ot-color-colr-table.hh"
28 #include "hb-ot-color-cpal-table.hh"
29 #include "hb-ot-color-sbix-table.hh"
30 #include "hb-ot-color-svg-table.hh"
31
32 #include "hb-ft.h"
33
34 #include <ft2build.h>
35 #include FT_FREETYPE_H
36 #include FT_GLYPH_H
37
38 #include <cairo.h>
39 #include <cairo-ft.h>
40 #include <cairo-svg.h>
41
42 #ifdef HAVE_GLIB
43 #include <glib.h>
44 #endif
45 #include <stdlib.h>
46 #include <stdio.h>
47
48 void cbdt_callback (const uint8_t* data, unsigned int length,
49                     unsigned int group, unsigned int gid)
50 {
51   char output_path[255];
52   sprintf (output_path, "out/cbdt-%d-%d.png", group, gid);
53   FILE *f = fopen (output_path, "wb");
54   fwrite (data, 1, length, f);
55   fclose (f);
56 }
57
58 void sbix_callback (const uint8_t* data, unsigned int length,
59                     unsigned int group, unsigned int gid)
60 {
61   char output_path[255];
62   sprintf (output_path, "out/sbix-%d-%d.png", group, gid);
63   FILE *f = fopen (output_path, "wb");
64   fwrite (data, 1, length, f);
65   fclose (f);
66 }
67
68 void svg_callback (const uint8_t* data, unsigned int length,
69                    unsigned int start_glyph, unsigned int end_glyph)
70 {
71   char output_path[255];
72   if (start_glyph == end_glyph)
73     sprintf (output_path, "out/svg-%d.svg", start_glyph);
74   else
75     sprintf (output_path, "out/svg-%d-%d.svg", start_glyph, end_glyph);
76
77   // append "z" if the content is gzipped
78   if ((data[0] == 0x1F) && (data[1] == 0x8B))
79     strcat (output_path, "z");
80
81   FILE *f = fopen (output_path, "wb");
82   fwrite (data, 1, length, f);
83   fclose (f);
84 }
85
86 void colr_cpal_rendering (cairo_font_face_t *cairo_face, unsigned int upem, unsigned int num_glyphs,
87                           const OT::COLR *colr, const OT::CPAL *cpal)
88 {
89   for (unsigned int i = 0; i < num_glyphs; ++i)
90   {
91     unsigned int first_layer_index, num_layers;
92     if (colr->get_base_glyph_record (i, &first_layer_index, &num_layers))
93     {
94       // Measure
95       cairo_text_extents_t extents;
96       {
97         cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
98         cairo_t *cr = cairo_create (surface);
99         cairo_set_font_face (cr, cairo_face);
100         cairo_set_font_size (cr, upem);
101
102         cairo_glyph_t *glyphs = (cairo_glyph_t *) calloc (num_layers, sizeof (cairo_glyph_t));
103         for (unsigned int j = 0; j < num_layers; ++j)
104         {
105           hb_codepoint_t glyph_id;
106           unsigned int color_index;
107           colr->get_layer_record (first_layer_index + j, &glyph_id, &color_index);
108           glyphs[j].index = glyph_id;
109         }
110         cairo_glyph_extents (cr, glyphs, num_layers, &extents);
111         free (glyphs);
112         cairo_surface_destroy (surface);
113         cairo_destroy (cr);
114       }
115
116       // Add a slight margin
117       extents.width += extents.width / 10;
118       extents.height += extents.height / 10;
119       extents.x_bearing -= extents.width / 20;
120       extents.y_bearing -= extents.height / 20;
121
122       // Render
123       unsigned int pallet_count = cpal->get_palette_count ();
124       for (unsigned int pallet = 0; pallet < pallet_count; ++pallet) {
125         char output_path[255];
126
127         // If we have more than one pallet, use a better namin
128         if (pallet_count == 1)
129           sprintf (output_path, "out/colr-%d.svg", i);
130         else
131           sprintf (output_path, "out/colr-%d-%d.svg", i, pallet);
132
133         cairo_surface_t *surface = cairo_svg_surface_create (output_path, extents.width, extents.height);
134         cairo_t *cr = cairo_create (surface);
135         cairo_set_font_face (cr, cairo_face);
136         cairo_set_font_size (cr, upem);
137
138         for (unsigned int j = 0; j < num_layers; ++j)
139         {
140           hb_codepoint_t glyph_id;
141           unsigned int color_index;
142           colr->get_layer_record (first_layer_index + j, &glyph_id, &color_index);
143
144           uint32_t color = cpal->get_color_record_argb (color_index, pallet);
145           int alpha = color & 0xFF;
146           int r = (color >> 8) & 0xFF;
147           int g = (color >> 16) & 0xFF;
148           int b = (color >> 24) & 0xFF;
149           cairo_set_source_rgba (cr, r / 255.f, g / 255.f, b / 255.f, alpha);
150
151           cairo_glyph_t glyph;
152           glyph.index = glyph_id;
153           glyph.x = -extents.x_bearing;
154           glyph.y = -extents.y_bearing;
155           cairo_show_glyphs (cr, &glyph, 1);
156         }
157
158         cairo_surface_destroy (surface);
159         cairo_destroy (cr);
160       }
161     }
162   }
163 }
164
165 void dump_glyphs (cairo_font_face_t *cairo_face, unsigned int upem, unsigned int num_glyphs)
166 {
167   // Dump every glyph available on the font
168   return; // disabled for now
169   for (unsigned int i = 0; i < num_glyphs; ++i)
170   {
171     cairo_text_extents_t extents;
172     cairo_glyph_t glyph = {0};
173     glyph.index = i;
174
175     // Measure
176     {
177       cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
178       cairo_t *cr = cairo_create (surface);
179       cairo_set_font_face (cr, cairo_face);
180       cairo_set_font_size (cr, upem);
181
182       cairo_glyph_extents (cr, &glyph, 1, &extents);
183       cairo_surface_destroy (surface);
184       cairo_destroy (cr);
185     }
186
187     // Add a slight margin
188     extents.width += extents.width / 10;
189     extents.height += extents.height / 10;
190     extents.x_bearing -= extents.width / 20;
191     extents.y_bearing -= extents.height / 20;
192
193     // Render
194     {
195       char output_path[255];
196       sprintf (output_path, "out/%d.svg", i);
197       cairo_surface_t *surface = cairo_svg_surface_create (output_path, extents.width, extents.height);
198       cairo_t *cr = cairo_create (surface);
199       cairo_set_font_face (cr, cairo_face);
200       cairo_set_font_size (cr, upem);
201       glyph.x = -extents.x_bearing;
202       glyph.y = -extents.y_bearing;
203       cairo_show_glyphs (cr, &glyph, 1);
204       cairo_surface_destroy (surface);
205       cairo_destroy (cr);
206     }
207   }
208 }
209
210 int main (int argc, char **argv)
211 {
212   if (argc != 2) {
213     fprintf (stderr, "usage: %s font-file.ttf\n", argv[0]);
214     exit (1);
215   }
216
217   hb_blob_t *blob = hb_blob_create_from_file (argv[1]);
218   hb_face_t *face = hb_face_create (blob, 0);
219   hb_font_t *font = hb_font_create (face);
220
221   OT::CBDT::accelerator_t cbdt;
222   cbdt.init (face);
223   cbdt.dump (cbdt_callback);
224   cbdt.fini ();
225
226   OT::sbix::accelerator_t sbix;
227   sbix.init (face);
228   sbix.dump (sbix_callback);
229   sbix.fini ();
230
231   OT::SVG::accelerator_t svg;
232   svg.init (face);
233   svg.dump (svg_callback);
234   svg.fini ();
235
236   OT::Sanitizer<OT::COLR> sanitizerCOLR;
237   hb_blob_t* colr_blob = sanitizerCOLR.sanitize (face->reference_table (HB_OT_TAG_COLR));
238   const OT::COLR *colr = colr_blob->as<OT::COLR> ();
239
240   OT::Sanitizer<OT::CPAL> sanitizerCPAL;
241   hb_blob_t* cpal_blob = sanitizerCPAL.sanitize (face->reference_table (HB_OT_TAG_CPAL));
242   const OT::CPAL *cpal = cpal_blob->as<OT::CPAL> ();
243
244   cairo_font_face_t *cairo_face;
245   {
246     FT_Library library;
247     FT_Init_FreeType (&library);
248     FT_Face ftface;
249     FT_New_Face (library, argv[1], 0, &ftface);
250     cairo_face = cairo_ft_font_face_create_for_ft_face (ftface, 0);
251   }
252   unsigned int num_glyphs = hb_face_get_glyph_count (face);
253   unsigned int upem = hb_face_get_upem (face);
254   colr_cpal_rendering (cairo_face, upem, num_glyphs, colr, cpal);
255   dump_glyphs (cairo_face, upem, num_glyphs);
256
257
258   hb_font_destroy (font);
259   hb_face_destroy (face);
260   hb_blob_destroy (blob);
261
262   return 0;
263 }