File: font.rst

package info (click to toggle)
pygame 1.9.6%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,060 kB
  • sloc: ansic: 59,765; python: 31,220; objc: 334; makefile: 57; cpp: 25
file content (324 lines) | stat: -rw-r--r-- 11,737 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
.. include:: common.txt

:mod:`pygame.font`
==================

.. module:: pygame.font
   :synopsis: pygame module for loading and rendering fonts

| :sl:`pygame module for loading and rendering fonts`

The font module allows for rendering TrueType fonts into a new Surface object.
It accepts any UCS-2 character ('\u0001' to '\uFFFF'). This module is optional
and requires SDL_ttf as a dependency. You should test that :mod:`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()``.

You can load fonts from the system by using the ``pygame.font.SysFont()``
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.

To use the :mod:`pygame.freetype` based ``pygame.ftfont`` as
:mod:`pygame.font` define the environment variable PYGAME_FREETYPE before the
first import of :mod:`pygame`. Module ``pygame.ftfont`` is a :mod:`pygame.font`
compatible module that passes all but one of the font module unit tests:
it does not have the UCS-2 limitation of the SDL_ttf based font module, so
fails to raise an exception for a code point greater than '\uFFFF'. If
:mod:`pygame.freetype` is unavailable then the SDL_ttf font module will be
loaded instead.

.. function:: init

   | :sl:`initialize the font module`
   | :sg:`init() -> None`

   This method is called automatically by ``pygame.init()``. 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.init ##

.. function:: quit

   | :sl:`uninitialize the font module`
   | :sg:`quit() -> None`

   Manually uninitialize SDL_ttf's font system. This is called automatically by
   ``pygame.quit()``.

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

   .. ## pygame.font.quit ##

.. function:: get_init

   | :sl:`true if the font module is initialized`
   | :sg:`get_init() -> bool`

   Test if the font module is initialized or not.

   .. ## pygame.font.get_init ##

.. function:: get_default_font

   | :sl:`get the filename of the default font`
   | :sg:`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.

   .. ## pygame.font.get_default_font ##

.. function:: get_fonts

   | :sl:`get all available fonts`
   | :sg:`get_fonts() -> 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.get_fonts ##

.. function:: match_font

   | :sl:`find a specific font on the system`
   | :sg:`match_font(name, bold=False, italic=False) -> 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.match_font ##

.. function:: SysFont

   | :sl:`create a Font object from the system fonts`
   | :sg:`SysFont(name, size, bold=False, italic=False) -> 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 fall back on loading the default pygame font. The font name
   can be a comma separated list of font names to look for.

   .. ## pygame.font.SysFont ##

.. class:: Font

   | :sl:`create a new Font object from a file`
   | :sg:`Font(filename, size) -> Font`
   | :sg:`Font(object, size) -> 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.

   .. method:: render

      | :sl:`draw text on a new Surface`
      | :sg:`render(text, antialias, color, background=None) -> 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()`` 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.
      Null characters ('\x00') raise a TypeError. Both Unicode and char (byte)
      strings are accepted. For Unicode strings only UCS-2 characters ('\u0001'
      to '\uFFFF') are recognized. Anything greater raises a UnicodeError. For
      char strings a ``LATIN1`` encoding is assumed. 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 8-bit 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' an 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
      at any time.

      .. ## Font.render ##

   .. method:: size

      | :sl:`determine the amount of space needed to render text`
      | :sg:`size(text) -> (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.size ##

   .. method:: set_underline

      | :sl:`control if text is rendered with an underline`
      | :sg:`set_underline(bool) -> 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.set_underline ##

   .. method:: get_underline

      | :sl:`check if text will be rendered with an underline`
      | :sg:`get_underline() -> bool`

      Return True when the font underline is enabled.

      .. ## Font.get_underline ##

   .. method:: set_bold

      | :sl:`enable fake rendering of bold text`
      | :sg:`set_bold(bool) -> 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.set_bold ##

   .. method:: get_bold

      | :sl:`check if text will be rendered bold`
      | :sg:`get_bold() -> bool`

      Return True when the font bold rendering mode is enabled.

      .. ## Font.get_bold ##

   .. method:: set_italic

      | :sl:`enable fake rendering of italic text`
      | :sg:`set_italic(bool) -> 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.set_italic ##

   .. method:: metrics

      | :sl:`gets the metrics for each character in the passed string`
      | :sg:`metrics(text) -> 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), ...]. None is entered in the list for each unrecognized
      character.

      .. ## Font.metrics ##

   .. method:: get_italic

      | :sl:`check if the text will be rendered italic`
      | :sg:`get_italic() -> bool`

      Return True when the font italic rendering mode is enabled.

      .. ## Font.get_italic ##

   .. method:: get_linesize

      | :sl:`get the line space of the font text`
      | :sg:`get_linesize() -> 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_linesize ##

   .. method:: get_height

      | :sl:`get the height of the font`
      | :sg:`get_height() -> int`

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

      .. ## Font.get_height ##

   .. method:: get_ascent

      | :sl:`get the ascent of the font`
      | :sg:`get_ascent() -> 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_ascent ##

   .. method:: get_descent

      | :sl:`get the descent of the font`
      | :sg:`get_descent() -> 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.

      .. ## Font.get_descent ##

   .. ## pygame.font.Font ##

.. ## pygame.font ##