% \iffalse
% --------------------------------------------------------------------
%<*utils>
% \fi
% 
% \subsubsection{Bounding boxes}
% 
%  Bounding box dimensions
%
%
%    \begin{macrocode}
\newdimen\wg@bb@minx
\newdimen\wg@bb@miny
\newdimen\wg@bb@maxx
\newdimen\wg@bb@maxy
%    \end{macrocode}
%
%
%
% Enable or disable bounding box tracking 
%
%    \begin{macrocode}
\newif\ifwg@notrelevantforpathsize\wg@notrelevantforpathsizefalse
%    \end{macrocode}
% 
%
% \begin{Macro}{wg@resetbb}
% Reset the bounding box tracking dimensions
%
% \begin{macrocode}
\def\wg@resetbb{%
  \global\wg@bb@minx=16000pt\relax%
  \global\wg@bb@miny=16000pt\relax%
  \global\wg@bb@maxx=-16000pt\relax%
  \global\wg@bb@maxy=-16000pt\relax%
}
%    \end{macrocode}
% \end{Macro}
%
% \begin{Macro}{\old@pgf@protocolsize}
%   Save PGF's bounding box algorithm
%   
%    \begin{macrocode}
\let\old@pgf@protocolsize\pgf@protocolsizes
%    \end{macrocode}
% \end{Macro}
%
% \begin{Macro}{\wg@protocolsizes}
%   Our bounding box algorithm
%
%    \begin{macrocode}
\def\wg@protocolsizes#1#2{%
  \old@pgf@protocolsize{#1}{#2}
  \ifwg@notrelevantforpathsize\else%
  \ifdim#1<\wg@bb@minx\global\wg@bb@minx#1\fi%
  \ifdim#1>\wg@bb@maxx\global\wg@bb@maxx#1\fi%
  \ifdim#2<\wg@bb@miny\global\wg@bb@miny#2\fi%
  \ifdim#2>\wg@bb@maxy\global\wg@bb@maxy#2\fi%
  \fi
}
%    \end{macrocode}
% \end{Macro}
%
% % \begin{environment}{getbbl}
%   Environment that tracks the local bounding box
%
%    \begin{macrocode}
\newenvironment{getbbl}{%
  \wg@resetbb%
  \wg@notrelevantforpathsizefalse%
  \global\let\pgf@protocolsizes\wg@protocolsizes}{%
  \gdef\pgf@sh@ns@L{rectangle}
  \gdef\pgf@sh@np@L{%
    \def\southwest{\pgfqpoint{\the\wg@bb@minx}{\the\wg@bb@miny}}%
    \def\northeast{\pgfqpoint{\the\wg@bb@maxx}{\the\wg@bb@maxy}}%
  }
  \gdef\pgf@sh@nt@L{{1}{0}{0}{1}{0pt}{0pt}}
  \gdef\pgf@sh@pi@L{\pgfpictureid}
  \global\let\pgf@protocolsizes\old@pgf@protocolsize
}
%    \end{macrocode}
% \end{environment}
% 
% \begin{environment}{getbb}
%   Environment to track global bounding box
%   
%    \begin{macrocode}
\newenvironment{getbb}{%
  \wg@resetbb%
  \wg@notrelevantforpathsizefalse%
  \global\let\pgf@protocolsizes\wg@protocolsizes}{%
  \gdef\pgf@sh@ns@M{rectangle}
  \gdef\pgf@sh@np@M{%
    \def\southwest{\pgfqpoint{\the\wg@bb@minx}{\the\wg@bb@miny}}%
    \def\northeast{\pgfqpoint{\the\wg@bb@maxx}{\the\wg@bb@maxy}}%
  }
  \gdef\pgf@sh@nt@M{{1}{0}{0}{1}{0pt}{0pt}}
  % \pgfgettransform\pgf@temp%
  % \xdef\pgf@sh@nt@M{\pgf@temp}
  % \pgfgettransformentries{\wg@tmp@a}{\wg@tmp@b}{\wg@tmp@c}{\wg@tmp@d}{\pgf@temp}{\pgf@temp}
  % \message{^^JTransform of M: \meaning\pgf@temp}
  % \xdef\pgf@sh@nt@M{{\wg@tmp@a}{\wg@tmp@b}{\wg@tmp@c}{\wg@tmp@d}{0pt}{0pt}}%
  % \message{^^JTransform of M: \meaning\pgf@sh@nt@M}
  \gdef\pgf@sh@pi@M{\pgfpictureid}
  \global\let\pgf@protocolsizes\old@pgf@protocolsize
}
%    \end{macrocode}
% \end{environment}
% 
% \iffalse
% --------------------------------------------------------------------
% \fi
% 
% \subsubsection{Some utilities to get bounding boxes and the like}
%
%
% All coordinates, and such are recorded in centimetres.  It is worth
% remembering that the Tikz coordinate system has the $y$ axis point
% upward, while typical image software has the $y$ axis point down.
% \texttt{pdftocairo} typically assumes a 150 PPI (pixels-per-inch)
% resolution.
%
% That means that scaling factor becomes
%
% $$
% \frac{150\mathrm{pixel}}{2.54\mathrm{cm}} =
% 59.055\frac{\mathrm{pixel}}{\mathrm{cm}}
% $$
% 
% \iffalse Using definition in terms of printers feet - the one to
% use!
% 
% PNG: 1674 x 1101
% PDF:   "lower left": [-0.02107,-0.02107],
%        "upper right": [28.31705,18.60843]
% Width: 28.31705+0.02107 = 28.33812
% Height: 18.60843+0.02107 = 18.62950
% Pixel / cm: 1674 / 28.33812 = 59.07237318495369488166
%             1101 / 18.62950 = 59.09981480984460130438
%             Average         = 59.08609399739914809302
%          
% Calculated = 150 / 2.54 = 59.05511811023622047244
%
% Using 1/72.27
% 
% "lower left": [-0.02109,-0.02109],
%  "upper right": [28.321,18.61102]
%  Width: 28.321+0.02109=28.34209
%  Height: 18.61102+0.02109=18.63211
% Pixel / cm: 1674 / 28.34209 = 59.06409866033168337267
%             1101 / 18.63211 = 59.09153606328000425072
%             Average         = 59.07781736180584381169
% \fi
% 
% Since we want to write all dimensions in centimetres, we need to be
% able to convert \texttt{pt} dimensions to centimetres.  We make two
% macros to do that for us.
%
% The exact definition of 1pt is
%
% $$1\,\mathrm{pt} = \frac{249}{250}12"\frac{1}{864}=\frac{83}{6000}1"
% = 0.03513\overline{6}$$ 
% 
%    \begin{macrocode}
% 2.54 / 72.27 = .03514598035145980351
% \def\wg@pt@to@cm#1{\pgfmathparse{#1 * 0.0351460}}
\def\wg@pt@to@cm#1{\pgfmathparse{#1 * 0.0351367}}
\def\ptpoint@to@cm#1#2{%
  \wg@pt@to@cm{#1}\edef\x{\pgfmathresult}%
  \wg@pt@to@cm{#2}\edef\y{\pgfmathresult}}
%    \end{macrocode}
%
% The next macro gets an anchors coordinates and stores them (in units
% of centimetres) in \cs{tmp@x} and \cs{tmp@y}
%
%    \begin{macrocode}
\def\wg@get@nchor#1#2{%
  \wg@dbg{2}{Get anchor coordinates #1.#2}
  \pgfpointanchor{#1}{#2}%
  \wg@dbg{2}{ `\the\pgf@x',`\the\pgf@y'}
  \pgfgetlastxy\tmp@x\tmp@y%
  \wg@dbg{2}{ `\tmp@x',`\tmp@y'}
  \wg@pt@to@cm{\tmp@x}\edef\tmp@x{\pgfmathresult}
  \wg@pt@to@cm{\tmp@y}\edef\tmp@y{\pgfmathresult}
}
%    \end{macrocode}
%
% This does the same as above, but transform to the global coordinate
% system.
% 
%    \begin{macrocode}
\def\wg@get@global@nchor#1#2{%
  \pgfpointanchor{#1}{#2}%
  \pgfgetlastxy\tmp@x\tmp@y%
  \pgfpointtransformed{\pgfpoint{\tmp@x}{\tmp@y}}
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  %% \message{^^JAnchor #1.#2 @ (\the\pgf@xa,\the\pgf@ya)}
  \wg@pt@to@cm{\the\pgf@xa}\edef\tmp@x{\pgfmathresult}
  \wg@pt@to@cm{\the\pgf@ya}\edef\tmp@y{\pgfmathresult}
}
%    \end{macrocode}
%
% This records the bounding box given by a named node.  The result is
% stored in the macros \cs{llx}, \cs{lly}, \cs{urx}, and \cs{ury}. 
% 
%    \begin{macrocode} 
\def\wg@get@bb#1{%
  \wg@get@nchor{#1}{south west}
  \edef\llx{\tmp@x}
  \edef\lly{\tmp@y}
  \wg@get@nchor{#1}{north east}
  \edef\urx{\tmp@x}
  \edef\ury{\tmp@y}
}
\def\wglogbb#1{%
  \wg@get@bb{#1}%
  \message{^^J`#1' BB: (\llx,\lly) x (\urx,\ury)^^J}} 
%    \end{macrocode}
%
%
% 
% \iffalse
% --------------------------------------------------------------------
%</utils>
%\fi