Skip to content

Commit 751a410

Browse files
authored
Merge pull request #3282 from itzpr3d4t0r/add-set_linesize
Add `Font.set_linesize()` (TTF 2.24.0 feature)
2 parents fc36cd5 + ed59a94 commit 751a410

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
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: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,19 @@ solves no longer exists, it will likely be removed in the future.
514514

515515
.. ## Font.get_linesize ##
516516
517+
.. method:: set_linesize
518+
519+
| :sl:`set the line space of the font text`
520+
| :sg:`set_linesize(linesize) -> None`
521+
522+
Set the height in pixels for a line of text with the font. When rendering
523+
multiple lines of text this refers to the amount of space between lines.
524+
The value must be non-negative.
525+
526+
.. versionadded:: 2.5.4
527+
528+
.. ## Font.set_linesize ##
529+
517530
.. method:: get_height
518531

519532
| :sl:`get the height of the font`
@@ -527,7 +540,7 @@ solves no longer exists, it will likely be removed in the future.
527540
.. method:: set_point_size
528541

529542
| :sl:`set the point size of the font`
530-
| :sg:`set_point_size(size, /) -> int`
543+
| :sg:`set_point_size(size, /) -> None`
531544
532545
Sets the point size of the font, which is the value that was used to
533546
initialize this font.

src_c/doc/font_doc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
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) -> None\nset the line space of the font text"
3233
#define DOC_FONT_FONT_GETHEIGHT "get_height() -> int\nget the height of the font"
33-
#define DOC_FONT_FONT_SETPOINTSIZE "set_point_size(size, /) -> int\nset the point size of the font"
34+
#define DOC_FONT_FONT_SETPOINTSIZE "set_point_size(size, /) -> None\nset the point size of the font"
3435
#define DOC_FONT_FONT_GETPOINTSIZE "get_point_size() -> int\nget the point size of the font"
3536
#define DOC_FONT_FONT_GETASCENT "get_ascent() -> int\nget the ascent of the font"
3637
#define DOC_FONT_FONT_GETDESCENT "get_descent() -> int\nget the descent of the font"

src_c/font.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,38 @@ font_get_linesize(PyObject *self, PyObject *_null)
226226
#endif
227227
}
228228

229+
static PyObject *
230+
font_set_linesize(PyObject *self, PyObject *arg)
231+
{
232+
if (!PgFont_GenerationCheck(self)) {
233+
return RAISE_FONT_QUIT_ERROR();
234+
}
235+
236+
#if SDL_TTF_VERSION_ATLEAST(2, 24, 0)
237+
TTF_Font *font = PyFont_AsFont(self);
238+
239+
if (!PyLong_Check(arg)) {
240+
return RAISE(PyExc_TypeError, "linesize must be an integer");
241+
}
242+
int linesize = PyLong_AsLong(arg);
243+
if (linesize == -1 && PyErr_Occurred()) {
244+
return NULL;
245+
}
246+
247+
if (linesize < 0) {
248+
return RAISE(PyExc_ValueError, "linesize must be >= 0");
249+
}
250+
251+
TTF_SetFontLineSkip(font, linesize);
252+
253+
Py_RETURN_NONE;
254+
#else
255+
return RAISE(
256+
PyExc_NotImplementedError,
257+
"TTF_SetFontLineSkip is not available in this version of SDL_ttf");
258+
#endif
259+
}
260+
229261
static PyObject *
230262
_font_get_style_flag_as_py_bool(PyObject *self, int flag)
231263
{
@@ -1148,6 +1180,7 @@ static PyMethodDef font_methods[] = {
11481180
{"get_ascent", font_get_ascent, METH_NOARGS, DOC_FONT_FONT_GETASCENT},
11491181
{"get_linesize", font_get_linesize, METH_NOARGS,
11501182
DOC_FONT_FONT_GETLINESIZE},
1183+
{"set_linesize", font_set_linesize, METH_O, DOC_FONT_FONT_SETLINESIZE},
11511184
{"get_bold", font_get_bold, METH_NOARGS, DOC_FONT_FONT_GETBOLD},
11521185
{"set_bold", font_set_bold, METH_O, DOC_FONT_FONT_SETBOLD},
11531186
{"get_italic", font_get_italic, METH_NOARGS, DOC_FONT_FONT_GETITALIC},

test/font_test.py

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

388+
@unittest.skipIf(
389+
pygame.font.get_sdl_ttf_version() < (2, 24, 0),
390+
"supported in SDL_ttf 2.24.0 onwards",
391+
)
392+
def test_set_linesize(self):
393+
if pygame_font.__name__ == "pygame.ftfont":
394+
return # not a pygame.ftfont thing
395+
396+
f = pygame_font.Font(None, 20)
397+
linesize = f.get_linesize()
398+
399+
# check increasing linesize
400+
f.set_linesize(linesize + 1)
401+
self.assertEqual(f.get_linesize(), linesize + 1)
402+
403+
# check random linesize
404+
expected_linesizes = [30, 1, 22, 34, 5, 10, 0]
405+
for expected_size in expected_linesizes:
406+
f.set_linesize(expected_size)
407+
self.assertEqual(f.get_linesize(), expected_size)
408+
409+
# check invalid linesize
410+
with self.assertRaises(ValueError):
411+
f.set_linesize(-1)
412+
with self.assertRaises(OverflowError):
413+
f.set_linesize(2**100)
414+
with self.assertRaises(TypeError):
415+
f.set_linesize(12.0)
416+
388417
def test_metrics(self):
389418
# Ensure bytes decoding works correctly. Can only compare results
390419
# with unicode for now.
@@ -912,6 +941,11 @@ def test_font_method_should_raise_exception_after_quit(self):
912941
skip_methods.add("set_point_size")
913942
skip_methods.add("point_size")
914943

944+
if version >= (2, 24, 0):
945+
methods.append(("set_linesize", (2,)))
946+
else:
947+
skip_methods.add("set_linesize")
948+
915949
if version < (2, 20, 0):
916950
skip_methods.add("align")
917951

0 commit comments

Comments
 (0)