% ku-frontpage.sty
%------------------------------------------------------------------------------
% Copyright (c) 2021 by M.O.Larsen.
% Copyright (c) 2021 Malte Leip <malte@leip.net>
%
% Distributed under the terms of the MIT License:
% -----------------------------------------------------------------------------
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
% SOFTWARE.
% -----------------------------------------------------------------------------
%
%
% LaTeX style for front pages adhering to University of Copenhagen's design
% guide (http://designguide.ku.dk/) for SCIENCE, SUND, and SAMF faculty, and
% main KU-style.
%
% Original author:
%   Mads Ohm Larsen <mads@ohmbrix.dk>
%
% Inspired by Christian Aastrups package 'ku-forside' from 2007
% (http://www.math.ku.dk/~m00cha/), however translated all call to their
% English equivalant and included newer logos.
%
% Layout inspired by Joshua Hunt's PhD thesis.
%------------------------------------------------------------------------------
\ProvidesPackage{ku-frontpage}[2021/11/04 v1.1 Frontpages with University of Copenhagen logos]

%%% Define 'if''s for language.
\newif\if@kuenglish                     \DeclareOption{english}{\@kuenglishtrue}
\newif\if@kudanish                      \DeclareOption{danish}{\@kudanishtrue}

%%% Define 'if''s for faculties.
\newif\if@science                       \DeclareOption{science}{\@sciencetrue}
\newif\if@sund                          \DeclareOption{sund}{\@sundtrue}
\newif\if@samf                          \DeclareOption{samf}{\@samftrue}
\newif\if@ku                            \DeclareOption{ku}{\@kutrue}

%%% Define 'if' for submission statement as required by PhD regulations.
\newif\if@submissionstatement\@submissionstatementfalse
\DeclareOption{submissionstatement}{\@submissionstatementtrue}

%%% Define 'if''s for other things.
\newif\if@hyperref                      \DeclareOption{hyperref}{\@hyperreftrue}
\newif\if@dropcaps                      \DeclareOption{dropcaps}{\@dropcapstrue}

%%% Warning about unknown options
\DeclareOption*{
  \ClassWarning{ku-frontpage}{Unknown option '\CurrentOption'}
}

%%% Define standard values.
\ExecuteOptions{english}
\ExecuteOptions{science}

%%% No more options.
\ProcessOptions\relax

%%% Parse options.
\if@kuenglish   \def\KULANG{english}    \fi
\if@kudanish    \def\KULANG{danish}     \fi

\if@science     \def\KUFACULTY{science} \fi
\if@sund        \def\KUFACULTY{sund}    \fi
\if@samf        \def\KUFACULTY{samf}    \fi
\if@ku          \def\KUFACULTY{ku}      \fi

%%% Some packages are required for this package.
% Absolutely positioned graphics - LPPL 1.2
\RequirePackage{eso-pic}
% Absolutely positioned text - LPPL 1.3
\RequirePackage[absolute]{textpos}
% Graphics stuff, defining colors, etc. - LPPL 1.3c
\RequirePackage{graphicx}
% For \ifthenelse - LPPL 1.3c
\RequirePackage{ifthen}
% For patching commands - LPPL 1.3
\RequirePackage{xpatch}
% For \ifdefstring - LPPL 1.3c
\RequirePackage{etoolbox}

%%% Define colors
\definecolor{KU}{HTML}{901A1E}
\definecolor{KUscience}{HTML}{46743C}
\definecolor{KUsund}{HTML}{2A216A}

%%% Since 2020 the red KU-logo and color should be used rather than
%%% faculty logos and colors.
\def\KUCOLOR{KU}

%%% Define the page to have 12 horizontal modules and 24 vertical modules.
\TPGrid{12}{24}

%%% Define custom methods for frontpage.
\def\KU@ASSIGNMENT{Use \texttt{$\backslash$assignment$\lbrace \ldots \rbrace$}}
\def\KU@AUTHOR{Use \texttt{$\backslash$author$\lbrace \ldots \rbrace$}}
\def\KU@TITLE{Use \texttt{$\backslash$title$\lbrace \ldots \rbrace$}}
\def\KU@SUBTITLE{Use \texttt{$\backslash$subtitle$\lbrace \ldots \rbrace$}}
\def\KU@ADVISOR{Use \texttt{$\backslash$advisor$\lbrace \ldots \rbrace$}}
\def\KU@DATE{Use \texttt{$\backslash$date$\lbrace \ldots \rbrace$}}
\def\KU@FRONTPAGEIMAGE{}

%%% Construct the correct submission statement.
\if@submissionstatement
  \def\KU@SUBMISSIONSTATEMENT{
    Use \texttt{$\backslash$submissionstatement$\lbrace \ldots \rbrace$}
  }
  \ifdefstring{\KUFACULTY}{science}
  {
    % See https://www.science.ku.dk/phd/rules/
    % Section 5.1 of the rules, under "Formal requirements to the PhD thesis"
    \def\KU@SUBMISSIONSTATEMENT{%
      This thesis has been submitted to
      the PhD School of The Faculty of Science, University of Copenhagen%
    }
  }{}
  \ifdefstring{\KUFACULTY}{sund}
  {
    % See https://healthsciences.ku.dk/phd/guidelines/
    % Section 5.1 of the rules, under "Specific requirements"
    \def\KU@SUBMISSIONSTATEMENT{%
      This thesis has been submitted to the Graduate School
      of the Faculty of Health and Medical Sciences,\\ University of Copenhagen%
    }
  }{}
\else
  \def\KU@SUBMISSIONSTATEMENT{}
\fi

%%% Make sure the commands we want to patch exist.
%%% For example, some classes (like scrbook) define \subtitle, others do not.
\providecommand{\author}[1]{}
\providecommand{\title}[1]{}
\providecommand{\date}[1]{}
\providecommand{\assignment}[1]{}
\providecommand{\subtitle}[1]{}
\providecommand{\advisor}[1]{}
\providecommand{\frontpageimage}[1]{}
\providecommand{\submissionstatement}[1]{}

%%% Patch commands
\xpretocmd{\author}{\def\KU@AUTHOR{#1}}{}{\PackageWarning{ku-frontpage}{Tried to patch the 'author' command, but failed. Instead you will have to use the 'frontpageauthor' command to set the author for the frontpage, and the 'author' command to set the author for the rest of the document. See the readme for more information.}{} \newcommand{\frontpageauthor}[1]{\def\KU@AUTHOR{#1}} \def\KU@AUTHOR{Use \texttt{$\backslash$frontpageauthor$\lbrace \ldots \rbrace$}} }
\xpretocmd{\title}{\def\KU@TITLE{#1}}{}{\PackageWarning{ku-frontpage}{Tried to patch the 'title' command, but failed. Instead you will have to use the 'frontpagetitle' command to set the title for the frontpage, and the 'title' command to set the title for the rest of the document. See the readme for more information.}{} \newcommand{\frontpagetitle}[1]{\def\KU@TITLE{#1}} \def\KU@TITLE{Use \texttt{$\backslash$frontpagetitle$\lbrace \ldots \rbrace$}} }
\xpretocmd{\date}{\def\KU@DATE{#1}}{}{\PackageWarning{ku-frontpage}{Tried to patch the 'date' command, but failed. Instead you will have to use the 'frontpagedate' command to set the date for the frontpage, and the 'date' command to set the date for the rest of the document. See the readme for more information.}{} \newcommand{\frontpagedate}[1]{\def\KU@DATE{#1}} \def\KU@DATE{Use \texttt{$\backslash$frontpagedate$\lbrace \ldots \rbrace$}} }
\xpretocmd{\subtitle}{\def\KU@SUBTITLE{#1}}{}{\PackageWarning{ku-frontpage}{Tried to patch the 'subtitle' command, but failed. Instead you will have to use the 'frontpagesubtitle' command to set the title for the frontpage, and the 'subtitle' command to set the subtitle for the rest of the document. See the readme for more information.}{} \newcommand{\frontpagesubtitle}[1]{\def\KU@SUBTITLE{#1}} \def\KU@SUBTITLE{Use \texttt{$\backslash$frontpagesubtitle$\lbrace \ldots \rbrace$}} }

\xpretocmd{\assignment}{\def\KU@ASSIGNMENT{#1}}{}{\PackageError{ku-frontpage}{Tried to patch assignment command, but failed.}{}}
\xpretocmd{\advisor}{\def\KU@ADVISOR{#1}}{}{\PackageError{ku-frontpage}{Tried to patch advisor command, but failed.}{}}
\xpretocmd{\frontpageimage}{\def\KU@FRONTPAGEIMAGE{#1}}{}{\PackageError{ku-frontpage}{Tried to patch frontpageimage command, but failed.}{}}
\xpretocmd{\submissionstatement}{\def\KU@SUBMISSIONSTATEMENT{#1}}{}{\PackageError{ku-frontpage}{Tried to patch submissionstatement command, but failed.}{}}

%%% Include hyperref for PDF attributes.
\if@hyperref
  \RequirePackage{hyperref} % LPPL 1.3
  \newcommand{\kupdfsetup}[3]{
    \hypersetup{
      pdftitle={#1},
      pdfsubject={#2},
      pdfauthor={#3},
      colorlinks=true,
      citecolor={\KUCOLOR},
      linkcolor={\KUCOLOR},
      urlcolor={\KUCOLOR},
    }
  }
\fi

%%% Define drop caps
\if@dropcaps
  \RequirePackage{lettrine} % LPPL 1.3

  \renewcommand\LettrineFontHook{\color{\KUCOLOR}}
  \renewcommand{\DefaultOptionsFile}{NoFileOnPurposeHack.cfg}

  \LettrineOptionsFor{A}{loversize= 0.05,lhang= 0.10,findent= 0.000\LettrineWidth,nindent= 0.50em}
  \LettrineOptionsFor{B}{loversize= 0.10,lhang= 0.30,findent= 0.025\LettrineWidth,nindent= 0.50em}
  \LettrineOptionsFor{C}{loversize= 0.00,lhang= 0.15,findent= 0.175\LettrineWidth,nindent= 0.50em}
  \LettrineOptionsFor{D}{loversize= 0.10,lhang= 0.30,findent=-0.025\LettrineWidth,nindent= 0.50em}
  \LettrineOptionsFor{E}{loversize= 0.10,lhang= 0.10,findent= 0.100\LettrineWidth,nindent= 0.50em}
  \LettrineOptionsFor{F}{loversize= 0.10,lhang= 0.30,findent= 0.100\LettrineWidth,nindent= 0.50em}
  \LettrineOptionsFor{G}{loversize=-0.08,lhang= 0.10,findent= 0.000\LettrineWidth,nindent= 0.50em}
  \LettrineOptionsFor{H}{loversize=-0.01,lhang= 0.00,findent= 0.040\LettrineWidth,nindent= 0.50em}
  \LettrineOptionsFor{I}{loversize= 0.10,lhang= 0.00,findent= 0.050\LettrineWidth,nindent= 0.50em}
  \LettrineOptionsFor{J}{loversize=-0.08,lhang= 0.00,findent=-0.025\LettrineWidth,nindent= 0.50em}
  \LettrineOptionsFor{K}{loversize=-0.05,lhang= 0.00,findent=-0.050\LettrineWidth,nindent= 0.60\LettrineWidth}
  \LettrineOptionsFor{L}{loversize= 0.00,lhang= 0.00,findent= 0.100\LettrineWidth,nindent= 0.40\LettrineWidth}
  \LettrineOptionsFor{M}{loversize= 0.10,lhang= 0.40,findent= 0.040\LettrineWidth,nindent= 0.10\LettrineWidth}
  \LettrineOptionsFor{N}{loversize= 0.00,lhang= 0.20,findent=-0.010\LettrineWidth,nindent= 0.40\LettrineWidth}
  \LettrineOptionsFor{O}{loversize= 0.10,lhang= 0.20,findent=-0.010\LettrineWidth,nindent= 0.50em}
  \LettrineOptionsFor{P}{loversize= 0.10,lhang= 0.30,findent= 0.050\LettrineWidth,nindent= 0.50em}
  \LettrineOptionsFor{Q}{loversize= 0.00,lhang= 0.20,findent= 0.050\LettrineWidth,nindent= 0.70\LettrineWidth}
  \LettrineOptionsFor{R}{loversize=-0.05,lhang= 0.25,nindent= 0.500\LettrineWidth,nindent= 0.50\LettrineWidth}
  \LettrineOptionsFor{S}{loversize= 0.10,lhang= 0.00,findent= 0.000\LettrineWidth,nindent= 0.50em}
  \LettrineOptionsFor{T}{loversize= 0.10,lhang= 0.35,findent= 0.100\LettrineWidth,nindent= 0.50em}
  \LettrineOptionsFor{U}{loversize= 0.10,lhang= 0.20,findent= 0.050\LettrineWidth,nindent= 0.50em}
  \LettrineOptionsFor{V}{loversize= 0.10,lhang= 0.40,findent= 0.050\LettrineWidth,nindent= 0.50em}
  \LettrineOptionsFor{W}{loversize= 0.10,lhang= 0.30,findent= 0.040\LettrineWidth,nindent= 0.50em}
  \LettrineOptionsFor{X}{loversize= 0.10,lhang= 0.40,findent= 0.040\LettrineWidth,nindent= 0.20\LettrineWidth}
  \LettrineOptionsFor{Y}{loversize= 0.10,lhang= 0.20,findent= 0.100\LettrineWidth,nindent= 0.20\LettrineWidth}
  \LettrineOptionsFor{Z}{loversize=-0.05,lhang= 0.00,findent= 0.050\LettrineWidth,nindent= 0.55\LettrineWidth}
\fi

%%% Construct the titlepage
\def\KUbold{\fontseries{bx}\selectfont}
\def\KUsemibold{\fontseries{sb}\selectfont}

\def\maketitle{
  \thispagestyle{empty}
  \AddToShipoutPicture*{\put( 0, -35){\includegraphics*{ku-frontpage/logos/ku-logo}}}
  \AddToShipoutPicture*{\put(-8,   8){\includegraphics*{ku-frontpage/logos/\KUFACULTY-\KULANG}}}

  \ifthenelse{\equal{\KU@FRONTPAGEIMAGE}{}}{}{
    \AddToShipoutPicture*{
      \AtPageUpperLeft{\raisebox{-185mm}{\hspace{55pt}\includegraphics*[width=\textwidth, height=100mm, keepaspectratio]{\KU@FRONTPAGEIMAGE}}}
    }
  }

  \begin{textblock}{10}(1.14, 15.9)
    \noindent\fontsize{16pt}{18pt}\KUsemibold\KU@ASSIGNMENT
  \end{textblock}

  \begin{textblock}{10}(1.14, 16.9)
    \noindent\fontsize{28pt}{30pt}\KUsemibold\KU@TITLE
    \par\vspace{2mm}
    \noindent\fontsize{14pt}{16pt}\KUsemibold\KU@SUBTITLE
    \par\vspace{4mm}
    \noindent\fontsize{16pt}{18pt}\KUsemibold\KU@AUTHOR
  \end{textblock}

  \begin{textblock}{10}(1.14, 20.5)
    \begingroup
      \fontsize{11pt}{12pt}\KUsemibold
      \noindent\KU@ADVISOR
      \par\vspace{4mm}
      \noindent\KU@DATE
    \endgroup
    \par\vspace{4mm}
    \fontsize{11pt}{12pt}
    \noindent\KU@SUBMISSIONSTATEMENT
  \end{textblock}

  \phantom{...}
  \newpage
  \noindent
}
