% Copyright 2007-2009 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.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Quadripoles

\long\def\pgfcircdeclarequadpole#1#2#3{
    \pgfdeclareshape{#1}
    {
        \anchor{center}{
            \northwest
            \pgf@x=0pt
            \pgf@y=0pt
        }
        \savedmacro{\stretto}{\def\stretto{\ctikzvalof{quadpoles/#1/inner}}}
        \savedanchor\northwest{%
            \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/quadpoles/#1/height}\pgf@circ@Rlen
            \pgf@y=.5\pgf@y
            \pgf@x=.5\pgf@circ@Rlen
            \pgf@x=-\pgfkeysvalueof{/tikz/circuitikz/quadpoles/#1/width}\pgf@x
        }
        \anchor{A2}{
            \northwest
            \pgf@y=-\pgf@y
        }
        \anchor{B1}{
            \northwest
            \pgf@x=-\pgf@x
        }
        \anchor{A1}{
            \northwest
        }
        \anchor{B2}{
            \northwest
            \pgf@x=-\pgf@x
            \pgf@y=-\pgf@y
        }
        %% notice for the dot anchors: I use the cute inductors as reference
        %% size; if you change one you have to change all of them.
        \anchor{inner dot A1}{
            \northwest
            \pgfmathsetlength\pgf@x{\stretto*\pgf@x +
            \ctikzvalof{bipoles/cuteinductor/height}*\pgf@circ@Rlen/2}
            \pgfmathsetlength\pgf@y{0.5*\pgf@circ@Rlen}
        }
        \anchor{outer dot A1}{
            \northwest
            \pgfmathsetlength\pgf@x{\stretto*\pgf@x -
            \ctikzvalof{bipoles/cuteinductor/height}*\pgf@circ@Rlen/2}
            \pgfmathsetlength\pgf@y{0.5*\pgf@circ@Rlen}
        }
        \anchor{inner dot A2}{
            \northwest
            \pgfmathsetlength\pgf@x{\stretto*\pgf@x +
            \ctikzvalof{bipoles/cuteinductor/height}*\pgf@circ@Rlen/2}
            \pgfmathsetlength\pgf@y{-0.5*\pgf@circ@Rlen}
        }
        \anchor{outer dot A2}{
            \northwest
            \pgfmathsetlength\pgf@x{\stretto*\pgf@x -
            \ctikzvalof{bipoles/cuteinductor/height}*\pgf@circ@Rlen/2}
            \pgfmathsetlength\pgf@y{-0.5*\pgf@circ@Rlen}
        }
        \anchor{inner dot B1}{
            \northwest
            \pgfmathsetlength\pgf@x{-\stretto*\pgf@x -
            \ctikzvalof{bipoles/cuteinductor/height}*\pgf@circ@Rlen/2}
            \pgfmathsetlength\pgf@y{0.5*\pgf@circ@Rlen}
        }
        \anchor{outer dot B1}{
            \northwest
            \pgfmathsetlength\pgf@x{-\stretto*\pgf@x +
            \ctikzvalof{bipoles/cuteinductor/height}*\pgf@circ@Rlen/2}
            \pgfmathsetlength\pgf@y{0.5*\pgf@circ@Rlen}
        }
        \anchor{inner dot B2}{
            \northwest
            \pgfmathsetlength\pgf@x{-\stretto*\pgf@x -
            \ctikzvalof{bipoles/cuteinductor/height}*\pgf@circ@Rlen/2}
            \pgfmathsetlength\pgf@y{-0.5*\pgf@circ@Rlen}
        }
        \anchor{outer dot B2}{
            \northwest
            \pgfmathsetlength\pgf@x{-\stretto*\pgf@x +
            \ctikzvalof{bipoles/cuteinductor/height}*\pgf@circ@Rlen/2}
            \pgfmathsetlength\pgf@y{-0.5*\pgf@circ@Rlen}
        }
        \anchor{north}{
            \northwest
            \pgf@x=0pt
        }
        \anchor{south}{
            \northwest
            \pgf@x=0pt
            \pgf@y=-\pgf@y
        }
        \anchor{west}{
            \northwest
            \pgf@y=0pt
        }
        \anchor{east}{
            \northwest
            \pgf@y=0pt
            \pgf@x=-\pgf@x
        }
        \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
        }
        \anchor{base}{
            \northwest
            \pgf@x=0pt
        }
        #3
        \backgroundpath{
            \pgfsetcolor{\pgfkeysvalueof{/tikz/circuitikz/color}}

            \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
            #2
        }
    }
}


\def\pgf@circ@drawtransformerbasicanchor{
    \pgfkeysvalueof{/tikz/circuitikz/quadpoles/trans/height}
    \anchor{AA2}{
        \northwest
        \pgf@x=\pgfkeysvalueof{/tikz/circuitikz/quadpoles/transformer/width1}\pgf@x
        \pgf@x=.7\pgf@x
        \pgf@y=-\pgf@y
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/quadpoles/transformer/height1}\pgf@y
    }
    \anchor{BB1}{
        \northwest
        \pgf@x=-\pgf@x
        \pgf@x=\pgfkeysvalueof{/tikz/circuitikz/quadpoles/transformer/width1}\pgf@x
        \pgf@x=.7\pgf@x
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/quadpoles/transformer/height1}\pgf@y
    }
    \anchor{AA1}{
        \northwest
        \pgf@x=\pgfkeysvalueof{/tikz/circuitikz/quadpoles/transformer/width1}\pgf@x
        \pgf@x=.7\pgf@x
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/quadpoles/transformer/height1}\pgf@y
    }
    \anchor{BB2}{
        \northwest
        \pgf@x=-\pgf@x
        \pgf@x=\pgfkeysvalueof{/tikz/circuitikz/quadpoles/transformer/width1}\pgf@x
        \pgf@x=.7\pgf@x
        \pgf@y=-\pgf@y
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/quadpoles/transformer/height1}\pgf@y
    }
}

