mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-11-04 09:57:43 +03:00 
			
		
		
		
	deprecate font.getsize and related functions
This commit is contained in:
		
							parent
							
								
									8de74b9fab
								
							
						
					
					
						commit
						406fe59242
					
				| 
						 | 
				
			
			@ -34,6 +34,7 @@ import math
 | 
			
		|||
import numbers
 | 
			
		||||
 | 
			
		||||
from . import Image, ImageColor
 | 
			
		||||
from ._deprecate import deprecate
 | 
			
		||||
 | 
			
		||||
"""
 | 
			
		||||
A simple 2D drawing interface for PIL images.
 | 
			
		||||
| 
						 | 
				
			
			@ -372,6 +373,18 @@ class ImageDraw:
 | 
			
		|||
 | 
			
		||||
        return text.split(split_character)
 | 
			
		||||
 | 
			
		||||
    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
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    def text(
 | 
			
		||||
        self,
 | 
			
		||||
        xy,
 | 
			
		||||
| 
						 | 
				
			
			@ -511,9 +524,7 @@ class ImageDraw:
 | 
			
		|||
        widths = []
 | 
			
		||||
        max_width = 0
 | 
			
		||||
        lines = self._multiline_split(text)
 | 
			
		||||
        line_spacing = (
 | 
			
		||||
            self.textsize("A", font=font, stroke_width=stroke_width)[1] + spacing
 | 
			
		||||
        )
 | 
			
		||||
        line_spacing = self._multiline_spacing(font, spacing, stroke_width)
 | 
			
		||||
        for line in lines:
 | 
			
		||||
            line_width = self.textlength(
 | 
			
		||||
                line, font, direction=direction, features=features, language=language
 | 
			
		||||
| 
						 | 
				
			
			@ -571,16 +582,33 @@ 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")
 | 
			
		||||
        if self._multiline_check(text):
 | 
			
		||||
            return self.multiline_textsize(
 | 
			
		||||
                text, font, spacing, direction, features, language, stroke_width
 | 
			
		||||
                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)
 | 
			
		||||
        return font.getsize(
 | 
			
		||||
            text,
 | 
			
		||||
            direction,
 | 
			
		||||
            features,
 | 
			
		||||
            language,
 | 
			
		||||
            stroke_width,
 | 
			
		||||
            __internal__=True,
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    def multiline_textsize(
 | 
			
		||||
        self,
 | 
			
		||||
| 
						 | 
				
			
			@ -591,15 +619,23 @@ class ImageDraw:
 | 
			
		|||
        features=None,
 | 
			
		||||
        language=None,
 | 
			
		||||
        stroke_width=0,
 | 
			
		||||
        __internal__=False,
 | 
			
		||||
    ):
 | 
			
		||||
        if not __internal__:
 | 
			
		||||
            deprecate("multiline_textsize", 10, "multiline_textbbox")
 | 
			
		||||
        max_width = 0
 | 
			
		||||
        lines = self._multiline_split(text)
 | 
			
		||||
        line_spacing = (
 | 
			
		||||
            self.textsize("A", font=font, stroke_width=stroke_width)[1] + spacing
 | 
			
		||||
        )
 | 
			
		||||
        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
 | 
			
		||||
                line,
 | 
			
		||||
                font,
 | 
			
		||||
                spacing,
 | 
			
		||||
                direction,
 | 
			
		||||
                features,
 | 
			
		||||
                language,
 | 
			
		||||
                stroke_width,
 | 
			
		||||
                __internal__=True,
 | 
			
		||||
            )
 | 
			
		||||
            max_width = max(max_width, line_width)
 | 
			
		||||
        return max_width, len(lines) * line_spacing - spacing
 | 
			
		||||
| 
						 | 
				
			
			@ -625,8 +661,14 @@ class ImageDraw:
 | 
			
		|||
        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
 | 
			
		||||
                text,
 | 
			
		||||
                font,
 | 
			
		||||
                direction=direction,
 | 
			
		||||
                features=features,
 | 
			
		||||
                language=language,
 | 
			
		||||
                __internal__=True,
 | 
			
		||||
            )
 | 
			
		||||
            if direction == "ttb":
 | 
			
		||||
                return size[1]
 | 
			
		||||
| 
						 | 
				
			
			@ -704,9 +746,7 @@ class ImageDraw:
 | 
			
		|||
        widths = []
 | 
			
		||||
        max_width = 0
 | 
			
		||||
        lines = self._multiline_split(text)
 | 
			
		||||
        line_spacing = (
 | 
			
		||||
            self.textsize("A", font=font, stroke_width=stroke_width)[1] + spacing
 | 
			
		||||
        )
 | 
			
		||||
        line_spacing = self._multiline_spacing(font, spacing, stroke_width)
 | 
			
		||||
        for line in lines:
 | 
			
		||||
            line_width = self.textlength(
 | 
			
		||||
                line,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -143,6 +143,8 @@ class ImageFont:
 | 
			
		|||
 | 
			
		||||
        :return: (width, height)
 | 
			
		||||
        """
 | 
			
		||||
        if not kwargs.get("__internal__"):
 | 
			
		||||
            deprecate("getsize", 10, "getbbox or getlength")
 | 
			
		||||
        return self.font.getsize(text)
 | 
			
		||||
 | 
			
		||||
    def getmask(self, text, mode="", *args, **kwargs):
 | 
			
		||||
