\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{wmletterhead}[2014/12/15 provides LaTeX WM letter head class]

% Created by Eugeniy E. Mikhailov 
% v1.0 2014/12/15
% Note logo files belong to W&M
% 'wmletterhead' class accepts all options of the 'letter' class and the following
% additional options which specify the header logo
%   prior2014dec - old logo prior 2014
%   fullcolor - full color logo with yellowish WM cypher
%   black - all black logo
%   green - all green logo
%   white - all white logo (quite use less on white background)
% DEFAULT options: [fullcolor]
% Recomended use:
% \documentclass[12pt,fullcolor]{wmletterhead}

%-------------- define WM header logo file ---------------------------------
\providecommand{\wmheaderlogo}{}% nologo

% this is prior December 2014 logo with Wren building
\DeclareOption{prior2014dec}{%
  \renewcommand{\wmheaderlogo}%
    {wmlogos/wmheader2012}%
}

% full colored logo starting from December 2014
\DeclareOption{fullcolor}{%
  \renewcommand{\wmheaderlogo}%
    {wmlogos/wm_horizontal_single_line_full_color}%
}

% green logo starting from December 2014
\DeclareOption{green}{%
  \renewcommand{\wmheaderlogo}%
    {wmlogos/wm_horizontal_single_line_green}%
}

% black logo starting from December 2014
\DeclareOption{black}{%
  \renewcommand{\wmheaderlogo}%
    {wmlogos/wm_horizontal_single_line_black}%
}

% black logo starting from December 2014
\DeclareOption{white}{%
  \renewcommand{\wmheaderlogo}%
    {wmlogos/wm_horizontal_single_line_white}%
}
%-------------- end of define WM header logo file ------------------------------


% by default all unknown options are passed to 'letter' class
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{letter}}

% DEFAULT options
\ExecuteOptions{fullcolor}
\ProcessOptions\relax

\LoadClass{letter}
\RequirePackage{calc}
\RequirePackage{graphicx}
\RequirePackage{geometry} % easy page layout manipulation
\RequirePackage{fancyhdr} % Headers !
\RequirePackage{xcolor}

\definecolor{WMgold}{rgb}{0.73,0.59,0.36}

% --------------------------------------------------
% Change the letter class default for signature indent
\setlength{\longindentation}{0pt} % this is indent of signature

% --------------------------------------------------
% WM logos parameters
\newlength{\wmlogoheight}
\newlength{\wmlogowidth}
\newlength{\wmlogohpos}
\newlength{\wmlogovpos}
\newlength{\wmlogoWhpos} % distance from the left logo edge to the 'W' in William
\newlength{\wmlogoWvpos} % distance from the top  logo edge to the 'W' in William

% below are measured in the Word template
\setlength{\wmlogoheight}{0.65in} 
\setlength{\wmlogowidth}{3.64in}% ideally should be scaled from \wmlogoheight
\setlength{\wmlogohpos}{0.76in}%  ideally should be calculated from \wmleftmargin and \wmlogoheight
\setlength{\wmlogovpos}{0.75in}%  same as above
\setlength{\wmlogoWhpos}{0.82in}% same as above
\setlength{\wmlogoWvpos}{.15in}% same as above

% --------------------------------------------------
% Required by WM
\newlength{\wmlogoseparation}
\setlength{\wmlogoseparation}{0.5in} % header text from main text separation

\newlength{\wmleftmargin}
\setlength{\wmleftmargin}{1.64in}

\newlength{\wmrightmargin}
\setlength{\wmrightmargin}{1.0in}

\newlength{\wmbottommargin}
\setlength{\wmbottommargin}{1.0in} % guessed

\newlength{\wmtopmargin} %start position for address or logo
\setlength{\wmtopmargin}{1.0in}

\colorlet{wmtaglinecolor}{WMgold}

% --------------------------------------------------
% internal use sizes to lineup logo and from (W&M) address
\newlength{\wmaddressheight} % to be calculated at the opening
\newlength{\wmaddresstotalheight}
\newlength{\wmaddresswidth}
\newlength{\wmaddresshpos}
% --------------------------------------------------

% --------------------------------------------------
% Let's calculate top margin
%\newlength{\wmtopmargin}
%\setlength{\wmtopmargin}{\wmlogoheight+\wmlogovpos+\wmlogoseparation}%
% --------------------------------------------------

\geometry{
  letterpaper,
  top=\wmtopmargin,
  left=\wmleftmargin,
  right=\wmrightmargin,
  bottom=\wmbottommargin,
  marginparwidth=0in,
  marginparsep=0in,
  %showframe % this shows layout frames
}


% -------------------------------------------------------------------
%% WM fancy letter head definition
\newcommand{\wmaddress}{
  \ifx\@empty\fromaddress
    {}
  \else
    \begin{tabular}{l@{}}\ignorespaces
      \fromaddress%
    \end{tabular}%
  \fi
}

\newcommand{\setwmaddresssizes}{
  \settototalheight{\wmaddresstotalheight}{\hbox{\wmaddress}}%
  \settoheight{\wmaddressheight}{\hbox{\wmaddress}}%
  \settowidth{\wmaddresswidth}{\hbox{\wmaddress}}%
}

\newcommand{\wmfromwithlogo}{
  \setwmaddresssizes
    \raisebox{\wmaddressheight-\wmlogoheight+\wmlogoWvpos}[\wmaddressheight]{
      \hskip -\wmlogoWhpos%
        \includegraphics[height=\wmlogoheight{},keepaspectratio=true]{\wmheaderlogo}
    }
  \hfill\wmaddress{}%
}

% -------------------------------------------------------------------
% This takes care of WM tagline which we will put in the central footer
\newcommand*{\tagline}[1]{\def\lettertagline{#1}}
\tagline{}

% -------------------------------------------------------------------
% fancyheader definitions
% this is very rudimentary and probably not needed
\fancypagestyle{empty}{% looks like this has no effect for the letter class
  \renewcommand{\headrulewidth}{0pt}
  \fancyfoot[C]{\thepage}%
}

\fancypagestyle{plain}{% this takes care of the 2nd and the following pages
  \renewcommand{\headrulewidth}{0pt}
  \fancyfoot[C]{\thepage}%
}

\fancypagestyle{firstpage}{% some how this is the very first page style
  \renewcommand{\headrulewidth}{0pt}
  \ifx\lettertagline\@empty
    \fancyfoot[C]{\thepage}%
  \else
    \fancyfoot[C]{{\color{wmtaglinecolor}\lettertagline}\\\thepage}%
  \fi
}

\pagestyle{plain}% important! if switched to fancy pages 2, 3, ... do not have logo

% -------------------------------------------------------------------
% Now let redefine opening so address goes to the right of logo
\renewcommand*{\opening}[1]{
  \setwmaddresssizes
  \thispagestyle{firstpage}%
  \wmfromwithlogo
  \vskip \wmlogoseparation
  {\raggedleft\@date\par}%
  {\raggedright \toname \\ \toaddress \par}%
  %\vspace{2\parskip}%
  #1\par\nobreak}
% -------------------------------------------------------------------

% vim: tabstop=2 shiftwidth=2 expandtab
