% Copyright 2018-2020 by Romano Giannetti
% Copyright 2015-2020 by Stefan Lindner
% Copyright 2013-2020 by Stefan Erhardt
% Copyright 2007-2020 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.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  Voltage management

%% styles
\ctikzset{bipole voltage style/.style={}}
\tikzset{bipole voltage style/.code={
        \ctikzset{bipole voltage style/.style={#1}}
}}
\tikzset{bipole voltage append style/.code={
        \ctikzset{bipole voltage style/.append style={#1}}
}}

\ctikzset{v^>/.style = {
        v = #1,
        \circuitikzbasekey/bipole/voltage/direction = forward,
        \circuitikzbasekey/bipole/voltage/position = above
    }
}

\ctikzset{v^</.style = {
        v = #1,
        \circuitikzbasekey/bipole/voltage/direction = backward,
        \circuitikzbasekey/bipole/voltage/position = above
    }
}

\ctikzset{v_>/.style = {
        v = #1,
        \circuitikzbasekey/bipole/voltage/direction = forward,
        \circuitikzbasekey/bipole/voltage/position = below
    }
}

\ctikzset{v_</.style = {
        v = #1,
        \circuitikzbasekey/bipole/voltage/direction = backward,
        \circuitikzbasekey/bipole/voltage/position = below
    }
}

\ctikzset{v_/.style = {v = #1, \circuitikzbasekey/bipole/voltage/position = below} }
\ctikzset{v^/.style = {v = #1, \circuitikzbasekey/bipole/voltage/position  = above} }
\ctikzset{v>/.style = {v = #1, \circuitikzbasekey/bipole/voltage/direction = forward} }
\ctikzset{v</.style = {v = #1, \circuitikzbasekey/bipole/voltage/direction = backward} }

% Default position varies whether the component is a voltage source
% or not
\ctikzset{v/.code = {
        \ifpgf@circuit@bipole@isvoltage
            \pgfkeys{\circuitikzbasekey/bipole/voltage/position=above,
            \circuitikzbasekey/bipole/voltage/direction=forward}
        \else
            \ifpgf@circ@oldvoltagedirection
                \pgfkeys{\circuitikzbasekey/bipole/voltage/position=below,
                \circuitikzbasekey/bipole/voltage/direction=backward}
            \else
                \pgfkeys{\circuitikzbasekey/bipole/voltage/position=below,
                \circuitikzbasekey/bipole/voltage/direction=forward}
            \fi
        \fi
        \ifpgf@circ@oldvoltagedirection
            \ifpgf@circuit@bipole@iscurrent\ifpgf@circ@fixbatteries
                \pgfkeys{\circuitikzbasekey/bipole/voltage/position=below,
                \circuitikzbasekey/bipole/voltage/direction=forward}
        \fi\fi
        \else
        \ifpgf@circuit@bipole@iscurrent
            \ifpgf@circuit@bipole@current@backward
                \pgfkeys{\circuitikzbasekey/bipole/voltage/position=below,
                \circuitikzbasekey/bipole/voltage/direction=forward}
            \else
                \pgfkeys{\circuitikzbasekey/bipole/voltage/position=below,
                \circuitikzbasekey/bipole/voltage/direction=backward}
            \fi\fi\fi
            \pgfkeys{/tikz/circuitikz/bipole/voltage/label/name=#1}
            \ctikzsetvalof{bipole/voltage/label/unit}{}
            \ifpgf@circ@siunitx
                \pgf@circ@handleSI{#1}
                \ifpgf@circ@siunitx@res
                    \edef\pgf@temp{\pgf@circ@handleSI@val}
                    \pgfkeyslet{/tikz/circuitikz/bipole/voltage/label/name}{\pgf@temp}
                    \edef\pgf@temp{\pgf@circ@handleSI@unit}
                    \pgfkeyslet{/tikz/circuitikz/bipole/voltage/label/unit}{\pgf@temp}
                \else
            \fi
            \else
        \fi
    }
}

% american voltage font selection and symbol definition
% the default font command is {} --- nothing
\def\pgf@circ@avfont{\ctikzvalof{voltage/american font}}
%
% plus and minus symbols (default is $+$ and $-$, see pgfcirc.defines.tex)
%
\def\pgf@circ@avplus{\ctikzvalof{voltage/american plus}}
\def\pgf@circ@avminus{\ctikzvalof{voltage/american minus}}

%%
\def\setscaledRlenforclass{%
    \csname pgf@sh@ma@\ctikzvalof{bipole/name}\endcsname
    \ifdefined\ctikzclass
        \edef\pgf@temp{/tikz/circuitikz/\ctikzclass/scale}
        \pgfkeysifdefined{\pgf@temp}
            {\pgf@circ@scaled@Rlen=\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            {\pgf@circ@scaled@Rlen=\pgf@circ@Rlen}
    \else
        \pgf@circ@scaled@Rlen=\pgf@circ@Rlen
    \fi
}

%% Output routine for generic bipoles

\def\pgf@circ@drawvoltagegeneric{
    \pgfextra{
        % \typeout{NAME:\ctikzvalof{bipole/name}}
        \edef\pgf@temp{/tikz/circuitikz/bipoles/\ctikzvalof{bipole/kind}/voltage/straight label distance}
        \pgfkeysifdefined{\pgf@temp}
        {
            \edef\partheight{\ctikzvalof{bipoles/\ctikzvalof{bipole/kind}/voltage/straight label distance}}
            \edef\tmpdistfromline{(\partheight\pgf@circ@scaled@Rlen)}
        }
        {
            \pgfkeysifdefined{/tikz/circuitikz/bipoles/voltage/straight label distance}
            {
                \edef\partheight{\ctikzvalof{bipoles/voltage/straight label distance}}
                \edef\tmpdistfromline{(\partheight\pgf@circ@scaled@Rlen)}
            }
            {%calculate default value from part height
                \edef\pgf@temp{/tikz/circuitikz/bipoles/\ctikzvalof{bipole/kind}/height}
                \pgfkeysifdefined{\pgf@temp}
                {
                    \edef\partheight{0.5*\ctikzvalof{bipoles/\ctikzvalof{bipole/kind}/height}}
                    \edef\tmpdistfromline{(\partheight\pgf@circ@scaled@Rlen+0.2\pgf@circ@scaled@Rlen)}
                }
                {
                    \edef\tmpdistfromline{(.5\pgf@circ@scaled@Rlen)} %fallback to fixed value
                }
            }
        }
        % \typeout{KIND:\ctikzvalof{bipole/kind}\space RLEN:\the\pgf@circ@Rlen\space SCALED:\the\pgf@circ@scaled@Rlen}
        \ifnum \ctikzvalof{mirror value}=-1
        \ifpgf@circuit@bipole@inverted
            \ifpgf@circuit@bipole@voltage@straight
                \def\distfromline{\tmpdistfromline}
            \else
                \def\distfromline{\ctikzvalof{voltage/distance from line}\pgf@circ@scaled@Rlen}
            \fi
            \else
            \ifpgf@circuit@bipole@voltage@straight
                \def\distfromline{-\tmpdistfromline}
            \else
                \def\distfromline{-\ctikzvalof{voltage/distance from line}\pgf@circ@scaled@Rlen}
            \fi
        \fi
        \else
            \ifpgf@circuit@bipole@inverted
                \ifpgf@circuit@bipole@voltage@straight
                    \def\distfromline{-\tmpdistfromline}
                \else
                    \def\distfromline{-\ctikzvalof{voltage/distance from line}\pgf@circ@scaled@Rlen}
                \fi
                \else
                \ifpgf@circuit@bipole@voltage@straight
                    \def\distfromline{\tmpdistfromline}
                \else
                    \def\distfromline{\ctikzvalof{voltage/distance from line}\pgf@circ@scaled@Rlen}
                \fi
            \fi
        \fi
        \ifpgf@circuit@bipole@voltage@below
            \def\pgf@circ@voltage@angle{90}
        \else
            \def\pgf@circ@voltage@angle{-90}
        \fi
        \edef\pgf@temp{/tikz/circuitikz/bipoles/\ctikzvalof{bipole/kind}/voltage/distance from node}
        \pgfkeysifdefined{\pgf@temp}
            { \edef\distancefromnode{\ctikzvalof{bipoles/\ctikzvalof{bipole/kind}/voltage/distance from node}} }
            { \edef\distancefromnode{\ctikzvalof{voltage/distance from node}} }
        \edef\pgf@temp{/tikz/circuitikz/bipoles/\ctikzvalof{bipole/kind}/voltage/bump b}
        \pgfkeysifdefined{\pgf@temp}
            { \edef\bumpb{\ctikzvalof{bipoles/\ctikzvalof{bipole/kind}/voltage/bump b}} }
            { \edef\bumpb{\ctikzvalof{voltage/bump b}} }
        \edef\shiftv{\ctikzvalof{voltage/shift}}
        % additional per-bipole voltage shift (internal)
        \edef\pgf@temp{/tikz/circuitikz/bipoles/\ctikzvalof{bipole/kind}/voltage/additional label shift}
        \pgfkeysifdefined{\pgf@temp}
        {
            \edef\addvshift{\ctikzvalof{bipoles/\ctikzvalof{bipole/kind}/voltage/additional label shift}}
        }
        {
            \edef\addvshift{0}
        }
        \newdimen{\absvshift}
        \pgfmathsetlength{\absvshift}{(1+\shiftv+\addvshift)*(\distfromline)}
        % put this to true to see the voltage label coordinate anchors
        \newif\ifpgf@circ@debugv\pgf@circ@debugvfalse
    }
    % %\pgf@circ@Rlen/\ctikzvalof{current arrow scale} is equal to the length of the currarrow
    coordinate (pgfcirc@midtmp) at ($(\tikztostart) ! \pgf@circ@Rlen/\ctikzvalof{current arrow scale} ! (anchorstartnode)$) %absolute move, minimum space is length of arrowhead
    coordinate (pgfcirc@midtmp) at ($(pgfcirc@midtmp) ! \distancefromnode ! (anchorstartnode)$)
    coordinate (pgfcirc@Vfrom@flat) at (pgfcirc@midtmp)
    coordinate (pgfcirc@Vfrom) at ($(pgfcirc@midtmp) ! -\distfromline ! \pgf@circ@voltage@angle:(anchorstartnode)$)

    coordinate (pgfcirc@midtmp) at ($(\tikztotarget) ! \pgf@circ@Rlen/\ctikzvalof{current arrow scale} ! (anchorendnode)$)%absolute move, minimum space is length of arrowhead
    coordinate (pgfcirc@midtmp) at ($(pgfcirc@midtmp) ! \distancefromnode ! (anchorendnode)$)
    coordinate (pgfcirc@Vto@flat) at (pgfcirc@midtmp)
    coordinate (pgfcirc@Vto) at ($(pgfcirc@midtmp) ! \distfromline ! \pgf@circ@voltage@angle : (anchorendnode)$)

    \ifpgf@circuit@bipole@voltage@below
        \ifpgf@circ@debugv
            node [ocirc, fill=red] at (anchorstartnode) {}
            node [ocirc, fill=blue] at (anchorendnode) {}
            node [ocirc, fill=green] at (pgfcirc@Vto) {}
            node [ocirc, fill=yellow] at (pgfcirc@Vfrom) {}
            node [odiamondpole, fill=green!50!black] at (pgfcirc@Vto@flat) {}
            node [odiamondpole, fill=orange] at (pgfcirc@Vfrom@flat) {}
        \fi
        coordinate (pgfcirc@Vto) at ($(pgfcirc@Vto@flat) ! \absvshift!90 :  (anchorendnode)$)
        coordinate (pgfcirc@Vfrom) at ($(pgfcirc@Vfrom@flat) ! \absvshift!-90 :  (anchorstartnode)$)
        coordinate (pgfcirc@Vcont1t) at ($(\ctikzvalof{bipole/name}.center) ! \bumpb ! (\ctikzvalof{bipole/name}.-110)$)
        coordinate (pgfcirc@Vcont2t) at ($(\ctikzvalof{bipole/name}.center) ! \bumpb ! (\ctikzvalof{bipole/name}.-70)$)
        coordinate (pgfcirc@Vcont1) at ($(pgfcirc@Vcont1t) ! -\absvshift!90 : (pgfcirc@Vcont2t)$)
        coordinate (pgfcirc@Vcont2) at ($(pgfcirc@Vcont2t) ! -\absvshift!-90 : (pgfcirc@Vcont1t)$)
        \ifpgf@circ@debugv
            node [odiamondpole, fill=green] at (pgfcirc@Vto) {}
            node [odiamondpole, fill=yellow] at (pgfcirc@Vfrom) {}
            node [osquarepole, fill=red] at (pgfcirc@Vcont1) {}
            node [osquarepole, fill=blue] at (pgfcirc@Vcont2) {}
        \fi
    \else
        \ifpgf@circ@debugv
            node [ocirc, fill=red] at (anchorstartnode) {}
            node [ocirc, fill=blue] at (anchorendnode) {}
            node [ocirc, fill=green] at (pgfcirc@Vto) {}
            node [ocirc, fill=yellow] at (pgfcirc@Vfrom) {}
            node [odiamondpole, fill=green] at (pgfcirc@Vto@flat) {}
            node [odiamondpole, fill=yellow] at (pgfcirc@Vfrom@flat) {}
        \fi
        coordinate (pgfcirc@Vto) at ($(pgfcirc@Vto@flat) ! -\absvshift!90 :  (anchorendnode)$)
        coordinate (pgfcirc@Vfrom) at ($(pgfcirc@Vfrom@flat) ! -\absvshift!-90 :  (anchorstartnode)$)
        coordinate (pgfcirc@Vcont1t) at ($(\ctikzvalof{bipole/name}.center) ! \bumpb ! (\ctikzvalof{bipole/name}.110)$)
        coordinate (pgfcirc@Vcont2t) at ($(\ctikzvalof{bipole/name}.center) ! \bumpb ! (\ctikzvalof{bipole/name}.70)$)
        coordinate (pgfcirc@Vcont1) at ($(pgfcirc@Vcont1t) ! \absvshift!90 : (pgfcirc@Vcont2t)$)
        coordinate (pgfcirc@Vcont2) at ($(pgfcirc@Vcont2t) ! \absvshift!-90 : (pgfcirc@Vcont1t)$)
        \ifpgf@circ@debugv
            node [odiamondpole, fill=green] at (pgfcirc@Vto) {}
            node [odiamondpole, fill=yellow] at (pgfcirc@Vfrom) {}
            node [osquarepole, fill=red] at (pgfcirc@Vcont1) {}
            node [osquarepole, fill=blue] at (pgfcirc@Vcont2) {}
        \fi
    \fi

    \ifpgf@circuit@europeanvoltage
        \ifpgf@circuit@bipole@voltage@straight
            \ifpgf@circuit@bipole@voltage@backward
                (pgfcirc@Vto) --(pgfcirc@Vfrom) node[currarrow, sloped,  allow upside down, pos=1,anchor=tip] {}
            \else
                (pgfcirc@Vfrom) --(pgfcirc@Vto) node[currarrow, sloped,  allow upside down, pos=1,anchor=tip] {}
            \fi
        \else
            \ifpgf@circuit@bipole@voltage@backward
                (pgfcirc@Vto) .. controls (pgfcirc@Vcont2)  and (pgfcirc@Vcont1) ..
                node[currarrow, sloped,  allow upside down, pos=1, anchor=tip] {}
                (pgfcirc@Vfrom)
            \else
                (pgfcirc@Vfrom) .. controls (pgfcirc@Vcont1)  and (pgfcirc@Vcont2) ..
                node[currarrow, sloped,  allow upside down, pos=1, anchor=tip] {}
                (pgfcirc@Vto)
            \fi
        \fi
    \else % american
        \ifx\@@kind\@@open % open circuit; put + and - directly on it
            \ifpgf@circuit@bipole@voltage@backward
                \ifpgf@circ@oldvoltagedirection
                    (pgfcirc@Vfrom@flat) node[inner sep=0, node font=\pgf@circ@avfont,
                        anchor=center]{\pgf@circ@avplus}
                    (pgfcirc@Vto@flat) node[inner sep=0, node font=\pgf@circ@avfont,
                        anchor=center]{\pgf@circ@avminus}
                \else
                    (pgfcirc@Vfrom@flat) node[inner sep=0, node font=\pgf@circ@avfont,
                        anchor=center]{\pgf@circ@avminus}
                    (pgfcirc@Vto@flat) node[inner sep=0, node font=\pgf@circ@avfont,
                        anchor=center]{\pgf@circ@avplus}
                \fi
                \else
                \ifpgf@circ@oldvoltagedirection
                    (pgfcirc@Vfrom@flat) node[inner sep=0, node font=\pgf@circ@avfont,
                        anchor=center]{\pgf@circ@avminus}
                    (pgfcirc@Vto@flat) node[inner sep=0, node font=\pgf@circ@avfont,
                        anchor=center]{\pgf@circ@avplus}
                \else
                    (pgfcirc@Vfrom@flat) node[inner sep=0, node font=\pgf@circ@avfont,
                        anchor=center]{\pgf@circ@avplus}
                    (pgfcirc@Vto@flat) node[inner sep=0, node font=\pgf@circ@avfont,
                        anchor=center]{\pgf@circ@avminus}
                \fi
            \fi
        \else % not open circuit
            \ifpgf@circuit@bipole@voltage@backward
                \ifpgf@circ@oldvoltagedirection
                    (pgfcirc@Vfrom) node[inner sep=0, node font=\pgf@circ@avfont,
                        anchor=\pgf@circ@bipole@voltage@label@anchor]{\pgf@circ@avplus}
                    (pgfcirc@Vto) node[inner sep=0, node font=\pgf@circ@avfont,
                        anchor=\pgf@circ@bipole@voltage@label@anchor]{\pgf@circ@avminus}
                \else
                    (pgfcirc@Vfrom) node[inner sep=0, node font=\pgf@circ@avfont,
                        anchor=\pgf@circ@bipole@voltage@label@anchor]{\pgf@circ@avminus}
                    (pgfcirc@Vto) node[inner sep=0, node font=\pgf@circ@avfont,
                        anchor=\pgf@circ@bipole@voltage@label@anchor]{\pgf@circ@avplus}
                \fi
                \else
                \ifpgf@circ@oldvoltagedirection
                    (pgfcirc@Vfrom) node[inner sep=0, node font=\pgf@circ@avfont,
                        anchor=\pgf@circ@bipole@voltage@label@anchor]{\pgf@circ@avminus}
                    (pgfcirc@Vto) node[inner sep=0, node font=\pgf@circ@avfont,
                        anchor=\pgf@circ@bipole@voltage@label@anchor]{\pgf@circ@avplus}
                \else
                    (pgfcirc@Vfrom) node[inner sep=0, node font=\pgf@circ@avfont,
                        anchor=\pgf@circ@bipole@voltage@label@anchor]{\pgf@circ@avplus}
                    (pgfcirc@Vto) node[inner sep=0, node font=\pgf@circ@avfont,
                        anchor=\pgf@circ@bipole@voltage@label@anchor]{\pgf@circ@avminus}
                \fi
            \fi
        \fi
    \fi
}

%% Output routine for voltage sources
\def\pgf@circ@drawvoltagegenerator{
    % the following is affected indirectly by voltage/shift, you can move the arrow with voltage/bump a.
    % it's not perfect, but I can't find the way to do it correctly...
    \pgfextra{
        \edef\shiftv{\ctikzvalof{voltage/shift}}
        \edef\bumpa{\ctikzvalof{voltage/bump a}}
        \pgfmathsetmacro{\bumpaplus}{\bumpa + 0.5*\shiftv} % coefficient added "by feel". Sorry.
    }
    \ifpgf@circuit@bipole@voltage@below
        coordinate (pgfcirc@Vfrom) at ($(\ctikzvalof{bipole/name}.center) ! \bumpaplus ! (\ctikzvalof{bipole/name}.-120)$)
        coordinate (pgfcirc@Vto) at ($(\ctikzvalof{bipole/name}.center) ! \bumpaplus ! (\ctikzvalof{bipole/name}.-60)$)
    \else
        coordinate (pgfcirc@Vfrom) at ($ (\ctikzvalof{bipole/name}.center) ! \bumpaplus ! (\ctikzvalof{bipole/name}.120)$)
        coordinate (pgfcirc@Vto) at ($ (\ctikzvalof{bipole/name}.center) ! \bumpaplus ! (\ctikzvalof{bipole/name}.60)$)
    \fi
    % fix the (unused in this case) Vcont1/2 coords for label placement along the line
    coordinate (pgfcirc@Vcont1) at (pgfcirc@Vto)
    coordinate (pgfcirc@Vcont2) at (pgfcirc@Vfrom)
    \ifpgf@circuit@europeanvoltage
        \ifpgf@circuit@bipole@voltage@backward
            (pgfcirc@Vto)  -- node[currarrow, sloped,  allow upside down, pos=1, anchor=tip] {} (pgfcirc@Vfrom)
        \else
            (pgfcirc@Vfrom)  -- node[currarrow, sloped,  allow upside down, pos=1, anchor=tip] {} (pgfcirc@Vto)
        \fi
        \else% american voltage
        \ifpgf@circuit@bipole@voltageoutsideofsymbol
            % if it is a battery, must put + and -

            \ifpgf@circ@fixbatteries
                \ifpgf@circuit@bipole@voltage@backward
                    (pgfcirc@Vfrom)  node[node font=\pgf@circ@avfont] {\pgf@circ@avplus}
                    (pgfcirc@Vto) node[node font=\pgf@circ@avfont] {\pgf@circ@avminus}
                \else
                    (pgfcirc@Vfrom)  node[node font=\pgf@circ@avfont] {\pgf@circ@avminus}
                    (pgfcirc@Vto) node[node font=\pgf@circ@avfont] {\pgf@circ@avplus}
                \fi
                \else
                \ifpgf@circuit@bipole@voltage@backward
                    (pgfcirc@Vfrom)  node[node font=\pgf@circ@avfont] {\pgf@circ@avminus}
                    (pgfcirc@Vto) node[node font=\pgf@circ@avfont] {\pgf@circ@avplus}
                \else
                    (pgfcirc@Vfrom)  node[node font=\pgf@circ@avfont] {\pgf@circ@avplus}
                    (pgfcirc@Vto) node[node font=\pgf@circ@avfont] {\pgf@circ@avminus}
                \fi
            \fi
        \fi
    \fi
}

%% Output routine
%% this is the entry point
%%
\def\pgf@circ@drawvoltage{% node name
    \pgfextra{ %WARNING: indentation is probably wrong
        \edef\pgfcircmathresult{\expandafter\pgf@circ@stripdecimals\pgf@circ@direction\pgf@nil}
        \ifnum\pgfcircmathresult >4 \ifnum\pgfcircmathresult <86
        \ifpgf@circuit@bipole@voltage@below
            \def\pgf@circ@bipole@voltage@label@anchor{north west}
        \else
            \def\pgf@circ@bipole@voltage@label@anchor{south east}
        \fi
        \fi\fi
        \ifnum\pgfcircmathresult >85 \ifnum\pgfcircmathresult <95
        \ifpgf@circuit@bipole@voltage@below
            \def\pgf@circ@bipole@voltage@label@anchor{west}
        \else
            \def\pgf@circ@bipole@voltage@label@anchor{east}
        \fi
        \fi\fi
        \ifnum\pgfcircmathresult >94 \ifnum\pgfcircmathresult <176
        \ifpgf@circuit@bipole@voltage@below
            \def\pgf@circ@bipole@voltage@label@anchor{south west}
        \else \def\pgf@circ@bipole@voltage@label@anchor{north east}
        \fi
        \fi\fi
        \ifnum\pgfcircmathresult >175 \ifnum\pgfcircmathresult <185
        \ifpgf@circuit@bipole@voltage@below
            \def\pgf@circ@bipole@voltage@label@anchor{south}
        \else\def\pgf@circ@bipole@voltage@label@anchor{north}
        \fi
        \fi\fi
        \ifnum\pgfcircmathresult >184 \ifnum\pgfcircmathresult <266
        \ifpgf@circuit@bipole@voltage@below
            \def\pgf@circ@bipole@voltage@label@anchor{south east}
        \else\def\pgf@circ@bipole@voltage@label@anchor{north west}
        \fi
        \fi\fi
        \ifnum\pgfcircmathresult >265 \ifnum\pgfcircmathresult <275
        \ifpgf@circuit@bipole@voltage@below
            \def\pgf@circ@bipole@voltage@label@anchor{east}
        \else \def\pgf@circ@bipole@voltage@label@anchor{west}
        \fi
        \fi\fi
        \ifnum\pgfcircmathresult >274 \ifnum\pgfcircmathresult <356
        \ifpgf@circuit@bipole@voltage@below
            \def\pgf@circ@bipole@voltage@label@anchor{north east}
        \else\def\pgf@circ@bipole@voltage@label@anchor{south west}
        \fi
        \fi\fi
        \ifnum\pgfcircmathresult >-1 \ifnum\pgfcircmathresult <5
        \ifpgf@circuit@bipole@voltage@below
            \def\pgf@circ@bipole@voltage@label@anchor{north}
        \else\def\pgf@circ@bipole@voltage@label@anchor{south}
        \fi
        \fi\fi
        \ifnum\pgfcircmathresult >355 \ifnum\pgfcircmathresult <361
        \ifpgf@circuit@bipole@voltage@below
            \def\pgf@circ@bipole@voltage@label@anchor{north}
        \else\def\pgf@circ@bipole@voltage@label@anchor{south}
        \fi
        \fi\fi

        % this must be set *before* changing for mirroring and inverting; in that case
        % the xscale/yscale parameters take it into account
        \ifpgf@circuit@bipole@voltage@below
            \def\pgf@circ@bipole@voltage@label@where{-90}
        \else
            \def\pgf@circ@bipole@voltage@label@where{90}
        \fi

        % magic to counteract the scale and yscale effects (there should be a better way...)
        \ifnum \ctikzvalof{mirror value}=-1
            \ifpgf@circuit@bipole@voltage@below
                \pgf@circuit@bipole@voltage@belowfalse
            \else
                \pgf@circuit@bipole@voltage@belowtrue
            \fi
        \fi

        \ifpgf@circuit@bipole@inverted
            \ifpgf@circuit@bipole@voltage@below
                \pgf@circuit@bipole@voltage@belowfalse
            \else
                \pgf@circuit@bipole@voltage@belowtrue
            \fi
        \fi

        % take into account scaling
        \setscaledRlenforclass

        \edef\pgf@temp{/tikz/circuitikz/bipoles/\ctikzvalof{bipole/kind}/voltage/european label distance}
        \pgfkeysifdefined{\pgf@temp}
            { \edef\eudist{\ctikzvalof{bipoles/\ctikzvalof{bipole/kind}/voltage/european label distance}} }
            { \edef\eudist{\ctikzvalof{voltage/european label distance}} }
        % find the height of the bipole or use a default value
        \edef\pgf@temp{/tikz/circuitikz/bipoles/\ctikzvalof{bipole/kind}/height}
        \pgfkeysifdefined{\pgf@temp}
            {\pgfmathsetmacro{\partheightf}{0.5*\ctikzvalof{bipoles/\ctikzvalof{bipole/kind}/height}}
             \edef\partheight{\partheightf\pgf@circ@scaled@Rlen}}
            {\edef\partheight{(.5\pgf@circ@scaled@Rlen)}} %fallback to fixed value
        \newdimen{\alshift}
        % this is more or less the same of the legacy code; we shift the american label a bit
        % outside the (+) -- (-) line
        \pgfmathsetlength{\alshift}{(\ctikzvalof{voltage/american label distance}-0.6)*\partheight}
        \pgfsetcornersarced{\pgfpointorigin}% do not use rounded corners!
        % set the macro for detecting open
        \edef\@@kind{\ctikzvalof{bipole/kind}}\edef\@@open{open}
        \ifpgf@adjust@open@voltage\else\edef\@@open{this-will-nEver-match}\fi
        % \typeout{KIND\space\@@kind}
    }%end pgfextra

    \ifpgf@circuit@bipole@isvoltage
        \pgf@circ@drawvoltagegenerator
    \else
        \pgf@circ@drawvoltagegeneric
    \fi

    % \pgfextra{\typeout{LABEL\space KIND:\@@kind\space OPEN:\@@open}}

    \ifpgf@circuit@bipole@voltage@straight
        coordinate (Vlab) at ($(pgfcirc@Vto)!0.5!(pgfcirc@Vfrom) $)
        node [anchor=\pgf@circ@bipole@voltage@label@anchor, inner sep=2pt,
        \circuitikzbasekey/bipole voltage style](\ctikzvalof{bipole/name}voltage)
        at (Vlab) {\pgf@circ@finallabels{voltage/label}}
    \else
        \ifpgf@circuit@europeanvoltage
            coordinate (Vlab) at ($(pgfcirc@Vcont1)!0.5!(pgfcirc@Vcont2)$)
        \else
            coordinate (Vlab) at ($(pgfcirc@Vfrom)!0.5!(pgfcirc@Vto)$)
            \ifpgf@circuit@bipole@isvoltage\else
            % add a bit of space for american labels above their symbols in the normal case. You can avoid that
            % with voltage/american label distance=0.5 (it's measured from the center of the component, in heights)
                coordinate (Vlab) at ($(Vlab) ! \alshift ! \pgf@circ@bipole@voltage@label@where :(pgfcirc@Vto)$)
            \fi
        \fi

        \ifpgf@circuit@europeanvoltage
            node [anchor=\pgf@circ@bipole@voltage@label@anchor, inner sep=2pt,
            \circuitikzbasekey/bipole voltage style](\ctikzvalof{bipole/name}voltage)
            at (Vlab) {\pgf@circ@finallabels{voltage/label}}
        \else % american voltages
            \ifx\@@kind\@@open
                coordinate (Vlab) at ($(pgfcirc@Vfrom@flat)!0.5!(pgfcirc@Vto@flat)$)
                node [anchor=center, inner sep=2pt,
                \circuitikzbasekey/bipole voltage style](\ctikzvalof{bipole/name}voltage)
                at (Vlab) {\pgf@circ@finallabels{voltage/label}}
            \else
                node [anchor=\pgf@circ@bipole@voltage@label@anchor, inner sep=2pt,
                \circuitikzbasekey/bipole voltage style](\ctikzvalof{bipole/name}voltage)
                at (Vlab) {\pgf@circ@finallabels{voltage/label}}
            \fi
        \fi
    \fi
}%end drawvoltages
\endinput
