Commit Graph
266 Commits
Author SHA1 Message Date
Behdad EsfahbodandClaude Opus 4.8 aad69a017b [instancer] Walk MVAR value records at the source valueRecordSize
MVAR::subset() copied the source valueRecordSize into the output
header but then walked the source array with a typed
VariationValueRecord pointer (8-byte stride) and embedded 8 bytes per
record. For a font whose MVAR uses value records longer than 8 bytes
(valueRecordSize > 8, which the spec and MVAR::sanitize() allow), every
record after the first was read from the wrong offset and the emitted
header described a record array that overlaps the table's own
ItemVariationStore, silently losing metric variations on partial
instancing.

Step through the source records with StructAtOffset at the table's own
valueRecordSize stride, the same way sanitize() and get_var() do, and
declare VariationValueRecord::static_size in the emitted header so it
always matches the 8-byte records that are written. Output for the
usual 8-byte fonts is byte-identical.

Adds a subset regression test (mvar_partial_instance_record_size) built
from NotoSans-VF.abc with its MVAR records re-padded to 12 bytes; its
expected instanced output is byte-identical to the 8-byte
mvar_partial_instance wght=300:600 file and it fails on the unpatched
tree.

Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-EJUT4BZL.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-08 14:33:10 -06:00
Behdad Esfahbod 2b1e11ee50 [subset] Use serialized contour coordinates for IUP optimization (#6547)
A shifted default can leave fractional contour coordinates that round to
identical positions in glyf. Selecting IUP references against those
fractions can omit deltas that no longer interpolate correctly after
serialization. The reported four-point case loses 499 units.

Round the real contour points after merging default deltas and before
selecting IUP references. The gvar caller already uses a private point
copy, so the plan's coordinates and metric calculations stay intact.
Leave the four phantom points unrounded.

Add a tuple-store regression covering both axes and signs, and regenerate
six affected subset expectations. The reported fixture now reconstructs
exactly. All nine checked optimized fixtures stay within the existing
0.5-unit tolerance against optimization-disabled output, with unchanged
non-gvar tables apart from the head checksum.

Testing: meson test -C build (276 passed).

Fixes: https://github.com/harfbuzz/harfbuzz/issues/6462
Reported-by: olsklcode
Assisted-by: OpenAI Codex
2026-11-01 19:29:45 -07:00
Behdad Esfahbod be6b6ab2b5 [subset] Use outline bounds for instanced CFF2 metrics (#6469)
The bounds vectors used to rebuild hhea and vhea expect non-negative
widths and heights, with a sentinel for glyphs without outlines.
CFF2 instancing stored negative extents.height values and replaced the
sentinel for empty glyphs. Store yMax - yMin and preserve empty bounds.

Regenerate the three affected expected fonts through the subset test
generator. Only header metrics and checksums change. The new vhea
values agree with FontTools recalculation from the glyph outlines.
Both affected test cases fail without the source fix; the full Meson
suite passes with it (293 tests).

Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-3LQ6CVVN.

Assisted-by: OpenAI Codex
2026-10-29 19:07:27 -06:00
Behdad Esfahbod d58303f500 [subset] Choose the smallest CFF charset format (#6456)
Split long CFF charset runs into format 1 chunks and compare the
resulting size with formats 0 and 2. Previously, any run longer than
256 glyphs forced all ranges to format 2.

This reduces the CFF table for NotoSansSC-Thin.otf by 13,938 bytes
without changing decoded table contents.

Assisted-by: OpenAI Codex
2026-10-27 14:39:02 -06:00
Behdad Esfahbod de3011962e [subset] Round CFF2 instances on absolute coordinates (#6427)
Rounding relative charstring operands independently accumulates errors,
opening contours and displacing stem hints after instancing. Track
absolute outline coordinates and stem edges, round their defaults and
surviving variation deltas, then emit relative operands again.

Preserve compact path operators. Conservatively retain relative rounding
for every glyph containing flex1, avoiding variation-dependent direction
analysis. Leave charstrings without blends unchanged.

Add a generated regression font covering closed contours, compact curves,
flexes, masks, and fractional static operands. Check full and partial
instances, contour closure, point accuracy, and the flex1 fallback. Refresh
the existing serialized CFF expectations.

Testing: the complete Meson suite passes (294 passed, one skipped), as do
the CFF2 API tests under ASan and UBSan.

See-also: https://github.com/fonttools/fonttools/pull/4338
Assisted-by: OpenAI Codex
2026-10-22 07:01:35 -06:00
Behdad Esfahbod 144235cf10 [instancer] Switch delta calculations back to floats (#6424)
The scalar cache stores floats, so computing uncached tuple scalars and
instancer deltas as doubles made results depend on whether the cache was
used. Restore single-precision calculations throughout the instancer and
update the affected CFF2 partial-instancing fixtures.

The current glyf partial-instancing comparison still matches fontTools,
so it does not need a new no_fonttools opt-out.

Fixes: https://github.com/harfbuzz/harfbuzz/issues/6423
Assisted-by: OpenAI Codex <codex@openai.com>
2026-10-20 18:13:50 -06:00
Behdad Esfahbod 173eeb59a3 [subset] Allow unchanged VARC axis range requests (#6371)
Assisted-by: Codex
2026-10-11 00:38:36 -06:00
Behdad Esfahbodandartemkulyk bc1e757db1 [subset/CBDT] Share strike data without losing bitmaps on OOM (#6368)
Identical CBLC subtables can share exact image blocks instead of
copying the same CBDT bytes for every strike. Carry forward the sharing
implementation and expected font outputs from #6287.

A cache allocation failure previously discarded a strike without
setting a serializer error, so subsetting could succeed with no bitmap
strikes. Treat cache insertion as best-effort and stop further insertions
after a cache error. Propagate failures in the required temporary image
and offset buffers to the serializer instead of silently dropping data.

Add a regression that fails all three cache allocations in turn and
compares both strikes and every image byte with uncached output. It also
checks required allocation failures cannot silently discard a strike.
The cache regression fails against the original PR and passes here.

The full configured Meson suite passed: 289 tests, including all 224
CBDT subset cases. MSVC and native 32-bit builds were not run locally.


Assisted-by: OpenAI Codex

Co-authored-by: artemkulyk <artem.kulyk@gmail.com>
2026-10-11 00:36:18 -06:00
Behdad Esfahbod d89fa94461 [subset] Preserve distinct feature parameters and variations (#6350)
Matching lookup lists alone cannot establish that same-tag features are
duplicates. Different feature parameters carry distinct UI metadata, and
alternate features can have different behavior in each language system.

Compare complete supported parameter data before merging, and preserve
indices referenced by retained feature variations. Fully pinned fonts can
still merge features after resolving their alternates.

Add GSUB/GPOS regressions with differing cv01 character arrays, size
values, ss01 names, and ss02 alternatives, plus identical ss03 controls.
The full Meson suite passes, as do reduced-feature compilation checks.

Assisted-by: Codex
2026-10-10 17:07:29 -06:00
Behdad Esfahbod fd0c6c9a69 [subset] Preserve parameters and names in alternate features (#6349)
Alternate feature serialization lacks the source feature tag, so it
cannot identify and retain cvXX, ssXX, or size parameters. Name-ID
closure also visits only the original base feature, losing labels
referenced by retained alternates or fully instantiated substitutions.

Carry source tags for retained features after deduplication. Collect
name IDs from the current base feature, retained variation records,
and original features used by catch-all records.

Add ordinary, full, partial, and range regressions for all three
parameter formats in GSUB and GPOS. Ten of twelve regression subtests
fail before the fix. The complete Meson suite passes: 284 tests.
The changed layout units also compile with HB_NO_VAR.

Assisted-by: Codex
2026-10-10 04:12:54 -06:00
Behdad Esfahbod a2ccf95182 [subset] Preserve IFTB offsets when downgrading CFF2 (#6348)
Pass the requested minimum CharStrings offset size to the CFF1 INDEX
serializer, matching the CFF2 path. The conversion previously emitted
short offsets even with HB_SUBSET_FLAGS_IFTB_REQUIREMENTS enabled.

Add a Cantarell downgrade regression, registered with the existing
IFTB tests only when experimental API support is enabled. The original
subsetter fails it with and without preprocessing.

Full Meson suites pass with experimental API enabled (282 tests) and
disabled (263 tests, one skipped).

Assisted-by: Codex
2026-10-10 01:46:09 -06:00
Behdad Esfahbod 3396dcb35b [subset] Keep original IUP contour points across serialization retries (#6345)
IUP adds rebased constant tuples to the plan's cached contour points.
When serialization needs a larger buffer, another gvar serialization
adds the same constants again. The final optimizer then chooses points
against a different outline than the one stored in glyf.

Copy the cached points before IUP instancing so every serialization
attempt starts from the original contour coordinates. Check allocation
failure through the usual vector error path.

Add a shifted-default Roboto Flex regression that fails on the original
subsetter. The entire configured Meson suite passes (278 tests, one
skipped).

Assisted-by: Codex
2026-10-10 01:13:14 -06:00
Behdad Esfahbod be6a74f303 [subset] Preserve line directions when specializing CFF charstrings (#6346)
hlineto and vlineto alternate directions for successive arguments.
Combining commands by their operator alone changes geometry: two
vertical segments become a vertical segment followed by a horizontal
one. Merging a mixed run also needs its argument-count parity.

Require odd parity before combining opposite starting directions and
even parity before combining identical h/v operators.

Test all 1,020 orientation sequences through eight segments at stack
limits 4 and 48, reconstructing every point after specialization. The
original specializer fails that test. Add a shifted Source Serif
instance and update its existing downgrade reference. The entire
configured Meson suite passes (277 tests, one skipped).

Assisted-by: Codex
2026-10-10 01:12:39 -06:00
Behdad Esfahbod 1e6ea99af7 [subset] Preserve deltas when a contour exceeds the IUP set limit (#6344)
The IUP optimizer returns success for contours larger than its 512-bit
sets can represent, leaving every optimized reference false. The
resulting partial instance loses the contour's variation data.

Keep all explicit references when skipping optimization at that limit.
This preserves the existing bounded search without changing geometry.

Add a 600-point contour regression. The original subsetter fails both
normal and preprocessed checks. The entire configured Meson suite
passes (278 tests, one skipped).

Assisted-by: Codex
2026-10-10 00:36:51 -06:00
Behdad Esfahbod 372119f1df [subset] Clamp fully instantiated COLRv1 alpha before narrowing (#6343)
Variation deltas can take solid and gradient stop alpha outside [0, 1].
Narrowing those values to F2DOT14 before clamping wraps alpha 2 to -2
and alpha -3 to 1, reversing the intended opacity.

Clamp full instances in float before storing alpha. Keep partial bases
unclamped because surviving axes can bring them back into range.

Add synthetic solid and gradient regressions for overflow, underflow,
and a partial base of 1.5 reduced to 0.5 by the retained axis. The old
subsetter fails four full-instance checks; the entire configured Meson
suite passes (278 tests, one skipped).

Assisted-by: Codex
2026-10-10 00:30:06 -06:00
artemkulyk 95ba917215 [subset/CPAL] Share color record blocks between identical palettes (#6285)
Palette dedup only compared the original colorRecordIndices start.
After subsetting, palettes that differed just in colors that got
dropped keep equal color sequences at different starts, and each got
its own copy of the color records.

Compare the retained colors and reuse the first matching block.
Palettes may share color records; each still exposes the same colors.

Tested with: meson test -C build

Assisted-by: opencode
2026-10-02 16:59:21 -06:00
Behdad Esfahbod 955770eaa8 [subset] Preserve universal first-match records after axis pinning
A satisfied condition set becomes universal when all its conditions are
dropped, even if no remaining condition requested retention. Mark that
record universal and retain it while other axes remain, preventing
later records and an obsolete default catch-all from changing behavior.

Serialize reduced unconditional records with a null ConditionSet.
Regress GSUB and GPOS together, with an earlier conditional record and
LookupVariationRecord ADD_DEFAULT_LOOKUPS interaction. Regenerate the
existing instance goldens for the unconditional-record representation.

Testing: full Meson suite, 281 passed and one expected skip.

Assisted-by: OpenAI Codex <noreply@openai.com>
2026-10-02 06:20:51 -06:00
artemkulyk 82ec48207b [subset] Drop degenerate VariationDevice records (#6286)
After instancing, GDEF maps all-zero delta rows to
HB_OT_LAYOUT_NO_VARIATIONS_INDEX. copy() still emitted a device with
the out-of-range varidx (0xFFFFFFFF), even though the delta is
already folded into the base value.

Drop the device in that case. gdef_partial_instance is marked
no_fonttools since fonttools keeps the dead device; the
empty_region_vardata and update_def_wght expected outputs are
regenerated.

Tested with: meson test -C build

Assisted-by: opencode
2026-09-29 18:09:44 -06:00
artemkulyk 7cc20b8f70 [subset/cmap] Emit idDelta for zero-delta identity runs (#6284)
serialize_rangeoffset_glyid() treated every zero idDelta as "this
range needs a glyphIdArray", which is what to_ranges() emits for
non-run ranges. A real run with delta 0 (glyph id == codepoint) got
the same treatment and cost two bytes per codepoint.

Only zero deltas are ambiguous. Check those against the mapping and
keep the ones that are genuinely identity as delta ranges.

Adds a subset test with a small hand-built glyf font whose cmap maps
U+0001..U+0007 to gids 1..7.

Tested with: meson test -C build

Assisted-by: opencode
2026-09-29 17:11:17 -06:00
Qunxin Liu bf6414323c [subset] Fix empty ClassDef not intersecting class zero (#6262)
* [subset] Fix empty ClassDefFormat2 not intersecting class zero

An empty ClassDef (rangeCount == 0) assigns class zero to every glyph, but
ClassDefFormat2::intersects_class() and intersected_classes() reported no
intersection when glyphs is not empty.

Adds a subset test that fails before and passes after.

* [subset] Fix empty ClassDefFormat1 not intersecting class zero

Same bug as the previous commit, in the format 1 variant. When glyphCount is
zero and startGlyphID is zero, `startGlyph + classValue.len - 1` underflows
to 0xFFFFFFFF, so neither of the two comparisons can be true and class zero
is never reported. (With a nonzero startGlyphID it happens to work.)

harfbuzz always serializes an empty ClassDef as format 2, so this is only
reachable with fonts produced by other tools.

Extends the previous test with a hand-built format 1 variant of the font.
2026-09-15 20:16:33 -06:00
Qunxin Liu 14e63042fd [subset] bug fix for BASE table
Embed VarStore offset field when table version is 1.1
2026-09-03 14:23:05 -06:00
Behdad EsfahbodandClaude Fable 5 8cb96426e0 [avar] Never downgrade avar2: instance NULL/empty-VarStore fonts on the avar2 path
An avar version 2 table with a NULL varStore offset is legal and maps
like plain v1 — but partial-instancing it used to fail outright, because
the planner errored against the missing store. Rather than falling back
to the v1 instancing path (which rescales every variation store and is
not always bit-exact), keep such fonts on the avar2 path: mapping
entries that don't resolve to a real store row are normalized to
NO_VARIATIONS_INDEX, mirroring runtime get_delta semantics, and offset
compensation creates the needed delta rows on demand in an empty store.
The instance stays version 2 and bit-exact, and consumers that ignore
the v2 tail still see the renormalized v1 segment maps.

Adds a subset test with a null-VarStore variant of the RobotoDelta test
font; the instancing test fails outright without the fix.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-15 11:48:03 -06:00
Behdad Esfahbod f26e0cd4ed [instancer] Fix HVAR partial instancing for map-less advance mappings (#6070)
* [instancer] Don't dedup VarStore rows when not optimizing

as_item_varstore dropped duplicate delta rows even with optimize=false,
where varidx_map is left empty and callers rely on original
VariationIndex values staying valid.  HVAR/VVAR subsetting uses that
mode when the source table has an implicit (map-less) advance mapping:
any two glyphs sharing identical deltas shifted every following row, so
the synthesized identity map pointed at wrong rows and past the end of
the VarData — advances silently froze or took neighbors' deltas when
partially instancing such fonts (e.g. Source Han fonts).

Fixes https://github.com/harfbuzz/harfbuzz/issues/6068

* [subset] Drop identity advance maps for map-less HVAR/VVAR sources

When the source table uses implicit glyph-id indexing, the subsetter
synthesized an advance DeltaSetIndexMap whose content was the identity
over every output glyph — equivalent to the implicit indexing it
replaced, just bigger.  Recognize that case in is_identity () and keep
the output map-less, like fontTools does.  Sources that carry a real
map keep one, preserving their form; truncated maps don't qualify since
glyphs past their end share the last value.

retain-num-glyphs expected files regenerated: NotoSansJP-VF.subset.otf
is map-less, and its redundant identity map is now dropped (verified
against fontTools output).

* [test/subset] Test partial instancing of map-less HVAR

ImplicitHVAR.ttf carries an HVAR with implicit (map-less) advance
indexing and deliberately duplicated delta rows, the combination that
broke before the row-dedup fix: rows shifted or fell off the end of the
rebuilt VarData and advances silently froze.  Expected outputs are
cross-checked against fontTools varLib.instancer + subset.
2026-07-29 11:29:23 -06:00
Behdad Esfahbod 33738aeda9 [subset/cff2] Harden partial instancing per review
- Dissolve private-dict blends of VarDatas unused by any retained
  charstring: under retain-gids, fdmap keeps FDs of hole glyphs the
  usage pass never scanned, and their dicts were rewritten against a
  dead transform.
- Take the partial path even when the store has regions but no
  VarData subtables; the verbatim-copy fallback emitted a region list
  whose axisCount contradicted the subsetted fvar (OTS-rejected).
- Make the usage pass honor drop-hints, so VarDatas referenced only by
  hint blends don't survive as bloat.
- Require an actual instancing request for the pinned path: a face
  without fvar plus a reused subset input made all_axes_pinned
  spuriously true.
- Error-compensate the fold rounding across private-dict operators:
  most blended dict values are delta-encoded, so independently rounded
  folds accumulated in the decoded absolutes (blue zones drifted by
  ~0.5 per element); now every absolute is within 0.5 of exact.
- Clarify HB_SUBSET_FLAGS_DOWNGRADE_CFF2 doc: it applies to fully
  pinned instancing.

Expected test outputs regenerated for the private-dict rounding
change.
2026-07-03 22:53:03 -06:00
Behdad Esfahbod f24bb86c95 [test/subset] Add CFF2 partial-instancing tests
Range limiting, pin-one-keep-one, a moved default, and axis dropping,
on AdobeVFPrototype.  Expected outputs are hb-subset's own (fontTools
TTX comparison does not apply to CFF2 charstring encodings); they were
verified geometrically against both the original font and fontTools
varLib.instancer output at a grid of locations in the kept ranges.
2026-07-03 21:39:55 -06:00
Qunxin Liu ac0979b6f4 [subset] fix bug in math table
Don't keep constructions for glyphs that are only added during math closure,
since we don't do recursive closure over math table.
2026-06-18 16:13:24 -06:00
Behdad Esfahbod 19f54f2cfd Merge pull request #5784 from harfbuzz/downgrade-cff-hintmask
[subset] Fix CFF2->CFF1 specializer hintmask encoding
2026-03-04 12:55:54 -07:00
Qunxin Liu 1c0a3a14d3 [subset] fix padding in gvar table
Add necessary padding if original font is using long format and
subsetted font is using short format
2026-01-20 14:47:54 -08:00
Qunxin Liu cf31e7ac4e [instancer] support full instancing for avar2 (#5676) 2025-12-11 10:54:49 -07:00
Qunxin Liu 0952bc0ac2 [subset] de-duplicate features (#5494) 2025-09-26 12:10:28 -04:00
Garret Rieger 54ab8f0df4 [repacker] make repacker object ids stable.
Prior to this change the object id for a vertex in the repacker graph is it's position in the topological ordering. As a result after each sorting the object ids change. This decouples the topological sorting from the object id assignments. A separate ordering list is retained. This simplifies handling around object ids which no longer needs to account for them shifting, and improves performance by eliminating the step of reassigned link object ids after each sort.
2025-09-15 16:34:44 -06:00
Behdad Esfahbod d6343fa3bf [instancer/varstore] Update test expectations 2025-09-13 13:38:47 -06:00
Garret Rieger aa58b43de4 [subset] Introduce HB_SUBSET_FLAGS_RETAIN_NUM_GLYPHS. (#5547)
Used in conjunction with retain gids, when set the num glyphs from the input font will be preserved. Empty glyphs will be inserted as necessary to maintain the num glyphs value. Put under experimental for now.
2025-09-12 14:24:41 -06:00
Behdad Esfahbod c9480db91b [instancer/varstore] Add a heuristic to speed up optimization 2025-09-12 12:51:26 -06:00
Qunxin Liu 77d073044e [subset] fix HVAR encoding width (#5523) 2025-09-02 15:40:51 -06:00
Khaled Hosny b7c55efe0d [subset] Fix shaping behaviour when pruning empty mark sets
See https://github.com/harfbuzz/harfbuzz/issues/5499
2025-08-26 00:45:26 +03:00
Qunxin Liu cd49b14b4f [subset] add more aggressive pruning for MarkLigPos and MarkMarkPos (#5434)
Remove lig/mark glyphs without defined anchor points after subsetting
2025-07-29 15:32:26 -06:00
Qunxin Liu 879c4ac784 [subset] MarkBasePos: remove base glyphs with empty base record
BaseRecord could become empty if all anchor offsets for retained mark
classes are null
2025-07-24 14:25:54 -07:00
Behdad Esfahbod 8eb2f07cc8 Merge branch 'main' into HVF 2025-05-26 13:23:08 -06:00
Garret Rieger fac64e5838 [instancer] add a test which checks calculation of vmtx tsb during CFF2 instancing. 2025-05-23 12:47:23 -06:00
Behdad Esfahbod a13fd9ae68 Merge branch 'main' into HVF 2025-05-21 12:23:19 -06:00
Garret Rieger 4e76727183 Add a test checks we can recover from cmap4 overflow. 2025-05-15 10:05:18 -06:00
Behdad Esfahbod aef3df5097 Merge branch 'main' into HVF 2025-04-20 00:37:24 -06:00
Garret Rieger efcb7d3de1 [subset] close unicodes over bidi mirror variants during subsetting.
Fixes #5281. Does the closure by default, but I've introduced a new flag and option to disable this behaviour since some users may want to get the minimal set if they know they don't need the mirrored variants.
2025-04-11 18:13:28 -06:00
Behdad Esfahbod 6b2563fff1 Merge branch 'main' into HVF 2025-04-06 15:05:25 -06:00
Behdad Esfahbod e5541a0b63 [subset/test/cmap14] Reduce tested combinations
Fixes https://github.com/harfbuzz/harfbuzz/issues/5254
2025-04-05 17:02:31 -06:00
Behdad Esfahbod d65fa93440 [subset/test/basics.tests] Reduce tested combinations 2025-04-05 16:58:06 -06:00
Behdad Esfahbod 19b4c6e559 [subset] "Fix" subset tests after recent fvar/avar precision changes 2025-03-02 17:00:17 -07:00
Garret Rieger ea1434b897 Split iup glyf partial instance tests into a separate set which ignores due to IUP rounding differences fonttools. 2025-02-25 19:02:11 +00:00
Behdad Esfahbod 9a0cf8d641 [test/subset/cff-japanese] Shrink test
Was taking too long.
2025-02-25 00:19:25 -07:00