mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-25 00:41:09 +01:00
[hb-draw] Add color glyph reuse in defs for smaller SVG output
Color glyphs are now defined once in <defs> and reused via <use> elements, similar to non-color glyphs. Previously each color glyph instance rendered the full paint operations inline, causing duplication. Tested with repeating emoji: reduces SVG size by ~44% for files with repeated color glyphs (e.g., 3 emoji instances: 5.8KB vs 11KB without reuse). Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e5a8f3c325
commit
e5ebd0465c
1 file changed
+42
-41
+42
-41
@@ -712,6 +712,29 @@ struct draw_output_t : output_options_t<>
|
||||
}
|
||||
}
|
||||
|
||||
/* Color glyph defs for reuse. */
|
||||
for (hb_codepoint_t gid : unique_gids)
|
||||
{
|
||||
if (!color_gids.count (gid))
|
||||
continue;
|
||||
|
||||
GString *body = g_string_new (nullptr);
|
||||
GString *defs = g_string_new (nullptr);
|
||||
|
||||
if (paint_glyph_to_svg (gid, defs, body))
|
||||
{
|
||||
if (defs->len)
|
||||
g_string_append_len (all_defs, defs->str, defs->len);
|
||||
|
||||
g_string_append_printf (all_defs, "<g id=\"cg%u\">\n", gid);
|
||||
g_string_append_len (all_defs, body->str, body->len);
|
||||
g_string_append (all_defs, "</g>\n");
|
||||
}
|
||||
|
||||
g_string_free (body, true);
|
||||
g_string_free (defs, true);
|
||||
}
|
||||
|
||||
if (!layout.upem_scale)
|
||||
{
|
||||
g_string_set_size (path, 0);
|
||||
@@ -732,53 +755,31 @@ struct draw_output_t : output_options_t<>
|
||||
const auto &offset = layout.offsets[li];
|
||||
for (const glyph_instance_t &glyph : line.glyphs)
|
||||
{
|
||||
g_string_set_size (path, 0);
|
||||
append_num (offset.first + glyph.x, precision, false);
|
||||
g_string_append_c (path, ' ');
|
||||
append_num (layout.upem_scale ? -(offset.second + glyph.y)
|
||||
: (offset.second + glyph.y),
|
||||
precision,
|
||||
false);
|
||||
|
||||
if (color_gids.count (glyph.gid))
|
||||
{
|
||||
GString *body = g_string_new (nullptr);
|
||||
GString *defs = g_string_new (nullptr);
|
||||
|
||||
if (paint_glyph_to_svg (glyph.gid, defs, body))
|
||||
{
|
||||
if (defs->len)
|
||||
g_string_append_len (all_defs, defs->str, defs->len);
|
||||
|
||||
/* Paint output is in upem coordinates.
|
||||
* In upem_scale mode there's no outer wrapper, so we Y-flip here.
|
||||
* In non-upem_scale mode we're inside scale(fsx,-fsy)*scale(1/upem)
|
||||
* which already flips Y, so just translate. */
|
||||
float tx = offset.first + glyph.x;
|
||||
float ty = offset.second + glyph.y;
|
||||
|
||||
g_string_append (all_body, "<g transform=\"translate(");
|
||||
append_num_to (all_body, tx, precision);
|
||||
g_string_append_c (all_body, ',');
|
||||
if (layout.upem_scale)
|
||||
{
|
||||
append_num_to (all_body, -ty, precision);
|
||||
g_string_append (all_body, ") scale(1,-1)\">\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
append_num_to (all_body, ty, precision);
|
||||
g_string_append (all_body, ")\">\n");
|
||||
}
|
||||
g_string_append_len (all_body, body->str, body->len);
|
||||
g_string_append (all_body, "</g>\n");
|
||||
}
|
||||
|
||||
g_string_free (body, true);
|
||||
g_string_free (defs, true);
|
||||
/* Color: use shared color glyph definition with Y-flip on <use>. */
|
||||
if (layout.upem_scale)
|
||||
g_string_append_printf (all_body,
|
||||
"<use href=\"#cg%u\" transform=\"translate(%s) scale(1,-1)\"/>\n",
|
||||
glyph.gid,
|
||||
path->str);
|
||||
else
|
||||
g_string_append_printf (all_body,
|
||||
"<use href=\"#cg%u\" transform=\"translate(%s)\"/>\n",
|
||||
glyph.gid,
|
||||
path->str);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Non-color: use shared outline with Y-flip on <use>. */
|
||||
g_string_set_size (path, 0);
|
||||
append_num (offset.first + glyph.x, precision, false);
|
||||
g_string_append_c (path, ' ');
|
||||
append_num (layout.upem_scale ? -(offset.second + glyph.y)
|
||||
: (offset.second + glyph.y),
|
||||
precision,
|
||||
false);
|
||||
if (layout.upem_scale)
|
||||
g_string_append_printf (all_body,
|
||||
"<use href=\"#p%u\" transform=\"translate(%s) scale(1,-1)\"/>\n",
|
||||
|
||||
Reference in new issue
Block a user