Commit Graph
16233 Commits
Author SHA1 Message Date
Behdad Esfahbod 282e55dfa3 [vector-paint-pdf] Use paint's scratch path instead of stack draw_t
hb_pdf_emit_glyph_path was allocating a full hb_vector_draw_t
on the stack (with its own heap-allocated buffers) just to
emit one glyph path.  Use paint->path (the existing scratch
buffer) with the PDF path sink directly — no temporary draw
context, no extra allocations.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 00:22:03 -06:00
Behdad Esfahbod 3a01d263d6 [vector] Add hb_buf_t; move precision to the buffer
Add hb_buf_t inheriting from hb_vector_t<char> with:
- precision field (default 2)
- append_num, append_c, append_str, append_len, append_unsigned
  convenience methods

Convert defs/body/path in both hb_vector_draw_t and
hb_vector_paint_t from hb_vector_t<char> to hb_buf_t.

Remove the standalone precision field from both structs;
set_precision/get_precision are now methods that propagate
to the buffers.  C API functions are thin wrappers.

Callers can still use the old free functions (hb_buf_t
IS-A hb_vector_t<char>).  The new methods allow gradual
migration.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 00:19:27 -06:00
Behdad Esfahbod b513aaace9 [vector-paint] Flush cache on foreground/palette/custom changes
The SVG color glyph cache keys on (glyph, palette, foreground)
but changing these setters didn't invalidate the cache, so
stale rendered color glyphs could be reused.

Call changed() from set_foreground, set_palette,
set_custom_palette_color, and clear_custom_palette_colors
when the value actually changes.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-17 00:00:38 -06:00
Behdad Esfahbod c3c51a6150 [vector-draw] Split draw funcs into per-format SVG/PDF sets
Replace the single unified draw callbacks (which switched on
format in every callback) with separate SVG and PDF callback
sets, matching the path sink split pattern.

SVG: straight-line M/L/Q/C/Z operators with comma separators.
PDF: straight-line m/l/c/h operators with space separators.
PDF omits quadratic_to — null fallback auto-promotes to cubic.

Also split append_xy and flush_path into format-specific
methods (append_xy_svg/pdf, flush_path_svg/pdf) in the .hh.

hb_vector_draw_funcs_get() now takes format and dispatches.
hb_vector_draw_get_funcs() passes draw->format through.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 23:49:37 -06:00
Behdad Esfahbod cbb0aeadb9 [vector] Split SVG paint backend into hb-vector-paint-svg.cc
Extract all SVG-specific paint callbacks (push_transform,
push_clip_glyph, push_clip_rectangle, push_clip_path,
pop_clip, color, image, gradients, push/pop_group,
color_glyph, custom_palette_color) and the SVG render
function into a new file, matching the existing PDF split.

hb-vector-paint.cc retains the public API, format dispatch,
glyph_impl, clear, and render.

Also:
- Move ensure_initialized to a method on hb_vector_paint_t
- Move hb_vector_color_glyph_cache_key helper to .hh
- Rename hb_vector_paint_funcs_get → hb_vector_paint_svg_funcs_get

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 23:40:51 -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 8793feb5cb [raster-paint] Fix alignment cast in background fill
Use hb_memcpy instead of casting uint8_t* to uint32_t*
to avoid -Wcast-align on platforms with strict alignment.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 22:46:06 -06:00
Behdad Esfahbod 2f78333f1b [vector-draw] Extract flush_path helper; apply foreground to all paths
Move the path→body flush logic into draw->flush_path() and
use it everywhere: per-glyph emission, pre-glyph flush, and
render-time final flush.

The helper applies the current foreground color to every
flushed path — both glyph outlines and free-form draw ops
(like show-extents overlays) inherit the foreground that
was set at flush time.

SVG: fill="rgb(...)" on the <path> inside the Y-flip <g>.
PDF: rg operator before each fill.

Regenerated vector test expected outputs.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 19:24:21 -06:00
Behdad Esfahbod 02f38ff8c3 [vector-draw] Per-glyph foreground color
Move foreground fill from a global wrapper (SVG <g fill>,
PDF initial rg) to per-glyph emission:

SVG: each <use> gets fill="rgb(...)" from the current
draw->foreground at emission time.

PDF: each glyph path gets an rg operator before fill.

