1
.. include:: common.txt
6
.. module:: pygame.font
7
:synopsis: pygame module for loading and rendering fonts
9
| :sl:`pygame module for loading and rendering fonts`
11
The font module allows for rendering TrueType fonts into a new Surface object.
12
It accepts any UCS-2 character ('\u0001' to '\uFFFF'). This module is optional
13
and requires SDL_ttf as a dependency. You should test that :mod:`pygame.font`
14
is available and initialized before attempting to use the module.
16
Most of the work done with fonts are done by using the actual Font objects. The
17
module by itself only has routines to initialize the module and create Font
18
objects with ``pygame.font.Font()``.
20
You can load fonts from the system by using the ``pygame.font.SysFont()``
21
function. There are a few other functions to help lookup the system fonts.
23
Pygame comes with a builtin default font. This can always be accessed by
24
passing None as the font name.
26
To use the :mod:`pygame.freetype` based ``pygame.ftfont`` as
27
:mod:`pygame.font` define the enviroment variable PYGAME_FREETYPE before the
28
first import of :mod:`pygame`. Module ``pygame.ftfont`` is a :mod:`pygame.font`
29
compatible module that passes all but one of the font module unit tests:
30
it does not have the UCS-2 limitation of the SDL_ttf based font module, so
31
fails to raise an exception for a code point greater than '\uFFFF'. If
32
:mod:`pygame.freetype` is unavailable then the SDL_ttf font module will be
37
| :sl:`initialize the font module`
38
| :sg:`init() -> None`
40
This method is called automatically by ``pygame.init()``. It initializes the
41
font module. The module must be initialized before any other functions will
44
It is safe to call this function more than once.
46
.. ## pygame.font.init ##
50
| :sl:`uninitialize the font module`
51
| :sg:`quit() -> None`
53
Manually uninitialize SDL_ttf's font system. This is called automatically by
56
It is safe to call this function even if font is currently not initialized.
58
.. ## pygame.font.quit ##
60
.. function:: get_init
62
| :sl:`true if the font module is initialized`
63
| :sg:`get_init() -> bool`
65
Test if the font module is initialized or not.
67
.. ## pygame.font.get_init ##
69
.. function:: get_default_font
71
| :sl:`get the filename of the default font`
72
| :sg:`get_default_font() -> string`
74
Return the filename of the system font. This is not the full path to the
75
file. This file can usually be found in the same directory as the font
76
module, but it can also be bundled in separate archives.
78
.. ## pygame.font.get_default_font ##
80
.. function:: get_fonts
82
| :sl:`get all available fonts`
83
| :sg:`get_fonts() -> list of strings`
85
Returns a list of all the fonts available on the system. The names of the
86
fonts will be set to lowercase with all spaces and punctuation removed. This
87
works on most systems, but some will return an empty list if they cannot
90
.. ## pygame.font.get_fonts ##
92
.. function:: match_font
94
| :sl:`find a specific font on the system`
95
| :sg:`match_font(name, bold=False, italic=False) -> path`
97
Returns the full path to a font file on the system. If bold or italic are
98
set to true, this will attempt to find the correct family of font.
100
The font name can actually be a comma separated list of font names to try.
101
If none of the given names are found, None is returned.
107
print pygame.font.match_font('bitstreamverasans')
108
# output is: /usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf
109
# (but only if you have Vera on your system)
111
.. ## pygame.font.match_font ##
113
.. function:: SysFont
115
| :sl:`create a Font object from the system fonts`
116
| :sg:`SysFont(name, size, bold=False, italic=False) -> Font`
118
Return a new Font object that is loaded from the system fonts. The font will
119
match the requested bold and italic flags. If a suitable system font is not
120
found this will fallback on loading the default pygame font. The font name
121
can be a comma separated list of font names to look for.
123
.. ## pygame.font.SysFont ##
127
| :sl:`create a new Font object from a file`
128
| :sg:`Font(filename, size) -> Font`
129
| :sg:`Font(object, size) -> Font`
131
Load a new font from a given filename or a python file object. The size is
132
the height of the font in pixels. If the filename is None the Pygame default
133
font will be loaded. If a font cannot be loaded from the arguments given an
134
exception will be raised. Once the font is created the size cannot be
137
Font objects are mainly used to render text into new Surface objects. The
138
render can emulate bold or italic features, but it is better to load from a
139
font with actual italic or bold glyphs. The rendered text can be regular
144
| :sl:`draw text on a new Surface`
145
| :sg:`render(text, antialias, color, background=None) -> Surface`
147
This creates a new Surface with the specified text rendered on it. Pygame
148
provides no way to directly draw text on an existing Surface: instead you
149
must use ``Font.render()`` to create an image (Surface) of the text, then
150
blit this image onto another Surface.
152
The text can only be a single line: newline characters are not rendered.
153
Null characters ('\x00') raise a TypeError. Both Unicode and char (byte)
154
strings are accepted. For Unicode strings only UCS-2 characters ('\u0001'
155
to '\uFFFF') are recognized. Anything greater raises a UnicodeError. For
156
char strings a ``LATIN1`` encoding is assumed. The antialias argument is
157
a boolean: if true the characters will have smooth edges. The color
158
argument is the color of the text [e.g.: (0,0,255) for blue]. The
159
optional background argument is a color to use for the text background.
160
If no background is passed the area outside the text will be transparent.
162
The Surface returned will be of the dimensions required to hold the text.
163
(the same as those returned by Font.size()). If an empty string is passed
164
for the text, a blank surface will be returned that is one pixel wide and
165
the height of the font.
167
Depending on the type of background and antialiasing used, this returns
168
different types of Surfaces. For performance reasons, it is good to know
169
what type of image will be used. If antialiasing is not used, the return
170
image will always be an 8bit image with a two color palette. If the
171
background is transparent a colorkey will be set. Antialiased images are
172
rendered to 24-bit ``RGB`` images. If the background is transparent a
173
pixel alpha will be included.
175
Optimization: if you know that the final destination for the text (on the
176
screen) will always have a solid background, and the text is antialiased,
177
you can improve performance by specifying the background color. This will
178
cause the resulting image to maintain transparency information by
179
colorkey rather than (much less efficient) alpha values.
181
If you render '\n' a unknown char will be rendered. Usually a rectangle.
182
Instead you need to handle new lines yourself.
184
Font rendering is not thread safe: only a single thread can render text
191
| :sl:`determine the amount of space needed to render text`
192
| :sg:`size(text) -> (width, height)`
194
Returns the dimensions needed to render the text. This can be used to
195
help determine the positioning needed for text before it is rendered. It
196
can also be used for wordwrapping and other layout effects.
198
Be aware that most fonts use kerning which adjusts the widths for
199
specific letter pairs. For example, the width for "ae" will not always
200
match the width for "a" + "e".
204
.. method:: set_underline
206
| :sl:`control if text is rendered with an underline`
207
| :sg:`set_underline(bool) -> None`
209
When enabled, all rendered fonts will include an underline. The underline
210
is always one pixel thick, regardless of font size. This can be mixed
211
with the bold and italic modes.
213
.. ## Font.set_underline ##
215
.. method:: get_underline
217
| :sl:`check if text will be rendered with an underline`
218
| :sg:`get_underline() -> bool`
220
Return True when the font underline is enabled.
222
.. ## Font.get_underline ##
226
| :sl:`enable fake rendering of bold text`
227
| :sg:`set_bold(bool) -> None`
229
Enables the bold rendering of text. This is a fake stretching of the font
230
that doesn't look good on many font types. If possible load the font from
231
a real bold font file. While bold, the font will have a different width
232
than when normal. This can be mixed with the italic and underline modes.
234
.. ## Font.set_bold ##
238
| :sl:`check if text will be rendered bold`
239
| :sg:`get_bold() -> bool`
241
Return True when the font bold rendering mode is enabled.
243
.. ## Font.get_bold ##
245
.. method:: set_italic
247
| :sl:`enable fake rendering of italic text`
248
| :sg:`set_italic(bool) -> None`
250
Enables fake rendering of italic text. This is a fake skewing of the font
251
that doesn't look good on many font types. If possible load the font from
252
a real italic font file. While italic the font will have a different
253
width than when normal. This can be mixed with the bold and underline
256
.. ## Font.set_italic ##
260
| :sl:`Gets the metrics for each character in the pased string.`
261
| :sg:`metrics(text) -> list`
263
The list contains tuples for each character, which contain the minimum
264
``X`` offset, the maximum ``X`` offset, the minimum ``Y`` offset, the
265
maximum ``Y`` offset and the advance offset (bearing plus width) of the
266
character. [(minx, maxx, miny, maxy, advance), (minx, maxx, miny, maxy,
267
advance), ...]. None is entered in the list for each unrecognized
270
.. ## Font.metrics ##
272
.. method:: get_italic
274
| :sl:`check if the text will be rendered italic`
275
| :sg:`get_italic() -> bool`
277
Return True when the font italic rendering mode is enabled.
279
.. ## Font.get_italic ##
281
.. method:: get_linesize
283
| :sl:`get the line space of the font text`
284
| :sg:`get_linesize() -> int`
286
Return the height in pixels for a line of text with the font. When
287
rendering multiple lines of text this is the recommended amount of space
290
.. ## Font.get_linesize ##
292
.. method:: get_height
294
| :sl:`get the height of the font`
295
| :sg:`get_height() -> int`
297
Return the height in pixels of the actual rendered text. This is the
298
average size for each glyph in the font.
300
.. ## Font.get_height ##
302
.. method:: get_ascent
304
| :sl:`get the ascent of the font`
305
| :sg:`get_ascent() -> int`
307
Return the height in pixels for the font ascent. The ascent is the number
308
of pixels from the font baseline to the top of the font.
310
.. ## Font.get_ascent ##
312
.. method:: get_descent
314
| :sl:`get the descent of the font`
315
| :sg:`get_descent() -> int`
317
Return the height in pixels for the font descent. The descent is the
318
number of pixels from the font baseline to the bottom of the font.
320
.. ## Font.get_descent ##
322
.. ## pygame.font.Font ##