Fonts in LaTeX, Part Three: pdfTeX and TrueType

Update: The information in this post is out of date: otftotfm does presently have support for TrueType outlines. See my errata post for more information.

In the previous part of this tutorial, I explained how to put together the minimal infrastructure needed to use an OpenType font with pdfLaTeX.  However, I used the tool otftotfm to generate the font metrics TeX needs to lay out text. However, otftotfm only supports OpenType fonts that use PostScript font outlines, as opposed to TrueType font outlines. So in this part of the tutorial I will explain how to put together the necessary infrastructure for TrueType fonts. In preparation for that, we will first make a few changes to what we had done earlier.

For those that would find it useful, I've put together a  zip file containing all the files from the tutorials (except the fonts, which I don't want to deal with distributing).

Firstly, we are going to move the uses of \DeclareUnicodeCharacter out of UPagella.fd and into uenc.def:

  1.  
  2. \ProvidesFile{uenc.def}
  3. % We are declaring an encoding named "U"
  4. \DeclareFontEncoding{U}{}{}
  5.  
  6. % Technically these are not "allowed" in .def files,
  7. % but this is really the logical place to put the
  8. % declarations.
  9.  
  10. % τ (0x03C4) maps to 0xF8 in the encoding
  11. \DeclareUnicodeCharacter{03C4}{\char"F8}
  12. % ε (0x03B5) maps to 0xF9 in the encoding
  13. \DeclareUnicodeCharacter{03B5}{\char"F9}
  14. % χ (0x03C7) maps to 0xFA in the encoding
  15. \DeclareUnicodeCharacter{03C7}{\char"FA}

As I mention in the comments, the documentation on font encoding definition files does not list \DeclareUnicodeCharacter to be one of the allowed declarations in a such a file, but it works, and it seems like the more logical place to configure it than in the font definition file.

Now that we have removed the uses of \DeclareUnicodeCharacter from UPagella.fd, it looks like:

  1.  
  2. \ProvidesFile{UPagella.fd}
  3.  
  4. % Delcaring a font family called "Pagella" for the encoding "U"
  5. \DeclareFontFamily{U}{Pagella}{}
  6.  
  7. % Declare that font family "Pagella", for encoding "U", has a shape
  8. % with weight medium (m) and normal (n) slant (in otherwords, upright)
  9. \DeclareFontShape{U}{Pagella}{m}{n}{
  10. % For all sizes...
  11. <->
  12. % ... use the font named
  13. TeXGyrePagella-Regular--custom--base
  14. }{}

I am going to use Deja Vu Sans as the example TrueType font. Fortunately, if you followed everything from the second part of the tutorial, there is not much that needs to be done.

First, we need to generate metrics for Deja Vu Sans. As before, if you are using TeX Live, you'll have the necessary program:

% ttf2tfm DejaVuSans.ttf -q -T custom
ttf2tfm: WARNING: Cannot find character `compwordmark'
         specified in input encoding.
...
...
ttf2tfm: WARNING: Cannot find character `zdotaccent'
         specified in input encoding.
DejaVuSans   DejaVuSans.ttf Encoding=custom.enc

The program ttf2tfm is kind of unusual in that it first takes the filename argument and then all the options. So we've passed it the TrueType font we want to generate metrics for, DejaVuSans.ttf, the option -q to tell it not to print quite so much information, and the option -T custom which tells it to use the encoding defined in the file custom.enc we created in previous part.

Unlike otftotfm, ttf2tfm does not generate an entry that we could use in our map file, custom.map, so we need to write one ourselves. You will want to start with the map we generated by otftotfm for Tex Gyre Pagella, and you will want to add the line:

DejaVuSans <custom.enc <DejaVuSans.ttf

This says to map the TeX font name DejaVuSans to the file DejaVuSans.ttf using the encoding custom.enc. To learn more about the format of map files, there is a section on them in the pdfTeX manual.

Now we just need to create a font definition file for Deja Vu Sans. However, it is essentially the same as the one we created for TeX Gyre Pagella:

  1.  
  2. \ProvidesFile{UDejaVuSans.fd}
  3.  
  4. % Delcaring a font family called "DejaVuSans" for the encoding "U"
  5. \DeclareFontFamily{U}{DejaVuSans}{}
  6.  
  7. % Declare that font family "DejaVuSans", for encoding "U", has a shape
  8. % with weight medium (m) and normal (n) slant (in otherwords, upright)
  9. \DeclareFontShape{U}{DejaVuSans}{m}{n}{
  10. % For all sizes...
  11. <->
  12. % ... use the font named
  13. DejaVuSans
  14. }{}

We have just replaced all occurrences of Pagella with DejaVuSans.

Finally, we just need to update our example document to use Deja Vu Sans:

  1.  
  2. \documentclass{article}
  3. \usepackage[utf8]{inputenc}
  4. \usepackage[U]{fontenc}
  5. \pdfmapfile{+custom.map}
  6. \renewcommand{\rmdefault}{Pagella}
  7. \renewcommand{\sfdefault}{DejaVuSans}
  8.  
  9. \begin{document}
  10. Testing pdfLaTeX!
  11.  
  12. Greek: τεχ.
  13.  
  14. \begin{sffamily}
  15. Testing pdfLaTeX!
  16.  
  17. Greek: τεχ.
  18. \end{sffamily}
  19. \end{document}

Here we have used \renewcommand to set the default sans serif font, \sfdefault, to be DejaVuSans. In the body of the document, we've copied the text and surrounded it with the sffamily environment to have it typeset in sans serif.

Now we have everything we need to run pdflatex:

% pdflatex test-pdflatex.tex
This is pdfTeXk, Version 3.141592-1.40.3 (Web2C 7.5.6)
 %&-line parsing enabled.
...
...
(./test-pdflatex.aux) (./upagella.fd) (./udejavusans.fd) [1]
(./test-pdflatex.aux) ){custom.enc}{a_qnnnfc.enc}<./TeXGyrePage
lla-Regular.pfb>
Output written on test-pdflatex.pdf (1 page, 34857 bytes).
Transcript written on test-pdflatex.log.

And we have the desired output:

Testing pdfLaTeX with both OpenType and TrueType fonts

And that's everything you need to get started with TrueType fonts and pdfLaTeX. Again, if you encounter any problems or notice any omissions, let me kow. I'll do some investigation and there will possibly be a fourth part on using fontinst.

Comments (3)