% -*- coding: utf-8 -*-
% ----------------------------------------------------------------------------
% Author:  Jianrui Lyu <tolvjr@163.com>
% Website: https://github.com/lvjr/concrete
% License: The LaTeX Project Public License 1.3c
% ----------------------------------------------------------------------------

\ProvidesPackage{beamerthemefermat}[2024-01-19 v2024A Beamer Theme Fermat]

\makeatletter

% compatible with old versions of beamer
\providecommand{\beamer@endinputifotherversion}[1]{}

\RequirePackage{tikz,etoolbox,adjustbox,varwidth,multicol}
\usetikzlibrary{decorations.pathmorphing}

\mode<presentation>

\setbeamersize{text margin left=8mm,text margin right=8mm}

%% ----------------- background canvas and background ----------------

\defbeamertemplate{background}{line}{%
  \begin{tikzpicture}
    \useasboundingbox (0,0) rectangle (\paperwidth,\paperheight);
    \draw[xstep=\paperwidth,ystep=1mm,color=tcolor] (0,0) grid (\paperwidth,\paperheight);
  \end{tikzpicture}%
}

\defbeamertemplate{background}{linear}{%
  \begin{tikzpicture}
    \useasboundingbox (0,0) rectangle (\paperwidth,\paperheight);
    \draw[pattern=horizontal lines, pattern color=tcolor]
      (0,0) rectangle (\paperwidth,\paperheight);
  \end{tikzpicture}%
}

\defbeamertemplate{background}{lattice}{%
  \begin{tikzpicture}
    \useasboundingbox (0,0) rectangle (\paperwidth,\paperheight);
    \draw[step=1mm,color=tcolor] (0,0) grid (\paperwidth,\paperheight);
  \end{tikzpicture}%
}

% need "text badly ragged" option for correct space skips
% see http://tex.stackexchange.com/a/132748/8956
\gdef\my@background@title
{%
  \begin{tikzpicture}
  \useasboundingbox (0,0) rectangle (\paperwidth,\paperheight);
  \fill[draw=dcolor,fill=fcolor]
    (0,0.25\paperheight) -- (0,0) -- (\paperwidth,0)
       -- (\paperwidth,0.25\paperheight) decorate[decoration={snake,segment length=3.15mm}] { -- cycle };
  \fill[draw=dcolor,fill=fcolor]
    (0,0.85\paperheight) -- (0,\paperheight) -- (\paperwidth,\paperheight)
       -- (\paperwidth,0.85\paperheight) decorate[decoration={snake,segment length=3.15mm}] { -- cycle };
  \node[text width=0.9\paperwidth,text badly ragged] at (0.5\paperwidth,0.925\paperheight) {%
    \begin{minipage}[c][0.15\paperheight]{0.9\paperwidth}%
      \centering
      \usebeamerfont{date}\usebeamercolor[fg]{date}\insertdate
    \end{minipage}
  };
  \node[text width=0.9\paperwidth,text badly ragged] at (0.5\paperwidth,0.55\paperheight) {%
    \begin{minipage}[c][0.6\paperheight]{0.9\paperwidth}
      \centering
      \usebeamerfont{title}\usebeamercolor[fg]{title}\inserttitle
      \ifx\insertsubtitle\@empty\else
        \\[5pt]\usebeamerfont{subtitle}\usebeamercolor[fg]{subtitle}
        \insertsubtitle
      \fi
    \end{minipage}
  };
  \node[text width=0.9\paperwidth,text badly ragged] at (0.5\paperwidth,0.125\paperheight) {%
    \begin{minipage}[c][0.25\paperheight]{0.9\paperwidth}
      \centering
      \usebeamerfont{author}\usebeamercolor[fg]{author}\insertauthor\\[3pt]
      \usebeamerfont{institute}\usebeamercolor[fg]{institute}\insertinstitute
    \end{minipage}
  };
  \end{tikzpicture}
}

\gdef\my@background@section
{%
  \begin{tikzpicture}
  \useasboundingbox (0,0) rectangle (\paperwidth,\paperheight);
  \fill[draw=dcolor,fill=fcolor]
    (0,0.12\paperheight) -- (0,0) -- (\paperwidth,0)
       -- (\paperwidth,0.12\paperheight) decorate[decoration={snake,segment length=3.15mm}] { -- cycle };
  \fill[draw=dcolor,fill=fcolor]
    (0,0.92\paperheight) -- (0,\paperheight) -- (\paperwidth,\paperheight)
       -- (\paperwidth,0.92\paperheight) decorate[decoration={snake,segment length=3.15mm}] { -- cycle };
  \node[text width=0.9\paperwidth,text badly ragged] at (0.5\paperwidth,0.52\paperheight) {%
    \begin{minipage}[c]{0.9\paperwidth}
      \usebeamertemplate{section page content}%
    \end{minipage}
  };
  \end{tikzpicture}
}

