mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-26 01:04:29 +03:00
Added multiline tests
This commit is contained in:
parent
9546fac7ec
commit
b7335ec9d9
BIN
Tests/images/multiline_text_center.png
Normal file
BIN
Tests/images/multiline_text_center.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.8 KiB |
BIN
Tests/images/multiline_text_right.png
Normal file
BIN
Tests/images/multiline_text_right.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.8 KiB |
BIN
Tests/images/multiline_text_spacing.png
Normal file
BIN
Tests/images/multiline_text_spacing.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.8 KiB |
|
@ -10,6 +10,8 @@ import copy
|
||||||
FONT_PATH = "Tests/fonts/FreeMono.ttf"
|
FONT_PATH = "Tests/fonts/FreeMono.ttf"
|
||||||
FONT_SIZE = 20
|
FONT_SIZE = 20
|
||||||
|
|
||||||
|
TEST_TEXT = "hey you\nyou are awesome\nthis looks awkward"
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from PIL import ImageFont
|
from PIL import ImageFont
|
||||||
|
@ -95,7 +97,7 @@ try:
|
||||||
txt = "Hello World!"
|
txt = "Hello World!"
|
||||||
ttf = ImageFont.truetype(font, FONT_SIZE)
|
ttf = ImageFont.truetype(font, FONT_SIZE)
|
||||||
ttf.getsize(txt)
|
ttf.getsize(txt)
|
||||||
|
|
||||||
img = Image.new("RGB", (256, 64), "white")
|
img = Image.new("RGB", (256, 64), "white")
|
||||||
d = ImageDraw.Draw(img)
|
d = ImageDraw.Draw(img)
|
||||||
d.text((10, 10), txt, font=ttf, fill='black')
|
d.text((10, 10), txt, font=ttf, fill='black')
|
||||||
|
@ -134,7 +136,7 @@ try:
|
||||||
draw = ImageDraw.Draw(im)
|
draw = ImageDraw.Draw(im)
|
||||||
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
|
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
|
||||||
line_spacing = draw.textsize('A', font=ttf)[1] + 4
|
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
|
y = 0
|
||||||
for line in lines:
|
for line in lines:
|
||||||
draw.text((0, y), line, font=ttf)
|
draw.text((0, y), line, font=ttf)
|
||||||
|
@ -148,6 +150,69 @@ try:
|
||||||
# at epsilon = ~38.
|
# at epsilon = ~38.
|
||||||
self.assert_image_similar(im, target_img, .5)
|
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):
|
def test_rotated_transposed_font(self):
|
||||||
img_grey = Image.new("L", (100, 100))
|
img_grey = Image.new("L", (100, 100))
|
||||||
draw = ImageDraw.Draw(img_grey)
|
draw = ImageDraw.Draw(img_grey)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user