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.

The script

The script described here deals with these problems: it allows you to create tables that describe kern values not only for characters within a font, but also for characters set in different font styles. The script collects the names of the typefaces ('font families') 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:

===Regular
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 make 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's already in the file:

===Regular
Ws	-60
-T	-100


===Italic
T-	-135

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

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 prededed 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.

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. 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 must be used exactly as InDesign shows them.

As to location, the script looks in the script folder for the data files. However, if you prefer to keep the data files somewhere else (more accessible, more easily backed up) you can do so but then you must create a text file with just one line in it. That line is the name of that folder (including the drive if necessary, e.g. /d/kdata or /c/stuff/). The name of this text file must be kern_folder.txt and must be stored in the script folder. So whatever you do, the script must be able to find, in the script folder, either the data files or a file that tells the script where to look for those data files.

Note: there's no need to have a data file for each font family in a document: the script just ignores font families for which it can't find a data file.

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 know 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, a P followed by a 1, 2, or 3 should be kerned together a bit; and that an italic r and a superscript digit need some more space than what they get by default. In fact, this goes for all dgits, 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 (slash and lower-case L): any lower-case letter
\u any upper-case letter
\w any letter or digit

You can also define your own character classes. 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 Y followed by a subscripted digit or a subscripted lower-case letter".

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

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.


Download script

Like the script? A donation will be appreciated.

Questions, comments? Get in touch