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