font - Pygame Documentation

pygame documentation | || Pygame Home || Help Contents || Reference Index || Cdrom || Cursors || Display || Draw || Event || Font || Image || Joystick || Key || Mask || Mixer || Mouse || Movie || Music || Overlay || Pixelarray || Pygame || Rect || Scrap || Sndarray || Sprite || Surface || Surfarray || Time || Transform

pygame.font

pygame module for loading and rendering fonts

pygame.font.init - initialize the font module | initialize the font module
pygame.font.quit - uninitialize the font module | uninitialize the font module
pygame.font.get_init - true if the font module is initialized | true if the font module is initialized
pygame.font.get_default_font - get the filename of the default font | get the filename of the default font
pygame.font.get_fonts - get all available fonts | get all available fonts
pygame.font.match_font - find a specific font on the system | find a specific font on the system
pygame.font.SysFont - create a Font object from the system fonts | create a Font object from the system fonts
pygame.font.Font - create a new Font object from a file | create a new Font object from a file

The font module allows for rendering TrueType fonts into a new Surface object. This module is optional and requires SDL_ttf as a dependency. You should test that `pygame.font` is available and initialized before attempting to use the module.

Most of the work done with fonts are done by using the actual Font objects. The module by itself only has routines to initialize the module and create Font objects with `pygame.font.Font - create a new Font object from a file`.

You can load fonts from the system by using the `pygame.font.SysFont - create a Font object from the system fonts` function. There are a few other functions to help lookup the system fonts.

Pygame comes with a builtin default font. This can always be accessed by passing None as the font name.

pygame.font.init

initialize the font module
`pygame.font.init(): return None`

This method is called automatically by `pygame.init - initialize all imported pygame modules`. It initializes the font module. The module must be initialized before any other functions will work.

It is safe to call this function more than once.

pygame.font.quit

uninitialize the font module
`pygame.font.quit(): return None`

Manually uninitialize SDL_ttf's font system. This is called automatically by `pygame.quit - uninitialize all pygame modules`.

It is safe to call this function even if font is currently not initialized.

pygame.font.get_init

true if the font module is initialized
`pygame.font.get_init(): return bool`

Test if the font module is initialized or not.

pygame.font.get_default_font

get the filename of the default font
`pygame.font.get_default_font(): return string`

Return the filename of the system font. This is not the full path to the file. This file can usually be found in the same directory as the font module, but it can also be bundled in separate archives.

pygame.font.get_fonts

get all available fonts
`pygame.font.get_fonts(): return list of strings`

Returns a list of all the fonts available on the system. The names of the fonts will be set to lowercase with all spaces and punctuation removed. This works on most systems, but some will return an empty list if they cannot find fonts.

pygame.font.match_font

find a specific font on the system
`pygame.font.match_font(name, bold=False, italic=False): return path`

Returns the full path to a font file on the system. If bold or italic are set to true, this will attempt to find the correct family of font.

The font name can actually be a comma separated list of font names to try. If none of the given names are found, None is returned.

Example:

        print pygame.font.match_font('bitstreamverasans')
        # output is: /usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf
        # (but only if you have Vera on your system)

pygame.font.SysFont

create a Font object from the system fonts
`pygame.font.SysFont(name, size, bold=False, italic=False): return Font`

Return a new Font object that is loaded from the system fonts. The font will match the requested bold and italic flags. If a suitable system font is not found this will fallback on loading the default pygame font. The font name can be a comma separated list of font names to look for.

pygame.font.Font

create a new Font object from a file
`pygame.font.Font(filename, size): return Font`
`pygame.font.Font(object, size): return Font`

Font.render - draw text on a new Surface | draw text on a new Surface
Font.size - determine the amount of space needed to render text | determine the amount of space needed to render text
Font.set_underline - control if text is rendered with an underline | control if text is rendered with an underline
Font.get_underline - check if text will be rendered with an underline | check if text will be rendered with an underline
Font.set_bold - enable fake rendering of bold text | enable fake rendering of bold text
Font.get_bold - check if text will be rendered bold | check if text will be rendered bold
Font.set_italic - enable fake rendering of italic text | enable fake rendering of italic text
Font.metrics - Gets the metrics for each character in the pased string. | Gets the metrics for each character in the pased string.
Font.get_italic - check if the text will be rendered italic | check if the text will be rendered italic
Font.get_linesize - get the line space of the font text | get the line space of the font text
Font.get_height - get the height of the font | get the height of the font
Font.get_ascent - get the ascent of the font | get the ascent of the font
Font.get_descent - get the descent of the font | get the descent of the font

