%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% nova/itqb/schl-defaults.clo
%% NOVA thesis document template
%%
%% This work is licensed under the
%% The LaTeX project public license (LPPL), version 1.3c
%% To view a copy of this license, visit
%% https://www.latex-project.org/lppl/lppl-1-3c/
%%
%% Authors / Contributors:
%%      - João Lourenço <joao.lourenco@itqb.unl.pt>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\typeout{NT FILE nova/itqb/schl-defaults.clo}%


%%=======================================================================
%% METADATA & GLOBAL PROPERTIES
%%=======================================================================

% All LaTeX engines (pdfLaTeX, XeLaTeX, LuaLaTeX) are accepted
\AddValidProcessor{nova/itqb}{pdf,lua,xe}

\ifoptiondefined{/novathesis/nova/itqb/cover/color}{}{
  % NOVA/ITQB
  \NTOptNewChoice{nova/itqb/cover/color}{gray, green}{gray}
}

% ITQB-NOVA specific customization
% 'keepmargins' is essential: \newstocksize is a wrapper around \newgeometry,
% so without it the new layout resets the margins to geometry's own defaults for
% b5paper -- asymmetric and alternating (inner ~3.17cm / outer ~2.11cm) -- and
% the \SetMargin(screen|paper,...) values below are silently discarded.  This
% hook runs at begindocument/end, i.e. AFTER \ntsetlayout has applied those
% margins at \AtEndPreamble, so it is the last word on the page layout.
% 'keepmargins' re-applies the current \Gm@?margin values on top of the new
% stock size, giving b5paper WITH the requested 3.0cm/3.0cm margins.
\AddToHook{begindocument/end}{%
  \newstocksize{layout=b5paper,keepmargins}%
}

\openany
\ntsetup{print/frontpage=true}


%%=======================================================================
%% STRINGS (UNIVERSITY & SCHOOL)
%%=======================================================================

\NewMemStore{Oeiras}
\SetOeiras()={\mbox{O}eiras}

% Faculty / School
\SetSchool(pt)={Instituto de Tecnologia Química e Biológica António Xavier}
\SetSchool(en)={Instituto de Tecnologia Química e Biológica António Xavier}
\SetSchool(fr)={Instituto de Tecnologia Química e Biológica António Xavier}
\SetSchool(it)={Instituto de Tecnologia Química e Biológica António Xavier}
\SetSchool(address)={\textbf{ITQB NOVA, Avenida da República, 2780-157 \GetOeiras(), Portugal} $|$ Tel +351 214 469 100 $|$ itqb.unl.pt}

\SetSchool(logo,neg)={nova-itqb-logo-mono}

\SetDissertationString(phd,en)={Dissertation presented to obtain the \textbf{Ph.D degree in}}
\SetDissertationString(phd,pt)={Dissertação apresentada para obter o \textbf{grau de Doutor em}}
\SetDissertationString(msc,en)={Dissertation presented to obtain the \textbf{M.Sc degree in}}
\SetDissertationString(msc,pt)={Dissertação apresentada para obter o \textbf{grau de Mestre em}}


%%=======================================================================
%% COMMITTEE
%%=======================================================================

\SetCommitteeOrder()={c,r,a,m}%


%%=======================================================================
%% FONTS
%%=======================================================================

% Loading of fonts must be delayed until class "novathesis" is loaded
\AtEndPreamble{%
  \RequirePackage{gillius2}%
}


%%=======================================================================
%% LAYOUT (MARGINS & REGIONS)
%%=======================================================================

% Set margins for "normal" pages
\SetMargin(screen,top)={3.5cm}
\SetMargin(screen,bottom)={2.5cm}
\SetMargin(screen,left)={3.0cm}
\SetMargin(screen,right)={3.0cm}
\SetMargin(paper,top)={3.5cm}
\SetMargin(paper,bottom)={2.5cm}
\SetMargin(paper,left)={3.0cm}
\SetMargin(paper,right)={3.0cm}

