home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s053 / 28.ddi / reloc.2 / $POSTSCRIPT / forms.ps < prev    next >
Encoding:
Text File  |  1990-12-08  |  5.6 KB  |  198 lines

  1. %ident    "@(#)////usr/lib/lp/postscript/forms.ps.sl 1.1 4.0 12/08/90 23738 AT&T-USL"
  2. %
  3. % Procedures that let you print any number of pages on each sheet of paper. It's
  4. % far from perfect and won't handle everything (eg. it's not recursive), but should
  5. % be good enough for now. Assumes the default page coordinate system has been set
  6. % up before setupforms is called. lastpage makes certain the last page is printed,
  7. % and should be called immediately after the %%Trailer comment.
  8. %
  9. % Three lines of code needed for page image clipping have been commented out for
  10. % now. It works, but can really slow things down on some versions of PostScript.
  11. % Uncomment them if you want to clip pages.
  12. %
  13.  
  14. /setupforms {
  15.     /formsperpage exch def
  16.  
  17.     /currentform 0 def
  18.     /slop 5 def
  19.     /min {2 copy gt {exch} if pop} def
  20.  
  21. %
  22. % Save the current environment. Needed because showpage will be redefined. Will be
  23. % restored in lastpage.
  24. %
  25.  
  26.     save
  27.  
  28. %
  29. % Number of rows and columns we'll need - may exchange them later.
  30. %
  31.  
  32.     /columns formsperpage sqrt ceiling cvi def
  33.     /rows formsperpage columns div ceiling cvi def
  34.  
  35. %
  36. % Slop leaves a little room around the edge so page images can be outlined and have
  37. % the borders show up. Distance is in default coordinates, so we need to figure out
  38. % how it maps into user coordinates.
  39. %
  40.  
  41.     6 array defaultmatrix
  42.     6 array currentmatrix
  43.     6 array invertmatrix
  44.     6 array concatmatrix
  45.     /tempmatrix exch def
  46.  
  47.     0 slop tempmatrix dtransform dup mul exch dup mul add sqrt
  48.     /slop exch def
  49.  
  50. %
  51. % Determine how big the image area is, using the clipping path bounding box minus
  52. % a little and leave the coordinates of the lower left corner of the clipping path
  53. % on the stack. Also temporarily set the size of each page (ie. formheight and
  54. % formwidth) from the clipping path - just in case old software uses this stuff.
  55. % Only works for coordinate systems that have been rotated by a multiple of 90
  56. % degrees.
  57. %
  58.  
  59.     newpath clippath pathbbox
  60.     2 index sub dup /formheight exch def slop 2 mul sub /pageheight exch def
  61.     2 index sub dup /formwidth exch def slop 2 mul sub /pagewidth exch def
  62.  
  63. %
  64. % New translators all store the size of each page in default coordinates in the
  65. % pagebbox array and it can be different than the size determined by the clipping
  66. % path. If we can find pagebbox use it to set the real dimensions of each page.
  67. % Leaves the coordinates of the lower left corner on the stack, (either from
  68. % pagebbox or clippath) so four numbers are there when we're done.
  69. %
  70.  
  71.     userdict /gotpagebbox known userdict /pagebbox known and {
  72.     newpath
  73.     pagebbox 0 get pagebbox 1 get tempmatrix transform moveto
  74.     pagebbox 0 get pagebbox 3 get tempmatrix transform lineto
  75.     pagebbox 2 get pagebbox 3 get tempmatrix transform lineto
  76.     pagebbox 2 get pagebbox 1 get tempmatrix transform lineto
  77.     closepath pathbbox
  78.     2 index sub /formheight exch def
  79.     2 index sub /formwidth exch def
  80.     } {2 copy} ifelse
  81.  
  82. %
  83. % Top two numbers are the displacement from the job's origin to the lower left
  84. % corner of each page image when we finish setting up the new coordinate system.
  85. %
  86.  
  87.     /ycorner exch def
  88.     /xcorner exch def
  89.  
  90. %
  91. % The two numbers left on the stack are the coordinates of the lower left corner
  92. % of the clipping path. Go there and then up a bit so page images can be outlined.
  93. %
  94.  
  95.     translate
  96.     slop slop translate
  97.  
  98. %
  99. % If the page is wider than high we may be able to do better if we exchange rows
  100. % and columns. Won't make a difference in the current orientation or if rows and
  101. % columns are the same.
  102. %
  103.  
  104.     pagewidth pageheight gt {
  105.     rows columns /rows exch def /columns exch def
  106.     } if
  107.  
  108. %
  109. % Find the orientation and scaling that makes things as large as possible. More
  110. % than what's really needed. First calculation essentially finds the minimum of
  111. % 1/rows and 1/columns.
  112. %
  113.  
  114.     pagewidth formwidth columns mul div pageheight formheight rows mul div min
  115.     pageheight formwidth columns mul div pagewidth formheight rows mul div min
  116.  
  117.     2 copy lt {
  118.     pagewidth pageheight /pagewidth exch def /pageheight exch def
  119.     -90 rotate
  120.     pagewidth neg 0 translate
  121.     exch
  122.     } if
  123.  
  124. %
  125. % Second number from the top is the best choice. Scale so everything will fit on
  126. % the current page, go back to the original origin, and then get ready for the
  127. % first page - which goes in the upper left corner.
  128. %
  129.  
  130.     pop dup dup scale
  131.     xcorner neg ycorner neg translate
  132.     0 rows 1 sub formheight mul translate
  133.  
  134. %
  135. % Try to center everything on the page - scaling we used is on top of the stack.
  136. %
  137.  
  138.     dup pagewidth exch div formwidth columns mul sub 2 div
  139.     exch pageheight exch div formheight rows mul sub 2 div translate
  140.  
  141. %
  142. % Redefine showpage. Assumes a save object is on top of the stack (we put another
  143. % one there later). 
  144. %
  145.  
  146.     /showpage {
  147.     restore
  148. %    initclip
  149.     formsperpage 1 gt {
  150.         gsave .1 setlinewidth outlineform stroke grestore
  151.     } if
  152.     formwidth 0 translate
  153.     /currentform currentform 1 add def
  154.     currentform columns mod 0 eq {
  155.         columns formwidth mul neg formheight neg translate
  156.     } if
  157.     currentform formsperpage mod 0 eq {
  158.         gsave showpage grestore
  159.         currentform columns mod formwidth mul neg
  160.         formsperpage columns idiv formheight mul translate
  161.         /currentform 0 def
  162.     } if
  163. %    outlineform clip newpath
  164.     save
  165.     } bind def
  166.  
  167.     /outlineform {
  168.     newpath
  169.     xcorner ycorner moveto
  170.     formwidth 0 rlineto
  171.     0 formheight rlineto
  172.     formwidth neg 0 rlineto
  173.     closepath
  174.     } bind def
  175.  
  176.     /lastpage {
  177.     formsperpage 1 gt {
  178.         currentform 0 ne {
  179.         save
  180.         0 1 formsperpage currentform sub formsperpage mod {
  181.             pop showpage
  182.         } for
  183.         restore
  184.         } if
  185.         pop restore
  186.     } if
  187.     } def
  188.  
  189. %
  190. % Clip the first page image and save the environment we just set up, including
  191. % the redefined showpage.
  192. %
  193.  
  194. %   outlineform clip
  195.     newpath
  196.     save
  197. } def
  198.