Use the HarfRust bulk UnicodeBuffer glyph-info append API to populate
the shaping buffer from HarfBuzz glyph infos after one slice cast. This
avoids per-codepoint add calls in the HarfBuzz-to-HarfRust bridge.
Testing: ninja -C build src/rust/libharfbuzz_rust.a
Assisted-by: OpenAI Codex
HarfBuzz language tags are already available as C string bytes.
Pass those bytes directly to HarfRust Language::new instead of
converting through UTF-8 and FromStr.
Testing: ninja -C build src/rust/libharfbuzz_rust.a
Assisted-by: OpenAI Codex
Most shaping calls pass maximum a couple of user features. Store the
converted HarfRust feature list in a SmallVec with the same inline
capacity that HarfRust uses for shape-plan user features, avoiding
a heap allocation for common cases.
Testing: ninja -C build src/rust/libharfbuzz_rust.a
Assisted-by: OpenAI Codex
HarfRust glyph infos and positions are layout-compatible with the
corresponding HarfBuzz buffer structs. Assert that size, alignment,
and visible field offsets match, then copy both arrays directly into
the HarfBuzz buffer instead of translating each glyph field-by-field.
Also, don't truncate hb buffer before resizing it to populate. This
would not zero the private fields (var1/var2, var), but that's not
visible to user nor used later.
Assisted-by: OpenAI Codex
get_base_glyph_paintrecord() looked up the BaseGlyphList with the
reference-returning bsearch(), which yields the all-zero Null object
when the glyph is absent, and then rejected non-matches by comparing
record->glyphId against the requested gid. For glyph 0 the Null
object's glyphId (0) equals the requested gid, so the sentinel was
taken for a real record. .notdef then resolved to a bogus empty paint,
and COLR::get_extents() returned success with 0,0,0,0 instead of
letting hb_ot_get_glyph_extents() fall back to the glyf outline.
Use bfind(), which reports presence directly, so a missing glyph
(including glyph 0) returns nullptr and extents fall back correctly.
This surfaced as a mark-positioning difference: fallback mark
positioning centers a mark using the base and mark glyph extents, so a
.notdef mark over a space was positioned differently from shapers that
report the real outline extents (e.g. fontations/HarfRust).
Testing:
- test/api/test-extents.c: glyph 0 (.notdef, has an outline) now
reports its outline extents; updated the expectation accordingly.
- b/util/hb-shape Noto-COLRv1.ttf -u 0020,0650 now matches
--font-funcs=fontations.
- meson test -C build (246 passed, 1 skipped)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
HB_NO_DRAW only disables the draw-dependent OT-font CFF extents
path. It should not imply full HB_NO_CFF for downstream builds
that independently disable CFF subsetting.
Keep the HB_TINY behavior by making HB_NO_CFF conditional on the
combination of HB_TINY and HB_NO_DRAW. Also avoid calling the CFF2
instance-metrics helper when OT-font CFF support is disabled, since
that helper needs the CFF2 extents implementation.
Context:
https://github.com/harfbuzz/harfbuzz/pull/5958#discussion_r3121421428https://github.com/harfbuzz/harfbuzz/issues/5955#issuecomment-4289771142https://github.com/harfbuzz/harfbuzz/issues/5955#issuecomment-4345573851
Tested by compiling src/harfbuzz-subset.cc directly with
-DHB_NO_DRAW -DHB_NO_SUBSET_CFF and checking the CFF2 extents symbol
is no longer undefined. Also direct-compiled src/harfbuzz.cc with the
same defines, plus src/harfbuzz-subset.cc for plain HB_TINY and
HB_TINY with HAVE_CONFIG_OVERRIDE_H using util/gpu/web.
Assisted-by: OpenAI Codex <codex@openai.com>
Avoid unnecessary round-trip of converting pre- and post-context from
Unicode codepoints to UTF-8 in C++ (hb-harfrust.cc) and back to Rust
&str (and eventually back to codepoints) in Rust (shape.rs).
Instead, use the new codepoint-based context setters in HarfRust.
Verified locally against CJK_article.html blink_perf menchmark to
gain approximately 2-4%.
Requires https://github.com/harfbuzz/harfrust/pull/371 and a newly
released HarfRust version.
Assisted-by: Google Antigravity
Co-authored-by: Behdad Esfahbod <behdad@behdad.org>
* [rust] Reinstate commented-out git branch for harfrust
For development purposes.
* [rust] Use new HarfRust GlyphFlags getter
Faster.
* [rust] Pin to new harfrust 0.8.2
When buffer verification detects non-monotone clusters, skip the
unsafe-to-break and unsafe-to-concat checks that rely on monotone
cluster order. Keep reporting the monotone-cluster failure as the
underlying verifier error.
Also turn the unsafe-to-break text range assertion into a verifier
failure so malformed intermediate verifier state does not abort the
process.
Testing:
meson test -C build
Assisted-by: OpenAI Codex
map_coords_16_16() converted roundf(delta * 4) back to int without
bounding the float first. With attacker-controlled avar2 long-word
(32-bit) ItemVariationStore deltas, delta * 4 can exceed INT_MAX, and
the implicit float-to-int conversion is undefined behavior per the C++
standard (out-of-range floating-to-integral conversion). The result was
clamped immediately afterwards, so there was no memory-safety impact,
but the conversion itself trips UBSan.
Clamp the float to the 16.16 normalized range before the conversion so
the narrowing can never go out of range.
Fixes: https://github.com/harfbuzz/harfbuzz/security/advisories/GHSA-xxfv-x2x5-8992
Assisted-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
`palt` (Proportional Alternate Widths) is widely used in Japanese
typography but was not retained in the default subset layout features,
unlike its vertical counterpart `vpal` and the closely related `halt`
/ `vhal` / `chws` / `vchw` already kept by default. As a result,
subsetting silently stripped `palt` data, and downstream CSS
`font-feature-settings: "palt"` had no effect.
Add `palt` to the default layout features list alongside the other
East Asian spacing features.
Verified locally with `meson test -C build` (240/240 OK, 0 failures)
and by subsetting NotoSansJP-Bold.ttf with `hb-subset`: `palt` is
stripped on main and retained with this patch, while the other CJK
spacing features (`halt`, `vpal`, `vhal`) are unaffected.
Fixes: https://github.com/harfbuzz/harfbuzz/issues/6006
Assisted-by: Claude
Wire the HarfRust shaper through the active hb_font_t callback
surface so custom hb_font_funcs_t are respected during shaping.
Add a harfrust-specific test-shape invocation to cover that path.
Tested: meson test -C build test-shape-harfrust
Tested: HB_SHAPER_LIST=harfrust meson test -C build harfbuzz:shape_threads
Tested: HB_SHAPER_LIST=harfrust meson test -C build test-shape
Assisted-by: OpenAI Codex
---------
Co-authored-by: Chad Brokaw <cbrokaw@gmail.com>
Round COLRv1 alpha values when packing them into 8-bit colors in the
native OpenType and FreeType paint paths.
This removes the old downward truncation bias and aligns native output
with the backend behavior expected by the updated paint fixtures.
Update the affected test-paint fixtures for the rounded alpha values.
Testing:
CCACHE_DISABLE=1 meson test -C build --print-errorlogs
Assisted-by: OpenAI Codex
Make the native COLRv1 paint paths report the root clip in font
units under the font transform. This matches the order used by
fontations and removes the need to pre-scale the HB-OT clip extents.
For the FreeType path, add a helper to convert FT_ClipBox values
back to font units before pushing them under the font transform, and
share that conversion with nested COLR glyph clip boxes.
Update paint and vector expectations for the new transform/clip stack.
Testing:
CCACHE_DISABLE=1 meson test -C build --print-errorlogs
Assisted-by: OpenAI Codex
Keep obsolete AAT offset-to-index helpers signed, but do the arithmetic
in 64-bit values before clamping back to an int index. This preserves
the existing large positive failure index used for invalid offsets while
avoiding signed int overflow for malformed data.
Testing:
- b/util/hb-shape CharcoalCY.dfont Empty-handed
- b/util/hb-shape CharcoalCY.dfont --unicodes=002D,002D
- test/shape/run-tests.py build/util/hb-shape test/shape/data/in-house/tests/kern-format2.tests
- meson test -Cb
Assisted-by: OpenAI Codex
Contextual and non-contextual AAT subtables replace glyphs in place.
When such a replacement produced the AAT deleted glyph, we changed only
the glyph ID and did not set the AAT-deleted metadata that later cleanup
uses to remove deleted glyphs from the buffer.
Make replace_glyph_inplace() match the other AAT replacement helpers by
setting the deleted scratch flag and glyph-info marker for glyph 0xFFFF.
Testing:
- b/util/hb-shape CharcoalCY.dfont --unicodes=002D,002D
- b/util/hb-shape CharcoalCY.dfont Empty-handed
- meson test -Cb
Assisted-by: OpenAI Codex
The legacy mort contextual subtable uses signed word offsets to
per-glyph substitution tables. A zero offset means that there is no
table for that action. We were treating the fields like morx lookup
indices with 0xFFFF as the sentinel, so zero-offset state entries tried
to substitute arbitrary glyphs from the start of the state table.
Keep morx using lookup-index semantics, but use signed offsets and the
zero sentinel for mort. This fixes CharcoalCY shaping of text such as
"Empty-handed", which previously produced out-of-font glyph IDs.
Testing:
- b/util/hb-shape CharcoalCY.dfont Empty-handed
- b/util/hb-shape CharcoalCY.dfont --unicodes=002D,002D
- meson test -Cb
Fixes: https://github.com/harfbuzz/harfbuzz/issues/5887
Assisted-by: OpenAI Codex
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
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.
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>
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.
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
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>
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.
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>
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>
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>
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>