home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-osu / up / HowTo next >
Encoding:
Text File  |  1993-07-14  |  5.6 KB  |  197 lines

  1. Quick summary of what this package does, and how to duplicate its
  2. effects if you don't like my code.
  3.  
  4. A conforming PostScript file, for my purposes, is anything that meets
  5. all of the following requirements:
  6.  
  7. 1) first line must begin with the string '%!PS-Adobe'.
  8. 2) Each page must begin with a '%%Page:' line, and the graphics state
  9.    must be the default at that point (no translation, scaling, or
  10.    rotation of the coordinate space can carry over between pages).
  11.    The pages must be numbered starting at 1, which is a bit pedantic
  12.    of me.  It's part of the spec, but it is by no means necessary for
  13.    the code to work.  It's a one-line check, and I'm thinking of just
  14.    scrapping it.
  15. 3) There must be a '%%Trailer' line.
  16.  
  17.  
  18. As for what I *do* with all this, it's simple:
  19.  
  20. 1) When I see the '%!PS-Adobe' line, I insert the following lines
  21.    (ignoring the old '%!PS' line):
  22.     %!PS-Adobe-2.0
  23.     %%Pages: (atend)
  24.    Page counts in the header are first-comes, first-served, so putting
  25.    mine at the very top will override anything that was in the
  26.    original file (and since the whole idea is to change the number of
  27.    pages, this is 'a good thing').  Since I do everything in one pass,
  28.    I have no idea how many pages there will be, so I defer it until
  29.    the end.
  30. 2) When I reach the end of the header comments (signified by the first
  31.    non-%% line or an explicit %%EndComments line), I insert the
  32.    user-defined prolog definitions into their own dictionary.  To
  33.    simplify nesting, I use the current PID as part of the dictionary
  34.    name.  I save the old value of showpage here, and redefine it to
  35.    print nothing.  It looks like this (modulo interpretation of
  36.    variables marked by <>):
  37.     %%BeginProcSet: up_prolog 1 <pid>
  38.     /UpDict<pid> $plines 3 add dict def
  39.     UpDict<pid> begin
  40.     <user prolog>
  41.     /UpShowpage {showpage} bind def
  42.     /UpState {} def
  43.     end
  44.     /showpage {} def
  45.     %%EndProcSet: up_prolog 1 <pid>
  46. 3) For every '%%Page:' line, I first delete it, and then, if it is the
  47.    first page on a sheet, I save the current VM state, and insert the
  48.    user-specified scaling, rotation, and translation commands.  It
  49.    looks like this (modulo the interpolation of the variables marked
  50.    by <>):
  51.     %%Page: ? <sheet number>
  52.     UpDict<pid> begin
  53.     save /UpState exch def
  54.     <even/odd>
  55.     <scale, translate, rotate>
  56.     <page positioning>
  57.     end
  58.    For pages that don't start a sheet, I just insert:
  59.     UpDict<pid> begin
  60.     <page positioning>
  61.     end
  62. 5) At the end of every sheet, I restore the VM state, and perform a
  63.    real showpage, like so:
  64.     UpDict<pid> begin UpState restore UpShowpage end
  65. 6) When I see the '%%Trailer' line, I replace it with:
  66.     UpDict<pid> begin UpState restore UpShowpage end
  67.     %%Trailer
  68.    You might suspect that this causes an error, or the printing of a
  69.    blank page, and if everything I said above were true, you'd be
  70.    right.  The catch is that #5 is a lie.  I detect the end of a sheet
  71.    by finding myself at the beginning of a new one, so the last sheet
  72.    never gets printed, unless it's done just before the trailer.
  73. 7) The last bit is simple.  After the very last line of the input is
  74.    printed, the actual sheet count is added.  This has to be this way,
  75.    since the *last* in a series of trailer comments is the one heeded.
  76.    It looks like this:
  77.     %%Pages: <sheet count>
  78.  
  79.  
  80. If this isn't entirely clear, here's a before/after.
  81.  
  82. ----------
  83. Conforming PS file, which prints 4 numbered pages (output of "makeup 4"):
  84.  
  85. %!PS-Adobe-1.0
  86. %%Creator: makeup
  87. %%Title: Page Layout Test
  88. %%CreationDate: Tue Oct 24 02:33:36 EDT 1989
  89. %%Pages: (atend)
  90. %%DocumentFonts: Times-Roman
  91. %%BoundingBox: 0 0 612 792
  92. %%EndComments
  93. /inch {72 mul} def
  94. /Nfont /Times-Roman findfont 5 inch scalefont def
  95. /drawpage {
  96.     2 setlinecap 3 setlinewidth 0 setgray
  97.     Nfont setfont
  98.     dup stringwidth
  99.     11 inch exch sub 2 div
  100.     exch 8.5 inch exch sub 2 div
  101.     exch moveto show
  102.     0.25 inch dup moveto
  103.     8 inch 0 rlineto
  104.     0 10.5 inch rlineto
  105.     -8 inch 0 rlineto
  106.     0 -10.5 inch rlineto
  107.     closepath stroke
  108.     showpage
  109. } def
  110. %%EndProlog
  111. %%Page: ? 1
  112. (1) drawpage
  113. %%Page: ? 2
  114. (2) drawpage
  115. %%Page: ? 3
  116. (3) drawpage
  117. %%Page: ? 4
  118. (4) drawpage
  119. %%Trailer
  120. %%Pages: 4
  121.  
  122. ----------
  123. Previous file, filtered to print two pages per sheet ("up -n 2up"):
  124.  
  125. %!PS-Adobe-2.0
  126. %%Pages: (atend)
  127. %%Creator: makeup
  128. %%Title: Page Layout Test
  129. %%CreationDate: Tue Oct 24 02:33:36 EDT 1989
  130. %%Pages: (atend)
  131. %%DocumentFonts: Times-Roman
  132. %%BoundingBox: 0 0 612 792
  133. %%EndComments
  134. %%BeginProcSet: up_prolog 1 7482
  135. /UpDict7482 23 3 add dict def
  136. UpDict7482 begin
  137. /inch {72 mul} def
  138. /moveU {0 11 inch translate} def
  139. /moveR {8.5 inch 0 translate} def
  140. /moveD {0 -11 inch translate} def
  141. /moveL {-8.5 inch 0 translate} def
  142. /rotR {-90 rotate} def
  143. /rotL {90 rotate} def
  144. /doSpiral {moveU moveR rotR 0.67 dup scale} def
  145. /moveHU { 0 5.5 inch translate} def
  146. /doRevSpiral {moveHU rotL 0.67 dup scale} def
  147. /UpShowpage {showpage} bind def
  148. /UpState {} def
  149. end
  150. /showpage {} def
  151. %%EndProcSet: up_prolog 1 7482
  152. /inch {72 mul} def
  153. /Nfont /Times-Roman findfont 5 inch scalefont def
  154. /drawpage {
  155.     2 setlinecap 3 setlinewidth 0 setgray
  156.     Nfont setfont
  157.     dup stringwidth
  158.     11 inch exch sub 2 div
  159.     exch 8.5 inch exch sub 2 div
  160.     exch moveto show
  161.     0.25 inch dup moveto
  162.     8 inch 0 rlineto
  163.     0 10.5 inch rlineto
  164.     -8 inch 0 rlineto
  165.     0 -10.5 inch rlineto
  166.     closepath stroke
  167.     showpage
  168. } def
  169. %%EndProlog
  170. %%Page: ? 1
  171. UpDict7482 begin
  172. save /UpState exch def
  173. 7.75 inch 0 translate rotL 11 17 div dup scale
  174. end
  175. (1) drawpage
  176. UpDict7482 begin
  177. moveR
  178. end
  179. (2) drawpage
  180. UpDict7482 begin UpState restore UpShowpage end
  181. %%Page: ? 2
  182. UpDict7482 begin
  183. save /UpState exch def
  184. 7.75 inch 0 translate rotL 11 17 div dup scale
  185. end
  186. (3) drawpage
  187. UpDict7482 begin
  188. moveR
  189. end
  190. (4) drawpage
  191. UpDict7482 begin UpState restore UpShowpage end
  192. %%Trailer
  193. %%Pages: 2
  194.  
  195. --
  196. J Greely (jgreely@cis.ohio-state.edu; osu-cis!jgreely)
  197.