pyglet.font
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.
pyglet.font.user
This module defines the usage and creation of user defined fonts in Pyglet.
Previously, pyglet only supported font renderers that are built into the operating system, such as
FreeType, DirectWrite, or Quartz. However, there are situations in which a user may not want or need all the
features a font can provide. They just need to put characters in a particular order without the hassle of exporting
into a separate file.
The UserDefinedMappingFont is provided for most use cases, which will allow you to
make an internal font that can be used where a font_name is required to identify a font.
A user defined font is also identified by its name. The name you choose should be unique to ensure it will not conflict with a system font. For example, do not use Arial, as that will collide with Windows systems.
With UserDefinedMappingFont you can pass a mapping of characters that point to your
ImageData.
mappings={'c': my_image_data, 'b': my_image_data, 'a': my_image_data}
For more custom behavior, a dict-like object can be used, such as a class.
class MyCustomMapping:
def get(self, char: str) -> ImageData | None:
# return ImageData if a character is found.
# return None if no character is found
mappings = MyCustomMapping()
- Once your font is created, you also must register it within pyglet to use it. This can be done through the
add_user_font()function.
When you register a user defined font, only those parameters will used to identify the font. If you have a font, but
want to have a italic enabled version. You must make a new instance of your font, but with the italic
parameter set as True. Same applies to the size parameter. The weight parameter can also be provided as
a string.
Scaling
By default, user font’s will not be scaled. In most use cases, you have a single font at a specific size that you want to use.
There are cases where a user may want to scale their font to be used at any size. We provide the following function:
get_scaled_user_font(). By providing the user defined font instance, and a new size, you will
get back a new font instance that is scaled to the new size. This new instance must also be registered the same way as
the base font.
When specifying the size parameter, that value is used to determine the ratio of scaling between the new size. So
if your base font is a size of 12, creating a scaled version at size 24 will be double the size of the base.
Warning
The PIL library is a required dependency to use the scaling functionality.
Added in version 2.0.15.
- exception UserDefinedFontException
An exception related to user font creation.
- class UserDefinedFontBase
Used as a base for all user defined fonts.
Added in version 2.0.15.
- glyph_renderer_class
alias of
UserDefinedGlyphRenderer
- property name: str
Return the Family Name of the font as a string.
- class UserDefinedMappingFont
The class allows the creation of user defined fonts from a set of mappings.
Added in version 2.0.15.