Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced __internal__ argument with module attribute #15

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
117 changes: 66 additions & 51 deletions src/PIL/ImageDraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
import math
import numbers

from . import Image, ImageColor
from ._deprecate import deprecate
from . import Image, ImageColor, _deprecate

"""
A simple 2D drawing interface for PIL images.
Expand Down Expand Up @@ -375,15 +374,19 @@ 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
)
_deprecate._suppress_internal_deprecations = True
try:
multiline_spacing = (
self.textsize(
"A",
font=font,
stroke_width=stroke_width,
)[1]
+ spacing
)
finally:
_deprecate._suppress_internal_deprecations = False
return multiline_spacing

def text(
self,
Expand Down Expand Up @@ -582,33 +585,40 @@ 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")
if not _deprecate._suppress_internal_deprecations:
_deprecate.deprecate("textsize", 10, "textbbox or textlength")
if self._multiline_check(text):
return self.multiline_textsize(
_deprecate._suppress_internal_deprecations = True
try:
size = self.multiline_textsize(
text,
font,
spacing,
direction,
features,
language,
stroke_width,
)
finally:
_deprecate._suppress_internal_deprecations = False
return size

if font is None:
font = self.getfont()
_deprecate._suppress_internal_deprecations = True
try:
size = 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,
)
finally:
_deprecate._suppress_internal_deprecations = False
return size

def multiline_textsize(
self,
Expand All @@ -619,24 +629,26 @@ def multiline_textsize(
features=None,
language=None,
stroke_width=0,
__internal__=False,
):
if not __internal__:
deprecate("multiline_textsize", 10, "multiline_textbbox")
if not _deprecate._suppress_internal_deprecations:
_deprecate.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,
)
_deprecate._suppress_internal_deprecations = True
try:
line_width, line_height = self.textsize(
line,
font,
spacing,
direction,
features,
language,
stroke_width,
)
finally:
_deprecate._suppress_internal_deprecations = False
max_width = max(max_width, line_width)
return max_width, len(lines) * line_spacing - spacing

Expand All @@ -661,15 +673,18 @@ def textlength(
try:
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,
)
_deprecate.deprecate("textlength support for fonts without getlength", 10)
_deprecate._suppress_internal_deprecations = True
try:
size = self.textsize(
text,
font,
direction=direction,
features=features,
language=language,
)
finally:
_deprecate._suppress_internal_deprecations = False
if direction == "ttb":
return size[1]
return size[0]
Expand Down
12 changes: 8 additions & 4 deletions src/PIL/ImageDraw2.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"""


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


class Pen:
Expand Down Expand Up @@ -177,8 +176,13 @@ 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)
_deprecate.deprecate("textsize", 10, "textbbox or textlength")
_deprecate._suppress_internal_deprecations = True
try:
size = self.draw.textsize(text, font=font.font)
finally:
_deprecate._suppress_internal_deprecations = False
return size

def textbbox(self, xy, text, font):
"""
Expand Down
47 changes: 25 additions & 22 deletions src/PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
from enum import IntEnum
from io import BytesIO

from . import Image
from ._deprecate import deprecate
from . import Image, _deprecate
from ._util import is_directory, is_path


Expand All @@ -47,7 +46,7 @@ def __getattr__(name):
if name.startswith(prefix):
name = name[len(prefix) :]
if name in enum.__members__:
deprecate(f"{prefix}{name}", 10, f"{enum.__name__}.{name}")
_deprecate.deprecate(f"{prefix}{name}", 10, f"{enum.__name__}.{name}")
return enum[name]
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")

Expand Down Expand Up @@ -143,8 +142,8 @@ def getsize(self, text, *args, **kwargs):

:return: (width, height)
"""
if not kwargs.get("__internal__"):
deprecate("getsize", 10, "getbbox or getlength")
if not _deprecate._suppress_internal_deprecations:
_deprecate.deprecate("getsize", 10, "getbbox or getlength")
return self.font.getsize(text)

def getmask(self, text, mode="", *args, **kwargs):
Expand Down Expand Up @@ -421,7 +420,6 @@ def getsize(
features=None,
language=None,
stroke_width=0,
__internal__=False,
):
"""
Returns width and height (in pixels) of given text if rendered in font with
Expand Down Expand Up @@ -473,8 +471,8 @@ def getsize(

:return: (width, height)
"""
if not __internal__:
deprecate("getsize", 10, "getbbox or getlength")
if not _deprecate._suppress_internal_deprecations:
_deprecate.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 @@ -532,17 +530,19 @@ def getsize_multiline(

:return: (width, height)
"""
deprecate("getsize_multiline", 10)
_deprecate.deprecate("getsize_multiline", 10)
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)
_deprecate._suppress_internal_deprecations = True
try:
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)
finally:
_deprecate._suppress_internal_deprecations = False

return max_width, len(lines) * line_spacing - spacing

Expand All @@ -556,7 +556,7 @@ def getoffset(self, text):

:return: A tuple of the x and y offset
"""
deprecate("getoffset", 10, "getbbox")
_deprecate.deprecate("getoffset", 10, "getbbox")
return self.font.getsize(text)[1]

def getmask(
Expand Down Expand Up @@ -727,7 +727,7 @@ def getmask2(
if fill is _UNSPECIFIED:
fill = Image.core.fill
else:
deprecate("fill", 10)
_deprecate.deprecate("fill", 10)
size, offset = self.font.getsize(
text, mode, direction, features, language, anchor
)
Expand Down Expand Up @@ -837,12 +837,15 @@ def __init__(self, font, orientation=None):
self.orientation = orientation # any 'transpose' argument, or None

def getsize(self, text, *args, **kwargs):
if not kwargs.get("__internal__"):
deprecate("getsize", 10, "getbbox or getlength")
if not _deprecate._suppress_internal_deprecations:
_deprecate.deprecate("getsize", 10, "getbbox or getlength")
_deprecate._suppress_internal_deprecations = True
try:
w, h = self.font.getsize(text, __internal__=True)
w, h = self.font.getsize(text)
except TypeError:
w, h = self.font.getsize(text)
finally:
_deprecate._suppress_internal_deprecations = False
if self.orientation in (Image.Transpose.ROTATE_90, Image.Transpose.ROTATE_270):
return h, w
return w, h
Expand Down
2 changes: 2 additions & 0 deletions src/PIL/_deprecate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from . import __version__

_suppress_internal_deprecations = False


def deprecate(
deprecated: str,
Expand Down