Font

class pygamelib.gfx.core.Font(font_name: str = None, search_directories: list = 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, search_directories: list = None) None
Parameters:
  • font_name (str) – The name of the font to load upon object construction.

  • search_directories (list) – A list of directories to search for the font. The items of the list are strings representing a relative or absolute path.

Important

The search directories must contain a “fonts” directory, that itself contains the font at the correct format.

Note

Version 1.3.0 comes with a pygamelib specific font called 8bits. It also comes with a handfull of fonts imported from the figlet fonts. Please go to http://www.figlet.org/ for more information.

The conversion script will be made available in the Pygamelib Github organization (https://github.com/pygamelib ).

Example:

myfont = Font("8bits")

Methods

__init__([font_name, search_directories])

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.

load([font_name])

Load a font by name.

Attributes

colorable

Returns the "colorability" of the font as specified in the font config file.

glyphs_map

Returns the glyph map of the font as specified in the font config file.

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).

monospace

Returns if the font is monospace as specified in the font config file.

name

Return the name of the font.

scalable

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

vertical_spacing

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

property colorable: bool

Returns the “colorability” of the font as specified in the font config file.

Return type:

bool

glyph(glyph_name: str = None, fg_color: Color = None, bg_color: Color = None) 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

Returns:

A glyphe as a Sprite

Return type:

Sprite

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)
property glyphs_map: dict

Returns the glyph map of the font as specified in the font config file.

Return type:

dict

property 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)
property 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).
property monospace: bool

Returns if the font is monospace as specified in the font config file.

Return type:

bool

property 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!")
property scalable: bool

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

Return type:

bool

property 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
)