From 385ad47a9be0c10d2d67b313de69b8646e74f9ee Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 11 Dec 2014 13:06:53 +0200 Subject: [PATCH 1/8] Test PSDraw --- Tests/test_psdraw.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Tests/test_psdraw.py diff --git a/Tests/test_psdraw.py b/Tests/test_psdraw.py new file mode 100644 index 000000000..04c32f23e --- /dev/null +++ b/Tests/test_psdraw.py @@ -0,0 +1,42 @@ +from helper import unittest, PillowTestCase, hopper + + +class TestPsDraw(PillowTestCase): + + def test_draw_postscript(self): + + # Taken from Pillow tutorial: + # http://pillow.readthedocs.org/en/latest/handbook/tutorial.html + + # Arrange + from PIL import Image + from PIL import PSDraw + tempfile = self.tempfile('temp.ps') + + im = Image.open("Tests/images/hopper.ppm") + title = "hopper" + box = (1*72, 2*72, 7*72, 10*72) # in points + + # Act + ps = PSDraw.PSDraw(tempfile) + ps.begin_document(title) + + # draw the image (75 dpi) + ps.image(box, im, 75) + ps.rectangle(box) + + # draw centered title + ps.setfont("HelveticaNarrow-Bold", 36) + w, h, b = ps.textsize(title) + ps.text((4*72-w/2, 1*72-h), title) + + ps.end_document() + + # Assert + # TODO + + +if __name__ == '__main__': + unittest.main() + +# End of file From 6da05b40269ee919d666160881b88bd12caaecb2 Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 11 Dec 2014 13:20:11 +0200 Subject: [PATCH 2/8] Use fp instead of filename --- Tests/test_psdraw.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Tests/test_psdraw.py b/Tests/test_psdraw.py index 04c32f23e..e59acc0e4 100644 --- a/Tests/test_psdraw.py +++ b/Tests/test_psdraw.py @@ -12,13 +12,14 @@ class TestPsDraw(PillowTestCase): from PIL import Image from PIL import PSDraw tempfile = self.tempfile('temp.ps') + fp = open(tempfile, "wb") im = Image.open("Tests/images/hopper.ppm") title = "hopper" box = (1*72, 2*72, 7*72, 10*72) # in points # Act - ps = PSDraw.PSDraw(tempfile) + ps = PSDraw.PSDraw(fp) ps.begin_document(title) # draw the image (75 dpi) @@ -31,6 +32,7 @@ class TestPsDraw(PillowTestCase): ps.text((4*72-w/2, 1*72-h), title) ps.end_document() + fp.close() # Assert # TODO From 31859521c9d24d4608c4fece12a6951defdd8392 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 27 Dec 2014 22:04:34 +0200 Subject: [PATCH 3/8] Update test as textsize() isn't implemented --- Tests/test_psdraw.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Tests/test_psdraw.py b/Tests/test_psdraw.py index e59acc0e4..cd0731c01 100644 --- a/Tests/test_psdraw.py +++ b/Tests/test_psdraw.py @@ -1,11 +1,11 @@ -from helper import unittest, PillowTestCase, hopper +from helper import unittest, PillowTestCase class TestPsDraw(PillowTestCase): def test_draw_postscript(self): - # Taken from Pillow tutorial: + # Based on Pillow tutorial, but there is no textsize: # http://pillow.readthedocs.org/en/latest/handbook/tutorial.html # Arrange @@ -16,7 +16,7 @@ class TestPsDraw(PillowTestCase): im = Image.open("Tests/images/hopper.ppm") title = "hopper" - box = (1*72, 2*72, 7*72, 10*72) # in points + box = (1*72, 2*72, 7*72, 10*72) # in points # Act ps = PSDraw.PSDraw(fp) @@ -26,16 +26,18 @@ class TestPsDraw(PillowTestCase): ps.image(box, im, 75) ps.rectangle(box) - # draw centered title - ps.setfont("HelveticaNarrow-Bold", 36) - w, h, b = ps.textsize(title) - ps.text((4*72-w/2, 1*72-h), title) + # draw title + ps.setfont("Courier", 36) + ps.text((3*72, 4*72), title) ps.end_document() fp.close() # Assert - # TODO + # Check non-zero file was created + import os + self.assertTrue(os.path.isfile(tempfile)) + self.assertGreater(os.path.getsize(tempfile), 0) if __name__ == '__main__': From 8eb117dc9e71f43fd9240e2b7dd25246eeaeb82f Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 27 Dec 2014 22:26:54 +0200 Subject: [PATCH 4/8] Fix for Python 3 --- PIL/PSDraw.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/PIL/PSDraw.py b/PIL/PSDraw.py index 5a24441a8..fc59c3158 100644 --- a/PIL/PSDraw.py +++ b/PIL/PSDraw.py @@ -35,23 +35,29 @@ class PSDraw: fp = sys.stdout self.fp = fp + def _fp_write(self, to_write): + if bytes is str: + self.fp.write(to_write) + else: + self.fp.write(bytes(to_write, 'UTF-8')) + def begin_document(self, id=None): """Set up printing of a document. (Write Postscript DSC header.)""" # FIXME: incomplete - self.fp.write("%!PS-Adobe-3.0\n" + self._fp_write("%!PS-Adobe-3.0\n" "save\n" "/showpage { } def\n" "%%EndComments\n" "%%BeginDocument\n") - # self.fp.write(ERROR_PS) # debugging! - self.fp.write(EDROFF_PS) - self.fp.write(VDI_PS) - self.fp.write("%%EndProlog\n") + # self.fp_write(ERROR_PS) # debugging! + self._fp_write(EDROFF_PS) + self._fp_write(VDI_PS) + self._fp_write("%%EndProlog\n") self.isofont = {} def end_document(self): """Ends printing. (Write Postscript DSC footer.)""" - self.fp.write("%%EndDocument\n" + self._fp_write("%%EndDocument\n" "restore showpage\n" "%%End\n") if hasattr(self.fp, "flush"): @@ -66,11 +72,11 @@ class PSDraw: """ if font not in self.isofont: # reencode font - self.fp.write("/PSDraw-%s ISOLatin1Encoding /%s E\n" % + self._fp_write("/PSDraw-%s ISOLatin1Encoding /%s E\n" % (font, font)) self.isofont[font] = 1 # rough - self.fp.write("/F0 %d /PSDraw-%s F\n" % (size, font)) + self._fp_write("/F0 %d /PSDraw-%s F\n" % (size, font)) def setink(self, ink): """ @@ -86,7 +92,7 @@ class PSDraw: left corner of the page). """ xy = xy0 + xy1 - self.fp.write("%d %d %d %d Vl\n" % xy) + self._fp_write("%d %d %d %d Vl\n" % xy) def rectangle(self, box): """ @@ -101,7 +107,7 @@ class PSDraw: %d %d M %d %d 0 Vr\n """ - self.fp.write("%d %d M %d %d 0 Vr\n" % box) + self._fp_write("%d %d M %d %d 0 Vr\n" % box) def text(self, xy, text): """ @@ -111,7 +117,7 @@ class PSDraw: text = "\\(".join(text.split("(")) text = "\\)".join(text.split(")")) xy = xy + (text,) - self.fp.write("%d %d M (%s) S\n" % xy) + self._fp_write("%d %d M (%s) S\n" % xy) def image(self, box, im, dpi=None): """Draw a PIL image, centered in the given box.""" @@ -135,14 +141,14 @@ class PSDraw: y = ymax dx = (xmax - x) / 2 + box[0] dy = (ymax - y) / 2 + box[1] - self.fp.write("gsave\n%f %f translate\n" % (dx, dy)) + self._fp_write("gsave\n%f %f translate\n" % (dx, dy)) if (x, y) != im.size: # EpsImagePlugin._save prints the image at (0,0,xsize,ysize) sx = x / im.size[0] sy = y / im.size[1] - self.fp.write("%f %f scale\n" % (sx, sy)) + self._fp_write("%f %f scale\n" % (sx, sy)) EpsImagePlugin._save(im, self.fp, None, 0) - self.fp.write("\ngrestore\n") + self._fp_write("\ngrestore\n") # -------------------------------------------------------------------- # Postscript driver From c2f433e5ac24029910b503e91d55c11d1b00b638 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 27 Dec 2014 22:41:56 +0200 Subject: [PATCH 5/8] Remove unimplemented setink() --- PIL/PSDraw.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/PIL/PSDraw.py b/PIL/PSDraw.py index fc59c3158..9a6b4655c 100644 --- a/PIL/PSDraw.py +++ b/PIL/PSDraw.py @@ -78,13 +78,6 @@ class PSDraw: # rough self._fp_write("/F0 %d /PSDraw-%s F\n" % (size, font)) - def setink(self, ink): - """ - .. warning:: This has been in the PIL API for ages but was never implemented. - - """ - print("*** NOT YET IMPLEMENTED ***") - def line(self, xy0, xy1): """ Draws a line between the two points. Coordinates are given in From 7f057ed962f13b03efadd8c627c596640576f819 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 27 Dec 2014 22:50:17 +0200 Subject: [PATCH 6/8] Test drawing diagonal lines --- Tests/test_psdraw.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Tests/test_psdraw.py b/Tests/test_psdraw.py index cd0731c01..9606a4392 100644 --- a/Tests/test_psdraw.py +++ b/Tests/test_psdraw.py @@ -22,6 +22,10 @@ class TestPsDraw(PillowTestCase): ps = PSDraw.PSDraw(fp) ps.begin_document(title) + # draw diagonal lines in a cross + ps.line((1*72, 2*72), (7*72, 10*72)) + ps.line((7*72, 2*72), (1*72, 10*72)) + # draw the image (75 dpi) ps.image(box, im, 75) ps.rectangle(box) From 1b8ba191e95ed6f234c1b107194e355befce24d2 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 27 Dec 2014 23:05:34 +0200 Subject: [PATCH 7/8] Update docs to remove reference to textsize() --- docs/handbook/tutorial.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/handbook/tutorial.rst b/docs/handbook/tutorial.rst index c6d2bf9e4..365c8e7a8 100644 --- a/docs/handbook/tutorial.rst +++ b/docs/handbook/tutorial.rst @@ -445,10 +445,9 @@ Drawing Postscript ps.image(box, im, 75) ps.rectangle(box) - # draw centered title + # draw title ps.setfont("HelveticaNarrow-Bold", 36) - w, h, b = ps.textsize(title) - ps.text((4*72-w/2, 1*72-h), title) + ps.text((3*72, 4*72), title) ps.end_document() From c4d9bb6eaa8b123fd8c941736cdf2cc79f834298 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 27 Dec 2014 23:07:41 +0200 Subject: [PATCH 8/8] flake8 --- PIL/PSDraw.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/PIL/PSDraw.py b/PIL/PSDraw.py index 9a6b4655c..6187e40ad 100644 --- a/PIL/PSDraw.py +++ b/PIL/PSDraw.py @@ -45,10 +45,10 @@ class PSDraw: """Set up printing of a document. (Write Postscript DSC header.)""" # FIXME: incomplete self._fp_write("%!PS-Adobe-3.0\n" - "save\n" - "/showpage { } def\n" - "%%EndComments\n" - "%%BeginDocument\n") + "save\n" + "/showpage { } def\n" + "%%EndComments\n" + "%%BeginDocument\n") # self.fp_write(ERROR_PS) # debugging! self._fp_write(EDROFF_PS) self._fp_write(VDI_PS) @@ -58,8 +58,8 @@ class PSDraw: def end_document(self): """Ends printing. (Write Postscript DSC footer.)""" self._fp_write("%%EndDocument\n" - "restore showpage\n" - "%%End\n") + "restore showpage\n" + "%%End\n") if hasattr(self.fp, "flush"): self.fp.flush() @@ -73,7 +73,7 @@ class PSDraw: if font not in self.isofont: # reencode font self._fp_write("/PSDraw-%s ISOLatin1Encoding /%s E\n" % - (font, font)) + (font, font)) self.isofont[font] = 1 # rough self._fp_write("/F0 %d /PSDraw-%s F\n" % (size, font))