Skip to content

Commit 4d66f93

Browse files
authored
Merge pull request #7247 from radarhere/getmask2_max_image_pixels
2 parents 0a43254 + 74859e9 commit 4d66f93

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/PIL/ImageFont.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -563,14 +563,21 @@ def getmask2(
563563
if start is None:
564564
start = (0, 0)
565565
im = None
566+
size = None
566567

567-
def fill(mode, size):
568-
nonlocal im
568+
def fill(mode, im_size):
569+
nonlocal im, size
570+
571+
size = im_size
572+
if Image.MAX_IMAGE_PIXELS is not None:
573+
pixels = max(1, size[0]) * max(1, size[1])
574+
if pixels > 2 * Image.MAX_IMAGE_PIXELS:
575+
return
569576

570577
im = Image.core.fill(mode, size)
571578
return im
572579

573-
size, offset = self.font.render(
580+
offset = self.font.render(
574581
text,
575582
fill,
576583
mode,
@@ -582,7 +589,6 @@ def fill(mode, size):
582589
ink,
583590
start[0],
584591
start[1],
585-
Image.MAX_IMAGE_PIXELS,
586592
)
587593
Image._decompression_bomb_check(size)
588594
return im, offset

src/_imagingft.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,6 @@ font_render(FontObject *self, PyObject *args) {
815815
float y_start = 0;
816816
int width, height, x_offset, y_offset;
817817
int horizontal_dir; /* is primary axis horizontal? */
818-
PyObject *max_image_pixels = Py_None;
819818

820819
/* render string into given buffer (the buffer *must* have
821820
the right size, or this will crash) */
@@ -833,8 +832,7 @@ font_render(FontObject *self, PyObject *args) {
833832
&anchor,
834833
&foreground_ink_long,
835834
&x_start,
836-
&y_start,
837-
&max_image_pixels)) {
835+
&y_start)) {
838836
return NULL;
839837
}
840838

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

880878
width += stroke_width * 2 + ceil(x_start);
881879
height += stroke_width * 2 + ceil(y_start);
882-
if (max_image_pixels != Py_None) {
883-
if ((long long)(width > 1 ? width : 1) * (height > 1 ? height : 1) > PyLong_AsLongLong(max_image_pixels) * 2) {
884-
PyMem_Del(glyph_info);
885-
return Py_BuildValue("(ii)(ii)", width, height, 0, 0);
886-
}
887-
}
888-
889880
image = PyObject_CallFunction(fill, "s(ii)", strcmp(mode, "RGBA") == 0 ? "RGBA" : "L", width, height);
890-
if (image == NULL) {
881+
if (image == Py_None) {
882+
PyMem_Del(glyph_info);
883+
return Py_BuildValue("ii", 0, 0);
884+
} else if (image == NULL) {
891885
PyMem_Del(glyph_info);
892886
return NULL;
893887
}
@@ -898,7 +892,7 @@ font_render(FontObject *self, PyObject *args) {
898892
y_offset -= stroke_width;
899893
if (count == 0 || width == 0 || height == 0) {
900894
PyMem_Del(glyph_info);
901-
return Py_BuildValue("(ii)(ii)", width, height, x_offset, y_offset);
895+
return Py_BuildValue("ii", x_offset, y_offset);
902896
}
903897

904898
if (stroke_width) {
@@ -1116,7 +1110,7 @@ font_render(FontObject *self, PyObject *args) {
11161110
Py_DECREF(image);
11171111
FT_Stroker_Done(stroker);
11181112
PyMem_Del(glyph_info);
1119-
return Py_BuildValue("(ii)(ii)", width, height, x_offset, y_offset);
1113+
return Py_BuildValue("ii", x_offset, y_offset);
11201114

11211115
glyph_error:
11221116
if (im->destroy) {

0 commit comments

Comments
 (0)