% Set margins for cover pages
\SetMargin(cover,top)={1.5cm}
\SetMargin(cover,bottom)={0.8cm}
\SetMargin(cover,left)={1.7cm}
\SetMargin(cover,right)={1.7cm}

\NewMemStore{Region}
\SetRegion(itqb,top)={-\paperheight+19.25mm}
\SetRegion(itqb,bottom)={\GetRegion*(itqb,top)-10.5mm}
\SetRegion(itqb,left)={10mm}
\SetRegion(itqb,right)={\paperwidth}
\SetRegion(itqb,round)={0.4cm}
\SetRegion(photo,top)={-81mm}
\SetRegion(photo,left)={1.5cm}
\SetRegion(photo,height)={11.0cm}
\SetRegion(photo,width)={\paperwidth-\GetRegion*(photo,left)-1.9cm}


%%=======================================================================
%% COLORS
%%=======================================================================

\providecolor{itqbgray207}{RGB}{207,207,207}
\providecolor{itqbgray189}{RGB}{189,189,189}
\providecolor{itqbgray127}{RGB}{127,127,127}
\providecolor{itqbgray89}{RGB}{89,89,89}
\providecolor{itqbgray38}{RGB}{38,38,38}
\providecolor{itqbgray25}{RGB}{25,25,25}
\providecolor{itqbgreen}{RGB}{39,177,153}

\NewMemStore{ItqbColor}
\SetItqbColor(gray,front,bar)={itqbgray89}
\SetItqbColor(gray,front,text)={white}
\SetItqbColor(gray,front,text2)={itqbgray207}
\SetItqbColor(gray,front,date)={itqbgray127}
\SetItqbColor(gray,front,rectangle)={itqbgray89}
\SetItqbColor(gray,back,text)={itqbgray38}
\SetItqbColor(gray,spine)={itqbgray38}
\SetItqbColor(gray,back)={itqbgray38}
\SetItqbColor(green,front,bar)={itqbgreen}
\SetItqbColor(green,front,text)={white}
\SetItqbColor(green,front,date)={itqbgray127}
\SetItqbColor(green,front,rectangle)={itqbgreen}
\SetItqbColor(green,back,text)={itqbgreen}
\SetItqbColor(green,front,text2)={itqbgray207}
\SetItqbColor(green,spine)={itqbgreen}
\SetItqbColor(green,back)={itqbgreen}


%%=======================================================================
%% SPINE
%%=======================================================================

\SetSpine(logo)={\GetSchool(logo,neg)}
\SetSpine*(bg,color)={\GetItqbColor(\option{/novathesis/nova/itqb/cover/color},spine)}
\SetSpine(date)={\textcolor{itqbgray207}{\GetOeiras()},
              \PrintDateISO{\GetDocDate(submission,year)-\GetDocDate(submission,month)}}

\SpineSetup[%
]{%
  date                        = {\GetSpine(date)},
  bg/color                    = {\GetSpine(bg,color)},
  box/logo/bg/image           = {\GetSpine(logo)},
  box/date/fg/color           = {white},
  box/title/fg/color          = {white},
  box/author/fg/color         = {white},
  box/date/len                = {4.5cm},
  box/title/len               = {11.0cm},
  box/author/len              = {3.0cm},
  box/logo/len                = {3.5cm},
  % debug,
}


%%=======================================================================
%% UTILITIES & HOOKS
%%=======================================================================

