test both layout engines, if available

This commit is contained in:
wiredfool 2017-06-13 09:31:29 -07:00
parent b8c04de043
commit 39327332df

View File

@ -1,7 +1,6 @@
from helper import unittest, PillowTestCase
from PIL import Image
from PIL import ImageDraw
from PIL import Image, ImageDraw, ImageFont, features
from io import BytesIO
import os
import sys
@ -12,12 +11,11 @@ FONT_SIZE = 20
TEST_TEXT = "hey you\nyou are awesome\nthis looks awkward"
HAS_FREETYPE = features.check('freetype2')
HAS_RAQM = features.check('raqm')
try:
from PIL import ImageFont
ImageFont.core.getfont # check if freetype is available
class SimplePatcher(object):
class SimplePatcher(object):
def __init__(self, parent_obj, attr_name, value):
self._parent_obj = parent_obj
self._attr_name = attr_name
@ -42,14 +40,20 @@ try:
else:
delattr(self._parent_obj, self._attr_name)
class TestImageFont(PillowTestCase):
@unittest.skipUnless(HAS_FREETYPE, "ImageFont not Available")
class TestImageFont(PillowTestCase):
LAYOUT_ENGINE = ImageFont.LAYOUT_BASIC
def get_font(self):
return ImageFont.truetype(FONT_PATH, FONT_SIZE,
layout_engine=self.LAYOUT_ENGINE)
def test_sanity(self):
self.assertRegexpMatches(
ImageFont.core.freetype2_version, r"\d+\.\d+\.\d+$")
def test_font_properties(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
ttf = self.get_font()
self.assertEqual(ttf.path, FONT_PATH)
self.assertEqual(ttf.size, FONT_SIZE)
@ -65,7 +69,7 @@ try:
self.assertEqual(ttf_copy.path, second_font_path)
def test_font_with_name(self):
ImageFont.truetype(FONT_PATH, FONT_SIZE)
self.get_font()
self._render(FONT_PATH)
self._clean()
@ -75,7 +79,8 @@ try:
return font_bytes
def test_font_with_filelike(self):
ImageFont.truetype(self._font_as_bytes(), FONT_SIZE)
ImageFont.truetype(self._font_as_bytes(), FONT_SIZE,
layout_engine=self.LAYOUT_ENGINE)
self._render(self._font_as_bytes())
# Usage note: making two fonts from the same buffer fails.
# shared_bytes = self._font_as_bytes()
@ -90,7 +95,8 @@ try:
def _render(self, font):
txt = "Hello World!"
ttf = ImageFont.truetype(font, FONT_SIZE)
ttf = ImageFont.truetype(font, FONT_SIZE,
layout_engine=self.LAYOUT_ENGINE)
ttf.getsize(txt)
img = Image.new("RGB", (256, 64), "white")
@ -115,7 +121,7 @@ try:
def test_textsize_equal(self):
im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
ttf = self.get_font()
txt = "Hello World!"
size = draw.textsize(txt, ttf)
@ -132,7 +138,7 @@ try:
def test_render_multiline(self):
im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
ttf = self.get_font()
line_spacing = draw.textsize('A', font=ttf)[1] + 4
lines = TEST_TEXT.split("\n")
y = 0
@ -149,7 +155,7 @@ try:
self.assert_image_similar(im, target_img, 6.2)
def test_render_multiline_text(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
ttf = self.get_font()
# Test that text() correctly connects to multiline_text()
# and that align defaults to left
@ -187,7 +193,7 @@ try:
def test_unknown_align(self):
im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
ttf = self.get_font()
# Act/Assert
self.assertRaises(AssertionError,
@ -196,7 +202,7 @@ try:
align="unknown"))
def test_multiline_size(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
ttf = self.get_font()
im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
@ -211,7 +217,7 @@ try:
del draw
def test_multiline_width(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
ttf = self.get_font()
im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
@ -221,7 +227,7 @@ try:
del draw
def test_multiline_spacing(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
ttf = self.get_font()
im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
@ -238,7 +244,7 @@ try:
img_grey = Image.new("L", (100, 100))
draw = ImageDraw.Draw(img_grey)
word = "testing"
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
font = self.get_font()
orientation = Image.ROTATE_90
transposed_font = ImageFont.TransposedFont(
@ -261,7 +267,7 @@ try:
img_grey = Image.new("L", (100, 100))
draw = ImageDraw.Draw(img_grey)
word = "testing"
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
font = self.get_font()
orientation = None
transposed_font = ImageFont.TransposedFont(
@ -282,7 +288,7 @@ try:
def test_rotated_transposed_font_get_mask(self):
# Arrange
text = "mask this"
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
font = self.get_font()
orientation = Image.ROTATE_90
transposed_font = ImageFont.TransposedFont(
font, orientation=orientation)
@ -296,7 +302,7 @@ try:
def test_unrotated_transposed_font_get_mask(self):
# Arrange
text = "mask this"
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
font = self.get_font()
orientation = None
transposed_font = ImageFont.TransposedFont(
font, orientation=orientation)
@ -309,7 +315,7 @@ try:
def test_free_type_font_get_name(self):
# Arrange
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
font = self.get_font()
# Act
name = font.getname()
@ -319,7 +325,7 @@ try:
def test_free_type_font_get_metrics(self):
# Arrange
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
font = self.get_font()
# Act
ascent, descent = font.getmetrics()
@ -331,7 +337,7 @@ try:
def test_free_type_font_get_offset(self):
# Arrange
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
font = self.get_font()
text = "offset this"
# Act
@ -342,7 +348,7 @@ try:
def test_free_type_font_get_mask(self):
# Arrange
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
font = self.get_font()
text = "mask this"
# Act
@ -450,7 +456,7 @@ try:
def test_imagefont_getters(self):
# Arrange
t = ImageFont.truetype(FONT_PATH, FONT_SIZE)
t = self.get_font()
# Act / Assert
self.assertEqual(t.getmetrics(), (16, 4))
@ -467,11 +473,9 @@ try:
self.assertEqual(t.getsize('a'), (12, 16))
except ImportError:
class TestImageFont(PillowTestCase):
def test_skip(self):
self.skipTest("ImportError")
@unittest.skipUnless(HAS_RAQM, "Raqm not Available")
class TestImageFont_RaqmLayout(TestImageFont):
LAYOUT_ENGINE = ImageFont.LAYOUT_RAQM
if __name__ == '__main__':
unittest.main()