% Copyright 2018-2021 by Romano Giannetti
% Copyright 2015-2021 by Stefan Lindner
% Copyright 2013-2021 by Stefan Erhardt
% Copyright 2007-2021 by Massimo Redaelli
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the files gpl-3.0_license.txt and lppl-1-3c_license.txt for more details.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  Other shapes

%% Nothing

\pgfdeclareshape{emptyshape}{
    \savedanchor{\northeast}{%
        \pgf@x=.5\wd\pgfnodeparttextbox%
        \pgf@y=.5\ht\pgfnodeparttextbox%
    }
    \anchor{north}{\northeast\pgf@x=0cm\relax}
    \anchor{east}{\northeast\pgf@y=0cm\relax}
    \anchor{south}{\northeast\pgf@y=-\pgf@y \pgf@x=0cm\relax}
    \anchor{west}{\northeast\pgf@y=0cm\pgf@x=-\pgf@x}
    \anchor{north east}{\northeast}
    \anchor{north west}{\northeast\pgf@x=-\pgf@x}
    \anchor{south east}{\northeast\pgf@y=-\pgf@y}
    \anchor{south west}{\northeast\pgf@y=-\pgf@y\pgf@x=-\pgf@x}
    \anchor{text}{\pgfpoint{-.5\wd\pgfnodeparttextbox}{\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox}}
    \anchor{center}{
        \pgfpointorigin
    }
}
%
% Provision for changing opacity. Only expert use, see the manual.
%
\ctikzset{poles/open fill opacity/.initial=1.0}% better not touch it
\tikzset{open poles opacity/.code={%
        \ctikzset{poles/open fill opacity=#1}%
}}
\ctikzset{poles/full fill opacity/.initial=1.0}% better not touch it
\tikzset{full poles opacity/.code={%
        \ctikzset{poles/full fill opacity=#1}%
}}

%
% Provision for changing default background
%

\ctikzset{open poles fill/.initial={white}}

%% Full terminal

\pgfdeclareshape{circ}{
    \anchor{center}{
        \pgfpointorigin
    }
    \savedanchor\northwest{%
        \pgf@y=\ctikzvalof{nodes width}\pgf@circ@Rlen
        \pgf@x=-\pgf@y
    }
    \anchor{center}{ \pgf@y=0pt \pgf@x=0pt }
    \anchor{east}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{e}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{west}{ \northwest \pgf@y=0pt }
    \anchor{w}{ \northwest \pgf@y=0pt }
    \anchor{south}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{s}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{north}{ \northwest \pgf@x=0pt }
    \anchor{n}{ \northwest \pgf@x=0pt }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }
    \anchorborder{
        \pgf@circ@res@left=\pgf@x
        \pgf@circ@res@up=\pgf@y
        \pgfpointborderellipse{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@up}
        }{\pgfpoint{\ctikzvalof{nodes width}*\pgf@circ@Rlen}{\ctikzvalof{nodes width}*\pgf@circ@Rlen}}
    }
    \behindforegroundpath{

        \pgfscope
            \pgfpathcircle{\pgfpointorigin}{\ctikzvalof{nodes width}*\pgf@circ@Rlen}
            \pgfsetcolor{\ctikzvalof{color}}
            \pgfsetfillopacity{\ctikzvalof{poles/full fill opacity}}% normally 1.0
            \pgfusepath{draw,fill}
        \endpgfscope

    }
}

%% Empty round terminal

