Skip to content

Commit 27f46c5

Browse files
committed
Add Font.set_linesize() (TTF 2.24.0 feature)
1 parent b6d2987 commit 27f46c5

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

buildconfig/stubs/pygame/font.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class Font:
7979
) -> list[tuple[int, int, int, int, int]]: ...
8080
def get_italic(self) -> bool: ...
8181
def get_linesize(self) -> int: ...
82+
def set_linesize(self, linesize: int, /) -> None: ...
8283
def get_height(self) -> int: ...
8384
def get_ascent(self) -> int: ...
8485
def get_descent(self) -> int: ...

docs/reST/ref/font.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,16 @@ solves no longer exists, it will likely be removed in the future.
511511

512512
.. ## Font.get_linesize ##
513513
514+
.. method:: set_linesize
515+
516+
| :sl:`set the line space of the font text`
517+
| :sg:`set_linesize(linesize) -> int`
518+
519+
Set the height in pixels for a line of text with the font. When rendering
520+
multiple lines of text this refers to the amount of space between lines.
521+
522+
.. ## Font.set_linesize ##
523+
514524
.. method:: get_height
515525

516526
| :sl:`get the height of the font`

src_c/doc/font_doc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#define DOC_FONT_FONT_METRICS "metrics(text, /) -> list\ngets the metrics for each character in the passed string"
3030
#define DOC_FONT_FONT_GETITALIC "get_italic() -> bool\ncheck if the text will be rendered italic"
3131
#define DOC_FONT_FONT_GETLINESIZE "get_linesize() -> int\nget the line space of the font text"
32+
#define DOC_FONT_FONT_SETLINESIZE "set_linesize(linesize) -> int\nset the line space of the font text"
3233
#define DOC_FONT_FONT_GETHEIGHT "get_height() -> int\nget the height of the font"
3334
#define DOC_FONT_FONT_SETPOINTSIZE "set_point_size(size, /) -> int\nset the point size of the font"
3435
#define DOC_FONT_FONT_GETPOINTSIZE "get_point_size() -> int\nget the point size of the font"

src_c/font.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,29 @@ font_get_linesize(PyObject *self, PyObject *_null)
205205
return PyLong_FromLong(TTF_FontLineSkip(font));
206206
}
207207

208+
static PyObject *
209+
font_set_linesize(PyObject *self, PyObject *arg)
210+
{
211+
if (!PgFont_GenerationCheck(self)) {
212+
return RAISE_FONT_QUIT_ERROR();
213+
}
214+
215+
#if SDL_TTF_VERSION_ATLEAST(2, 24, 0)
216+
TTF_Font *font = PyFont_AsFont(self);
217+
int linesize = PyLong_AsLong(arg);
218+
if (linesize < 0 || PyErr_Occurred()) {
219+
return RAISE(PyExc_ValueError, "linesize must be >= 0");
220+
}
221+
TTF_SetFontLineSkip(font, linesize);
222+
223+
Py_RETURN_NONE;
224+
#else
225+
return RAISE(
226+
PyExc_NotImplementedError,
227+
"TTF_SetFontLineSkip is not available in this version of SDL_ttf");
228+
#endif
229+
}
230+
208231
static PyObject *
209232
_font_get_style_flag_as_py_bool(PyObject *self, int flag)
210233
{
@@ -1053,6 +1076,7 @@ static PyMethodDef font_methods[] = {
10531076
{"get_ascent", font_get_ascent, METH_NOARGS, DOC_FONT_FONT_GETASCENT},
10541077
{"get_linesize", font_get_linesize, METH_NOARGS,
10551078
DOC_FONT_FONT_GETLINESIZE},
1079+
{"set_linesize", font_set_linesize, METH_O, DOC_FONT_FONT_SETLINESIZE},
10561080
{"get_bold", font_get_bold, METH_NOARGS, DOC_FONT_FONT_GETBOLD},
10571081
{"set_bold", font_set_bold, METH_O, DOC_FONT_FONT_SETBOLD},
10581082
{"get_italic", font_get_italic, METH_NOARGS, DOC_FONT_FONT_GETITALIC},

test/font_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,24 @@ def test_get_linesize(self):
385385
self.assertTrue(isinstance(linesize, int))
386386
self.assertTrue(linesize > 0)
387387

388+
def test_set_linesize(self):
389+
f = pygame_font.Font(None, 20)
390+
linesize = f.get_linesize()
391+
392+
# check increasing linesize
393+
f.set_linesize(linesize + 1)
394+
self.assertEqual(f.get_linesize(), linesize + 1)
395+
396+
# check random linesize
397+
expected_linesizes = [30, 1, 22, 34, 5, 10, 0]
398+
for expected_size in expected_linesizes:
399+
f.set_linesize(expected_size)
400+
self.assertEqual(f.get_linesize(), expected_size)
401+
402+
# check invalid linesize
403+
with self.assertRaises(ValueError):
404+
f.set_linesize(-1)
405+
388406
def test_metrics(self):
389407
# Ensure bytes decoding works correctly. Can only compare results
390408
# with unicode for now.
@@ -867,6 +885,7 @@ def test_font_method_should_raise_exception_after_quit(self):
867885
("get_height", ()),
868886
("get_italic", ()),
869887
("get_linesize", ()),
888+
("set_linesize", (2,)),
870889
("get_sized_descender", ()),
871890
("get_underline", ()),
872891
("metrics", ("any text",)),
@@ -882,6 +901,7 @@ def test_font_method_should_raise_exception_after_quit(self):
882901
("get_descent", ()),
883902
("get_ascent", ()),
884903
("get_linesize", ()),
904+
("set_linesize", (2,)),
885905
("get_bold", ()),
886906
("set_bold", (True,)),
887907
("get_italic", ()),

0 commit comments

Comments
 (0)