%%
%% This is file `bkltprnt.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% booklet.dtx  (with options: `prnt')
%% 
%% -----------------------------------------------------------------
%%   Author: Peter Wilson (Herries Press)
%%   Maintainer: Will Robertson (will dot robertson at latex-project dot org)
%%   Copyright 2000--2005 Peter R. Wilson
%% 
%%   This work may be distributed and/or modified under the
%%   conditions of the LaTeX Project Public License, either
%%   version 1.3c of this license or (at your option) any
%%   later version: <http://www.latex-project.org/lppl.txt>
%% 
%%   This work has the LPPL maintenance status "maintained".
%%   The Current Maintainer of this work is Will Robertson.
%% 
%%   This work consists of the files listed in the README file.
%% -----------------------------------------------------------------
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{bkltprnt}[2009/09/02 v0.7b support for booklet printing]

\newdimen\@targetwidth
\newdimen\@targetheight
\newdimen\@sourcewidth
\newdimen\@sourceheight

\newif\if@leftpage
  \@leftpagetrue
\newbox\@leftpage
\newbox\@rightpage
\newcount\@physicalpage

\multiply\maxdeadcycles by 40\relax

\newcount\bookletpage
  \bookletpage=0
\newcount\leftpagenumber
\newcount\rightpagenumber

\newcommand*{\twoupp@geboxes}{%
  \advance\bookletpage\@ne
  \expandafter\newbox\csname bookletbox\the\bookletpage\endcsname
  \ifnum \bookletpage < \c@signature
    \twoupp@geboxes
  \fi}
\AtBeginDocument{\twoupp@geboxes\bookletpage=\z@}

{\catcode`\p=12\catcode`\t=12\gdef\@@inv@@mag#1pt#2{\def#2{#1}}}
\def\target#1#2#3{%
  \mag #1\relax
  \@targetwidth=1000pt
  \divide\@targetwidth by #1\relax
  \expandafter\@@inv@@mag\the\@targetwidth\inv@targetmag
  \@targetwidth=#2\relax
  \@targetwidth=\inv@targetmag\@targetwidth
  \@targetheight=#3\relax
  \@targetheight=\inv@targetmag\@targetheight}

\def\source#1#2#3{%
  \@sourcewidth=1000pt
  \divide\@sourcewidth by #1\relax
  \expandafter\@@inv@@mag\the\@sourcewidth\inv@sourcemag
  \@sourcewidth=#2\relax
  \@sourcewidth=\inv@sourcemag\@sourcewidth
  \@sourceheight=#3\relax
  \@sourceheight=\inv@sourcemag\@sourceheight}

\renewcommand*{\setpdftargetpages}{%
  \ifprintoption
    \setlength{\pdfpageheight}{\@targetheight}%
    \setlength{\pdfpagewidth}{\@targetwidth}%
    \let\fixpdflayout\relax
  \fi}
\renewcommand*{\setdvipstargetpages}{%
  \ifprintoption
    \AtBeginDvi{\special{papersize=\the\@targetwidth,\the\@targetheight}}%
    \let\fixdvipslayout\relax
  \fi}

\def\targetBooklet{%
  \def\ship@@@leftpage{\save@booklet\@leftpage}%
  \def\ship@@@rightpage{\save@booklet\@rightpage}%
  \@leftpagefalse}
\targetBooklet

\def\targettopbottom{%
  \def\make@@halfpage{\make@@halftopbottom}%
  \def\make@fullpage{\make@fulltopbottom}%
  \sidebysidefalse}

\DeclareOption{four}{\setcounter{signature}{4}}
\DeclareOption{eight}{\setcounter{signature}{8}}
\DeclareOption{sixteen}{\setcounter{signature}{16}}
\DeclareOption{thirtytwo}{\setcounter{signature}{32}}
\DeclareOption{twouparticle}{\twouparticle}
\DeclareOption{landscape}{\twouplandscape}
\DeclareOption{largetypeblock}{\twoupplain}
\DeclareOption{1to1}{\twouponetoone}

\expandafter\let\csname &normal@shipout\endcsname\shipout
\def\shipout{%
  \if@leftpage
    \global\@leftpagefalse
    \def\next{\afterassignment\ship@leftpage\global\setbox\@leftpage=}%
  \else
    \global\@leftpagetrue
    \def\next{\afterassignment\ship@rightpage\global\setbox\@rightpage=}%
  \fi
  \next}

\def\ship@leftpage{%
  \ifvoid\@leftpage\aftergroup\ship@@leftpage\else\ship@@leftpage\fi}
\def\ship@rightpage{%
  \ifvoid\@rightpage\aftergroup\ship@@rightpage\else\ship@@rightpage\fi}

\def\ship@@leftpage{\make@halfpage\@leftpage\ship@@@leftpage}
\def\ship@@rightpage{\make@halfpage\@rightpage\ship@@@rightpage}

\def\make@halfpage#1{%
  \dp#1=\z@
  \setbox#1=\vbox to\@sourceheight{%
    \vskip \inv@sourcemag in
    \vskip \voffset
    \hbox to\@sourcewidth{\hskip\inv@sourcemag in\hskip\hoffset\box#1\hss}%
    \vss}%
  \make@@halfpage#1}