This lets callers change foreground between glyph calls
(e.g. per-glyph palette cycling) without post-processing.

Wire per-glyph palette cycling for the draw path in the
hb-vector util, matching how paint already does it.

Regenerated vector test expected outputs.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 19:20:13 -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 d774a9a4e2 [vector/raster] Add set_background / get_background API
Add hb_vector_paint_set_background() / get_background() and
hb_raster_paint_set_background() / get_background().

For vector: if set to a non-transparent color, the SVG renderer
emits a <rect> covering the viewBox before glyph content, and
the PDF renderer prepends a filled rectangle to the content
stream.  Both support semi-transparent backgrounds (SVG via
fill-opacity, PDF via ExtGState /ca).

For raster: the root surface is pre-filled with the background
color before glyph compositing begins.

Default is transparent (no background emitted).

The hb-vector CLI util wires --background to the new API.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 18:22:07 -06:00
Behdad Esfahbod 05d5e35f36 [vector-paint-pdf] Add SMask alpha for sweep gradient meshes
Sweep gradients use Coons patch meshes (ShadingType 6) which
encode vertex colors as RGB bytes — no alpha channel.

When any stop has alpha < 255, build a parallel DeviceGray
mesh with the alpha channel and wrap it in a Form XObject
soft mask, same pattern as the linear/radial SMask support.

Also refactor: extract add_shading_by_id for mesh shadings
that are already allocated as objects (they contain streams).

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 18:08:04 -06:00
Behdad Esfahbod 692454636a [vector-paint-pdf] Add SMask support for gradient alpha
PDF DeviceRGB shadings have no alpha channel, so gradient
stops with partial opacity (like emoji blushes fading to
transparent) were rendered as solid color.

When any color stop has alpha < 255, build a parallel
DeviceGray shading for the alpha channel, wrap it in a
Form XObject transparency group, and reference it as a
luminosity soft mask via ExtGState.  The mask is vector
(resolution-independent) — the PDF renderer evaluates
the grayscale function analytically.

Applied to both linear (Type 2) and radial (Type 3)
gradient handlers.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 18:08:04 -06:00
Behdad Esfahbod 05e9bd5099 [vector] Fix aggregate init for hb_vector_path_sink_t
Drop default member initializers for x_scale/y_scale since
C++11/14 doesn't allow aggregate initialization of structs
with default member initializers.  All callers now specify
all four fields explicitly.

Fixes CI build failure.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 17:50:47 -06:00
Behdad Esfahbod 3c533add46 [vector-paint-pdf] Normalize gradient color stops before emit
The SVG paint backend normalizes color-line stops to [0, 1]
(via hb_paint_normalize_color_line) and adjusts gradient
coordinates accordingly.  The PDF backend was emitting raw
stop offsets and unadjusted coordinates, causing mismatched
gradient geometry — most visibly, radial gradients with a
non-zero focal radius (like emoji blushes) rendered with
r0=0 instead of the correct adjusted inner radius.

Fetch and normalize stops before building the PDF gradient
function, then shift linear/radial coordinates by (mn, mx)
the same way SVG does.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 17:41:47 -06:00
Behdad Esfahbod c225835410 [vector] Scale clip path coordinates by 1/scale_factor
Clip path coordinates arrive in font-scale (input space) but
need to be in output space for both SVG and PDF.  Add x_scale
and y_scale fields to hb_vector_path_sink_t (default 1.0) and
divide through in the append_xy helpers.

For clip paths, the SVG and PDF paint backends set these to
scale_factor so the emitted coordinates land in output space
without needing a wrapping transform.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 17:25:24 -06:00
Behdad Esfahbod 8ed9cc3ff5 [vector-draw] Fix SVG show-extents: flush path before next glyph
Free-form draw ops (extents overlay rectangles/circles) emitted
via hb_vector_draw_get_funcs() accumulate in draw->path.  But
the SVG glyph emission clears draw->path when drawing each new
glyph outline, discarding the overlay from the previous glyph.

Flush any accumulated path data to body (wrapped in the Y-flip
<g>) before clearing for the next glyph outline, so overlays
for all glyphs survive to the final SVG.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 17:17:58 -06:00
Behdad Esfahbod 36bf8ecaf2 [vector] Auto-flush glyph caches on font change
Track the font pointer and serial in both hb_vector_draw_t and
hb_vector_paint_t.  On each draw/paint_glyph call, compare the
incoming font against the cached values; if the pointer or
serial changed, call changed() to flush all cached glyph data
(defined_glyphs, defs, color glyph cache, counters).

