% pgf/tikz library
% for signal flow diagrams
%
% Author: Dr. Karlheinz Ochs, Ruhr-University of Bochum, Germany
% Version: 0.1
% Date: 2007/01/05


%
% Styles for nodes, terminals, and operators.
%
% - nodes and terminals
\tikzstyle{node style}%
   = [draw=\pathdrawcolor,
      fill=\pathdrawcolor,
      minimum size=\nodesize]
\tikzstyle{terminal style}%
   = [draw=\pathdrawcolor,
      fill=\pathfillcolor,
      line width=\pathlinewidth,
      minimum size=\terminalsize]
\tikzstyle{node}%
   = [node shape,
      node style]
\tikzstyle{input}%
   = [input terminal shape,
      terminal style]
\tikzstyle{output}%
   = [output terminal shape,
      terminal style]
% - operators
\tikzstyle{operator color}%
   = [draw=\operatordrawcolor,
      fill=\operatorfillcolor]
\tikzstyle{operator style}%
   = [operator color,
      line width=\operatorlinewidth,
      minimum size=\operatorsize]
\tikzstyle{multiplier}%
   = [multiplier shape,
      operator style]
\tikzstyle{modulator}%
   = [modulator shape,
      operator style]
\tikzstyle{adder}%
   = [adder shape,
      operator style]
\tikzstyle{delay}%
   = [rectangle,
      operator style,
      minimum size=\delaysize]


%
% Circular shapes of operator symbols.
%
% #1 = additional inner symbol
\newcommand{\circularshape}[1]{%
  \savedanchor\centerpoint{\pgfpointorigin}
  % radius is half of maximum of \pgfshapeminwidth and \pgfshapeminheight
  \saveddimen\radius{%
    \setlength{\pgf@xa}{\pgfshapeminwidth}
    \setlength{\pgf@ya}{\pgfshapeminheight}
    \ifdim\pgf@xa>\pgf@ya
      \pgf@x=.5\pgf@xa
    \else
      \pgf@x=.5\pgf@ya
    \fi
  }
  \inheritanchor[from=circle]{center}
  \inheritanchorborder[from=circle]
  \inheritanchor[from=circle]{north}
  \inheritanchor[from=circle]{north east}
  \inheritanchor[from=circle]{east}
  \inheritanchor[from=circle]{south east}
  \inheritanchor[from=circle]{south}
  \inheritanchor[from=circle]{south west}
  \inheritanchor[from=circle]{west}
  \inheritanchor[from=circle]{north west}
  \inheritanchor[from=circle]{mid}
  \inheritanchor[from=circle]{mid west}
  \inheritanchor[from=circle]{mid east}
  \inheritanchor[from=circle]{base}
  \inheritanchor[from=circle]{base west}
  \inheritanchor[from=circle]{base east}
  % circular shaped frame
  \inheritbackgroundpath[from=circle]
  % additional inner symbol
  \foregroundpath{
    \centerpoint%
    \pgf@xc=\pgf@x%
    \pgf@yc=\pgf@y%
    \@tempdima=\radius%
    #1
  }
  % no text
  \nodeparts{}
}


%
% Anchors for default text box, see below.
%
\newcommand{\stdaboveanchor}{%
  \anchor{text}{%
    \centerpoint
    \advance\pgf@x by-0.5\wd\pgfnodeparttextbox%
    \advance\pgf@y by\radius
    \advance\pgf@y by+0.5mm
  }
}
\newcommand{\stdrightanchor}{%
  \anchor{text}{%
    \centerpoint
    \advance\pgf@x by\radius
    \advance\pgf@x by+0.5mm
    \advance\pgf@y by-0.5\ht\pgfnodeparttextbox%
    \advance\pgf@y by+0.5\dp\pgfnodeparttextbox%
  }
}
\newcommand{\stdbelowanchor}{%
  \anchor{text}{%
    \centerpoint
    \advance\pgf@x by-0.5\wd\pgfnodeparttextbox%
    \advance\pgf@y by-\ht\pgfnodeparttextbox%
    \advance\pgf@y by-\radius
    \advance\pgf@y by-0.5mm
  }
}
\newcommand{\stdleftanchor}{%
  \anchor{text}{%
    \centerpoint
    \advance\pgf@x by-\radius
    \advance\pgf@x by-\wd\pgfnodeparttextbox%
    \advance\pgf@x by-0.5mm
    \advance\pgf@y by-0.5\ht\pgfnodeparttextbox%
    \advance\pgf@y by+0.5\dp\pgfnodeparttextbox%
  }
}


