1
0

packfile.ps 10 KB

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