Contextual kerning

by Rainer Erich Scheichelbauer
en fr zh

22 March 2024 Published on 10 April 2013

Glyphs offers a smart way to add your own contextual kerning in a separate OpenType feature. Warning: rather advanced level.

So, you have optimized the sidebearings of your glyphs, then added kerning pairs where necessary. But still, you come across some cases where the white space between the letters still isn’t optimal in certain glyph triplets.

There are some usual suspects. For instance, when a glyph with a negative RSB is followed by a space and a glyph with a negative LSB, e.g. f space T, f space V and f space W. In these cases, the word space gets eaten up by the surrounding negative sidebearings. To a reader, the result may look like a forgotten space. So the white space needs to be increased a bit.

Or take the L quoteright A triplet, where the apostrophe (we assume quoteright will be used for the apostrophe) usually ends up too close to the A and too far away from the L. We need to fix that:

Typical triplets usually include space, punctuation and quotes, like period quoteright space. Also, it’s not necessarily always triplets, it can be quadruplets too, like f period space V etc. Of course, the mileage varies greatly depending on the design. The only way to find out which combinations you need is to have long texts set in your typeface, preferably with lots of punctuation in different languages, and look for spots where the white space seems messed up.

Contextual kerning lookup

Enter the kern feature. Glyphs actually creates it automatically for you at export time, using all the kerning pairs you created. Now, you can add some extra kerning on top of the existing kerning by adding a separate lookup to the kern feature. How? Well add a feature and call it kern:

To do that, go into File > Font Info > Features and add a new feature called kern by clicking on the plus button next to features in the left sidebar of the window. In the code, you start by adding the Automatic Code Placeholder, best through the Snippets menu in the bottom right of the Features window:

This will add the following bit of code:

# Automatic Code

This represents the automatically generated kern feature, derived from whatever you have in Window > Kerning. So, this way, you can tell Glyphs to add the code after or before the automatic code. Typically, you want it afterwards, so this placeholder is the first line of your code.

Now let’s assume we want to fix the triplets containing f space as well as the L quoteright A combo, which is important for languages like French and Italian. Okay, here’s what we put into kern:

# Automatic Code
pos f' 50 space [T V W Y];
pos L' -40 quoteright' 80 [A Aacute Agrave];

The first line looks for an f followed by a word space, but only before T, V, W or Y. Only in that case will the f space pair get an additional white space of 50 units.

The second line looks for an L, followed by an apostrophe and an A, with or without an accent. In that case, the apostrophe is shoved into the L by 40 units, while the space between the apostrophe and the A is increased by 80 units, effectively moving the apostrophe closer to the L, and increasing the space between L and A, preventing them from colliding with each other.

This is called a chaining contextual positioning with a marked subrun. If you’re familiar with AFDKO feature coding, the structure of the positioning rules may surprise you. The kerning values are not placed at the end of the line, but right between the names of the affected glyphs. Also, as with any contextual rule, some glyphs need to be marked with a ‘dumb quote’. In this case, we need to mark every glyph that is followed by a kerning value.

Interpolating kerning values

Ever since Glyphs 3.1.1, you can interpolate numbers in the feature code. The way this works is simple. After any kerning value, you specify additional coordinates in the designspace between parentheses, e.g., (wdth:150), followed by the kerning value at that designspace coordinate. Look at this:

# Automatic Code
pos f' 50 (wdth:150) 95 space [T V W Y];

The above piece of code means that, in the origin master, f will get an extra 50 units of space if it appears before space followed by any of the glyphs in [T V W Y]. In an extended style, or in other words, at designspace coordinate (wdth:150), that same extra space is 95 instead of 50. And the best thing: along the width axis, this value will interpolate for every instance, and even variable fonts. Pretty cool.

Let’s take it a notch further. What if you have multiple axes? Again, easy. You just specify multiple series of designspace coordinates with the kerning value in question. Like this:

# Automatic Code
pos f' 50 (wdth:150) 20 (wdth:150 wght:220) 70 (wdth:100 wght:220) 70 space [T V W Y];

If it makes things easier, you can also reformat like this:

# Automatic Code
pos f' 50 
  (wdth:150) 20
  (wdth:150 wght:220) 70
  (wdth:100 wght:220) 70
space [T V W Y];

It’s the same thing.


Under the hood, Glyphs adds a lookup called kernCustom to the end of the kern feature:

The sequence of marked glyphs, the ‘marked subrun’, must not be interrupted by unmarked glyphs. That means you can have x x' x' x' x but not x' x' x x' x'. Otherwise you get this error: ‘Unsupported contextual GPOS target sequence: only one run of marked glyphs is supported’.

And you cannot mix regular kerning with contextual kerning, because they are two different lookup types. If you do mix them, you will receive this error: ‘Lookup type different from previous rules in this lookup block’.


Since our lookup is feature code that gets injected into every instance at export time, the contextual kerning does not interpolate. But that is not much of a biggie, usually. Remember that the additional contextual kerning does not replace the other kerning pairs you already have, but it gets added on top of them.

Except for regular, non-contextual kerning, Glyphs does not give you GPOS preview in the Edit view. That’s bad luck for us contextual kerners. But there are two easy workarounds:

  1. Open Window > Text Preview, choose an instance and a font size, and type away. Text Preview supports GPOS including mark positioning and all kinds of kerning. Actually, what you see is a true export, rendered by Apple’s CoreText.

  2. Just keep exporting your fonts to the Adobe Fonts folder and you can test your kerning triplets, quadruplets and quintuplets in InDesign right away. After all, this is where it counts because it’s where people are going to use it.


Update 2020-01-08: corrected Font Info menu entry (thx @Galifer).
Update 2020-03-30: corrected typo.
Update 2022-07-25: updated title, related articles, minor formatting.
Update 2023-04-26: added Interpolating kerning values, and the automatic-code placeholder.
Update 2024-03-22: added Text Preview as a preview option.