\pgfdeclareshape{ocirc}{
    \anchor{center}{
        \pgfpointorigin
    }
    \savedanchor\northwest{%
        \pgf@y=\ctikzvalof{nodes width}\pgf@circ@Rlen
        \pgf@x=-\pgf@y
    }
    \anchor{center}{ \pgf@y=0pt \pgf@x=0pt }
    \anchor{east}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{e}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{west}{ \northwest \pgf@y=0pt }
    \anchor{w}{ \northwest \pgf@y=0pt }
    \anchor{south}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{s}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{north}{ \northwest \pgf@x=0pt }
    \anchor{n}{ \northwest \pgf@x=0pt }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }
    \anchorborder{
        \pgf@circ@res@left=\pgf@x
        \pgf@circ@res@up=\pgf@y
        \pgfpointborderellipse{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@up}
        }{\pgfpoint{\ctikzvalof{nodes width}*\pgf@circ@Rlen}{\ctikzvalof{nodes width}*\pgf@circ@Rlen}}
    }

    \behindforegroundpath{

        \pgfscope
            \pgfpathcircle{\pgfpointorigin}{\ctikzvalof{nodes width}*\pgf@circ@Rlen}
            \pgfsetcolor{\ctikzvalof{color}}
            \ifx\tikz@fillcolor\pgfutil@empty
                % set the default fill color to white
                \pgfsetfillcolor{\ctikzvalof{open poles fill}}
                % ...but override it if the class is defined!
                % note that this element has no class, but will inherit it when used
                % into another component
                \pgf@circ@setifdefinedfill{draw, fill}{draw, fill}
            \else
                \pgfsetfillcolor{\tikz@fillcolor}
            \fi
            \pgfsetfillopacity{\ctikzvalof{poles/open fill opacity}}% normally 1.0
            \pgfusepath{draw,fill}
        \endpgfscope

    }
}

%% Diamond terminal

\pgfdeclareshape{diamondpole}{
    \anchor{center}{
        \pgfpointorigin
    }
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@y}{sqrt(2)*\ctikzvalof{nodes width}*\pgf@circ@Rlen}
        \pgf@x=-\pgf@y
    }
    \anchor{center}{ \pgf@y=0pt \pgf@x=0pt }
    \anchor{east}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{e}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{west}{ \northwest \pgf@y=0pt }
    \anchor{w}{ \northwest \pgf@y=0pt }
    \anchor{south}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{s}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{north}{ \northwest \pgf@x=0pt }
    \anchor{n}{ \northwest \pgf@x=0pt }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }
    \anchorborder{
        % \typeout{IN\space X:\the\pgf@x\space Y:\the\pgf@y}
        \pgfmathsetmacro{\@@switchx}{ifthenelse(\pgf@x>0,1,-1)}
        \pgfmathsetmacro{\@@switchy}{ifthenelse(\pgf@y>0,1,-1)}
        \pgfmathsetlength{\pgf@xa}{abs(\pgf@x)}
        \pgfmathsetlength{\pgf@ya}{abs(\pgf@y)}
        \pgfextracty{\pgf@circ@res@up}{\northwest}
        % \typeout{MID\space X:\the\pgf@xa\space Y:\the\pgf@ya\space L:\the\pgf@circ@res@up}
        % \typeout{MID\space SX:\@@switchx\space SY:\@@switchy}
        \pgfpointintersectionoflines
            {\pgfpointorigin}{\pgfqpoint{\pgf@xa}{\pgf@ya}}
            {\pgfqpoint{0pt}{\pgf@circ@res@up}}{\pgfqpoint{\pgf@circ@res@up}{0pt}}
        % \typeout{CROSS \space X:\the\pgf@x\space Y:\the\pgf@y}
        \pgf@x=\@@switchx\pgf@x
        \pgf@y=\@@switchy\pgf@y
    }
    \behindforegroundpath{
        \pgfscope
            \pgfmathsetlength{\pgf@circ@res@temp}{\ctikzvalof{nodes width}*\pgf@circ@Rlen}
            \pgftransformrotate{45}
            \pgfpathrectanglecorners
            {\pgfpoint{-\pgf@circ@res@temp}{-\pgf@circ@res@temp}}
            {\pgfpoint{\pgf@circ@res@temp}{\pgf@circ@res@temp}}
            \pgfsetcolor{\ctikzvalof{color}}
            \pgfsetfillopacity{\ctikzvalof{poles/full fill opacity}}% normally 1.0
            \pgfusepath{draw,fill}
        \endpgfscope
    }
}

%% Diamond terminal, unfilled

