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

View File

@ -24,6 +24,8 @@
""" """
import warnings
from . import Image, ImageColor, ImageDraw, ImageFont, ImagePath from . import Image, ImageColor, ImageDraw, ImageFont, ImagePath
from ._deprecate import deprecate from ._deprecate import deprecate
@ -180,7 +182,9 @@ class Draw:
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textsize` .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textsize`
""" """
deprecate("textsize", 10, "textbbox or textlength") 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): def textbbox(self, xy, text, font):
""" """

View File

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