Commit Graph
21 Commits
Author SHA1 Message Date
Behdad EsfahbodandClaude Opus 5.5 d17c0dbe97 [raster] Fail hb_raster_draw_render() when over the pixel budget (#6616)
#6521 made hb_raster_draw_render() skip the scanline sweep when the
estimated pixel work exceeds the budget, but it still returned a
cleared image, so callers could not tell a budget failure from an
outline that legitimately covers nothing.

Check the budget before allocating the image and return NULL, the
same way render already reports allocation and configuration
failures.  The accumulated edges are still cleared by the existing
scope guard.  All in-tree callers (raster paint masks, hb-raster
utilities) already handle a NULL result.

test_budget_sweep now expects NULL for the over-budget case.  Full
meson test passes.

Follow-up to #6521

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 20:29:35 -06:00
Behdad EsfahbodandClaude Opus 4.8 8b5d290611 [raster] Bound embedded PNG size and charge the scanline sweep (#6521)
* [raster] Bound embedded PNG dimensions before decoding

hb_raster_paint_image() handed a font's embedded sbix/CBDT PNG to
deserialize_from_png(), which allocated and filled the whole image at
whatever size the PNG's IHDR declared (up to two buffers at the 1 GiB
HB_RASTER_MAX_BUFFER_SIZE cap each), and only charged src_width *
src_height against the pixel budget after the decode had finished.  A
33 KB sbix font with a blank 16384x16384 1-bit PNG therefore made the
raster backend allocate and touch ~2 GiB of memory per painted glyph,
independent of the requested output size.

Surfaces whose size comes from font data are already clamped to
HB_RASTER_MAX_AUTO_DIMENSION (4096 per side); the PNG decoder was not.
deserialize_from_png() gains a max_dimension parameter checked right
after png_get_IHDR(), before any allocation, and the paint callback
passes HB_RASTER_MAX_AUTO_DIMENSION.  The public
hb_raster_image_deserialize_from_png_or_fail() keeps its INT_MAX
behaviour through the default argument, matching the limit's documented
intent that explicitly requested sizes are not bounded.  With the cap a
decode is at most 16M pixels, whose cost fits the 2^26 pixel budget, so
the later charge_work() is adequate.

Behaviour change: sbix/CBDT PNG glyphs wider or taller than 4096 pixels
are no longer painted by the raster backend (the callback returns false,
as for any undecodable image).  The cairo backend has the same weakness
on a different code path and was already fixed separately (#6497).

test_image_png_size() in test/api/test-raster.cc paints a 4096- and a
4097-pixel-wide PNG and checks that only the former lands; it fails on
the unpatched library.

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

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

* [raster] Charge the scanline sweep to the pixel budget

hb_raster_draw_render() walks every active edge once per pixel row it
spans, a product (edges times rows) that neither HB_RASTER_MAX_DRAW_EDGES
(2^20) nor HB_RASTER_MAX_AUTO_DIMENSION (4096 rows) bounds, and that
nothing charged: the outline budget only sees traversal and curve
flattening.  A 10 KB glyf font of 199,800 full-height straight edges
therefore cost tens of seconds of CPU for a single render.

PR #6244 added the precharge for the paint path but sized it by the paint
*surface*, which comes from the font's declared glyph extents, while the
mask rasterizer auto-sizes the mask from the outline itself; a 1x1
ClipBox or empty bounding box slipped the full-height sweep past it.  The
direct draw path had no check at all.

This charges the sweep inside hb_raster_draw_render(), where the extents
really rendered are known, estimating it with the existing
hb_raster_draw_get_pixel_work() helper, now clamped to those extents (the
fixed ones, else the 4096 auto cap) rather than taking them as arguments.
The sweep is skipped when the estimate exceeds the per-session pixel
allowance raster-paint already uses, max(HB_BUDGET_RASTER_PIXELS,
HB_BUDGET_RASTER_PAINT_PASSES * width * height).  The two paint callers
now use the mask-relative estimate, and hb_raster_paint_attach_clip_rdr()
seeds HB_BUDGET_UNLIMITED into the mask rasterizer when the paint policy
is unlimited, so opting out of limits still disables the check.
hb_raster_draw_get_pixel_work() is HB_INTERNAL, so dropping its two
arguments changes no public API/ABI.

Behaviour change: an over-budget render returns a blank image of the
computed size (documented in the API comment) rather than NULL.  Letting
HB_BUDGET_UNLIMITED switch the check off matches the paint pixel budget's
semantics and keeps the edge and dimension caps active; whether this
bound should instead be an unconditional structural cap is a maintainer's
choice.

Testing: the finding's reproducer flips from BUG to OK, its attack render
dropping from ~39 s to 0.05 s of CPU while the control is unchanged; the
api suite passes under both the optimized and the ASan/UBSan build.
test_budget_sweep in test/api/test-raster.cc covers the direct draw path
with auto and fixed extents and the small-surface paint-clip path.

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

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

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 22:52:47 -06:00
Behdad Esfahbod df19e5053b [raster] Keep the interior of transformed rectangle clips (#6470)
The point-in-quad test rejected the interior side of each edge for
both winding orders. Reverse the comparisons for rectangle and mask
intersections, and clarify how the edge normal depends on winding.

Add a regression for skew, reflected skew, and rotation, with one or
two nested rectangle clips to exercise both intersection paths. The
center pixel was transparent before the fix. The full Meson suite
passes (293 tests).

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

Assisted-by: OpenAI Codex
2026-10-29 19:07:51 -06:00
Behdad Esfahbodandnussaiba shaikh 404378164a [raster] Keep paint surfaces and budgets tied to the active root (#6464)
Changing extents during painting could allocate small group surfaces
against the original root and clip, causing out-of-bounds reads and
writes. Keep all session surfaces at the root's extents.

Use the same extents for group clear charges and pixel budget
recharges, so changing the configured rectangle cannot undercount
work on the active image. Document that render clears extents rather
than deferring a mid-session change to the next image.

Adapt the pixel regression from #6450 and check the render lifecycle.
Add an internal test for new and cached group clear charges and
budget recharging, using small buffers.

Reproduced the heap overflow on main under ASan. The budget test
fails with #6450. With this change, both raster test targets pass
under ASan+UBSan and the full Meson suite passes (274 tests).


Assisted-by: OpenAI Codex

Co-authored-by: nussaiba shaikh <nussaibah@bugqore.com>
2026-10-29 17:04:03 -06:00
Behdad Esfahbod 94e9073251 [raster] Restore background and palette defaults on paint reset (#6361)
Paint reset omitted the background and palette, leaving settings from
the previous render despite its documented default-state guarantee.
Reset both fields and document that clear preserves these settings.

Add coverage for clear, reset, and rendering with a recycled image.
The regression fails before the fix. The full Meson suite passes:
290 tests passed and one existing test was skipped.

Fixes: https://github.com/harfbuzz/harfbuzz/issues/6356
Assisted-by: Codex
2026-10-10 18:32:30 -06:00
Behdad EsfahbodandArthur031221 f20f4c20bc [raster] Fix background color and stride handling (#6310)
* [raster] Fix background color premultiplication and stride handling

ensure_initialized()'s background fill had two bugs:

1. The background pixel was built with HB_COLOR(blue, green, red,
   alpha), which packs channels through HB_TAG's bit layout
   (b<<24 | g<<16 | r<<8 | a). Every other pixel in this buffer is
   premultiplied BGRA32 (b | g<<8 | r<<16 | a<<24, see hb-raster.hh's
   SRC_OVER comment: "premultiplied src over premultiplied dst"), so
   the background was both unpremultiplied and in the wrong byte
   positions: a half-transparent red background (blue=0, green=0,
   red=255, alpha=128) filled the buffer with 0x0000ff80 instead of
   the correct premultiplied 0x80800000.

2. The fill then wrote that pixel into a contiguous run of
   width * height uint32_t slots starting at the buffer origin,
   assuming every row is packed at exactly width * 4 bytes. Once a
   caller sets a stride wider than that (row padding, alignment,
   embedding the glyph image inside a larger atlas), the fill drifts
   across row boundaries after the first row, leaving later rows
   partially uninitialized or wrongly colored.

Build bg with color_to_premul_pixel(), the same helper solid fills
already use, and fill row by row using the actual stride.

Added two regression tests in test/api/test-raster.cc:
test_background_premultiplied (default stride, half-alpha background,
checks the stored pixel is premultiplied) and test_background_stride
(stride much wider than width * 4, opaque background, checks every
row).

Assisted-by: Claude

* [raster] Handle unaligned background strides

A caller-supplied stride does not have to be word-aligned, so casting
each row to uint32_t * can perform misaligned stores.

Use hb_packed_t<uint32_t> for the row writes, and exercise an odd
stride in the regression test with byte-wise pixel checks.

Tests: meson test -C build

Assisted-by: Codex <noreply@openai.com>

---------

Co-authored-by: Arthur031221 <levi74108520963@gmail.com>
2026-10-01 20:11:49 -06:00
Behdad Esfahbod c87f86eff1 [raster] Widen overflowing scanline accumulators (#6273)
The analytic rasterizer stored every row's area and cover in 32- and
16-bit accumulators. Highly overlapping outlines could exceed those
ranges and trigger signed overflow before coverage was clamped.

Keep compact SIMD accumulators for rows with at most 127 active edges.
Lazily use 64-bit scalar accumulators for denser rows, where the narrow
cover bound no longer holds. Add regressions for area overflow and
cover wrapping.

Tested with meson test -C build, the ASan/UBSan raster API test, all
raster fuzzer corpus chunks, and the supplied reproducer.

Assisted-by: OpenAI Codex <noreply@openai.com>
2026-09-21 11:58:49 -06:00
Behdad Esfahbod dfee0d4380 [raster] Split outline and pixel budgets; drive them via public API
raster-paint reached into the rasterizer through two internal functions:
hb_raster_draw_set_external_work() to inject a shared work counter and
hb_raster_draw_get_edge_work() to read scanline cost back.  Both are gone.

The paint session now carries two budgets for two different resources:

 - The outline budget bounds glyph outline traversal and curve
   flattening.  It is the public draw/paint budget: raster-paint seeds the
   rasterizer with hb_draw_set_budget() before each glyph and reads the
   remainder back with hb_draw_get_budget_remaining(), so a whole COLR
   walk shares one budget without touching rasterizer internals.

 - The pixel budget bounds scanline and fill work, which scales with
   surface area, not outline complexity.  It is charged from the public
   extents of the rendered mask (and other pixel areas) and keeps its
   surface-sized default.  An unlimited outline policy disables it too.

Folding pixel work into the outline budget was why the default had to be
inflated by the surface passes; the two now live apart, so HB_BUDGET_GLYPH
is a pure outline budget and the surface scaling belongs to the pixel
budget.  Standalone hb_raster_draw is unchanged: it draws once, so its
scanline work needs no session budget, only its structural caps.

COLR drives only push_clip_glyph, whose seed/emit/readback is a single
self-contained step, so the aggregate outline budget is always conserved
across a COLR walk.  The push_clip_path_start/end pair spans client draw
calls; like push/pop it must be balanced by the client for the outline
budget to reconcile, and each render is still bounded by the rasterizer's
structural caps regardless.  hb_draw_set_budget() clamps a negative seed
to zero, so outline exhaustion does not latch across glyphs; an empty
mask can then be rendered once more, but that path is bounded by the
pixel budget.

Add a regression test that paints a real multi-layer COLR glyph into a
1x1 surface (unbounded pixel budget) and checks the outline budget both
aggregates the layers' work and bounds the walk.

Tests: meson test -C build (275/275 passed); the raster and gpu COLR
regression seeds complete in 0.40s and 0.44s.

Assisted-by: Claude <noreply@anthropic.com>
2026-08-27 16:53:55 -06:00
Behdad Esfahbod c7797701c0 [raster] Share rendering work budgets
Store budget policy and live state on raster draw and paint contexts.
Make outline traversal and curve flattening borrow the paint session's
live counter, while standalone draw keeps its own finite session counter.

Preserve raster's dynamic surface-sized default, resolving it only after
initialization and never replacing concrete or unlimited client policy.
Recharge on clear and restore DEFAULT on reset. Add lifecycle and lazy
default coverage.

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

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 17:22:05 -06:00
nussaibah shaikh 8c9307b59a [raster] clamp coordinates before converting to int (#6090)
* [raster] clamp coordinates before converting to int

the transformed glyph extents and the flattened segment endpoints are floats derived from int32 inputs and can land far outside the int range, so the conversions were undefined and the extent subtraction overflowed on top of that. saturate through a shared helper and widen the subtraction.

* [raster] generalize int32 saturating cast into hb_clamp_to<>

Move the raster-local hb_raster_clamp_to_int32 helper into hb-algs.hh
as a generic hb_clamp_to<T> that saturates a floating-point value to
the range of any integer type, and use hb_clamp_to<int32_t> at the
three raster call sites.

Signed-off-by: nussaiba shaikh <nussaibah@bugqore.com>

---------

Signed-off-by: nussaiba shaikh <nussaibah@bugqore.com>
2026-07-29 02:59:29 -06:00
nussaibah shaikh 387ab23f7f [raster] Reject non-finite texel coords in hb_raster_paint_image (#6082)
The range test guarding the bilinear sampler is comparison-based, so NaN
coordinates pass it and reach (int) floorf() in
hb_raster_sample_bilinear_premul, where only x1/y1 are clamped.  The
determinant guard has the same shape, so a transform that overflows to
infinity is accepted and 1/inf turns inf * 0 into NaN.
2026-07-21 18:40:41 -06:00
Behdad EsfahbodandClaude Opus 4.6 591748f1e1 [draw][paint] Make get_funcs instance-aware; add get_format
The six public draw / paint funcs getters across raster,
vector, and gpu used to be singletons:

  hb_raster_draw_get_funcs (void)
  hb_raster_paint_get_funcs (void)
  hb_vector_draw_get_funcs (void)
  hb_vector_paint_get_funcs (void)
  hb_gpu_draw_get_funcs (void)
  hb_gpu_paint_get_funcs (void)

That works fine for raster and gpu, where there's a single
backend per type, but breaks for the vector backend where
SVG and PDF have separate funcs sets — callers using the
public getter always got SVG funcs and silently produced
SVG markup inside PDF output streams when invoking the new
arbitrary-path clip API on a PDF context.

Make all six take a const-pointer to the relevant context
instance.  vector_paint dispatches on paint->format; the
other five just use the instance for symmetry today (room
for future format-style dispatch later).

Also expose hb_vector_draw_get_format / hb_vector_paint_
get_format public getters so callers (or third parties)
can ask a vector context for its format without poking at
internals.

Also: hb_vector_paint_push_clip_path_end now wraps the
emitted SVG clipPath's path in transform="scale(1,-1)" so
free-form arbitrary clips at base body level land in the
same coord system the per-glyph <use scale(_,-sy)>
wrappers establish.  Without this, free-form clip-path
arbitrary outlines emitted between paint_glyph calls came
out mirrored across the X axis.

Updates internal callers (hb_vector_paint_glyph,
hb_pdf_emit_glyph_path, hb_raster_paint_push_clip_path_
start, hb_gpu_paint_push_clip_path_start, the GPU pen
adapter), util sites (raster-output.hh, vector-output.hh,
gpu/demo-font.cc), API tests (test-raster.cc, test-gpu.cc),
and the docs section index.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:21:49 -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 6e45962326 [raster] Guard direct SVG paint callbacks
The raster SVG image callback assumes hb_raster_paint_glyph()
seeded svg_font/svg_glyph state before the paint protocol
reaches HB_PAINT_IMAGE_FORMAT_SVG. Clients can also call
hb_font_paint_glyph_or_fail() directly with
hb_raster_paint_get_funcs(), which leaves svg_font unset and
crashes in hb_raster_svg_render().

Return false when the callback is reached without that
wrapper-only state, and cover the direct API path with a
regression test.

Testing: CCACHE_DISABLE=1 meson test -C build
Fixes https://github.com/harfbuzz/harfbuzz/security/advisories/GHSA-ggh9-cgg2-r4vr
Assisted-by: OpenAI Codex <codex@openai.com>
2026-03-26 14:41:14 -06:00
Khaled Hosny b327f0c672 [tests] Fix warning when building without zlib
../test/api/test-raster.cc:67:1: warning: unused function 'render_svg_glyph' [-Wunused-function]
   67 | render_svg_glyph (hb_face_t      *face,
      | ^~~~~~~~~~~~~~~~
1 warning generated.
2026-03-12 17:36:39 +02:00
Behdad Esfahbod b7e4f5fd62 [raster] Support gzip-compressed SVG documents
The raster SVG path treated hb_ot_color_glyph_reference_svg()
blobs as plain XML, even though SVG table documents may
legally be gzip encoded. That caused hb-raster to reject
fonts that store compressed SVG documents.

Detect gzip payloads in the raster SVG renderer, reject
documents whose advertised uncompressed size exceeds
HB_SVG_MAX_DOCUMENT_SIZE, and inflate the rest through
zlib while keeping the same size cap enforced during
decompression. Plain SVG documents are also bounded by the
same 16 MiB limit.

Add a raster API regression that renders the existing
plain and compressed noto_handwriting SVG fixtures and
verifies identical output.

Testing:
- meson test -C build

Assisted-by: OpenAI Codex
2026-03-11 14:18:31 -03:00
Behdad Esfahbod 5ea04afcda raster: adopt create_or_fail and strict render semantics 2026-02-24 04:46:51 -03:00
Behdad Esfahbod 742f79da08 raster: remove extents_from_glyph_extents API
Remove hb_raster_extents_from_glyph_extents and switch callers to the new transform-aware set_glyph_extents APIs on raster draw/paint. Also add test coverage for transformed glyph extents and update section symbol listings.
2026-02-23 12:10:41 -03:00
Behdad EsfahbodandKhaled Hosny 99a84d7f02 Update src/hb-raster.h
Co-authored-by: Khaled Hosny <khaled@aliftype.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 35ba082aa2 hb-raster: add test_glyph to test-raster
Renders glyph IDs 2-5 from fonts/adwaita.ttf at upem scale, writes
PGMs to /tmp/ for visual inspection, and asserts non-zero coverage.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 c9d459b8e9 hb-raster: fix tile bounds clipping; move test to test/api/
Fix out-of-bounds pixel write in rasterize_tile: border tiles that
extend past the image edge were writing px-x_origin beyond the row
stride, wrapping into the start of the next row and overwriting
correctly-rasterized pixels with zero.  Pass px1/py1 (exclusive
bounds) to rasterize_tile and clip the pixel loops accordingly.

Also: drop unused ex0 in pass 2 of tile assignment; move test-raster
from src/ to test/api/ and convert it to the GLib g_assert/hb_test_add
framework.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00