2 b855148c 2004-05-16 devnull % Procedures that let you print any number of pages on each sheet of paper. It's
3 b855148c 2004-05-16 devnull % far from perfect and won't handle everything (eg. it's not recursive), but should
4 b855148c 2004-05-16 devnull % be good enough for now. Assumes the default page coordinate system has been set
5 b855148c 2004-05-16 devnull % up before setupforms is called. lastpage makes certain the last page is printed,
6 b855148c 2004-05-16 devnull % and should be called immediately after the %%Trailer comment.
8 b855148c 2004-05-16 devnull % Three lines of code needed for page image clipping have been commented out for
9 b855148c 2004-05-16 devnull % now. It works, but can really slow things down on some versions of PostScript.
10 b855148c 2004-05-16 devnull % Uncomment them if you want to clip pages.
13 b855148c 2004-05-16 devnull /setupforms {
14 b855148c 2004-05-16 devnull /formsperpage exch def
16 b855148c 2004-05-16 devnull /currentform 0 def
17 b855148c 2004-05-16 devnull /slop 5 def
18 b855148c 2004-05-16 devnull /min {2 copy gt {exch} if pop} def
21 b855148c 2004-05-16 devnull % Save the current environment so the real showpage can be restored when we're all
22 b855148c 2004-05-16 devnull % done. Occasionally helps when a banner page is included with the job.
25 b855148c 2004-05-16 devnull /saveobj save def
28 b855148c 2004-05-16 devnull % Number of rows and columns we'll need - may exchange them later.
31 b855148c 2004-05-16 devnull /columns formsperpage sqrt ceiling cvi def
32 b855148c 2004-05-16 devnull /rows formsperpage columns div ceiling cvi def
35 b855148c 2004-05-16 devnull % Slop leaves a little room around the edge so page images can be outlined and have
36 b855148c 2004-05-16 devnull % the borders show up. Distance is in default coordinates, so we need to figure out
37 b855148c 2004-05-16 devnull % how it maps into user coordinates.
40 b855148c 2004-05-16 devnull 6 array defaultmatrix
41 b855148c 2004-05-16 devnull 6 array currentmatrix
42 b855148c 2004-05-16 devnull 6 array invertmatrix
43 b855148c 2004-05-16 devnull 6 array concatmatrix
44 b855148c 2004-05-16 devnull /tempmatrix exch def
46 b855148c 2004-05-16 devnull 0 slop tempmatrix dtransform dup mul exch dup mul add sqrt
47 b855148c 2004-05-16 devnull /slop exch def
50 b855148c 2004-05-16 devnull % Determine how big the image area is, using the clipping path bounding box minus
51 b855148c 2004-05-16 devnull % a little and leave the coordinates of the lower left corner of the clipping path
52 b855148c 2004-05-16 devnull % on the stack. Also temporarily set the size of each page (ie. formheight and
53 b855148c 2004-05-16 devnull % formwidth) from the clipping path - just in case old software uses this stuff.
54 b855148c 2004-05-16 devnull % Only works for coordinate systems that have been rotated by a multiple of 90
55 b855148c 2004-05-16 devnull % degrees.
58 b855148c 2004-05-16 devnull newpath clippath pathbbox
59 b855148c 2004-05-16 devnull 2 index sub dup /formheight exch def slop 2 mul sub /pageheight exch def
60 b855148c 2004-05-16 devnull 2 index sub dup /formwidth exch def slop 2 mul sub /pagewidth exch def
63 b855148c 2004-05-16 devnull % New translators all store the size of each page in default coordinates in the
64 b855148c 2004-05-16 devnull % pagebbox array and it can be different than the size determined by the clipping
65 b855148c 2004-05-16 devnull % path. If we can find pagebbox use it to set the real dimensions of each page.
66 b855148c 2004-05-16 devnull % Leaves the coordinates of the lower left corner on the stack, (either from
67 b855148c 2004-05-16 devnull % pagebbox or clippath) so four numbers are there when we're done.
70 b855148c 2004-05-16 devnull userdict /gotpagebbox known userdict /pagebbox known and {
72 b855148c 2004-05-16 devnull pagebbox 0 get pagebbox 1 get tempmatrix transform moveto
73 b855148c 2004-05-16 devnull pagebbox 0 get pagebbox 3 get tempmatrix transform lineto
74 b855148c 2004-05-16 devnull pagebbox 2 get pagebbox 3 get tempmatrix transform lineto
75 b855148c 2004-05-16 devnull pagebbox 2 get pagebbox 1 get tempmatrix transform lineto
76 b855148c 2004-05-16 devnull closepath pathbbox
77 b855148c 2004-05-16 devnull 2 index sub /formheight exch def
78 b855148c 2004-05-16 devnull 2 index sub /formwidth exch def
79 b855148c 2004-05-16 devnull } {2 copy} ifelse
82 b855148c 2004-05-16 devnull % Top two numbers are the displacement from the job's origin to the lower left
83 b855148c 2004-05-16 devnull % corner of each page image when we finish setting up the new coordinate system.
86 b855148c 2004-05-16 devnull /ycorner exch def
87 b855148c 2004-05-16 devnull /xcorner exch def
90 b855148c 2004-05-16 devnull % The two numbers left on the stack are the coordinates of the lower left corner
91 b855148c 2004-05-16 devnull % of the clipping path. Go there and then up a bit so page images can be outlined.
94 b855148c 2004-05-16 devnull translate
95 b855148c 2004-05-16 devnull slop slop translate
98 b855148c 2004-05-16 devnull % If the page is wider than high we may be able to do better if we exchange rows
99 b855148c 2004-05-16 devnull % and columns. Won't make a difference in the current orientation or if rows and
100 b855148c 2004-05-16 devnull % columns are the same.
103 b855148c 2004-05-16 devnull pagewidth pageheight gt {
104 b855148c 2004-05-16 devnull rows columns /rows exch def /columns exch def
108 b855148c 2004-05-16 devnull % Find the orientation and scaling that makes things as large as possible. More
109 b855148c 2004-05-16 devnull % than what's really needed. First calculation essentially finds the minimum of
110 b855148c 2004-05-16 devnull % 1/rows and 1/columns.
113 b855148c 2004-05-16 devnull pagewidth formwidth columns mul div pageheight formheight rows mul div min
114 b855148c 2004-05-16 devnull pageheight formwidth columns mul div pagewidth formheight rows mul div min
116 b855148c 2004-05-16 devnull 2 copy lt {
117 b855148c 2004-05-16 devnull rotation 1 eq {
118 b855148c 2004-05-16 devnull landscape {
119 b855148c 2004-05-16 devnull 0 pageheight translate
120 b855148c 2004-05-16 devnull -90 rotate
122 b855148c 2004-05-16 devnull pagewidth 0 translate
123 b855148c 2004-05-16 devnull 90 rotate
124 b855148c 2004-05-16 devnull } ifelse
126 b855148c 2004-05-16 devnull landscape {
127 b855148c 2004-05-16 devnull pagewidth 0 translate
128 b855148c 2004-05-16 devnull 90 rotate
130 b855148c 2004-05-16 devnull 0 pageheight translate
131 b855148c 2004-05-16 devnull -90 rotate
132 b855148c 2004-05-16 devnull } ifelse
133 b855148c 2004-05-16 devnull } ifelse
134 b855148c 2004-05-16 devnull pagewidth pageheight /pagewidth exch def /pageheight exch def
139 b855148c 2004-05-16 devnull % Second number from the top is the best choice. Scale so everything will fit on
140 b855148c 2004-05-16 devnull % the current page, go back to the original origin, and then get ready for the
141 b855148c 2004-05-16 devnull % first page - which goes in the upper left corner.
144 b855148c 2004-05-16 devnull pop dup dup scale
145 b855148c 2004-05-16 devnull xcorner neg ycorner neg translate
146 b855148c 2004-05-16 devnull 0 rows 1 sub formheight mul translate
149 b855148c 2004-05-16 devnull % Try to center everything on the page - scaling we used is on top of the stack.
152 b855148c 2004-05-16 devnull dup pagewidth exch div formwidth columns mul sub 2 div
153 b855148c 2004-05-16 devnull exch pageheight exch div formheight rows mul sub 2 div translate
156 b855148c 2004-05-16 devnull % Redefine showpage.
159 b855148c 2004-05-16 devnull /!PreForms~showpage~ /showpage load def % save current showpage
161 b855148c 2004-05-16 devnull /showpage {
162 b855148c 2004-05-16 devnull saveobj restore
163 b855148c 2004-05-16 devnull % initclip
164 b855148c 2004-05-16 devnull formsperpage 1 gt {
165 b855148c 2004-05-16 devnull gsave .1 setlinewidth outlineform stroke grestore
167 b855148c 2004-05-16 devnull formwidth 0 translate
168 b855148c 2004-05-16 devnull /currentform currentform 1 add def
169 b855148c 2004-05-16 devnull currentform columns mod 0 eq {
170 b855148c 2004-05-16 devnull columns formwidth mul neg formheight neg translate
172 b855148c 2004-05-16 devnull currentform formsperpage mod 0 eq {
173 b855148c 2004-05-16 devnull gsave !PreForms~showpage~ grestore
174 b855148c 2004-05-16 devnull currentform columns mod formwidth mul neg
175 b855148c 2004-05-16 devnull formsperpage columns idiv formheight mul translate
176 b855148c 2004-05-16 devnull /currentform 0 def
178 b855148c 2004-05-16 devnull % outlineform clip newpath
179 b855148c 2004-05-16 devnull /saveobj save def
180 b855148c 2004-05-16 devnull } bind def
182 b855148c 2004-05-16 devnull /outlineform {
184 b855148c 2004-05-16 devnull xcorner ycorner moveto
185 b855148c 2004-05-16 devnull formwidth 0 rlineto
186 b855148c 2004-05-16 devnull 0 formheight rlineto
187 b855148c 2004-05-16 devnull formwidth neg 0 rlineto
188 b855148c 2004-05-16 devnull closepath
189 b855148c 2004-05-16 devnull } bind def
191 b855148c 2004-05-16 devnull /lastpage {
192 b855148c 2004-05-16 devnull formsperpage 1 gt {
193 b855148c 2004-05-16 devnull currentform 0 ne {
194 b855148c 2004-05-16 devnull /saveobj save def
195 b855148c 2004-05-16 devnull 0 1 formsperpage currentform sub formsperpage mod {
196 b855148c 2004-05-16 devnull pop showpage
198 b855148c 2004-05-16 devnull saveobj restore
200 b855148c 2004-05-16 devnull saveobj restore
201 b855148c 2004-05-16 devnull saveobj restore
206 b855148c 2004-05-16 devnull % Clip the first page image and save the environment we just set up, including
207 b855148c 2004-05-16 devnull % the redefined showpage.
210 b855148c 2004-05-16 devnull % outlineform clip
212 b855148c 2004-05-16 devnull /saveobj save def