From a43971d2a323eb4a5741a3980f812b0b5c7e1d44 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 20:13:47 -0600 Subject: [PATCH 01/23] [vector] Add PDF output backend MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add HB_VECTOR_FORMAT_PDF to hb_vector_format_t. The hb-vector tool now produces self-contained PDF when invoked with -o out.pdf. PDF path operators map directly from the draw callbacks: move_to → m, line_to → l, cubic_to → c, close_path → h, with quadratic-to-cubic promotion (PDF has no Q operator). The render function builds a minimal PDF 1.4 with catalog, page, and content stream objects plus a cross-reference table. Implementation: - Extract hb_vector_draw_t struct to hb-vector-draw.hh so future backends can share it. - Draw callbacks branch on format for SVG vs PDF syntax. - hb_vector_draw_glyph and hb_vector_draw_render dispatch by format. - vector-output.hh selects format from file extension and skips paint (color glyphs) for PDF. Coordinate system needs refinement — the page layout inherits the SVG Y-flip transform, placing glyphs at the top with extra whitespace. Correct PDF coordinate handling is a follow-up. Assisted-by: Claude --- src/hb-vector-draw.hh | 41 +++++++ src/hb-vector-svg-draw.cc | 232 +++++++++++++++++++++++++++++--------- src/hb-vector.h | 1 + src/meson.build | 1 + util/vector-output.hh | 62 ++++++---- 5 files changed, 262 insertions(+), 75 deletions(-) create mode 100644 src/hb-vector-draw.hh diff --git a/src/hb-vector-draw.hh b/src/hb-vector-draw.hh new file mode 100644 index 000000000..e9294bdd2 --- /dev/null +++ b/src/hb-vector-draw.hh @@ -0,0 +1,41 @@ +#ifndef HB_VECTOR_DRAW_HH +#define HB_VECTOR_DRAW_HH + +#include "hb.hh" + +#include "hb-vector.h" +#include "hb-geometry.hh" +#include "hb-machinery.hh" +#include "hb-vector-svg-utils.hh" +#include "hb-vector-svg.hh" + +struct hb_vector_draw_t +{ + hb_object_header_t header; + + hb_vector_format_t format = HB_VECTOR_FORMAT_SVG; + hb_transform_t<> transform = {1, 0, 0, 1, 0, 0}; + float x_scale_factor = 1.f; + float y_scale_factor = 1.f; + hb_vector_extents_t extents = {0, 0, 0, 0}; + bool has_extents = false; + unsigned precision = 2; + bool flat = false; + + hb_vector_t defs; + hb_vector_t body; + hb_vector_t path; + hb_set_t *defined_glyphs = nullptr; + hb_blob_t *recycled_blob = nullptr; + + void append_xy (float x, float y) + { + float tx, ty; + hb_svg_transform_point (transform, x_scale_factor, y_scale_factor, x, y, &tx, &ty); + hb_svg_append_num (&path, tx, precision); + hb_svg_append_c (&path, format == HB_VECTOR_FORMAT_PDF ? ' ' : ','); + hb_svg_append_num (&path, ty, precision); + } +}; + +#endif /* HB_VECTOR_DRAW_HH */ diff --git a/src/hb-vector-svg-draw.cc b/src/hb-vector-svg-draw.cc index 82f31b4f9..84146f985 100644 --- a/src/hb-vector-svg-draw.cc +++ b/src/hb-vector-svg-draw.cc @@ -26,21 +26,16 @@ #include "hb.hh" -#include "hb-vector.h" +#include "hb-vector-draw.hh" #include "hb-blob.hh" -#include "hb-geometry.hh" -#include "hb-machinery.hh" #include "hb-map.hh" #include "hb-vector-svg-path.hh" #include "hb-vector-svg-subset.hh" -#include "hb-vector-svg-utils.hh" #include #include #include -#include "hb-vector-svg.hh" - HB_UNUSED static inline bool hb_svg_buffer_contains (const hb_vector_t &buf, const char *needle) { @@ -55,35 +50,6 @@ hb_svg_buffer_contains (const hb_vector_t &buf, const char *needle) return false; } -struct hb_vector_draw_t -{ - hb_object_header_t header; - - hb_vector_format_t format = HB_VECTOR_FORMAT_SVG; - hb_transform_t<> transform = {1, 0, 0, 1, 0, 0}; - float x_scale_factor = 1.f; - float y_scale_factor = 1.f; - hb_vector_extents_t extents = {0, 0, 0, 0}; - bool has_extents = false; - unsigned precision = 2; - bool flat = false; - - hb_vector_t defs; - hb_vector_t body; - hb_vector_t path; - hb_set_t *defined_glyphs = nullptr; - hb_blob_t *recycled_blob = nullptr; - - void append_xy (float x, float y) - { - float tx, ty; - hb_svg_transform_point (transform, x_scale_factor, y_scale_factor, x, y, &tx, &ty); - hb_svg_append_num (&path, tx, precision); - hb_svg_append_c (&path, ','); - hb_svg_append_num (&path, ty, precision); - } -}; - static void hb_vector_draw_move_to (hb_draw_funcs_t *, void *draw_data, @@ -92,8 +58,16 @@ hb_vector_draw_move_to (hb_draw_funcs_t *, void *) { auto *d = (hb_vector_draw_t *) draw_data; - hb_svg_append_c (&d->path, 'M'); - d->append_xy (to_x, to_y); + if (d->format == HB_VECTOR_FORMAT_PDF) + { + d->append_xy (to_x, to_y); + hb_svg_append_str (&d->path, " m\n"); + } + else + { + hb_svg_append_c (&d->path, 'M'); + d->append_xy (to_x, to_y); + } } static void @@ -104,23 +78,49 @@ hb_vector_draw_line_to (hb_draw_funcs_t *, void *) { auto *d = (hb_vector_draw_t *) draw_data; - hb_svg_append_c (&d->path, 'L'); - d->append_xy (to_x, to_y); + if (d->format == HB_VECTOR_FORMAT_PDF) + { + d->append_xy (to_x, to_y); + hb_svg_append_str (&d->path, " l\n"); + } + else + { + hb_svg_append_c (&d->path, 'L'); + d->append_xy (to_x, to_y); + } } static void hb_vector_draw_quadratic_to (hb_draw_funcs_t *, void *draw_data, - hb_draw_state_t *, + hb_draw_state_t *st, float cx, float cy, float to_x, float to_y, void *) { auto *d = (hb_vector_draw_t *) draw_data; - hb_svg_append_c (&d->path, 'Q'); - d->append_xy (cx, cy); - hb_svg_append_c (&d->path, ' '); - d->append_xy (to_x, to_y); + if (d->format == HB_VECTOR_FORMAT_PDF) + { + /* PDF has no quadratic operator; promote to cubic. */ + float sx = st->current_x, sy = st->current_y; + float c1x = sx + 2.f / 3.f * (cx - sx); + float c1y = sy + 2.f / 3.f * (cy - sy); + float c2x = to_x + 2.f / 3.f * (cx - to_x); + float c2y = to_y + 2.f / 3.f * (cy - to_y); + d->append_xy (c1x, c1y); + hb_svg_append_c (&d->path, ' '); + d->append_xy (c2x, c2y); + hb_svg_append_c (&d->path, ' '); + d->append_xy (to_x, to_y); + hb_svg_append_str (&d->path, " c\n"); + } + else + { + hb_svg_append_c (&d->path, 'Q'); + d->append_xy (cx, cy); + hb_svg_append_c (&d->path, ' '); + d->append_xy (to_x, to_y); + } } static void @@ -133,12 +133,24 @@ hb_vector_draw_cubic_to (hb_draw_funcs_t *, void *) { auto *d = (hb_vector_draw_t *) draw_data; - hb_svg_append_c (&d->path, 'C'); - d->append_xy (c1x, c1y); - hb_svg_append_c (&d->path, ' '); - d->append_xy (c2x, c2y); - hb_svg_append_c (&d->path, ' '); - d->append_xy (to_x, to_y); + if (d->format == HB_VECTOR_FORMAT_PDF) + { + d->append_xy (c1x, c1y); + hb_svg_append_c (&d->path, ' '); + d->append_xy (c2x, c2y); + hb_svg_append_c (&d->path, ' '); + d->append_xy (to_x, to_y); + hb_svg_append_str (&d->path, " c\n"); + } + else + { + hb_svg_append_c (&d->path, 'C'); + d->append_xy (c1x, c1y); + hb_svg_append_c (&d->path, ' '); + d->append_xy (c2x, c2y); + hb_svg_append_c (&d->path, ' '); + d->append_xy (to_x, to_y); + } } static void @@ -148,7 +160,10 @@ hb_vector_draw_close_path (hb_draw_funcs_t *, void *) { auto *d = (hb_vector_draw_t *) draw_data; - hb_svg_append_c (&d->path, 'Z'); + if (d->format == HB_VECTOR_FORMAT_PDF) + hb_svg_append_str (&d->path, "h\n"); + else + hb_svg_append_c (&d->path, 'Z'); } static inline void free_static_vector_draw_funcs (); @@ -195,7 +210,7 @@ hb_vector_draw_funcs_get () hb_vector_draw_t * hb_vector_draw_create_or_fail (hb_vector_format_t format) { - if (format != HB_VECTOR_FORMAT_SVG) + if (format != HB_VECTOR_FORMAT_SVG && format != HB_VECTOR_FORMAT_PDF) return nullptr; hb_vector_draw_t *draw = hb_object_create (); @@ -503,7 +518,8 @@ hb_vector_draw_glyph (hb_vector_draw_t *draw, float pen_y, hb_vector_extents_mode_t extents_mode) { - if (draw->format != HB_VECTOR_FORMAT_SVG) + if (draw->format != HB_VECTOR_FORMAT_SVG && + draw->format != HB_VECTOR_FORMAT_PDF) return false; if (extents_mode == HB_VECTOR_EXTENTS_MODE_EXPAND) @@ -530,6 +546,30 @@ hb_vector_draw_glyph (hb_vector_draw_t *draw, } } + if (draw->format == HB_VECTOR_FORMAT_PDF) + { + /* PDF: always inline, use transform to position. */ + hb_transform_t<> saved = draw->transform; + float xx = draw->transform.xx; + float yx = draw->transform.yx; + float xy = draw->transform.xy; + float yy = draw->transform.yy; + float tx = draw->transform.x0 + xx * pen_x + xy * pen_y; + float ty = draw->transform.y0 + yx * pen_x + yy * pen_y; + draw->transform = {xx, yx, xy, yy, tx, ty}; + + draw->path.clear (); + hb_font_draw_glyph (font, glyph, hb_vector_draw_funcs_get (), draw); + draw->transform = saved; + + if (!draw->path.length) + return false; + + hb_svg_append_len (&draw->body, draw->path.arrayZ, draw->path.length); + hb_svg_append_str (&draw->body, "f\n"); + return true; + } + bool needs_def = !draw->flat && !hb_set_has (draw->defined_glyphs, glyph); if (needs_def) { @@ -635,9 +675,95 @@ hb_vector_svg_set_precision (hb_vector_draw_t *draw, * * Since: 13.0.0 */ +static hb_blob_t * +hb_vector_draw_render_pdf (hb_vector_draw_t *draw) +{ + if (!draw->has_extents) + return nullptr; + + /* Collect the content stream (path operators). */ + hb_vector_t stream; + stream.alloc (draw->body.length + draw->path.length + 64); + + if (draw->body.length) + hb_svg_append_len (&stream, draw->body.arrayZ, draw->body.length); + else if (draw->path.length) + { + hb_svg_append_len (&stream, draw->path.arrayZ, draw->path.length); + hb_svg_append_str (&stream, "f\n"); + } + + /* Build PDF objects, tracking byte offsets for xref. */ + hb_vector_t out; + hb_svg_recover_recycled_buffer (draw->recycled_blob, &out); + out.alloc (stream.length + 512); + + unsigned offsets[5]; /* objects 1-4, plus end */ + + hb_svg_append_str (&out, "%PDF-1.4\n%\xC0\xC1\xC2\xC3\n"); + + /* Object 1: Catalog */ + offsets[0] = out.length; + hb_svg_append_str (&out, "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); + + /* Object 2: Pages */ + offsets[1] = out.length; + hb_svg_append_str (&out, "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"); + + /* Object 3: Page */ + offsets[2] = out.length; + hb_svg_append_str (&out, "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox ["); + hb_svg_append_num (&out, draw->extents.x, draw->precision); + hb_svg_append_c (&out, ' '); + hb_svg_append_num (&out, draw->extents.y, draw->precision); + hb_svg_append_c (&out, ' '); + hb_svg_append_num (&out, draw->extents.x + draw->extents.width, draw->precision); + hb_svg_append_c (&out, ' '); + hb_svg_append_num (&out, draw->extents.y + draw->extents.height, draw->precision); + hb_svg_append_str (&out, "] /Contents 4 0 R >>\nendobj\n"); + + /* Object 4: Content stream */ + offsets[3] = out.length; + hb_svg_append_str (&out, "4 0 obj\n<< /Length "); + hb_svg_append_unsigned (&out, stream.length); + hb_svg_append_str (&out, " >>\nstream\n"); + hb_svg_append_len (&out, stream.arrayZ, stream.length); + hb_svg_append_str (&out, "endstream\nendobj\n"); + + /* Cross-reference table */ + unsigned xref_offset = out.length; + hb_svg_append_str (&out, "xref\n0 5\n"); + hb_svg_append_str (&out, "0000000000 65535 f \n"); + for (unsigned i = 0; i < 4; i++) + { + char tmp[21]; + snprintf (tmp, sizeof (tmp), "%010u 00000 n \n", offsets[i]); + hb_svg_append_len (&out, tmp, 20); + } + + /* Trailer */ + hb_svg_append_str (&out, "trailer\n<< /Size 5 /Root 1 0 R >>\nstartxref\n"); + hb_svg_append_unsigned (&out, xref_offset); + hb_svg_append_str (&out, "\n%%EOF\n"); + + hb_blob_t *blob = hb_svg_blob_from_buffer (&draw->recycled_blob, &out); + + draw->path.clear (); + draw->defs.clear (); + draw->body.clear (); + hb_set_clear (draw->defined_glyphs); + draw->has_extents = false; + draw->extents = {0, 0, 0, 0}; + + return blob; +} + hb_blob_t * hb_vector_draw_render (hb_vector_draw_t *draw) { + if (draw->format == HB_VECTOR_FORMAT_PDF) + return hb_vector_draw_render_pdf (draw); + if (draw->format != HB_VECTOR_FORMAT_SVG) return nullptr; if (!draw->has_extents) diff --git a/src/hb-vector.h b/src/hb-vector.h index dd32f2ec8..8962dc94d 100644 --- a/src/hb-vector.h +++ b/src/hb-vector.h @@ -43,6 +43,7 @@ HB_BEGIN_DECLS typedef enum { HB_VECTOR_FORMAT_INVALID = HB_TAG_NONE, HB_VECTOR_FORMAT_SVG = HB_TAG ('s','v','g',' '), + HB_VECTOR_FORMAT_PDF = HB_TAG ('p','d','f',' '), } hb_vector_format_t; /** diff --git a/src/meson.build b/src/meson.build index 97b189175..740568db0 100644 --- a/src/meson.build +++ b/src/meson.build @@ -273,6 +273,7 @@ hb_base_sources = files( 'hb-unicode.hh', 'hb-utf.hh', 'hb-vector.hh', + 'hb-vector-draw.hh', 'hb.hh', ) diff --git a/util/vector-output.hh b/util/vector-output.hh index ef11ffa6f..5d374f96b 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -179,22 +179,30 @@ struct vector_output_t : output_options_t<>, view_options_t return; final_extents = extents; - hb_vector_draw_t *draw = hb_vector_draw_create_or_fail (HB_VECTOR_FORMAT_SVG); - hb_vector_paint_t *paint = hb_vector_paint_create_or_fail (HB_VECTOR_FORMAT_SVG); + hb_vector_format_t fmt = HB_VECTOR_FORMAT_SVG; + if (output_format && !strcmp (output_format, "pdf")) + fmt = HB_VECTOR_FORMAT_PDF; + + hb_vector_draw_t *draw = hb_vector_draw_create_or_fail (fmt); + hb_vector_paint_t *paint = (fmt == HB_VECTOR_FORMAT_SVG) + ? hb_vector_paint_create_or_fail (HB_VECTOR_FORMAT_SVG) : nullptr; hb_vector_draw_set_scale_factor (draw, 1.f, 1.f); hb_vector_draw_set_extents (draw, &extents); hb_vector_svg_set_precision (draw, precision); hb_vector_svg_set_flat (draw, flat); - hb_vector_paint_set_scale_factor (paint, 1.f, 1.f); - hb_vector_paint_set_extents (paint, &extents); - hb_vector_paint_set_foreground (paint, foreground); - hb_vector_paint_set_palette (paint, this->palette); - apply_custom_palette (paint); - init_palette_color_cache (); - hb_vector_svg_paint_set_precision (paint, precision); - hb_vector_svg_paint_set_flat (paint, flat); + if (paint) + { + hb_vector_paint_set_scale_factor (paint, 1.f, 1.f); + hb_vector_paint_set_extents (paint, &extents); + hb_vector_paint_set_foreground (paint, foreground); + hb_vector_paint_set_palette (paint, this->palette); + apply_custom_palette (paint); + init_palette_color_cache (); + hb_vector_svg_paint_set_precision (paint, precision); + hb_vector_svg_paint_set_flat (paint, flat); + } bool had_draw = false; bool had_paint = false; @@ -225,20 +233,23 @@ struct vector_output_t : output_options_t<>, view_options_t float pen_x = g.x + off_x; float pen_y = g.y + off_y; - if (use_foreground_palette) + if (paint) { - const rgba_color_t &c = - g_array_index (foreground_palette, rgba_color_t, - palette_glyph_index++ % foreground_palette->len); - hb_vector_paint_set_foreground (paint, HB_COLOR (c.b, c.g, c.r, c.a)); - } + if (use_foreground_palette) + { + const rgba_color_t &c = + g_array_index (foreground_palette, rgba_color_t, + palette_glyph_index++ % foreground_palette->len); + hb_vector_paint_set_foreground (paint, HB_COLOR (c.b, c.g, c.r, c.a)); + } - hb_vector_paint_set_transform (paint, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f); - if (hb_vector_paint_glyph (paint, upem_font, g.gid, pen_x, pen_y, - extents_mode)) - { - had_paint = true; - continue; + hb_vector_paint_set_transform (paint, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f); + if (hb_vector_paint_glyph (paint, upem_font, g.gid, pen_x, pen_y, + extents_mode)) + { + had_paint = true; + continue; + } } if (hb_vector_draw_glyph (draw, upem_font, g.gid, pen_x, pen_y, @@ -810,6 +821,13 @@ struct vector_output_t : output_options_t<>, view_options_t if (!in_data || !in_len) return; + /* PDF blobs are self-contained; write directly. */ + if (in_len > 5 && memcmp (in_data, "%PDF-", 5) == 0) + { + fwrite (in_data, 1, in_len, out_fp); + return; + } + const char *hdr_end = (const char *) memchr (in_data, '>', in_len); if (!hdr_end) return; From 3fc9ae5e49658ab6cc1a70f8bc5b90951e9fddc3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 20:17:08 -0600 Subject: [PATCH 02/23] [vector] Fix PDF coordinate system with Y-flip CTM Prepend a 1 0 0 -1 0 (ey+eh) cm to the content stream so that the SVG-space (Y-down) path coordinates render correctly in PDF (Y-up). Set the MediaBox to [ex 0 ex+ew eh]. Assisted-by: Claude --- src/hb-vector-svg-draw.cc | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/hb-vector-svg-draw.cc b/src/hb-vector-svg-draw.cc index 84146f985..e68ea5073 100644 --- a/src/hb-vector-svg-draw.cc +++ b/src/hb-vector-svg-draw.cc @@ -681,9 +681,21 @@ hb_vector_draw_render_pdf (hb_vector_draw_t *draw) if (!draw->has_extents) return nullptr; - /* Collect the content stream (path operators). */ + /* Collect the content stream. The path coordinates are in + * SVG space (Y-down). Prepend a CTM that flips Y so the + * PDF page (Y-up) renders correctly. */ + float ex = draw->extents.x; + float ey = draw->extents.y; + float ew = draw->extents.width; + float eh = draw->extents.height; + hb_vector_t stream; - stream.alloc (draw->body.length + draw->path.length + 64); + stream.alloc (draw->body.length + draw->path.length + 128); + + /* Flip Y: map SVG y-range [ey .. ey+eh] to PDF [0 .. eh]. */ + hb_svg_append_str (&stream, "1 0 0 -1 0 "); + hb_svg_append_num (&stream, ey + eh, draw->precision); + hb_svg_append_str (&stream, " cm\n"); if (draw->body.length) hb_svg_append_len (&stream, draw->body.arrayZ, draw->body.length); @@ -713,13 +725,11 @@ hb_vector_draw_render_pdf (hb_vector_draw_t *draw) /* Object 3: Page */ offsets[2] = out.length; hb_svg_append_str (&out, "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox ["); - hb_svg_append_num (&out, draw->extents.x, draw->precision); + hb_svg_append_num (&out, ex, draw->precision); + hb_svg_append_str (&out, " 0 "); + hb_svg_append_num (&out, ex + ew, draw->precision); hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, draw->extents.y, draw->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, draw->extents.x + draw->extents.width, draw->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, draw->extents.y + draw->extents.height, draw->precision); + hb_svg_append_num (&out, eh, draw->precision); hb_svg_append_str (&out, "] /Contents 4 0 R >>\nendobj\n"); /* Object 4: Content stream */ From 4faf03e9e9dc08f2652ed63dd30d797794ba5795 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 20:19:11 -0600 Subject: [PATCH 03/23] [vector] Fix PDF fill inversion by negating Y in coordinates The previous CTM-based Y-flip reversed path winding, causing inverted fills. Instead, negate Y directly in append_xy for PDF format. This preserves winding direction and produces correct non-zero fill results. Assisted-by: Claude --- src/hb-vector-draw.hh | 2 ++ src/hb-vector-svg-draw.cc | 12 ++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/hb-vector-draw.hh b/src/hb-vector-draw.hh index e9294bdd2..33971f23a 100644 --- a/src/hb-vector-draw.hh +++ b/src/hb-vector-draw.hh @@ -32,6 +32,8 @@ struct hb_vector_draw_t { float tx, ty; hb_svg_transform_point (transform, x_scale_factor, y_scale_factor, x, y, &tx, &ty); + if (format == HB_VECTOR_FORMAT_PDF) + ty = -ty; /* PDF is Y-up; negate here to preserve winding. */ hb_svg_append_num (&path, tx, precision); hb_svg_append_c (&path, format == HB_VECTOR_FORMAT_PDF ? ' ' : ','); hb_svg_append_num (&path, ty, precision); diff --git a/src/hb-vector-svg-draw.cc b/src/hb-vector-svg-draw.cc index e68ea5073..fd47a9337 100644 --- a/src/hb-vector-svg-draw.cc +++ b/src/hb-vector-svg-draw.cc @@ -692,10 +692,7 @@ hb_vector_draw_render_pdf (hb_vector_draw_t *draw) hb_vector_t stream; stream.alloc (draw->body.length + draw->path.length + 128); - /* Flip Y: map SVG y-range [ey .. ey+eh] to PDF [0 .. eh]. */ - hb_svg_append_str (&stream, "1 0 0 -1 0 "); - hb_svg_append_num (&stream, ey + eh, draw->precision); - hb_svg_append_str (&stream, " cm\n"); + /* Y is negated in append_xy, so no CTM needed. */ if (draw->body.length) hb_svg_append_len (&stream, draw->body.arrayZ, draw->body.length); @@ -723,13 +720,16 @@ hb_vector_draw_render_pdf (hb_vector_draw_t *draw) hb_svg_append_str (&out, "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"); /* Object 3: Page */ + /* Y was negated in append_xy; SVG [ey..ey+eh] → PDF [-(ey+eh)..(-ey)]. */ offsets[2] = out.length; hb_svg_append_str (&out, "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox ["); hb_svg_append_num (&out, ex, draw->precision); - hb_svg_append_str (&out, " 0 "); + hb_svg_append_c (&out, ' '); + hb_svg_append_num (&out, -(ey + eh), draw->precision); + hb_svg_append_c (&out, ' '); hb_svg_append_num (&out, ex + ew, draw->precision); hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, eh, draw->precision); + hb_svg_append_num (&out, -ey, draw->precision); hb_svg_append_str (&out, "] /Contents 4 0 R >>\nendobj\n"); /* Object 4: Content stream */ From 0b91bb09fb183a44ac60d685ab28c691e4fe05d4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 20:21:49 -0600 Subject: [PATCH 04/23] [vector] Fix PDF coordinate system Font and PDF coordinates are both Y-up, so no Y-flip is needed in the path data. The pen position from the SVG-space layout is the only thing that needs negating. Remove the Y negation from append_xy and the CTM from the content stream. Set the glyph transform to identity with negated pen_y. Assisted-by: Claude --- src/hb-vector-draw.hh | 2 -- src/hb-vector-svg-draw.cc | 13 ++++--------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/hb-vector-draw.hh b/src/hb-vector-draw.hh index 33971f23a..e9294bdd2 100644 --- a/src/hb-vector-draw.hh +++ b/src/hb-vector-draw.hh @@ -32,8 +32,6 @@ struct hb_vector_draw_t { float tx, ty; hb_svg_transform_point (transform, x_scale_factor, y_scale_factor, x, y, &tx, &ty); - if (format == HB_VECTOR_FORMAT_PDF) - ty = -ty; /* PDF is Y-up; negate here to preserve winding. */ hb_svg_append_num (&path, tx, precision); hb_svg_append_c (&path, format == HB_VECTOR_FORMAT_PDF ? ' ' : ','); hb_svg_append_num (&path, ty, precision); diff --git a/src/hb-vector-svg-draw.cc b/src/hb-vector-svg-draw.cc index fd47a9337..4b5309ef5 100644 --- a/src/hb-vector-svg-draw.cc +++ b/src/hb-vector-svg-draw.cc @@ -548,15 +548,10 @@ hb_vector_draw_glyph (hb_vector_draw_t *draw, if (draw->format == HB_VECTOR_FORMAT_PDF) { - /* PDF: always inline, use transform to position. */ + /* PDF: always inline. Pen position is in SVG space (Y-down); + * negate it since font/PDF coords are Y-up. */ hb_transform_t<> saved = draw->transform; - float xx = draw->transform.xx; - float yx = draw->transform.yx; - float xy = draw->transform.xy; - float yy = draw->transform.yy; - float tx = draw->transform.x0 + xx * pen_x + xy * pen_y; - float ty = draw->transform.y0 + yx * pen_x + yy * pen_y; - draw->transform = {xx, yx, xy, yy, tx, ty}; + draw->transform = {1, 0, 0, 1, pen_x, -pen_y}; draw->path.clear (); hb_font_draw_glyph (font, glyph, hb_vector_draw_funcs_get (), draw); @@ -692,7 +687,7 @@ hb_vector_draw_render_pdf (hb_vector_draw_t *draw) hb_vector_t stream; stream.alloc (draw->body.length + draw->path.length + 128); - /* Y is negated in append_xy, so no CTM needed. */ + /* Path coords are in font space (Y-up); no CTM needed. */ if (draw->body.length) hb_svg_append_len (&stream, draw->body.arrayZ, draw->body.length); From ca9f1f49cf48629ebe4a8226cbcf904c5f3de09e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 20:49:45 -0600 Subject: [PATCH 05/23] =?UTF-8?q?[vector]=20Fix=20PDF=20pen=20position=20?= =?UTF-8?q?=E2=80=94=20no=20Y=20negation=20needed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The pen coordinates from vector-output.hh are in font space (Y-up), not SVG space. The Y-flip in compute_extents only affects the extents, not the pen positions passed to hb_vector_draw_glyph. Use pen_y directly. Assisted-by: Claude --- src/hb-vector-svg-draw.cc | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/hb-vector-svg-draw.cc b/src/hb-vector-svg-draw.cc index 4b5309ef5..e571cdba4 100644 --- a/src/hb-vector-svg-draw.cc +++ b/src/hb-vector-svg-draw.cc @@ -548,10 +548,9 @@ hb_vector_draw_glyph (hb_vector_draw_t *draw, if (draw->format == HB_VECTOR_FORMAT_PDF) { - /* PDF: always inline. Pen position is in SVG space (Y-down); - * negate it since font/PDF coords are Y-up. */ + /* PDF: always inline. Pen and font coords are both Y-up. */ hb_transform_t<> saved = draw->transform; - draw->transform = {1, 0, 0, 1, pen_x, -pen_y}; + draw->transform = {1, 0, 0, 1, pen_x, pen_y}; draw->path.clear (); hb_font_draw_glyph (font, glyph, hb_vector_draw_funcs_get (), draw); @@ -714,8 +713,8 @@ hb_vector_draw_render_pdf (hb_vector_draw_t *draw) offsets[1] = out.length; hb_svg_append_str (&out, "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"); - /* Object 3: Page */ - /* Y was negated in append_xy; SVG [ey..ey+eh] → PDF [-(ey+eh)..(-ey)]. */ + /* Object 3: Page. Extents are in SVG space (y = -font_y). + * Convert back: font Y range = [-(ey+eh) .. -ey]. */ offsets[2] = out.length; hb_svg_append_str (&out, "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox ["); hb_svg_append_num (&out, ex, draw->precision); From d530482115875f1ccb2cb3d5c8ab549f2cfab8fc Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 21:00:40 -0600 Subject: [PATCH 06/23] [vector] Extract hb_vector_paint_t to shared header Move the paint struct and color glyph cache key to hb-vector-paint.hh so the upcoming PDF paint backend can share the type. Convert the SVG-specific transform helper methods to free functions. No functional change. Assisted-by: Claude --- src/hb-vector-paint.hh | 91 +++++++++++++++++++++++ src/hb-vector-svg-paint.cc | 143 +++++++++---------------------------- 2 files changed, 125 insertions(+), 109 deletions(-) create mode 100644 src/hb-vector-paint.hh diff --git a/src/hb-vector-paint.hh b/src/hb-vector-paint.hh new file mode 100644 index 000000000..4f090d07a --- /dev/null +++ b/src/hb-vector-paint.hh @@ -0,0 +1,91 @@ +#ifndef HB_VECTOR_PAINT_HH +#define HB_VECTOR_PAINT_HH + +#include "hb.hh" + +#include "hb-vector.h" +#include "hb-geometry.hh" +#include "hb-machinery.hh" +#include "hb-map.hh" +#include "hb-vector-svg-utils.hh" +#include "hb-vector-svg.hh" + +struct hb_svg_color_glyph_cache_key_t +{ + hb_codepoint_t glyph = HB_CODEPOINT_INVALID; + unsigned palette = 0; + hb_color_t foreground = 0; + + hb_svg_color_glyph_cache_key_t () = default; + hb_svg_color_glyph_cache_key_t (hb_codepoint_t g, unsigned p, hb_color_t f) + : glyph (g), palette (p), foreground (f) {} + + bool operator == (const hb_svg_color_glyph_cache_key_t &o) const + { + return glyph == o.glyph && + palette == o.palette && + foreground == o.foreground; + } + + uint32_t hash () const + { + uint32_t h = hb_hash (glyph); + h = h * 31u + hb_hash (palette); + h = h * 31u + hb_hash (foreground); + return h; + } +}; + +struct hb_vector_paint_t +{ + hb_object_header_t header; + + hb_vector_format_t format = HB_VECTOR_FORMAT_SVG; + hb_transform_t<> transform = {1, 0, 0, 1, 0, 0}; + float x_scale_factor = 1.f; + float y_scale_factor = 1.f; + hb_vector_extents_t extents = {0, 0, 0, 0}; + bool has_extents = false; + + hb_color_t foreground = HB_COLOR (0, 0, 0, 255); + int palette = 0; + hb_hashmap_t custom_palette_colors; + unsigned precision = 2; + bool flat = false; + + hb_vector_t defs; + hb_vector_t path; + hb_vector_t> group_stack; + uint64_t transform_group_open_mask = 0; + unsigned transform_group_depth = 0; + unsigned transform_group_overflow_depth = 0; + + unsigned clip_rect_counter = 0; + unsigned gradient_counter = 0; + unsigned color_glyph_counter = 0; + unsigned color_glyph_depth = 0; + hb_set_t *defined_outlines = nullptr; + hb_set_t *defined_clips = nullptr; + hb_set_t *active_color_glyphs = nullptr; + hb_hashmap_t defined_color_glyphs; + hb_vector_t color_stops_scratch; + hb_vector_t subset_body_scratch; + hb_vector_t captured_scratch; + hb_blob_t *recycled_blob = nullptr; + bool current_color_glyph_has_svg_image = false; + hb_codepoint_t current_svg_image_glyph = HB_CODEPOINT_INVALID; + hb_face_t *current_face = nullptr; + unsigned svg_image_counter = 0; + + hb_vector_t ¤t_body () { return group_stack.tail (); } +}; + +/* Implemented in hb-vector-svg-paint.cc */ +hb_paint_funcs_t * hb_vector_svg_paint_funcs_get (); +hb_blob_t * hb_vector_svg_paint_render (hb_vector_paint_t *paint); + +/* Implemented in hb-vector-pdf-paint.cc */ +hb_paint_funcs_t * hb_vector_pdf_paint_funcs_get (); +hb_blob_t * hb_vector_pdf_paint_render (hb_vector_paint_t *paint); + +#endif /* HB_VECTOR_PAINT_HH */ diff --git a/src/hb-vector-svg-paint.cc b/src/hb-vector-svg-paint.cc index 4d8a3b09d..bc33a6e85 100644 --- a/src/hb-vector-svg-paint.cc +++ b/src/hb-vector-svg-paint.cc @@ -26,124 +26,49 @@ #include "hb.hh" -#include "hb-vector.h" +#include "hb-vector-paint.hh" #include "hb-blob.hh" -#include "hb-geometry.hh" -#include "hb-machinery.hh" -#include "hb-map.hh" #include "hb-vector-svg-path.hh" #include "hb-vector-svg-subset.hh" -#include "hb-vector-svg-utils.hh" -#include #include #include -#include "hb-vector-svg.hh" - -struct hb_svg_color_glyph_cache_key_t +static void +hb_svg_paint_append_global_transform_prefix (hb_vector_paint_t *paint, hb_vector_t *buf) { - hb_codepoint_t glyph = HB_CODEPOINT_INVALID; - unsigned palette = 0; - hb_color_t foreground = 0; + if (paint->transform.xx == 1.f && paint->transform.yx == 0.f && + paint->transform.xy == 0.f && paint->transform.yy == 1.f && + paint->transform.x0 == 0.f && paint->transform.y0 == 0.f && + paint->x_scale_factor == 1.f && paint->y_scale_factor == 1.f) + return; - hb_svg_color_glyph_cache_key_t () = default; - hb_svg_color_glyph_cache_key_t (hb_codepoint_t g, unsigned p, hb_color_t f) - : glyph (g), palette (p), foreground (f) {} + unsigned sprec = hb_svg_scale_precision (paint->precision); + hb_svg_append_str (buf, "transform.xx / paint->x_scale_factor, sprec, true); + hb_svg_append_c (buf, ','); + hb_svg_append_num (buf, paint->transform.yx / paint->y_scale_factor, sprec, true); + hb_svg_append_c (buf, ','); + hb_svg_append_num (buf, paint->transform.xy / paint->x_scale_factor, sprec, true); + hb_svg_append_c (buf, ','); + hb_svg_append_num (buf, paint->transform.yy / paint->y_scale_factor, sprec, true); + hb_svg_append_c (buf, ','); + hb_svg_append_num (buf, paint->transform.x0 / paint->x_scale_factor, paint->precision); + hb_svg_append_c (buf, ','); + hb_svg_append_num (buf, paint->transform.y0 / paint->y_scale_factor, paint->precision); + hb_svg_append_str (buf, ")\">\n"); +} - bool operator == (const hb_svg_color_glyph_cache_key_t &o) const - { - return glyph == o.glyph && - palette == o.palette && - foreground == o.foreground; - } - - uint32_t hash () const - { - uint32_t h = hb_hash (glyph); - h = h * 31u + hb_hash (palette); - h = h * 31u + hb_hash (foreground); - return h; - } -}; - -struct hb_vector_paint_t +static void +hb_svg_paint_append_global_transform_suffix (hb_vector_paint_t *paint, hb_vector_t *buf) { - hb_object_header_t header; - - hb_vector_format_t format = HB_VECTOR_FORMAT_SVG; - hb_transform_t<> transform = {1, 0, 0, 1, 0, 0}; - float x_scale_factor = 1.f; - float y_scale_factor = 1.f; - hb_vector_extents_t extents = {0, 0, 0, 0}; - bool has_extents = false; - - hb_color_t foreground = HB_COLOR (0, 0, 0, 255); - int palette = 0; - hb_hashmap_t custom_palette_colors; - unsigned precision = 2; - bool flat = false; - - hb_vector_t defs; - hb_vector_t path; - hb_vector_t> group_stack; - uint64_t transform_group_open_mask = 0; - unsigned transform_group_depth = 0; - unsigned transform_group_overflow_depth = 0; - - unsigned clip_rect_counter = 0; - unsigned gradient_counter = 0; - unsigned color_glyph_counter = 0; - unsigned color_glyph_depth = 0; - hb_set_t *defined_outlines = nullptr; - hb_set_t *defined_clips = nullptr; - hb_set_t *active_color_glyphs = nullptr; - hb_hashmap_t defined_color_glyphs; - hb_vector_t color_stops_scratch; - hb_vector_t subset_body_scratch; - hb_vector_t captured_scratch; - hb_blob_t *recycled_blob = nullptr; - bool current_color_glyph_has_svg_image = false; - hb_codepoint_t current_svg_image_glyph = HB_CODEPOINT_INVALID; - hb_face_t *current_face = nullptr; - unsigned svg_image_counter = 0; - - hb_vector_t ¤t_body () { return group_stack.tail (); } - - void append_global_transform_prefix (hb_vector_t *buf) - { - if (transform.xx == 1.f && transform.yx == 0.f && - transform.xy == 0.f && transform.yy == 1.f && - transform.x0 == 0.f && transform.y0 == 0.f && - x_scale_factor == 1.f && y_scale_factor == 1.f) - return; - - unsigned sprec = hb_svg_scale_precision (precision); - hb_svg_append_str (buf, "\n"); - } - - void append_global_transform_suffix (hb_vector_t *buf) - { - if (transform.xx == 1.f && transform.yx == 0.f && - transform.xy == 0.f && transform.yy == 1.f && - transform.x0 == 0.f && transform.y0 == 0.f && - x_scale_factor == 1.f && y_scale_factor == 1.f) - return; - hb_svg_append_str (buf, "\n"); - } -}; + if (paint->transform.xx == 1.f && paint->transform.yx == 0.f && + paint->transform.xy == 0.f && paint->transform.yy == 1.f && + paint->transform.x0 == 0.f && paint->transform.y0 == 0.f && + paint->x_scale_factor == 1.f && paint->y_scale_factor == 1.f) + return; + hb_svg_append_str (buf, "\n"); +} static inline uint64_t hb_svg_pack_color_glyph_cache_entry (unsigned def_id, @@ -1808,9 +1733,9 @@ hb_vector_paint_render (hb_vector_paint_t *paint) hb_svg_append_str (&out, "\n"); } - paint->append_global_transform_prefix (&out); + hb_svg_paint_append_global_transform_prefix (paint, &out); hb_svg_append_len (&out, paint->group_stack.arrayZ[0].arrayZ, paint->group_stack.arrayZ[0].length); - paint->append_global_transform_suffix (&out); + hb_svg_paint_append_global_transform_suffix (paint, &out); hb_svg_append_str (&out, "\n"); From e046d2ba6da515e95b2bd61795a3cd7d5573fff4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 21:05:14 -0600 Subject: [PATCH 07/23] [vector] Add PDF color font rendering via paint API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add hb-vector-pdf-paint.cc with a complete set of PDF paint callbacks for the HarfBuzz paint API: push/pop_transform → q + cm / Q push/pop_clip_glyph → q + path + W n / Q push/pop_clip_rect → q + re + W n / Q color → rg + re + f push/pop_group → q / Q gradients → solid fallback (first stop color) The PDF paint backend reuses the draw funcs for glyph clip path emission and shares the hb_vector_paint_t struct with the SVG backend via hb-vector-paint.hh. hb_vector_paint_glyph dispatches to SVG or PDF paint funcs based on format. hb_vector_paint_render delegates to the PDF render function for PDF output. Tested with COLRv0 (RocherColorGX) — color layers render correctly with proper clipping. Assisted-by: Claude --- src/hb-vector-pdf-paint.cc | 438 +++++++++++++++++++++++++++++++++++++ src/hb-vector-svg-paint.cc | 30 ++- src/meson.build | 1 + util/vector-output.hh | 3 +- 4 files changed, 469 insertions(+), 3 deletions(-) create mode 100644 src/hb-vector-pdf-paint.cc diff --git a/src/hb-vector-pdf-paint.cc b/src/hb-vector-pdf-paint.cc new file mode 100644 index 000000000..782751fc4 --- /dev/null +++ b/src/hb-vector-pdf-paint.cc @@ -0,0 +1,438 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#include "hb.hh" + +#include "hb-vector-paint.hh" +#include "hb-vector-draw.hh" + +#include + +/* PDF paint callbacks for COLRv0/v1 color glyphs. + * + * Maps the HarfBuzz paint API to PDF content stream operators: + * push_transform / pop_transform → q + cm / Q + * push_clip_glyph → q + path + W n + * push_clip_rectangle → q + re + W n + * pop_clip → Q + * color → rg + re + f + * push_group / pop_group → q / Q + * linear/radial/sweep gradient → solid fallback (first stop color) + */ + +/* ---- helpers ---- */ + +static hb_bool_t +hb_pdf_paint_ensure_initialized (hb_vector_paint_t *paint) +{ + if (paint->group_stack.length) + return !paint->group_stack.in_error () && + !paint->group_stack.tail ().in_error (); + if (unlikely (!paint->group_stack.push_or_fail ())) + return false; + paint->group_stack.tail ().alloc (4096); + if (unlikely (paint->group_stack.tail ().in_error ())) + { + paint->group_stack.pop (); + return false; + } + return !paint->group_stack.in_error (); +} + +static void +hb_pdf_append_num (hb_vector_t *buf, float v, unsigned precision) +{ + hb_svg_append_num (buf, v, precision); +} + +/* Emit a glyph outline as PDF path operators into buf. */ +static void +hb_pdf_emit_glyph_path (hb_vector_paint_t *paint, + hb_font_t *font, + hb_codepoint_t glyph, + hb_vector_t *buf) +{ + /* Reuse the draw struct's path emission. Build a temporary + * draw context with PDF format and identity transform. */ + hb_vector_draw_t tmp; + tmp.format = HB_VECTOR_FORMAT_PDF; + tmp.precision = paint->precision; + tmp.transform = {1, 0, 0, 1, 0, 0}; + tmp.path.alloc (1024); + + hb_font_draw_glyph (font, glyph, + hb_vector_draw_get_funcs (), + &tmp); + + hb_svg_append_len (buf, tmp.path.arrayZ, tmp.path.length); +} + +/* ---- paint callbacks ---- */ + +static void +hb_pdf_paint_push_transform (hb_paint_funcs_t *, + void *paint_data, + float xx, float yx, + float xy, float yy, + float dx, float dy, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + + auto &body = paint->current_body (); + hb_svg_append_str (&body, "q\n"); + hb_pdf_append_num (&body, xx, paint->precision); + hb_svg_append_c (&body, ' '); + hb_pdf_append_num (&body, yx, paint->precision); + hb_svg_append_c (&body, ' '); + hb_pdf_append_num (&body, xy, paint->precision); + hb_svg_append_c (&body, ' '); + hb_pdf_append_num (&body, yy, paint->precision); + hb_svg_append_c (&body, ' '); + hb_pdf_append_num (&body, dx, paint->precision); + hb_svg_append_c (&body, ' '); + hb_pdf_append_num (&body, dy, paint->precision); + hb_svg_append_str (&body, " cm\n"); +} + +static void +hb_pdf_paint_pop_transform (hb_paint_funcs_t *, + void *paint_data, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + hb_svg_append_str (&paint->current_body (), "Q\n"); +} + +static void +hb_pdf_paint_push_clip_glyph (hb_paint_funcs_t *, + void *paint_data, + hb_codepoint_t glyph, + hb_font_t *font, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + + auto &body = paint->current_body (); + hb_svg_append_str (&body, "q\n"); + hb_pdf_emit_glyph_path (paint, font, glyph, &body); + hb_svg_append_str (&body, "W n\n"); +} + +static void +hb_pdf_paint_push_clip_rectangle (hb_paint_funcs_t *, + void *paint_data, + float xmin, float ymin, + float xmax, float ymax, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + + auto &body = paint->current_body (); + hb_svg_append_str (&body, "q\n"); + hb_pdf_append_num (&body, xmin, paint->precision); + hb_svg_append_c (&body, ' '); + hb_pdf_append_num (&body, ymin, paint->precision); + hb_svg_append_c (&body, ' '); + hb_pdf_append_num (&body, xmax - xmin, paint->precision); + hb_svg_append_c (&body, ' '); + hb_pdf_append_num (&body, ymax - ymin, paint->precision); + hb_svg_append_str (&body, " re W n\n"); +} + +static void +hb_pdf_paint_pop_clip (hb_paint_funcs_t *, + void *paint_data, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + hb_svg_append_str (&paint->current_body (), "Q\n"); +} + +static void +hb_pdf_paint_solid_color (hb_vector_paint_t *paint, hb_color_t c) +{ + auto &body = paint->current_body (); + + float r = hb_color_get_red (c) / 255.f; + float g = hb_color_get_green (c) / 255.f; + float b = hb_color_get_blue (c) / 255.f; + float a = hb_color_get_alpha (c) / 255.f; + + /* Set fill color. */ + hb_pdf_append_num (&body, r, 4); + hb_svg_append_c (&body, ' '); + hb_pdf_append_num (&body, g, 4); + hb_svg_append_c (&body, ' '); + hb_pdf_append_num (&body, b, 4); + hb_svg_append_str (&body, " rg\n"); + + /* Paint a huge rect (will be clipped). */ + if (a >= 1.f / 255.f) + hb_svg_append_str (&body, "-32767 -32767 65534 65534 re f\n"); +} + +static void +hb_pdf_paint_color (hb_paint_funcs_t *, + void *paint_data, + hb_bool_t is_foreground, + hb_color_t color, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + + hb_color_t c = color; + if (is_foreground) + c = HB_COLOR (hb_color_get_blue (paint->foreground), + hb_color_get_green (paint->foreground), + hb_color_get_red (paint->foreground), + (unsigned) hb_color_get_alpha (paint->foreground) * + hb_color_get_alpha (color) / 255); + + hb_pdf_paint_solid_color (paint, c); +} + +static hb_bool_t +hb_pdf_paint_image (hb_paint_funcs_t *, + void *, + hb_blob_t *, + unsigned, unsigned, + hb_tag_t, + float, + hb_glyph_extents_t *, + void *) +{ + /* Images not supported in PDF output. */ + return false; +} + +/* Gradient fallback: use the first color stop. */ +static hb_color_t +hb_pdf_gradient_fallback_color (hb_vector_paint_t *paint, + hb_color_line_t *color_line) +{ + unsigned count = 1; + hb_color_stop_t stop; + hb_color_line_get_color_stops (color_line, 0, &count, &stop); + if (count) + return hb_color_line_get_extend (color_line) == HB_PAINT_EXTEND_PAD + ? stop.color : HB_COLOR (0, 0, 0, 255); + (void) paint; + return HB_COLOR (0, 0, 0, 255); +} + +static void +hb_pdf_paint_linear_gradient (hb_paint_funcs_t *, + void *paint_data, + hb_color_line_t *color_line, + float, float, float, float, float, float, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + hb_pdf_paint_solid_color (paint, hb_pdf_gradient_fallback_color (paint, color_line)); +} + +static void +hb_pdf_paint_radial_gradient (hb_paint_funcs_t *, + void *paint_data, + hb_color_line_t *color_line, + float, float, float, float, float, float, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + hb_pdf_paint_solid_color (paint, hb_pdf_gradient_fallback_color (paint, color_line)); +} + +static void +hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, + void *paint_data, + hb_color_line_t *color_line, + float, float, float, float, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + hb_pdf_paint_solid_color (paint, hb_pdf_gradient_fallback_color (paint, color_line)); +} + +static void +hb_pdf_paint_push_group (hb_paint_funcs_t *, + void *paint_data, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + hb_svg_append_str (&paint->current_body (), "q\n"); +} + +static void +hb_pdf_paint_pop_group (hb_paint_funcs_t *, + void *paint_data, + hb_paint_composite_mode_t mode HB_UNUSED, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + /* PDF 1.4 doesn't support compositing modes; just restore. */ + hb_svg_append_str (&paint->current_body (), "Q\n"); +} + +/* ---- lazy loader for paint funcs ---- */ + +static inline void free_static_pdf_paint_funcs (); + +static struct hb_pdf_paint_funcs_lazy_loader_t + : hb_paint_funcs_lazy_loader_t +{ + static hb_paint_funcs_t *create () + { + hb_paint_funcs_t *funcs = hb_paint_funcs_create (); + hb_paint_funcs_set_push_transform_func (funcs, (hb_paint_push_transform_func_t) hb_pdf_paint_push_transform, nullptr, nullptr); + hb_paint_funcs_set_pop_transform_func (funcs, (hb_paint_pop_transform_func_t) hb_pdf_paint_pop_transform, nullptr, nullptr); + hb_paint_funcs_set_push_clip_glyph_func (funcs, (hb_paint_push_clip_glyph_func_t) hb_pdf_paint_push_clip_glyph, nullptr, nullptr); + hb_paint_funcs_set_push_clip_rectangle_func (funcs, (hb_paint_push_clip_rectangle_func_t) hb_pdf_paint_push_clip_rectangle, nullptr, nullptr); + hb_paint_funcs_set_pop_clip_func (funcs, (hb_paint_pop_clip_func_t) hb_pdf_paint_pop_clip, nullptr, nullptr); + hb_paint_funcs_set_color_func (funcs, (hb_paint_color_func_t) hb_pdf_paint_color, nullptr, nullptr); + hb_paint_funcs_set_image_func (funcs, (hb_paint_image_func_t) hb_pdf_paint_image, nullptr, nullptr); + hb_paint_funcs_set_linear_gradient_func (funcs, (hb_paint_linear_gradient_func_t) hb_pdf_paint_linear_gradient, nullptr, nullptr); + hb_paint_funcs_set_radial_gradient_func (funcs, (hb_paint_radial_gradient_func_t) hb_pdf_paint_radial_gradient, nullptr, nullptr); + hb_paint_funcs_set_sweep_gradient_func (funcs, (hb_paint_sweep_gradient_func_t) hb_pdf_paint_sweep_gradient, nullptr, nullptr); + hb_paint_funcs_set_push_group_func (funcs, (hb_paint_push_group_func_t) hb_pdf_paint_push_group, nullptr, nullptr); + hb_paint_funcs_set_pop_group_func (funcs, (hb_paint_pop_group_func_t) hb_pdf_paint_pop_group, nullptr, nullptr); + hb_paint_funcs_make_immutable (funcs); + hb_atexit (free_static_pdf_paint_funcs); + return funcs; + } +} static_pdf_paint_funcs; + +static inline void +free_static_pdf_paint_funcs () +{ + static_pdf_paint_funcs.free_instance (); +} + +hb_paint_funcs_t * +hb_vector_pdf_paint_funcs_get () +{ + return static_pdf_paint_funcs.get_unconst (); +} + +/* ---- render ---- */ + +hb_blob_t * +hb_vector_pdf_paint_render (hb_vector_paint_t *paint) +{ + if (!paint->has_extents) + return nullptr; + if (!paint->group_stack.length || + !paint->group_stack.arrayZ[0].length) + return nullptr; + + /* The content stream is in group_stack[0]. */ + hb_vector_t &content = paint->group_stack.arrayZ[0]; + + float ex = paint->extents.x; + float ey = paint->extents.y; + float ew = paint->extents.width; + float eh = paint->extents.height; + + /* Build PDF. */ + hb_vector_t out; + hb_svg_recover_recycled_buffer (paint->recycled_blob, &out); + out.alloc (content.length + 512); + + unsigned offsets[5]; + + hb_svg_append_str (&out, "%PDF-1.4\n%\xC0\xC1\xC2\xC3\n"); + + offsets[0] = out.length; + hb_svg_append_str (&out, "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); + + offsets[1] = out.length; + hb_svg_append_str (&out, "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"); + + offsets[2] = out.length; + hb_svg_append_str (&out, "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox ["); + hb_pdf_append_num (&out, ex, paint->precision); + hb_svg_append_c (&out, ' '); + hb_pdf_append_num (&out, -(ey + eh), paint->precision); + hb_svg_append_c (&out, ' '); + hb_pdf_append_num (&out, ex + ew, paint->precision); + hb_svg_append_c (&out, ' '); + hb_pdf_append_num (&out, -ey, paint->precision); + hb_svg_append_str (&out, "] /Contents 4 0 R >>\nendobj\n"); + + offsets[3] = out.length; + hb_svg_append_str (&out, "4 0 obj\n<< /Length "); + hb_svg_append_unsigned (&out, content.length); + hb_svg_append_str (&out, " >>\nstream\n"); + hb_svg_append_len (&out, content.arrayZ, content.length); + hb_svg_append_str (&out, "endstream\nendobj\n"); + + unsigned xref_offset = out.length; + hb_svg_append_str (&out, "xref\n0 5\n"); + hb_svg_append_str (&out, "0000000000 65535 f \n"); + for (unsigned i = 0; i < 4; i++) + { + char tmp[21]; + snprintf (tmp, sizeof (tmp), "%010u 00000 n \n", offsets[i]); + hb_svg_append_len (&out, tmp, 20); + } + + hb_svg_append_str (&out, "trailer\n<< /Size 5 /Root 1 0 R >>\nstartxref\n"); + hb_svg_append_unsigned (&out, xref_offset); + hb_svg_append_str (&out, "\n%%EOF\n"); + + hb_blob_t *blob = hb_svg_blob_from_buffer (&paint->recycled_blob, &out); + + /* Reset state. */ + paint->group_stack.clear (); + paint->defs.clear (); + paint->path.clear (); + paint->has_extents = false; + paint->extents = {0, 0, 0, 0}; + + return blob; +} diff --git a/src/hb-vector-svg-paint.cc b/src/hb-vector-svg-paint.cc index bc33a6e85..d9749eafc 100644 --- a/src/hb-vector-svg-paint.cc +++ b/src/hb-vector-svg-paint.cc @@ -1074,7 +1074,7 @@ hb_vector_paint_color_glyph (hb_paint_funcs_t *, hb_vector_paint_t * hb_vector_paint_create_or_fail (hb_vector_format_t format) { - if (format != HB_VECTOR_FORMAT_SVG) + if (format != HB_VECTOR_FORMAT_SVG && format != HB_VECTOR_FORMAT_PDF) return nullptr; hb_vector_paint_t *paint = hb_object_create (); @@ -1508,6 +1508,32 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint, if (unlikely (!hb_vector_paint_ensure_initialized (paint))) return false; + if (paint->format == HB_VECTOR_FORMAT_PDF) + { + /* PDF: emit transform + paint directly, no caching. */ + auto &body = paint->current_body (); + hb_svg_append_str (&body, "q\n"); + hb_svg_append_num (&body, xx, paint->precision); + hb_svg_append_c (&body, ' '); + hb_svg_append_num (&body, yx, paint->precision); + hb_svg_append_c (&body, ' '); + hb_svg_append_num (&body, -xy, paint->precision); + hb_svg_append_c (&body, ' '); + hb_svg_append_num (&body, -yy, paint->precision); + hb_svg_append_c (&body, ' '); + hb_svg_append_num (&body, tx, paint->precision); + hb_svg_append_c (&body, ' '); + hb_svg_append_num (&body, ty, paint->precision); + hb_svg_append_str (&body, " cm\n"); + + hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph, + hb_vector_pdf_paint_funcs_get (), paint, + (unsigned) paint->palette, + paint->foreground); + hb_svg_append_str (&body, "Q\n"); + return ret; + } + bool can_cache = !paint->flat; hb_svg_color_glyph_cache_key_t cache_key = hb_svg_color_glyph_cache_key (glyph, (unsigned) paint->palette, @@ -1698,6 +1724,8 @@ hb_vector_paint_clear_render_state (hb_vector_paint_t *paint) hb_blob_t * hb_vector_paint_render (hb_vector_paint_t *paint) { + if (paint->format == HB_VECTOR_FORMAT_PDF) + return hb_vector_pdf_paint_render (paint); if (paint->format != HB_VECTOR_FORMAT_SVG) return nullptr; if (!paint->has_extents) diff --git a/src/meson.build b/src/meson.build index 740568db0..78430aa7d 100644 --- a/src/meson.build +++ b/src/meson.build @@ -756,6 +756,7 @@ hb_vector_sources = files( 'hb-vector-svg-draw.cc', 'hb-vector-svg-path.cc', 'hb-vector-svg-paint.cc', + 'hb-vector-pdf-paint.cc', 'hb-vector-svg-path.hh', 'hb-vector-svg-subset.cc', 'hb-vector-svg-subset.hh', diff --git a/util/vector-output.hh b/util/vector-output.hh index 5d374f96b..fad2338a5 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -184,8 +184,7 @@ struct vector_output_t : output_options_t<>, view_options_t fmt = HB_VECTOR_FORMAT_PDF; hb_vector_draw_t *draw = hb_vector_draw_create_or_fail (fmt); - hb_vector_paint_t *paint = (fmt == HB_VECTOR_FORMAT_SVG) - ? hb_vector_paint_create_or_fail (HB_VECTOR_FORMAT_SVG) : nullptr; + hb_vector_paint_t *paint = hb_vector_paint_create_or_fail (fmt); hb_vector_draw_set_scale_factor (draw, 1.f, 1.f); hb_vector_draw_set_extents (draw, &extents); From e197c13f8b16272505f11263f8f160c141dc0548 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 21:10:37 -0600 Subject: [PATCH 08/23] [vector] Fix PDF paint glyph Y-flip Remove the -xy/-yy negation from the PDF paint glyph positioning transform. Font and PDF coordinates are both Y-up, so no flip is needed. Same fix as was applied to the draw path earlier. Assisted-by: Claude --- src/hb-vector-svg-paint.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/hb-vector-svg-paint.cc b/src/hb-vector-svg-paint.cc index d9749eafc..734ddc12d 100644 --- a/src/hb-vector-svg-paint.cc +++ b/src/hb-vector-svg-paint.cc @@ -1513,13 +1513,14 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint, /* PDF: emit transform + paint directly, no caching. */ auto &body = paint->current_body (); hb_svg_append_str (&body, "q\n"); + /* Font and PDF coords are both Y-up; no negation needed. */ hb_svg_append_num (&body, xx, paint->precision); hb_svg_append_c (&body, ' '); hb_svg_append_num (&body, yx, paint->precision); hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, -xy, paint->precision); + hb_svg_append_num (&body, xy, paint->precision); hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, -yy, paint->precision); + hb_svg_append_num (&body, yy, paint->precision); hb_svg_append_c (&body, ' '); hb_svg_append_num (&body, tx, paint->precision); hb_svg_append_c (&body, ' '); From 30be6b8b5348ff766a1cd6518f8dde4fe0957b62 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 21:25:42 -0600 Subject: [PATCH 09/23] [vector] Add alpha, linear, and radial gradients to PDF paint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rewrite hb-vector-pdf-paint.cc with a proper PDF object system that supports dynamic resource generation: Alpha: Solid colors with alpha < 1 now emit ExtGState objects with /ca (fill opacity) and reference them via /GSn gs in the content stream. Gradients: Linear gradients → PDF Type 2 (axial) shading with Type 2/3 interpolation functions for color stops. Radial gradients → PDF Type 3 (radial) shading. Sweep gradients → solid fallback (no PDF equivalent). Architecture: A hb_pdf_resources_t collector tracks extra PDF objects (ExtGState, Shading, Function) during painting. At render time, these are emitted as numbered objects and referenced from the Page's Resources dictionary. Mark internal symbols HB_INTERNAL to fix check-symbols test. Assisted-by: Claude --- src/hb-vector-paint.hh | 4 +- src/hb-vector-pdf-paint.cc | 458 +++++++++++++++++++++++++++++++------ 2 files changed, 392 insertions(+), 70 deletions(-) diff --git a/src/hb-vector-paint.hh b/src/hb-vector-paint.hh index 4f090d07a..01a46ff42 100644 --- a/src/hb-vector-paint.hh +++ b/src/hb-vector-paint.hh @@ -85,7 +85,7 @@ hb_paint_funcs_t * hb_vector_svg_paint_funcs_get (); hb_blob_t * hb_vector_svg_paint_render (hb_vector_paint_t *paint); /* Implemented in hb-vector-pdf-paint.cc */ -hb_paint_funcs_t * hb_vector_pdf_paint_funcs_get (); -hb_blob_t * hb_vector_pdf_paint_render (hb_vector_paint_t *paint); +HB_INTERNAL hb_paint_funcs_t * hb_vector_pdf_paint_funcs_get (); +HB_INTERNAL hb_blob_t * hb_vector_pdf_paint_render (hb_vector_paint_t *paint); #endif /* HB_VECTOR_PAINT_HH */ diff --git a/src/hb-vector-pdf-paint.cc b/src/hb-vector-pdf-paint.cc index 782751fc4..cd829732e 100644 --- a/src/hb-vector-pdf-paint.cc +++ b/src/hb-vector-pdf-paint.cc @@ -30,19 +30,119 @@ #include "hb-vector-draw.hh" #include +#include -/* PDF paint callbacks for COLRv0/v1 color glyphs. +/* PDF paint backend for COLRv0/v1 color font rendering. * - * Maps the HarfBuzz paint API to PDF content stream operators: - * push_transform / pop_transform → q + cm / Q - * push_clip_glyph → q + path + W n - * push_clip_rectangle → q + re + W n - * pop_clip → Q - * color → rg + re + f - * push_group / pop_group → q / Q - * linear/radial/sweep gradient → solid fallback (first stop color) + * Supports: + * - Solid colors with alpha (via ExtGState) + * - Glyph and rectangle clipping + * - Affine transforms + * - Groups (save/restore) + * - Linear gradients (PDF Type 2 axial shading) + * - Radial gradients (PDF Type 3 radial shading) + * - Sweep gradients (approximated via solid fallback) */ + +/* ---- PDF object collector ---- */ + +struct hb_pdf_obj_t +{ + hb_vector_t data; +}; + +/* Collects extra PDF objects (shadings, functions, ExtGState) + * during painting. Referenced from the content stream by name + * (e.g. /SH0, /GS0) and emitted at render time. */ +struct hb_pdf_resources_t +{ + hb_vector_t objects; /* extra objects, starting at id 5 */ + hb_vector_t extgstate_dict; /* /GS0 5 0 R /GS1 6 0 R ... */ + hb_vector_t shading_dict; /* /SH0 7 0 R ... */ + unsigned extgstate_count = 0; + unsigned shading_count = 0; + + unsigned add_object (hb_vector_t &&obj_data) + { + unsigned id = 5 + objects.length; /* objects 1-4 are fixed */ + hb_pdf_obj_t obj; + obj.data = std::move (obj_data); + objects.push (std::move (obj)); + return id; + } + + /* Add ExtGState for fill opacity, return resource name index. */ + unsigned add_extgstate_alpha (float alpha, unsigned precision) + { + unsigned idx = extgstate_count++; + hb_vector_t obj; + hb_svg_append_str (&obj, "<< /Type /ExtGState /ca "); + hb_svg_append_num (&obj, alpha, 4); + hb_svg_append_str (&obj, " >>"); + unsigned obj_id = add_object (std::move (obj)); + + /* Add to resource dict. */ + hb_svg_append_str (&extgstate_dict, "/GS"); + hb_svg_append_unsigned (&extgstate_dict, idx); + hb_svg_append_c (&extgstate_dict, ' '); + hb_svg_append_unsigned (&extgstate_dict, obj_id); + hb_svg_append_str (&extgstate_dict, " 0 R "); + (void) precision; + return idx; + } + + /* Add a shading + function for a gradient, return resource name index. */ + unsigned add_shading (hb_vector_t &&shading_data) + { + unsigned idx = shading_count++; + unsigned obj_id = add_object (std::move (shading_data)); + + hb_svg_append_str (&shading_dict, "/SH"); + hb_svg_append_unsigned (&shading_dict, idx); + hb_svg_append_c (&shading_dict, ' '); + hb_svg_append_unsigned (&shading_dict, obj_id); + hb_svg_append_str (&shading_dict, " 0 R "); + return idx; + } +}; + +/* Store resources pointer in the paint struct's defs buffer + * (repurposed — defs is unused for PDF). */ +static hb_pdf_resources_t * +hb_pdf_get_resources (hb_vector_paint_t *paint) +{ + if (!paint->defs.length) + { + /* First call: allocate and store. */ + auto *res = (hb_pdf_resources_t *) hb_calloc (1, sizeof (hb_pdf_resources_t)); + if (unlikely (!res)) return nullptr; + new (res) hb_pdf_resources_t (); + paint->defs.resize (sizeof (void *)); + memcpy (paint->defs.arrayZ, &res, sizeof (void *)); + } + hb_pdf_resources_t *res; + memcpy (&res, paint->defs.arrayZ, sizeof (void *)); + return res; +} + +static void +hb_pdf_free_resources (hb_vector_paint_t *paint) +{ + if (paint->defs.length >= sizeof (void *)) + { + hb_pdf_resources_t *res; + memcpy (&res, paint->defs.arrayZ, sizeof (void *)); + if (res) + { + res->~hb_pdf_resources_t (); + hb_free (res); + } + } + paint->defs.clear (); +} + + /* ---- helpers ---- */ static hb_bool_t @@ -62,12 +162,6 @@ hb_pdf_paint_ensure_initialized (hb_vector_paint_t *paint) return !paint->group_stack.in_error (); } -static void -hb_pdf_append_num (hb_vector_t *buf, float v, unsigned precision) -{ - hb_svg_append_num (buf, v, precision); -} - /* Emit a glyph outline as PDF path operators into buf. */ static void hb_pdf_emit_glyph_path (hb_vector_paint_t *paint, @@ -75,8 +169,6 @@ hb_pdf_emit_glyph_path (hb_vector_paint_t *paint, hb_codepoint_t glyph, hb_vector_t *buf) { - /* Reuse the draw struct's path emission. Build a temporary - * draw context with PDF format and identity transform. */ hb_vector_draw_t tmp; tmp.format = HB_VECTOR_FORMAT_PDF; tmp.precision = paint->precision; @@ -90,6 +182,7 @@ hb_pdf_emit_glyph_path (hb_vector_paint_t *paint, hb_svg_append_len (buf, tmp.path.arrayZ, tmp.path.length); } + /* ---- paint callbacks ---- */ static void @@ -106,17 +199,17 @@ hb_pdf_paint_push_transform (hb_paint_funcs_t *, auto &body = paint->current_body (); hb_svg_append_str (&body, "q\n"); - hb_pdf_append_num (&body, xx, paint->precision); + hb_svg_append_num (&body, xx, paint->precision); hb_svg_append_c (&body, ' '); - hb_pdf_append_num (&body, yx, paint->precision); + hb_svg_append_num (&body, yx, paint->precision); hb_svg_append_c (&body, ' '); - hb_pdf_append_num (&body, xy, paint->precision); + hb_svg_append_num (&body, xy, paint->precision); hb_svg_append_c (&body, ' '); - hb_pdf_append_num (&body, yy, paint->precision); + hb_svg_append_num (&body, yy, paint->precision); hb_svg_append_c (&body, ' '); - hb_pdf_append_num (&body, dx, paint->precision); + hb_svg_append_num (&body, dx, paint->precision); hb_svg_append_c (&body, ' '); - hb_pdf_append_num (&body, dy, paint->precision); + hb_svg_append_num (&body, dy, paint->precision); hb_svg_append_str (&body, " cm\n"); } @@ -161,13 +254,13 @@ hb_pdf_paint_push_clip_rectangle (hb_paint_funcs_t *, auto &body = paint->current_body (); hb_svg_append_str (&body, "q\n"); - hb_pdf_append_num (&body, xmin, paint->precision); + hb_svg_append_num (&body, xmin, paint->precision); hb_svg_append_c (&body, ' '); - hb_pdf_append_num (&body, ymin, paint->precision); + hb_svg_append_num (&body, ymin, paint->precision); hb_svg_append_c (&body, ' '); - hb_pdf_append_num (&body, xmax - xmin, paint->precision); + hb_svg_append_num (&body, xmax - xmin, paint->precision); hb_svg_append_c (&body, ' '); - hb_pdf_append_num (&body, ymax - ymin, paint->precision); + hb_svg_append_num (&body, ymax - ymin, paint->precision); hb_svg_append_str (&body, " re W n\n"); } @@ -182,6 +275,7 @@ hb_pdf_paint_pop_clip (hb_paint_funcs_t *, hb_svg_append_str (&paint->current_body (), "Q\n"); } +/* Paint a solid color, including alpha via ExtGState. */ static void hb_pdf_paint_solid_color (hb_vector_paint_t *paint, hb_color_t c) { @@ -192,17 +286,32 @@ hb_pdf_paint_solid_color (hb_vector_paint_t *paint, hb_color_t c) float b = hb_color_get_blue (c) / 255.f; float a = hb_color_get_alpha (c) / 255.f; + if (a < 1.f / 255.f) + return; + + /* Set alpha via ExtGState if needed. */ + if (a < 1.f - 1.f / 512.f) + { + auto *res = hb_pdf_get_resources (paint); + if (res) + { + unsigned gs_idx = res->add_extgstate_alpha (a, paint->precision); + hb_svg_append_str (&body, "/GS"); + hb_svg_append_unsigned (&body, gs_idx); + hb_svg_append_str (&body, " gs\n"); + } + } + /* Set fill color. */ - hb_pdf_append_num (&body, r, 4); + hb_svg_append_num (&body, r, 4); hb_svg_append_c (&body, ' '); - hb_pdf_append_num (&body, g, 4); + hb_svg_append_num (&body, g, 4); hb_svg_append_c (&body, ' '); - hb_pdf_append_num (&body, b, 4); + hb_svg_append_num (&body, b, 4); hb_svg_append_str (&body, " rg\n"); /* Paint a huge rect (will be clipped). */ - if (a >= 1.f / 255.f) - hb_svg_append_str (&body, "-32767 -32767 65534 65534 re f\n"); + hb_svg_append_str (&body, "-32767 -32767 65534 65534 re f\n"); } static void @@ -237,49 +346,209 @@ hb_pdf_paint_image (hb_paint_funcs_t *, hb_glyph_extents_t *, void *) { - /* Images not supported in PDF output. */ return false; } -/* Gradient fallback: use the first color stop. */ -static hb_color_t -hb_pdf_gradient_fallback_color (hb_vector_paint_t *paint, +/* ---- Gradient helpers ---- */ + +/* Build a PDF Type 2 (exponential interpolation) function for + * a single color stop pair. */ +static void +hb_pdf_build_interpolation_function (hb_vector_t *obj, + hb_color_t c0, hb_color_t c1) +{ + hb_svg_append_str (obj, "<< /FunctionType 2 /Domain [0 1] /N 1\n"); + hb_svg_append_str (obj, "/C0 ["); + hb_svg_append_num (obj, hb_color_get_red (c0) / 255.f, 4); + hb_svg_append_c (obj, ' '); + hb_svg_append_num (obj, hb_color_get_green (c0) / 255.f, 4); + hb_svg_append_c (obj, ' '); + hb_svg_append_num (obj, hb_color_get_blue (c0) / 255.f, 4); + hb_svg_append_str (obj, "]\n/C1 ["); + hb_svg_append_num (obj, hb_color_get_red (c1) / 255.f, 4); + hb_svg_append_c (obj, ' '); + hb_svg_append_num (obj, hb_color_get_green (c1) / 255.f, 4); + hb_svg_append_c (obj, ' '); + hb_svg_append_num (obj, hb_color_get_blue (c1) / 255.f, 4); + hb_svg_append_str (obj, "] >>"); +} + +/* Build a stitching function (Type 3) for multiple color stops. */ +static unsigned +hb_pdf_build_gradient_function (hb_pdf_resources_t *res, + hb_vector_paint_t *paint, hb_color_line_t *color_line) { - unsigned count = 1; - hb_color_stop_t stop; - hb_color_line_get_color_stops (color_line, 0, &count, &stop); - if (count) - return hb_color_line_get_extend (color_line) == HB_PAINT_EXTEND_PAD - ? stop.color : HB_COLOR (0, 0, 0, 255); - (void) paint; - return HB_COLOR (0, 0, 0, 255); + unsigned count = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr); + paint->color_stops_scratch.resize (count); + hb_color_line_get_color_stops (color_line, 0, &count, paint->color_stops_scratch.arrayZ); + + if (count < 2) + { + /* Single stop: constant function. */ + hb_color_t c = count ? paint->color_stops_scratch.arrayZ[0].color + : HB_COLOR (0, 0, 0, 255); + hb_vector_t obj; + hb_pdf_build_interpolation_function (&obj, c, c); + return res->add_object (std::move (obj)); + } + + /* Sort by offset. */ + paint->color_stops_scratch.as_array ().qsort ( + [] (const hb_color_stop_t &a, const hb_color_stop_t &b) + { return a.offset < b.offset; }); + + if (count == 2) + { + /* Two stops: single interpolation function. */ + hb_vector_t obj; + hb_pdf_build_interpolation_function (&obj, + paint->color_stops_scratch.arrayZ[0].color, + paint->color_stops_scratch.arrayZ[1].color); + return res->add_object (std::move (obj)); + } + + /* Multiple stops: create sub-functions and stitch. */ + hb_vector_t sub_func_ids; + for (unsigned i = 0; i + 1 < count; i++) + { + hb_vector_t sub; + hb_pdf_build_interpolation_function (&sub, + paint->color_stops_scratch.arrayZ[i].color, + paint->color_stops_scratch.arrayZ[i + 1].color); + sub_func_ids.push (res->add_object (std::move (sub))); + } + + /* Stitching function (Type 3). */ + hb_vector_t obj; + hb_svg_append_str (&obj, "<< /FunctionType 3 /Domain [0 1]\n"); + + /* Functions array. */ + hb_svg_append_str (&obj, "/Functions ["); + for (unsigned i = 0; i < sub_func_ids.length; i++) + { + if (i) hb_svg_append_c (&obj, ' '); + hb_svg_append_unsigned (&obj, sub_func_ids.arrayZ[i]); + hb_svg_append_str (&obj, " 0 R"); + } + hb_svg_append_str (&obj, "]\n"); + + /* Bounds. */ + hb_svg_append_str (&obj, "/Bounds ["); + for (unsigned i = 1; i + 1 < count; i++) + { + if (i > 1) hb_svg_append_c (&obj, ' '); + hb_svg_append_num (&obj, paint->color_stops_scratch.arrayZ[i].offset, 4); + } + hb_svg_append_str (&obj, "]\n"); + + /* Encode array. */ + hb_svg_append_str (&obj, "/Encode ["); + for (unsigned i = 0; i + 1 < count; i++) + { + if (i) hb_svg_append_c (&obj, ' '); + hb_svg_append_str (&obj, "0 1"); + } + hb_svg_append_str (&obj, "] >>"); + + return res->add_object (std::move (obj)); } static void hb_pdf_paint_linear_gradient (hb_paint_funcs_t *, void *paint_data, hb_color_line_t *color_line, - float, float, float, float, float, float, + float x0, float y0, + float x1, float y1, + float x2 HB_UNUSED, float y2 HB_UNUSED, void *) { auto *paint = (hb_vector_paint_t *) paint_data; if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) return; - hb_pdf_paint_solid_color (paint, hb_pdf_gradient_fallback_color (paint, color_line)); + auto *res = hb_pdf_get_resources (paint); + if (unlikely (!res)) + return; + + unsigned func_id = hb_pdf_build_gradient_function (res, paint, color_line); + + /* Build Type 2 (axial) shading. */ + hb_vector_t sh; + hb_svg_append_str (&sh, "<< /ShadingType 2 /ColorSpace /DeviceRGB\n"); + hb_svg_append_str (&sh, "/Coords ["); + hb_svg_append_num (&sh, x0, paint->precision); + hb_svg_append_c (&sh, ' '); + hb_svg_append_num (&sh, y0, paint->precision); + hb_svg_append_c (&sh, ' '); + hb_svg_append_num (&sh, x1, paint->precision); + hb_svg_append_c (&sh, ' '); + hb_svg_append_num (&sh, y1, paint->precision); + hb_svg_append_str (&sh, "]\n/Function "); + hb_svg_append_unsigned (&sh, func_id); + hb_svg_append_str (&sh, " 0 R\n"); + + hb_paint_extend_t extend = hb_color_line_get_extend (color_line); + if (extend == HB_PAINT_EXTEND_PAD) + hb_svg_append_str (&sh, "/Extend [true true]\n"); + + hb_svg_append_str (&sh, ">>"); + + unsigned sh_idx = res->add_shading (std::move (sh)); + + auto &body = paint->current_body (); + hb_svg_append_str (&body, "/SH"); + hb_svg_append_unsigned (&body, sh_idx); + hb_svg_append_str (&body, " sh\n"); } static void hb_pdf_paint_radial_gradient (hb_paint_funcs_t *, void *paint_data, hb_color_line_t *color_line, - float, float, float, float, float, float, + float x0, float y0, float r0, + float x1, float y1, float r1, void *) { auto *paint = (hb_vector_paint_t *) paint_data; if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) return; - hb_pdf_paint_solid_color (paint, hb_pdf_gradient_fallback_color (paint, color_line)); + auto *res = hb_pdf_get_resources (paint); + if (unlikely (!res)) + return; + + unsigned func_id = hb_pdf_build_gradient_function (res, paint, color_line); + + /* Build Type 3 (radial) shading. */ + hb_vector_t sh; + hb_svg_append_str (&sh, "<< /ShadingType 3 /ColorSpace /DeviceRGB\n"); + hb_svg_append_str (&sh, "/Coords ["); + hb_svg_append_num (&sh, x0, paint->precision); + hb_svg_append_c (&sh, ' '); + hb_svg_append_num (&sh, y0, paint->precision); + hb_svg_append_c (&sh, ' '); + hb_svg_append_num (&sh, r0, paint->precision); + hb_svg_append_c (&sh, ' '); + hb_svg_append_num (&sh, x1, paint->precision); + hb_svg_append_c (&sh, ' '); + hb_svg_append_num (&sh, y1, paint->precision); + hb_svg_append_c (&sh, ' '); + hb_svg_append_num (&sh, r1, paint->precision); + hb_svg_append_str (&sh, "]\n/Function "); + hb_svg_append_unsigned (&sh, func_id); + hb_svg_append_str (&sh, " 0 R\n"); + + hb_paint_extend_t extend = hb_color_line_get_extend (color_line); + if (extend == HB_PAINT_EXTEND_PAD) + hb_svg_append_str (&sh, "/Extend [true true]\n"); + + hb_svg_append_str (&sh, ">>"); + + unsigned sh_idx = res->add_shading (std::move (sh)); + + auto &body = paint->current_body (); + hb_svg_append_str (&body, "/SH"); + hb_svg_append_unsigned (&body, sh_idx); + hb_svg_append_str (&body, " sh\n"); } static void @@ -289,10 +558,15 @@ hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, float, float, float, float, void *) { + /* PDF has no native sweep gradient. Fall back to first stop. */ auto *paint = (hb_vector_paint_t *) paint_data; if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) return; - hb_pdf_paint_solid_color (paint, hb_pdf_gradient_fallback_color (paint, color_line)); + + unsigned count = 1; + hb_color_stop_t stop; + hb_color_line_get_color_stops (color_line, 0, &count, &stop); + hb_pdf_paint_solid_color (paint, count ? stop.color : HB_COLOR (0, 0, 0, 255)); } static void @@ -315,10 +589,10 @@ hb_pdf_paint_pop_group (hb_paint_funcs_t *, auto *paint = (hb_vector_paint_t *) paint_data; if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) return; - /* PDF 1.4 doesn't support compositing modes; just restore. */ hb_svg_append_str (&paint->current_body (), "Q\n"); } + /* ---- lazy loader for paint funcs ---- */ static inline void free_static_pdf_paint_funcs (); @@ -359,6 +633,7 @@ hb_vector_pdf_paint_funcs_get () return static_pdf_paint_funcs.get_unconst (); } + /* ---- render ---- */ hb_blob_t * @@ -370,66 +645,113 @@ hb_vector_pdf_paint_render (hb_vector_paint_t *paint) !paint->group_stack.arrayZ[0].length) return nullptr; - /* The content stream is in group_stack[0]. */ hb_vector_t &content = paint->group_stack.arrayZ[0]; + hb_pdf_resources_t *res = hb_pdf_get_resources (paint); float ex = paint->extents.x; float ey = paint->extents.y; float ew = paint->extents.width; float eh = paint->extents.height; + unsigned num_extra = res ? res->objects.length : 0; + unsigned total_objects = 4 + num_extra; /* 1-based: 1..total_objects */ + /* Build PDF. */ hb_vector_t out; hb_svg_recover_recycled_buffer (paint->recycled_blob, &out); - out.alloc (content.length + 512); + out.alloc (content.length + num_extra * 128 + 1024); - unsigned offsets[5]; + hb_vector_t offsets; + offsets.resize (total_objects); hb_svg_append_str (&out, "%PDF-1.4\n%\xC0\xC1\xC2\xC3\n"); - offsets[0] = out.length; + /* Object 1: Catalog */ + offsets.arrayZ[0] = out.length; hb_svg_append_str (&out, "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); - offsets[1] = out.length; + /* Object 2: Pages */ + offsets.arrayZ[1] = out.length; hb_svg_append_str (&out, "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"); - offsets[2] = out.length; + /* Object 3: Page */ + offsets.arrayZ[2] = out.length; hb_svg_append_str (&out, "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox ["); - hb_pdf_append_num (&out, ex, paint->precision); + hb_svg_append_num (&out, ex, paint->precision); hb_svg_append_c (&out, ' '); - hb_pdf_append_num (&out, -(ey + eh), paint->precision); + hb_svg_append_num (&out, -(ey + eh), paint->precision); hb_svg_append_c (&out, ' '); - hb_pdf_append_num (&out, ex + ew, paint->precision); + hb_svg_append_num (&out, ex + ew, paint->precision); hb_svg_append_c (&out, ' '); - hb_pdf_append_num (&out, -ey, paint->precision); - hb_svg_append_str (&out, "] /Contents 4 0 R >>\nendobj\n"); + hb_svg_append_num (&out, -ey, paint->precision); + hb_svg_append_str (&out, "]\n/Contents 4 0 R"); - offsets[3] = out.length; + /* Resources. */ + bool has_resources = res && + (res->extgstate_dict.length || res->shading_dict.length); + if (has_resources) + { + hb_svg_append_str (&out, "\n/Resources <<"); + if (res->extgstate_dict.length) + { + hb_svg_append_str (&out, " /ExtGState << "); + hb_svg_append_len (&out, res->extgstate_dict.arrayZ, res->extgstate_dict.length); + hb_svg_append_str (&out, ">>"); + } + if (res->shading_dict.length) + { + hb_svg_append_str (&out, " /Shading << "); + hb_svg_append_len (&out, res->shading_dict.arrayZ, res->shading_dict.length); + hb_svg_append_str (&out, ">>"); + } + hb_svg_append_str (&out, " >>"); + } + + hb_svg_append_str (&out, " >>\nendobj\n"); + + /* Object 4: Content stream */ + offsets.arrayZ[3] = out.length; hb_svg_append_str (&out, "4 0 obj\n<< /Length "); hb_svg_append_unsigned (&out, content.length); hb_svg_append_str (&out, " >>\nstream\n"); hb_svg_append_len (&out, content.arrayZ, content.length); hb_svg_append_str (&out, "endstream\nendobj\n"); + /* Extra objects (functions, shadings, ExtGState). */ + for (unsigned i = 0; i < num_extra; i++) + { + offsets.arrayZ[4 + i] = out.length; + hb_svg_append_unsigned (&out, 5 + i); + hb_svg_append_str (&out, " 0 obj\n"); + auto &obj = res->objects.arrayZ[i]; + hb_svg_append_len (&out, obj.data.arrayZ, obj.data.length); + hb_svg_append_str (&out, "\nendobj\n"); + } + + /* Cross-reference table */ unsigned xref_offset = out.length; - hb_svg_append_str (&out, "xref\n0 5\n"); - hb_svg_append_str (&out, "0000000000 65535 f \n"); - for (unsigned i = 0; i < 4; i++) + hb_svg_append_str (&out, "xref\n0 "); + hb_svg_append_unsigned (&out, total_objects + 1); + hb_svg_append_str (&out, "\n0000000000 65535 f \n"); + for (unsigned i = 0; i < total_objects; i++) { char tmp[21]; - snprintf (tmp, sizeof (tmp), "%010u 00000 n \n", offsets[i]); + snprintf (tmp, sizeof (tmp), "%010u 00000 n \n", offsets.arrayZ[i]); hb_svg_append_len (&out, tmp, 20); } - hb_svg_append_str (&out, "trailer\n<< /Size 5 /Root 1 0 R >>\nstartxref\n"); + /* Trailer */ + hb_svg_append_str (&out, "trailer\n<< /Size "); + hb_svg_append_unsigned (&out, total_objects + 1); + hb_svg_append_str (&out, " /Root 1 0 R >>\nstartxref\n"); hb_svg_append_unsigned (&out, xref_offset); hb_svg_append_str (&out, "\n%%EOF\n"); hb_blob_t *blob = hb_svg_blob_from_buffer (&paint->recycled_blob, &out); /* Reset state. */ + hb_pdf_free_resources (paint); paint->group_stack.clear (); - paint->defs.clear (); paint->path.clear (); paint->has_extents = false; paint->extents = {0, 0, 0, 0}; From 4086e6280872b28a16f6b8bda02deff7ae176360 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 21:33:07 -0600 Subject: [PATCH 10/23] [vector] Implement PDF sweep gradient via sector decomposition Decompose sweep gradients into 360 colored pie-slice sectors. Each sector evaluates the color line at its midpoint and emits a filled triangular path from the center to two points on a large-radius arc. Includes a color line evaluator that handles pad, repeat, and reflect extend modes with linear interpolation between stops. Assisted-by: Claude --- src/hb-vector-pdf-paint.cc | 131 +++++++++++++++++++++++++++++++++++-- 1 file changed, 125 insertions(+), 6 deletions(-) diff --git a/src/hb-vector-pdf-paint.cc b/src/hb-vector-pdf-paint.cc index cd829732e..07d745bd0 100644 --- a/src/hb-vector-pdf-paint.cc +++ b/src/hb-vector-pdf-paint.cc @@ -551,22 +551,141 @@ hb_pdf_paint_radial_gradient (hb_paint_funcs_t *, hb_svg_append_str (&body, " sh\n"); } +/* Evaluate color line at a given offset. */ +static hb_color_t +hb_pdf_evaluate_color_line (hb_color_stop_t *stops, unsigned len, + float t, hb_paint_extend_t extend) +{ + if (!len) + return HB_COLOR (0, 0, 0, 255); + + /* Handle extend modes. */ + if (extend == HB_PAINT_EXTEND_REPEAT) + t = t - floorf (t); + else if (extend == HB_PAINT_EXTEND_REFLECT) + { + t = t - floorf (t); + if (((int) floorf (t * 2.f)) & 1) + t = 1.f - t; + } + else /* PAD */ + t = hb_clamp (t, 0.f, 1.f); + + if (t <= stops[0].offset) + return stops[0].color; + if (t >= stops[len - 1].offset) + return stops[len - 1].color; + + /* Find enclosing stops. */ + unsigned i = 0; + for (; i + 1 < len; i++) + if (stops[i + 1].offset >= t) + break; + + float range = stops[i + 1].offset - stops[i].offset; + float frac = range > 0.f ? (t - stops[i].offset) / range : 0.f; + + hb_color_t c0 = stops[i].color; + hb_color_t c1 = stops[i + 1].color; + auto lerp = [&] (unsigned shift) -> unsigned { + unsigned v0 = (c0 >> shift) & 0xFF; + unsigned v1 = (c1 >> shift) & 0xFF; + return (unsigned) (v0 + frac * ((float) v1 - (float) v0) + 0.5f); + }; + return HB_COLOR (lerp (16), lerp (8), lerp (0), lerp (24)); +} + static void hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, void *paint_data, hb_color_line_t *color_line, - float, float, float, float, + float cx, float cy, + float start_angle, + float end_angle, void *) { - /* PDF has no native sweep gradient. Fall back to first stop. */ auto *paint = (hb_vector_paint_t *) paint_data; if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) return; - unsigned count = 1; - hb_color_stop_t stop; - hb_color_line_get_color_stops (color_line, 0, &count, &stop); - hb_pdf_paint_solid_color (paint, count ? stop.color : HB_COLOR (0, 0, 0, 255)); + /* Get and sort color stops. */ + unsigned count = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr); + paint->color_stops_scratch.resize (count); + hb_color_line_get_color_stops (color_line, 0, &count, paint->color_stops_scratch.arrayZ); + paint->color_stops_scratch.as_array ().qsort ( + [] (const hb_color_stop_t &a, const hb_color_stop_t &b) + { return a.offset < b.offset; }); + + hb_paint_extend_t extend = hb_color_line_get_extend (color_line); + + /* Decompose into colored pie-slice sectors. */ + const unsigned NUM_SECTORS = 360; + float R = 32767.f; /* large enough to cover any clip */ + + auto &body = paint->current_body (); + + float angle_range = end_angle - start_angle; + if (fabsf (angle_range) < 1e-10f) + return; + + for (unsigned i = 0; i < NUM_SECTORS; i++) + { + float t0 = (float) i / NUM_SECTORS; + float t1 = (float) (i + 1) / NUM_SECTORS; + float a0 = start_angle + t0 * angle_range; + float a1 = start_angle + t1 * angle_range; + + /* Evaluate color at midpoint of sector. */ + float t_mid = (t0 + t1) * 0.5f; + hb_color_t c = hb_pdf_evaluate_color_line (paint->color_stops_scratch.arrayZ, + count, t_mid, extend); + + float alpha = hb_color_get_alpha (c) / 255.f; + if (alpha < 1.f / 255.f) + continue; + + /* Set alpha if needed. */ + if (alpha < 1.f - 1.f / 512.f) + { + auto *res = hb_pdf_get_resources (paint); + if (res) + { + unsigned gs_idx = res->add_extgstate_alpha (alpha, paint->precision); + hb_svg_append_str (&body, "/GS"); + hb_svg_append_unsigned (&body, gs_idx); + hb_svg_append_str (&body, " gs\n"); + } + } + + /* Set fill color. */ + hb_svg_append_num (&body, hb_color_get_red (c) / 255.f, 4); + hb_svg_append_c (&body, ' '); + hb_svg_append_num (&body, hb_color_get_green (c) / 255.f, 4); + hb_svg_append_c (&body, ' '); + hb_svg_append_num (&body, hb_color_get_blue (c) / 255.f, 4); + hb_svg_append_str (&body, " rg\n"); + + /* Pie-slice path: center → outer edge at a0 → outer edge at a1 → close. */ + float cos0 = cosf (a0), sin0 = sinf (a0); + float cos1 = cosf (a1), sin1 = sinf (a1); + + hb_svg_append_num (&body, cx, paint->precision); + hb_svg_append_c (&body, ' '); + hb_svg_append_num (&body, cy, paint->precision); + hb_svg_append_str (&body, " m\n"); + + hb_svg_append_num (&body, cx + R * cos0, paint->precision); + hb_svg_append_c (&body, ' '); + hb_svg_append_num (&body, cy + R * sin0, paint->precision); + hb_svg_append_str (&body, " l\n"); + + hb_svg_append_num (&body, cx + R * cos1, paint->precision); + hb_svg_append_c (&body, ' '); + hb_svg_append_num (&body, cy + R * sin1, paint->precision); + hb_svg_append_str (&body, " l\n"); + + hb_svg_append_str (&body, "h f\n"); + } } static void From 6abf48a9e5bd7ac9466b0f54aefb35d4b25e85d7 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 21:38:51 -0600 Subject: [PATCH 11/23] [vector] Implement PDF sweep gradient via Type 6 Coons patches MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace the pie-slice sector fallback with proper Type 6 Coons patch mesh shading. Each sector of the sweep is a patch with: - Outer arc approximated by cubic Bézier (kappa method) - Inner arc at small radius to avoid degenerate geometry - Radial straight edges connecting inner and outer - Corner colors from the color line at sector boundaries Sectors are limited to 90° for accurate Bézier arc approximation, with a minimum of 4 patches per sweep. The mesh data is encoded as a binary stream with 16-bit coordinates, 8-bit color components, and 8-bit flags per the PDF Type 6 specification. Assisted-by: Claude --- src/hb-vector-pdf-paint.cc | 208 +++++++++++++++++++++++++++---------- 1 file changed, 153 insertions(+), 55 deletions(-) diff --git a/src/hb-vector-pdf-paint.cc b/src/hb-vector-pdf-paint.cc index 07d745bd0..40b90a745 100644 --- a/src/hb-vector-pdf-paint.cc +++ b/src/hb-vector-pdf-paint.cc @@ -564,8 +564,9 @@ hb_pdf_evaluate_color_line (hb_color_stop_t *stops, unsigned len, t = t - floorf (t); else if (extend == HB_PAINT_EXTEND_REFLECT) { - t = t - floorf (t); - if (((int) floorf (t * 2.f)) & 1) + float ft = floorf (t); + t = t - ft; + if (((int) ft) & 1) t = 1.f - t; } else /* PAD */ @@ -576,7 +577,6 @@ hb_pdf_evaluate_color_line (hb_color_stop_t *stops, unsigned len, if (t >= stops[len - 1].offset) return stops[len - 1].color; - /* Find enclosing stops. */ unsigned i = 0; for (; i + 1 < len; i++) if (stops[i + 1].offset >= t) @@ -595,6 +595,45 @@ hb_pdf_evaluate_color_line (hb_color_stop_t *stops, unsigned len, return HB_COLOR (lerp (16), lerp (8), lerp (0), lerp (24)); } +/* Encode a 16-bit big-endian unsigned value into buf. */ +static void +hb_pdf_encode_u16 (hb_vector_t *buf, uint16_t v) +{ + char bytes[2] = {(char) (v >> 8), (char) (v & 0xFF)}; + hb_svg_append_len (buf, bytes, 2); +} + +/* Encode a coordinate as 16-bit value relative to Decode range. */ +static void +hb_pdf_encode_coord (hb_vector_t *buf, + float val, float lo, float hi) +{ + float t = (val - lo) / (hi - lo); + t = hb_clamp (t, 0.f, 1.f); + hb_pdf_encode_u16 (buf, (uint16_t) (t * 65535.f + 0.5f)); +} + +/* Encode RGB from hb_color_t as 3 bytes. */ +static void +hb_pdf_encode_color_rgb (hb_vector_t *buf, hb_color_t c) +{ + char rgb[3] = {(char) hb_color_get_red (c), + (char) hb_color_get_green (c), + (char) hb_color_get_blue (c)}; + hb_svg_append_len (buf, rgb, 3); +} + +/* Encode one Coons patch control point. */ +static void +hb_pdf_encode_point (hb_vector_t *buf, + float x, float y, + float xlo, float xhi, + float ylo, float yhi) +{ + hb_pdf_encode_coord (buf, x, xlo, xhi); + hb_pdf_encode_coord (buf, y, ylo, yhi); +} + static void hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, void *paint_data, @@ -607,6 +646,9 @@ hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, auto *paint = (hb_vector_paint_t *) paint_data; if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) return; + auto *res = hb_pdf_get_resources (paint); + if (unlikely (!res)) + return; /* Get and sort color stops. */ unsigned count = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr); @@ -618,74 +660,130 @@ hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, hb_paint_extend_t extend = hb_color_line_get_extend (color_line); - /* Decompose into colored pie-slice sectors. */ - const unsigned NUM_SECTORS = 360; - float R = 32767.f; /* large enough to cover any clip */ - - auto &body = paint->current_body (); - float angle_range = end_angle - start_angle; if (fabsf (angle_range) < 1e-10f) return; - for (unsigned i = 0; i < NUM_SECTORS; i++) + /* Parameters. */ + const float R = 32767.f; + const float eps = 0.5f; /* small inner radius to avoid degenerate patch */ + const float MAX_SECTOR = (float) M_PI / 2.f; /* max 90° per patch */ + + unsigned num_sectors = (unsigned) ceilf (fabsf (angle_range) / MAX_SECTOR); + if (num_sectors < 4) num_sectors = 4; + + /* Coordinate bounds for Decode. */ + float xlo = cx - R - 1, xhi = cx + R + 1; + float ylo = cy - R - 1, yhi = cy + R + 1; + + /* Build mesh data stream. */ + hb_vector_t mesh; + mesh.alloc (num_sectors * 64); + + for (unsigned i = 0; i < num_sectors; i++) { - float t0 = (float) i / NUM_SECTORS; - float t1 = (float) (i + 1) / NUM_SECTORS; + float t0 = (float) i / num_sectors; + float t1 = (float) (i + 1) / num_sectors; float a0 = start_angle + t0 * angle_range; float a1 = start_angle + t1 * angle_range; + float da = a1 - a0; - /* Evaluate color at midpoint of sector. */ - float t_mid = (t0 + t1) * 0.5f; - hb_color_t c = hb_pdf_evaluate_color_line (paint->color_stops_scratch.arrayZ, - count, t_mid, extend); + /* Bézier approximation factor for circular arc. */ + float kappa = (4.f / 3.f) * tanf (da / 4.f); - float alpha = hb_color_get_alpha (c) / 255.f; - if (alpha < 1.f / 255.f) - continue; - - /* Set alpha if needed. */ - if (alpha < 1.f - 1.f / 512.f) - { - auto *res = hb_pdf_get_resources (paint); - if (res) - { - unsigned gs_idx = res->add_extgstate_alpha (alpha, paint->precision); - hb_svg_append_str (&body, "/GS"); - hb_svg_append_unsigned (&body, gs_idx); - hb_svg_append_str (&body, " gs\n"); - } - } - - /* Set fill color. */ - hb_svg_append_num (&body, hb_color_get_red (c) / 255.f, 4); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, hb_color_get_green (c) / 255.f, 4); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, hb_color_get_blue (c) / 255.f, 4); - hb_svg_append_str (&body, " rg\n"); - - /* Pie-slice path: center → outer edge at a0 → outer edge at a1 → close. */ float cos0 = cosf (a0), sin0 = sinf (a0); float cos1 = cosf (a1), sin1 = sinf (a1); - hb_svg_append_num (&body, cx, paint->precision); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, cy, paint->precision); - hb_svg_append_str (&body, " m\n"); + /* 4 corners: + * p0 = inner at a0 + * p3 = outer at a0 + * p6 = outer at a1 + * p9 = inner at a1 + */ + float p0x = cx + eps * cos0, p0y = cy + eps * sin0; + float p3x = cx + R * cos0, p3y = cy + R * sin0; + float p6x = cx + R * cos1, p6y = cy + R * sin1; + float p9x = cx + eps * cos1, p9y = cy + eps * sin1; - hb_svg_append_num (&body, cx + R * cos0, paint->precision); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, cy + R * sin0, paint->precision); - hb_svg_append_str (&body, " l\n"); + /* Edge 1: p0→p3, radial line (straight Bézier). */ + float e1_1x = p0x + (p3x - p0x) / 3.f; + float e1_1y = p0y + (p3y - p0y) / 3.f; + float e1_2x = p0x + 2.f * (p3x - p0x) / 3.f; + float e1_2y = p0y + 2.f * (p3y - p0y) / 3.f; - hb_svg_append_num (&body, cx + R * cos1, paint->precision); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, cy + R * sin1, paint->precision); - hb_svg_append_str (&body, " l\n"); + /* Edge 2: p3→p6, outer arc. */ + float e2_1x = p3x + kappa * R * (-sin0); + float e2_1y = p3y + kappa * R * ( cos0); + float e2_2x = p6x - kappa * R * (-sin1); + float e2_2y = p6y - kappa * R * ( cos1); - hb_svg_append_str (&body, "h f\n"); + /* Edge 3: p6→p9, radial line (straight, reversed). */ + float e3_1x = p6x + (p9x - p6x) / 3.f; + float e3_1y = p6y + (p9y - p6y) / 3.f; + float e3_2x = p6x + 2.f * (p9x - p6x) / 3.f; + float e3_2y = p6y + 2.f * (p9y - p6y) / 3.f; + + /* Edge 4: p9→p0, inner arc (reversed). */ + float e4_1x = p9x + kappa * eps * (-sin1); + float e4_1y = p9y + kappa * eps * ( cos1); + float e4_2x = p0x - kappa * eps * (-sin0); + float e4_2y = p0y - kappa * eps * ( cos0); + + /* Evaluate colors. */ + hb_color_t c0 = hb_pdf_evaluate_color_line (paint->color_stops_scratch.arrayZ, + count, t0, extend); + hb_color_t c1 = hb_pdf_evaluate_color_line (paint->color_stops_scratch.arrayZ, + count, t1, extend); + + /* Flag byte. */ + hb_svg_append_c (&mesh, '\0'); /* flag = 0, new patch */ + + /* 12 control points: edges 1,2,3,4. */ + hb_pdf_encode_point (&mesh, p0x, p0y, xlo, xhi, ylo, yhi); /* p0 */ + hb_pdf_encode_point (&mesh, e1_1x, e1_1y, xlo, xhi, ylo, yhi); /* p1 */ + hb_pdf_encode_point (&mesh, e1_2x, e1_2y, xlo, xhi, ylo, yhi); /* p2 */ + hb_pdf_encode_point (&mesh, p3x, p3y, xlo, xhi, ylo, yhi); /* p3 */ + hb_pdf_encode_point (&mesh, e2_1x, e2_1y, xlo, xhi, ylo, yhi); /* p4 */ + hb_pdf_encode_point (&mesh, e2_2x, e2_2y, xlo, xhi, ylo, yhi); /* p5 */ + hb_pdf_encode_point (&mesh, p6x, p6y, xlo, xhi, ylo, yhi); /* p6 */ + hb_pdf_encode_point (&mesh, e3_1x, e3_1y, xlo, xhi, ylo, yhi); /* p7 */ + hb_pdf_encode_point (&mesh, e3_2x, e3_2y, xlo, xhi, ylo, yhi); /* p8 */ + hb_pdf_encode_point (&mesh, p9x, p9y, xlo, xhi, ylo, yhi); /* p9 */ + hb_pdf_encode_point (&mesh, e4_1x, e4_1y, xlo, xhi, ylo, yhi); /* p10 */ + hb_pdf_encode_point (&mesh, e4_2x, e4_2y, xlo, xhi, ylo, yhi); /* p11 */ + + /* 4 corner colors: c0(p0), c1(p3), c2(p6), c3(p9). */ + hb_pdf_encode_color_rgb (&mesh, c0); /* c0 = inner start */ + hb_pdf_encode_color_rgb (&mesh, c0); /* c1 = outer start (same angle) */ + hb_pdf_encode_color_rgb (&mesh, c1); /* c2 = outer end */ + hb_pdf_encode_color_rgb (&mesh, c1); /* c3 = inner end */ } + + /* Build the shading stream object. */ + hb_vector_t sh; + hb_svg_append_str (&sh, "<< /ShadingType 6 /ColorSpace /DeviceRGB\n"); + hb_svg_append_str (&sh, "/BitsPerCoordinate 16 /BitsPerComponent 8 /BitsPerFlag 8\n"); + hb_svg_append_str (&sh, "/Decode ["); + hb_svg_append_num (&sh, xlo, 2); + hb_svg_append_c (&sh, ' '); + hb_svg_append_num (&sh, xhi, 2); + hb_svg_append_c (&sh, ' '); + hb_svg_append_num (&sh, ylo, 2); + hb_svg_append_c (&sh, ' '); + hb_svg_append_num (&sh, yhi, 2); + hb_svg_append_str (&sh, " 0 1 0 1 0 1]\n"); + hb_svg_append_str (&sh, "/Length "); + hb_svg_append_unsigned (&sh, mesh.length); + hb_svg_append_str (&sh, " >>\nstream\n"); + hb_svg_append_len (&sh, mesh.arrayZ, mesh.length); + hb_svg_append_str (&sh, "\nendstream"); + + unsigned sh_idx = res->add_shading (std::move (sh)); + + auto &body = paint->current_body (); + hb_svg_append_str (&body, "/SH"); + hb_svg_append_unsigned (&body, sh_idx); + hb_svg_append_str (&body, " sh\n"); } static void From 4087416a68b45ec19f62c99437e08be2c06a4f39 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 21:50:31 -0600 Subject: [PATCH 12/23] [vector] Add hb-vector-pdf-paint.cc to harfbuzz-world.cc Assisted-by: Claude --- src/harfbuzz-world.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/harfbuzz-world.cc b/src/harfbuzz-world.cc index fb730f86b..5922468eb 100644 --- a/src/harfbuzz-world.cc +++ b/src/harfbuzz-world.cc @@ -165,6 +165,7 @@ #ifdef HB_HAS_VECTOR #include "hb-static.cc" +#include "hb-vector-pdf-paint.cc" #include "hb-vector-svg-draw.cc" #include "hb-vector-svg-paint.cc" #include "hb-vector-svg-path.cc" From c67c323d0bc1bc307c430d8772cab560b3e0c753 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 21:56:08 -0600 Subject: [PATCH 13/23] [vector] Rename hb_svg_append_* to hb_buf_append_* The buffer helper functions (append_str, append_len, append_c, append_num, append_unsigned, append_hex_byte, append_base64, recover_recycled, blob_from) are format-agnostic and used by both SVG and PDF backends. Rename from hb_svg_* to hb_buf_* to reflect this. SVG-specific helpers (append_color, append_instance_transform, transform_point, scale_precision, etc.) keep their hb_svg_* names. Assisted-by: Claude --- src/hb-vector-draw.hh | 6 +- src/hb-vector-pdf-paint.cc | 362 +++++++++++++++--------------- src/hb-vector-svg-draw.cc | 166 +++++++------- src/hb-vector-svg-paint.cc | 434 ++++++++++++++++++------------------ src/hb-vector-svg-path.cc | 58 ++--- src/hb-vector-svg-subset.cc | 76 +++---- src/hb-vector-svg-utils.hh | 10 +- src/hb-vector-svg.hh | 90 ++++---- 8 files changed, 601 insertions(+), 601 deletions(-) diff --git a/src/hb-vector-draw.hh b/src/hb-vector-draw.hh index e9294bdd2..f1a09f728 100644 --- a/src/hb-vector-draw.hh +++ b/src/hb-vector-draw.hh @@ -32,9 +32,9 @@ struct hb_vector_draw_t { float tx, ty; hb_svg_transform_point (transform, x_scale_factor, y_scale_factor, x, y, &tx, &ty); - hb_svg_append_num (&path, tx, precision); - hb_svg_append_c (&path, format == HB_VECTOR_FORMAT_PDF ? ' ' : ','); - hb_svg_append_num (&path, ty, precision); + hb_buf_append_num (&path, tx, precision); + hb_buf_append_c (&path, format == HB_VECTOR_FORMAT_PDF ? ' ' : ','); + hb_buf_append_num (&path, ty, precision); } }; diff --git a/src/hb-vector-pdf-paint.cc b/src/hb-vector-pdf-paint.cc index 40b90a745..9908627ef 100644 --- a/src/hb-vector-pdf-paint.cc +++ b/src/hb-vector-pdf-paint.cc @@ -77,17 +77,17 @@ struct hb_pdf_resources_t { unsigned idx = extgstate_count++; hb_vector_t obj; - hb_svg_append_str (&obj, "<< /Type /ExtGState /ca "); - hb_svg_append_num (&obj, alpha, 4); - hb_svg_append_str (&obj, " >>"); + hb_buf_append_str (&obj, "<< /Type /ExtGState /ca "); + hb_buf_append_num (&obj, alpha, 4); + hb_buf_append_str (&obj, " >>"); unsigned obj_id = add_object (std::move (obj)); /* Add to resource dict. */ - hb_svg_append_str (&extgstate_dict, "/GS"); - hb_svg_append_unsigned (&extgstate_dict, idx); - hb_svg_append_c (&extgstate_dict, ' '); - hb_svg_append_unsigned (&extgstate_dict, obj_id); - hb_svg_append_str (&extgstate_dict, " 0 R "); + hb_buf_append_str (&extgstate_dict, "/GS"); + hb_buf_append_unsigned (&extgstate_dict, idx); + hb_buf_append_c (&extgstate_dict, ' '); + hb_buf_append_unsigned (&extgstate_dict, obj_id); + hb_buf_append_str (&extgstate_dict, " 0 R "); (void) precision; return idx; } @@ -98,11 +98,11 @@ struct hb_pdf_resources_t unsigned idx = shading_count++; unsigned obj_id = add_object (std::move (shading_data)); - hb_svg_append_str (&shading_dict, "/SH"); - hb_svg_append_unsigned (&shading_dict, idx); - hb_svg_append_c (&shading_dict, ' '); - hb_svg_append_unsigned (&shading_dict, obj_id); - hb_svg_append_str (&shading_dict, " 0 R "); + hb_buf_append_str (&shading_dict, "/SH"); + hb_buf_append_unsigned (&shading_dict, idx); + hb_buf_append_c (&shading_dict, ' '); + hb_buf_append_unsigned (&shading_dict, obj_id); + hb_buf_append_str (&shading_dict, " 0 R "); return idx; } }; @@ -179,7 +179,7 @@ hb_pdf_emit_glyph_path (hb_vector_paint_t *paint, hb_vector_draw_get_funcs (), &tmp); - hb_svg_append_len (buf, tmp.path.arrayZ, tmp.path.length); + hb_buf_append_len (buf, tmp.path.arrayZ, tmp.path.length); } @@ -198,19 +198,19 @@ hb_pdf_paint_push_transform (hb_paint_funcs_t *, return; auto &body = paint->current_body (); - hb_svg_append_str (&body, "q\n"); - hb_svg_append_num (&body, xx, paint->precision); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, yx, paint->precision); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, xy, paint->precision); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, yy, paint->precision); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, dx, paint->precision); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, dy, paint->precision); - hb_svg_append_str (&body, " cm\n"); + hb_buf_append_str (&body, "q\n"); + hb_buf_append_num (&body, xx, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, yx, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, xy, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, yy, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, dx, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, dy, paint->precision); + hb_buf_append_str (&body, " cm\n"); } static void @@ -221,7 +221,7 @@ hb_pdf_paint_pop_transform (hb_paint_funcs_t *, auto *paint = (hb_vector_paint_t *) paint_data; if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) return; - hb_svg_append_str (&paint->current_body (), "Q\n"); + hb_buf_append_str (&paint->current_body (), "Q\n"); } static void @@ -236,9 +236,9 @@ hb_pdf_paint_push_clip_glyph (hb_paint_funcs_t *, return; auto &body = paint->current_body (); - hb_svg_append_str (&body, "q\n"); + hb_buf_append_str (&body, "q\n"); hb_pdf_emit_glyph_path (paint, font, glyph, &body); - hb_svg_append_str (&body, "W n\n"); + hb_buf_append_str (&body, "W n\n"); } static void @@ -253,15 +253,15 @@ hb_pdf_paint_push_clip_rectangle (hb_paint_funcs_t *, return; auto &body = paint->current_body (); - hb_svg_append_str (&body, "q\n"); - hb_svg_append_num (&body, xmin, paint->precision); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, ymin, paint->precision); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, xmax - xmin, paint->precision); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, ymax - ymin, paint->precision); - hb_svg_append_str (&body, " re W n\n"); + hb_buf_append_str (&body, "q\n"); + hb_buf_append_num (&body, xmin, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, ymin, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, xmax - xmin, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, ymax - ymin, paint->precision); + hb_buf_append_str (&body, " re W n\n"); } static void @@ -272,7 +272,7 @@ hb_pdf_paint_pop_clip (hb_paint_funcs_t *, auto *paint = (hb_vector_paint_t *) paint_data; if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) return; - hb_svg_append_str (&paint->current_body (), "Q\n"); + hb_buf_append_str (&paint->current_body (), "Q\n"); } /* Paint a solid color, including alpha via ExtGState. */ @@ -296,22 +296,22 @@ hb_pdf_paint_solid_color (hb_vector_paint_t *paint, hb_color_t c) if (res) { unsigned gs_idx = res->add_extgstate_alpha (a, paint->precision); - hb_svg_append_str (&body, "/GS"); - hb_svg_append_unsigned (&body, gs_idx); - hb_svg_append_str (&body, " gs\n"); + hb_buf_append_str (&body, "/GS"); + hb_buf_append_unsigned (&body, gs_idx); + hb_buf_append_str (&body, " gs\n"); } } /* Set fill color. */ - hb_svg_append_num (&body, r, 4); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, g, 4); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, b, 4); - hb_svg_append_str (&body, " rg\n"); + hb_buf_append_num (&body, r, 4); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, g, 4); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, b, 4); + hb_buf_append_str (&body, " rg\n"); /* Paint a huge rect (will be clipped). */ - hb_svg_append_str (&body, "-32767 -32767 65534 65534 re f\n"); + hb_buf_append_str (&body, "-32767 -32767 65534 65534 re f\n"); } static void @@ -357,20 +357,20 @@ static void hb_pdf_build_interpolation_function (hb_vector_t *obj, hb_color_t c0, hb_color_t c1) { - hb_svg_append_str (obj, "<< /FunctionType 2 /Domain [0 1] /N 1\n"); - hb_svg_append_str (obj, "/C0 ["); - hb_svg_append_num (obj, hb_color_get_red (c0) / 255.f, 4); - hb_svg_append_c (obj, ' '); - hb_svg_append_num (obj, hb_color_get_green (c0) / 255.f, 4); - hb_svg_append_c (obj, ' '); - hb_svg_append_num (obj, hb_color_get_blue (c0) / 255.f, 4); - hb_svg_append_str (obj, "]\n/C1 ["); - hb_svg_append_num (obj, hb_color_get_red (c1) / 255.f, 4); - hb_svg_append_c (obj, ' '); - hb_svg_append_num (obj, hb_color_get_green (c1) / 255.f, 4); - hb_svg_append_c (obj, ' '); - hb_svg_append_num (obj, hb_color_get_blue (c1) / 255.f, 4); - hb_svg_append_str (obj, "] >>"); + hb_buf_append_str (obj, "<< /FunctionType 2 /Domain [0 1] /N 1\n"); + hb_buf_append_str (obj, "/C0 ["); + hb_buf_append_num (obj, hb_color_get_red (c0) / 255.f, 4); + hb_buf_append_c (obj, ' '); + hb_buf_append_num (obj, hb_color_get_green (c0) / 255.f, 4); + hb_buf_append_c (obj, ' '); + hb_buf_append_num (obj, hb_color_get_blue (c0) / 255.f, 4); + hb_buf_append_str (obj, "]\n/C1 ["); + hb_buf_append_num (obj, hb_color_get_red (c1) / 255.f, 4); + hb_buf_append_c (obj, ' '); + hb_buf_append_num (obj, hb_color_get_green (c1) / 255.f, 4); + hb_buf_append_c (obj, ' '); + hb_buf_append_num (obj, hb_color_get_blue (c1) / 255.f, 4); + hb_buf_append_str (obj, "] >>"); } /* Build a stitching function (Type 3) for multiple color stops. */ @@ -421,35 +421,35 @@ hb_pdf_build_gradient_function (hb_pdf_resources_t *res, /* Stitching function (Type 3). */ hb_vector_t obj; - hb_svg_append_str (&obj, "<< /FunctionType 3 /Domain [0 1]\n"); + hb_buf_append_str (&obj, "<< /FunctionType 3 /Domain [0 1]\n"); /* Functions array. */ - hb_svg_append_str (&obj, "/Functions ["); + hb_buf_append_str (&obj, "/Functions ["); for (unsigned i = 0; i < sub_func_ids.length; i++) { - if (i) hb_svg_append_c (&obj, ' '); - hb_svg_append_unsigned (&obj, sub_func_ids.arrayZ[i]); - hb_svg_append_str (&obj, " 0 R"); + if (i) hb_buf_append_c (&obj, ' '); + hb_buf_append_unsigned (&obj, sub_func_ids.arrayZ[i]); + hb_buf_append_str (&obj, " 0 R"); } - hb_svg_append_str (&obj, "]\n"); + hb_buf_append_str (&obj, "]\n"); /* Bounds. */ - hb_svg_append_str (&obj, "/Bounds ["); + hb_buf_append_str (&obj, "/Bounds ["); for (unsigned i = 1; i + 1 < count; i++) { - if (i > 1) hb_svg_append_c (&obj, ' '); - hb_svg_append_num (&obj, paint->color_stops_scratch.arrayZ[i].offset, 4); + if (i > 1) hb_buf_append_c (&obj, ' '); + hb_buf_append_num (&obj, paint->color_stops_scratch.arrayZ[i].offset, 4); } - hb_svg_append_str (&obj, "]\n"); + hb_buf_append_str (&obj, "]\n"); /* Encode array. */ - hb_svg_append_str (&obj, "/Encode ["); + hb_buf_append_str (&obj, "/Encode ["); for (unsigned i = 0; i + 1 < count; i++) { - if (i) hb_svg_append_c (&obj, ' '); - hb_svg_append_str (&obj, "0 1"); + if (i) hb_buf_append_c (&obj, ' '); + hb_buf_append_str (&obj, "0 1"); } - hb_svg_append_str (&obj, "] >>"); + hb_buf_append_str (&obj, "] >>"); return res->add_object (std::move (obj)); } @@ -474,31 +474,31 @@ hb_pdf_paint_linear_gradient (hb_paint_funcs_t *, /* Build Type 2 (axial) shading. */ hb_vector_t sh; - hb_svg_append_str (&sh, "<< /ShadingType 2 /ColorSpace /DeviceRGB\n"); - hb_svg_append_str (&sh, "/Coords ["); - hb_svg_append_num (&sh, x0, paint->precision); - hb_svg_append_c (&sh, ' '); - hb_svg_append_num (&sh, y0, paint->precision); - hb_svg_append_c (&sh, ' '); - hb_svg_append_num (&sh, x1, paint->precision); - hb_svg_append_c (&sh, ' '); - hb_svg_append_num (&sh, y1, paint->precision); - hb_svg_append_str (&sh, "]\n/Function "); - hb_svg_append_unsigned (&sh, func_id); - hb_svg_append_str (&sh, " 0 R\n"); + hb_buf_append_str (&sh, "<< /ShadingType 2 /ColorSpace /DeviceRGB\n"); + hb_buf_append_str (&sh, "/Coords ["); + hb_buf_append_num (&sh, x0, paint->precision); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, y0, paint->precision); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, x1, paint->precision); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, y1, paint->precision); + hb_buf_append_str (&sh, "]\n/Function "); + hb_buf_append_unsigned (&sh, func_id); + hb_buf_append_str (&sh, " 0 R\n"); hb_paint_extend_t extend = hb_color_line_get_extend (color_line); if (extend == HB_PAINT_EXTEND_PAD) - hb_svg_append_str (&sh, "/Extend [true true]\n"); + hb_buf_append_str (&sh, "/Extend [true true]\n"); - hb_svg_append_str (&sh, ">>"); + hb_buf_append_str (&sh, ">>"); unsigned sh_idx = res->add_shading (std::move (sh)); auto &body = paint->current_body (); - hb_svg_append_str (&body, "/SH"); - hb_svg_append_unsigned (&body, sh_idx); - hb_svg_append_str (&body, " sh\n"); + hb_buf_append_str (&body, "/SH"); + hb_buf_append_unsigned (&body, sh_idx); + hb_buf_append_str (&body, " sh\n"); } static void @@ -520,35 +520,35 @@ hb_pdf_paint_radial_gradient (hb_paint_funcs_t *, /* Build Type 3 (radial) shading. */ hb_vector_t sh; - hb_svg_append_str (&sh, "<< /ShadingType 3 /ColorSpace /DeviceRGB\n"); - hb_svg_append_str (&sh, "/Coords ["); - hb_svg_append_num (&sh, x0, paint->precision); - hb_svg_append_c (&sh, ' '); - hb_svg_append_num (&sh, y0, paint->precision); - hb_svg_append_c (&sh, ' '); - hb_svg_append_num (&sh, r0, paint->precision); - hb_svg_append_c (&sh, ' '); - hb_svg_append_num (&sh, x1, paint->precision); - hb_svg_append_c (&sh, ' '); - hb_svg_append_num (&sh, y1, paint->precision); - hb_svg_append_c (&sh, ' '); - hb_svg_append_num (&sh, r1, paint->precision); - hb_svg_append_str (&sh, "]\n/Function "); - hb_svg_append_unsigned (&sh, func_id); - hb_svg_append_str (&sh, " 0 R\n"); + hb_buf_append_str (&sh, "<< /ShadingType 3 /ColorSpace /DeviceRGB\n"); + hb_buf_append_str (&sh, "/Coords ["); + hb_buf_append_num (&sh, x0, paint->precision); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, y0, paint->precision); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, r0, paint->precision); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, x1, paint->precision); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, y1, paint->precision); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, r1, paint->precision); + hb_buf_append_str (&sh, "]\n/Function "); + hb_buf_append_unsigned (&sh, func_id); + hb_buf_append_str (&sh, " 0 R\n"); hb_paint_extend_t extend = hb_color_line_get_extend (color_line); if (extend == HB_PAINT_EXTEND_PAD) - hb_svg_append_str (&sh, "/Extend [true true]\n"); + hb_buf_append_str (&sh, "/Extend [true true]\n"); - hb_svg_append_str (&sh, ">>"); + hb_buf_append_str (&sh, ">>"); unsigned sh_idx = res->add_shading (std::move (sh)); auto &body = paint->current_body (); - hb_svg_append_str (&body, "/SH"); - hb_svg_append_unsigned (&body, sh_idx); - hb_svg_append_str (&body, " sh\n"); + hb_buf_append_str (&body, "/SH"); + hb_buf_append_unsigned (&body, sh_idx); + hb_buf_append_str (&body, " sh\n"); } /* Evaluate color line at a given offset. */ @@ -600,7 +600,7 @@ static void hb_pdf_encode_u16 (hb_vector_t *buf, uint16_t v) { char bytes[2] = {(char) (v >> 8), (char) (v & 0xFF)}; - hb_svg_append_len (buf, bytes, 2); + hb_buf_append_len (buf, bytes, 2); } /* Encode a coordinate as 16-bit value relative to Decode range. */ @@ -620,7 +620,7 @@ hb_pdf_encode_color_rgb (hb_vector_t *buf, hb_color_t c) char rgb[3] = {(char) hb_color_get_red (c), (char) hb_color_get_green (c), (char) hb_color_get_blue (c)}; - hb_svg_append_len (buf, rgb, 3); + hb_buf_append_len (buf, rgb, 3); } /* Encode one Coons patch control point. */ @@ -736,7 +736,7 @@ hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, count, t1, extend); /* Flag byte. */ - hb_svg_append_c (&mesh, '\0'); /* flag = 0, new patch */ + hb_buf_append_c (&mesh, '\0'); /* flag = 0, new patch */ /* 12 control points: edges 1,2,3,4. */ hb_pdf_encode_point (&mesh, p0x, p0y, xlo, xhi, ylo, yhi); /* p0 */ @@ -761,29 +761,29 @@ hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, /* Build the shading stream object. */ hb_vector_t sh; - hb_svg_append_str (&sh, "<< /ShadingType 6 /ColorSpace /DeviceRGB\n"); - hb_svg_append_str (&sh, "/BitsPerCoordinate 16 /BitsPerComponent 8 /BitsPerFlag 8\n"); - hb_svg_append_str (&sh, "/Decode ["); - hb_svg_append_num (&sh, xlo, 2); - hb_svg_append_c (&sh, ' '); - hb_svg_append_num (&sh, xhi, 2); - hb_svg_append_c (&sh, ' '); - hb_svg_append_num (&sh, ylo, 2); - hb_svg_append_c (&sh, ' '); - hb_svg_append_num (&sh, yhi, 2); - hb_svg_append_str (&sh, " 0 1 0 1 0 1]\n"); - hb_svg_append_str (&sh, "/Length "); - hb_svg_append_unsigned (&sh, mesh.length); - hb_svg_append_str (&sh, " >>\nstream\n"); - hb_svg_append_len (&sh, mesh.arrayZ, mesh.length); - hb_svg_append_str (&sh, "\nendstream"); + hb_buf_append_str (&sh, "<< /ShadingType 6 /ColorSpace /DeviceRGB\n"); + hb_buf_append_str (&sh, "/BitsPerCoordinate 16 /BitsPerComponent 8 /BitsPerFlag 8\n"); + hb_buf_append_str (&sh, "/Decode ["); + hb_buf_append_num (&sh, xlo, 2); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, xhi, 2); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, ylo, 2); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, yhi, 2); + hb_buf_append_str (&sh, " 0 1 0 1 0 1]\n"); + hb_buf_append_str (&sh, "/Length "); + hb_buf_append_unsigned (&sh, mesh.length); + hb_buf_append_str (&sh, " >>\nstream\n"); + hb_buf_append_len (&sh, mesh.arrayZ, mesh.length); + hb_buf_append_str (&sh, "\nendstream"); unsigned sh_idx = res->add_shading (std::move (sh)); auto &body = paint->current_body (); - hb_svg_append_str (&body, "/SH"); - hb_svg_append_unsigned (&body, sh_idx); - hb_svg_append_str (&body, " sh\n"); + hb_buf_append_str (&body, "/SH"); + hb_buf_append_unsigned (&body, sh_idx); + hb_buf_append_str (&body, " sh\n"); } static void @@ -794,7 +794,7 @@ hb_pdf_paint_push_group (hb_paint_funcs_t *, auto *paint = (hb_vector_paint_t *) paint_data; if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) return; - hb_svg_append_str (&paint->current_body (), "q\n"); + hb_buf_append_str (&paint->current_body (), "q\n"); } static void @@ -806,7 +806,7 @@ hb_pdf_paint_pop_group (hb_paint_funcs_t *, auto *paint = (hb_vector_paint_t *) paint_data; if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) return; - hb_svg_append_str (&paint->current_body (), "Q\n"); + hb_buf_append_str (&paint->current_body (), "Q\n"); } @@ -875,96 +875,96 @@ hb_vector_pdf_paint_render (hb_vector_paint_t *paint) /* Build PDF. */ hb_vector_t out; - hb_svg_recover_recycled_buffer (paint->recycled_blob, &out); + hb_buf_recover_recycled (paint->recycled_blob, &out); out.alloc (content.length + num_extra * 128 + 1024); hb_vector_t offsets; offsets.resize (total_objects); - hb_svg_append_str (&out, "%PDF-1.4\n%\xC0\xC1\xC2\xC3\n"); + hb_buf_append_str (&out, "%PDF-1.4\n%\xC0\xC1\xC2\xC3\n"); /* Object 1: Catalog */ offsets.arrayZ[0] = out.length; - hb_svg_append_str (&out, "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); + hb_buf_append_str (&out, "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); /* Object 2: Pages */ offsets.arrayZ[1] = out.length; - hb_svg_append_str (&out, "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"); + hb_buf_append_str (&out, "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"); /* Object 3: Page */ offsets.arrayZ[2] = out.length; - hb_svg_append_str (&out, "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox ["); - hb_svg_append_num (&out, ex, paint->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, -(ey + eh), paint->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, ex + ew, paint->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, -ey, paint->precision); - hb_svg_append_str (&out, "]\n/Contents 4 0 R"); + hb_buf_append_str (&out, "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox ["); + hb_buf_append_num (&out, ex, paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, -(ey + eh), paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, ex + ew, paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, -ey, paint->precision); + hb_buf_append_str (&out, "]\n/Contents 4 0 R"); /* Resources. */ bool has_resources = res && (res->extgstate_dict.length || res->shading_dict.length); if (has_resources) { - hb_svg_append_str (&out, "\n/Resources <<"); + hb_buf_append_str (&out, "\n/Resources <<"); if (res->extgstate_dict.length) { - hb_svg_append_str (&out, " /ExtGState << "); - hb_svg_append_len (&out, res->extgstate_dict.arrayZ, res->extgstate_dict.length); - hb_svg_append_str (&out, ">>"); + hb_buf_append_str (&out, " /ExtGState << "); + hb_buf_append_len (&out, res->extgstate_dict.arrayZ, res->extgstate_dict.length); + hb_buf_append_str (&out, ">>"); } if (res->shading_dict.length) { - hb_svg_append_str (&out, " /Shading << "); - hb_svg_append_len (&out, res->shading_dict.arrayZ, res->shading_dict.length); - hb_svg_append_str (&out, ">>"); + hb_buf_append_str (&out, " /Shading << "); + hb_buf_append_len (&out, res->shading_dict.arrayZ, res->shading_dict.length); + hb_buf_append_str (&out, ">>"); } - hb_svg_append_str (&out, " >>"); + hb_buf_append_str (&out, " >>"); } - hb_svg_append_str (&out, " >>\nendobj\n"); + hb_buf_append_str (&out, " >>\nendobj\n"); /* Object 4: Content stream */ offsets.arrayZ[3] = out.length; - hb_svg_append_str (&out, "4 0 obj\n<< /Length "); - hb_svg_append_unsigned (&out, content.length); - hb_svg_append_str (&out, " >>\nstream\n"); - hb_svg_append_len (&out, content.arrayZ, content.length); - hb_svg_append_str (&out, "endstream\nendobj\n"); + hb_buf_append_str (&out, "4 0 obj\n<< /Length "); + hb_buf_append_unsigned (&out, content.length); + hb_buf_append_str (&out, " >>\nstream\n"); + hb_buf_append_len (&out, content.arrayZ, content.length); + hb_buf_append_str (&out, "endstream\nendobj\n"); /* Extra objects (functions, shadings, ExtGState). */ for (unsigned i = 0; i < num_extra; i++) { offsets.arrayZ[4 + i] = out.length; - hb_svg_append_unsigned (&out, 5 + i); - hb_svg_append_str (&out, " 0 obj\n"); + hb_buf_append_unsigned (&out, 5 + i); + hb_buf_append_str (&out, " 0 obj\n"); auto &obj = res->objects.arrayZ[i]; - hb_svg_append_len (&out, obj.data.arrayZ, obj.data.length); - hb_svg_append_str (&out, "\nendobj\n"); + hb_buf_append_len (&out, obj.data.arrayZ, obj.data.length); + hb_buf_append_str (&out, "\nendobj\n"); } /* Cross-reference table */ unsigned xref_offset = out.length; - hb_svg_append_str (&out, "xref\n0 "); - hb_svg_append_unsigned (&out, total_objects + 1); - hb_svg_append_str (&out, "\n0000000000 65535 f \n"); + hb_buf_append_str (&out, "xref\n0 "); + hb_buf_append_unsigned (&out, total_objects + 1); + hb_buf_append_str (&out, "\n0000000000 65535 f \n"); for (unsigned i = 0; i < total_objects; i++) { char tmp[21]; snprintf (tmp, sizeof (tmp), "%010u 00000 n \n", offsets.arrayZ[i]); - hb_svg_append_len (&out, tmp, 20); + hb_buf_append_len (&out, tmp, 20); } /* Trailer */ - hb_svg_append_str (&out, "trailer\n<< /Size "); - hb_svg_append_unsigned (&out, total_objects + 1); - hb_svg_append_str (&out, " /Root 1 0 R >>\nstartxref\n"); - hb_svg_append_unsigned (&out, xref_offset); - hb_svg_append_str (&out, "\n%%EOF\n"); + hb_buf_append_str (&out, "trailer\n<< /Size "); + hb_buf_append_unsigned (&out, total_objects + 1); + hb_buf_append_str (&out, " /Root 1 0 R >>\nstartxref\n"); + hb_buf_append_unsigned (&out, xref_offset); + hb_buf_append_str (&out, "\n%%EOF\n"); - hb_blob_t *blob = hb_svg_blob_from_buffer (&paint->recycled_blob, &out); + hb_blob_t *blob = hb_buf_blob_from (&paint->recycled_blob, &out); /* Reset state. */ hb_pdf_free_resources (paint); diff --git a/src/hb-vector-svg-draw.cc b/src/hb-vector-svg-draw.cc index e571cdba4..a82f6095a 100644 --- a/src/hb-vector-svg-draw.cc +++ b/src/hb-vector-svg-draw.cc @@ -61,11 +61,11 @@ hb_vector_draw_move_to (hb_draw_funcs_t *, if (d->format == HB_VECTOR_FORMAT_PDF) { d->append_xy (to_x, to_y); - hb_svg_append_str (&d->path, " m\n"); + hb_buf_append_str (&d->path, " m\n"); } else { - hb_svg_append_c (&d->path, 'M'); + hb_buf_append_c (&d->path, 'M'); d->append_xy (to_x, to_y); } } @@ -81,11 +81,11 @@ hb_vector_draw_line_to (hb_draw_funcs_t *, if (d->format == HB_VECTOR_FORMAT_PDF) { d->append_xy (to_x, to_y); - hb_svg_append_str (&d->path, " l\n"); + hb_buf_append_str (&d->path, " l\n"); } else { - hb_svg_append_c (&d->path, 'L'); + hb_buf_append_c (&d->path, 'L'); d->append_xy (to_x, to_y); } } @@ -108,17 +108,17 @@ hb_vector_draw_quadratic_to (hb_draw_funcs_t *, float c2x = to_x + 2.f / 3.f * (cx - to_x); float c2y = to_y + 2.f / 3.f * (cy - to_y); d->append_xy (c1x, c1y); - hb_svg_append_c (&d->path, ' '); + hb_buf_append_c (&d->path, ' '); d->append_xy (c2x, c2y); - hb_svg_append_c (&d->path, ' '); + hb_buf_append_c (&d->path, ' '); d->append_xy (to_x, to_y); - hb_svg_append_str (&d->path, " c\n"); + hb_buf_append_str (&d->path, " c\n"); } else { - hb_svg_append_c (&d->path, 'Q'); + hb_buf_append_c (&d->path, 'Q'); d->append_xy (cx, cy); - hb_svg_append_c (&d->path, ' '); + hb_buf_append_c (&d->path, ' '); d->append_xy (to_x, to_y); } } @@ -136,19 +136,19 @@ hb_vector_draw_cubic_to (hb_draw_funcs_t *, if (d->format == HB_VECTOR_FORMAT_PDF) { d->append_xy (c1x, c1y); - hb_svg_append_c (&d->path, ' '); + hb_buf_append_c (&d->path, ' '); d->append_xy (c2x, c2y); - hb_svg_append_c (&d->path, ' '); + hb_buf_append_c (&d->path, ' '); d->append_xy (to_x, to_y); - hb_svg_append_str (&d->path, " c\n"); + hb_buf_append_str (&d->path, " c\n"); } else { - hb_svg_append_c (&d->path, 'C'); + hb_buf_append_c (&d->path, 'C'); d->append_xy (c1x, c1y); - hb_svg_append_c (&d->path, ' '); + hb_buf_append_c (&d->path, ' '); d->append_xy (c2x, c2y); - hb_svg_append_c (&d->path, ' '); + hb_buf_append_c (&d->path, ' '); d->append_xy (to_x, to_y); } } @@ -161,9 +161,9 @@ hb_vector_draw_close_path (hb_draw_funcs_t *, { auto *d = (hb_vector_draw_t *) draw_data; if (d->format == HB_VECTOR_FORMAT_PDF) - hb_svg_append_str (&d->path, "h\n"); + hb_buf_append_str (&d->path, "h\n"); else - hb_svg_append_c (&d->path, 'Z'); + hb_buf_append_c (&d->path, 'Z'); } static inline void free_static_vector_draw_funcs (); @@ -559,8 +559,8 @@ hb_vector_draw_glyph (hb_vector_draw_t *draw, if (!draw->path.length) return false; - hb_svg_append_len (&draw->body, draw->path.arrayZ, draw->path.length); - hb_svg_append_str (&draw->body, "f\n"); + hb_buf_append_len (&draw->body, draw->path.arrayZ, draw->path.length); + hb_buf_append_str (&draw->body, "f\n"); return true; } @@ -572,11 +572,11 @@ hb_vector_draw_glyph (hb_vector_draw_t *draw, hb_font_draw_glyph (font, glyph, hb_svg_path_draw_funcs_get (), &sink); if (!draw->path.length) return false; - hb_svg_append_str (&draw->defs, "defs, glyph); - hb_svg_append_str (&draw->defs, "\" d=\""); - hb_svg_append_len (&draw->defs, draw->path.arrayZ, draw->path.length); - hb_svg_append_str (&draw->defs, "\"/>\n"); + hb_buf_append_str (&draw->defs, "defs, glyph); + hb_buf_append_str (&draw->defs, "\" d=\""); + hb_buf_append_len (&draw->defs, draw->path.arrayZ, draw->path.length); + hb_buf_append_str (&draw->defs, "\"/>\n"); hb_set_add (draw->defined_glyphs, glyph); } @@ -596,15 +596,15 @@ 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_svg_append_str (&draw->body, "body, draw->path.arrayZ, draw->path.length); - hb_svg_append_str (&draw->body, "\" transform=\""); + hb_buf_append_str (&draw->body, "body, draw->path.arrayZ, draw->path.length); + hb_buf_append_str (&draw->body, "\" transform=\""); hb_svg_append_instance_transform (&draw->body, draw->precision, draw->x_scale_factor, draw->y_scale_factor, xx, yx, xy, yy, tx, ty); - hb_svg_append_str (&draw->body, "\"/>\n"); + hb_buf_append_str (&draw->body, "\"/>\n"); return true; } @@ -615,15 +615,15 @@ 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_svg_append_str (&draw->body, "body, glyph); - hb_svg_append_str (&draw->body, "\" transform=\""); + hb_buf_append_str (&draw->body, "body, glyph); + hb_buf_append_str (&draw->body, "\" transform=\""); hb_svg_append_instance_transform (&draw->body, draw->precision, draw->x_scale_factor, draw->y_scale_factor, xx, yx, xy, yy, tx, ty); - hb_svg_append_str (&draw->body, "\"/>\n"); + hb_buf_append_str (&draw->body, "\"/>\n"); return true; } @@ -689,68 +689,68 @@ hb_vector_draw_render_pdf (hb_vector_draw_t *draw) /* Path coords are in font space (Y-up); no CTM needed. */ if (draw->body.length) - hb_svg_append_len (&stream, draw->body.arrayZ, draw->body.length); + hb_buf_append_len (&stream, draw->body.arrayZ, draw->body.length); else if (draw->path.length) { - hb_svg_append_len (&stream, draw->path.arrayZ, draw->path.length); - hb_svg_append_str (&stream, "f\n"); + hb_buf_append_len (&stream, draw->path.arrayZ, draw->path.length); + hb_buf_append_str (&stream, "f\n"); } /* Build PDF objects, tracking byte offsets for xref. */ hb_vector_t out; - hb_svg_recover_recycled_buffer (draw->recycled_blob, &out); + hb_buf_recover_recycled (draw->recycled_blob, &out); out.alloc (stream.length + 512); unsigned offsets[5]; /* objects 1-4, plus end */ - hb_svg_append_str (&out, "%PDF-1.4\n%\xC0\xC1\xC2\xC3\n"); + hb_buf_append_str (&out, "%PDF-1.4\n%\xC0\xC1\xC2\xC3\n"); /* Object 1: Catalog */ offsets[0] = out.length; - hb_svg_append_str (&out, "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); + hb_buf_append_str (&out, "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); /* Object 2: Pages */ offsets[1] = out.length; - hb_svg_append_str (&out, "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"); + hb_buf_append_str (&out, "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"); /* Object 3: Page. Extents are in SVG space (y = -font_y). * Convert back: font Y range = [-(ey+eh) .. -ey]. */ offsets[2] = out.length; - hb_svg_append_str (&out, "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox ["); - hb_svg_append_num (&out, ex, draw->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, -(ey + eh), draw->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, ex + ew, draw->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, -ey, draw->precision); - hb_svg_append_str (&out, "] /Contents 4 0 R >>\nendobj\n"); + hb_buf_append_str (&out, "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox ["); + hb_buf_append_num (&out, ex, draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, -(ey + eh), draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, ex + ew, draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, -ey, draw->precision); + hb_buf_append_str (&out, "] /Contents 4 0 R >>\nendobj\n"); /* Object 4: Content stream */ offsets[3] = out.length; - hb_svg_append_str (&out, "4 0 obj\n<< /Length "); - hb_svg_append_unsigned (&out, stream.length); - hb_svg_append_str (&out, " >>\nstream\n"); - hb_svg_append_len (&out, stream.arrayZ, stream.length); - hb_svg_append_str (&out, "endstream\nendobj\n"); + hb_buf_append_str (&out, "4 0 obj\n<< /Length "); + hb_buf_append_unsigned (&out, stream.length); + hb_buf_append_str (&out, " >>\nstream\n"); + hb_buf_append_len (&out, stream.arrayZ, stream.length); + hb_buf_append_str (&out, "endstream\nendobj\n"); /* Cross-reference table */ unsigned xref_offset = out.length; - hb_svg_append_str (&out, "xref\n0 5\n"); - hb_svg_append_str (&out, "0000000000 65535 f \n"); + hb_buf_append_str (&out, "xref\n0 5\n"); + hb_buf_append_str (&out, "0000000000 65535 f \n"); for (unsigned i = 0; i < 4; i++) { char tmp[21]; snprintf (tmp, sizeof (tmp), "%010u 00000 n \n", offsets[i]); - hb_svg_append_len (&out, tmp, 20); + hb_buf_append_len (&out, tmp, 20); } /* Trailer */ - hb_svg_append_str (&out, "trailer\n<< /Size 5 /Root 1 0 R >>\nstartxref\n"); - hb_svg_append_unsigned (&out, xref_offset); - hb_svg_append_str (&out, "\n%%EOF\n"); + hb_buf_append_str (&out, "trailer\n<< /Size 5 /Root 1 0 R >>\nstartxref\n"); + hb_buf_append_unsigned (&out, xref_offset); + hb_buf_append_str (&out, "\n%%EOF\n"); - hb_blob_t *blob = hb_svg_blob_from_buffer (&draw->recycled_blob, &out); + hb_blob_t *blob = hb_buf_blob_from (&draw->recycled_blob, &out); draw->path.clear (); draw->defs.clear (); @@ -774,46 +774,46 @@ hb_vector_draw_render (hb_vector_draw_t *draw) return nullptr; hb_vector_t out; - hb_svg_recover_recycled_buffer (draw->recycled_blob, &out); + hb_buf_recover_recycled (draw->recycled_blob, &out); unsigned estimated = draw->defs.length + (draw->body.length ? draw->body.length : draw->path.length) + 256; out.alloc (estimated); - hb_svg_append_str (&out, "extents.x, draw->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, draw->extents.y, draw->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, draw->extents.width, draw->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, draw->extents.height, draw->precision); - hb_svg_append_str (&out, "\" width=\""); - hb_svg_append_num (&out, draw->extents.width, draw->precision); - hb_svg_append_str (&out, "\" height=\""); - hb_svg_append_num (&out, draw->extents.height, draw->precision); - hb_svg_append_str (&out, "\">\n"); + hb_buf_append_str (&out, "extents.x, draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, draw->extents.y, draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, draw->extents.width, draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, draw->extents.height, draw->precision); + hb_buf_append_str (&out, "\" width=\""); + hb_buf_append_num (&out, draw->extents.width, draw->precision); + hb_buf_append_str (&out, "\" height=\""); + hb_buf_append_num (&out, draw->extents.height, draw->precision); + hb_buf_append_str (&out, "\">\n"); if (draw->defs.length) { - hb_svg_append_str (&out, "\n"); - hb_svg_append_len (&out, draw->defs.arrayZ, draw->defs.length); - hb_svg_append_str (&out, "\n"); + hb_buf_append_str (&out, "\n"); + hb_buf_append_len (&out, draw->defs.arrayZ, draw->defs.length); + hb_buf_append_str (&out, "\n"); } if (draw->body.length) { - hb_svg_append_len (&out, draw->body.arrayZ, draw->body.length); + hb_buf_append_len (&out, draw->body.arrayZ, draw->body.length); } else if (draw->path.length) { - hb_svg_append_str (&out, "path.arrayZ, draw->path.length); - hb_svg_append_str (&out, "\"/>\n"); + hb_buf_append_str (&out, "path.arrayZ, draw->path.length); + hb_buf_append_str (&out, "\"/>\n"); } - hb_svg_append_str (&out, "\n"); + hb_buf_append_str (&out, "\n"); - hb_blob_t *blob = hb_svg_blob_from_buffer (&draw->recycled_blob, &out); + hb_blob_t *blob = hb_buf_blob_from (&draw->recycled_blob, &out); draw->path.clear (); draw->defs.clear (); diff --git a/src/hb-vector-svg-paint.cc b/src/hb-vector-svg-paint.cc index 734ddc12d..c002893ea 100644 --- a/src/hb-vector-svg-paint.cc +++ b/src/hb-vector-svg-paint.cc @@ -44,19 +44,19 @@ hb_svg_paint_append_global_transform_prefix (hb_vector_paint_t *paint, hb_vector return; unsigned sprec = hb_svg_scale_precision (paint->precision); - hb_svg_append_str (buf, "transform.xx / paint->x_scale_factor, sprec, true); - hb_svg_append_c (buf, ','); - hb_svg_append_num (buf, paint->transform.yx / paint->y_scale_factor, sprec, true); - hb_svg_append_c (buf, ','); - hb_svg_append_num (buf, paint->transform.xy / paint->x_scale_factor, sprec, true); - hb_svg_append_c (buf, ','); - hb_svg_append_num (buf, paint->transform.yy / paint->y_scale_factor, sprec, true); - hb_svg_append_c (buf, ','); - hb_svg_append_num (buf, paint->transform.x0 / paint->x_scale_factor, paint->precision); - hb_svg_append_c (buf, ','); - hb_svg_append_num (buf, paint->transform.y0 / paint->y_scale_factor, paint->precision); - hb_svg_append_str (buf, ")\">\n"); + hb_buf_append_str (buf, "transform.xx / paint->x_scale_factor, sprec, true); + hb_buf_append_c (buf, ','); + hb_buf_append_num (buf, paint->transform.yx / paint->y_scale_factor, sprec, true); + hb_buf_append_c (buf, ','); + hb_buf_append_num (buf, paint->transform.xy / paint->x_scale_factor, sprec, true); + hb_buf_append_c (buf, ','); + hb_buf_append_num (buf, paint->transform.yy / paint->y_scale_factor, sprec, true); + hb_buf_append_c (buf, ','); + hb_buf_append_num (buf, paint->transform.x0 / paint->x_scale_factor, paint->precision); + hb_buf_append_c (buf, ','); + hb_buf_append_num (buf, paint->transform.y0 / paint->y_scale_factor, paint->precision); + hb_buf_append_str (buf, ")\">\n"); } static void @@ -67,7 +67,7 @@ hb_svg_paint_append_global_transform_suffix (hb_vector_paint_t *paint, hb_vector paint->transform.x0 == 0.f && paint->transform.y0 == 0.f && paint->x_scale_factor == 1.f && paint->y_scale_factor == 1.f) return; - hb_svg_append_str (buf, "\n"); + hb_buf_append_str (buf, "\n"); } static inline uint64_t @@ -155,22 +155,22 @@ hb_svg_emit_color_stops (hb_vector_paint_t *paint, for (unsigned i = 0; i < stops->length; i++) { hb_color_t c = stops->arrayZ[i].color; - hb_svg_append_str (buf, "arrayZ[i].offset, 4); - hb_svg_append_str (buf, "\" stop-color=\"rgb("); - hb_svg_append_unsigned (buf, hb_color_get_red (c)); - hb_svg_append_c (buf, ','); - hb_svg_append_unsigned (buf, hb_color_get_green (c)); - hb_svg_append_c (buf, ','); - hb_svg_append_unsigned (buf, hb_color_get_blue (c)); - hb_svg_append_str (buf, ")\""); + hb_buf_append_str (buf, "arrayZ[i].offset, 4); + hb_buf_append_str (buf, "\" stop-color=\"rgb("); + hb_buf_append_unsigned (buf, hb_color_get_red (c)); + hb_buf_append_c (buf, ','); + hb_buf_append_unsigned (buf, hb_color_get_green (c)); + hb_buf_append_c (buf, ','); + hb_buf_append_unsigned (buf, hb_color_get_blue (c)); + hb_buf_append_str (buf, ")\""); if (hb_color_get_alpha (c) != 255) { - hb_svg_append_str (buf, " stop-opacity=\""); - hb_svg_append_num (buf, hb_color_get_alpha (c) / 255.f, 4); - hb_svg_append_c (buf, '"'); + hb_buf_append_str (buf, " stop-opacity=\""); + hb_buf_append_num (buf, hb_color_get_alpha (c) / 255.f, 4); + hb_buf_append_c (buf, '"'); } - hb_svg_append_str (buf, "/>\n"); + hb_buf_append_str (buf, "/>\n"); } } @@ -314,29 +314,29 @@ hb_svg_add_sweep_patch (hb_vector_t *body, hb_svg_rgba_t mid_color = hb_svg_lerp_rgba (color0, color1, 0.5f); hb_color_t mid = hb_svg_hb_color_from_rgba (mid_color); - hb_svg_append_str (body, "\n"); + hb_buf_append_str (body, "\n"); p0 = p1; color0 = color1; @@ -637,19 +637,19 @@ hb_vector_paint_push_transform (hb_paint_funcs_t *, auto &body = paint->current_body (); unsigned sprec = hb_svg_scale_precision (paint->precision); - hb_svg_append_str (&body, "precision); - hb_svg_append_c (&body, ','); - hb_svg_append_num (&body, dy, paint->precision); - hb_svg_append_str (&body, ")\">\n"); + hb_buf_append_str (&body, "precision); + hb_buf_append_c (&body, ','); + hb_buf_append_num (&body, dy, paint->precision); + hb_buf_append_str (&body, ")\">\n"); } static void @@ -671,7 +671,7 @@ hb_vector_paint_pop_transform (hb_paint_funcs_t *, hb_bool_t opened = !!(paint->transform_group_open_mask & 1ull); paint->transform_group_open_mask >>= 1; if (opened) - hb_svg_append_str (&paint->current_body (), "\n"); + hb_buf_append_str (&paint->current_body (), "\n"); } static void @@ -691,26 +691,26 @@ hb_vector_paint_push_clip_glyph (hb_paint_funcs_t *, paint->path.clear (); hb_svg_path_sink_t sink = {&paint->path, paint->precision}; hb_font_draw_glyph (font, glyph, hb_svg_path_draw_funcs_get (), &sink); - hb_svg_append_str (&paint->defs, "defs, glyph); - hb_svg_append_str (&paint->defs, "\" d=\""); - hb_svg_append_len (&paint->defs, paint->path.arrayZ, paint->path.length); - hb_svg_append_str (&paint->defs, "\"/>\n"); + hb_buf_append_str (&paint->defs, "defs, glyph); + hb_buf_append_str (&paint->defs, "\" d=\""); + hb_buf_append_len (&paint->defs, paint->path.arrayZ, paint->path.length); + hb_buf_append_str (&paint->defs, "\"/>\n"); } if (!hb_set_has (paint->defined_clips, glyph)) { hb_set_add (paint->defined_clips, glyph); - hb_svg_append_str (&paint->defs, "defs, glyph); - hb_svg_append_str (&paint->defs, "\">defs, glyph); - hb_svg_append_str (&paint->defs, "\"/>\n"); + hb_buf_append_str (&paint->defs, "defs, glyph); + hb_buf_append_str (&paint->defs, "\">defs, glyph); + hb_buf_append_str (&paint->defs, "\"/>\n"); } - hb_svg_append_str (&paint->current_body (), "current_body (), glyph); - hb_svg_append_str (&paint->current_body (), ")\">\n"); + hb_buf_append_str (&paint->current_body (), "current_body (), glyph); + hb_buf_append_str (&paint->current_body (), ")\">\n"); } static void @@ -725,21 +725,21 @@ hb_vector_paint_push_clip_rectangle (hb_paint_funcs_t *, return; unsigned clip_id = paint->clip_rect_counter++; - hb_svg_append_str (&paint->defs, "defs, clip_id); - hb_svg_append_str (&paint->defs, "\">defs, xmin, paint->precision); - hb_svg_append_str (&paint->defs, "\" y=\""); - hb_svg_append_num (&paint->defs, ymin, paint->precision); - hb_svg_append_str (&paint->defs, "\" width=\""); - hb_svg_append_num (&paint->defs, xmax - xmin, paint->precision); - hb_svg_append_str (&paint->defs, "\" height=\""); - hb_svg_append_num (&paint->defs, ymax - ymin, paint->precision); - hb_svg_append_str (&paint->defs, "\"/>\n"); + hb_buf_append_str (&paint->defs, "defs, clip_id); + hb_buf_append_str (&paint->defs, "\">defs, xmin, paint->precision); + hb_buf_append_str (&paint->defs, "\" y=\""); + hb_buf_append_num (&paint->defs, ymin, paint->precision); + hb_buf_append_str (&paint->defs, "\" width=\""); + hb_buf_append_num (&paint->defs, xmax - xmin, paint->precision); + hb_buf_append_str (&paint->defs, "\" height=\""); + hb_buf_append_num (&paint->defs, ymax - ymin, paint->precision); + hb_buf_append_str (&paint->defs, "\"/>\n"); - hb_svg_append_str (&paint->current_body (), "current_body (), clip_id); - hb_svg_append_str (&paint->current_body (), ")\">\n"); + hb_buf_append_str (&paint->current_body (), "current_body (), clip_id); + hb_buf_append_str (&paint->current_body (), ")\">\n"); } static void @@ -750,7 +750,7 @@ hb_vector_paint_pop_clip (hb_paint_funcs_t *, auto *paint = (hb_vector_paint_t *) paint_data; if (unlikely (!hb_vector_paint_ensure_initialized (paint))) return; - hb_svg_append_str (&paint->current_body (), "\n"); + hb_buf_append_str (&paint->current_body (), "\n"); } static void @@ -772,9 +772,9 @@ hb_vector_paint_color (hb_paint_funcs_t *, (unsigned) hb_color_get_alpha (paint->foreground) * hb_color_get_alpha (color) / 255); auto &body = paint->current_body (); - hb_svg_append_str (&body, "\n"); + hb_buf_append_str (&body, "\n"); } static hb_bool_t @@ -809,24 +809,24 @@ hb_vector_paint_image (hb_paint_funcs_t *, if (extents) { - hb_svg_append_str (&body, "x_bearing, paint->precision); - hb_svg_append_c (&body, ','); - hb_svg_append_num (&body, (float) extents->y_bearing, paint->precision); - hb_svg_append_str (&body, ") scale("); - hb_svg_append_num (&body, (float) extents->width / width, paint->precision); - hb_svg_append_c (&body, ','); - hb_svg_append_num (&body, (float) extents->height / height, paint->precision); - hb_svg_append_str (&body, ")\">\n"); + hb_buf_append_str (&body, "x_bearing, paint->precision); + hb_buf_append_c (&body, ','); + hb_buf_append_num (&body, (float) extents->y_bearing, paint->precision); + hb_buf_append_str (&body, ") scale("); + hb_buf_append_num (&body, (float) extents->width / width, paint->precision); + hb_buf_append_c (&body, ','); + hb_buf_append_num (&body, (float) extents->height / height, paint->precision); + hb_buf_append_str (&body, ")\">\n"); } - hb_svg_append_len (&body, + hb_buf_append_len (&body, paint->subset_body_scratch.arrayZ, paint->subset_body_scratch.length); - hb_svg_append_c (&body, '\n'); + hb_buf_append_c (&body, '\n'); if (extents) - hb_svg_append_str (&body, "\n"); + hb_buf_append_str (&body, "\n"); return true; } @@ -841,23 +841,23 @@ hb_vector_paint_image (hb_paint_funcs_t *, if (!png_data || !len) return false; - hb_svg_append_str (&body, "x_bearing, paint->precision); - hb_svg_append_c (&body, ','); - hb_svg_append_num (&body, (float) extents->y_bearing, paint->precision); - hb_svg_append_str (&body, ") scale("); - hb_svg_append_num (&body, (float) extents->width / width, paint->precision); - hb_svg_append_c (&body, ','); - hb_svg_append_num (&body, (float) extents->height / height, paint->precision); - hb_svg_append_str (&body, ")\">\n"); + hb_buf_append_str (&body, "x_bearing, paint->precision); + hb_buf_append_c (&body, ','); + hb_buf_append_num (&body, (float) extents->y_bearing, paint->precision); + hb_buf_append_str (&body, ") scale("); + hb_buf_append_num (&body, (float) extents->width / width, paint->precision); + hb_buf_append_c (&body, ','); + hb_buf_append_num (&body, (float) extents->height / height, paint->precision); + hb_buf_append_str (&body, ")\">\n"); - hb_svg_append_str (&body, "precision); - hb_svg_append_str (&body, "\" height=\""); - hb_svg_append_num (&body, (float) height, paint->precision); - hb_svg_append_str (&body, "\"/>\n\n"); + hb_buf_append_str (&body, "precision); + hb_buf_append_str (&body, "\" height=\""); + hb_buf_append_num (&body, (float) height, paint->precision); + hb_buf_append_str (&body, "\"/>\n\n"); return true; } @@ -886,26 +886,26 @@ hb_vector_paint_linear_gradient (hb_paint_funcs_t *, unsigned grad_id = paint->gradient_counter++; - hb_svg_append_str (&paint->defs, "defs, grad_id); - hb_svg_append_str (&paint->defs, "\" gradientUnits=\"userSpaceOnUse\" x1=\""); - hb_svg_append_num (&paint->defs, x0, paint->precision); - hb_svg_append_str (&paint->defs, "\" y1=\""); - hb_svg_append_num (&paint->defs, y0, paint->precision); - hb_svg_append_str (&paint->defs, "\" x2=\""); - hb_svg_append_num (&paint->defs, x1 + (x1 - x2), paint->precision); - hb_svg_append_str (&paint->defs, "\" y2=\""); - hb_svg_append_num (&paint->defs, y1 + (y1 - y2), paint->precision); - hb_svg_append_str (&paint->defs, "\" spreadMethod=\""); - hb_svg_append_str (&paint->defs, hb_svg_extend_mode_str (hb_color_line_get_extend (color_line))); - hb_svg_append_str (&paint->defs, "\">\n"); + hb_buf_append_str (&paint->defs, "defs, grad_id); + hb_buf_append_str (&paint->defs, "\" gradientUnits=\"userSpaceOnUse\" x1=\""); + hb_buf_append_num (&paint->defs, x0, paint->precision); + hb_buf_append_str (&paint->defs, "\" y1=\""); + hb_buf_append_num (&paint->defs, y0, paint->precision); + hb_buf_append_str (&paint->defs, "\" x2=\""); + hb_buf_append_num (&paint->defs, x1 + (x1 - x2), paint->precision); + hb_buf_append_str (&paint->defs, "\" y2=\""); + hb_buf_append_num (&paint->defs, y1 + (y1 - y2), paint->precision); + hb_buf_append_str (&paint->defs, "\" spreadMethod=\""); + hb_buf_append_str (&paint->defs, hb_svg_extend_mode_str (hb_color_line_get_extend (color_line))); + hb_buf_append_str (&paint->defs, "\">\n"); hb_svg_emit_color_stops (paint, &paint->defs, &stops); - hb_svg_append_str (&paint->defs, "\n"); + hb_buf_append_str (&paint->defs, "\n"); - hb_svg_append_str (&paint->current_body (), + hb_buf_append_str (&paint->current_body (), "current_body (), grad_id); - hb_svg_append_str (&paint->current_body (), ")\"/>\n"); + hb_buf_append_unsigned (&paint->current_body (), grad_id); + hb_buf_append_str (&paint->current_body (), ")\"/>\n"); } static void @@ -928,33 +928,33 @@ hb_vector_paint_radial_gradient (hb_paint_funcs_t *, unsigned grad_id = paint->gradient_counter++; - hb_svg_append_str (&paint->defs, "defs, grad_id); - hb_svg_append_str (&paint->defs, "\" gradientUnits=\"userSpaceOnUse\" cx=\""); - hb_svg_append_num (&paint->defs, x1, paint->precision); - hb_svg_append_str (&paint->defs, "\" cy=\""); - hb_svg_append_num (&paint->defs, y1, paint->precision); - hb_svg_append_str (&paint->defs, "\" r=\""); - hb_svg_append_num (&paint->defs, r1, paint->precision); - hb_svg_append_str (&paint->defs, "\" fx=\""); - hb_svg_append_num (&paint->defs, x0, paint->precision); - hb_svg_append_str (&paint->defs, "\" fy=\""); - hb_svg_append_num (&paint->defs, y0, paint->precision); + hb_buf_append_str (&paint->defs, "defs, grad_id); + hb_buf_append_str (&paint->defs, "\" gradientUnits=\"userSpaceOnUse\" cx=\""); + hb_buf_append_num (&paint->defs, x1, paint->precision); + hb_buf_append_str (&paint->defs, "\" cy=\""); + hb_buf_append_num (&paint->defs, y1, paint->precision); + hb_buf_append_str (&paint->defs, "\" r=\""); + hb_buf_append_num (&paint->defs, r1, paint->precision); + hb_buf_append_str (&paint->defs, "\" fx=\""); + hb_buf_append_num (&paint->defs, x0, paint->precision); + hb_buf_append_str (&paint->defs, "\" fy=\""); + hb_buf_append_num (&paint->defs, y0, paint->precision); if (r0 > 0) { - hb_svg_append_str (&paint->defs, "\" fr=\""); - hb_svg_append_num (&paint->defs, r0, paint->precision); + hb_buf_append_str (&paint->defs, "\" fr=\""); + hb_buf_append_num (&paint->defs, r0, paint->precision); } - hb_svg_append_str (&paint->defs, "\" spreadMethod=\""); - hb_svg_append_str (&paint->defs, hb_svg_extend_mode_str (hb_color_line_get_extend (color_line))); - hb_svg_append_str (&paint->defs, "\">\n"); + hb_buf_append_str (&paint->defs, "\" spreadMethod=\""); + hb_buf_append_str (&paint->defs, hb_svg_extend_mode_str (hb_color_line_get_extend (color_line))); + hb_buf_append_str (&paint->defs, "\">\n"); hb_svg_emit_color_stops (paint, &paint->defs, &stops); - hb_svg_append_str (&paint->defs, "\n"); + hb_buf_append_str (&paint->defs, "\n"); - hb_svg_append_str (&paint->current_body (), + hb_buf_append_str (&paint->current_body (), "current_body (), grad_id); - hb_svg_append_str (&paint->current_body (), ")\"/>\n"); + hb_buf_append_unsigned (&paint->current_body (), grad_id); + hb_buf_append_str (&paint->current_body (), ")\"/>\n"); } static void @@ -1015,14 +1015,14 @@ hb_vector_paint_pop_group (hb_paint_funcs_t *, const char *blend = hb_svg_composite_mode_str (mode); if (blend) { - hb_svg_append_str (&body, "\n"); - hb_svg_append_len (&body, group.arrayZ, group.length); - hb_svg_append_str (&body, "\n"); + hb_buf_append_str (&body, "\n"); + hb_buf_append_len (&body, group.arrayZ, group.length); + hb_buf_append_str (&body, "\n"); } else - hb_svg_append_len (&body, group.arrayZ, group.length); + hb_buf_append_len (&body, group.arrayZ, group.length); } static hb_bool_t @@ -1512,26 +1512,26 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint, { /* PDF: emit transform + paint directly, no caching. */ auto &body = paint->current_body (); - hb_svg_append_str (&body, "q\n"); + hb_buf_append_str (&body, "q\n"); /* Font and PDF coords are both Y-up; no negation needed. */ - hb_svg_append_num (&body, xx, paint->precision); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, yx, paint->precision); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, xy, paint->precision); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, yy, paint->precision); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, tx, paint->precision); - hb_svg_append_c (&body, ' '); - hb_svg_append_num (&body, ty, paint->precision); - hb_svg_append_str (&body, " cm\n"); + hb_buf_append_num (&body, xx, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, yx, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, xy, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, yy, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, tx, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, ty, paint->precision); + hb_buf_append_str (&body, " cm\n"); hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph, hb_vector_pdf_paint_funcs_get (), paint, (unsigned) paint->palette, paint->foreground); - hb_svg_append_str (&body, "Q\n"); + hb_buf_append_str (&body, "Q\n"); return ret; } @@ -1547,9 +1547,9 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint, unsigned def_id = hb_svg_cache_entry_def_id (entry); bool image_like = hb_svg_cache_entry_image_like (entry); auto &body = paint->current_body (); - hb_svg_append_str (&body, "precision, paint->x_scale_factor, @@ -1560,7 +1560,7 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint, paint->x_scale_factor, paint->y_scale_factor, xx, yx, xy, yy, tx, ty); - hb_svg_append_str (&body, "\"/>\n"); + hb_buf_append_str (&body, "\"/>\n"); return !body.in_error (); } } @@ -1603,18 +1603,18 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint, has_svg_image)))) return false; - hb_svg_append_str (&paint->defs, "defs, def_id); - hb_svg_append_str (&paint->defs, "\">\n"); - hb_svg_append_len (&paint->defs, + hb_buf_append_str (&paint->defs, "defs, def_id); + hb_buf_append_str (&paint->defs, "\">\n"); + hb_buf_append_len (&paint->defs, paint->captured_scratch.arrayZ, paint->captured_scratch.length); - hb_svg_append_str (&paint->defs, "\n"); + hb_buf_append_str (&paint->defs, "\n"); auto &body = paint->current_body (); - hb_svg_append_str (&body, "precision, paint->x_scale_factor, @@ -1625,16 +1625,16 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint, paint->x_scale_factor, paint->y_scale_factor, xx, yx, xy, yy, tx, ty); - hb_svg_append_str (&body, "\"/>\n"); + hb_buf_append_str (&body, "\"/>\n"); return !paint->defs.in_error () && !body.in_error (); } - hb_svg_append_str (&paint->current_body (), "current_body (), "current_body (), paint->precision, paint->x_scale_factor, paint->y_scale_factor, xx, yx, xy, yy, tx, ty); - hb_svg_append_str (&paint->current_body (), "\">\n"); + hb_buf_append_str (&paint->current_body (), "\">\n"); hb_codepoint_t old_gid = paint->current_svg_image_glyph; hb_face_t *old_face = paint->current_face; paint->current_svg_image_glyph = glyph; @@ -1645,7 +1645,7 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint, paint->foreground); paint->current_svg_image_glyph = old_gid; paint->current_face = old_face; - hb_svg_append_str (&paint->current_body (), "\n"); + hb_buf_append_str (&paint->current_body (), "\n"); return ret && !paint->defs.in_error () && !paint->current_body ().in_error (); @@ -1736,39 +1736,39 @@ hb_vector_paint_render (hb_vector_paint_t *paint) return nullptr; hb_vector_t out; - hb_svg_recover_recycled_buffer (paint->recycled_blob, &out); + hb_buf_recover_recycled (paint->recycled_blob, &out); unsigned estimated = paint->defs.length + paint->group_stack.arrayZ[0].length + 320; out.alloc (estimated); - hb_svg_append_str (&out, "extents.x, paint->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, paint->extents.y, paint->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, paint->extents.width, paint->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, paint->extents.height, paint->precision); - hb_svg_append_str (&out, "\" width=\""); - hb_svg_append_num (&out, paint->extents.width, paint->precision); - hb_svg_append_str (&out, "\" height=\""); - hb_svg_append_num (&out, paint->extents.height, paint->precision); - hb_svg_append_str (&out, "\">\n"); + hb_buf_append_str (&out, "extents.x, paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, paint->extents.y, paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, paint->extents.width, paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, paint->extents.height, paint->precision); + hb_buf_append_str (&out, "\" width=\""); + hb_buf_append_num (&out, paint->extents.width, paint->precision); + hb_buf_append_str (&out, "\" height=\""); + hb_buf_append_num (&out, paint->extents.height, paint->precision); + hb_buf_append_str (&out, "\">\n"); if (paint->defs.length) { - hb_svg_append_str (&out, "\n"); - hb_svg_append_len (&out, paint->defs.arrayZ, paint->defs.length); - hb_svg_append_str (&out, "\n"); + hb_buf_append_str (&out, "\n"); + hb_buf_append_len (&out, paint->defs.arrayZ, paint->defs.length); + hb_buf_append_str (&out, "\n"); } hb_svg_paint_append_global_transform_prefix (paint, &out); - hb_svg_append_len (&out, paint->group_stack.arrayZ[0].arrayZ, paint->group_stack.arrayZ[0].length); + hb_buf_append_len (&out, paint->group_stack.arrayZ[0].arrayZ, paint->group_stack.arrayZ[0].length); hb_svg_paint_append_global_transform_suffix (paint, &out); - hb_svg_append_str (&out, "\n"); + hb_buf_append_str (&out, "\n"); - hb_blob_t *blob = hb_svg_blob_from_buffer (&paint->recycled_blob, &out); + hb_blob_t *blob = hb_buf_blob_from (&paint->recycled_blob, &out); hb_vector_paint_clear_render_state (paint); diff --git a/src/hb-vector-svg-path.cc b/src/hb-vector-svg-path.cc index 286bc31a6..26acefb04 100644 --- a/src/hb-vector-svg-path.cc +++ b/src/hb-vector-svg-path.cc @@ -38,10 +38,10 @@ hb_svg_path_move_to (hb_draw_funcs_t *, void *) { auto *s = (hb_svg_path_sink_t *) draw_data; - hb_svg_append_c (s->path, 'M'); - hb_svg_append_num (s->path, to_x, s->precision); - hb_svg_append_c (s->path, ','); - hb_svg_append_num (s->path, to_y, s->precision); + hb_buf_append_c (s->path, 'M'); + hb_buf_append_num (s->path, to_x, s->precision); + hb_buf_append_c (s->path, ','); + hb_buf_append_num (s->path, to_y, s->precision); } static void @@ -52,10 +52,10 @@ hb_svg_path_line_to (hb_draw_funcs_t *, void *) { auto *s = (hb_svg_path_sink_t *) draw_data; - hb_svg_append_c (s->path, 'L'); - hb_svg_append_num (s->path, to_x, s->precision); - hb_svg_append_c (s->path, ','); - hb_svg_append_num (s->path, to_y, s->precision); + hb_buf_append_c (s->path, 'L'); + hb_buf_append_num (s->path, to_x, s->precision); + hb_buf_append_c (s->path, ','); + hb_buf_append_num (s->path, to_y, s->precision); } static void @@ -67,14 +67,14 @@ hb_svg_path_quadratic_to (hb_draw_funcs_t *, void *) { auto *s = (hb_svg_path_sink_t *) draw_data; - hb_svg_append_c (s->path, 'Q'); - hb_svg_append_num (s->path, cx, s->precision); - hb_svg_append_c (s->path, ','); - hb_svg_append_num (s->path, cy, s->precision); - hb_svg_append_c (s->path, ' '); - hb_svg_append_num (s->path, to_x, s->precision); - hb_svg_append_c (s->path, ','); - hb_svg_append_num (s->path, to_y, s->precision); + hb_buf_append_c (s->path, 'Q'); + hb_buf_append_num (s->path, cx, s->precision); + hb_buf_append_c (s->path, ','); + hb_buf_append_num (s->path, cy, s->precision); + hb_buf_append_c (s->path, ' '); + hb_buf_append_num (s->path, to_x, s->precision); + hb_buf_append_c (s->path, ','); + hb_buf_append_num (s->path, to_y, s->precision); } static void @@ -87,18 +87,18 @@ hb_svg_path_cubic_to (hb_draw_funcs_t *, void *) { auto *s = (hb_svg_path_sink_t *) draw_data; - hb_svg_append_c (s->path, 'C'); - hb_svg_append_num (s->path, c1x, s->precision); - hb_svg_append_c (s->path, ','); - hb_svg_append_num (s->path, c1y, s->precision); - hb_svg_append_c (s->path, ' '); - hb_svg_append_num (s->path, c2x, s->precision); - hb_svg_append_c (s->path, ','); - hb_svg_append_num (s->path, c2y, s->precision); - hb_svg_append_c (s->path, ' '); - hb_svg_append_num (s->path, to_x, s->precision); - hb_svg_append_c (s->path, ','); - hb_svg_append_num (s->path, to_y, s->precision); + hb_buf_append_c (s->path, 'C'); + hb_buf_append_num (s->path, c1x, s->precision); + hb_buf_append_c (s->path, ','); + hb_buf_append_num (s->path, c1y, s->precision); + hb_buf_append_c (s->path, ' '); + hb_buf_append_num (s->path, c2x, s->precision); + hb_buf_append_c (s->path, ','); + hb_buf_append_num (s->path, c2y, s->precision); + hb_buf_append_c (s->path, ' '); + hb_buf_append_num (s->path, to_x, s->precision); + hb_buf_append_c (s->path, ','); + hb_buf_append_num (s->path, to_y, s->precision); } static void @@ -108,7 +108,7 @@ hb_svg_path_close_path (hb_draw_funcs_t *, void *) { auto *s = (hb_svg_path_sink_t *) draw_data; - hb_svg_append_c (s->path, 'Z'); + hb_buf_append_c (s->path, 'Z'); } static inline void diff --git a/src/hb-vector-svg-subset.cc b/src/hb-vector-svg-subset.cc index 57209bc65..2c3e0aaea 100644 --- a/src/hb-vector-svg-subset.cc +++ b/src/hb-vector-svg-subset.cc @@ -63,113 +63,113 @@ hb_svg_append_with_prefix (hb_vector_t *out, { if (i + 4 <= n && !memcmp (s + i, "id=\"", 4)) { - if (!hb_svg_append_len (out, s + i, 4)) return false; + if (!hb_buf_append_len (out, s + i, 4)) return false; i += 4; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != '"') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } if (i + 4 <= n && !memcmp (s + i, "id='", 4)) { - if (!hb_svg_append_len (out, s + i, 4)) return false; + if (!hb_buf_append_len (out, s + i, 4)) return false; i += 4; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != '\'') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } if (i + 7 <= n && !memcmp (s + i, "href=\"#", 7)) { - if (!hb_svg_append_len (out, s + i, 7)) return false; + if (!hb_buf_append_len (out, s + i, 7)) return false; i += 7; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != '"') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } if (i + 7 <= n && !memcmp (s + i, "href='#", 7)) { - if (!hb_svg_append_len (out, s + i, 7)) return false; + if (!hb_buf_append_len (out, s + i, 7)) return false; i += 7; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != '\'') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } if (i + 13 <= n && !memcmp (s + i, "xlink:href=\"#", 13)) { - if (!hb_svg_append_len (out, s + i, 13)) return false; + if (!hb_buf_append_len (out, s + i, 13)) return false; i += 13; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != '"') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } if (i + 13 <= n && !memcmp (s + i, "xlink:href='#", 13)) { - if (!hb_svg_append_len (out, s + i, 13)) return false; + if (!hb_buf_append_len (out, s + i, 13)) return false; i += 13; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != '\'') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } if (i + 5 <= n && !memcmp (s + i, "url(#", 5)) { - if (!hb_svg_append_len (out, s + i, 5)) return false; + if (!hb_buf_append_len (out, s + i, 5)) return false; i += 5; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != ')') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } if (i + 6 <= n && !memcmp (s + i, "url(\"#", 6)) { - if (!hb_svg_append_len (out, s + i, 6)) return false; + if (!hb_buf_append_len (out, s + i, 6)) return false; i += 6; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != '"') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } if (i + 6 <= n && !memcmp (s + i, "url('#", 6)) { - if (!hb_svg_append_len (out, s + i, 6)) return false; + if (!hb_buf_append_len (out, s + i, 6)) return false; i += 6; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != '\'') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } - if (!hb_svg_append_c (out, s[i])) + if (!hb_buf_append_c (out, s[i])) return false; i++; } @@ -392,7 +392,7 @@ hb_svg_subset_glyph_image (hb_face_t *face, goto done; if (!hb_svg_append_with_prefix (defs_dst, svg + e.start, e.end - e.start, prefix, (unsigned) prefix_len)) goto done; - if (!hb_svg_append_c (defs_dst, '\n')) + if (!hb_buf_append_c (defs_dst, '\n')) goto done; } @@ -407,19 +407,19 @@ hb_svg_subset_glyph_image (hb_face_t *face, prefix, (unsigned) prefix_len)) goto done; - if (!hb_svg_append_str (body_dst, " width=\"")) + if (!hb_buf_append_str (body_dst, " width=\"")) goto done; - if (!hb_svg_append_unsigned (body_dst, upem)) + if (!hb_buf_append_unsigned (body_dst, upem)) goto done; - if (!hb_svg_append_str (body_dst, "\" height=\"")) + if (!hb_buf_append_str (body_dst, "\" height=\"")) goto done; - if (!hb_svg_append_unsigned (body_dst, upem)) + if (!hb_buf_append_unsigned (body_dst, upem)) goto done; - if (!hb_svg_append_str (body_dst, "\" overflow=\"visible")) + if (!hb_buf_append_str (body_dst, "\" overflow=\"visible")) goto done; - if (!hb_svg_append_c (body_dst, '"')) + if (!hb_buf_append_c (body_dst, '"')) goto done; - if (!hb_svg_append_c (body_dst, '>')) + if (!hb_buf_append_c (body_dst, '>')) goto done; ret = hb_svg_append_with_prefix (body_dst, svg + glyph_start + root_open_len, @@ -437,9 +437,9 @@ hb_svg_subset_glyph_image (hb_face_t *face, prefix, (unsigned) prefix_len)) goto done; - if (!hb_svg_append_str (body_dst, " overflow=\"visible\"")) + if (!hb_buf_append_str (body_dst, " overflow=\"visible\"")) goto done; - if (!hb_svg_append_c (body_dst, '>')) + if (!hb_buf_append_c (body_dst, '>')) goto done; ret = hb_svg_append_with_prefix (body_dst, svg + glyph_start + root_open_len, diff --git a/src/hb-vector-svg-utils.hh b/src/hb-vector-svg-utils.hh index 138b51b66..e29972dad 100644 --- a/src/hb-vector-svg-utils.hh +++ b/src/hb-vector-svg-utils.hh @@ -37,7 +37,7 @@ HB_INTERNAL const char * hb_svg_decimal_point_get (void); static inline bool -hb_svg_append_len (hb_vector_t *buf, +hb_buf_append_len (hb_vector_t *buf, const char *s, unsigned len) { @@ -49,13 +49,13 @@ hb_svg_append_len (hb_vector_t *buf, } static inline bool -hb_svg_append_c (hb_vector_t *buf, char c) +hb_buf_append_c (hb_vector_t *buf, char c) { return buf->push_or_fail (c); } static inline void -hb_svg_append_num (hb_vector_t *buf, +hb_buf_append_num (hb_vector_t *buf, float v, unsigned precision, bool keep_nonzero = false) @@ -82,7 +82,7 @@ hb_svg_append_num (hb_vector_t *buf, if (!(v == v) || !std::isfinite (v)) { - hb_svg_append_c (buf, '0'); + hb_buf_append_c (buf, '0'); return; } @@ -117,7 +117,7 @@ hb_svg_append_num (hb_vector_t *buf, *end = '\0'; } - hb_svg_append_len (buf, out, (unsigned) strlen (out)); + hb_buf_append_len (buf, out, (unsigned) strlen (out)); } static inline unsigned diff --git a/src/hb-vector-svg.hh b/src/hb-vector-svg.hh index 418d5bc46..5cab81e64 100644 --- a/src/hb-vector-svg.hh +++ b/src/hb-vector-svg.hh @@ -4,13 +4,13 @@ #include "hb-vector.h" static inline bool -hb_svg_append_str (hb_vector_t *buf, const char *s) +hb_buf_append_str (hb_vector_t *buf, const char *s) { - return hb_svg_append_len (buf, s, (unsigned) strlen (s)); + return hb_buf_append_len (buf, s, (unsigned) strlen (s)); } static inline bool -hb_svg_append_unsigned (hb_vector_t *buf, unsigned v) +hb_buf_append_unsigned (hb_vector_t *buf, unsigned v) { char tmp[10]; unsigned n = 0; @@ -29,15 +29,15 @@ hb_svg_append_unsigned (hb_vector_t *buf, unsigned v) } static inline bool -hb_svg_append_hex_byte (hb_vector_t *buf, unsigned v) +hb_buf_append_hex_byte (hb_vector_t *buf, unsigned v) { static const char hex[] = "0123456789ABCDEF"; char tmp[2] = {hex[(v >> 4) & 15], hex[v & 15]}; - return hb_svg_append_len (buf, tmp, 2); + return hb_buf_append_len (buf, tmp, 2); } static inline bool -hb_svg_append_base64 (hb_vector_t *buf, +hb_buf_append_base64 (hb_vector_t *buf, const uint8_t *data, unsigned len) { @@ -124,7 +124,7 @@ hb_svg_blob_meta_destroy (void *data) } static inline hb_blob_t * -hb_svg_blob_from_buffer (hb_blob_t **recycled_blob, +hb_buf_blob_from (hb_blob_t **recycled_blob, hb_vector_t *buf) { unsigned len = 0; @@ -197,7 +197,7 @@ hb_svg_blob_from_buffer (hb_blob_t **recycled_blob, } static inline void -hb_svg_recover_recycled_buffer (hb_blob_t *blob, +hb_buf_recover_recycled (hb_blob_t *blob, hb_vector_t *buf) { if (!blob) @@ -214,7 +214,7 @@ hb_svg_recover_recycled_buffer (hb_blob_t *blob, } static inline void -hb_svg_append_color (hb_vector_t *buf, +hb_buf_append_color (hb_vector_t *buf, hb_color_t color, bool with_alpha) { @@ -223,25 +223,25 @@ hb_svg_append_color (hb_vector_t *buf, unsigned g = hb_color_get_green (color); unsigned b = hb_color_get_blue (color); unsigned a = hb_color_get_alpha (color); - hb_svg_append_c (buf, '#'); + hb_buf_append_c (buf, '#'); if (((r >> 4) == (r & 0xF)) && ((g >> 4) == (g & 0xF)) && ((b >> 4) == (b & 0xF))) { - hb_svg_append_c (buf, hex[r & 0xF]); - hb_svg_append_c (buf, hex[g & 0xF]); - hb_svg_append_c (buf, hex[b & 0xF]); + hb_buf_append_c (buf, hex[r & 0xF]); + hb_buf_append_c (buf, hex[g & 0xF]); + hb_buf_append_c (buf, hex[b & 0xF]); } else { - hb_svg_append_hex_byte (buf, r); - hb_svg_append_hex_byte (buf, g); - hb_svg_append_hex_byte (buf, b); + hb_buf_append_hex_byte (buf, r); + hb_buf_append_hex_byte (buf, g); + hb_buf_append_hex_byte (buf, b); } if (with_alpha && a != 255) { - hb_svg_append_str (buf, "\" fill-opacity=\""); - hb_svg_append_num (buf, a / 255.f, 4); + hb_buf_append_str (buf, "\" fill-opacity=\""); + hb_buf_append_num (buf, a / 255.f, 4); } } @@ -321,31 +321,31 @@ hb_svg_append_instance_transform (hb_vector_t *out, { float sx = 1.f / x_scale_factor; float sy = 1.f / y_scale_factor; - hb_svg_append_str (out, "translate("); - hb_svg_append_num (out, tx / x_scale_factor, precision); - hb_svg_append_c (out, ','); - hb_svg_append_num (out, -ty / y_scale_factor, precision); - hb_svg_append_str (out, ") scale("); - hb_svg_append_num (out, sx, sprec, true); - hb_svg_append_c (out, ','); - hb_svg_append_num (out, -sy, sprec, true); - hb_svg_append_c (out, ')'); + hb_buf_append_str (out, "translate("); + hb_buf_append_num (out, tx / x_scale_factor, precision); + hb_buf_append_c (out, ','); + hb_buf_append_num (out, -ty / y_scale_factor, precision); + hb_buf_append_str (out, ") scale("); + hb_buf_append_num (out, sx, sprec, true); + hb_buf_append_c (out, ','); + hb_buf_append_num (out, -sy, sprec, true); + hb_buf_append_c (out, ')'); } else { - hb_svg_append_str (out, "matrix("); - hb_svg_append_num (out, xx / x_scale_factor, sprec, true); - hb_svg_append_c (out, ','); - hb_svg_append_num (out, yx / y_scale_factor, sprec, true); - hb_svg_append_c (out, ','); - hb_svg_append_num (out, -xy / x_scale_factor, sprec, true); - hb_svg_append_c (out, ','); - hb_svg_append_num (out, -yy / y_scale_factor, sprec, true); - hb_svg_append_c (out, ','); - hb_svg_append_num (out, tx / x_scale_factor, precision); - hb_svg_append_c (out, ','); - hb_svg_append_num (out, -ty / y_scale_factor, precision); - hb_svg_append_c (out, ')'); + hb_buf_append_str (out, "matrix("); + hb_buf_append_num (out, xx / x_scale_factor, sprec, true); + hb_buf_append_c (out, ','); + hb_buf_append_num (out, yx / y_scale_factor, sprec, true); + hb_buf_append_c (out, ','); + hb_buf_append_num (out, -xy / x_scale_factor, sprec, true); + hb_buf_append_c (out, ','); + hb_buf_append_num (out, -yy / y_scale_factor, sprec, true); + hb_buf_append_c (out, ','); + hb_buf_append_num (out, tx / x_scale_factor, precision); + hb_buf_append_c (out, ','); + hb_buf_append_num (out, -ty / y_scale_factor, precision); + hb_buf_append_c (out, ')'); } } @@ -356,11 +356,11 @@ hb_svg_append_image_instance_translate (hb_vector_t *out, float y_scale_factor, float tx, float ty) { - hb_svg_append_str (out, "translate("); - hb_svg_append_num (out, tx / x_scale_factor, precision); - hb_svg_append_c (out, ','); - hb_svg_append_num (out, -ty / y_scale_factor, precision); - hb_svg_append_c (out, ')'); + hb_buf_append_str (out, "translate("); + hb_buf_append_num (out, tx / x_scale_factor, precision); + hb_buf_append_c (out, ','); + hb_buf_append_num (out, -ty / y_scale_factor, precision); + hb_buf_append_c (out, ')'); } #endif /* HB_VECTOR_SVG_HH */ From 39f622285e6768c7483899c6564b403577d77d6e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 22:40:19 -0600 Subject: [PATCH 14/23] [vector] Fix PDF sweep gradient to tile across full circle MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous implementation only covered start_angle to end_angle with a fixed number of equal sectors, which broke repeat/reflect extend modes and produced too few patches for oscillating gradients. Rewrite to use the same tiling logic as the SVG sweep gradient: map color stops to angles, handle pad/repeat/reflect extend modes, and tile across the full 0-2π range. Each color stop interval becomes one Coons patch (further split at 90° max for arc quality). Tested with sweep_270_440_reflect_narrow glyph from test_glyphs-glyf_colr_1_variable.ttf. Assisted-by: Claude Opus 4.6 (1M context) --- src/hb-vector-pdf-paint.cc | 403 +++++++++++++++++++++++++------------ 1 file changed, 275 insertions(+), 128 deletions(-) diff --git a/src/hb-vector-pdf-paint.cc b/src/hb-vector-pdf-paint.cc index 9908627ef..3c502907c 100644 --- a/src/hb-vector-pdf-paint.cc +++ b/src/hb-vector-pdf-paint.cc @@ -551,46 +551,13 @@ hb_pdf_paint_radial_gradient (hb_paint_funcs_t *, hb_buf_append_str (&body, " sh\n"); } -/* Evaluate color line at a given offset. */ static hb_color_t -hb_pdf_evaluate_color_line (hb_color_stop_t *stops, unsigned len, - float t, hb_paint_extend_t extend) +hb_pdf_lerp_color (hb_color_t c0, hb_color_t c1, float t) { - if (!len) - return HB_COLOR (0, 0, 0, 255); - - /* Handle extend modes. */ - if (extend == HB_PAINT_EXTEND_REPEAT) - t = t - floorf (t); - else if (extend == HB_PAINT_EXTEND_REFLECT) - { - float ft = floorf (t); - t = t - ft; - if (((int) ft) & 1) - t = 1.f - t; - } - else /* PAD */ - t = hb_clamp (t, 0.f, 1.f); - - if (t <= stops[0].offset) - return stops[0].color; - if (t >= stops[len - 1].offset) - return stops[len - 1].color; - - unsigned i = 0; - for (; i + 1 < len; i++) - if (stops[i + 1].offset >= t) - break; - - float range = stops[i + 1].offset - stops[i].offset; - float frac = range > 0.f ? (t - stops[i].offset) / range : 0.f; - - hb_color_t c0 = stops[i].color; - hb_color_t c1 = stops[i + 1].color; auto lerp = [&] (unsigned shift) -> unsigned { unsigned v0 = (c0 >> shift) & 0xFF; unsigned v1 = (c1 >> shift) & 0xFF; - return (unsigned) (v0 + frac * ((float) v1 - (float) v0) + 0.5f); + return (unsigned) (v0 + t * ((float) v1 - (float) v0) + 0.5f); }; return HB_COLOR (lerp (16), lerp (8), lerp (0), lerp (24)); } @@ -634,6 +601,88 @@ hb_pdf_encode_point (hb_vector_t *buf, hb_pdf_encode_coord (buf, y, ylo, yhi); } +/* Emit one Coons patch sector into the mesh stream. + * Splits large arcs into sub-patches of max 90°. */ +static void +hb_pdf_add_sweep_patch (hb_vector_t *mesh, + float cx, float cy, + float xlo, float xhi, float ylo, float yhi, + float a0, hb_color_t c0_in, + float a1, hb_color_t c1_in) +{ + const float R = 32767.f; + const float eps = 0.5f; + const float MAX_SECTOR = (float) M_PI / 2.f; + + int num_splits = (int) ceilf (fabsf (a1 - a0) / MAX_SECTOR); + if (num_splits < 1) num_splits = 1; + + for (int s = 0; s < num_splits; s++) + { + float k0 = (float) s / num_splits; + float k1 = (float) (s + 1) / num_splits; + float sa0 = a0 + k0 * (a1 - a0); + float sa1 = a0 + k1 * (a1 - a0); + hb_color_t sc0 = hb_pdf_lerp_color (c0_in, c1_in, k0); + hb_color_t sc1 = hb_pdf_lerp_color (c0_in, c1_in, k1); + + float da = sa1 - sa0; + float kappa = (4.f / 3.f) * tanf (da / 4.f); + + float cos0 = cosf (sa0), sin0 = sinf (sa0); + float cos1 = cosf (sa1), sin1 = sinf (sa1); + + float p0x = cx + eps * cos0, p0y = cy + eps * sin0; + float p3x = cx + R * cos0, p3y = cy + R * sin0; + float p6x = cx + R * cos1, p6y = cy + R * sin1; + float p9x = cx + eps * cos1, p9y = cy + eps * sin1; + + /* Edge 1: p0→p3, radial straight line. */ + float e1_1x = p0x + (p3x - p0x) / 3.f; + float e1_1y = p0y + (p3y - p0y) / 3.f; + float e1_2x = p0x + 2.f * (p3x - p0x) / 3.f; + float e1_2y = p0y + 2.f * (p3y - p0y) / 3.f; + + /* Edge 2: p3→p6, outer arc. */ + float e2_1x = p3x + kappa * R * (-sin0); + float e2_1y = p3y + kappa * R * ( cos0); + float e2_2x = p6x - kappa * R * (-sin1); + float e2_2y = p6y - kappa * R * ( cos1); + + /* Edge 3: p6→p9, radial straight line. */ + float e3_1x = p6x + (p9x - p6x) / 3.f; + float e3_1y = p6y + (p9y - p6y) / 3.f; + float e3_2x = p6x + 2.f * (p9x - p6x) / 3.f; + float e3_2y = p6y + 2.f * (p9y - p6y) / 3.f; + + /* Edge 4: p9→p0, inner arc. */ + float e4_1x = p9x + kappa * eps * (-sin1); + float e4_1y = p9y + kappa * eps * ( cos1); + float e4_2x = p0x - kappa * eps * (-sin0); + float e4_2y = p0y - kappa * eps * ( cos0); + + hb_buf_append_c (mesh, '\0'); /* flag = 0, new patch */ + + hb_pdf_encode_point (mesh, p0x, p0y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, e1_1x, e1_1y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, e1_2x, e1_2y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, p3x, p3y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, e2_1x, e2_1y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, e2_2x, e2_2y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, p6x, p6y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, e3_1x, e3_1y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, e3_2x, e3_2y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, p9x, p9y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, e4_1x, e4_1y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, e4_2x, e4_2y, xlo, xhi, ylo, yhi); + + hb_pdf_encode_color_rgb (mesh, sc0); /* inner start */ + hb_pdf_encode_color_rgb (mesh, sc0); /* outer start */ + hb_pdf_encode_color_rgb (mesh, sc1); /* outer end */ + hb_pdf_encode_color_rgb (mesh, sc1); /* inner end */ + } +} + static void hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, void *paint_data, @@ -651,114 +700,212 @@ hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, return; /* Get and sort color stops. */ - unsigned count = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr); - paint->color_stops_scratch.resize (count); - hb_color_line_get_color_stops (color_line, 0, &count, paint->color_stops_scratch.arrayZ); + unsigned n_stops = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr); + paint->color_stops_scratch.resize (n_stops); + hb_color_line_get_color_stops (color_line, 0, &n_stops, paint->color_stops_scratch.arrayZ); paint->color_stops_scratch.as_array ().qsort ( [] (const hb_color_stop_t &a, const hb_color_stop_t &b) { return a.offset < b.offset; }); + hb_color_stop_t *stops = paint->color_stops_scratch.arrayZ; + + if (!n_stops) + return; hb_paint_extend_t extend = hb_color_line_get_extend (color_line); - float angle_range = end_angle - start_angle; - if (fabsf (angle_range) < 1e-10f) - return; - - /* Parameters. */ const float R = 32767.f; - const float eps = 0.5f; /* small inner radius to avoid degenerate patch */ - const float MAX_SECTOR = (float) M_PI / 2.f; /* max 90° per patch */ - - unsigned num_sectors = (unsigned) ceilf (fabsf (angle_range) / MAX_SECTOR); - if (num_sectors < 4) num_sectors = 4; - - /* Coordinate bounds for Decode. */ float xlo = cx - R - 1, xhi = cx + R + 1; float ylo = cy - R - 1, yhi = cy + R + 1; - /* Build mesh data stream. */ hb_vector_t mesh; - mesh.alloc (num_sectors * 64); + mesh.alloc (256); - for (unsigned i = 0; i < num_sectors; i++) + /* Same tiling logic as hb_svg_add_sweep_gradient_patches. */ + + if (start_angle == end_angle) { - float t0 = (float) i / num_sectors; - float t1 = (float) (i + 1) / num_sectors; - float a0 = start_angle + t0 * angle_range; - float a1 = start_angle + t1 * angle_range; - float da = a1 - a0; - - /* Bézier approximation factor for circular arc. */ - float kappa = (4.f / 3.f) * tanf (da / 4.f); - - float cos0 = cosf (a0), sin0 = sinf (a0); - float cos1 = cosf (a1), sin1 = sinf (a1); - - /* 4 corners: - * p0 = inner at a0 - * p3 = outer at a0 - * p6 = outer at a1 - * p9 = inner at a1 - */ - float p0x = cx + eps * cos0, p0y = cy + eps * sin0; - float p3x = cx + R * cos0, p3y = cy + R * sin0; - float p6x = cx + R * cos1, p6y = cy + R * sin1; - float p9x = cx + eps * cos1, p9y = cy + eps * sin1; - - /* Edge 1: p0→p3, radial line (straight Bézier). */ - float e1_1x = p0x + (p3x - p0x) / 3.f; - float e1_1y = p0y + (p3y - p0y) / 3.f; - float e1_2x = p0x + 2.f * (p3x - p0x) / 3.f; - float e1_2y = p0y + 2.f * (p3y - p0y) / 3.f; - - /* Edge 2: p3→p6, outer arc. */ - float e2_1x = p3x + kappa * R * (-sin0); - float e2_1y = p3y + kappa * R * ( cos0); - float e2_2x = p6x - kappa * R * (-sin1); - float e2_2y = p6y - kappa * R * ( cos1); - - /* Edge 3: p6→p9, radial line (straight, reversed). */ - float e3_1x = p6x + (p9x - p6x) / 3.f; - float e3_1y = p6y + (p9y - p6y) / 3.f; - float e3_2x = p6x + 2.f * (p9x - p6x) / 3.f; - float e3_2y = p6y + 2.f * (p9y - p6y) / 3.f; - - /* Edge 4: p9→p0, inner arc (reversed). */ - float e4_1x = p9x + kappa * eps * (-sin1); - float e4_1y = p9y + kappa * eps * ( cos1); - float e4_2x = p0x - kappa * eps * (-sin0); - float e4_2y = p0y - kappa * eps * ( cos0); - - /* Evaluate colors. */ - hb_color_t c0 = hb_pdf_evaluate_color_line (paint->color_stops_scratch.arrayZ, - count, t0, extend); - hb_color_t c1 = hb_pdf_evaluate_color_line (paint->color_stops_scratch.arrayZ, - count, t1, extend); - - /* Flag byte. */ - hb_buf_append_c (&mesh, '\0'); /* flag = 0, new patch */ - - /* 12 control points: edges 1,2,3,4. */ - hb_pdf_encode_point (&mesh, p0x, p0y, xlo, xhi, ylo, yhi); /* p0 */ - hb_pdf_encode_point (&mesh, e1_1x, e1_1y, xlo, xhi, ylo, yhi); /* p1 */ - hb_pdf_encode_point (&mesh, e1_2x, e1_2y, xlo, xhi, ylo, yhi); /* p2 */ - hb_pdf_encode_point (&mesh, p3x, p3y, xlo, xhi, ylo, yhi); /* p3 */ - hb_pdf_encode_point (&mesh, e2_1x, e2_1y, xlo, xhi, ylo, yhi); /* p4 */ - hb_pdf_encode_point (&mesh, e2_2x, e2_2y, xlo, xhi, ylo, yhi); /* p5 */ - hb_pdf_encode_point (&mesh, p6x, p6y, xlo, xhi, ylo, yhi); /* p6 */ - hb_pdf_encode_point (&mesh, e3_1x, e3_1y, xlo, xhi, ylo, yhi); /* p7 */ - hb_pdf_encode_point (&mesh, e3_2x, e3_2y, xlo, xhi, ylo, yhi); /* p8 */ - hb_pdf_encode_point (&mesh, p9x, p9y, xlo, xhi, ylo, yhi); /* p9 */ - hb_pdf_encode_point (&mesh, e4_1x, e4_1y, xlo, xhi, ylo, yhi); /* p10 */ - hb_pdf_encode_point (&mesh, e4_2x, e4_2y, xlo, xhi, ylo, yhi); /* p11 */ - - /* 4 corner colors: c0(p0), c1(p3), c2(p6), c3(p9). */ - hb_pdf_encode_color_rgb (&mesh, c0); /* c0 = inner start */ - hb_pdf_encode_color_rgb (&mesh, c0); /* c1 = outer start (same angle) */ - hb_pdf_encode_color_rgb (&mesh, c1); /* c2 = outer end */ - hb_pdf_encode_color_rgb (&mesh, c1); /* c3 = inner end */ + if (extend == HB_PAINT_EXTEND_PAD) + { + if (start_angle > 0.f) + hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, + 0.f, stops[0].color, start_angle, stops[0].color); + if (end_angle < HB_2_PI) + hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, + end_angle, stops[n_stops - 1].color, HB_2_PI, stops[n_stops - 1].color); + } + goto emit; } + if (end_angle < start_angle) + { + float tmp = start_angle; start_angle = end_angle; end_angle = tmp; + for (unsigned i = 0; i < n_stops - 1 - i; i++) + { + hb_color_stop_t t = stops[i]; + stops[i] = stops[n_stops - 1 - i]; + stops[n_stops - 1 - i] = t; + } + for (unsigned i = 0; i < n_stops; i++) + stops[i].offset = 1.f - stops[i].offset; + } + + { + /* Map stop offsets to angles. */ + float angles_buf[16]; + hb_color_t colors_buf[16]; + float *angles = angles_buf; + hb_color_t *colors = colors_buf; + bool dynamic = false; + + if (n_stops > 16) + { + angles = (float *) hb_malloc (sizeof (float) * n_stops); + colors = (hb_color_t *) hb_malloc (sizeof (hb_color_t) * n_stops); + if (!angles || !colors) + { + hb_free (angles); + hb_free (colors); + goto emit; + } + dynamic = true; + } + + for (unsigned i = 0; i < n_stops; i++) + { + angles[i] = start_angle + stops[i].offset * (end_angle - start_angle); + colors[i] = stops[i].color; + } + + if (extend == HB_PAINT_EXTEND_PAD) + { + unsigned pos; + hb_color_t color0 = colors[0]; + for (pos = 0; pos < n_stops; pos++) + { + if (angles[pos] >= 0) + { + if (pos > 0) + { + float f = (0.f - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); + color0 = hb_pdf_lerp_color (colors[pos - 1], colors[pos], f); + } + break; + } + } + if (pos == n_stops) + { + color0 = colors[n_stops - 1]; + hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, + 0.f, color0, HB_2_PI, color0); + goto done; + } + hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, + 0.f, color0, angles[pos], colors[pos]); + for (pos++; pos < n_stops; pos++) + { + if (angles[pos] <= HB_2_PI) + hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, + angles[pos - 1], colors[pos - 1], angles[pos], colors[pos]); + else + { + float f = (HB_2_PI - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); + hb_color_t color1 = hb_pdf_lerp_color (colors[pos - 1], colors[pos], f); + hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, + angles[pos - 1], colors[pos - 1], HB_2_PI, color1); + break; + } + } + if (pos == n_stops) + { + color0 = colors[n_stops - 1]; + hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, + angles[n_stops - 1], color0, HB_2_PI, color0); + goto done; + } + } + else + { + float span = angles[n_stops - 1] - angles[0]; + if (fabsf (span) < 1e-6f) + goto done; + + int k = 0; + if (angles[0] >= 0) + { + float ss = angles[0]; + while (ss > 0) + { + if (span > 0) { ss -= span; k--; } + else { ss += span; k++; } + } + } + else + { + float ee = angles[n_stops - 1]; + while (ee < 0) + { + if (span > 0) { ee += span; k++; } + else { ee -= span; k--; } + } + } + + span = fabsf (span); + for (int l = k; l < 1000; l++) + { + for (unsigned i = 1; i < n_stops; i++) + { + float a0_l, a1_l; + hb_color_t col0, col1; + if ((l % 2 != 0) && (extend == HB_PAINT_EXTEND_REFLECT)) + { + a0_l = angles[0] + angles[n_stops - 1] - angles[n_stops - i] + l * span; + a1_l = angles[0] + angles[n_stops - 1] - angles[n_stops - 1 - i] + l * span; + col0 = colors[n_stops - i]; + col1 = colors[n_stops - 1 - i]; + } + else + { + a0_l = angles[i - 1] + l * span; + a1_l = angles[i] + l * span; + col0 = colors[i - 1]; + col1 = colors[i]; + } + + if (a1_l < 0.f) continue; + if (a0_l < 0.f) + { + float f = (0.f - a0_l) / (a1_l - a0_l); + hb_color_t c = hb_pdf_lerp_color (col0, col1, f); + hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, + 0.f, c, a1_l, col1); + } + else if (a1_l >= HB_2_PI) + { + float f = (HB_2_PI - a0_l) / (a1_l - a0_l); + hb_color_t c = hb_pdf_lerp_color (col0, col1, f); + hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, + a0_l, col0, HB_2_PI, c); + goto done; + } + else + hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, + a0_l, col0, a1_l, col1); + } + } + } + +done: + if (dynamic) + { + hb_free (angles); + hb_free (colors); + } + } + +emit: + if (!mesh.length) + return; + /* Build the shading stream object. */ hb_vector_t sh; hb_buf_append_str (&sh, "<< /ShadingType 6 /ColorSpace /DeviceRGB\n"); From 1141e97ef493cbd8df1bc62e32fbdb57600ab6e5 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 22:54:39 -0600 Subject: [PATCH 15/23] [paint] Add hb_paint_push_group_for() API Add a new push_group variant that receives the compositing mode upfront. This lets backends like PDF emit blend-mode state before painting the group content, avoiding the need to buffer and retroactively wrap. The default (nil) implementation calls hb_paint_push_group(), so existing backends are unaffected. Callers updated: - COLR PaintComposite now calls push_group_for() for both groups - Rust/fontations push_layer passes the mode through - hb-raster SVG context accepts mode parameter (default SRC_OVER) All 233 tests pass. Assisted-by: Claude Opus 4.6 (1M context) --- docs/harfbuzz-sections.txt | 3 +++ src/OT/Color/COLR/COLR.hh | 4 ++-- src/hb-paint.cc | 27 ++++++++++++++++++++++ src/hb-paint.h | 43 ++++++++++++++++++++++++++++++++++++ src/hb-paint.hh | 6 +++++ src/hb-raster-svg-context.hh | 4 ++-- src/rust/font.rs | 5 +++-- 7 files changed, 86 insertions(+), 6 deletions(-) diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index 26f8d3af9..30ac24aad 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -291,6 +291,8 @@ hb_paint_funcs_set_sweep_gradient_func hb_paint_composite_mode_t hb_paint_push_group_func_t hb_paint_funcs_set_push_group_func +hb_paint_push_group_for_func_t +hb_paint_funcs_set_push_group_for_func hb_paint_pop_group_func_t hb_paint_funcs_set_pop_group_func hb_paint_custom_palette_color_func_t @@ -310,6 +312,7 @@ hb_paint_linear_gradient hb_paint_radial_gradient hb_paint_sweep_gradient hb_paint_push_group +hb_paint_push_group_for hb_paint_pop_group hb_paint_custom_palette_color diff --git a/src/OT/Color/COLR/COLR.hh b/src/OT/Color/COLR/COLR.hh index c3b5808eb..62d3a3ce1 100644 --- a/src/OT/Color/COLR/COLR.hh +++ b/src/OT/Color/COLR/COLR.hh @@ -1526,9 +1526,9 @@ struct PaintComposite void paint_glyph (hb_paint_context_t *c) const { TRACE_PAINT (this); - c->funcs->push_group (c->data); + c->funcs->push_group_for (c->data, HB_PAINT_COMPOSITE_MODE_SRC_OVER); c->recurse (this+backdrop); - c->funcs->push_group (c->data); + c->funcs->push_group_for (c->data, (hb_paint_composite_mode_t) (int) mode); c->recurse (this+src); c->funcs->pop_group (c->data, (hb_paint_composite_mode_t) (int) mode); c->funcs->pop_group (c->data, HB_PAINT_COMPOSITE_MODE_SRC_OVER); diff --git a/src/hb-paint.cc b/src/hb-paint.cc index ffdd3c3a5..68fc0cade 100644 --- a/src/hb-paint.cc +++ b/src/hb-paint.cc @@ -118,6 +118,14 @@ static void hb_paint_push_group_nil (hb_paint_funcs_t *funcs, void *paint_data, void *user_data) {} +static void +hb_paint_push_group_for_nil (hb_paint_funcs_t *funcs, void *paint_data, + hb_paint_composite_mode_t mode, + void *user_data) +{ + hb_paint_push_group (funcs, paint_data); +} + static void hb_paint_pop_group_nil (hb_paint_funcs_t *funcs, void *paint_data, hb_paint_composite_mode_t mode, @@ -723,6 +731,25 @@ hb_paint_push_group (hb_paint_funcs_t *funcs, void *paint_data) funcs->push_group (paint_data); } +/** + * hb_paint_push_group_for: + * @funcs: paint functions + * @paint_data: associated data passed by the caller + * @mode: the compositing mode that will be used when the group is popped + * + * Perform a "push-group" paint operation, with the compositing + * mode known in advance. By default, this calls + * hb_paint_push_group(). + * + * XSince: REPLACEME + */ +void +hb_paint_push_group_for (hb_paint_funcs_t *funcs, void *paint_data, + hb_paint_composite_mode_t mode) +{ + funcs->push_group_for (paint_data, mode); +} + /** * hb_paint_pop_group: * @funcs: paint functions diff --git a/src/hb-paint.h b/src/hb-paint.h index 787118c46..4fd6c6fbc 100644 --- a/src/hb-paint.h +++ b/src/hb-paint.h @@ -660,6 +660,28 @@ typedef void (*hb_paint_push_group_func_t) (hb_paint_funcs_t *funcs, void *paint_data, void *user_data); +/** + * hb_paint_push_group_for_func_t: + * @funcs: paint functions object + * @paint_data: The data accompanying the paint functions in hb_font_paint_glyph() + * @mode: the compositing mode that will be used when the group is popped + * @user_data: User data pointer passed to hb_paint_funcs_set_push_group_for_func() + * + * A virtual method for the #hb_paint_funcs_t to use + * an intermediate surface for subsequent paint calls, + * with the compositing mode known in advance. + * + * This is like #hb_paint_push_group_func_t, but the + * compositing mode is provided at push time. By default + * this calls #hb_paint_push_group_func_t. + * + * XSince: REPLACEME + */ +typedef void (*hb_paint_push_group_for_func_t) (hb_paint_funcs_t *funcs, + void *paint_data, + hb_paint_composite_mode_t mode, + void *user_data); + /** * hb_paint_pop_group_func_t: * @funcs: paint functions object @@ -916,6 +938,23 @@ hb_paint_funcs_set_push_group_func (hb_paint_funcs_t *funcs, void *user_data, hb_destroy_func_t destroy); +/** + * hb_paint_funcs_set_push_group_for_func: + * @funcs: A paint functions struct + * @func: (closure user_data) (destroy destroy) (scope notified): The push-group-for callback + * @user_data: Data to pass to @func + * @destroy: (nullable): Function to call when @user_data is no longer needed + * + * Sets the push-group-for callback on the paint functions struct. + * + * XSince: REPLACEME + */ +HB_EXTERN void +hb_paint_funcs_set_push_group_for_func (hb_paint_funcs_t *funcs, + hb_paint_push_group_for_func_t func, + void *user_data, + hb_destroy_func_t destroy); + /** * hb_paint_funcs_set_pop_group_func: * @funcs: A paint functions struct @@ -1026,6 +1065,10 @@ hb_paint_sweep_gradient (hb_paint_funcs_t *funcs, void *paint_data, HB_EXTERN void hb_paint_push_group (hb_paint_funcs_t *funcs, void *paint_data); +HB_EXTERN void +hb_paint_push_group_for (hb_paint_funcs_t *funcs, void *paint_data, + hb_paint_composite_mode_t mode); + HB_EXTERN void hb_paint_pop_group (hb_paint_funcs_t *funcs, void *paint_data, hb_paint_composite_mode_t mode); diff --git a/src/hb-paint.hh b/src/hb-paint.hh index bbeeb0319..be4655b7b 100644 --- a/src/hb-paint.hh +++ b/src/hb-paint.hh @@ -43,6 +43,7 @@ HB_PAINT_FUNC_IMPLEMENT (radial_gradient) \ HB_PAINT_FUNC_IMPLEMENT (sweep_gradient) \ HB_PAINT_FUNC_IMPLEMENT (push_group) \ + HB_PAINT_FUNC_IMPLEMENT (push_group_for) \ HB_PAINT_FUNC_IMPLEMENT (pop_group) \ HB_PAINT_FUNC_IMPLEMENT (custom_palette_color) \ /* ^--- Add new callbacks here */ @@ -146,6 +147,11 @@ struct hb_paint_funcs_t void push_group (void *paint_data) { func.push_group (this, paint_data, !user_data ? nullptr : user_data->push_group); } + void push_group_for (void *paint_data, + hb_paint_composite_mode_t mode) + { func.push_group_for (this, paint_data, + mode, + !user_data ? nullptr : user_data->push_group_for); } void pop_group (void *paint_data, hb_paint_composite_mode_t mode) { func.pop_group (this, paint_data, diff --git a/src/hb-raster-svg-context.hh b/src/hb-raster-svg-context.hh index 5589f1d4c..6ed02a0b2 100644 --- a/src/hb-raster-svg-context.hh +++ b/src/hb-raster-svg-context.hh @@ -85,10 +85,10 @@ struct hb_svg_render_context_t { hb_paint_pop_transform (pfuncs, paint); } - bool push_group () + bool push_group (hb_paint_composite_mode_t mode = HB_PAINT_COMPOSITE_MODE_SRC_OVER) { unsigned depth = paint->surface_stack.length; - hb_paint_push_group (pfuncs, paint); + hb_paint_push_group_for (pfuncs, paint, mode); return paint->surface_stack.length > depth; } void pop_group (hb_paint_composite_mode_t mode) diff --git a/src/rust/font.rs b/src/rust/font.rs index 08f44e145..8e6b526b0 100644 --- a/src/rust/font.rs +++ b/src/rust/font.rs @@ -879,9 +879,10 @@ impl ColorPainter for HbColorPainter<'_> { } } } - fn push_layer(&mut self, _mode: CompositeMode) { + fn push_layer(&mut self, mode: CompositeMode) { + let mode = mode as hb_paint_composite_mode_t; unsafe { - hb_paint_push_group(self.paint_funcs, self.paint_data); + hb_paint_push_group_for(self.paint_funcs, self.paint_data, mode); } } fn pop_layer_with_mode(&mut self, mode: CompositeMode) { From 184b5bb7c3cfc035febbf9ea0a4faf1ac9430e74 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 22:59:13 -0600 Subject: [PATCH 16/23] [vector] Implement PDF blend modes via push_group_for Use the new push_group_for API to emit PDF ExtGState with /BM (blend mode) when the compositing mode has a PDF 1.4 equivalent. Maps 15 blend modes: Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity. Porter-Duff modes (Clear, Src, Dst, etc.) have no direct PDF /BM equivalent and fall back to the default (Normal/SrcOver). Assisted-by: Claude Opus 4.6 (1M context) --- src/hb-vector-pdf-paint.cc | 83 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/src/hb-vector-pdf-paint.cc b/src/hb-vector-pdf-paint.cc index 3c502907c..632de8fad 100644 --- a/src/hb-vector-pdf-paint.cc +++ b/src/hb-vector-pdf-paint.cc @@ -92,6 +92,24 @@ struct hb_pdf_resources_t return idx; } + /* Add ExtGState for blend mode, return resource name index. */ + unsigned add_extgstate_blend (const char *bm) + { + unsigned idx = extgstate_count++; + hb_vector_t obj; + hb_buf_append_str (&obj, "<< /Type /ExtGState /BM /"); + hb_buf_append_str (&obj, bm); + hb_buf_append_str (&obj, " >>"); + unsigned obj_id = add_object (std::move (obj)); + + hb_buf_append_str (&extgstate_dict, "/GS"); + hb_buf_append_unsigned (&extgstate_dict, idx); + hb_buf_append_c (&extgstate_dict, ' '); + hb_buf_append_unsigned (&extgstate_dict, obj_id); + hb_buf_append_str (&extgstate_dict, " 0 R "); + return idx; + } + /* Add a shading + function for a gradient, return resource name index. */ unsigned add_shading (hb_vector_t &&shading_data) { @@ -933,6 +951,43 @@ emit: hb_buf_append_str (&body, " sh\n"); } +static const char * +hb_pdf_blend_mode_name (hb_paint_composite_mode_t mode) +{ + switch (mode) + { + case HB_PAINT_COMPOSITE_MODE_MULTIPLY: return "Multiply"; + case HB_PAINT_COMPOSITE_MODE_SCREEN: return "Screen"; + case HB_PAINT_COMPOSITE_MODE_OVERLAY: return "Overlay"; + case HB_PAINT_COMPOSITE_MODE_DARKEN: return "Darken"; + case HB_PAINT_COMPOSITE_MODE_LIGHTEN: return "Lighten"; + case HB_PAINT_COMPOSITE_MODE_COLOR_DODGE: return "ColorDodge"; + case HB_PAINT_COMPOSITE_MODE_COLOR_BURN: return "ColorBurn"; + case HB_PAINT_COMPOSITE_MODE_HARD_LIGHT: return "HardLight"; + case HB_PAINT_COMPOSITE_MODE_SOFT_LIGHT: return "SoftLight"; + case HB_PAINT_COMPOSITE_MODE_DIFFERENCE: return "Difference"; + case HB_PAINT_COMPOSITE_MODE_EXCLUSION: return "Exclusion"; + case HB_PAINT_COMPOSITE_MODE_HSL_HUE: return "Hue"; + case HB_PAINT_COMPOSITE_MODE_HSL_SATURATION: return "Saturation"; + case HB_PAINT_COMPOSITE_MODE_HSL_COLOR: return "Color"; + case HB_PAINT_COMPOSITE_MODE_HSL_LUMINOSITY: return "Luminosity"; + case HB_PAINT_COMPOSITE_MODE_CLEAR: + case HB_PAINT_COMPOSITE_MODE_SRC: + case HB_PAINT_COMPOSITE_MODE_DEST: + case HB_PAINT_COMPOSITE_MODE_SRC_OVER: + case HB_PAINT_COMPOSITE_MODE_DEST_OVER: + case HB_PAINT_COMPOSITE_MODE_SRC_IN: + case HB_PAINT_COMPOSITE_MODE_DEST_IN: + case HB_PAINT_COMPOSITE_MODE_SRC_OUT: + case HB_PAINT_COMPOSITE_MODE_DEST_OUT: + case HB_PAINT_COMPOSITE_MODE_SRC_ATOP: + case HB_PAINT_COMPOSITE_MODE_DEST_ATOP: + case HB_PAINT_COMPOSITE_MODE_XOR: + case HB_PAINT_COMPOSITE_MODE_PLUS: + default: return nullptr; /* Normal / no PDF equivalent */ + } +} + static void hb_pdf_paint_push_group (hb_paint_funcs_t *, void *paint_data, @@ -944,6 +999,33 @@ hb_pdf_paint_push_group (hb_paint_funcs_t *, hb_buf_append_str (&paint->current_body (), "q\n"); } +static void +hb_pdf_paint_push_group_for (hb_paint_funcs_t *, + void *paint_data, + hb_paint_composite_mode_t mode, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + + auto &body = paint->current_body (); + hb_buf_append_str (&body, "q\n"); + + const char *bm = hb_pdf_blend_mode_name (mode); + if (bm) + { + auto *res = hb_pdf_get_resources (paint); + if (likely (res)) + { + unsigned gs_idx = res->add_extgstate_blend (bm); + hb_buf_append_str (&body, "/GS"); + hb_buf_append_unsigned (&body, gs_idx); + hb_buf_append_str (&body, " gs\n"); + } + } +} + static void hb_pdf_paint_pop_group (hb_paint_funcs_t *, void *paint_data, @@ -978,6 +1060,7 @@ static struct hb_pdf_paint_funcs_lazy_loader_t hb_paint_funcs_set_radial_gradient_func (funcs, (hb_paint_radial_gradient_func_t) hb_pdf_paint_radial_gradient, nullptr, nullptr); hb_paint_funcs_set_sweep_gradient_func (funcs, (hb_paint_sweep_gradient_func_t) hb_pdf_paint_sweep_gradient, nullptr, nullptr); hb_paint_funcs_set_push_group_func (funcs, (hb_paint_push_group_func_t) hb_pdf_paint_push_group, nullptr, nullptr); + hb_paint_funcs_set_push_group_for_func (funcs, (hb_paint_push_group_for_func_t) hb_pdf_paint_push_group_for, nullptr, nullptr); hb_paint_funcs_set_pop_group_func (funcs, (hb_paint_pop_group_func_t) hb_pdf_paint_pop_group, nullptr, nullptr); hb_paint_funcs_make_immutable (funcs); hb_atexit (free_static_pdf_paint_funcs); From d80e1da9cdcc8aebc619f15a1f3024146eeb46ff Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 23:29:21 -0600 Subject: [PATCH 17/23] [vector] Implement PDF PNG image embedding with transparency Add PNG image support to the PDF paint backend. Parses PNG chunks to extract IHDR metadata and IDAT compressed data, then embeds directly as PDF XObject Image with /FlateDecode and /Predictor 15 (PNG predictor), avoiding any re-encoding of pixel data. Supports: - Grayscale, RGB, Gray+Alpha, RGBA (8-bit) - Indexed color (palette PNG) via /Indexed colorspace with PLTE - Transparency for indexed PNG via SMask: decompresses IDAT, un-filters PNG rows, maps palette indices to tRNS alpha values, and emits a separate DeviceGray soft mask image Tested with NotoColorEmoji.ttf (CBDT, indexed PNG with partial alpha) and sbix.ttf (SBIX, RGBA PNG). Assisted-by: Claude Opus 4.6 (1M context) --- src/hb-vector-pdf-paint.cc | 364 ++++++++++++++++++++++++++++++++++++- 1 file changed, 356 insertions(+), 8 deletions(-) diff --git a/src/hb-vector-pdf-paint.cc b/src/hb-vector-pdf-paint.cc index 632de8fad..2fcb5a9e6 100644 --- a/src/hb-vector-pdf-paint.cc +++ b/src/hb-vector-pdf-paint.cc @@ -32,6 +32,10 @@ #include #include +#ifdef HAVE_ZLIB +#include +#endif + /* PDF paint backend for COLRv0/v1 color font rendering. * * Supports: @@ -55,13 +59,21 @@ struct hb_pdf_obj_t /* Collects extra PDF objects (shadings, functions, ExtGState) * during painting. Referenced from the content stream by name * (e.g. /SH0, /GS0) and emitted at render time. */ +static bool +hb_pdf_build_indexed_smask (hb_vector_t *out, + const char *idat_data, unsigned idat_len, + unsigned width, unsigned height, + const uint8_t *trns, unsigned trns_len); + struct hb_pdf_resources_t { hb_vector_t objects; /* extra objects, starting at id 5 */ hb_vector_t extgstate_dict; /* /GS0 5 0 R /GS1 6 0 R ... */ hb_vector_t shading_dict; /* /SH0 7 0 R ... */ + hb_vector_t xobject_dict; /* /Im0 8 0 R ... */ unsigned extgstate_count = 0; unsigned shading_count = 0; + unsigned xobject_count = 0; unsigned add_object (hb_vector_t &&obj_data) { @@ -123,6 +135,104 @@ struct hb_pdf_resources_t hb_buf_append_str (&shading_dict, " 0 R "); return idx; } + + /* Add an XObject image, return resource name index. + * idat_data/idat_len is the concatenated PNG IDAT payload (zlib data). + * colors is 1 (gray), 3 (RGB), or 4 (RGBA); for indexed, pass colors=1. + * plte/plte_len is the PLTE chunk data for indexed images (may be null). */ + unsigned add_xobject_png_image (const char *idat_data, unsigned idat_len, + unsigned width, unsigned height, + unsigned colors, bool has_alpha, + const uint8_t *plte = nullptr, + unsigned plte_len = 0, + const uint8_t *trns = nullptr, + unsigned trns_len = 0) + { + unsigned idx = xobject_count++; + hb_vector_t obj; + hb_buf_append_str (&obj, "<< /Type /XObject /Subtype /Image\n"); + hb_buf_append_str (&obj, "/Width "); + hb_buf_append_unsigned (&obj, width); + hb_buf_append_str (&obj, " /Height "); + hb_buf_append_unsigned (&obj, height); + hb_buf_append_str (&obj, "\n/BitsPerComponent 8\n"); + + if (plte && plte_len >= 3) + { + /* Indexed color: /ColorSpace [/Indexed /DeviceRGB N ] */ + unsigned n_entries = plte_len / 3; + hb_buf_append_str (&obj, "/ColorSpace [/Indexed /DeviceRGB "); + hb_buf_append_unsigned (&obj, n_entries - 1); + hb_buf_append_str (&obj, " <"); + for (unsigned i = 0; i < n_entries * 3; i++) + { + hb_buf_append_c (&obj, "0123456789ABCDEF"[plte[i] >> 4]); + hb_buf_append_c (&obj, "0123456789ABCDEF"[plte[i] & 0xF]); + } + hb_buf_append_str (&obj, ">]\n"); + } + else + { + hb_buf_append_str (&obj, "/ColorSpace "); + unsigned color_channels = has_alpha ? colors - 1 : colors; + hb_buf_append_str (&obj, color_channels == 1 ? "/DeviceGray" : "/DeviceRGB"); + hb_buf_append_c (&obj, '\n'); + } + + /* Build SMask for indexed images with tRNS transparency. */ + unsigned smask_id = 0; + if (plte && trns && trns_len) + { + hb_vector_t smask_stream; + if (hb_pdf_build_indexed_smask (&smask_stream, idat_data, idat_len, + width, height, trns, trns_len)) + { + hb_vector_t smask_obj; + hb_buf_append_str (&smask_obj, "<< /Type /XObject /Subtype /Image\n"); + hb_buf_append_str (&smask_obj, "/Width "); + hb_buf_append_unsigned (&smask_obj, width); + hb_buf_append_str (&smask_obj, " /Height "); + hb_buf_append_unsigned (&smask_obj, height); + hb_buf_append_str (&smask_obj, "\n/ColorSpace /DeviceGray /BitsPerComponent 8\n"); + hb_buf_append_str (&smask_obj, "/Length "); + hb_buf_append_unsigned (&smask_obj, smask_stream.length); + hb_buf_append_str (&smask_obj, " >>\nstream\n"); + hb_buf_append_len (&smask_obj, smask_stream.arrayZ, smask_stream.length); + hb_buf_append_str (&smask_obj, "\nendstream"); + smask_id = add_object (std::move (smask_obj)); + } + } + + if (smask_id) + { + hb_buf_append_str (&obj, "/SMask "); + hb_buf_append_unsigned (&obj, smask_id); + hb_buf_append_str (&obj, " 0 R\n"); + } + + hb_buf_append_str (&obj, "/Filter /FlateDecode\n"); + hb_buf_append_str (&obj, "/DecodeParms << /Predictor 15 /Colors "); + hb_buf_append_unsigned (&obj, colors); + hb_buf_append_str (&obj, " /BitsPerComponent 8 /Columns "); + hb_buf_append_unsigned (&obj, width); + hb_buf_append_str (&obj, " >>\n"); + hb_buf_append_str (&obj, "/Length "); + hb_buf_append_unsigned (&obj, idat_len); + hb_buf_append_str (&obj, " >>\nstream\n"); + hb_buf_append_len (&obj, idat_data, idat_len); + hb_buf_append_str (&obj, "\nendstream"); + + (void) has_alpha; + + unsigned obj_id = add_object (std::move (obj)); + + hb_buf_append_str (&xobject_dict, "/Im"); + hb_buf_append_unsigned (&xobject_dict, idx); + hb_buf_append_c (&xobject_dict, ' '); + hb_buf_append_unsigned (&xobject_dict, obj_id); + hb_buf_append_str (&xobject_dict, " 0 R "); + return idx; + } }; /* Store resources pointer in the paint struct's defs buffer @@ -354,17 +464,249 @@ hb_pdf_paint_color (hb_paint_funcs_t *, hb_pdf_paint_solid_color (paint, c); } +/* Build an uncompressed alpha mask from indexed PNG IDAT data + tRNS. + * Decompresses IDAT, un-filters PNG rows, maps palette indices to alpha. */ +static bool +hb_pdf_build_indexed_smask (hb_vector_t *out, + const char *idat_data, unsigned idat_len, + unsigned width, unsigned height, + const uint8_t *trns, unsigned trns_len) +{ +#ifndef HAVE_ZLIB + (void) out; (void) idat_data; (void) idat_len; + (void) width; (void) height; (void) trns; (void) trns_len; + return false; +#else + /* Decompress IDAT (zlib). */ + unsigned raw_len = (width + 1) * height; /* 1 filter byte per row + width bytes */ + uint8_t *raw = (uint8_t *) hb_malloc (raw_len); + if (!raw) return false; + + z_stream stream = {}; + stream.next_in = (Bytef *) idat_data; + stream.avail_in = idat_len; + stream.next_out = (Bytef *) raw; + stream.avail_out = raw_len; + + if (inflateInit (&stream) != Z_OK) + { + hb_free (raw); + return false; + } + int status = inflate (&stream, Z_FINISH); + inflateEnd (&stream); + if (status != Z_STREAM_END) + { + hb_free (raw); + return false; + } + + /* Un-filter and map to alpha. */ + if (!out->resize (width * height)) + { + hb_free (raw); + return false; + } + + uint8_t *unfiltered = (uint8_t *) hb_malloc (width); + if (!unfiltered) + { + hb_free (raw); + return false; + } + uint8_t *prev_unfiltered = (uint8_t *) hb_calloc (width, 1); + if (!prev_unfiltered) + { + hb_free (unfiltered); + hb_free (raw); + return false; + } + + for (unsigned y = 0; y < height; y++) + { + uint8_t *row = raw + y * (width + 1); + uint8_t filter = row[0]; + uint8_t *pixels = row + 1; + + for (unsigned x = 0; x < width; x++) + { + uint8_t a = (x > 0) ? unfiltered[x - 1] : 0; + uint8_t b = prev_unfiltered[x]; + uint8_t c = (x > 0) ? prev_unfiltered[x - 1] : 0; + uint8_t val = pixels[x]; + + switch (filter) + { + case 0: unfiltered[x] = val; break; /* None */ + case 1: unfiltered[x] = val + a; break; /* Sub */ + case 2: unfiltered[x] = val + b; break; /* Up */ + case 3: unfiltered[x] = val + (uint8_t) (((unsigned) a + b) / 2); break; /* Average */ + case 4: /* Paeth */ + { + int p = (int) a + (int) b - (int) c; + int pa = abs (p - (int) a); + int pb = abs (p - (int) b); + int pc = abs (p - (int) c); + uint8_t pr = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c; + unfiltered[x] = val + pr; + break; + } + default: unfiltered[x] = val; break; + } + + /* Map palette index to alpha. */ + uint8_t idx = unfiltered[x]; + out->arrayZ[y * width + x] = (idx < trns_len) ? trns[idx] : 0xFF; + } + + /* Swap buffers. */ + uint8_t *tmp = prev_unfiltered; + prev_unfiltered = unfiltered; + unfiltered = tmp; + } + + hb_free (unfiltered); + hb_free (prev_unfiltered); + hb_free (raw); + return true; +#endif +} + +/* Read a big-endian uint32 from a byte pointer. */ +static inline uint32_t +hb_pdf_png_u32 (const uint8_t *p) +{ + return ((uint32_t) p[0] << 24) | ((uint32_t) p[1] << 16) | + ((uint32_t) p[2] << 8) | (uint32_t) p[3]; +} + static hb_bool_t hb_pdf_paint_image (hb_paint_funcs_t *, - void *, - hb_blob_t *, - unsigned, unsigned, - hb_tag_t, - float, - hb_glyph_extents_t *, + void *paint_data, + hb_blob_t *image, + unsigned width, + unsigned height, + hb_tag_t format, + float slant HB_UNUSED, + hb_glyph_extents_t *extents, void *) { - return false; + if (format != HB_TAG ('p','n','g',' ')) + return false; + if (!extents || !width || !height) + return false; + + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return false; + auto *res = hb_pdf_get_resources (paint); + if (unlikely (!res)) + return false; + + unsigned len = 0; + const uint8_t *data = (const uint8_t *) hb_blob_get_data (image, &len); + if (!data || len < 8) + return false; + + /* Verify PNG signature. */ + static const uint8_t png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10}; + if (hb_memcmp (data, png_sig, 8) != 0) + return false; + + /* Parse PNG chunks: extract IHDR and concatenate IDAT payloads. */ + unsigned png_w = 0, png_h = 0; + unsigned colors = 3; /* default RGB */ + uint8_t color_type = 2; + bool has_alpha = false; + const uint8_t *plte_data = nullptr; + unsigned plte_len = 0; + const uint8_t *trns_data = nullptr; + unsigned trns_len = 0; + hb_vector_t idat; + + unsigned pos = 8; + while (pos + 12 <= len) + { + uint32_t chunk_len = hb_pdf_png_u32 (data + pos); + uint32_t chunk_type = hb_pdf_png_u32 (data + pos + 4); + if (pos + 12 + chunk_len > len) + break; + const uint8_t *chunk_data = data + pos + 8; + + if (chunk_type == 0x49484452u) /* IHDR */ + { + if (chunk_len < 13) return false; + png_w = hb_pdf_png_u32 (chunk_data); + png_h = hb_pdf_png_u32 (chunk_data + 4); + uint8_t bit_depth = chunk_data[8]; + color_type = chunk_data[9]; + if (bit_depth != 8) return false; /* only 8-bit supported */ + + switch (color_type) + { + case 0: colors = 1; has_alpha = false; break; /* Grayscale */ + case 2: colors = 3; has_alpha = false; break; /* RGB */ + case 3: colors = 1; has_alpha = false; break; /* Indexed */ + case 4: colors = 2; has_alpha = true; break; /* Gray+Alpha */ + case 6: colors = 4; has_alpha = true; break; /* RGBA */ + default: return false; + } + } + else if (chunk_type == 0x504C5445u) /* PLTE */ + { + plte_data = chunk_data; + plte_len = chunk_len; + } + else if (chunk_type == 0x74524E53u) /* tRNS */ + { + trns_data = chunk_data; + trns_len = chunk_len; + } + else if (chunk_type == 0x49444154u) /* IDAT */ + { + hb_buf_append_len (&idat, (const char *) chunk_data, chunk_len); + } + else if (chunk_type == 0x49454E44u) /* IEND */ + break; + + pos += 12 + chunk_len; + } + + if (!png_w || !png_h || !idat.length) + return false; + + unsigned im_idx = res->add_xobject_png_image (idat.arrayZ, idat.length, + png_w, png_h, + colors, has_alpha, + plte_data, plte_len, + trns_data, trns_len); + + /* Emit: save state, set CTM to map image (0,0)-(1,1) to extents, paint. */ + auto &body = paint->current_body (); + hb_buf_append_str (&body, "q\n"); + + /* Image space: (0,0) at bottom-left, (1,1) at top-right. + * We need to map to extents (x_bearing, y_bearing+height) .. (x_bearing+width, y_bearing). + * Since font coords are Y-up like PDF, y_bearing is the top. */ + float ix = (float) extents->x_bearing; + float iy = (float) extents->y_bearing + (float) extents->height; + float iw = (float) extents->width; + float ih = (float) -extents->height; /* negative because image Y goes up but height is negative in extents */ + + hb_buf_append_num (&body, iw, paint->precision); + hb_buf_append_str (&body, " 0 0 "); + hb_buf_append_num (&body, ih, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, ix, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, iy, paint->precision); + hb_buf_append_str (&body, " cm\n"); + + hb_buf_append_str (&body, "/Im"); + hb_buf_append_unsigned (&body, im_idx); + hb_buf_append_str (&body, " Do\nQ\n"); + + return true; } /* ---- Gradient helpers ---- */ @@ -1135,7 +1477,7 @@ hb_vector_pdf_paint_render (hb_vector_paint_t *paint) /* Resources. */ bool has_resources = res && - (res->extgstate_dict.length || res->shading_dict.length); + (res->extgstate_dict.length || res->shading_dict.length || res->xobject_dict.length); if (has_resources) { hb_buf_append_str (&out, "\n/Resources <<"); @@ -1151,6 +1493,12 @@ hb_vector_pdf_paint_render (hb_vector_paint_t *paint) hb_buf_append_len (&out, res->shading_dict.arrayZ, res->shading_dict.length); hb_buf_append_str (&out, ">>"); } + if (res->xobject_dict.length) + { + hb_buf_append_str (&out, " /XObject << "); + hb_buf_append_len (&out, res->xobject_dict.arrayZ, res->xobject_dict.length); + hb_buf_append_str (&out, ">>"); + } hb_buf_append_str (&out, " >>"); } From 0967927b888d0c2958835a9e7a4c325ec283ffc9 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Apr 2026 23:45:37 -0600 Subject: [PATCH 18/23] [vector] Extract shared sweep gradient tiling logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move the sweep gradient color stop tiling (pad/repeat/reflect across 0-2π) into a shared template hb_sweep_gradient_tiles() in hb-vector-svg-utils.hh. Both SVG and PDF backends now call it with their own patch emitter callback. Also adds hb_color_lerp() to the shared header (fixing an R/B channel swap bug in the PDF version's interpolation). Net reduction of ~170 lines of duplicated tiling code. Assisted-by: Claude Opus 4.6 (1M context) --- src/hb-vector-pdf-paint.cc | 201 ++---------------------------------- src/hb-vector-svg-paint.cc | 205 ++----------------------------------- src/hb-vector-svg-utils.hh | 196 +++++++++++++++++++++++++++++++++++ 3 files changed, 214 insertions(+), 388 deletions(-) diff --git a/src/hb-vector-pdf-paint.cc b/src/hb-vector-pdf-paint.cc index 2fcb5a9e6..dd94f3498 100644 --- a/src/hb-vector-pdf-paint.cc +++ b/src/hb-vector-pdf-paint.cc @@ -911,16 +911,6 @@ hb_pdf_paint_radial_gradient (hb_paint_funcs_t *, hb_buf_append_str (&body, " sh\n"); } -static hb_color_t -hb_pdf_lerp_color (hb_color_t c0, hb_color_t c1, float t) -{ - auto lerp = [&] (unsigned shift) -> unsigned { - unsigned v0 = (c0 >> shift) & 0xFF; - unsigned v1 = (c1 >> shift) & 0xFF; - return (unsigned) (v0 + t * ((float) v1 - (float) v0) + 0.5f); - }; - return HB_COLOR (lerp (16), lerp (8), lerp (0), lerp (24)); -} /* Encode a 16-bit big-endian unsigned value into buf. */ static void @@ -983,8 +973,8 @@ hb_pdf_add_sweep_patch (hb_vector_t *mesh, float k1 = (float) (s + 1) / num_splits; float sa0 = a0 + k0 * (a1 - a0); float sa1 = a0 + k1 * (a1 - a0); - hb_color_t sc0 = hb_pdf_lerp_color (c0_in, c1_in, k0); - hb_color_t sc1 = hb_pdf_lerp_color (c0_in, c1_in, k1); + hb_color_t sc0 = hb_color_lerp (c0_in, c1_in, k0); + hb_color_t sc1 = hb_color_lerp (c0_in, c1_in, k1); float da = sa1 - sa0; float kappa = (4.f / 3.f) * tanf (da / 4.f); @@ -1080,189 +1070,12 @@ hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, hb_vector_t mesh; mesh.alloc (256); - /* Same tiling logic as hb_svg_add_sweep_gradient_patches. */ + hb_sweep_gradient_tiles (stops, n_stops, extend, + start_angle, end_angle, + [&] (float a0, hb_color_t c0, float a1, hb_color_t c1) + { hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, + a0, c0, a1, c1); }); - if (start_angle == end_angle) - { - if (extend == HB_PAINT_EXTEND_PAD) - { - if (start_angle > 0.f) - hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, - 0.f, stops[0].color, start_angle, stops[0].color); - if (end_angle < HB_2_PI) - hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, - end_angle, stops[n_stops - 1].color, HB_2_PI, stops[n_stops - 1].color); - } - goto emit; - } - - if (end_angle < start_angle) - { - float tmp = start_angle; start_angle = end_angle; end_angle = tmp; - for (unsigned i = 0; i < n_stops - 1 - i; i++) - { - hb_color_stop_t t = stops[i]; - stops[i] = stops[n_stops - 1 - i]; - stops[n_stops - 1 - i] = t; - } - for (unsigned i = 0; i < n_stops; i++) - stops[i].offset = 1.f - stops[i].offset; - } - - { - /* Map stop offsets to angles. */ - float angles_buf[16]; - hb_color_t colors_buf[16]; - float *angles = angles_buf; - hb_color_t *colors = colors_buf; - bool dynamic = false; - - if (n_stops > 16) - { - angles = (float *) hb_malloc (sizeof (float) * n_stops); - colors = (hb_color_t *) hb_malloc (sizeof (hb_color_t) * n_stops); - if (!angles || !colors) - { - hb_free (angles); - hb_free (colors); - goto emit; - } - dynamic = true; - } - - for (unsigned i = 0; i < n_stops; i++) - { - angles[i] = start_angle + stops[i].offset * (end_angle - start_angle); - colors[i] = stops[i].color; - } - - if (extend == HB_PAINT_EXTEND_PAD) - { - unsigned pos; - hb_color_t color0 = colors[0]; - for (pos = 0; pos < n_stops; pos++) - { - if (angles[pos] >= 0) - { - if (pos > 0) - { - float f = (0.f - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); - color0 = hb_pdf_lerp_color (colors[pos - 1], colors[pos], f); - } - break; - } - } - if (pos == n_stops) - { - color0 = colors[n_stops - 1]; - hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, - 0.f, color0, HB_2_PI, color0); - goto done; - } - hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, - 0.f, color0, angles[pos], colors[pos]); - for (pos++; pos < n_stops; pos++) - { - if (angles[pos] <= HB_2_PI) - hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, - angles[pos - 1], colors[pos - 1], angles[pos], colors[pos]); - else - { - float f = (HB_2_PI - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); - hb_color_t color1 = hb_pdf_lerp_color (colors[pos - 1], colors[pos], f); - hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, - angles[pos - 1], colors[pos - 1], HB_2_PI, color1); - break; - } - } - if (pos == n_stops) - { - color0 = colors[n_stops - 1]; - hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, - angles[n_stops - 1], color0, HB_2_PI, color0); - goto done; - } - } - else - { - float span = angles[n_stops - 1] - angles[0]; - if (fabsf (span) < 1e-6f) - goto done; - - int k = 0; - if (angles[0] >= 0) - { - float ss = angles[0]; - while (ss > 0) - { - if (span > 0) { ss -= span; k--; } - else { ss += span; k++; } - } - } - else - { - float ee = angles[n_stops - 1]; - while (ee < 0) - { - if (span > 0) { ee += span; k++; } - else { ee -= span; k--; } - } - } - - span = fabsf (span); - for (int l = k; l < 1000; l++) - { - for (unsigned i = 1; i < n_stops; i++) - { - float a0_l, a1_l; - hb_color_t col0, col1; - if ((l % 2 != 0) && (extend == HB_PAINT_EXTEND_REFLECT)) - { - a0_l = angles[0] + angles[n_stops - 1] - angles[n_stops - i] + l * span; - a1_l = angles[0] + angles[n_stops - 1] - angles[n_stops - 1 - i] + l * span; - col0 = colors[n_stops - i]; - col1 = colors[n_stops - 1 - i]; - } - else - { - a0_l = angles[i - 1] + l * span; - a1_l = angles[i] + l * span; - col0 = colors[i - 1]; - col1 = colors[i]; - } - - if (a1_l < 0.f) continue; - if (a0_l < 0.f) - { - float f = (0.f - a0_l) / (a1_l - a0_l); - hb_color_t c = hb_pdf_lerp_color (col0, col1, f); - hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, - 0.f, c, a1_l, col1); - } - else if (a1_l >= HB_2_PI) - { - float f = (HB_2_PI - a0_l) / (a1_l - a0_l); - hb_color_t c = hb_pdf_lerp_color (col0, col1, f); - hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, - a0_l, col0, HB_2_PI, c); - goto done; - } - else - hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, - a0_l, col0, a1_l, col1); - } - } - } - -done: - if (dynamic) - { - hb_free (angles); - hb_free (colors); - } - } - -emit: if (!mesh.length) return; diff --git a/src/hb-vector-svg-paint.cc b/src/hb-vector-svg-paint.cc index c002893ea..b5ebe8aca 100644 --- a/src/hb-vector-svg-paint.cc +++ b/src/hb-vector-svg-paint.cc @@ -343,191 +343,6 @@ hb_svg_add_sweep_patch (hb_vector_t *body, } } -static void -hb_svg_add_sweep_gradient_patches (hb_vector_t *body, - unsigned precision, - hb_color_stop_t *stops, - unsigned n_stops, - hb_paint_extend_t extend, - float cx, float cy, float radius, - float start_angle, float end_angle) -{ - if (!n_stops) return; - - hb_svg_rgba_t colors_buf[16]; - float angles_buf[16]; - hb_svg_rgba_t *colors = colors_buf; - float *angles = angles_buf; - bool dynamic = false; - - if (start_angle == end_angle) - { - if (extend == HB_PAINT_EXTEND_PAD) - { - if (start_angle > 0.f) - { - hb_svg_rgba_t c = hb_svg_rgba_from_hb_color (stops[0].color); - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, 0.f, c, start_angle, c); - } - if (end_angle < HB_2_PI) - { - hb_svg_rgba_t c = hb_svg_rgba_from_hb_color (stops[n_stops - 1].color); - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, end_angle, c, HB_2_PI, c); - } - } - return; - } - - if (end_angle < start_angle) - { - float tmp = start_angle; start_angle = end_angle; end_angle = tmp; - for (unsigned i = 0; i < n_stops - 1 - i; i++) - { - hb_color_stop_t t = stops[i]; - stops[i] = stops[n_stops - 1 - i]; - stops[n_stops - 1 - i] = t; - } - for (unsigned i = 0; i < n_stops; i++) - stops[i].offset = 1.f - stops[i].offset; - } - - if (n_stops > 16) - { - angles = (float *) hb_malloc (sizeof (float) * n_stops); - colors = (hb_svg_rgba_t *) hb_malloc (sizeof (hb_svg_rgba_t) * n_stops); - if (!angles || !colors) - { - hb_free (angles); - hb_free (colors); - return; - } - dynamic = true; - } - - for (unsigned i = 0; i < n_stops; i++) - { - angles[i] = start_angle + stops[i].offset * (end_angle - start_angle); - colors[i] = hb_svg_rgba_from_hb_color (stops[i].color); - } - - if (extend == HB_PAINT_EXTEND_PAD) - { - unsigned pos; - hb_svg_rgba_t color0 = colors[0]; - for (pos = 0; pos < n_stops; pos++) - { - if (angles[pos] >= 0) - { - if (pos > 0) - { - float f = (0.f - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); - color0 = hb_svg_lerp_rgba (colors[pos - 1], colors[pos], f); - } - break; - } - } - if (pos == n_stops) - { - color0 = colors[n_stops - 1]; - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, 0.f, color0, HB_2_PI, color0); - goto done; - } - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, 0.f, color0, angles[pos], colors[pos]); - for (pos++; pos < n_stops; pos++) - { - if (angles[pos] <= HB_2_PI) - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, angles[pos - 1], colors[pos - 1], angles[pos], colors[pos]); - else - { - float f = (HB_2_PI - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); - hb_svg_rgba_t color1 = hb_svg_lerp_rgba (colors[pos - 1], colors[pos], f); - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, angles[pos - 1], colors[pos - 1], HB_2_PI, color1); - break; - } - } - if (pos == n_stops) - { - color0 = colors[n_stops - 1]; - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, angles[n_stops - 1], color0, HB_2_PI, color0); - goto done; - } - } - else - { - float span = angles[n_stops - 1] - angles[0]; - if (fabsf (span) < 1e-6f) - goto done; - - int k = 0; - if (angles[0] >= 0) - { - float ss = angles[0]; - while (ss > 0) - { - if (span > 0) { ss -= span; k--; } - else { ss += span; k++; } - } - } - else - { - float ee = angles[n_stops - 1]; - while (ee < 0) - { - if (span > 0) { ee += span; k++; } - else { ee -= span; k--; } - } - } - - span = fabsf (span); - for (int l = k; l < 1000; l++) - { - for (unsigned i = 1; i < n_stops; i++) - { - float a0_l, a1_l; - const hb_svg_rgba_t *col0, *col1; - if ((l % 2 != 0) && (extend == HB_PAINT_EXTEND_REFLECT)) - { - a0_l = angles[0] + angles[n_stops - 1] - angles[n_stops - i] + l * span; - a1_l = angles[0] + angles[n_stops - 1] - angles[n_stops - 1 - i] + l * span; - col0 = &colors[n_stops - i]; - col1 = &colors[n_stops - 1 - i]; - } - else - { - a0_l = angles[i - 1] + l * span; - a1_l = angles[i] + l * span; - col0 = &colors[i - 1]; - col1 = &colors[i]; - } - - if (a1_l < 0.f) continue; - if (a0_l < 0.f) - { - float f = (0.f - a0_l) / (a1_l - a0_l); - hb_svg_rgba_t c = hb_svg_lerp_rgba (*col0, *col1, f); - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, 0.f, c, a1_l, *col1); - } - else if (a1_l >= HB_2_PI) - { - float f = (HB_2_PI - a0_l) / (a1_l - a0_l); - hb_svg_rgba_t c = hb_svg_lerp_rgba (*col0, *col1, f); - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, a0_l, *col0, HB_2_PI, c); - goto done; - } - else - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, a0_l, *col0, a1_l, *col1); - } - } - } - -done: - if (dynamic) - { - hb_free (angles); - hb_free (colors); - } -} - static void hb_vector_paint_push_transform (hb_paint_funcs_t *, void *, float, float, float, float, float, float, @@ -974,15 +789,17 @@ hb_vector_paint_sweep_gradient (hb_paint_funcs_t *, return; qsort (stops.arrayZ, stops.length, sizeof (hb_color_stop_t), hb_svg_color_stop_cmp); - hb_svg_add_sweep_gradient_patches (&paint->current_body (), - paint->precision, - stops.arrayZ, - stops.length, - hb_color_line_get_extend (color_line), - cx, cy, - 32767.f, - start_angle, - end_angle); + + auto *body = &paint->current_body (); + unsigned precision = paint->precision; + float radius = 32767.f; + hb_sweep_gradient_tiles (stops.arrayZ, stops.length, + hb_color_line_get_extend (color_line), + start_angle, end_angle, + [&] (float a0, hb_color_t c0, float a1, hb_color_t c1) + { hb_svg_add_sweep_patch (body, precision, cx, cy, radius, + a0, hb_svg_rgba_from_hb_color (c0), + a1, hb_svg_rgba_from_hb_color (c1)); }); } static void diff --git a/src/hb-vector-svg-utils.hh b/src/hb-vector-svg-utils.hh index e29972dad..c8fd9e76c 100644 --- a/src/hb-vector-svg-utils.hh +++ b/src/hb-vector-svg-utils.hh @@ -126,4 +126,200 @@ hb_svg_scale_precision (unsigned precision) return precision < 7 ? 7 : precision; } +static inline hb_color_t +hb_color_lerp (hb_color_t c0, hb_color_t c1, float t) +{ + auto lerp = [&] (unsigned shift) -> unsigned { + unsigned v0 = (c0 >> shift) & 0xFF; + unsigned v1 = (c1 >> shift) & 0xFF; + return (unsigned) (v0 + t * ((float) v1 - (float) v0) + 0.5f); + }; + return HB_COLOR (lerp (0), lerp (8), lerp (16), lerp (24)); +} + +/* Iterate sweep gradient color stop tiling across the full 0..2π range, + * calling emit_patch (a0, c0, a1, c1) for each sector. + * + * Handles pad, repeat, and reflect extend modes. Stops must be + * pre-sorted by offset. */ +template +static inline void +hb_sweep_gradient_tiles (hb_color_stop_t *stops, + unsigned n_stops, + hb_paint_extend_t extend, + float start_angle, + float end_angle, + Func emit_patch) +{ + if (!n_stops) return; + + if (start_angle == end_angle) + { + if (extend == HB_PAINT_EXTEND_PAD) + { + if (start_angle > 0.f) + emit_patch (0.f, stops[0].color, start_angle, stops[0].color); + if (end_angle < HB_2_PI) + emit_patch (end_angle, stops[n_stops - 1].color, HB_2_PI, stops[n_stops - 1].color); + } + return; + } + + if (end_angle < start_angle) + { + float tmp = start_angle; start_angle = end_angle; end_angle = tmp; + for (unsigned i = 0; i < n_stops - 1 - i; i++) + { + hb_color_stop_t t = stops[i]; + stops[i] = stops[n_stops - 1 - i]; + stops[n_stops - 1 - i] = t; + } + for (unsigned i = 0; i < n_stops; i++) + stops[i].offset = 1.f - stops[i].offset; + } + + /* Map stop offsets to angles. */ + float angles_buf[16]; + hb_color_t colors_buf[16]; + float *angles = angles_buf; + hb_color_t *colors = colors_buf; + bool dynamic = false; + + if (n_stops > 16) + { + angles = (float *) hb_malloc (sizeof (float) * n_stops); + colors = (hb_color_t *) hb_malloc (sizeof (hb_color_t) * n_stops); + if (!angles || !colors) + { + hb_free (angles); + hb_free (colors); + return; + } + dynamic = true; + } + + for (unsigned i = 0; i < n_stops; i++) + { + angles[i] = start_angle + stops[i].offset * (end_angle - start_angle); + colors[i] = stops[i].color; + } + + if (extend == HB_PAINT_EXTEND_PAD) + { + unsigned pos; + hb_color_t color0 = colors[0]; + for (pos = 0; pos < n_stops; pos++) + { + if (angles[pos] >= 0) + { + if (pos > 0) + { + float f = (0.f - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); + color0 = hb_color_lerp (colors[pos - 1], colors[pos], f); + } + break; + } + } + if (pos == n_stops) + { + color0 = colors[n_stops - 1]; + emit_patch (0.f, color0, HB_2_PI, color0); + goto done; + } + emit_patch (0.f, color0, angles[pos], colors[pos]); + for (pos++; pos < n_stops; pos++) + { + if (angles[pos] <= HB_2_PI) + emit_patch (angles[pos - 1], colors[pos - 1], angles[pos], colors[pos]); + else + { + float f = (HB_2_PI - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); + hb_color_t color1 = hb_color_lerp (colors[pos - 1], colors[pos], f); + emit_patch (angles[pos - 1], colors[pos - 1], HB_2_PI, color1); + break; + } + } + if (pos == n_stops) + { + color0 = colors[n_stops - 1]; + emit_patch (angles[n_stops - 1], color0, HB_2_PI, color0); + goto done; + } + } + else + { + float span = angles[n_stops - 1] - angles[0]; + if (fabsf (span) < 1e-6f) + goto done; + + int k = 0; + if (angles[0] >= 0) + { + float ss = angles[0]; + while (ss > 0) + { + if (span > 0) { ss -= span; k--; } + else { ss += span; k++; } + } + } + else + { + float ee = angles[n_stops - 1]; + while (ee < 0) + { + if (span > 0) { ee += span; k++; } + else { ee -= span; k--; } + } + } + + span = fabsf (span); + for (int l = k; l < 1000; l++) + { + for (unsigned i = 1; i < n_stops; i++) + { + float a0_l, a1_l; + hb_color_t col0, col1; + if ((l % 2 != 0) && (extend == HB_PAINT_EXTEND_REFLECT)) + { + a0_l = angles[0] + angles[n_stops - 1] - angles[n_stops - i] + l * span; + a1_l = angles[0] + angles[n_stops - 1] - angles[n_stops - 1 - i] + l * span; + col0 = colors[n_stops - i]; + col1 = colors[n_stops - 1 - i]; + } + else + { + a0_l = angles[i - 1] + l * span; + a1_l = angles[i] + l * span; + col0 = colors[i - 1]; + col1 = colors[i]; + } + + if (a1_l < 0.f) continue; + if (a0_l < 0.f) + { + float f = (0.f - a0_l) / (a1_l - a0_l); + hb_color_t c = hb_color_lerp (col0, col1, f); + emit_patch (0.f, c, a1_l, col1); + } + else if (a1_l >= HB_2_PI) + { + float f = (HB_2_PI - a0_l) / (a1_l - a0_l); + hb_color_t c = hb_color_lerp (col0, col1, f); + emit_patch (a0_l, col0, HB_2_PI, c); + goto done; + } + else + emit_patch (a0_l, col0, a1_l, col1); + } + } + } + +done: + if (dynamic) + { + hb_free (angles); + hb_free (colors); + } +} + #endif /* HB_VECTOR_SVG_UTILS_HH */ From 30ee1bb2c62060a0548218f7ea5ab254635552be Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 10 Apr 2026 12:16:55 -0600 Subject: [PATCH 19/23] [vector] Rename hb_vector_svg_*set_{flat,precision} API Drop "svg" from the names since flat/precision settings apply equally to PDF output. The new names follow the hb_vector_draw_* / hb_vector_paint_* convention: hb_vector_svg_set_flat -> hb_vector_draw_set_flat hb_vector_svg_set_precision -> hb_vector_draw_set_precision hb_vector_svg_paint_set_flat -> hb_vector_paint_set_flat hb_vector_svg_paint_set_precision -> hb_vector_paint_set_precision Assisted-by: Claude Opus 4.6 (1M context) --- NEWS | 8 ++++---- src/hb-vector-svg-draw.cc | 12 ++++++------ src/hb-vector-svg-paint.cc | 12 ++++++------ src/hb-vector.h | 8 ++++---- test/fuzzing/hb-vector-fuzzer.cc | 8 ++++---- util/vector-output.hh | 8 ++++---- 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/NEWS b/NEWS index 460737c2c..90dff9baf 100644 --- a/NEWS +++ b/NEWS @@ -259,10 +259,10 @@ Wednesday, March 4, 2026 +hb_vector_paint_set_scale_factor() +hb_vector_paint_set_transform() +hb_vector_paint_set_user_data() - +hb_vector_svg_paint_set_flat() - +hb_vector_svg_paint_set_precision() - +hb_vector_svg_set_flat() - +hb_vector_svg_set_precision() + +hb_vector_paint_set_flat() + +hb_vector_paint_set_precision() + +hb_vector_draw_set_flat() + +hb_vector_draw_set_precision() Overview of changes leading to 12.3.2 diff --git a/src/hb-vector-svg-draw.cc b/src/hb-vector-svg-draw.cc index a82f6095a..e33b9f352 100644 --- a/src/hb-vector-svg-draw.cc +++ b/src/hb-vector-svg-draw.cc @@ -628,32 +628,32 @@ hb_vector_draw_glyph (hb_vector_draw_t *draw, } /** - * hb_vector_svg_set_flat: + * hb_vector_draw_set_flat: * @draw: a draw context. * @flat: whether to flatten geometry and disable reuse. * - * Enables or disables SVG draw flattening. + * Enables or disables draw flattening. * * Since: 13.0.0 */ void -hb_vector_svg_set_flat (hb_vector_draw_t *draw, +hb_vector_draw_set_flat (hb_vector_draw_t *draw, hb_bool_t flat) { draw->flat = !!flat; } /** - * hb_vector_svg_set_precision: + * hb_vector_draw_set_precision: * @draw: a draw context. * @precision: decimal precision. * - * Sets numeric output precision for SVG draw output. + * Sets numeric output precision for draw output. * * Since: 13.0.0 */ void -hb_vector_svg_set_precision (hb_vector_draw_t *draw, +hb_vector_draw_set_precision (hb_vector_draw_t *draw, unsigned precision) { draw->precision = hb_min (precision, 12u); diff --git a/src/hb-vector-svg-paint.cc b/src/hb-vector-svg-paint.cc index b5ebe8aca..01efa2531 100644 --- a/src/hb-vector-svg-paint.cc +++ b/src/hb-vector-svg-paint.cc @@ -1469,32 +1469,32 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint, } /** - * hb_vector_svg_paint_set_flat: + * hb_vector_paint_set_flat: * @paint: a paint context. * @flat: whether to flatten paint output and disable glyph-group reuse. * - * Enables or disables SVG paint flattening. + * Enables or disables paint flattening. * * Since: 13.0.0 */ void -hb_vector_svg_paint_set_flat (hb_vector_paint_t *paint, +hb_vector_paint_set_flat (hb_vector_paint_t *paint, hb_bool_t flat) { paint->flat = !!flat; } /** - * hb_vector_svg_paint_set_precision: + * hb_vector_paint_set_precision: * @paint: a paint context. * @precision: decimal precision. * - * Sets numeric output precision for SVG paint output. + * Sets numeric output precision for paint output. * * Since: 13.0.0 */ void -hb_vector_svg_paint_set_precision (hb_vector_paint_t *paint, +hb_vector_paint_set_precision (hb_vector_paint_t *paint, unsigned precision) { paint->precision = hb_min (precision, 12u); diff --git a/src/hb-vector.h b/src/hb-vector.h index 8962dc94d..ad7e77560 100644 --- a/src/hb-vector.h +++ b/src/hb-vector.h @@ -162,11 +162,11 @@ hb_vector_draw_glyph (hb_vector_draw_t *draw, hb_vector_extents_mode_t extents_mode); HB_EXTERN void -hb_vector_svg_set_flat (hb_vector_draw_t *draw, +hb_vector_draw_set_flat (hb_vector_draw_t *draw, hb_bool_t flat); HB_EXTERN void -hb_vector_svg_set_precision (hb_vector_draw_t *draw, +hb_vector_draw_set_precision (hb_vector_draw_t *draw, unsigned precision); HB_EXTERN hb_blob_t * @@ -264,11 +264,11 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint, hb_vector_extents_mode_t extents_mode); HB_EXTERN void -hb_vector_svg_paint_set_flat (hb_vector_paint_t *paint, +hb_vector_paint_set_flat (hb_vector_paint_t *paint, hb_bool_t flat); HB_EXTERN void -hb_vector_svg_paint_set_precision (hb_vector_paint_t *paint, +hb_vector_paint_set_precision (hb_vector_paint_t *paint, unsigned precision); HB_EXTERN hb_blob_t * diff --git a/test/fuzzing/hb-vector-fuzzer.cc b/test/fuzzing/hb-vector-fuzzer.cc index fc096a739..e58400a9b 100644 --- a/test/fuzzing/hb-vector-fuzzer.cc +++ b/test/fuzzing/hb-vector-fuzzer.cc @@ -21,10 +21,10 @@ extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) hb_vector_paint_set_foreground (paint, HB_COLOR (0, 0, 0, 255)); unsigned precision = size ? data[size - 1] % 5 : 0; - hb_vector_svg_set_precision (draw, precision); - hb_vector_svg_paint_set_precision (paint, precision); - hb_vector_svg_set_flat (draw, size > 1 ? !!(data[size - 2] & 1) : false); - hb_vector_svg_paint_set_flat (paint, size > 2 ? !!(data[size - 3] & 1) : false); + hb_vector_draw_set_precision (draw, precision); + hb_vector_paint_set_precision (paint, precision); + hb_vector_draw_set_flat (draw, size > 1 ? !!(data[size - 2] & 1) : false); + hb_vector_paint_set_flat (paint, size > 2 ? !!(data[size - 3] & 1) : false); unsigned glyph_count = hb_face_get_glyph_count (input.face); unsigned limit = glyph_count > 16 ? 16 : glyph_count; diff --git a/util/vector-output.hh b/util/vector-output.hh index fad2338a5..f8f3f6fd1 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -188,8 +188,8 @@ struct vector_output_t : output_options_t<>, view_options_t hb_vector_draw_set_scale_factor (draw, 1.f, 1.f); hb_vector_draw_set_extents (draw, &extents); - hb_vector_svg_set_precision (draw, precision); - hb_vector_svg_set_flat (draw, flat); + hb_vector_draw_set_precision (draw, precision); + hb_vector_draw_set_flat (draw, flat); if (paint) { @@ -199,8 +199,8 @@ struct vector_output_t : output_options_t<>, view_options_t hb_vector_paint_set_palette (paint, this->palette); apply_custom_palette (paint); init_palette_color_cache (); - hb_vector_svg_paint_set_precision (paint, precision); - hb_vector_svg_paint_set_flat (paint, flat); + hb_vector_paint_set_precision (paint, precision); + hb_vector_paint_set_flat (paint, flat); } bool had_draw = false; From ca8f48de2d0c1271dbdbedf135bb592665010cef Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 10 Apr 2026 12:28:36 -0600 Subject: [PATCH 20/23] [vector] Rename source files and internal API for consistency Rename files to follow hb-vector-{draw,paint}[-{svg,pdf}] convention: hb-vector-svg-draw.cc -> hb-vector-draw.cc hb-vector-svg-paint.cc -> hb-vector-paint.cc hb-vector-pdf-paint.cc -> hb-vector-paint-pdf.cc Rename internal API to match: hb_vector_pdf_paint_funcs_get -> hb_vector_paint_pdf_funcs_get hb_vector_pdf_paint_render -> hb_vector_paint_render_pdf Remove stale declarations for hb_vector_svg_paint_funcs_get and hb_vector_svg_paint_render which were declared but never defined. Update meson.build, CMakeLists.txt, and harfbuzz-world.cc. Assisted-by: Claude Opus 4.6 (1M context) --- CMakeLists.txt | 5 +++-- src/harfbuzz-world.cc | 6 +++--- src/{hb-vector-svg-draw.cc => hb-vector-draw.cc} | 0 src/{hb-vector-pdf-paint.cc => hb-vector-paint-pdf.cc} | 4 ++-- src/{hb-vector-svg-paint.cc => hb-vector-paint.cc} | 4 ++-- src/hb-vector-paint.hh | 10 +++------- src/meson.build | 6 +++--- 7 files changed, 16 insertions(+), 19 deletions(-) rename src/{hb-vector-svg-draw.cc => hb-vector-draw.cc} (100%) rename src/{hb-vector-pdf-paint.cc => hb-vector-paint-pdf.cc} (99%) rename src/{hb-vector-svg-paint.cc => hb-vector-paint.cc} (99%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ae6aa06e..d9063fc85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -282,9 +282,10 @@ set (vector_project_sources ${PROJECT_SOURCE_DIR}/src/hb-zlib.cc ${PROJECT_SOURCE_DIR}/src/hb-zlib.hh ${PROJECT_SOURCE_DIR}/src/hb-vector.cc - ${PROJECT_SOURCE_DIR}/src/hb-vector-svg-draw.cc + ${PROJECT_SOURCE_DIR}/src/hb-vector-draw.cc ${PROJECT_SOURCE_DIR}/src/hb-vector-svg-path.cc - ${PROJECT_SOURCE_DIR}/src/hb-vector-svg-paint.cc + ${PROJECT_SOURCE_DIR}/src/hb-vector-paint.cc + ${PROJECT_SOURCE_DIR}/src/hb-vector-paint-pdf.cc ${PROJECT_SOURCE_DIR}/src/hb-vector-svg-path.hh ${PROJECT_SOURCE_DIR}/src/hb-vector-svg-subset.cc ${PROJECT_SOURCE_DIR}/src/hb-vector-svg-subset.hh diff --git a/src/harfbuzz-world.cc b/src/harfbuzz-world.cc index 5922468eb..156231773 100644 --- a/src/harfbuzz-world.cc +++ b/src/harfbuzz-world.cc @@ -165,9 +165,9 @@ #ifdef HB_HAS_VECTOR #include "hb-static.cc" -#include "hb-vector-pdf-paint.cc" -#include "hb-vector-svg-draw.cc" -#include "hb-vector-svg-paint.cc" +#include "hb-vector-draw.cc" +#include "hb-vector-paint-pdf.cc" +#include "hb-vector-paint.cc" #include "hb-vector-svg-path.cc" #include "hb-vector-svg-subset.cc" #include "hb-vector-svg-utils.cc" diff --git a/src/hb-vector-svg-draw.cc b/src/hb-vector-draw.cc similarity index 100% rename from src/hb-vector-svg-draw.cc rename to src/hb-vector-draw.cc diff --git a/src/hb-vector-pdf-paint.cc b/src/hb-vector-paint-pdf.cc similarity index 99% rename from src/hb-vector-pdf-paint.cc rename to src/hb-vector-paint-pdf.cc index dd94f3498..9b7923573 100644 --- a/src/hb-vector-pdf-paint.cc +++ b/src/hb-vector-paint-pdf.cc @@ -1230,7 +1230,7 @@ free_static_pdf_paint_funcs () } hb_paint_funcs_t * -hb_vector_pdf_paint_funcs_get () +hb_vector_paint_pdf_funcs_get () { return static_pdf_paint_funcs.get_unconst (); } @@ -1239,7 +1239,7 @@ hb_vector_pdf_paint_funcs_get () /* ---- render ---- */ hb_blob_t * -hb_vector_pdf_paint_render (hb_vector_paint_t *paint) +hb_vector_paint_render_pdf (hb_vector_paint_t *paint) { if (!paint->has_extents) return nullptr; diff --git a/src/hb-vector-svg-paint.cc b/src/hb-vector-paint.cc similarity index 99% rename from src/hb-vector-svg-paint.cc rename to src/hb-vector-paint.cc index 01efa2531..554ae6b14 100644 --- a/src/hb-vector-svg-paint.cc +++ b/src/hb-vector-paint.cc @@ -1345,7 +1345,7 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint, hb_buf_append_str (&body, " cm\n"); hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph, - hb_vector_pdf_paint_funcs_get (), paint, + hb_vector_paint_pdf_funcs_get (), paint, (unsigned) paint->palette, paint->foreground); hb_buf_append_str (&body, "Q\n"); @@ -1543,7 +1543,7 @@ hb_blob_t * hb_vector_paint_render (hb_vector_paint_t *paint) { if (paint->format == HB_VECTOR_FORMAT_PDF) - return hb_vector_pdf_paint_render (paint); + return hb_vector_paint_render_pdf (paint); if (paint->format != HB_VECTOR_FORMAT_SVG) return nullptr; if (!paint->has_extents) diff --git a/src/hb-vector-paint.hh b/src/hb-vector-paint.hh index 01a46ff42..0b9ea9eef 100644 --- a/src/hb-vector-paint.hh +++ b/src/hb-vector-paint.hh @@ -80,12 +80,8 @@ struct hb_vector_paint_t hb_vector_t ¤t_body () { return group_stack.tail (); } }; -/* Implemented in hb-vector-svg-paint.cc */ -hb_paint_funcs_t * hb_vector_svg_paint_funcs_get (); -hb_blob_t * hb_vector_svg_paint_render (hb_vector_paint_t *paint); - -/* Implemented in hb-vector-pdf-paint.cc */ -HB_INTERNAL hb_paint_funcs_t * hb_vector_pdf_paint_funcs_get (); -HB_INTERNAL hb_blob_t * hb_vector_pdf_paint_render (hb_vector_paint_t *paint); +/* Implemented in hb-vector-paint-pdf.cc */ +HB_INTERNAL hb_paint_funcs_t * hb_vector_paint_pdf_funcs_get (); +HB_INTERNAL hb_blob_t * hb_vector_paint_render_pdf (hb_vector_paint_t *paint); #endif /* HB_VECTOR_PAINT_HH */ diff --git a/src/meson.build b/src/meson.build index 78430aa7d..67ab3e8dc 100644 --- a/src/meson.build +++ b/src/meson.build @@ -753,10 +753,10 @@ hb_vector_sources = files( 'hb-zlib.cc', 'hb-zlib.hh', 'hb-vector.cc', - 'hb-vector-svg-draw.cc', + 'hb-vector-draw.cc', 'hb-vector-svg-path.cc', - 'hb-vector-svg-paint.cc', - 'hb-vector-pdf-paint.cc', + 'hb-vector-paint.cc', + 'hb-vector-paint-pdf.cc', 'hb-vector-svg-path.hh', 'hb-vector-svg-subset.cc', 'hb-vector-svg-subset.hh', From 3f6c0e48ad9d24dcec7c3ed2fb1c12b0b1318528 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 10 Apr 2026 12:34:21 -0600 Subject: [PATCH 21/23] [vector] Switch format selection from if/else to switch Replace all if/else chains on hb_vector_format_t with switch statements, making the format dispatch explicit and extensible. Assisted-by: Claude Opus 4.6 (1M context) --- src/hb-vector-draw.cc | 425 +++++++++++++++++++++++------------------ src/hb-vector-draw.hh | 7 +- src/hb-vector-paint.cc | 381 +++++++++++++++++++----------------- 3 files changed, 442 insertions(+), 371 deletions(-) diff --git a/src/hb-vector-draw.cc b/src/hb-vector-draw.cc index e33b9f352..6cff3c9e7 100644 --- a/src/hb-vector-draw.cc +++ b/src/hb-vector-draw.cc @@ -58,15 +58,17 @@ hb_vector_draw_move_to (hb_draw_funcs_t *, void *) { auto *d = (hb_vector_draw_t *) draw_data; - if (d->format == HB_VECTOR_FORMAT_PDF) + switch (d->format) { - d->append_xy (to_x, to_y); - hb_buf_append_str (&d->path, " m\n"); - } - else - { - hb_buf_append_c (&d->path, 'M'); - d->append_xy (to_x, to_y); + case HB_VECTOR_FORMAT_PDF: + d->append_xy (to_x, to_y); + hb_buf_append_str (&d->path, " m\n"); + break; + case HB_VECTOR_FORMAT_SVG: + hb_buf_append_c (&d->path, 'M'); + d->append_xy (to_x, to_y); + break; + case HB_VECTOR_FORMAT_INVALID: default: break; } } @@ -78,15 +80,17 @@ hb_vector_draw_line_to (hb_draw_funcs_t *, void *) { auto *d = (hb_vector_draw_t *) draw_data; - if (d->format == HB_VECTOR_FORMAT_PDF) + switch (d->format) { - d->append_xy (to_x, to_y); - hb_buf_append_str (&d->path, " l\n"); - } - else - { - hb_buf_append_c (&d->path, 'L'); - d->append_xy (to_x, to_y); + case HB_VECTOR_FORMAT_PDF: + d->append_xy (to_x, to_y); + hb_buf_append_str (&d->path, " l\n"); + break; + case HB_VECTOR_FORMAT_SVG: + hb_buf_append_c (&d->path, 'L'); + d->append_xy (to_x, to_y); + break; + case HB_VECTOR_FORMAT_INVALID: default: break; } } @@ -99,27 +103,31 @@ hb_vector_draw_quadratic_to (hb_draw_funcs_t *, void *) { auto *d = (hb_vector_draw_t *) draw_data; - if (d->format == HB_VECTOR_FORMAT_PDF) + switch (d->format) { - /* PDF has no quadratic operator; promote to cubic. */ - float sx = st->current_x, sy = st->current_y; - float c1x = sx + 2.f / 3.f * (cx - sx); - float c1y = sy + 2.f / 3.f * (cy - sy); - float c2x = to_x + 2.f / 3.f * (cx - to_x); - float c2y = to_y + 2.f / 3.f * (cy - to_y); - d->append_xy (c1x, c1y); - hb_buf_append_c (&d->path, ' '); - d->append_xy (c2x, c2y); - hb_buf_append_c (&d->path, ' '); - d->append_xy (to_x, to_y); - hb_buf_append_str (&d->path, " c\n"); - } - else - { - hb_buf_append_c (&d->path, 'Q'); - d->append_xy (cx, cy); - hb_buf_append_c (&d->path, ' '); - d->append_xy (to_x, to_y); + case HB_VECTOR_FORMAT_PDF: + { + /* PDF has no quadratic operator; promote to cubic. */ + float sx = st->current_x, sy = st->current_y; + float c1x = sx + 2.f / 3.f * (cx - sx); + float c1y = sy + 2.f / 3.f * (cy - sy); + float c2x = to_x + 2.f / 3.f * (cx - to_x); + float c2y = to_y + 2.f / 3.f * (cy - to_y); + d->append_xy (c1x, c1y); + hb_buf_append_c (&d->path, ' '); + d->append_xy (c2x, c2y); + hb_buf_append_c (&d->path, ' '); + d->append_xy (to_x, to_y); + hb_buf_append_str (&d->path, " c\n"); + break; + } + case HB_VECTOR_FORMAT_SVG: + hb_buf_append_c (&d->path, 'Q'); + d->append_xy (cx, cy); + hb_buf_append_c (&d->path, ' '); + d->append_xy (to_x, to_y); + break; + case HB_VECTOR_FORMAT_INVALID: default: break; } } @@ -133,23 +141,25 @@ hb_vector_draw_cubic_to (hb_draw_funcs_t *, void *) { auto *d = (hb_vector_draw_t *) draw_data; - if (d->format == HB_VECTOR_FORMAT_PDF) + switch (d->format) { - d->append_xy (c1x, c1y); - hb_buf_append_c (&d->path, ' '); - d->append_xy (c2x, c2y); - hb_buf_append_c (&d->path, ' '); - d->append_xy (to_x, to_y); - hb_buf_append_str (&d->path, " c\n"); - } - else - { - hb_buf_append_c (&d->path, 'C'); - d->append_xy (c1x, c1y); - hb_buf_append_c (&d->path, ' '); - d->append_xy (c2x, c2y); - hb_buf_append_c (&d->path, ' '); - d->append_xy (to_x, to_y); + case HB_VECTOR_FORMAT_PDF: + d->append_xy (c1x, c1y); + hb_buf_append_c (&d->path, ' '); + d->append_xy (c2x, c2y); + hb_buf_append_c (&d->path, ' '); + d->append_xy (to_x, to_y); + hb_buf_append_str (&d->path, " c\n"); + break; + case HB_VECTOR_FORMAT_SVG: + hb_buf_append_c (&d->path, 'C'); + d->append_xy (c1x, c1y); + hb_buf_append_c (&d->path, ' '); + d->append_xy (c2x, c2y); + hb_buf_append_c (&d->path, ' '); + d->append_xy (to_x, to_y); + break; + case HB_VECTOR_FORMAT_INVALID: default: break; } } @@ -160,10 +170,16 @@ hb_vector_draw_close_path (hb_draw_funcs_t *, void *) { auto *d = (hb_vector_draw_t *) draw_data; - if (d->format == HB_VECTOR_FORMAT_PDF) - hb_buf_append_str (&d->path, "h\n"); - else - hb_buf_append_c (&d->path, 'Z'); + switch (d->format) + { + case HB_VECTOR_FORMAT_PDF: + hb_buf_append_str (&d->path, "h\n"); + break; + case HB_VECTOR_FORMAT_SVG: + hb_buf_append_c (&d->path, 'Z'); + break; + case HB_VECTOR_FORMAT_INVALID: default: break; + } } static inline void free_static_vector_draw_funcs (); @@ -210,8 +226,14 @@ hb_vector_draw_funcs_get () hb_vector_draw_t * hb_vector_draw_create_or_fail (hb_vector_format_t format) { - if (format != HB_VECTOR_FORMAT_SVG && format != HB_VECTOR_FORMAT_PDF) - return nullptr; + switch (format) + { + case HB_VECTOR_FORMAT_SVG: + case HB_VECTOR_FORMAT_PDF: + break; + case HB_VECTOR_FORMAT_INVALID: default: + return nullptr; + } hb_vector_draw_t *draw = hb_object_create (); if (unlikely (!draw)) @@ -518,9 +540,14 @@ hb_vector_draw_glyph (hb_vector_draw_t *draw, float pen_y, hb_vector_extents_mode_t extents_mode) { - if (draw->format != HB_VECTOR_FORMAT_SVG && - draw->format != HB_VECTOR_FORMAT_PDF) - return false; + switch (draw->format) + { + case HB_VECTOR_FORMAT_SVG: + case HB_VECTOR_FORMAT_PDF: + break; + case HB_VECTOR_FORMAT_INVALID: default: + return false; + } if (extents_mode == HB_VECTOR_EXTENTS_MODE_EXPAND) { @@ -546,85 +573,94 @@ hb_vector_draw_glyph (hb_vector_draw_t *draw, } } - if (draw->format == HB_VECTOR_FORMAT_PDF) + switch (draw->format) { - /* PDF: always inline. Pen and font coords are both Y-up. */ - hb_transform_t<> saved = draw->transform; - draw->transform = {1, 0, 0, 1, pen_x, pen_y}; + case HB_VECTOR_FORMAT_PDF: + { + /* PDF: always inline. Pen and font coords are both Y-up. */ + hb_transform_t<> saved = draw->transform; + draw->transform = {1, 0, 0, 1, pen_x, pen_y}; - draw->path.clear (); - hb_font_draw_glyph (font, glyph, hb_vector_draw_funcs_get (), draw); - draw->transform = saved; + draw->path.clear (); + hb_font_draw_glyph (font, glyph, hb_vector_draw_funcs_get (), draw); + draw->transform = saved; - if (!draw->path.length) + if (!draw->path.length) + return false; + + hb_buf_append_len (&draw->body, draw->path.arrayZ, draw->path.length); + hb_buf_append_str (&draw->body, "f\n"); + return true; + } + + case HB_VECTOR_FORMAT_SVG: + { + bool needs_def = !draw->flat && !hb_set_has (draw->defined_glyphs, glyph); + if (needs_def) + { + draw->path.clear (); + hb_svg_path_sink_t sink = {&draw->path, draw->precision}; + hb_font_draw_glyph (font, glyph, hb_svg_path_draw_funcs_get (), &sink); + if (!draw->path.length) + return false; + hb_buf_append_str (&draw->defs, "defs, glyph); + hb_buf_append_str (&draw->defs, "\" d=\""); + hb_buf_append_len (&draw->defs, draw->path.arrayZ, draw->path.length); + hb_buf_append_str (&draw->defs, "\"/>\n"); + hb_set_add (draw->defined_glyphs, glyph); + } + + if (draw->flat) + { + draw->path.clear (); + hb_svg_path_sink_t sink = {&draw->path, draw->precision}; + hb_font_draw_glyph (font, glyph, hb_svg_path_draw_funcs_get (), &sink); + + if (!draw->path.length) + return false; + + float xx = draw->transform.xx; + float yx = draw->transform.yx; + float xy = draw->transform.xy; + float yy = draw->transform.yy; + 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, draw->path.arrayZ, draw->path.length); + hb_buf_append_str (&draw->body, "\" transform=\""); + hb_svg_append_instance_transform (&draw->body, + draw->precision, + draw->x_scale_factor, + draw->y_scale_factor, + xx, yx, xy, yy, tx, ty); + hb_buf_append_str (&draw->body, "\"/>\n"); + return true; + } + + float xx = draw->transform.xx; + float yx = draw->transform.yx; + float xy = draw->transform.xy; + float yy = draw->transform.yy; + 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, glyph); + hb_buf_append_str (&draw->body, "\" transform=\""); + hb_svg_append_instance_transform (&draw->body, + draw->precision, + draw->x_scale_factor, + draw->y_scale_factor, + xx, yx, xy, yy, tx, ty); + hb_buf_append_str (&draw->body, "\"/>\n"); + return true; + } + + case HB_VECTOR_FORMAT_INVALID: default: return false; - - hb_buf_append_len (&draw->body, draw->path.arrayZ, draw->path.length); - hb_buf_append_str (&draw->body, "f\n"); - return true; } - - bool needs_def = !draw->flat && !hb_set_has (draw->defined_glyphs, glyph); - if (needs_def) - { - draw->path.clear (); - hb_svg_path_sink_t sink = {&draw->path, draw->precision}; - hb_font_draw_glyph (font, glyph, hb_svg_path_draw_funcs_get (), &sink); - if (!draw->path.length) - return false; - hb_buf_append_str (&draw->defs, "defs, glyph); - hb_buf_append_str (&draw->defs, "\" d=\""); - hb_buf_append_len (&draw->defs, draw->path.arrayZ, draw->path.length); - hb_buf_append_str (&draw->defs, "\"/>\n"); - hb_set_add (draw->defined_glyphs, glyph); - } - - if (draw->flat) - { - draw->path.clear (); - hb_svg_path_sink_t sink = {&draw->path, draw->precision}; - hb_font_draw_glyph (font, glyph, hb_svg_path_draw_funcs_get (), &sink); - - if (!draw->path.length) - return false; - - float xx = draw->transform.xx; - float yx = draw->transform.yx; - float xy = draw->transform.xy; - float yy = draw->transform.yy; - 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, draw->path.arrayZ, draw->path.length); - hb_buf_append_str (&draw->body, "\" transform=\""); - hb_svg_append_instance_transform (&draw->body, - draw->precision, - draw->x_scale_factor, - draw->y_scale_factor, - xx, yx, xy, yy, tx, ty); - hb_buf_append_str (&draw->body, "\"/>\n"); - return true; - } - - float xx = draw->transform.xx; - float yx = draw->transform.yx; - float xy = draw->transform.xy; - float yy = draw->transform.yy; - 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, glyph); - hb_buf_append_str (&draw->body, "\" transform=\""); - hb_svg_append_instance_transform (&draw->body, - draw->precision, - draw->x_scale_factor, - draw->y_scale_factor, - xx, yx, xy, yy, tx, ty); - hb_buf_append_str (&draw->body, "\"/>\n"); - return true; } /** @@ -765,64 +801,71 @@ hb_vector_draw_render_pdf (hb_vector_draw_t *draw) hb_blob_t * hb_vector_draw_render (hb_vector_draw_t *draw) { - if (draw->format == HB_VECTOR_FORMAT_PDF) - return hb_vector_draw_render_pdf (draw); - - if (draw->format != HB_VECTOR_FORMAT_SVG) - return nullptr; - if (!draw->has_extents) - return nullptr; - - hb_vector_t out; - hb_buf_recover_recycled (draw->recycled_blob, &out); - unsigned estimated = draw->defs.length + - (draw->body.length ? draw->body.length : draw->path.length) + - 256; - out.alloc (estimated); - hb_buf_append_str (&out, "extents.x, draw->precision); - hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, draw->extents.y, draw->precision); - hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, draw->extents.width, draw->precision); - hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, draw->extents.height, draw->precision); - hb_buf_append_str (&out, "\" width=\""); - hb_buf_append_num (&out, draw->extents.width, draw->precision); - hb_buf_append_str (&out, "\" height=\""); - hb_buf_append_num (&out, draw->extents.height, draw->precision); - hb_buf_append_str (&out, "\">\n"); - - if (draw->defs.length) + switch (draw->format) { - hb_buf_append_str (&out, "\n"); - hb_buf_append_len (&out, draw->defs.arrayZ, draw->defs.length); - hb_buf_append_str (&out, "\n"); + case HB_VECTOR_FORMAT_PDF: + return hb_vector_draw_render_pdf (draw); + + case HB_VECTOR_FORMAT_SVG: + { + if (!draw->has_extents) + return nullptr; + + hb_vector_t out; + hb_buf_recover_recycled (draw->recycled_blob, &out); + unsigned estimated = draw->defs.length + + (draw->body.length ? draw->body.length : draw->path.length) + + 256; + out.alloc (estimated); + hb_buf_append_str (&out, "extents.x, draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, draw->extents.y, draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, draw->extents.width, draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, draw->extents.height, draw->precision); + hb_buf_append_str (&out, "\" width=\""); + hb_buf_append_num (&out, draw->extents.width, draw->precision); + hb_buf_append_str (&out, "\" height=\""); + hb_buf_append_num (&out, draw->extents.height, draw->precision); + hb_buf_append_str (&out, "\">\n"); + + if (draw->defs.length) + { + hb_buf_append_str (&out, "\n"); + hb_buf_append_len (&out, draw->defs.arrayZ, draw->defs.length); + hb_buf_append_str (&out, "\n"); + } + + if (draw->body.length) + { + hb_buf_append_len (&out, draw->body.arrayZ, draw->body.length); + } + else if (draw->path.length) + { + hb_buf_append_str (&out, "path.arrayZ, draw->path.length); + hb_buf_append_str (&out, "\"/>\n"); + } + + hb_buf_append_str (&out, "\n"); + + hb_blob_t *blob = hb_buf_blob_from (&draw->recycled_blob, &out); + + draw->path.clear (); + draw->defs.clear (); + draw->body.clear (); + hb_set_clear (draw->defined_glyphs); + draw->has_extents = false; + draw->extents = {0, 0, 0, 0}; + + return blob; + } + + case HB_VECTOR_FORMAT_INVALID: default: + return nullptr; } - - if (draw->body.length) - { - hb_buf_append_len (&out, draw->body.arrayZ, draw->body.length); - } - else if (draw->path.length) - { - hb_buf_append_str (&out, "path.arrayZ, draw->path.length); - hb_buf_append_str (&out, "\"/>\n"); - } - - hb_buf_append_str (&out, "\n"); - - hb_blob_t *blob = hb_buf_blob_from (&draw->recycled_blob, &out); - - draw->path.clear (); - draw->defs.clear (); - draw->body.clear (); - hb_set_clear (draw->defined_glyphs); - draw->has_extents = false; - draw->extents = {0, 0, 0, 0}; - - return blob; } /** diff --git a/src/hb-vector-draw.hh b/src/hb-vector-draw.hh index f1a09f728..de14cfb7b 100644 --- a/src/hb-vector-draw.hh +++ b/src/hb-vector-draw.hh @@ -33,7 +33,12 @@ struct hb_vector_draw_t float tx, ty; hb_svg_transform_point (transform, x_scale_factor, y_scale_factor, x, y, &tx, &ty); hb_buf_append_num (&path, tx, precision); - hb_buf_append_c (&path, format == HB_VECTOR_FORMAT_PDF ? ' ' : ','); + switch (format) + { + case HB_VECTOR_FORMAT_PDF: hb_buf_append_c (&path, ' '); break; + case HB_VECTOR_FORMAT_SVG: hb_buf_append_c (&path, ','); break; + case HB_VECTOR_FORMAT_INVALID: default: break; + } hb_buf_append_num (&path, ty, precision); } }; diff --git a/src/hb-vector-paint.cc b/src/hb-vector-paint.cc index 554ae6b14..175d69f9d 100644 --- a/src/hb-vector-paint.cc +++ b/src/hb-vector-paint.cc @@ -891,8 +891,14 @@ hb_vector_paint_color_glyph (hb_paint_funcs_t *, hb_vector_paint_t * hb_vector_paint_create_or_fail (hb_vector_format_t format) { - if (format != HB_VECTOR_FORMAT_SVG && format != HB_VECTOR_FORMAT_PDF) - return nullptr; + switch (format) + { + case HB_VECTOR_FORMAT_SVG: + case HB_VECTOR_FORMAT_PDF: + break; + case HB_VECTOR_FORMAT_INVALID: default: + return nullptr; + } hb_vector_paint_t *paint = hb_object_create (); if (unlikely (!paint)) @@ -1325,147 +1331,156 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint, if (unlikely (!hb_vector_paint_ensure_initialized (paint))) return false; - if (paint->format == HB_VECTOR_FORMAT_PDF) + switch (paint->format) { - /* PDF: emit transform + paint directly, no caching. */ - auto &body = paint->current_body (); - hb_buf_append_str (&body, "q\n"); - /* Font and PDF coords are both Y-up; no negation needed. */ - hb_buf_append_num (&body, xx, paint->precision); - hb_buf_append_c (&body, ' '); - hb_buf_append_num (&body, yx, paint->precision); - hb_buf_append_c (&body, ' '); - hb_buf_append_num (&body, xy, paint->precision); - hb_buf_append_c (&body, ' '); - hb_buf_append_num (&body, yy, paint->precision); - hb_buf_append_c (&body, ' '); - hb_buf_append_num (&body, tx, paint->precision); - hb_buf_append_c (&body, ' '); - hb_buf_append_num (&body, ty, paint->precision); - hb_buf_append_str (&body, " cm\n"); - - hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph, - hb_vector_paint_pdf_funcs_get (), paint, - (unsigned) paint->palette, - paint->foreground); - hb_buf_append_str (&body, "Q\n"); - return ret; - } - - bool can_cache = !paint->flat; - hb_svg_color_glyph_cache_key_t cache_key = hb_svg_color_glyph_cache_key (glyph, - (unsigned) paint->palette, - paint->foreground); - if (can_cache) - { - if (paint->defined_color_glyphs.has (cache_key)) + case HB_VECTOR_FORMAT_PDF: { - uint64_t entry = paint->defined_color_glyphs.get (cache_key); - unsigned def_id = hb_svg_cache_entry_def_id (entry); - bool image_like = hb_svg_cache_entry_image_like (entry); + /* PDF: emit transform + paint directly, no caching. */ auto &body = paint->current_body (); - hb_buf_append_str (&body, "precision, - paint->x_scale_factor, - paint->y_scale_factor, - tx, ty); - else - hb_svg_append_instance_transform (&body, paint->precision, - paint->x_scale_factor, - paint->y_scale_factor, - xx, yx, xy, yy, tx, ty); - hb_buf_append_str (&body, "\"/>\n"); - return !body.in_error (); + hb_buf_append_str (&body, "q\n"); + /* Font and PDF coords are both Y-up; no negation needed. */ + hb_buf_append_num (&body, xx, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, yx, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, xy, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, yy, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, tx, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, ty, paint->precision); + hb_buf_append_str (&body, " cm\n"); + + hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph, + hb_vector_paint_pdf_funcs_get (), paint, + (unsigned) paint->palette, + paint->foreground); + hb_buf_append_str (&body, "Q\n"); + return ret; } - } - bool has_svg_image = false; - if (can_cache) - { - if (unlikely (!paint->group_stack.push_or_fail (hb_vector_t {}))) - return false; - paint->current_color_glyph_has_svg_image = false; - } - - if (can_cache) - { - hb_codepoint_t old_gid = paint->current_svg_image_glyph; - hb_face_t *old_face = paint->current_face; - paint->current_svg_image_glyph = glyph; - paint->current_face = hb_font_get_face (font); - hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph, - hb_vector_paint_get_funcs (), paint, - (unsigned) paint->palette, - paint->foreground); - paint->current_svg_image_glyph = old_gid; - paint->current_face = old_face; - if (unlikely (!ret)) + case HB_VECTOR_FORMAT_SVG: { - paint->group_stack.pop (); - return false; + bool can_cache = !paint->flat; + hb_svg_color_glyph_cache_key_t cache_key = hb_svg_color_glyph_cache_key (glyph, + (unsigned) paint->palette, + paint->foreground); + if (can_cache) + { + if (paint->defined_color_glyphs.has (cache_key)) + { + uint64_t entry = paint->defined_color_glyphs.get (cache_key); + unsigned def_id = hb_svg_cache_entry_def_id (entry); + bool image_like = hb_svg_cache_entry_image_like (entry); + auto &body = paint->current_body (); + hb_buf_append_str (&body, "precision, + paint->x_scale_factor, + paint->y_scale_factor, + tx, ty); + else + hb_svg_append_instance_transform (&body, paint->precision, + paint->x_scale_factor, + paint->y_scale_factor, + xx, yx, xy, yy, tx, ty); + hb_buf_append_str (&body, "\"/>\n"); + return !body.in_error (); + } + } + + bool has_svg_image = false; + if (can_cache) + { + if (unlikely (!paint->group_stack.push_or_fail (hb_vector_t {}))) + return false; + paint->current_color_glyph_has_svg_image = false; + } + + if (can_cache) + { + hb_codepoint_t old_gid = paint->current_svg_image_glyph; + hb_face_t *old_face = paint->current_face; + paint->current_svg_image_glyph = glyph; + paint->current_face = hb_font_get_face (font); + hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph, + hb_vector_paint_get_funcs (), paint, + (unsigned) paint->palette, + paint->foreground); + paint->current_svg_image_glyph = old_gid; + paint->current_face = old_face; + if (unlikely (!ret)) + { + paint->group_stack.pop (); + return false; + } + + paint->captured_scratch = paint->group_stack.pop (); + has_svg_image = paint->current_color_glyph_has_svg_image; + if (unlikely (paint->captured_scratch.in_error () || + !paint->captured_scratch.length)) + return false; + + unsigned def_id = paint->color_glyph_counter++; + if (unlikely (!paint->defined_color_glyphs.set (cache_key, + hb_svg_pack_color_glyph_cache_entry (def_id, + has_svg_image)))) + return false; + + hb_buf_append_str (&paint->defs, "defs, def_id); + hb_buf_append_str (&paint->defs, "\">\n"); + hb_buf_append_len (&paint->defs, + paint->captured_scratch.arrayZ, + paint->captured_scratch.length); + hb_buf_append_str (&paint->defs, "\n"); + + auto &body = paint->current_body (); + hb_buf_append_str (&body, "precision, + paint->x_scale_factor, + paint->y_scale_factor, + tx, ty); + else + hb_svg_append_instance_transform (&body, paint->precision, + paint->x_scale_factor, + paint->y_scale_factor, + xx, yx, xy, yy, tx, ty); + hb_buf_append_str (&body, "\"/>\n"); + return !paint->defs.in_error () && !body.in_error (); + } + + hb_buf_append_str (&paint->current_body (), "current_body (), paint->precision, + paint->x_scale_factor, + paint->y_scale_factor, + xx, yx, xy, yy, tx, ty); + hb_buf_append_str (&paint->current_body (), "\">\n"); + hb_codepoint_t old_gid = paint->current_svg_image_glyph; + hb_face_t *old_face = paint->current_face; + paint->current_svg_image_glyph = glyph; + paint->current_face = hb_font_get_face (font); + hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph, + hb_vector_paint_get_funcs (), paint, + (unsigned) paint->palette, + paint->foreground); + paint->current_svg_image_glyph = old_gid; + paint->current_face = old_face; + hb_buf_append_str (&paint->current_body (), "\n"); + return ret && + !paint->defs.in_error () && + !paint->current_body ().in_error (); } - paint->captured_scratch = paint->group_stack.pop (); - has_svg_image = paint->current_color_glyph_has_svg_image; - if (unlikely (paint->captured_scratch.in_error () || - !paint->captured_scratch.length)) + case HB_VECTOR_FORMAT_INVALID: default: return false; - - unsigned def_id = paint->color_glyph_counter++; - if (unlikely (!paint->defined_color_glyphs.set (cache_key, - hb_svg_pack_color_glyph_cache_entry (def_id, - has_svg_image)))) - return false; - - hb_buf_append_str (&paint->defs, "defs, def_id); - hb_buf_append_str (&paint->defs, "\">\n"); - hb_buf_append_len (&paint->defs, - paint->captured_scratch.arrayZ, - paint->captured_scratch.length); - hb_buf_append_str (&paint->defs, "\n"); - - auto &body = paint->current_body (); - hb_buf_append_str (&body, "precision, - paint->x_scale_factor, - paint->y_scale_factor, - tx, ty); - else - hb_svg_append_instance_transform (&body, paint->precision, - paint->x_scale_factor, - paint->y_scale_factor, - xx, yx, xy, yy, tx, ty); - hb_buf_append_str (&body, "\"/>\n"); - return !paint->defs.in_error () && !body.in_error (); } - - hb_buf_append_str (&paint->current_body (), "current_body (), paint->precision, - paint->x_scale_factor, - paint->y_scale_factor, - xx, yx, xy, yy, tx, ty); - hb_buf_append_str (&paint->current_body (), "\">\n"); - hb_codepoint_t old_gid = paint->current_svg_image_glyph; - hb_face_t *old_face = paint->current_face; - paint->current_svg_image_glyph = glyph; - paint->current_face = hb_font_get_face (font); - hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph, - hb_vector_paint_get_funcs (), paint, - (unsigned) paint->palette, - paint->foreground); - paint->current_svg_image_glyph = old_gid; - paint->current_face = old_face; - hb_buf_append_str (&paint->current_body (), "\n"); - return ret && - !paint->defs.in_error () && - !paint->current_body ().in_error (); } /** @@ -1542,54 +1557,62 @@ hb_vector_paint_clear_render_state (hb_vector_paint_t *paint) hb_blob_t * hb_vector_paint_render (hb_vector_paint_t *paint) { - if (paint->format == HB_VECTOR_FORMAT_PDF) - return hb_vector_paint_render_pdf (paint); - if (paint->format != HB_VECTOR_FORMAT_SVG) - return nullptr; - if (!paint->has_extents) - return nullptr; - - if (unlikely (!hb_vector_paint_ensure_initialized (paint))) - return nullptr; - - hb_vector_t out; - hb_buf_recover_recycled (paint->recycled_blob, &out); - unsigned estimated = paint->defs.length + - paint->group_stack.arrayZ[0].length + - 320; - out.alloc (estimated); - hb_buf_append_str (&out, "extents.x, paint->precision); - hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, paint->extents.y, paint->precision); - hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, paint->extents.width, paint->precision); - hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, paint->extents.height, paint->precision); - hb_buf_append_str (&out, "\" width=\""); - hb_buf_append_num (&out, paint->extents.width, paint->precision); - hb_buf_append_str (&out, "\" height=\""); - hb_buf_append_num (&out, paint->extents.height, paint->precision); - hb_buf_append_str (&out, "\">\n"); - - if (paint->defs.length) + switch (paint->format) { - hb_buf_append_str (&out, "\n"); - hb_buf_append_len (&out, paint->defs.arrayZ, paint->defs.length); - hb_buf_append_str (&out, "\n"); + case HB_VECTOR_FORMAT_PDF: + return hb_vector_paint_render_pdf (paint); + + case HB_VECTOR_FORMAT_SVG: + { + if (!paint->has_extents) + return nullptr; + + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return nullptr; + + hb_vector_t out; + hb_buf_recover_recycled (paint->recycled_blob, &out); + unsigned estimated = paint->defs.length + + paint->group_stack.arrayZ[0].length + + 320; + out.alloc (estimated); + hb_buf_append_str (&out, "extents.x, paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, paint->extents.y, paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, paint->extents.width, paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, paint->extents.height, paint->precision); + hb_buf_append_str (&out, "\" width=\""); + hb_buf_append_num (&out, paint->extents.width, paint->precision); + hb_buf_append_str (&out, "\" height=\""); + hb_buf_append_num (&out, paint->extents.height, paint->precision); + hb_buf_append_str (&out, "\">\n"); + + if (paint->defs.length) + { + hb_buf_append_str (&out, "\n"); + hb_buf_append_len (&out, paint->defs.arrayZ, paint->defs.length); + hb_buf_append_str (&out, "\n"); + } + + hb_svg_paint_append_global_transform_prefix (paint, &out); + hb_buf_append_len (&out, paint->group_stack.arrayZ[0].arrayZ, paint->group_stack.arrayZ[0].length); + hb_svg_paint_append_global_transform_suffix (paint, &out); + + hb_buf_append_str (&out, "\n"); + + hb_blob_t *blob = hb_buf_blob_from (&paint->recycled_blob, &out); + + hb_vector_paint_clear_render_state (paint); + + return blob; + } + + case HB_VECTOR_FORMAT_INVALID: default: + return nullptr; } - - hb_svg_paint_append_global_transform_prefix (paint, &out); - hb_buf_append_len (&out, paint->group_stack.arrayZ[0].arrayZ, paint->group_stack.arrayZ[0].length); - hb_svg_paint_append_global_transform_suffix (paint, &out); - - hb_buf_append_str (&out, "\n"); - - hb_blob_t *blob = hb_buf_blob_from (&paint->recycled_blob, &out); - - hb_vector_paint_clear_render_state (paint); - - return blob; } /** From 7b6f26e844cc433b229b4e7819bdb20726b9ec44 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 11 Apr 2026 22:02:29 -0600 Subject: [PATCH 22/23] [vector] Address review feedback - Default hb_vector_{draw,paint}_t::format to HB_VECTOR_FORMAT_INVALID instead of HB_VECTOR_FORMAT_SVG. - Update renamed hb_vector_{draw,paint}_set_{flat,precision} API docs from "Since: 13.0.0" to "XSince: REPLACEME". - Extract hb_vector_draw_render_svg() helper to match the hb_vector_draw_render_pdf() pattern; move the misplaced hb_vector_draw_render doc comment to the actual function and update it to say "output blob" since the renderer is no longer SVG-only. - Revert 14.0.0 NEWS entry edits; the API rename will be announced in the next release entry. - Update docs/harfbuzz-sections.txt to reflect the API renames. - hb-vector tool: expose -O/--output-format with supported formats "svg/pdf", validate format, update description string, and switch PDF detection to g_ascii_strcasecmp for case-insensitive matching. Assisted-by: Claude Opus 4.6 (1M context) --- NEWS | 8 +-- docs/harfbuzz-sections.txt | 8 +-- src/hb-vector-draw.cc | 142 +++++++++++++++++++------------------ src/hb-vector-draw.hh | 2 +- src/hb-vector-paint.cc | 4 +- src/hb-vector-paint.hh | 2 +- util/vector-output.hh | 24 ++++++- 7 files changed, 106 insertions(+), 84 deletions(-) diff --git a/NEWS b/NEWS index 90dff9baf..460737c2c 100644 --- a/NEWS +++ b/NEWS @@ -259,10 +259,10 @@ Wednesday, March 4, 2026 +hb_vector_paint_set_scale_factor() +hb_vector_paint_set_transform() +hb_vector_paint_set_user_data() - +hb_vector_paint_set_flat() - +hb_vector_paint_set_precision() - +hb_vector_draw_set_flat() - +hb_vector_draw_set_precision() + +hb_vector_svg_paint_set_flat() + +hb_vector_svg_paint_set_precision() + +hb_vector_svg_set_flat() + +hb_vector_svg_set_precision() Overview of changes leading to 12.3.2 diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index 30ac24aad..66477f7dd 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -1081,8 +1081,8 @@ hb_vector_draw_get_extents hb_vector_draw_set_glyph_extents hb_vector_draw_get_funcs hb_vector_draw_glyph -hb_vector_svg_set_flat -hb_vector_svg_set_precision +hb_vector_draw_set_flat +hb_vector_draw_set_precision hb_vector_draw_render hb_vector_draw_reset hb_vector_draw_recycle_blob @@ -1105,8 +1105,8 @@ hb_vector_paint_set_custom_palette_color hb_vector_paint_clear_custom_palette_colors hb_vector_paint_get_funcs hb_vector_paint_glyph -hb_vector_svg_paint_set_flat -hb_vector_svg_paint_set_precision +hb_vector_paint_set_flat +hb_vector_paint_set_precision hb_vector_paint_render hb_vector_paint_reset hb_vector_paint_recycle_blob diff --git a/src/hb-vector-draw.cc b/src/hb-vector-draw.cc index 6cff3c9e7..9103afcb2 100644 --- a/src/hb-vector-draw.cc +++ b/src/hb-vector-draw.cc @@ -670,7 +670,7 @@ hb_vector_draw_glyph (hb_vector_draw_t *draw, * * Enables or disables draw flattening. * - * Since: 13.0.0 + * XSince: REPLACEME */ void hb_vector_draw_set_flat (hb_vector_draw_t *draw, @@ -686,7 +686,7 @@ hb_vector_draw_set_flat (hb_vector_draw_t *draw, * * Sets numeric output precision for draw output. * - * Since: 13.0.0 + * XSince: REPLACEME */ void hb_vector_draw_set_precision (hb_vector_draw_t *draw, @@ -695,16 +695,6 @@ hb_vector_draw_set_precision (hb_vector_draw_t *draw, draw->precision = hb_min (precision, 12u); } -/** - * hb_vector_draw_render: - * @draw: a draw context. - * - * Renders accumulated draw content to an SVG blob. - * - * Return value: (transfer full) (nullable): output blob, or `NULL` if rendering cannot proceed. - * - * Since: 13.0.0 - */ static hb_blob_t * hb_vector_draw_render_pdf (hb_vector_draw_t *draw) { @@ -798,71 +788,85 @@ hb_vector_draw_render_pdf (hb_vector_draw_t *draw) return blob; } +static hb_blob_t * +hb_vector_draw_render_svg (hb_vector_draw_t *draw) +{ + if (!draw->has_extents) + return nullptr; + + hb_vector_t out; + hb_buf_recover_recycled (draw->recycled_blob, &out); + unsigned estimated = draw->defs.length + + (draw->body.length ? draw->body.length : draw->path.length) + + 256; + out.alloc (estimated); + hb_buf_append_str (&out, "extents.x, draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, draw->extents.y, draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, draw->extents.width, draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, draw->extents.height, draw->precision); + hb_buf_append_str (&out, "\" width=\""); + hb_buf_append_num (&out, draw->extents.width, draw->precision); + hb_buf_append_str (&out, "\" height=\""); + hb_buf_append_num (&out, draw->extents.height, draw->precision); + hb_buf_append_str (&out, "\">\n"); + + if (draw->defs.length) + { + hb_buf_append_str (&out, "\n"); + hb_buf_append_len (&out, draw->defs.arrayZ, draw->defs.length); + hb_buf_append_str (&out, "\n"); + } + + if (draw->body.length) + { + hb_buf_append_len (&out, draw->body.arrayZ, draw->body.length); + } + else if (draw->path.length) + { + hb_buf_append_str (&out, "path.arrayZ, draw->path.length); + hb_buf_append_str (&out, "\"/>\n"); + } + + hb_buf_append_str (&out, "\n"); + + hb_blob_t *blob = hb_buf_blob_from (&draw->recycled_blob, &out); + + draw->path.clear (); + draw->defs.clear (); + draw->body.clear (); + hb_set_clear (draw->defined_glyphs); + draw->has_extents = false; + draw->extents = {0, 0, 0, 0}; + + return blob; +} + +/** + * hb_vector_draw_render: + * @draw: a draw context. + * + * Renders accumulated draw content to an output blob. + * + * Return value: (transfer full) (nullable): output blob, or `NULL` if rendering cannot proceed. + * + * Since: 13.0.0 + */ hb_blob_t * hb_vector_draw_render (hb_vector_draw_t *draw) { switch (draw->format) { + case HB_VECTOR_FORMAT_SVG: + return hb_vector_draw_render_svg (draw); + case HB_VECTOR_FORMAT_PDF: return hb_vector_draw_render_pdf (draw); - case HB_VECTOR_FORMAT_SVG: - { - if (!draw->has_extents) - return nullptr; - - hb_vector_t out; - hb_buf_recover_recycled (draw->recycled_blob, &out); - unsigned estimated = draw->defs.length + - (draw->body.length ? draw->body.length : draw->path.length) + - 256; - out.alloc (estimated); - hb_buf_append_str (&out, "extents.x, draw->precision); - hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, draw->extents.y, draw->precision); - hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, draw->extents.width, draw->precision); - hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, draw->extents.height, draw->precision); - hb_buf_append_str (&out, "\" width=\""); - hb_buf_append_num (&out, draw->extents.width, draw->precision); - hb_buf_append_str (&out, "\" height=\""); - hb_buf_append_num (&out, draw->extents.height, draw->precision); - hb_buf_append_str (&out, "\">\n"); - - if (draw->defs.length) - { - hb_buf_append_str (&out, "\n"); - hb_buf_append_len (&out, draw->defs.arrayZ, draw->defs.length); - hb_buf_append_str (&out, "\n"); - } - - if (draw->body.length) - { - hb_buf_append_len (&out, draw->body.arrayZ, draw->body.length); - } - else if (draw->path.length) - { - hb_buf_append_str (&out, "path.arrayZ, draw->path.length); - hb_buf_append_str (&out, "\"/>\n"); - } - - hb_buf_append_str (&out, "\n"); - - hb_blob_t *blob = hb_buf_blob_from (&draw->recycled_blob, &out); - - draw->path.clear (); - draw->defs.clear (); - draw->body.clear (); - hb_set_clear (draw->defined_glyphs); - draw->has_extents = false; - draw->extents = {0, 0, 0, 0}; - - return blob; - } - case HB_VECTOR_FORMAT_INVALID: default: return nullptr; } diff --git a/src/hb-vector-draw.hh b/src/hb-vector-draw.hh index de14cfb7b..6f7603c33 100644 --- a/src/hb-vector-draw.hh +++ b/src/hb-vector-draw.hh @@ -13,7 +13,7 @@ struct hb_vector_draw_t { hb_object_header_t header; - hb_vector_format_t format = HB_VECTOR_FORMAT_SVG; + hb_vector_format_t format = HB_VECTOR_FORMAT_INVALID; hb_transform_t<> transform = {1, 0, 0, 1, 0, 0}; float x_scale_factor = 1.f; float y_scale_factor = 1.f; diff --git a/src/hb-vector-paint.cc b/src/hb-vector-paint.cc index 175d69f9d..0342fa4c3 100644 --- a/src/hb-vector-paint.cc +++ b/src/hb-vector-paint.cc @@ -1490,7 +1490,7 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint, * * Enables or disables paint flattening. * - * Since: 13.0.0 + * XSince: REPLACEME */ void hb_vector_paint_set_flat (hb_vector_paint_t *paint, @@ -1506,7 +1506,7 @@ hb_vector_paint_set_flat (hb_vector_paint_t *paint, * * Sets numeric output precision for paint output. * - * Since: 13.0.0 + * XSince: REPLACEME */ void hb_vector_paint_set_precision (hb_vector_paint_t *paint, diff --git a/src/hb-vector-paint.hh b/src/hb-vector-paint.hh index 0b9ea9eef..befd88d8f 100644 --- a/src/hb-vector-paint.hh +++ b/src/hb-vector-paint.hh @@ -40,7 +40,7 @@ struct hb_vector_paint_t { hb_object_header_t header; - hb_vector_format_t format = HB_VECTOR_FORMAT_SVG; + hb_vector_format_t format = HB_VECTOR_FORMAT_INVALID; hb_transform_t<> transform = {1, 0, 0, 1, 0, 0}; float x_scale_factor = 1.f; float y_scale_factor = 1.f; diff --git a/util/vector-output.hh b/util/vector-output.hh index f8f3f6fd1..c1d22bf0c 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -36,6 +36,12 @@ #include "hb-ot.h" +static const char *vector_supported_formats[] = { + "svg", + "pdf", + nullptr +}; + struct vector_output_t : output_options_t<>, view_options_t { static const bool repeat_shape = false; @@ -49,9 +55,9 @@ struct vector_output_t : output_options_t<>, view_options_t void add_options (option_parser_t *parser) { parser->set_summary ("Draw text with given font."); - parser->set_description ("Shows shaped glyph outlines as SVG."); + parser->set_description ("Shows shaped glyph outlines as SVG or PDF."); - output_options_t::add_options (parser); + output_options_t::add_options (parser, vector_supported_formats); view_options_t::add_options (parser); GOptionEntry entries[] = @@ -80,6 +86,18 @@ struct vector_output_t : output_options_t<>, view_options_t return; } + if (output_format && + g_ascii_strcasecmp (output_format, "svg") != 0 && + g_ascii_strcasecmp (output_format, "pdf") != 0) + { + char *items = g_strjoinv ("/", const_cast (vector_supported_formats)); + g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, + "Unknown output format `%s'; supported formats are: %s", + output_format, items); + g_free (items); + return; + } + foreground = HB_COLOR (foreground_color.b, foreground_color.g, foreground_color.r, @@ -180,7 +198,7 @@ struct vector_output_t : output_options_t<>, view_options_t final_extents = extents; hb_vector_format_t fmt = HB_VECTOR_FORMAT_SVG; - if (output_format && !strcmp (output_format, "pdf")) + if (output_format && g_ascii_strcasecmp (output_format, "pdf") == 0) fmt = HB_VECTOR_FORMAT_PDF; hb_vector_draw_t *draw = hb_vector_draw_create_or_fail (fmt); From 90741274fcc790736a1f2a7db22ddc0264946c9f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 11 Apr 2026 22:06:35 -0600 Subject: [PATCH 23/23] [vector] Extract hb_vector_paint_render_svg helper Move the SVG case out of the hb_vector_paint_render switch into its own static helper, matching the hb_vector_paint_render_pdf and hb_vector_draw_render_{svg,pdf} pattern. Assisted-by: Claude Opus 4.6 (1M context) --- src/hb-vector-paint.cc | 102 +++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 49 deletions(-) diff --git a/src/hb-vector-paint.cc b/src/hb-vector-paint.cc index 0342fa4c3..09dc11a7b 100644 --- a/src/hb-vector-paint.cc +++ b/src/hb-vector-paint.cc @@ -1544,11 +1544,60 @@ hb_vector_paint_clear_render_state (hb_vector_paint_t *paint) paint->captured_scratch.clear (); } +static hb_blob_t * +hb_vector_paint_render_svg (hb_vector_paint_t *paint) +{ + if (!paint->has_extents) + return nullptr; + + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return nullptr; + + hb_vector_t out; + hb_buf_recover_recycled (paint->recycled_blob, &out); + unsigned estimated = paint->defs.length + + paint->group_stack.arrayZ[0].length + + 320; + out.alloc (estimated); + hb_buf_append_str (&out, "extents.x, paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, paint->extents.y, paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, paint->extents.width, paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, paint->extents.height, paint->precision); + hb_buf_append_str (&out, "\" width=\""); + hb_buf_append_num (&out, paint->extents.width, paint->precision); + hb_buf_append_str (&out, "\" height=\""); + hb_buf_append_num (&out, paint->extents.height, paint->precision); + hb_buf_append_str (&out, "\">\n"); + + if (paint->defs.length) + { + hb_buf_append_str (&out, "\n"); + hb_buf_append_len (&out, paint->defs.arrayZ, paint->defs.length); + hb_buf_append_str (&out, "\n"); + } + + hb_svg_paint_append_global_transform_prefix (paint, &out); + hb_buf_append_len (&out, paint->group_stack.arrayZ[0].arrayZ, paint->group_stack.arrayZ[0].length); + hb_svg_paint_append_global_transform_suffix (paint, &out); + + hb_buf_append_str (&out, "\n"); + + hb_blob_t *blob = hb_buf_blob_from (&paint->recycled_blob, &out); + + hb_vector_paint_clear_render_state (paint); + + return blob; +} + /** * hb_vector_paint_render: * @paint: a paint context. * - * Renders accumulated paint content to an SVG blob. + * Renders accumulated paint content to an output blob. * * Return value: (transfer full) (nullable): output blob, or `NULL` if rendering cannot proceed. * @@ -1559,57 +1608,12 @@ hb_vector_paint_render (hb_vector_paint_t *paint) { switch (paint->format) { + case HB_VECTOR_FORMAT_SVG: + return hb_vector_paint_render_svg (paint); + case HB_VECTOR_FORMAT_PDF: return hb_vector_paint_render_pdf (paint); - case HB_VECTOR_FORMAT_SVG: - { - if (!paint->has_extents) - return nullptr; - - if (unlikely (!hb_vector_paint_ensure_initialized (paint))) - return nullptr; - - hb_vector_t out; - hb_buf_recover_recycled (paint->recycled_blob, &out); - unsigned estimated = paint->defs.length + - paint->group_stack.arrayZ[0].length + - 320; - out.alloc (estimated); - hb_buf_append_str (&out, "extents.x, paint->precision); - hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, paint->extents.y, paint->precision); - hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, paint->extents.width, paint->precision); - hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, paint->extents.height, paint->precision); - hb_buf_append_str (&out, "\" width=\""); - hb_buf_append_num (&out, paint->extents.width, paint->precision); - hb_buf_append_str (&out, "\" height=\""); - hb_buf_append_num (&out, paint->extents.height, paint->precision); - hb_buf_append_str (&out, "\">\n"); - - if (paint->defs.length) - { - hb_buf_append_str (&out, "\n"); - hb_buf_append_len (&out, paint->defs.arrayZ, paint->defs.length); - hb_buf_append_str (&out, "\n"); - } - - hb_svg_paint_append_global_transform_prefix (paint, &out); - hb_buf_append_len (&out, paint->group_stack.arrayZ[0].arrayZ, paint->group_stack.arrayZ[0].length); - hb_svg_paint_append_global_transform_suffix (paint, &out); - - hb_buf_append_str (&out, "\n"); - - hb_blob_t *blob = hb_buf_blob_from (&paint->recycled_blob, &out); - - hb_vector_paint_clear_render_state (paint); - - return blob; - } - case HB_VECTOR_FORMAT_INVALID: default: return nullptr; }