\newcommand{\@resizetowidth}[2]{%
  \setbox0=\hbox{#2}%
  \@tempdima=\dimexpr#1%
  \ifdim\wd0>\@tempdima\relax%
    \resizebox{\@tempdima}{!}{\box0}%
  \else
    \box0%
  \fi
}

\SetThesisCover(textfont)={\sffamily}
% \NTAddToHook{cover/text/pre}{%
%   \sffamily%
% }

\ntaddtocover[tikz]{1-1}{%
    \sffamily%
      \@for\nt@tmpi:={top,bottom,left,right}\do{%
          \datamatchcs{@VAL@\nt@tmpi}{Margin}(%
            {cover,\@DOCTYPE,\@DEGREEACR,#1,\nt@tmpi},%
            {cover,\@DOCCLASS,\@DEGREEACR,#1,\nt@tmpi},%
            {cover,\@DEGREEACR,#1,\nt@tmpi},%
            {cover,\@DOCTYPE,\@DEGREEACR,\nt@tmpi},%
            {cover,\@DOCCLASS,\@DEGREEACR,\nt@tmpi},%
            {cover,\@DEGREEACR,\nt@tmpi},%
            {cover,\@DOCTYPE,#1,\nt@tmpi},%
            {cover,\@DOCCLASS,#1,\nt@tmpi},%
            {cover,#1,\nt@tmpi},%
            {cover,\@DOCTYPE,\nt@tmpi},%
            {cover,\@DOCCLASS,\nt@tmpi},%
            {cover,\nt@tmpi},
          )%
      }%
    \setbox0=\hbox{%
        \scalebox{2.15}{%
        \fontsize{19}{19}\selectfont\bfseries\GetDocDate(submission,year)}}
    \begin{scope}[shift=(current page.north west), xshift=\@VAL@left]
      % Logo at top-left
      \node [inner sep=0pt, anchor=north west, xshift=0, yshift=-\@VAL@top]
                      {\includegraphics[height=1.7cm]{nova-itqb-logo}};
      % Date at top-right
      \node [inner sep=0pt,anchor=east,
             xshift=\paperwidth-\@VAL@left-10mm,
             yshift=-\@VAL@top-2.5mm,
             color={\expanded{%
           \GetItqbColor(\option{/novathesis/nova/itqb/cover/color},front,date)}},
       rotate=90,
      ] (date)
      {\fontsize{19}{19}\selectfont%
       \textbf{\GetDocDate(submission,year)}};
  \draw[line width=0.7mm,
      \expanded{%
           \GetItqbColor(\option{/novathesis/nova/itqb/cover/color},front,date)}
     ] ($(date.north west) - (2mm,0)$) -- ($(date.north east) - (2mm,0)$);
      % Title
      \node [inner sep=0pt, anchor=south west, yshift=-\@VAL@top-38.75mm]
                      {\fontsize{19}{19}\selectfont%
                       \parbox{\dimexpr\paperwidth-\@VAL@left-\@VAL@right}%
                              {\@resizetowidth{\linewidth}%
                                              {\textbf{%
                         \begin{tabular}{@{}l@{}}%
                         \GetDocTitle(\@LANG@COVER,main)%
                         \end{tabular}%
                       }}}};
      % Subtitle
      \node [inner sep=0pt, anchor=west, yshift=-\@VAL@top-44.3mm]
                      {\fontsize{16.0}{16.0}\selectfont%front.pdf
                       \parbox{\paperwidth-\@VAL@left-\@VAL@right}%
                              {\@resizetowidth{\linewidth}%
                                              {\GetDocTitle(\@LANG@COVER,sub)}}};
      % Author
      \node [inner sep=0pt, anchor=west, yshift=-\@VAL@top-54.7mm]
                      {\fontsize{15}{15}\selectfont\textbf{\GetDocAuthor(name)}};
      % Photo
      \edef\@itqbheight{\GetRegion*(photo,height)}
      \edef\@itqbwidth{\GetRegion*(photo,width)}
      \draw [path picture={\node at (path picture bounding box.center)
                      {\includegraphics[height=\@itqbheight]{nova-itqb-photo}};},
                      rounded corners=8mm, xshift=-\@VAL@left]
                      ({\GetRegion*(photo,left)},\GetRegion*(photo,top))
                      rectangle
                      (\expanded{\GetRegion*(photo,left)+\GetRegion*(photo,width)},
                                 \expanded{\GetRegion*(photo,top)-\GetRegion*(photo,height)});
      % Dissertation string
      \node [inner sep=0pt,anchor=west, yshift={\GetRegion*(itqb,top)+28.75mm}]
                      {\fontsize{14}{14}\selectfont
                       \GetDissertationString(\@DOCCLASS,%
                                              \@LANG@COVER)};
      % Major field
      \node [inner sep=0pt,anchor=west, yshift={\GetRegion*(itqb,top)+20.0mm}]
                      {\fontsize{15}{15}\selectfont
                       \GetMajorField(\@LANG@COVER)};
      % Date
      \node [inner sep=0pt,anchor=west, yshift={\GetRegion*(itqb,top)+8.4mm}]
                      {\fontsize{12}{12}\selectfont
                       \mbox{O}eiras,
                       \PrintDateISO{\GetDocDate(submission,year)-\GetDocDate(submission,month)}};
      % Rectangle
      \fill [xshift=-\@VAL@left,
             \expanded{%
         \GetItqbColor(\option{/novathesis/nova/itqb/cover/color},front,rectangle)}
       ]
                     (\GetRegion*(itqb,left),{\GetRegion*(itqb,top)})
                  -- (\GetRegion*(itqb,right),{\GetRegion*(itqb,top)})
                  -- (\GetRegion*(itqb,right),{\GetRegion*(itqb,bottom)})
                  {[rounded corners={\GetRegion*(itqb,round)}]
                  -- (\GetRegion*(itqb,left),{\GetRegion*(itqb,bottom)})}
                  -- cycle {};
      % Text in rectangle
      \node [inner sep=0pt, anchor=west, xshift={1mm},
                            yshift={(\GetRegion*(itqb,bottom)+\GetRegion*(itqb,top))/2}]
                    {\fontsize{9}{9}\selectfont
                    {\color{white}INSTITUTO DE TECNOLOGIA QUÍMICA E BIOLÓGICA }
                    {\color{itqbgray207}ANTÓNIO XAVIER}
                    {\color{white}$|$ UNIVERSIDADE NOVA DE LISBOA}};
    \end{scope}
}


%%=======================================================================
%% BACK COVER ELEMENTS
%%=======================================================================

\ntaddtocover[tikz]{N-2}{%
  \sffamily%
  \setbox0=  \hbox{\scalebox{2}{\bfseries\GetDocDate(submission,year)}}
  \begin{scope}[shift=(current page.north west)]
    % Filling back cover
    \fill [color={\expanded{\GetItqbColor(\option{/novathesis/nova/itqb/cover/color},back)}}]
                    (0,0) rectangle (\paperwidth,-\paperheight);
    % Logo above rectangle
    \node [inner sep=0pt,anchor=north west,xshift=11mm,yshift={3.5cm+\GetRegion*(itqb,top)}]
                    {\includegraphics[width=11.7cm]{nova-itqb-logo-mono}};
    \begin{scope}[xscale=-1,xshift=-\paperwidth]
      % Rectangle
      \fill [color=white] (\GetRegion*(itqb,left),{\GetRegion*(itqb,top)})
                  -- (\GetRegion*(itqb,right),{\GetRegion*(itqb,top)})
                  -- (\GetRegion*(itqb,right),{\GetRegion*(itqb,bottom)})
                  {[rounded corners={\GetRegion*(itqb,round)}]
                  -- (\GetRegion*(itqb,left),{\GetRegion*(itqb,bottom)})}
                  -- cycle {};
      % Text in rectangle
      \node [inner sep=0pt, anchor=west, xshift={-\paperwidth+11mm},
                            yshift={(\GetRegion*(itqb,bottom)+\GetRegion*(itqb,top))/2},
                            color={\GetItqbColor(\option{/novathesis/nova/itqb/cover/color},back,text)}]
                      {\fontsize{10}{10}\selectfont
                       \GetSchool(address)};
    \end{scope}
  \end{scope}
}
