~pygame/pygame/trunk

« back to all changes in this revision

Viewing changes to docs/reST/ref/font.rst

  • Committer: pygame
  • Date: 2017-01-10 00:31:42 UTC
  • Revision ID: git-v1:2eea4f299a2e791f884608d7ed601558634af73c
commit 1639c41a8cb3433046882ede92c80ce69d59016b
Author: Thomas Kluyver <takowl@gmail.com>
Date:   Sun Jan 8 18:46:46 2017 +0000

    Build newer versions of libogg and libvorbis into Linux base images

    Closes #317
    Closes #323

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
.. include:: common.txt
 
2
 
 
3
:mod:`pygame.font`
 
4
==================
 
5
 
 
6
.. module:: pygame.font
 
7
   :synopsis: pygame module for loading and rendering fonts
 
8
 
 
9
| :sl:`pygame module for loading and rendering fonts`
 
10
 
 
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.
 
15
 
 
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()``.
 
19
 
 
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.
 
22
 
 
23
Pygame comes with a builtin default font. This can always be accessed by
 
24
passing None as the font name.
 
25
 
 
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
 
33
loaded instead.
 
34
 
 
35
.. function:: init
 
36
 
 
37
   | :sl:`initialize the font module`
 
38
   | :sg:`init() -> None`
 
39
 
 
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
 
42
   work.
 
43
 
 
44
   It is safe to call this function more than once.
 
45
 
 
46
   .. ## pygame.font.init ##
 
47
 
 
48
.. function:: quit
 
49
 
 
50
   | :sl:`uninitialize the font module`
 
51
   | :sg:`quit() -> None`
 
52
 
 
53
   Manually uninitialize SDL_ttf's font system. This is called automatically by
 
54
   ``pygame.quit()``.
 
55
 
 
56
   It is safe to call this function even if font is currently not initialized.
 
57
 
 
58
   .. ## pygame.font.quit ##
 
59
 
 
60
.. function:: get_init
 
61
 
 
62
   | :sl:`true if the font module is initialized`
 
63
   | :sg:`get_init() -> bool`
 
64
 
 
65
   Test if the font module is initialized or not.
 
66
 
 
67
   .. ## pygame.font.get_init ##
 
68
 
 
69
.. function:: get_default_font
 
70
 
 
71
   | :sl:`get the filename of the default font`
 
72
   | :sg:`get_default_font() -> string`
 
73
 
 
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.
 
77
 
 
78
   .. ## pygame.font.get_default_font ##
 
79
 
 
80
.. function:: get_fonts
 
81
 
 
82
   | :sl:`get all available fonts`
 
83
   | :sg:`get_fonts() -> list of strings`
 
84
 
 
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
 
88
   find fonts.
 
89
 
 
90
   .. ## pygame.font.get_fonts ##
 
91
 
 
92
.. function:: match_font
 
93
 
 
94
   | :sl:`find a specific font on the system`
 
95
   | :sg:`match_font(name, bold=False, italic=False) -> path`
 
96
 
 
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.
 
99
 
 
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.
 
102
 
 
103
   Example:
 
104
 
 
105
   ::
 
106
 
 
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)
 
110
 
 
111
   .. ## pygame.font.match_font ##
 
112
 
 
113
.. function:: SysFont
 
114
 
 
115
   | :sl:`create a Font object from the system fonts`
 
116
   | :sg:`SysFont(name, size, bold=False, italic=False) -> Font`
 
117
 
 
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.
 
122
 
 
123
   .. ## pygame.font.SysFont ##
 
124
 
 
125
.. class:: Font
 
126
 
 
127
   | :sl:`create a new Font object from a file`
 
128
   | :sg:`Font(filename, size) -> Font`
 
129
   | :sg:`Font(object, size) -> Font`
 
130
 
 
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
 
135
   changed.
 
136
 
 
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
 
140
   strings or unicode.
 
141
 
 
142
   .. method:: render
 
143
 
 
144
      | :sl:`draw text on a new Surface`
 
145
      | :sg:`render(text, antialias, color, background=None) -> Surface`
 
146
 
 
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.
 