\pgfdeclareshape{odiamondpole}{
    \anchor{center}{
        \pgfpointorigin
    }
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@y}{sqrt(2)*\ctikzvalof{nodes width}*\pgf@circ@Rlen}
        \pgf@x=-\pgf@y
    }
    \anchor{center}{ \pgf@y=0pt \pgf@x=0pt }
    \anchor{east}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{e}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{west}{ \northwest \pgf@y=0pt }
    \anchor{w}{ \northwest \pgf@y=0pt }
    \anchor{south}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{s}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{north}{ \northwest \pgf@x=0pt }
    \anchor{n}{ \northwest \pgf@x=0pt }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }
    \anchorborder{
        % \typeout{IN\space X:\the\pgf@x\space Y:\the\pgf@y}
        \pgfmathsetmacro{\@@switchx}{ifthenelse(\pgf@x>0,1,-1)}
        \pgfmathsetmacro{\@@switchy}{ifthenelse(\pgf@y>0,1,-1)}
        \pgfmathsetlength{\pgf@xa}{abs(\pgf@x)}
        \pgfmathsetlength{\pgf@ya}{abs(\pgf@y)}
        \pgfextracty{\pgf@circ@res@up}{\northwest}
        % \typeout{MID\space X:\the\pgf@xa\space Y:\the\pgf@ya\space L:\the\pgf@circ@res@up}
        % \typeout{MID\space SX:\@@switchx\space SY:\@@switchy}
        \pgfpointintersectionoflines
            {\pgfpointorigin}{\pgfqpoint{\pgf@xa}{\pgf@ya}}
            {\pgfqpoint{0pt}{\pgf@circ@res@up}}{\pgfqpoint{\pgf@circ@res@up}{0pt}}
        % \typeout{CROSS \space X:\the\pgf@x\space Y:\the\pgf@y}
        \pgf@x=\@@switchx\pgf@x
        \pgf@y=\@@switchy\pgf@y
    }
    \behindforegroundpath{
        \pgfscope
            \pgfmathsetlength{\pgf@circ@res@temp}{\ctikzvalof{nodes width}*\pgf@circ@Rlen}
            \pgftransformrotate{45}
            \pgfpathrectanglecorners
            {\pgfpoint{-\pgf@circ@res@temp}{-\pgf@circ@res@temp}}
            {\pgfpoint{\pgf@circ@res@temp}{\pgf@circ@res@temp}}
            \pgfsetcolor{\ctikzvalof{color}}
            \ifx\tikz@fillcolor\pgfutil@empty
                % set the default fill color to white
                \pgfsetfillcolor{\ctikzvalof{open poles fill}}
                % ...but override it if the class is defined!
                % note that this element has no class, but will inherit it when used
                % into another component
                \pgf@circ@setifdefinedfill{draw, fill}{draw, fill}
            \else
                \pgfsetfillcolor{\tikz@fillcolor}
            \fi
            \pgfsetfillopacity{\ctikzvalof{poles/open fill opacity}}% normally 1.0
            \pgfusepath{draw,fill}
        \endpgfscope
    }
}

%% square terminal, filled

\pgfdeclareshape{squarepole}{
    \anchor{center}{
        \pgfpointorigin
    }
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@y}{\ctikzvalof{nodes width}*\pgf@circ@Rlen}
        \pgf@x=-\pgf@y
    }
    \anchor{center}{ \pgf@y=0pt \pgf@x=0pt }
    \anchor{east}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{e}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{west}{ \northwest \pgf@y=0pt }
    \anchor{w}{ \northwest \pgf@y=0pt }
    \anchor{south}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{s}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{north}{ \northwest \pgf@x=0pt }
    \anchor{n}{ \northwest \pgf@x=0pt }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }
    \anchorborder{
        \pgf@xa=\pgf@x
        \pgf@ya=\pgf@y
        \pgfextracty{\pgf@circ@res@up}{\northwest}
        \pgfpointborderrectangle
            {\pgfqpoint{\pgf@xa}{\pgf@ya}}
            {\pgfqpoint{\pgf@circ@res@up}{\pgf@circ@res@up}}
    }
    \behindforegroundpath{
        \pgfscope
            \pgfmathsetlength{\pgf@circ@res@temp}{\ctikzvalof{nodes width}*\pgf@circ@Rlen}
            \pgfpathrectanglecorners
            {\pgfpoint{-\pgf@circ@res@temp}{-\pgf@circ@res@temp}}
            {\pgfpoint{\pgf@circ@res@temp}{\pgf@circ@res@temp}}
            \pgfsetcolor{\ctikzvalof{color}}
            \pgfsetfillopacity{\ctikzvalof{poles/full fill opacity}}% normally 1.0
            \pgfusepath{draw,fill}
        \endpgfscope
    }
}
%% square terminal, unfilled

