mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-25 01:16:16 +03:00
Merge pull request #1055 from hugovk/test_psdraw
Update and test PSDraw
This commit is contained in:
commit
5390aa1692
|
@ -35,23 +35,29 @@ class PSDraw:
|
||||||
fp = sys.stdout
|
fp = sys.stdout
|
||||||
self.fp = fp
|
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):
|
def begin_document(self, id=None):
|
||||||
"""Set up printing of a document. (Write Postscript DSC header.)"""
|
"""Set up printing of a document. (Write Postscript DSC header.)"""
|
||||||
# FIXME: incomplete
|
# FIXME: incomplete
|
||||||
self.fp.write("%!PS-Adobe-3.0\n"
|
self._fp_write("%!PS-Adobe-3.0\n"
|
||||||
"save\n"
|
"save\n"
|
||||||
"/showpage { } def\n"
|
"/showpage { } def\n"
|
||||||
"%%EndComments\n"
|
"%%EndComments\n"
|
||||||
"%%BeginDocument\n")
|
"%%BeginDocument\n")
|
||||||
# self.fp.write(ERROR_PS) # debugging!
|
# self.fp_write(ERROR_PS) # debugging!
|
||||||
self.fp.write(EDROFF_PS)
|
self._fp_write(EDROFF_PS)
|
||||||
self.fp.write(VDI_PS)
|
self._fp_write(VDI_PS)
|
||||||
self.fp.write("%%EndProlog\n")
|
self._fp_write("%%EndProlog\n")
|
||||||
self.isofont = {}
|
self.isofont = {}
|
||||||
|
|
||||||
def end_document(self):
|
def end_document(self):
|
||||||
"""Ends printing. (Write Postscript DSC footer.)"""
|
"""Ends printing. (Write Postscript DSC footer.)"""
|
||||||
self.fp.write("%%EndDocument\n"
|
self._fp_write("%%EndDocument\n"
|
||||||
"restore showpage\n"
|
"restore showpage\n"
|
||||||
"%%End\n")
|
"%%End\n")
|
||||||
if hasattr(self.fp, "flush"):
|
if hasattr(self.fp, "flush"):
|
||||||
|
@ -66,18 +72,11 @@ class PSDraw:
|
||||||
"""
|
"""
|
||||||
if font not in self.isofont:
|
if font not in self.isofont:
|
||||||
# reencode font
|
# reencode font
|
||||||
self.fp.write("/PSDraw-%s ISOLatin1Encoding /%s E\n" %
|
self._fp_write("/PSDraw-%s ISOLatin1Encoding /%s E\n" %
|
||||||
(font, font))
|
(font, font))
|
||||||
self.isofont[font] = 1
|
self.isofont[font] = 1
|
||||||
# rough
|
# 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):
|
|
||||||
"""
|
|
||||||
.. warning:: This has been in the PIL API for ages but was never implemented.
|
|
||||||
|
|
||||||
"""
|
|
||||||
print("*** NOT YET IMPLEMENTED ***")
|
|
||||||
|
|
||||||
def line(self, xy0, xy1):
|
def line(self, xy0, xy1):
|
||||||
"""
|
"""
|
||||||
|
@ -86,7 +85,7 @@ class PSDraw:
|
||||||
left corner of the page).
|
left corner of the page).
|
||||||
"""
|
"""
|
||||||
xy = xy0 + xy1
|
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):
|
def rectangle(self, box):
|
||||||
"""
|
"""
|
||||||
|
@ -101,7 +100,7 @@ class PSDraw:
|
||||||
|
|
||||||
%d %d M %d %d 0 Vr\n
|
%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):
|
def text(self, xy, text):
|
||||||
"""
|
"""
|
||||||
|
@ -111,7 +110,7 @@ class PSDraw:
|
||||||
text = "\\(".join(text.split("("))
|
text = "\\(".join(text.split("("))
|
||||||
text = "\\)".join(text.split(")"))
|
text = "\\)".join(text.split(")"))
|
||||||
xy = xy + (text,)
|
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):
|
def image(self, box, im, dpi=None):
|
||||||
"""Draw a PIL image, centered in the given box."""
|
"""Draw a PIL image, centered in the given box."""
|
||||||
|
@ -135,14 +134,14 @@ class PSDraw:
|
||||||
y = ymax
|
y = ymax
|
||||||
dx = (xmax - x) / 2 + box[0]
|
dx = (xmax - x) / 2 + box[0]
|
||||||
dy = (ymax - y) / 2 + box[1]
|
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:
|
if (x, y) != im.size:
|
||||||
# EpsImagePlugin._save prints the image at (0,0,xsize,ysize)
|
# EpsImagePlugin._save prints the image at (0,0,xsize,ysize)
|
||||||
sx = x / im.size[0]
|
sx = x / im.size[0]
|
||||||
sy = y / im.size[1]
|
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)
|
EpsImagePlugin._save(im, self.fp, None, 0)
|
||||||
self.fp.write("\ngrestore\n")
|
self._fp_write("\ngrestore\n")
|
||||||
|
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
# Postscript driver
|
# Postscript driver
|
||||||
|
|
50
Tests/test_psdraw.py
Normal file
50
Tests/test_psdraw.py
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
from helper import unittest, PillowTestCase
|
||||||
|
|
||||||
|
|
||||||
|
class TestPsDraw(PillowTestCase):
|
||||||
|
|
||||||
|
def test_draw_postscript(self):
|
||||||
|
|
||||||
|
# Based on Pillow tutorial, but there is no textsize:
|
||||||
|
# http://pillow.readthedocs.org/en/latest/handbook/tutorial.html
|
||||||
|
|
||||||
|
# Arrange
|
||||||
|
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(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)
|
||||||
|
|
||||||
|
# draw title
|
||||||
|
ps.setfont("Courier", 36)
|
||||||
|
ps.text((3*72, 4*72), title)
|
||||||
|
|
||||||
|
ps.end_document()
|
||||||
|
fp.close()
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
# Check non-zero file was created
|
||||||
|
import os
|
||||||
|
self.assertTrue(os.path.isfile(tempfile))
|
||||||
|
self.assertGreater(os.path.getsize(tempfile), 0)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
|
|
||||||
|
# End of file
|
|
@ -445,10 +445,9 @@ Drawing Postscript
|
||||||
ps.image(box, im, 75)
|
ps.image(box, im, 75)
|
||||||
ps.rectangle(box)
|
ps.rectangle(box)
|
||||||
|
|
||||||
# draw centered title
|
# draw title
|
||||||
ps.setfont("HelveticaNarrow-Bold", 36)
|
ps.setfont("HelveticaNarrow-Bold", 36)
|
||||||
w, h, b = ps.textsize(title)
|
ps.text((3*72, 4*72), title)
|
||||||
ps.text((4*72-w/2, 1*72-h), title)
|
|
||||||
|
|
||||||
ps.end_document()
|
ps.end_document()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user