The cached_font holds a reference so pointer-reuse after
destroy is detected correctly.

This makes context reuse across font changes safe without
requiring callers to call any explicit invalidation API.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:59:19 -06:00
Behdad Esfahbod 80ca5617ff [vector] Split path draw funcs into per-format singletons (#5935)
Replace the single hb_vector_path_draw_funcs_get() (which
dispatched on sink->format via a switch in every callback)
with two format-specific singletons:

  hb_vector_svg_path_draw_funcs_get()
  hb_vector_pdf_path_draw_funcs_get()

Each callback set is straight-line code with no branching.
The PDF set omits the quadratic_to callback, so the
framework's null fallback auto-promotes quadratics to cubics
— removing the manual quad-to-cubic conversion that was
previously hand-rolled in the PDF quadratic_to case.

A convenience dispatcher hb_vector_path_draw_funcs_get(format)
is kept for callers that select at runtime.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:58:56 -06:00
Behdad Esfahbod 68afee084c [vector-paint-pdf] Fix use-of-uninitialized in indexed smask
inflate() with Z_FINISH can return Z_STREAM_END after
decompressing fewer bytes than the expected raw_len when
the IDAT data is truncated or the width/height don't match
the actual compressed payload.  The un-filter loop then
reads uninitialized tail bytes from the raw buffer.

Check that stream.total_out matches raw_len after inflate;
bail out if the sizes disagree.

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

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:32:38 -06:00
Behdad Esfahbod 76d543adc9 fixup! [draw][paint] Make get_funcs instance-aware; add get_format 2026-04-16 16:23:32 -06:00
Behdad Esfahbod 6636f042fc Address PR #5933 review feedback
- Reset Since tags to XSince: REPLACEME for changed
  get_funcs signatures (gpu, raster, vector draw+paint).
- Replace (transfer none) annotation syntax with prose in
  clip_path_start docs (hb-paint.cc and hb-paint.h).
- Document that returned draw funcs/data are only valid
  until the matching clip_path_end call, and no other paint
  calls should be made in between.
- Use switch instead of ternary for format in
  hb-vector-path.cc.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:22:42 -06:00
Behdad EsfahbodandClaude Opus 4.6 48fdb0ec34 [cairo] Implement arbitrary-path clip in paint funcs
Wire push_clip_path_start / push_clip_path_end into the
cairo paint backend.  start() saves the cairo state, opens
a fresh path, and hands the caller hb_cairo_draw_get_funcs
with the cairo_t as draw data — same draw funcs that
push_clip_glyph already uses internally to walk a glyph
outline as a cairo path.  end() closes the path and calls
cairo_clip; pop_clip's cairo_restore tears down the saved
state as before.

Lets cairo paint consumers compose arbitrary-path clips
(say, an outline for a non-glyph shape) the same way
push_clip_glyph builds a glyph clip.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:21:49 -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 EsfahbodandClaude Opus 4.6 38f1760d7d [vector-draw] Flush pending free-form path on glyph entry
The previous render-side flush handled the trailing batch
of free-form draw ops, but each new hb_vector_draw_glyph
call wipes the scratch first thing — so any rect (etc.)
queued between two glyphs was silently dropped.

Add a flush at the top of hb_vector_draw_glyph that emits
the accumulated scratch as its own <path>/fill before
proceeding.  Lets callers interleave hb_draw_*() calls
between glyphs and have every batch land in the output.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:18:20 -06:00
Behdad EsfahbodandClaude Opus 4.6 acfb6784c3 [vector-draw] Flush leftover scratch path on render
hb_vector_draw_glyph for SVG copies the glyph outline into
defs and emits a <use> in body, but didn't clear the
internal draw->path scratch on its way out — and the PDF
path wasn't clearing it after appending to body either.
The render functions then mutually-excluded body vs path
emission with an else-if, so any free-form draw ops a
caller did via hb_draw_*() on hb_vector_draw_get_funcs()
would either inherit the trailing glyph bytes (SVG) or be
silently dropped (PDF render only fired when body was
empty).

Two-line fix: clear draw->path at the end of each glyph
emission so subsequent draw-funcs calls start with an
empty scratch, and turn the render branches into two
sibling ifs so any leftover free-form path gets flushed
as its own <path d="..."/> (SVG) or path + "f\n" (PDF)
alongside the body content.

Unblocks --show-extents in hb-vector by letting the util
just call hb_draw_rectangle on the draw context after each
glyph.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:18:20 -06:00
Behdad EsfahbodandClaude Opus 4.6 98470a3e02 [draw] Rename hb_draw_rect to hb_draw_rectangle
API rename for the not-yet-released stroked/filled
rectangle helper added to hb-draw — full names
(hb_draw_line, hb_draw_circle, hb_draw_rectangle) read
better than mixed full/abbreviated.

Updates the public header, impl, the gpu doc reference,
the GPU API test, the GPU demo's extents overlay, the
extents-overlay paint helper, and the docs section
listing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:18:20 -06:00
Behdad EsfahbodandClaude Opus 4.6 8f735c05cd [draw] Normalize w/h in hb_draw_rect
Negative w/h are documented as supported but the stroked
variant's math (outer grows by stroke_width, inner shrinks
by stroke_width) silently inverts — outer ends up smaller
than inner, the ring becomes a filled outer rect, and
arbitrary-path clip users (who feed raw font glyph extents
with y_bearing-style negative height) see the clip fill
everything instead of just the stroke ring.

