% Copyright 2018 by Mark Wibrow
%
% 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 file doc/generic/pgf/licenses/LICENSE for more details.

\usepgflibrary{shapes.gates.logic}%

\pgfkeys{/pgf/.cd,
  and gate IEC symbol/.initial=$\char`\&$,
  nand gate IEC symbol/.initial=$\char`\&$,
  or gate IEC symbol/.initial=$\geq1$,
  nor gate IEC symbol/.initial=$\geq1$,
  xor gate IEC symbol/.initial={$=1$},
  xnor gate IEC symbol/.initial={$=1$},
  not gate IEC symbol/.initial=1,
  buffer gate IEC symbol/.initial=1,
  logic gate IEC symbol align/.initial=top,
  logic gate IEC symbol color/.initial=% no value
}%

\pgfkeys{/pgf/logic gate IEC symbol align/.cd,
  top/.code=\pgftransformyshift{\innerhalfheight},
  bottom/.code=\pgftransformyshift{-\innerhalfheight},
  left/.code=\pgftransformxshift{-\innerhalfwidth},
  right/.code=\pgftransformxshift{\innerhalfwidth},
}%

\def\pgf@lib@sh@logicgates@IEC@foregroundpath#1{%
  \dimensions%
  {%
    \pgftransformshift{\centerpoint}%
    \edef\pgf@lib@sh@temp{\pgfkeysvalueof{/pgf/logic gate IEC symbol align}}%
    \edef\pgf@lib@sh@@temp{/pgf/logic gate IEC symbol align/.cd, \pgf@lib@sh@temp}%
    \expandafter\pgfkeys\expandafter{\pgf@lib@sh@@temp}%
    \expandafter\pgftext\expandafter[\pgf@lib@sh@temp]{%
      \pgfkeysgetvalue{/pgf/logic gate IEC symbol color}{\pgf@lib@sh@temp}%
      \ifx\pgf@lib@sh@temp\pgfutil@empty%
      \else%
        \expandafter\color\expandafter{\pgf@lib@sh@temp}%
      \fi%
      \pgfkeysvalueof{/pgf/#1 gate IEC symbol}%
    }%
  }%
}%


