Skip to content

Commit

Permalink
Fix x0>x1 error for rect fills, per new Pillow
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jsvine committed Apr 8, 2023
1 parent cf17ad1 commit db136b7
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions pdfplumber/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,13 @@ def draw_rect(

x0, top, x1, bottom = bbox
half = stroke_width / 2
x0 += half
top += half
x1 -= half
bottom -= half
x0 = min(x0 + half, (x0 + x1) / 2)
top = min(top + half, (top + bottom) / 2)
x1 = max(x1 - half, (x0 + x1) / 2)
bottom = max(bottom - half, (top + bottom) / 2)

self.draw.rectangle(
self._reproject_bbox((x0, top, x1, bottom)), fill, COLORS.TRANSPARENT
)
fill_bbox = self._reproject_bbox((x0, top, x1, bottom))
self.draw.rectangle(fill_bbox, fill, COLORS.TRANSPARENT)

if stroke_width > 0:
segments = [
Expand Down

0 comments on commit db136b7

Please sign in to comment.