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\newdimen\epsfxsize    % horizontal size after scaling
63\newdimen\epsfysize    % vertical size after scaling
64\newdimen\epsftsize    % horizontal size before scaling
65\newdimen\epsfrsize    % vertical size before scaling
66\newdimen\epsftmp      % register for arithmetic manipulation
67\newdimen\pspoints     % conversion factor
68%
69\pspoints=1bp          % Adobe points are `big'
70\epsfxsize=0pt         % Default value, means `use natural size'
71\epsfysize=0pt         % ditto
72%
73\def\epsfbox#1{\global\def\epsfllx{72}\global\def\epsflly{72}%
74   \global\def\epsfurx{540}\global\def\epsfury{720}%
75   \def\lbracket{[}\def\testit{#1}\ifx\testit\lbracket
76   \let\next=\epsfgetlitbb\else\let\next=\epsfnormal\fi\next{#1}}%
77%
78\def\epsfgetlitbb#1#2 #3 #4 #5]#6{\epsfgrab #2 #3 #4 #5 .\\%
79   \epsfsetgraph{#6}}%
80%
81\def\epsfnormal#1{\epsfgetbb{#1}\epsfsetgraph{#1}}%
82%
83\def\epsfgetbb#1{%
84%
85%   The first thing we need to do is to open the
86%   PostScript file, if possible.
87%
88\openin\epsffilein=#1
89\ifeof\epsffilein\errmessage{I couldn't open #1, will ignore it}\else
90%
91%   Okay, we got it. Now we'll scan lines until we find one that doesn't
92%   start with %. We're looking for the bounding box comment.
93%
94   {\epsffileoktrue \chardef\other=12
95    \def\do##1{\catcode`##1=\other}\dospecials \catcode`\ =10
96    \loop
97       \read\epsffilein to \epsffileline
98       \ifeof\epsffilein\epsffileokfalse\else
99%
100%   We check to see if the first character is a % sign;
101%   if not, we stop reading (unless the line was entirely blank);
102%   if so, we look further and stop only if the line begins with
103%   `%%BoundingBox:'.
104%
105          \expandafter\epsfaux\epsffileline:. \\%
106       \fi
107   \ifepsffileok\repeat
108   \ifepsfbbfound\else
109    \ifepsfverbose\message{No bounding box comment in #1; using defaults}\fi\fi
110   }\closein\epsffilein\fi}%
111%
112%   Now we have to calculate the scale and offset values to use.
113%   First we compute the natural sizes.
114%
115\def\epsfsetgraph#1{%
116   \epsfrsize=\epsfury\pspoints
117   \advance\epsfrsize by-\epsflly\pspoints
118   \epsftsize=\epsfurx\pspoints
119   \advance\epsftsize by-\epsfllx\pspoints
120%
121%   If `epsfxsize' is 0, we default to the natural size of the picture.
122%   Otherwise we scale the graph to be \epsfxsize wide.
123%
124   \epsfxsize\epsfsize\epsftsize\epsfrsize
125   \ifnum\epsfxsize=0 \ifnum\epsfysize=0
126      \epsfxsize=\epsftsize \epsfysize=\epsfrsize
127%
128%   We have a sticky problem here:  TeX doesn't do floating point arithmetic!
129%   Our goal is to compute y = rx/t. The following loop does this reasonably
130%   fast, with an error of at most about 16 sp (about 1/4000 pt).
131%
132     \else\epsftmp=\epsftsize \divide\epsftmp\epsfrsize
133       \epsfxsize=\epsfysize \multiply\epsfxsize\epsftmp
134       \multiply\epsftmp\epsfrsize \advance\epsftsize-\epsftmp
135       \epsftmp=\epsfysize
136       \loop \advance\epsftsize\epsftsize \divide\epsftmp 2
137       \ifnum\epsftmp>0
138          \ifnum\epsftsize<\epsfrsize\else
139             \advance\epsftsize-\epsfrsize \advance\epsfxsize\epsftmp \fi
140       \repeat
141     \fi
142   \else\epsftmp=\epsfrsize \divide\epsftmp\epsftsize
143     \epsfysize=\epsfxsize \multiply\epsfysize\epsftmp
144     \multiply\epsftmp\epsftsize \advance\epsfrsize-\epsftmp
145     \epsftmp=\epsfxsize
146     \loop \advance\epsfrsize\epsfrsize \divide\epsftmp 2
147     \ifnum\epsftmp>0
148        \ifnum\epsfrsize<\epsftsize\else
149           \advance\epsfrsize-\epsftsize \advance\epsfysize\epsftmp \fi
150     \repeat
151   \fi
152%
153%  Finally, we make the vbox and stick in a \special that dvips can parse.
154%
155   \ifepsfverbose\message{#1: width=\the\epsfxsize, height=\the\epsfysize}\fi
156   \epsftmp=10\epsfxsize \divide\epsftmp\pspoints
157   \vbox to\epsfysize{\vfil\hbox to\epsfxsize{%
158      \special{PSfile=#1 llx=\epsfllx\space lly=\epsflly\space
159          urx=\epsfurx\space ury=\epsfury\space rwi=\number\epsftmp}%
160      \hfil}}%
161\epsfxsize=0pt\epsfysize=0pt}%
162
163%
164%   We still need to define the tricky \epsfaux macro. This requires
165%   a couple of magic constants for comparison purposes.
166%
167{\catcode`\%=12 \global\let\epsfpercent=%\global\def\epsfbblit{%BoundingBox}}%
168%
169%   So we're ready to check for `%BoundingBox:' and to grab the
170%   values if they are found.
171%
172\long\def\epsfaux#1#2:#3\\{\ifx#1\epsfpercent
173   \def\testit{#2}\ifx\testit\epsfbblit
174      \epsfgrab #3 . . . \\%
175      \epsffileokfalse
176      \global\epsfbbfoundtrue
177   \fi\else\ifx#1\par\else\epsffileokfalse\fi\fi}%
178%
179%   Here we grab the values and stuff them in the appropriate definitions.
180%
181\def\epsfgrab #1 #2 #3 #4 #5\\{%
182   \global\def\epsfllx{#1}\ifx\epsfllx\empty
183      \epsfgrab #2 #3 #4 #5 .\\\else
184   \global\def\epsflly{#2}%
185   \global\def\epsfurx{#3}\global\def\epsfury{#4}\fi}%
186%
187%   We default the epsfsize macro.
188%
189\def\epsfsize#1#2{\epsfxsize}
190%
191%   Finally, another definition for compatibility with older macros.
192%
193\let\epsffile=\epsfbox
194