Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

PSDraw.py 6.4KB

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