sdl2.ext.bitmapfont - Basic Bitmap Font Rendering

This module provides the BitmapFont class, which allows for basic font rendering in PySDL2 without depending on the SDL_ttf library.

class sdl2.ext.bitmapfont.BitmapFont(font_img, size=None, mapping=None)[source]

A class for rendering text using a given bitmap font.

This class takes an image of equally-spaced font characters and a font map indicating the location of each character on the image, and uses these to render text using the given font. This class is based on base SDL2 functions and does not require the SDL_ttf library to be installed.

The font mapping table is a list of strings, with each string representing a row of characters on the font image surface. Each character within each line is assumed to be of equal height and width, but this can be adjusted using the remap() method.

For example, the built-in bitmap font font.bmp has the following layout:

../../_images/font.png

The default font mapping table, which matches the layout of font.bmp, looks like this:

fontmap = [
    '0123456789',
    'ABCDEFGHIJ',
    'KLMNOPQRST',
    'UVWXYZ    ',
    'abcdefghij',
    'klmnopqrst',
    'uvwxyz    ',
    ',;.:!?+-()',
 ]
Parameters:
  • font_img (SDL_Surface or str) – A surface or path to a file containing a valid bitmap (.bmp) font image.
  • size (tuple, optional) – A (width, height) tuple defining the size of each character in the bitmap font. If not specified, this will be inferred automatically from the fontmap and font image.
  • fontmap (list, optional) – A list of strings defining the locations of characters in the font image. If not specified, the default font map defined above will be used.
remap(c, x, y, w, h)[source]

Updates the source rectangle for a given font character.

This method can be used to fine-tune the character mappings in the font image to produce better spacing in the rendered text.

Parameters:
  • c (str) – The character to remap in the font image.
  • x (int) – The x coordinate (in pixels) of the top-left corner of the new rectangle for the character.
  • y (int) – The y coordinate (in pixels) of the top-left corner of the new rectangle for the character.
  • w (int) – The width (in pixels) of the new rectangle for the character.
  • h (int) – The height (in pixels) of the new rectangle for the character.
render_text(text, line_h=None, as_argb=True)[source]

Renders a string of text to a new surface.

If a newline character (\n) is encountered in the string, it will be rendered as a line break in the rendered text.

By default, this function also converts the rendered text from the native format of the font image to 32-bit ARGB, for consistency across functions and better compatibility with SDL2 renderers. To disable ARGB conversion, set the as_argb parameter to False.

Parameters:
  • text (str) – The string of text to render.
  • line_h (int, optional) – The line height (in pixels) to use for each line of the rendered text. If not specified, the maximum character height for the font will be used. Defaults to None.
  • as_argb (bool, optional) – Whether the output surface should be converted to 32-bit ARGB pixel format or left as-is. Defaults to True (convert to ARGB).
Returns:

A surface containing the rendered text.

Return type:

SDL_Surface

Raises:
  • ValueError – If a character in the text is not provided by the
  • current font.
render_on(target, text, offset=(0, 0), line_h=None)[source]

Renders a string of text to an existing surface.

If a newline character (\n) is encountered in the string, it will be rendered as a line break in the rendered text.

Parameters:
  • target (SDL_Surface) – The surface on which to render the given string.
  • text (str) – The string of text to render to the target surface.
  • offset (tuple, optional) – The (x, y) coordinates of the target surface on which the top-left corner of the rendered text will be placed. Defaults to (0, 0).
  • line_h (int, optional) – The line height (in pixels) to use for each line of the rendered text. If not specified, the maximum character height for the font will be used. Defaults to None.
Returns:

The (x1, y1, x2, y2) rectangle of the target surface on which the text was rendered.

Return type:

tuple

Raises:
  • ValueError – If a character in the text is not provided by the
  • current font.
contains(c)[source]

Checks whether a given character is mapped within the font.

Parameters:c (str) – The character to check for within the font map.
Returns:True if the font contains the character, otherwise False.
Return type:bool