%% pgflibrarynfold.code.tex
%% Copyright 2023 Jonathan Schulz
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
% http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008-05-04 or later.
%
% This work has the LPPL maintenance status 'maintained'.
%
% The Current Maintainer of this work is Jonathan Schulz.
%
% This work consists of the files
% pgflibrarybezieroffset.code.tex, pgflibrarynfold.code.tex, pgflibraryoffsetpath.code.tex, tikz-nfold-doc.tex, tikzlibrarynfold.code.tex, tikz-nfold-doc.tex, and tikz-nfold-doc.pdf.
%
%
% A commented version of this file can be found on https://github.com/jonschz/tikz-nfold .
%


\usepgflibrary{offsetpath}


\newcount\pgf@nfold@index
\def\pgf@nfold@run@loop{%
  \pgf@nfold@index=\pgf@nfold@order%
  \pgf@nfold@run@loop@%
}

\def\pgf@nfold@run@loop@{%
  \pgf@nfold@loop@inner%
  \advance\pgf@nfold@index by -1\relax
  \ifnum\pgf@nfold@index>0\relax%
    \pgf@nfold@run@loop@%
  \fi%
}

\def\pgf@nfold@loop@inner{%
  \pgfmathsetmacro{\pgf@nfold@shift@fraction}%
    {-1+2*(\pgf@nfold@index-1)/(\pgf@nfold@order-1)}%
  \pgfmathsetmacro{\pgf@nfold@shiftamount}{\pgf@nfold@hwidth*\pgf@nfold@shift@fraction}
  \pgftransformreset%
  \parsedpath%
  \pgfsyssoftpath@flushcurrentpath%
  \pgf@up@action%
}

\def\pgf@nfold@compute@widths@from@double{
    \pgf@x=\pgflinewidth\relax%
    \pgf@y=\pgf@x\relax%
    \advance\pgf@x-\pgfinnerlinewidth\relax%
    \advance\pgf@y+\pgfinnerlinewidth\relax%
    \pgf@x=.5\pgf@x\relax%
    \pgf@y=.25\pgf@y\relax%
}

\def\pgf@nfold@render@cached@softpath{%
  \pgfscope%
    \pgf@nfold@compute@widths@from@double%
    \pgfsetlinewidth\pgf@x%
    \edef\pgf@nfold@hwidth{\the\pgf@y}%
    \pgfprocessround{\pgf@nfold@cachedpath}{\pgf@nfold@cachedpath}%
    \pgf@nfold@parsearrows%
    \pgf@nfold@parsesoftpath{\pgf@nfold@cachedpath}{\parsedpath}%
    \pgf@nfold@run@loop%
  \endpgfscope%
}


\pgfkeys{
  /pgf/nfold/.code={%
    \pgf@nfold@order=#1\relax%
    \ifnum\pgf@nfold@order<1\relax%
      \pgfutil@packageerror{tikz-nfold}{The key /pgf/nfold must take a value of at least 1, got \the\pgf@nfold@order}{}%
    \fi%
    \ifnum\pgf@nfold@order>\pgf@nfold@intersec@numcached\relax
      \ifdefined\pgfintersectionofpaths\else
        \pgf@nfold@intersectionsnotloadedtrue
      \fi
    \fi
  },
  /pgf/nfold/.default=2
}

\endinput
