From 6aba1df7275598a811643e7fcc2be4e5a1dfa4a0 Mon Sep 17 00:00:00 2001 From: nulano Date: Sun, 7 Apr 2019 21:56:57 +0200 Subject: [PATCH] update Py_UNICODE to Py_UCS4 --- src/_imagingft.c | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/_imagingft.c b/src/_imagingft.c index f6bd787ef..291d745d7 100644 --- a/src/_imagingft.c +++ b/src/_imagingft.c @@ -326,6 +326,7 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw) static int font_getchar(PyObject* string, int index, FT_ULong* char_out) { +#if PY_VERSION_HEX < 0x03000000 if (PyUnicode_Check(string)) { Py_UNICODE* p = PyUnicode_AS_UNICODE(string); int size = PyUnicode_GET_SIZE(string); @@ -335,7 +336,6 @@ font_getchar(PyObject* string, int index, FT_ULong* char_out) return 1; } -#if PY_VERSION_HEX < 0x03000000 if (PyString_Check(string)) { unsigned char* p = (unsigned char*) PyString_AS_STRING(string); int size = PyString_GET_SIZE(string); @@ -344,6 +344,13 @@ font_getchar(PyObject* string, int index, FT_ULong* char_out) *char_out = (unsigned char) p[index]; return 1; } +#else + if (PyUnicode_Check(string)) { + if (index >= PyUnicode_GetLength(string)) + return 0; + *char_out = PyUnicode_ReadChar(string, index); + return 1; + } #endif return 0; @@ -365,6 +372,7 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir, PyObject * goto failed; } +#if PY_VERSION_HEX < 0x03000000 if (PyUnicode_Check(string)) { Py_UNICODE *text = PyUnicode_AS_UNICODE(string); Py_ssize_t size = PyUnicode_GET_SIZE(string); @@ -384,9 +392,7 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir, PyObject * } } - } -#if PY_VERSION_HEX < 0x03000000 - else if (PyString_Check(string)) { + } else if (PyString_Check(string)) { char *text = PyString_AS_STRING(string); int size = PyString_GET_SIZE(string); if (! size) { @@ -403,6 +409,28 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir, PyObject * } } } +#else + if (PyUnicode_Check(string)) { + Py_UCS4 *text = PyUnicode_AsUCS4Copy(string); + Py_ssize_t size = PyUnicode_GetLength(string); + if (!text || !size) { + /* return 0 and clean up, no glyphs==no size, + and raqm fails with empty strings */ + goto failed; + } + int set_text = (*p_raqm.set_text)(rq, (const uint32_t *)(text), size); + PyMem_Free(text); + if (!set_text) { + PyErr_SetString(PyExc_ValueError, "raqm_set_text() failed"); + goto failed; + } + if (lang) { + if (!(*p_raqm.set_language)(rq, lang, start, size)) { + PyErr_SetString(PyExc_ValueError, "raqm_set_language() failed"); + goto failed; + } + } + } #endif else { PyErr_SetString(PyExc_TypeError, "expected string");