\gdef\my@background@subsection
{%
  \begin{tikzpicture}
  \useasboundingbox (0,0) rectangle (\paperwidth,\paperheight);
  \fill[draw=dcolor,fill=fcolor]
    (0,0.12\paperheight) -- (0,0) -- (\paperwidth,0)
       -- (\paperwidth,0.12\paperheight) decorate[decoration={snake,segment length=3.15mm}] { -- cycle };
  \fill[draw=dcolor,fill=fcolor]
    (0,0.92\paperheight) -- (0,\paperheight) -- (\paperwidth,\paperheight)
       -- (\paperwidth,0.92\paperheight) decorate[decoration={snake,segment length=3.15mm}] { -- cycle };
  \node[text width=0.9\paperwidth,text badly ragged] at (0.5\paperwidth,0.52\paperheight) {%
    \begin{minipage}[c]{0.9\paperwidth}
      \usebeamertemplate{subsection page content}%
    \end{minipage}
  };
  \end{tikzpicture}
}

%% -------------------------- title page -----------------------------

\defbeamertemplate{title page}{snake}
{%
  \nointerlineskip
  \begin{adjustbox}{width=\paperwidth,center}%
    \my@background@title
  \end{adjustbox}%
}

%% ----------------------- section and subsection --------------------

\patchcmd{\@vwid@sift}{\PackageWarning{varwidth}{Failed to reprocess entire contents}}{}{}{}