\def\pgf@circ@drawtransformerbasicbody{
    \pgfscope
        \pgfslopedattimetrue
        \pgfallowupsidedownattimetrue
        \pgftransformlineattime{.5}{%
            \pgfpoint%
            {\stretto\pgf@circ@res@left}%
            {\pgf@circ@res@up}%
            }{%
            \pgfpoint
            {\stretto\pgf@circ@res@left}%
            {\pgf@circ@res@down}%
        }

        \edef\pgf@circ@temp{\ctikzvalof{inductor}}%
        \def\pgf@temp{european}%
        \ifx\pgf@temp\pgf@circ@temp%
            \pgfnode{fullgenericshape}{center}{}{pgf@inductor1}{\pgfusepath{stroke}}
        \else%
            \def\pgf@temp{cute}
            \ifx\pgf@temp\pgf@circ@temp%
                \pgfnode{cuteinductorshape}{center}{}{pgf@inductor1}{\pgfusepath{stroke}}
            \else%
                \pgfnode{americaninductorshape}{center}{}{pgf@inductor1}{\pgfusepath{stroke}}
            \fi%
        \fi%


    \endpgfscope
    \pgfscope
        \pgfslopedattimetrue
        \pgfallowupsidedownattimetrue
        \pgftransformlineattime{.5}{%
            \pgfpoint%
            {\stretto\pgf@circ@res@right}%
            {\pgf@circ@res@down}%
            }{%
            \pgfpoint
            {\stretto\pgf@circ@res@right}%
            {\pgf@circ@res@up}%
        }

        \edef\pgf@circ@temp{\ctikzvalof{inductor}}%
        \def\pgf@temp{european}%
        \ifx\pgf@temp\pgf@circ@temp%
            \pgfnode{fullgenericshape}{center}{}{pgf@inductor2}{\pgfusepath{stroke}}
        \else%
            \def\pgf@temp{cute}
            \ifx\pgf@temp\pgf@circ@temp%
                \pgfnode{cuteinductorshape}{center}{}{pgf@inductor2}{\pgfusepath{stroke}}
            \else%
                \pgfnode{americaninductorshape}{center}{}{pgf@inductor2}{\pgfusepath{stroke}}
            \fi%
        \fi%

    \endpgfscope

    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{\stretto\pgf@circ@res@left}{\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpointanchor{pgf@inductor1}{b}}

    \pgfpathmoveto{\pgfpointanchor{pgf@inductor1}{a}}
    \pgfpathlineto{\pgfpoint{\stretto\pgf@circ@res@left}{\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@down}}

    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{\stretto\pgf@circ@res@right}{\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpointanchor{pgf@inductor2}{a}}

    \pgfpathmoveto{\pgfpointanchor{pgf@inductor2}{b}}
    \pgfpathlineto{\pgfpoint{\stretto\pgf@circ@res@right}{\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down}}

    \pgfusepath{draw}

}


