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