2019-02-03 18:34:53 +03:00
|
|
|
from .helper import PillowTestCase
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
from PIL import Image, FontFile, PcfFontFile
|
|
|
|
from PIL import ImageFont, ImageDraw
|
2018-04-20 02:19:13 +03:00
|
|
|
from PIL._util import py3
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2013-04-21 12:30:59 +04:00
|
|
|
codecs = dir(Image.core)
|
|
|
|
|
2017-11-02 14:11:38 +03:00
|
|
|
fontname = "Tests/fonts/10x20-ISO8859-1.pcf"
|
2012-10-16 00:26:38 +04:00
|
|
|
|
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):
|
2017-08-26 03:20:30 +03:00
|
|
|
with open(fontname, "rb") as test_file:
|
|
|
|
font = PcfFontFile.PcfFontFile(test_file)
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertIsInstance(font, FontFile.FontFile)
|
2018-04-10 16:00:33 +03:00
|
|
|
# check the number of characters in the font
|
2017-11-02 14:11:38 +03:00
|
|
|
self.assertEqual(len([_f for _f in font.glyph if _f]), 223)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2014-06-26 04:08:24 +04:00
|
|
|
tempname = self.tempfile("temp.pil")
|
2019-06-13 18:54:24 +03:00
|
|
|
self.addCleanup(self.delete_tempfile, tempname[:-4] + ".pbm")
|
2014-06-10 13:10:47 +04:00
|
|
|
font.save(tempname)
|
2017-11-02 14:37:24 +03:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
with Image.open(tempname.replace(".pil", ".pbm")) as loaded:
|
|
|
|
with Image.open("Tests/fonts/10x20.pbm") as target:
|
2017-11-02 14:37:24 +03:00
|
|
|
self.assert_image_equal(loaded, target)
|
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
with open(tempname, "rb") as f_loaded:
|
|
|
|
with open("Tests/fonts/10x20.pil", "rb") as f_target:
|
2017-11-02 14:37:24 +03:00
|
|
|
self.assertEqual(f_loaded.read(), f_target.read())
|
2014-06-10 13:10:47 +04:00
|
|
|
return tempname
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_sanity(self):
|
|
|
|
self.save_font()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2015-07-03 08:03:25 +03:00
|
|
|
def test_invalid_file(self):
|
|
|
|
with open("Tests/images/flower.jpg", "rb") as fp:
|
2017-09-01 14:05:40 +03:00
|
|
|
self.assertRaises(SyntaxError, PcfFontFile.PcfFontFile, fp)
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2017-11-02 14:11:38 +03:00
|
|
|
def test_draw(self):
|
2014-06-10 13:10:47 +04:00
|
|
|
tempname = self.save_font()
|
|
|
|
font = ImageFont.load(tempname)
|
2018-03-06 11:53:07 +03:00
|
|
|
im = Image.new("L", (130, 30), "white")
|
2017-11-02 14:11:38 +03:00
|
|
|
draw = ImageDraw.Draw(im)
|
2019-06-13 18:54:24 +03:00
|
|
|
draw.text((0, 0), message, "black", font=font)
|
|
|
|
with Image.open("Tests/images/test_draw_pbm_target.png") as target:
|
2017-11-02 14:11:38 +03:00
|
|
|
self.assert_image_similar(im, target, 0)
|
2018-01-27 09:07:24 +03:00
|
|
|
|
2017-11-02 15:04:24 +03:00
|
|
|
def test_textsize(self):
|
|
|
|
tempname = self.save_font()
|
|
|
|
font = ImageFont.load(tempname)
|
|
|
|
for i in range(255):
|
2018-03-06 11:53:07 +03:00
|
|
|
(dx, dy) = font.getsize(chr(i))
|
2017-11-02 15:04:24 +03:00
|
|
|
self.assertEqual(dy, 20)
|
2018-03-06 11:53:07 +03:00
|
|
|
self.assertIn(dx, (0, 10))
|
2017-11-02 15:47:23 +03:00
|
|
|
for l in range(len(message)):
|
2019-06-13 18:54:24 +03:00
|
|
|
msg = message[: l + 1]
|
|
|
|
self.assertEqual(font.getsize(msg), (len(msg) * 10, 20))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def _test_high_characters(self, message):
|
|
|
|
tempname = self.save_font()
|
|
|
|
font = ImageFont.load(tempname)
|
2018-04-09 16:48:36 +03:00
|
|
|
im = Image.new("L", (750, 30), "white")
|
2017-11-02 14:11:38 +03:00
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
draw.text((0, 0), message, "black", font=font)
|
2019-06-13 18:54:24 +03:00
|
|
|
with Image.open("Tests/images/high_ascii_chars.png") as target:
|
2017-11-02 14:11:38 +03:00
|
|
|
self.assert_image_similar(im, target, 0)
|
2014-02-05 08:41:27 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_high_characters(self):
|
2019-06-13 18:54:24 +03:00
|
|
|
message = "".join(chr(i + 1) for i in range(140, 232))
|
2014-06-10 13:10:47 +04:00
|
|
|
self._test_high_characters(message)
|
|
|
|
# accept bytes instances in Py3.
|
2018-04-19 12:40:56 +03:00
|
|
|
if py3:
|
2019-06-13 18:54:24 +03:00
|
|
|
self._test_high_characters(message.encode("latin1"))
|