Font sizes

When loading a font you must specify the font size it is to be rendered at, in points. Points are a somewhat historical but conventional unit used in both display and print media. There are various conflicting definitions for the actual length of a point, but pyglet uses the PostScript definition: 1 point = 1/72 inches.

Font resolution

The actual rendered size of the font on screen depends on the display resolution. pyglet uses a default DPI of 96 on all operating systems. Most Mac OS X applications use a DPI of 72, so the font sizes will not match up on that operating system. However, application developers can be assured that font sizes remain consistent in pyglet across platforms.

The DPI can be specified directly in the pyglet.font.load function, and as an argument to the TextLayout constructor.

Determining font size

Once a font is loaded at a particular size, you can query its pixel size with the attributes:

Font.ascent
Font.descent

These measurements are shown in the diagram below.

font_metrics.png

Font metrics. Note that the descent is usually negative as it descends below the baseline.

You can calculate the distance between successive lines of text as:

ascent - descent + leading

where leading is the number of pixels to insert between each line of text.