\def\pgf@lib@sh@logicgates@dimensions@IEC#1{%
  \pgf@x.5\wd\pgfnodeparttextbox%
  \pgf@y.5\ht\pgfnodeparttextbox%
  \advance\pgf@y.5\dp\pgfnodeparttextbox%
  %
  % Adjust for the width of the gate symbol.
  %
  \setbox\pgf@hbox\hbox{%
    % wrap in `\pgfinterruptpicture' to restore font, like how `\pgfnode` sets
    % `\pgfnodeparttextbox`
    \pgfinterruptpicture
      {\pgfkeysvalueof{/pgf/#1 gate IEC symbol}}% add braces as color-aftergroup-fix TT
    \endpgfinterruptpicture
  }%
  \pgf@xa.5\wd\pgf@hbox%
  \pgf@ya.5\ht\pgf@hbox%
  \advance\pgf@ya.5\dp\pgf@hbox%
  \ifdim\pgf@x<\pgf@xa%
    \pgf@x\pgf@xa%
  \fi%
  \ifdim\pgf@y<\pgf@ya%
    \pgf@y\pgf@ya%
  \fi%
  \pgfmathaddtolength\pgf@x{\pgfkeysvalueof{/pgf/inner xsep}}%
  \pgfmathaddtolength\pgf@y{\pgfkeysvalueof{/pgf/inner ysep}}%
  %
  % Adjust for minimum height and width.
  %
  \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/minimum width}}%
  \pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/minimum height}}%
  \ifdim\pgf@x<.5\pgf@xa%
    \pgf@x.5\pgf@xa%
  \fi%
  \ifdim\pgf@y<.5\pgf@ya%
    \pgf@y.5\pgf@ya%
  \fi%
  %
  % Adjust the height for the number of inputs.
  %
  \c@pgf@counta\pgf@lib@sh@logicgate@numinputs%
  \advance\c@pgf@counta1\relax%
  \pgfmathsetlength\pgf@yb{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
  \pgf@yb2.0\pgf@yb%
  \advance\pgf@yb\pgflinewidth%
  \pgfmathsetlength\pgf@yc{\pgfkeysvalueof{/pgf/logic gate input sep}}%
  \edef\inputsep{\the\pgf@yc}%
  \addtosavedmacro\inputsep%
  \ifdim\pgf@yb<\pgf@yc%
    \pgf@yb\pgf@yc%
  \fi%
  \multiply\pgf@yb\c@pgf@counta%
  \ifdim\pgf@y<.5\pgf@yb%
    \pgf@y.5\pgf@yb%
  \fi%
  %
  \pgf@xa\pgf@x%
  \pgfmathaddtolength\pgf@xa{-\pgfkeysvalueof{/pgf/inner xsep}}%
  \edef\innerhalfwidth{\the\pgf@xa}%
  \addtosavedmacro\innerhalfwidth%
  %
  \pgf@ya\pgf@y%
  \pgfmathaddtolength\pgf@ya{-\pgfkeysvalueof{/pgf/inner ysep}}%
  \edef\innerhalfheight{\the\pgf@ya}%
  \addtosavedmacro\innerhalfheight%
  %
  % Take into account the outer sep.
  %
  \pgfmathaddtolength\pgf@x{\pgfkeysvalueof{/pgf/outer xsep}}%
  \pgfmathaddtolength\pgf@y{\pgfkeysvalueof{/pgf/outer ysep}}%
  %
  \edef\halfwidth{\the\pgf@x}%
  \edef\halfheight{\the\pgf@y}%
  \addtosavedmacro\halfwidth%
  \addtosavedmacro\halfheight%
}%

\def\pgf@lib@sh@logicgate@IEC@inputanchor#1{%
  \dimensions%
  \centerpoint%
  \advance\pgf@x-\halfwidth%
  \expandafter\ifx\expandafter\pgf@lib@sh@itext\csname input-#1\endcsname%
    \advance\pgf@x-\invertedradius%
    \advance\pgf@x-\outerinvertedradius%
  \fi%
  %
  \pgfutil@tempdima\halfheight%
  \multiply\pgfutil@tempdima2\relax%
  \c@pgf@counta\numinputs%
  \advance\c@pgf@counta1\relax%
  \divide\pgfutil@tempdima\c@pgf@counta%
  \multiply\pgfutil@tempdima#1\relax%
  \advance\pgf@y\halfheight%
  \advance\pgf@y-\pgfutil@tempdima%
}%



% Shape and gate IEC
%
\pgfdeclareshape{and gate IEC}{%
  \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@and gate IEC\endcsname{%
    \pgf@lib@sh@logicgate@parseinputs{1024}% Maximum 1024 (!) inputs.
    %
    \pgfmathloop%
    \ifnum\pgfmathcounter>\pgf@lib@sh@logicgate@numinputs%
    \else%
      \pgfutil@ifundefined{pgf@anchor@and gate IEC@input \pgfmathcounter}{%
        \expandafter\xdef\csname pgf@anchor@and gate IEC@input \pgfmathcounter\endcsname{%
          \noexpand\pgf@lib@sh@logicgate@IEC@inputanchor{\pgfmathcounter}%
        }%
      }{}%
    \repeatpgfmathloop%
    \ifnum\pgf@lib@sh@logicgate@numinputs<2\relax%
      \pgferror{An and gate must have at least two inputs}%
    \fi%
  }%
  \savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
  \saveddimen\invertedradius{%
    \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
  }%
  \saveddimen\outerinvertedradius{%
    \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
    \advance\pgf@x.5\pgflinewidth%
  }%
  \savedmacro\dimensions{%
    \pgf@lib@sh@logicgates@dimensions@IEC{and}%
  }%
  \savedanchor\centerpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y.5\ht\pgfnodeparttextbox%
    \advance\pgf@y-.5\dp\pgfnodeparttextbox%
  }%
  \savedanchor\midpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgfmathsetlength\pgf@y{+0.5ex}%
  }%
  \savedanchor\basepoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y0pt%
  }%
  \anchor{center}{\centerpoint}%
  \anchor{mid}{\midpoint}%
  \anchor{mid west}{%
    \dimensions%
    \midpoint%
    \advance\pgf@x-\halfwidth}%
  \anchor{mid east}{%
    \dimensions%
    \midpoint%
    \advance\pgf@x\halfwidth}%
  \anchor{base}{\basepoint}%
  \anchor{base west}{%
    \dimensions%
    \basepoint%
    \advance\pgf@x-\halfwidth}%
  \anchor{base east}{%
    \dimensions%
    \basepoint%
    \advance\pgf@x\halfwidth}%
  \anchor{north}{\dimensions\centerpoint\advance\pgf@y\halfheight}%
  \anchor{south}{\dimensions\centerpoint\advance\pgf@y-\halfheight}%
  \anchor{east}{\dimensions\centerpoint\advance\pgf@x\halfwidth}%
  \anchor{west}{\dimensions\centerpoint\advance\pgf@x-\halfwidth}%
  \anchor{north east}{\dimensions\centerpoint\advance\pgf@x\halfwidth\advance\pgf@y\halfheight}%
  \anchor{north west}{\dimensions\centerpoint\advance\pgf@x-\halfwidth\advance\pgf@y\halfheight}%
  \anchor{south east}{\dimensions\centerpoint\advance\pgf@x\halfwidth\advance\pgf@y-\halfheight}%
  \anchor{south west}{\dimensions\centerpoint\advance\pgf@x-\halfwidth\advance\pgf@y-\halfheight}%
  \anchor{output}{\dimensions\centerpoint\advance\pgf@x\halfwidth}%
  \backgroundpath{%
    \dimensions%
    \pgf@xc\halfwidth%
    \pgf@yc\halfheight%
    \pgfmathaddtolength\pgf@xc{-\pgfkeysvalueof{/pgf/outer xsep}}%
    \pgfmathaddtolength\pgf@yc{-\pgfkeysvalueof{/pgf/outer xsep}}%
    {%
      \pgftransformshift{\centerpoint}%
      \pgfpathrectangle{\pgfqpoint{-\pgf@xc}{-\pgf@yc}}%
        {%
          \pgf@xc2.0\pgf@xc%
          \pgf@yc2.0\pgf@yc%
          \pgfqpoint{\pgf@xc}{\pgf@yc}%
        }%
      \pgfpathclose%
      %
      % Draw inputs.
      %
      \pgf@xc-\halfwidth%
      \advance\pgf@xc-\invertedradius%
      \pgf@yc\halfheight%
      \pgfutil@tempdima2.0\pgf@yc%
      \c@pgf@counta\numinputs%
      \advance\c@pgf@counta1\relax%
      \divide\pgfutil@tempdima\c@pgf@counta%
      \pgfmathloop%
      \ifnum\pgfmathcounter>\numinputs%
      \else%
        \advance\pgf@yc-\pgfutil@tempdima%
        \expandafter\ifx\expandafter\pgf@lib@sh@itext\csname input-\pgfmathcounter\endcsname%
          {%
            \pgfpathcircle{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\invertedradius}%
          }%
        \fi%
      \repeatpgfmathloop%
    }%
  }%
  \foregroundpath{\pgf@lib@sh@logicgates@IEC@foregroundpath{and}}%
  \anchorborder{%
    \pgfextract@process\externalpoint{}%
    \dimensions%
    \pgfpointadd{\centerpoint}{\pgfpointborderrectangle{\externalpoint}{\pgfqpoint{\halfwidth}{\halfheight}}}%
  }%
}%





% Shape nand gate IEC
%
\pgfdeclareshape{nand gate IEC}{%
  \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@nand gate IEC\endcsname{%
    \pgf@lib@sh@logicgate@parseinputs{1024}% Maximum 1024 (!) inputs.
    %
    \pgfmathloop%
    \ifnum\pgfmathcounter>\pgf@lib@sh@logicgate@numinputs%
    \else%
      \pgfutil@ifundefined{pgf@anchor@nand gate IEC@input \pgfmathcounter}{%
        \expandafter\xdef\csname pgf@anchor@nand gate IEC@input \pgfmathcounter\endcsname{%
          \noexpand\pgf@lib@sh@logicgate@IEC@inputanchor{\pgfmathcounter}%
        }%
      }{}%
    \repeatpgfmathloop%
    \ifnum\pgf@lib@sh@logicgate@numinputs<2\relax%
      \pgferror{A nand gate must have at least two inputs}%
    \fi%
  }%
  \savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
  \saveddimen\invertedradius{%
    \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
  }%
  \saveddimen\outerinvertedradius{%
    \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
    \advance\pgf@x.5\pgflinewidth%
  }%
  \savedmacro\dimensions{%
    \pgf@lib@sh@logicgates@dimensions@IEC{nand}%
  }%
  \savedanchor\centerpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y.5\ht\pgfnodeparttextbox%
    \advance\pgf@y-.5\dp\pgfnodeparttextbox%
  }%
  \savedanchor\midpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgfmathsetlength\pgf@y{+0.5ex}%
  }%
  \savedanchor\basepoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y0pt%
  }%
  \inheritanchor[from=and gate IEC]{center}%
  \inheritanchor[from=and gate IEC]{mid}%
  \inheritanchor[from=and gate IEC]{mid west}%
  \inheritanchor[from=and gate IEC]{mid east}%
  \inheritanchor[from=and gate IEC]{base}%
  \inheritanchor[from=and gate IEC]{base west}%
  \inheritanchor[from=and gate IEC]{base east}%
  \inheritanchor[from=and gate IEC]{north}%
  \inheritanchor[from=and gate IEC]{south}%
  \inheritanchor[from=and gate IEC]{east}%
  \inheritanchor[from=and gate IEC]{west}%
  \inheritanchor[from=and gate IEC]{north east}%
  \inheritanchor[from=and gate IEC]{north west}%
  \inheritanchor[from=and gate IEC]{south east}%
  \inheritanchor[from=and gate IEC]{south west}%
  \anchor{output}{%
    \csname pgf@anchor@and gate IEC@east\endcsname%
    \advance\pgf@x\invertedradius%
    \advance\pgf@x\outerinvertedradius%
  }%
  \backgroundpath{%
    \csname pgf@sh@bg@and gate IEC\endcsname%
    \pgfpathcircle{%
      \centerpoint%
      \advance\pgf@x\halfwidth%
      \advance\pgf@x\invertedradius%
    }{+\invertedradius}%
  }%
  \foregroundpath{\pgf@lib@sh@logicgates@IEC@foregroundpath{nand}}%
  \inheritanchorborder[from=and gate IEC]%
}%





% Shape or gate IEC
%
\pgfdeclareshape{or gate IEC}{%
  \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@or gate IEC\endcsname{%
    \pgf@lib@sh@logicgate@parseinputs{1024}% Maximum 1024 (!) inputs.
    %
    \pgfmathloop%
    \ifnum\pgfmathcounter>\pgf@lib@sh@logicgate@numinputs%
    \else%
      \pgfutil@ifundefined{pgf@anchor@or gate IEC@input \pgfmathcounter}{%
        \expandafter\xdef\csname pgf@anchor@or gate IEC@input \pgfmathcounter\endcsname{%
          \noexpand\pgf@lib@sh@logicgate@IEC@inputanchor{\pgfmathcounter}%
        }%
      }{}%
    \repeatpgfmathloop%
    \ifnum\pgf@lib@sh@logicgate@numinputs<2\relax%
      \pgferror{An or gate must have at least two inputs}%
    \fi%
  }%
  \savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
  \saveddimen\invertedradius{%
    \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
  }%
  \saveddimen\outerinvertedradius{%
    \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
    \advance\pgf@x.5\pgflinewidth%
  }%
  \savedmacro\dimensions{%
    \pgf@lib@sh@logicgates@dimensions@IEC{or}%
  }%
  \savedanchor\centerpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y.5\ht\pgfnodeparttextbox%
    \advance\pgf@y-.5\dp\pgfnodeparttextbox%
  }%
  \savedanchor\midpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgfmathsetlength\pgf@y{+0.5ex}%
  }%
  \savedanchor\basepoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y0pt%
  }%
  \inheritanchor[from=and gate IEC]{center}%
  \inheritanchor[from=and gate IEC]{mid}%
  \inheritanchor[from=and gate IEC]{mid west}%
  \inheritanchor[from=and gate IEC]{mid east}%
  \inheritanchor[from=and gate IEC]{base}%
  \inheritanchor[from=and gate IEC]{base west}%
  \inheritanchor[from=and gate IEC]{base east}%
  \inheritanchor[from=and gate IEC]{north}%
  \inheritanchor[from=and gate IEC]{south}%
  \inheritanchor[from=and gate IEC]{east}%
  \inheritanchor[from=and gate IEC]{west}%
  \inheritanchor[from=and gate IEC]{north east}%
  \inheritanchor[from=and gate IEC]{north west}%
  \inheritanchor[from=and gate IEC]{south east}%
  \inheritanchor[from=and gate IEC]{south west}%
  \inheritanchor[from=and gate IEC]{output}%
  \inheritbackgroundpath[from=and gate IEC]%
  \foregroundpath{\pgf@lib@sh@logicgates@IEC@foregroundpath{or}}%
  \inheritanchorborder[from=and gate IEC]%
}%





% Shape nor gate IEC
%
\pgfdeclareshape{nor gate IEC}{%
  \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@nor gate IEC\endcsname{%
    \pgf@lib@sh@logicgate@parseinputs{1024}% Maximum 1024 (!) inputs.
    %
    \pgfmathloop%
    \ifnum\pgfmathcounter>\pgf@lib@sh@logicgate@numinputs%
    \else%
      \pgfutil@ifundefined{pgf@anchor@nor gate IEC@input \pgfmathcounter}{%
        \expandafter\xdef\csname pgf@anchor@nor gate IEC@input \pgfmathcounter\endcsname{%
          \noexpand\pgf@lib@sh@logicgate@IEC@inputanchor{\pgfmathcounter}%
        }%
      }{}%
    \repeatpgfmathloop%
    \ifnum\pgf@lib@sh@logicgate@numinputs<2\relax%
      \pgferror{A nor gate must have at least two inputs}%
    \fi%
  }%
  \savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
  \saveddimen\invertedradius{%
    \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
  }%
  \saveddimen\outerinvertedradius{%
    \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
    \advance\pgf@x.5\pgflinewidth%
  }%
  \savedmacro\dimensions{%
    \pgf@lib@sh@logicgates@dimensions@IEC{nor}%
  }%
  \savedanchor\centerpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y.5\ht\pgfnodeparttextbox%
    \advance\pgf@y-.5\dp\pgfnodeparttextbox%
  }%
  \savedanchor\midpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgfmathsetlength\pgf@y{+0.5ex}%
  }%
  \savedanchor\basepoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y0pt%
  }%
  \inheritanchor[from=and gate IEC]{center}%
  \inheritanchor[from=and gate IEC]{mid}%
  \inheritanchor[from=and gate IEC]{mid west}%
  \inheritanchor[from=and gate IEC]{mid east}%
  \inheritanchor[from=and gate IEC]{base}%
  \inheritanchor[from=and gate IEC]{base west}%
  \inheritanchor[from=and gate IEC]{base east}%
  \inheritanchor[from=and gate IEC]{north}%
  \inheritanchor[from=and gate IEC]{south}%
  \inheritanchor[from=and gate IEC]{east}%
  \inheritanchor[from=and gate IEC]{west}%
  \inheritanchor[from=and gate IEC]{north east}%
  \inheritanchor[from=and gate IEC]{north west}%
  \inheritanchor[from=and gate IEC]{south east}%
  \inheritanchor[from=and gate IEC]{south west}%
  \inheritanchor[from=nand gate IEC]{output}%%
  \inheritbackgroundpath[from=nand gate IEC]%
  \foregroundpath{\pgf@lib@sh@logicgates@IEC@foregroundpath{nor}}%
  \inheritanchorborder[from=and gate IEC]%
}%





% Shape xor gate IEC
%
\pgfdeclareshape{xor gate IEC}{%
  \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@xor gate IEC\endcsname{%
    \pgf@lib@sh@logicgate@parseinputs{2}% Maximum 1024 (!) inputs.
    %
    \pgfmathloop%
    \ifnum\pgfmathcounter>\pgf@lib@sh@logicgate@numinputs%
    \else%
      \pgfutil@ifundefined{pgf@anchor@xor gate IEC@input \pgfmathcounter}{%
        \expandafter\xdef\csname pgf@anchor@xor gate IEC@input \pgfmathcounter\endcsname{%
          \noexpand\pgf@lib@sh@logicgate@IEC@inputanchor{\pgfmathcounter}%
        }%
      }{}%
    \repeatpgfmathloop%
    \ifnum\pgf@lib@sh@logicgate@numinputs<2\relax%
      \pgferror{An xor gate must have two inputs}%
    \fi%
  }%
  \savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
  \saveddimen\invertedradius{%
    \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
  }%
  \saveddimen\outerinvertedradius{%
    \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
    \advance\pgf@x.5\pgflinewidth%
  }%
  \savedmacro\dimensions{%
    \pgf@lib@sh@logicgates@dimensions@IEC{xor}%
  }%
  \savedanchor\centerpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y.5\ht\pgfnodeparttextbox%
    \advance\pgf@y-.5\dp\pgfnodeparttextbox%
  }%
  \savedanchor\midpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgfmathsetlength\pgf@y{+0.5ex}%
  }%
  \savedanchor\basepoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y0pt%
  }%
  \inheritanchor[from=and gate IEC]{center}%
  \inheritanchor[from=and gate IEC]{mid}%
  \inheritanchor[from=and gate IEC]{mid west}%
  \inheritanchor[from=and gate IEC]{mid east}%
  \inheritanchor[from=and gate IEC]{base}%
  \inheritanchor[from=and gate IEC]{base west}%
  \inheritanchor[from=and gate IEC]{base east}%
  \inheritanchor[from=and gate IEC]{north}%
  \inheritanchor[from=and gate IEC]{south}%
  \inheritanchor[from=and gate IEC]{east}%
  \inheritanchor[from=and gate IEC]{west}%
  \inheritanchor[from=and gate IEC]{north east}%
  \inheritanchor[from=and gate IEC]{north west}%
  \inheritanchor[from=and gate IEC]{south east}%
  \inheritanchor[from=and gate IEC]{south west}%
  \inheritanchor[from=and gate IEC]{output}%
  \inheritbackgroundpath[from=and gate IEC]%
  \foregroundpath{\pgf@lib@sh@logicgates@IEC@foregroundpath{xor}}%
  \inheritanchorborder[from=and gate IEC]%
}%




% Shape xnor gate IEC
%

\pgfdeclareshape{xnor gate IEC}{%
  \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@xnor gate IEC\endcsname{%
    \pgf@lib@sh@logicgate@parseinputs{1024}% Maximum 1024 (!) inputs.
    %
    \pgfmathloop%
    \ifnum\pgfmathcounter>\pgf@lib@sh@logicgate@numinputs%
    \else%
      \pgfutil@ifundefined{pgf@anchor@xnor gate IEC@input \pgfmathcounter}{%
        \expandafter\xdef\csname pgf@anchor@xnor gate IEC@input \pgfmathcounter\endcsname{%
          \noexpand\pgf@lib@sh@logicgate@IEC@inputanchor{\pgfmathcounter}%
        }%
      }{}%
    \repeatpgfmathloop%
    \ifnum\pgf@lib@sh@logicgate@numinputs<2\relax%
      \pgferror{A xnor gate must have two inputs}%
    \fi%
  }%
  \savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
  \saveddimen\invertedradius{%
    \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
  }%
  \saveddimen\outerinvertedradius{%
    \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
    \advance\pgf@x.5\pgflinewidth%
  }%
  \savedmacro\dimensions{%
    \pgf@lib@sh@logicgates@dimensions@IEC{xnor}%
  }%
  \savedanchor\centerpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y.5\ht\pgfnodeparttextbox%
    \advance\pgf@y-.5\dp\pgfnodeparttextbox%
  }%
  \savedanchor\midpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgfmathsetlength\pgf@y{+0.5ex}%
  }%
  \savedanchor\basepoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y0pt%
  }%
  \inheritanchor[from=and gate IEC]{center}%
  \inheritanchor[from=and gate IEC]{mid}%
  \inheritanchor[from=and gate IEC]{mid west}%
  \inheritanchor[from=and gate IEC]{mid east}%
  \inheritanchor[from=and gate IEC]{base}%
  \inheritanchor[from=and gate IEC]{base west}%
  \inheritanchor[from=and gate IEC]{base east}%
  \inheritanchor[from=and gate IEC]{north}%
  \inheritanchor[from=and gate IEC]{south}%
  \inheritanchor[from=and gate IEC]{east}%
  \inheritanchor[from=and gate IEC]{west}%
  \inheritanchor[from=and gate IEC]{north east}%
  \inheritanchor[from=and gate IEC]{north west}%
  \inheritanchor[from=and gate IEC]{south east}%
  \inheritanchor[from=and gate IEC]{south west}%
  \inheritanchor[from=nand gate IEC]{output}%
  \inheritbackgroundpath[from=nand gate IEC]%
  \foregroundpath{\pgf@lib@sh@logicgates@IEC@foregroundpath{xnor}}%
  \inheritanchorborder[from=and gate IEC]%
}%




% Shape buffer gate IEC
%
\pgfdeclareshape{buffer gate IEC}{%
  \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@buffer gate IEC\endcsname{%
    \pgf@lib@sh@logicgate@parseinputs{1}% Maximum 1 input.
    %
    \expandafter\gdef\csname pgf@anchor@buffer gate IEC@input\endcsname{%
      \pgf@lib@sh@logicgate@IEC@inputanchor{1}%
    }%
    \ifnum\pgf@lib@sh@logicgate@numinputs=0\relax%
      \pgferror{A buffer gate must have one inputs}%
    \fi%
  }%
  \savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
  \saveddimen\invertedradius{%
    \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
  }%
  \saveddimen\outerinvertedradius{%
    \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
    \advance\pgf@x.5\pgflinewidth%
  }%
  \savedmacro\dimensions{%
    \pgf@lib@sh@logicgates@dimensions@IEC{xor}%
  }%
  \savedanchor\centerpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y.5\ht\pgfnodeparttextbox%
    \advance\pgf@y-.5\dp\pgfnodeparttextbox%
  }%
  \savedanchor\midpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgfmathsetlength\pgf@y{+0.5ex}%
  }%
  \savedanchor\basepoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y0pt%
  }%
  \inheritanchor[from=and gate IEC]{center}%
  \inheritanchor[from=and gate IEC]{mid}%
  \inheritanchor[from=and gate IEC]{mid west}%
  \inheritanchor[from=and gate IEC]{mid east}%
  \inheritanchor[from=and gate IEC]{base}%
  \inheritanchor[from=and gate IEC]{base west}%
  \inheritanchor[from=and gate IEC]{base east}%
  \inheritanchor[from=and gate IEC]{north}%
  \inheritanchor[from=and gate IEC]{south}%
  \inheritanchor[from=and gate IEC]{east}%
  \inheritanchor[from=and gate IEC]{west}%
  \inheritanchor[from=and gate IEC]{north east}%
  \inheritanchor[from=and gate IEC]{north west}%
  \inheritanchor[from=and gate IEC]{south east}%
  \inheritanchor[from=and gate IEC]{south west}%
  \inheritanchor[from=and gate IEC]{output}%
  \inheritbackgroundpath[from=and gate IEC]%
  \foregroundpath{\pgf@lib@sh@logicgates@IEC@foregroundpath{buffer}}%
  \inheritanchorborder[from=and gate IEC]%
}%





% Shape not gate IEC
%
\pgfdeclareshape{not gate IEC}{%
  \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@not gate IEC\endcsname{%
    \pgf@lib@sh@logicgate@parseinputs{1}%
    %
    \pgfutil@ifundefined{pgf@anchor@not gate IEC@input}{%
      \expandafter\gdef\csname pgf@anchor@not gate IEC@input\endcsname{%
          \pgf@lib@sh@logicgate@IEC@inputanchor{1}%
        }}{}%
    \ifnum\pgf@lib@sh@logicgate@numinputs=0\relax%
      \pgferror{A not gate must have one input}%
    \fi%
  }%
  \savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
  \saveddimen\invertedradius{%
    \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
  }%
  \saveddimen\outerinvertedradius{%
    \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
    \advance\pgf@x.5\pgflinewidth%
  }%
  \savedmacro\dimensions{%
    \pgf@lib@sh@logicgates@dimensions@IEC{not}%
  }%
  \savedanchor\centerpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y.5\ht\pgfnodeparttextbox%
    \advance\pgf@y-.5\dp\pgfnodeparttextbox%
  }%
  \savedanchor\midpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgfmathsetlength\pgf@y{+0.5ex}%
  }%
  \savedanchor\basepoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y0pt%
  }%
  \inheritanchor[from=and gate IEC]{center}%
  \inheritanchor[from=and gate IEC]{mid}%
  \inheritanchor[from=and gate IEC]{mid west}%
  \inheritanchor[from=and gate IEC]{mid east}%
  \inheritanchor[from=and gate IEC]{base}%
  \inheritanchor[from=and gate IEC]{base west}%
  \inheritanchor[from=and gate IEC]{base east}%
  \inheritanchor[from=and gate IEC]{north}%
  \inheritanchor[from=and gate IEC]{south}%
  \inheritanchor[from=and gate IEC]{east}%
  \inheritanchor[from=and gate IEC]{west}%
  \inheritanchor[from=and gate IEC]{north east}%
  \inheritanchor[from=and gate IEC]{north west}%
  \inheritanchor[from=and gate IEC]{south east}%
  \inheritanchor[from=and gate IEC]{south west}%
  \inheritanchor[from=nand gate IEC]{output}%
  \inheritbackgroundpath[from=nand gate IEC]%
  \foregroundpath{\pgf@lib@sh@logicgates@IEC@foregroundpath{not}}%
  \inheritanchorborder[from=and gate IEC]%
}%

\endinput