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

  1. %    Copyright (C) 1998 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: cid2code.ps,v 1.1 2000/03/09 08:40:39 lpd Exp $
  16. % Construct an inverse map from CIDs to codes.
  17.  
  18. % Create an inverse map from CIDs to code values.
  19. % We only use this for 16-bit Unicode, so it has some limitations.
  20. % After invoking .cmap2code, loading a CMap file prints out the map
  21. % instead of doing what it usually does.
  22.  
  23. /.cmap2codedict 10 dict begin
  24.  
  25. /begincmap {
  26.   mark
  27. } def
  28. /endcmap {
  29.         % Stack: mark code_lo1 code_hi1 cid1 ...
  30.   20 dict begin
  31.     /depth counttomark 3 sub def
  32.         % Do a first pass to determine the maximum CID.
  33.     0 0 3 depth {
  34.       1 add /d exch def
  35.       d index d 2 add index 1 get add d 3 add index 1 get sub .max
  36.     } for
  37.     1 add /ncid exch def
  38.     /map ncid 2 mul string def
  39.         % Now fill in the map.
  40.     0 3 depth {
  41.       /d exch def
  42.       d index 2 mul /cid2 exch def
  43.       d 1 add index /hi exch def
  44.       d 2 add index 2 string copy /lo exch def
  45.       lo 1 get 1 hi 1 get {
  46.     map cid2 lo 0 get put
  47.     map cid2 1 add 3 -1 roll put
  48.     /cid2 cid2 2 add def
  49.       } for
  50.     } for
  51.         % Print the map.
  52.     (%stdout) (w) file
  53.       dup (<) print
  54.       dup /ASCIIHexEncode filter
  55.     dup map writestring
  56.       closefile
  57.       () = flush
  58.     closefile
  59.   end
  60. } def
  61. %/begincodespacerange
  62. /endcodespacerange {cleartomark} def
  63. %/usecmap
  64.  
  65. %/beginbfchar
  66. /endbfchar {cleartomark} def
  67. %/beginbfrange
  68. /endbfrange {cleartomark} def
  69.  
  70. %/begincidchar
  71. /endcidchar {
  72.   counttomark 2 idiv { dup counttomark 1 add 3 roll } repeat pop
  73. } def
  74. %/begincidrange
  75. /endcidrange {
  76.   counttomark 1 add -1 roll pop
  77. } def
  78.  
  79. %/beginnotdefchar
  80. /endnotdefchar {cleartomark} def
  81. %/beginnotdefrange
  82. /endnotdefrange {cleartomark} def
  83.  
  84. currentdict end readonly def
  85.  
  86. /.cmap2code {        % - .cmap2code -
  87.   /CIDInit /ProcSet findresource dup length dict copy
  88.   .cmap2codedict { 3 copy put pop pop } forall
  89.   /CIDInit exch /ProcSet defineresource pop
  90. } def
  91.  
  92. % Extract and print reverse mapping information from a cid2code.txt file.
  93. /.printhex2 {        % <int16> .printhex2 -
  94.   (<) print
  95.   16#10000 add 16 =string cvrs 1 4 getinterval print
  96.   (>) print
  97. } def
  98. /.cid2code {        % <cmaptemplate> <file> <column> .cid2code -
  99.   30 dict begin
  100.   /column exch def
  101.   (r) file /f exch def
  102.   (%!) =
  103.   (/CIDInit /ProcSet findresource begin   12 dict begin   begincmap) =
  104.         % Print the information from the template.
  105.   {
  106.     exch ==only ( ) print
  107.     dup type /dicttype eq {
  108.       dup length =only ( dict dup begin) = {
  109.     (  ) print exch ===only ( ) print ===only ( def) =
  110.       } forall (end def) =
  111.     } {
  112.       ===only
  113.     } ifelse ( def) =
  114.   } forall
  115.         % Read the data from the cid2code.txt file.
  116.   {
  117.     f =string readline pop (CID\t) anchorsearch { pop pop exit } if pop
  118.   } loop
  119.   /map [ {
  120.     f =string readline not { pop exit } if
  121.     column { (\t) search pop pop pop } repeat
  122.     (\t) search { exch pop exch pop } if
  123.     (,) search { exch pop exch pop } if
  124.     dup length 4 ne { pop (*) } if
  125.     dup (*) eq { pop (0000) } if
  126.     (16#) exch concatstrings cvi
  127.   } loop ] def
  128.         % Print the code space range(s).
  129.   /maxcid map length 1 sub def
  130.   mark maxcid
  131.   dup 255 and 255 eq {
  132.     0 exch
  133.   } {
  134.     dup 16#ff00 and exch 0 2 index 1 sub
  135.   } ifelse
  136.   counttomark 2 idiv dup =only ( begincodespacerange) = {
  137.     exch .printhex2 .printhex2 () =
  138.   } repeat (endcodespacerange) =
  139.         % Print the map data.
  140.   0 1 100 maxcid {
  141.     /lo exch def
  142.     /hi lo 99 add maxcid .min def
  143.     0 lo 1 hi { map exch get 0 ne { 1 add } if } for
  144.     dup 0 eq {
  145.       pop
  146.     } {
  147.       =only ( begincidchar) = lo 1 hi {
  148.     map 1 index get dup 0 eq { pop pop } { exch .printhex2 = } ifelse
  149.       } for (endcidchar) =
  150.     } ifelse
  151.   } for
  152.         % Wrap up.
  153.   (endcmap   CMapName currentdict /CMap defineresource pop   end   end) =
  154.   f closefile
  155.   end
  156. } bind def