pyglet.font

Submodules

Load fonts.

pyglet will automatically load any system-installed fonts. You can add additional fonts (for example, from your program resources) using add_file() or add_directory(). These fonts are then available in the same way as system-installed fonts:

from pyglet import font
font.add_file('action_man.ttf')
action_man = font.load('Action Man', 16)
# or
from pyglet import resource
resource.add_font('action_man.ttf')
action_man = font.load('Action Man')

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

Font names

For portable results, identify a font by its family name and pass weight, style, and stretch separately. A font’s OpenType Full Name identifies a specific face, and legacy platform aliases can combine a family with one or more traits; these names are not consistent across operating systems.

Set pyglet.Options.font_name_compatibility to accept such Full Names and platform compatibility aliases when pyglet needs to map them itself. The option adds compatibility lookup; it does not cause native platform font APIs to reject Full Names when it is disabled. In particular, DirectWrite must enumerate font families to resolve GDI/RBIZ aliases, so enabling it can make first-time font resolution slower on Windows systems with many installed fonts.

class FontGroup(name: str) → None

An ordered set of fallback font families.

Each character is rendered by the first added family that contains it. If no family contains a character, the first family supplies its normal missing-glyph representation.

Added in version 3.0.

add(family: str) → FontGroup

Add a font family to the fallback chain.

Families are checked in the order in which they are added.

Return type:

FontGroup

class FontRangeGroup(name: str) → None

A collection of fonts selected by explicit Unicode ranges.

The first matching range chooses the family. If no range matches, the first added family is used. Unlike FontGroup, this class does not probe subsequent families for missing characters.

Added in version 3.0.

add(
family: str,
start: int | str,
end: int | str,
) → FontRangeGroup

Add a font family responsible for a range of characters.

Return type:

FontRangeGroup

add_directory(directory: str) → None

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

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

Parameters:

directory (str) – Directory that contains font files.

Return type:

None

add_file(font: str | BinaryIO | bytes) → None

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 use a font added with this method, you should pass the face name (not the file name) to :meth:pyglet.font.load() or any other place where you normally specify a font.

Parameters:

font (str | BinaryIO | bytes) – Filename, file-like object, or bytes to load fonts from.

Return type:

None

add_group(font_group: FontGroupBase) → None

Add a font group to pyglet’s list of font groups.

Return type:

None

add_user_font(font: UserDefinedFontBase) → None

Add a custom font created by the user.

A strong reference needs to be applied to the font object, otherwise pyglet may not find the font later.

Parameters:

font (UserDefinedFontBase) – A font class instance defined by user.

Raises:

Exception – If font provided is not derived from UserDefinedFontBase.

Return type:

None

get_custom_font_names() → tuple[str, ...]

The names of font families added to pyglet via add_file().

Added in version 3.0.

Return type:

tuple[str, ...]

have_font(name: str) → bool

Check if specified font name is available in the system database or user font database.

Return type:

bool

load(
name: str | Iterable[str] | None = None,
size: float | None = None,
weight: Weight | str | None = Weight.NORMAL,
style: Style | str | None = Style.NORMAL,
stretch: Stretch | str | None = Stretch.NORMAL,
dpi: int | None = None,
) → Font

Load a font for rendering.

Parameters:
  • name (str | Iterable[str] | None) – 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. The default font will be platform dependent.

  • size (float | None) – Size of the font, in points. The returned font may be an exact match or the closest available.

  • weight (Weight | str | None) – If set, a specific weight variant is returned if one exists for the given font family and size. For example, “bold” can be specified. Refer to Weight for valid options.

  • style (Style | str | None) – If specified, an italic variant can be returned if one exists for the given family and size. If a font is oblique, or italic, either will fallback to choose that variation. Refer to Style for valid options.

  • stretch (Stretch | str | None) – If specified a stretch variant is returned, if one exists for the given family and size. Refer to Stretch for valid options.

  • dpi (int | None) – int The assumed resolution of the display device, for the purposes of determining the pixel size of the font. Defaults to 96.

Return type:

Font