% TeXdraw macros

% $Id: texdraw.tex,v 2.3 1995/12/19 texdraw-V2R0 $

%   Copyright (C) 1991-1995  Peter Kabal

% The TeXdraw routines in this file are provided free of charge without
% warranty of any kind.  Note that the TeXdraw routines are copyrighted.
% They may be distributed freely provided that the recipients also
% acquire the right to distribute them freely.  The notices to this
% effect must be preserved when the files are distributed.

%  Peter Kabal
%  Department of Electrical Engineering
%  McGill University
%  3480 University
%  Montreal, Quebec
%  Canada  H3A 2A7

%  kabal@TSP.EE.McGill.CA
\def\setRevDate $#1 #2 #3${#2}
\def\TeXdrawId{\setRevDate $Date: 1995/12/19 16:40:42 $ TeXdraw V2R0}
\chardef\catamp=\the\catcode`\@
\catcode`\@=11
\long
\def\centertexdraw #1{\hbox to \hsize{\hss
\btexdraw #1\etexdraw
\hss}}
\def\btexdraw {\x@pix=0             \y@pix=0
\x@segoffpix=\x@pix  \y@segoffpix=\y@pix
\t@exdrawdef
\setbox\t@xdbox=\vbox\bgroup\offinterlineskip
\global\d@bs=0
\global\t@extonlytrue
\global\p@osinitfalse
\s@avemove \x@pix \y@pix
\m@pendingfalse
\global\p@osinitfalse
\p@athfalse
\the\everytexdraw}
\def\etexdraw {\ift@extonly \else
\t@drclose
\fi
\egroup
\ifdim \wd\t@xdbox>0pt
\t@xderror {TeXdraw box non-zero size,
possible extraneous text}%
\fi
\vbox {\offinterlineskip
\pixtobp \xminpix \l@lxbp  \pixtobp \yminpix \l@lybp
\pixtobp \xmaxpix \u@rxbp  \pixtobp \ymaxpix \u@rybp
\hbox{\t@xdinclude 
[{\l@lxbp},{\l@lybp}][{\u@rxbp},{\u@rybp}]{\p@sfile}}%
\pixtodim \xminpix \t@xpos  \pixtodim \yminpix \t@ypos
\kern \t@ypos
\hbox {\kern -\t@xpos
\box\t@xdbox
\kern \t@xpos}%
\kern -\t@ypos\relax}}
\def\drawdim #1 {\def\d@dim{#1\relax}}
\def\setunitscale #1 {\edef\u@nitsc{#1}%
\realmult \u@nitsc \s@egsc \d@sc}
\def\relunitscale #1 {\realmult {#1}\u@nitsc \u@nitsc
\realmult \u@nitsc \s@egsc \d@sc}
\def\setsegscale #1 {\edef\s@egsc {#1}%
\realmult \u@nitsc \s@egsc \d@sc}
\def\relsegscale #1 {\realmult {#1}\s@egsc \s@egsc
\realmult \u@nitsc \s@egsc \d@sc}
\def\bsegment {\ifp@ath
\f@lushbs
\f@lushmove
\fi
\begingroup
\x@segoffpix=\x@pix
\y@segoffpix=\y@pix
\setsegscale 1
\global\advance \d@bs by 1\relax}
\def\esegment {\endgroup
\ifnum \d@bs=0
\writetx {es}%
\else
\global\advance \d@bs by -1
\fi}
\def\savecurrpos (#1 #2){\getsympos (#1 #2)\a@rgx\a@rgy
\s@etcsn \a@rgx {\the\x@pix}%
\s@etcsn \a@rgy {\the\y@pix}}
\def\savepos (#1 #2)(#3 #4){\getpos (#1 #2)\a@rgx\a@rgy
\coordtopix \a@rgx \t@pixa
\advance \t@pixa by \x@segoffpix
\coordtopix \a@rgy \t@pixb
\advance \t@pixb by \y@segoffpix
\getsympos (#3 #4)\a@rgx\a@rgy
\s@etcsn \a@rgx {\the\t@pixa}%
\s@etcsn \a@rgy {\the\t@pixb}}
\def\linewd #1 {\coordtopix {#1}\t@pixa
\f@lushbs
\writetx {\the\t@pixa\space sl}}
\def\setgray #1 {\f@lushbs
\writetx {#1 sg}}
\def\lpatt (#1){\listtopix (#1)\p@ixlist
\f@lushbs
\writetx {[\p@ixlist] sd}}
\def\lvec (#1 #2){\getpos (#1 #2)\a@rgx\a@rgy
\s@etpospix \a@rgx \a@rgy
\writeps {\the\x@pix\space \the\y@pix\space lv}}
\def\rlvec (#1 #2){\getpos (#1 #2)\a@rgx\a@rgy
\r@elpospix \a@rgx \a@rgy
\writeps {\the\x@pix\space \the\y@pix\space lv}}
\def\move (#1 #2){\getpos (#1 #2)\a@rgx\a@rgy
\s@etpospix \a@rgx \a@rgy
\s@avemove \x@pix \y@pix}
\def\rmove (#1 #2){\getpos (#1 #2)\a@rgx\a@rgy
\r@elpospix \a@rgx \a@rgy
\s@avemove \x@pix \y@pix}
\def\lcir r:#1 {\coordtopix {#1}\t@pixa
\writeps {\the\t@pixa\space cr}%
\r@elupd \t@pixa \t@pixa
\r@elupd {-\t@pixa}{-\t@pixa}}
\def\fcir f:#1 r:#2 {\coordtopix {#2}\t@pixa
\writeps {\the\t@pixa\space #1 fc}%
\r@elupd \t@pixa \t@pixa
\r@elupd {-\t@pixa}{-\t@pixa}}
\def\lellip rx:#1 ry:#2 {\coordtopix {#1}\t@pixa
\coordtopix {#2}\t@pixb
\writeps {\the\t@pixa\space \the\t@pixb\space el}%
\r@elupd \t@pixa \t@pixb
\r@elupd {-\t@pixa}{-\t@pixb}}
\def\fellip f:#1 rx:#2 ry:#3 {\coordtopix {#2}\t@pixa
\coordtopix {#3}\t@pixb
\writeps {\the\t@pixa\space \the\t@pixb\space #1 fe}%
\r@elupd \t@pixa \t@pixb
\r@elupd {-\t@pixa}{-\t@pixb}}
\def\larc r:#1 sd:#2 ed:#3 {\coordtopix {#1}\t@pixa
\writeps {\the\t@pixa\space #2 #3 ar}}
\def\ifill f:#1 {\writeps {#1 fl}}
\def\lfill f:#1 {\writeps {#1 fp}}
\def\htext #1{\def\testit {#1}%
\ifx \testit\l@paren
\let\next=\h@move
\else
\let\next=\h@text
\fi
\next {#1}}
\def\rtext td:#1 #2{\def\testit {#2}%
\ifx \testit\l@paren
\let\next=\r@move
\else
\let\next=\r@text
\fi
\next td:#1 {#2}}
\def\vtext {\rtext td:90 }
\def\textref h:#1 v:#2 {\ifx #1R%
\edef\l@stuff {\hss}\edef\r@stuff {}%
\else
\ifx #1C%
\edef\l@stuff {\hss}\edef\r@stuff {\hss}%
\else
\edef\l@stuff {}\edef\r@stuff {\hss}%
\fi
\fi
\ifx #2T%
\edef\t@stuff {}\edef\b@stuff {\vss}%
\else
\ifx #2C%
\edef\t@stuff {\vss}\edef\b@stuff {\vss}%
\else
\edef\t@stuff {\vss}\edef\b@stuff {}%
\fi
\fi}
\def\avec (#1 #2){\getpos (#1 #2)\a@rgx\a@rgy
\s@etpospix \a@rgx \a@rgy
\writeps {\the\x@pix\space \the\y@pix\space (\a@type)
\the\a@lenpix\space \the\a@widpix\space av}}
\def\ravec (#1 #2){\getpos (#1 #2)\a@rgx\a@rgy
\r@elpospix \a@rgx \a@rgy
\writeps {\the\x@pix\space \the\y@pix\space (\a@type)
\the\a@lenpix\space \the\a@widpix\space av}}
\def\arrowheadsize l:#1 w:#2 {\coordtopix{#1}\a@lenpix
\coordtopix{#2}\a@widpix}
\def\arrowheadtype t:#1 {\edef\a@type{#1}}
\def\clvec (#1 #2)(#3 #4)(#5 #6)%
{\getpos (#1 #2)\a@rgx\a@rgy
\coordtopix \a@rgx\t@pixa
\advance \t@pixa by \x@segoffpix
\coordtopix \a@rgy\t@pixb
\advance \t@pixb by \y@segoffpix
\getpos (#3 #4)\a@rgx\a@rgy
\coordtopix \a@rgx\t@pixc
\advance \t@pixc by \x@segoffpix
\coordtopix \a@rgy\t@pixd
\advance \t@pixd by \y@segoffpix
\getpos (#5 #6)\a@rgx\a@rgy
\s@etpospix \a@rgx \a@rgy
\writeps {\the\t@pixa\space \the\t@pixb\space
\the\t@pixc\space \the\t@pixd\space
\the\x@pix\space \the\y@pix\space cv}}
\def\drawbb {\bsegment
\drawdim bp
\linewd 0.24
\setunitscale {\p@sfactor}
\writeps {\the\xminpix\space \the\yminpix\space mv}%
\writeps {\the\xminpix\space \the\ymaxpix\space lv}%
\writeps {\the\xmaxpix\space \the\ymaxpix\space lv}%
\writeps {\the\xmaxpix\space \the\yminpix\space lv}%
\writeps {\the\xminpix\space \the\yminpix\space lv}%
\esegment}
\def\getpos (#1 #2)#3#4{\g@etargxy #1 #2 {} \\#3#4%
\c@heckast #3%
\ifa@st
\g@etsympix #3\t@pixa
\advance \t@pixa by -\x@segoffpix
\pixtocoord \t@pixa #3%
\fi
\c@heckast #4%
\ifa@st
\g@etsympix #4\t@pixa
\advance \t@pixa by -\y@segoffpix
\pixtocoord \t@pixa #4%
\fi}
\def\getsympos (#1 #2)#3#4{\g@etargxy #1 #2 {} \\#3#4%
\c@heckast #3%
\ifa@st \else
\t@xderror {TeXdraw: invalid symbolic coordinate}%
\fi
\c@heckast #4%
\ifa@st \else
\t@xderror {TeXdraw: invalid symbolic coordinate}%
\fi}
\def\listtopix (#1)#2{\def #2{}%
\edef\l@ist {#1 }%
\m@oretrue
\loop
\expandafter\g@etitem \l@ist \\\a@rgx\l@ist
\a@pppix \a@rgx #2%
\ifx \l@ist\empty
\m@orefalse
\fi
\ifm@ore
\repeat}
\def\realmult #1#2#3{\dimen0=#1pt
\dimen2=#2\dimen0
\edef #3{\expandafter\c@lean\the\dimen2}}
\def\intdiv #1#2#3{\t@counta=#1
\t@countb=#2
\ifnum \t@countb<0
\t@counta=-\t@counta
\t@countb=-\t@countb
\fi
\t@countd=1
\ifnum \t@counta<0
\t@counta=-\t@counta
\t@countd=-1
\fi
\t@countc=\t@counta  \divide \t@countc by \t@countb
\t@counte=\t@countc  \multiply \t@counte by \t@countb
\advance \t@counta by -\t@counte
\t@counte=-1
\loop
\advance \t@counte by 1
\ifnum \t@counte<16
\multiply \t@countc by 2
\multiply \t@counta by 2
\ifnum \t@counta<\t@countb \else
\advance \t@countc by 1
\advance \t@counta by -\t@countb
\fi
\repeat
\divide \t@countb by 2
\ifnum \t@counta<\t@countb
\advance \t@countc by 1
\fi
\ifnum \t@countd<0
\t@countc=-\t@countc
\fi
\dimen0=\t@countc sp
\edef #3{\expandafter\c@lean\the\dimen0}}
\def\coordtopix #1#2{\dimen0=#1\d@dim
\dimen2=\d@sc\dimen0
\t@counta=\dimen2
\t@countb=\s@ppix
\divide \t@countb by 2
\ifnum \t@counta<0
\advance \t@counta by -\t@countb
\else
\advance \t@counta by \t@countb
\fi
\divide \t@counta by \s@ppix
#2=\t@counta}
\def\pixtocoord #1#2{\t@counta=#1%
\multiply \t@counta by \s@ppix
\dimen0=\d@sc\d@dim
\t@countb=\dimen0
\intdiv \t@counta \t@countb #2}
\def\pixtodim #1#2{\t@countb=#1%
\multiply \t@countb by \s@ppix
#2=\t@countb sp\relax}
\def\pixtobp #1#2{\dimen0=\p@sfactor pt
\t@counta=\dimen0
\multiply \t@counta by #1%
\ifnum \t@counta < 0
\advance \t@counta by -32768
\else
\advance \t@counta by 32768
\fi
\divide \t@counta by 65536
\edef #2{\the\t@counta}}
\newcount\t@counta    \newcount\t@countb
\newcount\t@countc    \newcount\t@countd
\newcount\t@counte
\newcount\t@pixa      \newcount\t@pixb
\newcount\t@pixc      \newcount\t@pixd
\newdimen\t@xpos      \newdimen\t@ypos
\newcount\xminpix      \newcount\xmaxpix
\newcount\yminpix      \newcount\ymaxpix
\newcount\a@lenpix     \newcount\a@widpix
\newcount\x@pix        \newcount\y@pix
\newcount\x@segoffpix  \newcount\y@segoffpix
\newcount\x@savepix    \newcount\y@savepix
\newcount\s@ppix
\newcount\d@bs
\newcount\t@xdnum
\global\t@xdnum=0
\newbox\t@xdbox
\newwrite\drawfile
\newif\ifm@pending
\newif\ifp@ath
\newif\ifa@st
\newif\ifm@ore
\newif \ift@extonly
\newif\ifp@osinit
\newtoks\everytexdraw
\def\l@paren{(}
\def\a@st{*}
\catcode`\%=12
\def\p@b {%!}  \def\p@p {%%}
\catcode`\%=14
\catcode`\{=12  \catcode`\}=12  \catcode`\u=1 \catcode`\v=2
\def\l@br u{v  \def\r@br u}v
\catcode `\{=1  \catcode`\}=2   \catcode`\u=11 \catcode`\v=11
{\catcode`\p=12 \catcode`\t=12
\gdef\c@lean #1pt{#1}}
\def\sppix#1/#2 {\dimen0=1#2 \s@ppix=\dimen0
\t@counta=#1%
\divide \t@counta by 2
\advance \s@ppix by \t@counta
\divide \s@ppix by #1%
\t@counta=\s@ppix
\multiply \t@counta by 65536
\advance \t@counta by 32891
\divide \t@counta by 65782
\dimen0=\t@counta sp
\edef\p@sfactor {\expandafter\c@lean\the\dimen0}}
\def\g@etargxy #1 #2 #3 #4\\#5#6{\def #5{#1}%
\ifx #5\empty
\g@etargxy #2 #3 #4 \\#5#6%  leading blank
\else
\def #6{#2}%
\def\next {#3}%
\ifx \next\empty \else
\t@xderror {TeXdraw: invalid coordinate}%
\fi
\fi}
\def\c@heckast #1{\expandafter
\c@heckastll #1\\}
\def\c@heckastll #1#2\\{\def\testit {#1}%
\ifx \testit\a@st
\a@sttrue
\else
\a@stfalse
\fi}
\def\g@etsympix #1#2{\expandafter
\ifx \csname #1\endcsname \relax
\t@xderror {TeXdraw: undefined symbolic coordinate}%
\fi
#2=\csname #1\endcsname}
\def\s@etcsn #1#2{\expandafter
\xdef\csname#1\endcsname {#2}}
\def\g@etitem #1 #2\\#3#4{\edef #4{#2}\edef #3{#1}}
\def\a@pppix #1#2{\edef\next {#1}%
\ifx \next\empty \else
\coordtopix {#1}\t@pixa
\ifx #2\empty
\edef #2{\the\t@pixa}%
\else
\edef #2{#2 \the\t@pixa}%
\fi
\fi}
\def\s@etpospix #1#2{\coordtopix {#1}\x@pix
\advance \x@pix by \x@segoffpix
\coordtopix {#2}\y@pix
\advance \y@pix by \y@segoffpix
\u@pdateminmax \x@pix \y@pix}
\def\r@elpospix #1#2{\coordtopix {#1}\t@pixa
\advance \x@pix by \t@pixa
\coordtopix {#2}\t@pixa
\advance \y@pix by \t@pixa
\u@pdateminmax \x@pix \y@pix}
\def\r@elupd #1#2{\t@counta=\x@pix
\advance\t@counta by #1%
\t@countb=\y@pix
\advance\t@countb by #2%
\u@pdateminmax \t@counta \t@countb}
\def\u@pdateminmax #1#2{\ifnum #1>\xmaxpix
\global\xmaxpix=#1%
\fi
\ifnum #1<\xminpix
\global\xminpix=#1%
\fi
\ifnum #2>\ymaxpix
\global\ymaxpix=#2%
\fi
\ifnum #2<\yminpix
\global\yminpix=#2%
\fi}
\def\s@avemove #1#2{\x@savepix=#1\y@savepix=#2%
\m@pendingtrue
\ifp@osinit \else
\global\p@osinittrue
\global\xminpix=\x@savepix \global\yminpix=\y@savepix
\global\xmaxpix=\x@savepix \global\ymaxpix=\y@savepix
\fi}
\def\f@lushmove {\global\p@osinittrue
\ifm@pending
\writetx {\the\x@savepix\space \the\y@savepix\space mv}%
\m@pendingfalse
\p@athfalse
\fi}
\def\f@lushbs {\loop
\ifnum \d@bs>0
\writetx {bs}%
\global\advance \d@bs by -1
\repeat}
\def\h@move #1#2 #3)#4{\move (#2 #3)%
\h@text {#4}}
\def\h@text #1{\pixtodim \x@pix \t@xpos
\pixtodim \y@pix \t@ypos
\vbox to 0pt{\normalbaselines
\t@stuff
\kern -\t@ypos
\hbox to 0pt{\l@stuff
\kern \t@xpos
\hbox {#1}%
\kern -\t@xpos
\r@stuff}%
\kern \t@ypos
\b@stuff\relax}}
\def\r@move td:#1 #2#3 #4)#5{\move (#3 #4)%
\r@text td:#1 {#5}}
\def\r@text td:#1 #2{\vbox to 0pt{\pixtodim \x@pix \t@xpos
\pixtodim \y@pix \t@ypos
\kern -\t@ypos
\hbox to 0pt{\kern \t@xpos
\rottxt {#1}{\z@sb {#2}}%
\hss}%
\vss}}
\def\z@sb #1{\vbox to 0pt{\normalbaselines
\t@stuff
\hbox to 0pt{\l@stuff \hbox {#1}\r@stuff}%
\b@stuff}}
\ifx \rotatebox\@undefined
\def\rottxt #1#2{\bgroup
\special {ps: gsave currentpoint currentpoint translate
#1\space neg rotate
neg exch neg exch translate}%
#2%
\special {ps: currentpoint grestore moveto}%
\egroup}
\else
\let\rottxt=\rotatebox
\fi
\ifx \t@xderror\@undefined
\let\t@xderror=\errmessage
\fi
\def\t@exdrawdef {\sppix 300/in
\drawdim in
\edef\u@nitsc {1}%
\setsegscale 1
\arrowheadsize l:0.16 w:0.08
\arrowheadtype t:T
\textref h:L v:B }
\ifx \includegraphics\@undefined
\def\t@xdinclude [#1,#2][#3,#4]#5{%
\begingroup
\message {<#5>}%
\leavevmode
\t@counta=-#1%
\t@countb=-#2%
\setbox0=\hbox{%
\special {PSfile="#5"\space
hoffset=\the\t@counta\space voffset=\the\t@countb}}%
\t@ypos=#4 bp%
\advance \t@ypos by -#2 bp%
\t@xpos=#3 bp%
\advance \t@xpos by -#1 bp%
\dp0=0pt \ht0=\t@ypos  \wd0=\t@xpos
\box0%
\endgroup}
\else
\let\t@xdinclude=\includegraphics
\fi
\def\writeps #1{\f@lushbs
\f@lushmove
\p@athtrue
\writetx {#1}}
\def\writetx #1{\ift@extonly
\global\t@extonlyfalse
\t@xdpsfn \p@sfile
\t@dropen \p@sfile
\fi
\w@rps {#1}}
\def\w@rps #1{\immediate\write\drawfile {#1}}
\def\t@xdpsfn #1{%
\global\advance \t@xdnum by 1
\ifnum \t@xdnum<10
\xdef #1{\jobname.ps\the\t@xdnum}
\else
\xdef #1{\jobname.p\the\t@xdnum}
\fi
}
\def\t@dropen #1{%
\immediate\openout\drawfile=#1%
\w@rps {\p@b PS-Adobe-3.0 EPSF-3.0}%
\w@rps {\p@p BoundingBox: (atend)}%
\w@rps {\p@p Title: TeXdraw drawing: #1}%
\w@rps {\p@p Pages: 1}%
\w@rps {\p@p Creator: \TeXdrawId}%
\w@rps {\p@p CreationDate: \the\year/\the\month/\the\day}%
\w@rps {50 dict begin}%
\w@rps {/mv {stroke moveto} def}%
\w@rps {/lv {lineto} def}%
\w@rps {/st {currentpoint stroke moveto} def}%
\w@rps {/sl {st setlinewidth} def}%
\w@rps {/sd {st 0 setdash} def}%
\w@rps {/sg {st setgray} def}%
\w@rps {/bs {gsave} def /es {stroke grestore} def}%
\w@rps {/fl \l@br gsave setgray fill grestore}%
\w@rps    { currentpoint newpath moveto\r@br\space def}%
\w@rps {/fp {gsave setgray fill grestore st} def}%
\w@rps {/cv {curveto} def}%
\w@rps {/cr \l@br gsave currentpoint newpath 3 -1 roll 0 360 arc}%
\w@rps    { stroke grestore\r@br\space def}%
\w@rps {/fc \l@br gsave setgray currentpoint newpath}%
\w@rps    { 3 -1 roll 0 360 arc fill grestore\r@br\space def}%
\w@rps {/ar {gsave currentpoint newpath 5 2 roll arc stroke grestore} def}%
\w@rps {/el \l@br gsave /svm matrix currentmatrix def}%
\w@rps    { currentpoint translate scale newpath 0 0 1 0 360 arc}%
\w@rps    { svm setmatrix stroke grestore\r@br\space def}%
\w@rps {/fe \l@br gsave setgray currentpoint translate scale newpath}%
\w@rps    { 0 0 1 0 360 arc fill grestore\r@br\space def}%
\w@rps {/av \l@br /hhwid exch 2 div def /hlen exch def}%
\w@rps    { /ah exch def /tipy exch def /tipx exch def}%
\w@rps    { currentpoint /taily exch def /tailx exch def}%
\w@rps    { /dx tipx tailx sub def /dy tipy taily sub def}%
\w@rps    { /alen dx dx mul dy dy mul add sqrt def}%
\w@rps    { /blen alen hlen sub def}%
\w@rps    { gsave tailx taily translate dy dx atan rotate}%
\w@rps    { (V) ah ne {blen 0 gt {blen 0 lineto} if} {alen 0 lineto} ifelse}%
\w@rps    { stroke blen hhwid neg moveto alen 0 lineto blen hhwid lineto}%
\w@rps    { (T) ah eq {closepath} if}%
\w@rps    { (W) ah eq {gsave 1 setgray fill grestore closepath} if}%
\w@rps    { (F) ah eq {fill} {stroke} ifelse}%
\w@rps    { grestore tipx tipy moveto\r@br\space def}%
\w@rps {\p@sfactor\space \p@sfactor\space scale}%
\w@rps {1 setlinecap 1 setlinejoin}%
\w@rps {3 setlinewidth [] 0 setdash}%
\w@rps {0 0 moveto}%
}
\def\t@drclose {%
\bgroup
\w@rps {stroke end showpage}%
\w@rps {\p@p Trailer:}%
\pixtobp \xminpix \l@lxbp  \pixtobp \yminpix \l@lybp
\pixtobp \xmaxpix \u@rxbp  \pixtobp \ymaxpix \u@rybp
\w@rps {\p@p BoundingBox: \l@lxbp\space \l@lybp\space
\u@rxbp\space \u@rybp}%
\w@rps {\p@p EOF}%
\egroup
\immediate\closeout\drawfile
}
\catcode`\@=\catamp
