From b7e4f5fd62753807b1caecab3aca37ba5749b6f3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 11 Mar 2026 14:12:43 -0300 Subject: [PATCH 01/14] [raster] Support gzip-compressed SVG documents The raster SVG path treated hb_ot_color_glyph_reference_svg() blobs as plain XML, even though SVG table documents may legally be gzip encoded. That caused hb-raster to reject fonts that store compressed SVG documents. Detect gzip payloads in the raster SVG renderer, reject documents whose advertised uncompressed size exceeds HB_SVG_MAX_DOCUMENT_SIZE, and inflate the rest through zlib while keeping the same size cap enforced during decompression. Plain SVG documents are also bounded by the same 16 MiB limit. Add a raster API regression that renders the existing plain and compressed noto_handwriting SVG fixtures and verifies identical output. Testing: - meson test -C build Assisted-by: OpenAI Codex --- meson.build | 5 ++ src/hb-limits.hh | 4 + src/hb-raster-svg-render.cc | 170 ++++++++++++++++++++++++++++++++++-- src/meson.build | 3 + test/api/test-raster.cc | 66 ++++++++++++++ 5 files changed, 243 insertions(+), 5 deletions(-) diff --git a/meson.build b/meson.build index cce0ef760..8c59d195d 100644 --- a/meson.build +++ b/meson.build @@ -160,6 +160,7 @@ endif chafa_dep = dependency('chafa', version: chafa_min_version, required: get_option('chafa')) png_dep = dependency('libpng', required: false) +zlib_dep = dependency('zlib', required: get_option('raster')) fontations_dep_found = false if get_option('fontations').enabled() @@ -223,6 +224,10 @@ if png_dep.found() conf.set('HAVE_PNG', 1) endif +if zlib_dep.found() + conf.set('HAVE_ZLIB', 1) +endif + if fontations_dep_found conf.set('HAVE_FONTATIONS', 1) endif diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 5b9954d98..c115f9a86 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -112,6 +112,10 @@ #define HB_SVG_MAX_PATH_SEGMENTS 262144 #endif +#ifndef HB_SVG_MAX_DOCUMENT_SIZE +#define HB_SVG_MAX_DOCUMENT_SIZE ((size_t) 16 << 20) +#endif + #ifndef HB_RASTER_MAX_BUFFER_SIZE #define HB_RASTER_MAX_BUFFER_SIZE ((size_t) 1 << 24) #endif diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index 3e557d13b..268a2b6b6 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -40,11 +40,124 @@ #include #include +#ifdef HAVE_ZLIB +#include +#endif #ifndef HB_NO_RASTER_SVG #define SVG_MAX_DEPTH 32 +static inline bool +hb_raster_svg_blob_is_gzip (const char *data, + unsigned data_len) +{ + return data_len >= 3 && + (unsigned char) data[0] == 0x1Fu && + (unsigned char) data[1] == 0x8Bu && + (unsigned char) data[2] == 0x08u; +} + +static inline bool +hb_raster_svg_gzip_get_uncompressed_size (const char *data, + unsigned data_len, + uint32_t *size) +{ + if (data_len < 4) + return false; + + const unsigned char *trailer = (const unsigned char *) data + data_len - 4; + if (size) + *size = (uint32_t) trailer[0] | + ((uint32_t) trailer[1] << 8) | + ((uint32_t) trailer[2] << 16) | + ((uint32_t) trailer[3] << 24); + return true; +} + +#ifdef HAVE_ZLIB +static hb_blob_t * +hb_raster_svg_decompress_blob (hb_blob_t *blob) +{ + unsigned compressed_len = 0; + const uint8_t *compressed = (const uint8_t *) hb_blob_get_data (blob, &compressed_len); + if (!compressed || !compressed_len) + return nullptr; + + z_stream stream = {}; + stream.next_in = (Bytef *) compressed; + stream.avail_in = compressed_len; + + if (inflateInit2 (&stream, 16 + MAX_WBITS) != Z_OK) + return nullptr; + + uint32_t expected_size = 0; + hb_raster_svg_gzip_get_uncompressed_size ((const char *) compressed, + compressed_len, + &expected_size); + + size_t allocated = hb_min ((size_t) hb_max (expected_size, 4096u), + (size_t) HB_SVG_MAX_DOCUMENT_SIZE); + char *output = (char *) hb_malloc (allocated); + if (!output) + { + inflateEnd (&stream); + return nullptr; + } + + int status = Z_OK; + while (true) + { + size_t produced = (size_t) stream.total_out; + if (unlikely (produced >= (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) + goto fail; + + if (produced == allocated) + { + size_t new_allocated = hb_min (allocated * 2, (size_t) HB_SVG_MAX_DOCUMENT_SIZE); + if (unlikely (new_allocated <= allocated)) + goto fail; + + char *new_output = (char *) hb_realloc (output, new_allocated); + if (unlikely (!new_output)) + goto fail; + + output = new_output; + allocated = new_allocated; + } + + stream.next_out = (Bytef *) output + stream.total_out; + stream.avail_out = (uInt) (allocated - (size_t) stream.total_out); + + status = inflate (&stream, Z_FINISH); + if (status == Z_STREAM_END) + break; + + if ((status == Z_OK || status == Z_BUF_ERROR) && stream.avail_out == 0) + continue; + + goto fail; + } + + inflateEnd (&stream); + + if (unlikely ((size_t) stream.total_out > (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) + goto fail_free; + + return hb_blob_create_or_fail (output, + (unsigned) stream.total_out, + HB_MEMORY_MODE_WRITABLE, + output, + hb_free); + +fail: + inflateEnd (&stream); +fail_free: + hb_free (output); + return nullptr; +} +#endif + /* * 11. Element renderer — recursive SVG rendering */ @@ -433,9 +546,55 @@ hb_raster_svg_render (hb_raster_paint_t *paint, unsigned palette, hb_color_t foreground) { - unsigned data_len; - const char *data = hb_blob_get_data (blob, &data_len); - if (!data || !data_len) return false; + hb_blob_t *render_blob = hb_blob_reference (blob); + if (!render_blob) + return false; + + unsigned data_len = 0; + const char *data = hb_blob_get_data (render_blob, &data_len); + if (!data || !data_len) + { + hb_blob_destroy (render_blob); + return false; + } + + if (hb_raster_svg_blob_is_gzip (data, data_len)) + { + uint32_t expected_size = 0; + if (hb_raster_svg_gzip_get_uncompressed_size (data, data_len, &expected_size) && + unlikely ((size_t) expected_size > (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) + { + hb_blob_destroy (render_blob); + return false; + } + +#ifdef HAVE_ZLIB + hb_blob_t *uncompressed = hb_raster_svg_decompress_blob (render_blob); + if (!uncompressed) + { + hb_blob_destroy (render_blob); + return false; + } + + hb_blob_destroy (render_blob); + render_blob = uncompressed; + data = hb_blob_get_data (render_blob, &data_len); + if (!data || !data_len) + { + hb_blob_destroy (render_blob); + return false; + } +#else + hb_blob_destroy (render_blob); + return false; +#endif + } + + if (unlikely ((size_t) data_len > (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) + { + hb_blob_destroy (render_blob); + return false; + } hb_face_t *face HB_UNUSED = hb_font_get_face (font); const OT::SVG::svg_doc_cache_t *doc_cache = nullptr; @@ -447,7 +606,7 @@ hb_raster_svg_render (hb_raster_paint_t *paint, if (face && hb_ot_color_glyph_get_svg_document_index (face, glyph, &doc_index) && hb_ot_color_get_svg_document_glyph_range (face, doc_index, &start_glyph, &end_glyph)) - doc_cache = face->table.SVG->get_or_create_doc_cache (blob, data, data_len, + doc_cache = face->table.SVG->get_or_create_doc_cache (render_blob, data, data_len, doc_index, start_glyph, end_glyph); if (doc_cache) @@ -518,7 +677,7 @@ hb_raster_svg_render (hb_raster_paint_t *paint, if (id.len) { if (id.len == (unsigned) glyph_id_len && - 0 == memcmp (id.data, glyph_id_str, (unsigned) glyph_id_len)) + 0 == hb_memcmp (id.data, glyph_id_str, (unsigned) glyph_id_len)) { hb_paint_push_font_transform (ctx.pfuncs, ctx.paint, font); ctx.push_transform (1, 0, 0, -1, 0, 0); @@ -533,6 +692,7 @@ hb_raster_svg_render (hb_raster_paint_t *paint, } } + hb_blob_destroy (render_blob); return found_glyph; } diff --git a/src/meson.build b/src/meson.build index 67e00c06c..c0195dac1 100644 --- a/src/meson.build +++ b/src/meson.build @@ -764,6 +764,9 @@ if not get_option('raster').disabled() if png_dep.found() raster_deps += [png_dep] endif + if zlib_dep.found() + raster_deps += [zlib_dep] + endif libharfbuzz_raster = library('harfbuzz-raster', hb_raster_sources, include_directories: incconfig, diff --git a/test/api/test-raster.cc b/test/api/test-raster.cc index b32760e56..72d4c3891 100644 --- a/test/api/test-raster.cc +++ b/test/api/test-raster.cc @@ -26,6 +26,7 @@ #include "hb-test.h" #include "hb-raster.h" +#include /* ── Helpers ─────────────────────────────────────────────────────── */ @@ -62,6 +63,32 @@ pixel_at (hb_raster_image_t *img, int x, int y) return hb_raster_image_get_buffer (img)[row * ext.stride + col]; } +static hb_raster_image_t * +render_svg_glyph (hb_face_t *face, + hb_codepoint_t glyph) +{ + g_assert_nonnull (face); + + hb_font_t *font = hb_font_create (face); + g_assert_nonnull (font); + hb_font_set_scale (font, 128, 128); + + hb_raster_paint_t *paint = hb_raster_paint_create_or_fail (); + g_assert_nonnull (paint); + hb_raster_paint_set_foreground (paint, HB_COLOR (0, 0, 0, 255)); + + hb_raster_extents_t extents = {-32, -192, 256, 256, 0}; + hb_raster_paint_set_extents (paint, &extents); + g_assert_true (hb_raster_paint_glyph (paint, font, glyph, 0.f, 0.f, 0, HB_COLOR (0, 0, 0, 255))); + + hb_raster_image_t *img = hb_raster_paint_render (paint); + g_assert_nonnull (img); + + hb_raster_paint_destroy (paint); + hb_font_destroy (font); + return img; +} + /* ── Test 1: rectangle geometry ──────────────────────────────────── */ @@ -224,6 +251,44 @@ test_set_glyph_extents_with_transform (void) hb_raster_draw_destroy (rdr); } +/* ── Test 6: gzip-compressed SVG documents ──────────────────────── */ + +static void +test_svgz_render_matches_svg (void) +{ +#if !defined (HB_NO_SVG) && defined (HAVE_ZLIB) + hb_face_t *svg_face = hb_test_open_font_file ("../fuzzing/fonts/noto_handwriting-untouchedsvg.ttf"); + hb_face_t *svgz_face = hb_test_open_font_file ("../fuzzing/fonts/noto_handwriting-untouchedsvgz.ttf"); + + hb_raster_image_t *svg = render_svg_glyph (svg_face, 7); + hb_raster_image_t *svgz = render_svg_glyph (svgz_face, 7); + + hb_raster_extents_t svg_extents; + hb_raster_extents_t svgz_extents; + hb_raster_image_get_extents (svg, &svg_extents); + hb_raster_image_get_extents (svgz, &svgz_extents); + + g_assert_cmpint (hb_raster_image_get_format (svg), ==, hb_raster_image_get_format (svgz)); + g_assert_cmpint (svg_extents.x_origin, ==, svgz_extents.x_origin); + g_assert_cmpint (svg_extents.y_origin, ==, svgz_extents.y_origin); + g_assert_cmpuint (svg_extents.width, ==, svgz_extents.width); + g_assert_cmpuint (svg_extents.height, ==, svgz_extents.height); + g_assert_cmpuint (svg_extents.stride, ==, svgz_extents.stride); + g_assert_cmpuint (svg_extents.width, >, 0); + g_assert_cmpuint (svg_extents.height, >, 0); + + size_t len = (size_t) svg_extents.stride * svg_extents.height; + g_assert_cmpint (memcmp (hb_raster_image_get_buffer (svg), + hb_raster_image_get_buffer (svgz), + len), ==, 0); + + hb_raster_image_destroy (svg); + hb_raster_image_destroy (svgz); + hb_face_destroy (svg_face); + hb_face_destroy (svgz_face); +#endif +} + /* ── main ────────────────────────────────────────────────────────── */ @@ -237,6 +302,7 @@ main (int argc, char **argv) hb_test_add (test_subpixel_edge); hb_test_add (test_transform); hb_test_add (test_set_glyph_extents_with_transform); + hb_test_add (test_svgz_render_matches_svg); return hb_test_run (); } From 329c733e144b82ceb2755472b2375c8b324fe568 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 11 Mar 2026 12:49:20 -0600 Subject: [PATCH 02/14] [meson] Report zlib in raster dependency summary Show zlib in the configure summary next to libpng for the\nraster library dependencies.\n\nTesting:\n- meson setup build --reconfigure\n\nAssisted-by: OpenAI Codex --- meson.build | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index 8c59d195d..03ad689a4 100644 --- a/meson.build +++ b/meson.build @@ -160,7 +160,7 @@ endif chafa_dep = dependency('chafa', version: chafa_min_version, required: get_option('chafa')) png_dep = dependency('libpng', required: false) -zlib_dep = dependency('zlib', required: get_option('raster')) +zlib_dep = dependency('zlib', required: false) fontations_dep_found = false if get_option('fontations').enabled() @@ -484,6 +484,7 @@ build_summary = { }, 'Dependencies used by additional libraries': {'PNG': conf.get('HAVE_PNG', 0) == 1, + 'Zlib': conf.get('HAVE_ZLIB', 0) == 1, }, 'Other features': {'Utilities': not get_option('utilities').disabled(), From fc5bc355d38d3a9c5573e55c71e5f6c9d19e3380 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 11 Mar 2026 15:57:20 -0300 Subject: [PATCH 03/14] [raster] Simplify SVG render cleanup Simplify hb_raster_svg_render() to use one cleanup path and keep the blob lifetime handling in one place. While doing that, drop dead checks that only depended on hb_blob_reference() returning nullptr or hb_blob_get_data() returning nullptr for non-empty blobs. Testing: - meson test -C build Assisted-by: OpenAI Codex --- src/hb-raster-svg-render.cc | 103 ++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 57 deletions(-) diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index 268a2b6b6..3ef79ba45 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -546,63 +546,55 @@ hb_raster_svg_render (hb_raster_paint_t *paint, unsigned palette, hb_color_t foreground) { + bool ret = false; hb_blob_t *render_blob = hb_blob_reference (blob); - if (!render_blob) - return false; - - unsigned data_len = 0; - const char *data = hb_blob_get_data (render_blob, &data_len); - if (!data || !data_len) - { - hb_blob_destroy (render_blob); - return false; - } - - if (hb_raster_svg_blob_is_gzip (data, data_len)) - { - uint32_t expected_size = 0; - if (hb_raster_svg_gzip_get_uncompressed_size (data, data_len, &expected_size) && - unlikely ((size_t) expected_size > (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) - { - hb_blob_destroy (render_blob); - return false; - } - -#ifdef HAVE_ZLIB - hb_blob_t *uncompressed = hb_raster_svg_decompress_blob (render_blob); - if (!uncompressed) - { - hb_blob_destroy (render_blob); - return false; - } - - hb_blob_destroy (render_blob); - render_blob = uncompressed; - data = hb_blob_get_data (render_blob, &data_len); - if (!data || !data_len) - { - hb_blob_destroy (render_blob); - return false; - } -#else - hb_blob_destroy (render_blob); - return false; -#endif - } - - if (unlikely ((size_t) data_len > (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) - { - hb_blob_destroy (render_blob); - return false; - } - hb_face_t *face HB_UNUSED = hb_font_get_face (font); const OT::SVG::svg_doc_cache_t *doc_cache = nullptr; #ifndef HB_NO_SVG unsigned doc_index = 0; hb_codepoint_t start_glyph = HB_CODEPOINT_INVALID; hb_codepoint_t end_glyph = HB_CODEPOINT_INVALID; +#endif + hb_paint_funcs_t *pfuncs = hb_raster_paint_get_funcs (); + hb_svg_render_context_t ctx; + hb_svg_cascade_t initial_state; + hb_svg_defs_scan_context_t scan_ctx; + bool found_glyph = false; +#ifndef HB_NO_SVG + unsigned glyph_start = 0, glyph_end = 0; +#endif + unsigned data_len = 0; + const char *data = hb_blob_get_data (render_blob, &data_len); + if (!data_len) + goto done; + + if (hb_raster_svg_blob_is_gzip (data, data_len)) + { + uint32_t expected_size = 0; + if (hb_raster_svg_gzip_get_uncompressed_size (data, data_len, &expected_size) && + unlikely ((size_t) expected_size > (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) + goto done; + +#ifdef HAVE_ZLIB + hb_blob_t *uncompressed = hb_raster_svg_decompress_blob (render_blob); + if (!uncompressed) + goto done; + + hb_blob_destroy (render_blob); + render_blob = uncompressed; + data = hb_blob_get_data (render_blob, &data_len); + if (!data_len) + goto done; +#else + goto done; +#endif + } + + if (unlikely ((size_t) data_len > (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) + goto done; + +#ifndef HB_NO_SVG if (face && hb_ot_color_glyph_get_svg_document_index (face, glyph, &doc_index) && hb_ot_color_get_svg_document_glyph_range (face, doc_index, &start_glyph, &end_glyph)) @@ -613,9 +605,6 @@ hb_raster_svg_render (hb_raster_paint_t *paint, data = face->table.SVG->doc_cache_get_svg (doc_cache, &data_len); #endif - hb_paint_funcs_t *pfuncs = hb_raster_paint_get_funcs (); - - hb_svg_render_context_t ctx; ctx.paint = paint; ctx.pfuncs = pfuncs; ctx.font = font; @@ -628,10 +617,9 @@ hb_raster_svg_render (hb_raster_paint_t *paint, #endif ctx.doc_cache = doc_cache; - hb_svg_cascade_t initial_state; initial_state.color = foreground; - hb_svg_defs_scan_context_t scan_ctx = { + scan_ctx = { &ctx.defs, ctx.pfuncs, ctx.paint, ctx.foreground, hb_font_get_face (ctx.font), ctx.palette, @@ -640,9 +628,7 @@ hb_raster_svg_render (hb_raster_paint_t *paint, }; hb_raster_svg_collect_defs (&scan_ctx, data, data_len); - bool found_glyph = false; #ifndef HB_NO_SVG - unsigned glyph_start = 0, glyph_end = 0; if (doc_cache && face->table.SVG->doc_cache_get_glyph_span (doc_cache, glyph, &glyph_start, &glyph_end)) { hb_svg_xml_parser_t parser (data + glyph_start, glyph_end - glyph_start); @@ -692,8 +678,11 @@ hb_raster_svg_render (hb_raster_paint_t *paint, } } + ret = found_glyph; + +done: hb_blob_destroy (render_blob); - return found_glyph; + return ret; } #endif /* !HB_NO_RASTER_SVG */ From 17e40091c70dc7118b8731387592973a55837607 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 11 Mar 2026 15:59:20 -0300 Subject: [PATCH 04/14] [docs] Clarify local agent rules Add explicit local guidance for two recurring issues: use file-backed input for multi-line commit messages, and prefer HarfBuzz helpers from hb-algs.hh over raw libc memory routines where those helpers are available. Also state explicitly that STL containers are not used in library code. Testing: - meson test -C build Assisted-by: OpenAI Codex --- AGENTS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/AGENTS.md b/AGENTS.md index 809056fac..0cdfbe7a7 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -21,6 +21,8 @@ Before non-trivial work, read: - Preserve optional-feature behavior, reduced-feature builds, and compile-time feature guards. - Keep out-of-memory behavior in mind. New code should fail safely and follow existing allocation and error-handling patterns. - Prefer HarfBuzz's established `likely`/`unlikely` and nil-object/null-pattern conventions where they fit the surrounding code. +- Prefer HarfBuzz helpers from `hb-algs.hh` such as `hb_memcpy`, `hb_memcmp`, and `hb_memset` over raw `memcpy`, `memcmp`, and `memset` where those helpers are available. +- Do not introduce STL containers in library code. Follow HarfBuzz container and utility patterns instead. - Leave unrelated user changes and untracked artifacts alone. ## Repo map @@ -93,6 +95,7 @@ For API work: - Before any commit, run the entire test suite with `meson test -C build`. - Use descriptive commit messages with consistent bracketed subsystem prefixes such as `[subset]`, `[raster]`, `[util]`, or `[meson]`. - Wrap commit message bodies to about 70 columns. +- For multi-line commit messages, write the message from a file or editor-backed input. Do not pass escaped `\n` sequences via shell `-m` arguments. - Explain root cause, fix, and testing in the commit body when testing was actually performed or is relevant. - When relevant, link issues or PRs with trailers such as `Fixes:`. - Always include an `Assisted-by:` trailer on commits you write through the agent. From 1e9084874fa7d41a2c6f5d54e3f4d3c5901cacbe Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 11 Mar 2026 16:00:44 -0300 Subject: [PATCH 05/14] [docs] Allow docs-only commits without tests Clarify the local agent guidance so simple documentation-only changes may be committed without running the full test suite, as long as they do not touch code, build logic, generated outputs, or test inputs. Assisted-by: OpenAI Codex --- AGENTS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/AGENTS.md b/AGENTS.md index 0cdfbe7a7..160f4e12f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -93,6 +93,7 @@ For API work: ## Commit guidance - Before any commit, run the entire test suite with `meson test -C build`. +- Exception: simple documentation-only changes may be committed without running tests if they do not affect code, build logic, generated outputs, or test inputs. - Use descriptive commit messages with consistent bracketed subsystem prefixes such as `[subset]`, `[raster]`, `[util]`, or `[meson]`. - Wrap commit message bodies to about 70 columns. - For multi-line commit messages, write the message from a file or editor-backed input. Do not pass escaped `\n` sequences via shell `-m` arguments. From 99f4e9e71917181d46e6a7c5f8c795f5b051e041 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 11 Mar 2026 16:52:20 -0300 Subject: [PATCH 06/14] [ci] Install raster image dependencies on Linux Install libpng-dev and zlib1g-dev explicitly in the Linux GitHub Actions job so raster dependency detection does not rely on transitive packages. Testing: - meson test -C build Assisted-by: OpenAI Codex --- .github/workflows/linux.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index ad0f9aa62..49bd5c4be 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -34,7 +34,9 @@ jobs: libglib2.0-dev \ libgraphite2-dev \ libicu-dev \ + libpng-dev \ pkg-config \ + zlib1g-dev \ help2man - name: Setup Python uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 From e948ac9a3a00918e8354153692056b832123b038 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 11 Mar 2026 16:53:38 -0300 Subject: [PATCH 07/14] [docs] Allow CI-only commits without tests Extend the local AGENTS.md exception so simple CI-only changes may also skip the full test suite, provided they do not affect code, build logic, generated outputs, or test inputs. Assisted-by: OpenAI Codex --- AGENTS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index 160f4e12f..df503412f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -93,7 +93,7 @@ For API work: ## Commit guidance - Before any commit, run the entire test suite with `meson test -C build`. -- Exception: simple documentation-only changes may be committed without running tests if they do not affect code, build logic, generated outputs, or test inputs. +- Exception: simple documentation-only or CI-only changes may be committed without running tests if they do not affect code, build logic, generated outputs, or test inputs. - Use descriptive commit messages with consistent bracketed subsystem prefixes such as `[subset]`, `[raster]`, `[util]`, or `[meson]`. - Wrap commit message bodies to about 70 columns. - For multi-line commit messages, write the message from a file or editor-backed input. Do not pass escaped `\n` sequences via shell `-m` arguments. From 00da68ef5c367f469959e32a388e93b6159cff8f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 11 Mar 2026 12:49:20 -0600 Subject: [PATCH 08/14] [svg] Share svgz normalization across raster and vector Vector SVG extraction still treated SVG table documents as plain text, so svgz payloads worked in raster but failed in hb-vector. Move the gzip helpers into a new internal hb-zlib.hh header and add a shared SVG blob normalization helper in svg.hh. Raster and vector now both normalize SVG documents before doc-cache or rendering/subsetting logic sees them, with the same 16 MiB uncompressed size limit. Also link zlib into harfbuzz-vector when available and add a vector regression that checks matching output for the existing plain and compressed handwriting SVG fonts. Tested: - meson test -C build Assisted-by: OpenAI Codex --- src/OT/Color/svg/svg.hh | 51 ++++++ src/hb-raster-svg-render.cc | 146 +----------------- src/hb-vector-svg-subset.cc | 86 ++++++----- src/hb-zlib.hh | 145 +++++++++++++++++ src/meson.build | 8 +- .../data/expected/basic/handwriting-svg.svg | 11 ++ test/vector/data/tests/basic.tests | 2 + 7 files changed, 270 insertions(+), 179 deletions(-) create mode 100644 src/hb-zlib.hh create mode 100644 test/vector/data/expected/basic/handwriting-svg.svg diff --git a/src/OT/Color/svg/svg.hh b/src/OT/Color/svg/svg.hh index ac70ce17d..4acfa7561 100644 --- a/src/OT/Color/svg/svg.hh +++ b/src/OT/Color/svg/svg.hh @@ -27,8 +27,10 @@ #include "../../../hb-open-type.hh" #include "../../../hb-blob.hh" +#include "../../../hb-limits.hh" #include "../../../hb-map.hh" #include "../../../hb-paint.hh" +#include "../../../hb-zlib.hh" #include #include @@ -562,6 +564,55 @@ parse_cache_entries_linear (const char *svg, } /* namespace _hb_svg_cache_impl */ +static inline hb_blob_t * +hb_ot_svg_reference_normalized_blob (hb_blob_t *image, + const char **svg, + unsigned *len) +{ + hb_blob_t *blob = hb_blob_reference (image); + unsigned data_len = 0; + const char *data = hb_blob_get_data (blob, &data_len); + + if (!data || !data_len) + goto fail; + + if (hb_blob_is_gzip (data, data_len)) + { + uint32_t expected_size = 0; + if (hb_gzip_get_uncompressed_size (data, data_len, &expected_size) && + unlikely ((size_t) expected_size > (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) + goto fail; + +#ifdef HAVE_ZLIB + hb_blob_t *uncompressed = hb_blob_decompress_gzip (blob, + HB_SVG_MAX_DOCUMENT_SIZE); + if (!uncompressed) + goto fail; + + hb_blob_destroy (blob); + blob = uncompressed; + data = hb_blob_get_data (blob, &data_len); + if (!data || !data_len) + goto fail; +#else + goto fail; +#endif + } + + if (unlikely ((size_t) data_len > (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) + goto fail; + + if (svg) *svg = data; + if (len) *len = data_len; + return blob; + +fail: + hb_blob_destroy (blob); + if (svg) *svg = nullptr; + if (len) *len = 0; + return nullptr; +} + inline SVG::accelerator_t::accelerator_t (hb_face_t *face) { diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index 3ef79ba45..96c58335f 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -37,127 +37,15 @@ #include "hb-raster-svg-bbox.hh" #include "hb-raster-svg-fill.hh" #include "hb-raster-svg-use.hh" +#include "OT/Color/svg/svg.hh" #include #include -#ifdef HAVE_ZLIB -#include -#endif #ifndef HB_NO_RASTER_SVG #define SVG_MAX_DEPTH 32 -static inline bool -hb_raster_svg_blob_is_gzip (const char *data, - unsigned data_len) -{ - return data_len >= 3 && - (unsigned char) data[0] == 0x1Fu && - (unsigned char) data[1] == 0x8Bu && - (unsigned char) data[2] == 0x08u; -} - -static inline bool -hb_raster_svg_gzip_get_uncompressed_size (const char *data, - unsigned data_len, - uint32_t *size) -{ - if (data_len < 4) - return false; - - const unsigned char *trailer = (const unsigned char *) data + data_len - 4; - if (size) - *size = (uint32_t) trailer[0] | - ((uint32_t) trailer[1] << 8) | - ((uint32_t) trailer[2] << 16) | - ((uint32_t) trailer[3] << 24); - return true; -} - -#ifdef HAVE_ZLIB -static hb_blob_t * -hb_raster_svg_decompress_blob (hb_blob_t *blob) -{ - unsigned compressed_len = 0; - const uint8_t *compressed = (const uint8_t *) hb_blob_get_data (blob, &compressed_len); - if (!compressed || !compressed_len) - return nullptr; - - z_stream stream = {}; - stream.next_in = (Bytef *) compressed; - stream.avail_in = compressed_len; - - if (inflateInit2 (&stream, 16 + MAX_WBITS) != Z_OK) - return nullptr; - - uint32_t expected_size = 0; - hb_raster_svg_gzip_get_uncompressed_size ((const char *) compressed, - compressed_len, - &expected_size); - - size_t allocated = hb_min ((size_t) hb_max (expected_size, 4096u), - (size_t) HB_SVG_MAX_DOCUMENT_SIZE); - char *output = (char *) hb_malloc (allocated); - if (!output) - { - inflateEnd (&stream); - return nullptr; - } - - int status = Z_OK; - while (true) - { - size_t produced = (size_t) stream.total_out; - if (unlikely (produced >= (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) - goto fail; - - if (produced == allocated) - { - size_t new_allocated = hb_min (allocated * 2, (size_t) HB_SVG_MAX_DOCUMENT_SIZE); - if (unlikely (new_allocated <= allocated)) - goto fail; - - char *new_output = (char *) hb_realloc (output, new_allocated); - if (unlikely (!new_output)) - goto fail; - - output = new_output; - allocated = new_allocated; - } - - stream.next_out = (Bytef *) output + stream.total_out; - stream.avail_out = (uInt) (allocated - (size_t) stream.total_out); - - status = inflate (&stream, Z_FINISH); - if (status == Z_STREAM_END) - break; - - if ((status == Z_OK || status == Z_BUF_ERROR) && stream.avail_out == 0) - continue; - - goto fail; - } - - inflateEnd (&stream); - - if (unlikely ((size_t) stream.total_out > (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) - goto fail_free; - - return hb_blob_create_or_fail (output, - (unsigned) stream.total_out, - HB_MEMORY_MODE_WRITABLE, - output, - hb_free); - -fail: - inflateEnd (&stream); -fail_free: - hb_free (output); - return nullptr; -} -#endif - /* * 11. Element renderer — recursive SVG rendering */ @@ -547,7 +435,7 @@ hb_raster_svg_render (hb_raster_paint_t *paint, hb_color_t foreground) { bool ret = false; - hb_blob_t *render_blob = hb_blob_reference (blob); + hb_blob_t *render_blob = nullptr; hb_face_t *face HB_UNUSED = hb_font_get_face (font); const OT::SVG::svg_doc_cache_t *doc_cache = nullptr; #ifndef HB_NO_SVG @@ -565,33 +453,9 @@ hb_raster_svg_render (hb_raster_paint_t *paint, #endif unsigned data_len = 0; - const char *data = hb_blob_get_data (render_blob, &data_len); - if (!data_len) - goto done; - - if (hb_raster_svg_blob_is_gzip (data, data_len)) - { - uint32_t expected_size = 0; - if (hb_raster_svg_gzip_get_uncompressed_size (data, data_len, &expected_size) && - unlikely ((size_t) expected_size > (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) - goto done; - -#ifdef HAVE_ZLIB - hb_blob_t *uncompressed = hb_raster_svg_decompress_blob (render_blob); - if (!uncompressed) - goto done; - - hb_blob_destroy (render_blob); - render_blob = uncompressed; - data = hb_blob_get_data (render_blob, &data_len); - if (!data_len) - goto done; -#else - goto done; -#endif - } - - if (unlikely ((size_t) data_len > (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) + const char *data = nullptr; + render_blob = OT::hb_ot_svg_reference_normalized_blob (blob, &data, &data_len); + if (!render_blob || !data_len) goto done; #ifndef HB_NO_SVG diff --git a/src/hb-vector-svg-subset.cc b/src/hb-vector-svg-subset.cc index 3cc06a916..3d1e56227 100644 --- a/src/hb-vector-svg-subset.cc +++ b/src/hb-vector-svg-subset.cc @@ -262,47 +262,55 @@ hb_svg_subset_glyph_image (hb_face_t *face, hb_vector_t *defs_dst, hb_vector_t *body_dst) { - if (glyph == HB_CODEPOINT_INVALID || !image_counter || !defs_dst || !body_dst) - return false; - - unsigned len; - const char *svg = hb_blob_get_data (image, &len); - if (!svg || !len) - return false; - + bool ret = false; + hb_blob_t *normalized_image = nullptr; + unsigned len = 0; + const char *svg = nullptr; unsigned doc_index = 0; - if (!hb_ot_color_glyph_get_svg_document_index (face, glyph, &doc_index)) - return false; - hb_codepoint_t start_glyph = HB_CODEPOINT_INVALID; hb_codepoint_t end_glyph = HB_CODEPOINT_INVALID; - if (!hb_ot_color_get_svg_document_glyph_range (face, doc_index, &start_glyph, &end_glyph)) - return false; + const OT::SVG::svg_doc_cache_t *doc_cache = nullptr; + unsigned glyph_start = 0, glyph_end = 0; + const hb_vector_t *defs_entries = nullptr; + hb_vector_t needed_ids; + hb_hashmap_t needed_ids_set; + hb_vector_t chosen_defs; + hb_vector_t chosen_def_marks; + char prefix[32]; + int prefix_len = 0; - auto *doc_cache = face->table.SVG->get_or_create_doc_cache (image, svg, len, - doc_index, start_glyph, end_glyph); + if (glyph == HB_CODEPOINT_INVALID || !image_counter || !defs_dst || !body_dst) + goto done; + + normalized_image = OT::hb_ot_svg_reference_normalized_blob (image, &svg, &len); + if (!normalized_image || !svg || !len) + goto done; + + if (!hb_ot_color_glyph_get_svg_document_index (face, glyph, &doc_index)) + goto done; + + if (!hb_ot_color_get_svg_document_glyph_range (face, doc_index, &start_glyph, &end_glyph)) + goto done; + + doc_cache = face->table.SVG->get_or_create_doc_cache (normalized_image, svg, len, + doc_index, start_glyph, end_glyph); if (!doc_cache) - return false; + goto done; svg = face->table.SVG->doc_cache_get_svg (doc_cache, &len); - unsigned glyph_start = 0, glyph_end = 0; if (!face->table.SVG->doc_cache_get_glyph_span (doc_cache, glyph, &glyph_start, &glyph_end)) - return false; + goto done; - auto *defs_entries = face->table.SVG->doc_cache_get_defs_entries (doc_cache); + defs_entries = face->table.SVG->doc_cache_get_defs_entries (doc_cache); - hb_vector_t needed_ids; needed_ids.alloc (16); - hb_hashmap_t needed_ids_set; if (!hb_svg_collect_refs (svg + glyph_start, glyph_end - glyph_start, &needed_ids, &needed_ids_set)) - return false; + goto done; - hb_vector_t chosen_defs; chosen_defs.alloc (16); - hb_vector_t chosen_def_marks; if (unlikely (!chosen_def_marks.resize ((int) defs_entries->length))) - return false; + goto done; hb_memset (chosen_def_marks.arrayZ, 0, defs_entries->length); for (unsigned qi = 0; qi < needed_ids.length; qi++) { @@ -316,20 +324,19 @@ hb_svg_subset_glyph_image (hb_face_t *face, { chosen_def_marks.arrayZ[i] = 1; if (unlikely (!chosen_defs.push_or_fail (i))) - return false; + goto done; if (!hb_svg_collect_refs (svg + e.start, e.end - e.start, &needed_ids, &needed_ids_set)) - return false; + goto done; } break; } } } - char prefix[32]; - int prefix_len = snprintf (prefix, sizeof (prefix), "hbimg%u_", (*image_counter)++); + prefix_len = snprintf (prefix, sizeof (prefix), "hbimg%u_", (*image_counter)++); if (prefix_len <= 0 || (unsigned) prefix_len >= sizeof (prefix)) - return false; + goto done; body_dst->alloc (body_dst->length + (glyph_end - glyph_start) + (unsigned) prefix_len + 32); @@ -337,15 +344,20 @@ hb_svg_subset_glyph_image (hb_face_t *face, { const auto &e = defs_entries->arrayZ[chosen_defs.arrayZ[i]]; if (!hb_svg_append_with_prefix (defs_dst, svg + e.start, e.end - e.start, prefix, (unsigned) prefix_len)) - return false; - if (!hb_svg_append_c (defs_dst, '\n')) return false; + goto done; + if (!hb_svg_append_c (defs_dst, '\n')) + goto done; } - return hb_svg_append_with_prefix (body_dst, - svg + glyph_start, - glyph_end - glyph_start, - prefix, - (unsigned) prefix_len); + ret = hb_svg_append_with_prefix (body_dst, + svg + glyph_start, + glyph_end - glyph_start, + prefix, + (unsigned) prefix_len); + +done: + hb_blob_destroy (normalized_image); + return ret; } #else bool diff --git a/src/hb-zlib.hh b/src/hb-zlib.hh new file mode 100644 index 000000000..d9a26ed51 --- /dev/null +++ b/src/hb-zlib.hh @@ -0,0 +1,145 @@ +/* + * 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. + */ + +#ifndef HB_ZLIB_HH +#define HB_ZLIB_HH + +#include "hb-blob.hh" + +#ifdef HAVE_ZLIB +#include +#endif + +static inline bool +hb_blob_is_gzip (const char *data, + unsigned data_len) +{ + return data_len >= 3 && + (unsigned char) data[0] == 0x1Fu && + (unsigned char) data[1] == 0x8Bu && + (unsigned char) data[2] == 0x08u; +} + +static inline bool +hb_gzip_get_uncompressed_size (const char *data, + unsigned data_len, + uint32_t *size) +{ + if (data_len < 4) + return false; + + const unsigned char *trailer = (const unsigned char *) data + data_len - 4; + if (size) + *size = (uint32_t) trailer[0] | + ((uint32_t) trailer[1] << 8) | + ((uint32_t) trailer[2] << 16) | + ((uint32_t) trailer[3] << 24); + return true; +} + +#ifdef HAVE_ZLIB +static inline hb_blob_t * +hb_blob_decompress_gzip (hb_blob_t *blob, + unsigned max_output_len) +{ + unsigned compressed_len = 0; + const uint8_t *compressed = (const uint8_t *) hb_blob_get_data (blob, &compressed_len); + if (!compressed || !compressed_len) + return nullptr; + + z_stream stream = {}; + stream.next_in = (Bytef *) compressed; + stream.avail_in = compressed_len; + + if (inflateInit2 (&stream, 16 + MAX_WBITS) != Z_OK) + return nullptr; + + uint32_t expected_size = 0; + hb_gzip_get_uncompressed_size ((const char *) compressed, + compressed_len, + &expected_size); + + size_t allocated = hb_min ((size_t) hb_max (expected_size, 4096u), + (size_t) max_output_len); + char *output = (char *) hb_malloc (allocated); + if (!output) + { + inflateEnd (&stream); + return nullptr; + } + + int status = Z_OK; + while (true) + { + size_t produced = (size_t) stream.total_out; + if (unlikely (produced >= (size_t) max_output_len)) + goto fail; + + if (produced == allocated) + { + size_t new_allocated = hb_min (allocated * 2, (size_t) max_output_len); + if (unlikely (new_allocated <= allocated)) + goto fail; + + char *new_output = (char *) hb_realloc (output, new_allocated); + if (unlikely (!new_output)) + goto fail; + + output = new_output; + allocated = new_allocated; + } + + stream.next_out = (Bytef *) output + stream.total_out; + stream.avail_out = (uInt) (allocated - (size_t) stream.total_out); + + status = inflate (&stream, Z_FINISH); + if (status == Z_STREAM_END) + break; + + if ((status == Z_OK || status == Z_BUF_ERROR) && stream.avail_out == 0) + continue; + + goto fail; + } + + inflateEnd (&stream); + + if (unlikely ((size_t) stream.total_out > (size_t) max_output_len)) + goto fail_free; + + return hb_blob_create_or_fail (output, + (unsigned) stream.total_out, + HB_MEMORY_MODE_WRITABLE, + output, + hb_free); + +fail: + inflateEnd (&stream); +fail_free: + hb_free (output); + return nullptr; +} +#endif + +#endif /* HB_ZLIB_HH */ diff --git a/src/meson.build b/src/meson.build index c0195dac1..90c5618e8 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-zlib.hh', 'hb.hh', ) @@ -804,9 +805,14 @@ endif libharfbuzz_vector_dep = null_dep if not get_option('vector').disabled() + vector_deps = [m_dep] + if zlib_dep.found() + vector_deps += [zlib_dep] + endif + libharfbuzz_vector = library('harfbuzz-vector', hb_vector_sources, include_directories: incconfig, - dependencies: [m_dep], + dependencies: vector_deps, link_with: [libharfbuzz], cpp_args: cpp_args + extra_hb_cpp_args, soversion: hb_so_version, diff --git a/test/vector/data/expected/basic/handwriting-svg.svg b/test/vector/data/expected/basic/handwriting-svg.svg new file mode 100644 index 000000000..1bc6486b4 --- /dev/null +++ b/test/vector/data/expected/basic/handwriting-svg.svg @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/test/vector/data/tests/basic.tests b/test/vector/data/tests/basic.tests index 3cedc3904..32394776a 100644 --- a/test/vector/data/tests/basic.tests +++ b/test/vector/data/tests/basic.tests @@ -13,3 +13,5 @@ ../../../api/fonts/AdobeVFPrototype.abc.otf;--variations=wght=900,CNTR=100;U+0061,U+0062,U+0063;../expected/basic/cff2-var-cntr.svg ../../../api/fonts/SourceSerifVariable-Roman-VVAR.abc.ttf;--direction=ttb --variations=wght=900;U+0061,U+0062,U+0063;../expected/basic/glyf-vvar-ttb-wght900.svg ../fonts/2db082c6c462d5056544e56e7ea13bf58c33a832.ttf;--logical;U+0646,U+0633,U+062A,U+0639,U+0644,U+06CC,U+0642;../expected/basic/iran-nastaliq-y-offsets.svg +../../../fuzzing/fonts/noto_handwriting-untouchedsvg.ttf;;U+270D;../expected/basic/handwriting-svg.svg +../../../fuzzing/fonts/noto_handwriting-untouchedsvgz.ttf;;U+270D;../expected/basic/handwriting-svg.svg From d9116dda9c4810b27eab9af83c52d3605185a133 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 11 Mar 2026 20:51:41 -0300 Subject: [PATCH 09/14] [svg] Use em-square root viewport fallback Root SVG documents in OpenType SVG glyph data can omit width and height while still defining a viewBox. HarfBuzz was falling back to the viewBox size in that case, which made fonts like emojione-svg.otf render with the wrong placement and scale. Use the font upem as the fallback viewport size for the root SVG document. Raster now applies that when mapping the root viewBox, and vector injects matching width/height on extracted root SVG glyph documents. Tested: - meson test -C build Assisted-by: OpenAI Codex --- src/hb-raster-svg-render.cc | 13 ++++- src/hb-vector-svg-subset.cc | 98 ++++++++++++++++++++++++++++++++++--- 2 files changed, 103 insertions(+), 8 deletions(-) diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index 96c58335f..e39b0d93e 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -155,8 +155,17 @@ svg_render_container_element (hb_svg_render_context_t *ctx, viewport_h = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, geom_style_props.height, "height")); if (!(viewport_w > 0.f && viewport_h > 0.f)) { - viewport_w = vb_w; - viewport_h = vb_h; + if (ctx->depth == 1) + { + unsigned upem = hb_font_get_face (ctx->font)->get_upem (); + viewport_w = (float) upem; + viewport_h = (float) upem; + } + else + { + viewport_w = vb_w; + viewport_h = vb_h; + } } has_viewbox_transform = hb_raster_svg_compute_viewbox_transform (viewport_w, viewport_h, diff --git a/src/hb-vector-svg-subset.cc b/src/hb-vector-svg-subset.cc index 3d1e56227..922153c4e 100644 --- a/src/hb-vector-svg-subset.cc +++ b/src/hb-vector-svg-subset.cc @@ -30,6 +30,7 @@ #include "hb-vector-svg-subset.hh" #ifndef HB_NO_SVG #include "OT/Color/svg/svg.hh" +#include "hb-raster-svg-parse.hh" #endif #include "hb-vector-svg-utils.hh" #include "hb-map.hh" @@ -40,6 +41,33 @@ #include #ifndef HB_NO_SVG +static bool +hb_svg_append_str (hb_vector_t *out, + const char *s) +{ + return hb_svg_append_len (out, s, (unsigned) strlen (s)); +} + +static bool +hb_svg_append_unsigned (hb_vector_t *out, + unsigned v) +{ + char tmp[10]; + unsigned n = 0; + do { + tmp[n++] = (char) ('0' + (v % 10)); + v /= 10; + } while (v); + + unsigned old_len = out->length; + if (unlikely (!out->resize_dirty ((int) (old_len + n)))) + return false; + + for (unsigned i = 0; i < n; i++) + out->arrayZ[old_len + i] = tmp[n - 1 - i]; + return true; +} + static bool hb_svg_append_with_prefix (hb_vector_t *out, const char *s, @@ -254,6 +282,30 @@ hb_svg_collect_refs (const char *s, return true; } +static bool +hb_svg_find_root_open_tag (const char *svg, + unsigned len, + unsigned *root_open_len, + bool *missing_viewport) +{ + hb_svg_xml_parser_t parser (svg, len); + hb_svg_token_type_t tok = parser.next (); + if (!((tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) && + parser.tag_name.eq ("svg"))) + return false; + + if (missing_viewport) + { + *missing_viewport = + parser.find_attr ("width").is_null () || + parser.find_attr ("height").is_null (); + } + + if (root_open_len) + *root_open_len = (unsigned) (parser.p - parser.tag_start); + return true; +} + bool hb_svg_subset_glyph_image (hb_face_t *face, hb_blob_t *image, @@ -276,6 +328,9 @@ hb_svg_subset_glyph_image (hb_face_t *face, hb_hashmap_t needed_ids_set; hb_vector_t chosen_defs; hb_vector_t chosen_def_marks; + unsigned root_open_len = 0; + bool glyph_is_root_svg = false; + bool root_missing_viewport = false; char prefix[32]; int prefix_len = 0; @@ -302,6 +357,9 @@ hb_svg_subset_glyph_image (hb_face_t *face, goto done; defs_entries = face->table.SVG->doc_cache_get_defs_entries (doc_cache); + if (!hb_svg_find_root_open_tag (svg, len, &root_open_len, &root_missing_viewport)) + goto done; + glyph_is_root_svg = glyph_start == 0; needed_ids.alloc (16); if (!hb_svg_collect_refs (svg + glyph_start, glyph_end - glyph_start, @@ -338,7 +396,7 @@ hb_svg_subset_glyph_image (hb_face_t *face, if (prefix_len <= 0 || (unsigned) prefix_len >= sizeof (prefix)) goto done; - body_dst->alloc (body_dst->length + (glyph_end - glyph_start) + (unsigned) prefix_len + 32); + body_dst->alloc (body_dst->length + (glyph_end - glyph_start) + 64); for (unsigned i = 0; i < chosen_defs.length; i++) { @@ -349,11 +407,39 @@ hb_svg_subset_glyph_image (hb_face_t *face, goto done; } - ret = hb_svg_append_with_prefix (body_dst, - svg + glyph_start, - glyph_end - glyph_start, - prefix, - (unsigned) prefix_len); + if (glyph_is_root_svg && root_missing_viewport && root_open_len > 1) + { + unsigned upem = face->get_upem (); + if (!hb_svg_append_with_prefix (body_dst, + svg + glyph_start, + root_open_len - 1, + prefix, + (unsigned) prefix_len)) + goto done; + if (!hb_svg_append_str (body_dst, " width=\"")) + goto done; + if (!hb_svg_append_unsigned (body_dst, upem)) + goto done; + if (!hb_svg_append_str (body_dst, "\" height=\"")) + goto done; + if (!hb_svg_append_unsigned (body_dst, upem)) + goto done; + if (!hb_svg_append_c (body_dst, '"')) + goto done; + if (!hb_svg_append_c (body_dst, '>')) + goto done; + ret = hb_svg_append_with_prefix (body_dst, + svg + glyph_start + root_open_len, + glyph_end - glyph_start - root_open_len, + prefix, + (unsigned) prefix_len); + } + else + ret = hb_svg_append_with_prefix (body_dst, + svg + glyph_start, + glyph_end - glyph_start, + prefix, + (unsigned) prefix_len); done: hb_blob_destroy (normalized_image); From fd8535af45a65a3bb949e3ad5ad9d33e21119975 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 11 Mar 2026 21:03:31 -0300 Subject: [PATCH 10/14] [vector] Keep extracted root SVG overflow visible When hb-vector emits a root SVG glyph document as a nested , browser SVG renderers clip it to that nested viewport. OpenType SVG root documents are not meant to be clipped that way. Set overflow="visible" on extracted root SVG glyphs so browser rendering matches the intended OpenType SVG behavior. Tested: - meson test -C build Assisted-by: OpenAI Codex --- src/hb-vector-svg-subset.cc | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/hb-vector-svg-subset.cc b/src/hb-vector-svg-subset.cc index 922153c4e..36db8ca70 100644 --- a/src/hb-vector-svg-subset.cc +++ b/src/hb-vector-svg-subset.cc @@ -424,6 +424,8 @@ hb_svg_subset_glyph_image (hb_face_t *face, goto done; if (!hb_svg_append_unsigned (body_dst, upem)) goto done; + if (!hb_svg_append_str (body_dst, "\" overflow=\"visible")) + goto done; if (!hb_svg_append_c (body_dst, '"')) goto done; if (!hb_svg_append_c (body_dst, '>')) @@ -434,6 +436,24 @@ hb_svg_subset_glyph_image (hb_face_t *face, prefix, (unsigned) prefix_len); } + else if (glyph_is_root_svg && root_open_len > 1) + { + if (!hb_svg_append_with_prefix (body_dst, + svg + glyph_start, + root_open_len - 1, + prefix, + (unsigned) prefix_len)) + goto done; + if (!hb_svg_append_str (body_dst, " overflow=\"visible\"")) + goto done; + if (!hb_svg_append_c (body_dst, '>')) + goto done; + ret = hb_svg_append_with_prefix (body_dst, + svg + glyph_start + root_open_len, + glyph_end - glyph_start - root_open_len, + prefix, + (unsigned) prefix_len); + } else ret = hb_svg_append_with_prefix (body_dst, svg + glyph_start, From 2ea0e18a430789a74d5601bda543330bb1691a1f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 11 Mar 2026 22:12:40 -0300 Subject: [PATCH 11/14] [util] Move shared ANSI image helpers out of Cairo wrapper The RGB24-to-ANSI output path was implemented in helper-cairo-ansi.hh, so hb-raster picked up a Cairo header dependency even though it only needed the shared image-output code. That broke builds configured with -Dcairo=disabled. Move the shared callback, Chafa, and RGB24-to-ANSI helpers into helper-image-output.hh, keep helper-cairo-ansi.hh focused on adapting Cairo surfaces, and switch raster-output.hh to include only the shared image helper. Testing: meson test -C build Assisted-by: OpenAI Codex --- util/ansi-print.hh | 6 +- util/helper-cairo-ansi.hh | 175 +---------------------------------- util/helper-image-output.hh | 180 +++++++++++++++++++++++++++++++++++- util/raster-output.hh | 1 - 4 files changed, 180 insertions(+), 182 deletions(-) diff --git a/util/ansi-print.hh b/util/ansi-print.hh index 9f53cb75b..531acbbc4 100644 --- a/util/ansi-print.hh +++ b/util/ansi-print.hh @@ -29,8 +29,6 @@ #include "hb.hh" -#include - #include #include #include @@ -375,8 +373,8 @@ ansi_print_image_rgb24 (const uint32_t *data, unsigned int width, unsigned int height, unsigned int stride, - cairo_write_func_t write_func, - void *closure) + helper_image_write_func_t write_func, + void *closure) { image_t image (width, height, data, stride); diff --git a/util/helper-cairo-ansi.hh b/util/helper-cairo-ansi.hh index 144b97620..6c644c75f 100644 --- a/util/helper-cairo-ansi.hh +++ b/util/helper-cairo-ansi.hh @@ -27,182 +27,9 @@ #ifndef HELPER_CAIRO_ANSI_HH #define HELPER_CAIRO_ANSI_HH -#include "hb.hh" - +#include "helper-image-output.hh" #include -#include "ansi-print.hh" - -#ifdef HAVE_CHAFA -# include - -/* Similar to ansi-print.cc */ -# define CELL_W 8 -# define CELL_H (2 * CELL_W) - -static void -chafa_print_image_rgb24 (const void *data, int width, int height, int stride, int level, - cairo_write_func_t write_func, - void *closure) -{ - ChafaTermInfo *term_info; - ChafaSymbolMap *symbol_map; - ChafaCanvasConfig *config; - ChafaCanvas *canvas; - GString *gs; - unsigned int cols = (width + CELL_W - 1) / CELL_W; - unsigned int rows = (height + CELL_H - 1) / CELL_H; - gchar **environ; - ChafaCanvasMode mode; - ChafaPixelMode pixel_mode; - - /* Adapt to terminal; use sixels if available, and fall back to symbols - * with as many colors as are supported */ - - chafa_set_n_threads (1); // https://github.com/hpjansson/chafa/issues/125#issuecomment-1397475217 - - environ = g_get_environ (); - term_info = chafa_term_db_detect (chafa_term_db_get_default (), - environ); - - pixel_mode = CHAFA_PIXEL_MODE_SYMBOLS; - - if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_BEGIN_SIXELS)) - { - pixel_mode = CHAFA_PIXEL_MODE_SIXELS; - mode = CHAFA_CANVAS_MODE_TRUECOLOR; - } - else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_SET_COLOR_FGBG_DIRECT)) - mode = CHAFA_CANVAS_MODE_TRUECOLOR; - else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_SET_COLOR_FGBG_256)) - mode = CHAFA_CANVAS_MODE_INDEXED_240; - else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_SET_COLOR_FGBG_16)) - mode = CHAFA_CANVAS_MODE_INDEXED_16; - else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_INVERT_COLORS)) - mode = CHAFA_CANVAS_MODE_FGBG_BGFG; - else - mode = CHAFA_CANVAS_MODE_FGBG; - - /* Create the configuration */ - - symbol_map = chafa_symbol_map_new (); - chafa_symbol_map_add_by_tags (symbol_map, - (ChafaSymbolTags) (CHAFA_SYMBOL_TAG_BLOCK - | CHAFA_SYMBOL_TAG_SPACE - | (level >= 2 ? CHAFA_SYMBOL_TAG_WEDGE : 0) - | (level >= 3 ? CHAFA_SYMBOL_TAG_ALL : 0) - )); - - config = chafa_canvas_config_new (); - chafa_canvas_config_set_canvas_mode (config, mode); - chafa_canvas_config_set_pixel_mode (config, pixel_mode); - chafa_canvas_config_set_cell_geometry (config, CELL_W, CELL_H); - chafa_canvas_config_set_geometry (config, cols, rows); - chafa_canvas_config_set_symbol_map (config, symbol_map); - chafa_canvas_config_set_color_extractor (config, CHAFA_COLOR_EXTRACTOR_MEDIAN); - chafa_canvas_config_set_work_factor (config, 1.0f); - - /* Create canvas, draw to it and render output string */ - - canvas = chafa_canvas_new (config); - chafa_canvas_draw_all_pixels (canvas, - /* Cairo byte order is host native */ - G_BYTE_ORDER == G_LITTLE_ENDIAN - ? CHAFA_PIXEL_BGRA8_PREMULTIPLIED - : CHAFA_PIXEL_ARGB8_PREMULTIPLIED, - (const guint8 *) data, - width, - height, - stride); - gs = chafa_canvas_print (canvas, term_info); - - /* Print the string */ - - write_func (closure, (const unsigned char *) gs->str, gs->len); - - if (pixel_mode != CHAFA_PIXEL_MODE_SIXELS) - write_func (closure, (const unsigned char *) "\n", 1); - - /* Free resources */ - - g_string_free (gs, TRUE); - chafa_canvas_unref (canvas); - chafa_canvas_config_unref (config); - chafa_symbol_map_unref (symbol_map); - chafa_term_info_unref (term_info); - g_strfreev (environ); -} - -#endif /* HAVE_CHAFA */ - -static inline cairo_status_t -helper_image_write_to_ansi_stream_rgb24 (const uint32_t *data, - unsigned int width, - unsigned int height, - unsigned int stride, - cairo_write_func_t write_func, - void *closure) -{ - /* We don't have rows to spare on the terminal window... - * Find the tight image top/bottom and only print in between. */ - - /* Use corner color as background color. */ - uint32_t bg_color = data ? * (uint32_t *) data : 0; - - /* Drop first row while empty */ - auto orig_data = data; - while (height) - { - unsigned int i; - for (i = 0; i < width; i++) - if (data[i] != bg_color) - break; - if (i < width) - break; - data += stride / 4; - height--; - } - if (orig_data < data) - { - data -= stride / 4; - height++; /* Add one first blank row for padding. */ - } - - /* Drop last row while empty */ - auto orig_height = height; - while (height) - { - const uint32_t *row = data + (height - 1) * stride / 4; - unsigned int i; - for (i = 0; i < width; i++) - if (row[i] != bg_color) - break; - if (i < width) - break; - height--; - } - if (height < orig_height) - height++; /* Add one last blank row for padding. */ - - if (width && height) - { -#ifdef HAVE_CHAFA - const char *env = getenv ("HB_CHAFA"); - int chafa_level = 1; - if (env) - chafa_level = atoi (env); - if (chafa_level) - chafa_print_image_rgb24 (data, width, height, stride, chafa_level, - write_func, closure); - else -#endif - ansi_print_image_rgb24 (data, width, height, stride / 4, - write_func, closure); - } - - return CAIRO_STATUS_SUCCESS; -} - static inline cairo_status_t helper_cairo_surface_write_to_ansi_stream (cairo_surface_t *surface, cairo_write_func_t write_func, diff --git a/util/helper-image-output.hh b/util/helper-image-output.hh index 19993e017..1cdf9e1ac 100644 --- a/util/helper-image-output.hh +++ b/util/helper-image-output.hh @@ -29,7 +29,23 @@ #include "hb.hh" +#ifdef HAVE_CAIRO #include +#endif + +#ifdef HAVE_CAIRO +using helper_image_status_t = cairo_status_t; +using helper_image_write_func_t = cairo_write_func_t; +#define HELPER_IMAGE_STATUS_SUCCESS CAIRO_STATUS_SUCCESS +#else +using helper_image_status_t = int; +using helper_image_write_func_t = helper_image_status_t (*) (void *closure, + const unsigned char *data, + unsigned int size); +#define HELPER_IMAGE_STATUS_SUCCESS 0 +#endif + +#include "ansi-print.hh" enum class helper_image_protocol_t { NONE = 0, @@ -37,7 +53,7 @@ enum class helper_image_protocol_t { KITTY, }; -static inline cairo_status_t +static inline helper_image_status_t helper_image_stdio_write_func (void *closure, const unsigned char *data, unsigned int size) @@ -53,7 +69,7 @@ helper_image_stdio_write_func (void *closure, fail (false, "Failed to write output: %s", strerror (errno)); } - return CAIRO_STATUS_SUCCESS; + return HELPER_IMAGE_STATUS_SUCCESS; } static inline const char * @@ -98,7 +114,7 @@ helper_image_get_implicit_output_format (FILE *fp, static inline void helper_image_write_png_data (const unsigned char *data, unsigned int length, - cairo_write_func_t write_func, + helper_image_write_func_t write_func, void *closure, helper_image_protocol_t protocol) { @@ -154,4 +170,162 @@ helper_image_write_png_data (const unsigned char *data, g_string_free (string, TRUE); } +#ifdef HAVE_CHAFA +# include + +/* Similar to ansi-print.cc */ +# define CELL_W 8 +# define CELL_H (2 * CELL_W) + +static void +chafa_print_image_rgb24 (const void *data, int width, int height, int stride, int level, + helper_image_write_func_t write_func, + void *closure) +{ + ChafaTermInfo *term_info; + ChafaSymbolMap *symbol_map; + ChafaCanvasConfig *config; + ChafaCanvas *canvas; + GString *gs; + unsigned int cols = (width + CELL_W - 1) / CELL_W; + unsigned int rows = (height + CELL_H - 1) / CELL_H; + gchar **environ; + ChafaCanvasMode mode; + ChafaPixelMode pixel_mode; + + /* Adapt to terminal; use sixels if available, and fall back to symbols + * with as many colors as are supported */ + + chafa_set_n_threads (1); // https://github.com/hpjansson/chafa/issues/125#issuecomment-1397475217 + + environ = g_get_environ (); + term_info = chafa_term_db_detect (chafa_term_db_get_default (), + environ); + + pixel_mode = CHAFA_PIXEL_MODE_SYMBOLS; + + if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_BEGIN_SIXELS)) + { + pixel_mode = CHAFA_PIXEL_MODE_SIXELS; + mode = CHAFA_CANVAS_MODE_TRUECOLOR; + } + else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_SET_COLOR_FGBG_DIRECT)) + mode = CHAFA_CANVAS_MODE_TRUECOLOR; + else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_SET_COLOR_FGBG_256)) + mode = CHAFA_CANVAS_MODE_INDEXED_240; + else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_SET_COLOR_FGBG_16)) + mode = CHAFA_CANVAS_MODE_INDEXED_16; + else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_INVERT_COLORS)) + mode = CHAFA_CANVAS_MODE_FGBG_BGFG; + else + mode = CHAFA_CANVAS_MODE_FGBG; + + symbol_map = chafa_symbol_map_new (); + chafa_symbol_map_add_by_tags (symbol_map, + (ChafaSymbolTags) (CHAFA_SYMBOL_TAG_BLOCK + | CHAFA_SYMBOL_TAG_SPACE + | (level >= 2 ? CHAFA_SYMBOL_TAG_WEDGE : 0) + | (level >= 3 ? CHAFA_SYMBOL_TAG_ALL : 0) + )); + + config = chafa_canvas_config_new (); + chafa_canvas_config_set_canvas_mode (config, mode); + chafa_canvas_config_set_pixel_mode (config, pixel_mode); + chafa_canvas_config_set_cell_geometry (config, CELL_W, CELL_H); + chafa_canvas_config_set_geometry (config, cols, rows); + chafa_canvas_config_set_symbol_map (config, symbol_map); + chafa_canvas_config_set_color_extractor (config, CHAFA_COLOR_EXTRACTOR_MEDIAN); + chafa_canvas_config_set_work_factor (config, 1.0f); + + canvas = chafa_canvas_new (config); + chafa_canvas_draw_all_pixels (canvas, + G_BYTE_ORDER == G_LITTLE_ENDIAN + ? CHAFA_PIXEL_BGRA8_PREMULTIPLIED + : CHAFA_PIXEL_ARGB8_PREMULTIPLIED, + (const guint8 *) data, + width, + height, + stride); + gs = chafa_canvas_print (canvas, term_info); + + write_func (closure, (const unsigned char *) gs->str, gs->len); + + if (pixel_mode != CHAFA_PIXEL_MODE_SIXELS) + write_func (closure, (const unsigned char *) "\n", 1); + + g_string_free (gs, TRUE); + chafa_canvas_unref (canvas); + chafa_canvas_config_unref (config); + chafa_symbol_map_unref (symbol_map); + chafa_term_info_unref (term_info); + g_strfreev (environ); +} + +#endif /* HAVE_CHAFA */ + +static inline helper_image_status_t +helper_image_write_to_ansi_stream_rgb24 (const uint32_t *data, + unsigned int width, + unsigned int height, + unsigned int stride, + helper_image_write_func_t write_func, + void *closure) +{ + /* We don't have rows to spare on the terminal window... + * Find the tight image top/bottom and only print in between. */ + + uint32_t bg_color = data ? * (uint32_t *) data : 0; + + auto orig_data = data; + while (height) + { + unsigned int i; + for (i = 0; i < width; i++) + if (data[i] != bg_color) + break; + if (i < width) + break; + data += stride / 4; + height--; + } + if (orig_data < data) + { + data -= stride / 4; + height++; + } + + auto orig_height = height; + while (height) + { + const uint32_t *row = data + (height - 1) * stride / 4; + unsigned int i; + for (i = 0; i < width; i++) + if (row[i] != bg_color) + break; + if (i < width) + break; + height--; + } + if (height < orig_height) + height++; + + if (width && height) + { +#ifdef HAVE_CHAFA + const char *env = getenv ("HB_CHAFA"); + int chafa_level = 1; + if (env) + chafa_level = atoi (env); + if (chafa_level) + chafa_print_image_rgb24 (data, width, height, stride, chafa_level, + write_func, closure); + else +#endif + ansi_print_image_rgb24 (data, width, height, stride / 4, + write_func, closure); + } + + return HELPER_IMAGE_STATUS_SUCCESS; +} + #endif diff --git a/util/raster-output.hh b/util/raster-output.hh index e6343be98..51cbc00ce 100644 --- a/util/raster-output.hh +++ b/util/raster-output.hh @@ -30,7 +30,6 @@ #include "options.hh" #include "output-options.hh" #include "view-options.hh" -#include "helper-cairo-ansi.hh" #include "helper-image-output.hh" #include "hb-raster.h" #include "hb-ot.h" From 8fc1d1bfd13ec21341606ec2421cb3904f79f0d0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 12 Mar 2026 01:46:46 -0300 Subject: [PATCH 12/14] [vector] Allow duplicate SVG refs during subsetting Some SVG glyph documents reuse the same referenced element multiple times. The vector subsetter treated a duplicate ref as a hard failure, which made hb-vector drop glyphs like U+270D from the handwriting picosvg test fonts entirely. Treat duplicate and empty refs as non-fatal while still uniquing the collected ids. Add regressions for the handwriting picosvg and picosvgz fonts, reusing a shared expected output for the pair. Fixes: https://github.com/harfbuzz/harfbuzz/issues/5829 Assisted-by: OpenAI Codex --- src/hb-vector-svg-subset.cc | 4 ++-- .../expected/basic/handwriting-picosvg.svg | 21 +++++++++++++++++++ test/vector/data/tests/basic.tests | 2 ++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 test/vector/data/expected/basic/handwriting-picosvg.svg diff --git a/src/hb-vector-svg-subset.cc b/src/hb-vector-svg-subset.cc index 36db8ca70..54c25e24c 100644 --- a/src/hb-vector-svg-subset.cc +++ b/src/hb-vector-svg-subset.cc @@ -199,10 +199,10 @@ hb_svg_add_unique_id (hb_vector_t *v, const char *p, unsigned n) { - if (!n) return false; + if (!n) return true; OT::SVG::svg_id_span_t key = {p, n}; if (seen_ids->has (key)) - return false; + return true; if (unlikely (!seen_ids->set (key, true))) return false; auto *slot = v->push (); diff --git a/test/vector/data/expected/basic/handwriting-picosvg.svg b/test/vector/data/expected/basic/handwriting-picosvg.svg new file mode 100644 index 000000000..0853e02fd --- /dev/null +++ b/test/vector/data/expected/basic/handwriting-picosvg.svg @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/test/vector/data/tests/basic.tests b/test/vector/data/tests/basic.tests index 32394776a..42712bf00 100644 --- a/test/vector/data/tests/basic.tests +++ b/test/vector/data/tests/basic.tests @@ -15,3 +15,5 @@ ../fonts/2db082c6c462d5056544e56e7ea13bf58c33a832.ttf;--logical;U+0646,U+0633,U+062A,U+0639,U+0644,U+06CC,U+0642;../expected/basic/iran-nastaliq-y-offsets.svg ../../../fuzzing/fonts/noto_handwriting-untouchedsvg.ttf;;U+270D;../expected/basic/handwriting-svg.svg ../../../fuzzing/fonts/noto_handwriting-untouchedsvgz.ttf;;U+270D;../expected/basic/handwriting-svg.svg +../../../fuzzing/fonts/noto_handwriting-picosvg.ttf;;U+270D;../expected/basic/handwriting-picosvg.svg +../../../fuzzing/fonts/noto_handwriting-picosvgz.ttf;;U+270D;../expected/basic/handwriting-picosvg.svg From 8e6b9ecf4e566b8a1dc624ae3543f0b4eac6efa5 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 12 Mar 2026 01:49:07 -0300 Subject: [PATCH 13/14] [test] Add vector coverage for handwriting color fonts Extend the vector regression tests to cover the remaining handwriting fuzzing fonts: sbix, glyf COLRv1, and CFF/CFF2 COLRv1. Reuse the expected output where the CFF and CFF2 variants serialize identically. Assisted-by: OpenAI Codex --- .../expected/basic/handwriting-cff-colr-1.svg | 71 +++++++++++++++++++ .../basic/handwriting-glyf-colr-1.svg | 71 +++++++++++++++++++ .../data/expected/basic/handwriting-sbix.svg | 12 ++++ test/vector/data/tests/basic.tests | 4 ++ 4 files changed, 158 insertions(+) create mode 100644 test/vector/data/expected/basic/handwriting-cff-colr-1.svg create mode 100644 test/vector/data/expected/basic/handwriting-glyf-colr-1.svg create mode 100644 test/vector/data/expected/basic/handwriting-sbix.svg diff --git a/test/vector/data/expected/basic/handwriting-cff-colr-1.svg b/test/vector/data/expected/basic/handwriting-cff-colr-1.svg new file mode 100644 index 000000000..b2210f447 --- /dev/null +++ b/test/vector/data/expected/basic/handwriting-cff-colr-1.svg @@ -0,0 +1,71 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/vector/data/expected/basic/handwriting-glyf-colr-1.svg b/test/vector/data/expected/basic/handwriting-glyf-colr-1.svg new file mode 100644 index 000000000..490d431eb --- /dev/null +++ b/test/vector/data/expected/basic/handwriting-glyf-colr-1.svg @@ -0,0 +1,71 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/vector/data/expected/basic/handwriting-sbix.svg b/test/vector/data/expected/basic/handwriting-sbix.svg new file mode 100644 index 000000000..67de99add --- /dev/null +++ b/test/vector/data/expected/basic/handwriting-sbix.svg @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/test/vector/data/tests/basic.tests b/test/vector/data/tests/basic.tests index 42712bf00..2bfadd233 100644 --- a/test/vector/data/tests/basic.tests +++ b/test/vector/data/tests/basic.tests @@ -17,3 +17,7 @@ ../../../fuzzing/fonts/noto_handwriting-untouchedsvgz.ttf;;U+270D;../expected/basic/handwriting-svg.svg ../../../fuzzing/fonts/noto_handwriting-picosvg.ttf;;U+270D;../expected/basic/handwriting-picosvg.svg ../../../fuzzing/fonts/noto_handwriting-picosvgz.ttf;;U+270D;../expected/basic/handwriting-picosvg.svg +../../../fuzzing/fonts/noto_handwriting-sbix.ttf;;U+270D;../expected/basic/handwriting-sbix.svg +../../../fuzzing/fonts/noto_handwriting-glyf_colr_1.ttf;;U+270D;../expected/basic/handwriting-glyf-colr-1.svg +../../../fuzzing/fonts/noto_handwriting-cff_colr_1.otf;;U+270D;../expected/basic/handwriting-cff-colr-1.svg +../../../fuzzing/fonts/noto_handwriting-cff2_colr_1.otf;;U+270D;../expected/basic/handwriting-cff-colr-1.svg From 1607db69709e9899c99284361dc446d6f9badcf9 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 12 Mar 2026 02:09:35 -0300 Subject: [PATCH 14/14] [svg] Keep zlib private to raster and vector SVG decoding The core libharfbuzz SVG table code should not include zlib headers or link against zlib. Only the raster and vector libraries need to normalize svgz documents before parsing them. Move the gzip decoder into a private hb-zlib translation unit that is built only by harfbuzz-raster and harfbuzz-vector, and move SVG blob normalization into a private helper header used only there. Keep the shared SVG table and cache logic in libharfbuzz free of zlib includes. Assisted-by: OpenAI Codex --- src/OT/Color/svg/svg.hh | 93 ++++++++++++++++----------------- src/hb-zlib.cc | 98 +++++++++++++++++++++++++++++++++++ src/hb-zlib.hh | 112 +--------------------------------------- src/meson.build | 5 +- 4 files changed, 148 insertions(+), 160 deletions(-) create mode 100644 src/hb-zlib.cc diff --git a/src/OT/Color/svg/svg.hh b/src/OT/Color/svg/svg.hh index 4acfa7561..d6fb71832 100644 --- a/src/OT/Color/svg/svg.hh +++ b/src/OT/Color/svg/svg.hh @@ -44,6 +44,50 @@ namespace OT { +static inline hb_blob_t * +hb_ot_svg_reference_normalized_blob (hb_blob_t *image, + const char **svg, + unsigned *len) +{ + hb_blob_t *blob = hb_blob_reference (image); + unsigned data_len = 0; + const char *data = hb_blob_get_data (blob, &data_len); + + if (!data || !data_len) + goto fail; + + if (hb_blob_is_gzip (data, data_len)) + { + uint32_t expected_size = 0; + if (hb_gzip_get_uncompressed_size (data, data_len, &expected_size) && + unlikely ((size_t) expected_size > (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) + goto fail; + + hb_blob_t *uncompressed = hb_blob_decompress_gzip (blob, + HB_SVG_MAX_DOCUMENT_SIZE); + if (!uncompressed) + goto fail; + + hb_blob_destroy (blob); + blob = uncompressed; + data = hb_blob_get_data (blob, &data_len); + if (!data || !data_len) + goto fail; + } + + if (unlikely ((size_t) data_len > (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) + goto fail; + + if (svg) *svg = data; + if (len) *len = data_len; + return blob; + +fail: + hb_blob_destroy (blob); + if (svg) *svg = nullptr; + if (len) *len = 0; + return nullptr; +} struct SVGDocumentIndexEntry { @@ -564,55 +608,6 @@ parse_cache_entries_linear (const char *svg, } /* namespace _hb_svg_cache_impl */ -static inline hb_blob_t * -hb_ot_svg_reference_normalized_blob (hb_blob_t *image, - const char **svg, - unsigned *len) -{ - hb_blob_t *blob = hb_blob_reference (image); - unsigned data_len = 0; - const char *data = hb_blob_get_data (blob, &data_len); - - if (!data || !data_len) - goto fail; - - if (hb_blob_is_gzip (data, data_len)) - { - uint32_t expected_size = 0; - if (hb_gzip_get_uncompressed_size (data, data_len, &expected_size) && - unlikely ((size_t) expected_size > (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) - goto fail; - -#ifdef HAVE_ZLIB - hb_blob_t *uncompressed = hb_blob_decompress_gzip (blob, - HB_SVG_MAX_DOCUMENT_SIZE); - if (!uncompressed) - goto fail; - - hb_blob_destroy (blob); - blob = uncompressed; - data = hb_blob_get_data (blob, &data_len); - if (!data || !data_len) - goto fail; -#else - goto fail; -#endif - } - - if (unlikely ((size_t) data_len > (size_t) HB_SVG_MAX_DOCUMENT_SIZE)) - goto fail; - - if (svg) *svg = data; - if (len) *len = data_len; - return blob; - -fail: - hb_blob_destroy (blob); - if (svg) *svg = nullptr; - if (len) *len = 0; - return nullptr; -} - inline SVG::accelerator_t::accelerator_t (hb_face_t *face) { diff --git a/src/hb-zlib.cc b/src/hb-zlib.cc new file mode 100644 index 000000000..2703e943d --- /dev/null +++ b/src/hb-zlib.cc @@ -0,0 +1,98 @@ +#ifndef HB_ZLIB_CC +#define HB_ZLIB_CC + +#include "hb.hh" + +#include "hb-zlib.hh" + +#ifdef HAVE_ZLIB +#include +#endif + +hb_blob_t * +hb_blob_decompress_gzip (hb_blob_t *blob, + unsigned max_output_len) +{ +#ifndef HAVE_ZLIB + return nullptr; +#else + unsigned compressed_len = 0; + const uint8_t *compressed = (const uint8_t *) hb_blob_get_data (blob, &compressed_len); + if (!compressed || !compressed_len) + return nullptr; + + z_stream stream = {}; + stream.next_in = (Bytef *) compressed; + stream.avail_in = compressed_len; + + if (inflateInit2 (&stream, 16 + MAX_WBITS) != Z_OK) + return nullptr; + + uint32_t expected_size = 0; + hb_gzip_get_uncompressed_size ((const char *) compressed, + compressed_len, + &expected_size); + + size_t allocated = hb_min ((size_t) hb_max (expected_size, 4096u), + (size_t) max_output_len); + char *output = (char *) hb_malloc (allocated); + if (!output) + { + inflateEnd (&stream); + return nullptr; + } + + int status = Z_OK; + while (true) + { + size_t produced = (size_t) stream.total_out; + if (unlikely (produced >= (size_t) max_output_len)) + goto fail; + + if (produced == allocated) + { + size_t new_allocated = hb_min (allocated * 2, (size_t) max_output_len); + if (unlikely (new_allocated <= allocated)) + goto fail; + + char *new_output = (char *) hb_realloc (output, new_allocated); + if (unlikely (!new_output)) + goto fail; + + output = new_output; + allocated = new_allocated; + } + + stream.next_out = (Bytef *) output + stream.total_out; + stream.avail_out = (uInt) (allocated - (size_t) stream.total_out); + + status = inflate (&stream, Z_FINISH); + if (status == Z_STREAM_END) + break; + + if ((status == Z_OK || status == Z_BUF_ERROR) && stream.avail_out == 0) + continue; + + goto fail; + } + + inflateEnd (&stream); + + if (unlikely ((size_t) stream.total_out > (size_t) max_output_len)) + goto fail_free; + + return hb_blob_create_or_fail (output, + (unsigned) stream.total_out, + HB_MEMORY_MODE_WRITABLE, + output, + hb_free); + +fail: + inflateEnd (&stream); +fail_free: + hb_free (output); + return nullptr; +#endif +} + +#endif /* HB_ZLIB_CC */ diff --git a/src/hb-zlib.hh b/src/hb-zlib.hh index d9a26ed51..3b8eb791a 100644 --- a/src/hb-zlib.hh +++ b/src/hb-zlib.hh @@ -1,36 +1,8 @@ -/* - * 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. - */ - #ifndef HB_ZLIB_HH #define HB_ZLIB_HH #include "hb-blob.hh" -#ifdef HAVE_ZLIB -#include -#endif - static inline bool hb_blob_is_gzip (const char *data, unsigned data_len) @@ -58,88 +30,8 @@ hb_gzip_get_uncompressed_size (const char *data, return true; } -#ifdef HAVE_ZLIB -static inline hb_blob_t * +HB_INTERNAL hb_blob_t * hb_blob_decompress_gzip (hb_blob_t *blob, - unsigned max_output_len) -{ - unsigned compressed_len = 0; - const uint8_t *compressed = (const uint8_t *) hb_blob_get_data (blob, &compressed_len); - if (!compressed || !compressed_len) - return nullptr; - - z_stream stream = {}; - stream.next_in = (Bytef *) compressed; - stream.avail_in = compressed_len; - - if (inflateInit2 (&stream, 16 + MAX_WBITS) != Z_OK) - return nullptr; - - uint32_t expected_size = 0; - hb_gzip_get_uncompressed_size ((const char *) compressed, - compressed_len, - &expected_size); - - size_t allocated = hb_min ((size_t) hb_max (expected_size, 4096u), - (size_t) max_output_len); - char *output = (char *) hb_malloc (allocated); - if (!output) - { - inflateEnd (&stream); - return nullptr; - } - - int status = Z_OK; - while (true) - { - size_t produced = (size_t) stream.total_out; - if (unlikely (produced >= (size_t) max_output_len)) - goto fail; - - if (produced == allocated) - { - size_t new_allocated = hb_min (allocated * 2, (size_t) max_output_len); - if (unlikely (new_allocated <= allocated)) - goto fail; - - char *new_output = (char *) hb_realloc (output, new_allocated); - if (unlikely (!new_output)) - goto fail; - - output = new_output; - allocated = new_allocated; - } - - stream.next_out = (Bytef *) output + stream.total_out; - stream.avail_out = (uInt) (allocated - (size_t) stream.total_out); - - status = inflate (&stream, Z_FINISH); - if (status == Z_STREAM_END) - break; - - if ((status == Z_OK || status == Z_BUF_ERROR) && stream.avail_out == 0) - continue; - - goto fail; - } - - inflateEnd (&stream); - - if (unlikely ((size_t) stream.total_out > (size_t) max_output_len)) - goto fail_free; - - return hb_blob_create_or_fail (output, - (unsigned) stream.total_out, - HB_MEMORY_MODE_WRITABLE, - output, - hb_free); - -fail: - inflateEnd (&stream); -fail_free: - hb_free (output); - return nullptr; -} -#endif + unsigned max_output_len); #endif /* HB_ZLIB_HH */ diff --git a/src/meson.build b/src/meson.build index 90c5618e8..0d93ef744 100644 --- a/src/meson.build +++ b/src/meson.build @@ -273,7 +273,6 @@ hb_base_sources = files( 'hb-unicode.hh', 'hb-utf.hh', 'hb-vector.hh', - 'hb-zlib.hh', 'hb.hh', ) @@ -705,6 +704,8 @@ if not get_option('subset').disabled() endif hb_raster_sources = [ + 'hb-zlib.cc', + 'hb-zlib.hh', 'hb-raster.cc', 'hb-raster-image.cc', 'hb-raster-image.hh', @@ -743,6 +744,8 @@ hb_raster_headers = [ ] hb_vector_sources = [ + 'hb-zlib.cc', + 'hb-zlib.hh', 'hb-vector.cc', 'hb-vector-svg-draw.cc', 'hb-vector-svg-path.cc',