\pgfdeclareshape{osquarepole}{
    \anchor{center}{
        \pgfpointorigin
    }
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@y}{\ctikzvalof{nodes width}*\pgf@circ@Rlen}
        \pgf@x=-\pgf@y
    }
    \anchor{center}{ \pgf@y=0pt \pgf@x=0pt }
    \anchor{east}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{e}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{west}{ \northwest \pgf@y=0pt }
    \anchor{w}{ \northwest \pgf@y=0pt }
    \anchor{south}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{s}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{north}{ \northwest \pgf@x=0pt }
    \anchor{n}{ \northwest \pgf@x=0pt }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }
    \anchorborder{
        \pgf@xa=\pgf@x
        \pgf@ya=\pgf@y
        \pgfextracty{\pgf@circ@res@up}{\northwest}
        \pgfpointborderrectangle
            {\pgfqpoint{\pgf@xa}{\pgf@ya}}
            {\pgfqpoint{\pgf@circ@res@up}{\pgf@circ@res@up}}
    }
    \behindforegroundpath{
        \pgfscope
            \pgfmathsetlength{\pgf@circ@res@temp}{\ctikzvalof{nodes width}*\pgf@circ@Rlen}
            \pgfpathrectanglecorners
            {\pgfpoint{-\pgf@circ@res@temp}{-\pgf@circ@res@temp}}
            {\pgfpoint{\pgf@circ@res@temp}{\pgf@circ@res@temp}}
            \pgfsetcolor{\ctikzvalof{color}}
            \ifx\tikz@fillcolor\pgfutil@empty
                % set the default fill color to white
                \pgfsetfillcolor{\ctikzvalof{open poles fill}}
                % ...but override it if the class is defined!
                % note that this element has no class, but will inherit it when used
                % into another component
                \pgf@circ@setifdefinedfill{draw, fill}{draw, fill}
            \else
                \pgfsetfillcolor{\tikz@fillcolor}
            \fi
            \pgfsetfillopacity{\ctikzvalof{poles/open fill opacity}}% normally 1.0
            \pgfusepath{draw,fill}
        \endpgfscope
    }
}
% BNC connector

\pgfdeclareshape{bnc}{
    \anchor{center}{
        \pgfpointorigin
    }
    % BNC size is 2.5 times the size of the internal "ocirc"
    \savedanchor\northwest{%
        \pgf@y=\ctikzvalof{nodes width}\pgf@circ@Rlen
        \pgf@y=2.5\pgf@y
        \pgf@x=-\pgf@y
    }
    % center is on the opening
    \anchor{center}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{zero}{ \pgfpointorigin }
    \anchor{hot}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{shield}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    % geo-anchors
    \anchor{east}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{right}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{west}{ \northwest \pgf@y=0pt }
    \anchor{left}{ \northwest \pgf@y=0pt }
    \anchor{south}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{north}{ \northwest \pgf@x=0pt }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }
    % put the node text above and centered
    \anchor{text}{\pgfextracty{\pgf@circ@res@up}{\northwest}
        \pgfpoint{-.5\wd\pgfnodeparttextbox}{
            \dimexpr.5\dp\pgfnodeparttextbox+.5\ht\pgfnodeparttextbox+\pgf@circ@res@up\relax
        }
    }
    \anchorborder{
        \pgf@circ@res@left=\pgf@x
        \pgf@circ@res@up=\pgf@y
        \pgfpointborderellipse{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@up}
        }{\pgfpoint{2.5*\ctikzvalof{nodes width}*\pgf@circ@Rlen}{2.5*\ctikzvalof{nodes width}*\pgf@circ@Rlen}}
    }
    \behindforegroundpath{
        \pgfextracty{\pgf@circ@res@other}{\northwest}
        \pgf@circ@res@step=\ctikzvalof{nodes width}\pgf@circ@Rlen
        \pgfscope
            \pgfstartlinewidth=\pgflinewidth
            \pgf@circ@setlinewidth{bipoles}{\pgflinewidth}
            \pgfsetcolor{\ctikzvalof{color}}
            % external circle
            \pgfscope
                % clipping path: first a rectangle bigger then the shape
                % to avoid problems with the line thickness
                \pgfpathrectanglecorners{\pgfpoint{-2\pgf@circ@res@other}{-2\pgf@circ@res@other}}
                    {\pgfpoint{2\pgf@circ@res@other}{2\pgf@circ@res@other}}
                % next the opening to the right
                \pgfpathrectanglecorners{\pgfpoint{-\pgf@circ@res@step}{-\pgf@circ@res@step}}
                    {\pgfpoint{2\pgf@circ@res@other}{\pgf@circ@res@step}}
                % do the difference and clip before drawing
                \pgfseteorule
                \pgfusepath{clip}
                \pgfpathcircle{\pgfpointorigin}{\pgf@circ@res@other}
                \pgfusepath{draw}
            \endpgfscope
            % internal circle
            \pgfpathcircle{\pgfpointorigin}{\pgf@circ@res@step}
            \pgf@circ@draworfill
            % and the contact line to the right
            \pgfsetlinewidth{\pgfstartlinewidth}
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step}{0pt}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{0pt}}
            \pgfusepath{draw}
        \endpgfscope
    }
}