\pgfcircdeclarequadpole{transformer}{
    \pgf@circ@drawtransformerbasicbody
}{\pgf@circ@drawtransformerbasicanchor}

\pgfcircdeclarequadpole{transformer core}{

    \pgf@circ@drawtransformerbasicbody

    \pgfmoveto{\pgfpoint%
        {\ctikzvalof{quadpoles/transformer core/core width}\pgf@circ@res@right}%
        {\ctikzvalof{quadpoles/transformer core/core height}\pgf@circ@res@down}%
    }
    \pgflineto{
        \pgfpoint
        {\ctikzvalof{quadpoles/transformer core/core width}\pgf@circ@res@right}%
        {\ctikzvalof{quadpoles/transformer core/core height}\pgf@circ@res@up}%
    }

    \pgfmoveto{\pgfpoint%
        {\ctikzvalof{quadpoles/transformer core/core width}\pgf@circ@res@left}%
        {\ctikzvalof{quadpoles/transformer core/core height}\pgf@circ@res@down}%
    }
    \pgflineto{
        \pgfpoint
        {\ctikzvalof{quadpoles/transformer core/core width}\pgf@circ@res@left}%
        {\ctikzvalof{quadpoles/transformer core/core height}\pgf@circ@res@up}%
    }

    \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/bipoles/thickness}\pgflinewidth}
    \pgfusepath{draw}
}{\pgf@circ@drawtransformerbasicanchor}


\pgfcircdeclarequadpole{gyrator}{

    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{\stretto\pgf@circ@res@left}{\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{\stretto\pgf@circ@res@left}{\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@down}}

    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{\stretto\pgf@circ@res@right}{\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{\stretto\pgf@circ@res@right}{\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down}}

    \pgfusepath{draw}

    \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/quadpoles/thickness}\pgflinewidth}
    \pgfpathmoveto{\pgfpoint{\stretto\pgf@circ@res@left}{.7*\stretto\pgf@circ@res@down}}
    \pgfpatharc{90}{270}{.7*\stretto\pgf@circ@res@down}

    \pgfpathmoveto{\pgfpoint{\stretto\pgf@circ@res@right}{.7*\stretto\pgf@circ@res@up}}
    \pgfpatharc{-90}{90}{.7*\stretto\pgf@circ@res@down}
    \pgfusepath{draw}

}{}

%% four-port
% defines a general outer box for four-ports
% TikZ usage:
%		\draw (0,0) node[coupler](coup){\SI{-3}{dB}}
%		(coup.port1) to[short,-o] ++(-1,0)
%

