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 diff --git a/AGENTS.md b/AGENTS.md index 8ba04ef44..df503412f 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 @@ -91,10 +93,10 @@ For API work: ## Commit guidance - Before any commit, run the entire test suite with `meson test -C build`. +- 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. -- When passing commit bodies through the shell, use real newlines, not literal - `\n` escapes. Prefer `$'...'` quoting or a temporary commit message file. +- 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. diff --git a/meson.build b/meson.build index bb6d45c4c..c6af11f1e 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: false) 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 @@ -487,6 +492,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(), diff --git a/src/OT/Color/svg/svg.hh b/src/OT/Color/svg/svg.hh index ac70ce17d..d6fb71832 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 @@ -42,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 { 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..e39b0d93e 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -37,6 +37,7 @@ #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 @@ -154,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, @@ -433,30 +443,41 @@ 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; - + bool ret = false; + 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 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 = 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 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) 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; @@ -469,10 +490,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, @@ -481,9 +501,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); @@ -518,7 +536,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,7 +551,11 @@ hb_raster_svg_render (hb_raster_paint_t *paint, } } - return found_glyph; + ret = found_glyph; + +done: + hb_blob_destroy (render_blob); + return ret; } #endif /* !HB_NO_RASTER_SVG */ diff --git a/src/hb-vector-svg-subset.cc b/src/hb-vector-svg-subset.cc index 3cc06a916..54c25e24c 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, @@ -171,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 (); @@ -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, @@ -262,47 +314,61 @@ 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; + unsigned root_open_len = 0; + bool glyph_is_root_svg = false; + bool root_missing_viewport = false; + 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); + if (!hb_svg_find_root_open_tag (svg, len, &root_open_len, &root_missing_viewport)) + goto done; + glyph_is_root_svg = glyph_start == 0; - 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,36 +382,88 @@ 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); + body_dst->alloc (body_dst->length + (glyph_end - glyph_start) + 64); for (unsigned i = 0; i < chosen_defs.length; i++) { 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, + 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, - glyph_end - glyph_start, + root_open_len - 1, prefix, - (unsigned) prefix_len); + (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_str (body_dst, "\" overflow=\"visible")) + 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 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, + glyph_end - glyph_start, + prefix, + (unsigned) prefix_len); + +done: + hb_blob_destroy (normalized_image); + return ret; } #else bool 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 new file mode 100644 index 000000000..3b8eb791a --- /dev/null +++ b/src/hb-zlib.hh @@ -0,0 +1,37 @@ +#ifndef HB_ZLIB_HH +#define HB_ZLIB_HH + +#include "hb-blob.hh" + +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; +} + +HB_INTERNAL hb_blob_t * +hb_blob_decompress_gzip (hb_blob_t *blob, + unsigned max_output_len); + +#endif /* HB_ZLIB_HH */ diff --git a/src/meson.build b/src/meson.build index c95e9f16d..156a770ef 100644 --- a/src/meson.build +++ b/src/meson.build @@ -706,6 +706,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', @@ -744,6 +746,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', @@ -766,6 +770,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, @@ -803,9 +810,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/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 (); } 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-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/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/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..2bfadd233 100644 --- a/test/vector/data/tests/basic.tests +++ b/test/vector/data/tests/basic.tests @@ -13,3 +13,11 @@ ../../../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 +../../../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 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"