Features, part 2: con­textual sub­sti­tutions

by Rainer Erich Scheichelbauer
en fr zh

25 July 2022 Published on 21 July 2012

You want to substitute glyphs, but only under certain circumstances? Contextual substitutions are the way to go.

I’m sure you know how to activate contextual alternates in InDesign. And you might have heard that some fonts do some really cool stuff with it. Here’s how to build that into your font.

The features we need to focus on are calt (Contextual Alternates) and clig (Contextual Ligatures). Both are activated when you choose Contextual Alternates in InDesign.

Create the alternate glyph

So, take one of your fonts and let’s put an alternate glyph in it. The alternative should replace the standard glyph only under certain circumstances. As an example, I’ll pick the j.

I think in this case, the j should retract its terminal a little if it is preceded by a g. So we’ll generate a new glyph by choosing Glyph > Add Glyphs… In the text field that appears, enter j=j.alt and click Generate. This will create a glyph called j.alt with the j as component. You can subsequently decompose the component via the context menu and adjust the terminal.

You can ‘type’ the alternate j by hitting Cmd-F and typing j.alt into the search field that appears.

Write the contextual substitution

Now, we need to write the calt feature. Hit Cmd-I to bring up the Font Info and switch to the Features tab. Click and hold the plus button and create a new feature. After creating the new feature, it is ready to be recalled. We'll call it calt. In the upper text field, you enter:

sub g j' by j.alt;

If everything’s done right, your feature window should look something like this:

Between the sub and the by, there’s the complete context the feature is looking for. In our case, that’s the g, followed by the j. But we just want to replace the j, not the g. So we mark the j with a single straight quote: j'. The substitute after the keyword by, j.alt, will only apply to the marked letters, in this case just j.

Let’s try it out and see if it works. Hit the Compile button, open up a new Edit tab in the Font window, pick calt from the Features menu at the bottom left and type a word that contains ‘gj’.

Add a class to the substitution rule

It’s a good idea that our j pulls back its tail when there’s a g preceding it. But it should also do so when there are other letters with a descender preceding it, like a q, a y, or another j. Of course, we could write something like this into our calt feature:

sub g j' by j.alt;
sub q j' by j.alt;
sub y j' by j.alt;
sub j j' by j.alt;

But that’s cumbersome, especially if you need to change something later. Here’s a more efficient way of doing the same thing:

sub [g j q y] j' by j.alt;

The brackets create a class. It’s a way of saying ‘any of these letters’. So the feature says, ‘replace j by j.alt if any of the letters g, j, q or y precede it’.

You can use classes for the substitution as well. All you have to do is to make sure that both classes have the same size and that the glyphs are in the same order. Imagine we also have a jcircumflex.alt, then our feature could look like this:

sub [g j q y] [j jcircumflex]' by [j.alt jcircumflex.alt];

When you need a class more often, then you do not want to re-type the whole class over and over again. Especially not if you find out that one glyph is missing in the class and you need to change everything. Then it is a better idea to click on the Plus button and create a class definition. You can rename it by double clicking on the xxxx, just like you would rename a feature tag. A class name must start with a letter from A-Z or a-z, and can also contain underscore, period and figures. The name can be longer than four letters. I’ll pick the name descender_on_the_right for our class and add g j q y to it:

Now, we can rewrite our calt feature as follows:

sub @descender_on_the_right [j jcircumflex]' by [j.alt jcircumflex.alt];

The at sign (@) marks a class name. If you forget that, Glyphs will think it’s a glyph name and spit out an error message.

That’s about it. Or, actually, not quite. In the next part, Features, part 3: Contextual substitutions, we’ll dig a little deeper and code advanced contextual features.

SAMPLE FONT: Jordana by Stella Chupik
Update 2015-06-22: updated for Glyphs 2.
Update 2019-03-29: deleted Font menu; it is now the Glyph menu. Corrected typos.
Update 2022-07-21: updated title, related articles, minor formatting.