% Copyright 2018 by Mark Wibrow and Till Tantau
%
% 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.

\usepgfmodule{decorations}%


%
% Path replacing decorations
%
% These decorations change the path by replacing it by another path.
% For instance, a line in the path might be replaced by small
% ticks or unconnected curves or crosses. Applying a chopping
% decoration to a path means that the path can no longer be used for
% filling in the original manner.
%



%
%
% Kind 1: Path chopping with open subpaths
%
%


% ticks decoration
%
% Parameters: \pgfdecorationsegmentlength, \pgfdecorationsegmentamplitude

\pgfdeclaredecoration{ticks}{ticks}
{%
  \state{ticks}[width=+\pgfdecorationsegmentlength]
  {
    \pgfpathmoveto{\pgfqpoint{0pt}{\pgfdecorationsegmentamplitude}}
    \pgfpathlineto{\pgfqpoint{0pt}{-\pgfdecorationsegmentamplitude}}
  }%
  \state{final}
  {
    \pgfpathmoveto{\pgfqpoint{0pt}{\pgfdecorationsegmentamplitude}}
    \pgfpathlineto{\pgfqpoint{0pt}{-\pgfdecorationsegmentamplitude}}
    \pgfpathmoveto{\pgfpointdecoratedpathlast}
  }%
}%



% expanding waves decoration
%
% Parameters: \pgfdecorationsegmentangle, \pgfdecorationsegmentlength

\pgfdeclaredecoration{expanding waves}{initial}
{%
  \state{initial}[width=+\pgfdecorationsegmentlength,next state=wave]
  {}%

  \state{wave}[switch if less than=+\pgfdecorationsegmentlength to last,
               width=+\pgfdecorationsegmentlength]
  {
    \pgfpathmoveto{
      \pgfpointadd
      {\pgfqpoint{-\pgfdecoratedcompleteddistance}{0pt}}%
      {\pgfpointpolar{\pgfdecorationsegmentangle}{+\pgfdecoratedcompleteddistance}}}%
    \pgfpatharc{\pgfdecorationsegmentangle}{-\pgfdecorationsegmentangle}{+\pgfdecoratedcompleteddistance}%
  }%
  \state{last}[width=+0pt,next state=final]
  {
    \pgfpathmoveto{
      \pgfpointadd
      {\pgfqpoint{-\pgfdecoratedcompleteddistance}{0pt}}%
      {\pgfpointpolar{\pgfdecorationsegmentangle}{+\pgfdecoratedcompleteddistance}}}%
    \pgfpatharc{\pgfdecorationsegmentangle}{-\pgfdecorationsegmentangle}{+\pgfdecoratedcompleteddistance}%
  }%
  \state{final}
  {
    \pgfpathmoveto{\pgfpointdecoratedpathlast}
  }%
}%



% waves decoration

\pgfdeclaredecoration{waves}{wave}
{%
  \state{wave}[width=\pgfdecorationsegmentlength]
  {
    \pgftransformxshift{+\pgfdecorationsegmentlength}
    \pgfpathmoveto{
      \pgfpointadd
      {\pgfqpoint{-\pgfkeysvalueof{/pgf/decoration/start radius}}{0pt}}%
      {\pgfpointpolar{\pgfdecorationsegmentangle}{\pgfkeysvalueof{/pgf/decoration/start radius}}}}%
    \pgfpatharc{\pgfdecorationsegmentangle}{-\pgfdecorationsegmentangle}{\pgfkeysvalueof{/pgf/decoration/start radius}}%
  }%
  \state{final}
  {
    \pgfpathmoveto{\pgfpointdecoratedpathlast}
  }%
}%



% border decoration
%
% Parameters: \pgfdecorationsegmentlength, \pgfdecorationsegmentamplitude, \pgfdecorationsegmentangle

\pgfdeclaredecoration{border}{tick}
{%
  \state{tick}[switch if less than=+\pgfdecorationsegmentlength to last,
               width=+\pgfdecorationsegmentlength]
  {
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathlineto{\pgfpointpolar{\pgfdecorationsegmentangle}{+\pgfdecorationsegmentamplitude}}
  }%
  \state{last}[width=+\pgfdecorationsegmentamplitude,next state=final]
  {
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathlineto{\pgfpointpolar{\pgfdecorationsegmentangle}{+\pgfdecorationsegmentamplitude}}
  }%
  \state{final}
  {
    \pgfpathmoveto{\pgfpointdecoratedpathlast}
  }%
}%






% brace decorations
%
% Parameters: \pgfdecorationsegmentamplitude