%% Fill for correct rectangular joins

\pgfdeclareshape{rectjoinfill}{
    \savedanchor{\northeast}{%
        \pgf@x=.5\pgflinewidth%
        \pgf@y=.5\pgflinewidth%
    }
    \anchor{north}{\northeast\pgf@x=0cm\relax}
    \anchor{east}{\northeast\pgf@y=0cm\relax}
    \anchor{south}{\northeast\pgf@y=-\pgf@y \pgf@x=0cm\relax}
    \anchor{west}{\northeast\pgf@y=0cm\pgf@x=-\pgf@x}
    \anchor{north east}{\northeast}
    \anchor{north west}{\northeast\pgf@x=-\pgf@x}
    \anchor{south east}{\northeast\pgf@y=-\pgf@y}
    \anchor{south west}{\northeast\pgf@y=-\pgf@y\pgf@x=-\pgf@x}
    \anchor{center}{
        \pgfpointorigin
    }
    \anchorborder{
        \pgf@circ@res@left=\pgf@x
        \pgf@circ@res@up=\pgf@y
    }
    \behindforegroundpath{
        \pgfscope
            \pgfpathrectanglecorners
            {\pgfpoint{0}{.5\pgflinewidth}}
            {\pgfpoint{0}{-.5\pgflinewidth}}
            \pgfsetcolor{\ctikzvalof{color}}
            \pgfusepath{draw,fill}
        \endpgfscope
    }
}

%% transistor arrow

\def\pgf@circ@find@linescale{
    % find the scale inverse of the scale factor: line width do not scale
    % with scale=..., transform shape so we have to counteract it.
    \iftikz@fullytransformed % this is true if `transform shape` is active
        % from @Circumscribe https://tex.stackexchange.com/a/474035/38080
        % Note that this trick is not working inside a `spy` environment...
        \pgfgettransformentries{\scaleA}{\scaleB}{\scaleC}{\scaleD}{\whatevs}{\whatevs}%
        \pgfmathsetmacro{\@@factor}{1.0/sqrt(abs(\scaleA*\scaleD-\scaleB*\scaleC))}%
    \else
        \pgfmathsetmacro{\@@factor}{1.0}
    \fi
}

