Commit Graph
65 Commits
Author SHA1 Message Date
nussaibah shaikh 6145d99262 Free the text copy on every hb_shape_justify() return path (#6422)
hb_shape_justify() copies the buffer text so it can reshape while
solving for the axis value, but only the solver path at the end
frees the copy. The three "nothing to solve" success returns
(default advance already in range, maximum expansion still short of
the target, maximum shrink still above it) and the three shaping
failure returns leak it, so ordinary line justification leaks one
text copy per call.

Free it with a scope guard right after the allocation, as the rest
of the tree does, and drop the explicit free at the end.

Reproduced with leaks --atExit on a plain experimental-API build:
four calls on Estedad-VF.ttf give four ROOT LEAKs at hb-shape.cc:319,
none with the fix. Add test-shape-justify.c covering all five paths
under the experimental-API test list so the asan-ubsan job runs it
under LeakSanitizer. The full ASan+UBSan Meson suite passes
(289 ok, 2 skipped).

Assisted-by: Claude
2026-10-20 11:25:59 -06:00
Behdad Esfahbod fac913b1de [math] Preserve and apply shared GDEF variation indices (#6385)
MATH devices were evaluated against an empty variation store, and the
subsetter dropped them without folding deltas into the base values.
MATH users were also omitted when pruning GDEF's shared store.

Evaluate MATH VariationIndex devices with GDEF's store. Collect indices
from retained constants, glyph information, and assemblies, then fold
pinned deltas and remap residual devices during subsetting. Preserve
ordinary ppem device corrections and reject unrepresentable values.

Add a reproducible two-axis fixture covering every MathValueRecord
location, axis orders, full and partial instances, and glyph pruning.
All 255 configured tests pass; reduced-feature syntax checks pass.

Assisted-by: Codex
2026-10-11 00:41:07 -06:00
Behdad Esfahbod 17af97aae4 Merge remote-tracking branch 'origin/main' into beyond-64k
Reconcile the beyond-64K table type changes with the dependency API,
graph result handling, and variation-budget updates from main. Preserve
the branch's wider table variants while adopting the new behavior.

Build and test with Meson (281 passed, 1 skipped).

Assisted-by: OpenAI Codex <noreply@openai.com>
2026-10-01 12:59:54 -06:00
Behdad Esfahbod 50046742d7 Merge branch 'main' into varc-subsetting
Resolve the VARC record-decoding conflict by preserving the shared
decoder while threading main's rendering budget through axis and
variation work.

Tests: meson test -C build (277 passed, 1 skipped)

Assisted-by: OpenAI Codex <codex@openai.com>
2026-09-07 12:16:12 -06:00
Behdad Esfahbod 8f3ab5ab87 [cairo] Share work across nested glyph rendering
Cairo's draw callbacks previously received a raw cairo_t, so paint-driven
outline traversals could not share a renderer budget.  Nested color glyphs
also construct fresh HarfBuzz paint contexts while populating the same
scaled-font glyph cache.

Attach a finite budget owner to the scaled font and let each draw/paint
context borrow it.  Recharge only for the outermost glyph callback, expose
the budget callbacks on both function sets, and charge emitted path and
nested color-glyph work.  Exercise the original vector timeout seed through
the Cairo user-font renderer.

Tests: meson test -C build (275/275 passed)

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 17:51:34 -06:00
Behdad Esfahbod cf7f500d65 [subset] Support VARC tables 2026-08-25 14:47:33 -06:00
ff217d3039 [graphite2] Fix cluster values when the buffer was reversed (#6156)
When the requested direction differs from the script's horizontal
direction, the Graphite2 shaper reverses the buffer before segmenting.
Input cluster values then decrease along the buffer, so assigning each
Graphite group the cluster of its base character can leave earlier
characters outside the resulting cluster map.

Preserve the source span of every Graphite glyph while constructing the
output. At monotone cluster levels, seed each glyph from the minimum
endpoint of that span and use merge_clusters() over each Graphite output
range. Keep the historical base-character behavior at non-monotone
levels.

HarfBuzz cluster values are opaque client labels and can be repeated or
gapped. Merging them can therefore coalesce multiple Graphite layout
groups. Preserve the Graphite group index separately for positioning so
every group's advance is emitted even when its HarfBuzz cluster was
merged. Validate Graphite slot source bounds before indexing the input.

Add shape regressions using a subset of Awami Nastaliq and an API test
with repeated input clusters. The latter used to merge the output
clusters correctly but drop an entire layout-group advance, reducing
the total from 3200 to 1269.

Tested with:
meson test -C build test-graphite2
meson test -C build --suite shape
meson test -C build



Assisted-by: Codex <codex@openai.com>

Co-authored-by: Michael Brown <michael.brown@rubicotech.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-22 12:48:13 -06:00
Khaled Hosny 6e67c5feed [ot] Add hb_ot_fetch_bits()
For fetching various bit fields scattered around OpenType tables. Many of
these are legacy or unreliable, but applications might need them for
various legacy reasons.

Fixes https://github.com/harfbuzz/harfbuzz/issues/1920
2026-07-31 04:31:26 +03:00
Behdad Esfahbod 23a0aa4746 Merge branch 'main' into avar2-instancing 2026-07-30 12:20:25 -06:00
Behdad Esfahbod 25fc94439a Merge pull request #5733 from skef/depend-draft-pr
OpenType font dependency graph support
2026-07-30 11:12:48 -06:00
copilot-swe-agent[bot] 96f6aa848a Merge branch 'main' into beyond-64k
Resolve conflicts by taking hb_barrier() additions from main for all
switch dispatchers (COLR, Coverage, CursivePos, SinglePos,
ReverseChainSingleSubst) while keeping the beyond-64k extended
format cases.

For GDEF.hh: take main's clean GDEFVersion1_2<Types> template union
approach (which already handles beyond-64k via GDEFVersion1_2<MediumTypes>)
and incorporate the LigCaretList<Types> templating from beyond-64k,
expanding get_lig_carets() and collect_variation_indices() to dispatch
directly per version.

For coverage-graph.hh: keep beyond-64k's <Types> template but add
main's const qualifier to the vertex parameter.

For gsubgpos-graph.hh: keep beyond-64k's delegation to
OT::GSUBGPOS::get_lookup_list_field_offset() since GSUBGPOS uses the
flat struct approach in this branch.

For hb-ot-cmap-table.hh: keep beyond-64k's find_variation_selector_subtable()
which handles both format 14 and format 15 (beyond-64k).

Also fix PaintGlyph<Types>::paint_glyph out-of-line template definition
to include the template parameter, incorporating the get_solid_color
optimization from main.

Assisted-by: GitHub Copilot
2026-07-29 17:48:41 +00:00
Behdad EsfahbodandClaude Opus 4.8 d75c58a2e4 [ot] Fix NULL-deref when nullable output arrays are passed to list getters
Several public "list getter" APIs document their output array as nullable
(pass NULL to just query the total count without storing anything), but
their implementations gated the copy only on the count pointer:

    if (count)
    {
      + source.sub_array (start_offset, count)
      | hb_sink (hb_array (out, *count));   // writes through out even if NULL
    }

Passing a non-NULL count with a NULL output array on a populated table
therefore wrote through the NULL pointer and crashed (SIGSEGV).

Gate each copy on the output pointer as well (the same idiom already used
by hb_color_line_get_color_stops: "if (count && out)").  This is a no-op
when the output array is non-NULL and restores the documented behaviour of
returning the total count when it is NULL.  Affected getters:

  - hb_ot_color_palette_get_colors            (CPAL)
  - hb_ot_color_glyph_get_layers              (COLR v0)
  - hb_ot_meta_get_entry_tags                 (meta)
  - hb_aat_layout_get_feature_types           (AAT feat)
  - hb_aat_layout_feature_type_get_selector_infos (AAT feat)
  - hb_ot_layout_feature_get_characters       (GSUB cvXX)
  - hb_ot_layout_lookup_get_glyph_alternates  (GSUB aalt)

Add test/api/test-ot-nullable-output.c exercising each route with a NULL
output array; every case crashed before this change.

Fixes: GHSA-v5mv-8hhw-vff8
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 00:28:35 -06:00
Behdad EsfahbodandClaude Opus 4.8 37b92ed336 [test] Add avar2 partial-instancing differential tests
Add TestAvar2Instance.ttf, a 1.6KB fontTools-built avar2 font with five
axes (wght, wdth, GRAD, XOPQ, opsz), interior avar v1 breakpoints on
wght, XOPQ and opsz, a wght delta row driven by wdth, a single delta
row shared by GRAD and XOPQ (driven by wght), wdth and opsz mapped to
NO_VARIATION_INDEX, and opsz's default near its axis minimum (so a
far-moved default makes the retained avar v1 segment steep in the new
space, the quantization-limited regime for offset compensation).

test-subset-avar2.c checks that a partial instance produces the same
final normalized coordinates (hb_font_get_var_coords_normalized: fvar
+ avar v1 + avar v2) as the original font at the same user-space
location, within 2 F2Dot14 units (5 for the steepest case, chosen one
unit below what dropping the interior-breakpoint collection produces,
verified by mutation testing), over grids of locations including the
exact kink positions. The eleven subtests cover: same-default and
moved-default restriction (both directions), shared-varIdx rows
(restricted, pinned, and both restricted), pinning at and off the
default, a pinned axis driven by a free axis, NO_VARIATION_INDEX axes
(fresh VarData creation), interior avar v1 breakpoints, and steep
moved-far-from-min defaults.

The subtests abort without the three preceding fixes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 16:35:49 -06:00
Skef IterumandClaude Sonnet 4.6 9a5d99f850 [depend] Address review feedback: preprocessor guards, minor API and naming fixes
Replace HB_DEPEND_API with HB_NO_SUBSET_DEPEND gated on HB_EXPERIMENTAL_API
and HB_LEAN in hb-config.hh, with HB_SUBSET_DEPEND_API opt-in to allow
enabling without full experimental API. Remove depend_api meson option;
depend tests now build under experimental_api=true. Fold depend symbols into
experimental symbols in gen-def.py.

Internal fixes: use free_set_list.pop(), push_or_fail() for allocation safety,
return const hb_set_t* from internal get_set_from_index. Ligature.hh: use
hb_sink iterator, rename ligset→ligset_idx, source→gid. Remove stale
test_depend_cmap unit test (cmap extraction was removed in d4d4c635).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-05 15:09:12 -07:00
Skef Iterum 29cf3aa519 Add depend API: graph extraction with contextual filtering
This commit implements the experimental depend API for extracting glyph
dependency graphs from OpenType fonts. The depend API provides the foundation
for computing glyph closures and analyzing font structure.

The implementation significantly improves upon the initial draft (commit
3f59433d5) by adding contextual filtering to reduce over-approximation while
still capturing complete dependency graphs.

Core Architecture
-----------------

The depend implementation operates in graph extraction mode: it walks all font
tables once to build a complete dependency graph. The key innovation is
contextual filtering that reduces over-approximation compared to the naive
approach.

Contextual Filtering for Reduced Over-Approximation
----------------------------------------------------

The initial implementation (3f59433d5) recorded dependencies for ALL glyphs in
every lookup's coverage, leading to significant over-approximation. The current
implementation filters dependencies based on context:

**Individual Lookups**: Each lookup's depend() method filters by active glyphs:

  // Old approach (3f59433d5)
  + hb_iter (this+coverage)  // ALL coverage glyphs
  | hb_apply (record_dependency)

  // New approach
  hb_set_t intersection;
  (this+coverage).intersect_set (c->parent_active_glyphs(), intersection);
  + hb_iter (intersection)  // ONLY contextually relevant glyphs
  | hb_apply (record_dependency)

**Contextual Lookups**: ContextFormat1/2/3 and ChainContextFormat1/2/3 set up
active glyph contexts for nested lookups:

  hb_set_t* cur_active_glyphs = c->push_cur_active_glyphs();
  get_coverage().intersect_set(*c->glyphs, *cur_active_glyphs);

  // Process rules with filtered coverage
  + hb_zip (this+coverage, ruleSet)
  | hb_filter ([&] (hb_codepoint_t g) {
      return c->parent_active_glyphs().has(g);
    }, hb_first)
  | hb_apply (process_rule)

This significantly reduces over-approximation for simple cases while maintaining
complete graph coverage.

**Intentional Over-Approximation at Nested Boundaries**: When contextual rules
invoke nested lookups, we pass ALL glyphs to ensure no edges are missed:

  // In context_depend_recurse_lookups()
  cur_active_glyphs->set (*c->glyphs);  // Universe for nested lookups

This design choice prioritizes completeness over precision at contextual
boundaries, which is appropriate for graph extraction.

Edge Deduplication and Performance
-----------------------------------

Dependency extraction can generate millions of potential edges. To maintain
acceptable performance:

- Edge hashing (compute_edge_hash): O(1) duplicate detection via hash table
  keyed on (source, table_tag, layout_tag, dependent, ligature_set_contents)
- Hash collision handling: Falls back to O(n) linear search through glyph's
  existing dependencies only on collision (rare)
- Component vectors: Stores edge components for collision detection
- Post-extraction cleanup: Frees temporary structures (edge_hashes,
  lookup_features) to reduce memory footprint

Table-Specific Extraction
--------------------------

- **cmap**: Unicode → glyph mappings including UVS (Unicode Variation Sequences)
- **GSUB**: All substitution rules with feature tags and ligature set tracking
  - Handles all 8 GSUB lookup types including Extension (type 7)
  - Records which features reference each dependency
  - Groups ligature alternatives via ligature_set indices
- **glyf**: TrueType composite glyph components
- **CFF**: CFF1 SEAC (Standard Encoding Accented Character) composites
- **COLR**: Color layer dependencies for both COLRv0 and COLRv1
  - Filters self-referential PaintGlyph to prevent trivial cycles
- **MATH**: Mathematical variant glyphs and assembly components

CFF SEAC dependencies use table_tag='CFF ' with layout_tag=0 and
ligature_set=HB_CODEPOINT_INVALID, matching the pattern used for glyf.

Data Structure: hb_depend_data_record_t
----------------------------------------

Each dependency edge contains:
- table_tag: Source table (GSUB, glyf, CFF, COLR, MATH, cmap)
- dependent: Target glyph ID
- layout_tag: Feature tag (for GSUB) or UVS codepoint (for cmap), else 0
- ligature_set: Index into sets array for ligature alternatives, else INVALID
- context_set: Reserved for future contextual filtering (currently unused)

Graph stored as vector indexed by glyph ID, each containing vector of records.

Testing and Documentation
--------------------------

- test/api/test-ot-depend.c: Comprehensive test suite (645 lines)
  - Tests all table types and GSUB substitution formats
  - Includes regression tests for edge cases
  - Uses ContextFormat2-Depend-Test.ttf (minimized from 486KB to 85KB)

- test/fuzzing/hb-depend-fuzzer.cc: Production-quality closure implementation
  - Demonstrates correct ligature set handling
  - Includes feature filtering and over-approximation analysis
  - Reference implementation for depend-based closure computation

- docs/depend-api.md: Public API documentation with usage examples
- docs/depend-for-closure.md: Guide to using depend for closure computation
- docs/depend-implementation.md: Implementation details and architecture

Build System
-------------

- Meson option: -Ddepend_api=true
- Compile flag: -DHB_DEPEND_API
- Symbols in libharfbuzz-subset.dylib (applications must link both
  libharfbuzz and libharfbuzz-subset)

Status: Experimental API, subject to change.
2026-06-05 15:09:11 -07:00
Behdad Esfahbod b98497786e [ot] Test extended COLR and VORG formats
Exercise PaintGlyph2 and VORG version 2 using glyph IDs above 65535,
including remapping during subsetting. Ignore both extended formats in
HB_NO_BEYOND_64K builds.

Assisted-by: OpenAI Codex
2026-06-05 13:13:50 +02:00
Behdad EsfahbodandChad Brokaw 77abf89986 [harfrust] Honor hb font funcs during shaping (#6002)
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>
2026-05-28 13:33:21 -04:00
Behdad EsfahbodandClaude Opus 4.6 971e607319 [gpu] Add API test and google-benchmark encode benchmark
test-gpu: 7 subtests covering create/destroy, empty encode,
glyph encode, reset, draw funcs singleton, shader sources,
and recycle_blob.

benchmark-gpu: benchmarks draw, encode, and draw+encode
per-glyph across several fonts using google-benchmark.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 22:27:40 -06:00
Khaled Hosny b91d4b8d75 Add meson options to disable building harfbuzz-subset 2026-03-04 18:12:45 +02:00
Khaled Hosny 9520926168 Add meson options to disable building harfbuzz-raster 2026-03-04 18:12:45 +02:00
Behdad EsfahbodandClaude Sonnet 4.6 c9d459b8e9 hb-raster: fix tile bounds clipping; move test to test/api/
Fix out-of-bounds pixel write in rasterize_tile: border tiles that
extend past the image edge were writing px-x_origin beyond the row
stride, wrapping into the start of the next row and overwriting
correctly-rasterized pixels with zero.  Pass px1/py1 (exclusive
bounds) to rasterize_tile and clip the pixel loops accordingly.

Also: drop unused ex0 in pass 2 of tile assignment; move test-raster
from src/ to test/api/ and convert it to the GLib g_assert/hb_test_add
framework.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad Esfahbod f69ecc9438 [test/api] Set protocol:tap 2025-04-06 02:34:48 -06:00
Behdad Esfahbod 6c98519c7e [test/api] Simplify deps 2025-04-05 21:41:01 -06:00
Khaled Hosny d74606bbff [test/ot-math] Port to use hb_test_open_font_file()
Avoids the unnecessary dependency on FreeType.
2025-04-06 03:22:20 +02:00
Behdad Esfahbod 8687f5c38e [face] Add tests for new constructors 2025-03-11 11:56:00 -06:00
Behdad Esfahbod 756668d35c [directwrite] Drop dependency again as per review
https://github.com/harfbuzz/harfbuzz/pull/5132#issuecomment-2714186460
2025-03-11 11:27:02 -06:00
Behdad Esfahbod 6b2f859232 [test-c] Disable gobject include
It can't find its dependent headers since not installed.
2025-03-11 00:37:11 -06:00
Behdad Esfahbod 41dcc493e5 [test-cplusplus] Simplify 2025-03-10 23:49:05 -06:00
Behdad Esfahbod dbad6cdfec [test-c(plusplus)] Add more headers 2025-03-10 23:23:16 -06:00
Behdad Esfahbod 3c02fcd0e8 [test-multithread] Simplify 2025-03-10 23:16:30 -06:00
Khaled Hosny a8fd29718a [directwrite] Add hb_directwrite_font_create()/font_get_font()
Uses hb_directwrite_face_create() under the hood, but copies variations
from directwrite to hb-font.
2025-02-20 21:40:27 +02:00
Khaled Hosny a7d7715f10 [shape-plan] Add hb_ot_shape_plan_get_features
This should get the features on a shape plan after executing it.

Initially I wanted to return an array of tags, but then there can be
user features that are not enabled globally, so I thought returning
hb_feature_t with value and range would be better. There is a TODO since
I couldn’t figure out how to get the value and range from the feature
mask. But also it may be overkill and a simple boolean indicating wither
it is a global feature or not would be enough.

I wounder also what should happen to non-user features that are applied
selectively, like init or medi, does ot make sense to indicate whether
they are global or not?

This is inspired by the discussion in:
https://github.com/fontforge/fontforge/pull/5522#pullrequestreview-2574321449,
but it might be useful to other HarfBuzz users.
2025-02-14 21:54:30 +02:00
Behdad Esfahbod c55738fa77 [test] Set MALLOC_CHECK_ to 1 instead of 2; kill G_SLICE=always-malloc
MALLOC_CHECK_=2 makes running tests twice slower on macOS. Value 1
is as fast as not checking at all. It's not like this ever caught
a bug for us or anything. We have so many sanitizers and fuzzers
funning on CI, which catch all kinds of memory problems.

G_SLICE is long gone in glib.

The slowest part of running the test suite now is the google-benchmark
tests, that get run by us... I'll see if I can disable. I had reported
it before at https://github.com/harfbuzz/harfbuzz/issues/4153
2025-02-09 08:11:01 +00:00
Behdad Esfahbod 8896b1d57a [test] Add test for get_table_tags
So far just tests the default implementation.
2024-08-06 12:34:15 -06:00
Behdad Esfahbod d07d70aef1 [varc] Add test 2024-04-16 17:10:46 -06:00
Behdad Esfahbod 132391ad62 [layout] Add test for font_extents 2023-05-19 16:10:21 -06:00
Matthias Clasen eb0a025e49 Add a test for glyph names
This verifies that hb_font_get_glyph_name
returns false for nonexisting glyphs.
2023-01-24 21:35:04 -07:00
Matthias Clasen dd64266ea4 Add a test for glyph extents
This verifies a recent fix for COLRv1 returning
0,0,-1,1 for extents of non-painting glyphs.
2023-01-24 18:08:22 -07:00
Garret Rieger 0f01a83622 [subset] add basic test for CFF2 subsetting.
FontTools does not yet support CFF2 subsetting so we can't add a comparison test. Instead add a golden file test.
2023-01-09 21:15:11 -07:00
Matthias Clasen 313f74a693 Add a basic test for hb-coretext api
This tests what would be my minimum assumption
about this api. It was written blindly.
2022-12-28 11:14:22 -07:00
Matthias Clasen 661baf403c Add a basic test for hb-ft api
This tests what would be my minimum assumption
about this api.
2022-12-28 11:14:22 -07:00
Matthias Clasen ca190aaba4 Split off the hb-paint tests
They belong in their own file.
2022-12-23 13:37:59 -07:00
Qunxin Liu a35757c6bc [repacker] expose hb_subset_repack() API, hb_object_t and hb_link_t structs 2022-03-10 17:26:20 -07:00
Behdad Esfahbod 379e526aa4 [test] Add test for current hmtx logic 2022-02-14 15:02:31 -06:00
Behdad Esfahbod ed6d287d11 [ot-face] Load num-glyphs from loca table before maxp
Implements [boring-expansion] [maxp] Relax
https://github.com/be-fonts/boring-expansion-spec/issues/6
2022-02-14 13:52:30 -06:00
Matthias Clasen 8dc86d0703 Add a test font scale rounding behavior
This test verifies the fix for
https://github.com/harfbuzz/harfbuzz/issues/3274
2021-11-01 17:59:48 -07:00
Khaled Hosny f60caad7ff [test] hb_ot_layout_table_get_script_tags() 2021-08-11 16:33:37 -06:00
Khaled Hosny f998d7e1ce [test] Add API test for lookup_collect_glyphs()
We don’t seem to have any tests exercising this API at all, and it is a
good check of GSUB/GPOS code.
2021-08-03 10:06:27 -06:00
Ebrahim Byagowi 694a0c6bbc [meson] Always add icu deps 2020-07-06 22:19:59 +04:30
Ebrahim Byagowi 1348a2c865 [layout] hb_ot_layout_lookup_get_alternates, a new API
An API to retrieve one-to-one alternates from a given GSUB lookup index.
2020-06-18 09:11:07 +04:30