Merge pull request #1647 from radarhere/multiline

Allowed text method to pass on multiline_text method specific arguments
This commit is contained in:
wiredfool 2016-02-01 01:18:51 -08:00
commit c09ba49f0f
3 changed files with 34 additions and 8 deletions

View File

@ -241,9 +241,9 @@ class ImageDraw(object):
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, *args, **kwargs):
if self._multiline_check(text):
return self.multiline_text(xy, text, fill, font, anchor)
return self.multiline_text(xy, text, fill, font, anchor, *args, **kwargs)
ink, fill = self._getink(fill)
if font is None:
@ -288,9 +288,9 @@ class ImageDraw(object):
##
# Get the size of a given string, in pixels.
def textsize(self, text, font=None):
def textsize(self, text, font=None, *args, **kwargs):
if self._multiline_check(text):
return self.multiline_textsize(text, font)
return self.multiline_textsize(text, font, *args, **kwargs)
if font is None:
font = self.getfont()

View File

@ -121,6 +121,7 @@ try:
size = draw.textsize(txt, ttf)
draw.text((10, 10), txt, font=ttf)
draw.rectangle((10, 10, 10 + size[0], 10 + size[1]))
del draw
target = 'Tests/images/rectangle_surrounding_text.png'
target_img = Image.open(target)
@ -159,12 +160,20 @@ try:
self.assert_image_similar(im, target_img, .5)
# Test that text() can pass on additional arguments
# to multiline_text()
draw.text((0, 0), TEST_TEXT, fill=None, font=ttf, anchor=None,
spacing=4, align="left")
draw.text((0, 0), TEST_TEXT, None, ttf, None, 4, "left")
del draw
# Test align center and right
for align, ext in {"center": "_center",
"right": "_right"}.items():
im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
draw.multiline_text((0, 0), TEST_TEXT, font=ttf, align=align)
del draw
target = 'Tests/images/multiline_text'+ext+'.png'
target_img = Image.open(target)
@ -191,6 +200,12 @@ try:
self.assertEqual(draw.textsize(TEST_TEXT, font=ttf),
draw.multiline_textsize(TEST_TEXT, font=ttf))
# Test that textsize() can pass on additional arguments
# to multiline_textsize()
draw.textsize(TEST_TEXT, font=ttf, spacing=4)
draw.textsize(TEST_TEXT, ttf, 4)
del draw
def test_multiline_width(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
im = Image.new(mode='RGB', size=(300, 100))
@ -199,6 +214,7 @@ try:
self.assertEqual(draw.textsize("longest line", font=ttf)[0],
draw.multiline_textsize("longest line\nline",
font=ttf)[0])
del draw
def test_multiline_spacing(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
@ -206,6 +222,7 @@ try:
im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
draw.multiline_text((0, 0), TEST_TEXT, font=ttf, spacing=10)
del draw
target = 'Tests/images/multiline_text_spacing.png'
target_img = Image.open(target)
@ -229,6 +246,7 @@ try:
# Rotated font
draw.font = transposed_font
box_size_b = draw.textsize(word)
del draw
# Check (w,h) of box a is (h,w) of box b
self.assertEqual(box_size_a[0], box_size_b[1])
@ -251,6 +269,7 @@ try:
# Rotated font
draw.font = transposed_font
box_size_b = draw.textsize(word)
del draw
# Check boxes a and b are same size
self.assertEqual(box_size_a, box_size_b)
@ -346,6 +365,7 @@ try:
# Act
default_font = ImageFont.load_default()
draw.text((10, 10), txt, font=default_font)
del draw
# Assert
self.assert_image_equal(im, target_img)

View File

@ -227,15 +227,19 @@ Methods
Draw a shape.
.. py:method:: PIL.ImageDraw.Draw.text(xy, text, fill=None, font=None, anchor=None)
.. py:method:: PIL.ImageDraw.Draw.text(xy, text, fill=None, font=None, anchor=None, spacing=0, align="left")
Draws the string at the given position.
:param xy: Top left corner of the text.
:param text: Text to be drawn. If it contains any newline characters,
the text is passed on to mulitiline_text()
the text is passed on to multiline_text()
:param fill: Color to use for the text.
:param font: An :py:class:`~PIL.ImageFont.ImageFont` instance.
:param spacing: If the text is passed on to multiline_text(),
the number of pixels between lines.
:param align: If the text is passed on to multiline_text(),
"left", "center" or "right".
.. py:method:: PIL.ImageDraw.Draw.multiline_text(xy, text, fill=None, font=None, anchor=None, spacing=0, align="left")
@ -249,13 +253,15 @@ Methods
:param spacing: The number of pixels between lines.
:param align: "left", "center" or "right".
.. py:method:: PIL.ImageDraw.Draw.textsize(text, font=None)
.. py:method:: PIL.ImageDraw.Draw.textsize(text, font=None, spacing=0)
Return the size of the given string, in pixels.
:param text: Text to be measured. If it contains any newline characters,
the text is passed on to mulitiline_textsize()
the text is passed on to multiline_textsize()
:param font: An :py:class:`~PIL.ImageFont.ImageFont` instance.
:param spacing: If the text is passed on to multiline_textsize(),
the number of pixels between lines.
.. py:method:: PIL.ImageDraw.Draw.multiline_textsize(text, font=None, spacing=0)