Skip to content

Commit

Permalink
Add Font.set_linesize() (TTF 2.24.0 feature)
Browse files Browse the repository at this point in the history
  • Loading branch information
itzpr3d4t0r committed Jan 5, 2025
1 parent e5e2e5f commit c3122ec
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions buildconfig/stubs/pygame/font.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Font:
) -> list[tuple[int, int, int, int, int]]: ...
def get_italic(self) -> bool: ...
def get_linesize(self) -> int: ...
def set_linesize(self, linesize: int, /) -> None: ...
def get_height(self) -> int: ...
def get_ascent(self) -> int: ...
def get_descent(self) -> int: ...
Expand Down
10 changes: 10 additions & 0 deletions docs/reST/ref/font.rst
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,16 @@ solves no longer exists, it will likely be removed in the future.

.. ## Font.get_linesize ##
.. method:: set_linesize

| :sl:`set the line space of the font text`
| :sg:`set_linesize(linesize) -> int`
Set the height in pixels for a line of text with the font. When rendering
multiple lines of text this refers to the amount of space between lines.

.. ## Font.set_linesize ##
.. method:: get_height

| :sl:`get the height of the font`
Expand Down
1 change: 1 addition & 0 deletions src_c/doc/font_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#define DOC_FONT_FONT_METRICS "metrics(text, /) -> list\ngets the metrics for each character in the passed string"
#define DOC_FONT_FONT_GETITALIC "get_italic() -> bool\ncheck if the text will be rendered italic"
#define DOC_FONT_FONT_GETLINESIZE "get_linesize() -> int\nget the line space of the font text"
#define DOC_FONT_FONT_SETLINESIZE "set_linesize(linesize) -> int\nset the line space of the font text"
#define DOC_FONT_FONT_GETHEIGHT "get_height() -> int\nget the height of the font"
#define DOC_FONT_FONT_SETPOINTSIZE "set_point_size(size, /) -> int\nset the point size of the font"
#define DOC_FONT_FONT_GETPOINTSIZE "get_point_size() -> int\nget the point size of the font"
Expand Down
24 changes: 24 additions & 0 deletions src_c/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,29 @@ font_get_linesize(PyObject *self, PyObject *_null)
return PyLong_FromLong(TTF_FontLineSkip(font));
}

static PyObject *
font_set_linesize(PyObject *self, PyObject *arg)
{
if (!PgFont_GenerationCheck(self)) {
return RAISE_FONT_QUIT_ERROR();
}

#if SDL_TTF_VERSION_ATLEAST(2, 24, 0)
TTF_Font *font = PyFont_AsFont(self);
int linesize = PyLong_AsLong(arg);
if (linesize < 0 || PyErr_Occurred()) {
return RAISE(PyExc_ValueError, "linesize must be >= 0");
}
TTF_SetFontLineSkip(font, linesize);

Py_RETURN_NONE;
#else
return RAISE(
PyExc_NotImplementedError,
"TTF_SetFontLineSkip is not available in this version of SDL_ttf");
#endif
}

static PyObject *
_font_get_style_flag_as_py_bool(PyObject *self, int flag)
{
Expand Down Expand Up @@ -1053,6 +1076,7 @@ static PyMethodDef font_methods[] = {
{"get_ascent", font_get_ascent, METH_NOARGS, DOC_FONT_FONT_GETASCENT},
{"get_linesize", font_get_linesize, METH_NOARGS,
DOC_FONT_FONT_GETLINESIZE},
{"set_linesize", font_set_linesize, METH_O, DOC_FONT_FONT_SETLINESIZE},
{"get_bold", font_get_bold, METH_NOARGS, DOC_FONT_FONT_GETBOLD},
{"set_bold", font_set_bold, METH_O, DOC_FONT_FONT_SETBOLD},
{"get_italic", font_get_italic, METH_NOARGS, DOC_FONT_FONT_GETITALIC},
Expand Down
20 changes: 20 additions & 0 deletions test/font_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,24 @@ def test_get_linesize(self):
self.assertTrue(isinstance(linesize, int))
self.assertTrue(linesize > 0)

def test_set_linesize(self):
f = pygame_font.Font(None, 20)
linesize = f.get_linesize()

# check increasing linesize
f.set_linesize(linesize + 1)
self.assertEqual(f.get_linesize(), linesize + 1)

# check random linesize
expected_linesizes = [30, 1, 22, 34, 5, 10, 0]
for expected_size in expected_linesizes:
f.set_linesize(expected_size)
self.assertEqual(f.get_linesize(), expected_size)

# check invalid linesize
with self.assertRaises(ValueError):
f.set_linesize(-1)

def test_metrics(self):
# Ensure bytes decoding works correctly. Can only compare results
# with unicode for now.
Expand Down Expand Up @@ -867,6 +885,7 @@ def test_font_method_should_raise_exception_after_quit(self):
("get_height", ()),
("get_italic", ()),
("get_linesize", ()),
("set_linesize", (2,)),
("get_sized_descender", ()),
("get_underline", ()),
("metrics", ("any text",)),
Expand All @@ -882,6 +901,7 @@ def test_font_method_should_raise_exception_after_quit(self):
("get_descent", ()),
("get_ascent", ()),
("get_linesize", ()),
("set_linesize", (2,)),
("get_bold", ()),
("set_bold", (True,)),
("get_italic", ()),
Expand Down

0 comments on commit c3122ec

Please sign in to comment.