Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Font.set_linesize() (TTF 2.24.0 feature) #3282

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 27 additions & 0 deletions src_c/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,32 @@ 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 (PyErr_Occurred())
return NULL;

if (linesize < 0)
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 +1079,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
27 changes: 27 additions & 0 deletions test/font_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,28 @@ def test_get_linesize(self):
self.assertTrue(isinstance(linesize, int))
self.assertTrue(linesize > 0)

@unittest.skipIf(
pygame.font.get_sdl_ttf_version() < (2, 24, 0),
"supported in SDL_ttf 2.24.0 onwards",
)
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 @@ -906,6 +928,11 @@ def test_font_method_should_raise_exception_after_quit(self):
skip_methods.add("set_point_size")
skip_methods.add("point_size")

if version >= (2, 24, 0):
methods.append(("set_linesize", (2,)))
else:
skip_methods.add("set_linesize")

if version < (2, 20, 0):
skip_methods.add("align")

Expand Down
Loading