pygame.freetype
Enhanced Pygame module for loading and rendering font faces
pygame.freetype.get_error Get the latest error
pygame.freetype.get_version Get the FreeType version
pygame.freetype.init Initialize the underlying FreeType 2 library.
pygame.freetype.quit Shuts down the underlying FreeType 2 library.
pygame.freetype.was_init Returns 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.Face Creates a new Face instance from a supported font file.

— Note that some features may change before a formal release

This module allows for rendering all face formats supported by FreeType, namely TTF, Type1, CFF, OpenType, SFNT, PCF, FNT, BDF, PFR and Type42 faces. It can render any UTF-32 character in a font file.

This module is optional, and replaces all of the functionality of the original ‘font’ module, whilst expanding it. This module depends in no way on the SDL_ttf library.

You should test that pygame.freetypeEnhanced Pygame module for loading and rendering font faces is initialized before attempting to use the module; if the module is available and loaded, it will be automatically initialized by pygame.init()

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

You can load faces from the system by using the pygame.freetype.SysFont() function. There are a few other functions to help lookup the 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 Face constructor.

New in Pygame 1.9.2

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

Returns the description of the last error which occurred in the FreeType library, or None if no errors have occurred.

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

Gets 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, and 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, if the module is available, 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 amount 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 face scaling.

pygame.freetype.quit()
Shuts 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()
Returns 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. At initial module load time the value is 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.Face
Creates a new Face instance from a supported font file.
Face(file, style=STYLE_NONE, ptsize=-1, face_index=0, vertical=0, ucs4=0, resolution=0) -> Face
pygame.freetype.Face.name Gets the name of the font face.
pygame.freetype.Face.path Gets the path of the font file
pygame.freetype.Face.get_rect Gets the size and offset of rendered text
pygame.freetype.Face.get_metrics Gets glyph metrics for the face’s characters
pygame.freetype.Face.height Gets the unscaled height of the face in font units
pygame.freetype.Face.ascender get the unscaled ascent of the face in font units
pygame.freetype.Face.descender get the unscaled descent of the face in font units
pygame.freetype.Face.get_sized_ascender Gets the scaled ascent the face in pixels
pygame.freetype.Face.get_sized_descender Gets the scaled descent the face in pixels
pygame.freetype.Face.get_sized_height Gets the scaled height of the face in pixels
pygame.freetype.Face.get_sized_glyph_height Gets the scaled height of the face in pixels
pygame.freetype.Face.render Renders text on a surface
pygame.freetype.Face.render_to Renders text to an existing surface
pygame.freetype.Face.render_raw Renders text as a string of bytes
pygame.freetype.Face.render_raw_to Renders text as a string of ints to an array
pygame.freetype.Face.style Gets or sets the face’s style
pygame.freetype.Face.underline Gets or sets the face’s underline style
pygame.freetype.Face.strong Gets or sets the face’s strong style
pygame.freetype.Face.oblique Gets or sets the face’s oblique style
pygame.freetype.Face.wide Gets or sets the face’s wide style
pygame.freetype.Face.strength Gets or sets the strength of the strong or wide styles
pygame.freetype.Face.underline_adjustment Gets or sets an adjustment factor for the underline position
pygame.freetype.Face.fixed_width Gets whether the face is fixed-width
pygame.freetype.Face.antialiased Face antialiasing mode
pygame.freetype.Face.kerning Character kerning mode
pygame.freetype.Face.vertical Face vertical mode
pygame.freetype.Face.origin Face render to text origin mode
pygame.freetype.Face.pad padded boundary mode
pygame.freetype.Face.ucs4 Enables UCS-4 mode
pygame.freetype.Face.resolution Output pixel resolution in dots per inch
pygame.freetype.Face.set_transform assign a glyph transformation matrix
pygame.freetype.Face.delete_transform delete a glyph transformation matrix
pygame.freetype.Face.get_transform return the user assigned transformation matrix, or None

‘file’ can be either a string representing the font’s filename, a file-like object containing the font, or None; in this last case the default, built-in font will be used.

