gs_btokn.ps 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. % Copyright (C) 1994, 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: gs_btokn.ps,v 1.9 2002/04/19 06:52:25 lpd Exp $
  16. % Initialization file for binary tokens.
  17. % When this is run, systemdict is still writable,
  18. % but everything defined here goes into level2dict.
  19. % Define whether or not to allow writing dictionaries.
  20. % This is a non-standard feature!
  21. /WRITEDICTS false def
  22. languagelevel 1 .setlanguagelevel
  23. level2dict begin
  24. % Initialization for the system name table.
  25. mark
  26. % 0
  27. /abs /add /aload /anchorsearch /and
  28. /arc /arcn /arct /arcto /array
  29. /ashow /astore /awidthshow /begin /bind
  30. /bitshift /ceiling /charpath /clear /cleartomark
  31. % 20
  32. /clip /clippath /closepath /concat /concatmatrix
  33. /copy /count /counttomark /currentcmykcolor /currentdash
  34. /currentdict /currentfile /currentfont /currentgray /currentgstate
  35. /currenthsbcolor /currentlinecap /currentlinejoin /currentlinewidth /currentmatrix
  36. % 40
  37. /currentpoint /currentrgbcolor /currentshared /curveto /cvi
  38. /cvlit /cvn /cvr /cvrs /cvs
  39. /cvx /def /defineusername /dict /div
  40. /dtransform /dup /end /eoclip /eofill
  41. % 60
  42. /eoviewclip /eq /exch /exec /exit
  43. /file /fill /findfont /flattenpath /floor
  44. /flush /flushfile /for /forall /ge
  45. /get /getinterval /grestore /gsave /gstate
  46. % 80
  47. /gt /identmatrix /idiv /idtransform /if
  48. /ifelse /image /imagemask /index /ineofill
  49. /infill /initviewclip /inueofill /inufill /invertmatrix
  50. /itransform /known /le /length /lineto
  51. % 100
  52. /load /loop /lt /makefont /matrix
  53. /maxlength /mod /moveto /mul /ne
  54. /neg /newpath /not /null /or
  55. /pathbbox /pathforall /pop /print /printobject
  56. % 120
  57. /put /putinterval /rcurveto /read /readhexstring
  58. /readline /readstring /rectclip /rectfill /rectstroke
  59. /rectviewclip /repeat /restore /rlineto /rmoveto
  60. /roll /rotate /round /save /scale
  61. % 140
  62. /scalefont /search /selectfont /setbbox /setcachedevice
  63. /setcachedevice2 /setcharwidth /setcmykcolor /setdash /setfont
  64. /setgray /setgstate /sethsbcolor /setlinecap /setlinejoin
  65. /setlinewidth /setmatrix /setrgbcolor /setshared /shareddict
  66. % 160
  67. /show /showpage /stop /stopped /store
  68. /string /stringwidth /stroke /strokepath /sub
  69. /systemdict /token /transform /translate /truncate
  70. /type /uappend /ucache /ueofill /ufill
  71. % 180
  72. /undef /upath /userdict /ustroke /viewclip
  73. /viewclippath /where /widthshow /write /writehexstring
  74. /writeobject /writestring /wtranslation /xor /xshow
  75. /xyshow /yshow /FontDirectory /SharedFontDirectory /Courier
  76. % 200
  77. /Courier-Bold /Courier-BoldOblique /Courier-Oblique /Helvetica /Helvetica-Bold
  78. /Helvetica-BoldOblique /Helvetica-Oblique /Symbol /Times-Bold /Times-BoldItalic
  79. /Times-Italic /Times-Roman /execuserobject /currentcolor /currentcolorspace
  80. /currentglobal /execform /filter /findresource /globaldict
  81. % 220
  82. /makepattern /setcolor /setcolorspace /setglobal /setpagedevice
  83. /setpattern
  84. % pad to 256
  85. counttomark 256 exch sub { 0 } repeat
  86. % 256
  87. /= /== /ISOLatin1Encoding /StandardEncoding
  88. % 260
  89. ([) cvn (]) cvn /atan /banddevice /bytesavailable
  90. /cachestatus /closefile /colorimage /condition /copypage
  91. /cos /countdictstack /countexecstack /cshow /currentblackgeneration
  92. /currentcacheparams /currentcolorscreen /currentcolortransfer /currentcontext /currentflat
  93. % 280
  94. /currenthalftone /currenthalftonephase /currentmiterlimit /currentobjectformat /currentpacking
  95. /currentscreen /currentstrokeadjust /currenttransfer /currentundercolorremoval /defaultmatrix
  96. /definefont /deletefile /detach /deviceinfo /dictstack
  97. /echo /erasepage /errordict /execstack /executeonly
  98. % 300
  99. /exp /false /filenameforall /fileposition /fork
  100. /framedevice /grestoreall /handleerror /initclip /initgraphics
  101. /initmatrix /instroke /inustroke /join /kshow
  102. /ln /lock /log /mark /monitor
  103. % 320
  104. /noaccess /notify /nulldevice /packedarray /quit
  105. /rand /rcheck /readonly /realtime /renamefile
  106. /renderbands /resetfile /reversepath /rootfont /rrand
  107. /run /scheck /setblackgeneration /setcachelimit /setcacheparams
  108. % 340
  109. /setcolorscreen /setcolortransfer /setfileposition /setflat /sethalftone
  110. /sethalftonephase /setmiterlimit /setobjectformat /setpacking /setscreen
  111. /setstrokeadjust /settransfer /setucacheparams /setundercolorremoval /sin
  112. /sqrt /srand /stack /status /statusdict
  113. % 360
  114. /true /ucachestatus /undefinefont /usertime /ustrokepath
  115. /version /vmreclaim /vmstatus /wait /wcheck
  116. /xcheck /yield /defineuserobject /undefineuserobject /UserObjects
  117. /cleardictstack
  118. % 376
  119. /A /B /C /D /E /F /G /H /I /J /K /L /M
  120. /N /O /P /Q /R /S /T /U /V /W /X /Y /Z
  121. /a /b /c /d /e /f /g /h /i /j /k /l /m
  122. /n /o /p /q /r /s /t /u /v /w /x /y /z
  123. % 428
  124. /setvmthreshold (<<) cvn
  125. (>>) cvn /currentcolorrendering /currentdevparams /currentoverprint /currentpagedevice
  126. /currentsystemparams /currentuserparams /defineresource /findencoding /gcheck
  127. % 440
  128. /glyphshow /languagelevel /product /pstack /resourceforall
  129. /resourcestatus /revision /serialnumber /setcolorrendering /setdevparams
  130. /setoverprint /setsystemparams /setuserparams /startjob /undefineresource
  131. /GlobalFontDirectory /ASCII85Decode /ASCII85Encode /ASCIIHexDecode /ASCIIHexEncode
  132. % 460
  133. /CCITTFaxDecode /CCITTFaxEncode /DCTDecode /DCTEncode /LZWDecode
  134. /LZWEncode /NullEncode /RunLengthDecode /RunLengthEncode /SubFileDecode
  135. /CIEBasedA /CIEBasedABC /DeviceCMYK /DeviceGray /DeviceRGB
  136. /Indexed /Pattern /Separation /CIEBasedDEF /CIEBasedDEFG
  137. % 480
  138. /DeviceN
  139. % 481 -- end
  140. .packtomark .installsystemnames
  141. % Define printobject and writeobject.
  142. % These are mostly implemented in PostScript, so that we don't have to
  143. % worry about interrupts or callbacks when writing to the output file.
  144. % Define procedures for accumulating the space required to represent
  145. % an object in binary form. The procedures for composite objects (arrays
  146. % and dictionaries) leave different results on the stack:
  147. % <#refs> <#chars> <simple_obj> -proc- <#refs> <#chars>
  148. % <#refs> <#chars> <array|dict> -proc- <array|dict> <#refs> <#chars>
  149. % This is required so that .writeobjects can also accumulate the actual
  150. % list of composite objects to write in the binary object sequence.
  151. /cntdict mark
  152. /integertype /pop load
  153. /realtype 1 index
  154. /marktype 1 index
  155. /nulltype 1 index
  156. /booleantype 1 index
  157. /nametype { length add } bind
  158. /stringtype 1 index
  159. /arraytype null
  160. /dicttype null
  161. .dicttomark def
  162. /.cntobj { % <<arguments and results as for procedures in cntdict>>
  163. dup type //cntdict exch get exec
  164. } .bind def
  165. cntdict /arraytype {
  166. dup dup length 5 -1 roll add 4 2 roll { .cntobj } forall
  167. } bind put
  168. cntdict /dicttype {
  169. WRITEDICTS {
  170. dup dup length 2 mul 5 -1 roll add 4 2 roll {
  171. % We have to use .execn here, rather than simply rolling the
  172. % value under the top elements, because key might involve arrays
  173. % or dictionaries.
  174. cvlit {.cntobj} exch 2 .execn .cntobj
  175. } forall
  176. } {
  177. /writeobject load /typecheck signalerror
  178. } ifelse
  179. } bind put
  180. /w2dict mark
  181. /nametype { 2 copy .writecvs pop } bind
  182. /stringtype 1 index
  183. .dicttomark def
  184. /.bosheader { % <top_length> <total_length> <string8> .bosheader
  185. % <string4|8>
  186. dup 0 currentobjectformat 127 add put % object format => BOS tag
  187. 2 index 255 le 2 index 65531 le and {
  188. % Use the short header format: tag toplen(1) totlen(2)
  189. exch 4 add exch
  190. 0 4 getinterval
  191. dup 1 5 -1 roll put
  192. } {
  193. % Use the long header format: tag 0(1) toplen(2) totlen(4)
  194. exch 8 add exch
  195. 0 0 4 2 roll .bosobject exch pop exch pop % store with byte swapping
  196. } ifelse % Stack: shortlen str
  197. exch dup -8 bitshift exch 255 and % str hibyte lobyte
  198. currentobjectformat 1 and 0 eq { % lsb first
  199. exch
  200. } if
  201. 2 index 3 3 -1 roll put
  202. 1 index 2 3 -1 roll put
  203. } .bind def
  204. /.writeobjects { % <file> <tag> <array> .writeobjects -
  205. mark exch
  206. % Count the space required for refs and strings.
  207. dup length 0 3 -1 roll
  208. % Stack: file tag -mark- #refs #chars array
  209. dup 4 1 roll { .cntobj } forall
  210. % Write the header.
  211. % Stack: file tag -mark- array1 ... (array|dict)N #refs #chars
  212. counttomark 3 add -2 roll 4 1 roll
  213. % Stack: -mark- array1 ... (array|dict)N tag #refs #chars file
  214. dup counttomark 1 sub index length
  215. 4 index 3 bitshift 4 index add
  216. (xxxxxxxx) .bosheader writestring
  217. % Write the objects per se.
  218. 3 1 roll pop
  219. counttomark 1 sub index length 3 bitshift exch
  220. 3 bitshift
  221. % Stack: -mark- array1 ... (array|dict)N tag file ref# char#
  222. counttomark 4 sub {
  223. counttomark -1 roll dup 6 1 roll
  224. % Stack: ... objN tag file ref# char# objN
  225. dup type /dicttype eq { % can't be first object
  226. { 5 1 roll (x\000xxxxxx) .bosobject
  227. 3 index exch writestring
  228. 4 -1 roll (x\000xxxxxx) .bosobject
  229. 3 index exch writestring
  230. } forall
  231. } {
  232. { (xxxxxxxx) .bosobject
  233. dup 1 6 index put
  234. 3 index exch writestring
  235. 4 -1 roll pop 0 4 1 roll % clear tag
  236. } forall
  237. } ifelse
  238. } repeat
  239. % Write the strings and names.
  240. pop pop exch pop
  241. % Stack: -mark- array1 ... array|dictN file
  242. counttomark 1 sub {
  243. counttomark -1 roll {
  244. % The counting pass ensured that the keys and values
  245. % of any dictionary must be writable objects.
  246. % Hence, we are processing a dictionary iff
  247. % the next-to-top stack element is not a file.
  248. 1 index type /filetype ne {
  249. exch 2 index exch dup type //w2dict exch .knownget
  250. { exec } { pop } ifelse pop
  251. } if
  252. dup type //w2dict exch .knownget { exec } { pop } ifelse
  253. } forall
  254. } repeat
  255. % Clean up.
  256. % Stack: -mark- file
  257. pop pop
  258. } odef
  259. /printobject { % <obj> <tag> printobject -
  260. (%stdout) (w) file 2 index 2 index writeobject pop pop
  261. } odef
  262. /writeobject { % <file> <obj> <tag> writeobject -
  263. 3 copy exch
  264. % We must allocate the array in local VM
  265. % to avoid a possible invalidaccess.
  266. .currentglobal false .setglobal exch 1 array astore exch .setglobal
  267. .writeobjects pop pop pop
  268. } odef
  269. % Implement binary error message output.
  270. /.objectprinttest { % <obj> .objectprinttest -
  271. % This is a pseudo-operator so it will restore the stack
  272. % if it gets an error.
  273. mark 0 0 3 index .cntobj cleartomark pop
  274. } bind odef
  275. /.printerror {
  276. $error /binary get .languagelevel 2 ge and {
  277. currentobjectformat 0 ne {
  278. [ /Error $error /errorname get $error /command get
  279. % Convert the object with cvs if it isn't printable.
  280. dup { .objectprinttest } .internalstopped {
  281. pop 100 string cvs
  282. } if
  283. false ] 250 printobject
  284. }
  285. //.printerror % known to be a procedure
  286. ifelse
  287. }
  288. //.printerror % known to be a procedure
  289. ifelse
  290. } bind def
  291. currentdict /cntdict .undef
  292. currentdict /w2dict .undef
  293. % End of level2dict
  294. end
  295. .setlanguagelevel