%%
%% This is file `hsetup.sty' which is part of the hletter package
%%
%% Copyright (C) 2011 Brian Housley
%%
%% It may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.2
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%% � �http://www.latex-project.org/lppl.txt
%% and version 1.2 or later is part of all distributions of LaTeX
%% version 1999/12/01 or later.
%%
%% Problems, bugs and comments to
%% brian.housley@gcs.ch
%%

%   hsetup.sty
%   ==========
%   File to setup the headings for the hletter.cls and hhead.sty
%         copyright - B. Housley,  2. July, 2011
%   You may make changes to this file if you rename it!
%
%
\RequirePackage{ifthen}
\RequirePackage{graphicx}
%\typeout{** hsetup.sty **}

\newcount\hltype
\newlength{\logoheight}
\newcommand*{\logo}[2][24mm]{\setlength{\logoheight}{#1}\gdef\logoname{#2}}
\newcommand*{\newoption}[2]{\DeclareOption{#1}{\global\hltype=#2}%
   \typeout{*** Option #2 has name #1}}
\newcommand*{\centrepos}[1]{\def\centreoffset{#1}} \centrepos{10mm}
%
% border parameters:
\newcommand*{\border}[3][40mm]{\logo[#2]{#3}%
   \tmpdima=#1 \global\hl@logopos=\tmpdima
   \global\divide \hl@logopos by\unitlength}
%
\newif\ifdraft   \draftfalse
\def\hl@lang{e}
\newcount\hlangcnt
\newcount\hl@tmp
\newcount\hl@down
\newcount\hl@up
\newcount\hl@logopos \global\hl@logopos=30
\newcount\hl@barpos \global\hl@barpos=3
\newdimen\tmpdima
\newdimen\tmpdimb
\newdimen\hl@myln
%
\global\def\pindt{0pt}
%
\input hdefine.clo  % read options for types of letter
%
\global\def\pindt{1.5em}
\DeclareOption{english}{\global\hlangcnt=0 \def\hl@lang{e}}
\DeclareOption{german}{\global\hlangcnt=1 \def\hl@lang{g}}
\DeclareOption{french}{\global\hlangcnt=2 \def\hl@lang{f}}
\DeclareOption{draft}{\drafttrue}
\ifhlet \DeclareOption*{\PassOptionsToClass{\CurrentOption}{letter}}\fi
\ProcessOptions\relax
\ifhlet \LoadClass[a4paper]{letter}
\ifdraft \setlength\overfullrule{5pt}\fi\fi
%
\ifnum\hlangcnt=0 \RequirePackage[french,german,british]{babel}%
  \AtBeginDocument{\selectlanguage{british}}\fi
\ifnum\hlangcnt=1 \RequirePackage[english,french,german]{babel}\fi
\ifnum\hlangcnt=2 \RequirePackage[german,english,french]{babel}%
   \addto\extrasfrench{\renewcommand{\enclname}{en annexe}%
    \renewcommand{\ccname}{copie \`a}}\fi
%
\setlength{\parindent}{\pindt}
%
\setlength{\unitlength}{1mm}
%
% add some strange commands to use the to-address in a tabular even
%  if it inside {..}'s (as in merge letters)
\def\dotoadd#1{\setbox0\hbox{\expandafter\cmda#1} \ifnum\myc=1\settoadd{#1}\else
    \expandafter\settoadd#1\relax\fi}
\def\settoadd#1{\global\setbox\addrbox\hbox{\begin{tabular}{@{}l@{}}#1\end{tabular}}}

\newcount\myc
\def\cmda#1{\global\myc=0 \cmdb#1\end}
\def\cmdb#1{\ifx#1\end \let\next=\relax
   \else \global\advance\myc by1 \let\next=\cmdb\fi \next}
%
%  declare some fonts for the header
%
    \ifx\newfa\@undefined
       \DeclareFixedFont{\newfa}{OT1}{phv}{bx}{n}{10pt}
       \DeclareFixedFont{\newfb}{OT1}{phv}{bx}{n}{8pt}
       \DeclareFixedFont{\newfc}{OT1}{phv}{m}{n}{8pt} \fi
%
      \def\addressA#1{\def\hl@adda{#1}} \addressA{}
      \def\addressB#1{\def\hl@addb{#1}} \addressB{}
      \def\addressC#1{\def\hl@addc{#1}} \addressC{}
      \def\extraA#1{\def\hl@extraa{#1}} \extraA{}
      \def\extraB#1{\def\hl@extrab{#1}} \extraB{}
      \def\extraC#1{\def\hl@extrac{#1}} \extraC{}
      \def\centreA#1{\def\hl@centa{#1}} \centreA{}
      \def\centreB#1{\def\hl@centb{#1}} \centreB{}
      \def\centreC#1{\def\hl@centc{#1}} \centreC{}
      \def\centreD#1{\def\hl@centd{#1}} \centreD{}
      \def\centreE#1{\def\hl@cente{#1}} \centreE{}
      \def\centreF#1{\def\hl@centf{#1}} \centreF{}
      \def\bottomL#1{\def\hl@bottl{#1}} \bottomL{}
      \def\bottomC#1{\def\hl@bottc{#1}} \bottomC{}
      \def\bottomR#1{\def\hl@bottr{#1}} \bottomR{}
      \InputIfFileExists{hlet\hl@lang.clo}{}{%
        \typeout{**}\typeout{** clo file missing: substituting hlete.clo **}\typeout{**}%
        \input hlete.clo}
%
\newbox\addrbox
%%%%%%%%%%%%%%%%%%%%%%% make the heading
\newcommand{\dohead}[4]{\cleardoublepage\thispagestyle{empty}
%  parameters: 1- top right text; 2- introduction; 3- from address; 4- to address.
%
  \let\oldbls=\baselinestretch  % for the heading do not stretch the baseline
  \renewcommand{\baselinestretch}{1.0}\small\normalsize
  \global\hl@myln=79.6mm \global\advance\hl@myln by -\oddsidemargin\relax
%
%%  \ifx#2\@empty \global\hl@down=3934474\else  % used only in idhead.sty
%%      \global\hl@down=12232300\fi             %
  \ifx#2\@empty \global\hl@down=3841240\else  % 46mm - 1" used only in hhead.sty
      \global\hl@down=12232300\fi             %
    \global\advance\hl@down by-\topmargin
    \global\advance\hl@down by-\headheight
    \global\advance\hl@down by-\headsep
%    \global\advance\hl@down by-\topskip   % should I use this?
    \hl@tmp=\unitlength \divide\hl@tmp by2
    \global\advance\hl@down by \hl@tmp
    \global\divide\hl@down by \unitlength
%
    \ifx#2\@empty \ifnum\hl@logopos=30\dimen255=\logoheight \advance\dimen255
      by-24mm \count255=\dimen255 \divide\count255 by\unitlength
         \ifnum\count255>0 \global\advance\hl@barpos by-\count255\else\count255=0\fi
      \ifnum\hl@down<0 \global\hl@up=\hl@down \global\advance\hl@up by-\count255
         \global\hl@down=-\hl@down \global\advance\hl@down by \count255\else
        \global\hl@up=-\count255 \global\advance\hl@down by\count255\fi\fi
    \else
      \ifnum\hl@down<0 \PackageWarningNoLine{hsetup}{** top-margin
          seems to be too large **}\global\hl@down=0\fi
    \fi
%
   \noindent\hspace*{\hl@myln}%
     \ifx#2\@empty
       \begin{picture}(0,\hl@down)(0,\hl@up)\else
       \begin{picture}(0,\hl@down)(0,-41)\fi
%
%  un-comment next few lines to make a dash box around the picture contents
%
%   \ifx#2\@empty \hl@tmp=\hl@down \advance\hl@tmp by 44
%        \put(-90,\hl@up){\dashbox{1}(180,\hl@tmp){}}\else
%       \hl@tmp=\hl@down \advance\hl@tmp by 41
%        \put(-90,-41){\dashbox{1}(180,\hl@tmp){}}\fi
%%
%   \ifx\@empty#1\else\put(95,40){\makebox(0,0)[tr]{\texttt{#1}}}\fi
%%
%\put(0,0){\circle*{1}}           % when testing to see where the origin is
%\showthe\hl@down \showthe\hl@up  % and what we calculated
%
   \ifx#3\@empty
%
% header with logo
%
    \ifx\logoname\@undefined\typeout{** no logo defined **}\else
       \ifx\sealbx\@undefined \newbox{\sealbx}
         \global\setbox\sealbx\hbox{\includegraphics[height=\logoheight]{\logoname}}\fi
%
     \put(-90,\hl@logopos){\makebox(0,0)[tl]{\usebox{\sealbx}}}\fi
     \put(90,30){\makebox(0,0)[tr]{%
       \renewcommand{\arraystretch}{0.7}\begin{tabular}{@{}r@{}}
       \ifx\@empty\hl@adda\else \newfa \hl@adda \\ \fi
       \ifx\@empty\hl@addb\else \newfb \hl@addb \\ \fi
       \ifx\@empty\hl@addc\else \newfb \hl@addc \\ \fi
       \rule{0pt}{.8em}%
       \ifx\@empty\hl@extraa\else \newfc \hl@extraa \\ \fi
       \ifx\@empty\hl@extrab\else \newfc \hl@extrab \\ \fi
       \ifx\@empty\hl@extrac\else \newfc \hl@extrac \\ \fi
      \end{tabular}}}
     \tmpdima=\centreoffset
     \ifnum\tmpdima<0 \advance\tmpdima by-.5mm\else\advance\tmpdima by.5mm\fi
     \hl@tmp=\tmpdima
     \divide\hl@tmp by\unitlength
     \put(\hl@tmp,30){\makebox(0,0)[tl]{%
       \renewcommand{\arraystretch}{0.7}\begin{tabular}{@{}l@{}}
       \ifx\@empty\hl@centa\else \newfa \hl@centa \\ \fi
       \ifx\@empty\hl@centb\else \newfb \hl@centb \\ \fi
       \ifx\@empty\hl@centc\else \newfb \hl@centc \\ \fi
       \rule{0pt}{.8em}%
       \ifx\@empty\hl@centd\else \newfc \hl@centd \\ \fi
       \ifx\@empty\hl@cente\else \newfc \hl@cente \\ \fi
       \ifx\@empty\hl@centf\else \newfc \hl@centf \\ \fi
      \end{tabular}}}
     \ifx\@empty\hl@bottl\else \put(-90,-244){\makebox(0,0)[bl]{\newfc\hl@bottl}}\fi
     \ifx\@empty\hl@bottc\else \put(0,-244){\makebox(0,0)[b]{\newfc \hl@bottc}}\fi
     \ifx\@empty\hl@bottr\else \put(90,-244){\makebox(0,0)[br]{\newfc\hl@bottr}}\fi
\else
%  private letter
      \hl@tmp=\hl@myln
        \divide\hl@tmp by \unitlength%
        \put(-\hl@tmp,20){\makebox(0,0)[tl]{\begin{tabular}{@{}l@{}}#3\end{tabular}}}\fi
% now do a bar if a header only otherwise the to-address
      \ifx#2\@empty \thicklines
        \ifnum\hl@bar=0\else\put(0,\hl@barpos){\line(1,0){\hl@bar}}
          \put(0,\hl@barpos){\line(-1,0){\hl@bar}}\global\hl@barpos=3\fi
        \end{picture}\par
      \else
        \dotoadd{#4}
        \settowidth{\tmpdima}{\copy\addrbox}
        \ifthenelse{\lengthtest{\tmpdima>94mm}}{%
         \ClassWarningNoLine{hletter}{** Address too wide for window **}}%
%
         {\settoheight{\tmpdima}{\copy\addrbox}
          \settodepth{\tmpdimb}{\copy\addrbox}
          \addtolength{\tmpdima}{\tmpdimb}
%
          \ifthenelse{\lengthtest{\tmpdima>44mm}}{%
          \ClassWarningNoLine{hletter}{** Address too high for window **}}{}%
         }
        \ifdraft\put(7,-41){\framebox(94,44){}}\fi
        \put(53.5,-20.5){\makebox(0,0){\copy\addrbox}}
        \linethickness{.2pt}
        \put(-104.5,-105){\line(1,0){8}}
        \put(-104.5,-63.5){\line(1,0){5}}
        \end{picture}\par
        \smallskip
        \hfill\@date\par
      \fi
\bigskip \renewcommand{\baselinestretch}{\oldbls}\small\normalsize
}  % end of dohead