pygame.freetype
Enhanced Pygame module for loading and rendering computer fonts
pygame.freetype.get_error Return the latest FreeType2 error
pygame.freetype.get_version Return the FreeType 2 version
pygame.freetype.init Initialize the underlying FreeType 2 library.
pygame.freetype.quit Shut down the underlying FreeType 2 library.
pygame.freetype.was_init Return whether the the FreeType 2 library is initialized.
pygame.freetype.get_default_resolution Return the default pixel size in dots per inch
pygame.freetype.set_default_resolution Set the default pixel size in dots per inch for the module
pygame.freetype.get_default_font Get the filename of the default font
pygame.freetype.Font Create a new Font instance from a supported font file.

— Note that some features may change before a formal release

The pygame.freetypeEnhanced Pygame module for loading and rendering computer fonts module allows for the rendering of all font file formats supported by FreeType, namely TTF, Type1, CFF, OpenType, SFNT, PCF, FNT, BDF, PFR and Type42 fonts. It can render any UTF-32 character in a font file.

This module is a replacement for pygame.fontpygame module for loading and rendering fonts. It has all of the functionality of the original, plus many new features. Yet is has absolutely no dependencies on the SDL_ttf library. The pygame.freetypeEnhanced Pygame module for loading and rendering computer fonts module is not itself backward compatible with pygame.fontpygame module for loading and rendering fonts. Instead, a new pygame.ftfont provides a drop-in replacement for pygame.fontpygame module for loading and rendering fonts.

Most of the work done with fonts is done by using the actual Font objects. The module by itself only has routines to initialize itself and create Font objects with pygame.freetype.Font().

You can load fonts from the system by using the pygame.freetype.SysFont() function. There are a few other functions to help find system fonts.

For now undefined character codes are replaced with the undefined character. How undefined codes are handled may become configurable in a future release.

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

New in Pygame 1.9.2

pygame.freetype.get_error()
Return the latest FreeType2 error
get_error() -> str

Return a description of the last error which occurred in the FreeType2 library, or None if no errors have occurred.

pygame.freetype.get_version()
Return the FreeType 2 version
get_version() -> (int, int, int)

Returns the version of the FreeType2 library which was used to build the ‘freetype’ module.

Note that the freetype module depends on the FreeType 2 library. It will not compile with the original FreeType 1.0. Hence, the first element of the tuple will always be “2”.

pygame.freetype.init()
Initialize the underlying FreeType 2 library.
init(cache_size=64, resolution=72) -> None

This function initializes the underlying FreeType 2 library and must be called before trying to use any of the functionality of the ‘freetype’ module.

However, this function will be automatically called by pygame.init(). It is safe to call this function more than once.

Optionally, you may specify a default size for the Glyph cache: this is the maximum number of glyphs that will be cached at any given time by the module. Exceedingly small values will be automatically tuned for performance. Also a default pixel resolution, in dots per inch, can be given to adjust font scaling.

pygame.freetype.quit()
Shut down the underlying FreeType 2 library.
quit() -> None

This function de-initializes the freetype module. After calling this function, you should not invoke any class, method or function related to the freetype module as they are likely to fail or might give unpredictable results. It is safe to call this function even if the module hasn’t been initialized yet.

pygame.freetype.was_init()
Return whether the the FreeType 2 library is initialized.
was_init() -> bool

Returns whether the the FreeType 2 library is initialized.

pygame.freetype.get_default_resolution()
Return the default pixel size in dots per inch
get_default_resolution() -> long

Returns the default pixel size, in dots per inch for the module. If not changed it will be 72.

pygame.freetype.set_default_resolution()
Set the default pixel size in dots per inch for the module
set_default_resolution([resolution]) -> None

Set the default pixel size, in dots per inch, for the module. If the optional argument is omitted or zero the resolution is reset to 72.

pygame.freetype.get_default_font()
Get the filename of the default font
get_default_font() -> 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.

