\NeedsTeXFormat{LaTeX2e}[2015/01/01]
\ProvidesClass{imepaper}[2021/07/28 v2.0 IME/USP standard letterheaded paper]

% WHY IS THIS BASED ON ARTICLE.CLS INSTEAD OF LETTER.CLS?
%
% 1. In letter.cls, \opening and \closing are practically
%    mandatory, but we wanted them to be optional;
%
% 2. To customize the appearance, we needed to reimplement
%    \opening and \closing anyway;
%
% 3. Features of letter.cls beyond \opening and \closing
%    are unlikely to be very useful;
%
% 4. Reading the letter.cls docs is important, but we
%    wanted a simple example to be enough for the user.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%% BASE CLASS, ESSENTIAL PACKAGES, PAPER/FONT SIZE %%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\RequirePackage{etoolbox}

% Should we strictly follow the layout of the MS-Word template? Probably
% not, as it does not conform very well to the visual identity manual
\newtoggle{@IMEaltLayout}
\DeclareOption{altlayout}{\toggletrue{@IMEaltLayout}}
\DeclareOption{noaltlayout}{\togglefalse{@IMEaltLayout}}

\newtoggle{@IMEwide}
\newtoggle{@IMEcolor}
\DeclareOption{bigmargins}{\togglefalse{@IMEwide}}
\DeclareOption{nobigmargins}{\toggletrue{@IMEwide}}
\DeclareOption{nocolor}{\togglefalse{@IMEcolor}}
\DeclareOption{color}{\toggletrue{@IMEcolor}}
\ExecuteOptions{noaltlayout,color,bigmargins}

% Handle our default for the standard size options. Options are processed in
% the order of their definition in the class file. This means that passing
% "10pt,11pt" or "11pt,10pt" to the article class produces the same effect:
% 11pt size is selected. So, to actually allow the user to select the size,
% we cannot simply pass our default to the article class and let the user
% override it with a different option; we need to absorb the supplied options
% and pass a single value to the article class. At the same time, we do not
% need to worry about a4paper: it is the first of all paper size options
% defined in the article class, so if the user supplies a different size
% it will override a4paper.

\DeclareOption{10pt}{\def\@IMEptsize{10pt}}
\DeclareOption{11pt}{\def\@IMEptsize{11pt}}
\DeclareOption{12pt}{\def\@IMEptsize{12pt}}
\ExecuteOptions{11pt,nobigmargins}

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions\relax
\LoadClass[\@IMEptsize,a4paper]{article}

\RequirePackage{babel}

\RequirePackage[parfill]{parskip}

\RequirePackage{setspace} % In case the user wants to change this

\RequirePackage{geometry}
\geometry{top=47.5mm, headsep=13.5mm, headheight=18mm, footskip=10.8mm}

\iftoggle{@IMEwide}
  {\geometry{left=22mm, right=27mm}}
  {\geometry{left=30mm, right=30mm}}

\iftoggle{@IMEaltLayout}
  {\geometry{bottom=31.4mm}} % Word template
  %{\geometry{bottom=26mm}} % visual identity manual
  % We will not actually use 26mm because the footer would be only
  % 7mm apart from the end of the paper, which may cause problems
  % in some printers. Also, I find this to be visually better.
  {\geometry{bottom=29mm}}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%% FOOTER FONT, LOGO COLOR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% The footer uses Arial Narrow, but that font is only available on Windows.
% Let's use Liberation Sans Narrow instead, as it is free and reasonably
% similar. This forces us to use XeTeX or LuaTeX.
\RequirePackage{fontspec}
\newfontfamily\liberationSansNarrow
  {LiberationSansNarrow-Regular.ttf}
  [
    Ligatures = TeX,
    BoldFont=LiberationSansNarrow-Bold.ttf,
  ]

\RequirePackage[hyperref,svgnames,x11names,table]{xcolor}

