2 * Copyright © 2018 Ebrahim Byagowi
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
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"
35 #include FT_FREETYPE_H
40 #include <cairo-svg.h>
48 void cbdt_callback (const uint8_t* data, unsigned int length,
49 unsigned int group, unsigned int gid)
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);
58 void sbix_callback (const uint8_t* data, unsigned int length,
59 unsigned int group, unsigned int gid)
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);
68 void svg_callback (const uint8_t* data, unsigned int length,
69 unsigned int start_glyph, unsigned int end_glyph)
71 char output_path[255];
72 if (start_glyph == end_glyph)
73 sprintf (output_path, "out/svg-%d.svg", start_glyph);
75 sprintf (output_path, "out/svg-%d-%d.svg", start_glyph, end_glyph);
77 // append "z" if the content is gzipped
78 if ((data[0] == 0x1F) && (data[1] == 0x8B))
79 strcat (output_path, "z");
81 FILE *f = fopen (output_path, "wb");
82 fwrite (data, 1, length, f);
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)
89 for (unsigned int i = 0; i < num_glyphs; ++i)
91 unsigned int first_layer_index, num_layers;
92 if (colr->get_base_glyph_record (i, &first_layer_index, &num_layers))
95 cairo_text_extents_t extents;
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);
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)
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;
110 cairo_glyph_extents (cr, glyphs, num_layers, &extents);
112 cairo_surface_destroy (surface);
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;
123 unsigned int pallet_count = cpal->get_palette_count ();
124 for (unsigned int pallet = 0; pallet < pallet_count; ++pallet) {
125 char output_path[255];
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);
131 sprintf (output_path, "out/colr-%d-%d.svg", i, pallet);
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);
138 for (unsigned int j = 0; j < num_layers; ++j)
140 hb_codepoint_t glyph_id;
141 unsigned int color_index;
142 colr->get_layer_record (first_layer_index + j, &glyph_id, &color_index);
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);
152 glyph.index = glyph_id;
153 glyph.x = -extents.x_bearing;
154 glyph.y = -extents.y_bearing;
155 cairo_show_glyphs (cr, &glyph, 1);
158 cairo_surface_destroy (surface);
165 void dump_glyphs (cairo_font_face_t *cairo_face, unsigned int upem, unsigned int num_glyphs)
167 // Dump every glyph available on the font
168 return; // disabled for now
169 for (unsigned int i = 0; i < num_glyphs; ++i)
171 cairo_text_extents_t extents;
172 cairo_glyph_t glyph = {0};
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);
182 cairo_glyph_extents (cr, &glyph, 1, &extents);
183 cairo_surface_destroy (surface);
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;
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);
210 int main (int argc, char **argv)
213 fprintf (stderr, "usage: %s font-file.ttf\n", argv[0]);
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);
221 OT::CBDT::accelerator_t cbdt;
223 cbdt.dump (cbdt_callback);
226 OT::sbix::accelerator_t sbix;
228 sbix.dump (sbix_callback);
231 OT::SVG::accelerator_t svg;
233 svg.dump (svg_callback);
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> ();
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> ();
244 cairo_font_face_t *cairo_face;
247 FT_Init_FreeType (&library);
249 FT_New_Face (library, argv[1], 0, &ftface);
250 cairo_face = cairo_ft_font_face_create_for_ft_face (ftface, 0);
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);
258 hb_font_destroy (font);
259 hb_face_destroy (face);
260 hb_blob_destroy (blob);