Lookup Type 6 — Chaining Contextual Substitution
Chaining Contextual Substitution is a GSUB rule that replaces marked (') glyphs only when the surrounding context matches.
sub e' t' c by ampersand;Rules are written inside a feature block. Used in the clig feature, for example:
feature clig { sub e' t' c by ampersand;} clig;Syntax
Section titled “Syntax”sub <backtrack sequence> <marked input sequence> <lookahead sequence> by <replacement>;<backtrack sequence>— the sequence of glyphs required before the input sequence. Can be omitted.<marked input sequence>— the sequence of glyphs being substituted. Each glyph name or glyph class name is marked with'.<lookahead sequence>— the sequence of glyphs required after the input sequence. Can be omitted.<replacement>— the single glyph or glyph class that replaces the input sequence.
The rule applies only when the backtrack sequence, input sequence, and lookahead sequence all match together. All three sequences are written in the order they occur in the text. The specification notes that font tool developers often confuse this syntax, because the binary GSUB stores the backtrack sequence in reverse order.
Only glyphs in the input sequence can be substituted. Glyphs in the backtrack sequence and lookahead sequence cannot be replaced.
You can write the replacement directly with by only when there is a single substitution operation, and it is either a Single Substitution or a Ligature Substitution. Which one it is depends on the number of marked glyphs and glyph classes. Any other substitution must be written by referencing a lookup.
# one mark → Single Substitutionsub [A-Z] [a.sc-z.sc]' by [a-z];# two or more marks → Ligature Substitution treating the whole input sequence as one componentsub e' t' c by ampersand;Shorthand
Section titled “Shorthand”sub is the short form of substitute. These two rules are the same:
substitute e' t' c by ampersand;sub e' t' c by ampersand;Referencing lookups
Section titled “Referencing lookups”Instead of writing a replacement, a glyph or glyph class in the input sequence can specify the lookup to apply at that position, with lookup <name>. Each position can specify a different lookup, and substitutions of different types can be mixed. When the referenced lookup has multiple rules, the rule that matches the context at that position is applied.
lookup CNTXT_LIGS { sub f i by fi; sub c t by c_t;} CNTXT_LIGS;
feature calt { sub [a e i o u] f' lookup CNTXT_LIGS i'; sub [a e i o u] c' lookup CNTXT_LIGS t';} calt;In this example, only f i and c t after a vowel become ligatures.
Specifying exceptions (ignore sub)
Section titled “Specifying exceptions (ignore sub)”An exception to a rule is expressed by placing an ignore sub statement before the rule, within the same lookup.
ignore sub <backtrack sequence> <marked input sequence> <lookahead sequence>;A matching sequence is not substituted, and at that position, later rules are also skipped. The backtrack sequence and lookahead sequence can be omitted, but at least one marked glyph or glyph class is required. Mark the position where the substitution would occur in the rule you want to except. Multiple sequences can be combined into a single statement, separated by commas.
Try it
Section titled “Try it”A ligature formed only in context (clig)
Section titled “A ligature formed only in context (clig)”A rule from the Feature File specification’s example, with glyphs not in this font removed. e and t are replaced with an ampersand only when followed by c.
A ligature formed only at the start of a word (clig)
Section titled “A ligature formed only at the start of a word (clig)”The form the specification gives as an example of detecting a word boundary. Marking sequences with a preceding letter (@LETTER) as an exception with ignore sub makes the rule apply only at the start of a word.
Features that use this lookup type
Section titled “Features that use this lookup type”The OpenType specification recommends implementing the following features with this lookup type.
calt— Contextual Alternates. In scripts such as cursive scripts designed to have glyphs connect, replaces a glyph with a form that connects better, depending on context.clig— Contextual Ligatures. Forms ligatures recommended only in specific contexts.cswh— Contextual Swash. Replaces the default form with a swash form, depending on context.rclt— Required Contextual Alternates. Contextual substitutions similar tocalt, but required for correct layout and not meant to be turned off.ordn— Ordinals. Replaces the letters following a figure with forms designed for ordinals.