% The visual identity manual states that the logo color is (cmyk or rgb):
%\definecolor{imeblue}{cmyk}{1,0.8,0,0.4} % Used in the EPS file
\definecolor{imeblue}{RGB}{20,45,105} % Used in the MS-Word template footer
%
% The manual also states it is pantone 287C: https://www.pantone.com/color-finder/287-C
%\definecolor{imeblue}{cmyk}{1,0.81,0,0.23}
%\definecolor{imeblue}{RGB}{0,48,135}
%
% Or pantone 293U: https://www.pantone.com/color-finder/293-U
%\definecolor{imeblue}{cmyk}{1,0.73,0,0.05}
%\definecolor{imeblue}{RGB}{35,91,168}
%
% Opening the EPS file in inkscape converts the color to
%\definecolor{imeblue}{cmyk}{0.8,0.58,0,0.6}
%\definecolor{imeblue}{RGB}{20,43,102}
%
% Opening the visual identity manual PDF in inkscape converts the color to
%\definecolor{imeblue}{RGB}{0,46,110}
%\definecolor{imeblue}{cmyk}{1,.58,0,.57}
% which is also used in the JPEG image embedded in the MS-Word template
%
%
% In short, if there is no color correction mechanism in place, anything goes :)

\iftoggle{@IMEcolor}
  {
    \def\@IMEthecolor{imeblue}
    \def\@IMEcolorfilename{azul}
  }
  {
    \def\@IMEthecolor{black}
    \def\@IMEcolorfilename{preto}
  }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%% HEADERS AND FOOTERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\RequirePackage{graphicx}
\RequirePackage{fancyhdr}

\renewcommand{\headrulewidth}{0pt}

\fancyhf{}
\fancypagestyle{plain}{
  % The alignment specified in the visual identity manual means that:
  %
  % - USP logo height * 6/4 = IME logo height
  % - IME logo height / 6 = USP logo shift upwards

  \iftoggle{@IMEaltLayout}
  {
    % Word template
    \fancyhead[L]{\includegraphics[height=1.425cm]{ime-horizontal-\@IMEcolorfilename}}
    \fancyhead[R]{\raisebox{.2375cm}{\includegraphics[height=.95cm]{usp-\@IMEcolorfilename}}}
  }
  {
    % visual identity manual
    \fancyhead[L]{\includegraphics[height=1.5cm]{ime-horizontal-\@IMEcolorfilename}}
    \fancyhead[R]{\raisebox{.25cm}{\includegraphics[height=1cm]{usp-\@IMEcolorfilename}}}
  }

  \fancyfoot[L]{\imefooter}
}
\pagestyle{plain}

\def\imefooter{%
    \begin{minipage}[t]{\textwidth}%
      \parskip=1pt
      \liberationSansNarrow
      \fontsize{11pt}{11pt}\selectfont
      \textcolor{\@IMEthecolor}{\textbf{\@IMEdept}}\par
      \fontsize{9pt}{10pt}\selectfont
      \textcolor{\@IMEthecolor}{%
        Rua do Matão, 1010 | Cidade Universitária | São Paulo\hspace{.1em}-\hspace{.1em}SP | CEP 05508-090\\
        \@IMEphone \@IMEemail www.ime.usp.br%
      }\par%
    \end{minipage}%
}

\iftoggle{@IMEwide}
  {
    \fancyheadoffset[L]{7mm}\fancyheadoffset[R]{12mm}
    \iftoggle{@IMEaltLayout}
      {\fancyfootoffset{2mm}} % Word template
      {\fancyfootoffset{7mm}} % visual identity manual
  }
  {
    \fancyheadoffset{15mm}
    \iftoggle{@IMEaltLayout}
      {\fancyfootoffset{10mm}} % Word template
      {\fancyfootoffset{15mm}} % visual identity manual
  }


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TRANSLATIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\providecommand\@subjectname{Subject}
\providecommand\@phonename{Phone}

\addto\captionsbrazilian{%
  \renewcommand\@subjectname{Ref.}%
  \renewcommand\@phonename{Tel}%
}

% We prefer "brazilian", but let's cover all bases
\addto\captionsbrazil{%
  \renewcommand\@subjectname{Ref.}%
  \renewcommand\@phonename{Tel}%
}

% The TeX default varies between english and american
\addto\captionsenglish{%
  \renewcommand\@subjectname{Subject}%
  \renewcommand\@phonename{Phone}%
}

