epsf.sty 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. % EPSF.TEX macro file:
  2. % Written by Tomas Rokicki of Radical Eye Software, 29 Mar 1989.
  3. % Revised by Don Knuth, 3 Jan 1990.
  4. % Revised by Tomas Rokicki to accept bounding boxes with no
  5. % space after the colon, 18 Jul 1990.
  6. %
  7. % TeX macros to include an Encapsulated PostScript graphic.
  8. % Works by finding the bounding box comment,
  9. % calculating the correct scale values, and inserting a vbox
  10. % of the appropriate size at the current position in the TeX document.
  11. %
  12. % To use with the center environment of LaTeX, preface the \epsffile
  13. % call with a \leavevmode. (LaTeX should probably supply this itself
  14. % for the center environment.)
  15. %
  16. % To use, simply say
  17. % \input epsf % somewhere early on in your TeX file
  18. % \epsfbox{filename.ps} % where you want to insert a vbox for a figure
  19. %
  20. % Alternatively, you can type
  21. %
  22. % \epsfbox[0 0 30 50]{filename.ps} % to supply your own BB
  23. %
  24. % which will not read in the file, and will instead use the bounding
  25. % box you specify.
  26. %
  27. % The effect will be to typeset the figure as a TeX box, at the
  28. % point of your \epsfbox command. By default, the graphic will have its
  29. % `natural' width (namely the width of its bounding box, as described
  30. % in filename.ps). The TeX box will have depth zero.
  31. %
  32. % You can enlarge or reduce the figure by saying
  33. % \epsfxsize=<dimen> \epsfbox{filename.ps}
  34. % (or
  35. % \epsfysize=<dimen> \epsfbox{filename.ps})
  36. % instead. Then the width of the TeX box will be \epsfxsize and its
  37. % height will be scaled proportionately (or the height will be
  38. % \epsfysize and its width will be scaled proportiontally). The
  39. % width (and height) is restored to zero after each use.
  40. %
  41. % A more general facility for sizing is available by defining the
  42. % \epsfsize macro. Normally you can redefine this macro
  43. % to do almost anything. The first parameter is the natural x size of
  44. % the PostScript graphic, the second parameter is the natural y size
  45. % of the PostScript graphic. It must return the xsize to use, or 0 if
  46. % natural scaling is to be used. Common uses include:
  47. %
  48. % \epsfxsize % just leave the old value alone
  49. % 0pt % use the natural sizes
  50. % #1 % use the natural sizes
  51. % \hsize % scale to full width
  52. % 0.5#1 % scale to 50% of natural size
  53. % \ifnum#1>\hsize\hsize\else#1\fi % smaller of natural, hsize
  54. %
  55. % If you want TeX to report the size of the figure (as a message
  56. % on your terminal when it processes each figure), say `\epsfverbosetrue'.
  57. %
  58. \newread\epsffilein % file to \read
  59. \newif\ifepsffileok % continue looking for the bounding box?
  60. \newif\ifepsfbbfound % success?
  61. \newif\ifepsfverbose % report what you're making?
  62. \newif\ifepsfdraft % use draft mode?
  63. \newdimen\epsfxsize % horizontal size after scaling
  64. \newdimen\epsfysize % vertical size after scaling
  65. \newdimen\epsftsize % horizontal size before scaling
  66. \newdimen\epsfrsize % vertical size before scaling
  67. \newdimen\epsftmp % register for arithmetic manipulation
  68. \newdimen\pspoints % conversion factor
  69. %
  70. \pspoints=1bp % Adobe points are `big'
  71. \epsfxsize=0pt % Default value, means `use natural size'
  72. \epsfysize=0pt % ditto
  73. %
  74. \def\epsfbox#1{\global\def\epsfllx{72}\global\def\epsflly{72}%
  75. \global\def\epsfurx{540}\global\def\epsfury{720}%
  76. \def\lbracket{[}\def\testit{#1}\ifx\testit\lbracket
  77. \let\next=\epsfgetlitbb\else\let\next=\epsfnormal\fi\next{#1}}%
  78. %
  79. \def\epsfgetlitbb#1#2 #3 #4 #5]#6{\epsfgrab #2 #3 #4 #5 .\\%
  80. \epsfsetgraph{#6}}%
  81. %
  82. \def\epsfnormal#1{\epsfgetbb{#1}\epsfsetgraph{#1}}%
  83. %
  84. \def\epsfgetbb#1{%
  85. %
  86. % The first thing we need to do is to open the
  87. % PostScript file, if possible.
  88. %
  89. \openin\epsffilein=#1
  90. \ifeof\epsffilein\errmessage{I couldn't open #1, will ignore it}\else
  91. %
  92. % Okay, we got it. Now we'll scan lines until we find one that doesn't
  93. % start with %. We're looking for the bounding box comment.
  94. %
  95. {\epsffileoktrue \chardef\other=12
  96. \def\do##1{\catcode`##1=\other}\dospecials \catcode`\ =10
  97. \loop
  98. \read\epsffilein to \epsffileline
  99. \ifeof\epsffilein\epsffileokfalse\else
  100. %
  101. % We check to see if the first character is a % sign;
  102. % if not, we stop reading (unless the line was entirely blank);
  103. % if so, we look further and stop only if the line begins with
  104. % `%%BoundingBox:'.
  105. %
  106. \expandafter\epsfaux\epsffileline:. \\%
  107. \fi
  108. \ifepsffileok\repeat
  109. \ifepsfbbfound\else
  110. \ifepsfverbose\message{No bounding box comment in #1; using defaults}\fi\fi
  111. }\closein\epsffilein\fi}%
  112. %
  113. % Now we have to calculate the scale and offset values to use.
  114. % First we compute the natural sizes.
  115. %
  116. \def\epsfclipon{\def\epsfclipstring{ clip}}%
  117. \def\epsfclipoff{\def\epsfclipstring{\ifepsfdraft\space clip\fi}}%
  118. \epsfclipoff
  119. %
  120. \def\epsfsetgraph#1{%
  121. \epsfrsize=\epsfury\pspoints
  122. \advance\epsfrsize by-\epsflly\pspoints
  123. \epsftsize=\epsfurx\pspoints
  124. \advance\epsftsize by-\epsfllx\pspoints
  125. %
  126. % If `epsfxsize' is 0, we default to the natural size of the picture.
  127. % Otherwise we scale the graph to be \epsfxsize wide.
  128. %
  129. \epsfxsize\epsfsize\epsftsize\epsfrsize
  130. \ifnum\epsfxsize=0 \ifnum\epsfysize=0
  131. \epsfxsize=\epsftsize \epsfysize=\epsfrsize
  132. \epsfrsize=0pt
  133. %
  134. % We have a sticky problem here: TeX doesn't do floating point arithmetic!
  135. % Our goal is to compute y = rx/t. The following loop does this reasonably
  136. % fast, with an error of at most about 16 sp (about 1/4000 pt).
  137. %
  138. \else\epsftmp=\epsftsize \divide\epsftmp\epsfrsize
  139. \epsfxsize=\epsfysize \multiply\epsfxsize\epsftmp
  140. \multiply\epsftmp\epsfrsize \advance\epsftsize-\epsftmp
  141. \epsftmp=\epsfysize
  142. \loop \advance\epsftsize\epsftsize \divide\epsftmp 2
  143. \ifnum\epsftmp>0
  144. \ifnum\epsftsize<\epsfrsize\else
  145. \advance\epsftsize-\epsfrsize \advance\epsfxsize\epsftmp \fi
  146. \repeat
  147. \epsfrsize=0pt
  148. \fi
  149. \else \ifnum\epsfysize=0
  150. \epsftmp=\epsfrsize \divide\epsftmp\epsftsize
  151. \epsfysize=\epsfxsize \multiply\epsfysize\epsftmp
  152. \multiply\epsftmp\epsftsize \advance\epsfrsize-\epsftmp
  153. \epsftmp=\epsfxsize
  154. \loop \advance\epsfrsize\epsfrsize \divide\epsftmp 2
  155. \ifnum\epsftmp>0
  156. \ifnum\epsfrsize<\epsftsize\else
  157. \advance\epsfrsize-\epsftsize \advance\epsfysize\epsftmp \fi
  158. \repeat
  159. \epsfrsize=0pt
  160. \else
  161. \epsfrsize=\epsfysize
  162. \fi
  163. \fi
  164. %
  165. % Finally, we make the vbox and stick in a \special that dvips can parse.
  166. %
  167. \ifepsfverbose\message{#1: width=\the\epsfxsize, height=\the\epsfysize}\fi
  168. \epsftmp=10\epsfxsize \divide\epsftmp\pspoints
  169. \vbox to\epsfysize{\vfil\hbox to\epsfxsize{%
  170. \ifnum\epsfrsize=0\relax
  171. \special{PSfile=\ifepsfdraft psdraft.ps\else#1\fi\space
  172. llx=\epsfllx\space lly=\epsflly\space
  173. urx=\epsfurx\space ury=\epsfury\space rwi=\number\epsftmp
  174. \epsfclipstring}%
  175. \else
  176. \epsfrsize=10\epsfysize \divide\epsfrsize\pspoints
  177. \special{PSfile=\ifepsfdraft psdraft.ps\else#1\fi\space
  178. llx=\epsfllx\space lly=\epsflly\space
  179. urx=\epsfurx\space ury=\epsfury\space rwi=\number\epsftmp\space
  180. rhi=\number\epsfrsize \epsfclipstring}%
  181. \fi
  182. \hfil}}%
  183. \global\epsfxsize=0pt\global\epsfysize=0pt}%
  184. %
  185. % We still need to define the tricky \epsfaux macro. This requires
  186. % a couple of magic constants for comparison purposes.
  187. %
  188. {\catcode`\%=12 \global\let\epsfpercent=%\global\def\epsfbblit{%BoundingBox}}%
  189. %
  190. % So we're ready to check for `%BoundingBox:' and to grab the
  191. % values if they are found.
  192. %
  193. \long\def\epsfaux#1#2:#3\\{\ifx#1\epsfpercent
  194. \def\testit{#2}\ifx\testit\epsfbblit
  195. \epsfgrab #3 . . . \\%
  196. \epsffileokfalse
  197. \global\epsfbbfoundtrue
  198. \fi\else\ifx#1\par\else\epsffileokfalse\fi\fi}%
  199. %
  200. % Here we grab the values and stuff them in the appropriate definitions.
  201. %
  202. \def\epsfempty{}%
  203. \def\epsfgrab #1 #2 #3 #4 #5\\{%
  204. \global\def\epsfllx{#1}\ifx\epsfllx\epsfempty
  205. \epsfgrab #2 #3 #4 #5 .\\\else
  206. \global\def\epsflly{#2}%
  207. \global\def\epsfurx{#3}\global\def\epsfury{#4}\fi}%
  208. %
  209. % We default the epsfsize macro.
  210. %
  211. \def\epsfsize#1#2{\epsfxsize}
  212. %
  213. % Finally, another definition for compatibility with older macros.
  214. %
  215. \let\epsffile=\epsfbox