\pgfdeclaredecoration{brace}{brace}
{%
  \state{brace}[width=+\pgfdecoratedremainingdistance,next state=final]
  {
    \pgf@yc=\pgfdecorationsegmentaspect\pgfdecoratedremainingdistance
    \ifdim2\pgfdecorationsegmentamplitude>\pgf@yc
      \pgf@yc=0.5\pgf@yc
    \else
      \pgf@yc=\pgfdecorationsegmentamplitude
    \fi
    \pgf@xc=\pgfdecorationsegmentaspect\pgfdecoratedremainingdistance
    \advance\pgf@xc-\pgfdecoratedremainingdistance
    \ifdim-2\pgfdecorationsegmentamplitude<\pgf@xc
      \pgf@xc=-0.5\pgf@xc
    \else
      \pgf@xc=\pgfdecorationsegmentamplitude
    \fi
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathcurveto
    {\pgfqpoint{.15\pgf@yc}{.3\pgfdecorationsegmentamplitude}}
    {\pgfqpoint{.5\pgf@yc}{.5\pgfdecorationsegmentamplitude}}
    {\pgfqpoint{\pgf@yc}{.5\pgfdecorationsegmentamplitude}}
    {
      \pgftransformxshift{+\pgfdecorationsegmentaspect\pgfdecoratedremainingdistance}
      \pgfpathlineto{\pgfqpoint{-\pgf@yc}{.5\pgfdecorationsegmentamplitude}}
      \pgfpathcurveto
      {\pgfqpoint{-.5\pgf@yc}{.5\pgfdecorationsegmentamplitude}}
      {\pgfqpoint{-.15\pgf@yc}{.7\pgfdecorationsegmentamplitude}}
      {\pgfqpoint{0\pgf@yc}{1\pgfdecorationsegmentamplitude}}
      \pgfpathcurveto
      {\pgfqpoint{.15\pgf@xc}{.7\pgfdecorationsegmentamplitude}}
      {\pgfqpoint{.5\pgf@xc}{.5\pgfdecorationsegmentamplitude}}
      {\pgfqpoint{\pgf@xc}{.5\pgfdecorationsegmentamplitude}}
    }
    {
      \pgftransformxshift{+\pgfdecoratedremainingdistance}
      \pgfpathlineto{\pgfqpoint{-\pgf@xc}{.5\pgfdecorationsegmentamplitude}}
      \pgfpathcurveto
      {\pgfqpoint{-.5\pgf@xc}{.5\pgfdecorationsegmentamplitude}}
      {\pgfqpoint{-.15\pgf@xc}{.3\pgfdecorationsegmentamplitude}}
      {\pgfqpoint{0pt}{0pt}}
    }
  }%
  \state{final}
  {}%
}%




% show path construction decoration
%
\pgfdeclaredecoration{show path construction}{check for moveto}{%
    \state{check for moveto}[width=0pt, next state=segment,
      persistent precomputation={%
        \begingroup%
            \pgf@lib@decoraions@installinputsegmentpoints%
              \ifx\pgfdecorationpreviousinputsegment\pgfdecorationinputsegmentmoveto%
                  \pgfdecoratedinputsegmentmovetocode%
              \fi%
            \endgroup%
        }]{}%
    \state{segment}[width=\pgfdecoratedinputsegmentremainingdistance, next state=check for moveto,
      persistent precomputation={%
        \begingroup%
            \pgf@lib@decoraions@installinputsegmentpoints%
                \csname pgfdecoratedinputsegment\pgfdecorationcurrentinputsegment code\endcsname%
            \endgroup%
        }]{}%
    \state{final}{%
        \egroup%
        \begingroup%
        \pgf@lib@decoraions@installinputsegmentpoints%
            \csname pgfdecoratedinputsegment\pgfdecorationcurrentinputsegment code\endcsname%
        \endgroup%
        \bgroup%
    }%
}%

\pgfkeys{
    /pgf/decoration/.cd,
        moveto code/.code=\def\pgfdecoratedinputsegmentmovetocode{#1},
        lineto code/.code=\def\pgfdecoratedinputsegmentlinetocode{#1},
        curveto code/.code=\def\pgfdecoratedinputsegmentcurvetocode{#1},
      closepath code/.code=\def\pgfdecoratedinputsegmentclosepathcode{#1}
}%

\let\pgfdecoratedinputsegmentmovetocode\pgfutil@empty%
\let\pgfdecoratedinputsegmentlinetocode\pgfutil@empty%
\let\pgfdecoratedinputsegmentcurvetocode\pgfutil@empty%
\let\pgfdecoratedinputsegmentclosepathcode\pgfutil@empty%

\def\pgf@lib@decoraions@installinputsegmentpoints{%
    \let\pgfpointdecoratedinputsegmentfirst\pgf@decorate@inputsegment@first%
    \let\pgfpointdecoratedinputsegmentlast\pgf@decorate@inputsegment@last%
    \let\pgfpointdecoratedinputsegmentsupporta\pgf@decorate@inputsegment@supporta%
    \let\pgfpointdecoratedinputsegmentsupportb\pgf@decorate@inputsegment@supportb%
}%


\endinput
