Skip to content

Commit d48453d

Browse files
authored
Rewrite and re-document the BitmapFont class (#208)
* Rewrite and redocument the BitmapFont class * Document exception types for BitmapFont rendering
1 parent ba1adee commit d48453d

File tree

4 files changed

+390
-172
lines changed

4 files changed

+390
-172
lines changed

doc/modules/ext/bitmapfont.rst

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,8 @@
1-
Basic Bitmap Font Rendering
2-
===========================
1+
`sdl2.ext.bitmapfont` - Basic Bitmap Font Rendering
2+
===================================================
33

44
This module provides the :class:`~sdl2.ext.BitmapFont` class, which allows for
55
basic font rendering in PySDL2 without depending on the SDL_ttf library.
66

7-
8-
.. class:: BitmapFont(surface : Sprite, size : iterable[, mapping=None)
9-
10-
A bitmap graphics to character mapping. The :class:`BitmapFont` class
11-
uses an image *surface* to find and render font character glyphs for
12-
text. It requires a mapping table, which denotes the characters
13-
available on the image.
14-
15-
The mapping table is a list of strings, where each string reflects a
16-
*line* of characters on the image. Each character within each line
17-
has the same size as specified by the size argument.
18-
19-
A typical mapping table might look like ::
20-
21-
[ '0123456789',
22-
'ABCDEFGHIJ',
23-
'KLMNOPQRST',
24-
'UVWXYZ ',
25-
'abcdefghij',
26-
'klmnopqrst',
27-
'uvwxyz ',
28-
',;.:!?+-()' ]
29-
30-
.. attribute:: surface
31-
32-
The :class:`sdl2.SDL_Surface` containing the character bitmaps.
33-
34-
.. attribute:: offsets
35-
36-
A dict containing the character offsets on the :attr:`surface`.
37-
38-
.. attribute:: mapping
39-
40-
The character mapping table, a list of strings.
41-
42-
.. attribute:: size
43-
44-
The size of an individual glyph bitmap on the font.
45-
46-
.. method:: render(text : string[, bpp=None]) -> Sprite
47-
48-
Renders the passed text on a new :class:`Sprite` and returns it.
49-
If no explicit *bpp* are provided, the bpp settings of the
50-
:attr:`.surface` are used.
51-
52-
.. method:: render_on(surface : Sprite, text : string[, \
53-
offset=(0, 0)]) -> (int, int, int, int)
54-
55-
Renders a text on the passed sprite, starting at a specific
56-
offset. The top-left start position of the text will be the
57-
passed *offset* and a 4-value tuple with the changed area will be
58-
returned.
59-
60-
.. method:: contains(c : string) -> bool
61-
62-
Checks, whether a certain character exists in the font.
63-
64-
.. method:: can_render(text : string) -> bool
65-
66-
Checks, whether all characters in the passed *text* can be rendered.
7+
.. automodule:: sdl2.ext.bitmapfont
8+
:members:

doc/news.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ New Features:
5757
when using SDL2 2.0.10 or newer (PR #207)
5858
* Added :meth:`sdl2.ext.Renderer.blit` as an alias for the
5959
:meth:`sdl2.ext.Renderer.copy` method (PR #207)
60+
* Added a new method :meth:`~sdl2.ext.BitmapFont.remap` to the
61+
:obj:`~sdl2.ext.BitmapFont` class to allow specifying custom character
62+
widths and heights for each mapped character in a bitmap font (PR #208)
63+
* Added a new argument ``line_h`` to :meth:`sdl2.ext.BitmapFont.render_on` to
64+
allow specifying custom line heights (PR #208)
6065

6166
Fixed Bugs:
6267

@@ -73,6 +78,14 @@ Fixed Bugs:
7378
try to unlock RLE surfaces once their corresponding view objects are deleted.
7479
This prevents a segmentation fault when a view is garbage-collected but the
7580
surface has already been freed (PR #204)
81+
* Fixed a bug where the rectangle returned by
82+
:meth:`sdl2.ext.BitmapFont.render_on` would overestimate the size of the
83+
rendered text by one character in both width and height (PR #208)
84+
* :meth:`sdl2.ext.BitmapFont.contains` no longer assumes that the font map
85+
contains a space (PR #208)
86+
* Rendering multiline text with the :class:`sdl2.ext.BitmapFont` class now
87+
always splits lines using the newline (`\n`) character. Previously on
88+
Windows, it would only split on Windows-style line endings (`\r\n`) (PR #208)
7689

7790
API Changes:
7891

@@ -113,6 +126,11 @@ Deprecation Notices:
113126
instead (PR #205)
114127
* The :func:`sdl2.ext.get_image_formats` function has been deprecated, as it
115128
gives inaccurate results in most cases (PR #205)
129+
* The :meth:`sdl2.ext.BitmapFont.can_render` method has been deprecated (PR #208)
130+
* The :meth:`sdl2.ext.BitmapFont.render` method has been deprecated in favor of
131+
:meth:`sdl2.ext.BitmapFont.render_text`, which returns an SDL surface instead
132+
of a SoftwareSprite and ensures the output surface is in ARGB8888 format by
133+
default (PR #208)
116134

117135

118136
0.9.9

0 commit comments

Comments
 (0)