diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index d6be12a48..1e67dbcb0 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -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 diff --git a/src/hb-vector-draw.cc b/src/hb-vector-draw.cc index bbef3b1f5..fe1d81015 100644 --- a/src/hb-vector-draw.cc +++ b/src/hb-vector-draw.cc @@ -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, "defs, "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, "body, "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 &> (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. diff --git a/src/hb-vector-draw.hh b/src/hb-vector-draw.hh index 6d0309891..50b728119 100644 --- a/src/hb-vector-draw.hh +++ b/src/hb-vector-draw.hh @@ -21,6 +21,7 @@ struct hb_vector_draw_t bool has_extents = false; unsigned precision = 2; bool flat = false; + hb_vector_t id_prefix; hb_vector_t defs; hb_vector_t body; diff --git a/src/hb-vector-paint.cc b/src/hb-vector-paint.cc index cc7c559eb..238ee8534 100644 --- a/src/hb-vector-paint.cc +++ b/src/hb-vector-paint.cc @@ -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, "defs, "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, "defs, "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, "\">defs, "\">defs, pfx, pfx_len); + hb_buf_append_c (&paint->defs, 'p'); hb_buf_append_unsigned (&paint->defs, glyph); hb_buf_append_str (&paint->defs, "\"/>\n"); } - hb_buf_append_str (&paint->current_body (), "current_body (), "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, "defs, "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, "\">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, "\"/>\n"); - hb_buf_append_str (&paint->current_body (), "current_body (), "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, "defs, "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, "\n"); hb_buf_append_str (&paint->current_body (), - "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, "defs, "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, "\n"); hb_buf_append_str (&paint->current_body (), - "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, "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, "defs, "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 &> (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. diff --git a/src/hb-vector-paint.hh b/src/hb-vector-paint.hh index 1b7315dab..b3abcaeef 100644 --- a/src/hb-vector-paint.hh +++ b/src/hb-vector-paint.hh @@ -52,6 +52,7 @@ struct hb_vector_paint_t hb_hashmap_t custom_palette_colors; unsigned precision = 2; bool flat = false; + hb_vector_t id_prefix; hb_vector_t defs; hb_vector_t path; diff --git a/src/hb-vector.h b/src/hb-vector.h index 49e00c585..978ce0d04 100644 --- a/src/hb-vector.h +++ b/src/hb-vector.h @@ -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);