% \iffalse
% --------------------------------------------------------------------
% <*chit>
% \fi
% 
% \subsubsection{The \texttt{chit} key namespace}
%
% Some stuff to consider wrt.~line widths.  Setting the line width in
% the \texttt{chit} scope overrides frame settings.  The frame stroke
% can be larger but not smaller.  Setting the stroke width in the
% symbol scope sets it for the symbol only.  Thus, to get a thin
% border, we need to
% \begin{itemize}
% \item Set a small line width in the top chit scope.
% \item Possible set a larger line width in the frame sub-scope.
% \item Set a larger line width in the symbol sub-scope. 
% \end{itemize}
% I do not know why this is.
% 
% \begin{TikzKey}{/chit/full,
%   /chit/symbol,
%   /chit/left,
%   /chit/right,
%   /chit/upper left,
%   /chit/upper right,
%   /chit/lower left,
%   /chit/lower right,
%   /chit/factors,
%   /chit/extra,
%   /chit/setup,
%   /chit/bevel,
%   /chit/id}
%
%   The parts of a chit 
%   
%    \begin{macrocode}
\newif\ifchit@clip\chit@cliptrue
\tikzset{%
  /chit/.search also={/tikz},
  /chit/.cd,
  full/.store in=\chit@full,               full/.initial=,%
  symbol/.store in=\chit@symbol,           symbol/.initial=,%
  left/.store in=\chit@left,               left/.initial=,%
  unique/.style={/chit/left={#1}},%
  right/.store in=\chit@right,             right/.initial=,%
  parent/.style={/chit/right={#1}},%
  upper left/.store in=\chit@upper@left,   upper left/.initial=,%
  upper right/.store in=\chit@upper@right, upper right/.initial=,%
  lower left/.store in=\chit@lower@left,   lower left/.initial=,%
  lower right/.store in=\chit@lower@right, lower right/.initial=,%
  factors/.store in=\chit@factors,         factors/.initial=,%
  setup/.store in=\chit@setup,             setup/.initial=,%
  id/.store in=\chit@id,                   id/.initial=,%
  frame/.store in=\chit@frame,             frame/.initial=,%
  extra/.store in=\chit@extra,             extra/.initial=,%
  bev/.store in=\chit@bevel,               bev/.initial=,
  bevel fraction/.store in=\chit@bevel@frac,bevel fraction/.initial=10,
  bevel/.is choice,
  bevel/none/.style       = {/chit/bev=},
  bevel/north west/.style = {/chit/bev=1},
  bevel/north east/.style = {/chit/bev=2},
  bevel/south west/.style = {/chit/bev=3},
  bevel/south east/.style = {/chit/bev=4},
  bevel/NW/.style         = {/chit/bev=1},
  bevel/NE/.style         = {/chit/bev=2},
  bevel/SW/.style         = {/chit/bev=3},
  bevel/SE/.style         = {/chit/bev=4},
  bevel/.default          = north west,
  clip/.is if=chit@clip,%
  nato shape/.store in=\chit@n@to@shape,
}
%    \end{macrocode}
% \end{TikzKey}
%
% \begin{TikzKey}{/tikz/chit/full,
%   /tikz/chit/symbol,
%   /tikz/chit/left,
%   /tikz/chit/right,
%   /tikz/chit/upper left,
%   /tikz/chit/upper right,
%   /tikz/chit/lower left,
%   /tikz/chit/lower right,
%   /tikz/chit/factors,
%   /tikz/chit/setup,
%   /tikz/chit/id}
%
% Styles of each element in a chit.  Users may override these at their
% own peril.  That is, it is OK to override them, but the user should
% be careful. 
% 
%    \begin{macrocode}
\def\chit@sym@sc@le{.4}
\tikzset{
  chit/symbol/.style={scale=\chit@sym@sc@le,transform shape},
  chit/parts/.style={shape=rectangle,transform shape},
  chit/factors/.style={chit/parts,anchor=south},
  chit/left/.style={chit/parts,anchor=base,rotate=90},%Anchor was south
  chit/right/.style={chit/parts,anchor=north,rotate=90},
  chit/upper left/.style={chit/parts,anchor=north west},
  chit/upper right/.style={chit/parts,anchor=north east},
  chit/lower left/.style={chit/parts,anchor=south west},
  chit/lower right/.style={chit/parts,anchor=south east},
  chit/setup/.style={chit/parts},
  chit/full/.style={chit/parts},
  chit/frame/.try={draw=pgfstrokecolor},
  chit/bevel highlight/.style={fill=white,opacity=.25},
  chit/bevel shadow/.style={fill=black,opacity=.25},
}
\def\chit@bevel@frac{10}
\newif\ifchit@draw@frame\chit@draw@frametrue
\tikzset{
  chit/frame style/.search also={/tikz},
  chit/frame style/.cd,
  none/.code={\chit@draw@framefalse},
  draw/.code={%
    \chit@dbg{2}{Frame draw option `#1'}
    \edef\tikz@temp{#1}%
    \ifx\tikz@temp\tikz@nonetext%
      \chit@draw@framefalse%
    \else%
      \chit@draw@frametrue%
      \tikzset{/tikz/draw=#1}
    \fi
  }
}
    
%    \end{macrocode}
% \end{TikzKey}
% 
% \iffalse
% --------------------------------------------------------------------
% \fi
% 
% \subsubsection{The \texttt{chit} styles}
% 
% \begin{TikzKey}{/tikz/chit}
%   This key sets up a node to make a chit.  The key takes a single
%   argument which in turn must contain key--value pairs in the
%   \texttt{/chit} (or \texttt{/tikz}) namespace(s).   We set the
%   \texttt{shape} parameter of the node, and calls the passed keys in
%   the \texttt{/chit} namespace to set-up elements of the chit. 
%
%    \begin{macrocode}
\tikzset{%
  chit/.code={%
    \chit@dbg{2}{chit arguments are `#1'}%
    \pgfkeys{/tikz/transform shape,/tikz/shape=chit}%
    \pgfkeys{/chit/.cd,nato shape=natoapp6c,#1}}}
%    \end{macrocode}
% \end{TikzKey}
%
% We define a counter to set-up unique names for chit nodes.
% 
%    \begin{macrocode}
\newcounter{chit@id}\setcounter{chit@id}{0}
%    \end{macrocode}
%
% \iffalse
% --------------------------------------------------------------------
% \fi
% \subsubsection{The \cs{chit} shape}
%
% \begin{Macro}{\chit@n@to,\@chit@n@to,\@@chit@n@to,\@chit@n@to@}
%
%   These macros puts the NATO App6(c) symbol into a chit.  The first
%   macro takes the identifier and position of the symbol, and then
%   scans for options.  If no options are given, then we go directly
%   to the rendering (\cs{@chit@n@to@}).  Otherwise, we may also need
%   to scan for an offset given as \parg{delta-x,delta-y}.
% 
%    \begin{macrocode}
\def\chit@n@to#1#2{%
  %% Without a following start square bracket '[' by-pass to final
  \chit@dbg{1}{Chit NATO App6(c) first step `#1' `#2'}
  \@ifnextchar[{%
    %\message{^^JStart square bracket}%
    \@chit@n@to{#1}{#2}}{%
    %\message{^^JNo start square bracket}%
    \@chit@n@to@{#1}{#2}}%]]
}
%    \end{macrocode}
%
% The following macro is called if we had no options.
% 
%    \begin{macrocode}
\def\chit@n@to@shape{natoapp6c}
\def\@chit@n@to@#1#2#3\@end@chit@n@to{%
  \chit@dbg{1}{Chit NATO App6(c) w/o offset:
    ^^J  Options:  `#3'
    ^^J  ID:       `#1'
    ^^J  Position: `#2'
    ^^J  Style:    `\chit@n@to@shape'
  }
  \chit@dbg{2}{NATO shape: `\chit@n@to@shape'}
  \node[chit/symbol,\chit@n@to@shape={#3,id=#1}] (#1) at (#2) {};
  \chit@dbg{4}{Chit NATO App6(c) ended}%
}
%    \end{macrocode}
%
% This is called if we had an option-like argument. Check if we have
% an offset
% 
%    \begin{macrocode}
\def\@chit@n@to#1#2[#3]{%
  \chit@dbg{1}{Chit NATO App6(c) second step `#1' `#2' `#3'}
  \@ifnextchar({\@@chit@n@to{#1}{#2}{#3}}{\@@chit@n@to{#1}{#2}{#3}(0,0)}%)
}
%    \end{macrocode}
%
% This called if we had option-like argument.
% 
%    \begin{macrocode}
\def\@@chit@n@to#1#2#3(#4)\@end@chit@n@to{%
  \chit@dbg{1}{Chit NATO App6(c) w/offset:
    ^^J  Options:  #3
    ^^J  ID:       #1
    ^^J  Position: #2
    ^^J  Offset:   #4}
  \chit@dbg{10}{==== NATO shape: `\chit@n@to@shape' ====}
  \node[chit/symbol,\chit@n@to@shape={#3,id=#1}] (#1) at ($(#2)+(#4)$) {};}
%    \end{macrocode}
% \end{Macro}
% 
% \begin{Macro}{\chit@tr@ns@nchor,\chit@nchor}
%
% Get anchor of sub-symbol element in chit.  We need to do this,
% because the symbol is translated and scaled.
% 
%    \begin{macrocode}
\def\chit@tr@ns@nchor{%
  \chit@dbg{10}{Translating anchor `\the\pgf@x',`\the\pgf@y'}
  \wg@tmpa=\pgf@x%
  \wg@tmpb=\pgf@y%
  \symbol%
  \chit@dbg{10}{Symbol origin `\the\pgf@x',`\the\pgf@y'}
  \wg@tmpc=\pgf@x%
  \wg@tmpd=\pgf@y%
  \pgf@x=\chit@sym@sc@le\wg@tmpa%
  \pgf@y=\chit@sym@sc@le\wg@tmpb%
  \chit@dbg{10}{Scaled anchor `\the\pgf@x',`\the\pgf@y'}
  \advance\pgf@x\wg@tmpc%
  \advance\pgf@y\wg@tmpd%
  \chit@dbg{10}{Offset anchor `\the\pgf@x',`\the\pgf@y'}
}
%    \end{macrocode}
%    
%    \begin{macrocode}
\def\chit@nchor#1#2{%
  \chit@dbg{10}{Get chit sub anchor of `#1' `#2'}%
  \wg@sub@nchor{#1}{#2}%
  \chit@tr@ns@nchor%
  \chit@dbg{10}{Got chit sub anchor of `\the\pgf@x `\the\pgf@y}%
}
\def\chit@sym@nchor#1{%
  \chit@dbg{10}{Get chit symbol `#1'}%
  \edef\tmpid{\id symbol}%
  \chit@nchor{\tmpid}{#1}}
\def\chit@report{}
\tikzset{
  zone turn/.style={},
  zone mult/.style={}
}
\def\chit@bkg@p@th{%
  \northeast%
  \wg@tmpa=\pgf@x\wg@tmpb=\pgf@y%
  \pgfpathmoveto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
  \wg@tmpa=-\wg@tmpa \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
  \wg@tmpb=-\wg@tmpb \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
  \wg@tmpa=-\wg@tmpa \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
  \pgfclosepath%
}  
%    \end{macrocode}
% \end{Macro}
% 
% Now follows the actual \text{chit} shape.  This is rather long, so
% we will break it up a bit
%    \begin{macrocode}
\def\chit@bevel@path#1{
  \scope[#1]
  \wg@tmpc=\wg@tmpa%
  \wg@tmpd=\wg@tmpb%
  %% Absolute values 
  \ifdim\wg@tmpa<0pt\multiply\wg@tmpc by -1\fi%
  \ifdim\wg@tmpb<0pt\multiply\wg@tmpd by -1\fi%
  % Why the hell do I need this?
  \chit@dbg{10}{`\the\wg@tmpa' `\the\wg@tmpb'}%
  %% Smallest dimension 
  \ifdim\wg@tmpc>\wg@tmpd \wg@tmpc=\wg@tmpd\fi%
  \ifdim\wg@tmpc<\wg@tmpd \wg@tmpd=\wg@tmpc\fi%
  %% Restore sign
  \ifdim\wg@tmpa<0pt\multiply\wg@tmpc by -1\fi%
  \ifdim\wg@tmpb<0pt\multiply\wg@tmpd by -1\fi%
  %% Take the fraction
  \multiply\wg@tmpc by \chit@bevel@frac%
  \multiply\wg@tmpd by \chit@bevel@frac%
  \divide\wg@tmpc100%
  \divide\wg@tmpd100%
  \pgfpathmoveto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
  % Move down along edge
  \wg@tmpb=-\wg@tmpb%
  \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
  % Move left along edge
  \wg@tmpa=-\wg@tmpa%
  \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
  % Move in and up 
  \advance\wg@tmpa\wg@tmpc%
  \advance\wg@tmpb\wg@tmpd%
  \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
  % Move right, but in 
  \advance\wg@tmpa-\wg@tmpc\wg@tmpa=-\wg@tmpa%
  \advance\wg@tmpa-\wg@tmpc%
  \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
  % Move up but down
  \advance\wg@tmpb-\wg@tmpd\wg@tmpb=-\wg@tmpb%
  \advance\wg@tmpb-\wg@tmpd%
  \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
  \pgfclosepath%
  \pgfusepath{fill}%
  \endscope%
}
%    \end{macrocode}
% 
% The first thing is we declare some saved anchors.  These are
% computed (and defined as internal macros) when the shape is
% instantised.  The anchors give the centre and north east corner of
% the node, the place to put the NATO App6(c) symbol and factors.   We
% also set a dimension for the margins (corner and factors elements). 
% 
%    \begin{macrocode}
\pgfdeclareshape{chit}{
  \savedanchor\center{\pgf@x=0cm\pgf@y=0cm}
  \savedanchor\northeast{\pgf@x=0.6cm\pgf@y=\pgf@x}
  \savedanchor\symbol{\pgf@x=0cm\pgf@y=0.1cm}
  \savedanchor\factors{\pgf@x=0cm\pgf@y=-0.5cm}
  \saveddimen\margin{\pgf@x=0.04cm}
%    \end{macrocode}
%
% Next, we define some saved macros.  These are called (and declares
% internal macros) when the shape is instantised.  We define macros
% for the identifier, 
%
%
%    \begin{macrocode}
  \savedmacro\id{%
    \chit@dbg{4}{Chit ID: \meaning\chit@id}%
    \@ifundefined{chit@id}{\let\chit@id\pgfutil@empty}{}%
    \ifx\chit@id\pgfutil@empty%
      \wg@r@ndom@id%
      \edef\id{chit\wg@uuid}%
    \else%
      \edef\id{\chit@id}%
    \fi%
    \chit@dbg{4}{Chit ID stored: \meaning\chit@id}
  }
  \savedmacro\chitframeopt{%
    \let\chitframeopt\pgfutil@empty%
    \@ifundefined{chit@frame}{}{%
      \edef\chitframeopt{\chit@frame}}
    \chit@dbg{3}{Chit Frame options: \meaning\chitframeopt}%
  }
  \savedmacro\thisname{\def\thisname{chit}}
%    \end{macrocode}
%
% We define the regular anchors of the shape.  That is, the centre,
% corners, and edges.
% 
%    \begin{macrocode}
  \anchor{center}{\center}
  \anchor{north east}{\northeast}
  \anchor{north west}{\northeast\pgf@x=-\pgf@x}
  \anchor{south west}{\northeast\pgf@x=-\pgf@x\pgf@y=-\pgf@y}
  \anchor{south east}{\northeast\pgf@y=-\pgf@y}
  \anchor{north}     {\northeast\pgf@x=0cm}
  \anchor{south}     {\northeast\pgf@x=0cm\pgf@y=-\pgf@y}
  \anchor{east}      {\northeast\pgf@y=0cm}
  \anchor{west}      {\northeast\pgf@x=-\pgf@x\pgf@y=0cm}
%    \end{macrocode}
%
% Next, we want to be able to reference the symbol anchors too.  So we
% define these anchors from the embedded node anchors.  Note, these
% anchors will not exist if the chit is made with
% \texttt{full=}\meta{args}.
% 
%    \begin{macrocode}
  \anchor{symbol north east}{\chit@sym@nchor{north east}}
  \anchor{symbol north west}{\chit@sym@nchor{north west}}
  \anchor{symbol south east}{\chit@sym@nchor{south east}}
  \anchor{symbol south west}{\chit@sym@nchor{south west}}
  \anchor{symbol north}     {\chit@sym@nchor{north}}
  \anchor{symbol west}      {\chit@sym@nchor{west}}
  \anchor{symbol south}     {\chit@sym@nchor{south}}
  \anchor{symbol east}      {\chit@sym@nchor{east}}
  \anchor{symbol upper}     {\chit@sym@nchor{upper}}
  \anchor{symbol lower}     {\chit@sym@nchor{lower}}
  \anchor{symbol left}      {\chit@sym@nchor{left}}
  \anchor{symbol right}     {\chit@sym@nchor{right}}
  \anchor{symbol echelon}   {\chit@sym@nchor{echelon}}
  \anchor{symbol below}     {\chit@sym@nchor{below}}
%    \end{macrocode}
%
% Some anchors to sub-elements.  Some of them only exists if we have
% NATO App6(c) symbol in the chit.
% 
%    \begin{macrocode}
  \anchor{symbol}    {\symbol}
  \anchor{factors}   {\factors}
  \anchor{left} {\chit@sym@nchor{west}\advance\pgf@x-\margin}
  \anchor{right}{\chit@sym@nchor{east}\advance\pgf@x+\margin}
  \anchor{upper right} {%
    \northeast \advance\pgf@x-\margin \advance\pgf@y-\margin%
  }
  \anchor{upper left}{
    \northeast \advance\pgf@x-\margin \advance\pgf@y-\margin \pgf@x=-\pgf@x%
  }
  \anchor{lower right} {%
    \northeast \advance\pgf@x-\margin \advance\pgf@y-\margin \pgf@y=-\pgf@y%
  }
  \anchor{lower left}{
    \northeast \advance\pgf@x-\margin \advance\pgf@y-\margin%
    \pgf@x=-\pgf@x \pgf@y=-\pgf@y%
  }
%    \end{macrocode}
%
% Now for the actual path.  For the background path, we simply specify
% the frame.   This is so that this will get drawn (and possibly
% filled) using the appropriate options.
% 
%    \begin{macrocode}  
  \backgroundpath{%
    %% This is the outline of the chit only.  The rest of the chit is
    %% made on the foreground "path".
    \chit@dbg{1}{Chit drawing background path}
    \chit@bkg@p@th%
    % \northeast%
    % \wg@tmpa=\pgf@x\wg@tmpb=\pgf@y%
    % \pgfpathmoveto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
    % \wg@tmpa=-\wg@tmpa \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
    % \wg@tmpb=-\wg@tmpb \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
    % \wg@tmpa=-\wg@tmpa \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
    % \pgfclosepath
  }
%    \end{macrocode}
%
% Finally, we make the foreground rendered path.   This is where we do
% the most stuff.   We do it in the \emph{behind} foreground path so
% that we can ensure things are drawn the way we want it.
%
% The first thing is to set-up the clipping to the chit frame. 
%
%    \begin{macrocode}
  \behindforegroundpath{%
    \chit@dbg{1}{Chit drawing foreground path}
    % \chit@dbg{4}{%
    %   Chit foreground: \meaning\id
    %   ^^J  ID (set):    \meaning\chit@id
    %   ^^J  Symbol:      \meaning\chit@symbol
    %   ^^J  Full:        \meaning\chit@full
    %   ^^J  Factors:     \meaning\chit@factors
    %   ^^J  Left:        \meaning\chit@left
    %   ^^J  Right:       \meaning\chit@right
    %   ^^J  Upper left:  \meaning\chit@upper@left
    %   ^^J  Lower left:  \meaning\chit@lower@left
    %   ^^J  Upper right: \meaning\chit@upper@right
    %   ^^J  Lower right: \meaning\chit@lower@right
    %   ^^J  Extra:       \meaning\chit@extra
    %   ^^J  Bevel:       \meaning\chit@bevel
    %   ^^J  Frame:       \meaning\chit@frame}
    \chit@dbg{1}{Chit report}
    \chit@report{}
    \chit@dbg{1}{Chit start scope}
    \pgfscope
    %
    \ifchit@clip%
      \chit@dbg{1}{Chit clip path}
      \chit@bkg@p@th%
      % \northeast%
      % \wg@tmpa=\pgf@x\wg@tmpb=\pgf@y%
      % \pgfpathmoveto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
      % \wg@tmpa=-\wg@tmpa \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
      % \wg@tmpb=-\wg@tmpb \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
      % \wg@tmpa=-\wg@tmpa \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
      % \pgfclosepath%
      \pgfusepath{clip}%
    \fi%
%    \end{macrocode}
%
% If we do not have the \texttt{symbol} key set, then we set the
% \texttt{full} key as a picture. 
% 
%    \begin{macrocode}
    \@ifundefined{chit@symbol}{%
      %% Draw full stuff
      \@ifundefined{chit@full}{}{%
        \chit@dbg{1}{Chit draw full image: `\meaning\chit@full'}
        \center\wg@tmpa=\pgf@x\wg@tmpb=\pgf@y%
        \wg@pic@all{\chit@full}{}{\the\wg@tmpa,\the\wg@tmpb}{chit/full}}%
    }{% With NATO symbol
%    \end{macrocode}
%
%  Otherwise, we put in a node with shape \texttt{natoapp6c} and pass
%  the \texttt{symbol} key--value pairs as options. 
%
%    \begin{macrocode}
      \chit@dbg{1}{Chit draw symbol image}
      \edef\symid{\id symbol}%
      \symbol%
      \edef\args{{\symid}{\the\pgf@x,\the\pgf@y}\chit@symbol}%
      \chit@dbg{6}{Arguments to chit NATO symbol: \meaning\args}%
      \chit@dbg{1}{Chit draw nato image `\symid'}
      \expandafter\chit@n@to\args\@end@chit@n@to%
      \chit@dbg{6}{After making NATO symbol in chit}%
%    \end{macrocode}
%
% Having made the NATO App6(c) symbol, which we gave the node name
% \meta{id}\texttt{symbol} where \meta{id} is the ID of this chit, we
% can make the rest of the chit elements.   These are the left and
% right elements, which are set west and east of the symbol,
% respectively; the factors; and the four corner elements.
%
% If the respective elements have not been specified, we do not make
% them.
%
% First the left and right elements.  Note that these uses the anchors
% of the embedded \texttt{natoapp6c} node for placement.
%
%    \begin{macrocode}
      % Put in left of symbol
      \@ifundefined{chit@left}{}{%
        \chit@dbg{2}{Chit draw left: `\meaning\chit@left'}
        \wg@nchor{\thisname}{left}
        \begin{scope}[]
          \wg@pic@all{\chit@left}{}{\pgf@x,\pgf@y}{chit/left}%
        \end{scope}}%
      % Put in right of symbol
      \@ifundefined{chit@right}{}{%
        \chit@dbg{2}{Chit draw right: `\meaning\chit@right'}
        \wg@nchor{\thisname}{right}
        \begin{scope}[]
          \wg@pic@all{\chit@right}{}{\pgf@x,\pgf@y}{chit/right}%
        \end{scope}}%
%    \end{macrocode}
%
% Next, we want to put in the corner elements.   But before we do
% that, we use our saved anchors and dimensions to calculate the
% coordinates.  Note that the corner elements are anchored to the
% corners (plus margin) of the chit frame. 
%
%    \begin{macrocode}
      % Get coordinates
      \northeast%
      \wg@tmpa=\pgf@x%
      \wg@tmpb=\pgf@y%
      \advance\wg@tmpa-\margin%
      \advance\wg@tmpb-\margin%
%    \end{macrocode}
%
% With the coordinates extracted, we set the four corner
% elements. Note, for the anchoring to work, we should specify
% pictures that have anchors (e.g., nodes).  If not, we must take care
% to give offsets or the like. 
%
%    \begin{macrocode}
      % Put in upper left corner
      \@ifundefined{chit@upper@left}{}{%
        \chit@dbg{1}{Chit draw upper left: `\meaning\chit@upper@left'}%
        \wg@nchor{\thisname}{upper left}
        \begin{scope}[]
          \wg@pic@all{\chit@upper@left}{}{\pgf@x,\pgf@y}{chit/upper left}%
        \end{scope}}
      % Put in upper right corner
      \@ifundefined{chit@upper@right}{}{%
        \chit@dbg{1}{Chit draw upper right: `\meaning\chit@upper@right'}
        \wg@nchor{\thisname}{upper right}%
        \begin{scope}[]
          \wg@pic@all{\chit@upper@right}{}{\pgf@x,\pgf@y}{chit/upper right}%
        \end{scope}}
      % Put in lower left corner
      \@ifundefined{chit@lower@left}{}{%
        \chit@dbg{1}{Chit draw lower left: `\meaning\chit@lower@left'}
        \wg@nchor{\thisname}{lower left}%
        \begin{scope}[]
          \wg@pic@all{\chit@lower@left}{}{\pgf@x,\pgf@y}{chit/lower left}%
        \end{scope}}
      % Put in lower right corner
      \@ifundefined{chit@lower@right}{}{%
        \chit@dbg{1}{Chit draw lower right: `\meaning\chit@lower@right'}
        \wg@nchor{\thisname}{lower right}%
        \begin{scope}[]
          \wg@pic@all{\chit@lower@right}{}{\pgf@x,\pgf@y}{chit/lower right}%
        \end{scope}}
%    \end{macrocode}
%
% Finally, we put in the unit factors.   They are put at the bottom of
% the chit frame (plus margin) and are typically anchored to the south
% anchor of the element.  Note, we can put in several factors if need
% be. 
%
%    \begin{macrocode}
      % Put in factors
      \@ifundefined{chit@factors}{}{%
        \chit@dbg{1}{Chit draw factors: `\meaning\chit@factors'}
        \advance\wg@tmpb-\margin%
        \begin{scope}[]
          \wg@pic@all{\chit@factors}{}{0,-\the\wg@tmpb}{chit/factors}%
        \end{scope}}%
      % Put in extra
      \@ifundefined{chit@extra}{}{%
        \chit@dbg{1}{Chit draw extra: `\meaning\chit@extra'}
        \begin{scope}[]
          \wg@pic@all{\chit@extra}{}{0,0}{chit/factors}%
        \end{scope}}%
    }% End of full or symbol
    \endpgfscope%
    % Make bevel?
    \@ifundefined{chit@bevel}{\let\chit@bevel\empty}{}
    \ifx\chit@bevel\empty\else%
      \chit@dbg{1}{Chit draw bevel}
      %% South east bevel  
      \northeast%
      \wg@tmpa=-\pgf@x\wg@tmpb=-\pgf@y%
      \ifcase\chit@bevel\relax%
      \or% 1
      \or\wg@tmpa=-\wg@tmpa% 2
      \or\wg@tmpb=-\wg@tmpb% 3
      \or\wg@tmpa=-\wg@tmpa\wg@tmpb=-\wg@tmpb%4
      \fi
      \chit@bevel@path{chit/bevel highlight}
      %% North west bevel
      \northeast%
      \wg@tmpa=\pgf@x\wg@tmpb=\pgf@y%
      \ifcase\chit@bevel\relax%
      \or% 1
      \or\wg@tmpa=-\wg@tmpa% 2
      \or\wg@tmpb=-\wg@tmpb% 3
      \or\wg@tmpa=-\wg@tmpa\wg@tmpb=-\wg@tmpb%4
      \fi
      \chit@bevel@path{chit/bevel shadow}
    \fi
    % Draw frame?
    \chit@dbg{1}{Chit draw frame: `\meaning\chitframeopt'}
    \edef\tmp@opt{[chit/frame style/.cd,chit/frame/.try,\chitframeopt]}
    \chit@dbg{1}{Chit draw frame: `\meaning\tmp@opt}
    \expandafter\scope\tmp@opt
      \northeast%
      \wg@tmpa=\pgf@x\wg@tmpb=\pgf@y%
      \pgfpathmoveto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
      \wg@tmpa=-\wg@tmpa \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
      \wg@tmpb=-\wg@tmpb \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
      \wg@tmpa=-\wg@tmpa \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
      \pgfclosepath%
      \chit@dbg{3}{Line width for frame: `\the\pgflinewidth'}
      \ifchit@draw@frame\pgfusepath{stroke}\fi%
      \chit@draw@frametrue%
      %\iftikz@mode@fill\pgfusepath{fill}\fi%
    \endscope%
    \chit@dbg{1}{Chit end of shape}
  }
}
%    \end{macrocode}
%    
% \iffalse
% --------------------------------------------------------------------
% \fi
% \subsubsection{The \cs{chit} wrapper macro}
% 
% \begin{Macro}{\chit,\chit@,\chit@@}
%
%   The macro to make the chits.  This is a wrapper around a
%   \texttt{node} with shape \texttt{chit}.   The syntax of this macro
%   is 
%
%   \begin{Syntax}
%     \cs{chit}\oarg{chit options}\parg{position}\parg{identifier};
%   \end{Syntax}
%
%   Note that the trailing semi-colon is optional.  Here \meta{chit
%   options} are any key-value pairs in the \texttt{/chit} (and
%   \texttt{/tikz}) namespace. 
%   
%   The first macro parses for options. 
%
%    \begin{macrocode}
\def\chit{%
  \chit@dbg{5}{Chit}
  \@ifnextchar[{\chit@}{\chit@[]}%]
}
%    \end{macrocode}
% 
% Parse for coordinates. 
%
%    \begin{macrocode}
\def\chit@[#1]{%
  \chit@dbg{5}{Chit second: `#1'}
  \@ifnextchar({\chit@@{#1}}{\chit@@{#1}(0,0)}%)
}
%    \end{macrocode}
%
% Parse for name.
%
%    \begin{macrocode}
\def\chit@@#1(#2){%
  \@ifnextchar({\chit@@@{#1}{#2}}{\chit@@@{#1}{#2}()}%)
}
%    \end{macrocode}
%
% The work horse.  This simply makes a \cs{node} with the shape
% \texttt{chit}.   Note, we allow for a trailing semi-colon
% (\texttt{;}) to have a similar feel to other \TikZ{} macros.
%
% The macro will execute the style \texttt{/tikz/every chit} if
% defined.  Note that this will be executed \emph{before} the usual
% \texttt{every chip node} style. 
%
%    \begin{macrocode}
\def\chit@@@#1#2(#3){%
  \chit@dbg{5}{Chit final:
    ^^J  Options:    #1
    ^^J  Position:   #2
    ^^J  Name:       `#3'}
  \let\name\pgfutil@empty%
  \chit@dbg{1}{=== Before chit node}%
  \node[chit={/tikz/every chit/.try,id=#3,#1}] (tmp) at (#2) {};
  \chit@dbg{2}{=== After chit node}%
  \ifx|#3|\relax%
  \else%
    \chit@dbg{3}{=== Renaming chit to user defined name `#3'}%
    \pgfnoderename{#3}{tmp}%
  \fi%
  \@ifnextchar;{\@gobble}{}%
}
%    \end{macrocode}
% \end{Macro}
% 
% \iffalse
% --------------------------------------------------------------------
% \fi
% \subsubsection{The Kriegspiel option for chits}
%
% This will select a Kriegspiel-like chit layout. That is, if we pass
% \texttt{kriegspiel} as a keyword to the \texttt{chit} style, then we
% will make a Kriegspiel-like chit (oblong, other symbols).
%
%    \begin{macrocode}
\tikzset{
  /chit/kriegspiel/.code={
    \pgfkeys{%
      /tikz/shape=kriegspiel,%
      /chit/nato shape=kriegspiel symbol}
  }
}
%    \end{macrocode}
%
% Shape of a Kriegspiel like chit.  This mainly inherits from the
% \texttt{chit} shape but overrides a number of anchors.
%
%    \begin{macrocode}
\pgfdeclareshape{kriegspiel}{
  \inheritsavedanchors[from=chit]
  \savedanchor\northeast{\pgf@x=0.8cm\pgf@y=0.4cm}
  \savedanchor\symbol{\pgf@x=0cm\pgf@y=0.15cm}
  \savedanchor\factors{\pgf@x=.2cm\pgf@y=-.4cm}
  \savedmacro\thisname{\def\thisname{kriegspiel}}
  \saveddimen\margin{\pgf@x=0.03cm}
  \inheritanchor[from=chit]{center}
  \inheritanchor[from=chit]{north east}
  \inheritanchor[from=chit]{north west}
  \inheritanchor[from=chit]{south west}
  \inheritanchor[from=chit]{south east}
  \inheritanchor[from=chit]{north}
  \inheritanchor[from=chit]{south}
  \inheritanchor[from=chit]{east}
  \inheritanchor[from=chit]{west}
  \inheritanchor[from=chit]{symbol north east}
  \inheritanchor[from=chit]{symbol north west}
  \inheritanchor[from=chit]{symbol south east}
  \inheritanchor[from=chit]{symbol south west}
  \inheritanchor[from=chit]{symbol north}
  \inheritanchor[from=chit]{symbol west}
  \inheritanchor[from=chit]{symbol south}
  \inheritanchor[from=chit]{symbol east}
  \inheritanchor[from=chit]{symbol upper}
  \inheritanchor[from=chit]{symbol lower}
  \inheritanchor[from=chit]{symbol left}
  \inheritanchor[from=chit]{symbol right}
  \inheritanchor[from=chit]{symbol echelon}
  \inheritanchor[from=chit]{symbol below}
  \inheritanchor[from=chit]{symbol}
  \inheritanchor[from=chit]{factors}
  \inheritanchor[from=chit]{left}
  \inheritanchor[from=chit]{right}
  \inheritanchor[from=chit]{upper right}
  \inheritanchor[from=chit]{upper left}
  \inheritanchor[from=chit]{lower right} 
  \inheritanchor[from=chit]{lower left}
  % \anchor{upper right} {%
  %   \northeast%
  %   \advance\pgf@x-\margin%
  %   \advance\pgf@y-\margin%
  %   \pgf@y=-\pgf@y%
  %   \advance\pgf@y5pt%
  % }
  % \anchor{upper left}{
  %   \northeast%
  %   \advance\pgf@x-\margin%
  %   \advance\pgf@y-\margin%
  %   \pgf@x=-\pgf@x%
  %   \pgf@y=-\pgf@y%
  %   \advance\pgf@y5pt%
  % }
  %%
  \inheritbackgroundpath[from=chit]
  \inheritbehindforegroundpath[from=chit]
}
%    \end{macrocode}
% 
%    \begin{macrocode}
%    \end{macrocode}
% 
% \iffalse
% </chit>
% --------------------------------------------------------------------
% \fi
