1
0

pdfwrite.ps 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. % Copyright (C) 1999, 2000, 2001 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: pdfwrite.ps,v 1.11 2003/05/20 13:46:22 alexcher Exp $
  16. % Writer for transmuting PDF files.
  17. % NOTES:
  18. % We do editing by replacing objects (in the cache) and then doing a
  19. % simple recursive walk with object renumbering.
  20. % Free variables:
  21. % RMap [per input file] (dict): input_obj# => output_obj#
  22. % PDFfile (file): current input file
  23. % OFile (file): current output file
  24. % XRef (dict): output_obj# => output_file_pos
  25. % ToWrite: 0..N-1 => [obj# gen#]
  26. /.setlanguagelevel where { pop 2 .setlanguagelevel } if
  27. .currentglobal true .setglobal
  28. /PDFWRDEBUG where { pop } { /PDFWRDEBUG false def } ifelse
  29. % ======== Long dictionary support =============== %
  30. % The key must be a non-negative iteger.
  31. /ld_dict { % <len> ld_dict <ldict>
  32. pop << 0 <<>> >>
  33. } bind def
  34. /ld_length { % <ldict> ld_length <length>
  35. 0 exch { exch pop length add } forall
  36. } bind def
  37. /ld_get { % <ldict> <key> ld_get <any>
  38. dup 3 1 roll -15 bitshift get exch get
  39. } bind def
  40. /ld_put { % <ldict> <key> <any> ld_put -
  41. 3 1 roll dup % any ldict key key
  42. 4 1 roll -15 bitshift % key any ldict key>>15
  43. 2 copy known {
  44. get % key any subdict
  45. 3 1 roll put % -
  46. } {
  47. 64 dict dup 6 1 roll % <<>> key any ldict key>>15 <<>>
  48. put put
  49. } ifelse % -
  50. } bind def
  51. /ld_known { % <ldict> <key> ld_known <bool>
  52. dup 3 1 roll -15 bitshift % key <<>> key<<15
  53. 2 copy known {
  54. get exch known
  55. } {
  56. pop pop pop //false
  57. } ifelse
  58. } bind def
  59. /ld_knownget { % <ldict> <key> ld_known false | <any> true
  60. dup 3 1 roll -15 bitshift % key <<>> key<<15
  61. 2 copy known {
  62. get exch .knownget
  63. } {
  64. pop pop pop //false
  65. } ifelse
  66. } bind def
  67. /ld_def { % <key> <any> ld_def -
  68. currentdict 3 1 roll ld_put
  69. } bind def
  70. /ld_forall { % <ldict> <proc} ld_forall -
  71. { forall exch pop } aload pop
  72. 4 2 roll 4 packedarray cvx forall
  73. } bind def
  74. /ld_clone { % <ldict> ld_clone <ldict copy>
  75. << exch { dup length dict copy } forall >>
  76. } bind def
  77. % ================ Object mapping ================ %
  78. % Initialize the object number and location map.
  79. /omapinit { % - omapinit -
  80. /RMap 100 ld_dict def
  81. /XRef 100 ld_dict def
  82. PDFWRDEBUG { (omapinit) = } if
  83. } bind def
  84. % Map an object number.
  85. /omapnew { % <oldobj#> omap <newobj#> <isnew>
  86. RMap 1 index ld_knownget {
  87. exch pop //false
  88. } {
  89. PDFWRDEBUG { (omap\() print dup =only } if
  90. RMap dup ld_length 1 add % old# <<>> len+1
  91. 2 index exch dup % old# <<>> old# len+1 len+1
  92. 5 1 roll % len+1 old# <<>> old# len+1
  93. ld_put pop //true % len+1 true
  94. PDFWRDEBUG { (\) = ) print 1 index = } if
  95. } ifelse
  96. } bind def
  97. /omap { % <oldobj#> omap <newobj#>
  98. omapnew pop
  99. } bind def
  100. % Save and restore the object map.
  101. % Note that currentomap either returns a copy or calls omapinit.
  102. /currentomap { % <copy> currentomap <omap>
  103. {
  104. [RMap ld_clone XRef ld_clone]
  105. } {
  106. [RMap XRef] omapinit
  107. } ifelse
  108. } bind def
  109. /setomap { % <omap> setomap -
  110. aload pop /XRef exch def /RMap exch def
  111. PDFWRDEBUG {
  112. (setomap: #Xref = ) print XRef ld_length =only
  113. (, #RMap = ) print RMap ld_length =
  114. } if
  115. } bind def
  116. % ================ Writing ================ %
  117. % ---------------- Low-level output ---------------- %
  118. % Write a string on the output file.
  119. /ows { % <string> ows -
  120. OFile exch writestring
  121. } bind def
  122. % ---------------- Scalars ---------------- %
  123. % Note that the '#' character isn't legal in a name unless it is a prefix
  124. % for a hex encoded character (for PDF 1.2 and later). The following assumes
  125. % that the names are already valid PDF 1.2+ names so that we can treat the
  126. % '#' as a legal character. The next two hex characters are already in the
  127. % set of valid name characters. PDF 1.1 and earlier allowed spaces in names
  128. % which probably wouldn't make it past the tokenizer anyway.
  129. /pdfnamechars
  130. (!"#$&'*+,-.0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\^_`abcdefghijklmnopqrstuvwxyz|~)
  131. readonly def
  132. /pdfwritename { % <name> pdfwritename -
  133. (/) ows .namestring {
  134. ( ) dup 0 4 -1 roll put
  135. //pdfnamechars 1 index search {
  136. pop pop pop
  137. } {
  138. pop 0 get 256 add 16 =string cvrs
  139. dup 0 (#) 0 get put
  140. } ifelse ows
  141. } forall
  142. } bind def
  143. % ---------------- Composite objects ---------------- %
  144. /pdfwriteprocs mark
  145. /resolveR { pdfwriteref }
  146. /O { pdfwritenewref }
  147. .dicttomark readonly def
  148. /pdfwritearray { % <array> pdfwritearray -
  149. dup xcheck {
  150. aload pop //pdfwriteprocs exch get exec
  151. } {
  152. % Because of a bug in Acrobat's parser for linearization parameters,
  153. % we have to include some whitespace after the opening [ (!).
  154. ([ ) ows { pdfwritevalue (\n) ows } forall (]) ows
  155. } ifelse
  156. } bind def
  157. /pdfwritedict { % <dict> pdfwritedict -
  158. dup xcheck {
  159. pdfwritestream
  160. } {
  161. (<<) ows {
  162. exch pdfwritevalue ( ) ows pdfwritevalue (\n) ows
  163. } forall (>>) ows
  164. } ifelse
  165. } bind def
  166. % ---------------- References ---------------- %
  167. /pdfwritenewref { % <newobj#> pdfwritenewref -
  168. OFile exch write=only ( 0 R) ows
  169. } bind def
  170. /pdfwriteref { % <obj#> <gen#> pdfwriteref -
  171. 1 index omapnew {
  172. ToWrite dup length 5 -2 roll 2 packedarray put
  173. } {
  174. exch pop exch pop
  175. } ifelse
  176. pdfwritenewref
  177. } bind def
  178. /pdfcopystring 200 string def
  179. /pdfwritestream { % <streamdict> pdfwritestream -
  180. % Remove File, FilePosition, and StreamKey;
  181. % optimize by replacing an indirect Length.
  182. dup dup length dict copy
  183. % Stack: origdict dict
  184. dup /File undef dup /FilePosition undef dup /StreamKey undef
  185. dup /Length get dup oforce ne {
  186. dup /Length 2 copy oget put
  187. } if
  188. exch dup /File get dup 3 -1 roll /FilePosition get setfileposition
  189. pdfcopystream
  190. } bind def
  191. % We put copying the stream contents in separate procedures so that we
  192. % can replace this function if desired.
  193. /pdfcopybytes { % <fromfile> <tofile> <length> pdfcopybytes -
  194. {
  195. dup 0 eq { exit } if
  196. //pdfcopystring 0 2 index 2 index length .min getinterval
  197. 3 index exch readstring 3 1 roll
  198. 3 index 1 index writestring length sub exch not { exit } if
  199. } loop pop pop pop
  200. } bind def
  201. /pdfcopystream { % <newstreamdict> <file> pdfcopystream -
  202. % (file has been positioned)
  203. 1 index pdfwritevalue (stream\n) ows
  204. exch /Length get OFile exch pdfcopybytes
  205. (endstream) ows
  206. } bind def
  207. % ---------------- General values/objects ---------------- %
  208. /pdfwritetypes mark
  209. % Scalars
  210. /nulltype { pop (null) ows } bind
  211. /integertype { =string cvs ows } bind
  212. /booleantype 1 index
  213. /realtype { OFile exch write===only } bind
  214. /stringtype 1 index
  215. /nametype { pdfwritename } bind
  216. % Composite/reference objects
  217. /arraytype { pdfwritearray } bind
  218. /packedarraytype 1 index
  219. /dicttype { pdfwritedict } bind
  220. .dicttomark readonly def
  221. /pdfwritevalue { % <obj> pdfwritevalue -
  222. PDFWRDEBUG { (****Writing: ) print dup === flush } if
  223. //pdfwritetypes 1 index type get exec
  224. } bind def
  225. % We make pdfwriteobjdef a separate procedure for external use.
  226. /pdfwriteobjheader { % <newobj#> pdfwriteobjheader -
  227. XRef 1 index OFile .fileposition ld_put
  228. PDFWRDEBUG { (XRef\() print dup =only (\) = ) print XRef 1 index ld_get = } if
  229. OFile exch write=only ( 0 obj\n) ows
  230. } bind def
  231. /pdfwriteobjdef { % <newobj#> <value> pdfwriteobjdef -
  232. exch pdfwriteobjheader
  233. pdfwritevalue (\nendobj\n) ows
  234. } bind def
  235. /pdfwriteobj { % <obj#> <gen#> pdfwriteobj -
  236. 1 index exch resolveR exch omap exch pdfwriteobjdef
  237. } bind def
  238. % ---------------- File-level entities ---------------- %
  239. % Write a PDF file header.
  240. % Free variables: OFile, PDFversion.
  241. /pdfwriteheader { % - pdfwriteheader -
  242. (%PDF-) ows OFile PDFversion write=
  243. (%\347\363\317\323\n) ows
  244. } bind def
  245. % Write a cross-reference table and trailer.
  246. /pdfwritexref { % <firstobj#> <#objs> pdfwritexref -
  247. (xref\n) ows
  248. OFile 2 index write=only ( ) ows OFile 1 index write=
  249. 1 index add 1 sub 1 exch {
  250. dup 0 eq {
  251. pop (0000000000 65535 f \n) ows
  252. } {
  253. XRef exch ld_get 1000000000 add =string cvs
  254. dup 0 (0) 0 get put
  255. ows ( 00000 n \n) ows
  256. } ifelse
  257. } for
  258. } bind def
  259. /pdfwritetrailer { % <trailer> pdfwritetrailer -
  260. (trailer\n) ows pdfwritevalue (\n) ows
  261. } bind def
  262. /pdfwritestartxref { % <startpos> pdfwritestartxref -
  263. (startxref\n) ows OFile exch write=
  264. (%%EOF\n) ows
  265. } bind def
  266. % ================ Top-level control ================ %
  267. /pdfwrite { % <file> <trailer> pdfwrite -
  268. 10 dict begin
  269. /trailer exch def
  270. /OFile exch def
  271. /ToWrite 100 dict def
  272. omapinit
  273. % Write the PDF file header.
  274. pdfwriteheader
  275. % Write the objects.
  276. trailer {
  277. exch pop dup xcheck { % The only executable objects are references.
  278. aload pop pop pdfwriteobj
  279. } {
  280. pop
  281. } ifelse
  282. } forall
  283. % Walk the object graph.
  284. {
  285. ToWrite dup length dup 0 eq { pop pop exit } if
  286. 1 sub 2 copy get 3 1 roll undef aload pop pdfwriteobj
  287. } loop
  288. % Write the xref table and trailer.
  289. /xref OFile fileposition def
  290. 0 XRef ld_length 1 add pdfwritexref
  291. trailer dup length 1 add dict copy
  292. dup /Size XRef ld_length 1 add put pdfwritetrailer
  293. xref pdfwritestartxref
  294. end
  295. } bind def
  296. .setglobal