# How does TeX know how to space different typefaces?

My understanding of how TeX makes things look so nice is that it goes over each paragraph and spaces each letter/word to look 'right' based on how dark it is, how wide it is, and the characteristics of the letters around it.

My question is: How does this work with different typefaces? Each one could have totally different letter sizes and empty space within them and whatnot. Is it based on some trait of the typeface that TeX can determine, such as width? Is it embedded in the fonts file? Or am I giving TeX far too much credit?

I am wondering as I always assumed that this was part of what the packages that I used to activate the font did, but LuaTeX and XeTeX allow you to use TTF and OTF font files, which I'm assuming don't contain anything fancy like this.

-
Actually, OTF fonts can (and often do) contain kerning indications (and even much fancier things). –  ℝaphink Oct 6 '11 at 22:35

The interword space is based on parameters stored in the font file (.tfm). Precisely, each font has at least seven length stored in it

\fontdimen1 is the "slant per point" (expressed in points)
\fontdimen2 is the interword space
\fontdimen3 is the interword stretch
\fontdimen4 is the interword shrink
\fontdimen5 is the x-height
\fontdimen7 is the "extra space"

The relevant parameters are 2, 3 and 4; between two words, the first of which is in font \x TeX inserts glue equivalent to

\hskip \fontdimen1\x plus \fontdimen2\x minus \fontdimen3\x

The "extra space" enters the scene when the current space factor is at least 2000 (see the TeXbook or TeX by Topic for details). It's the font designer's responsibility to set these parameters to suitable values depending on the blackness and the overall size of the font.

It's always possible to override these parameters by giving a non zero value to \spaceskip and \xspaceskip.

XeTeX and LuaTeX convert OTF font parameters into values for the \fontdimen parameters; for example, with Linux Libertine we get

\fontdimen2 2.5pt
\fontdimen3 1.25pt
\fontdimen4 0.83333pt
\fontdimen7 0.83333pt

-
So it works on a per-word basis, not per-character. That makes more sense. So why does e look closer to x than a in 'examination' in my document? Is that built into the typeface? –  Canageek Oct 6 '11 at 22:58
The .tfm file contains also information for intercharacter kerning. Such information about pair of characters is provided also in .otf font files. Also ligatures are defined in the font (for instance, typewriter fonts typically don't have ff ligatures). –  egreg Oct 6 '11 at 23:13