cid2code.ps 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. % Copyright (C) 1998, 2000 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This file is part of AFPL Ghostscript.
  4. %
  5. % AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  6. % distributor accepts any responsibility for the consequences of using it, or
  7. % for whether it serves any particular purpose or works at all, unless he or
  8. % she says so in writing. Refer to the Aladdin Free Public License (the
  9. % "License") for full details.
  10. %
  11. % Every copy of AFPL Ghostscript must include a copy of the License, normally
  12. % in a plain ASCII text file named PUBLIC. The License grants you the right
  13. % to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14. % conditions described in the License. Among other things, the License
  15. % requires that the copyright notice and this notice be preserved on all
  16. % copies.
  17. % $Id: cid2code.ps,v 1.3 2000/11/30 18:22:11 lpd Exp $
  18. % Construct an inverse map from CIDs to codes.
  19. % Create an inverse map from CIDs to code values.
  20. % We only use this for 16-bit Unicode, so it has some limitations.
  21. % After invoking .cmap2code, loading a CMap file prints out the map
  22. % instead of doing what it usually does. For example:
  23. %
  24. % gs -dNODISPLAY -dBATCH lib/cid2code.ps -c .cmap2code\
  25. % -f Resource/CMap/UniJIS-UCS2-H > mapfile
  26. /.cmap2codedict 10 dict begin
  27. /begincmap {
  28. mark
  29. } def
  30. /endcmap {
  31. % Stack: mark code_lo1 code_hi1 cid1 ...
  32. 20 dict begin
  33. /depth counttomark 3 sub def
  34. % Do a first pass to determine the maximum CID.
  35. 0 0 3 depth {
  36. 1 add /d exch def
  37. d index d 2 add index 1 get add d 3 add index 1 get sub .max
  38. } for
  39. 1 add /ncid exch def
  40. /map ncid 2 mul string def
  41. % Now fill in the map.
  42. 0 3 depth {
  43. /d exch def
  44. d index 2 mul /cid2 exch def
  45. d 1 add index /hi exch def
  46. d 2 add index 2 string copy /lo exch def
  47. lo 1 get 1 hi 1 get {
  48. map cid2 lo 0 get put
  49. map cid2 1 add 3 -1 roll put
  50. /cid2 cid2 2 add def
  51. } for
  52. } for
  53. % Print the map.
  54. (%stdout) (w) file
  55. dup (<) print
  56. dup /ASCIIHexEncode filter
  57. dup map writestring
  58. closefile
  59. () = flush
  60. closefile
  61. end
  62. } def
  63. %/begincodespacerange
  64. /endcodespacerange {cleartomark} def
  65. %/usecmap
  66. %/beginbfchar
  67. /endbfchar {cleartomark} def
  68. %/beginbfrange
  69. /endbfrange {cleartomark} def
  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. %/beginnotdefchar
  79. /endnotdefchar {cleartomark} def
  80. %/beginnotdefrange
  81. /endnotdefrange {cleartomark} def
  82. currentdict end readonly def
  83. /.cmap2code { % - .cmap2code -
  84. /CIDInit /ProcSet findresource dup length dict copy
  85. .cmap2codedict { 3 copy put pop pop } forall
  86. /CIDInit exch /ProcSet defineresource pop
  87. } def
  88. % Extract and print reverse mapping information from a cid2code.txt file.
  89. /.printhex2 { % <int16> .printhex2 -
  90. (<) print
  91. 16#10000 add 16 =string cvrs 1 4 getinterval print
  92. (>) print
  93. } def
  94. /.cid2code { % <cmaptemplate> <file> <column> .cid2code -
  95. 30 dict begin
  96. /column exch def
  97. (r) file /f exch def
  98. (%!) =
  99. (/CIDInit /ProcSet findresource begin 12 dict begin begincmap) =
  100. % Print the information from the template.
  101. {
  102. exch ==only ( ) print
  103. dup type /dicttype eq {
  104. dup length =only ( dict dup begin) = {
  105. ( ) print exch ===only ( ) print ===only ( def) =
  106. } forall (end def) =
  107. } {
  108. ===only
  109. } ifelse ( def) =
  110. } forall
  111. % Read the data from the cid2code.txt file.
  112. {
  113. f =string readline pop (CID\t) anchorsearch { pop pop exit } if pop
  114. } loop
  115. /map [ {
  116. f =string readline not { pop exit } if
  117. column { (\t) search pop pop pop } repeat
  118. (\t) search { exch pop exch pop } if
  119. (,) search { exch pop exch pop } if
  120. dup length 4 ne { pop (*) } if
  121. dup (*) eq { pop (0000) } if
  122. (16#) exch concatstrings cvi
  123. } loop ] def
  124. % Print the code space range(s).
  125. /maxcid map length 1 sub def
  126. mark maxcid
  127. dup 255 and 255 eq {
  128. 0 exch
  129. } {
  130. dup 16#ff00 and exch 0 2 index 1 sub
  131. } ifelse
  132. counttomark 2 idiv dup =only ( begincodespacerange) = {
  133. exch .printhex2 .printhex2 () =
  134. } repeat (endcodespacerange) =
  135. % Print the map data.
  136. 0 1 100 maxcid {
  137. /lo exch def
  138. /hi lo 99 add maxcid .min def
  139. 0 lo 1 hi { map exch get 0 ne { 1 add } if } for
  140. dup 0 eq {
  141. pop
  142. } {
  143. =only ( begincidchar) = lo 1 hi {
  144. map 1 index get dup 0 eq { pop pop } { exch .printhex2 = } ifelse
  145. } for (endcidchar) =
  146. } ifelse
  147. } for
  148. % Wrap up.
  149. (endcmap CMapName currentdict /CMap defineresource pop end end) =
  150. f closefile
  151. end
  152. } bind def