151
 
 
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.
 
161
 
 
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.
 
166
 
 
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.
 
174
 
 
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.
 
180
 
 
181
      If you render '\n' a unknown char will be rendered. Usually a rectangle.
 
182
      Instead you need to handle new lines yourself.
 
183
 
 
184
      Font rendering is not thread safe: only a single thread can render text
 
185
      at any time.
 
186
 
 
187
      .. ## Font.render ##
 
188
 
 
189
   .. method:: size
 
190
 
 
191
      | :sl:`determine the amount of space needed to render text`
 
192
      | :sg:`size(text) -> (width, height)`
 
193
 
 
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.
 
197
 
 
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".
 
201
 
 
202
      .. ## Font.size ##
 
203
 
 
204
   .. method:: set_underline
 
205
 
 
206
      | :sl:`control if text is rendered with an underline`
 
207
      | :sg:`set_underline(bool) -> None`
 
208
 
 
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.
 
212
 
 
213
      .. ## Font.set_underline ##
 
214
 
 
215
   .. method:: get_underline
 
216
 
 
217
      | :sl:`check if text will be rendered with an underline`
 
218
      | :sg:`get_underline() -> bool`
 
219
 
 
220
      Return True when the font underline is enabled.
 
221
 
 
222
      .. ## Font.get_underline ##
 
223
 
 
224
   .. method:: set_bold
 
225
 
 
226
      | :sl:`enable fake rendering of bold text`
 
227
      | :sg:`set_bold(bool) -> None`
 
228
 
 
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.
 
233
 
 
234
      .. ## Font.set_bold ##
 
235
 
 
236
   .. method:: get_bold
 
237
 
 
238
      | :sl:`check if text will be rendered bold`
 
239
      | :sg:`get_bold() -> bool`
 
240
 
 
241
      Return True when the font bold rendering mode is enabled.
 
242
 
 
243
      .. ## Font.get_bold ##
 
244
 
 
245
   .. method:: set_italic
 
246
 
 
247
      | :sl:`enable fake rendering of italic text`
 
248
      | :sg:`set_italic(bool) -> None`
 
249
 
 
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
 
254
      modes.
 
255
 
 
256
      .. ## Font.set_italic ##
 
257
 
 
258
   .. method:: metrics
 
259
 
 
260
      | :sl:`Gets the metrics for each character in the pased string.`
 
261
      | :sg:`metrics(text) -> list`
 
262
 
 
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
 
268
      character.
 
269
 
 
270
      .. ## Font.metrics ##
 
271
 
 
272
   .. method:: get_italic
 
273
 
 
274
      | :sl:`check if the text will be rendered italic`
 
275
      | :sg:`get_italic() -> bool`
 
276
 
 
277
      Return True when the font italic rendering mode is enabled.
 
278
 
 
279
      .. ## Font.get_italic ##
 
280
 
 
281
   .. method:: get_linesize
 
282
 
 
283
      | :sl:`get the line space of the font text`
 
284
      | :sg:`get_linesize() -> int`
 
285
 
 
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
 
288
      between lines.
 
289
 
 
290
      .. ## Font.get_linesize ##
 
291
 
 
292
   .. method:: get_height
 
293
 
 
294
      | :sl:`get the height of the font`
 
295
      | :sg:`get_height() -> int`
 
296
 
 
297
      Return the height in pixels of the actual rendered text. This is the
 
298
      average size for each glyph in the font.
 
299
 
 
300
      .. ## Font.get_height ##
 
301
 
 
302
   .. method:: get_ascent
 
303
 
 
304
      | :sl:`get the ascent of the font`
 
305
      | :sg:`get_ascent() -> int`
 
306
 
 
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.
 
309
 
 
310
      .. ## Font.get_ascent ##
 
311
 
 
312
   .. method:: get_descent
 
313
 
 
314
      | :sl:`get the descent of the font`
 
315
      | :sg:`get_descent() -> int`
 
316
 
 
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.
 
319
 
 
320
      .. ## Font.get_descent ##
 
321
 
 
322
   .. ## pygame.font.Font ##
 
323
 
 
324
.. ## pygame.font ##