Font Methods

GD provides some limited support for using different fonts when drawing text on images. Strings may be drawn horizontally or rotated 90 degrees vertically. Because of the way that fonts are implemented, there is currently no support for other angles of text rotation, which would require the use of an external rendering engine such as Ghostscript or the FreeType package. The Unix version of GD also comes with the program bdftogd(written by Jan Pazdziora) that will allow you to convert fonts from the Unix bdf format to a format that can be included with GD on compilation. Other programs are available for converting TrueType and PostScript fonts to bdf format.

The five built-in fonts are imported into your script’s namespace as the global variables gdGiantFont, gdLargeFont, gdMediumBoldFont, gdSmallFont,and gdTinyFont (see Figure 4.4). They are all monospaced fonts with 256 characters in their character sets. The dimensions of each can be determined with the GD::Font::width and GD::Font::height object methods, or by consulting Table 4.2.

Table 4-2. The Dimensions of the Five Standard GD Fonts

Font name

Width (pixels)

Height (pixels)

gdTinyFont

5

8

gdSmallFont

6

12

gdMediumBoldFont

7

13

gdLargeFont

8

16

gdGiantFont

9

15

The fonts available from GD

Figure 4-4. The fonts available from GD

The GD::Fontobject is not currently all that useful; it is implemented primarily as a means for future expansion of the way that GD deals with fonts. However, there are a few methods for getting information about a font, described below.

Get Programming Web Graphics with Perl and GNU Softwar now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.