Adjust kerning

It's not possible in InDesign to adjust a font's kerning table, which is a shame because that's an area where even the best-designed fonts leave some space for improvement – and these improvements are relatively easy to implement.

Apart from possible shortcomings in a font's kerning table, a limitation of the traditional approach to kerning is that the scope of a font's kerning table is just that font; it's not possible for a font to define kerning between its own characters and characters from another font. This means that nothing can be done about clashes of pairs such as an italic f followed by a roman closing parenthesis and the gaping space between letters such as P, V, and W and a following subscripted letter or digit.

Another, somewhat related, problem is that the left side-bearing of a character can cause it to appear misplaced when it is the first character in a paragraph (a side-bearing is the space on the left and right of a character built into the font). Extreme examples are the Minion Pro italic V and W:

bad kerning of sidebearing

The script handles such cases as well; see 'Kerning' the first character in a paragraph.

The script

The script described here deals with these problems: it allows you to create tables that specify kern values not only for characters in a font, but also for characters set in different font styles, that is, from different fonts. The script collects the names of the typefaces ('font families') used in a document, then reads the user-defined kerning data from a file and applies these data to the document.

Kerning data are stored in a text file. The structure of such a kern data file is simple:

Ws	-60
-T	-100

The first line denotes a font style; the script identifies font styles by the three = symbols. Under this heading follow any number of kern instructions. Each instruction consists of a character pair (Ws and -T in the example) followed by a tab, followed by a kern value: positive numbers for positive kerning (loosening), negative values for tightening the space between two characters. The kern value is applied to the space between the two characters in the pair. (You can type more tabs between the characters and the kern value to line up the kerning values and make the file more readable.) Unicode characters can be entered either as characters (just copy and paste the characters from InDesign) or by their unicode numbers, using the format \x{0158}.

A font style followed by any number of kern instructions makes up a section. You can add more sections to cover other font styles and to deal with characters from different font styles. To kern character pairs in another font style, just list these under what is already in the file:

Ws	-60
-T	-100

T-	-135

The script ignores empty lines, so you can use them freely to make the file more readable.

Note: The names of font styles must be entered exactly as you see them in InDesign's Character panel. If a font family uses a regular font style called 55 Roman, then use that in the kerning file:

===55 Roman
Ws	-80

To adjust the kerning between two characters from different font styles, type the two font styles after each other separated by a tab:

===Italic	Regular
f\) 35

In this example, the kerning between an italic f and a roman ) is set to 35. The order in which you type the names of the font styles is important: turn them around in this example and the script will try to find a roman f followed by an italic ). Note: the closing parenthesis must be entered as \). Other characters that need to be preceded by a backslash are ()[]{}.$^+=?.

Spacing between full-size characters and super-/subscripts is dealt with similarly:

===Regular sub
P1	-80
P2	-80
P3	-80

===Italic super
r1	30
r2	30

Use these words exactly as given here: sub for subscript and super for superscript. Note that any styling for the super- and subscripts is ignored: Regular and Italic in the two examples apply only to P and r, respectively. Here, super and sub match characters to which the normal super- and subscript formatting has been applied. To match characters formatted with OpenType superscript (or superior), use otsuper and otsub. (See below for a full list of keywords.)

Another type of format recognised by the script is the figure style (tabular/proportional oldstyle/lining). For instance, this section fixes the space between 7 and a following en-dash (~=) or dot (\.):

===tabular_lining	Regular
7~=	-20
7\.	-100

The forms of the four figure formats to use in the script are proportional_lining, proportional_oldstyle, tabular_lining, and tabular_oldstyle. Note: Figure formats are handled only in roman (Regular).

Before continuing with more powerful kerning entries, we'll first deal with the names and locations of the kern data files.

The kern data files

Kern data files are text files. They're are best kept in UTF-8 format (as the example, below). The name of the file is constructed around the name of a font family. If the script finds Helvetica in a document, it wants to read kerning data from a file called kern_Helvetica.txt. That's the way to name files: kern_ followed by a font-family name followed by the extension .txt. Capitalisation can be ignored, but spaces, hyphens, underscores, etc. must be used exactly as you see in the font's name in InDesign. As to location, the script looks in the script folder for the data files.

Note: there's no need to have a data file for every font family in a document: the script ignores font families for which it can't find a data file; when it finishes it does display a list of font families that it couldn't find a data file for.

Advanced searches

You can use GREP expressions in the search patterns in a data file. Use the standard InDesign format for these expressions. If you are familiar with InDesign's GREP formalism you needn't read any further.

If you're not familiar with InDesign's GREP, here are a short outline and some simple examples to get you started. As you'll see, you don't need much knowledge of GREP to make your data file much more flexible.

Take this fragment from a data file:

===Regular sub
P1	-80
P2	-80
P3	-80

===Italic super
r1	30
r2	30

This says that in the fontstyle Regular, P and following 1, 2, or 3 should be kerned together a bit; and that italic r and a superscript digit need some more space than what they get by default. In fact, this goes for all digits, and we can use the catch-all \d for that (\d stands for 'any digit'). The fragment can then be reduced to this:

===Regular sub
P\d	-80

===Italic super
r\d	30
Here are some other character classes (or 'wildcards'):

\l (backslash and lower-case L): any lower-case letter
\u any upper-case letter
\w any letter or digit

Another useful feature is the character class. Anything enclosed by square brackets is considered a character class, and this is a flexible and useful tool. Here are some examples:

===Italic	Regular
[rv][:;]	80

===Regular sub
P\d		-80
[VWY][\d\l]	-60

The first line here makes the script look for an italic r or v followed by a roman : or ; – the search pattern in this one line finds four different instances: r:, r;, v:, and v;. The second instruction tells the script to find P followed by a subscripted digit; the third line reads "find V, W, and/or Y followed by a subscripted digit or a subscripted lower-case letter".

If you want to learn more about how to use GREP in InDesign, see GREP in InDesign.

Kerning across spaces

The script sets the kerning between the first and second characters of the strings found in the text. You can make use of that to kern characters in a certain context. For example, to add a bit of space after an upper-case letter when it's followed by a space and a T, use this:

\u T  30

The script finds \u[space]T, and applies the kerning to the insertion point between the capital and the space.

Another example of creating context is the use of a lookbehind. In many fonts, in a name such as A. Wilson, there's too much space between the dot and the W. To set negative kerning between the dot and the W (and similarly shaped letters, such as V), use the following line:

(?<=A)\. [VW]   -50

The script finds dot[space]W only when they're preceded by A, the A is not part of the found string. The period is therefore the first character of the found string, and script adds the kern value after it.

'Kerning' the first character in a paragraph

To kern the first character in a text frame, add the character preceded by a caret:

^V   -160

The caret stands for 'Beginning of a paragraph'. It's not possible to add a kerning value before the first character of a paragraph, but the script gets around that by inserting a discretionary hyphen before the character. Then it sets the kerning value to that hyphen and the first real character:

fixed bad kerning of sidebearing

Kerning series of identical characters

To kern series of of identical characters, such as 111111, you need to use a search string like the following:

1(?=1)	20

If you search for 11, you end up with 1 11 1 11, in other words, every other pair of 1s is ignored. By using the so-called lookahead construction (?= ) you avoid that problem.

For more information on how to use GREP searches, see Grep in InDesign.

For an example of a kern data file and more examples of search criteria, see the file kern_Minion Pro.txt that I use myself. If you have any problem with data files or how to define certain search criteria, drop me a line.


The keywords you can use are of two types:

1. Any font style associated with a typeface can be used in the script. To see which font styles a typeface has, select some text in an InDesign document and look in the Character panel (Ctrl/Cmd+T).

2. These special keywords can be used as outlined above:


Resetting selected kern pairs

You might want to reset certain kern values to metric ('automatic'). For instance, if you used a GREP expression that matched more than you had thought, you might want to undo the effect of that particular expression, refine it, and run the improved version.

To reset an expression, simply repeat it and use M as the kerning value. Suppose you used this line in the data file to reduce the space between lower-case letters and upper-case V and W:

===Regular	Regular
[\l][VW]	-60

You find that this doesn't work well (naturally) because of the ascender letters so you want to undo it. All you need to do is to use this line in your data file:

===Regular	Regular
[\l][VW]	M

Instead of the kern value you use M (m works too). You could include this line in your data file, but as this is a one-off it may be better to create a separate, temporary, data file.

Creating kern data files from manually kerned documents

If you have one or more documents with lots of manually kerned character pairs, you can use the (rewritten) kern_report.jsx script to create kern data files. Those text files are formatted in the way required by the kern script.

Speed enhancement

Peter Ahlstrom provided an interesting tip. Large kerning files and large documents can cause long script running times. This appears to be caused in part by the presence of GREP styles, so disabling them can speed up the script. In addition, justified text appears to process slower than left-aligned text ('ragged').

Version history

28 Feb. 2018: Added support for 'kerning' the first character of a paragraph to neutralise a character's left side-bearing.

10 Mar. 2016: Added a note about the rewritten kern_report script, which now writes text data files from manually kerned documents, ready for use in the kern script.

25 Mar. 2016: Added a note on the spelling of font style names.

24 Jan. 2015: Added a note on kerning across spacing (script is unchanged).

1 Jan. 2014: Added a note that to kern series of identical characters such as 11111, you need to search 1(?=1). Added the option to target a selected story (in addition to the active document or all open documents).

29 Oct. 2011: Fixed bug that sometimes caused confusion among typefaces.

17 Nov. 2010: Fixed bug that prevented the kerning of different styles.

4 Nov. 2010: Added possibility to reset selected kern pairs (proposed by Stephan Möbius; see here).

16 June 2010: Fixed issue with the sample date file (kern_Minion Pro.txt).

24 May 2010: Added support for OpenType superior and inferior.

Useful script? Saved you lots of time?

Consider making a donation. To make a donation, please press the button below. This is Paypal's payment system; you don't need a Paypal account to use it: you can use several types/brands of credit and debit card.

Peter Kahrel's paypal account

Download script

Back to script index

Installing and running scripts

Questions, comments? Get in touch