%
% Circular shapes of operator symbols with default text box.
%
% #1 = place of the default text box
\newbox\pgfnodepartabovebox
\newbox\pgfnodepartrightbox
\newbox\pgfnodepartbelowbox
\newbox\pgfnodepartleftbox
\newcommand{\labelledcircularshape}[1]{%
  % node parts
  \nodeparts{text,above,right,below,left}
  % anchors
  \savedanchor\centerpoint{\pgfpointorigin}
  % radius is half of maximum of \pgfshapeminwidth and \pgfshapeminheight
  \saveddimen\radius{%
    \setlength{\pgf@xa}{\pgfshapeminwidth}
    \setlength{\pgf@ya}{\pgfshapeminheight}
    \ifdim\pgf@xa>\pgf@ya
      \pgf@x=.5\pgf@xa
    \else
      \pgf@x=.5\pgf@ya
    \fi
  }
  \inheritanchor[from=circle]{center}
  \inheritanchorborder[from=circle]
  \inheritanchor[from=circle]{north}
  \inheritanchor[from=circle]{north east}
  \inheritanchor[from=circle]{east}
  \inheritanchor[from=circle]{south east}
  \inheritanchor[from=circle]{south}
  \inheritanchor[from=circle]{south west}
  \inheritanchor[from=circle]{west}
  \inheritanchor[from=circle]{north west}
  \inheritanchor[from=circle]{mid}
  \inheritanchor[from=circle]{mid west}
  \inheritanchor[from=circle]{mid east}
  \inheritanchor[from=circle]{base}
  \inheritanchor[from=circle]{base west}
  \inheritanchor[from=circle]{base east}
  #1
  \anchor{above}{%
    \centerpoint
    \advance\pgf@x by-0.5\wd\pgfnodepartabovebox%
    \advance\pgf@y by\radius
    \advance\pgf@y by+0.5mm
  }
  \anchor{right}{%
    \centerpoint
    \advance\pgf@x by\radius
    \advance\pgf@x by+0.5mm
    \advance\pgf@y by-0.5\ht\pgfnodepartrightbox%
    \advance\pgf@y by+0.5\dp\pgfnodeparttextbox%
  }
  \anchor{below}{%
    \centerpoint
    \advance\pgf@x by-0.5\wd\pgfnodepartbelowbox%
    \advance\pgf@y by-\ht\pgfnodepartbelowbox%
    \advance\pgf@y by-\radius
    \advance\pgf@y by-0.5mm
  }
  \anchor{left}{%
    \centerpoint
    \advance\pgf@x by-\radius
    \advance\pgf@x by-\wd\pgfnodepartleftbox%
    \advance\pgf@x by-0.5mm
    \advance\pgf@y by-0.5\ht\pgfnodepartleftbox%
    \advance\pgf@y by+0.5\dp\pgfnodeparttextbox%
  }
  % circular shaped frame
  \inheritbackgroundpath[from=circle]
}


%
% Shape of a node.
%
\pgfdeclareshape{node shape}{\labelledcircularshape{\stdaboveanchor}}%

%
% Shape of an input terminal.
%
\pgfdeclareshape{input terminal shape}{\labelledcircularshape{\stdleftanchor}}%
%
% Shape of an output terminal.
%
\pgfdeclareshape{output terminal shape}{\labelledcircularshape{\stdrightanchor}}%
%
% Shape of a multiplier.
%
\pgfdeclareshape{multiplier shape}{\labelledcircularshape{\stdleftanchor}}%
%
% Shape of a modulator.
%
\pgfdeclareshape{modulator shape}{\circularshape{%
    \pgfpathmoveto{\pgfpointadd{\pgfpoint{\pgf@xc}{\pgf@yc}}
                  {\pgfpoint{-0.707107\@tempdima}{-0.707107\@tempdima}}}
    \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xc}{\pgf@yc}}
                  {\pgfpoint{0.707107\@tempdima}{0.707107\@tempdima}}}
    \pgfpathmoveto{\pgfpointadd{\pgfpoint{\pgf@xc}{\pgf@yc}}
                  {\pgfpoint{0.707107\@tempdima}{-0.707107\@tempdima}}}
    \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xc}{\pgf@yc}}
                  {\pgfpoint{-0.707107\@tempdima}{0.707107\@tempdima}}}
}}%
%
% Shape of an adder.
%
\pgfdeclareshape{adder shape}{\circularshape{%
    \pgfpathmoveto{\pgfpointadd{\pgfpoint{\pgf@xc}{\pgf@yc}}
                  {\pgfpoint{-0.5\@tempdima}{0\@tempdima}}}
    \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xc}{\pgf@yc}}
                  {\pgfpoint{0.5\@tempdima}{0\@tempdima}}}
    \pgfpathmoveto{\pgfpointadd{\pgfpoint{\pgf@xc}{\pgf@yc}}
                  {\pgfpoint{0\@tempdima}{-0.5\@tempdima}}}
    \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xc}{\pgf@yc}}
                  {\pgfpoint{0\@tempdima}{0.5\@tempdima}}}
}}%

\endinput