\long\def\pgfcircdeclarefourport#1#2{
    \pgfdeclareshape{#1}{
        \anchor{center}{
            \northwest
            \pgf@x=0pt
            \pgf@y=0pt
        }
        \savedanchor\northwest{%
            \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/quadpoles/#1/height}\pgf@circ@Rlen
            \pgf@y=.5\pgf@y
            \pgf@x=.5\pgf@circ@Rlen
            \pgf@x=-\pgfkeysvalueof{/tikz/circuitikz/quadpoles/#1/width}\pgf@x
        }
        \anchor{north}{
            \northwest
            \pgf@x=0pt
        }
        \anchor{south}{
            \northwest
            \pgf@x=0pt
            \pgf@y=-\pgf@y
        }
        \anchor{west}{
            \northwest
            \pgf@y=0pt
        }
        \anchor{east}{
            \northwest
            \pgf@y=0pt
            \pgf@x=-\pgf@x
        }
        \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
        }
        \anchor{port1}{
            \northwest
            \pgf@y=-0.5\pgf@y
        }
        \anchor{port2}{
            \northwest
            \pgf@x=-\pgf@x
            \pgf@y=-0.5\pgf@y
        }
        \anchor{port3}{
            \northwest
            \pgf@x=-\pgf@x
            \pgf@y=0.5\pgf@y
        }
        \anchor{port4}{
            \northwest
            \pgf@y=0.5\pgf@y
        }
        \anchor{1}{
            \northwest
            \pgf@y=-0.5\pgf@y
        }
        \anchor{2}{
            \northwest
            \pgf@x=-\pgf@x
            \pgf@y=-0.5\pgf@y
        }
        \anchor{3}{
            \northwest
            \pgf@x=-\pgf@x
            \pgf@y=0.5\pgf@y
        }
        \anchor{4}{
            \northwest
            \pgf@y=0.5\pgf@y
        }

        \anchor{text}{
            \northwest
            \advance \pgf@y by 0.5\ht\pgfnodeparttextbox
            \pgf@x=-.5\wd\pgfnodeparttextbox
        }
        \backgroundpath{
            \pgfsetcolor{\pgfkeysvalueof{/tikz/circuitikz/color}}

            \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

            \pgfstartlinewidth=\pgflinewidth

            % draw outer box
            \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/bipoles/thickness}\pgfstartlinewidth}
            \pgfpathrectanglecorners{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@up}}{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down}}
            \pgfusepath{draw}

            % draw inner stuff
            #2

            % draw inner text
            \pgftext[center,x=-0.15\pgf@circ@res@step,y=0]{\ctikzvalof{bipoles/twoport/text}}
        }
    }
}

% four-port
\pgfcircdeclarefourport{fourport}{}


% straight coupler
\pgfcircdeclarefourport{coupler}{
    \pgfsetlinewidth{\pgfstartlinewidth}
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0.5\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{0.5\pgf@circ@res@up}}
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0.5\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{0.5\pgf@circ@res@down}}
    \pgfusepath{draw}

    \pgfpathmoveto{\pgfpoint{0.7\pgf@circ@res@left}{0.4\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{0.7\pgf@circ@res@right}{0.4\pgf@circ@res@up}}
    \pgfsetarrows{latex-latex}
    \pgfusepath{draw}
    \pgfpathmoveto{\pgfpoint{0.7\pgf@circ@res@left}{0.4\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{0.7\pgf@circ@res@right}{0.4\pgf@circ@res@down}}
    \pgfsetarrows{latex-latex}
    \pgfusepath{draw}
}

% "bended" coupler
\pgfcircdeclarefourport{coupler2}{
    \pgfsetlinewidth{\pgfstartlinewidth}
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0.5\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{0.5\pgf@circ@res@up}}
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0.5\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{0.5\pgf@circ@res@down}}
    \pgfusepath{draw}

    \pgfscope
        \pgfpathmoveto{\pgfpoint{0.1\pgf@circ@res@left}{0pt}}
        \pgfpatharc{0}{90} {0.4\pgf@circ@res@up}
        \pgfsetarrowsend{latex}
        \pgfusepath{draw}
    \endpgfscope
    \pgfscope
        \pgfpathmoveto{\pgfpoint{0.1\pgf@circ@res@left}{0pt}}
        \pgfpatharc{0}{-90} {0.4\pgf@circ@res@up}
        \pgfsetarrowsend{latex}
        \pgfusepath{draw}
    \endpgfscope
    \pgfscope
        \pgfpathmoveto{\pgfpoint{0.1\pgf@circ@res@right}{0pt}}
        \pgfpatharc{180}{90} {0.4\pgf@circ@res@up}
        \pgfsetarrowsend{latex}
        \pgfusepath{draw}
    \endpgfscope
    \pgfscope
        \pgfpathmoveto{\pgfpoint{0.1\pgf@circ@res@right}{0pt}}
        \pgfpatharc{-180}{-90} {0.4\pgf@circ@res@up}
        \pgfsetarrowsend{latex}
        \pgfusepath{draw}
    \endpgfscope
}

% contrib Kristofer M. Monisit

