Skip to content

Commit f71cfec

Browse files
authored
Merge pull request #7645 from nulano/font-bomb
Simplify FreeTypeFont.render
2 parents 865a23a + 30015f6 commit f71cfec

File tree

2 files changed

+7
-24
lines changed

2 files changed

+7
-24
lines changed

src/PIL/ImageFont.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -584,22 +584,13 @@ def getmask2(
584584
_string_length_check(text)
585585
if start is None:
586586
start = (0, 0)
587-
im = None
588-
size = None
589587

590588
def fill(width, height):
591-
nonlocal im, size
592-
593589
size = (width, height)
594-
if Image.MAX_IMAGE_PIXELS is not None:
595-
pixels = max(1, width) * max(1, height)
596-
if pixels > 2 * Image.MAX_IMAGE_PIXELS:
597-
return
598-
599-
im = Image.core.fill("RGBA" if mode == "RGBA" else "L", size)
600-
return im
590+
Image._decompression_bomb_check(size)
591+
return Image.core.fill("RGBA" if mode == "RGBA" else "L", size)
601592

602-
offset = self.font.render(
593+
return self.font.render(
603594
text,
604595
fill,
605596
mode,
@@ -612,8 +603,6 @@ def fill(width, height):
612603
start[0],
613604
start[1],
614605
)
615-
Image._decompression_bomb_check(size)
616-
return im, offset
617606

618607
def font_variant(
619608
self, font=None, size=None, index=None, encoding=None, layout_engine=None

src/_imagingft.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ font_render(FontObject *self, PyObject *args) {
880880
image = PyObject_CallFunction(fill, "ii", width, height);
881881
if (image == Py_None) {
882882
PyMem_Del(glyph_info);
883-
return Py_BuildValue("ii", 0, 0);
883+
return Py_BuildValue("N(ii)", image, 0, 0);
884884
} else if (image == NULL) {
885885
PyMem_Del(glyph_info);
886886
return NULL;
@@ -894,7 +894,7 @@ font_render(FontObject *self, PyObject *args) {
894894
y_offset -= stroke_width;
895895
if (count == 0 || width == 0 || height == 0) {
896896
PyMem_Del(glyph_info);
897-
return Py_BuildValue("ii", x_offset, y_offset);
897+
return Py_BuildValue("N(ii)", image, x_offset, y_offset);
898898
}
899899

900900
if (stroke_width) {
@@ -1130,18 +1130,12 @@ font_render(FontObject *self, PyObject *args) {
11301130
if (bitmap_converted_ready) {
11311131
FT_Bitmap_Done(library, &bitmap_converted);
11321132
}
1133-
Py_DECREF(image);
11341133
FT_Stroker_Done(stroker);
11351134
PyMem_Del(glyph_info);
1136-
return Py_BuildValue("ii", x_offset, y_offset);
1135+
return Py_BuildValue("N(ii)", image, x_offset, y_offset);
11371136

11381137
glyph_error:
1139-
if (im->destroy) {
1140-
im->destroy(im);
1141-
}
1142-
if (im->image) {
1143-
free(im->image);
1144-
}
1138+
Py_DECREF(image);
11451139
if (stroker != NULL) {
11461140
FT_Done_Glyph(glyph);
11471141
}

0 commit comments

Comments
 (0)