home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2002 February / INTERNET88.ISO / pc / software / windows / bits / pdf995 / data1.cab / Program_Executable_Files / res / packfile.ps < prev    next >
Encoding:
Text File  |  2001-12-08  |  10.7 KB  |  337 lines

  1. %    Copyright (C) 1994, 1995, 1996 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of GNU Ghostscript.
  3. % GNU Ghostscript is distributed in the hope that it will be useful, but
  4. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  5. % to anyone for the consequences of using it or for whether it serves any
  6. % particular purpose or works at all, unless he says so in writing.  Refer
  7. % to the GNU General Public License for full details.
  8. % Everyone is granted permission to copy, modify and redistribute GNU
  9. % Ghostscript, but only under the conditions described in the GNU General
  10. % Public License.  A copy of this license is supposed to have been given
  11. % to you along with GNU Ghostscript so you can know your rights and
  12. % responsibilities.  It should be in a file named COPYING.  Among other
  13. % things, the copyright notice and this notice must be preserved on all
  14. % copies.
  15.  
  16. % $RCSfile: packfile.ps,v $ $Revision: 1.2.2.1 $
  17. % packfile.ps
  18. % Pack groups of files together, with compression, for use in
  19. % storage-scarce environments.
  20.  
  21. % ****** NOTE: This file must be kept consistent with gs_pfile.ps.
  22.  
  23. % ---------------- Huffman coding utilities ---------------- %
  24.  
  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.
  27. /zruns 256 array def
  28. /zfreq 256 array def
  29. /nzfreq 256 array def
  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
  33.  
  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
  39.  } bind def
  40.  
  41. % Accumulate statistics from an individual file.
  42. /addstats        % <file> addstats -
  43.  { 0
  44.     { 1 index //statbuf readstring 3 1 roll
  45.     % Stack: file eof numzeros data
  46.        { dup 0 eq
  47.       { pop 1 add
  48.       }
  49.       { 1 index 0 ne
  50.          { exch 255 min
  51.            //zruns exch 2 copy get 1 add put
  52.            0 exch //zfreq
  53.          }
  54.          { //nzfreq
  55.          }
  56.         ifelse
  57.         exch 2 copy get 1 add put
  58.       }
  59.      ifelse
  60.        } forall
  61.       exch not { exit } if (.) print flush
  62.     } loop
  63.    pop closefile
  64.  } bind def
  65.  
  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
  71.    dup 0 1 255
  72.     { zfreq 1 index get nzfreq 2 index get add put dup
  73.     } for
  74.    0 zruns 1 get put
  75.    256 zruns 2 maxzrun 2 sub getinterval putinterval
  76.    dup dup length 1 sub 1 put    % EOD
  77.    maxcode .computecodes
  78.  } bind def
  79.  
  80. % ---------------- File handling ---------------- %
  81.  
  82. % Copy one file to another.
  83. % Close the input file, but not the output file.
  84. /copyfile        % <infile> <outfile> copyfile <outfile> <length>
  85.  { 0 mark statbuf
  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
  90.     } loop
  91.    cleartomark 3 -1 roll closefile dup == flush
  92.  } bind def
  93.  
  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>
  99.  { exch save
  100.    systemdict /executeonly /readonly load put
  101.    systemdict /noaccess /readonly load put
  102.    systemdict readonly pop
  103.    wrfont_dict begin
  104.      /binary_CharStrings true def
  105.      /binary_tokens true def
  106.      /encrypt_CharStrings false def
  107.      /standard_only false def
  108.      /use_lenIV 0 def
  109.      /smallest_output true def
  110.    end
  111.    exch findfont setfont
  112.    1 index writefont
  113.    restore
  114.  } bind def
  115.  
  116. % ---------------- Main program ---------------- %
  117.  
  118. % Find the length of a file.
  119. /filelength        % <filename> filelength <length>
  120.  { status { pop pop exch pop } { -1 } ifelse
  121.  } bind def
  122.  
  123. % Define the header string for a compressed file.
  124. /beginfilestring
  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
  129. }) readonly def
  130.  
  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
  134.  } bind def
  135.  
  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.
  140. /tempname (t.em) def
  141. /packfiles        % <filenames> <outfile> packfiles <outfile> <posdict>
  142.  {    % Concatenate files to a temporary file.
  143.    tempname (w) file
  144.    dup /MoveToFrontEncode filter
  145.    dup <<
  146.     /BlockSize 1000000
  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
  154.     } forall
  155.    closefile closefile closefile pop exch
  156.         % Stack: posdict outfile
  157.     % Compute an optimal Huffman code.
  158.    initstats
  159.    tempname (r) file addstats
  160.     % Actually compress the file.
  161.     % Write the decompression information on the output first.
  162.    dup tempname filelength write==
  163.    dup maxcode write==
  164.     % Write the code table as a homogenous number array.
  165.    statcodes exch
  166.      dup 149 write dup 32 write dup 2 index length write16
  167.      exch { 2 copy write16 pop } forall
  168.    dup <<
  169.     /MaxCodeLength maxcode
  170.     /EndOfData true
  171.     /EncodeZeroRuns 256
  172.     /Tables statcodes
  173.    >> /BoundedHuffmanEncode filter
  174.    tempname (r) file exch copyfile pop closefile
  175.    exch
  176.  } bind def
  177.  
  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
  181.    dup
  182.     { dup type /stringtype eq { exit } if
  183.       Fontmap exch get
  184.     }
  185.    loop
  186.         % Stack: fontname filename
  187.    dup dup
  188.     { (.) search not { exit } if
  189.       exch pop exch 3 -1 roll pop
  190.     }
  191.    loop
  192.         % Stack: fontname filename noextname extension
  193.    exch
  194.     { (/) search not { (\\) search not { exit } if } if
  195.       pop pop
  196.     }
  197.    loop
  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
  207.     }
  208.     { 3 -1 roll pop
  209.       exch findlibfile pop exch pop
  210.       exch copyfile pop
  211.     }
  212.    ifelse closefile
  213.  } bind def
  214.  
  215. % ---------------- Production code ---------------- %
  216.  
  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:
  230. %        (ALL.cmp) run
  231. %      (substituting the definition of allmapname if you changed it).
  232.  
  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
  237.  
  238. % Load an alternate Fontmap that references the synthetic oblique and
  239. % narrow fonts.
  240. true .setglobal
  241. (fontmap.shs) findlibfile pop exch pop .loadFontmap
  242. false .setglobal
  243.  
  244. % Define the packaging of fonts into font groups.
  245. % Fewer larger groups compress better, but make decompression slower.
  246. /Lists [
  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
  254. ]
  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]
  266. [/Symbol
  267.  /ZapfChancery-MediumItalic
  268.  /ZapfDingbats]
  269. ] def
  270.  
  271. % We need to register the fonts under their true names, not aliases.
  272. /Lists Lists mark exch
  273.  { mark exch
  274.     {  { Fontmap 1 index get dup type /nametype ne { pop exit } if
  275.      exch pop
  276.        }
  277.       loop
  278.     }
  279.    forall ]
  280.  }
  281. forall ] def
  282.  
  283. % Squeeze the fonts to their .cpf format.
  284. (Squeezing... ) print flush
  285. /fdict mark
  286. Lists
  287.  { { dup squeezefont } forall } forall
  288. .dicttomark def
  289. (done.\n) print flush
  290.  
  291. % Invert fdict.
  292. /f2dict fdict length dict def
  293. fdict { exch f2dict 3 1 roll put } forall
  294.  
  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
  300. Lists
  301.  { dup == flush
  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
  308.     }
  309.    forall
  310.  }
  311. forall
  312. all closefile
  313. (done.\n) print flush
  314.  
  315. % Write the Fontmap addendum for accessing compressed fonts.
  316. (Writing ) print allmapname print (... ) print flush
  317. allmapname (w) file
  318. dup (%!
  319. /.runpackedlibfile where{pop}{(gs_pfile.ps)runlibfile}ifelse
  320. .currentglobal true .setglobal
  321. ) writestring
  322. posdict
  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
  327. ) writestring
  328.  }
  329. forall
  330. dup (.setglobal
  331. ) writestring
  332. closefile
  333. (done.\n) print flush
  334.