| 
						 | 
				
			
			@ -386,7 +388,13 @@ class FreeTypeFont:
 | 
			
		|||
        return left, top, left + width, top + height
 | 
			
		||||
 | 
			
		||||
    def getsize(
 | 
			
		||||
        self, text, direction=None, features=None, language=None, stroke_width=0
 | 
			
		||||
        self,
 | 
			
		||||
        text,
 | 
			
		||||
        direction=None,
 | 
			
		||||
        features=None,
 | 
			
		||||
        language=None,
 | 
			
		||||
        stroke_width=0,
 | 
			
		||||
        __internal__=False,
 | 
			
		||||
    ):
 | 
			
		||||
        """
 | 
			
		||||
        Returns width and height (in pixels) of given text if rendered in font with
 | 
			
		||||
| 
						 | 
				
			
			@ -438,6 +446,8 @@ class FreeTypeFont:
 | 
			
		|||
 | 
			
		||||
        :return: (width, height)
 | 
			
		||||
        """
 | 
			
		||||
        if not __internal__:
 | 
			
		||||
            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)
 | 
			
		||||
| 
						 | 
				
			
			@ -495,12 +505,15 @@ class FreeTypeFont:
 | 
			
		|||
 | 
			
		||||
        :return: (width, height)
 | 
			
		||||
        """
 | 
			
		||||
        deprecate("getsize_multiline", 10)
 | 
			
		||||
        max_width = 0
 | 
			
		||||
        lines = self._multiline_split(text)
 | 
			
		||||
        line_spacing = self.getsize("A", stroke_width=stroke_width)[1] + spacing
 | 
			
		||||
        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
 | 
			
		||||
                line, direction, features, language, stroke_width, __internal__=True
 | 
			
		||||
            )
 | 
			
		||||
            max_width = max(max_width, line_width)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -516,6 +529,7 @@ class FreeTypeFont:
 | 
			
		|||
 | 
			
		||||
        :return: A tuple of the x and y offset
 | 
			
		||||
        """
 | 
			
		||||
        deprecate("getoffset", 10, "getbbox")
 | 
			
		||||
        return self.font.getsize(text)[1]
 | 
			
		||||
 | 
			
		||||
    def getmask(
 | 
			
		||||
| 
						 | 
				
			
			@ -796,6 +810,11 @@ class TransposedFont:
 | 
			
		|||
        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")
 | 
			
		||||
        try:
 | 
			
		||||
            w, h = self.font.getsize(text, __internal__=True)
 | 
			
		||||
        except TypeError:
 | 
			
		||||
            w, h = self.font.getsize(text)
 | 
			
		||||
        if self.orientation in (Image.Transpose.ROTATE_90, Image.Transpose.ROTATE_270):
 | 
			
		||||
            return h, w
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user