]> git.lizzy.rs Git - plan9front.git/blob - sys/lib/ghostscript/ps2epsi.ps
etherbcm: handle 64-bit host addresses, use PCIWADDR() instead of PADDR()
[plan9front.git] / sys / lib / ghostscript / ps2epsi.ps
1 %    Copyright (C) 1990, 2000 Aladdin Enterprises.  All rights reserved.
2
3 % This software is provided AS-IS with no warranty, either express or
4 % implied.
5
6 % This software is distributed under license and may not be copied,
7 % modified or distributed except as expressly authorized under the terms
8 % of the license contained in the file LICENSE in this distribution.
9
10 % For more information about licensing, please refer to
11 % http://www.ghostscript.com/licensing/. For information on
12 % commercial licensing, go to http://www.artifex.com/licensing/ or
13 % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14 % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15
16 % $Id: ps2epsi.ps,v 1.10 2003/05/07 16:37:17 ray Exp $
17 % Convert an arbitrary PostScript file to an EPSI file.
18 %
19 % Please do not contact these users if you have questions.  They no longer
20 % have the time, interest, or current expertise to keep this code working.
21 % If you find bugs, please send proposed fixes to bug-gs@ghostscript.com.
22 %
23 % Bug fix 2002-04-20 by rayjj: Bounding box was incorrect since it depended
24 %   on the dither pattern and gray shade at the boundary. Changed to use
25 %   8-bit grayscale preview image to allow correct bounding box (at the
26 %   expense of a 8x larger preview image). Also moved .setsafe until after
27 %   the device and file operations are complete (but still before the input
28 %   file is processed.
29 % Bug fix 2000-04-11 by lpd: if a font didn't have a FontName (which is the
30 %   case for bitmap fonts produced by recent versions of dvips), setfont
31 %   caused an error.
32 % Bug fix 8/21/99 by lpd: many of the margin and width computations were
33 %   wrong (off by 1).  The code only "worked" because the bugs were
34 %   (mostly) in conservative directions.
35 % Modified 3/17/98 by lpd to make it possible to run this file without
36 %   running the ps2epsi script first, for debugging.
37 % Bug fix 9/29/97 by lpd <ghost@aladdin.com>: if the page size wasn't an
38 %   exact multiple of 8 bits, an incorrect bounding box (or a rangecheck
39 %   error) could occur.
40 % Patched 7/26/95 by
41 %       Greg P. Kochanski <gpk@bell-labs.com>
42 %   to add many new DSC comments and make the comments conforming.
43 % Original version contributed by
44 %       George Cameron <george@bio-medical-physics.aberdeen.ac.uk>
45 %
46
47 % Initialize, and redefine copypage and showpage.
48
49 % ps2edict is normally defined in the pre-loaded code created by the
50 % ps2epsi script.
51 /ps2edict where { pop } { /ps2edict 25 dict def } ifelse
52 ps2edict begin
53
54                                 % The main procedure
55   /ps2epsi
56    {                            % Open the file
57      outfile (w) file /epsifile exch def
58                                         % Get the device parameters
59      currentdevice getdeviceprops .dicttomark 
60      /HWSize get aload pop
61        /devheight exch def
62        /devwidth exch def
63      matrix defaultmatrix
64        /devmatrix exch def
65                                 % Make a corresponding 8-bit deep memory device
66      devmatrix devwidth devheight
67      256 string 0 1 255 { 1 index exch dup 255 exch sub put } for
68      makeimagedevice
69      /arraydevice exch def
70      arraydevice
71      % Turn on anti-aliasing
72      mark /TextAlphaBits 4 /GraphicsAlphaBits 4 6 -1 roll
73      putdeviceprops
74      setdevice  % (does an erasepage)
75      /rowwidth devwidth def
76      /row rowwidth string def
77      /zerorow rowwidth string def       % all zero
78                                 % Replace the definition of showpage
79      userdict /showpage { ps2edict begin epsipage end } bind put
80      userdict /setfont { ps2edict begin epsisetfont end } bind put
81      //systemdict /.setsafe known { .setsafe } if
82    } bind def
83
84  /epsifontdict 100 dict def
85
86  /epsisetfont
87  {
88  % code here keeps a list of font names in dictionary epsifontdict
89  /tmpfont exch def
90  tmpfont /FontName known {
91    /tmpfontname tmpfont /FontName get def
92    epsifontdict tmpfontname known not { epsifontdict tmpfontname 0 put } if
93    epsifontdict tmpfontname 2 copy get 1 add put
94  } if
95  tmpfont setfont
96  } bind def
97
98 % Get a scan line from the memory device, zeroing any bits beyond
99 % the device width.
100 /getscanline {          % <device> <y> <string> getscanline <string>
101   dup 4 1 roll copyscanlines pop
102   16#ff00 devwidth 7 and neg bitshift 255 and
103   dup 0 ne {
104     1 index dup length 1 sub 2 copy get 4 -1 roll and put
105   } {
106     pop
107   } ifelse
108 } bind def
109
110 /margintest {           % <y-start> <step> <y-limit> margintest <y-non-blank>
111                         % <y-start> <step> <y-limit> margintest -
112   { dup arraydevice exch row getscanline
113     zerorow ne { exit } if pop
114   } for
115 } bind def
116
117
118   /epsiNameStr 200 string def
119   /epsiNpages 0 def
120   /epsiNpageStr 20 string def
121   /epsipage
122    {      
123      /epsiNpages epsiNpages 1 add def
124      /loopcount devheight 1 sub def
125
126      % Find top margin -- minimum Y of non-blank scan line.
127      -1 0 1 loopcount margintest
128      dup -1 eq { (blank page!!\n) print quit }{ exch pop } ifelse 
129      /tm exch def
130
131      % Find bottom margin -- maximum Y of non-blank scan line.
132      loopcount -1 0 margintest
133      /bm exch def
134      
135      % Initialise limit variables
136      /loopcount rowwidth 1 sub def
137      /lm loopcount def 
138      /rm 0 def 
139
140      % Find left and right boundaries of image
141      tm 1 bm
142       { % Get more data
143         arraydevice exch row getscanline pop
144         % Scan from left to find first non-zero element
145         % We save first the element, then the index
146         -1 0 1 loopcount
147         { dup row exch get 0 ne { exch pop exit }{ pop } ifelse
148         } for
149         % If we found -1, row is blank ..
150         dup -1 ne 
151         { % Find the leftmost index
152           dup lm lt
153           % If the new index is less, we save index and element
154           { /lm exch def } { pop } ifelse
155           % Now find the rightmost index
156           loopcount -1 0
157           { dup row exch get 0 ne { exit }{ pop } ifelse
158           } for
159           dup rm gt
160           % If the new index is greater, we save index and element
161           { /rm exch def } { pop } ifelse
162         } if
163       } for
164
165      % Calculate the bounding box values.
166      % Note that these must be corrected to produce closed-open intervals.
167      /llx lm def
168      /lly devheight bm sub 1 sub def
169      /urx rm 1 add def
170      /ury devheight tm sub def
171
172     % Write out the magic string and bounding box information
173      epsifile (%!PS-Adobe-2.0 EPSF-1.2\n) writestring
174          /epsititle where { pop epsifile epsititle writestring } if
175          /epsicreator where { pop epsifile epsicreator writestring } if
176          /epsicrdt where { pop epsifile epsicrdt writestring } if
177          /epsifor where { pop epsifile epsifor writestring } if
178      epsifile flushfile
179
180         % Write out the page count:
181          epsifile (%%Pages: ) writestring
182          epsifile epsiNpages epsiNpageStr cvs writestring
183          epsifile (\n) writestring
184      epsifile flushfile
185
186         % Write out the list of used fonts:
187          epsifile (%%DocumentFonts:) writestring
188          epsifontdict {
189                                         epsifile ( ) writestring
190                                         pop epsiNameStr cvs epsifile exch writestring
191                                         } forall
192          epsifile (\n) writestring
193      epsifile flushfile
194
195      epsifile (%%BoundingBox: ) writestring
196      epsifile llx write==only epsifile ( ) writestring
197      epsifile lly write==only epsifile ( ) writestring
198      epsifile urx write==only epsifile ( ) writestring
199      epsifile ury write==
200
201     % Define character and bit widths for the output line buffer:
202      /cwidth rm lm sub 1 add def
203      /out cwidth string def
204
205      epsifile (%%EndComments\n\n) writestring
206      epsifile (%%BeginProlog\n) writestring
207      epsifile (%%BeginPreview: ) writestring
208      epsifile cwidth write==only epsifile ( ) writestring
209      epsifile bm tm sub 1 add write==only epsifile ( 8 ) writestring
210      epsifile bm tm sub 1 add
211      cwidth 39 add 40 idiv mul write==
212      epsifile flushfile
213
214      gsave
215
216      tm 1 bm
217       { % Get a scan line interval from the array device
218         arraydevice exch row copyscanlines lm cwidth getinterval
219         % Write out the hex data as 40 bytes per line (82 chars)
220         0 40 cwidth
221          { epsifile (% ) writestring 
222            epsifile exch 2 index exch
223            dup cwidth exch sub 40 .min getinterval writehexstring
224            epsifile (\n) writestring
225          } for
226         pop
227       } for
228
229      epsifile (%%EndImage\n) writestring
230      epsifile (%%EndPreview\n) writestring
231      epsifile flushfile
232      grestore
233      erasepage initgraphics
234
235      DonePage 0 1 put
236    } bind def
237
238
239 (outfile) getenv
240   { /outfile exch def 
241     ps2epsi
242
243     /DonePage 1 string def
244     (%stdin) (r) file cvx execute0
245     DonePage 0 get 0 eq { showpage } if
246   } if
247
248 end
249 quit