2 % Written by Tomas Rokicki of Radical Eye Software, 29 Mar 1989.
3 % Revised by Don Knuth, 3 Jan 1990.
5 % TeX macros to include an Encapsulated PostScript graphic.
6 % Works by finding the bounding box comment,
7 % calculating the correct scale values, and inserting a vbox
8 % of the appropriate size at the current position in the TeX document.
11 % \input epsf % somewhere early on in your TeX file
12 % \epsfbox{filename.ps} % where you want to insert a vbox for a figure
14 % The effect will be to typeset the figure as a TeX box, at the
15 % point of your \epsfbox command. By default, the graphic will have its
16 % `natural' width (namely the width of its bounding box, as described
17 % in filename.ps). The TeX box will have depth zero.
19 % You can enlarge or reduce the figure by saying
20 % \epsfxsize=<dimen> \epsfille{filename.ps}
21 % instead. Then the width of the TeX box will be \epsfxsize, and its
22 % height will be scaled proportionately.
23 % (The \epsfbox macro resets \epsfxsize to zero after each use.)
25 % If you want TeX to report the size of the figure (as a message
26 % on your terminal when it processes each figure), say `\epsfverbosetrue'.
29 \newread\epsffilein % file to \read
30 \newif\ifepsffileok % continue looking for the bounding box?
31 \newif\ifepsfbbfound % success?
32 \newif\ifepsfverbose % report what you're making?
33 \newdimen\epsfxsize % horizontal size after scaling
34 \newdimen\epsfysize % vertical size after scaling
35 \newdimen\epsftsize % horizontal size before scaling
36 \newdimen\epsfrsize % vertical size before scaling
37 \newdimen\epsftmp % register for arithmetic manipulation
38 \newdimen\pspoints % conversion factor
40 \pspoints=1truebp % Adobe points are `big'
41 \epsfxsize=0pt % Default value, means `use natural size'
46 % The first thing we need to do is to open the
47 % PostScript file, if possible.
50 \ifeof\epsffilein\errmessage{I couldn't open #1, will ignore it}\else
52 % Okay, we got it. Now we'll scan lines until we find one that doesn't
53 % start with %. We're looking for the bounding box comment.
55 {\epsffileoktrue \chardef\other=12
56 \def\do##1{\catcode`##1=\other}\dospecials \catcode`\ =10
58 \read\epsffilein to \epsffileline
59 \ifeof\epsffilein\epsffileokfalse\else
61 % We check to see if the first character is a % sign;
62 % if not, we stop reading (unless the line was entirely blank);
63 % if so, we look further and stop only if the line begins with
66 \expandafter\epsfaux\epsffileline. \\%
70 \ifepsfverbose\message{No bounding box comment in #1; using defaults}\fi
71 \global\def\epsfllx{72}%
72 \global\def\epsflly{72}%
73 \global\def\epsfurx{540}%
74 \global\def\epsfury{720}\fi
77 % Now we have to calculate the scale and offset values to use.
78 % First we compute the natural sizes.
80 \epsfrsize=\epsfury\pspoints
81 \advance\epsfrsize by-\epsflly\pspoints
82 \epsftsize=\epsfurx\pspoints
83 \advance\epsftsize by-\epsfllx\pspoints
85 % If `epsfxsize' is 0, we default to the natural size of the picture.
86 % Otherwise we scale the graph to be \epsfxsize wide.
88 \ifnum\epsfxsize=0 \epsfxsize=\epsftsize \epsfysize=\epsfrsize
90 % We have a sticky problem here: TeX doesn't do floating point arithmetic!
91 % Our goal is to compute y = rx/t. The following loop does this reasonably
92 % fast, with an error of at most about 16 sp (about 1/4000 pt).
94 \else\epsftmp=\epsfrsize \divide\epsftmp\epsftsize
95 \epsfysize=\epsfxsize \multiply\epsfysize\epsftmp
96 \multiply\epsftmp\epsftsize \advance\epsfrsize-\epsftmp
98 \loop \advance\epsfrsize\epsfrsize \divide\epsftmp 2
100 \ifnum\epsfrsize<\epsftsize\else
101 \advance\epsfrsize-\epsftsize \advance\epsfysize\epsftmp \fi
105 % Finally, we make the vbox and stick in a \special that dvips can parse.
107 \ifepsfverbose\message{#1: width=\the\epsfxsize, height=\the\epsfysize}\fi
108 \epsftmp=10\epsfxsize \divide\epsftmp\pspoints
109 \vbox to\epsfysize{\vfil\hbox to\epsfxsize{%
110 \special{psfile=#1 llx=\epsfllx\space lly=\epsflly\space
111 urx=\epsfurx\space ury=\epsfury\space rwi=\number\epsftmp}%
116 % We still need to define the tricky \epsfaux macro. This requires
117 % a couple of magic constants for comparison purposes.
119 {\catcode`\%=12 \global\let\epsfpercent=%\global\def\epsfbblit{%BoundingBox:}}%
121 % So we're ready to check for `%BoundingBox:' and to grab the
122 % values if they are found.
124 \long\def\epsfaux#1#2 #3\\{\ifx#1\epsfpercent
125 \def\testit{#2}\ifx\testit\epsfbblit
126 \epsfgrab #3 . . . \\%
128 \global\epsfbbfoundtrue
129 \fi\else\ifx#1\par\else\epsffileokfalse\fi\fi}%
131 % Here we grab the values and stuff them in the appropriate definitions.
133 \def\epsfgrab#1 #2 #3 #4 #5\\{\global\def\epsfllx{#1}\global\def\epsflly{#2}%
134 \global\def\epsfurx{#3}\global\def\epsfury{#4}}%
136 % Finally, another definition for compatibility with older macros.
138 \let\epsffile=\epsfbox