% SRON document class

\NeedsTeXFormat{LaTeX2e}[1999/06/01]
\ProvidesClass{sron}[2016/04/02 SRON technical note]
\LoadClass{article}

\RequirePackage[table]{xcolor}
\definecolor{sronblue}{rgb}{0.043, 0.239, 0.569}

% DVI/PDF dependent packages
% - compile your tex file with "latex" to create a DVI file
% - compile your tex file with "pdflatex" to create a PDF file

%Bibliography
%backend=biber is better if you have many references, want Unicode support or want to sort in Danish
%backend=bibtex can give an error that \ifprefchar is not defined, when encountering a "van der" name.
\newcommand{\ifprefchar}{\ifpunctmark{'}}
\usepackage[backend=bibtex,sorting=none,defernumbers=true,citestyle=numeric-comp]{biblatex}
\DeclareBibliographyCategory{AD}
\defbibheading{AD}{\section*{Applicable Documents}\addcontentsline{toc}{section}{Applicable Documents}}
\DeclareBibliographyCategory{RD}
\defbibheading{RD}{\section*{Reference Documents}\addcontentsline{toc}{section}{Reference Documents}}

\RequirePackage{ifpdf}
\ifpdf
 \pdfcompresslevel=9
 \pdfoptionpdfminorversion=3
 \RequirePackage[pdftex, a4paper, hmargin=2cm, top=1cm, bottom=2cm, 
   includehead, nofoot, head=93pt, headsep=\baselineskip]{geometry}
 \RequirePackage[pdftex]{graphicx}
 \DeclareGraphicsExtensions{.pdf,.jpg,.png,.mps}
 \RequirePackage[pdftex]{hyperref}
 \RequirePackage[pdftex]{pict2e}
\else
 \RequirePackage[dvips, a4paper, hmargin=2cm, top=1cm, bottom=2cm, 
   includehead, nofoot, head=93pt, headsep=\baselineskip]{geometry}
 \RequirePackage[dvips]{graphicx}
 \DeclareGraphicsExtensions{.eps,.ps}
 \RequirePackage[dvips]{hyperref}
 \RequirePackage[dvips]{pict2e}
\fi
\hypersetup{
        pdfstartview={FitH},
        linktocpage=true,
        colorlinks=true,
        anchorcolor=black,
        linkcolor=sronblue,
        citecolor=sronblue,
        filecolor=sronblue,
        urlcolor=sronblue,
        menucolor=red}

% Fonts and text sizes in SRON style, but spacing between characters
% and around section headings is kept at LaTeX defaults.

\RequirePackage[T1]{fontenc}
\RequirePackage{textcomp}
\IfFileExists{arev.sty}{\newcommand{\sron@arev}{true}}{%
\ClassWarningNoLine{sron}{Install Arev Sans font for better quality}}
\IfFileExists{mdacmr.fd}{\newcommand{\sron@mathdesign}{true}}{%
\ClassWarningNoLine{sron}{Install Math Design fonts for better quality}}
\RequirePackage{ifthen}
\ifthenelse{\isundefined{\sron@arev} \OR \isundefined{\sron@mathdesign}}{%
\RequirePackage[standard-baselineskips]{cmbright}
\RequirePackage{amsfonts}
}{%
\RequirePackage{arev} % Arev Sans font
% Missing text symbols are taken from Bera Sans
\DeclareFontFamily{TS1}{fav}{}
\DeclareFontShape{TS1}{fav}{m}{n}{<-> fvsr8c}{}
\DeclareFontShape{TS1}{fav}{m}{it}{<-> fvsro8c}{}
\DeclareFontShape{TS1}{fav}{b}{n}{<-> fvsb8c}{}
\DeclareFontShape{TS1}{fav}{b}{it}{<-> fvsbo8c}{}
\DeclareFontShape{TS1}{fav}{m}{sl}{<->ssub * fav/m/it}{}
\DeclareFontShape{TS1}{fav}{bx}{n}{<->ssub * fav/b/n}{}
\DeclareFontShape{TS1}{fav}{bx}{it}{<->ssub * fav/b/it}{}
\DeclareFontShape{TS1}{fav}{b}{sl}{<->ssub * fav/b/it}{}
\DeclareFontShape{TS1}{fav}{bx}{sl}{<->ssub * fav/b/sl}{}
% Avoiding warnings about bold mathematical symbols
\SetSymbolFont{symbols}{bold}{OMS}{zavm}{m}{n}
}
\RequirePackage{amsmath, bm} % not needed within package, just very handy

% Font sizes are roughly scaled by \sqrt{6/5}^x with
% x=-8,-4,-2,-1,0,2,4,6,8,10 and the /baselineskip is 1.6 times larger.

\renewcommand{\tiny}{\fontsize{4}{6.4}\selectfont}
\renewcommand{\scriptsize}{\fontsize{5.8}{9.3}\selectfont}
\renewcommand{\footnotesize}{\fontsize{6.9}{11.1}\selectfont}
\renewcommand{\small}{\fontsize{7.6}{12.2}\selectfont}
\renewcommand{\normalsize}{\fontsize{8.6}{13.5}\selectfont}
\renewcommand{\large}{\fontsize{10}{16}\selectfont}
\renewcommand{\Large}{\fontsize{12}{19.2}\selectfont}
\renewcommand{\LARGE}{\fontsize{14.4}{23.1}\selectfont}
\renewcommand{\huge}{\fontsize{17.28}{27.7}\selectfont}
\renewcommand{\Huge}{\fontsize{20.74}{33.2}\selectfont}

\DeclareMathSizes{5.8}  {5.8}  {4.6}  {4.6}
\DeclareMathSizes{6.9}  {6.9}  {5.2}  {4.6}
\DeclareMathSizes{7.6}  {7.6}  {5.8}  {4.6}
\DeclareMathSizes{8.6}  {8.6}  {6.6}  {5.2}
\DeclareMathSizes{10}   {10}   {7.6}  {6}
\DeclareMathSizes{12}   {12}   {9}    {7}
\DeclareMathSizes{14.4} {14.4} {10}   {8}
\DeclareMathSizes{17.28}{17.28}{12}   {10}
\DeclareMathSizes{20.74}{20.74}{14.4} {12}

\RequirePackage{sectsty}
\sectionfont{\large}
\subsectionfont{\normalsize}
\subsubsectionfont{\mdseries\itshape\normalsize}
\paragraphfont{\mdseries\itshape}
\subparagraphfont{\mdseries\itshape}

% Additional style settings

\RequirePackage{ragged2e} % left justified with hyphenation
\RaggedRight

\setlength{\parindent}{2pc}
\setlength{\belowcaptionskip}{2pt}
\setlength{\abovecaptionskip}{2pt}

\RequirePackage{enumitem}
\setitemize{nolistsep,leftmargin=*}
\setenumerate{nolistsep,leftmargin=*}

% Alter some LaTeX defaults for better treatment of figures:
% See p.105 of "TeX Unbound" for suggested values.
% See pp. 199-200 of Lamport's "LaTeX" book for details.
% General parameters, for ALL pages:
\renewcommand{\topfraction}{0.9} % max fraction of floats at top
\renewcommand{\bottomfraction}{0.8} % max fraction of floats at bottom
% Parameters for TEXT pages (not float pages):
\setcounter{topnumber}{2}
\setcounter{bottomnumber}{2}
\setcounter{totalnumber}{4}  % 2 may work better
\setcounter{dbltopnumber}{2} % for 2-column pages
\renewcommand{\dbltopfraction}{0.9} % fit big float above 2-col. text
\renewcommand{\textfraction}{0.07} % allow minimal text with figs
% Parameters for FLOAT pages (not text pages):
\renewcommand{\floatpagefraction}{0.7} % require fuller float pages
% N.B.: floatpagefraction must be less than topfraction
\renewcommand{\dblfloatpagefraction}{0.7} % require fuller float pages

% SRON logo

\newlength{\s@unitlength}
\@ifundefined{closepath}{%
\ClassWarningNoLine{sron}{Update package pict2e for a correct SRON logo}
\newcommand{\sron}[1][0.22]{{\color{sronblue}\LARGE\bf SRON}}}{%
\newcommand{\sron}[1][0.22]{%
\setlength{\s@unitlength}{\unitlength}
\setlength{\unitlength}{#1pt}
\begin{picture}(110.4042,39.2822)(25.6138,43.6729)
% Text: SRON
\moveto(109.712,56.2539)
\curveto(109.712,64.1582)(104.184,69.3477)(95.9126,69.3477)
\curveto(87.6421,69.3477)(82.1138,64.1582)(82.1138,56.2539)
\curveto(82.1138,48.3916)(87.6421,43.1621)(95.9126,43.1621)
\curveto(104.184,43.1621)(109.712,48.3916)(109.712,56.2539)
\closepath
\moveto(103.635,56.2539)
\curveto(103.635,51.1035)(100.597,47.6436)(95.9126,47.6436)
\curveto(91.1875,47.6436)(88.1909,51.1035)(88.1909,56.2539)
\curveto(88.1909,61.4063)(91.1875,64.8652)(95.9126,64.8652)
\curveto(100.597,64.8652)(103.635,61.4063)(103.635,56.2539)
\fillpath
\moveto(132.854,43.6729)
\lineto(119.096,59.6357)
\lineto(119.096,43.6729)
\lineto(113.948,43.6729)
\lineto(113.948,68.915)
\lineto(118.463,68.915)
\lineto(130.828,54.3672)
\lineto(130.828,68.915)
\lineto(136.018,68.915)
\lineto(136.018,43.6729)
\lineto(132.854,43.6729)
\fillpath
\moveto(35.7729,60.2383)
\curveto(35.2134,60.9395)(34.8853,61.7461)(34.8853,62.668)
\curveto(34.8853,65.2715)(37.5513,66.9277)(41.2339,66.9277)
\curveto(44.5361,66.9277)(47.1396,66.3955)(51.3301,63.3789)
\lineto(51.3301,71.0693)
\curveto(49.1714,72.1348)(44.8535,73.2578)(41.2339,73.2578)
\curveto(34.0215,73.2578)(28.4048,70.1631)(26.749,65.0059)
\curveto(29.0576,62.4473)(32.1694,61.)(35.7729,60.2383)
\fillpath
\moveto(69.8867,56.9961)
\curveto(68.4014,56.3408)(65.3438,56.5918)(65.3438,56.5918)
\lineto(65.3438,65.1016)
\lineto(67.2461,65.1016)
\curveto(71.6001,65.1016)(72.6572,63.5254)(72.6572,61.4775)
\curveto(72.6572,59.4688)(71.7471,57.8184)(69.8867,56.9961)
\closepath
\moveto(73.5337,54.8779)
\curveto(75.6855,55.9404)(78.4707,58.2207)(78.4707,61.7988)
\curveto(78.4707,66.9492)(73.6606,69.3076)(67.9634,69.3076)
\curveto(64.5029,69.3076)(62.4771,68.915)(59.5654,68.4434)
\lineto(59.5654,52.0303)
\curveto(59.5654,52.0303)(64.0684,52.1211)(68.0425,53.043)
\lineto(75.9385,43.6729)
\lineto(83.2393,43.6729)
\lineto(73.5337,54.8779)
\fillpath
% Lower part of large S
\color{sronblue}
\moveto(26.252,61.9063)
\curveto(26.251,61.8457)(26.249,61.7852)(26.249,61.7217)
\curveto(26.249,57.5215)(28.5352,54.209)(34.2495,51.6631)
\lineto(39.6465,49.2979)
\curveto(42.25,48.1738)(43.7109,47.0488)(43.7109,44.8018)
\curveto(43.7109,42.4941)(41.0439,40.3652)(37.8052,40.3652)
\curveto(32.7896,40.3652)(28.7251,41.7266)(25.6138,43.5596)
\lineto(26.4395,35.3955)
\curveto(30.186,34.1523)(34.3135,33.9756)(38.1865,33.9756)
\curveto(46.2505,33.9756)(52.6001,38.6484)(52.6001,45.5713)
\curveto(52.6001,48.1416)(51.5952,50.2852)(50.0854,51.9678)
\curveto(38.9424,53.1113)(30.6206,56.0674)(26.252,61.9063)
\fillpath
\end{picture}
\setlength{\unitlength}{\s@unitlength}
}}

% Definitions needed for header

\RequirePackage{multirow}
\RequirePackage{array}
\RequirePackage{lastpage}
\RequirePackage{tabularx}
\RequirePackage{makecell}

\RequirePackage{datetime}
\newdateformat{mydate}{\THEDAY\ \monthname[\THEMONTH] \THEYEAR}
\mydate

\newcolumntype{x}[1]{%
>{\centering\hspace{0pt}}p{#1}}

\newcommand{\@T}{\rule{0pt}{2.6ex}}
\newcommand{\@B}{\rule[-1.2ex]{0pt}{0pt}}
\newcommand{\@s}{\enspace}
\newcommand{\minitab}[2][l]{\begin{tabular}{#1}#2\end{tabular}}

\newcommand{\sronheader}[8]{%
\ifthenelse{\equal{\@institutenumber}{}}{}{\renewcommand{\baselinestretch}{0.85}}%
\ifthenelse{\equal{\@level}{}}{}{\renewcommand{\baselinestretch}{0.75}}%
\ifthenelse{\equal{\@class}{}}{}{\renewcommand{\baselinestretch}{0.77}}%
\normalsize
\setlength{\tabcolsep}{0pt}
\begin{tabularx}{\textwidth}{| x{0.3\textwidth} | x{0.3\textwidth} | X |}
\hline
\minitab[c]{\sron[0.8] \\ \rule{0.3\textwidth}{0.4pt} \\ \Large\bf\Gape[4pt][7pt]{#1}} &
{\large\bf \minitab[c]{#2}} &
\minitab[l l l]{
\@T\bf \@s Doc. no. & \bf :\@s & #3 \\
\ifthenelse{\equal{\@institutenumber}{}}{}{\bf \@s Inst. no. & \bf :\@s & #4 \\}%
\bf \@s Issue & \bf :\@s & #5 \\
\ifthenelse{\equal{\@level}{}}{}{\bf \@s Level & \bf :\@s & #6 \\}%
\ifthenelse{\equal{\@class}{}}{}{\bf \@s Class & \bf :\@s & #7 \\}%
\bf \@s Date & \bf :\@s & #8 \\
\@B\bf \@s Page & \bf :\@s & \thepage\ of \pageref*{LastPage}} \\
\hline
\end{tabularx}
}

% Header itself

\newcommand{\project}[1]{\def\@project{#1}}
\project{}
\newcommand{\documenttype}[1]{\def\@documenttype{#1}}
\documenttype{}
\newcommand{\documentnumber}[1]{\def\@documentnumber{#1}}
\documentnumber{}
\newcommand{\institutenumber}[1]{\def\@institutenumber{#1}}
\institutenumber{}
\newcommand{\issue}[1]{\def\@issue{#1}}
\issue{}
\newcommand{\level}[1]{\def\@level{#1}}
\level{}
\newcommand{\class}[1]{\def\@class{#1}}
\class{}
\date{\today}

\RequirePackage{fancyhdr}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot{}
\lhead{}
\chead{\sronheader{\@project}{\@documenttype}{\@documentnumber}{\@institutenumber}{\@issue}{\@level}{\@class}{\@date}}
\rhead{}
\pagestyle{fancy}

% Title and authors

\author{}
\newcommand{\checkedby}[1]{\def\@checkedby{#1}}
\checkedby{}
\newcommand{\agreedby}[1]{\def\@agreedby{#1}}
\agreedby{}
\newcommand{\authorisedby}[1]{\def\@authorisedby{#1}}
\authorisedby{}

\newlength{\s@parindent}
\renewcommand\maketitle{%
\hypersetup{pdftitle={\@title},pdfauthor={\@author}}
\setlength{\s@parindent}{\parindent}
\setlength{\parindent}{0pt}
{\LARGE\bf \@title\\}
\setlength{\parindent}{\s@parindent}
\bigskip
\begin{description}
\ifx\@author\@empty\relax\else\item[Prepared by:] \@author\fi
\ifx\@checkedby\@empty\relax\else\item[Checked by:] \@checkedby\fi
\ifx\@agreedby\@empty\relax\else\item[PA agreed by:] \@agreedby\fi
\ifx\@authorisedby\@empty\relax\else\item[Authorised by:] \@authorisedby\fi
\end{description}
}

% Fields for digital signatures.
% Needs eforms.sty, insdljs.sty, taborder.sty and epdftex.def,
% or the whole acrotex bundle from CTAN.
\IfFileExists{eforms.sty}%
  {\RequirePackage{eforms}
   \newcommand{\@signaturefield}[1]{\sigField{##1}{90.9mm}{9.8mm}}}%
  {\ClassWarningNoLine{sron}{Install eforms from the acrotex bundle to add signature fields}
   \newcommand{\@signaturefield}[1]{}}
\newcommand{\@signatureset}[2]{%
  \raisebox{-6.8pt}[0pt]{%
    \begin{minipage}[t]{90.9mm}%
    \ifnum #1>0\@signaturefield{#2} \fi%
    \ifnum #1>1\@signaturefield{#22}\fi%
    \end{minipage}}}

% \maketitleforsignatures gives just a table
% \maketitleforsignatures[a,b,c,d] with a, b, c and d each either 0, 1 or 2
% adds this number of signature fields to the prepared, checked, agreed and authorised boxes, respectively.
\newcommand{\maketitleforsignatures}[1][]{%
\ifthenelse{\equal{#1}{}}%
  {\maketitleforsignatures@h{0,0,0,0}}%
  {\maketitleforsignatures@h{#1}}
}
\def\maketitleforsignatures@h#1{\expandafter\maketitleforsignatures@i#1,,,,\@nil}
\def\maketitleforsignatures@i#1,#2,#3,#4,#5\@nil{%
\hypersetup{pdftitle={\@title},pdfauthor={\@author}}
%\hypersetup{pdftitle={\@documentnumber}, pdfsubject={\@title}, pdfauthor={\@author}}
\setlength{\s@parindent}{\parindent}
\setlength{\parindent}{0pt}
{\LARGE\bf \@title\\}
\vspace{3cm}
\arrayrulecolor{gray!25}
\begin{tabularx}{\textwidth}{@{}|l|p{0.25\textwidth}|X|}
\hline\@T
\textbf{Prepared by:} & \begin{minipage}[t]{0.25\textwidth}\@author\end{minipage} & \@signatureset{#1}{prepared} \\[1.5cm]%
\hline\@T
\textbf{Checked by:} & \begin{minipage}[t]{0.25\textwidth}\@checkedby\end{minipage} & \@signatureset{#2}{checked} \\[1.5cm]%
\hline\@T
\textbf{PA agreed by:} & \begin{minipage}[t]{0.25\textwidth}\@agreedby\end{minipage} & \@signatureset{#3}{agreed} \\[1.5cm]%
\hline\@T
\textbf{Authorised by:} & \begin{minipage}[t]{0.25\textwidth}\@authorisedby\end{minipage} & \@signatureset{#4}{authorised} \\[1.5cm]%
\hline
\end{tabularx}
\arrayrulecolor{black}
\setlength{\parindent}{\s@parindent}
}


% Table of contents

\RequirePackage[nottoc]{tocbibind}
\RequirePackage{tocloft}
\renewcommand{\contentsname}{Table of Contents}
\renewcommand{\cfttoctitlefont}{\normalfont\large\bfseries}
\renewcommand{\cftsecfont}{\normalfont}
\renewcommand{\cftsecleader}{\normalfont\cftdotfill{\cftsecdotsep}}
\renewcommand{\cftsecdotsep}{\cftdotsep}
\renewcommand{\cftsecpagefont}{\normalfont}
\addtocontents{toc}{\protect\thispagestyle{fancy}}

% Environments for first sections

\newenvironment{distributionlist}{\bigskip\noindent\textbf{Distribution}\begin{itemize}[label={},leftmargin=0cm]}{\end{itemize}}

\newenvironment{changerecordtable}{%
\section*{Document Change Record}
\begin{tabular}{llll}
Issue & Date & Changed Section & Description of Change \\
\hline \\[-13pt]}{\end{tabular}\medskip}

\newenvironment{abbreviationtable}{%
\section*{Abbreviations and Acronyms}
\addcontentsline{toc}{section}{Abbreviations and Acronyms}
\begin{tabular}{ll}
Item & Meaning \\
\hline \\[-13pt]}{\end{tabular}\medskip}
