Commit Graph
31 Commits
Author SHA1 Message Date
Wei-Lun Li 24ded4335b [font] Saturate synthetic bold and glyph origin arithmetic (#6585)
* [font] Saturate synthetic bold and glyph origin arithmetic

#6154 made hb_position_t arithmetic saturate and #6296 did the same
for synthetic_glyph_extents(), but the rest of hb_font_t still used
plain signed arithmetic:

- changed() computed the embolden strength with abs (x_scale), which
  overflows for INT32_MIN, and converted the float product to int
  without a range check. A strength of INT32_MIN could also not be
  negated later on, for negative scales.
- get_font_[hv]_extents(), the advance, origin and contour point
  getters added the strength with += and could overflow.
- add_offset(), subtract_offset() and apply_offset() overflow when
  they apply a large origin, and so do the origin fallbacks in
  apply_glyph_[hv]_origins_with_fallback().
- em_multf() converted its float result to hb_position_t without a
  range check, which is undefined for a value such as an ascender
  above the em at a scale close to INT32_MAX.

Use hb_saturate_add(), hb_saturate_sub() and hb_clamp_to() in these
places, and keep the strength away from INT32_MIN so that negating it
stays defined. Results that fit do not change.

The shift variables that were int are now hb_position_t, since the
saturating helpers deduce one type and int32_t is not int everywhere.

Tested with test-font under UBSan: the new tests fail on the base
commit at hb-font.hh:365, :848, :1154 and :1167, and pass here.

Assisted-by: Claude

* [test] Cover plural origins and synthetic slant saturation

Review feedback: the plural origin getters and the slant
clamp in get_glyph_contour_point were not covered by the
tests, so removing their saturation would still pass.

test_synthetic_bold_saturates now also calls
hb_font_get_glyph_h_origins and hb_font_get_glyph_v_origins
once each and checks both coordinates for INT32_MAX. A new
test_synthetic_slant_saturates sets a slant of 1 on a contour
point one short of INT32_MAX in both axes and checks that x
saturates to INT32_MAX.

Verified by mutation: replacing the plural hb_saturate_add
calls with plain addition makes the first test fail (the
value wraps), and dropping the hb_clamp_to in the slant path
makes the second fail (x comes out as -2). Full
meson test -C build: 270 ok, 0 fail, 7 skipped
(subset+repack, missing repacker dependency; same as the
baseline run for this PR).

Assisted-by: Claude

* [font] Apply the synthetic slant shift before clamping

get_glyph_contour_point clamped the slant shift to
hb_position_t before adding it to x. When x and the shift
have opposite signs and partly cancel, clamping first
changes the result: a contour point at x = -2000000000,
y = 2000000000 with a slant of 2 should land at
x = 2000000000, but came out as 147483647, because the
shift was clamped to INT32_MAX before the addition.

Add the shift first and clamp the sum instead. The
existing saturation case is unchanged: hb_clamp_to
widens to double to do the clamp, so an out-of-range sum
still saturates instead of hitting an undefined
float-to-int conversion.

The new test_synthetic_slant_cancelling covers the
cancelling case. Verified by mutation: restoring the
clamp-first order makes it fail with 147483647, the
reported value. Full meson test -C build: 270 ok,
0 fail, 7 skipped, same as baseline.

Assisted-by: Claude
2026-11-09 11:47:15 -06:00
m00dy b269f9e1ad [font] Copy the default font-functions environment value (#6563)
The default cache borrows HB_FONT_FUNCS storage from the environment.
Copy its initial value with the existing lazy-loader conventions so
later changes to caller-owned storage do not change the default.

Preserve environment selection under HB_NO_ATEXIT. In that build the
copy has process lifetime; HB_NO_GETENV still uses the normal default.

Strengthen the coverage so an ignored environment value can no longer
hide behind the first backend: the environment test also checks that the
FreeType face is not selected when the default is ot, and a new
subprocess test selects ft, a non-first entry, before any font is
created.

The snapshot test fails before the library fix and passes afterward.
All 17 font API cases pass. The full suite has 276 passes and no
failures. A throwaway variant with the environment lookup stubbed out
fails the new nonfirst test, so the assertion does bite.

Assisted-by: an AI coding assistant
2026-11-03 15:18:24 -06:00
nussaibah shaikh 47b18b6ebd [font] Don't read stale design coords in hb_font_set_variation() (#6402)
hb_font_set_variation() seeds its new design coords from the font's
existing ones for every axis of the current face, trusting an assert
that their lengths match. hb_font_set_face() keeps the font's
variation coords, which stay sized for the old face, so switching to
a face with more axes and then calling hb_font_set_variation() reads
past the old array: a heap-buffer-overflow read with NDEBUG, an
assertion failure otherwise.

Only reuse the existing design coords when their length matches the
current face; otherwise start from the defaults, as when none are set.

Reproduced the overflow under ASan with NDEBUG and the assertion
failure in the default build. Added a regression test in test-font.c
that fails without the fix. The full ASan+UBSan Meson suite passes.

Assisted-by: Claude
2026-10-16 12:18:00 -06:00
Mar 5365d272d5 [font] Deprecate hb_font_set_face()
Changing the face now resets font funcs. Deprecate the setter in favor
of creating a new font with hb_font_create() and setting its properties.
Keep the exported symbol and move its declaration to hb-deprecated.h.

Document the deprecation in NEWS and the API section index. Guard the
face-setter tests with HB_DISABLE_DEPRECATED, and suppress expected
warnings from their deliberate use of the deprecated API.

The full ASan Meson suite passes (270 passed, 1 skipped). test-font also
builds and passes with HB_DISABLE_DEPRECATED. Its source passes Clang
-Werror checks both with and without deprecated APIs enabled.

Assisted-by: Claude
Assisted-by: Codex
2026-10-16 00:19:21 +03:00
Mar ef64f7ea8b [font] Reset font funcs in hb_font_set_face()
Font funcs and their data may hold pointers into the font's face.
hb_font_set_face() released the old face but kept those funcs, causing
stale lookups or a use-after-free when the last face reference was gone.

Release the font funcs and their data while the old face is still alive,
then install the default funcs for the new face. Preserve the other font
properties and document the callback reset in the API docs and NEWS.

Add regressions for OpenType lookups and custom font-data destruction.
Reproduced stale glyph lookup and the heap-use-after-free under ASan
before the fix. The full ASan Meson suite passes (270 passed, 1 skipped),
and the two regressions also pass with HB_FONT_FUNCS=ft.

Assisted-by: Claude
Assisted-by: Codex
2026-10-16 00:19:21 +03:00
Behdad Esfahbod 5d86ce2980 [rust] Keep disjoint interleaved fields on the batch path
Overlapping bounding ranges do not imply that strided input and output
slots overlap. For equal strides, check field separation modulo the
stride, including the gap across record boundaries. Share this check
between advances and vertical origins.

Cover interleaved glyph/position records and hb-ot-normalize's mapping
from codepoint to var1.u32 within the same glyph-info array.
Tests: full Meson suite (281 passed, 1 skipped), using opt-level=0 for
Clippy's nightly compiler workaround. An exhaustive standalone check
covered 3,136,320 stride/offset/count layouts. Seven-run medians for
interleaved Roboto improved 2.64x for horizontal advances, 22.77x for
vertical advances, and 1.77x for vertical origins.

Assisted-by: OpenAI Codex
2026-10-03 19:32:19 -06:00
Behdad Esfahbod cdf5edb49e [rust] Batch Fontations vertical origins
Register the vertical-origins batch callback. Compute X using HarfBuzz's
batched horizontal advances, then compute Y with v_origin_y_batched.
Retain HarfBuzz line and glyph-extents callbacks for fallback behavior.
Preserve scalar write order for aliased inputs, overlapping outputs, and
repeated X slots while batching normal interleaved position buffers.

Add default/varied glyf, CFF, and CFF2 coverage with strided, unaligned,
in-place, repeated, invalid-glyph, and zero-count queries.
Tests: full Meson suite (281 passed, 1 skipped); font-only Clippy; unchanged
144-case metric snapshots. Another 48 full-font batch cases (53,622 glyph
origins) matched the old scalar implementation. Clippy used opt-level=0
for the nightly compiler workaround.

Seven-run origin medians improved about 19% overall, about 36-38% for
unvaried glyf fonts; varied glyf and CFF/CFF2 remain dominated by extents.

Assisted-by: OpenAI Codex
2026-10-03 19:19:55 -06:00
Behdad Esfahbod 71fc895c78 [rust] Scope strided output references to each metrics batch
Replace safe pointer helpers that manufactured static mutable references
with explicit unsafe reads/writes and a callback-scoped output iterator.
Compute byte offsets in pointer width. Preserve direct batch writes for
aligned, disjoint buffers; use stack outputs for unaligned, repeated, or
overlapping slots without allocating or creating aliased references.

Add unaligned nominal/advance, padding, repeated output, and in-place
advance coverage. Tests: full Meson suite (281 passed, 1 skipped), font-only
Clippy, and unchanged 144-case metric snapshots. Clippy used opt-level=0
for the nightly compiler workaround.

Seven-run medians across 36 nominal/horizontal/vertical advance cases,
compared again with the saved old library on one CPU, showed no slowdown:
nominal about 6% faster, horizontal unchanged, vertical about 36% faster.

Assisted-by: OpenAI Codex
2026-10-03 19:11:51 -06:00
Behdad Esfahbod 59e454c899 [rust] Reuse Fontations instances when coordinates are unchanged
Font serial changes include scale changes, which need not discard the
read-fonts instance's variation and metrics caches. Compare normalized
coordinates using the builder's padding, truncation, and default-location
rules, and rebuild only when the effective coordinates change.

Test scale changes, zero coordinates, omitted axes, extra axes, and resets
against fresh Fontations instances. Preserve existing rounding behavior.
Tests: full Meson suite (281 passed, 1 skipped); 144 font/scale/location
snapshots matched the previous implementation. Clippy used opt-level=0
for the nightly compiler workaround.

Assisted-by: OpenAI Codex
2026-10-03 19:03:23 -06:00
Behdad Esfahbod f902205a0c [rust] Publish the glyph-name cache without replacement
Concurrent readers could replace and destroy a face's reverse-name map
while another reader was using it. Publish with replace=false, reclaim
losing maps, and retrieve the winner. Return failure if publication fails
without a winner instead of retrying indefinitely on allocation failure.

Add a concurrent initialization regression that fails before the fix.
Tests: full Meson suite (281 passed, 1 skipped). Clippy used opt-level=0
to avoid a nightly compiler timeout; library compilation remained opt=3.

Assisted-by: OpenAI Codex
2026-10-03 18:58:43 -06:00
Behdad Esfahbod 600f1cc8ae [rust] Use read-fonts model for Fontations font functions
Move character mapping, line and glyph metrics, vertical origins, and
glyph names to the new Font model. Use its batched advance APIs while
preserving horizontal rounding and HarfBuzz fallback callbacks.

Share the blob owner with the shaper and remove the metrics lifetime
transmute. Enable two existing vertical variation regressions for
Fontations and add strided batch advance coverage.

Tests: ninja -C build lib; meson test -C build (281 passed, 1 skipped).
Font-only and shape-only cargo checks also passed; the shape-only check
used CARGO_INCREMENTAL=0 after a nightly compiler crash.

Assisted-by: OpenAI Codex
2026-10-03 17:25:05 -06:00
nussaibah shaikh 3c8a65ed9d [font] fix signed integer overflow in synthetic_glyph_extents (#6296)
* [font] fix signed integer overflow in synthetic_glyph_extents

synthetic_glyph_extents applies synthetic slant and embolden to a
glyph's extents with plain hb_position_t arithmetic: x_bearing +
width and y_bearing + height in the slant branch, plus the += / -=
shifts in the embolden branch. glyf's contour_bounds_t and the sbix
png-dimension path each clamp x_bearing/width and y_bearing/height to
the full hb_position_t range independently via
hb_clamp_to<hb_position_t>(), so one field can reach INT32_MIN/MAX on
its own and the sum overflows, which is undefined behavior (UBSan
signed-integer-overflow at hb-font.hh:221). Reached from
hb_font_get_glyph_extents when synthetic slant/bold is set and the
glyph has extents but no drawable outline.

This is the sibling of 873dbc1 (scale_glyph_extents), which fixed the
same x_bearing + width overflow in the adjacent function. Accumulate
the slant terms in double and the embolden terms in int64, saturating
each result to hb_position_t with hb_clamp_to, matching that fix and
the copy in hb-ft.cc. Valid in-range extents are unchanged.

Add a regression test in test/api/test-font.c that drives the sink
with INT32-extreme extents; it aborts under the asan/ubsan job without
the fix. Full meson test -C build stays green.

* [font] use int64_t for slant accumulation in synthetic_glyph_extents
2026-09-30 13:04:22 -06:00
Behdad Esfahbod 155e1e633b [test/api] Use g_assert_true instead of g_assert
The latter terminates the program. The former marks the test
as failing.
2025-04-06 03:49:15 -06:00
Behdad Esfahbod 74bb7c52f3 Fix a compiler warning 2025-02-24 20:21:38 -07:00
Khaled Hosny 4825e5e2b4 [face] Add fallback implementation to hb_face_reference_blob
If referencing the face blob is not possible (e.g. not implemented by
the font functions), use face builder to create a blob out of
individual table blobs.

Fixes https://github.com/harfbuzz/harfbuzz/issues/5036
2025-02-04 15:45:22 +02:00
Behdad Esfahbod 148d883680 [test] Don't call deprecated API 2019-05-13 14:34:53 -07:00
Chun-wei Fan 1e09add232 test/api: Fix building on pre-C99 compilers
Ensure variables are declared at the top of the block.
2018-12-12 01:32:01 +08:00
Behdad Esfahbod 777c22425c [test] More funcs in test-ot-face 2018-11-04 02:40:20 -05:00
Ebrahim Byagowi 8931bc4a6b [test] Fix -Wunused-parameter on test-font.c 2018-10-20 23:23:32 +03:30
Behdad Esfahbod f11c557662 [test] Fix leak 2018-10-20 12:20:57 -07:00
Behdad Esfahbod 77d5c3df07 [font] Add failing test amongst font-func parallels infinite-looping
Reported by Nona while updating Android to HarfBuzz 2.0.0.
2018-10-19 19:01:01 -07:00
Behdad Esfahbod dcfcb950b8 [test] Fix -Wunused-parameter warnings 2018-09-30 18:14:50 +02:00
Ebrahim Byagowi ef9307fd22 Fix test-font leak issue
See also https://github.com/harfbuzz/harfbuzz/pull/1169
2018-09-22 16:45:31 +03:30
Philip Withnall a6ced90efc test: Fix some memory leaks in test-font.c (#409)
Coverity IDs: 141039, 141040, 141041
2017-02-07 18:18:33 -08:00
Behdad Esfahbod da29b43794 Separate nil font-funcs from parent-peeking font-funcs
API change: all fonts created with hb_font_create() now inherit from
(ie have parent) hb_font_get_empty().
2015-11-04 20:22:44 -08:00
Behdad Esfahbod 3e905e396b Add hb_font_set_parent()
No reason to not have it.  Makes life easier later.
We (hb-ft, hb-ot-font, etc) can use this API to inject new
parent into a font...
2015-10-08 12:51:02 -04:00
Behdad Esfahbod 88da7bba9f Default font scale to face upem
Makes for a better default and avoids nasty inheritance issues.
See mailing list thread "Default hb_font_t scale".
2015-10-02 14:38:20 +01:00
Behdad Esfahbod 1866e17114 Make hb_font_create_sub_font() NOT make parent immutable
We don't rely on that.  However, whenever hb_font_make_immutable()
is called, it makes its parenting chain immutable.
2015-10-02 14:21:29 +01:00
Behdad Esfahbod eb0bf3ae66 Relax inert checks
Previously, when creating an object from inert inputs (eg:
"hb_font_create(hb_face_get_empty())") we returned the inert
empty object.  This is not helpful as there are legitimate
usecases to do that.

We now never return the inert object unless allocation failed.

Tests are revised to reflect.
2015-04-09 19:16:57 -07:00
Behdad Esfahbod 7235f33f9e Fix misc warnings reported by cppcheck
https://bugs.freedesktop.org/show_bug.cgi?id=65544
2013-06-10 15:08:49 -04:00
Behdad Esfahbod 4d6dafd47f Rename test/ to test/api/ 2012-01-19 14:52:02 -05:00