packfile.ps 10 KB

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