1 % Copyright (C) 1990, 2000 Aladdin Enterprises. All rights reserved.
3 % This software is provided AS-IS with no warranty, either express or
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.
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.
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.
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.
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
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
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
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>
47 % Initialize, and redefine copypage and showpage.
49 % ps2edict is normally defined in the pre-loaded code created by the
51 /ps2edict where { pop } { /ps2edict 25 dict def } ifelse
57 outfile (w) file /epsifile exch def
58 % Get the device parameters
59 currentdevice getdeviceprops .dicttomark
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
71 % Turn on anti-aliasing
72 mark /TextAlphaBits 4 /GraphicsAlphaBits 4 6 -1 roll
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
84 /epsifontdict 100 dict def
88 % code here keeps a list of font names in dictionary epsifontdict
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
98 % Get a scan line from the memory device, zeroing any bits beyond
100 /getscanline { % <device> <y> <string> getscanline <string>
101 dup 4 1 roll copyscanlines pop
102 16#ff00 devwidth 7 and neg bitshift 255 and
104 1 index dup length 1 sub 2 copy get 4 -1 roll and put
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
118 /epsiNameStr 200 string def
120 /epsiNpageStr 20 string def
123 /epsiNpages epsiNpages 1 add def
124 /loopcount devheight 1 sub def
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
131 % Find bottom margin -- maximum Y of non-blank scan line.
132 loopcount -1 0 margintest
135 % Initialise limit variables
136 /loopcount rowwidth 1 sub def
140 % Find left and right boundaries of image
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
147 { dup row exch get 0 ne { exch pop exit }{ pop } ifelse
149 % If we found -1, row is blank ..
151 { % Find the leftmost index
153 % If the new index is less, we save index and element
154 { /lm exch def } { pop } ifelse
155 % Now find the rightmost index
157 { dup row exch get 0 ne { exit }{ pop } ifelse
160 % If the new index is greater, we save index and element
161 { /rm exch def } { pop } ifelse
165 % Calculate the bounding box values.
166 % Note that these must be corrected to produce closed-open intervals.
168 /lly devheight bm sub 1 sub def
170 /ury devheight tm sub def
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
180 % Write out the page count:
181 epsifile (%%Pages: ) writestring
182 epsifile epsiNpages epsiNpageStr cvs writestring
183 epsifile (\n) writestring
186 % Write out the list of used fonts:
187 epsifile (%%DocumentFonts:) writestring
189 epsifile ( ) writestring
190 pop epsiNameStr cvs epsifile exch writestring
192 epsifile (\n) writestring
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
201 % Define character and bit widths for the output line buffer:
202 /cwidth rm lm sub 1 add def
203 /out cwidth string def
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==
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)
221 { epsifile (% ) writestring
222 epsifile exch 2 index exch
223 dup cwidth exch sub 40 .min getinterval writehexstring
224 epsifile (\n) writestring
229 epsifile (%%EndImage\n) writestring
230 epsifile (%%EndPreview\n) writestring
233 erasepage initgraphics
243 /DonePage 1 string def
244 (%stdin) (r) file cvx execute0
245 DonePage 0 get 0 eq { showpage } if