%%% -*- Mode: TeX -*-

%%%% Merging PostScript Illustrations

%%% These TeX macros enable easy insertion of PostScript
%%% Illustrations into documents produced by TeX and LaTeX.
%%% The illustrations are automatically sized.

%%% Usage
%%%    \input{psadobe}         % load macros
%%%    \psadobe{filename.ps}   % for each illustration

%%% References
%%%   Adobe Systems, Inc
%%%     PostScript Language Reference Manual
%%%     Addison-Wesley, 1986
%%%     Appendix C, pages 279-286

%%% Copyright Gerald Roylance 1987
%%%   This file may be copied or distributed as long as this
%%%   copyright notice is not removed.

%%% Bugs and Fixes
%%%   convert \mag to a float at runtime [Rich, 12/87]
%%%   removed spurious cr's in psadobeextract, -bb, -header [Rich, 11/87]
%%%   make psfile switch to horizontal mode 
%%%     now LaTeX \leavevmode isn't needed [GLR, 11/87]
%%%   changed \errmessage to \message for file not found
%%%     in \psadobeheader [Rich, 12/87]
%%%   added \psadobepercent [Rich, 12/87]
%%%   added \xpsadobe and \xpsadobepercent [Rich, 2/22]


%%%% Usage

%%% To load the macros, use
%%%       \input{psadobe}
%%% The macros are read from TeX's default input area.

%%% To insert an illustration, use
%%%       \psadobe{filename.ps}
%%% The expression is treated exactly like a character.
%%% You will probably want to center it or do other positioning
%%% commands.  The illustration is also magnified by the \mag
%%% parameter.  If you don't want this magnification, then
%%% use
%%%       \psadobenomag{filename.ps}

%%% The filename may present some difficulty because
%%% both TeX and the dvi converter use the filename.
%%% You may have to provide a fully qualified filename.

%%% The illustrations to be inserted must use
%%% the Adobe PostScript File Structuring Conventions (PFSC).
%%% These macros do automatic sizing of the inserted illustation
%%% by reading the PFSC header comment of the PostScript file.
%%% The Lisp Machine program Illustrate produces the correct format.
%%% MacIntosh programs do not provide a tight bounding box.

