Commit Graph
10 Commits
Author SHA1 Message Date
Behdad Esfahbod cfe86ce5e7 [subset] Harden VARC closure handling
Propagate allocation and component-decoding failures from VARC closure
instead of continuing with an incomplete glyph set. Treat malformed VARC
records as dependency-extraction failures as well.

Document VARC dependency edges and exercise subset variation-store
remapping at non-default coordinates.

Tested with:
- meson test -C build
- standalone lean and world syntax checks
- CMake harfbuzz-subset build

Assisted-by: OpenAI Codex <openai-codex@users.noreply.github.com>
2026-09-10 12:11:13 -06:00
Skef IterumandClaude Sonnet 4.6 211d7646f0 [depend] Update documentation for public API rename
Update all doc files to reflect the hb_subset_depend_* rename, the
removal of print from the public API, the new struct-based entry type,
the array-getter signature for lookup_glyph, and the switch from the
old depend_api/HB_DEPEND_API build controls to experimental_api /
HB_NO_SUBSET_DEPEND / HB_SUBSET_DEPEND_API.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-05 15:09:12 -07:00
Skef IterumandClaude Sonnet 4.5 a1114985d6 Remove cmap depend extraction - UVS now handled via query API
With UVS (Unicode Variation Sequence) closure now handled directly via
hb_font_get_variation_glyph() query API, the cmap depend extraction code
is no longer needed. This commit removes all cmap-related depend code:

Code removals:
- src/hb-depend.cc: Remove get_cmap_dependencies() function
- src/hb-depend.hh: Remove get_cmap_dependencies() declaration
- src/hb-ot-cmap-table.hh: Remove 4 depend() methods (NonDefaultUVS,
  VariationSelectorRecord, CmapSubtableFormat14, cmap)

Documentation updates:
- docs/depend-api.md: Remove cmap from table list, add UVS note
- docs/depend-for-closure.md: Update closure steps, reference impl notes
- docs/depend-implementation.md: Update table list in Future Considerations

The old cmap depend code only extracted format 14 (UVS) edges, so removing
it does not affect fonts without UVS mappings.

Testing: 153 fonts in test/api/fonts (10k iterations each), 10 fonts with
UVS mappings (1M iterations each) - all pass.

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-06-05 15:09:12 -07:00
Skef IterumandClaude ec97c40d5f Fix CI: Filter depend API symbols from exports when depend_api=false
The depend API follows the same pattern as experimental_api: the code
is always compiled (guarded by #ifdef HB_DEPEND_API), but symbols
should only be exported when the option is enabled.

Changes:
1. gen-def.py: Add --depend-api flag and filter list (like --experimental-api)
2. src/meson.build: Pass --depend-api flag when option is enabled

This ensures depend symbols are excluded from .def files when depend_api=false
(the CI default), fixing macOS check-symbols test failures.

🤖 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 Opus 4.5 b351a2dc9c Fix under-approximation in context set filtering and update edge flag docs
Two related fixes:

1. Context set under-approximation fix (hb-ot-layout-gsubgpos.hh):
   When a direct requirement (from input positions) overlapped with a
   disjunctive set (backtrack/lookahead), the code incorrectly added
   the filtered (remaining) set as an additional requirement. Fix:
   if position_context intersects with original_set at all, the
   disjunctive requirement is already satisfied - don't add anything.

2. Parity checker ligature fix (hb-depend-closure-parity.cc):
   When processing deferred ligatures, store {dependent, ligature_set,
   flags} at deferral time instead of re-scanning edges later. This
   preserves the feature filtering that was already done and correctly
   propagates edge flags.

Also updates edge flag documentation to correctly explain the mechanism:
- Depend extraction records edges based on static input coverage/class
- During closure, lookups within a rule are applied sequentially
- Lookups at earlier positions may transform later positions
- Multiple lookups at same position may interact (one produces a glyph,
  another immediately consumes it as an "intermediate")
- So a glyph matching coverage might not persist when closure traverses

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

Co-Authored-By: Claude Opus 4.5 <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
Skef Iterum 4ea82e1afc Add comprehensive documentation for depend API
This commit adds complete documentation for the dependency analysis API
at all levels: public API, internal structures, and implementation.

Public API documentation (src/hb-depend.cc, src/hb-depend.h):
- Replaced "XXX Todo" placeholder in SECTION comment with detailed
  module description explaining dependency graphs and use cases
- Fixed misleading hb_depend_t description (was only mentioning UVS)
- Added full documentation for hb_depend_print() with debugging notes
- Added usage examples to all three public functions
- Updated all version tags from "?.0.0" to "REPLACEME"
- Added experimental API warnings throughout

Internal documentation (src/hb-depend-data.hh, src/hb-depend.hh):
- Documented 5 internal structures with purpose and ownership semantics
- Added comments explaining data relationships and lifecycle

Algorithm documentation (src/hb-depend.cc):
- Added detailed algorithm comments to 5 extraction methods explaining
  how dependencies are collected from each OpenType table (cmap, GSUB,
  glyf, COLR, MATH)

User guide (docs/depend-api.md):
- Created comprehensive 250-line API guide with:
  * Introduction with mechanism overview
  * Basic usage example (iterating dependencies)
  * Advanced examples (computing reachable glyphs, ligature sets)
  * API details and compilation instructions
  * Implementation notes including ownership semantics
  * Use cases (subsetting, coverage analysis, binning, testing)
- Prominent experimental API warnings

Build configuration (meson_options.txt):
- Updated option description to emphasize experimental status

All documentation follows HarfBuzz conventions and includes gtk-doc
formatting for API reference generation.
2026-06-05 15:09:11 -07:00