You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
7.9 KiB

1 month ago
  1. % Copyright (C) 2001-2023 Artifex Software, Inc.
  2. % All Rights Reserved.
  3. %
  4. % This software is provided AS-IS with no warranty, either express or
  5. % implied.
  6. %
  7. % This software is distributed under license and may not be copied,
  8. % modified or distributed except as expressly authorized under the terms
  9. % of the license contained in the file LICENSE in this distribution.
  10. %
  11. % Refer to licensing information at http://www.artifex.com or contact
  12. % Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  13. % CA 94129, USA, for further information.
  14. %
  15. % pdf2dsc.ps
  16. % read pdf file and produce DSC "index" file.
  17. %
  18. % Input file is named PDFname
  19. % Output file is named DSCname
  20. %
  21. % Run using:
  22. % gs -dNODISPLAY -sPDFname=pdffilename -sDSCname=tempfilename pdf2dsc.ps
  23. % Then display the PDF file with
  24. % gs tempfilename
  25. %
  26. % Modified by Jason McCarty, bug 688071
  27. % Add PageLabels support.
  28. % Modified by Geoff Keating <geoffk@ozemail.com.au> 21/12/98:
  29. % Add DocumentMedia, PageMedia comments
  30. % Use inherited BoundingBox and Orientation
  31. % Reformat, add new macro 'puts', generally clean up
  32. % Modified by Johannes Plass <plass@dipmza.physik.uni-mainz.de> 1996-11-05:
  33. % Adds BoundingBox and Orientation if available.
  34. % Modified by rjl/lpd 9/19/96
  35. % Updates for compatibility with modified pdf_*.ps code for handling
  36. % page ranges (i.e., partial files) better.
  37. % Modified by Geoff Keating <Geoff.Keating@anu.edu.au> 7/3/96:
  38. % include Title and CreationDate DSC comments (these are displayed by
  39. % Ghostview);
  40. % reduce the size of typical output files by a factor of about 3.
  41. % Modified by L. Peter Deutsch 3/18/96:
  42. % Removes unnecessary and error-prone code duplicated from pdf_main.ps
  43. % Modified by L. Peter Deutsch for GS 3.33
  44. % Originally by Russell Lang 1995-04-26
  45. /PDFfile PDFname (r) file def
  46. /DSCfile DSCname (w) file def
  47. systemdict /.setsafe known { .setsafe } if
  48. /puts { DSCfile exch writestring } bind def
  49. /DSCstring 255 string def
  50. /MediaTypes 10 dict def
  51. PDFfile runpdfbegin
  52. /FirstPage where { pop } { /FirstPage 1 def } ifelse
  53. /LastPage where { pop } { /LastPage pdfpagecount def } ifelse
  54. % scan through for media sizes, keep them in the dictionary
  55. FirstPage 1 LastPage {
  56. pdfgetpage /MediaBox pget pop % MediaBox is a required attribute
  57. aload pop
  58. 3 -1 roll sub 3 1 roll exch sub exch
  59. 2 array astore
  60. aload 3 1 roll 10 string cvs exch 10 string cvs
  61. (x) 3 -1 roll concatstrings concatstrings cvn
  62. MediaTypes 3 1 roll exch put
  63. } for
  64. % write header and prolog
  65. (%!PS-Adobe-3.0\n) puts
  66. Trailer /Info knownoget
  67. {
  68. dup /Title knownoget
  69. {
  70. (%%Title: ) puts
  71. DSCfile exch write==
  72. }
  73. if
  74. /CreationDate knownoget
  75. {
  76. (%%CreationDate: ) puts
  77. DSCfile exch write==
  78. }
  79. if
  80. }
  81. if
  82. % This is really supposed to be sorted by frequency of usage...
  83. (%%DocumentMedia: )
  84. MediaTypes {
  85. exch pop
  86. 1 index puts
  87. (y) puts dup 1 get DSCstring cvs puts
  88. (x) puts dup 0 get DSCstring cvs puts
  89. ( ) puts dup 0 get DSCstring cvs puts
  90. ( ) puts 1 get DSCstring cvs puts
  91. ( 70 white ()\n) puts
  92. pop (%%+ )
  93. } forall
  94. pop
  95. (%%Pages: ) puts
  96. LastPage FirstPage sub 1 add DSCstring cvs puts
  97. (\n%%EndComments\n) puts
  98. (%%BeginProlog\n) puts
  99. (/Page null def\n/Page# 0 def\n/PDFSave null def\n) puts
  100. (/DSCPageCount 0 def\n) puts
  101. (/DoPDFPage {dup /Page# exch store dup dopdfpages } def\n) puts
  102. (%%EndProlog\n) puts
  103. (%%BeginSetup\n) puts
  104. DSCfile PDFname write==only
  105. ( \(r\) file { DELAYSAFER { .setsafe } if } stopped pop\n) puts
  106. ( runpdfbegin\n) puts
  107. ( process_trailer_attrs\n) puts
  108. (%%EndSetup\n) puts
  109. /.hasPageLabels false def % see "Page Labels" in the PDF Reference
  110. Trailer /Root knownoget {
  111. /PageLabels knownoget {
  112. /PageLabels exch def
  113. /.pageCounter 1 def
  114. /.pageCounterType /D def
  115. /.pagePrefix () def
  116. % (TEXT) .ToLower (text) -- convert text to lowercase -- only letters!
  117. /.ToLower {
  118. dup length 1 sub -1 0 {
  119. 1 index exch 2 copy get 2#00100000 or put
  120. } for
  121. } def
  122. % int .CvAlpha (int in alphabetic base 26) -- convert a positive
  123. % integer to base 26 in capital letters, with 1=A; i.e. A..Z, AA..AZ, ...
  124. /.CvAlpha { % using cvrs seems futile since this isn't zero-based ...
  125. [ exch % construct an array of ASCII values, in reverse
  126. { % the remainder stays on the top of stack
  127. dup 0 eq { pop exit } if % quit if the value is zero
  128. dup 26 mod dup 0 eq { 26 add } if % so that the division is correct
  129. dup 64 add 3 1 roll sub 26 idiv % save the ASCII value and iterate
  130. } loop ]
  131. dup length dup string 3 1 roll
  132. dup -1 1 { % put the letters in a string
  133. 4 copy sub exch 4 -1 roll 1 sub get put
  134. } for pop pop
  135. } def
  136. % int .CvRoman (int in capital Roman numerals)
  137. % convert a positive integer to capital Roman numerals
  138. % return a decimal string if >= 4000
  139. /.CvRoman {
  140. dup DSCstring cvs % start with the decimal representation
  141. exch 4000 lt { % convert only if Roman numerals can represent this
  142. dup length
  143. [ [ () (I) (II) (III) (IV) (V) (VI) (VII) (VIII) (IX) ]
  144. [ () (X) (XX) (XXX) (XL) (L) (LX) (LXX) (LXXX) (XC) ]
  145. [ () (C) (CC) (CCC) (CD) (D) (DC) (DCC) (DCCC) (CM) ]
  146. [ () (M) (MM) (MMM) ] ] % Roman equivalents
  147. () % append the Roman equivalent of each decimal digit to this string
  148. 2 index -1 1 {
  149. 2 index 1 index 1 sub get
  150. 5 index 5 index 4 -1 roll sub get
  151. 48 sub get concatstrings
  152. } for
  153. 4 1 roll pop pop pop
  154. } if
  155. } def
  156. /PageToString <<
  157. /D { DSCstring cvs }
  158. /R { .CvRoman }
  159. /r { .CvRoman .ToLower }
  160. /A { .CvAlpha }
  161. /a { .CvAlpha .ToLower }
  162. >> def
  163. /.hasPageLabels true def
  164. } if
  165. } if
  166. % process each page
  167. FirstPage 1 LastPage {
  168. (%%Page: ) puts
  169. .hasPageLabels {
  170. dup 1 sub PageLabels exch numoget dup null ne {
  171. % page labels changed at this page, reset the values
  172. dup /S known { dup /S get } { null } ifelse
  173. /.pageCounterType exch def
  174. dup /P known { dup /P get } { () } ifelse
  175. /.pagePrefix exch def
  176. dup /St known { /St get } { pop 1 } ifelse
  177. /.pageCounter exch def
  178. } { pop } ifelse
  179. % output the page label
  180. (\() .pagePrefix
  181. .pageCounterType //null ne dup {
  182. PageToString .pageCounterType known and
  183. } if { % format the page number
  184. .pageCounter dup 0 gt { % don't try to format nonpositive numbers
  185. PageToString .pageCounterType get exec
  186. } {
  187. DSCstring cvs
  188. } ifelse
  189. } { () } ifelse
  190. (\)) concatstrings concatstrings concatstrings puts
  191. /.pageCounter .pageCounter 1 add def
  192. } {
  193. dup DSCstring cvs puts
  194. } ifelse
  195. ( ) puts
  196. dup DSCstring cvs puts
  197. (\n) puts
  198. dup pdfgetpage
  199. dup /MediaBox pget pop
  200. (%%PageMedia: y) puts
  201. aload pop 3 -1 roll sub DSCstring cvs puts
  202. (x) puts exch sub DSCstring cvs puts
  203. (\n) puts
  204. dup /CropBox pget {
  205. (%%PageBoundingBox: ) puts
  206. {DSCfile exch write=only ( ) puts} forall
  207. (\n) puts
  208. } if
  209. /Rotate pget {
  210. (%%PageOrientation: ) puts
  211. 90 div cvi 4 mod dup 0 lt {4 add} if
  212. [(Portrait) (Landscape) (UpsideDown) (Seascape)] exch get puts
  213. (\n) puts
  214. } if
  215. DSCfile exch DSCstring cvs writestring
  216. ( DoPDFPage\n) puts
  217. } for
  218. runpdfend
  219. % write trailer
  220. (%%Trailer\n) puts
  221. (runpdfend\n) puts
  222. (%%EOF\n) puts
  223. % close output file and exit
  224. DSCfile closefile
  225. quit
  226. % end of pdf2dsc.ps