Package pyglet.font

Load fonts and render text.

This is a fairly-low level interface to text rendering. Obtain a font using load:

from pyglet import font
arial = font.load('Arial', 14, bold=True, italic=False)

pyglet will load any system-installed fonts. You can add additional fonts (for example, from your program resources) using add_file or add_directory.

Obtain a list of Glyph objects for a string of text using the Font object:

text = 'Hello, world!'
glyphs = arial.get_glyphs(text)

The most efficient way to render these glyphs is with a GlyphString:

glyph_string = GlyphString(text, glyphs)
glyph_string.draw()

There are also a variety of methods in both Font and GlyphString to facilitate word-wrapping.

A convenient way to render a string of text is with a Text:

text = Text(font, text)
text.draw()

See the pyglet.font.base module for documentation on the base classes used by this package.

Submodules

pyglet.font.base
Abstract classes used by pyglet.font implementations.

Classes

  GlyphString
An immutable string of glyphs that can be rendered quickly.
  Text
Simple displayable text.

Functions

Font load(name=None, size=None, bold=False, italic=False, dpi=None)
Load a font for rendering.
  add_file(font)
Add a font to pyglet's search path.
  add_directory(dir)
Add a directory of fonts to pyglet's search path.

Variables

  __package__ = 'pyglet.font'

Function Details

load

load(name=None, size=None, bold=False, italic=False, dpi=None)
Load a font for rendering.
Parameters:
name : str, or list of str
Font family, for example, "Times New Roman". If a list of names is provided, the first one matching a known font is used. If no font can be matched to the name(s), a default font is used. In pyglet 1.1, the name may be omitted.
size : float
Size of the font, in points. The returned font may be an exact match or the closest available. In pyglet 1.1, the size may be omitted, and defaults to 12pt.
bold : bool
If True, a bold variant is returned, if one exists for the given family and size.
italic : bool
If True, an italic variant is returned, if one exists for the given family and size.
dpi : float
The assumed resolution of the display device, for the purposes of determining the pixel size of the font. Defaults to 96.
Returns: Font

add_file

add_file(font)

Add a font to pyglet's search path.

In order to load a font that is not installed on the system, you must call this method to tell pyglet that it exists. You can supply either a filename or any file-like object.

The font format is platform-dependent, but is typically a TrueType font file containing a single font face. Note that to load this file after adding it you must specify the face name to load, not the filename.

Parameters:
font : str or file
Filename or file-like object to load fonts from.

add_directory

add_directory(dir)

Add a directory of fonts to pyglet's search path.

This function simply calls add_file for each file with a .ttf extension in the given directory. Subdirectories are not searched.

Parameters:
dir : str
Directory that contains font files.