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.
- 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.ttfextension in the given directory. Subdirectories are not searched.
- 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.
- 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:
- get_custom_font_names() tuple[str, ...]
The names of font families added to pyglet via
add_file().Added in version 3.0.
- have_font(name: str) bool
Check if specified font name is available in the system database or user font database.
- Return type:
- load(
- name: str | Iterable[str] | None = None,
- size: float | None = None,
- weight: str | None = 'normal',
- style: str | None = 'normal',
- stretch: str | None = 'normal',
- dpi: int | None = None,
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 (
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 toWeightfor valid options.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 toStylefor valid options.stretch (
str|None) – If specified a stretch variant is returned, if one exists for the given family and size. Refer toStretchfor 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