%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Usage Notes:
%% * The basic document structure is
%%     \documentclass{INTposter}
%%     \photo{SeattleDowntown}
%%     \title{INT}
%%     \begin{document{
%%     \section*{2013 Programs}
%%     \event[label]{title}{dates}{organizers}
%%     \event[label]{title}{dates}{organizers}
%%     ...
%%     \section*{2014 Programs}
%%     \event[label]{title}{dates}{organizers}
%%     ...
%%     \end{document}
%% * Divisions should be specified using something like
%%     \section*{2013 Programs}
%%   The first word (the date in this case) will be extracted an processed
%%   specially.
%% * Each individual program should be entered using
%%     \event[label]{title}{dates}{organizers}
%%   The label (i.e. "INT-13-1a") may be ignored depending on the style.
%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%% To Do:
%% - Section spacing. Maybe make vertical spacing flexible to fill a certain
%%   block.
%% - Calculate baseline of last line of text to position qr code properly
%%   (hacked right now)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Base
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{ifxetex}        % For fonts we require XeLaTeX
\RequireXeTeX
\ProvidesClass{INTposter}
\PassOptionsToClass{16pt}{scrartcl}
\LoadClass{scrartcl}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Options
\RequirePackage{xkeyval}
\newlength\int@l@bleed          % Amount to bleed photos etc.
\newlength\int@l@trim           % Trim space
\DeclareOptionX{bleed}[0.125in]{\setlength\int@l@bleed{#1}}
\DeclareOptionX{trim}[1.0in]{\setlength\int@l@trim{#1}}
\ProcessOptionsX\relax

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Parameters
\setlength\parindent{1em}       % Used for general indentation and spacing
\newlength\sectionskip          % Space before section heading.
\setlength\sectionskip{1.25\baselineskip} 
\newlength\int@w@margin         % Text margin at right
\setlength\int@w@margin{6em}

\def\int@skip@beforesection{0pt}       % No skip for first heading
\def\int@skip@aftersection{\baselineskip}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Packages
\RequirePackage{calc}           % Calculations (not so useful... only works for
                                % lengths)
\PassOptionsToPackage{papersize={11in+2\int@l@trim,    % 11x17 with trim
                                 17in+2\int@l@trim},
                      layout=ansibpaper,  % Straight 11x17
                      layoutoffset=\int@l@trim,
                      showcrop}
                    {geometry}
\RequirePackage{geometry}       % Controls page layout, margins etc.
\let\width\Gm@layoutwidth
\let\height\Gm@layoutheight

\RequirePackage{fontspec}       % Fonts

% See this post about color models in LaTeX:
% http://tex.stackexchange.com/a/9973/6903
\PassOptionsToPackage{cmyk}{xcolor}
\RequirePackage{xcolor}         % Colours

\RequirePackage{etoolbox}       % Helpful tools for latex programming
\RequirePackage{suffix}         % Help defining * commands like \section*
\RequirePackage{xstring}        % Maniuplation of strings in latex

\RequirePackage{fp}             % Floating point arithmetic
\PassOptionsToPackage{absolute,
                      %showboxes
                      }{textpos}
\RequirePackage{textpos}        % Position of text-boxes
%\RequirePackage{graphicx}       % Inclusion of graphics.
\graphicspath{{images/}}

% The xcoffins package allows us to position boxes (coffins) precisely.  We are
% just using textpos for now.
%\RequirePackage{xcoffins}
%\NewCoffin\TitleBox

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fonts
\defaultfontfeatures{SmallCapsFeatures={LetterSpace=0}}
\setkomafont{title}{\fontsize{80}{90}\fontspec{Edelsans}}
\setmainfont{Calluna}
\setkomafont{section}    {\fontsize{35}{44}\fontspec{Calluna Sans}%
                           \addfontfeature{SmallCapsFeatures={LetterSpace=8}}%
                                           \scshape\mdseries\selectfont%
                                           \MakeLowercase}
\newkomafont{elabel}     {\fontsize{16}{22}\fontspec{Calluna Sans}%
  \scshape\selectfont}
\newkomafont{etitle}     {\fontsize{20}{22}\fontspec{Calluna Sans}}
\newkomafont{edate}      {\fontsize{16}{22}\fontspec{Calluna Sans}}
\newkomafont{eorganizers}{\fontsize{14}{14}\fontspec{Calluna Sans}}

\newkomafont{photocredit}{\fontsize{10}{12}\fontspec{Calluna Sans}}
%%% Special fonts for INT Logo
\def\intlogotext{%
  \noindent
  \def\@ff{\fontsize{32}{40}%
           \addfontfeature{LetterSpace=17.5}%
           \addfontfeature{WordSpace=1.41}}%
  \fontspec{Miso Bold}\@ff
  INSTITUTE {\fontspec{Miso Light}\@ff f\kern5pt% Need kern bc let.space ignored
    \fontspec{Satellite}\@ff or}
  NUCLEAR THEORY}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Vertical text flow.  This was used for the "INT" logo in the left block.
\newcommand{\insertbetween}[2][ ]{%
  {\def\result{}%
    \StrSplit{#2}{1}{\firstchar}{\rest}%
    \eappto\result{\firstchar#1}%
    \IfStrEq{\rest}{}{}{\int@insertbetween{#1}{\rest}}%
    \result%
  }
}

\renewcommand{\insertbetween}[2][ ]{%
  {\def\result{}%
    \StrSplit{#2}{1}{\firstchar}{\rest}%
    \edef\result{\firstchar#1}%
    \IfStrEq{\rest}{}{}{\int@insertbetween{#1}{\rest}}%
    \centering\result%
  }
}
% Process the remaining characters recursively
\newcommand\int@insertbetween[2]{%
  \StrSplit{#2}{1}{\firstchar}{\rest}%
  \eappto\result{\firstchar #1}%
  \IfStrEq{\rest}{}{}{\int@insertbetween{#1}{\rest}}%
}

\newcommand{\vertical}[1]{{\insertbetween[\par]{#1}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Page Layout
%

% Add an offset corresponding to the trim layout.
\TPGrid[\int@l@trim,\int@l@trim]{1}{1}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Colors

\definecolor{backgroundgray}{cmyk}{0.0, 0.0, 0.0, 0.5}
\definecolor{richblack}{cmyk}{0.0,0.4,0.0,1}

\def\int@c@margin{richblack}           % Left margin strip
\def\int@c@margintext{white}           % Text in left margin
\def\int@c@text{black}                 % Main text colour
\def\int@c@textbackground{white}       % Text in left margin
\def\int@c@logotext{white}             % Logo text (on photo)
\def\int@c@photocredit{backgroundgray} % Photo credit

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Image

% Use this to specify the image
\newlength\int@h@photo
\newlength\int@w@photo
\newlength\int@h@photocredit
\newlength\int@w@photocredit
\def\int@photoargs{}
\def\int@photo{\PackageWarning{INTposter}{No Photo Specified}}
\NewDocumentCommand \photo { O{} m } {
  % Get the height of the image using the method discussed here:
  % http://tex.stackexchange.com/a/3658/6903
  \def\int@photo{%
    \includegraphics[width=\int@w@photo+\int@l@bleed]{#2}%
  }
  \def\int@photocredit{\usekomafont{photocredit}{#1}}
  \settoheight\int@h@photo{\int@photo}
  \addtolength\int@h@photo{-\int@l@bleed}
  \settoheight\int@h@photocredit{\int@photocredit}
  \settowidth\int@w@photocredit{\int@photocredit}
}

\def\int@qrcode{}
\newcommand\qrcode[1]{\gdef\int@qrcode{#1}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculations
% There are various ways of calculating things in LaTeX.  Still not sure of the
% best way.  Compare the packages fp, calc, and eTeX primatives.
%
% Here is a helpful function from
% http://tex.stackexchange.com/a/6424/6903
\newcommand*{\int@DivideLengths}[2]{%
  \strip@pt\dimexpr\number\numexpr\number\dimexpr#1\relax*65536/%
  \number\dimexpr#2\relax\relax sp\relax
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Layout.
% The 11x17 page has aspect ratio 1.5454... is almost described by a pentagon:
% the height of a pentagon, which, in terms of the base, is 
%   sin(2*pi/5) + sin(2*2*pi/5) = 1.5388417685876268
% Natural division points are given by the inner vertices of the inscribed star,
% which gives a left margin that is 1/(sec(pi/5)+4)=0.1909830056 of the width.
\newlength\int@w@border
\setlength\int@w@border{0.1909830056\width}
\setlength\int@w@photo{\width-\int@w@border}

\AtBeginDocument{
  \pagestyle{empty}
  \textblockcolour{red}

  % Black strip on left (bleed off page)
  \begin{textblock*}{\int@w@border+\int@l@bleed}(-\int@l@bleed,-\int@l@bleed)
    \textblockcolour{\int@c@margin}
    \noindent
    \rule{0pt}{\height+2\int@l@bleed}
  \end{textblock*}

  % INT logo in border.  Numbers are adjusted manually
  \begin{textblock*}{\int@w@border}(0.0175\width,0.0242\height) % top aligned
    \textblockcolour{}          % Transparent
    \includegraphics[width=0.56287\int@w@border]{logo_inv}
  \end{textblock*}

  % QR code
  \newlength\int@h@qr
  \newlength\int@w@qr
  \setlength\int@h@qr{1.25in}
  \setlength\int@w@qr{1.25in}
  \setlength\int@h@qr{1.214in}
  \setlength\int@w@qr{1.214in}
  \def\int@qrindent{0.125in}   % Adjusted by hand to match the top of last entry
  \begin{textblock*}{\int@w@qr}(\width-\int@w@qr-\int@qrindent,
                                \height-\int@h@qr-\int@qrindent)
    \noindent\includegraphics[width=\int@w@qr,
                              height=\int@h@qr,
                              ]{\int@qrcode}
  \end{textblock*}
  
  % Image at top of page and photo credit.
  \begin{textblock*}{\int@w@photo+\int@l@bleed}(\int@w@border,-\int@l@bleed)
    \noindent
    \int@photo
  \end{textblock*}
  \begin{textblock*}{\int@w@photocredit}
                    (\int@w@border + \int@w@photo 
                    - \int@w@photocredit - \int@qrindent,
                    \int@h@photo + 2pt)
    \textblockcolour{}          % Transparent
    \color{\int@c@photocredit}
    \noindent\int@photocredit                    
  \end{textblock*}

  % Add the INT logo text, carefully positioned to make sure it is legible.
  \newlength\int@textoffset
  \setlength\int@textoffset{\int@w@border +\parindent}
  \setlength\textwidth{\width - \int@textoffset - \int@w@margin}
  \begin{textblock*}{\textwidth}(\int@textoffset, 0.45in)
    \textblockcolour{}          % Transparent
    \color{\int@c@logotext}
    \intlogotext
  \end{textblock*}
}

\AfterEndPreamble{
  % Start of actual text
  \begin{textblock*}{\textwidth}(\int@textoffset,%
                                 \int@h@photo + \parindent)
    \textblockcolour{}          % Transparent
    \color{\int@c@text}
}
\AtEndEnvironment{document}{
   \end{textblock*}
}

% Use this to remove content like workshops
\newcommand\exclude[1]{}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Section typesetting
\RequirePackage{xparse}
\NewDocumentCommand \sectionheading { o m } {
  \begingroup
    \usekomafont{sectioning}
    \vspace{\int@skip@beforesection}
    \gdef\int@skip@beforesection{\sectionskip} % Skip for subsequent sections
    \noindent
    \makebox[0pt]{\llap{\color{\int@c@margintext}%
        \usekomafont{section}{#1}\hspace{2\parindent}}}%
    \noindent\usekomafont{section}{#2}
  \endgroup
  \vspace{\int@skip@aftersection}
}

% Redefine \section and \section* to do the same thing with an optional argumnet
% to specify the year.
\RenewDocumentCommand \section { s o m } {\sectionheading[#2]{#3}}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Event typesetting
% Here is the \event command that typesets each event
\iffalse
\newcommand\event[4][]{
  \def\label{\MakeLowercase{#1}}
  \def\title{#2}
  \def\date{#3}
  \def\organizers{#4}
  \noindent
  \makebox[0pt]{\llap{
      \color{\int@c@margintext}
      \raisebox{0pt}[0pt][0pt]{\parbox[t]{8em}{%
        {\usekomafont{elabel}\label}\newline
        {\usekomafont{edate}\date}\\
      }}%
      \hspace{2\parindent}}}%
  {\usekomafont{etitle}\title}\\
  {\usekomafont{eorganizers}\organizers}\\
  \vspace{\baselineskip}
}

\else
% Version 2: no labels
\newcommand\event[4][]{
  \def\label{\MakeLowercase{#1}}
  \def\title{#2}
  \noexpandarg\StrSubstitute[1]{#3}{--}{\\to }[\date]
  \def\organizers{#4}
  \noindent
  \makebox[0pt]{\llap{
      \color{\int@c@margintext}
      \raisebox{0pt}[0pt][0pt]{\parbox[t]{8em}{%
          \raggedleft\usekomafont{edate}\date
        }}%
      \hspace{2\parindent}}}%
  {\raggedright
    \noindent\usekomafont{etitle}\title\par} % Need \par here to get leading.
  \vspace{5pt}
  {\noindent\usekomafont{eorganizers}\organizers\par}
  \vspace{\baselineskip}
}
\fi
