\ExplSyntaxOn

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Custom headers and footers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\bool_if:NT \l__rpg_layout_bool
  {
\RequirePackage {fancyhdr} % Adaptation of the footers

% Setup for custom footer
\pagestyle {fancy}

\cs_if_free:NF \chaptermark
  {
    \renewcommand*{\chaptermark}[1]
      {
        \markboth
          {
            \MakeUppercase
              {
                \ifnum\value{secnumdepth}>-1
                  \chaptertitlename\ \thechapter :~
                \fi
                #1
              }
          }
          {}
      }
  }

\renewcommand*{\headrulewidth}{0pt} % no rule for header
\renewcommand*{\footrulewidth}{0pt} % no rule for footer

\fancyhf {} % clear all headers and footers

% Header is used to include the page background
\fancyhead{
  \bool_if:NT \l__rpg_show_background_bool
    {
    \begin{tikzpicture}[remember~picture,overlay]
      \node[inner~sep=0pt] at (current~page.center) {\includegraphics[width=\paperwidth,height=\paperheight]{img/paper}};
    \end{tikzpicture}
    }
}

% Left-even page footer
\fancyfoot[LE]{%
    \RpgFontFooter \leftmark
    \begin{tikzpicture}[remember~picture,overlay]
      \bool_if:NT \l__rpg_show_footer_scroll_bool {
        \node[xscale=-1,inner~sep=0pt,anchor=south,nearly~opaque] at (current~page.south) {\includegraphics[width=\paperwidth,height=.6in]{img/footerscroll}};
      }
      \node[inner~sep=0pt,anchor=south,xshift=.28in,yshift=.39in] at (current~page.south~west) {\RpgFontPageNumber \thepage};
    \end{tikzpicture}
  }

% Right-odd page footer
\fancyfoot[RO]{%
    \RpgFontFooter \leftmark
    \begin{tikzpicture}[remember~picture,overlay]
      \bool_if:NT \l__rpg_show_footer_scroll_bool {
        \node[inner~sep=0pt,anchor=south,nearly~opaque] at (current~page.south) {\includegraphics[width=\paperwidth,height=.6in]{img/footerscroll}};
      }
      \node[inner~sep=0pt,anchor=south,xshift=-.28in,yshift=.39in] at (current~page.south~east) {\RpgFontPageNumber \thepage};
    \end{tikzpicture}
  }

\fancypagestyle {plain} {}

\fancypagestyle{empty}{%
  \fancyfoot{}
}

}
