Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 2409d5f | 2011-04-21 17:14:28 -0400 | [diff] [blame] | 2 | * Copyright © 2007,2008,2009 Red Hat, Inc. |
Ebrahim Byagowi | 23277be | 2020-01-24 18:49:48 +0330 | [diff] [blame] | 3 | * Copyright © 2018,2019,2020 Ebrahim Byagowi |
| 4 | * Copyright © 2018 Khaled Hosny |
Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 5 | * |
Behdad Esfahbod | c755cb3 | 2010-04-22 00:11:43 -0400 | [diff] [blame] | 6 | * This is part of HarfBuzz, a text shaping library. |
Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 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 | |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 29 | #include "hb.h" |
| 30 | #include "hb-ot.h" |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 31 | |
| 32 | #include <stdlib.h> |
| 33 | #include <stdio.h> |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 34 | #include <string.h> |
Behdad Esfahbod | 7c8e844 | 2012-08-28 17:57:49 -0400 | [diff] [blame] | 35 | |
Ebrahim Byagowi | bb4cdf8 | 2019-06-25 01:42:42 +0430 | [diff] [blame] | 36 | #ifdef HB_NO_OPEN |
| 37 | #define hb_blob_create_from_file(x) hb_blob_get_empty () |
| 38 | #endif |
| 39 | |
Ebrahim Byagowi | 23277be | 2020-01-24 18:49:48 +0330 | [diff] [blame] | 40 | static void |
| 41 | svg_dump (hb_face_t *face, unsigned face_index) |
| 42 | { |
| 43 | unsigned glyph_count = hb_face_get_glyph_count (face); |
| 44 | |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 45 | for (unsigned glyph_id = 0; glyph_id < glyph_count; ++glyph_id) |
Ebrahim Byagowi | 23277be | 2020-01-24 18:49:48 +0330 | [diff] [blame] | 46 | { |
| 47 | hb_blob_t *blob = hb_ot_color_glyph_reference_svg (face, glyph_id); |
| 48 | |
| 49 | if (hb_blob_get_length (blob) == 0) continue; |
| 50 | |
| 51 | unsigned length; |
| 52 | const char *data = hb_blob_get_data (blob, &length); |
| 53 | |
| 54 | char output_path[255]; |
| 55 | sprintf (output_path, "out/svg-%u-%u.svg%s", |
| 56 | glyph_id, |
| 57 | face_index, |
| 58 | // append "z" if the content is gzipped, https://stackoverflow.com/a/6059405 |
| 59 | (length > 2 && (data[0] == '\x1F') && (data[1] == '\x8B')) ? "z" : ""); |
| 60 | |
| 61 | FILE *f = fopen (output_path, "wb"); |
| 62 | fwrite (data, 1, length, f); |
| 63 | fclose (f); |
| 64 | |
| 65 | hb_blob_destroy (blob); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /* _png API is so easy to use unlike the below code, don't get confused */ |
| 70 | static void |
| 71 | png_dump (hb_face_t *face, unsigned face_index) |
| 72 | { |
| 73 | unsigned glyph_count = hb_face_get_glyph_count (face); |
| 74 | hb_font_t *font = hb_font_create (face); |
| 75 | |
| 76 | /* scans the font for strikes */ |
| 77 | unsigned sample_glyph_id; |
| 78 | /* we don't care about different strikes for different glyphs at this point */ |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 79 | for (sample_glyph_id = 0; sample_glyph_id < glyph_count; ++sample_glyph_id) |
Ebrahim Byagowi | 23277be | 2020-01-24 18:49:48 +0330 | [diff] [blame] | 80 | { |
| 81 | hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, sample_glyph_id); |
| 82 | unsigned blob_length = hb_blob_get_length (blob); |
| 83 | hb_blob_destroy (blob); |
| 84 | if (blob_length != 0) |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | unsigned upem = hb_face_get_upem (face); |
| 89 | unsigned blob_length = 0; |
| 90 | unsigned strike = 0; |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 91 | for (unsigned ppem = 1; ppem < upem; ++ppem) |
Ebrahim Byagowi | 23277be | 2020-01-24 18:49:48 +0330 | [diff] [blame] | 92 | { |
| 93 | hb_font_set_ppem (font, ppem, ppem); |
| 94 | hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, sample_glyph_id); |
| 95 | unsigned new_blob_length = hb_blob_get_length (blob); |
| 96 | hb_blob_destroy (blob); |
| 97 | if (new_blob_length != blob_length) |
| 98 | { |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 99 | for (unsigned glyph_id = 0; glyph_id < glyph_count; ++glyph_id) |
Ebrahim Byagowi | 23277be | 2020-01-24 18:49:48 +0330 | [diff] [blame] | 100 | { |
| 101 | hb_blob_t *blob = hb_ot_color_glyph_reference_png (font, glyph_id); |
| 102 | |
| 103 | if (hb_blob_get_length (blob) == 0) continue; |
| 104 | |
| 105 | unsigned length; |
| 106 | const char *data = hb_blob_get_data (blob, &length); |
| 107 | |
| 108 | char output_path[255]; |
| 109 | sprintf (output_path, "out/png-%u-%u-%u.png", glyph_id, strike, face_index); |
| 110 | |
| 111 | FILE *f = fopen (output_path, "wb"); |
| 112 | fwrite (data, 1, length, f); |
| 113 | fclose (f); |
| 114 | |
| 115 | hb_blob_destroy (blob); |
| 116 | } |
| 117 | |
| 118 | strike++; |
| 119 | blob_length = new_blob_length; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | hb_font_destroy (font); |
| 124 | } |
| 125 | |
| 126 | struct user_data_t |
| 127 | { |
| 128 | FILE *f; |
| 129 | hb_position_t ascender; |
| 130 | }; |
| 131 | |
| 132 | static void |
| 133 | move_to (hb_position_t to_x, hb_position_t to_y, user_data_t &user_data) |
| 134 | { |
| 135 | fprintf (user_data.f, "M%d,%d", to_x, user_data.ascender - to_y); |
| 136 | } |
| 137 | |
| 138 | static void |
| 139 | line_to (hb_position_t to_x, hb_position_t to_y, user_data_t &user_data) |
| 140 | { |
| 141 | fprintf (user_data.f, "L%d,%d", to_x, user_data.ascender - to_y); |
| 142 | } |
| 143 | |
| 144 | static void |
| 145 | conic_to (hb_position_t control_x, hb_position_t control_y, |
| 146 | hb_position_t to_x, hb_position_t to_y, |
| 147 | user_data_t &user_data) |
| 148 | { |
| 149 | fprintf (user_data.f, "Q%d,%d %d,%d", control_x, user_data.ascender - control_y, |
| 150 | to_x, user_data.ascender - to_y); |
| 151 | } |
| 152 | |
| 153 | static void |
| 154 | cubic_to (hb_position_t control1_x, hb_position_t control1_y, |
| 155 | hb_position_t control2_x, hb_position_t control2_y, |
| 156 | hb_position_t to_x, hb_position_t to_y, |
| 157 | user_data_t &user_data) |
| 158 | { |
| 159 | fprintf (user_data.f, "C%d,%d %d,%d %d,%d", control1_x, user_data.ascender - control1_y, |
| 160 | control2_x, user_data.ascender - control2_y, |
| 161 | to_x, user_data.ascender - to_y); |
| 162 | } |
| 163 | |
| 164 | static void |
| 165 | close_path (user_data_t &user_data) |
| 166 | { |
| 167 | fprintf (user_data.f, "Z"); |
| 168 | } |
| 169 | |
| 170 | static void |
| 171 | layered_glyph_dump (hb_font_t *font, hb_ot_glyph_decompose_funcs_t *funcs, unsigned face_index) |
| 172 | { |
| 173 | hb_face_t *face = hb_font_get_face (font); |
| 174 | unsigned num_glyphs = hb_face_get_glyph_count (face); |
| 175 | for (hb_codepoint_t gid = 0; gid < num_glyphs; ++gid) |
| 176 | { |
| 177 | unsigned num_layers = hb_ot_color_glyph_get_layers (face, gid, 0, nullptr, nullptr); |
| 178 | if (!num_layers) continue; |
| 179 | |
| 180 | hb_ot_color_layer_t *layers = (hb_ot_color_layer_t*) malloc (num_layers * sizeof (hb_ot_color_layer_t)); |
| 181 | |
| 182 | hb_ot_color_glyph_get_layers (face, gid, 0, &num_layers, layers); |
| 183 | if (num_layers) |
| 184 | { |
| 185 | hb_font_extents_t font_extents; |
| 186 | hb_font_get_extents_for_direction (font, HB_DIRECTION_LTR, &font_extents); |
| 187 | hb_glyph_extents_t extents = {0}; |
| 188 | if (!hb_font_get_glyph_extents (font, gid, &extents)) |
| 189 | { |
| 190 | printf ("Skip gid: %d\n", gid); |
| 191 | continue; |
| 192 | } |
| 193 | |
| 194 | unsigned palette_count = hb_ot_color_palette_get_count (face); |
| 195 | for (unsigned palette = 0; palette < palette_count; ++palette) |
| 196 | { |
| 197 | unsigned num_colors = hb_ot_color_palette_get_colors (face, palette, 0, nullptr, nullptr); |
| 198 | if (!num_colors) |
| 199 | continue; |
| 200 | |
| 201 | char output_path[255]; |
| 202 | sprintf (output_path, "out/colr-%u-%u-%u.svg", gid, palette, face_index); |
| 203 | FILE *f = fopen (output_path, "wb"); |
| 204 | fprintf (f, "<svg xmlns=\"http://www.w3.org/2000/svg\"" |
| 205 | " viewBox=\"%d %d %d %d\">\n", |
| 206 | extents.x_bearing, 0, |
| 207 | extents.x_bearing + extents.width, -extents.height); |
| 208 | user_data_t user_data; |
| 209 | user_data.ascender = extents.y_bearing; |
| 210 | user_data.f = f; |
| 211 | |
| 212 | hb_color_t *colors = (hb_color_t*) calloc (num_colors, sizeof (hb_color_t)); |
| 213 | hb_ot_color_palette_get_colors (face, palette, 0, &num_colors, colors); |
| 214 | if (num_colors) |
| 215 | { |
| 216 | for (unsigned layer = 0; layer < num_layers; ++layer) |
| 217 | { |
| 218 | hb_color_t color = 0x000000FF; |
| 219 | if (layers[layer].color_index != 0xFFFF) |
| 220 | color = colors[layers[layer].color_index]; |
| 221 | fprintf (f, "<path fill=\"#%02X%02X%02X\" ", |
| 222 | hb_color_get_red (color), hb_color_get_green (color), hb_color_get_green (color)); |
| 223 | if (hb_color_get_alpha (color) != 255) |
| 224 | fprintf (f, "fill-opacity=\"%.3f\"", (double) hb_color_get_alpha (color) / 255.); |
| 225 | fprintf (f, "d=\""); |
| 226 | if (!hb_ot_glyph_decompose (font, layers[layer].glyph, funcs, &user_data)) |
| 227 | printf ("Failed to decompose layer %d while %d\n", layers[layer].glyph, gid); |
| 228 | fprintf (f, "\"/>\n"); |
| 229 | } |
| 230 | } |
| 231 | free (colors); |
| 232 | |
| 233 | fprintf (f, "</svg>"); |
| 234 | fclose (f); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | |
| 239 | free (layers); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | static void |
| 244 | dump_glyphs (hb_font_t *font, hb_ot_glyph_decompose_funcs_t *funcs, unsigned face_index) |
| 245 | { |
| 246 | unsigned num_glyphs = hb_face_get_glyph_count (hb_font_get_face (font)); |
| 247 | for (unsigned gid = 0; gid < num_glyphs; ++gid) |
| 248 | { |
| 249 | hb_font_extents_t font_extents; |
| 250 | hb_font_get_extents_for_direction (font, HB_DIRECTION_LTR, &font_extents); |
| 251 | hb_glyph_extents_t extents = {0}; |
| 252 | if (!hb_font_get_glyph_extents (font, gid, &extents)) |
| 253 | { |
| 254 | printf ("Skip gid: %d\n", gid); |
| 255 | continue; |
| 256 | } |
| 257 | |
| 258 | char output_path[255]; |
| 259 | sprintf (output_path, "out/%u-%u.svg", face_index, gid); |
| 260 | FILE *f = fopen (output_path, "wb"); |
| 261 | fprintf (f, "<svg xmlns=\"http://www.w3.org/2000/svg\"" |
| 262 | " viewBox=\"%d %d %d %d\"><path d=\"", |
| 263 | extents.x_bearing, 0, |
| 264 | extents.x_bearing + extents.width, font_extents.ascender - font_extents.descender); |
| 265 | user_data_t user_data; |
| 266 | user_data.ascender = font_extents.ascender; |
| 267 | user_data.f = f; |
| 268 | if (!hb_ot_glyph_decompose (font, gid, funcs, &user_data)) |
| 269 | printf ("Failed to decompose gid: %d\n", gid); |
| 270 | fprintf (f, "\"/></svg>"); |
| 271 | fclose (f); |
| 272 | } |
| 273 | } |
| 274 | |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 275 | static void |
| 276 | dump_glyphs (hb_blob_t *blob, const char *font_name) |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 277 | { |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 278 | FILE *font_name_file = fopen ("out/.dumped_font_name", "r"); |
| 279 | if (font_name_file != nullptr) |
Ebrahim Byagowi | 0558413 | 2019-10-01 13:49:55 +0330 | [diff] [blame] | 280 | { |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 281 | fprintf (stderr, "Purge or move ./out folder in order to run a new glyph dump,\n" |
| 282 | "run it like `rm -rf out && mkdir out && src/main font-file.ttf`\n"); |
| 283 | return; |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 284 | } |
| 285 | |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 286 | font_name_file = fopen ("out/.dumped_font_name", "w"); |
| 287 | if (font_name_file == nullptr) |
| 288 | { |
| 289 | fprintf (stderr, "./out is not accessible as a folder, create it please\n"); |
| 290 | return; |
| 291 | } |
| 292 | fwrite (font_name, 1, strlen (font_name), font_name_file); |
| 293 | fclose (font_name_file); |
Behdad Esfahbod | ce48f03 | 2009-11-02 14:35:51 -0500 | [diff] [blame] | 294 | |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 295 | hb_ot_glyph_decompose_funcs_t *funcs = hb_ot_glyph_decompose_funcs_create (); |
| 296 | hb_ot_glyph_decompose_funcs_set_move_to_func (funcs, (hb_ot_glyph_decompose_move_to_func_t) move_to); |
| 297 | hb_ot_glyph_decompose_funcs_set_line_to_func (funcs, (hb_ot_glyph_decompose_line_to_func_t) line_to); |
| 298 | hb_ot_glyph_decompose_funcs_set_conic_to_func (funcs, (hb_ot_glyph_decompose_conic_to_func_t) conic_to); |
| 299 | hb_ot_glyph_decompose_funcs_set_cubic_to_func (funcs, (hb_ot_glyph_decompose_cubic_to_func_t) cubic_to); |
| 300 | hb_ot_glyph_decompose_funcs_set_close_path_func (funcs, (hb_ot_glyph_decompose_close_path_func_t) close_path); |
| 301 | |
| 302 | unsigned num_faces = hb_face_count (blob); |
| 303 | for (unsigned face_index = 0; face_index < num_faces; ++face_index) |
| 304 | { |
| 305 | hb_face_t *face = hb_face_create (blob, face_index); |
| 306 | hb_font_t *font = hb_font_create (face); |
| 307 | |
| 308 | if (hb_ot_color_has_png (face)) |
| 309 | printf ("Dumping png (CBDT/sbix)...\n"); |
| 310 | png_dump (face, face_index); |
| 311 | |
| 312 | if (hb_ot_color_has_svg (face)) |
| 313 | printf ("Dumping svg (SVG )...\n"); |
| 314 | svg_dump (face, face_index); |
| 315 | |
| 316 | if (hb_ot_color_has_layers (face) && hb_ot_color_has_palettes (face)) |
| 317 | printf ("Dumping layered color glyphs (COLR/CPAL)...\n"); |
| 318 | layered_glyph_dump (font, funcs, face_index); |
| 319 | |
| 320 | dump_glyphs (font, funcs, face_index); |
| 321 | |
| 322 | hb_font_destroy (font); |
| 323 | hb_face_destroy (face); |
| 324 | } |
| 325 | |
| 326 | hb_ot_glyph_decompose_funcs_destroy (funcs); |
| 327 | } |
| 328 | |
| 329 | /* Only this part of this mini app uses private API */ |
| 330 | #include "hb-static.cc" |
| 331 | #include "hb-open-file.hh" |
| 332 | #include "hb-ot-layout-gdef-table.hh" |
| 333 | #include "hb-ot-layout-gsubgpos.hh" |
| 334 | |
| 335 | using namespace OT; |
| 336 | |
| 337 | static void |
| 338 | print_layout_info_using_private_api (hb_blob_t *blob) |
| 339 | { |
| 340 | const char *font_data = hb_blob_get_data (blob, nullptr); |
| 341 | hb_blob_t *font_blob = hb_sanitize_context_t ().sanitize_blob<OpenTypeFontFile> (blob); |
Behdad Esfahbod | eba1c16 | 2018-05-08 02:47:42 -0700 | [diff] [blame] | 342 | const OpenTypeFontFile* sanitized = font_blob->as<OpenTypeFontFile> (); |
Behdad Esfahbod | b912fbe | 2018-08-06 06:30:12 -0700 | [diff] [blame] | 343 | if (!font_blob->data) |
Ebrahim Byagowi | c55aa14 | 2018-04-18 00:01:20 +0430 | [diff] [blame] | 344 | { |
| 345 | printf ("Sanitization of the file wasn't successful. Exit"); |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 346 | exit (1); |
Ebrahim Byagowi | c55aa14 | 2018-04-18 00:01:20 +0430 | [diff] [blame] | 347 | } |
| 348 | const OpenTypeFontFile& ot = *sanitized; |
| 349 | |
Ebrahim Byagowi | 0558413 | 2019-10-01 13:49:55 +0330 | [diff] [blame] | 350 | switch (ot.get_tag ()) |
| 351 | { |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 352 | case OpenTypeFontFile::TrueTypeTag: |
| 353 | printf ("OpenType font with TrueType outlines\n"); |
| 354 | break; |
| 355 | case OpenTypeFontFile::CFFTag: |
| 356 | printf ("OpenType font with CFF (Type1) outlines\n"); |
| 357 | break; |
| 358 | case OpenTypeFontFile::TTCTag: |
| 359 | printf ("TrueType Collection of OpenType fonts\n"); |
| 360 | break; |
Behdad Esfahbod | ce5694c | 2010-05-04 14:10:18 -0400 | [diff] [blame] | 361 | case OpenTypeFontFile::TrueTag: |
| 362 | printf ("Obsolete Apple TrueType font\n"); |
| 363 | break; |
| 364 | case OpenTypeFontFile::Typ1Tag: |
| 365 | printf ("Obsolete Apple Type1 font in SFNT container\n"); |
| 366 | break; |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 367 | case OpenTypeFontFile::DFontTag: |
| 368 | printf ("DFont Mac Resource Fork\n"); |
| 369 | break; |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 370 | default: |
| 371 | printf ("Unknown font format\n"); |
| 372 | break; |
| 373 | } |
| 374 | |
Ebrahim Byagowi | 23277be | 2020-01-24 18:49:48 +0330 | [diff] [blame] | 375 | unsigned num_faces = hb_face_count (blob); |
| 376 | printf ("%d font(s) found in file\n", num_faces); |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 377 | for (unsigned n_font = 0; n_font < num_faces; ++n_font) |
Ebrahim Byagowi | 0558413 | 2019-10-01 13:49:55 +0330 | [diff] [blame] | 378 | { |
Behdad Esfahbod | 54e5aac | 2008-01-27 21:19:51 -0500 | [diff] [blame] | 379 | const OpenTypeFontFace &font = ot.get_face (n_font); |
Ebrahim Byagowi | 23277be | 2020-01-24 18:49:48 +0330 | [diff] [blame] | 380 | printf ("Font %d of %d:\n", n_font, num_faces); |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 381 | |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 382 | unsigned num_tables = font.get_table_count (); |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 383 | printf (" %d table(s) found in font\n", num_tables); |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 384 | for (unsigned n_table = 0; n_table < num_tables; ++n_table) |
Ebrahim Byagowi | 0558413 | 2019-10-01 13:49:55 +0330 | [diff] [blame] | 385 | { |
Behdad Esfahbod | 54e5aac | 2008-01-27 21:19:51 -0500 | [diff] [blame] | 386 | const OpenTypeTable &table = font.get_table (n_table); |
Behdad Esfahbod | 15164d9 | 2009-08-04 13:57:41 -0400 | [diff] [blame] | 387 | printf (" Table %2d of %2d: %.4s (0x%08x+0x%08x)\n", n_table, num_tables, |
Ebrahim Byagowi | c55aa14 | 2018-04-18 00:01:20 +0430 | [diff] [blame] | 388 | (const char *) table.tag, |
Ebrahim Byagowi | 23277be | 2020-01-24 18:49:48 +0330 | [diff] [blame] | 389 | (unsigned) table.offset, |
| 390 | (unsigned) table.length); |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 391 | |
Ebrahim Byagowi | 0558413 | 2019-10-01 13:49:55 +0330 | [diff] [blame] | 392 | switch (table.tag) |
| 393 | { |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 394 | |
Behdad Esfahbod | a130ee6 | 2017-11-14 20:30:03 -0800 | [diff] [blame] | 395 | case HB_OT_TAG_GSUB: |
| 396 | case HB_OT_TAG_GPOS: |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 397 | { |
| 398 | |
Behdad Esfahbod | b84ceb2 | 2019-12-10 13:02:48 -0600 | [diff] [blame] | 399 | const GSUBGPOS &g = *reinterpret_cast<const GSUBGPOS *> (font_data + table.offset); |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 400 | |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 401 | unsigned num_scripts = g.get_script_count (); |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 402 | printf (" %d script(s) found in table\n", num_scripts); |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 403 | for (unsigned n_script = 0; n_script < num_scripts; ++n_script) |
Ebrahim Byagowi | 0558413 | 2019-10-01 13:49:55 +0330 | [diff] [blame] | 404 | { |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 405 | const Script &script = g.get_script (n_script); |
| 406 | printf (" Script %2d of %2d: %.4s\n", n_script, num_scripts, |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 407 | (const char *) g.get_script_tag (n_script)); |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 408 | |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 409 | if (!script.has_default_lang_sys ()) |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 410 | printf (" No default language system\n"); |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 411 | int num_langsys = script.get_lang_sys_count (); |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 412 | printf (" %d language system(s) found in script\n", num_langsys); |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 413 | for (int n_langsys = script.has_default_lang_sys () ? -1 : 0; n_langsys < num_langsys; ++n_langsys) |
| 414 | { |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 415 | const LangSys &langsys = n_langsys == -1 |
| 416 | ? script.get_default_lang_sys () |
| 417 | : script.get_lang_sys (n_langsys); |
Behdad Esfahbod | 1a2a4a0 | 2012-05-05 22:38:20 +0200 | [diff] [blame] | 418 | if (n_langsys == -1) |
| 419 | printf (" Default Language System\n"); |
| 420 | else |
| 421 | printf (" Language System %2d of %2d: %.4s\n", n_langsys, num_langsys, |
Ebrahim Byagowi | 23277be | 2020-01-24 18:49:48 +0330 | [diff] [blame] | 422 | (const char *) script.get_lang_sys_tag (n_langsys)); |
Behdad Esfahbod | 0ddecab | 2014-05-01 16:01:40 -0700 | [diff] [blame] | 423 | if (!langsys.has_required_feature ()) |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 424 | printf (" No required feature\n"); |
Behdad Esfahbod | 0ddecab | 2014-05-01 16:01:40 -0700 | [diff] [blame] | 425 | else |
| 426 | printf (" Required feature index: %d\n", |
| 427 | langsys.get_required_feature_index ()); |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 428 | |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 429 | unsigned num_features = langsys.get_feature_count (); |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 430 | printf (" %d feature(s) found in language system\n", num_features); |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 431 | for (unsigned n_feature = 0; n_feature < num_features; ++n_feature) |
Ebrahim Byagowi | 0558413 | 2019-10-01 13:49:55 +0330 | [diff] [blame] | 432 | { |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 433 | printf (" Feature index %2d of %2d: %d\n", n_feature, num_features, |
Ebrahim Byagowi | 0558413 | 2019-10-01 13:49:55 +0330 | [diff] [blame] | 434 | langsys.get_feature_index (n_feature)); |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 435 | } |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 436 | } |
| 437 | } |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 438 | |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 439 | unsigned num_features = g.get_feature_count (); |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 440 | printf (" %d feature(s) found in table\n", num_features); |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 441 | for (unsigned n_feature = 0; n_feature < num_features; ++n_feature) |
Ebrahim Byagowi | 0558413 | 2019-10-01 13:49:55 +0330 | [diff] [blame] | 442 | { |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 443 | const Feature &feature = g.get_feature (n_feature); |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 444 | unsigned num_lookups = feature.get_lookup_count (); |
Jonathan Kew | da13293 | 2014-04-27 14:05:24 +0100 | [diff] [blame] | 445 | printf (" Feature %2d of %2d: %c%c%c%c\n", n_feature, num_features, |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 446 | HB_UNTAG (g.get_feature_tag (n_feature))); |
Behdad Esfahbod | 0ddecab | 2014-05-01 16:01:40 -0700 | [diff] [blame] | 447 | |
Behdad Esfahbod | 2d15e72 | 2009-04-15 19:50:16 -0400 | [diff] [blame] | 448 | printf (" %d lookup(s) found in feature\n", num_lookups); |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 449 | for (unsigned n_lookup = 0; n_lookup < num_lookups; ++n_lookup) { |
Behdad Esfahbod | 2d15e72 | 2009-04-15 19:50:16 -0400 | [diff] [blame] | 450 | printf (" Lookup index %2d of %2d: %d\n", n_lookup, num_lookups, |
Ebrahim Byagowi | 0558413 | 2019-10-01 13:49:55 +0330 | [diff] [blame] | 451 | feature.get_lookup_index (n_lookup)); |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 452 | } |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 453 | } |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 454 | |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 455 | unsigned num_lookups = g.get_lookup_count (); |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 456 | printf (" %d lookup(s) found in table\n", num_lookups); |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 457 | for (unsigned n_lookup = 0; n_lookup < num_lookups; ++n_lookup) |
Ebrahim Byagowi | 0558413 | 2019-10-01 13:49:55 +0330 | [diff] [blame] | 458 | { |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 459 | const Lookup &lookup = g.get_lookup (n_lookup); |
Behdad Esfahbod | 8c69e65 | 2010-10-27 22:07:49 -0400 | [diff] [blame] | 460 | printf (" Lookup %2d of %2d: type %d, props 0x%04X\n", n_lookup, num_lookups, |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 461 | lookup.get_type (), lookup.get_props ()); |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 462 | } |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 463 | |
| 464 | } |
| 465 | break; |
| 466 | |
Behdad Esfahbod | 6c48f20 | 2013-09-09 15:43:10 -0400 | [diff] [blame] | 467 | case GDEF::tableTag: |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 468 | { |
| 469 | |
Behdad Esfahbod | b84ceb2 | 2019-12-10 13:02:48 -0600 | [diff] [blame] | 470 | const GDEF &gdef = *reinterpret_cast<const GDEF *> (font_data + table.offset); |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 471 | |
| 472 | printf (" Has %sglyph classes\n", |
| 473 | gdef.has_glyph_classes () ? "" : "no "); |
| 474 | printf (" Has %smark attachment types\n", |
| 475 | gdef.has_mark_attachment_types () ? "" : "no "); |
Behdad Esfahbod | 79420ad | 2009-05-26 12:24:16 -0400 | [diff] [blame] | 476 | printf (" Has %sattach points\n", |
| 477 | gdef.has_attach_points () ? "" : "no "); |
| 478 | printf (" Has %slig carets\n", |
| 479 | gdef.has_lig_carets () ? "" : "no "); |
Behdad Esfahbod | 67cb811 | 2009-08-09 13:05:08 -0400 | [diff] [blame] | 480 | printf (" Has %smark sets\n", |
| 481 | gdef.has_mark_sets () ? "" : "no "); |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 482 | break; |
Behdad Esfahbod | 62964af | 2009-05-26 12:40:10 -0400 | [diff] [blame] | 483 | } |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | } |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 487 | } |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 488 | |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 489 | int |
| 490 | main (int argc, char **argv) |
| 491 | { |
| 492 | if (argc != 2) |
Ebrahim Byagowi | 23277be | 2020-01-24 18:49:48 +0330 | [diff] [blame] | 493 | { |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 494 | fprintf (stderr, "usage: %s font-file.ttf\n", argv[0]); |
| 495 | exit (1); |
Ebrahim Byagowi | 23277be | 2020-01-24 18:49:48 +0330 | [diff] [blame] | 496 | } |
| 497 | |
Ebrahim Byagowi | 2d14735 | 2020-01-24 19:41:26 +0330 | [diff] [blame^] | 498 | hb_blob_t *blob = hb_blob_create_from_file (argv[1]); |
| 499 | printf ("Opened font file %s: %d bytes long\n", argv[1], hb_blob_get_length (blob)); |
| 500 | print_layout_info_using_private_api (blob); |
| 501 | dump_glyphs (blob, argv[1]); |
Ebrahim Byagowi | 23277be | 2020-01-24 18:49:48 +0330 | [diff] [blame] | 502 | hb_blob_destroy (blob); |
| 503 | |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 504 | return 0; |
| 505 | } |