\pgfdeclareshape{fd op amp}
{
    \anchor{center}{\pgfpointorigin}
    \savedanchor\northwest{%
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/height}\pgf@circ@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/width}\pgf@circ@Rlen
        \pgf@x=.5\pgf@x
    }
    \savedanchor\outline{%
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/height}\pgf@circ@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/width}\pgf@circ@Rlen
        \pgf@x=.5\pgf@x
    \ifpgf@circuit@oa@oplusup\else\pgf@y=-\pgf@y\fi
    }
    \anchor{south}{
        \northwest
        \pgf@y=-\pgf@y\pgf@x=0pt\relax
    }
    \anchor{north}{
        \northwest\pgf@x=0pt\relax
    }
    \savedanchor\left{%
        \pgf@y=0pt
    }
    \savedanchor\inOneFixed{%
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/height}\pgf@circ@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/input height}\pgf@y
        \pgf@x=-\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/width}\pgf@circ@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{in up}{
        \inOneFixed
    }
    \anchor{in down}{
        \inOneFixed
        \pgf@y=-\pgf@y
    }
    \savedanchor\inOne{%
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/height}\pgf@circ@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/input height}\pgf@y
        \pgf@x=-\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/width}\pgf@circ@Rlen
        \pgf@x=.5\pgf@x
    \ifpgf@circuit@oa@iplusup\pgf@y=-\pgf@y\fi
    }
    \anchor{-}{
        \inOne
    }
    \anchor{+}{
        \inOne
        \pgf@y=-\pgf@y
    }
    \savedanchor\up{%
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/height}\pgf@circ@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/width}\pgf@circ@Rlen
        \pgf@x=.5\pgf@x
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgfpointlineattime{
            \pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/up pos}}{
            \pgfpoint{
            \pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/port width}\pgf@circ@res@left}
        {\pgf@circ@res@up}}
        {\pgfpoint{.7\pgf@circ@res@right}{0pt}}
    }
    \anchor{up}{
        \up
    }
    \anchor{down}{
        \up
        \pgf@y=-\pgf@y
    }
    \anchor{out up}{
        \northwest
        \pgf@y=.5\pgf@y
        \pgf@x=-.7\pgf@x
    }
    \anchor{out down}{
        \northwest
        \pgf@y=-.5\pgf@y
        \pgf@x=-.7\pgf@x
    }
    \anchor{out +}{
        \outline
        \pgf@y=.5\pgf@y
        \pgf@x=-.7\pgf@x
    }
    \anchor{out -}{
        \outline
        \pgf@y=-.5\pgf@y
        \pgf@x=-.7\pgf@x
    }
    \anchor{west}{
        \left
    }
    \anchor{east}{
        \left
        \pgf@x=-.7\pgf@x
    }

    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-.7\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-.7\pgf@x \pgf@y=-\pgf@y }
    \backgroundpath{
        \pgfsetcolor{\pgfkeysvalueof{/tikz/circuitikz/color}}

        \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

        % Triangle
        \pgfscope
            \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/quadpoles/thickness}\pgflinewidth}
            \pgftransformxshift{.7\pgf@circ@res@left}
            \pgf@circ@res@step=\pgf@circ@res@right
            \advance\pgf@circ@res@step by -\pgf@circ@res@left
            \pgf@circ@res@step=.7\pgf@circ@res@step
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step}{0pt}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@step}{0pt}}
            \pgfpathclose
            \pgf@circ@draworfill
        \endpgfscope
        % Negative input terminal
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
            {\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/input height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/port width}\pgf@circ@res@left}
            {\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/input height}\pgf@circ@res@up}}
        \pgftext[left, at=\pgfpoint{\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/port width}\pgf@circ@res@left}{\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/input height}\pgf@circ@res@up}]{\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/font} \ifpgf@circuit@oa@iplusup$+$\else$-$\fi}

        % Positive input terminal
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
            {\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/input height}\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint
            {\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/port width}\pgf@circ@res@left}
            {\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/input height}\pgf@circ@res@down}}
        \pgftext[left, at=\pgfpoint{\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/port width}\pgf@circ@res@left}{\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/input height}\pgf@circ@res@down}]{\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/font} \ifpgf@circuit@oa@iplusup$-$\else$+$\fi}

        % Negative output terminal
        \pgfpathmoveto{\pgfpoint
            {0.7\pgf@circ@res@right}
            {\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/input height}\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint
            {\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/port width}}
            {\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/input height}\pgf@circ@res@down}}
        \pgftext[left, at=\pgfpoint{0.3\pgf@circ@res@left}{.3\pgf@circ@res@down}]{\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/font} \ifpgf@circuit@oa@oplusup$-$\else$+$\fi}

        % Positive output terminal
        \pgfpathmoveto{\pgfpoint
            {0.7\pgf@circ@res@right}
            {\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/input height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/port width}}
            {\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/input height}\pgf@circ@res@up}}
        \pgftext[left, at=\pgfpoint{0.3\pgf@circ@res@left}{.3\pgf@circ@res@up}]{\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/font} \ifpgf@circuit@oa@oplusup$+$\else$-$\fi}

        \pgfsetrectcap
        % Draw them all!
        \pgfusepath{draw}

    }
}


