1 % Copyright (C) 1994, 1995, 1996 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: packfile.ps,v 1.5 2002/06/02 12:03:28 mpsuzuki Exp $
18 % Pack groups of files together, with compression, for use in
19 % storage-scarce environments.
21 % ****** NOTE: This file must be kept consistent with gs_pfile.ps.
23 % ---------------- Huffman coding utilities ---------------- %
25 % We count runs of zeros, and individual byte frequencies separately
26 % depending on whether they follow or do not follow a run of zeros.
30 /maxcode 13 def % max code length, must be between 10 and 16
31 /maxzrun 100 def % max length of zero run, must be <= 100
32 /statbuf 10000 string def
34 % Initialize statistics.
35 /initstats % - initstats -
36 { 0 1 255 { zruns exch 0 put } for
37 0 1 255 { zfreq exch 0 put } for
38 0 1 255 { nzfreq exch 0 put } for
41 % Accumulate statistics from an individual file.
42 /addstats % <file> addstats -
44 { 1 index //statbuf readstring 3 1 roll
45 % Stack: file eof numzeros data
51 //zruns exch 2 copy get 1 add put
57 exch 2 copy get 1 add put
61 exch not { exit } if (.) print flush
66 % Compute the Huffman codes from the statistics.
67 /statcodes % - statcodes <array>
68 { maxcode 1 add 256 add maxzrun 2 sub add 1 add array % full array
69 dup maxcode 1 add dup 2 index length exch sub getinterval % data
70 % Put statistics into array
72 { zfreq 1 index get nzfreq 2 index get add put dup
75 256 zruns 2 maxzrun 2 sub getinterval putinterval
76 dup dup length 1 sub 1 put % EOD
80 % ---------------- File handling ---------------- %
82 % Copy one file to another.
83 % Close the input file, but not the output file.
84 /copyfile % <infile> <outfile> copyfile <outfile> <length>
86 { 4 index 1 index readstring
87 exch 5 index 1 index writestring
88 length 5 -1 roll add 4 1 roll
89 not { exit } if (.) print flush
91 cleartomark 3 -1 roll closefile dup == flush
94 % Represent a Type 1 font in its most compressed format.
95 % Requires -dWRITESYSTEMDICT to run.
96 % Does not close the output file.
97 (wrfont.ps) runlibfile
98 /compressfont % <fontname> <outfile> compressfont <outfile>
100 systemdict /executeonly /readonly load put
101 systemdict /noaccess /readonly load put
102 systemdict readonly pop
104 /binary_CharStrings true def
105 /binary_tokens true def
106 /encrypt_CharStrings false def
107 /standard_only false def
109 /smallest_output true def
111 exch findfont setfont
116 % ---------------- Main program ---------------- %
118 % Find the length of a file.
119 /filelength % <filename> filelength <length>
120 { status { pop pop exch pop } { -1 } ifelse
123 % Define the header string for a compressed file.
125 ({dup token pop exch[/MaxCodeLength 2 index token pop/Tables 4 index token pop
126 /EndOfData true/EncodeZeroRuns 256 .dicttomark
127 /BoundedHuffmanDecode filter/MoveToFrontDecode filter
128 [/BlockSize 4 -1 roll .dicttomark/BWBlockSortDecode filter
131 % Write a 16-bit big-endian non-negative integer on a file.
132 /write16 % <file> <int> write16 -
133 { 2 copy -8 bitshift write 255 and write
136 % Compress a group of files together.
137 % Return a dictionary in which the keys are the input file names
138 % and the values are [startpos length] in the uncompressed concatenation.
139 % Does not open or close the output file.
141 /packfiles % <filenames> <outfile> packfiles <outfile> <posdict>
142 { % Concatenate files to a temporary file.
144 dup /MoveToFrontEncode filter
147 >> /BWBlockSortEncode filter
148 % Stack: filenames outfile tempfile mtfe bwe
149 5 -1 roll dup length dict 0 6 2 roll
150 { % Stack: outfile posdict pos tempfile mtfe bwe infilename
151 dup ==only dup (r) file 2 index copyfile exch pop
152 dup 7 index 4 2 roll 7 index exch 2 array astore put
153 5 -1 roll add 4 1 roll
155 closefile closefile closefile pop exch
156 % Stack: posdict outfile
157 % Compute an optimal Huffman code.
159 tempname (r) file addstats
160 % Actually compress the file.
161 % Write the decompression information on the output first.
162 dup tempname filelength write==
164 % Write the code table as a homogenous number array.
166 dup 149 write dup 32 write dup 2 index length write16
167 exch { 2 copy write16 pop } forall
169 /MaxCodeLength maxcode
173 >> /BoundedHuffmanEncode filter
174 tempname (r) file exch copyfile pop closefile
178 % Squeeze a font to a .cpf file in anticipation of compression.
179 /squeezefont % <fontname> squeezefont <filename.cpf>
180 { dup type /nametype ne { cvn } if
182 { dup type /stringtype eq { exit } if
186 % Stack: fontname filename
188 { (.) search not { exit } if
189 exch pop exch 3 -1 roll pop
192 % Stack: fontname filename noextname extension
194 { (/) search not { (\\) search not { exit } if } if
198 % If the font extension is anything other than
199 % .pfa or .pfb, we assume it can't be rewritten
200 % using compressfont.
201 % Stack: fontname filename extension basename
202 (.cpf) concatstrings dup 5 1 roll (w) file
203 % Stack: outfilename fontname filename extension outfile
204 exch dup (pfa) eq exch (pfb) eq or
205 % Stack: outfilename fontname filename outfile bool
206 { exch pop compressfont
209 exch findlibfile pop exch pop
215 % ---------------- Production code ---------------- %
217 % The following code constructs a packed version of the commercial-quality
218 % fonts available from Aladdin Enterprises. To use this code:
219 % - If desired, change the output file names below.
220 % - Make sure you have the synthetic font data (fontmap.shs and the
221 % *.ps files for the commercial fonts) in a directory that is on
222 % Ghostscript's search path.
223 % - Construct the packed fonts by running
224 % gs -dNODISPLAY -dWRITESYSTEMDICT packfile.ps
225 % - If desired, move the output files to the directory that will be
226 % used at run time. You no longer need the *.pfb or *.ps files
227 % for the original fonts; however, you do still need the Fontmap
228 % for these fonts, because it defines the font name aliases.
229 % - Add the following line to the end of gs_fonts.ps:
231 % (substituting the definition of allmapname if you changed it).
233 % Define the output file names. The extensions are arbitrary;
234 % any legal file name is allowed.
235 /allname (ALL.cff) def % the compressed font file
236 /allmapname (ALL.cmp) def % the Fontmap override file
238 % Load an alternate Fontmap that references the synthetic oblique and
241 (fontmap.shs) findlibfile pop exch pop .loadFontmap
244 % Define the packaging of fonts into font groups.
245 % Fewer larger groups compress better, but make decompression slower.
247 [ % The oblique and narrow fonts are synthetic,
248 % and take very little space.
249 /AvantGarde-BookOblique /AvantGarde-DemiOblique
250 /Courier-Oblique /Courier-BoldOblique
251 /Helvetica-Oblique /Helvetica-BoldOblique
252 /Helvetica-Narrow /Helvetica-Narrow-Oblique
253 /Helvetica-Narrow-Bold /Helvetica-Narrow-BoldOblique
255 [/AvantGarde-Book /AvantGarde-Demi
256 /Bookman-Light] [/Bookman-LightItalic
257 /Bookman-Demi /Bookman-DemiItalic
258 /Courier] [/Courier-Bold
259 /Helvetica /Helvetica-Bold]
260 [/NewCenturySchlbk-Roman /NewCenturySchlbk-Italic
261 /NewCenturySchlbk-Bold /NewCenturySchlbk-BoldItalic]
262 [/Palatino-Roman /Palatino-Italic
263 /Palatino-Bold /Palatino-BoldItalic]
264 [/Times-Roman /Times-Italic
265 /Times-Bold /Times-BoldItalic]
267 /ZapfChancery-MediumItalic
271 % We need to register the fonts under their true names, not aliases.
272 /Lists Lists mark exch
274 { { Fontmap 1 index get dup type /nametype ne { pop exit } if
283 % Squeeze the fonts to their .cpf format.
284 (Squeezing... ) print flush
287 { { dup squeezefont } forall } forall
289 (done.\n) print flush
292 /f2dict fdict length dict def
293 fdict { exch f2dict 3 1 roll put } forall
295 % Construct the compressed font file.
296 (Creating ) print allname print (... ) print flush
297 /posdict fdict length dict def
298 /all allname (w) file def
299 all beginfilestring writestring
302 /fbegin all fileposition def
303 mark exch { fdict exch get } forall ]
304 all packfiles exch pop
305 /flength all fileposition fbegin sub def
306 { fbegin flength 3 -1 roll aload pop 4 packedarray
307 exch f2dict exch get exch posdict 3 1 roll put
313 (done.\n) print flush
315 % Write the Fontmap addendum for accessing compressed fonts.
316 (Writing ) print allmapname print (... ) print flush
319 /.runpackedlibfile where{pop}{(gs_pfile.ps)runlibfile}ifelse
320 .currentglobal true .setglobal
323 { exch 2 index exch write==only exch dup ({) writestring
324 dup allname write==only
325 exch { 1 index dup ( ) writestring exch write==only } forall
326 dup ( .runpackedlibfile}bind .definefontmap
333 (done.\n) print flush