Commit Graph
21277 Commits
Author SHA1 Message Date
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 99ffe47b10 [vector] Normalize extents in set_extents
Accept caller-supplied extents in any orientation and
normalize to min-corner origin with positive width/height.
Lets callers use the glyph-extents-style {top, -height} form
matching hb_glyph_extents_t convention, and brings set_extents
in line with the Y-up internal model the paint/draw contexts
now use.

Assisted-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 12:48:13 -06:00
Behdad Esfahbod b3c3e6a818 [vector] Add public hb_vector_draw_new_path() API
Exposes the path separation as a public function so callers
using the draw funcs directly can separate glyph outlines.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 11:41:44 -06:00
Behdad Esfahbod 750df72e2b [NEWS/docs] Update for vector API changes
- Remove draw svg_prefix from New API list and sections.txt
- Document pen_x/pen_y removal, dedup removal, _glyph
  simplification in the Vector library section

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 11:36:05 -06:00
Behdad Esfahbod 31c518df7a [raster] Push/pop transform in paint _glyph_impl, not ensure_initialized
Remove the initial base_transform push from ensure_initialized.
Instead, _glyph_impl pushes/pops the positioning transform via
the paint funcs, matching the vector paint model.

current_transform() returns identity when the stack is empty.
pop_transform allows popping to empty (for the final glyph-level
pop).

This makes changing set_transform between glyph calls work
correctly — the transform is applied fresh for each glyph.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 11:31:12 -06:00
Behdad Esfahbod 36174584ba [vector/raster/gpu] Document _glyph functions as convenience
Each _glyph_or_fail function now documents what it is
equivalent to in terms of the underlying font draw/paint
API + the context's own funcs.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 11:19:55 -06:00
Behdad Esfahbod cf57fa72a7 [vector] Unify ensure_initialized, remove PDF duplicate
Remove hb_pdf_paint_ensure_initialized — it was identical to
paint->ensure_initialized().  All PDF callbacks now call the
common method.  Remove redundant call from _glyph_impl since
paint callbacks handle it.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 11:13:12 -06:00
Behdad Esfahbod 127f060681 [raster] Move decl around 2026-04-18 10:58:44 -06:00
Behdad Esfahbod 57e056a22c [vector] Return nullptr from paint get_funcs for invalid format
Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 10:58:31 -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 bc3713f5f5 [vector] Add new_path(), make _glyph_or_fail trivially thin
_glyph_or_fail is now: extend extents, new_path, draw.
No format switch, no flush — just accumulate.

new_path() flushes any pending path and starts a fresh one,
separating glyph outlines so they don't interact under fill
rules.

Flush also happens on foreground change and at render time,
so the last pending path is always emitted.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 04:14:34 -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 786bf59b95 [vector] Remove global transform wrapper from SVG paint render
The transform is transient per _glyph call, not a global
state for the entire document.  Each glyph's <use> already
carries its own transform from the caller's set_transform.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 03:46:30 -06:00
Behdad Esfahbod f469f05a40 [vector/raster] Remove pen_x/pen_y from _glyph APIs
These are convenience APIs, not typesetting APIs — positioning
is the caller's responsibility via set_transform().  This
matches the GPU API which never had pen parameters.

All callers updated to use set_transform with pen position
before each _glyph call.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 03:32:33 -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 edc8c18ef8 Link to harfbuzz-world.cc playground in README
Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 21:01:25 -06:00
Behdad Esfahbod 0830c4b449 [gpu/demo] Use union of ink + logical extents for autofit
Nastaliq and other deep-descender scripts were clipped because
the fit used only logical extents (one em height).  Now uses
the union of ink and logical extents so all ink is visible.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 20:19:02 -06:00
Behdad Esfahbod 036a5ad86f [gpu] Add font feature support to demo
- demo_buffer_set_features() sets global features for hb_shape
- web_set_features() wasm export parses comma-separated features
- Shell message handler accepts { kind: "features", value }
- Features applied to all shape calls in demo_buffer_add_text

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 20:05:43 -06:00
Behdad Esfahbod fd0a18dcc2 [gpu/web] Add key message handler for embed mode
Allows the host page to simulate keypresses (e.g. 'b' for
dark mode toggle) via postMessage { kind: 'key', value }.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 18:59:43 -06:00
Behdad Esfahbod b2a09c2948 Merge pull request #5945 from harfbuzz/fuzzer-fixes-apr17
Fuzzer fixes apr17
2026-04-17 16:53:00 -06:00
Behdad Esfahbod 77b0369078 [hb-vector.png] Fixup 2026-04-17 16:50:36 -06:00
Behdad Esfahbod bb2b4bfbc2 [fuzzing] Cap font scale in raster fuzzer to avoid OOM
Malicious fonts with extreme scale values cause huge pixel
buffer allocations across multiple push_group surfaces.  The
library correctly bounds individual surfaces at 1 GB, but
stacked group surfaces can exceed the fuzzer RSS limit.

