Skip to content

Commit 7b88eba

Browse files
authored
Add defaults to enable passing no arguments when creating a pygame.Font() (#1853)
undefined
1 parent c0d09f7 commit 7b88eba

File tree

6 files changed

+46
-6
lines changed

6 files changed

+46
-6
lines changed

buildconfig/stubs/pygame/font.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Font:
3333
underline: bool
3434
strikethrough: bool
3535
align: int
36-
def __init__(self, name: Optional[FileArg], size: int) -> None: ...
36+
def __init__(self, filename: Optional[FileArg] = None, size: int = 20) -> None: ...
3737
def render(
3838
self,
3939
text: Union[str, bytes, None],

docs/reST/ref/font.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ solves no longer exists, it will likely be removed in the future.
159159
.. class:: Font
160160

161161
| :sl:`create a new Font object from a file`
162+
| :sg:`Font(filename=None, size=20) -> Font`
162163
| :sg:`Font(filename, size) -> Font`
163164
| :sg:`Font(pathlib.Path, size) -> Font`
164165
| :sg:`Font(object, size) -> Font`
@@ -173,6 +174,9 @@ solves no longer exists, it will likely be removed in the future.
173174
render can emulate bold or italic features, but it is better to load from a
174175
font with actual italic or bold glyphs.
175176

177+
.. versionchanged:: 2.1.4 If no arguments are given then the default font will be used and
178+
a font size of 20 is used.
179+
176180
.. versionchanged:: 2.1.4 This class is also available through the ``pygame.Font``
177181
alias.
178182

src_c/doc/font_doc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#define DOC_PYGAMEFONTGETFONTS "get_fonts() -> list of strings\nget all available fonts"
99
#define DOC_PYGAMEFONTMATCHFONT "match_font(name, bold=False, italic=False) -> path\nfind a specific font on the system"
1010
#define DOC_PYGAMEFONTSYSFONT "SysFont(name, size, bold=False, italic=False) -> Font\ncreate a Font object from the system fonts"
11-
#define DOC_PYGAMEFONTFONT "Font(filename, size) -> Font\nFont(pathlib.Path, size) -> Font\nFont(object, size) -> Font\ncreate a new Font object from a file"
11+
#define DOC_PYGAMEFONTFONT "Font(filename=None, size=20) -> Font\nFont(filename, size) -> Font\nFont(pathlib.Path, size) -> Font\nFont(object, size) -> Font\ncreate a new Font object from a file"
1212
#define DOC_FONTBOLD "bold -> bool\nGets or sets whether the font should be rendered in (faked) bold."
1313
#define DOC_FONTITALIC "italic -> bool\nGets or sets whether the font should be rendered in (faked) italics."
1414
#define DOC_FONTUNDERLINE "underline -> bool\nGets or sets whether the font should be rendered with an underline."
@@ -73,6 +73,7 @@ pygame.font.SysFont
7373
create a Font object from the system fonts
7474
7575
pygame.font.Font
76+
Font(filename=None, size=20) -> Font
7677
Font(filename, size) -> Font
7778
Font(pathlib.Path, size) -> Font
7879
Font(object, size) -> Font

src_c/font.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ static const char pkgdatamodule_name[] = "pygame.pkgdata";
7171
static const char resourcefunc_name[] = "getResource";
7272
#endif
7373
static const char font_defaultname[] = "freesansbold.ttf";
74+
static const int font_defaultsize = 20;
7475

7576
#ifndef SDL_TTF_VERSION_ATLEAST
7677
/**
@@ -889,13 +890,16 @@ font_dealloc(PyFontObject *self)
889890
static int
890891
font_init(PyFontObject *self, PyObject *args, PyObject *kwds)
891892
{
892-
int fontsize;
893+
int fontsize = font_defaultsize;
893894
TTF_Font *font = NULL;
894-
PyObject *obj;
895+
PyObject *obj = Py_None;
895896
SDL_RWops *rw;
896897

898+
static char *kwlist[] = {"filename", "size", NULL};
899+
897900
self->font = NULL;
898-
if (!PyArg_ParseTuple(args, "Oi", &obj, &fontsize)) {
901+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi", kwlist, &obj,
902+
&fontsize)) {
899903
return -1;
900904
}
901905

src_py/ftfont.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Font(_Font):
3434
__unull = "\x00"
3535
__bnull = b"\x00"
3636

37-
def __init__(self, file, size=-1):
37+
def __init__(self, file=None, size=20):
3838
size = max(size, 1)
3939
if isinstance(file, str):
4040
try:

test/font_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ def setUp(self):
299299
def tearDown(self):
300300
pygame_font.quit()
301301

302+
def test_default_parameters(self):
303+
f = pygame_font.Font()
304+
302305
def test_font_alias(self):
303306
"""Check if pygame.Font is present and the correct type."""
304307
self.assertIs(pygame.Font, pygame.font.Font)
@@ -561,12 +564,27 @@ def test_load_from_file(self):
561564
)
562565
f = pygame_font.Font(font_path, 20)
563566

567+
def test_load_from_file_default(self):
568+
font_name = pygame_font.get_default_font()
569+
font_path = os.path.join(
570+
os.path.split(pygame.__file__)[0], pygame_font.get_default_font()
571+
)
572+
f = pygame_font.Font(font_path)
573+
564574
def test_load_from_pathlib(self):
565575
font_name = pygame_font.get_default_font()
566576
font_path = os.path.join(
567577
os.path.split(pygame.__file__)[0], pygame_font.get_default_font()
568578
)
569579
f = pygame_font.Font(pathlib.Path(font_path), 20)
580+
f = pygame_font.Font(pathlib.Path(font_path))
581+
582+
def test_load_from_pathlib_default(self):
583+
font_name = pygame_font.get_default_font()
584+
font_path = os.path.join(
585+
os.path.split(pygame.__file__)[0], pygame_font.get_default_font()
586+
)
587+
f = pygame_font.Font(pathlib.Path(font_path))
570588

571589
def test_load_from_file_obj(self):
572590
font_name = pygame_font.get_default_font()
@@ -576,11 +594,24 @@ def test_load_from_file_obj(self):
576594
with open(font_path, "rb") as f:
577595
font = pygame_font.Font(f, 20)
578596

597+
def test_load_from_file_obj_default(self):
598+
font_name = pygame_font.get_default_font()
599+
font_path = os.path.join(
600+
os.path.split(pygame.__file__)[0], pygame_font.get_default_font()
601+
)
602+
with open(font_path, "rb") as f:
603+
font = pygame_font.Font(f)
604+
579605
def test_load_default_font_filename(self):
580606
# In font_init, a special case is when the filename argument is
581607
# identical to the default font file name.
582608
f = pygame_font.Font(pygame_font.get_default_font(), 20)
583609

610+
def test_load_default_font_filename_default(self):
611+
# In font_init, a special case is when the filename argument is
612+
# identical to the default font file name.
613+
f = pygame_font.Font(pygame_font.get_default_font())
614+
584615
def _load_unicode(self, path):
585616
import shutil
586617

0 commit comments

Comments
 (0)