%%% The .dvi converter must implement \special{PSFILE...}
%%% (and, of course, output PostScript).  The MIT AI Lab
%%% uses the Unix program dvi2ps (available from ???) and
%%% a Lisp Machine dvi converter (available from the
%%% Symbolics LispMachine Users' Group).

%%% For further information, see MIT AI Laboratory Working Paper 299a.

%%% Source locations at the MIT AI Laboratory
%%%   Canonical
%%%     OZ:OZ:<GLR.TEXT>PSADOBE.TEX
%%%   Slaved
%%%     OZ:TEXLOCAL:PSADOBE.TEX
%%%        -> OZ:KANSAS:<TEX.LOCAL>PSADOBE.TEX
%%%     wh:/usr/lib/tex/localinputs/psadobe.tex
%%%     REAGAN:SYS;TEX;INPUTS;PSADOBE.TEX
%%%    


%%%% Subroutines

%%% The fundamental insertion macro.  This inserts a PostScript file
%%% putting the origin at (x,y) and building a TeX box whose
%%% size is (w,h).  Nothing fancy is done.  x,y,w,h are TeX dimensions.
%%% The magnification m is a string that should be something like 1.40.
%%%
%%% psfile(x,y,w,h,m,filename)
%%%
\newbox\psvoid
\def\psfile#1#2#3#4#5#6{%
  \unhbox\psvoid% switch to horizontal mode (just like a letter would)
  \hbox to #3{%
     \kern-#1%
     \vbox to #4{%
        \vfil%
        \kern#2%
        \special{psfile=#6 hscale=#5 vscale=#5}%
        \kern-#2}%
     \kern#1\hfil}}

%%% Convert an integer to a floating point string.
%%% This gross hack works if \mag < 10000
%%%
\def\psfloater#1#2#3#4#5{#2.#3#4#5}
\def\psklutz#1{%
  \edef\next{\count255=#1 \advance\count255 by 10000 \the\count255}
  \next}
\def\psfloat#1{%
  \expandafter\psfloater\psklutz{#1}}

%%% But it loses, so use a worse hack [not used -- CR]
\newcount\float \float=\mag \advance\float by 10000
\def\floater#1#2#3#4#5{#2.#3#4#5}
\def\psadobefloatmag{\expandafter\floater\the\float}


%%%% Parse a PostScript Header

%%% The header looks like
%%%
%%%    %!PS-Adobe-1.0
%%%    %%Creator: Illustrate Version 11.0
%%%    %%CreationDate: 6/23/87 17:35:01
%%%    %%Pages: 1
%%%    %%BoundingBox: 466.12912 718.63934 612.0283 792.0283
%%%    %%EndComments
%%%
%%% *** Should complain if #2 is (atend)

%%% Dimensions of the illustration
\newdimen\psadobex
\newdimen\psadobey
\newdimen\psadobew
\newdimen\psadobeh

\newif\ifpsadobeflag

\newread\psadobefile

%%% psadobeextract(junk llx lly urx ury true)

\catcode`\%=12

\def\psadobeextract #1 #2 #3 #4 #5%%BoundingBox:\heaven#6{\global\psadobex= #2 #6bp
  \global\psadobey= #3 #6bp
  \global\psadobew= #4 #6bp
  \global\advance\psadobew by -\psadobex
  \global\psadobeh= #5 #6bp
  \global\advance\psadobeh by -\psadobey}

\def\psadobebb #1%%BoundingBox:#2\EOL#3{\ifx#2\heaven \psadobeflagtrue 
    \else \psadobeflagfalse \psadobeextract#2{#3}\closein\psadobefile\fi}

\def\psadobeheader#1#2{\openin\psadobefile=#2
  \global\psadobex= 72 #1bp
  \global\psadobey= 72 #1bp
  \global\psadobew= 72 #1bp
  \global\psadobeh= 72 #1bp
  \ifeof\psadobefile
    \message{Postscript file not found: #2.}\else
    \begingroup
       \catcode`\%=12
       \loop
	  \ifeof\psadobefile
	     \psadobeflagfalse
	     \errmessage{No Bounding Box information in #2}\else
             \read\psadobefile to \line
             \expandafter\psadobebb\line %%BoundingBox:\heaven\EOL{#1}\fi
          \ifpsadobeflag\repeat
       \catcode`\%=14
       \endgroup\fi}

\catcode`\%=14


%%%% Automatic Sizing of a PostScript File

%%% Scale the dimensions by N/D
%%%   this blows up with overflows, so we don't use it
\def\psadobescale#1#2{%
  \multiply\psadobex by #1 \divide\psadobex by #2
  \multiply\psadobey by #1 \divide\psadobey by #2
  \multiply\psadobew by #1 \divide\psadobew by #2
  \multiply\psadobeh by #1 \divide\psadobeh by #2}

%%% psadobemagnify(mag, true, file)
%%%
\def\psadobemagnify#1#2#3{%
  \psadobeheader{#2}{#3}%
  \psfile{\psadobex}{\psadobey}{\psadobew}%
         {\psadobeh}{#1}{#3}}



%%%% External Macros

%%% These are the advertised calls
%%%   everything else may change

%%% Insert a Postscript file with scaling by \mag
%%% [Float mag has to be done here because of expansion problems -- CR]

\def\psadobe#1{\xpsadobe{#1}{#1}}

%%% Following auxiliary definition allows a different pathname syntax
%%% to be used on Tex'ing machine versus dvi->ps machine.

\def\xpsadobe#1#2{%
  \psadobeheader{}{#1}%
  {\float=\mag \advance\float by 10000
   \psfile{\psadobex}{\psadobey}{\psadobew}%
         {\psadobeh}{\expandafter\floater\the\float}{#2}}}

%%% Insert a PostScript file without scaling by \mag
%%%
\def\psadobenomag#1{%
  \psadobemagnify{1.000}{true}{#1}}

%%% Insert a PostScript file, scaling by current value of \mag and a given percent
%%% N.B. The first argument is the *percent* scaling (i.e. 100 is normal size).
%%%
%%% psadobepercent( percent, file )

\def\psadobepercent#1#2{\xpsadobepercent{#1}{#2}{#2}}

%%% Similarly to \xpsadobe

\def\xpsadobepercent#1#2#3{%
  \psadobeheader{}{#2}%
  \global\divide\psadobex by 100\global\multiply\psadobex by #1%
  \global\divide\psadobey by 100\global\multiply\psadobey by #1%
  \global\divide\psadobew by 100\global\multiply\psadobew by #1%
  \global\divide\psadobeh by 100\global\multiply\psadobeh by #1%
  {\float=\mag \multiply\float by #1\divide\float by 100
	\advance\float by 10000
	  \psfile{\psadobex}{\psadobey}{\psadobew}%
	       {\psadobeh}{\expandafter\floater\the\float}{#3}}}

\endinput