class pygame.freetype.Font
Create a new Font instance from a supported font file.
Font(file, style=STYLE_NONE, ptsize=-1, font_index=0, vertical=0, ucs4=0, resolution=0) -> Font
pygame.freetype.Font.name Proper font name.
pygame.freetype.Font.path Font file path
pygame.freetype.Font.get_rect Return the size and offset of rendered text
pygame.freetype.Font.get_metrics Return the glyph metrics for the given text
pygame.freetype.Font.height The unscaled height of the font in font units
pygame.freetype.Font.ascender The unscaled ascent of the font in font units
pygame.freetype.Font.descender The unscaled descent of the font in font units
pygame.freetype.Font.get_sized_ascender The scaled ascent of the font in pixels
pygame.freetype.Font.get_sized_descender The scaled descent of the font in pixels
pygame.freetype.Font.get_sized_height The scaled height of the font in pixels
pygame.freetype.Font.get_sized_glyph_height The scaled bounding box height of the font in pixels
pygame.freetype.Font.render Return rendered text as a surface
pygame.freetype.Font.render_to Render text onto an existing surface
pygame.freetype.Font.render_raw Return rendered text as a string of bytes
pygame.freetype.Font.render_raw_to Render text into an array of ints
pygame.freetype.Font.style The font’s style flags
pygame.freetype.Font.underline The state of the font’s underline style flag
pygame.freetype.Font.strong The state of the font’s strong style flag
pygame.freetype.Font.oblique The state of the font’s oblique style flag
pygame.freetype.Font.wide The state of the font’s wide style flag
pygame.freetype.Font.strength The strength associated with the strong or wide font styles
pygame.freetype.Font.underline_adjustment Adjustment factor for the underline position
pygame.freetype.Font.fixed_width Gets whether the font is fixed-width
pygame.freetype.Font.antialiased Font anti-aliasing mode
pygame.freetype.Font.kerning Character kerning mode
pygame.freetype.Font.vertical Font vertical mode
pygame.freetype.Font.origin Font render to text origin mode
pygame.freetype.Font.pad padded boundary mode
pygame.freetype.Font.ucs4 Enable UCS-4 mode
pygame.freetype.Font.resolution Pixel resolution in dots per inch

Argument file can be either a string representing the font’s filename, a file-like object containing the font, or None; if None, the default, built-in font is used.

Optionally, a ptsize argument may be specified to set the default size in points, which will be used when rendering the font. The size can also be passed explicitly to each method call. Because of the way the caching system works, specifying a default size on the constructor doesn’t imply a performance gain over manually passing the size on each function call.

If the font file has more than one font, the font to load can be chosen with the index argument. An exception is raised for an out-of-range font index value.

The style argument will set the default style (oblique, underline, strong) used to draw this font. This style may be overridden on any Font.render() call.

The optional vertical argument, an integer, sets the default orientation for the font: 0 (False) for horizontal, any other value (True) for vertical. See Font.vertical.

The optional ucs4 argument, an integer, sets the default text translation mode: 0 (False) recognize UTF-16 surrogate pairs, any other value (True), to treat Unicode text as UCS-4, with no surrogate pairs. See Font.ucs4.

The optional resolution argument sets the pixel size, in dots per inch, for use in scaling glyphs for this Font instance. If 0 then the default module value, set by freetype.init(), is used. The Font object’s resolution can only be changed by reinitializing the Font instance.

name
Proper font name.
name -> string

Read only. Returns the real (long) name of the font, as recorded in the font file.

path
Font file path
path -> unicode

Read only. Returns the path of the loaded font file

get_rect()
Return the size and offset of rendered text
get_rect(text, style=STYLE_DEFAULT, rotation=0, ptsize=default) -> rect

Gets the final dimensions and origin, in pixels, of ‘text’ using the current point size, style, rotation and orientation. These are either taken from the arguments, if given, else from the default values set for the font object.

Returns a rect containing the width and height of the text’s bounding box and the position of the text’s origin. The origin can be used to align separately rendered pieces of text. It gives the baseline position and bearing at the start of the text.

If text is a char (byte) string, then its encoding is assumed to be LATIN1.

get_metrics()
Return the glyph metrics for the given text
get_metrics(text, ptsize=default) -> [(...), ...]

Returns the glyph metrics for each character in ‘text’.

The glyph metrics are returned inside a list; each character will be represented as a tuple inside the list with the following values:

(min_x, max_x, min_y, max_y, horizontal_advance_x, horizontal_advance_y)

The bounding box min_x, max_y, min_y, and max_y values are returned as grid-fitted pixel coordinates of type int. The advance values are float values.

The calculations are done using the font’s default size in points. Optionally you may specify another point size to use.

The metrics are adjusted for the current rotation, strong, and oblique settings.

If text is a char (byte) string, then its encoding is assumed to be LATIN1.

height
The unscaled height of the font in font units
height -> int

Read only. Gets the height of the font. This is the average value of all glyphs in the font.