Normalize w/h to non-negative at the top of hb_draw_rect
so the rest of the stroke math produces the expected
outer-contains-inner ring.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:18:20 -06:00
Behdad EsfahbodandClaude Opus 4.6 0477004945 [gpu-paint] Implement arbitrary-path clip
Each GPU paint clip becomes a Slug sub-blob in c->sub_blobs
(curves + band index + extents) that the encoded shader
tests against independently — there's no glyph-id lookup at
shader time.  That means an arbitrary user-drawn path is
representable on GPU: hand the caller the same
hb_gpu_draw_get_funcs() the glyph-clip path already feeds
through hb_gpu_draw_glyph, and encode the result into a
sub-blob the same way.

push_clip_path_start preps scratch_draw (the existing
per-clip encoder scratch) with the paint's font scale and
captures cur_transform for the forthcoming sub-blob.
push_clip_path_end runs hb_gpu_draw_encode on the captured
geometry and commits the resulting sub-blob and its design-
unit extents to clip_stack.  pending_clip_t grows a
sub_blob_index field and four extent fields; emit_clip_sub_
blob short-circuits for path clips (sub_blob_index >= 0) by
accumulating the stored extents and returning the pre-baked
index, skipping re-encoding per layer.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:18:20 -06:00
Behdad EsfahbodandClaude Opus 4.6 100c992dc5 [raster-paint] Implement arbitrary-path clip
Wire push_clip_path_start / push_clip_path_end into the
raster backend.  Factor the existing emitter-based clip
helper in two so both paths share the mask-render +
clip-intersect tail:

  * hb_raster_paint_finalize_path_clip does the render,
    alpha-buffer allocate, and intersect-with-old-clip
    work.
  * The emitter-based helper now preps clip_rdr with the
    current effective transform, calls emit() to populate
    edges, then calls the finalize helper.
  * push_clip_path_start preps clip_rdr exactly the same
    way and hands the caller the raster-draw funcs +
    clip_rdr as draw-data; the caller drives the path in.
    _end just runs the finalize pass.

Since clip_rdr is shared across all clip pushes, this
reuses the object's recycled mask-image and edge buffers
without extra allocations per clip.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:18:20 -06:00
Behdad EsfahbodandClaude Opus 4.6 afb6a5411e [vector-paint-pdf] Implement arbitrary-path clip
Wire push_clip_path_start / push_clip_path_end through the
PDF backend using the shared hb_vector_path_sink_t.  start()
emits 'q' to save state and hands back a sink that streams
PDF path operators directly into the body buffer; end()
closes with 'W n' to install the accumulated path as the
clip.  The existing pop_clip closes the q/Q pair.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:18:20 -06:00
Behdad EsfahbodandClaude Opus 4.6 834826289e [vector] Fold svg-path sink into a format-aware vector-path
Rename hb-vector-svg-path.{cc,hh} to hb-vector-path.{cc,hh}
and make the sink format-aware (SVG or PDF), so the PDF
backend can reuse it for arbitrary-path clipping without
spinning up its own path serializer.