\pgfdeclareshape{trarrow}{%
    % this arrow is only filled but grows with the linewidth, more or less
    % like currarrow do
    \savedanchor{\northeast}{%
        \pgf@circ@res@step = \pgf@circ@Rlen
        \pgf@circ@find@linescale
        \divide \pgf@circ@res@step by \ctikzvalof{current arrow scale}
        \pgfpoint{0.7*\pgf@circ@res@step +0.5*\@@factor*\pgflinewidth}
            {0.8*\pgf@circ@res@step+0.7593*\@@factor*\pgflinewidth}
    }
    % The arrow size should be more or less the same of a currarrow, which is
    % both filled and stroke, for backward output compatibility (more or less)
    %
    %      angle \beta       W is \pgf@circ@Rlen/\ctikzvalof{current arrow scale}
    %    |-\__               currarrow as the tip at (W,0)
    %    |    |              and the upper tail at (-0.7*W, 0.8*W)
    %    |    \__            it then "overshoot" do to the linew width L
    %    |       \__ xangle \alpha
    %    ---0------->
    %
    %   \beta = atan(0.7/0.8)  \alpha=atan(0.8/1.7)
    %   tip overshoot is (L/2)/sin(\alpha) = 1.743*L only in x direction
    %   tail overshoot is -(L/2) in x, and (L/2)/sin(\beta) = 0.7539*L in y
    %
    \savedanchor{\northwest}{%
        \pgf@circ@res@step = \pgf@circ@Rlen
        \divide \pgf@circ@res@step by \ctikzvalof{current arrow scale}
        \pgf@circ@find@linescale
        \pgfpoint{-0.7*\pgf@circ@res@step -0.5*\@@factor*\pgflinewidth}
            {0.8*\pgf@circ@res@step+0.7593*\@@factor*\pgflinewidth}
    }
    \savedanchor{\tip}{%
        \pgf@circ@res@step = \pgf@circ@Rlen
        \divide \pgf@circ@res@step by \ctikzvalof{current arrow scale}
        \pgf@circ@find@linescale
        \pgfpoint{\pgf@circ@res@step + 1.743*\@@factor*\pgflinewidth}{0pt}
    }
    \anchor{north}{\northeast\pgf@x=0cm\relax}
    \anchor{east}{\northeast\pgf@y=0cm\relax}
    \anchor{south}{\northeast\pgf@y=-\pgf@y \pgf@x=0cm\relax}
    \anchor{west}{\northeast\pgf@y=0cm\pgf@x=-\pgf@x}
    \anchor{north east}{\northeast}
    \anchor{north west}{\northeast\pgf@x=-\pgf@x}
    \anchor{south east}{\northeast\pgf@y=-\pgf@y}
    \anchor{south west}{\northeast\pgf@y=-\pgf@y\pgf@x=-\pgf@x}
    \anchor{center}{
        \pgfpointorigin
    }
    \anchor{tip}{
        \tip
    }
    \anchor{btip}{% this anchor is behind the tip of half a linewidth
        \tip
        \pgf@circ@find@linescale
        \pgf@circ@res@temp=\@@factor\pgflinewidth
        \advance\pgf@x by -.5\pgf@circ@res@temp
    }
    \behindforegroundpath{
        \pgfscope
            \northwest
            \pgf@circ@res@up=\pgf@y
            \pgf@circ@res@left=\pgf@x
            \tip
            \pgf@circ@res@step = \pgf@x
            %
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0pt}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{1\pgf@circ@res@step}{0pt}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{-\pgf@circ@res@up}}
            \pgfpathclose
            \pgfsetcolor{\ctikzvalof{color}}
            \pgfusepath{fill} % just fill
        \endpgfscope
    }
}

%% Current arrow

\pgfdeclareshape{currarrow}{
    \savedanchor{\northeast}{%
        \pgf@circ@res@step = \pgf@circ@Rlen
        \divide \pgf@circ@res@step by \ctikzvalof{current arrow scale}
        \pgf@x=.5\pgf@circ@res@step
        \pgf@y=\pgf@x%
    }
    \anchor{north}{\northeast\pgf@x=0cm\relax}
    \anchor{east}{\northeast\pgf@y=0cm\relax}
    \anchor{south}{\northeast\pgf@y=-\pgf@y \pgf@x=0cm\relax}
    \anchor{west}{\northeast\pgf@y=0cm\pgf@x=-\pgf@x}
    \anchor{north east}{\northeast}
    \anchor{north west}{\northeast\pgf@x=-\pgf@x}
    \anchor{south east}{\northeast\pgf@y=-\pgf@y}
    \anchor{south west}{\northeast\pgf@y=-\pgf@y\pgf@x=-\pgf@x}
    \anchor{center}{
        \pgfpointorigin
    }
    \anchor{tip}{
        \pgfpointorigin
        \pgf@circ@res@step = \pgf@circ@Rlen
        \divide \pgf@circ@res@step by \ctikzvalof{current arrow scale}
        \pgf@x	=\pgf@circ@res@step
    }
    \behindforegroundpath{
        \pgfscope
            \pgf@circ@res@step = \pgf@circ@Rlen
            \divide \pgf@circ@res@step by \ctikzvalof{current arrow scale}

            \pgfpathmoveto{\pgfpoint{-.7\pgf@circ@res@step}{0pt}}
            \pgfpathlineto{\pgfpoint{-.7\pgf@circ@res@step}{-.8\pgf@circ@res@step}}
            \pgfpathlineto{\pgfpoint{1\pgf@circ@res@step}{0pt}}
            \pgfpathlineto{\pgfpoint{-.7\pgf@circ@res@step}{.8\pgf@circ@res@step}}
            \pgfpathlineto{\pgfpoint{-.7\pgf@circ@res@step}{0pt}}
            \pgfsetcolor{\ctikzvalof{color}}
            \pgfusepath{draw,fill}

        \endpgfscope
    }
}

