Font

class pygamelib.gfx.core.Font(font_name: str = None)

Bases: object

New in version 1.3.0.

The Font class allow to load and manipulate a pygamelib “font”. A font consist of a sprite collection and a configuration file.

If you want to create your own font, please have a look at the font creation tutorial.

In general the Font class is not used directly but passed to a Text object. The text is then rendered using the font.

For performance consideration, it is advised to load the font once and to reuse the object in multiple text objects.

Glyphs are cached (particularly if you change the colors) so it is always beneficial to reuse a font object.

Example:

myfont = Font("8bits")
# If you print() mytext, it will use the terminal font and print in cyan.
# But if you Sreen.place() it, it will render using the 8bits sprite font.
mytext = Text("Here's a cool text", fg_color = Color(0,255,255), font=myfont)
__init__(font_name: str = None) → None
Parameters:font_name (str) – The name of the font to load upon object construction.

Note

There is only one font coming with the 1.3.0 release: 8bits. More will be added later.

Example:

myfont = Font("8bits")

Methods

__init__(font_name)
param font_name:
 The name of the font to load upon object construction.
glyph(glyph_name, fg_color, bg_color) This method take a glyph name in parameter and returns its representation as a Sprite.
height() Returns the height of the font as specified in the font config file.
horizontal_spacing() Returns the horizontal spacing recommended by the font (as specified in the font config file).
load(font_name) Load a font by name.
name() Return the name of the font.
vertical_spacing() Returns the vertical spacing recommended by the font (as specified in the font config file).
glyph(glyph_name: str = None, fg_color: pygamelib.gfx.core.Color = None, bg_color: pygamelib.gfx.core.Color = None) → pygamelib.gfx.core.Sprite

This method take a glyph name in parameter and returns its representation as a Sprite.

The glyph name is usually the name of a character (like “a”) but it is not mandatory and can be anything. The default glyph (returned when no glyph matches the requested glyph) is called “default” for example.

Parameters:glyph_name (str) – The glyph name

Example:

myfont = Font("8bits")
row = 5
column = 10
for letter in "this is a text":
    glyph = myfont.glyph(letter)
    screen.place(glyph, row, column)
    column += glyph.width + myfont.horizontal_spacing()

# Please note that in real life you would just do this
mytext = Text("this is a text", font=myfont)
screen.place(mytext, row, column)
height() → int

Returns the height of the font as specified in the font config file.

Return type:int

Example:

screen.place(text, last_row + myfont.height(), first_text_column)
horizontal_spacing() → int

Returns the horizontal spacing recommended by the font (as specified in the font config file).

As a user of the font class using the Font class to change the look of some text, you will rarely use that value directly (it is directly used by Text.render_to_buffer()).

If your goal is to use the Font class to do glyph rendering as you see fit, use the horizontal spacing value to place each glyph relatively to the one on its left or right.

Return type:int
load(font_name: str = None) → None

Load a font by name. Once the font is loaded glyphs can be accessed through the glyph() method.

This method is automatically called is the Font constructor is called with a font name.

Parameters:font_name (str) – The name of the font to load upon object construction.

Example:

# The 2 following examples do exactly the same thing.
# Example 1: instantiate and load
myfont = Font()
myfont.load("8bits")
# Example 2: load from instantiation
myfont2 = Font("8bits")
# At that point myfont and myfont2 are exactly the same (and there is no
# good justification to instantiate or load the font twice).
name() → str

Return the name of the font. The name is the string that was used to load the font.

Example:

myfont = Font("8bits")
if myfont.name() != "8bits":
    print("Something very wrong just occurred!")
vertical_spacing() → int

Returns the vertical spacing recommended by the font (as specified in the font config file).

Return type:int

Example:

screen.place(
    text,
    last_row + myfont.height() + myfont.vertical_spacing(),
    first_text_column
)