Cap the font scale to 1000 in the harness so the exercised
surface stays within fuzzer memory constraints.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 16:29:06 -06:00
Behdad Esfahbod e4d1127676 [test] Add one more fuzzer font 2026-04-17 16:23:42 -06:00
Behdad Esfahbod 0f9e0ba2a5 [gpu] Fix signed integer overflow in extents computation
Glyph extents from malicious fonts can have extreme values
that overflow when computing x_bearing + width or the
width/height deltas in hb_gpu_paint_encode.  Widen to int64_t
before the arithmetic.

Fixes: https://oss-fuzz.com/testcase-detail/crash-8a9ba2164fd1e6a6fd7a8ed566cf4d0350c1357e

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 16:20:14 -06:00
Behdad Esfahbod 82cba44644 [gpu] Fix out-of-bounds read in emit_all_clip_sub_blobs
When clip nesting exceeds HB_GPU_PAINT_MAX_CLIP_DEPTH (3),
push_clip_glyph sets unsupported=true but still increments
clip_depth.  emit_all_clip_sub_blobs then iterated clip_depth
without capping, reading past the fixed-size clip_stack array.

Cap the iteration at HB_GPU_PAINT_MAX_CLIP_DEPTH.

Fixes: https://oss-fuzz.com/testcase-detail/6573156621156352

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 16:17:42 -06:00
Behdad Esfahbod d689790d5d hb-shape.png: Optimize 2026-04-17 15:58:27 -06:00
Behdad Esfahbod d5819716b3 Add hb-shape.png 2026-04-17 15:53:57 -06:00
Behdad Esfahbod e9c62e1ad3 [vector] Center logo to match others 2026-04-17 15:43:28 -06:00
Behdad Esfahbod 13cac4f57e [NEWS] Document GPU shader API changes
Coverage out parameter, premultiplied gradient interpolation,
foreground alpha fix, and the shader signature change.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 15:18:58 -06:00
Behdad Esfahbod af73332390 Merge pull request #5943 from harfbuzz/gpu-gamma
Fix hb-gpu gamma / stem-darkening interaction with color glyphs
2026-04-17 15:17:29 -06:00
Behdad Esfahbod 86eab665a8 [gpu] Update hb_gpu_paint() docs for coverage out parameter
Update the signature, parameter documentation, and sample code
to reflect the new edge coverage out parameter.  Stem darkening
example now shows the coverage-based correction pattern.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 14:13:00 -06:00
Behdad Esfahbod d8b1d6bb57 [gpu] Return edge coverage from hb_gpu_paint(); apply stem darkening and gamma to it
hb_gpu_paint() now takes an out float coverage parameter that
receives the maximum slug coverage across all paint layers.
This lets the caller apply stem darkening and gamma correction
to edge antialiasing only, without distorting interior paint
colors (gradients, semi-transparent layers).

The demo shaders (GLSL, MSL, HLSL, WGSL) use the new parameter
to share a single coverage-correction block between draw and
paint paths.  Also fixes the blend func to GL_ONE for correct
premultiplied compositing.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 14:09:34 -06:00
Behdad Esfahbod 43b8568139 [vector] Fix potential use-after-free in check_font()
Reference the new font before destroying the old one, in case
both pointers are the same object.  In practice the caller always
holds its own reference so this cannot trigger, but the pattern
is safer and silences Coverity CID 503195/503196.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 13:43:21 -06:00
Behdad Esfahbod a5bd445578 [vector] Expand section docs with usage examples and caveats
Add sample code for both draw and paint contexts, note that
render clears accumulated content (get extents first), and
document the semi-transparent overlap compositing behavior.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 13:36:57 -06:00
Behdad Esfahbod ba66cfcc0b [util] Document --background=none in help string
Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 13:36:57 -06:00
Behdad Esfahbod 0272f02af7 [vector] Adjust duplicate header and improve docs 2026-04-17 13:36:57 -06:00
Behdad Esfahbod 15a451c205 Merge pull request #5941 from harfbuzz/vector-touchups
vector touchups
2026-04-17 13:35:54 -06:00
Behdad Esfahbod 3c9fe077b1 [gpu] Interpolate gradient color stops in premultiplied space
The OpenType COLR spec requires gradient interpolation in
premultiplied space.  The shader was interpolating unpremultiplied
stop colors directly, which produced washed-out transitions when
a stop fades toward transparency (e.g. opaque red to transparent
gives desaturated pink instead of red at decreasing opacity).

