home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / GhostScript / !GhostScr / 6_01 / lib / packfile.ps < prev    next >
Text File  |  2000-03-09  |  11KB  |  336 lines

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