%% instrumentation amplifier diff output

\pgfdeclareshape{fd inst amp}
{
    % when tikz calls the anchor it wants the relative position in the lengths
    % \pgf@x  \pgf@y
    % \pgfpoint* functions set that variables
    % anchors are visible outside and run on use
    \anchor{center}{\pgfpointorigin}
    % savedanchors are internals and run on node creation (not use)
    % bounding-box top left
    \savedanchor\northwest{%
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/height}\pgf@circ@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/width}\pgf@circ@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{nw}{
        \northwest
    }
    \anchor{south}{
        \northwest
        \pgf@y=-\pgf@y\pgf@x=0pt\relax
    }
    \anchor{north}{
        \northwest\pgf@x=0pt\relax
    }
    \savedanchor\left{%
        \pgf@y=0pt
    }
    \anchor{leftedge}
    {\left
        \pgf@x = \pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/port width}\pgf@x
    }
    \savedanchor\inOneFixed{%
          \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/height}\pgf@circ@Rlen
          \pgf@y=.5\pgf@y
          \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/input height}\pgf@y
          \pgf@x=-\pgfkeysvalueof{/tikz/circuitikz/tripoles/op amp/width}\pgf@circ@Rlen
          \pgf@x=.5\pgf@x
    }
    \anchor{in up}{
        \inOneFixed
    }
    \anchor{in down}{
        \inOneFixed
        \pgf@y=-\pgf@y
    }
    \savedanchor\inOne{%
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/height}\pgf@circ@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/input height}\pgf@y
        \pgf@x=-\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/width}\pgf@circ@Rlen
        \pgf@x=.5\pgf@x
        \ifpgf@circuit@oa@iplusup\pgf@y=-\pgf@y\fi
    }
    \anchor{-}{
        \inOne
    }
    \anchor{+}{
        \inOne
        \pgf@y=-\pgf@y
    }
    \savedanchor\up{%
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/height}\pgf@circ@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/width}\pgf@circ@Rlen
        \pgf@x=.5\pgf@x
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgfpointlineattime{
            \pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/up pos}}{
            \pgfpoint{
            \pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/port width}\pgf@circ@res@left}
        {\pgf@circ@res@up}}
        {\pgfpoint{.7\pgf@circ@res@right}{.6\pgf@circ@res@up}}
    }
    \anchor{up}{
        \up
    }
    \anchor{down}{
        \up
        \pgf@y=-\pgf@y
    }
    % reference voltage input anchors.
    \savedanchor\refv{%
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/height}\pgf@circ@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/width}\pgf@circ@Rlen
        \pgf@x=.5\pgf@x
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgfpointlineattime{
            \pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/refv pos}}{
            \pgfpoint{
            \pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/port width}\pgf@circ@res@left}
        {\pgf@circ@res@up}}
        {\pgfpoint{.7\pgf@circ@res@right}{.6\pgf@circ@res@up}}
    }
    % we need both because they are normally drawn under the amp, and if you
    % mirror it vertically you need them
    \anchor{refv up}{
        \refv
    }
    \anchor{refv down}{
        \refv
        \pgf@y=-\pgf@y
    }
    \savedanchor\outport{
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/height}\pgf@circ@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/input height}\pgf@y
        \pgf@x=-\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/width}\pgf@circ@Rlen
        \pgf@x=-.5\pgf@x
        \ifpgf@circuit@oa@oplusup\else\pgf@y=-\pgf@y\fi
    }
    \anchor{out}{
        \outport
        \pgf@y=0pt
    }
    \anchor{out +}{
        \outport
    }
    \anchor{out -}{
        \outport
        \pgf@y=-\pgf@y
    }
    \savedanchor\outportfixed{
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/height}\pgf@circ@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/input height}\pgf@y
        \pgf@x=-\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/width}\pgf@circ@Rlen
        \pgf@x=-.5\pgf@x
    }
    \anchor{out up}{
        \outportfixed
    }
    \anchor{out down}{
        \outportfixed
        \pgf@y=-\pgf@y
    }
    %
    \anchor{west}{
        \left
    }
    \anchor{east}{
        \left
        \pgf@x=-\pgf@x
    }
    \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 }
    \anchor{text}{\pgfpoint{-.5\wd\pgfnodeparttextbox}{\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox}}

    % let's start drawing the component
    \backgroundpath{
        \pgfsetcolor{\pgfkeysvalueof{/tikz/circuitikz/color}}
        %
        \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
        % main component, normally in thicker lines
        \pgfscope
            \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/tripoles/thickness}\pgflinewidth}
            \pgftransformxshift{.7\pgf@circ@res@left}
            \pgf@circ@res@step=\pgf@circ@res@right
            \advance\pgf@circ@res@step by -\pgf@circ@res@left
            \pgf@circ@res@step=.7\pgf@circ@res@step
            %first point (near output)
            \pgfpathmoveto{\pgfpoint{1.4\pgf@circ@res@right}{0}}
            %from the exit to the top (short side)... (note that the .6 must be copied in \up and \refv anchors
            \pgfpathlineto{\pgfpoint{1.4\pgf@circ@res@right}{.6\pgf@circ@res@up}}
            % and then to the input "front up", "down", to the output short side "down"
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpoint{1.4\pgf@circ@res@right}{.6\pgf@circ@res@down}}
            % ...and close
            \pgfpathclose
            \pgf@circ@draworfill
        \endpgfscope
        % input terminal up
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
        {\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/input height}\pgf@circ@res@up}}
        %
        \pgfpathlineto{\pgfpoint
            {\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/port width}\pgf@circ@res@left}
        {\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/input height}\pgf@circ@res@up}}
        %
        \pgftext[left, at=\pgfpoint{\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/port width}\pgf@circ@res@left}{\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/input height}\pgf@circ@res@up}]{\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/font} \ifpgf@circuit@oa@iplusup$+$\else$-$\fi}

        % input terminal down
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
        {\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/input height}\pgf@circ@res@down}}
        %
        \pgfpathlineto{\pgfpoint
            {\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/port width}\pgf@circ@res@left}
        {\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/input height}\pgf@circ@res@down}}
        \pgftext[left, at=\pgfpoint{\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/port width}\pgf@circ@res@left}{\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/input height}\pgf@circ@res@down}]{\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/font} \ifpgf@circuit@oa@iplusup$-$\else$+$\fi}
        % output leads down and up
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/input height}\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{.7\pgf@circ@res@right}{\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/input height}\pgf@circ@res@down}} %
        \pgftext[right, at=\pgfpoint{\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/port width}\pgf@circ@res@right}{\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/input height}\pgf@circ@res@down}]{\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/font}\ifpgf@circuit@oa@oplusup$-\;$\else$+\;$\fi}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/input height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{.7\pgf@circ@res@right}{\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/input height}\pgf@circ@res@up}} %
        \pgftext[right, at=\pgfpoint{\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/port width}\pgf@circ@res@right}{\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/input height}\pgf@circ@res@up}]{\pgfkeysvalueof{/tikz/circuitikz/tripoles/fd inst amp/font}\ifpgf@circuit@oa@oplusup$+\;$\else$-\;$\fi}
        %
        \pgfsetrectcap
        \pgfusepath{draw}
    }
}