Premultiply stop colors before mix(), unpremultiply the result.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 03:57:17 -06:00
Behdad Esfahbod 178402d7b4 [gpu/demo] Fix blend func for premultiplied paint output
hb_gpu_paint() returns premultiplied RGBA, as documented in
hb-gpu.cc.  The GL demo was using GL_SRC_ALPHA which
double-applied alpha, darkening semi-transparent colors.

Use GL_ONE as the API docs prescribe.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 03:42:55 -06:00
Behdad Esfahbod af21e3c227 [vector/raster] Remove is_foreground substitution; pass foreground directly
These backends use immediate-mode foreground: they pass their
foreground color to hb_font_paint_glyph() and use the resolved
color from the callback directly.  The is_foreground substitution
was redundant (a no-op when foreground alpha is 255) and prevented
semi-transparent foreground alpha from working correctly.

Now the foreground is passed as-is (including its alpha) to
hb_font_paint_glyph(), and the color callback ignores
is_foreground — the color already carries the correct value.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 03:35:29 -06:00
Behdad Esfahbod dba796ff28 [gpu] Fix foreground alpha: only modulate alpha channel, not RGB
The previous commit scaled all four components of the foreground
uniform by the baked paint-tree alpha, which darkened the RGB
towards black.  The foreground is unpremultiplied, so only the
alpha channel should be modulated; the shader's later premultiply
step handles the rest.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 03:24:30 -06:00
Behdad Esfahbod 9031600cc2 [paint] Document is_foreground semantics on color callback and color stops
Explain the two valid usage modes: immediate (use @color directly)
and deferred (substitute own foreground RGB, combine alpha).  Note
that deferred mode requires passing an opaque foreground to
hb_font_paint_glyph().

hb_color_stop_t defers to hb_paint_color_func_t for the
is_foreground/color contract.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 03:17:50 -06:00
Behdad Esfahbod f546f73b1f [gpu] Preserve paint-tree alpha for foreground entries in shaders
When is_foreground was set, the shader replaced the entire baked
color with the foreground uniform, discarding the paint-tree alpha
encoded in the color's alpha channel.  This meant COLRv1 glyphs
with a separate alpha on their foreground color sentinel would
lose that alpha at render time.

Now the shader multiplies the foreground uniform by the baked alpha
(ct.a / 32767 for LAYER_SOLID, b.a / 32767 for gradient stops),
preserving the paint-tree modulation.

Updated all four shader languages (GLSL, HLSL, MSL, WGSL) and
regenerated the .hh artifacts.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 03:11:59 -06:00
Behdad Esfahbod 7f333335b1 [font] Synthesized mono paint: pass is_foreground=true, alpha=255
The synthesized fallback should signal is_foreground=true so
backends like cairo that handle foreground specially can detect
it.  Pass alpha=255 since the caller passes opaque foreground
to paint_glyph; backends that care about foreground will apply
their own alpha.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 02:44:20 -06:00
Behdad Esfahbod d17dbe74d1 Revert "[vector] Remove is_foreground substitution from paint backends"
This reverts commit 9d1ce8c942.
2026-04-17 02:40:19 -06:00
Behdad Esfahbod 4e2a9b0c58 [vector/raster] Pass opaque foreground to paint_glyph
The COLR walker's get_color bakes foreground_alpha into
the returned color.  Since our backends don't substitute
paint->foreground in their callbacks (they trust the walker's
resolved color), passing foreground with its own alpha would
double-count it.

Force alpha=255 when passing foreground to hb_font_paint_glyph
and _or_fail, matching what cairo does.  The backend applies
foreground alpha through its own mechanisms (draw fill-opacity,
raster compositing).

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 02:29:27 -06:00
Behdad Esfahbod e76163ebc8 [vector-draw] Add PDF fill opacity via inline ExtGState
The PDF draw context was dropping foreground alpha because
it had no resource dict for ExtGState.  Semi-transparent
fills (e.g. --rainbow) rendered fully opaque.

Add a per-flush ExtGState with /ca (fill opacity) when
alpha < 255.  The ExtGState entries are inlined in the
page's resource dict — no separate PDF objects needed.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 02:18:47 -06:00