hb_vector_svg_path_sink_t gains a hb_vector_format_t field
and becomes hb_vector_path_sink_t; the draw callbacks
switch on format and emit SVG path-data or PDF path
operators into the external buffer.  hb-vector-draw.cc and
hb-vector-paint.cc (push_clip_glyph + the recent arbitrary
clip-path start) are updated to the new name with
format=SVG.  No behavior change on the SVG side.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:18:20 -06:00
Behdad EsfahbodandClaude Opus 4.6 e358e565e5 [vector-paint] Implement arbitrary-path clip
Wire push_clip_path_start / push_clip_path_end into the
vector-paint (SVG) backend.  start() clears the per-paint
scratch path and hands back the SVG path sink + its draw
funcs; end() emits a <clipPath><path d="..."/></clipPath>
into defs and opens a matching <g clip-path="url(...)"> on
the body.  The existing pop_clip closes that group.

IDs use the "cp" prefix to distinguish from "c" (rects) and
"clip-g" (glyph clips).  clip_path_counter is reset along
with the other body counters on paint reset.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:18:20 -06:00
Behdad EsfahbodandClaude Opus 4.6 93777bcb19 [paint] Use language="plain" in clip-path usage block
HarfBuzz gtk-doc convention is |[<!-- language="plain" -->,
not "C".  Aligns with every other code block in src/.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:18:20 -06:00
Behdad EsfahbodandClaude Opus 4.6 a0c670108e [paint] Add usage example to hb_paint_push_clip_path_start docs
Illustrate the expected call sequence: start → draw_* →
close_path → end → (painted ops) → pop_clip, so
implementers of the paint API don't have to reassemble the
contract from the individual function docs.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:18:20 -06:00
Behdad Esfahbod 599c99b589 [paint] Add arbitrary-path clip API
Add push_clip_path_start / push_clip_path_end to the
hb_paint_funcs_t vtable, for callers that want to clip paint
output to an arbitrary shape (not just a glyph or rectangle).

  hb_draw_funcs_t *df = hb_paint_push_clip_path_start (pf, pd, &dd);
  hb_draw_move_to (df, dd, NULL, ...);
  hb_draw_line_to (df, dd, NULL, ...);
  ...
  hb_draw_close_path (df, dd, NULL);
  hb_paint_push_clip_path_end (pf, pd);
  /* paint ops here are clipped to the emitted path */
  hb_paint_pop_clip (pf, pd);

The backend hands back a draw_funcs + draw_data pair (transfer
none) that accumulates the clip path; end() signals the
caller is done drawing clip geometry and the accumulated
path becomes an active clip.  pop_clip closes the scope,
symmetric with push_clip_glyph / push_clip_rectangle.

Only public API and the nil vtable stubs land here.  Backend
implementations (hb-vector, hb-raster, hb-gpu) follow in
separate commits.

Partial: https://github.com/harfbuzz/harfbuzz/issues/5930

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:18:20 -06:00
Behdad Esfahbod 90595602d3 [font] Document paint_glyph_or_fail success/failure modes
Spell out when the function succeeds: COLRv0 paint layers,
COLRv1 paint graph, or a bitmap image rendered by the font's
callbacks.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:16:38 -06:00
Behdad Esfahbod 18825a19a1 Address PR #5931 review feedback
- Reset Since tags to XSince: REPLACEME for new non-or_fail
  glyph functions (gpu, raster, vector paint).
- Remove duplicate doc comment on static
  hb_vector_paint_glyph_impl helper.
- Fix hb_gpu_paint_glyph doc: remove internal LAYER_SOLID
  reference.
- Drop hb_ot_color_has_svg() from color-detection in all utils
  (cairo, raster, vector, hb-raster-all); HarfBuzz does not
  render SVG table natively, so checking it caused fallback to
  monochrome drawing of the actual outlines.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:16:38 -06:00
Behdad Esfahbod 390ec06fa7 Merge pull request #5931 from harfbuzz/paint_or_fails
[vector/raster/gpu] Add *_glyph_or_fail() variants across all three
2026-04-16 16:13:05 -06:00
Behdad Esfahbod bc328d4d8c [vector] Fix sign of ty in EXPAND extents transform
The per-glyph EXPAND extents transform negated xy and yy to
convert from font Y-up to SVG/PDF Y-down, but kept ty
(the pen's Y translation) positive.  The actual SVG instance
transform negates both the scale and the translation:

  translate(tx/sf, -ty/sf) scale(1/sf, -1/sf)

So the rendered Y position of glyph-local point (x, y) is
(-y - ty) / sf, but the EXPAND was computing (-y + ty) / sf.
The wrong sign caused pen_y offsets (large in Nastaliq due to
vertical glyph stacking) to inflate the viewBox downward
instead of cancelling out.

Visible as excess whitespace below the descender in SVG output
for scripts like Nastaliq that use large y_offsets.

Fix: negate ty in the extents_transform, matching the SVG emit.
Applied to both hb-vector-draw and hb-vector-paint EXPAND paths.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 15:40:59 -06:00
Behdad Esfahbod 64d2c7311b [paint] Promote reduce_anchors / normalize_color_line / sweep_tiles to public (#5932)
Every COLRv1 renderer ends up rewriting these three helpers;
they now live on hb-paint.h so Skia, FreeType-SVG, and other
downstream paint-API implementers can consume a single
canonical implementation.

  * hb_paint_reduce_linear_anchors() -- 3-anchor (P0, P1, P2)
    to 2-point axis projection.
  * hb_paint_normalize_color_line() -- sort + rescale stops
    to [0, 1]; returns (min, max) so callers can shift
    gradient geometry to match.
  * hb_paint_sweep_gradient_tiles() + matching
    hb_paint_sweep_gradient_tile_func_t callback typedef --
    iterates the full 0..2π sweep produced by a color-stop
    list, invoking the callback once per (start, end)
    angular segment.  Handles PAD / REPEAT / REFLECT.

Implementation moves from src/hb-paint.hh (private, static
inline) to src/hb-paint.cc (public, HB_EXTERN).  The sweep
tiler loses its C++ template/lambda flavor and grows a
user_data parameter alongside the function-pointer callback;
the three in-tree callers (cairo, vector-paint, vector-paint-pdf)
now use standalone trampoline functions + context structs
instead of captured lambdas.

Fixes: https://github.com/harfbuzz/harfbuzz/issues/5928

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 14:56:15 -06:00
Behdad EsfahbodandClaude Opus 4.6 e933f71b0d [cairo][util/hb-view] Honor --draw / --paint via cairo color mode
hb-cairo's render_color_glyph drops _or_fail in favor of
the synthesizing hb_font_paint_glyph, so the paint callback
handles every glyph (color or mono) through the paint
pipeline instead of returning NOT_IMPLEMENTED on non-color
glyphs and forcing cairo to fall through to the outline
path.

Hb-view's helper-cairo then picks paint vs draw the same
way the other utils do — --paint wins over --draw,
otherwise auto-detect via the font's color tables — and
expresses "draw" by setting CAIRO_COLOR_MODE_NO_COLOR on
the font options (which makes cairo skip render_color_glyph
entirely, falling through to render_glyph for the cheaper
outline path).  No new public API.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 14:07:38 -06:00
Behdad EsfahbodandClaude Opus 4.6 dba4dab3ef [vector-paint] Fold scale_factor into PDF per-glyph cm
Follow-up to 152aa0fd8c.  The SVG branch of
hb_vector_paint_glyph divides the instance transform by
x_scale_factor/y_scale_factor (via
hb_vector_svg_append_instance_transform) so input-space
glyph paths land in the pixel-space viewBox.  The PDF
branch emitted the raw paint matrix, which kept the CTM in
input space while the MediaBox lived in pixel space —
at any non-identity scale_factor, the glyph rendered
entirely outside the page.

Divide the matrix columns by sx/sy in the emitted cm so
PDF matches SVG.  Covers hb-vector's font-size != 20 case
and the website's scale_factor = 64 case, where emoji
rendered blank at 72 px.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 02:42:29 -06:00
Behdad Esfahbod 98f3a42ee2 [vector/raster/gpu] Add *_glyph_or_fail() variants across all three
Consolidate the {vector,raster,gpu}_{draw,paint}_glyph APIs
to match the hb_font_{draw,paint}_glyph / _or_fail pattern:

  * Each existing *_glyph() public function now returns void.
    For the draw variants this is purely a return-value drop.
    For the paint variants the void entry additionally gains a
    synthesize fallback for non-color glyphs (calls
    hb_font_paint_glyph instead of _or_fail) so a mono glyph
    still produces output via a foreground-colored outline.

  * New *_glyph_or_fail() variants return hb_bool_t, reporting
    exactly what hb_font_{draw,paint}_glyph_or_fail() would.
    Callers that care about distinguishing color paint from
    synthesized outline now have a clean boolean signal.

Refactors the internal impls behind a shared static helper
parameterised by a `fallible` flag; the two public entries
delegate, passing true (for _or_fail) or false (for the void
alias).

For GPU specifically, paint_glyph_or_fail no longer folds the
encoder's unsupported flag into its return -- that state was
always better reported from hb_gpu_paint_encode() (which
returns NULL), and conflating the two prevented callers from
telling "font has no paint data" from "encoder hit a limit".

While in there, remove a dead branch at the end of
hb_vector_paint_glyph's SVG case (both inner blocks had
already returned; the tail was unreachable).

Updates docs/harfbuzz-sections.txt with the six new symbols
and the NEWS note with the Changed API callouts.  Updates
in-tree callers (hb-vector-svg-all, hb-raster-all, test-gpu,
util/vector-output.hh) to the _or_fail variants where they
previously read the return value.

Fixes: https://github.com/harfbuzz/harfbuzz/issues/5926

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 00:36:37 -06:00
Behdad Esfahbod 152aa0fd8c [vector] Divide explicit set_extents by scale_factor
hb_vector_{paint,draw}_set_extents stored caller-supplied
extents as-is, while per-glyph extents accumulated via
hb_vector_set_glyph_extents_common were divided by
scale_factor on the way in.  When callers passed extents in
input-space (the natural unit alongside the glyph geometry
they're feeding) and a non-1 scale_factor, the resulting
viewBox didn't match what the glyphs drew.

Make set_extents divide by scale_factor too, matching the
accumulate path; callers can now pass input-space extents
uniformly.

Also:

* hb-vector-paint's global transform wrapper dropped the
  redundant /scale_factor division.  Each per-glyph
  <use transform="... scale(1/scale_factor, ...)"> already
  applies scale_factor; wrapping the body in another
  /scale_factor matrix double-scaled the output, shrinking
  color-glyph output to 1/scale_factor^2 of its intended
  size.  The wrapper now only fires when the user set a
  non-identity paint->transform, and emits that transform
  as-is.

* util/vector-output.hh simplification: now that set_extents
  handles the divide, drop the local pre-multiply of extents
  by upem_to_pixel -- pass the upem-space extents through.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 22:04:26 -06:00
Behdad Esfahbod d8980e94e0 [draw] Add line/rect/circle drawing helpers (#5925)
* [gpu] Add hb_gpu_draw_line / _rect / _circle shape helpers

The GPU draw encoder already consumes arbitrary curves via the
hb_draw_funcs_t machinery -- that's how glyph outlines feed in.
Expose three convenience helpers so callers can emit common
shapes (tapered line, rectangle, circle) without having to
hand-roll the curve math:

    hb_gpu_draw_line   (draw, x0,y0,w0, x1,y1,w1);
    hb_gpu_draw_rect   (draw, x,y,w,h, stroke_width);
    hb_gpu_draw_circle (draw, cx,cy,r, stroke_width);

stroke_width = NaN means "filled".  A finite positive value
produces a stroked outline by emitting an outer contour CCW
and an inner contour CW so the fill rule carves a hole.

For shapes beyond these three helpers, callers can feed any
outline through hb_draw_move_to / _line_to / _quadratic_to /
_cubic_to / _close_path with the funcs from
hb_gpu_draw_get_funcs() and the hb_gpu_draw_t as data -- the
helpers are thin wrappers over that path.

Useful for game UI: health bars, progress rings, custom
line-art decorations, rendered through the same slug shader
as text.

Assisted-by: Claude <noreply@anthropic.com>

* [util/gpu] Add --show-extents flag

Exercises hb_gpu_draw_rect: when the flag is set, each glyph
blob encodes its ink-extent frame as an extra stroked
rectangle alongside the glyph outline, so the frame is cached
with the glyph and renders with no extra draw call.

The frame is padded outward by stroke_width / 2 so the stroke
sits entirely outside the glyph ink (otherwise the inner half
would knock out against the glyph shape, since both fill in
the same blob).

Cmdline-only for now -- a runtime 'e' toggle would require a
buffer re-shape hook that native hb-gpu doesn't yet expose.

Assisted-by: Claude <noreply@anthropic.com>

* [util/gpu] Cmdline toggles mirroring interactive keybindings

Add --dark / --no-vsync / --fullscreen / --no-stem-darken /
--debug so scripts and CI can start hb-gpu in any of the
states previously reachable only by pressing a key
interactively.  Applied via the same keystroke path by
synthesizing the corresponding key characters through
demo_view_type() at startup -- single source of truth for
each toggle.

Assisted-by: Claude <noreply@anthropic.com>

* [draw] Promote shape helpers from hb-gpu-draw to hb-draw

Three shape-emitting helpers (line, rect, circle) operate on
any hb_draw_funcs_t, not specifically the GPU backend.  Move
them out of the GPU-only API surface into the core hb-draw
API so any pen (raster, vector, gpu, user-supplied) gets
them.

API rename / move:

  hb_gpu_draw_line   -> hb_draw_line   (in src/hb-draw.h)
  hb_gpu_draw_rect   -> hb_draw_rect
  hb_gpu_draw_circle -> hb_draw_circle

Signatures gain a (dfuncs, draw_data, st) prefix to match the
existing hb_draw_move_to / line_to / cubic_to / close_path
shape -- the helpers are thin wrappers over those.

Callers in test/api/test-gpu.cc and util/gpu/demo-font.cc
updated to call hb_draw_* with hb_gpu_draw_get_funcs() and a
local hb_draw_state_t.

Updated harfbuzz-sections.txt with the new entries; removed
hb_gpu_draw_line / _rect / _circle from hb-gpu.h.

Assisted-by: Claude <noreply@anthropic.com>

* [draw] hb_draw_line: NaN second width defaults to first

Common case is a uniform-width line.  Letting @w1 default to
@w0 when NaN is passed makes the call shorter:

  hb_draw_line (..., x0, y0, 4, x1, y1, NAN);  /* uniform 4 */

Mirrors the NaN-as-sentinel convention used in hb_draw_rect /
hb_draw_circle for the stroke_width parameter.

Assisted-by: Claude <noreply@anthropic.com>

* [draw] hb_draw_line: add cap parameter (butt / square)

Add hb_draw_line_cap_t enum with HB_DRAW_LINE_CAP_BUTT (the
existing default behavior) and HB_DRAW_LINE_CAP_SQUARE, which
extends each endpoint along the line direction by half its
local stroke width.

Square caps let four hb_draw_line() calls form a closed
rectangle without gaps at the corners -- a meaningfully
cheaper alternative to hb_draw_rect() for thin outlined
rectangles, since each line's bbox is just stroke_width tall
instead of covering the full rectangle interior.

Round caps not implemented yet; can be added as a third enum
value when a use case shows up.

Assisted-by: Claude <noreply@anthropic.com>
2026-04-15 21:49:45 -06:00
Behdad Esfahbod f53d20c425 [gpu] Use centralized paint helpers; rename sweep tiler
Two cleanups:

* Drop hb-gpu-paint's local reduce_linear_anchors and
  normalize_stops, use the shared
  hb_paint_reduce_linear_anchors() and
  hb_paint_normalize_color_line() from hb-paint.hh.  All
  four backends (cairo, raster, vector, gpu) now share one
  implementation.

* Rename the sweep-gradient tiler from hb_sweep_gradient_tiles
  to hb_paint_sweep_gradient_tiles to match the namespace
  convention of the other paint helpers.

No behavior change.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 17:31:37 -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 6bbf421030 [paint] Centralize normalize_color_line in hb-paint.hh
Same routine appeared in hb-cairo-utils and hb-raster-paint.
Move to hb-paint.hh as hb_paint_normalize_color_line() and
update both call sites.  Behavior unchanged.

(hb-vector-paint and hb-gpu-paint still need normalization
work, separately.)

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 17:31:37 -06:00
Matej Smycka 488922519f [arabic] Fix overflow handling in apply_stch (#5929) 2026-04-15 16:19:06 -06:00