Skip to content

Commit 2f93a16

Browse files
authored
Added keyword arguments to Font.render (#2000)
* Added kwargs to font.render * Added docs and tests for kwargs for font.render * Fix incompatible pointer * Added wraplength to font.render * Attempt to fix failing test * Remove changes to ftfont.py * Change a test to only run for FontTest class * Fix formatting issue * Adjusted keyword arg names for font.render and updated docs * Fix documentation error * Changed arguments fgcolor and bgcolor in ftfont * Adjusted tests to include freetype font * Adjusted formatting * Fixed failing test in font_test * Fixing font_test again * Fixing font_test yet again * Fix formatting * Renamed fgcolor to color * Fixed remaining mentions of background
1 parent 88c3685 commit 2f93a16

File tree

6 files changed

+44
-13
lines changed

6 files changed

+44
-13
lines changed

buildconfig/stubs/pygame/font.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Font:
4040
text: Union[str, bytes, None],
4141
antialias: bool,
4242
color: ColorValue,
43-
background: Optional[ColorValue] = None,
43+
bgcolor: Optional[ColorValue] = None,
4444
wraplength: int = 0
4545
) -> Surface: ...
4646
def size(self, text: Union[str, bytes]) -> Tuple[int, int]: ...

docs/reST/ref/font.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ solves no longer exists, it will likely be removed in the future.
278278
.. method:: render
279279

280280
| :sl:`draw text on a new Surface`
281-
| :sg:`render(text, antialias, color, background=None, wraplength=0) -> Surface`
281+
| :sg:`render(text, antialias, color, bgcolor=None, wraplength=0) -> Surface`
282282
283283
This creates a new Surface with the specified text rendered on it.
284284
:mod:`pygame.font` provides no way to directly draw text on an existing
@@ -292,9 +292,9 @@ solves no longer exists, it will likely be removed in the future.
292292
UCS-4 range are supported. For char strings a ``LATIN1`` encoding is
293293
assumed. The antialias argument is a boolean: if True the characters
294294
will have smooth edges. The color argument is the color of the text
295-
[e.g.: (0,0,255) for blue]. The optional background argument is a color
296-
to use for the text background. If no background is passed the area
297-
outside the text will be transparent.
295+
[e.g.: (0,0,255) for blue]. The optional bgcolor argument is a color
296+
to use for the text background. If bgcolor is ``None`` the area outside
297+
the text will be transparent.
298298

299299
The `wraplength` argument describes the width (in pixels) a line of text
300300
should be before wrapping to a new line. See
@@ -332,6 +332,8 @@ solves no longer exists, it will likely be removed in the future.
332332

333333
.. versionadded:: 2.1.4 wraplength parameter
334334

335+
.. versionchanged:: 2.3.0 now supports keyword arguments.
336+
335337
.. ## Font.render ##
336338
337339
.. method:: size

src_c/doc/font_doc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#define DOC_FONT_FONT_UNDERLINE "underline -> bool\nGets or sets whether the font should be rendered with an underline."
1616
#define DOC_FONT_FONT_STRIKETHROUGH "strikethrough -> bool\nGets or sets whether the font should be rendered with a strikethrough."
1717
#define DOC_FONT_FONT_ALIGN "align -> int\nSet how rendered text is aligned when given a wrap length"
18-
#define DOC_FONT_FONT_RENDER "render(text, antialias, color, background=None, wraplength=0) -> Surface\ndraw text on a new Surface"
18+
#define DOC_FONT_FONT_RENDER "render(text, antialias, color, bgcolor=None, wraplength=0) -> Surface\ndraw text on a new Surface"
1919
#define DOC_FONT_FONT_SIZE "size(text) -> (width, height)\ndetermine the amount of space needed to render text"
2020
#define DOC_FONT_FONT_SETUNDERLINE "set_underline(bool) -> None\ncontrol if text is rendered with an underline"
2121
#define DOC_FONT_FONT_GETUNDERLINE "get_underline() -> bool\ncheck if text will be rendered with an underline"

src_c/font.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ font_set_strikethrough(PyObject *self, PyObject *arg)
475475
}
476476

