[vector] Flat mode: inline clip-path="path('...')" instead of <clipPath>

Flat output was still emitting <clipPath id="..."> into <defs>
and referencing them via clip-path="url(#...)".  That meant
flat SVGs couldn't be safely concatenated: two documents'
short IDs (c0, clip-g10, ...) would collide and url(#c0)
resolves to whichever element appears first in document
order, producing the wrong clip and giant paint rects
falling through their clips.

SVG 2 lets clip-path take inline basic-shape or path()
values.  In flat mode, emit:

  push_clip_glyph     -> clip-path="path('<glyph-d>')"
  push_clip_rectangle -> clip-path="path('M..H..V..H..Z')"

...directly on the wrapping <g>, skipping defs entirely.
The resulting flat SVG truly has no defs for clips and is
safe to embed alongside other hb-vector output.

Gradients still use defs (paint servers need URL refs), but
those only affect color, not geometry -- a gradient
resolution mismatch yields wrong colors, never the
unclipped-rect bleed that was the visible symptom.

Verified on util/gpu/web/ / harfbuzz-world.cc's Noto color
emoji subset: no <clipPath> tags, no clip-path="url(...)",
69 inline clip-path="path('...')" refs instead.  Meson
test --suite vector remains 22/22 green.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.6 committed 2026-04-15 11:34:51 -06:00
1 parent 8c99cf0a9a
commit 3fe077d713
1 file changed
+31
+31
View File
@@ -479,6 +479,20 @@ hb_vector_paint_push_clip_glyph (hb_paint_funcs_t *,
if (unlikely (!hb_vector_paint_ensure_initialized (paint)))
return;
if (paint->flat)
{
/* Inline clip: emit clip-path="path('...')" so the
* output has no defs to reference and the whole SVG
* is safe to concatenate with other hb-vector output. */
paint->path.clear ();
hb_vector_svg_path_sink_t sink = {&paint->path, paint->precision};
hb_font_draw_glyph (font, glyph, hb_vector_svg_path_draw_funcs_get (), &sink);
hb_buf_append_str (&paint->current_body (), "<g clip-path=\"path('");
hb_buf_append_len (&paint->current_body (), paint->path.arrayZ, paint->path.length);
hb_buf_append_str (&paint->current_body (), "')\">\n");
return;
}
if (!hb_set_has (paint->defined_outlines, glyph))
{
hb_set_add (paint->defined_outlines, glyph);
@@ -518,6 +532,23 @@ hb_vector_paint_push_clip_rectangle (hb_paint_funcs_t *,
if (unlikely (!hb_vector_paint_ensure_initialized (paint)))
return;
if (paint->flat)
{
/* Inline clip as a polygon via clip-path="path('...')". */
hb_buf_append_str (&paint->current_body (), "<g clip-path=\"path('M");
hb_buf_append_num (&paint->current_body (), xmin, paint->precision);
hb_buf_append_c (&paint->current_body (), ' ');
hb_buf_append_num (&paint->current_body (), ymin, paint->precision);
hb_buf_append_str (&paint->current_body (), "H");
hb_buf_append_num (&paint->current_body (), xmax, paint->precision);
hb_buf_append_str (&paint->current_body (), "V");
hb_buf_append_num (&paint->current_body (), ymax, paint->precision);
hb_buf_append_str (&paint->current_body (), "H");
hb_buf_append_num (&paint->current_body (), xmin, paint->precision);
hb_buf_append_str (&paint->current_body (), "Z')\">\n");
return;
}
unsigned clip_id = paint->clip_rect_counter++;
hb_buf_append_str (&paint->defs, "<clipPath id=\"c");
hb_buf_append_unsigned (&paint->defs, clip_id);