mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-24 00:04:09 +03:00
Moved multiline split character to common functions
This commit is contained in:
parent
d626679c23
commit
77169b2fdb
|
@ -256,8 +256,18 @@ class ImageDraw(object):
|
||||||
##
|
##
|
||||||
# Draw text.
|
# Draw text.
|
||||||
|
|
||||||
|
def _multiline_check(self, text):
|
||||||
|
split_character = "\n" if isinstance(text, type("")) else b"\n"
|
||||||
|
|
||||||
|
return split_character in text
|
||||||
|
|
||||||
|
def _multiline_split(self, text):
|
||||||
|
split_character = "\n" if isinstance(text, type("")) else b"\n"
|
||||||
|
|
||||||
|
return text.split(split_character)
|
||||||
|
|
||||||
def text(self, xy, text, fill=None, font=None, anchor=None):
|
def text(self, xy, text, fill=None, font=None, anchor=None):
|
||||||
if "\n" in text:
|
if self._multiline_check(text):
|
||||||
return self.multiline_text(xy, text, fill, font, anchor)
|
return self.multiline_text(xy, text, fill, font, anchor)
|
||||||
|
|
||||||
ink, fill = self._getink(fill)
|
ink, fill = self._getink(fill)
|
||||||
|
@ -280,7 +290,7 @@ class ImageDraw(object):
|
||||||
spacing=0, align="left"):
|
spacing=0, align="left"):
|
||||||
widths, heights = [], []
|
widths, heights = [], []
|
||||||
max_width = 0
|
max_width = 0
|
||||||
lines = text.split("\n")
|
lines = self._multiline_split(text)
|
||||||
for line in lines:
|
for line in lines:
|
||||||
line_width, line_height = self.textsize(line, font)
|
line_width, line_height = self.textsize(line, font)
|
||||||
widths.append(line_width)
|
widths.append(line_width)
|
||||||
|
@ -295,34 +305,32 @@ class ImageDraw(object):
|
||||||
elif align == "right":
|
elif align == "right":
|
||||||
left += (max_width - widths[idx])
|
left += (max_width - widths[idx])
|
||||||
else:
|
else:
|
||||||
assert False, 'align must be either "left", "center" or "right"'
|
assert False, 'align must be "left", "center" or "right"'
|
||||||
self.text((left, top),
|
self.text((left, top), line, fill, font, anchor)
|
||||||
line, fill, font, anchor)
|
|
||||||
top += heights[idx] + spacing
|
top += heights[idx] + spacing
|
||||||
left = xy[0]
|
left = xy[0]
|
||||||
|
|
||||||
def multiline_textsize(self, text, font=None, spacing=0):
|
|
||||||
max_width = 0
|
|
||||||
height = 0
|
|
||||||
lines = text.split("\n")
|
|
||||||
for line in lines:
|
|
||||||
line_width, line_height = self.textsize(line, font)
|
|
||||||
height += line_height + spacing
|
|
||||||
max_width = max(max_width, line_width)
|
|
||||||
return max_width, height
|
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Get the size of a given string, in pixels.
|
# Get the size of a given string, in pixels.
|
||||||
|
|
||||||
def textsize(self, text, font=None):
|
def textsize(self, text, font=None):
|
||||||
if "\n" in text:
|
if self._multiline_check(text):
|
||||||
return multiline_textsize(text, font)
|
return self.multiline_textsize(text, font)
|
||||||
|
|
||||||
if font is None:
|
if font is None:
|
||||||
font = self.getfont()
|
font = self.getfont()
|
||||||
return font.getsize(text)
|
return font.getsize(text)
|
||||||
|
|
||||||
|
def multiline_textsize(self, text, font=None, spacing=0):
|
||||||
|
max_width = 0
|
||||||
|
height = 0
|
||||||
|
lines = self._multiline_split(text)
|
||||||
|
for line in lines:
|
||||||
|
line_width, line_height = self.textsize(line, font)
|
||||||
|
height += line_height + spacing
|
||||||
|
max_width = max(max_width, line_width)
|
||||||
|
return max_width, height
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# A simple 2D drawing interface for PIL images.
|
# A simple 2D drawing interface for PIL images.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user