%%% -*-latex-*-
%%%
%%% Typeset cards for keeper secrets.
%%%
%%% (c) 2012 Mark Wooding
%%%

%%%----- Licensing notice ---------------------------------------------------
%%%
%%% This file is part of the distorted.org.uk key management suite.
%%%
%%% distorted-keys is free software; you can redistribute it and/or modify
%%% it under the terms of the GNU General Public License as published by
%%% the Free Software Foundation; either version 2 of the License, or
%%% (at your option) any later version.
%%%
%%% distorted-keys is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%%% GNU General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License
%%% along with distorted-keys; if not, write to the Free Software Foundation,
%%% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

\documentclass[a4paper, landscape, 12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
%%\usepackage[palatino, helvetica, courier, maths = cmr]{mdwfonts}
\usepackage{graphicx}

%% Report errors with enough context that we can debug them.
\errorcontextlines=999

%% Basic layout for the cards.  We use the paragraph filling machinery, but
%% don't actually need most of the trimmings.
\parindent=0pt
\parfillskip=0pt
\pagestyle{empty}

%% Page layout: try to use most of the page.  The document class will already
%% have set up the paper size, but we do the rest here.
\hoffset=-1in \voffset=-1in
\oddsidemargin=20mm
\textwidth=\paperwidth \advance\textwidth by -2\oddsidemargin
\topmargin=20mm
\headheight=0pt \headsep=0pt
\textheight=\paperheight \advance\textheight by -2\topmargin
\AtBeginDocument{\special{papersize=\the\paperwidth,\the\paperheight}}

%% Parameters for the cards and guide rules.
\newdimen\cardwd \cardwd=82mm
\newdimen\cardht \cardht=49mm
\newdimen\guidelen \guidelen=10mm
\newdimen\rulewd \rulewd=0.6pt

%% Typesetting the secret as text.  The macro \snarf TOKEN T0 T1 ... T7
%% gathers T0 T1 ... T7 into a single argument and passes them to TOKEN, as
%% long as T0 is not \relax.  We use this to process the secret text in a
%% continuation-passing style.
\def\snarf#1#2{%
  \ifx#2\relax\let\next\empty%
  \else\def\next{\snarfdo#1#2}%
  \fi%
  \next%
}
\def\snarfdo#1#2#3#4#5#6#7#8#9{#1{#2#3#4#5#6#7#8#9}}

%% Print the left and right halves of the line, with a separator.  Use boxes
%% for the lines so that TeX will work out the width of the enclosing vbox
%% for us.  The basic usage is \line TEXT \relax ... \relax, with eight
%% \relax tokens: this is enough to complete both \snarf calls.
\def\line{\snarf\lineleft}
\def\lineleft#1{\hbox\bgroup#1 \snarf\lineright}
\def\lineright#1{#1\egroup\line}

%% Typeset a card containing a secret.  Usage is \card{INDEX}{SECRET}.
\def\card#1#2{%
  %%
  %% Make sure we're setting a paragraph.
  \leavevmode%
  %%
  %% Initial material: a stretchy space on the left.
  \hbox{}\nobreak\hfil%
  %%
  %% An alignment for the guide markers surrounding the actual card.
  \vbox{\halign{&##\cr%
    %%
    %% Top left guides.
    \vrule width \guidelen height \rulewd depth 0pt%
    \vrule width \rulewd depth 0pt height \guidelen%
    &%
    %%
    %% Top centre gap.
    \hfil%
    &%
    %%
    %% Top right guides.
    \vrule width \rulewd depth 0pt height \guidelen%
    \vrule width \guidelen height \rulewd depth 0pt%
    \cr%
    %%
    %% Left gap.
    &%
    %%
    %% The actual card.
    \vbox to \cardht{%
      %%
      %% We actually do more or less sensible typesetting.  TeX will set the
      %% box width from the hsize, and we should leave a small margin all
      %% around.
      \parfillskip=0pt plus 1fil%
      \leftskip=1em \rightskip=1em%
      \hsize=\cardwd%
      %%
      %% The heading.
      \hrule height 0pt \prevdepth = 0pt%
      \medskip%
      {\large\bfseries\textsf{\keeper} secret #1/\total}%
      %%
      %% The QR-code and the text of the secret.
      \vfil%
      $%
      \vcenter{\hbox{\includegraphics[scale = 2.4]{#1.eps}}}%
      \hfil%
      \vcenter{\ttfamily%
        \line#2%
        \relax\relax\relax\relax\relax\relax\relax\relax%
      }%
      $%
      %%
      %% And we're done.
      \vfil%
    }%
    &%
    %%
    %% Right gap.
    \cr%
    %%
    %% Bottom left guides.
    \vrule width \guidelen depth \rulewd height 0pt%
    \vrule width \rulewd depth \guidelen height 0pt%
    &%
    %% Bottom centre gap.
    \hfil%
    &%
    %% Bottom right guides.
    \vrule width \rulewd depth \guidelen height 0pt%
    \vrule width \guidelen depth \rulewd height 0pt%
    \cr%
    %%
    %% Leave a small vertical space at the bottom to separate lines of cards.
    \strut \cr%
  }}%
  %%
  %% End material: a stretchy space to match the one at the start, and then
  %% allow a break.
  \nobreak\hfil\hbox{}%
  \penalty0%
}

%%%----- That's all, folks --------------------------------------------------
