Pillow/Tests/test_font_pcf.py

67 lines
2.0 KiB
Python
Raw Normal View History

2014-07-07 21:03:50 +04:00
from helper import unittest, PillowTestCase
from PIL import Image, FontFile, PcfFontFile
from PIL import ImageFont, ImageDraw
codecs = dir(Image.core)
fontname = "Tests/fonts/helvO18.pcf"
2014-06-10 13:10:47 +04:00
message = "hello, world"
class TestFontPcf(PillowTestCase):
def setUp(self):
if "zip_encoder" not in codecs or "zip_decoder" not in codecs:
self.skipTest("zlib support not available")
def save_font(self):
2015-04-24 11:24:52 +03:00
test_file = open(fontname, "rb")
font = PcfFontFile.PcfFontFile(test_file)
2014-06-10 13:10:47 +04:00
self.assertIsInstance(font, FontFile.FontFile)
self.assertEqual(len([_f for _f in font.glyph if _f]), 192)
tempname = self.tempfile("temp.pil")
self.addCleanup(self.delete_tempfile, tempname[:-4]+'.pbm')
2014-06-10 13:10:47 +04:00
font.save(tempname)
return tempname
2014-06-10 13:10:47 +04:00
def test_sanity(self):
self.save_font()
2015-07-03 08:03:25 +03:00
def test_invalid_file(self):
with open("Tests/images/flower.jpg", "rb") as fp:
self.assertRaises(SyntaxError, lambda: PcfFontFile.PcfFontFile(fp))
2014-06-10 13:10:47 +04:00
def xtest_draw(self):
2014-06-10 13:10:47 +04:00
tempname = self.save_font()
font = ImageFont.load(tempname)
image = Image.new("L", font.getsize(message), "white")
draw = ImageDraw.Draw(image)
draw.text((0, 0), message, font=font)
# assert_signature(image, "7216c60f988dea43a46bb68321e3c1b03ec62aee")
2014-06-10 13:10:47 +04:00
def _test_high_characters(self, message):
2014-06-10 13:10:47 +04:00
tempname = self.save_font()
font = ImageFont.load(tempname)
image = Image.new("L", font.getsize(message), "white")
draw = ImageDraw.Draw(image)
draw.text((0, 0), message, font=font)
2014-02-05 08:41:27 +04:00
2014-06-10 13:10:47 +04:00
compare = Image.open('Tests/images/high_ascii_chars.png')
self.assert_image_equal(image, compare)
2014-06-10 13:10:47 +04:00
def test_high_characters(self):
message = "".join([chr(i+1) for i in range(140, 232)])
self._test_high_characters(message)
# accept bytes instances in Py3.
if bytes is not str:
self._test_high_characters(message.encode('latin1'))
2014-02-05 08:41:27 +04:00
2014-06-10 13:10:47 +04:00
if __name__ == '__main__':
unittest.main()