[tool] Optimize COLR glyph dump

Move palette colors fetching out of gid iteration so not
fetching all the colors of a palette each time.
This commit is contained in:
Ebrahim Byagowi 2020-02-08 10:57:07 +03:30
parent 0b76e8130e
commit 5c1a023f67
1 changed files with 47 additions and 46 deletions

View File

@ -172,72 +172,73 @@ static void
layered_glyph_dump (hb_font_t *font, hb_draw_funcs_t *funcs, unsigned face_index) layered_glyph_dump (hb_font_t *font, hb_draw_funcs_t *funcs, unsigned face_index)
{ {
hb_face_t *face = hb_font_get_face (font); hb_face_t *face = hb_font_get_face (font);
unsigned num_glyphs = hb_face_get_glyph_count (face); unsigned palette_count = hb_ot_color_palette_get_count (face);
for (hb_codepoint_t gid = 0; gid < num_glyphs; ++gid) for (unsigned palette = 0; palette < palette_count; ++palette)
{ {
unsigned num_layers = hb_ot_color_glyph_get_layers (face, gid, 0, nullptr, nullptr); unsigned num_colors = hb_ot_color_palette_get_colors (face, palette, 0, nullptr, nullptr);
if (!num_layers) continue; if (!num_colors) continue;
hb_ot_color_layer_t *layers = (hb_ot_color_layer_t*) malloc (num_layers * sizeof (hb_ot_color_layer_t)); hb_color_t *colors = (hb_color_t*) calloc (num_colors, sizeof (hb_color_t));
hb_ot_color_palette_get_colors (face, palette, 0, &num_colors, colors);
hb_ot_color_glyph_get_layers (face, gid, 0, &num_layers, layers); if (!num_colors)
if (num_layers)
{ {
hb_font_extents_t font_extents; free (colors);
hb_font_get_extents_for_direction (font, HB_DIRECTION_LTR, &font_extents); continue;
hb_glyph_extents_t extents = {0}; }
if (!hb_font_get_glyph_extents (font, gid, &extents))
{
printf ("Skip gid: %d\n", gid);
continue;
}
unsigned palette_count = hb_ot_color_palette_get_count (face); unsigned num_glyphs = hb_face_get_glyph_count (face);
for (unsigned palette = 0; palette < palette_count; ++palette) for (hb_codepoint_t gid = 0; gid < num_glyphs; ++gid)
{
unsigned num_layers = hb_ot_color_glyph_get_layers (face, gid, 0, nullptr, nullptr);
if (!num_layers) continue;
hb_ot_color_layer_t *layers = (hb_ot_color_layer_t*) malloc (num_layers * sizeof (hb_ot_color_layer_t));
hb_ot_color_glyph_get_layers (face, gid, 0, &num_layers, layers);
if (num_layers)
{ {
unsigned num_colors = hb_ot_color_palette_get_colors (face, palette, 0, nullptr, nullptr); hb_font_extents_t font_extents;
if (!num_colors) hb_font_get_extents_for_direction (font, HB_DIRECTION_LTR, &font_extents);
hb_glyph_extents_t extents = {0};
if (!hb_font_get_glyph_extents (font, gid, &extents))
{
printf ("Skip gid: %d\n", gid);
continue; continue;
}
char output_path[255]; char output_path[255];
sprintf (output_path, "out/colr-%u-%u-%u.svg", gid, palette, face_index); sprintf (output_path, "out/colr-%u-%u-%u.svg", gid, palette, face_index);
FILE *f = fopen (output_path, "wb"); FILE *f = fopen (output_path, "wb");
fprintf (f, "<svg xmlns=\"http://www.w3.org/2000/svg\"" fprintf (f, "<svg xmlns=\"http://www.w3.org/2000/svg\""
" viewBox=\"%d %d %d %d\">\n", " viewBox=\"%d %d %d %d\">\n",
extents.x_bearing, 0, extents.x_bearing, 0,
extents.x_bearing + extents.width, -extents.height); extents.x_bearing + extents.width, -extents.height);
user_data_t user_data; user_data_t user_data;
user_data.ascender = extents.y_bearing; user_data.ascender = extents.y_bearing;
user_data.f = f; user_data.f = f;
hb_color_t *colors = (hb_color_t*) calloc (num_colors, sizeof (hb_color_t)); for (unsigned layer = 0; layer < num_layers; ++layer)
hb_ot_color_palette_get_colors (face, palette, 0, &num_colors, colors);
if (num_colors)
{ {
for (unsigned layer = 0; layer < num_layers; ++layer) hb_color_t color = 0x000000FF;
{ if (layers[layer].color_index != 0xFFFF)
hb_color_t color = 0x000000FF; color = colors[layers[layer].color_index];
if (layers[layer].color_index != 0xFFFF) fprintf (f, "<path fill=\"#%02X%02X%02X\" ",
color = colors[layers[layer].color_index]; hb_color_get_red (color), hb_color_get_green (color), hb_color_get_green (color));
fprintf (f, "<path fill=\"#%02X%02X%02X\" ", if (hb_color_get_alpha (color) != 255)
hb_color_get_red (color), hb_color_get_green (color), hb_color_get_green (color)); fprintf (f, "fill-opacity=\"%.3f\"", (double) hb_color_get_alpha (color) / 255.);
if (hb_color_get_alpha (color) != 255) fprintf (f, "d=\"");
fprintf (f, "fill-opacity=\"%.3f\"", (double) hb_color_get_alpha (color) / 255.); if (!hb_font_draw_glyph (font, layers[layer].glyph, funcs, &user_data))
fprintf (f, "d=\""); printf ("Failed to decompose layer %d while %d\n", layers[layer].glyph, gid);
if (!hb_font_draw_glyph (font, layers[layer].glyph, funcs, &user_data)) fprintf (f, "\"/>\n");
printf ("Failed to decompose layer %d while %d\n", layers[layer].glyph, gid);
fprintf (f, "\"/>\n");
}
} }
free (colors);
fprintf (f, "</svg>"); fprintf (f, "</svg>");
fclose (f); fclose (f);
} }
free (layers);
} }
free (colors);
free (layers);
} }
} }
@ -279,7 +280,7 @@ dump_glyphs (hb_blob_t *blob, const char *font_name)
FILE *font_name_file = fopen ("out/.dumped_font_name", "r"); FILE *font_name_file = fopen ("out/.dumped_font_name", "r");
if (font_name_file != nullptr) if (font_name_file != nullptr)
{ {
fprintf (stderr, "Purge or move ./out folder in order to run a new glyph dump,\n" fprintf (stderr, "Purge or rename ./out folder if you like to run a glyph dump,\n"
"run it like `rm -rf out && mkdir out && src/main font-file.ttf`\n"); "run it like `rm -rf out && mkdir out && src/main font-file.ttf`\n");
return; return;
} }