% Macros for IC-UNICAMP letters with UNICAMP and IC logos.
% Created by Jorge Stolfi sometime in 1992--2007.
% All dimensions are suitable for "US letter" or "a4" size paper.
{ 
  \def\release{2010-03-08}
  \def\lastedit{2010-03-11 17:58:45 by stolfi}
  \typeout{letter-ic.sty release {\release} last edit {\lastedit}} 
}

\ProvidesPackage{letter-ic}
\RequirePackage{logo-ic-unicamp}
\RequirePackage{logo-unicamp}

\advance\endlinechar by -256\relax % Ignore line breaks in this package

  \newsavebox{\ICL@headbox}
  \newsavebox{\ICL@footbox}

  % Usage:
  %   \usepackage{letter-ic}
  %   \input{letter-data-myself}
  %   \begin{letter}
  %     \english % or \portuguese
  %     \myopinion % To get the disclaimer above the footer
  %
  %     \placedate{St. Anford, \today}
  %     \sendto{Mr. Bean//Jellybean Corp.//Candytown, NM}
  %
  %     Dear Mr. Bean,
  %     ...
  %     Sincerely,
  %     \signed{C. Rice\\Site Coordinator\\CandyExpo 2001}
  %     \writtenby{CR/ms}
  %   \end{letter}
  % 
  % In multipage letters, the letterhead and letterfoot
  % are inserted only once, in the page where the \sendto{}
  % macro is called.  To insert them elsewhere, use
  % \insertletterhead and \insertletterfoot explicitly.

  \newenvironment{letter}
    {
      \bgroup
        \newpage
        \c@page\@ne
        \raggedbottom

        \def\portuguese{
          \let\senderinst=\portsenderinst\relax
          \let\senderuniv=\portsenderuniv\relax
          \let\sendertitle=\portsendertitle\relax
          \let\senderaddress=\portsenderaddress\relax
          \let\disclaimertext=\portdisclaimertext\relax
          \def\today{
            \number\day\space de \ifcase\month\or
            janeiro\or fevereiro\or {mar\c co}\or abril\or maio\or junho\or
            julho\or agosto\or setembro\or outubro\or novembro\or dezembro\fi
            \space de \number\year
          }
          \ICL@makeheadfoot
        }

        \def\english{
          \let\senderinst=\englsenderinst\relax
          \let\senderuniv=\englsenderuniv\relax
          \let\sendertitle=\englsendertitle\relax
          \let\senderaddress=\englsenderaddress\relax
          \let\disclaimertext=\engldisclaimertext\relax
          \def\today{
            \ifcase\month\or
            January\or February\or March\or April\or May\or June\or
            July\or August\or September\or October\or November\or December\fi
            \space\number\day, \number\year
          }
          \ICL@makeheadfoot
        }
        
        % Amount of vertical overshoot for header and footer contents
        \def\ICL@headVhang{0.50in}
        \def\ICL@footVhang{0.50in}
        
        % Amount of lateral overshoot for header and footer contents
        \def\ICL@headHhang{0.0mm} % Was 6.5mm
        \def\ICL@footHhang{0.0mm} % Was 0.0mm
        
        \def\ICL@disclaimer{
        } % Redefined by \myopinion

        \def\myopinion{
          \def\ICL@disclaimer{
            \vbox{
              \offinterlineskip
              \hrule height 0pt width 0pt
              \kern 2pt
              \begin{center}
                \tiny\rm \disclaimertext
              \end{center}
              \hrule height 0pt width 0pt
            }
          }
          \ICL@makeheadfoot
        }

        \english % by default
        
        \pagestyle{plain}
        \thispagestyle{empty} 

        \normalsize\rm
        \parskip=1.0ex
        \lineskip=1pt 
        \normallineskip=1pt
        \def\baselinestretch{1}
        \topsep=0.5ex \partopsep=0pt \itemsep=0.5ex
        \rightskip=0pt \leftskip=0pt
        \rightmargin=0pt \leftmargin=0pt
        \marginparwidth=0pt \marginparsep=0pt
        \parindent=2em
        \def\insertletterhead{\begin{figure*}[t]\usebox{\ICL@headbox}\end{figure*}}
        \def\insertletterfoot{\begin{figure*}[b]\usebox{\ICL@footbox}\end{figure*}}
    }
    {
        \vfill
        \pagebreak
      \egroup
    }

  \newcommand{\placedate}[1]{%
    \begin{raggedleft}
      #1\par
    \end{raggedleft}
    \vspace{2\parskip}
  }

  \newcommand{\sendto}[1]{
    \begin{raggedright} 
      \noindent #1\par
    \end{raggedright}
    \nobreak
    \vspace{2\parskip}
    \insertletterhead
    \insertletterfoot
  }

  % Usage: \signatureblock{SIGNATURE}{NAME\\TITLE}
  \newcommand{\signatureblock}[2]{
    \par\nobreak
    \begin{raggedleft}
      ${}$
      \hbox{
        \vrule height 1.75\baselineskip depth 0.25\baselineskip width 0pt
        #1
      }\\
      ${}$
      \hbox{
        \begin{tabular}{r}
        #2
        \end{tabular}
        \quad\quad\quad\quad
      }
      \par
    \end{raggedleft}
  }

  % Usage: \signed{NAME\\TITLE}
  \newcommand{\signed}[1]{
    \signatureblock
      {~}
      {#1}  
  }
  
  % Requires the "graphics" package
  % Usage: \epssigned{SIGFILE}{SCALE}{NAME\\TITLE}
  \newcommand{\epssigned}[3]{
    \signatureblock
      {\scalebox{#2}{\hbox{\includegraphics{#1}}}}
      {#3}
  }
  
  \newcommand{\writtenby}[1]{
    \vfill
    \begin{raggedright}
      \normalsize\tt #1
    \end{raggedright}
    \vspace{0pt plus -1fill}
  }
 
% Letterhead/letterfoot elements:

  % These definitions are used to build the letter header 
  % and letter footer.  They should be redefined
  % by the user.

  \def\sendername{}
  \def\portsendertitle{}
  \def\englsendertitle{}
  \def\portsenderaddress{}
  \def\englsenderaddress{}
  \def\sendertel{}
  \def\senderfax{}
  \def\senderemail{}
  \def\senderwww{}
  \def\disclaimertext{}

  \def\ICL@logoheight{\iclogoheight}   % Nominal height of both logos
  \def\ICL@headjust{center}            % Justification of header
  
% Letterhead and letterfoot 

  \newcommand{\ICL@headfont}{
    \def\ss{\usefont{T1}{cmss}{m}{n}} 
    \let\rm=\ss\relax
    \def\tt{\usefont{T1}{cmtt}{m}{n}}
    \ss
  }

  \newcommand{\ICL@makeheadfoot}{
    \savebox{\ICL@headbox}{
      \ICL@makeheadbox{\textwidth}
    }
    \savebox{\ICL@footbox}{
      \ICL@makefootbox{\textwidth}
    }
  }

  % Usage: \ICL@makeheadbox{WIDTH}
  % Expands to a box of the given {WIDTH}, with nominal height 0pt
  % and nominal depth {\ICL@logoheight} minus {\ICL@headVhang}, that
  % actually extends {\ICL@headVhang} above the baseline.
  \newcommand{\ICL@makeheadbox}[1]{
    \begingroup
      \setbox2\hbox{
        \begin{minipage}{4.0in}
          \begin{\ICL@headjust}
            \ICL@headfont
            {\normalsize\ss \senderinst\\}
            {\normalsize\ss \senderuniv}
          \end{\ICL@headjust}
        \end{minipage}
      }    
      \setbox4\hbox{
        \begin{minipage}{4.0in}
          \begin{\ICL@headjust}
            \ICL@headfont
            {\small\ss \senderaddress}
          \end{\ICL@headjust}
        \end{minipage}
      }    
      \vtop{
        \offinterlineskip
        \hrule height 0pt width 0pt
        \kern -\ICL@headVhang
        \hbox to #1{
          \vrule width 0pt
          \kern -\ICL@headHhang
          \kern 1mm
          \unicamplogo
          \kern 6mm
          \vbox to \ICL@logoheight{
            \setlength{\textwidth}{0pt}
            \offinterlineskip
            \hrule height 0pt width 0pt
            \box2
            \vss
            \box4
            \hrule height 0pt width 0pt
          }
          \hss
          \iclogo
          \kern -\ICL@headHhang
          \kern -1mm
          \vrule width 0pt
        }
        \kern 5pt
        \hrule
        \kern 2pt
        \hrule height 0pt width 0pt
      }
    \endgroup
  }

  % Usage: \ICL@makefootbox{WIDTH}
  % Expands to a box of the given {WIDTH}, with nominal 
  % zero depth, which actually extends {\ICL@footVhang}
  % below the baseline.
  \newcommand{\ICL@makefootbox}[1]{
    \begingroup
      \setbox2\hbox{
        \begin{minipage}{2.6in}
          \begin{raggedright}
            \parindent=0pt
            \ICL@headfont\small\ss
            \sendername\\
            \sendertitle\\
            \senderwww\par
          \end{raggedright}
        \end{minipage}
      }
      \setbox4\hbox{
        \begin{minipage}{2.6in}
          \begin{raggedleft}
            \ICL@headfont\small\ss
            \senderemail\\
            \sendertel\\
            \senderfax\par
          \end{raggedleft}
        \end{minipage}
      }
      \vbox{
        \offinterlineskip
        \hrule height 0pt width 0pt
        \hbox to #1{
          \vrule height 0pt width 0pt
          \hss
          \vtop{
            \parindent 0pt
            \hrule height 0pt width 0pt
            \ICL@disclaimer
            \kern 2pt
            \hrule
            \kern 5pt
            \hbox to #1{
              \vrule height 0pt width 0pt
              \kern -\ICL@footHhang
              \box2
              \hss
              \lower 4mm \hbox{\scalebox{0.50}{\hbox{\iclogobare}}}
              \hss
              \box4
              \kern -\ICL@footHhang
              \vrule height 0pt width 0pt
            }
            \hrule height 0pt width 0pt
          }
          \hss
        }
        \kern -\ICL@footVhang
        \hrule height 0pt width 0pt
      }
    \endgroup
  }

\advance\endlinechar by 256 % Restore previous \endlinechar
