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 full unicode support to Font.metrics #3328

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions docs/reST/ref/font.rst
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,9 @@ solves no longer exists, it will likely be removed in the future.
advance), ...]. None is entered in the list for each unrecognized
character.

.. versionchanged:: 2.5.4 This function now supports all unicode codepoints.
Previously, only a subset that was representable in UCS-2 was supported.

.. ## Font.metrics ##

.. method:: get_italic
Expand Down
51 changes: 22 additions & 29 deletions src_c/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,83 +832,76 @@ font_metrics(PyObject *self, PyObject *textobj)

TTF_Font *font = PyFont_AsFont(self);
PyObject *list;
Py_ssize_t length;
Py_ssize_t i;
int minx;
int maxx;
int miny;
int maxy;
int advance;
PyObject *obj;
PyObject *listitem;
Uint16 *buffer;
Uint16 ch;
PyObject *temp;
int surrogate;
Py_UCS4 *buffer;
Py_UCS4 ch;
if (!PgFont_GenerationCheck(self)) {
return RAISE_FONT_QUIT_ERROR();
}

if (PyUnicode_Check(textobj)) {
obj = textobj;
Py_INCREF(obj);
Py_INCREF(textobj);
}
else if (PyBytes_Check(textobj)) {
obj = PyUnicode_FromEncodedObject(textobj, "UTF-8", NULL);
if (!obj) {
textobj = PyUnicode_FromEncodedObject(textobj, "UTF-8", NULL);
if (!textobj) {
return NULL;
}
}
else {
return RAISE_TEXT_TYPE_ERROR();
}
temp = PyUnicode_AsUTF16String(obj);
Py_DECREF(obj);
if (!temp)
buffer = PyUnicode_AsUCS4Copy(textobj);
Py_DECREF(textobj);
if (!buffer)
return NULL;
obj = temp;

list = PyList_New(0);
if (!list) {
Py_DECREF(obj);
PyMem_Free(buffer);
return NULL;
}
buffer = (Uint16 *)PyBytes_AS_STRING(obj);
length = PyBytes_GET_SIZE(obj) / sizeof(Uint16);
for (i = 1 /* skip BOM */; i < length; i++) {
ch = buffer[i];
surrogate = Py_UNICODE_IS_SURROGATE(ch);
for (i = 0; (ch = buffer[i]); i++) {
/* TODO:
* TTF_GlyphMetrics() seems to return a value for any character,
* using the default invalid character, if the char is not found.
*/
if (!surrogate && /* conditional and */
!TTF_GlyphMetrics(font, (Uint16)ch, &minx, &maxx, &miny, &maxy,
&advance)) {
#if SDL_TTF_VERSION_ATLEAST(2, 0, 18)
if (!TTF_GlyphMetrics32(font, ch, &minx, &maxx, &miny, &maxy,
&advance))
#else
if (ch <= 0xFFFF && !TTF_GlyphMetrics(font, (Uint16)ch, &minx, &maxx,
&miny, &maxy, &advance))
#endif
{
listitem =
Py_BuildValue("(iiiii)", minx, maxx, miny, maxy, advance);
if (!listitem) {
Py_DECREF(list);
Py_DECREF(obj);
PyMem_Free(buffer);
return NULL;
}
}
else {
/* Not UCS-2 or no matching metrics. */
/* Not UCS-2 (and old SDL) or no matching metrics. */
Py_INCREF(Py_None);
listitem = Py_None;
if (surrogate)
i++;
}
if (0 != PyList_Append(list, listitem)) {
Py_DECREF(list);
Py_DECREF(listitem);
Py_DECREF(obj);
PyMem_Free(buffer);
return NULL; /* Exception already set. */
}
Py_DECREF(listitem);
}
Py_DECREF(obj);
PyMem_Free(buffer);
return list;
}

Expand Down
8 changes: 7 additions & 1 deletion test/font_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,13 @@ def test_metrics(self):
bm = f.metrics(u)

self.assertEqual(len(bm), 1)
self.assertIsNone(bm[0])
if (
pygame.font.get_sdl_ttf_version() >= (2, 0, 18)
and pygame_font.__name__ != "pygame.ftfont"
):
self.assertIsNotNone(bm[0])
else:
self.assertIsNone(bm[0])

return # unfinished
# The documentation is useless here. How large a list?
Expand Down
Loading