Moved MAX_IMAGE_PIXELS check to Python

This commit is contained in:
Andrew Murray 2023-07-01 20:52:44 +10:00
parent c827f3b30f
commit 74859e965d
2 changed files with 17 additions and 17 deletions

View File

@ -563,14 +563,21 @@ class FreeTypeFont:
if start is None: if start is None:
start = (0, 0) start = (0, 0)
im = None im = None
size = None
def fill(mode, size): def fill(mode, im_size):
nonlocal im nonlocal im, size
size = im_size
if Image.MAX_IMAGE_PIXELS is not None:
pixels = max(1, size[0]) * max(1, size[1])
if pixels > 2 * Image.MAX_IMAGE_PIXELS:
return
im = Image.core.fill(mode, size) im = Image.core.fill(mode, size)
return im return im
size, offset = self.font.render( offset = self.font.render(
text, text,
fill, fill,
mode, mode,
@ -582,7 +589,6 @@ class FreeTypeFont:
ink, ink,
start[0], start[0],
start[1], start[1],
Image.MAX_IMAGE_PIXELS,
) )
Image._decompression_bomb_check(size) Image._decompression_bomb_check(size)
return im, offset return im, offset

View File

@ -815,7 +815,6 @@ font_render(FontObject *self, PyObject *args) {
float y_start = 0; float y_start = 0;
int width, height, x_offset, y_offset; int width, height, x_offset, y_offset;
int horizontal_dir; /* is primary axis horizontal? */ int horizontal_dir; /* is primary axis horizontal? */
PyObject *max_image_pixels = Py_None;
/* render string into given buffer (the buffer *must* have /* render string into given buffer (the buffer *must* have
the right size, or this will crash) */ the right size, or this will crash) */
@ -833,8 +832,7 @@ font_render(FontObject *self, PyObject *args) {
&anchor, &anchor,
&foreground_ink_long, &foreground_ink_long,
&x_start, &x_start,
&y_start, &y_start)) {
&max_image_pixels)) {
return NULL; return NULL;
} }
@ -879,15 +877,11 @@ font_render(FontObject *self, PyObject *args) {
width += stroke_width * 2 + ceil(x_start); width += stroke_width * 2 + ceil(x_start);
height += stroke_width * 2 + ceil(y_start); height += stroke_width * 2 + ceil(y_start);
if (max_image_pixels != Py_None) {
if ((long long)(width > 1 ? width : 1) * (height > 1 ? height : 1) > PyLong_AsLongLong(max_image_pixels) * 2) {
PyMem_Del(glyph_info);
return Py_BuildValue("(ii)(ii)", width, height, 0, 0);
}
}
image = PyObject_CallFunction(fill, "s(ii)", strcmp(mode, "RGBA") == 0 ? "RGBA" : "L", width, height); image = PyObject_CallFunction(fill, "s(ii)", strcmp(mode, "RGBA") == 0 ? "RGBA" : "L", width, height);
if (image == NULL) { if (image == Py_None) {
PyMem_Del(glyph_info);
return Py_BuildValue("ii", 0, 0);
} else if (image == NULL) {
PyMem_Del(glyph_info); PyMem_Del(glyph_info);
return NULL; return NULL;
} }
@ -898,7 +892,7 @@ font_render(FontObject *self, PyObject *args) {
y_offset -= stroke_width; y_offset -= stroke_width;
if (count == 0 || width == 0 || height == 0) { if (count == 0 || width == 0 || height == 0) {
PyMem_Del(glyph_info); PyMem_Del(glyph_info);
return Py_BuildValue("(ii)(ii)", width, height, x_offset, y_offset); return Py_BuildValue("ii", x_offset, y_offset);
} }
if (stroke_width) { if (stroke_width) {
@ -1116,7 +1110,7 @@ font_render(FontObject *self, PyObject *args) {
Py_DECREF(image); Py_DECREF(image);
FT_Stroker_Done(stroker); FT_Stroker_Done(stroker);
PyMem_Del(glyph_info); PyMem_Del(glyph_info);
return Py_BuildValue("(ii)(ii)", width, height, x_offset, y_offset); return Py_BuildValue("ii", x_offset, y_offset);
glyph_error: glyph_error:
if (im->destroy) { if (im->destroy) {