\def\make@@halfsidebyside#1{%
  \global\setbox#1=\vbox to\@targetheight{\vss
    \hbox to.5\@targetwidth{\hss\box#1\hss}\vss}}
\def\make@@halfpage{\make@@halfsidebyside}

\def\make@@halftopbottom#1{%
  \global\setbox#1=\vbox to.5\@targetheight{\vss
    \hbox to\@targetwidth{\hss\box#1\hss}\vss}}

\def\ship@twoup{%
  \begingroup
    \voffset=-\inv@targetmag in
    \hoffset=\voffset
    \global\advance\@physicalpage by 1
    \count\z@=\@physicalpage
    \csname &normal@shipout\endcsname\make@fullpage
  \endgroup}

\def\make@fullsidebyside{%
  \hbox{\box\@leftpage\pagesep@sidebyside\box\@rightpage}}
\def\make@fulltopbottom{%
  \vbox{\offinterlineskip\box\@leftpage\pagesep@topbottom\box\@rightpage}}

\def\make@fullpage{\make@fullsidebyside}

\def\pagesep@sidebyside{%
  \begingroup
    \advance\pageseplength by \pagesepoffset
    \pagesepwidth=\inv@targetmag\pagesepwidth
    \kern -.5\pagesepwidth
    \vrule height \inv@targetmag\pageseplength
           depth -\inv@targetmag\pagesepoffset
           width \pagesepwidth
    \kern -.5\pagesepwidth
  \endgroup}
\def\pagesep@topbottom{%
  \begingroup
    \pagesepwidth=\inv@targetmag\pagesepwidth
    \vskip -.5\pagesepwidth
    \moveright\inv@targetmag\pagesepoffset\hbox{%
      \vrule height\pagesepwidth width\inv@targetmag\pageseplength}%
    \vskip -.5\pagesepwidth
  \endgroup}

\begingroup
\let\newbox\relax
\gdef\save@booklet#1{%
  \begingroup
    \globaldefs=1
    \ifnum\c@sigcount=\z@\bookletpage=0\fi
    \advance\bookletpage by 1
    \addtocounter{sigcount}{1}
%%%%    \expandafter\newbox\csname bookletbox\the\bookletpage\endcsname
    \expandafter\setbox\csname bookletbox\the\bookletpage\endcsname\box#1%
  \endgroup
  \ifnum\c@sigcount=\c@signature     %%% PW
    \twoup@eject%
    \setcounter{sigcount}{0}%
  \fi}
\endgroup

\def\make@bookletpage#1{%
  \setbox\ifodd#1\@rightpage\else\@leftpage\fi=%
    \expandafter\box\csname bookletbox\the#1\endcsname}

\def\booklet@loop{%
  \count\z@\rightpagenumber
  \make@bookletpage\leftpagenumber
  \make@bookletpage\rightpagenumber
  \ship@twoup
  \Booklet@@loop
}

\def\Booklet@@loop{%
  \advance\rightpagenumber by 1
  \advance\leftpagenumber by -1
  \ifnum\leftpagenumber<\rightpagenumber\else\expandafter\booklet@loop\fi}

\def\twoupemptypage{\shipout\hbox{}}

\def\twoup@eject{
  \leftpagenumber\bookletpage
  \advance\leftpagenumber by 3
  \divide\leftpagenumber by 4
  \multiply\leftpagenumber by 4
  \rightpagenumber=1
  \ifnum\leftpagenumber>\bookletpage
    \setbox\@leftpage\hbox{}%
    \make@@halfpage\@leftpage
    \loop
      \setbox\@rightpage\copy\@leftpage
      \save@booklet\@rightpage
    \ifnum\leftpagenumber>\bookletpage
    \repeat
  \fi
  \booklet@loop}

\begingroup
\catcode`\>=9\relax
>>\gdef\twoupclearpage{\clearpage\twoup@eject}
>>\expandafter\@temptokena\expandafter{\enddocument}
>>\xdef\enddocument{\noexpand\twoupclearpage\the\@temptokena}
\endgroup

\def\TwoupWrites{%
  \let\TwoupSaved@write\write
  \let\TwoupSaved@read\read
  \let\TwoupSaved@openout\openout
  \let\TwoupSaved@closeout\closeout
  \def\write{\TwoupSaved@write-1{}\immediate\TwoupSaved@write}%
  \def\read{\TwoupSaved@write-1{}\immediate\TwoupSaved@read}%
  \def\openout{\TwoupSaved@write-1{}\immediate\TwoupSaved@openout}%
  \def\closeout{\TwoupSaved@write-1{}\immediate\TwoupSaved@closeout}%
  \let\TwoupWrites\relax}

\def\twouparticle{\target{\magstepminus1}{\paperheight}{\paperwidth}}
\def\twoupplain{\target{\magstepminus2}{\paperheight}{\paperwidth}}
\def\twouplandscape{\target{\magstepminus2}{\paperwidth}{\paperheight}%
  \targettopbottom}
\def\twouponetoone{\target{\magstep0}{\paperheight}{\paperwidth}}

\source{\magstep0}{\paperwidth}{\paperheight}
\pagesepwidth 0pt
\pageseplength 6.5in
\pagesepoffset 1in
\TwoupWrites

\ExecuteOptions{twouparticle,thirtytwo}
\ProcessOptions

\endinput
%%
%% End of file `bkltprnt.sty'.