ascender()
The unscaled ascent of the font in font units
ascender -> int

Read only. Return the number of units from the font’s baseline to the top of the bounding box.

descender
The unscaled descent of the font in font units
descender -> int

Read only. Return the height in font units for the font descent. The descent is the number of units from the font’s baseline to the bottom of the bounding box.

get_sized_ascender
The scaled ascent of the font in pixels
get_sized_ascender() -> int

Return the number of units from the font’s baseline to the top of the bounding box. It is not adjusted for strong or rotation.

get_sized_descender()
The scaled descent of the font in pixels
get_sized_descender() -> int

Return the number of pixels from the font’s baseline to the top of the bounding box. It is not adjusted for strong or rotation.

get_sized_height
The scaled height of the font in pixels
get_sized_height() -> int

Read only. Gets the height of the font. This is the average value of all glyphs in the font. It is not adjusted for strong or rotation.

get_sized_glyph_height()
The scaled bounding box height of the font in pixels
get_sized_glyph_height() -> int

Return the glyph bounding box height of the font in pixels. This is the average value of all glyphs in the font. It is not adjusted for strong or rotation.

render()
Return rendered text as a surface
render(text, fgcolor, bgcolor=None, style=STYLE_DEFAULT, rotation=0, ptsize=default) -> (Surface, Rect)

Returns a new pygame.Surfacepygame object for representing images, with the text rendered to it in the color given by ‘fgcolor’. If bgcolor is given, the surface will be filled with this color. If no background color is given, the surface is filled with zero alpha opacity. Normally the returned surface has a 32 bit pixel size. However, if bgcolor is None and anti-aliasing is disabled a two color 8 bit surface with colorkey set for the background color is returned.

The return value is a tuple: the new surface and the bounding rectangle giving the size and origin of the rendered text.

If an empty string is passed for text then the returned Rect is zero width and the height of the font. If dest is None the returned surface is the same dimensions as the boundary rect. The rect will test False.

The rendering is done using the font’s default size in points and its default style, without any rotation, and taking into account fonts which are set to be drawn vertically via the Font.vertical() attribute. Optionally you may specify another point size to use via the ‘ptsize’ argument, a text rotation via the ‘rotation’ argument, or a new text style via the ‘style’ argument.

If text is a char (byte) string, then its encoding is assumed to be LATIN1.

render_to()
Render text onto an existing surface
render(surf, dest, text, fgcolor, bgcolor=None, style=STYLE_DEFAULT, rotation=0, ptsize=default) -> Rect

Renders the string ‘text’ to a pygame.Surfacepygame object for representing images ‘surf’, using the color ‘fgcolor’.

Argument ‘dest’ is an (x, y) surface coordinate pair. If either x or y is not an integer it is converted to one if possible. Any sequence, including Rect, for which the first two elements are positions x and y is accepted.

If a background color is given, the surface is first filled with that color. The text is blitted next. Both the background fill and text rendering involve full alpha blits. That is, the alpha values of both the foreground and background colors, as well as those of the destination surface if it has per-pixel alpha.

The return value is a rectangle giving the size and position of the rendered text within the surface.

If an empty string is passed for text then the returned Rect is zero width and the height of the font. The rect will test False.

By default, the point size and style set for the font are used if not passed as arguments. The text is unrotated unless a non-zero rotation value is given.

If text is a char (byte) string, then its encoding is assumed to be LATIN1.

render_raw()
Return rendered text as a string of bytes
render_raw(text, style=STYLE_DEFAULT, rotation=0, ptsize=default, invert=False) -> (bytes, (int, int))

Like Font.render() but the tuple returned is an 8 bit monochrome string of bytes and its size. The foreground color is 255, the background 0, useful as an alpha mask for a foreground pattern.

render_raw_to()
Render text into an array of ints
render_raw_to(array, text, dest=None, style=STYLE_DEFAULT, rotation=0, ptsize=default, invert=False) -> (int, int)

Render to an array object exposing an array struct interface. The array must be two dimensional with integer items. The default dest value, None, is equivalent to (0, 0).

style
The font’s style flags
style <-> int

Gets or sets the default style of the Font. This default style will be used for all text rendering and size calculations unless overridden specifically in the `render()` or `get_size()` calls. The style value may be a bit-wise OR of one or more of the following constants:

STYLE_NONE
STYLE_UNDERLINE
STYLE_OBLIQUE
STYLE_STRONG
STYLE_WIDE

