cid2code.ps 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. % Copyright (C) 1998, 2000 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This software is provided AS-IS with no warranty, either express or
  4. % implied.
  5. %
  6. % This software is distributed under license and may not be copied,
  7. % modified or distributed except as expressly authorized under the terms
  8. % of the license contained in the file LICENSE in this distribution.
  9. %
  10. % For more information about licensing, please refer to
  11. % http://www.ghostscript.com/licensing/. For information on
  12. % commercial licensing, go to http://www.artifex.com/licensing/ or
  13. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  14. % San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  15. % $Id: cid2code.ps,v 1.5 2002/02/21 21:49:28 giles Exp $
  16. % Construct an inverse map from CIDs to codes.
  17. % Create an inverse map from CIDs to code values.
  18. % We only use this for 16-bit Unicode, so it has some limitations.
  19. % After invoking .cmap2code, loading a CMap file prints out the map
  20. % instead of doing what it usually does. For example:
  21. %
  22. % gs -dNODISPLAY -dBATCH lib/cid2code.ps -c .cmap2code\
  23. % -f Resource/CMap/UniJIS-UCS2-H > mapfile
  24. /.cmap2codedict 10 dict begin
  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. %/beginbfchar
  65. /endbfchar {cleartomark} def
  66. %/beginbfrange
  67. /endbfrange {cleartomark} def
  68. %/begincidchar
  69. /endcidchar {
  70. counttomark 2 idiv { dup counttomark 1 add 3 roll } repeat pop
  71. } def
  72. %/begincidrange
  73. /endcidrange {
  74. counttomark 1 add -1 roll pop
  75. } def
  76. %/beginnotdefchar
  77. /endnotdefchar {cleartomark} def
  78. %/beginnotdefrange
  79. /endnotdefrange {cleartomark} def
  80. currentdict end readonly def
  81. /.cmap2code { % - .cmap2code -
  82. /CIDInit /ProcSet findresource dup length dict copy
  83. .cmap2codedict { 3 copy put pop pop } forall
  84. /CIDInit exch /ProcSet defineresource pop
  85. } def
  86. % Extract and print reverse mapping information from a cid2code.txt file.
  87. /.printhex2 { % <int16> .printhex2 -
  88. (<) print
  89. 16#10000 add 16 =string cvrs 1 4 getinterval print
  90. (>) print
  91. } def
  92. /.cid2code { % <cmaptemplate> <file> <column> .cid2code -
  93. 30 dict begin
  94. /column exch def
  95. (r) file /f exch def
  96. (%!) =
  97. (/CIDInit /ProcSet findresource begin 12 dict begin begincmap) =
  98. % Print the information from the template.
  99. {
  100. exch ==only ( ) print
  101. dup type /dicttype eq {
  102. dup length =only ( dict dup begin) = {
  103. ( ) print exch ===only ( ) print ===only ( def) =
  104. } forall (end def) =
  105. } {
  106. ===only
  107. } ifelse ( def) =
  108. } forall
  109. % Read the data from the cid2code.txt file.
  110. {
  111. f =string readline pop (CID\t) anchorsearch { pop pop exit } if pop
  112. } loop
  113. /map [ {
  114. f =string readline not { pop exit } if
  115. column { (\t) search pop pop pop } repeat
  116. (\t) search { exch pop exch pop } if
  117. (,) search { exch pop exch pop } if
  118. dup length 4 ne { pop (*) } if
  119. dup (*) eq { pop (0000) } if
  120. (16#) exch concatstrings cvi
  121. } loop ] def
  122. % Print the code space range(s).
  123. /maxcid map length 1 sub def
  124. mark maxcid
  125. dup 255 and 255 eq {
  126. 0 exch
  127. } {
  128. dup 16#ff00 and exch 0 2 index 1 sub
  129. } ifelse
  130. counttomark 2 idiv dup =only ( begincodespacerange) = {
  131. exch .printhex2 .printhex2 () =
  132. } repeat (endcodespacerange) =
  133. % Print the map data.
  134. 0 1 100 maxcid {
  135. /lo exch def
  136. /hi lo 99 add maxcid .min def
  137. 0 lo 1 hi { map exch get 0 ne { 1 add } if } for
  138. dup 0 eq {
  139. pop
  140. } {
  141. =only ( begincidchar) = lo 1 hi {
  142. map 1 index get dup 0 eq { pop pop } { exch .printhex2 = } ifelse
  143. } for (endcidchar) =
  144. } ifelse
  145. } for
  146. % Wrap up.
  147. (endcmap CMapName currentdict /CMap defineresource pop end end) =
  148. f closefile
  149. end
  150. } bind def