Commit Graph
16320 Commits
Author SHA1 Message Date
Behdad Esfahbod 4b287d9f86 [rust] Remove accidental change to comments 2026-05-26 17:21:18 +02:00
Behdad Esfahbod 53c761783b [harfrust] Update shape glue for harfrust 0.8 (#6001)
Update the Rust bridge and Cargo dependency to harfrust 0.8.
Switch shaping calls to ShapeOptions, keep the cached shaper
path, and remove an unused face_data argument from the shaping
ABI.

Tested: meson compile -C build
Assisted-by: OpenAI Codex
2026-05-26 09:18:15 -06:00
Dominik Röttsches 0a7092643f [rust] Windows build fixes
Cast and add #[allow(clippy::unnecessary_cast)] as the C compiler
chooses different signedness for the hb_script_t and hb_tag_t enum
types depending on architecture. Add casts where needed and add
clippy suppressions to avoid warnings when compiled on systems
where the enum is unsigned by default.
2026-05-25 15:21:45 +03:00
Behdad Esfahbod f3c6b3ab92 [algs] Saturating arithmetic for get_size() on 32-bit (#5988)
On 32-bit platforms size_t is unsigned int, so the typical
get_size() computation `min_size + count * static_size`
silently wraps when the product exceeds UINT_MAX.  The
truncated value then flows into sanitize / serialize as a
small but plausible size, defeating bounds checks.

Add three helpers in hb-algs.hh:

  hb_unsigned_mul_saturate (a, b, ...)
  hb_unsigned_add_saturate (a, b, ...)
  hb_unsigned_mul_add_saturate (a, b, c)   /* a*b + c */

They saturate to SIZE_MAX on overflow.  Variadic overloads
left-fold so 3+ arguments work.  On 64-bit (size_t wider than
unsigned int), all three reduce to plain a*b / a+b — the
inputs to size computations are at most unsigned int so the
result fits in size_t without overflow.

Convert every get_size() that does arithmetic over variable
inputs:

  hb-open-type.hh        UnsizedArrayOf, ArrayOf,
                         HeadlessArrayOf, ArrayOfM1,
                         sorted-array-with-header, CFFIndex
  hb-ot-layout-common.hh CmapSubtableFormat-style,
                         VarRegionList, VarData, MultiVarData,
                         DeltaSetIndexMapFormat01
  hb-ot-hdmx-table.hh    DeviceRecord, hdmx
  hb-ot-cff-common.hh    Charset0, Charset wrapper
  hb-ot-cff1-table.hh    Encoding, Charset wrapper
                         (also widened unsigned accumulator
                         to size_t)
  hb-ot-os2-table.hh     OS/2 (widened accumulator to size_t)
  hb-ot-var-common.hh    TupleVariationHeader,
                         TupleVariationData accumulator
  hb-ot-var-hvar-table.hh DeltaSetIndexMap
  hb-vector.hh           hb_vector_t::get_size

When saturation kicks in, check_range() and extend_size()
naturally reject the resulting value (SIZE_MAX truncated to
unsigned is UINT_MAX, far past any real blob; extend_size
already rejects sizes >= INT_MAX).

Assisted-by: Claude <noreply@anthropic.com>
2026-05-18 15:11:34 -06:00
Dominik Röttsches 4321818220 Cross-platform clippy-friendly casts in FFI
Bindgen generates a std::os::raw::c_char as return type for
functions like hb_blob_get_data. Depending on platform/arch, this
can be an i8 or u8. In the Rust side code, receiving
the data, these are interpreted as u8. Perform the pointer
casts in a way that compiles without clippy warnings about
unnecessary casts when the c_char type was already u8.
2026-05-18 17:02:39 +03:00
Khaled Hosny 8d24ed6cd6 [arabic-pua] Fix mapping for isolated lam alef maksura ligature (#5985)
Correct the ArabicPUATraditional.txt entry for 0xF295 which should be
isolated not final form. Also enable 0xFC43 ligature so the ligature is
actually used.

Fixes https://github.com/harfbuzz/harfbuzz/issues/5984
2026-05-17 15:24:40 -06:00
Behdad Esfahbod c31d554e4a Some more 32bit overflow hardening
Should fix https://issues.chromium.org/issues/513143921
plus speculative fixes.
2026-05-17 14:24:10 +01:00
Behdad Esfahbod 56b9727aa7 [check-symbols] Err on weakly-public symbols as well
https://github.com/harfbuzz/harfbuzz/issues/5895#issuecomment-4461712887
2026-05-16 13:17:31 +01:00
Behdad Esfahbod cf9c3eb36e [vector] Fix heap buffer overflow in append_base64 / append_len
When the sum old_len + out_len (or old_len + l) exceeded INT_MAX,
the (int) cast turned it negative.  resize_full() treated negative
size as zero, alloc(0) succeeded, and the subsequent write loop
overflowed the buffer.

Fix both layers:

- resize_full(): reject negative size_ (return false) instead of
  silently treating it as zero.  This is defense-in-depth for every
  caller that casts unsigned to int.

- append_len() and append_base64(): use hb_unsigned_add_overflows()
  to detect unsigned wraparound before the cast.  This catches the
  case where old_len + new_bytes > UINT_MAX wraps to a small
  positive value that would pass the negative check.

Assisted-by: Claude <noreply@anthropic.com>
2026-05-16 13:06:40 +01:00
Dominik Röttsches 0cf18e7b64 Fix warnings in embedder build with newer rustc (#5983)
Wrap no_mangle in unsafe() to address compiler warnings
with newer rust compilers/editions.
2026-05-13 07:52:32 -06:00
Dominik Röttsches 3986312ea2 Make Rust global_allocator mapping optional (#5982)
In embedding projects, the global_allocator might already have been
overriden. There is no known way to keep the allocator library-local,
and allocator-bound containers are not a stable Rust feature yet.

Add an option to disable the allocator mapping to hb_malloc and friends,
so that an embedder can keep their own allocator.
2026-05-13 07:51:52 -06:00
Behdad Esfahbod de77426147 [gpu-paint] Limit clip rasterization ops
Fixes timeout https://oss-fuzz.com/testcase-detail/4582727033880576
2026-05-05 10:10:08 -06:00
Behdad EsfahbodandClaude Opus 4.7 09795f3215 [post] Fix flipped glyph-name sort comparator on macOS
The std::sort revert (d3a1de25e3) inadvertently inverted the
post-table glyph-name sort.  hb_array_t::cmp has reversed
argument order — x.cmp(y) returns the sign of (y - x), not
(x - y) — so dropping the `> 0` from the bool-returning
lambda turned ascending into descending order.

On Linux the quicksort happened to leave the array in an
order that bsearch could still find names; on macOS, where
the system sort behaves differently, this exposed the bug.

Pass (b, a) to find_glyph_name(...).cmp(...) to recover the
intended ascending order.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 12:08:21 -06:00
Behdad Esfahbod 58deffd897 [qsort] Lower threshold from 32 to 24 2026-05-02 11:59:22 -06:00
Behdad EsfahbodandClaude Opus 4.7 e064b9f79d [algs] Defer insertion sort to single final pass; bump threshold to 32
Restructure hb_qsort_inline so the recursive partitioning loop
stops at subranges of size <= threshold without sorting them,
and the entry point runs one insertion sort over the entire
array at the end.  After the partitioning loop, every element
is within the threshold of its sorted position, so the final
pass is O(n * threshold) — cache-friendlier than a per-leaf
insertion sort, which is what libstdc++'s std::sort does too.

Also bump the threshold from 16 to 32, which the deferred
scheme prefers (per-leaf insertion sort prefers 16; the
deferred form amortizes a larger threshold better).

Bench (NotoSansCJK GPU encode, smaller is better):
  std::sort baseline:                                570 ms
  per-leaf insertion sort, threshold 16 (parent):    613 ms
  per-leaf insertion sort, threshold 32:             623 ms
  deferred insertion sort, threshold 16:             620 ms
  deferred insertion sort, threshold 32 (this):      610 ms
  deferred insertion sort, threshold 64:             610 ms
  deferred insertion sort, threshold 96:             620 ms

Tested: 240/240 tests pass; nm -D libharfbuzz.so shows zero
remaining _ZSt sort weak symbols.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 11:56:14 -06:00
Behdad EsfahbodandClaude Opus 4.7 691033793a [algs] Replace byte-based sort with typed two-way quicksort
Rewrite the internal qsort used by hb_array_t::qsort(Compar) as a
typed template hb_qsort_inline<T, Compar>:

  * Operates on T* directly, so swap is T-typed (one move per
    half-element instead of a per-byte loop).  hb_swap is used,
    which uses ADL std::swap.
  * Two-way Hoare partition instead of three-way Bentley-McIlroy.
    The previous three-way scheme paid extra branch overhead in
    the inner partition loop without payoff on the typical
    HarfBuzz workload (sorting unique curve indices, glyph IDs,
    feature tags, etc).
  * Median-of-three pivot, parked at last-1.
  * Tail-call elimination on the larger partition; recurse only
    on the smaller.  Bounds stack depth at O(log n).
  * Insertion-sort threshold bumped from 10 to 16, matching
    libstdc++'s std::sort.

The byte-based sort_r_simple (and helpers SORT_R_SWAP, sort_r_swap,
sort_r_cmpswap, sort_r_swap_blocks) is removed; nothing else used
it.  hb_qsort still forwards to system qsort.

Bench (NotoSansCJK GPU encode, smaller is better):
  std::sort baseline:                 570 ms
  byte-based sort_r_simple (step 1):  650 ms (+14%)
  three-way typed quicksort:          640 ms
  two-way typed quicksort (this):     613 ms (+7.5% vs baseline)

ABI: zero std:: weak symbols leak from libharfbuzz.so, same as
step 1.

Tested: 240/240 tests pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 11:45:28 -06:00
Behdad EsfahbodandClaude Opus 4.7 d3a1de25e3 [array] Replace std::sort with internal sort_r_simple
std::sort instantiates several internal STL helpers
(__introsort_loop, __insertion_sort, __adjust_heap, __heap_select,
__unguarded_linear_insert) as weak symbols in the std:: namespace.
These leaked into libharfbuzz.so for every distinct (T, Compar)
pair, polluting the public ABI surface.

Route hb_array_t::qsort(Compar) through our existing templated
sort_r_simple instead.  The comparator is still inlined into the
sort body (sort_r_simple has been comparator-templated since
05762b2fac).

Drop the bool-returning comparator overload in favor of a single
int-sign convention; convert the call sites added during the
std::sort migration back to int-sign comparators.

Step 1 of two: this restores ABI hygiene at the cost of typed-
element swap (sort_r_simple swaps byte-by-byte).  A follow-up
will introduce a typed sort to recover that perf.

Tested: 240/240 tests pass; nm -D libharfbuzz.so shows zero
remaining _ZSt sort weak symbols.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 11:32:17 -06:00
Qunxin Liu 209a7237b9 [repacker] fuzzer fix: duplicate shared liga_sets before splitting (#5972)
fix for: https://oss-fuzz.com/testcase-detail/5291844211900416
2026-05-01 14:50:05 -06:00
Andrew Tribick ab6aa4f449 Allow line-ending changes in gen-gpu-shader-artifacts.py (#5968) 2026-04-28 13:59:28 -06:00
Behdad Esfahbod e6741e2205 [harfbuzz-world] Fix macros
Fixes https://github.com/harfbuzz/uharfbuzz/pull/289#discussion_r3149665614
2026-04-28 15:52:29 +03:00
Behdad Esfahbod 73db8cc1a5 [gpu] Fix HLSL paint fragment compile error
At least on wine this was needed.
2026-04-23 03:18:15 -06:00
Behdad Esfahbod f7511709ee [gpu] Remove outdated comment 2026-04-23 02:40:49 -06:00
Behdad Esfahbod 1b9c0c9dd6 [subset/gvar] Protect against overflow on 32bit systems 2026-04-22 13:17:59 -06:00
Behdad Esfahbod 0091ae360e [hb-config.hh] Tweak HB_TINY / HB_NO_DRAW / HB_NO_CFF relationship
Fixes https://github.com/harfbuzz/harfbuzz/issues/5955
2026-04-22 00:30:35 +02:00
Khaled Hosny b0ffab42d4 14.2.0 2026-04-20 05:10:48 +02:00
Behdad Esfahbod 6988bda8ac [gpu] Move blob-recycling from gpu to blob.hh 2026-04-20 01:29:49 +02:00
Behdad Esfahbod 98202cd72f [gpu-paint] Cosmetic 2026-04-20 01:29:49 +02:00
Behdad Esfahbod 90ba85bebc [gpu-draw] Cosmetic 2026-04-20 01:29:49 +02:00
Behdad Esfahbod d26e90c6c8 [gpu] Add HB_GPU_SHADER_LANG_INVALID 2026-04-20 01:29:49 +02:00
Behdad Esfahbod 521d7b2e99 [vector] Approximate a couple of Porter-Duff modes
The rest need structural change.
2026-04-20 01:29:49 +02:00
Behdad Esfahbod 1ae32fb6a7 [vector-pdf] Clean up color-stop passing around 2026-04-20 01:29:49 +02:00
Behdad Esfahbod f433a59dac [vector-svg] Cosmetic 2026-04-20 01:29:49 +02:00
Behdad Esfahbod 0b718a9f09 [raster/vector/gpu] Streamline color-stop fetching 2026-04-20 01:29:49 +02:00
Behdad Esfahbod 74514fc7d6 [vector-svg] Streamline set_svg_prefix() 2026-04-20 01:29:49 +02:00
Behdad Esfahbod 6ef43b340d [hb-vector] Touchups 2026-04-20 01:29:49 +02:00
Behdad Esfahbod 067539a45d [raster] Rename hb-raster-utils.hh to hb-raster.hh 2026-04-20 01:29:49 +02:00
Behdad Esfahbod a7bcaac21a Merge pull request #5948 from harfbuzz/vector-no-reuse-no-pen
[vector/raster] Simplify draw/paint API
2026-04-19 12:55:24 -06:00
Behdad Esfahbod e5755c8274 [raster/vector] Address review feedback 2026-04-19 12:52:17 -06:00
parasol-aserandjeffhuang 1ea68a9f57 [fontations] Fix OOB write on glyph-name callback with size == 0 (#5946) (#5947)
* [fontations] Fix OOB write on glyph-name callback with size == 0

The Rust `_hb_fontations_glyph_name()` callback computed
`glyph_name.len().min(size as usize - 1)`. When a caller passed
`size == 0`, `size as usize - 1` underflowed a `usize` to
`usize::MAX`, the `min()` clamp collapsed to `glyph_name.len()`,
and the subsequent `copy_nonoverlapping` / trailing NUL wrote
`glyph_name.len() + 1` bytes into a zero-capacity caller buffer.
This is an out-of-bounds write reachable from the public API
whenever the build was configured with `-Dfontations=enabled`
and the application had called `hb_fontations_font_set_funcs()`.

Short-circuit the `size == 0` case before the subtraction.
Return `true` in that case to match the existing post and CFF1
backends (`hb-ot-post-table.hh`, `hb-ot-cff1-table.hh`), which
both early-out with `return true` on `!buf_len` — the glyph
does have a name; we simply had no buffer to write it into.

Regression tests added in `test/api/test-glyph-names.c` cover
the zero-size path on post and CFF1 backends, a sub-font, a
NULL buffer, and `size == 1` / small-buffer / exact-fit cases
so the truncation path does not silently regress.

Fixes: https://github.com/harfbuzz/harfbuzz/issues/5946
Assisted-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* [font] Reject glyph-name lookups with size == 0 at the wrapper

`hb_font_t::get_glyph_name()` previously pre-wrote the trailing
NUL only when `size != 0` but still dispatched to the backend
callback with `size == 0`. Every current glyph-name backend has
to re-validate that argument: `post` and `cff1` short-circuit
with `return true`, the new Rust `fontations` callback now
short-circuits too (prior commit), and the CoreText callback in
`hb-coretext-font.cc` has a structurally similar `(CFIndex)size
- 1` pattern.

Normalize this at the wrapper layer: return `false` immediately
on `size == 0`, before any backend dispatch and before the
`*name = '\0'` write. This makes the zero-size case a single,
auditable no-op for every glyph-name provider (native or Rust,
current or future) and removes the CoreText underflow from the
reachable surface through the same chokepoint.

Observable change: callers that invoked
`hb_font_get_glyph_name(font, gid, buf, 0)` on post/cff1 backed
fonts used to get `true` back; they now get `false`. `size == 0`
queries carry no useful information for the caller, so this is
considered a safe hardening rather than a contract break.

Assisted-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* [font] Preserve glyph-name zero-size probe semantics

Keep `hb_font_get_glyph_name(..., NULL, 0)` as a valid
return-value-only probe instead of rejecting `size == 0` in the
wrapper.

Fix the remaining backend-specific zero-size paths in hb-ft and
hb-coretext-font so they answer the probe without dereferencing the
caller buffer, and update the API tests to assert the preserved
behavior.

Testing: CCACHE_DISABLE=1 meson test -C build test-glyph-names
Testing: CCACHE_DISABLE=1 meson test -C build test-ft
Assisted-by: OpenAI Codex <codex@openai.com>

---------

Co-authored-by: jeffhuang <jeff@docker.xydrsucermoubd24xgo33yhsgd.bx.internal.cloudapp.net>
2026-04-18 19:54:54 -06:00
David Corbett 27f1be922e [indic] Categorize U+1CF5 and U+1CF6 as CS (#5949) 2026-04-18 19:54:27 -06: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 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 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