How to Build Contextual Chaining Lookups in FontForge

Contextual chaining lookups are advanced OpenType features that trigger glyph substitutions or positioning adjustments only when specific glyphs precede or follow a target character. This guide explains the core mechanics of contextual chaining and provides a clear, step-by-step process for creating and configuring these lookups inside the open-source FontForge font editor.


What Are Contextual Chaining Lookups?

In OpenType typography, a standard substitution replaces a glyph or group of glyphs unconditionally. A contextual chaining lookup (GSUB Lookup Type 6 or GPOS Lookup Type 8) adds conditional rules based on surrounding text.

A contextual chain divides a sequence of characters into three distinct components:

  1. Backtrack: The glyphs that must appear immediately before the target glyph.
  2. Input: The target glyph or glyphs that will actually be transformed or repositioned.
  3. Lookahead: The glyphs that must appear immediately after the target glyph.

Contextual chaining lookups do not execute transformations directly. Instead, when the engine matches the combined Backtrack, Input, and Lookahead sequence, the lookup calls a secondary, standard lookup (such as a Single Substitution) to apply changes strictly to the Input sequence.

Common use cases include:

  • Replacing standard characters with terminal swashes at the end of words.
  • Swapping an 'f' for a short-armed alternate when followed by a tall ascender (e.g., 'b', 'h', or 'k').
  • Managing non-standard ligatures in complex scripts like Arabic or Devanagari.

Step 1: Create the Base Substitution Lookup

Because a contextual lookup only points to other transformations, you must first create the simple lookup that performs the actual replacement.

  1. Open your project in FontForge.
  2. Go to Element > Font Info.
  3. Select Lookups from the left navigation panel, then click the GSUB tab.
  4. Click Add Lookup.
  5. Set the Type to Single Substitution.
  6. Provide a descriptive name for your lookup (e.g., lookup_f_alt_sub).
  7. Click OK, then select the newly created lookup and click Add Subtable.
  8. In the subtable mapping list, map the default glyph to its alternate (for example, map f to f.short).
  9. Click OK to save the subtable.

Step 2: Create the Contextual Chaining Lookup

With the base replacement ready, create the rule that defines when it should trigger.

  1. In the GSUB tab of the Lookups panel, click Add Lookup.
  2. Set the Type to Contextual Chaining.
  3. Choose the OpenType feature tag under which this should operate, typically Contextual Alternates (calt).
  4. Name the lookup (e.g., lookup_contextual_f).
  5. Click OK.

Step 3: Configure the Context Matching Method

  1. Select your new contextual chaining lookup and click Add Subtable.
  2. FontForge will present formatting options for specifying your rules:
    • By Glyphs: Useful for simple, specific rules with individual characters.
    • By Classes: Recommended for rules applying to groups of characters (e.g., vowels, ascenders).
    • By Coverage: An advanced, compact method matching arrays of glyph sets.
  3. For general use, select By Classes or By Glyphs. (The following steps assume By Glyphs for straightforward individual rules).

Step 4: Define the Sequences

In the rule configuration window:

  1. Set Backtrack (Optional): Enter any glyphs that must precede your target. Leave this blank if your rule only depends on what follows the input.
  2. Set Input: Enter the target glyph being acted upon (e.g., f).
  3. Set Lookahead: Enter the glyphs that must follow the target (e.g., b, h, l).

Step 5: Assign the Nested Lookup to the Input

Once the sequence is structured, link the rule to the substitution created in Step 1.

  1. In the Lookup References section of the subtable window, locate the Sequence Index (the position of the target glyph inside your Input list, starting at index 0).
  2. Set the position index corresponding to your target glyph.
  3. Assign your base substitution (lookup_f_alt_sub) to that position index.
  4. Click OK to save the rule, then click OK to exit the Font Info dialog.

Step 6: Test the Implementation

Verify that the contextual substitution triggers accurately without affecting other text:

  1. Open the Metrics window by navigating to Metrics > New Metrics Window.
  2. Type a string containing your sequence (e.g., fb, fh, fl).
  3. Confirm that the input character switches to its specified alternate.
  4. Type an unaffected sequence (e.g., fa, fo) to verify that the alternate does not trigger when the lookahead conditions are not met.