type1ops.ps 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. % Copyright (C) 1992, 1997, 1998, 1999 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: type1ops.ps,v 1.2 2000/09/19 18:29:11 lpd Exp $
  18. % type1ops.ps
  19. % Define the Type 1 and Type 2 font opcodes for use by Ghostscript utilities.
  20. % Define the default value of lenIV.
  21. % Note that this expects the current font to be on the dictionary stack.
  22. /lenIV { FontType 2 eq { -1 } { 4 } ifelse } def
  23. % ---------------- Encoding ---------------- %
  24. /Type1encode 70 dict
  25. % Data types
  26. dup /nulltype {
  27. pop ()
  28. } put
  29. dup /nametype {
  30. Type1encode exch get
  31. } put
  32. dup /stringtype {
  33. } put
  34. dup /integertype {
  35. dup dup -107 ge exch 107 le and {
  36. 139 add (x) dup 0 4 -1 roll put
  37. } {
  38. dup dup -1131 ge exch 1131 le and {
  39. dup 0 ge { 16#f694 } { neg 16#fa94 } ifelse add
  40. (xx) dup dup 0 4 index -8 bitshift put
  41. 1 4 -1 roll 255 and put
  42. } {
  43. (\377xxxx) 1 1 4 {
  44. dup 8 mul 32 sub 3 index exch bitshift 255 and
  45. 2 index 3 1 roll put
  46. } for exch pop
  47. } ifelse
  48. } ifelse
  49. } put
  50. % Operators
  51. % Identical or similar in Type 1 and Type 2.
  52. /c_hstem 1 def dup /hstem <01> put
  53. /c_vstem 3 def dup /vstem <03> put
  54. /c_vmoveto 4 def dup /vmoveto <04> put
  55. /c_rlineto 5 def dup /rlineto <05> put
  56. /c_hlineto 6 def dup /hlineto <06> put
  57. /c_vlineto 7 def dup /vlineto <07> put
  58. /c_rrcurveto 8 def dup /rrcurveto <08> put
  59. /c_callsubr 10 def /s_callsubr <0a> def dup /callsubr s_callsubr put
  60. /c_return 11 def dup /return <0b> put
  61. /c_escape 12 def
  62. /ce_div 12 def /s_div <0c0c> def dup /div s_div put
  63. /c_endchar 14 def /s_endchar <0e> def dup /endchar s_endchar put
  64. /c_rmoveto 21 def dup /rmoveto <15> put
  65. /c_hmoveto 22 def dup /hmoveto <16> put
  66. /c_vhcurveto 30 def dup /vhcurveto <1e> put
  67. /c_hvcurveto 31 def dup /hvcurveto <1f> put
  68. % Only in Type 1.
  69. /c_closepath 9 def dup /closepath <09> put
  70. /c_hsbw 13 def /s_hsbw <0d> def dup /hsbw s_hsbw put
  71. /ce_dotsection 0 def /s_dotsection <0c06> def dup /dotsection s_dotsection put
  72. /ce_vstem3 1 def /s_vstem3 <0c01> def dup /vstem3 s_vstem3 put
  73. /ce_hstem3 2 def /s_hstem3 <0c02> def dup /hstem3 s_hstem3 put
  74. /ce_seac 6 def /s_seac <0c06> def dup /seac s_seac put
  75. /ce_sbw 7 def /s_sbw <0c07> def dup /sbw s_sbw put
  76. /ce_callothersubr 16 def /s_callothersubr <0c10> def dup /callothersubr s_callothersubr put
  77. /ce_pop 17 def /s_pop <0c11> def dup /pop s_pop put
  78. /ce_setcurrentpoint 33 def /s_setcurrentpoint <0c21> def dup /setcurrentpoint s_setcurrentpoint put
  79. /s_setcurrentpoint_hmoveto s_setcurrentpoint <8b16> concatstrings def
  80. % Only in Type 2.
  81. dup /blend <10> put
  82. dup /hstemhm <12> put
  83. dup /hintmask <13> put
  84. dup /cntrmask <14> put
  85. dup /vstemhm <17> put
  86. dup /rcurveline <18> put
  87. dup /rlinecurve <19> put
  88. dup /vvcurveto <1a> put
  89. dup /hhcurveto <1b> put
  90. dup /callgsubr <1d> put
  91. dup /and <0c03> put
  92. dup /or <0c04> put
  93. dup /not <0c05> put
  94. dup /store <0c08> put
  95. dup /abs <0c09> put
  96. dup /add <0c0a> put
  97. dup /sub <0c0b> put
  98. dup /load <0c0d> put
  99. dup /neg <0c0c> put
  100. dup /eq <0c0f> put
  101. dup /drop <0c12> put
  102. dup /put <0c14> put
  103. dup /get <0c15> put
  104. dup /ifelse <0c16> put
  105. dup /random <0c17> put
  106. dup /mul <0c18> put
  107. dup /sqrt <0c1a> put
  108. dup /dup <0c1b> put
  109. dup /exch <0c1c> put
  110. dup /index <0c1d> put
  111. dup /roll <0c1e> put
  112. dup /hflex <0c22> put
  113. dup /flex <0c23> put
  114. dup /hflex1 <0c24> put
  115. dup /flex1 <0c25> put
  116. readonly def
  117. % ---------------- Decoding ---------------- %
  118. /Type1decode 512 array
  119. Type1encode {
  120. dup type /stringtype eq {
  121. dup length 1 eq { 0 get } { 1 get 256 add } ifelse
  122. % stack: array key code
  123. exch 2 index 3 1 roll put
  124. } {
  125. pop pop
  126. } ifelse
  127. } forall
  128. dup 12 {
  129. dup read pop dup Type1decode exch 256 add get dup null ne
  130. { exch pop }
  131. { pop 2 string dup 0 12 put dup 1 4 -1 roll put }
  132. ifelse
  133. } put
  134. dup 28 { % Type 2 only
  135. dup read pop 128 xor 128 sub 8 bitshift
  136. 1 index read pop add
  137. } put
  138. 32 1 246 { 2 copy dup 139 sub put pop } for
  139. dup 247 { dup read pop 108 add } put
  140. dup 248 { dup read pop 364 add } put
  141. dup 249 { dup read pop 620 add } put
  142. dup 250 { dup read pop 876 add } put
  143. dup 251 { dup read pop 108 add neg } put
  144. dup 252 { dup read pop 364 add neg } put
  145. dup 253 { dup read pop 620 add neg } put
  146. dup 254 { dup read pop 876 add neg } put
  147. dup 255 { % Different for Type 1 and Type 2
  148. dup read pop 128 xor 128 sub
  149. 3 { 8 bitshift 1 index read pop add } repeat
  150. FontType 2 eq { 65536.0 div } if
  151. } put
  152. readonly def
  153. % ---------------- Procedures ---------------- %
  154. % For these utilities, a CharString is represented by a sequence of
  155. % integers, reals, strings, and names, either in an array or on the stack.
  156. % Integers and reals represent themselves; strings are other data that
  157. % appears in the CharString; names are CharString operator names.
  158. % A CharString in an array is called a "charproc"; a CharString on
  159. % the stack is called a "charstack", and is delimited by a mark.
  160. % Individual elements are called "chartokens".
  161. % ------ Encoding ------ %
  162. % Get the string for a chartoken.
  163. % Note that this string may be overwritten by the next call.
  164. /chartoken_string { % <chartoken> chartoken_string <string>
  165. dup type Type1encode exch get exec
  166. } bind def
  167. % Compute the length of a CharString.
  168. /chartoken_length { % <chartoken> chartoken_length <length>
  169. chartoken_string length
  170. } bind def
  171. /charproc_length { % <charproc> charproc_length <length>
  172. 0 exch { chartoken_length add } forall
  173. } bind def
  174. /charstack_length { % <charstack> charstack_length <charstack> <length>
  175. counttomark 0 exch -1 1 { index chartoken_length add } for
  176. } bind def
  177. % Write a CharString to a file. Normally this will be a NullEncode filter
  178. % writing on a string of the correct length.
  179. /chartoken_write { % <file> <chartoken> chartoken_write -
  180. chartoken_string writestring
  181. } bind def
  182. /charproc_write { % <file> <charproc> charproc_write -
  183. { 1 index exch chartoken_write } forall pop
  184. } bind def
  185. % Note that the arguments of charstack_write are backwards.
  186. /charstack_write { % <charstack> <file> charstack_write -
  187. counttomark 1 sub -1 1 { index 1 index exch chartoken_write } for
  188. cleartomark
  189. } bind def
  190. % Convert a charproc or charstack to an *un*encrypted CharString.
  191. /charproc_string { % <charproc> charproc_string <string>
  192. mark exch aload pop charstack_string
  193. } bind def
  194. /charstack_string { % <charstack> charstack_string <string>
  195. charstack_length lenIV 0 gt {
  196. lenIV add string
  197. dup dup length lenIV sub lenIV exch getinterval % skip lenIV
  198. } {
  199. string
  200. } ifelse
  201. /NullEncode filter
  202. exch 1 index counttomark 1 add 2 roll
  203. charstack_write closefile
  204. % lenIV 0 ge { 4330 exch dup .type1encrypt exch pop readonly } if
  205. } bind def
  206. % ------ Decoding ------ %
  207. % Decode a CharString (unencrypted).
  208. /charstack_read { % <file> charstack_read <no-mark-charstack>
  209. { dup read not { pop exit } if
  210. Type1decode 1 index get dup null eq {
  211. pop 1 string dup 0 4 -1 roll put
  212. } {
  213. exch pop exec
  214. } ifelse exch
  215. } loop
  216. } bind def