Contextual Kerning with GPOS in FontForge

Contextual kerning allows you to adjust the spacing between characters based on surrounding glyphs rather than relying strictly on two-character pairs. In FontForge, this behavior is implemented using OpenType GPOS (Glyph Positioning) tables through Contextual Chaining lookups. This guide details how to configure the necessary base positioning lookups, build contextual chaining rules using backtrack, input, and lookahead sequences, and test the resulting metrics inside FontForge.

Understanding the Structure of Contextual Kerning

In OpenType, contextual lookups do not shift glyphs directly. Instead, a contextual rule identifies a specific sequence of characters and calls a secondary, standard positioning lookup to adjust the target glyph.

Implementing contextual kerning in FontForge requires two components:

  1. A nested positioning subtable: A standard Single Positioning or Pair Positioning lookup that defines the actual value of the metric adjustment.
  2. A contextual chaining subtable: The master rule that checks the environment (preceding, current, and succeeding glyphs) and applies the nested positioning subtable when a match occurs.

Step 1: Create the Nested Positioning Lookup

Before defining the context, you must create the lookup that applies the physical spacing offset.

  1. Open your font in FontForge.
  2. Navigate to Element > Font Info.
  3. In the sidebar, select Lookups, then click the GPOS tab at the top.
  4. Click Add Lookup.
  5. Set the Type to Pair Position (kerning) or Single Position.
  6. Set the Feature to none, or leave it blank, because this lookup will be triggered exclusively by the contextual rule, not directly by the layout engine.
  7. Click OK, select the newly created lookup, and click Add Subtable.
  8. Populate this subtable with the specific adjustment. For instance, define an adjustment for the target glyph (e.g., shifting the letter L to the left by -50 units).

Step 2: Create the Contextual Chaining Lookup

Once the adjustment lookup exists, configure the parent rule that defines the trigger conditions.

  1. Still inside Font Info > Lookups > GPOS, click Add Lookup.
  2. Set the Type to Contextual Chaining.
  3. In the Feature column, select or type kern (Kerning).
  4. Assign a descriptive name to the lookup.
  5. Click OK.
  6. Select the new contextual lookup and click Add Subtable.

Step 3: Define Context Classes or Glyph Lists

FontForge will present the Contextual Chaining dialog. You can write rules using glyph lists, glyph classes, or coverage tables. For specific character chains, glyph lists are the most straightforward.

A contextual sequence contains three areas:

  • Backtrack: The glyphs that must appear immediately before the target glyphs.
  • Match (Input): The glyphs that are actually being evaluated and modified.
  • Lookahead: The glyphs that must appear immediately after the target glyphs.

To set up the rule:

  1. Choose By Glyphs or By Classes (use By Glyphs for simple individual character matches).
  2. Enter the glyph names into the respective fields:
    • Backtrack: Enter any preceding characters (e.g., quotedblleft).
    • Match: Enter the active character being repositioned (e.g., A).
    • Lookahead: Enter any trailing characters (e.g., T).
  3. Under the Sequence Position section, specify which glyph in the "Match" sequence receives the adjustment. Sequence position 0 corresponds to the first glyph in the Match list.
  4. Assign the nested lookup you created in Step 1 to that position.
  5. Click OK to save the subtable.

Step 4: Validate and Test the Contextual Rules

After saving the lookups, test the behavior directly within FontForge to ensure the rules fire correctly without conflicts.

  1. Navigate to Metrics > New Metrics Window.
  2. Type the character sequence matching your rule (for example, the combination of your backtrack, match, and lookahead characters).
  3. Ensure that the Features panel in the Metrics Window has the kern feature enabled.
  4. Verify that the target glyph moves according to the defined nested lookup when the full sequence is typed, and remains in its standard position when adjacent characters differ.
  5. Go to File > Generate Fonts and compile the font as an OpenType font (.otf or .ttf) to preserve the GPOS table structures.