Replaced __internal__ argument with warning filters

This commit is contained in:
Andrew Murray 2022-07-01 20:33:59 +10:00 committed by Ondrej Baranovič
parent 729fe6f8b0
commit 8a6050ee5b
3 changed files with 70 additions and 69 deletions

View File

@ -32,6 +32,7 @@
import math
import numbers
import warnings
from . import Image, ImageColor
from ._deprecate import deprecate
@ -375,15 +376,16 @@ class ImageDraw:
def _multiline_spacing(self, font, spacing, stroke_width):
# this can be replaced with self.textbbox(...)[3] when textsize is removed
return (
self.textsize(
"A",
font=font,
stroke_width=stroke_width,
__internal__=True,
)[1]
+ spacing
)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
return (
self.textsize(
"A",
font=font,
stroke_width=stroke_width,
)[1]
+ spacing
)
def text(
self,
@ -582,34 +584,34 @@ class ImageDraw:
features=None,
language=None,
stroke_width=0,
__internal__=False,
):
"""Get the size of a given string, in pixels."""
if not __internal__:
deprecate("textsize", 10, "textbbox or textlength")
deprecate("textsize", 10, "textbbox or textlength")
if self._multiline_check(text):
return self.multiline_textsize(
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
return self.multiline_textsize(
text,
font,
spacing,
direction,
features,
language,
stroke_width,
)
if font is None:
font = self.getfont()
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
return font.getsize(
text,
font,
spacing,
direction,
features,
language,
stroke_width,
__internal__=True,
)
if font is None:
font = self.getfont()
return font.getsize(
text,
direction,
features,
language,
stroke_width,
__internal__=True,
)
def multiline_textsize(
self,
text,
@ -619,25 +621,24 @@ class ImageDraw:
features=None,
language=None,
stroke_width=0,
__internal__=False,
):
if not __internal__:
deprecate("multiline_textsize", 10, "multiline_textbbox")
deprecate("multiline_textsize", 10, "multiline_textbbox")
max_width = 0
lines = self._multiline_split(text)
line_spacing = self._multiline_spacing(font, spacing, stroke_width)
for line in lines:
line_width, line_height = self.textsize(
line,
font,
spacing,
direction,
features,
language,
stroke_width,
__internal__=True,
)
max_width = max(max_width, line_width)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
for line in lines:
line_width, line_height = self.textsize(
line,
font,
spacing,
direction,
features,
language,
stroke_width,
)
max_width = max(max_width, line_width)
return max_width, len(lines) * line_spacing - spacing
def textlength(
@ -662,14 +663,15 @@ class ImageDraw:
return font.getlength(text, mode, direction, features, language)
except AttributeError:
deprecate("textlength support for fonts without getlength", 10)
size = self.textsize(
text,
font,
direction=direction,
features=features,
language=language,
__internal__=True,
)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
size = self.textsize(
text,
font,
direction=direction,
features=features,
language=language,
)
if direction == "ttb":
return size[1]
return size[0]

View File

@ -24,6 +24,8 @@
"""
import warnings
from . import Image, ImageColor, ImageDraw, ImageFont, ImagePath
from ._deprecate import deprecate
@ -180,7 +182,9 @@ class Draw:
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textsize`
"""
deprecate("textsize", 10, "textbbox or textlength")
return self.draw.textsize(text, font=font.font, __internal__=True)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
return self.draw.textsize(text, font=font.font)
def textbbox(self, xy, text, font):
"""

View File

@ -147,8 +147,7 @@ class ImageFont:
:return: (width, height)
"""
if not kwargs.get("__internal__"):
deprecate("getsize", 10, "getbbox or getlength")
deprecate("getsize", 10, "getbbox or getlength")
return self.font.getsize(text)
def getmask(self, text, mode="", *args, **kwargs):
@ -425,7 +424,6 @@ class FreeTypeFont:
features=None,
language=None,
stroke_width=0,
__internal__=False,
):
"""
.. deprecated:: 9.2.0
@ -479,8 +477,7 @@ class FreeTypeFont:
:return: (width, height)
"""
if not __internal__:
deprecate("getsize", 10, "getbbox or getlength")
deprecate("getsize", 10, "getbbox or getlength")
# vertical offset is added for historical reasons
# see https://github.com/python-pillow/Pillow/pull/4910#discussion_r486682929
size, offset = self.font.getsize(text, "L", direction, features, language)
@ -545,14 +542,14 @@ class FreeTypeFont:
deprecate("getsize_multiline", 10, "ImageDraw.multiline_textbbox")
max_width = 0
lines = self._multiline_split(text)
line_spacing = (
self.getsize("A", stroke_width=stroke_width, __internal__=True)[1] + spacing
)
for line in lines:
line_width, line_height = self.getsize(
line, direction, features, language, stroke_width, __internal__=True
)
max_width = max(max_width, line_width)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
line_spacing = self.getsize("A", stroke_width=stroke_width)[1] + spacing
for line in lines:
line_width, line_height = self.getsize(
line, direction, features, language, stroke_width
)
max_width = max(max_width, line_width)
return max_width, len(lines) * line_spacing - spacing
@ -856,11 +853,9 @@ class TransposedFont:
Use :py:meth:`.getbbox` or :py:meth:`.getlength` instead.
"""
if not kwargs.get("__internal__"):
deprecate("getsize", 10, "getbbox or getlength")
try:
w, h = self.font.getsize(text, __internal__=True)
except TypeError:
deprecate("getsize", 10, "getbbox or getlength")
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
w, h = self.font.getsize(text)
if self.orientation in (Image.Transpose.ROTATE_90, Image.Transpose.ROTATE_270):
return h, w