%% Flow arrow

\pgfdeclareshape{flowarrow}{
    \savedanchor{\northeast}{%
        \pgf@circ@res@step = \pgf@circ@Rlen
        \divide \pgf@circ@res@step by \ctikzvalof{current arrow scale}
        \pgf@y=.5\pgf@circ@res@step
        \pgf@circ@res@step = \pgf@circ@Rlen
        \divide \pgf@circ@res@step by 4
        \pgf@x=\pgf@circ@res@step%
    }
    \anchor{north}{\northeast\pgf@x=0cm\relax}
    \anchor{east}{\northeast\pgf@y=0cm\relax}
    \anchor{south}{\northeast\pgf@y=-\pgf@y \pgf@x=0cm\relax}
    \anchor{west}{\northeast\pgf@y=0cm\pgf@x=-\pgf@x}
    \anchor{north east}{\northeast}
    \anchor{north west}{\northeast\pgf@x=-\pgf@x}
    \anchor{south east}{\northeast\pgf@y=-\pgf@y}
    \anchor{south west}{\northeast\pgf@y=-\pgf@y\pgf@x=-\pgf@x}
    \anchor{text}{% text centered above
        \pgfpointorigin
        \pgfpoint{-.5\wd\pgfnodeparttextbox}{\dimexpr.5\dp\pgfnodeparttextbox+.5\ht\pgfnodeparttextbox}
    }
    \anchor{center}{
        \pgfpointorigin
    }
    \anchor{tip}{
        \pgfpointorigin
        \pgf@circ@res@step = \pgf@circ@Rlen
        \divide \pgf@circ@res@step by \ctikzvalof{current arrow scale}
        \pgf@x	=\pgf@circ@res@step
    }
    \behindforegroundpath{
        \pgfscope
            \pgf@circ@res@step = \pgf@circ@Rlen
            \divide \pgf@circ@res@step by 4
            \pgfpathmoveto{\pgfpoint{-\pgf@circ@res@step}{0pt}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@step}{0pt}}
            \pgfsetcolor{\ctikzvalof{color}}
            \pgfusepath{draw}
            \pgftransformshift{\pgfpoint{\pgf@circ@res@step}{0pt}}
            \pgfnode{currarrow}{tip}{}{}{\pgfusepath{fill}}
        \endpgfscope
    }
}

%% Input arrow

\pgfdeclareshape{inputarrow}{
    \savedanchor{\northeast}{% this is really not northeast, really -northwest
        \pgf@circ@res@step = \pgf@circ@Rlen
        \divide \pgf@circ@res@step by \ctikzvalof{current arrow scale}
        \pgf@y=.5\pgf@circ@res@step
        \pgf@x=1.7\pgf@circ@res@step
    }
    \anchor{north}{\northeast\pgf@x=0cm\relax}
    \anchor{east}{\northeast\pgf@y=0cm\relax\pgf@x=0cm\relax}
    \anchor{south}{\northeast\pgf@y=-\pgf@y \pgf@x=0cm\relax}
    \anchor{west}{\northeast\pgf@y=0cm\pgf@x=-\pgf@x}
    \anchor{north east}{\northeast\pgf@x=0cm\relax}
    \anchor{north west}{\northeast\pgf@x=-\pgf@x}
    \anchor{south east}{\northeast\pgf@y=-\pgf@y\pgf@x=0cm\relax}
    \anchor{south west}{\northeast\pgf@y=-\pgf@y\pgf@x=-\pgf@x}
    \savedanchor{\tip}{
        \pgfpointorigin
    }
    \anchor{center}{
        \tip
    }
    \anchor{tip}{
        \tip
    }
    \behindforegroundpath{

        \pgfscope
            \pgf@circ@res@step = \pgf@circ@Rlen
            \divide \pgf@circ@res@step by 16

            \pgfpathmoveto{\pgfpoint{-1.7\pgf@circ@res@step}{0pt}}
            \pgfpathlineto{\pgfpoint{-1.7\pgf@circ@res@step}{-.8\pgf@circ@res@step}}
            \pgfpathlineto{\pgfpoint{0pt}{0pt}}
            \pgfpathlineto{\pgfpoint{-1.7\pgf@circ@res@step}{.8\pgf@circ@res@step}}
            \pgfpathlineto{\pgfpoint{-1.7\pgf@circ@res@step}{0pt}}
            \pgfsetcolor{\ctikzvalof{color}}
            \pgfusepath{fill}

        \endpgfscope
    }
}