\def\sectionintocskip{0.5em}
\patchcmd{\beamer@sectionintoc}{\vskip1.5em}{\vskip\sectionintocskip}{}{}
% patch for centering toc with varwidth package
\patchcmd{\beamer@sectionintoc}{%
  \hbox{\vbox{%
    \def\beamer@breakhere{\\}%
    \beamer@tocact{\ifnum\c@section=#1\beamer@toc@cs\else\beamer@toc@os\fi}{section in toc}}}%
}{%
  \hbox{%
    \def\beamer@breakhere{}%
    \beamer@tocact{\ifnum\c@section=#1\beamer@toc@cs\else\beamer@toc@os\fi}{section in toc}}%
}{}{}
% patch for centering toc in pdflatex with varwidth package
\let\my@beamer@subsectionintoc=\beamer@subsectionintoc
\patchcmd{\my@beamer@subsectionintoc}{%
  \def\beamer@breakhere{\\}%
  \beamer@tocact{\ifnum\c@section=#1\ifnum\c@subsection=#2\beamer@toc@css\else\beamer@toc@oss\fi\else\beamer@toc@ooss\fi}
  {subsection in toc}%
}{%
  \def\beamer@breakhere{}%
  \ifnum\c@section=#1\ifnum\c@subsection=#2%
    \vskip.5em\hbox{\beamer@tocact\beamer@toc@css{subsection in toc}}%
  \else
    \vskip.5em\hbox{\beamer@tocact\beamer@toc@oss{subsection in toc}}%
  \fi\fi
}{}{}

% create new template for section
\defbeamertemplate{section}{simple}{%
  \insertsectionnumber.\hspace*{1ex}\insertsection%
}

\defbeamertemplate{at begin section}{normal}{%
  \frame{\sectionpage}
}
\defbeamertemplate{at begin section}{plain}{%
  \frame[plain]{\sectionpage}
}
\AtBeginSection[]{\usebeamertemplate{at begin section}}

\defbeamertemplate{section page}{snake}{%
  \nointerlineskip
  \begin{adjustbox}{width=\paperwidth,center}%
    \my@background@section
  \end{adjustbox}%
}

\defbeamertemplate{section page content}{simple}{
  \begin{beamercolorbox}[wd=\textwidth,center]{section title}%
    \usebeamerfont{section title}
    \usebeamertemplate{section}%
  \end{beamercolorbox}
}
\defbeamertemplate{section page content}{level}{
  \begin{center}
  \begin{varwidth}{\textwidth}
    \tableofcontents[sectionstyle=show/shaded,subsectionstyle=hide]
  \end{varwidth}
  \end{center}
}
\defbeamertemplate{section page content}{split}{
  \begin{multicols}{2}
    \tableofcontents[sectionstyle=show/shaded,subsectionstyle=shaded]
  \end{multicols}
}

% create new template for subsection
\defbeamertemplate{subsection}{simple}{%
  \insertsectionnumber.\insertsubsectionnumber\hspace*{2ex}\insertsubsection%
}

\defbeamertemplate{at begin subsection}{normal}{%
  \frame{\subsectionpage}
}
\defbeamertemplate{at begin subsection}{plain}{%
  \frame[plain]{\subsectionpage}
}
\AtBeginSubsection[]{\usebeamertemplate{at begin subsection}}

\defbeamertemplate{subsection page}{snake}{%
  \nointerlineskip
  \begin{adjustbox}{width=\paperwidth,center}%
    \my@background@subsection
  \end{adjustbox}%
}

\defbeamertemplate{subsection page content}{simple}{
  \begin{beamercolorbox}[wd=\textwidth,center]{subsection title}%
  \usebeamerfont{subsection title}%
  \usebeamertemplate{subsection}%
  \end{beamercolorbox}%
}
\defbeamertemplate{subsection page content}{level}{
  \begin{center}
  \let\beamer@subsectionintoc=\my@beamer@subsectionintoc
  \begin{varwidth}{\textwidth}
    \tableofcontents[sectionstyle=hide,subsectionstyle=show/shaded/hide]
  \end{varwidth}
  \end{center}
}
\defbeamertemplate{subsection page content}{split}{
  \begin{multicols}{2}
    \tableofcontents[sectionstyle=show/shaded,subsectionstyle=show/shaded]
  \end{multicols}
}

\defbeamertemplate{subsection in toc}{triangle}{%
  \leavevmode\leftskip=2.5em\scriptsize\raise1.25pt%
  \rlap{\hskip-1.5em\donotcoloroutermaths$\blacktriangleright$}%
  \normalsize\inserttocsubsection\par
}

%% ---------------------- headline and footline ----------------------

\newif\ifmy@subsection \my@subsectionfalse

\newcommand{\my@sectionentry}[5]{%
  \hfill
  \ifnum\c@section=#1%
    \usebeamercolor{footline highlight}%
  \else%
    \usebeamercolor{footline}%
  \fi%
  \hyperlink{Navigation#3}{%
    \colorbox{bg}{%
      \hspace{0.2em}\rule[-1ex]{0pt}{3.25ex}\color{fg}#2\hspace{0.2em}%
    }%
  }%
  \let\slideentry=\my@slideentry
}

\newcommand{\my@subsectionentry}[5]{\my@subsectiontrue}

\newcommand{\my@slideentry}[6]{%
  \ifmy@subsection
    \usebeamercolor{footline}%
    \def\my@box{$\circ$}%
    \ifnum\c@section=#1%
      \usebeamercolor{footline highlight}%
      \ifnum\c@subsection=#2%
        \def\my@box{$\bullet$}%
      \fi
    \fi
    \beamer@link(#4){%
      \colorbox{bg}{%
        \rule[-1ex]{0pt}{3.25ex}\hspace{0.1em}\color{fg}\my@box\hspace{0.1em}%
      }%
    }%
    \my@subsectionfalse
  \fi
}

\defbeamertemplate{footline}{progress}{%
  \begin{beamercolorbox}[wd=\paperwidth,ht=0.6pt,sep=0pt]{separator line}
  \end{beamercolorbox}
  \nointerlineskip
  \begin{beamercolorbox}[wd=\paperwidth,ht=2.25ex,dp=1ex]{footline}%
    \usebeamerfont{footline}%
    \kern\beamer@leftmargin
    \setlength{\fboxsep}{0pt}%
    \let\sectionentry=\my@sectionentry
    \let\beamer@subsectionentry=\my@subsectionentry
    \hskip 0pt plus -1fill\dohead
    \hskip 0pt plus 9fill\relax
    \Acrobatmenu{GoToPage}{\insertframenumber{}/\inserttotalframenumber}%
    \kern\beamer@rightmargin
  \end{beamercolorbox}%
}

%% ------------------------- frame title -----------------------------

\defbeamertemplate{frametitle}{simple}{%
  \nointerlineskip
  \begin{beamercolorbox}[wd=\paperwidth,sep=0pt,leftskip=\beamer@leftmargin,%
                         rightskip=\beamer@rightmargin]{frametitle}
    \usebeamerfont{frametitle}%
    \rule[-3.6mm]{0pt}{12mm}\insertframetitle\rule[-3.6mm]{0pt}{12mm}\par
  \end{beamercolorbox}
  \nointerlineskip
  \begin{beamercolorbox}[wd=\paperwidth,ht=0.6pt,sep=0pt]{separator line}
  \end{beamercolorbox}
}

%% --------------------------- font theme ----------------------------

\setbeamerfont{title}{size=\LARGE}
\setbeamerfont{subtitle}{size=\large}
\setbeamerfont{author}{size=\large}
\setbeamerfont{institute}{size=\normalsize}
\setbeamerfont{date}{size=\large}
\setbeamerfont{section in toc}{size=\large}
\setbeamerfont{subsection in toc}{size=\large}
\setbeamerfont{frametitle}{size=\large}
\setbeamerfont{block title}{size=\normalsize}

\usefonttheme{professionalfonts}
%\usepackage{arev}

%\setlength{\parskip}{5pt plus 1pt minus 1pt}

%% ---------------------------- color theme --------------------------

% always use rgb colors in pdf files
\substitutecolormodel{hsb}{rgb}

\definecolor{red99}{Hsb}{0,0.9,0.9}
\definecolor{brown49}{Hsb}{30,0.4,0.9}
\definecolor{yellow29}{Hsb}{60,0.2,0.9}
\definecolor{yellow86}{Hsb}{60,0.8,0.6}
\definecolor{cyan95}{Hsb}{180,0.9,0.5}
\definecolor{blue77}{Hsb}{240,0.7,0.7}
\definecolor{blue99}{Hsb}{240,0.9,0.9}
\definecolor{magenta88}{Hsb}{300,0.8,0.8}

\colorlet{text1}{black}
\colorlet{back1}{white}
\colorlet{accent1}{blue99}
\colorlet{accent2}{cyan95}
\colorlet{accent3}{red99}
\colorlet{accent4}{yellow86}
\colorlet{accent5}{magenta88}
\colorlet{filler1}{accent1!40!back1}
\colorlet{filler2}{accent2!40!back1}
\colorlet{filler3}{accent3!40!back1}
\colorlet{filler4}{accent4!40!back1}
\colorlet{filler5}{accent5!40!back1}
\colorlet{tcolor}{text1!10!back1}
\colorlet{dcolor}{cyan95}
\colorlet{fcolor}{brown49}

\setbeamercolor{normal text}{bg=yellow29,fg=black}
\setbeamercolor{structure}{fg=blue99}
\setbeamercolor{local structure}{fg=cyan95}
\setbeamercolor{footline}{bg=brown49,fg=black}
\setbeamercolor{footline highlight}{parent=footline,bg=filler2}
\setbeamercolor{separator line}{bg=accent2}
\setbeamercolor{title}{use=structure,fg=structure.fg}
\setbeamercolor{subtitle}{use={structure,normal text},fg=structure.fg!90!normal text.bg}
\setbeamercolor{section in toc}{parent=title}
\setbeamercolor{subsection in toc}{parent=subtitle}
\setbeamercolor{section in toc shaded}{parent=normal text}
\setbeamercolor{subsection in toc shaded}{parent=normal text}
\setbeamercolor{frametitle}{fg=text1,bg=brown49}
\setbeamercolor{theoremname}{parent=subtitle}
\setbeamercolor{proofname}{parent=subtitle}
\setbeamercolor{block title}{fg=accent1,bg=gray}
\setbeamercolor{block body}{bg=lightgray}
\setbeamercolor{block title example}{fg=accent2,bg=gray}
\setbeamercolor{block body example}{bg=lightgray}
\setbeamercolor{block title alerted}{fg=accent3,bg=gray}
\setbeamercolor{block body alerted}{bg=lightgray}

%% ------------------------ select templates -------------------------

\setbeamertemplate{background canvas}[default]
\setbeamertemplate{background}[line]
\setbeamertemplate{footline}[progress]
\setbeamertemplate{title page}[snake]
\setbeamertemplate{at begin section}[plain]
\setbeamertemplate{at begin subsection}[plain]
\setbeamertemplate{section page}[snake]
\setbeamertemplate{section page content}[level]
\setbeamertemplate{subsection page}[snake]
\setbeamertemplate{subsection page content}[level]
\setbeamertemplate{frametitle}[simple]
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{section}[simple]
\setbeamertemplate{subsection}[simple]
\setbeamertemplate{section in toc}[sections numbered]
\setbeamertemplate{subsection in toc}[subsections numbered]
\setbeamertemplate{section in toc shaded}[default][100]
\setbeamertemplate{subsection in toc shaded}[default][100]
\setbeamertemplate{itemize items}[default]
\setbeamertemplate{enumerate items}[default]

%% ----------------------- handout mode ------------------------------

\mode<handout>{
  \setbeamertemplate{background canvas}{}
  \setbeamertemplate{background}{}
  \colorlet{dcolor}{darkgray}
  \colorlet{fcolor}{white}
  \setbeamercolor{normal text}{fg=black, bg=white}
  \setbeamercolor{footline highlight}{bg=,fg=blue99}
  \setbeamercolor{title}{fg=blue}
  \setbeamercolor{subtitle}{fg=blue!70!gray}
  \setbeamercolor{section in toc}{fg=blue!90!gray}
  \setbeamercolor{subsection in toc}{fg=blue!80!gray}
  \setbeamercolor{section in toc shaded}{fg=lightgray}
  \setbeamercolor{subsection in toc shaded}{fg=lightgray}
  \setbeamercolor{frametitle}{fg=blue!70!gray,bg=}
  \setbeamercolor{theoremname}{fg=blue!60!gray}
  \setbeamercolor{proofname}{fg=blue!60!gray}
  \setbeamercolor{footline}{bg=white,fg=black}
}

\mode
<all>

\makeatother
