Skip to content

Commit db136b7

Browse files
committed
Fix x0>x1 error for rect fills, per new Pillow
Pillow 9.5 throws an error when you try to draw a rectangle where x0>x1 or top>bottom. For rects with small width or height but thick strokes, we ended up hitting that error because we were trying to subtract the stroke width from the area to be filled.
1 parent cf17ad1 commit db136b7

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

pdfplumber/display.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,13 @@ def draw_rect(
231231

232232
x0, top, x1, bottom = bbox
233233
half = stroke_width / 2
234-
x0 += half
235-
top += half
236-
x1 -= half
237-
bottom -= half
234+
x0 = min(x0 + half, (x0 + x1) / 2)
235+
top = min(top + half, (top + bottom) / 2)
236+
x1 = max(x1 - half, (x0 + x1) / 2)
237+
bottom = max(bottom - half, (top + bottom) / 2)
238238

239-
self.draw.rectangle(
240-
self._reproject_bbox((x0, top, x1, bottom)), fill, COLORS.TRANSPARENT
241-
)
239+
fill_bbox = self._reproject_bbox((x0, top, x1, bottom))
240+
self.draw.rectangle(fill_bbox, fill, COLORS.TRANSPARENT)
242241

243242
if stroke_width > 0:
244243
segments = [

0 commit comments

Comments
 (0)