星火微课系统客户端
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ps2epsi.ps 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. % Copyright (C) 1990, 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: ps2epsi.ps 9619 2009-04-07 10:20:02Z ken $
  16. % Convert an arbitrary PostScript file to an EPSI file.
  17. %
  18. % Please do not contact these users if you have questions. They no longer
  19. % have the time, interest, or current expertise to keep this code working.
  20. % If you find bugs, please send proposed fixes to bug-gs@ghostscript.com.
  21. %
  22. % Bug fix 2002-04-20 by rayjj: Bounding box was incorrect since it depended
  23. % on the dither pattern and gray shade at the boundary. Changed to use
  24. % 8-bit grayscale preview image to allow correct bounding box (at the
  25. % expense of a 8x larger preview image). Also moved .setsafe until after
  26. % the device and file operations are complete (but still before the input
  27. % file is processed.
  28. % Bug fix 2000-04-11 by lpd: if a font didn't have a FontName (which is the
  29. % case for bitmap fonts produced by recent versions of dvips), setfont
  30. % caused an error.
  31. % Bug fix 8/21/99 by lpd: many of the margin and width computations were
  32. % wrong (off by 1). The code only "worked" because the bugs were
  33. % (mostly) in conservative directions.
  34. % Modified 3/17/98 by lpd to make it possible to run this file without
  35. % running the ps2epsi script first, for debugging.
  36. % Bug fix 9/29/97 by lpd <ghost@aladdin.com>: if the page size wasn't an
  37. % exact multiple of 8 bits, an incorrect bounding box (or a rangecheck
  38. % error) could occur.
  39. % Patched 7/26/95 by
  40. % Greg P. Kochanski <gpk@bell-labs.com>
  41. % to add many new DSC comments and make the comments conforming.
  42. % Original version contributed by
  43. % George Cameron <george@bio-medical-physics.aberdeen.ac.uk>
  44. %
  45. % Initialize, and redefine copypage and showpage.
  46. % ps2edict is normally defined in the pre-loaded code created by the
  47. % ps2epsi script.
  48. /ps2edict where { pop } { /ps2edict 25 dict def } ifelse
  49. ps2edict begin
  50. % The main procedure
  51. /ps2epsi
  52. {
  53. % bbox written to outfile by bbox device from ps2epsi command file
  54. outfile (r) file /epsifile exch def
  55. /BBoxString epsifile 256 string readline pop def
  56. /HiresBBoxString epsifile 256 string readline pop def
  57. epsifile closefile
  58. % Open the file
  59. outfile (w) file /epsifile exch def
  60. % Get the device parameters
  61. currentdevice getdeviceprops .dicttomark
  62. /HWSize get aload pop
  63. /devheight exch def
  64. /devwidth exch def
  65. matrix defaultmatrix
  66. /devmatrix exch def
  67. % Make a corresponding 8-bit deep memory device
  68. devmatrix devwidth devheight
  69. 256 string 0 1 255 { 1 index exch dup 255 exch sub put } for
  70. makeimagedevice
  71. /arraydevice exch def
  72. arraydevice
  73. % Turn on anti-aliasing
  74. mark /TextAlphaBits 4 /GraphicsAlphaBits 4 6 -1 roll
  75. putdeviceprops
  76. setdevice % (does an erasepage)
  77. /rowwidth devwidth def
  78. /row rowwidth string def
  79. /zerorow rowwidth string def % all zero
  80. % Replace the definition of showpage
  81. userdict /showpage { ps2edict begin epsipage end } bind put
  82. userdict /setfont { ps2edict begin epsisetfont end } bind put
  83. userdict /setpagedevice /pop load put
  84. //systemdict /.setsafe known { .setsafe } if
  85. } bind def
  86. /epsifontdict 100 dict def
  87. /epsisetfont
  88. {
  89. % code here keeps a list of font names in dictionary epsifontdict
  90. /tmpfont exch def
  91. tmpfont /FontName known {
  92. /tmpfontname tmpfont /FontName get def
  93. epsifontdict tmpfontname known not { epsifontdict tmpfontname 0 put } if
  94. epsifontdict tmpfontname 2 copy get 1 add put
  95. } if
  96. tmpfont setfont
  97. } bind def
  98. % Get a scan line from the memory device, zeroing any bits beyond
  99. % the device width.
  100. /getscanline { % <device> <y> <string> getscanline <string>
  101. dup 4 1 roll copyscanlines pop
  102. 16#ff00 devwidth 7 and neg bitshift 255 and
  103. dup 0 ne {
  104. 1 index dup length 1 sub 2 copy get 4 -1 roll and put
  105. } {
  106. pop
  107. } ifelse
  108. } bind def
  109. /margintest { % <y-start> <step> <y-limit> margintest <y-non-blank>
  110. % <y-start> <step> <y-limit> margintest -
  111. { dup arraydevice exch row getscanline
  112. zerorow ne { exit } if pop
  113. } for
  114. } bind def
  115. /epsiNameStr 200 string def
  116. /epsiNpages 0 def
  117. /epsiNpageStr 20 string def
  118. /epsipage
  119. {
  120. /epsiNpages epsiNpages 1 add def
  121. /loopcount devheight 1 sub def
  122. % Find top margin -- minimum Y of non-blank scan line.
  123. -1 0 1 loopcount margintest
  124. dup -1 eq { (blank page!!\n) print quit }{ exch pop } ifelse
  125. /tm exch def
  126. % Find bottom margin -- maximum Y of non-blank scan line.
  127. loopcount -1 0 margintest
  128. /bm exch def
  129. % Initialise limit variables
  130. /loopcount rowwidth 1 sub def
  131. /lm loopcount def
  132. /rm 0 def
  133. % Find left and right boundaries of image
  134. tm 1 bm
  135. { % Get more data
  136. arraydevice exch row getscanline pop
  137. % Scan from left to find first non-zero element
  138. % We save first the element, then the index
  139. -1 0 1 loopcount
  140. { dup row exch get 0 ne { exch pop exit }{ pop } ifelse
  141. } for
  142. % If we found -1, row is blank ..
  143. dup -1 ne
  144. { % Find the leftmost index
  145. dup lm lt
  146. % If the new index is less, we save index and element
  147. { /lm exch def } { pop } ifelse
  148. % Now find the rightmost index
  149. loopcount -1 0
  150. { dup row exch get 0 ne { exit }{ pop } ifelse
  151. } for
  152. dup rm gt
  153. % If the new index is greater, we save index and element
  154. { /rm exch def } { pop } ifelse
  155. } if
  156. } for
  157. % Write out the magic string and bounding box information
  158. epsifile (%!PS-Adobe-2.0 EPSF-1.2\n) writestring
  159. /epsititle where { pop epsifile epsititle writestring } if
  160. /epsicreator where { pop epsifile epsicreator writestring } if
  161. /epsicrdt where { pop epsifile epsicrdt writestring } if
  162. /epsifor where { pop epsifile epsifor writestring } if
  163. epsifile flushfile
  164. % Write out the page count:
  165. epsifile (%%Pages: ) writestring
  166. epsifile epsiNpages epsiNpageStr cvs writestring
  167. epsifile (\n) writestring
  168. epsifile flushfile
  169. % Write out the list of used fonts:
  170. epsifile (%%DocumentFonts:) writestring
  171. epsifontdict {
  172. epsifile ( ) writestring
  173. pop epsiNameStr cvs epsifile exch writestring
  174. } forall
  175. epsifile (\n) writestring
  176. epsifile flushfile
  177. epsifile BBoxString writestring epsifile (\r) writestring
  178. epsifile HiresBBoxString writestring epsifile (\r) writestring
  179. % Define character and bit widths for the output line buffer:
  180. /cwidth rm lm sub 1 add def
  181. /out cwidth string def
  182. epsifile (%%EndComments\n\n) writestring
  183. epsifile (%%BeginProlog\n) writestring
  184. epsifile (%%BeginPreview: ) writestring
  185. epsifile cwidth write==only epsifile ( ) writestring
  186. epsifile bm tm sub 1 add write==only epsifile ( 8 ) writestring
  187. epsifile bm tm sub 1 add
  188. cwidth 39 add 40 idiv mul write==
  189. epsifile flushfile
  190. gsave
  191. tm 1 bm
  192. { % Get a scan line interval from the array device
  193. arraydevice exch row copyscanlines lm cwidth getinterval
  194. % Write out the hex data as 40 bytes per line (82 chars)
  195. 0 40 cwidth
  196. { epsifile (% ) writestring
  197. epsifile exch 2 index exch
  198. dup cwidth exch sub 40 .min getinterval writehexstring
  199. epsifile (\n) writestring
  200. } for
  201. pop
  202. } for
  203. epsifile (%%EndImage\n) writestring
  204. epsifile (%%EndPreview\n) writestring
  205. epsifile flushfile
  206. grestore
  207. erasepage initgraphics
  208. DonePage 0 1 put
  209. } bind def
  210. (outfile) getenv
  211. { /outfile exch def
  212. ps2epsi
  213. /DonePage 1 string def
  214. (%stdin) (r) file cvx execute0
  215. DonePage 0 get 0 eq { showpage } if
  216. } if
  217. end
  218. quit