Added multiline tests

This commit is contained in:
Andrew Murray 2015-06-18 17:51:33 +10:00
parent 9546fac7ec
commit b7335ec9d9
4 changed files with 67 additions and 2 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@ -10,6 +10,8 @@ import copy
FONT_PATH = "Tests/fonts/FreeMono.ttf"
FONT_SIZE = 20
TEST_TEXT = "hey you\nyou are awesome\nthis looks awkward"
try:
from PIL import ImageFont
@ -95,7 +97,7 @@ try:
txt = "Hello World!"
ttf = ImageFont.truetype(font, FONT_SIZE)
ttf.getsize(txt)
img = Image.new("RGB", (256, 64), "white")
d = ImageDraw.Draw(img)
d.text((10, 10), txt, font=ttf, fill='black')
@ -134,7 +136,7 @@ try:
draw = ImageDraw.Draw(im)
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
line_spacing = draw.textsize('A', font=ttf)[1] + 4
lines = ['hey you', 'you are awesome', 'this looks awkward']
lines = TEST_TEXT.split("\n")
y = 0
for line in lines:
draw.text((0, y), line, font=ttf)
@ -148,6 +150,69 @@ try:
# at epsilon = ~38.
self.assert_image_similar(im, target_img, .5)
def test_render_multiline_text(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
# Test that text() correctly connects to multiline_text()
# and that align defaults to left
im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
draw.text((0, 0), TEST_TEXT, font=ttf)
target = 'Tests/images/multiline_text.png'
target_img = Image.open(target)
self.assert_image_similar(im, target_img, .5)
# 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)
target = 'Tests/images/multiline_text'+ext+'.png'
target_img = Image.open(target)
self.assert_image_similar(im, target_img, .5)
def test_unknown_align(self):
im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
# Act/Assert
self.assertRaises(AssertionError, lambda: draw.multiline_text((0, 0), TEST_TEXT, font=ttf, align="unknown"))
def test_multiline_size(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
# Test that textsize() correctly connects to multiline_textsize()
self.assertEqual(draw.textsize(TEST_TEXT, font=ttf),
draw.multiline_textsize(TEST_TEXT, font=ttf))
def test_multiline_width(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
self.assertEqual(draw.textsize("longest line", font=ttf)[0],
draw.multiline_textsize("longest line\nline", font=ttf)[0])
def test_multiline_spacing(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
draw.multiline_text((0, 0), TEST_TEXT, font=ttf, spacing=10)
target = 'Tests/images/multiline_text_spacing.png'
target_img = Image.open(target)
self.assert_image_similar(im, target_img, .5)
def test_rotated_transposed_font(self):
img_grey = Image.new("L", (100, 100))
draw = ImageDraw.Draw(img_grey)