Skip to content

Commit

Permalink
Replaced __internal__ argument with warning filters
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere authored and nulano committed Jul 1, 2022
1 parent 729fe6f commit 8a6050e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 69 deletions.
102 changes: 52 additions & 50 deletions src/PIL/ImageDraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import math
import numbers
import warnings

from . import Image, ImageColor
from ._deprecate import deprecate
Expand Down Expand Up @@ -375,15 +376,16 @@ def _multiline_split(self, text):

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,
Expand Down Expand Up @@ -582,34 +584,34 @@ def textsize(
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,
Expand All @@ -619,25 +621,24 @@ def multiline_textsize(
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(
Expand All @@ -662,14 +663,15 @@ def textlength(
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]
Expand Down
6 changes: 5 additions & 1 deletion src/PIL/ImageDraw2.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"""


import warnings

from . import Image, ImageColor, ImageDraw, ImageFont, ImagePath
from ._deprecate import deprecate

Expand Down Expand Up @@ -180,7 +182,9 @@ def textsize(self, text, font):
.. 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):
"""
Expand Down
31 changes: 13 additions & 18 deletions src/PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ def getsize(self, text, *args, **kwargs):
: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):
Expand Down Expand Up @@ -425,7 +424,6 @@ def getsize(
features=None,
language=None,
stroke_width=0,
__internal__=False,
):
"""
.. deprecated:: 9.2.0
Expand Down Expand Up @@ -479,8 +477,7 @@ def getsize(
: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)
Expand Down Expand Up @@ -545,14 +542,14 @@ def getsize_multiline(
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

Expand Down Expand Up @@ -856,11 +853,9 @@ def getsize(self, text, *args, **kwargs):
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
Expand Down

0 comments on commit 8a6050e

Please sign in to comment.