aports/testing/py3-blockdiag/0001-Add-support-for-Pillow-10.patch
2024-07-05 13:34:45 +00:00

120 lines
4.6 KiB
Diff

From 744232a4c2719c344c8210516515b65b705d4aaf Mon Sep 17 00:00:00 2001
From: Wenlong Zhang <zhangwenlong@loongson.cn>
Date: Fri, 5 Jul 2024 09:48:48 +0000
Subject: [PATCH] Add support for Pillow 10
backport from upstream:https://github.com/blockdiag/blockdiag/pull/179
---
src/blockdiag/imagedraw/png.py | 48 +++++++++++++++++++++++++++-------
1 file changed, 38 insertions(+), 10 deletions(-)
diff --git a/src/blockdiag/imagedraw/png.py b/src/blockdiag/imagedraw/png.py
index 3cac05a..35b541e 100644
--- a/src/blockdiag/imagedraw/png.py
+++ b/src/blockdiag/imagedraw/png.py
@@ -29,6 +29,20 @@ from blockdiag.utils import XY, Box, Size, images
from blockdiag.utils.fontmap import FontMap, parse_fontpath
from blockdiag.utils.myitertools import istep, stepslice
+# to support pillow < 9.1.0
+if not hasattr(Image, 'Resampling'):
+ from enum import IntEnum
+
+ class Resampling(IntEnum):
+ NEAREST = 0
+ BOX = 4
+ BILINEAR = 2
+ HAMMING = 5
+ BICUBIC = 3
+ LANCZOS = 1
+
+ Image.Resampling = Resampling
+
def point_pairs(xylist):
iterable = iter(xylist)
@@ -147,7 +161,7 @@ class ImageDrawExBase(base.ImageDraw):
self.draw = ImageDraw.Draw(self._image)
def resizeCanvas(self, size):
- self._image = self._image.resize(size, Image.ANTIALIAS)
+ self._image = self._image.resize(size, Image.Resampling.LANCZOS)
self.draw = ImageDraw.Draw(self._image)
def arc(self, box, start, end, **kwargs):
@@ -273,13 +287,20 @@ class ImageDrawExBase(base.ImageDraw):
def textlinesize(self, string, font):
ttfont = ttfont_for(font)
if ttfont is None:
- size = self.draw.textsize(string, font=None)
-
+ if hasattr(self.draw, 'textbbox'):
+ left, top, right, bottom = self.draw.textbbox((0, 0), string)
+ size = (right - left, bottom - top)
+ else:
+ size = self.draw.textsize(string, font=None)
font_ratio = font.size * 1.0 / FontMap.BASE_FONTSIZE
size = Size(int(size[0] * font_ratio),
int(size[1] * font_ratio))
else:
- size = Size(*ttfont.getsize(string))
+ if hasattr(ttfont, 'getbbox'):
+ left, top, right, bottom = ttfont.getbbox(string)
+ size = Size(right - left, bottom - top)
+ else:
+ size = Size(*ttfont.getsize(string))
return size
@@ -291,7 +312,11 @@ class ImageDrawExBase(base.ImageDraw):
if self.scale_ratio == 1 and font.size == FontMap.BASE_FONTSIZE:
self.draw.text(xy, string, fill=fill)
else:
- size = self.draw.textsize(string)
+ if hasattr(self.draw, 'textbbox'):
+ left, top, right, bottom = self.draw.textbbox((0, 0), string)
+ size = (right - left, bottom - top)
+ else:
+ size = self.draw.textsize(string)
image = Image.new('RGBA', size)
draw = ImageDraw.Draw(image)
draw.text((0, 0), string, fill=fill)
@@ -299,11 +324,14 @@ class ImageDrawExBase(base.ImageDraw):
basesize = (size[0] * self.scale_ratio,
size[1] * self.scale_ratio)
- text_image = image.resize(basesize, Image.ANTIALIAS)
+ text_image = image.resize(basesize, Image.Resampling.LANCZOS)
self.paste(text_image, xy, text_image)
else:
- size = ttfont.getsize(string)
-
+ if hasattr(ttfont, 'getbbox'):
+ left, top, right, bottom = ttfont.getbbox(string)
+ size = (right - left, bottom - top)
+ else:
+ size = ttfont.getsize(string)
# Generate mask to support BDF(bitmap font)
mask = Image.new('1', size)
draw = ImageDraw.Draw(mask)
@@ -370,7 +398,7 @@ class ImageDrawExBase(base.ImageDraw):
# resize image.
w = min([box.width, image.size[0] * self.scale_ratio])
h = min([box.height, image.size[1] * self.scale_ratio])
- image.thumbnail((w, h), Image.ANTIALIAS)
+ image.thumbnail((w, h), Image.Resampling.LANCZOS)
# centering image.
w, h = image.size
@@ -404,7 +432,7 @@ class ImageDrawExBase(base.ImageDraw):
y = int(self._image.size[1] / self.scale_ratio)
size = (x, y)
- self._image.thumbnail(size, Image.ANTIALIAS)
+ self._image.thumbnail(size, Image.Resampling.LANCZOS)
if self.filename:
self._image.save(self.filename, _format)
--
2.45.2