These constants may be found on the FreeType constants module. Optionally, the default style can be modified or obtained accessing the individual style attributes (underline, oblique, strong).

underline
The state of the font’s underline style flag
underline <-> bool

Gets or sets whether the font will be underlined when drawing text. This default style value will be used for all text rendering and size calculations unless overridden specifically in the `render()` or `get_size()` calls, via the ‘style’ parameter.

strong
The state of the font’s strong style flag
strong <-> bool

Gets or sets whether the font will be bold when drawing text. This default style value will be used for all text rendering and size calculations unless overridden specifically in the `render()` or `get_size()` calls, via the ‘style’ parameter.

oblique
The state of the font’s oblique style flag
oblique <-> bool

Gets or sets whether the font will be rendered as oblique. This default style value will be used for all text rendering and size calculations unless overridden specifically in the `render()` or `get_size()` calls, via the ‘style’ parameter.

wide
The state of the font’s wide style flag
wide <-> bool

Gets or sets whether the font will be stretched horizontally when drawing text. It produces a result similar to font.Font’s bold. This style is only available for unrotated text.

strength
The strength associated with the strong or wide font styles
strength <-> float

The amount by which a font glyph’s size is enlarged for the strong or wide transformations, as a fraction of the untransformed size. For the wide style only the horizontal dimension is increased. For strong text both the horizontal and vertical dimensions are enlarged. A wide style of strength 1/12 is equivalent to the font.Font bold style. The default is 1/36.

underline_adjustment
Adjustment factor for the underline position
underline_adjustment <-> float

Gets or sets a factor which, when positive, is multiplied with the font’s underline offset to adjust the underline position. A negative value turns an underline into a strike-through or overline. It is multiplied with the ascender. Accepted values are between -2.0 and 2.0 inclusive. A value of 0.5 closely matches Tango underlining. A value of 1.0 mimics SDL_ttf.

fixed_width
Gets whether the font is fixed-width
fixed_width -> bool

Read only. Returns whether this Font is a fixed-width (bitmap) font.

Note that scalable fonts whose glyphs are all the same width (i.e. monospace TTF fonts used for programming) are not considered fixed width.

antialiased
Font anti-aliasing mode
antialiased <-> bool

Gets or sets the font’s anti-aliasing mode. This defaults to True on all fonts, which are rendered with full 8 bit blending.

Setting this to False will enable monochrome rendering. This should provide a small speed gain and reduce cache memory size.

kerning
Character kerning mode
kerning -> bool

Gets or sets the font’s kerning mode. This defaults to False on all fonts, which will be rendered by default without kerning.

Setting this to true will change all rendering methods to do kerning between character pairs for surface size calculation and all render operations.

vertical
Font vertical mode
vertical -> bool

Gets or sets whether the font is a vertical font such as fonts in fonts representing Kanji glyphs or other styles of vertical writing.

Changing this attribute will cause the font to be rendering vertically, and affects all other methods which manage glyphs or text layouts to use vertical metrics accordingly.

Note that the FreeType library doesn’t automatically detect whether a font contains glyphs which are always supposed to be drawn vertically, so this attribute must be set manually by the user.

Also note that several font formats (especially bitmap based ones) don’t contain the necessary metrics to draw glyphs vertically, so drawing in those cases will give unspecified results.

origin
Font render to text origin mode
vertical -> bool

If set True, then when rendering to an existing surface, the position is taken to be that of the text origin. Otherwise the render position is the top-left corner of the text bounding box.

pad
padded boundary mode
pad -> bool

If set True, then the text boundary rectangle will be inflated to match that of font.Font. Otherwise, the boundary rectangle is just large enough for the text.

ucs4
Enable UCS-4 mode
ucs4 <-> bool

Gets or sets the decoding of Unicode text. By default, the freetype module performs UTF-16 surrogate pair decoding on Unicode text. This allows 32-bit escape sequences (‘Uxxxxxxxx’) between 0x10000 and 0x10FFFF to represent their corresponding UTF-32 code points on Python interpreters built with a UCS-2 unicode type (on Windows, for instance). It also means character values within the UTF-16 surrogate area (0xD800 to 0xDFFF) are considered part of a surrogate pair. A malformed surrogate pair will raise an UnicodeEncodeError. Setting ucs4 True turns surrogate pair decoding off, letting interpreters with a UCS-4 unicode type access the full UCS-4 character range.

resolution
Pixel resolution in dots per inch
resolution -> int

Gets the pixel size used in scaling font glyphs for this Font instance.