Skip to content

Commit

Permalink
Fix for Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Dec 27, 2014
1 parent 3185952 commit 8eb117d
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions PIL/PSDraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,29 @@ def __init__(self, fp=None):
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"):
Expand All @@ -66,11 +72,11 @@ def setfont(self, font, size):
"""
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):
"""
Expand All @@ -86,7 +92,7 @@ def line(self, xy0, xy1):
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):
"""
Expand All @@ -101,7 +107,7 @@ def rectangle(self, box):
%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):
"""
Expand All @@ -111,7 +117,7 @@ def text(self, xy, text):
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."""
Expand All @@ -135,14 +141,14 @@ def image(self, box, im, dpi=None):
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
Expand Down

0 comments on commit 8eb117d

Please sign in to comment.