Commit Graph
978 Commits
Author SHA1 Message Date
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
Khaled Hosny 9d7bba088a Try to fix msan bot (#6124)
* [test/fuzzing] Skip hb-depend-closure-parity without GLib

It depends on GLib unlike the other fuzzers.

* [ci] Build the msan bot without external dependencies

We don’t care about msan errors in code that is not ours, and some
libraries like GLib are not built with memory sanitizers in mind.

* [ci] Don't pipe sanitizer test output

The pipe was masking meson exit status, and it was redundant anyway
since the sanitizer output is is already symbolized and demangled.

* [COLR] Fix integer overflow in get_extents

Use to_glyph_extents(), which guards against this.

Update test expectation since we now round instead of truncate.
2026-07-30 19:02:25 -06:00
Behdad Esfahbod 23a0aa4746 Merge branch 'main' into avar2-instancing 2026-07-30 12:20:25 -06:00
Behdad Esfahbod c5eda499b4 Merge branch 'main' into codex-review-pr-5733 2026-07-30 11:40:20 -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
nussaibah shaikh 8c9307b59a [raster] clamp coordinates before converting to int (#6090)
* [raster] clamp coordinates before converting to int

the transformed glyph extents and the flattened segment endpoints are floats derived from int32 inputs and can land far outside the int range, so the conversions were undefined and the extent subtraction overflowed on top of that. saturate through a shared helper and widen the subtraction.

* [raster] generalize int32 saturating cast into hb_clamp_to<>

Move the raster-local hb_raster_clamp_to_int32 helper into hb-algs.hh
as a generic hb_clamp_to<T> that saturates a floating-point value to
the range of any integer type, and use hb_clamp_to<int32_t> at the
three raster call sites.

Signed-off-by: nussaiba shaikh <nussaibah@bugqore.com>

---------

Signed-off-by: nussaiba shaikh <nussaibah@bugqore.com>
2026-07-29 02:59:29 -06:00
Behdad Esfahbod da7f951ef0 Add more hb_barrier()s in switches (#6106)
Fixes https://github.com/harfbuzz/harfbuzz/security/advisories/GHSA-pmg2-m89x-pc6j
2026-07-27 19:20:18 -06:00
Khaled Hosny f3dea1e602 [subset] Add HB_SUBSET_FLAGS_CFF_IDENTITY_CHARSET
When set and subsetting a CID-keyed CFF font, the output charset is
written as identity: CID = new GID.
2026-07-27 22:12:16 +03:00
Sai Asish Y cce964cb4f [ot-tag] guard against NULL language in hb_ot_tags_to_script_and_language (#6085)
hb_ot_tag_to_language returns NULL for HB_OT_TAG_DEFAULT_LANGUAGE, and hb_language_to_string returns NULL for it, so the script-suffix path called strlen on a NULL pointer whenever the primary script tag did not match the input. Skip the suffix construction when there is no base language string and leave the default (NULL) language in place.

Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
2026-07-21 18:47:36 -06:00
nussaibah shaikh 53900fd388 [gpu] bound clip depth in push_clip_path_end (#6073)
push_clip_path_end wrote clip_stack[clip_depth] without the
HB_GPU_PAINT_MAX_CLIP_DEPTH check that push_clip_glyph and
push_clip_path_start both apply. clip_depth can be pushed past
the fixed 3-entry clip_stack, so closing a clip path while the
stack is full writes a pending_clip_t past the array. Guard it
like the sibling clip pushes.
2026-07-21 18:45:53 -06:00
nussaibah shaikh 387ab23f7f [raster] Reject non-finite texel coords in hb_raster_paint_image (#6082)
The range test guarding the bilinear sampler is comparison-based, so NaN
coordinates pass it and reach (int) floorf() in
hb_raster_sample_bilinear_premul, where only x1/y1 are clamped.  The
determinant guard has the same shape, so a transform that overflows to
infinity is accepted and 1/inf turns inf * 0 into NaN.
2026-07-21 18:40:41 -06:00
Nussu06 9de7e9d563 [paint] pass color_glyph its own user_data
The dispatch wrapper handed the callback user_data->push_clip_glyph
instead of user_data->color_glyph, so a client registering both
callbacks with different contexts got the wrong pointer on every
COLR glyph.
2026-07-17 11:43:34 -06:00
Garret Rieger d7861576ae More fixes for review comments. 2026-07-14 16:26:49 -06:00
Garret Rieger 2725565dab Address review comments. 2026-07-14 16:26:49 -06:00
Garret Rieger 99380f0e44 Add new hb_subset_input_to_command_line_or_fail().
This generates an equivalent hb-subset command line string for a given subset input.

Assisted by gemini.
2026-07-14 16:26:49 -06:00
Behdad Esfahbod 4fbbe7ae4f [ot-font] Invalidate vertical origin cache on scale change
The vertical-origin cache stores scaled y-origin values, but it was
validated against serial_coords. Changing only the font scale leaves
serial_coords unchanged, so hb_font_set_scale() could keep serving stale
vertical origins.

Validate scale-dependent OT font caches against the full font serial and
check that serial before using the batched vertical-origin callback. Add
an API regression that changes y_scale on one font and verifies the
vertical origin is recomputed.

Testing:
meson compile -C build test/api/test-ot-face
meson test -C build test-ot-face --print-errorlogs
meson test -C build --suite api --print-errorlogs

Fixes: https://github.com/harfbuzz/harfbuzz/issues/6066
Assisted-by: OpenAI Codex
2026-07-14 14:02:38 -06:00
Behdad Esfahbod 41ec50d54b [subset] Include serialize API from subset header
The subset umbrella header already carries the public subset API, but it
missed hb-subset-serialize.h even though the serialize symbols are part
of the subset section. Include the serialize header from hb-subset.h so
users can get the API through the top-level subset header.

Keep hb-subset-serialize.h directly includable for compatibility, but
emit a diagnostic asking users to include hb-subset.h instead. Update the
local repacker tests to use the umbrella header and verify the API is
available there.

Testing: meson compile -C build test/api/test-subset-repacker test/fuzzing/hb-repacker-fuzzer
Testing: meson test -C build --suite api
Testing: meson test -C build

Assisted-by: OpenAI Codex
2026-07-14 12:01:38 -06:00
Behdad Esfahbod 77256e876b [depend] Initialize test layout tag filter
The dependency test passes layout_tag to find_dependency() as an
in/out parameter. Initialize it to HB_TAG_NONE so the helper treats it
as an output slot instead of filtering by an indeterminate stack value.

Testing: meson test -C build --suite api

Assisted-by: OpenAI Codex
2026-07-14 11:21:16 -06:00
Khaled Hosny e2588968ae [COLR] Emit fill_glyph for glyph clips with a solid paint
Detect the fill-glyph pattern in the COLRv1 graph: a PaintGlyph whose
child is a (Var)PaintSolid, and emit a single fill_glyph instead of
push_clip_glyph + color + pop_clip.
2026-07-12 22:20:21 +03:00
Khaled Hosny b4ed968183 [test/paint] Test the fill_path callback 2026-07-12 19:36:15 +03:00
Behdad Esfahbod d287ee3ee2 [ft] Fix get_table_tags returning no tags on streaming faces
_hb_ft_get_table_tags() (used for FT faces created over a custom stream,
i.e. when ft_face->stream->read is set) did

    if (!table_count) return population;
    else *table_count = 0;
    ...
    end_offset = hb_min (start_offset + *table_count, population);

zeroing *table_count before using it as the requested buffer size, so
end_offset collapsed to start_offset, the copy loop never ran, and
hb_face_get_table_tags() always reported zero tags (and wrote none).

Restructure to match the CoreText / face-builder / default backends: keep
the caller's *table_count, check the start_offset + *table_count sum for
overflow, clamp to the table population, then copy.  Also guard the copy
on table_tags so a NULL output array (count-only query) is handled.

The existing FT test used FT_New_Memory_Face, whose in-memory face routes
through the default blob table-tags path and never exercised this
callback.  Add a streaming-face variant (FT_Open_Face with a read stream)
that does; it failed (count 0 vs 3) before this fix.

Assisted-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 01:08:45 -06:00
Behdad Esfahbod 706a2ba8d3 [ot] Guard remaining list-getter output arrays against NULL
Following GHSA-v5mv-8hhw-vff8, sweep the rest of the public "list getter"
APIs for the same defect: a NULL output array passed with a non-NULL count
on a populated table wrote through the NULL pointer and crashed (SIGSEGV).
These output params are not all annotated nullable, but the crash is
reachable from public C APIs and is a straightforward DoS, so guard the
write on the output pointer consistently (matching hb_color_line_get_color_stops
and the fixes in the previous two commits).  When the output is NULL the
getter now just returns the total count.

Getters hardened:

  - MathKern::get_entries              (hb_ot_math_get_glyph_kernings)
  - MathGlyphAssembly::get_parts       (hb_ot_math_get_glyph_assembly)
  - MathGlyphConstruction::get_variants (hb_ot_math_get_glyph_variants)
  - IndexArray::get_indexes            (hb_ot_layout_language_get_feature_indexes,
                                        hb_ot_layout_feature_get_lookups, ...)
  - RecordListOf::get_tags             (hb_ot_layout_table_get_script_tags,
                                        _get_feature_tags, _get_language_tags, ...)
  - AttachList::get_attach_points      (hb_ot_layout_get_attach_points)
  - LigGlyph::get_lig_carets           (hb_ot_layout_get_ligature_carets)
  - fvar get_axis_infos / get_axes_deprecated / get_instance_coords
                                        (hb_ot_var_get_axis_infos, hb_ot_var_get_axes,
                                        hb_ot_var_named_instance_get_design_coords)
  - OpenTypeOffsetTable / CoreText / face-builder get_table_tags
                                        (hb_face_get_table_tags)

Extend test/api/test-ot-nullable-output.c with table-tags, fvar axis-info
and MATH kerning cases; each crashed before this change.

Assisted-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 00:53:56 -06: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 dfa9711dc9 [test] Add avar2 self-contained axis tests
Make the coordinate comparison tag-based (self-contained pinned axes
are removed from the instance entirely; their baked contribution is
validated by the rendering checks) and the expected axis count
parametric.

New subtests: pinning opsz (NO_VARIATION_INDEX) off its default drops
the axis and bakes its constant final coordinate; pinning wdth combined
with a wght range restriction drops wdth while wght keeps offset
compensation (including wdth's constant push on wght's final
coordinate, re-added as a free-axis bias). The pin-at-default subtest
now expects wdth to be removed rather than kept hidden.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 18:57:00 -06:00
Behdad EsfahbodandClaude Opus 4.8 7c87fea07e [test] Add avar2 variation-culling tests
Extend TestAvar2Instance.ttf with a HIDDEN parametric axis XTRA
(driven by wght through a new avar2 delta row, final coord spanning
[-0.2, +0.3]), a gvar table for 'A' (tuples on XTRA inside and outside
the reachable range, on wght, and on GRAD), and an HVAR table with
matching regions.

The new culling subtest restricts wght to 300:400:500 and checks, over
a grid of retained locations, that final normalized coordinates AND
rendering-level outputs (advance width, glyph extents) match the
original font, and that the instanced gvar and HVAR shrank. The
moved-default subtest also gains rendering checks.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 18:57:00 -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
Behdad Esfahbod 6a0bcf4318 [VARC] Fix test fonts glyphDataFormat (#6046) 2026-07-02 14:01:19 -06:00
Garret Rieger 22d6df2a19 Fix missing addition of idDelta when collecting cmap4 unicodes and mappings. (#6038)
From the spec: The value c is the character code in question, and i is the segment index in which c appears. If the value obtained from the indexing operation is not 0 (which indicates missingGlyph), idDelta[i] is added to it to get the glyph index. The idDelta arithmetic is modulo 65536.
2026-06-24 03:00:52 -06:00
Skef Iterum c0059c1f61 [depend] Fix docs build and add missing hb-subset.h includes
The SECTION annotation in hb-depend.cc used the old name "hb-depend"
which didn't match the "hb-subset-depend" FILE name in sections.txt
after the header rename. This caused gtkdoc to report the section
description as unused and the section symbols as undocumented. Also
add HB_SUBSET_DEPEND_EDGE_FLAGS_T_DEFINED to a Private subsection
so gtkdoc doesn't flag it as an unused declaration.

The header move from hb.h to hb-subset.h (e3e754f9c) also left
util/hb-subset-depend.cc and test/api/test-ot-depend.c without
access to hb-subset-depend.h, causing build failures on sanitizer
CI jobs that build the utility and test. Add the missing
#include <hb-subset.h> to both.

Assisted-by: Claude <noreply@anthropic.com>
2026-06-05 15:09:13 -07:00
Skef IterumandClaude Sonnet 4.6 ea0b0767e7 [depend] Change hb_subset_depend_lookup_glyph to array-getter pattern
Follows HarfBuzz convention: return total count, take start_offset +
entry_count IN/OUT + entries array. This allows callers to query the
total without filling, page through entries, or fetch all at once.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-05 15:09:12 -07:00
Skef IterumandClaude Sonnet 4.6 6a5f32c505 [depend] Public API overhaul: rename to hb_subset_depend_*, struct fill, remove print
- Rename all public symbols hb_depend_* → hb_subset_depend_*
- Rename hb_depend_t → hb_subset_depend_t
- Replace get_glyph_entry (multi out-param) with lookup_glyph (fills
  hb_subset_depend_entry_t struct; fields use ligature_set_index /
  context_set_index)
- Rename get_set_from_index → lookup_set
- Remove hb_depend_print from public API (util/hb-subset-depend TODO)
- Move hb_subset_depend_edge_flags_t to hb-depend.h; apply
  HB_MARK_AS_FLAG_T in hb-depend-data.hh (internal header only, since
  hb-algs.hh is not available in public headers)
- Rename HB_DEPEND_EDGE_FLAG_* → HB_SUBSET_DEPEND_EDGE_FLAG_*
- Update gen-def.py, check-symbols.py, and all test callers

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-05 15:09:12 -07: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 975948b728 Validate ligature set contents in SourceSansPro test
Instead of just checking that the ligature set is non-empty, now verify
it contains the expected component glyphs (1823 and 1829) that form the
ligature 1910 (uni03020300).

This matches the validation pattern used in the NotoSans-Bold test and
provides proper regression testing.
2026-06-05 15:09:11 -07:00
Skef IterumandClaude 236a5822b1 Add full ligature set validation to SourceSansPro test
Enhance test_depend_gsub_formats to fully exercise the ligature set API
for SourceSansPro-Regular.otf, not just check that a set index is returned.

Changes:
- Create temporary set to retrieve ligature set contents
- Call hb_depend_get_set_from_index to retrieve the set
- Verify the set is non-empty (contains component glyphs)
- Log the number of components found
- Destroy temporary set

This matches the validation pattern used in the NotoSans-Bold test and
ensures the full depend API is exercised consistently across all
ligature tests.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-06-05 15:09:11 -07:00
Skef IterumandClaude f298400085 Address code review feedback for depend API
This commit addresses multiple code review comments:

1. Switch RNG to std::mt19937 for deterministic tests
   - Replace std::rand/srand with std::mt19937 in parity checker
   - Use std::uniform_int_distribution for all random number generation
   - Ensures test determinism across platforms for non-error-injection tests

2. Convert debug fprintf to DEBUG_MSG_LEVEL
   - Convert GSUB lookup processing prints to DEBUG_MSG_LEVEL(DEPEND, nullptr, 1, 0, ...)
   - Visible at HB_DEBUG_DEPEND=2 or higher for detailed per-lookup debugging

3. Rename function for consistency
   - hb_depend_from_face → hb_depend_from_face_or_fail
   - Follows HarfBuzz convention for functions returning nullptr on failure
   - Update all call sites, documentation, and code comments

4. Adopt established set deduplication pattern
   - Change from hb_vector_t<hb_set_t> to hb_vector_t<hb::unique_ptr<hb_set_t>>
   - Use pointer-based hashmap: hb_hashmap_t<const hb_set_t*, hb_codepoint_t>
   - Leverages hb_hash's content-based deduplication via pointer dereferencing
   - Follows pattern established elsewhere in HarfBuzz codebase

5. Separate ligature and context set semantics
   - Rename free_set → free_ligature_set (no hashmap manipulation)
   - Rename new_set → new_ligature_set (no hashmap manipulation)
   - Rename find_or_create_set → find_or_create_context_set (only function using hashmap)
   - Makes it explicit which sets are deduplicated (context) vs unique (ligature)
   - Update all call sites in Ligature.hh, ReverseChainSingleSubstFormat1.hh, hb-ot-layout-gsubgpos.hh

6. Code cleanup
   - Remove unused add_to_ligature_set function (dead code)
   - Refactor new_ligature_set(cp) to delegate to new_ligature_set(set)
   - Move set_to_index declaration near sets/free_set_list for better organization

All tests pass.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-06-05 15:09:11 -07:00
Skef IterumandClaude ce4564da0d Add edge flags to detect over-approximation from contextual positions
Edges created from non-zero positions in multi-position contextual rules
can consume intermediate glyphs (glyphs produced then immediately consumed,
never settling in closure). This causes depend's closure to include extra
glyphs compared to subset's closure, which is expected behavior.

Added HB_DEPEND_EDGE_FLAG_FROM_CONTEXT_POSITION flag to mark these edges
at extraction time. Updated parity checker to track when such edges are
hit during closure computation and distinguish between:
- Expected over-approximation (hit flagged edge): informational only
- Unexpected over-approximation (no flagged edge): critical error

This enables detection of genuine bugs (unexpected over-approximation)
while allowing expected cases from intermediate glyphs.

Updated all API callers and documentation to include nullable flags
parameter. Renamed parity checker terminology from "fuzzer" to "parity"
for clarity.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-06-05 15:09:11 -07:00
Skef Iterum d0cee3ca7e Update depend API docs and add explicit test mode to fuzzer
Documentation updates:
- Add comprehensive context_set documentation to depend-api.md with
  encoding details and example code for check_context_satisfied()
- Update depend-for-closure.md: remove obsolete over-approximation
  analysis, reflect that recent testing shows no over-approximation
  with proper context_set filtering
- Update depend-implementation.md: correct descriptions of active
  glyph filtering, sequential accumulation, ligature sets, and
  context recording to match current implementation
- Fix terminology: change 'binning' to 'segmentation' throughout
- Fix hb-depend-data.hh:557: use HB_CODEPOINT_INVALID instead of 0
  for layout_tag default value

Fuzzer enhancements (hb-depend-closure-parity):
- Add explicit test mode with command-line options similar to
  hb-subset conventions:
    --unicodes=LIST       Unicode codepoints (U+XXXX, decimal, ranges)
    --gids=LIST           Glyph IDs (decimal, ranges)
    --no-layout-closure   Skip GSUB closure
    --layout-features=LIST GSUB features (comma-separated or *)
- Support multiple input formats:
    --unicodes=U+0041,U+0042,U+0043
    --unicodes=65-90
    --gids=10-50 --no-layout-closure
    --layout-features=liga,calt or --layout-features='*'
- Add robust input parsing with validation to prevent infinite loops
  on invalid input (e.g., --gids=abc or --unicodes=U+GGGG)
- Add error checking for conflicting options

This allows reproducing random test cases manually and testing
specific scenarios without finding the right seed.

Tested: All modes produce identical results to seed-based tests.
2026-06-05 15:09:11 -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
Fuqiao Xue 6dda1a083c Add HB_SCRIPT_KATAKANA_OR_HIRAGANA (#5995)
* Add HB_SCRIPT_KATAKANA_OR_HIRAGANA

Fix #1473.

* Refactor HB_SCRIPT_KATAKANA_OR_HIRAGANA
2026-06-02 02:33:52 -06:00
Behdad EsfahbodandClaude Opus 4.8 d639197ed5 [COLR] Fix glyph extents for .notdef in COLRv1 fonts (#6012)
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>
2026-06-01 03:23:05 -06: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 Esfahbod 719a1d3c10 [paint] Round COLRv1 alpha consistently
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
2026-05-27 12:42:55 +02:00
Behdad Esfahbod 788706d568 [test] Normalize linear paint transcripts
Reduce linear gradient anchors and normalize color lines before
printing them in test-paint. This makes the transcript reflect the
renderer-facing gradient geometry instead of backend-specific raw
COLRv1 anchors and stop ranges.

Also normalize signed zero in transform output so the fixtures stay
stable across backends.

Testing:
CCACHE_DISABLE=1 meson test -C build test-paint --print-errorlogs

Assisted-by: OpenAI Codex
2026-05-27 12:40:44 +02:00
Behdad Esfahbod 56ac9c925f [paint] Push COLRv1 root clip under font transform
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
2026-05-27 10:48:19 +02:00
Behdad Esfahbod 202202b349 [test] Disable stale beyond-64k API checks
The disabled assertions cover behavior from an older beyond-64k proposal.
That proposal has since changed, and current backends should not be
required to implement the stale loca-derived glyph count and metrics rules.

Keep the checks visible as disabled reminders until the library code is
adapted to the updated design.

Tested with:
CCACHE_DISABLE=1 HB_FONT_FUNCS=fontations meson test -C build test-be-glyph-advance test-be-num-glyphs

Assisted-by: OpenAI Codex
2026-05-27 09:38:31 +02:00
parasol-aserandjeffhuang da577bc545 test: strengthen buffer and map API coverage (#5979)
Add focused API tests for hb_buffer_create_similar() and buffer
configuration persistence across clear/reset. Also exercise public hb_map
copy, equality, hash, update, iteration, keys, and values paths with
order-independent assertions.

Validation:
meson test -C build-tgl-precheck --print-errorlogs --suite api test-buffer
meson test -C build-tgl-precheck --print-errorlogs --suite api test-map
meson test -C build-tgl-precheck --print-errorlogs --suite api test-buffer test-map

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

Co-authored-by: jeffhuang <jeff@docker.xydrsucermoubd24xgo33yhsgd.bx.internal.cloudapp.net>
2026-05-06 17:27:10 -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
Behdad EsfahbodandClaude Opus 4.6 591748f1e1 [draw][paint] Make get_funcs instance-aware; add get_format
The six public draw / paint funcs getters across raster,
vector, and gpu used to be singletons:

  hb_raster_draw_get_funcs (void)
  hb_raster_paint_get_funcs (void)
  hb_vector_draw_get_funcs (void)
  hb_vector_paint_get_funcs (void)
  hb_gpu_draw_get_funcs (void)
  hb_gpu_paint_get_funcs (void)

That works fine for raster and gpu, where there's a single
backend per type, but breaks for the vector backend where
SVG and PDF have separate funcs sets — callers using the
public getter always got SVG funcs and silently produced
SVG markup inside PDF output streams when invoking the new
arbitrary-path clip API on a PDF context.

Make all six take a const-pointer to the relevant context
instance.  vector_paint dispatches on paint->format; the
other five just use the instance for symmetry today (room
for future format-style dispatch later).

Also expose hb_vector_draw_get_format / hb_vector_paint_
get_format public getters so callers (or third parties)
can ask a vector context for its format without poking at
internals.

Also: hb_vector_paint_push_clip_path_end now wraps the
emitted SVG clipPath's path in transform="scale(1,-1)" so
free-form arbitrary clips at base body level land in the
same coord system the per-glyph <use scale(_,-sy)>
wrappers establish.  Without this, free-form clip-path
arbitrary outlines emitted between paint_glyph calls came
out mirrored across the X axis.

Updates internal callers (hb_vector_paint_glyph,
hb_pdf_emit_glyph_path, hb_raster_paint_push_clip_path_
start, hb_gpu_paint_push_clip_path_start, the GPU pen
adapter), util sites (raster-output.hh, vector-output.hh,
gpu/demo-font.cc), API tests (test-raster.cc, test-gpu.cc),
and the docs section index.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:21:49 -06:00
Behdad EsfahbodandClaude Opus 4.6 98470a3e02 [draw] Rename hb_draw_rect to hb_draw_rectangle
API rename for the not-yet-released stroked/filled
rectangle helper added to hb-draw — full names
(hb_draw_line, hb_draw_circle, hb_draw_rectangle) read
better than mixed full/abbreviated.

Updates the public header, impl, the gpu doc reference,
the GPU API test, the GPU demo's extents overlay, the
extents-overlay paint helper, and the docs section
listing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:18:20 -06:00
Behdad EsfahbodandClaude Opus 4.6 f3c36ef3d6 [test] Add API tests for push_clip_path_start/_end
Folded into the existing test-paint binary.  Two tests,
both font-independent (registered ahead of the glyph-based
flavor setup so they run even on builds without the test
COLRv1 font):

* test_push_clip_path_nil_returns_null: an unset paint-funcs
  table returns NULL from push_clip_path_start and clears
  the out-param draw_data, and push_clip_path_end is safe
  to call on the nil stub.

* test_push_clip_path_round_trip: registered start/end fire
  in order, the returned draw-funcs + draw-data round-trip
  through the public entry points, and every hb_draw_*()
  call reaches the sink accumulator.  Asserts the count
  includes hb_draw_close_path's synthetic back-to-start
  line_to so the contract is documented by the test.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:18:20 -06:00