Load a new font from a given filename or a python file object. The size is the height of the font in pixels. If the filename is None the Pygame default font will be loaded. If a font cannot be loaded from the arguments given an exception will be raised. Once the font is created the size cannot be changed.

Font objects are mainly used to render text into new Surface objects. The render can emulate bold or italic features, but it is better to load from a font with actual italic or bold glyphs. The rendered text can be regular strings or unicode.

Font.render

draw text on a new Surface
`Font.render(text, antialias, color, background=None): return Surface`

This creates a new Surface with the specified text rendered on it. Pygame provides no way to directly draw text on an existing Surface: instead you must use `Font.render - draw text on a new Surface` to create an image (Surface) of the text, then blit this image onto another Surface.

The text can only be a single line: newline characters are not rendered. The antialias argument is a boolean: if true the characters will have smooth edges. The color argument is the color of the text `[e.g.:` (0,0,255) for blue]. The optional background argument is a color to use for the text background. If no background is passed the area outside the text will be transparent.

The Surface returned will be of the dimensions required to hold the text. (the same as those returned by `Font.size())`. If an empty string is passed for the text, a blank surface will be returned that is one pixel wide and the height of the font.

Depending on the type of background and antialiasing used, this returns different types of Surfaces. For performance reasons, it is good to know what type of image will be used. If antialiasing is not used, the return image will always be an 8bit image with a two color palette. If the background is transparent a colorkey will be set. Antialiased images are rendered to 24-bit `RGB` images. If the background is transparent a pixel alpha will be included.

Optimization: if you know that the final destination for the text (on the screen) will always have a solid background, and the text is antialiased, you can improve performance by specifying the background color. This will cause the resulting image to maintain transparency information by colorkey rather than (much less efficient) alpha values.

If you render '\n' a unknown char will be rendered. Usually a rectangle. Instead you need to handle new lines yourself.

Font rendering is not thread safe: only a single thread can render text any time.

Font.size

determine the amount of space needed to render text
`Font.size(text): return (width, height)`

Returns the dimensions needed to render the text. This can be used to help determine the positioning needed for text before it is rendered. It can also be used for wordwrapping and other layout effects.

Be aware that most fonts use kerning which adjusts the widths for specific letter pairs. For example, the width for "ae" will not always match the width for "a" + "e".

Font.set_underline

control if text is rendered with an underline
`Font.set_underline(bool): return None`

When enabled, all rendered fonts will include an underline. The underline is always one pixel thick, regardless of font size. This can be mixed with the bold and italic modes.

Font.get_underline

check if text will be rendered with an underline
`Font.get_underline(): return bool`

Return True when the font underline is enabled.

Font.set_bold

enable fake rendering of bold text
`Font.set_bold(bool): return None`

Enables the bold rendering of text. This is a fake stretching of the font that doesn't look good on many font types. If possible load the font from a real bold font file. While bold, the font will have a different width than when normal. This can be mixed with the italic and underline modes.

Font.get_bold

check if text will be rendered bold
`Font.get_bold(): return bool`

Return True when the font bold rendering mode is enabled.

Font.set_italic

enable fake rendering of italic text
`Font.set_bold(bool): return None`

Enables fake rendering of italic text. This is a fake skewing of the font that doesn't look good on many font types. If possible load the font from a real italic font file. While italic the font will have a different width than when normal. This can be mixed with the bold and underline modes.

Font.metrics

Gets the metrics for each character in the pased string.
`Font.metrics(text): return list`

The list contains tuples for each character, which contain the minimum `X` offset, the maximum `X` offset, the minimum `Y` offset, the maximum `Y` offset and the advance offset (bearing plus width) of the character. [(minx, maxx, miny, maxy, advance), (minx, maxx, miny, maxy, advance), `...]`

Font.get_italic

check if the text will be rendered italic
`Font.get_italic(): return bool`

Return True when the font italic rendering mode is enabled.

Font.get_linesize

get the line space of the font text
`Font.get_linesize(): return int`

Return the height in pixels for a line of text with the font. When rendering multiple lines of text this is the recommended amount of space between lines.

Font.get_height

get the height of the font
`Font.get_height(): return int`

Return the height in pixels of the actual rendered text. This is the average size for each glyph in the font.

Font.get_ascent

get the ascent of the font
`Font.get_ascent(): return int`

Return the height in pixels for the font ascent. The ascent is the number of pixels from the font baseline to the top of the font.

Font.get_descent

get the descent of the font
`Font.get_descent(): return int`

Return the height in pixels for the font descent. The descent is the number of pixels from the font baseline to the bottom of the font.