Skip to content

Fix chaining context backtrack matching order - #372

Merged
blikblum merged 1 commit into
foliojs:masterfrom
lineto:fix/chain-context-backtrack-order
Oct 23, 2026
Merged

blikblum merged 1 commit into
foliojs:masterfrom
lineto:fix/chain-context-backtrack-order

Conversation

@lehni

@lehni lehni commented May 20, 2026 •

Copy link
Copy Markdown
Contributor

The chaining-context lookup (GSUB type 6, formats 1–3) stores backtrack coverage/class/glyph arrays in reverse buffer order per the OpenType spec: backtrack[0] is the glyph immediately before the input, backtrack[1] is one further back, etc. The current matcher feeds the stored order directly to sequenceMatches(-N, …), which iterates forward — so it ends up comparing backtrack[0] against the position farthest from the input. When all backtrack positions accept the same coverage (the common case) the bug is invisible, but rules with distinct positions silently fail to fire.

The fix reverses the backtrack array before matching, in all three chain-context formats.

Test

Added a shaping test using Noto Sans Syriac Estrangela's calt chain rule where:

  • backtrack[0] accepts a mark glyph (uni0731/uni0734/uni0737)
  • backtrack[1] accepts a positional consonant (uni0712.Init/uni0712.Medi/uni071A.Medi)
  • input is uni0713.Fina, substituted to uni0713.FinaWide

Input ܒܱܓ (U+0712 U+0731 U+0713) joins to [uni0712.Init, uni0731, uni0713.Fina]. Per spec the rule fires and produces uni0713.FinaWide (matching HarfBuzz); without the fix fontkit leaves uni0713.Fina unsubstituted.

@lehni
lehni force-pushed the fix/chain-context-backtrack-order branch 4 times, most recently from 22cd36c to 789c8bd Compare May 20, 2026 10:02
@lehni
lehni force-pushed the fix/chain-context-backtrack-order branch 2 times, most recently from 1377693 to d28d572 Compare October 23, 2026 13:22
- Backtrack arrays are stored in reverse buffer order per OT spec
- Reverse before matching so backtrack[0] aligns with position before input
@lehni
lehni force-pushed the fix/chain-context-backtrack-order branch from d28d572 to 7bad3aa Compare October 23, 2026 15:08
@blikblum
blikblum merged commit 2baedec into foliojs:master Oct 23, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants