From 6cfc2e0d9811bdff6ea83af1509025c4b31a4ad1 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 5 Jul 2014 21:17:36 +0300 Subject: [PATCH 1/3] Sanity test WIP FontFile.save2() in same way as save() --- Tests/test_font_pcf.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/Tests/test_font_pcf.py b/Tests/test_font_pcf.py index 8c4c04cd4..052adf96a 100644 --- a/Tests/test_font_pcf.py +++ b/Tests/test_font_pcf.py @@ -3,40 +3,53 @@ from helper import unittest, PillowTestCase, tearDownModule from PIL import Image, FontFile, PcfFontFile from PIL import ImageFont, ImageDraw -codecs = dir(Image.core) +CODECS = dir(Image.core) -fontname = "Tests/fonts/helvO18.pcf" +FONTNAME = "Tests/fonts/helvO18.pcf" -message = "hello, world" +MESSAGE = "hello, world" class TestFontPcf(PillowTestCase): def setUp(self): - if "zip_encoder" not in codecs or "zip_decoder" not in codecs: + if "zip_encoder" not in CODECS or "zip_decoder" not in CODECS: self.skipTest("zlib support not available") - def save_font(self): - file = open(fontname, "rb") + def open_font(self): + file = open(FONTNAME, "rb") font = PcfFontFile.PcfFontFile(file) self.assertIsInstance(font, FontFile.FontFile) self.assertEqual(len([_f for _f in font.glyph if _f]), 192) + return font + def get_tempname(self): tempname = self.tempfile("temp.pil") self.addCleanup(self.delete_tempfile, tempname[:-4]+'.pbm') + return tempname + + def save_font(self): + font = self.open_font() + tempname = self.get_tempname() font.save(tempname) return tempname + def save_font2(self): + font = self.open_font() + tempname = self.get_tempname() + font.save2(tempname) + def test_sanity(self): self.save_font() + self.save_font2() def xtest_draw(self): tempname = self.save_font() font = ImageFont.load(tempname) - image = Image.new("L", font.getsize(message), "white") + image = Image.new("L", font.getsize(MESSAGE), "white") draw = ImageDraw.Draw(image) - draw.text((0, 0), message, font=font) + draw.text((0, 0), MESSAGE, font=font) # assert_signature(image, "7216c60f988dea43a46bb68321e3c1b03ec62aee") def _test_high_characters(self, message): From 8cbe60f816c1fcf50df389c3818307aa6156d0a5 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 5 Jul 2014 22:17:27 +0300 Subject: [PATCH 2/3] Fix for Python 3 --- PIL/FontFile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PIL/FontFile.py b/PIL/FontFile.py index 7c5704c9d..4b8b1211b 100644 --- a/PIL/FontFile.py +++ b/PIL/FontFile.py @@ -134,7 +134,7 @@ class FontFile: fp = open(os.path.splitext(filename)[0] + ".pil", "wb") - fp.write(b"PILfont2\n" + self.name + "\n" + "DATA\n") + fp.write(b"PILfont2\n" + self.name.encode('ascii') + b"\n" + b"DATA\n") fp.write(data) From dff86fc23faa55f2eaff0db2953408b6383cce22 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 5 Jul 2014 22:33:21 +0300 Subject: [PATCH 3/3] flake8 --- PIL/FontFile.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/PIL/FontFile.py b/PIL/FontFile.py index 4b8b1211b..fbe67a115 100644 --- a/PIL/FontFile.py +++ b/PIL/FontFile.py @@ -26,6 +26,7 @@ except ImportError: WIDTH = 800 + def puti16(fp, values): # write network order (big-endian) 16-bit sequence for v in values: @@ -33,6 +34,7 @@ def puti16(fp, values): v += 65536 fp.write(_binary.o16be(v)) + ## # Base class for raster font file handlers. @@ -95,7 +97,6 @@ class FontFile: # print chr(i), dst, s self.metrics[i] = d, dst, s - def save1(self, filename): "Save font in version 1 format" @@ -107,7 +108,7 @@ class FontFile: # font metrics fp = open(os.path.splitext(filename)[0] + ".pil", "wb") fp.write(b"PILfont\n") - fp.write((";;;;;;%d;\n" % self.ysize).encode('ascii')) # HACK!!! + fp.write((";;;;;;%d;\n" % self.ysize).encode('ascii')) # HACK!!! fp.write(b"DATA\n") for id in range(256): m = self.metrics[id] @@ -117,7 +118,6 @@ class FontFile: puti16(fp, m[0] + m[1] + m[2]) fp.close() - def save2(self, filename): "Save font in version 2 format" @@ -142,5 +142,4 @@ class FontFile: fp.close() - - save = save1 # for now + save = save1 # for now