32 TUGboat, Volume 30 (2009), No. 1

A closer look at TrueType and pdfTEX • the contains correct names for most , and no names or wrong names for a few glyphs. H`anThˆe´ Th`anh This happens from time to time. Abstract One may wonder how the situation can be so Explanations and examples of using TrueType fonts complex with names in TrueType and still get anything typeset correctly. The reason is that directly with pdfTEX, especially the complications regarding encodings and glyph names. Type 1 fonts rely on correct names to work properly. Thus, if a glyph has a wrong name, it gets noticed immediately. In contrast, as mentioned before, True- 1 Glyph identity in Type 1 vs. TrueType Type does not use names for encoding. So, if glyph The most common outline font format for TEX is names in a TrueType font are wrong or missing, it is Type 1. The TrueType format is rather different usually not a big deal and can easily go unnoticed. from Type 1, and getting it right requires some extra The potential problem with using TrueType in work. In particular, it is important to understand pdfTEX is that we TEX users are accustomed to the how TrueType handles encoding and glyph names Type 1 encoding convention, which relies on correct (or more precisely, glyph identity). glyph names. Furthermore, most font tools rely on We start with Type 1, since most TEX users are this convention and all encoding files (.enc files) use more familiar with it. In the Type 1 format glyphs glyph names. But, as explained above, glyph names are referred to by names (such as ‘/A’, ‘/comma’, and in TrueType are not reliable. If we encounter a font so on). Each glyph is identified by its name; so, that does not have correct names for its glyphs, we given a glyph name, it is easy to tell whether or not need to do some more work. a Type 1 font contains that glyph. Encoding with If glyph names are not correct, we need another Type 1 is therefore simple: for each number n in the way to refer to a glyph in TrueType fonts. The range 0 to 255, an encoding tells us the name of the most reliable way seems to be via : usable glyph that should be used to render (or display) the TrueType fonts must provide a correct mapping from charcode n. Unicode value to glyph index. With TrueType the situation is not that sim- Since version 1.21a pdfTEX has supported the ple. TrueType does not use names to refer to glyphs, naming convention ‘uniXXXX’ in encoding (.enc) but rather so-called “indices”: each glyph is iden- files. This makes sense only with TrueType fonts. tified by an index, not a name. These indices are When pdfTEX sees for example ‘/uni12AB’, it simply numbers that differ from font to font. The • reads the hunicodei→hglyph-indexi table from TrueType format handles encodings by a mechanism the font, and called “cmap”, which (roughly) consists of tables mapping from character codes to glyph indices. A • looks up the value ‘12AB’ in the table, and if TrueType font can contain one or more such tables, found then uses the relevant glyph index. each corresponding to an encoding. The ttf2afm utility does the same lookup when it sees names like ‘uni12AB’. 2 Glyph names vs. Unicode in TrueType 3 Using TrueType in pdfTEX Because glyph names are not strictly necessary for Let’s review the minimal steps to get a TrueType TrueType, they are not always available inside a font working with pdfTEX: TrueType font. Given a TrueType font, one of the following cases may arise. • Generate an afm from the TrueType font using ttf2afm. Example: • The font contains correct names for all glyphs. ttf2afm -e 8r.enc -o times.afm times.ttf This is the ideal situation and is indeed often the case for high-quality Latin fonts. • Convert afm to tfm using any suitable tool — afm2tfm, fontinst, afm2pl, etc. Example: • The font contains wrong names for all or most afm2tfm times.afm -T 8r.enc of its glyphs. This is the worst situation that • Define the needed map entry for the font. Ex- often happens with poor-quality fonts, or fonts ample: converted from other formats. \pdfmapline{% • The font contains no glyph names at all. Newer +times TimesRoman <8r.enc

H`anThˆe´ Th`anh TUGboat, Volume 30 (2009), No. 1 33

(The font name ‘TimesRoman’ used in the map line can be more than one Unicode value mapping is declared inside the times.ttf file.) to a given glyph index. That is, given a glyph The above deals with the easiest case: when index, there may be no corresponding Unicode glyph names are correct. value, or there may be more than one. If there Now let us consider a font where we cannot rely is none, the glyph index will be used (‘index123’, on glyph names: Palatino version 1.40 from Linotype, for example). Now suppose that there are more for example. Let us assume that we want to use the than one, as in the example above, where 0162 T1 encoding with this font. So we put pala.ttf and and 021A are both mapped to glyph index 108 ec.enc in the current directory before proceeding In sum, we have asked ttf2afm to print glyphs further. by Unicode, and ttf2afm cannot know for sure First attempt: which value to use. Hence it outputs the first ttf2afm -e ec.enc -o pala.afm pala.ttf Unicode value and issues the warning. However, since the names in ec.enc are not If all we want to do is to use pala.ttf with the available in pala.ttf — in fact there are no names T1 encoding, probably the easiest way is to create a inside this font — we get a bunch of warnings: new enc file ec-uni.enc from ec.enc, with all glyph Warning: ttf2afm (file pala.ttf): no names names replaced by Unicode values. (This simple ap- available in ‘post’ table ... proach does not handle ligatures; see below.) This Warning: ttf2afm (file pala.ttf): glyph ‘grave’ can be done easily enough by a script that reads the not found AGL (Adobe Glyph List, http://www.adobe.com/ ... devnet/opentype/archives/glyphlist.txt) and and the output pala.afm will contain no names at converts all glyph names to Unicode. all, but instead weird entries like ‘index123’. Fur- Assuming that we have such a ec-uni.enc, the thermore, glyphs are not encoded: steps needed to create the tfm are as follows: C -1 ; WX 832 ; N index10 ; B 24 -3 807 689 ; ttf2afm -u -e ec-uni.enc -o pala-t1.afm pala.ttf afm2pl pala-t1.afm pltotf pala-t1.pl We try again, this time without any encoding: pltotf pala-t1.pl ttf2afm -o pala.afm pala.ttf We could then use the font in pdfTEX as follows: Since this time we did not ask ttf2afm to re- \pdfmapline{+pala-t1

A closer look at TrueType fonts and pdfTEX 34 TUGboat, Volume 30 (2009), No. 1

‘uniXXXX’ names, since that is what the actual glyph name can come from several sources: TrueType font requires. For example, if fontinst • the individual font itself; tells us to add a line saying • a predefined scheme called “the standard Mac- pala-agl-8r <8r.enc

H`anThˆe´ Th`anh