45 Commits
Author SHA1 Message Date
Behdad EsfahbodandClaude Opus 4.8 04d6d3912e [vector] Fix six OSS-scanner findings in the vector paint backends and gvar instancer (#6498)
* [instancer] Treat a zero-size pool allocation as success in gvar instancing

hb_alloc_pool_t::alloc(0) legitimately returns nullptr while no shared
chunk is open, but hb_vector_t::allocate_from_pool() took any nullptr as
an allocation failure and flagged the (empty) vector with set_error().
The next allocate_from_pool() on it then tripped
assert(!length && !allocated), aborting hb-subset during partial
instancing of a crafted ~1 KB variable font whose retained glyph has
more than 16,376 points (so no small request ever opens the shared
chunk) and whose tuple is split by the requested axis range.

Fix the root cause where the vector already special-cases zero-size
requests on the heap path: a null result is a failure only when bytes
were requested (size && !arrayZ).

tuple_delta_t::copy_from() also discarded the results of its seven
duplicate_vector_from_pool() calls, so a genuine pool failure during a
tuple clone would reach the same assert (or silently drop gvar under
NDEBUG); it now returns bool, change_tuple_var_axis_limit() marks out in
error, and change_tuple_variations_axis_limits() fails instead of
continuing.

A regression test for the zero-size pool allocation is added to
src/test-vector.cc. The attack font now instances to the same output as
the control; test-vector and the subset suite pass.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-N0R68QRI.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* [vector] Refuse PDF indexed-PNG soft masks larger than the document cap

hb_pdf_build_indexed_smask() allocated and filled (width+1)*height and
width*height bytes straight from the attacker-controlled PNG IHDR, while
HB_VECTOR_MAX_DOCUMENT_SIZE was only consulted when the finished mask was
appended to the PDF object.  A ~1 MB sbix/CBDT font could thus make the
PDF backend inflate and un-filter up to a 4 GiB mask per painted glyph,
only to drop it afterwards.

The mask is emitted uncompressed into a document-capped hb_vector_buf_t,
so a mask larger than the cap can never be appended: reject it alongside
the existing 32-bit overflow guards in the builder (the same pattern as
the raster PNG decoder's HB_RASTER_MAX_BUFFER_SIZE check), which bounds
both allocations (raw_len = mask_len + height) before any work is done.

add_xobject_png_image() additionally skips registering and referencing
the SMask object when its buffer tripped the cap, so the output no longer
contains a dangling empty-stream object for an edge-case mask that fits
the builder but not the object with its header.  Masks that fit today are
unaffected.

A regression test in test/vector/test-vector-budget.c hand-rolls a zlib
stream for a 4097x4096 indexed PNG with tRNS, paints it to PDF, and
asserts the render succeeds without an /SMask.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-6AWUPU2N.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* [vector] Reduce linear-gradient P2 anchor in the PDF backend

The PDF paint backend drew COLRv1 linear gradients along the raw P0->P1
segment and marked the rotation anchor P2 as HB_UNUSED, writing the
axial shading's /Coords straight from P0 and P1.  For any gradient whose
P2 - P0 is not perpendicular to P1 - P0 (how a font rotates or skews a
gradient), the PDF put the colour stops in the wrong places, unlike the
SVG, raster, GPU and cairo backends, which all call
hb_paint_reduce_linear_anchors() first.

Reduce the three COLR anchors to the two-point axis the same way and
derive the gradient coordinates from the reduced axis.  One change covers
both the colour shading and the soft-mask shading, since both use the
same gx0..gy1.

Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-S7CCVVN0.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* [vector] Size PDF gradient soft masks to cover the painted area

For a linear or radial COLRv1 gradient with a translucent stop, the PDF
backend painted the alpha ramp through a luminosity soft mask whose Form
XObject /BBox was the rectangle spanned by the gradient axis (linear) or
the enclosing square of the outer circle (radial).  An axial shading is
not confined to its axis rectangle, and a radial shading with /Extend
extends beyond its circles; the RGB fill relies on that.  A form's /BBox
clips everything it paints and the luminosity backdrop is black (mask 0)
outside it, so the translucent gradient was masked out entirely for a
horizontal or vertical axis (zero-area box) and cropped otherwise.

Give both soft-mask forms a large box covering the whole painted area,
as hb_pdf_paint_sweep_gradient() already does, instead of the axis or
circle box.

Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-3BXWX3QQ.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* [vector] Decline color_glyph in the SVG paint backend

The SVG backend's color_glyph callback re-entered hb_font_paint_glyph()
for every COLRv1 PaintColrGlyph, and each re-entry built a fresh COLR
paint context whose HB_MAX_GRAPH_EDGE_COUNT and HB_MAX_NESTING_LEVEL
counters started over.  The limits therefore applied per nested glyph
instead of per top-level glyph, so a DAG in which each level references
the next one f times was walked f^levels times, letting a sub-2 KB font
spin indefinitely; the same per-re-entry depth reset let a crafted font
overflow a small thread stack.

Return false, as hb-raster and the PDF backend do (and as the COLR and
FreeType walkers expect), so the walker paints the nested glyph inline
in the same context, where the existing decycler, nesting limit and edge
limit bound the whole graph.  The color_glyph_depth counter and
active_color_glyphs set existed only to guard the re-entry, so they are
dropped from hb_vector_paint_t and from create, destroy and clear.

A regression test building a 30-level, 3-reference COLR chain in memory
is added to test/vector/test-vector-budget.c for both SVG and PDF.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-YR4RMFWV.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* [vector] Make the document-size cap per document, not per buffer

HB_VECTOR_MAX_DOCUMENT_SIZE was enforced in hb_vector_buf_t against the
length of the one buffer being appended to, but both backends spread one
document across many buffers, so the cap bounded each buffer and not the
document.  A few-KB COLRv1 font could exceed it by a large multiple:

- SVG starts a fresh buffer per open paint group, so a chain of nested
  PaintComposite records keeps dozens of near-cap backdrop buffers alive
  at once (about 1 GB from a 1.3 KB font).  An open group now inherits
  the bytes already held by the buffers it will be appended to, via a
  base member folded into would_overflow(), so base + length of every
  open group stays within the cap.

- PDF keeps every object (functions, shadings, ExtGStates, images) in a
  buffer of its own with no running total, so one large gradient painted
  thousands of times produces millions of objects (gigabytes) that are
  only summed at render.  add_object() now charges a running total
  against the same cap with a sticky overflow flag; the image and
  gradient callbacks stop when res->in_error(), and render returns NULL
  in that state because the content stream would refer to dropped
  objects.

hb_vector_buf_t and hb_pdf_resources_t are internal, so there is no API
or ABI change; documents that fit under the cap are unchanged.
Regression tests are added to test/vector/test-vector-budget.c.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-C20MD9VR.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 17:16:03 -06:00
Behdad Esfahbod 6f6ed4cd5c [vector] Share recycling metadata across renderers (#6480)
Use one hidden user-data key for vector blob metadata. A static key in
its header gives each renderer a different address, so a blob recycled
between renderers can retain a user-data entry for released metadata.

Clear in_replace after replace_buffer() returns, including when the
buffer stays at the same address and its destroy callback is skipped.
Otherwise the final destruction keeps the metadata allocation alive.

Exercise cross-renderer and in-place recycling in all four SVG/PDF draw
and paint paths. The cross-renderer case fails under ASan before the
fix. With the fix the sanitizer check and all 293 Meson tests pass, and
macOS leaks reports no leaks (five metadata allocations before clearing
the in_replace flag).

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-HAG1BPA1
and ordinary bug ANT-2026-YE77VHKY.

Assisted-by: OpenAI Codex
2026-10-30 15:03:23 -06:00
Behdad EsfahbodandClaude Opus 4.8 b76754924e [vector] Make the document-size cap per document, not per buffer
HB_VECTOR_MAX_DOCUMENT_SIZE was enforced in hb_vector_buf_t against the
length of the one buffer being appended to, but both backends spread one
document across many buffers, so the cap bounded each buffer and not the
document.  A few-KB COLRv1 font could exceed it by a large multiple:

- SVG starts a fresh buffer per open paint group, so a chain of nested
  PaintComposite records keeps dozens of near-cap backdrop buffers alive
  at once (about 1 GB from a 1.3 KB font).  An open group now inherits
  the bytes already held by the buffers it will be appended to, via a
  base member folded into would_overflow(), so base + length of every
  open group stays within the cap.

- PDF keeps every object (functions, shadings, ExtGStates, images) in a
  buffer of its own with no running total, so one large gradient painted
  thousands of times produces millions of objects (gigabytes) that are
  only summed at render.  add_object() now charges a running total
  against the same cap with a sticky overflow flag; the image and
  gradient callbacks stop when res->in_error(), and render returns NULL
  in that state because the content stream would refer to dropped
  objects.

hb_vector_buf_t and hb_pdf_resources_t are internal, so there is no API
or ABI change; documents that fit under the cap are unchanged.
Regression tests are added to test/vector/test-vector-budget.c.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-C20MD9VR.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 14:57:14 -06:00
Behdad EsfahbodandClaude Opus 4.8 14e1f86467 [vector] Decline color_glyph in the SVG paint backend
The SVG backend's color_glyph callback re-entered hb_font_paint_glyph()
for every COLRv1 PaintColrGlyph, and each re-entry built a fresh COLR
paint context whose HB_MAX_GRAPH_EDGE_COUNT and HB_MAX_NESTING_LEVEL
counters started over.  The limits therefore applied per nested glyph
instead of per top-level glyph, so a DAG in which each level references
the next one f times was walked f^levels times, letting a sub-2 KB font
spin indefinitely; the same per-re-entry depth reset let a crafted font
overflow a small thread stack.

Return false, as hb-raster and the PDF backend do (and as the COLR and
FreeType walkers expect), so the walker paints the nested glyph inline
in the same context, where the existing decycler, nesting limit and edge
limit bound the whole graph.  The color_glyph_depth counter and
active_color_glyphs set existed only to guard the re-entry, so they are
dropped from hb_vector_paint_t and from create, destroy and clear.

A regression test building a 30-level, 3-reference COLR chain in memory
is added to test/vector/test-vector-budget.c for both SVG and PDF.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-YR4RMFWV.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 14:55:57 -06:00
Behdad EsfahbodandClaude Opus 4.8 a14c095e3f [vector] Refuse PDF indexed-PNG soft masks larger than the document cap
hb_pdf_build_indexed_smask() allocated and filled (width+1)*height and
width*height bytes straight from the attacker-controlled PNG IHDR, while
HB_VECTOR_MAX_DOCUMENT_SIZE was only consulted when the finished mask was
appended to the PDF object.  A ~1 MB sbix/CBDT font could thus make the
PDF backend inflate and un-filter up to a 4 GiB mask per painted glyph,
only to drop it afterwards.

The mask is emitted uncompressed into a document-capped hb_vector_buf_t,
so a mask larger than the cap can never be appended: reject it alongside
the existing 32-bit overflow guards in the builder (the same pattern as
the raster PNG decoder's HB_RASTER_MAX_BUFFER_SIZE check), which bounds
both allocations (raw_len = mask_len + height) before any work is done.

add_xobject_png_image() additionally skips registering and referencing
the SMask object when its buffer tripped the cap, so the output no longer
contains a dangling empty-stream object for an edge-case mask that fits
the builder but not the object with its header.  Masks that fit today are
unaffected.

A regression test in test/vector/test-vector-budget.c hand-rolls a zlib
stream for a 4097x4096 indexed PNG with tRNS, paints it to PDF, and
asserts the render succeeds without an /SMask.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-6AWUPU2N.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 14:54:23 -06:00
Behdad Esfahbod 90045a6551 [vector] Use font coordinates for PDF draw page bounds (#6472)
The PDF draw renderer emitted paths in Y-up coordinates but vertically
mirrored the MediaBox and background rectangle. Use the same extents
for the content, page, and background, matching the paint renderer.

Regenerate the existing PDF fixture with hb-vector. Only the page box
and background coordinates change. Its page now covers the glyphs'
outline Y range; the old page did not. The updated vector regression
fails on the old source. The full Meson suite passes (293 tests).

Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-0ER2GEWZ.

Assisted-by: OpenAI Codex
2026-10-29 19:30:02 -06:00
Behdad Esfahbod 3c0e2b42d5 [vector] Keep SVG path clips in the body's coordinate space (#6471)
The SVG body is flipped from font coordinates once at render time.
The path-clip callback still emitted its own vertical flip from the
older renderer, mirroring the clip relative to the drawing. Remove
that transform, matching rectangle and glyph clips.

Add a paint/show-extents vector regression and generate its expected
SVG with hb-vector. It fails on the old code. The full Meson suite
passes (293 tests).

Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-BZKWFGVZ.

Assisted-by: OpenAI Codex
2026-10-29 19:29:29 -06:00
Behdad Esfahbod 9d044f792d [vector] Clear accumulated content after failed renders (#6363)
Early returns for missing extents, empty PDF paint output, or other
render failures retained content despite the automatic-clear contract.
Move clearing to scope guards in the common draw and paint render
entry points, covering every return for SVG and PDF.

Document the failure behavior. Extend the lifecycle regression to
compare subsequent output with fresh contexts for both formats,
including PDF resources and empty PDF paint renders. The regression
fails before the fix. The full Meson suite passes: 290 passed and one
existing test was skipped. The lifecycle test also passes as C99.

Fixes: https://github.com/harfbuzz/harfbuzz/issues/6358
Assisted-by: Codex
2026-10-10 18:33:11 -06:00
Behdad Esfahbod d45fa42c5a [vector] Restore reset defaults and clear PDF draw resources (#6355)
Vector reset omitted draw colors, paint background, SVG id prefixes,
and custom palette overrides. Restore these defaults while preserving
configuration across clear.

The PDF draw context also kept its opacity resource dictionary and
counter after clear, including automatic clearing after render.
Clear both with the accumulated output so later documents do not carry
resources from earlier renders.

Add regressions for SVG/PDF reset, configuration preservation, custom
palette overrides, and fresh-context PDF equivalence after explicit
clear, reset, and repeated rendering. All three regression groups fail
against the original implementation.

Tested with meson test -C build: 288 passed, 1 skipped, 0 failed.
The vector suite and the issue reproducer pass. The regression also
compiles and runs as C99 with -Wall -Wextra -Werror.

Fixes: https://github.com/harfbuzz/harfbuzz/issues/6342
Assisted-by: OpenAI Codex
2026-10-10 16:59:13 -06:00
Behdad Esfahbod d997502000 [meson] Guard tests for disabled optional libraries (#6335)
* [meson] Skip vector tests when vector is disabled

The vector budget test was built unconditionally even when vector was
explicitly disabled. Its empty dependency supplied neither include paths
nor libraries, breaking otherwise valid reduced-feature builds.

Skip the vector test directory when the library is disabled. Add a Linux
CI build and test pass with vector disabled to cover this configuration.

Reproduced the missing hb.h failure on Linux before the fix. The full
suite with vector, raster, and GPU disabled passed 229 tests with one
skip; all three vector tests passed with vector enabled.

Fixes: https://github.com/harfbuzz/harfbuzz/issues/6277
Assisted-by: OpenAI Codex <codex@openai.com>

* [ci] Remove extra vector-disabled build pass

Keep the fix focused on guarding the vector test directory without adding
another build and test pass to the Linux workflow.

CI-only change; the previously validated Meson guard is unchanged.

Assisted-by: OpenAI Codex <codex@openai.com>

* [meson] Guard dependency fuzzers when subset is disabled

With experimental APIs enabled, dependency fuzzers referenced
libharfbuzz_subset even when the subset library was disabled, causing
Meson setup to fail with an unknown-variable error.

Require subset support when building the dependency fuzzers and when
registering their tests.

Full tests with experimental APIs enabled: 92 passed with subset
disabled, and 247 passed with subset enabled. Both dependency fuzzers
and their 13 tests remain registered in the enabled configuration.

Assisted-by: OpenAI Codex <codex@openai.com>
2026-10-07 12:55:53 -06:00
Behdad Esfahbod 7eec305e71 [vector] Cap serialized document size; un-conflate output from outline
The vector work budget charged both outline traversal and serialized
output bytes, and left embedded bitmaps entirely uncharged -- so a COLR
walk with many sweep-gradient or image layers could emit output that the
outline budget never sees, bounded only by the structural per-node caps
times the node count.

Bound the output with a flat document-size cap instead: hb_vector_buf_t
refuses to grow past HB_VECTOR_MAX_DOCUMENT_SIZE (16 MB) and folds that
into in_error(), so every emission site that already checks it -- path
commands, gradient meshes -- stops cleanly, and image embedding is bounded
for the first time.  clear() resets the sticky flag so a context reused
across renders (the documented clear -> paint -> render loop) recovers
after one over-cap glyph.  vector is one glyph per context, so a flat cap
suffices; SVG's whole document and PDF's (assembled through the same
hb_vector_buf_t) share it.

With output bounded structurally, the work budget goes back to pure
outline traversal: drop the per-command byte charge and the sweep-patch
charge so it no longer conflates two different resources.  It is still
needed on its own -- a CFF charstring can do a lot of work while emitting
little output.

Once the cap trips, remaining appends are dropped, so a truncated document
is malformed; render returns it (or null) rather than looping to build
gigabytes, which is the point.

Add a regression test that paints an oversized image and confirms the
document stays bounded and the context recovers on reuse; it fails if the
cap is removed.

Tests: meson test -C build (275/275 passed); the vector COLR regression
seed completes in 0.85s.

Assisted-by: Claude <noreply@anthropic.com>
2026-08-27 17:38:30 -06:00
Behdad Esfahbod b0606f9d8a [vector] Share rendering work budgets
Store budget policy and live state on vector draw and paint contexts.  Path
sinks borrow the owning context's live counter, making glyph traversal,
serialized path commands, and sweep-gradient patches consume one aggregate
budget throughout a clear-to-render session.

Recharge the configured policy on clear, restore the finite default on
reset, and expose the generic draw and paint budget callbacks for SVG and
PDF contexts.  Add lifecycle and traversal-depletion coverage for both
formats.

Tests: meson test -C build (273/273 passed)

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 17:17:20 -06:00
Khaled Hosny e2588968ae [COLR] Emit fill_glyph for glyph clips with a solid paint
Detect the fill-glyph pattern in the COLRv1 graph: a PaintGlyph whose
child is a (Var)PaintSolid, and emit a single fill_glyph instead of
push_clip_glyph + color + pop_clip.
2026-07-12 22:20:21 +03:00
Khaled Hosny 9ab56e8678 [vector] Implement fill_glyph callback
Fill the glyph shape directly. This simplifies the SVG/PDF output and
fixes the SVG clipping issue with COLRv0 layers and large scale, as
there is no clip now at all.
2026-07-08 20:30:03 +03:00
Khaled Hosny f7a8ef5022 [vector] Fix PDF MediaBox orientation
The MediaBox negated the y-range like SVG viewBox does, but the SVG
output is wrapped in scale(1,-1), and PDF content is emitted y-up.
2026-07-08 20:28:53 +03:00
Khaled Hosny a1b0b0046c [test/vector] Test painting a COLRv0 glyph at a large font scale
Add a test for painting a COLRv0 glyph at large scale. The output shows
the glyph clipped. We will fix this in the next commits.
2026-07-08 20:28:40 +03:00
Khaled Hosny 04f7a60bc4 [draw] Promote quadratic curves to cubics in double precision (#6057)
The fallback quadratic-to-cubic promotion works in single precision.
This results in different rounding values for different architectures.
Promote in double precision instead, which rounds deterministically.

Add a PDF test that exercises this, and make the test runner handle PDF
expectations.

This also effectively reverts d4511648db
2026-07-08 11:11:16 -06:00
Behdad Esfahbod 56ac9c925f [paint] Push COLRv1 root clip under font transform
Make the native COLRv1 paint paths report the root clip in font
units under the font transform. This matches the order used by
fontations and removes the need to pre-scale the HB-OT clip extents.

For the FreeType path, add a helper to convert FT_ClipBox values
back to font units before pushing them under the font transform, and
share that conversion with nested COLR glyph clip boxes.

Update paint and vector expectations for the new transform/clip stack.

Testing:
CCACHE_DISABLE=1 meson test -C build --print-errorlogs

Assisted-by: OpenAI Codex
2026-05-27 10:48:19 +02:00
Behdad Esfahbod e134bb760e [vector] Strip clip-glyph dedup in SVG paint
push_clip_glyph kept a hash + linear-scan cache of emitted
clip paths to dedup repeated clip-glyph uses within a single
COLRv1 paint tree.  With paint contexts being one-glyph per
use, the cache mostly churned; it also made hb_memcmp trip
on sanitizer-null when an empty path matched an equally-empty
existing entry (fuzzer-reached null-deref).

Drop the cache: always emit a fresh <path> + <clipPath> pair
per push_clip_glyph.  Removes defined_paths / content_entry_t
from the paint struct.

Regenerate the two handwriting COLR expected SVGs whose
clip-reuse now produces distinct path defs.

Assisted-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 13:07:05 -06:00
Behdad Esfahbod e5c87cf042 [util/vector] Produce Y-up extents for auto-fit box
include_point used to flip Y so the computed line-box extents
matched the old Y-down internal model.  With the library now
tracking extents in Y-up (font) space and wrapping SVG body
in <g transform="scale(1,-1)">, that flip sent the auto-fit
viewBox above the glyph ink instead of around it.

Regenerate the vector expected SVGs that had captured the
shifted viewBox.

Assisted-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 12:48:23 -06:00
Behdad Esfahbod dfef3f33ae [vector] Global Y-flip in SVG render, eliminate per-glyph flips
SVG render now wraps all body content in a single
<g transform="scale(1,-1)">.  All coordinates inside are
in Y-up (font) space.  The viewBox maps the flipped range.

This eliminates:
- Per-path scale(1,-1) wrappers in draw flush_path_svg
- Per-glyph Y-flip in paint _glyph_impl's push_transform
- The {-xy, -yy, -ty} negation in extents transforms
- Format switch in paint _glyph_impl

paint _glyph_impl now passes the raw transform to
push_transform — no format knowledge needed.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 04:50:21 -06:00
Behdad Esfahbod 2be60ac235 [vector] Strip paint SVG dedup, simplify paint _glyph_impl
Remove content-based color glyph dedup from paint — no more
<defs>/<use> for color glyphs.  Each glyph's paint output is
emitted inline, wrapped by push_transform/pop_transform from
the paint funcs (which emit <g transform="..."> for SVG and
q cm ... Q for PDF).

_glyph_impl is now format-agnostic: extend extents,
push_transform, paint, pop_transform.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 04:40:39 -06:00
Behdad Esfahbod 82734f4ce5 [vector] Simplify draw _glyph_or_fail to just draw + flush
Remove format switch from _glyph_or_fail — it now just extends
extents, calls hb_font_draw_glyph with the context's own funcs,
and flushes.  All format-specific work is in flush_path() and
the draw callbacks.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 04:06:26 -06:00
Behdad Esfahbod 992835a182 [vector] Strip SVG dedup from draw, remove svg_prefix API
Draw SVG output is now flat inline <path> elements — no
<defs>, no <use>, no id references.  Each glyph is an
independent <path d="..." transform="..." fill="..."/>.

Remove hb_vector_draw_set_svg_prefix / get_svg_prefix since
draw output no longer emits any ids.  Paint keeps svg_prefix
for gradient and clip ids which SVG requires.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 03:56:32 -06:00
Behdad Esfahbod c76b3a29a9 [vector] Content-based dedup, remove font reference caching
Replace glyph-ID-based dedup with content-based dedup for SVG
path definitions and color glyph groups.  Path/paint output is
hashed and compared byte-for-byte against existing defs;
identical content reuses the same def regardless of glyph ID.

This eliminates the font reference held by draw/paint contexts
(cached_font + hb_font_reference), which was keeping the font
and its face alive longer than the caller expected.  No font
pointer is stored at all now.

Also removes changed() — with content-based dedup, foreground
and palette changes produce different content that naturally
won't match cached entries.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 03:09:35 -06:00
Behdad Esfahbod bc7e513b2d [vector] Emit SVG coordinates in output-space
Scale all SVG paint and draw coordinates by 1/scale_factor
so paths, clips, gradients, transforms, and images emit
fractional pixel values instead of font-scale integers.

The per-glyph <use> transform now applies only a Y-flip
(scale(1,-1)) instead of scale(1/sf,-1/sf), since the
content is already in output-space.

Revert the 1000000 fill rect bandaid back to 32767 — in
output-space, 32767 pixels is always large enough.

SVG paint callbacks:
- push_clip_glyph: path sink divides by scale_factor
- push_clip_rectangle: coords divided by sx/sy
- push_transform: translation (dx,dy) divided by sx/sy
- gradient coords: divided by sx/sy
- sweep center: divided by sx/sy
- image extents: divided by sx/sy

SVG draw:
- Glyph outline path sink divides by scale_factor

Regenerated all vector test expected outputs.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 01:07:45 -06:00
Behdad Esfahbod 9161a689f6 [vector] Increase paint fill/sweep radius from 32767 to 1000000
The SVG and PDF paint backends use large clipped rectangles
to fill solid colors and gradient backgrounds.  The old
constant 32767 was sized for upem-space coordinates (~2048
max).  After the pixel-space change, coordinates can be in
font-scale (upem * SCALE, e.g. 131072 for a 2048-upem font
at SCALE=64).  Values above 32767 fell outside the fill
rect, causing COLRv0 glyph layers to be clipped at the top.

Bump all instances to 1000000 as a bandaid; proper fix is
to emit paint coordinates in output-space.

Regenerated vector test expected outputs.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 23:07:55 -06:00
Behdad Esfahbod 817d7342c8 [util/vector] Remove all SVG post-processing and CSS var machinery
The paint SVG backend bakes all colors directly — no CSS
var(--hb-...) references remain in the output.  So the util's
SVG parser, var resolver, palette cache, and slice_svg
infrastructure are all dead code.

write_blob is now just fwrite.

Removes ~340 lines: slice_svg, write_slice_resolving_palette_vars,
parse_var_color_expr, find_next_var_expr, find_matching_paren,
emit_css_color, lookup_palette_color, init_palette_color_cache,
skip_ascii_ws, trim_ascii_ws_right, parsed_var_color_expr_t,
and associated palette cache state.

Regenerated vector test expected outputs (color font SVGs
now emit directly without var resolution pass).

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 19:37:22 -06:00
Behdad Esfahbod 8b324b69a7 [util/vector] Remove SVG post-processing; write draw blobs directly
Now that the draw library handles per-glyph foreground fills
and background rects, the util no longer needs to parse and
re-emit SVG for draw blobs.  Draw blobs are written directly
to the output file, same as PDF.

Paint SVG still needs var(--hb-...) resolution, so paint
blobs go through the existing write_slice_resolving_palette_vars
path.

Removes ~120 lines: emit_fill_group_open/close,
emit_draw_body_with_palette, emit_fill_stroke_attrs_for_color,
emit_stroke_attrs, and the draw-specific branches in write_blob.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 19:28:16 -06:00
Behdad Esfahbod 7c54a401c0 [vector-draw] Add foreground/background color API
Add hb_vector_draw_set_{foreground,background}() and getters.

SVG: foreground wraps body in <g fill="rgb(...)">; background
emits a <rect> covering the viewBox before content.

PDF: foreground sets fill color via rg operator before glyph
content; background emits a filled rect at the start of the
content stream.

Default: foreground opaque black, background transparent.

Wire both into the hb-vector CLI util alongside the paint
equivalents.  Remove the old util-side emit_background_rect.

Regenerated vector test expected outputs.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 19:13:33 -06:00
Behdad Esfahbod 533246778a [NEWS] Nit 2026-04-16 18:56:34 -06:00
Behdad Esfahbod d1bb9a595c [util/vector] Drop upem_font; operate in font-scale space
The vector util was creating a separate upem-scaled font clone,
converting all shaping positions from font-scale to upem, and
setting scale_factor to upem/font_size to undo that on emit.

Simplify: use the caller's font directly.  Positions from
shaping are stored as-is (in font-scale = pixel * SCALE).
scale_factor is just SCALE (= 1 << subpixel_bits), so the
lib divides by SCALE on emit to produce pixel-space output.

This removes ~40 lines: the upem_font creation, variation
coord copying, scale_to_upem_x/y helpers, and the complex
upem_per_pixel margin/stroke conversions.  Margin conversion
simplifies from margin * upem * SCALE / x_scale to just
margin * SCALE.

Regenerated vector test expected outputs for the new
coordinate space.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 17:11:45 -06:00
Behdad Esfahbod 80807b128d [vector] Normalize color-line stops to [0, 1] before emit
SVG <stop offset="..."> requires offsets in [0, 1].  Out-of-
range stops were silently clamped by browsers, producing
visually wrong gradients for COLR fonts whose color lines
extend past those bounds.

Use the centralized hb_paint_normalize_color_line() to rescale
stops in place; shift the gradient axis (linear) / centers and
radii (radial) / start+end angles (sweep) by (mn, mx) along
the gradient axis to keep the visible gradient in the same
place.

Drop the now-unused local hb_vector_color_stop_cmp -- the
shared normalize helper sorts internally.

Regenerate the two affected basic.tests expected SVGs.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 17:31:37 -06:00
Behdad Esfahbod ce4199c303 [vector] Fix linear-gradient anchor reduction
hb-vector-paint emitted (2*x1 - x2, 2*y1 - y2) as the SVG end
point -- the reflection of P2 about P1, not the projection.
For COLR linear gradients the SVG end should be P1 projected
onto the line through P0 perpendicular to (P2 - P0); that's
the gradient's actual axis (perpendicular to the rotation
line).  With the old formula, fonts with rotation references
far from the color line ended up with massively over-scaled
gradient axes -- e.g. BungeeSpice's ABC showed solid red where
raster shows a full red->yellow gradient.

Add hb_paint_reduce_linear_anchors() to hb-paint.hh as the
shared canonical implementation (matches hb-raster-paint and
hb-cairo) and use it from hb-vector-paint.

Regenerate the two affected basic.tests expected SVGs; the
new gradient endpoints are now consistent with the cairo /
raster outputs.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 15:44:10 -06:00
Behdad EsfahbodandClaude Opus 4.6 ca75fd5ff9 [vector] Drop hb_vector_{draw,paint}_{set,get}_flat (#5927)
Flat mode disabled glyph reuse / caching to produce inline-
everything SVG output.  Its main use case was making
hb-vector SVGs safe to concatenate side-by-side in a single
document, but the recently-added svg_set_prefix API solves
that without giving up the cache.  No remaining callers in
tree (util/vector-output.hh's --flat option goes too).

Removes:
  - hb_vector_draw_set_flat / get_flat
  - hb_vector_paint_set_flat / get_flat
  - the `flat` field on both contexts
  - all `if (flat)` / `bool can_cache = !flat` branches
  - the --flat option in util/vector-output.hh
  - the four "flat*" expected files in test/vector/basic
  - the flat_draw / flat_paint params in the vector fuzzer
  - sections-list entries

meson test --no-rebuild green (241/241).

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 15:03:43 -06:00
Behdad Esfahbod 5e6ae10f62 [raster][vector] Remove SVG-font glyph source rendering
Remove support for rendering glyphs whose source is the OpenType
SVG table, in both hb-raster and hb-vector.  SVG is a large format
and our in-tree parser/renderer was half-finished; the complexity
was not justified by the partial coverage.

This is source-side only: hb-vector's SVG *output* backend (used
to serialize outline / COLR glyphs to SVG) is unaffected.  Fonts
that have an SVG table will still load; glyphs served only by that
table will not render from these libraries.

hb-raster:
  - Delete the hb-raster-svg-* file cluster (11 .cc/.hh files plus
    hb-raster-svg.hh: parse, base, bbox, clip, color, defs,
    defs-scan, fill, gradient, parse, render, use, context).
  - Drop svg_glyph/svg_font/svg_palette state from
    hb_raster_paint_t and the HB_PAINT_IMAGE_FORMAT_SVG branch in
    the image callback.
  - Drop hb_raster_paint_push_clip_path + hb_raster_svg_path_func_t
    (only used from the SVG renderer).
  - Drop zlib dependency (gzip decompression was only used for the
    SVG table reader; libpng brings its own zlib linkage for PNGs).

hb-vector:
  - Delete hb-vector-svg-subset.{cc,hh} (extracted font-SVG glyph
    for embedding in SVG output).
  - Remove the HB_TAG('s','v','g',' ') branch from the image paint
    callback; PNG embedding remains.
  - Drop svg_image tracking state from hb_vector_paint_t
    (current_color_glyph_has_svg_image, current_svg_image_glyph,
    current_face, svg_image_counter, subset_body_scratch).
  - Simplify the color-glyph cache to store just def_id instead of
    packed (def_id, image_like).
  - Drop hb_svg_append_image_instance_translate.

Build wiring: src/meson.build, CMakeLists.txt, harfbuzz-world.cc
updated to match.  Test suite: remove SVG-specific cases from
test/api/test-raster.cc and the handwriting-{svg,picosvg} vector
tests + expected fixtures.

~5700 LOC deleted, ~10 LOC added.  Full test suite (240 tests)
passes.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-11 22:46:35 -06:00
Behdad Esfahbod 8e6b9ecf4e [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
2026-03-12 01:49:07 -03:00
Behdad Esfahbod 8fc1d1bfd1 [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
2026-03-12 01:46:46 -03:00
Behdad Esfahbod 00da68ef5c [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
2026-03-11 20:23:51 -03:00
Behdad Esfahbod 90b252507d Update vector test expectations 2026-03-03 17:21:02 -03:00
Behdad Esfahbod ce292d3c10 [test/vector] Add Nastaliq Y-offset regression 2026-03-03 15:45:33 -03:00
Behdad Esfahbod ddafa6637c [test/vector] Handle empty option arrays in recorder 2026-03-03 15:45:28 -03:00
Behdad Esfahbod 12989d4e6a [util] Align vector/raster defaults with view and update vector baselines 2026-02-26 16:54:28 -03:00
Behdad Esfahbod 7929d24ed2 [test] Rename hb-vector-compare to hb-vector-svg-compare 2026-02-24 12:55:11 -03:00
Behdad Esfahbod 6653d62480 [test/util] Rename draw suite and tools to vector 2026-02-24 10:17:18 -03:00