C89 / Lint / Raqm<0.9.0 fixes

This commit is contained in:
nulano 2023-02-02 00:56:51 +00:00 committed by Nulano
parent 910511606b
commit 3b3cac84a8
2 changed files with 52 additions and 36 deletions

View File

@ -726,7 +726,8 @@ class FreeTypeFontFamily:
# example: # example:
# from PIL import Image, ImageDraw, ImageFont # from PIL import Image, ImageDraw, ImageFont
# le = ImageFont.Layout.RAQM # le = ImageFont.Layout.RAQM
# f1 = ImageFont.truetype(r"C:\Users\Nulano\AppData\Local\Microsoft\Windows\Fonts\SCRIPTIN.ttf", 24) # f1 = ImageFont.truetype(
# r"C:\Users\Nulano\AppData\Local\Microsoft\Windows\Fonts\SCRIPTIN.ttf", 24)
# f2 = ImageFont.truetype("segoeui.ttf", 24) # f2 = ImageFont.truetype("segoeui.ttf", 24)
# f3 = ImageFont.truetype("seguisym.ttf", 24) # f3 = ImageFont.truetype("seguisym.ttf", 24)
# ff = ImageFont.FreeTypeFontFamily(f1, f2, f3, layout_engine=le) # ff = ImageFont.FreeTypeFontFamily(f1, f2, f3, layout_engine=le)

View File