477477
static PyObject *
478-
font_render(PyObject *self, PyObject *args)
478+
font_render(PyObject *self, PyObject *args, PyObject *kwds)
479479
{
480480
TTF_Font *font = PyFont_AsFont(self);
481481
int antialias;
@@ -486,8 +486,12 @@ font_render(PyObject *self, PyObject *args)
486486
const char *astring = "";
487487
int wraplength = 0;
488488

489-
if (!PyArg_ParseTuple(args, "OpO|Oi", &text, &antialias, &fg_rgba_obj,
490-
&bg_rgba_obj, &wraplength)) {
489+
static char *kwlist[] = {"text", "antialias", "color",
490+
"bgcolor", "wraplength", NULL};
491+
492+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OpO|Oi", kwlist, &text,
493+
&antialias, &fg_rgba_obj, &bg_rgba_obj,
494+
&wraplength)) {
491495
return NULL;
492496
}
493497

@@ -871,7 +875,8 @@ static PyMethodDef font_methods[] = {
871875
{"set_strikethrough", font_set_strikethrough, METH_O,
872876
DOC_FONT_FONT_SETSTRIKETHROUGH},
873877
{"metrics", font_metrics, METH_O, DOC_FONT_FONT_METRICS},
874-
{"render", font_render, METH_VARARGS, DOC_FONT_FONT_RENDER},
878+
{"render", (PyCFunction)font_render, METH_VARARGS | METH_KEYWORDS,
879+
DOC_FONT_FONT_RENDER},
875880
{"size", font_size, METH_O, DOC_FONT_FONT_SIZE},
876881
{"set_script", font_set_script, METH_O, DOC_FONT_FONT_SETSCRIPT},
877882
{"set_direction", (PyCFunction)font_set_direction,

src_py/ftfont.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __init__(self, file=None, size=20):
5959
self.ucs4 = True
6060
self.underline_adjustment = 1.0
6161

62-
def render(self, text, antialias, color, background=None):
62+
def render(self, text, antialias, color, bgcolor=None):
6363
"""render(text, antialias, color, background=None) -> Surface
6464
draw text on a new Surface"""
6565

@@ -74,7 +74,7 @@ def render(self, text, antialias, color, background=None):
7474
)
7575
self.antialiased = bool(antialias)
7676
try:
77-
s, _ = super().render(text, color, background)
77+
s, _ = super().render(text, color, bgcolor)
7878
return s
7979
finally:
8080
self.antialiased = save_antialiased

test/font_test.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import pygame
1010
from pygame import font as pygame_font # So font can be replaced with ftfont
1111

12-
1312
FONTDIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures", "fonts")
1413

1514

@@ -245,6 +244,31 @@ def test_render_args(self):
245244
self.assertEqual(tuple(screen.get_at((0, 0)))[:3], (255, 255, 255))
246245
self.assertEqual(tuple(screen.get_at(font_rect.topleft))[:3], (255, 255, 255))
247246

247+
# ftfont and font render with different arguments
248+
if pygame_font.__name__ == "pygame.font":
249+
font_surface = f.render(
250+
text=" bar",
251+
antialias=True,
252+
color=(0, 0, 0),
253+
bgcolor=(255, 255, 255),
254+
wraplength=0,
255+
)
256+
else:
257+
font_surface = f.render(
258+
text=" bar",
259+
antialias=True,
260+
color=(0, 0, 0),
261+
bgcolor=(255, 255, 255),
262+
)
263+
screen.fill((10, 10, 10))
264+
font_rect = font_surface.get_rect()
265+
font_rect.topleft = rect.topleft
266+
self.assertTrue(font_surface)
267+
screen.blit(font_surface, font_rect, font_rect)
268+
pygame.display.update()
269+
self.assertEqual(tuple(screen.get_at((0, 0)))[:3], (255, 255, 255))
270+
self.assertEqual(tuple(screen.get_at(font_rect.topleft))[:3], (255, 255, 255))
271+
248272
# If we don't have a real display, don't do this test.
249273
# Transparent background doesn't seem to work without a read video card.
250274
if os.environ.get("SDL_VIDEODRIVER") != "dummy":

0 commit comments

Comments
 (0)