Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I calculate PDF leading from TTF?

Tags:

pdf

truetype

The PDF specification defines a text operator called ' (apostrophe). The definition is that it writes out some text, and moves to the next line based on the current leading state. The current leading state can be set using the TL operator. This makes it very easy to write lines of text if you know how much they should be spaced apart vertically:

16 TL
/F1 12 Tf
(Line 1) '
(Line 2) '
(Line 3) '
(Line 4) '

I am not using any libraries for this, as it is a learning exercise. I have written some code to directly parse TTF files and extract character widths, and other items that are required for PDF FontDescriptor dictionaries.

I have been reading the TTF file format specification in order to try to figure out where the line height is located or how it can be calculated, but I have had no luck.

Assuming I have the following givens:

  • The font size.
  • All relevant values from the TTF file (Ascender, Descender, Line Gap, etc.)

How do I calculate PDF leading from TTF?

edit

It appears that the Line Gap value from the TTF file is instrumental in calculating PDF leading, but some fonts do not provide a sensible value for it (Courier New on Windows, for example).

like image 419
Martin Avatar asked May 01 '09 14:05

Martin


1 Answers

By no means am I the least bit knowledgeable about fonts, or have a definite answer, but I figured I'd post some links regarding some information I found regarding the topic.

The Microsoft OpenType Specification (see the Baseline To Baseline Distances section, toward the bottom) defines leading as being equal to the LineGap.

The Apple Text System Overview states that:

Space added during typesetting between lines of type is called leading, after the slugs of lead used for that purpose in traditional metal-type page layout. (Leading is sometimes also called linegap.)

So, according to these links, LineGap is not just used to calculate the leading, but actually is the leading.

like image 96
Tarsier Avatar answered Oct 13 '22 19:10

Tarsier