You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
6.7 KiB

4 years ago
  1. #
  2. # The Python Imaging Library
  3. # $Id$
  4. #
  5. # simple postscript graphics interface
  6. #
  7. # History:
  8. # 1996-04-20 fl Created
  9. # 1999-01-10 fl Added gsave/grestore to image method
  10. # 2005-05-04 fl Fixed floating point issue in image (from Eric Etheridge)
  11. #
  12. # Copyright (c) 1997-2005 by Secret Labs AB. All rights reserved.
  13. # Copyright (c) 1996 by Fredrik Lundh.
  14. #
  15. # See the README file for information on usage and redistribution.
  16. #
  17. from . import EpsImagePlugin
  18. from ._util import py3
  19. import sys
  20. ##
  21. # Simple Postscript graphics interface.
  22. class PSDraw(object):
  23. """
  24. Sets up printing to the given file. If **fp** is omitted,
  25. :py:attr:`sys.stdout` is assumed.
  26. """
  27. def __init__(self, fp=None):
  28. if not fp:
  29. fp = sys.stdout
  30. self.fp = fp
  31. def _fp_write(self, to_write):
  32. if not py3 or self.fp == sys.stdout:
  33. self.fp.write(to_write)
  34. else:
  35. self.fp.write(bytes(to_write, 'UTF-8'))
  36. def begin_document(self, id=None):
  37. """Set up printing of a document. (Write Postscript DSC header.)"""
  38. # FIXME: incomplete
  39. self._fp_write("%!PS-Adobe-3.0\n"
  40. "save\n"
  41. "/showpage { } def\n"
  42. "%%EndComments\n"
  43. "%%BeginDocument\n")
  44. # self._fp_write(ERROR_PS) # debugging!
  45. self._fp_write(EDROFF_PS)
  46. self._fp_write(VDI_PS)
  47. self._fp_write("%%EndProlog\n")
  48. self.isofont = {}
  49. def end_document(self):
  50. """Ends printing. (Write Postscript DSC footer.)"""
  51. self._fp_write("%%EndDocument\n"
  52. "restore showpage\n"
  53. "%%End\n")
  54. if hasattr(self.fp, "flush"):
  55. self.fp.flush()
  56. def setfont(self, font, size):
  57. """
  58. Selects which font to use.
  59. :param font: A Postscript font name
  60. :param size: Size in points.
  61. """
  62. if font not in self.isofont:
  63. # reencode font
  64. self._fp_write("/PSDraw-%s ISOLatin1Encoding /%s E\n" %
  65. (font, font))
  66. self.isofont[font] = 1
  67. # rough
  68. self._fp_write("/F0 %d /PSDraw-%s F\n" % (size, font))
  69. def line(self, xy0, xy1):
  70. """
  71. Draws a line between the two points. Coordinates are given in
  72. Postscript point coordinates (72 points per inch, (0, 0) is the lower
  73. left corner of the page).
  74. """
  75. xy = xy0 + xy1
  76. self._fp_write("%d %d %d %d Vl\n" % xy)
  77. def rectangle(self, box):
  78. """
  79. Draws a rectangle.
  80. :param box: A 4-tuple of integers whose order and function is currently
  81. undocumented.
  82. Hint: the tuple is passed into this format string:
  83. .. code-block:: python
  84. %d %d M %d %d 0 Vr\n
  85. """
  86. self._fp_write("%d %d M %d %d 0 Vr\n" % box)
  87. def text(self, xy, text):
  88. """
  89. Draws text at the given position. You must use
  90. :py:meth:`~PIL.PSDraw.PSDraw.setfont` before calling this method.
  91. """
  92. text = "\\(".join(text.split("("))
  93. text = "\\)".join(text.split(")"))
  94. xy = xy + (text,)
  95. self._fp_write("%d %d M (%s) S\n" % xy)
  96. def image(self, box, im, dpi=None):
  97. """Draw a PIL image, centered in the given box."""
  98. # default resolution depends on mode
  99. if not dpi:
  100. if im.mode == "1":
  101. dpi = 200 # fax
  102. else:
  103. dpi = 100 # greyscale
  104. # image size (on paper)
  105. x = float(im.size[0] * 72) / dpi
  106. y = float(im.size[1] * 72) / dpi
  107. # max allowed size
  108. xmax = float(box[2] - box[0])
  109. ymax = float(box[3] - box[1])
  110. if x > xmax:
  111. y = y * xmax / x
  112. x = xmax
  113. if y > ymax:
  114. x = x * ymax / y
  115. y = ymax
  116. dx = (xmax - x) / 2 + box[0]
  117. dy = (ymax - y) / 2 + box[1]
  118. self._fp_write("gsave\n%f %f translate\n" % (dx, dy))
  119. if (x, y) != im.size:
  120. # EpsImagePlugin._save prints the image at (0,0,xsize,ysize)
  121. sx = x / im.size[0]
  122. sy = y / im.size[1]
  123. self._fp_write("%f %f scale\n" % (sx, sy))
  124. EpsImagePlugin._save(im, self.fp, None, 0)
  125. self._fp_write("\ngrestore\n")
  126. # --------------------------------------------------------------------
  127. # Postscript driver
  128. #
  129. # EDROFF.PS -- Postscript driver for Edroff 2
  130. #
  131. # History:
  132. # 94-01-25 fl: created (edroff 2.04)
  133. #
  134. # Copyright (c) Fredrik Lundh 1994.
  135. #
  136. EDROFF_PS = """\
  137. /S { show } bind def
  138. /P { moveto show } bind def
  139. /M { moveto } bind def
  140. /X { 0 rmoveto } bind def
  141. /Y { 0 exch rmoveto } bind def
  142. /E { findfont
  143. dup maxlength dict begin
  144. {
  145. 1 index /FID ne { def } { pop pop } ifelse
  146. } forall
  147. /Encoding exch def
  148. dup /FontName exch def
  149. currentdict end definefont pop
  150. } bind def
  151. /F { findfont exch scalefont dup setfont
  152. [ exch /setfont cvx ] cvx bind def
  153. } bind def
  154. """
  155. #
  156. # VDI.PS -- Postscript driver for VDI meta commands
  157. #
  158. # History:
  159. # 94-01-25 fl: created (edroff 2.04)
  160. #
  161. # Copyright (c) Fredrik Lundh 1994.
  162. #
  163. VDI_PS = """\
  164. /Vm { moveto } bind def
  165. /Va { newpath arcn stroke } bind def
  166. /Vl { moveto lineto stroke } bind def
  167. /Vc { newpath 0 360 arc closepath } bind def
  168. /Vr { exch dup 0 rlineto
  169. exch dup neg 0 exch rlineto
  170. exch neg 0 rlineto
  171. 0 exch rlineto
  172. 100 div setgray fill 0 setgray } bind def
  173. /Tm matrix def
  174. /Ve { Tm currentmatrix pop
  175. translate scale newpath 0 0 .5 0 360 arc closepath
  176. Tm setmatrix
  177. } bind def
  178. /Vf { currentgray exch setgray fill setgray } bind def
  179. """
  180. #
  181. # ERROR.PS -- Error handler
  182. #
  183. # History:
  184. # 89-11-21 fl: created (pslist 1.10)
  185. #
  186. ERROR_PS = """\
  187. /landscape false def
  188. /errorBUF 200 string def
  189. /errorNL { currentpoint 10 sub exch pop 72 exch moveto } def
  190. errordict begin /handleerror {
  191. initmatrix /Courier findfont 10 scalefont setfont
  192. newpath 72 720 moveto $error begin /newerror false def
  193. (PostScript Error) show errorNL errorNL
  194. (Error: ) show
  195. /errorname load errorBUF cvs show errorNL errorNL
  196. (Command: ) show
  197. /command load dup type /stringtype ne { errorBUF cvs } if show
  198. errorNL errorNL
  199. (VMstatus: ) show
  200. vmstatus errorBUF cvs show ( bytes available, ) show
  201. errorBUF cvs show ( bytes used at level ) show
  202. errorBUF cvs show errorNL errorNL
  203. (Operand stargck: ) show errorNL /ostargck load {
  204. dup type /stringtype ne { errorBUF cvs } if 72 0 rmoveto show errorNL
  205. } forall errorNL
  206. (Execution stargck: ) show errorNL /estargck load {
  207. dup type /stringtype ne { errorBUF cvs } if 72 0 rmoveto show errorNL
  208. } forall
  209. end showpage
  210. } def end
  211. """