@ -254,7 +254,7 @@ getfont(PyObject *self_, PyObject *args, PyObject *kw) {
static PyObject * static PyObject *
getfamily(PyObject *self_, PyObject *args, PyObject *kw) { getfamily(PyObject *self_, PyObject *args, PyObject *kw) {
/* create a font family object from a list of file names and a sizes (in pixels) */ /* create a font family object from a list of file names and a sizes (in pixels) */
int i, j;
FontFamilyObject *self; FontFamilyObject *self;
FontFamily *family; FontFamily *family;
int error = 0; int error = 0;
@ -299,7 +299,7 @@ getfamily(PyObject *self_, PyObject *args, PyObject *kw) {
return NULL; return NULL;
} }
for (int i = 0; i < family->font_count; i++) { for (i = 0; i < family->font_count; i++) {
char *filename; char *filename;
Py_ssize_t size; Py_ssize_t size;
Py_ssize_t index; Py_ssize_t index;
@ -369,11 +369,12 @@ getfamily(PyObject *self_, PyObject *args, PyObject *kw) {
geterror(error); geterror(error);
goto err; goto err;
} }
}
continue; return (PyObject *)self;
err: err:
for (int j = 0; j < i; j++) { for (j = 0; j < i; j++) {
if (family->faces[j]) { if (family->faces[j]) {
FT_Done_Face(family->faces[j]); FT_Done_Face(family->faces[j]);
} }
@ -385,9 +386,6 @@ getfamily(PyObject *self_, PyObject *args, PyObject *kw) {
return NULL; return NULL;
} }
return (PyObject *)self;
}
#ifdef HAVE_RAQM #ifdef HAVE_RAQM
static size_t static size_t
@ -399,8 +397,9 @@ text_layout_raqm(
const char *lang, const char *lang,
GlyphInfo **glyph_info GlyphInfo **glyph_info
) { ) {
size_t i = 0, count = 0, start = 0; int face = 0;
raqm_t *rq; size_t i = 0, j = 0, count = 0, start = 0;
raqm_t *rq = NULL;
raqm_glyph_t *glyphs = NULL; raqm_glyph_t *glyphs = NULL;
raqm_direction_t direction; raqm_direction_t direction;
@ -409,12 +408,6 @@ text_layout_raqm(
Py_ssize_t size; Py_ssize_t size;
int *fallback = NULL; int *fallback = NULL;
rq = raqm_create();
if (rq == NULL) {
PyErr_SetString(PyExc_ValueError, "raqm_create() failed.");
goto failed;
}
if (PyUnicode_Check(string)) { if (PyUnicode_Check(string)) {
text = PyUnicode_AsUCS4Copy(string); text = PyUnicode_AsUCS4Copy(string);
size = PyUnicode_GET_LENGTH(string); size = PyUnicode_GET_LENGTH(string);
@ -466,13 +459,29 @@ text_layout_raqm(
goto failed; goto failed;
} }
fallback[0] = -1; fallback[0] = -1;
for (size_t j = 1; j < size; j++) { for (j = 1; j < size; j++) {
fallback[j] = -2; fallback[j] = -2;
} }
} }
for (int face = 0; face < family->font_count; face++) { for (face = 0; face < family->font_count; face++) {
#ifdef RAQM_VERSION_ATLEAST
#if RAQM_VERSION_ATLEAST(0, 9, 0)
if (face >= 1) {
raqm_clear_contents(rq); raqm_clear_contents(rq);
} else
#endif
#endif
{
if (rq != NULL) {
raqm_destroy(rq);
}
rq = raqm_create();
if (rq == NULL) {
PyErr_SetString(PyExc_ValueError, "raqm_create() failed.");
goto failed;
}
}
int set_text = text != NULL ? raqm_set_text(rq, text, size) : raqm_set_text_utf8(rq, buffer, size); int set_text = text != NULL ? raqm_set_text(rq, text, size) : raqm_set_text_utf8(rq, buffer, size);
if (!set_text) { if (!set_text) {
@ -480,6 +489,7 @@ text_layout_raqm(
goto failed; goto failed;
} }
if (lang) { if (lang) {
start = 0;
if (!raqm_set_language(rq, lang, start, size)) { if (!raqm_set_language(rq, lang, start, size)) {
PyErr_SetString(PyExc_ValueError, "raqm_set_language() failed"); PyErr_SetString(PyExc_ValueError, "raqm_set_language() failed");
goto failed; goto failed;
@ -535,7 +545,7 @@ text_layout_raqm(
} }
} else { } else {
start = 0; start = 0;
for (size_t j = 0; j <= size; j++) { for (j = 0; j <= size; j++) {
if (j < size) { if (j < size) {
if (fallback[j] == -2) { if (fallback[j] == -2) {
/* not a cluster boundary */ /* not a cluster boundary */
@ -573,14 +583,14 @@ text_layout_raqm(
break; break;
} }
for (size_t j = 1; j < size; j++) { for (j = 1; j < size; j++) {
if (fallback[j] == -1) { if (fallback[j] == -1) {
fallback[j] = -2; fallback[j] = -2;
} }
} }
int missing = 0; int missing = 0;
for (size_t j = 0; j < count; j++) { for (j = 0; j < count; j++) {
int cluster = glyphs[j].cluster; int cluster = glyphs[j].cluster;
if (glyphs[j].index == 0) { if (glyphs[j].index == 0) {
/* cluster contains missing glyph */ /* cluster contains missing glyph */
@ -622,11 +632,15 @@ text_layout_raqm(
} }
failed: failed:
if (text) {
PyMem_Free(text); PyMem_Free(text);
}
if (fallback) { if (fallback) {
PyMem_Free(fallback); PyMem_Free(fallback);
} }
if (rq != NULL) {
raqm_destroy(rq); raqm_destroy(rq);
}
return count; return count;
} }
@ -643,7 +657,7 @@ text_layout_fallback(
int mask, int mask,
int color int color
) { ) {
int error, load_flags, i; int error, load_flags, i, j;
char *buffer = NULL; char *buffer = NULL;
FT_ULong ch; FT_ULong ch;
Py_ssize_t count; Py_ssize_t count;
@ -687,7 +701,7 @@ text_layout_fallback(
ch = PyUnicode_READ_CHAR(string, i); ch = PyUnicode_READ_CHAR(string, i);
} }
int found = 0; int found = 0;
for (int j = 0; !found && j < family->font_count; j++) { for (j = 0; !found && j < family->font_count; j++) {
FT_Face face = family->faces[j]; FT_Face face = family->faces[j];
(*glyph_info)[i].index = FT_Get_Char_Index(face, ch); (*glyph_info)[i].index = FT_Get_Char_Index(face, ch);
if ((*glyph_info)[i].index != 0) { if ((*glyph_info)[i].index != 0) {
@ -1855,7 +1869,8 @@ static PyTypeObject Font_Type = {
static void static void
family_dealloc(FontFamilyObject *self) { family_dealloc(FontFamilyObject *self) {
for (int i = 0; i < self->data.font_count; i++) { int i;
for (i = 0; i < self->data.font_count; i++) {
if (self->data.faces[i]) { if (self->data.faces[i]) {
FT_Done_Face(self->data.faces[i]); FT_Done_Face(self->data.faces[i]);
} }