Font.getsize needs direction and features

This commit is contained in:
wiredfool 2017-06-29 07:01:38 -07:00
parent 14293ea4b1
commit 90a9913705
2 changed files with 20 additions and 12 deletions

View File

@ -243,25 +243,30 @@ class ImageDraw(object):
left += (max_width - widths[idx]) left += (max_width - widths[idx])
else: else:
assert False, 'align must be "left", "center" or "right"' assert False, 'align must be "left", "center" or "right"'
self.text((left, top), line, fill, font, anchor, direction=direction, features=features) self.text((left, top), line, fill, font, anchor,
direction=direction, features=features)
top += line_spacing top += line_spacing
left = xy[0] left = xy[0]
def textsize(self, text, font=None, *args, **kwargs): def textsize(self, text, font=None, spacing=4, direction=None,
features=None):
"""Get the size of a given string, in pixels.""" """Get the size of a given string, in pixels."""
if self._multiline_check(text): if self._multiline_check(text):
return self.multiline_textsize(text, font, *args, **kwargs) return self.multiline_textsize(text, font, spacing,
direction, features)
if font is None: if font is None:
font = self.getfont() font = self.getfont()
return font.getsize(text) return font.getsize(text, direction, features)
def multiline_textsize(self, text, font=None, spacing=4): def multiline_textsize(self, text, font=None, spacing=4, direction=None,
features=None):
max_width = 0 max_width = 0
lines = self._multiline_split(text) lines = self._multiline_split(text)
line_spacing = self.textsize('A', font=font)[1] + spacing line_spacing = self.textsize('A', font=font)[1] + spacing
for line in lines: for line in lines:
line_width, line_height = self.textsize(line, font) line_width, line_height = self.textsize(line, font, spacing,
direction, features)
max_width = max(max_width, line_width) max_width = max(max_width, line_width)
return max_width, len(lines)*line_spacing return max_width, len(lines)*line_spacing

View File

@ -106,9 +106,12 @@ class ImageFont(object):
self.font = Image.core.font(image.im, data) self.font = Image.core.font(image.im, data)
# delegate critical operations to internal type def getsize(self, text, *args, **kwargs):
self.getsize = self.font.getsize return self.font.getsize(text)
self.getmask = self.font.getmask
def getmask(self, text, mode="", *args, **kwargs):
return self.font.getmask(text, mode)
## ##
@ -200,14 +203,14 @@ class TransposedFont(object):
self.font = font self.font = font
self.orientation = orientation # any 'transpose' argument, or None self.orientation = orientation # any 'transpose' argument, or None
def getsize(self, text): def getsize(self, text, *args, **kwargs):
w, h = self.font.getsize(text) w, h = self.font.getsize(text)
if self.orientation in (Image.ROTATE_90, Image.ROTATE_270): if self.orientation in (Image.ROTATE_90, Image.ROTATE_270):
return h, w return h, w
return w, h return w, h
def getmask(self, text, mode=""): def getmask(self, text, mode="", *args, **kwargs):
im = self.font.getmask(text, mode) im = self.font.getmask(text, mode, *args, **kwargs)
if self.orientation is not None: if self.orientation is not None:
return im.transpose(self.orientation) return im.transpose(self.orientation)
return im return im