Behdad EsfahbodandClaude Opus 4.8 0601dd774e [limits] depend build budget + GSUB closure budget (#6526)
* [subset] Charge Coverage and ClassDef glyph walks to a work budget

Coverage::subset() expands a format 2 Coverage glyph by glyph, up to
numGlyphs, every time an offset to it is subset, and nothing bounded
that work.  A 10-byte range Coverage referenced by 30,000 two-byte
offsets in a subtable listed 8 times costs 1.6e10 iterator steps,
while the sanitizer only bounds work by table bytes; a 60 KB font
keeps hb-subset busy ~50 s.  GDEF's MarkGlyphSets reaches the same
walk with no feature or lookup needing to survive planning.

Give hb_subset_context_t a per-table work budget
(HB_BUDGET_SUBSET_TABLE, in the hb_budget_spend() idiom of
hb-limits.hh) with a check_ops() helper that fails the serializer once
exhausted, and charge Coverage::subset() its capped population before
the walk.  Charging inside Coverage::subset() covers every caller
(ReverseChainSingleSubst, (Chain)ContextFormat3, GDEF MarkGlyphSets,
...) without touching them.  The same unbounded per-reference pattern
in ClassDefFormat1_3 / ClassDefFormat2_4::subset() (the range or
glyphset walk and the glyph_filter count) is charged the same way.

A hostile table that exceeds the budget now fails the subset cleanly
(hb_subset_or_fail returns NULL) in a fraction of a second instead of
succeeding slowly; valid-font output is byte-identical and the
heaviest local table spends a small fraction of the budget.
test_subset_shared_coverage_budget builds the reverse-chaining GSUB in
memory and asserts 256 shared references subset while 2048 fail.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-LLIWV9UK.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* [subset] Charge layout glyph walks to the existing subset budget

Drop the separate HB_BUDGET_SUBSET_TABLE budget and the second
hb_subset_context_t field it needed; check_ops() now charges
budget_remaining (HB_BUDGET_SUBSET_GLYF, 1 << 28), the per-table
budget glyf instancing already uses.  Both are per-table and shared
across buffer-size retries, and no table spends both.

The larger budget still bounds the attack (fails after 2^28 glyph
steps, ~3 s), so test_subset_shared_coverage_budget now expects the
failure at 8192 shared offsets instead of 2048.

Subsetting all glyphs with --layout-features='*' of Gulzar,
NotoSansKR-VF and GoogleSansFlex-VF keeps GSUB/GPOS/GDEF with output
byte-identical to before; the heaviest table (Gulzar GPOS) spends
~98K units, under 0.04% of the budget.  Full meson test: 278 OK.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* [depend] Clip context glyph sets to the face and charge a build budget

While extracting GSUB dependencies, find_or_create_glyph_set() turned
every Coverage (format 3 contexts) and class (format 2 contexts) that
a contextual rule mentions into an hb_set_t holding every glyph id the
table lists, whether the face has it or not, with no budget on the
expansion or on the second copy kept in the returned graph.  In
experimental builds, where the depend API and 24-bit Coverage formats
live together, a 6.5 KB font with 400 backtrack Coverage format 4
tables each covering [0, 0xFFFFFF] drives a build to ~2 GB.

Clip the cached sets to the face in find_or_create_glyph_set(), the
one place every context position goes through (the first input
Coverage and ReverseChainSingleSubst already clip this way), so the
copies kept in the graph shrink with them.  For real fonts this
changes nothing; only broken fonts stop carrying glyph ids past the
face.

Clipping alone is not enough: the temporary is still expanded in full,
and the glyph count itself is cheap to inflate with a MAXP table.  So
add a work budget for the whole build in the existing hb_budget_spend()
style: HB_BUDGET_DEPEND, budget_remaining on hb_depend_data_builder_t,
and charge_set(), which spends HB_BUDGET_4 per page while expanding a
temporary and HB_BUDGET_32 per page for every set the graph or context
keeps (new_set(), find_or_create_glyph_set(), find_or_create_recurse_set()).
Running out fails the build and hb_subset_depend_from_face_or_fail()
returns NULL, as it already does on allocation failure.  A
get_page_count() accessor is added to the bit-set types so the charge
does not reach into set internals; no public API change.

The 400-position font now peaks at ~11 MB instead of ~2 GB, the 64 KB
/ 4000-position font is refused, and hb-subset-depend output is
unchanged for real fonts.  Regression tests build the face with
hb_face_builder_create(): one asserts no glyph above the face count is
kept, the other that a 600-range face is refused.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-856K54QG.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* [layout] Charge GSUB glyph closure to a per-session work budget

hb_closure_context_t bounded GSUB glyph closure only by counting lookup
visits (HB_MAX_LOOKUP_VISIT_COUNT per round), and forgets which lookups
are done whenever the glyph set grows, so what one visit walks -- through
aliased offsets, many times the table size -- was charged to nothing.  A
LigatureSubst with 9000 ligatureSet offsets aliased onto one 64-component
Ligature, re-walked after every one of 1700 Context triggers that each
add a glyph, makes hb-subset --unicodes='*' of an 86 KB font spend ~110 s
of CPU.  A ReverseChain/ChainContextFormat3 with aliased backtrack
Coverages is a second way in.

Give the context an int64_t budget (HB_BUDGET_LAYOUT_CLOSURE records, in
the hb_budget_spend() idiom) spent as each closure() implementation walks
its arrays: dispatch() charges a visit its coverage record count;
ligature sets / ligatures / components, sequences, alternates,
single-subst intersections, rule sets and rules, context Coverages (via a
spend_coverages() helper) and the per-rule-set ClassDef walks are each
charged.  Coverage and ClassDef gain a get_record_count() helper because
intersects()/intersect_set() are linear in records, not population.  Once
the budget is spent, should_visit_lookup()/lookup_limit_exceeded() stop
the closure early, exactly as they do at the visit limit.

The budget lives in the context, so both public closure entry points (also
reached from hb_ot_shape_glyphs_closure() and the subset plan) are covered
with no API/ABI change.  Both attack shapes drop to ~1 s and the control
font's closure output is byte-identical; the heaviest real font in the
corpora uses about 1% of the budget.  A hostile GSUB now gets a truncated
closure, as one hitting the visit limit already does.
test_ot_layout_lookups_substitute_closure_budget builds the hostile table
in C and drives hb_ot_layout_lookups_substitute_closure() over it.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-J9K92HA9.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-10 22:29:59 -06:00
2026-11-04 23:03:36 +02:00
2019-04-21 12:26:07 -04:00
2026-04-06 17:08:50 -06:00
2022-06-21 13:19:08 -06:00
2026-11-04 23:03:36 +02:00
2026-11-04 23:03:36 +02:00
2026-01-24 14:04:46 +02:00
2023-04-14 11:35:34 -06:00
2022-08-03 11:31:34 -06:00
2023-11-06 13:25:42 -07:00

HarfBuzz

HarfBuzz Logo

Linux CI Status macoOS CI Status Windows CI Status OSS-Fuzz Status Coverity Scan Build Status Packaging status OpenSSF Scorecard

HarfBuzz started as a text shaping engine but has grown into a full font platform — the ffmpeg of text shaping. It primarily supports OpenType, but also Apple Advanced Typography.

HarfBuzz shapes the majority of text on modern screens.

HarfBuzz is optimized for robustness, correctness, and performance — in that order. Achieve all.

Try it live at harfbuzz-world.cc — an interactive playground for shaping, subsetting, rasterization, vector output, and GPU rendering, all running in your browser.

Here is a quick map of its components:

Core libraries

Library Description
libharfbuzz Text shaping, draw API, paint API. Highly configurable (see CONFIG.md). Optional integration backends compiled in: hb-ft (FreeType), hb-coretext (macOS), hb-uniscribe (Windows), hb-directwrite (Windows), hb-gdi (Windows), hb-glib, hb-graphite2.
libharfbuzz-subset Font subsetting and variable-font instancing.

Auxiliary libraries

Library Description
libharfbuzz-icu ICU Unicode integration.
libharfbuzz-cairo Cairo rendering integration.
libharfbuzz-gobject GObject/GI bindings.

Rendering libraries

Library Description
libharfbuzz-raster Glyph rasterization to bitmaps, including color fonts. Uses hb-draw and hb-paint.
libharfbuzz-vector Glyph output to vector formats (currently SVG), including color fonts. Uses hb-draw and hb-paint.
libharfbuzz-gpu Encodes glyph outlines for GPU rasterization (Slug algorithm). Provides shader sources in GLSL, WGSL, MSL, and HLSL. Live demo.

Notable missing feature: font hinting (including autohinting) is not implemented. For hinted rasterization, use FreeType or Skrifa.

For simplified builds, amalgamated sources are available: harfbuzz.cc (just libharfbuzz), harfbuzz-subset.cc (just libharfbuzz-subset), or harfbuzz-world.cc (everything, driven by a custom hb-features.h). For a live in-browser playground plus a worked example of the world.cc single-file build, see harfbuzz-world.cc.

Command-line tools

Tool Description
hb-shape Shape text and display glyph output.
hb-view Render shaped text to an image.
hb-subset Subset and optimize fonts.
hb-info Display font metadata.
hb-raster Render glyphs to bitmap images.
hb-vector Render glyphs to vector formats (SVG).
hb-gpu Interactive GPU text rendering.

The canonical source tree and bug trackers are available on github. Both development and user support discussion around HarfBuzz happen on github as well.

For license information, see COPYING.

API stability

The API that comes with hb.h will not change incompatibly. Other, peripheral, headers are more likely to go through minor modifications, but again, we do our best to never change API in an incompatible way. We will never break the ABI.

The API and ABI are stable even across major version number jumps. In fact, current HarfBuzz is API/ABI compatible all the way back to the 0.9.x series. If one day we need to break the API/ABI, that would be called a new library.

As such, we bump the major version number only when we add major new features, the minor version when there is new API, and the micro version when there are bug fixes.

Documentation

For user manual as well as API documentation, check: https://harfbuzz.github.io

Download

Tarball releases and Win32/Win64 binary bundles are available on the github releases page.

Development

For build information, see BUILD.md.

For custom configurations, see CONFIG.md.

For testing and profiling, see TESTING.md.

For using with Python, see README.python.md. There is also uharfbuzz.

For cross-compiling to Windows from Linux or macOS, see README.mingw.md.

To report bugs or submit patches please use github issues and pull-requests.

Developer documents

To get a better idea of where HarfBuzz stands in the text rendering stack you may want to read State of Text Rendering 2024. Here are a few presentation slides about HarfBuzz over the years:

More presentations and papers are available on behdad's website. In particular, the following studies are relevant to HarfBuzz development:

Name

HarfBuzz /hærfˈbɒːz/

From Persian حرف (Harf: letter) and باز (Buzz: open). Transliteration of the Persian calque for OpenType.

As a noun: The Open Source text shaping engine.

As an adjective: Insincerely talkative; glib. A nod to the GNOME project where HarfBuzz originates from.

The logo shows حرف‌باز in the IranNastaliq font, on a Damascus steel background.

Background: Originally there was this font format called TrueType. People and companies started calling their type engines all things ending in Type: FreeType, CoolType, ClearType, etc. And then came OpenType, which is the successor of TrueType. So, for my OpenType implementation, I decided to stick with the concept but use the Persian translation. Which is fitting given that Persian is written in the Arabic script, and OpenType is an extension of TrueType that adds support for complex script rendering, and HarfBuzz is an implementation of OpenType text shaping.

Users

HarfBuzz is used in Android, Chrome, ChromeOS, Firefox, Flutter, GNOME, GTK+, KDE, Qt, LibreOffice, OpenJDK, XeTeX, Adobe Photoshop, Illustrator, InDesign, Microsoft Edge, Amazon Kindle, PlayStation, Godot Engine, Unreal Engine, Figma, Canva, QuarkXPress, Scribus, smart TVs, car displays, and many other places.

xkcd-derived image

Distribution

Packaging status of HarfBuzz

Packaging status

S
Description
HarfBuzz text shaping engine
Readme
125 MiB
0 Stars 1 Watchers 0 Forks
Languages
C++ 73.3%
C 16.2%
Python 3.2%
Hack 2.3%
Meson 1.1%
Other 3.8%