Skip to content

Commit

Permalink
Merge pull request #7247 from radarhere/getmask2_max_image_pixels
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Oct 5, 2023
2 parents 0a43254 + 74859e9 commit 4d66f93
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
14 changes: 10 additions & 4 deletions src/PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,14 +563,21 @@ def getmask2(
if start is None:
start = (0, 0)
im = None
size = None

def fill(mode, size):
nonlocal im
def fill(mode, im_size):
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)
return im

size, offset = self.font.render(
offset = self.font.render(
text,
fill,
mode,
Expand All @@ -582,7 +589,6 @@ def fill(mode, size):
ink,
start[0],
start[1],
Image.MAX_IMAGE_PIXELS,
)
Image._decompression_bomb_check(size)
return im, offset
Expand Down
20 changes: 7 additions & 13 deletions src/_imagingft.c
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,6 @@ font_render(FontObject *self, PyObject *args) {
float y_start = 0;
int width, height, x_offset, y_offset;
int horizontal_dir; /* is primary axis horizontal? */
PyObject *max_image_pixels = Py_None;

/* render string into given buffer (the buffer *must* have
the right size, or this will crash) */
Expand All @@ -833,8 +832,7 @@ font_render(FontObject *self, PyObject *args) {
&anchor,
&foreground_ink_long,
&x_start,
&y_start,
&max_image_pixels)) {
&y_start)) {
return NULL;
}

Expand Down Expand Up @@ -879,15 +877,11 @@ font_render(FontObject *self, PyObject *args) {

width += stroke_width * 2 + ceil(x_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);
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);
return NULL;
}
Expand All @@ -898,7 +892,7 @@ font_render(FontObject *self, PyObject *args) {
y_offset -= stroke_width;
if (count == 0 || width == 0 || height == 0) {
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) {
Expand Down Expand Up @@ -1116,7 +1110,7 @@ font_render(FontObject *self, PyObject *args) {
Py_DECREF(image);
FT_Stroker_Done(stroker);
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:
if (im->destroy) {
Expand Down

0 comments on commit 4d66f93

Please sign in to comment.