home *** CD-ROM | disk | FTP | other *** search
/ jppd.dyndns.org / jppd.dyndns.org.tar / jppd.dyndns.org / QUERYPRO / Impressora_PDF / converter.exe / GPLGS / packfile.ps < prev    next >
Text File  |  2002-06-03  |  11KB  |  334 lines

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