%% box

\pgfdeclareshape{box}{
    \anchor{center}{
        \pgfpointorigin
    }
    \behindforegroundpath{

        \pgfscope
            \pgf@circ@res@step = \ctikzvalof{bipoles/twoport/width}\pgf@circ@Rlen
            \pgf@circ@res@step = 0.5\pgf@circ@res@step
            \pgf@circ@setlinewidth{bipoles}{\pgfstartlinewidth}
            \pgfpathrectanglecorners{\pgfpoint{-\pgf@circ@res@step}{\pgf@circ@res@step}}{\pgfpoint{\pgf@circ@res@step}{-\pgf@circ@res@step}}
            \pgf@circ@draworfill
        \endpgfscope
    }
}

%% box scaled with blocks

\pgfdeclareshape{blockbox}{
    \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{blocks/scale}\pgf@circ@Rlen}}
    \anchor{center}{
        \pgfpointorigin
    }
    \behindforegroundpath{
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{blocks/scale}\pgf@circ@Rlen}
        \pgfscope
            \pgf@circ@res@step = \ctikzvalof{bipoles/twoport/width}\pgf@circ@scaled@Rlen
            \pgf@circ@res@step = 0.5\pgf@circ@res@step
            \pgf@circ@setlinewidth{bipoles}{\pgfstartlinewidth}
            \pgfpathrectanglecorners{\pgfpoint{-\pgf@circ@res@step}{\pgf@circ@res@step}}{\pgfpoint{\pgf@circ@res@step}{-\pgf@circ@res@step}}
            \pgf@circ@draworfill
        \endpgfscope
    }
}

% full nodes for wire crossing

\pgfdeclareshape{jump crossing}
{
    \savedanchor\northwest{%
        \pgf@y=\ctikzvalof{bipoles/crossing/size}\pgf@circ@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\pgf@y
    }
    \anchor{center}{ \pgf@y=0pt \pgf@x=0pt }
    \anchor{east}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{e}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{west}{ \northwest \pgf@y=0pt }
    \anchor{w}{ \northwest \pgf@y=0pt }
    \anchor{south}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{s}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{north}{ \northwest \pgf@x=0pt }
    \anchor{n}{ \northwest \pgf@x=0pt }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }
    \behindbackgroundpath{
        \northwest
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@down = -\pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        % horizontal jumper
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0pt}}
        \pgfpathlineto{\pgfpoint{0.4\pgf@circ@res@left}{0pt}}
        \pgfpatharc{0}{-180}{0.4*\pgf@circ@res@left}
        \pgfsetbeveljoin
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        % vertical, broken path
        \pgfpathmoveto{\pgfpoint{0pt}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{0pt}{0.5\pgf@circ@res@up}}
        \pgfpathmoveto{\pgfpoint{0pt}{0.3\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}
        \pgfusepath{draw}

    }
}
\pgfdeclareshape{plain crossing}
{
    \savedanchor\northwest{%
        \pgf@y=\ctikzvalof{bipoles/crossing/size}\pgf@circ@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\pgf@y
    }
    \anchor{center}{ \pgf@y=0pt \pgf@x=0pt }
    \anchor{east}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{e}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{west}{ \northwest \pgf@y=0pt }
    \anchor{w}{ \northwest \pgf@y=0pt }
    \anchor{south}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{s}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{north}{ \northwest \pgf@x=0pt }
    \anchor{n}{ \northwest \pgf@x=0pt }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }
    \behindbackgroundpath{
        \northwest
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@down = -\pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        % horizontal jumper
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0pt}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        % vertical, broken path
        \pgfpathmoveto{\pgfpoint{0pt}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{0pt}{0.1\pgf@circ@res@up}}
        \pgfpathmoveto{\pgfpoint{0pt}{0.1\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}
        \pgfusepath{draw}

    }
}

\endinput
