mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-21 15:01:01 +01:00
[vector] Add hb_vector_{draw,paint}_svg_{set,get}_prefix
New public API for namespacing SVG output: callers that embed
multiple hb-vector-produced SVGs into the same document can
set a distinct id prefix per context, so hb-vector's short
internal IDs (p<N>, c<N>, clip-g<N>, gr<N>, cg<N>) don't
collide across snippets.
Example collision the new API prevents: page embeds two
hb-vector SVGs produced by different contexts. Both use
id="clip-g10"; the browser's url(#clip-g10) resolves to
whichever element appears first, so paint layers in the
second SVG get clipped against the first SVG's glyph outline
-- visible as giant unclipped color rects bleeding across
the wrong glyph shape.
With the new API the second caller passes e.g. "b-" as its
prefix; the clash disappears. Empty/NULL prefix preserves
the existing output byte-for-byte.
Affects every id / url(#...) / href="#..." emission in SVG
output:
- hb-vector-draw: <path id="p..."> + <use href="#p...">
- hb-vector-paint: <path>, <clipPath>, <linearGradient>,
<radialGradient>, <g id="cg..."> color-glyph cache, and
every corresponding url(#...) / href="#..." ref.
PDF output is unaffected (no ids).
Docs/harfbuzz-sections.txt updated; check-symbols passes.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
cd1af387ba
commit
c3b0c4e282
6 files changed
+164
-14
No files matched your search
@@ -1090,6 +1090,8 @@ hb_vector_draw_set_flat
|
||||
hb_vector_draw_get_flat
|
||||
hb_vector_draw_set_precision
|
||||
hb_vector_draw_get_precision
|
||||
hb_vector_draw_svg_set_prefix
|
||||
hb_vector_draw_svg_get_prefix
|
||||
hb_vector_draw_render
|
||||
hb_vector_draw_clear
|
||||
hb_vector_draw_reset
|
||||
@@ -1119,6 +1121,8 @@ hb_vector_paint_set_flat
|
||||
hb_vector_paint_get_flat
|
||||
hb_vector_paint_set_precision
|
||||
hb_vector_paint_get_precision
|
||||
hb_vector_paint_svg_set_prefix
|
||||
hb_vector_paint_svg_get_prefix
|
||||
hb_vector_paint_render
|
||||
hb_vector_paint_clear
|
||||
hb_vector_paint_reset
|
||||
|
||||
+50
-2
@@ -599,7 +599,9 @@ hb_vector_draw_glyph (hb_vector_draw_t *draw,
|
||||
hb_font_draw_glyph (font, glyph, hb_vector_svg_path_draw_funcs_get (), &sink);
|
||||
if (!draw->path.length)
|
||||
return false;
|
||||
hb_buf_append_str (&draw->defs, "<path id=\"p");
|
||||
hb_buf_append_str (&draw->defs, "<path id=\"");
|
||||
hb_buf_append_len (&draw->defs, draw->id_prefix.arrayZ, draw->id_prefix.length);
|
||||
hb_buf_append_c (&draw->defs, 'p');
|
||||
hb_buf_append_unsigned (&draw->defs, glyph);
|
||||
hb_buf_append_str (&draw->defs, "\" d=\"");
|
||||
hb_buf_append_len (&draw->defs, draw->path.arrayZ, draw->path.length);
|
||||
@@ -642,7 +644,9 @@ hb_vector_draw_glyph (hb_vector_draw_t *draw,
|
||||
float tx = draw->transform.x0 + xx * pen_x + xy * pen_y;
|
||||
float ty = draw->transform.y0 + yx * pen_x + yy * pen_y;
|
||||
|
||||
hb_buf_append_str (&draw->body, "<use href=\"#p");
|
||||
hb_buf_append_str (&draw->body, "<use href=\"#");
|
||||
hb_buf_append_len (&draw->body, draw->id_prefix.arrayZ, draw->id_prefix.length);
|
||||
hb_buf_append_c (&draw->body, 'p');
|
||||
hb_buf_append_unsigned (&draw->body, glyph);
|
||||
hb_buf_append_str (&draw->body, "\" transform=\"");
|
||||
hb_vector_svg_append_instance_transform (&draw->body,
|
||||
@@ -692,6 +696,50 @@ hb_vector_draw_get_flat (const hb_vector_draw_t *draw)
|
||||
return draw->flat;
|
||||
}
|
||||
|
||||
/**
|
||||
* hb_vector_draw_svg_set_prefix:
|
||||
* @draw: a draw context.
|
||||
* @prefix: a null-terminated ASCII string to prepend to every emitted
|
||||
* SVG `id` and `url(#...)` reference, or `NULL` for none.
|
||||
*
|
||||
* Namespaces the draw's SVG output. Callers that inject multiple
|
||||
* hb-vector SVGs into the same document must set a distinct prefix
|
||||
* per context so that the short IDs hb-vector uses for shared glyph
|
||||
* outlines don't collide in the DOM.
|
||||
*
|
||||
* No effect on PDF output.
|
||||
*
|
||||
* XSince: REPLACEME
|
||||
*/
|
||||
void
|
||||
hb_vector_draw_svg_set_prefix (hb_vector_draw_t *draw,
|
||||
const char *prefix)
|
||||
{
|
||||
draw->id_prefix.resize (0);
|
||||
if (prefix)
|
||||
hb_buf_append_str (&draw->id_prefix, prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* hb_vector_draw_svg_get_prefix:
|
||||
* @draw: a draw context.
|
||||
*
|
||||
* Returns the SVG id prefix previously set on @draw, or `""` if
|
||||
* none was set.
|
||||
*
|
||||
* Return value: the SVG id prefix.
|
||||
*
|
||||
* XSince: REPLACEME
|
||||
*/
|
||||
const char *
|
||||
hb_vector_draw_svg_get_prefix (const hb_vector_draw_t *draw)
|
||||
{
|
||||
if (!draw->id_prefix.length) return "";
|
||||
const_cast<hb_vector_t<char> &> (draw->id_prefix).alloc (draw->id_prefix.length + 1, false);
|
||||
draw->id_prefix.arrayZ[draw->id_prefix.length] = '\0';
|
||||
return draw->id_prefix.arrayZ;
|
||||
}
|
||||
|
||||
/**
|
||||
* hb_vector_draw_set_precision:
|
||||
* @draw: a draw context.
|
||||
|
||||
@@ -21,6 +21,7 @@ struct hb_vector_draw_t
|
||||
bool has_extents = false;
|
||||
unsigned precision = 2;
|
||||
bool flat = false;
|
||||
hb_vector_t<char> id_prefix;
|
||||
|
||||
hb_vector_t<char> defs;
|
||||
hb_vector_t<char> body;
|
||||
|
||||
+94
-12
@@ -479,13 +479,18 @@ hb_vector_paint_push_clip_glyph (hb_paint_funcs_t *,
|
||||
if (unlikely (!hb_vector_paint_ensure_initialized (paint)))
|
||||
return;
|
||||
|
||||
const char *pfx = paint->id_prefix.arrayZ;
|
||||
unsigned pfx_len = paint->id_prefix.length;
|
||||
|
||||
if (!hb_set_has (paint->defined_outlines, glyph))
|
||||
{
|
||||
hb_set_add (paint->defined_outlines, glyph);
|
||||
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->defs, "<path id=\"p");
|
||||
hb_buf_append_str (&paint->defs, "<path id=\"");
|
||||
hb_buf_append_len (&paint->defs, pfx, pfx_len);
|
||||
hb_buf_append_c (&paint->defs, 'p');
|
||||
hb_buf_append_unsigned (&paint->defs, glyph);
|
||||
hb_buf_append_str (&paint->defs, "\" d=\"");
|
||||
hb_buf_append_len (&paint->defs, paint->path.arrayZ, paint->path.length);
|
||||
@@ -495,14 +500,20 @@ hb_vector_paint_push_clip_glyph (hb_paint_funcs_t *,
|
||||
if (!hb_set_has (paint->defined_clips, glyph))
|
||||
{
|
||||
hb_set_add (paint->defined_clips, glyph);
|
||||
hb_buf_append_str (&paint->defs, "<clipPath id=\"clip-g");
|
||||
hb_buf_append_str (&paint->defs, "<clipPath id=\"");
|
||||
hb_buf_append_len (&paint->defs, pfx, pfx_len);
|
||||
hb_buf_append_str (&paint->defs, "clip-g");
|
||||
hb_buf_append_unsigned (&paint->defs, glyph);
|
||||
hb_buf_append_str (&paint->defs, "\"><use href=\"#p");
|
||||
hb_buf_append_str (&paint->defs, "\"><use href=\"#");
|
||||
hb_buf_append_len (&paint->defs, pfx, pfx_len);
|
||||
hb_buf_append_c (&paint->defs, 'p');
|
||||
hb_buf_append_unsigned (&paint->defs, glyph);
|
||||
hb_buf_append_str (&paint->defs, "\"/></clipPath>\n");
|
||||
}
|
||||
|
||||
hb_buf_append_str (&paint->current_body (), "<g clip-path=\"url(#clip-g");
|
||||
hb_buf_append_str (&paint->current_body (), "<g clip-path=\"url(#");
|
||||
hb_buf_append_len (&paint->current_body (), pfx, pfx_len);
|
||||
hb_buf_append_str (&paint->current_body (), "clip-g");
|
||||
hb_buf_append_unsigned (&paint->current_body (), glyph);
|
||||
hb_buf_append_str (&paint->current_body (), ")\">\n");
|
||||
}
|
||||
@@ -518,8 +529,12 @@ hb_vector_paint_push_clip_rectangle (hb_paint_funcs_t *,
|
||||
if (unlikely (!hb_vector_paint_ensure_initialized (paint)))
|
||||
return;
|
||||
|
||||
const char *pfx = paint->id_prefix.arrayZ;
|
||||
unsigned pfx_len = paint->id_prefix.length;
|
||||
unsigned clip_id = paint->clip_rect_counter++;
|
||||
hb_buf_append_str (&paint->defs, "<clipPath id=\"c");
|
||||
hb_buf_append_str (&paint->defs, "<clipPath id=\"");
|
||||
hb_buf_append_len (&paint->defs, pfx, pfx_len);
|
||||
hb_buf_append_c (&paint->defs, 'c');
|
||||
hb_buf_append_unsigned (&paint->defs, clip_id);
|
||||
hb_buf_append_str (&paint->defs, "\"><rect x=\"");
|
||||
hb_buf_append_num (&paint->defs, xmin, paint->precision);
|
||||
@@ -531,7 +546,9 @@ hb_vector_paint_push_clip_rectangle (hb_paint_funcs_t *,
|
||||
hb_buf_append_num (&paint->defs, ymax - ymin, paint->precision);
|
||||
hb_buf_append_str (&paint->defs, "\"/></clipPath>\n");
|
||||
|
||||
hb_buf_append_str (&paint->current_body (), "<g clip-path=\"url(#c");
|
||||
hb_buf_append_str (&paint->current_body (), "<g clip-path=\"url(#");
|
||||
hb_buf_append_len (&paint->current_body (), pfx, pfx_len);
|
||||
hb_buf_append_c (&paint->current_body (), 'c');
|
||||
hb_buf_append_unsigned (&paint->current_body (), clip_id);
|
||||
hb_buf_append_str (&paint->current_body (), ")\">\n");
|
||||
}
|
||||
@@ -640,9 +657,13 @@ hb_vector_paint_linear_gradient (hb_paint_funcs_t *,
|
||||
|
||||
qsort (stops.arrayZ, stops.length, sizeof (hb_color_stop_t), hb_vector_color_stop_cmp);
|
||||
|
||||
const char *pfx = paint->id_prefix.arrayZ;
|
||||
unsigned pfx_len = paint->id_prefix.length;
|
||||
unsigned grad_id = paint->gradient_counter++;
|
||||
|
||||
hb_buf_append_str (&paint->defs, "<linearGradient id=\"gr");
|
||||
hb_buf_append_str (&paint->defs, "<linearGradient id=\"");
|
||||
hb_buf_append_len (&paint->defs, pfx, pfx_len);
|
||||
hb_buf_append_str (&paint->defs, "gr");
|
||||
hb_buf_append_unsigned (&paint->defs, grad_id);
|
||||
hb_buf_append_str (&paint->defs, "\" gradientUnits=\"userSpaceOnUse\" x1=\"");
|
||||
hb_buf_append_num (&paint->defs, x0, paint->precision);
|
||||
@@ -659,7 +680,9 @@ hb_vector_paint_linear_gradient (hb_paint_funcs_t *,
|
||||
hb_buf_append_str (&paint->defs, "</linearGradient>\n");
|
||||
|
||||
hb_buf_append_str (&paint->current_body (),
|
||||
"<rect x=\"-32767\" y=\"-32767\" width=\"65534\" height=\"65534\" fill=\"url(#gr");
|
||||
"<rect x=\"-32767\" y=\"-32767\" width=\"65534\" height=\"65534\" fill=\"url(#");
|
||||
hb_buf_append_len (&paint->current_body (), pfx, pfx_len);
|
||||
hb_buf_append_str (&paint->current_body (), "gr");
|
||||
hb_buf_append_unsigned (&paint->current_body (), grad_id);
|
||||
hb_buf_append_str (&paint->current_body (), ")\"/>\n");
|
||||
}
|
||||
@@ -682,9 +705,13 @@ hb_vector_paint_radial_gradient (hb_paint_funcs_t *,
|
||||
|
||||
qsort (stops.arrayZ, stops.length, sizeof (hb_color_stop_t), hb_vector_color_stop_cmp);
|
||||
|
||||
const char *pfx = paint->id_prefix.arrayZ;
|
||||
unsigned pfx_len = paint->id_prefix.length;
|
||||
unsigned grad_id = paint->gradient_counter++;
|
||||
|
||||
hb_buf_append_str (&paint->defs, "<radialGradient id=\"gr");
|
||||
hb_buf_append_str (&paint->defs, "<radialGradient id=\"");
|
||||
hb_buf_append_len (&paint->defs, pfx, pfx_len);
|
||||
hb_buf_append_str (&paint->defs, "gr");
|
||||
hb_buf_append_unsigned (&paint->defs, grad_id);
|
||||
hb_buf_append_str (&paint->defs, "\" gradientUnits=\"userSpaceOnUse\" cx=\"");
|
||||
hb_buf_append_num (&paint->defs, x1, paint->precision);
|
||||
@@ -708,7 +735,9 @@ hb_vector_paint_radial_gradient (hb_paint_funcs_t *,
|
||||
hb_buf_append_str (&paint->defs, "</radialGradient>\n");
|
||||
|
||||
hb_buf_append_str (&paint->current_body (),
|
||||
"<rect x=\"-32767\" y=\"-32767\" width=\"65534\" height=\"65534\" fill=\"url(#gr");
|
||||
"<rect x=\"-32767\" y=\"-32767\" width=\"65534\" height=\"65534\" fill=\"url(#");
|
||||
hb_buf_append_len (&paint->current_body (), pfx, pfx_len);
|
||||
hb_buf_append_str (&paint->current_body (), "gr");
|
||||
hb_buf_append_unsigned (&paint->current_body (), grad_id);
|
||||
hb_buf_append_str (&paint->current_body (), ")\"/>\n");
|
||||
}
|
||||
@@ -1342,7 +1371,9 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint,
|
||||
{
|
||||
unsigned def_id = paint->defined_color_glyphs.get (cache_key);
|
||||
auto &body = paint->current_body ();
|
||||
hb_buf_append_str (&body, "<use href=\"#cg");
|
||||
hb_buf_append_str (&body, "<use href=\"#");
|
||||
hb_buf_append_len (&body, paint->id_prefix.arrayZ, paint->id_prefix.length);
|
||||
hb_buf_append_str (&body, "cg");
|
||||
hb_buf_append_unsigned (&body, def_id);
|
||||
hb_buf_append_str (&body, "\" transform=\"");
|
||||
hb_vector_svg_append_instance_transform (&body, paint->precision,
|
||||
@@ -1378,7 +1409,9 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint,
|
||||
if (unlikely (!paint->defined_color_glyphs.set (cache_key, def_id)))
|
||||
return false;
|
||||
|
||||
hb_buf_append_str (&paint->defs, "<g id=\"cg");
|
||||
hb_buf_append_str (&paint->defs, "<g id=\"");
|
||||
hb_buf_append_len (&paint->defs, paint->id_prefix.arrayZ, paint->id_prefix.length);
|
||||
hb_buf_append_str (&paint->defs, "cg");
|
||||
hb_buf_append_unsigned (&paint->defs, def_id);
|
||||
hb_buf_append_str (&paint->defs, "\">\n");
|
||||
hb_buf_append_len (&paint->defs,
|
||||
@@ -1452,6 +1485,55 @@ hb_vector_paint_get_flat (const hb_vector_paint_t *paint)
|
||||
return paint->flat;
|
||||
}
|
||||
|
||||
/**
|
||||
* hb_vector_paint_svg_set_prefix:
|
||||
* @paint: a paint context.
|
||||
* @prefix: a null-terminated ASCII string to prepend to every emitted
|
||||
* SVG `id` and `url(#...)` reference, or `NULL` for none.
|
||||
*
|
||||
* Namespaces the paint's SVG output. Callers that inject multiple
|
||||
* hb-vector SVGs into the same document (e.g. several glyph previews
|
||||
* on one page) must set a distinct prefix per context so that the
|
||||
* short IDs hb-vector uses for clipPaths, gradients, and use-refs
|
||||
* don't collide in the DOM.
|
||||
*
|
||||
* No effect on PDF output.
|
||||
*
|
||||
* XSince: REPLACEME
|
||||
*/
|
||||
void
|
||||
hb_vector_paint_svg_set_prefix (hb_vector_paint_t *paint,
|
||||
const char *prefix)
|
||||
{
|
||||
paint->id_prefix.resize (0);
|
||||
if (prefix)
|
||||
hb_buf_append_str (&paint->id_prefix, prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* hb_vector_paint_svg_get_prefix:
|
||||
* @paint: a paint context.
|
||||
*
|
||||
* Returns the SVG id prefix previously set on @paint, or `""` if
|
||||
* none was set. The pointer remains valid until the next call to
|
||||
* hb_vector_paint_svg_set_prefix() or hb_vector_paint_reset() on the
|
||||
* same context.
|
||||
*
|
||||
* Return value: the SVG id prefix.
|
||||
*
|
||||
* XSince: REPLACEME
|
||||
*/
|
||||
const char *
|
||||
hb_vector_paint_svg_get_prefix (const hb_vector_paint_t *paint)
|
||||
{
|
||||
if (!paint->id_prefix.length) return "";
|
||||
/* id_prefix is appended via hb_buf_append_str which does NOT
|
||||
* NUL-terminate; ensure a trailing NUL. */
|
||||
const_cast<hb_vector_t<char> &> (paint->id_prefix).alloc (paint->id_prefix.length + 1, false);
|
||||
paint->id_prefix.arrayZ[paint->id_prefix.length] = '\0';
|
||||
return paint->id_prefix.arrayZ;
|
||||
}
|
||||
|
||||
/**
|
||||
* hb_vector_paint_set_precision:
|
||||
* @paint: a paint context.
|
||||
|
||||
@@ -52,6 +52,7 @@ struct hb_vector_paint_t
|
||||
hb_hashmap_t<unsigned, hb_color_t> custom_palette_colors;
|
||||
unsigned precision = 2;
|
||||
bool flat = false;
|
||||
hb_vector_t<char> id_prefix;
|
||||
|
||||
hb_vector_t<char> defs;
|
||||
hb_vector_t<char> path;
|
||||
|
||||
@@ -176,6 +176,13 @@ hb_vector_draw_set_precision (hb_vector_draw_t *draw,
|
||||
HB_EXTERN unsigned
|
||||
hb_vector_draw_get_precision (const hb_vector_draw_t *draw);
|
||||
|
||||
HB_EXTERN void
|
||||
hb_vector_draw_svg_set_prefix (hb_vector_draw_t *draw,
|
||||
const char *prefix);
|
||||
|
||||
HB_EXTERN const char *
|
||||
hb_vector_draw_svg_get_prefix (const hb_vector_draw_t *draw);
|
||||
|
||||
HB_EXTERN hb_blob_t *
|
||||
hb_vector_draw_render (hb_vector_draw_t *draw);
|
||||
|
||||
@@ -293,6 +300,13 @@ hb_vector_paint_set_precision (hb_vector_paint_t *paint,
|
||||
HB_EXTERN unsigned
|
||||
hb_vector_paint_get_precision (const hb_vector_paint_t *paint);
|
||||
|
||||
HB_EXTERN void
|
||||
hb_vector_paint_svg_set_prefix (hb_vector_paint_t *paint,
|
||||
const char *prefix);
|
||||
|
||||
HB_EXTERN const char *
|
||||
hb_vector_paint_svg_get_prefix (const hb_vector_paint_t *paint);
|
||||
|
||||
HB_EXTERN hb_blob_t *
|
||||
hb_vector_paint_render (hb_vector_paint_t *paint);
|
||||
|
||||
|
||||
Reference in new issue
Block a user