Debian Codegraph @

texmf-dist/tex/plain/knuth-local/xepsf.tex

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