\addto\captionsamerican{%
  \renewcommand\@subjectname{Subject}%
  \renewcommand\@phonename{Phone}%
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% DEPARTMENT, PHONE, EMAIL (USED IN THE FOOTER) %%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\gdef\@IMEdept{}
\newcommand\dept[1]{\gdef\@IMEdept{#1}}
\newcommand\depto[1]{\dept{#1}} % just in case

\gdef\@IMEphone{}
\newcommand\phone[1]{\gdef\@IMEphone{\@phonename: #1 | }}
\newcommand\fone[1]{\phone{#1}} % just in case

\gdef\@IMEemail{}
\newcommand\email[1]{\gdef\@IMEemail{#1 | }}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% OPTIONAL OPENING AND CLOSING COMMANDS %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\@subject}{} % Rendered as "\textbf{Subject:} something"
\newcommand{\subject}[1]{\renewcommand{\@subject}{#1}}
\newcommand{\@simplesubject}{} % Subject without prefix, for something like "Memo 123"
\newcommand{\simplesubject}[1]{\renewcommand{\@simplesubject}{#1}}
\newcommand{\@signature}{} % "Mr. John Doe, head of department"
\newcommand{\signature}[1]{\renewcommand{\@signature}{#1}}
\newcommand{\@sigimage}{}
\newcommand{\sigimage}[1]{\renewcommand{\@sigimage}{#1}}
\newcommand{\@addressee}{}
\newcommand{\addressee}[1]{\renewcommand{\@addressee}{#1}}
\newcommand{\@beforesig}{1.2cm}
\newcommand{\beforesig}[1]{\renewcommand{\@beforesig}{#1}}
\newcommand{\@aftersig}{1.5cm}
\newcommand{\aftersig}[1]{\renewcommand{\@aftersig}{#1}}
\newcommand{\@sigheight}{3cm}
\newcommand{\sigheight}[1]{\renewcommand{\@sigheight}{#1}}

\newcommand{\opening}[1]{\par
  % We use a group because:
  % 1. If \@subject contains multiple lines, the space to the
  %    following paragraph is reduced (the parbox is higher
  %    than a single line, which interacts with \lineskiplimit).
  %    We compensate that by setting a larger \lineskip;
  % 2. We make local changes to \parskip;
  % 3. We force singlespacing here;
  % 4. We set \parfillskip to zero to prevent a warning because
  %    the parbox fills up the entire last line.
  \bgroup
  \setlength\parfillskip{0pt plus 1fil}
  \lineskip.8\baselineskip
  \parskip=4pt
  \singlespacing
  \@date\par
  \@addressee\par
  \@simplesubject\par
  \settowidth{\@tempdima}{\textbf{\@subjectname:} }
  \setlength{\@tempdima}{-\@tempdima}\addtolength{\@tempdima}{\textwidth}
  \ifdefvoid{\@subject}{}{\textbf{\@subjectname:} \parbox[t]{\@tempdima}{\@subject}\par}
  \parskip=0pt
  \rule{.4\textwidth}{0.6pt}\par
  \egroup
  \vspace{2\parskip}
  \bgroup \bfseries #1 \egroup\par
  \vspace{2\parskip}
}

\newcommand{\closing}[1]{\par
  \bgroup\singlespacing
  \setlength\parfillskip{0pt plus 1fil} % See the comment in \opening

  \vspace*{\fill}
  \hspace{.5\textwidth}\parbox{.5\textwidth}{\centering #1}\par

  \vspace{\@beforesig}

  \ifdefvoid{\@sigimage}{}
  {
    \smash{
      \hspace{.5\textwidth}\parbox{.5\textwidth}{\centering
      \includegraphics*[height=\@sigheight]{\@sigimage}}
    }\par
    \vspace{-1\baselineskip}
    \vspace{-1\parskip}
  }

  \vspace{\@aftersig}

  \hspace{.5\textwidth}\parbox{.5\textwidth}{\centering \@signature}\par

  \vspace{2\baselineskip}

  \egroup
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% DEFAULT FONT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% This font is reasonably similar (and metric-compatible) to calibri
% (the default Windows font), which is used in the MS-Word template
\usepackage[default]{carlito}

\endinput