Optionally, a *ptsize* argument may be specified to set the default size in points which will be used to render the face. Such size can also be specified manually on 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 face, the *index* argument may be specified to specify which face index to load. Defaults to 0; face loading will fail if the given index is not contained in the font.

The ‘style’ argument will set the default style (oblique, underline, strong) used to draw this face. This style may be overriden on any Face.render() call.

The optional vertical argument, an integer, sets the default orientation for the face: 0 (False) for horizontal, any other value (True) for vertical. See Face.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 Face.ucs4.

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

name
Gets the name of the font face.
name -> string

Read only. Returns the real (long) name of the font type face, as specified on the font file.

path
Gets the path of the font file
path -> unicode

Read only. Returns the path of the loaded font file

get_rect()
Gets 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 face 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()
Gets glyph metrics for the face’s characters
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 face’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
Gets the unscaled height of the face in font units
height -> int

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

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

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

descender
get the unscaled descent of the face in font units
descender -> int

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

get_sized_ascender
Gets the scaled ascent the face in pixels
get_sized_ascender() -> int

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

get_sized_descender()
Gets the scaled descent the face in pixels
get_sized_descender() -> int

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

get_sized_height
Gets the scaled height of the face in pixels
get_sized_height() -> int

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

get_sized_glyph_height()
Gets the scaled height of the face in pixels
get_sized_glyph_height() -> int

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

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

Renturns 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 antialiasing 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 face. 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 face’s default size in points and its default style, without any rotation, and taking into account faces which are set to be drawn vertically via the Face.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()
Renders text to 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 face. The rect will test False.

By default, the point size and style set for the face 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()
Renders text as a string of bytes
render_raw(text, style=STYLE_DEFAULT, rotation=0, ptsize=default, invert=False) -> (bytes, (int, int))

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

render_raw_to()
Renders text as a string of ints to an array
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
Gets or sets the face’s style
style -> int

Gets or sets the default style of the Face. This default style will be used for all text rendering and size calculations unless overriden specifically in the `render()` or `get_size()` calls. The style value may be a bitwise 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
Gets or sets the face’s underline style
underline -> bool

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

strong
Gets or sets the face’s strong style
strong -> bool

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

oblique
Gets or sets the face’s oblique style
oblique -> bool

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

wide
Gets or sets the face’s wide style
wide -> bool

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

strength
Gets or sets the strength of the strong or wide styles
strength -> float

The amount by which a face 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
Gets or sets an adjustment factor for the underline position
underline_adjustment -> float

Gets or sets a factor which, when positive, is multiplied with the face’s underline offset to adjust the underline position. A negative value turns an underline into a strikethrough 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 face is fixed-width
fixed_width -> bool

Read only. Returns whether this Face is a fixed-width (bitmap) face.

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

antialiased
Face antialiasing mode
antialiased -> bool

Gets or sets the face’s antialiasing mode. This defaults to True on all faces, 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 face’s kerning mode. This defaults to False on all faces, 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
Face vertical mode
vertical -> bool

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

Changing this attribute will cause the face 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 face contains glyphs which are always supposed to be drawn vertically, so this attribute must be set manually by the user.

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

origin
Face 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
Enables UCS-4 mode
ucs4 -> bool

Gets or sets the decoding of Unicode textdecoding. 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
Output pixel resolution in dots per inch
resolution -> int

Gets the pixel size used in scaling face glyphs for this Face instance.

set_transform()
assign a glyph transformation matrix
set_transform(xx, xy, yx, yy) -> None

Set a transform matrix for the face. If None, no matrix assigned. The arguments can be any numeric type that can be converted to a double. The matrix is applied after the strong transformation, but before oblique and rotation.

delete_transform()
delete a glyph transformation matrix
set_transform(xx, xy, yx, yy) -> None

Remove the transformation matrix, if any.

get_transform()
return the user assigned transformation matrix, or None
get_transform() -> (double, double, double, double) or None

Return the transform matrix for the face. If None, no matrix is assigned.