% pdcoput5.dtx 5.3 1995/04/06 -- output routine for zines

%%%@TeX-definition-file {
%%% filename       = "$texmf/tex/plain/pdcmac/pdcoput5.tex",
%%% version        = "5.3",
%%% date           = "1995/04/06",
%%% package        = "pdcmac 1.0",
%%% author         = "P. Damian Cugley",
%%% email          = "damian.cugley@comlab.ox.ac.uk",
%%% address        = "Oxford University Computing Laboratory,
%%%                   Parks Road, Oxford  OX1 3QD, UK",
%%% codetable      = "USASCII",
%%% keywords       = "TeX, plain TeX, macros",
%%% supported      = "Maybe",
%%% abstract       = "TeX output routine for magazine-style layout.
%%%                   This file was generated by running
%%%                   plain TeX on pdcoput5.dtx",
%%% copyright      = "Copyright (c) 1991-1995 P. Damian Cugley",
%%% copying        = "DO NOT DISTRIBUTE THIS FILE.
%%%                   Distribute pdcoput5.dtx only as part of the
%%%                   package it came in.",
%%% dependencies   = "",
%%% }

\message{5.3 <pdc 1995/04/06>}

\countdef\counta=255
\dimendef\dimena=0 \dimendef\dimenb=2
\toksdef\toksa=0 \toksdef\toksb=2
\long\def\qappend#1#2{%
    \toksa={\\{#2}}\toksb=\expandafter{#1}%
    \xdef#1{\the\toksb\the\toksa}%
}
\def\qremove#1#2{%
    \expandafter\glopoff#2\glopoff#1#2%
}
\long\def\glopoff\\#1#2\glopoff#3#4{%
    \gdef#3{#1}\gdef#4{#2}%
}
\newdimen\paperwd \newdimen\paperht
\newdimen\bodywd \newdimen\bodyht
\newdimen\gridwd \newdimen\gridht
\newdimen\colwd \newdimen\colht
\newdimen\topmg    \topmg=20mm
\newdimen\outermg  \outermg=20mm
\newdimen\innermg  \innermg=20mm
\newdimen\botmg    \botmg=27mm
\newskip\headlineskip  \headlineskip=10mm
\newskip\footlineskip  \footlineskip=10mm
\newdimen\caprule  \caprule=0.4pt
\newskip\capsep    \capsep=5mm
\newdimen\colrule  \colrule=0.4pt
\newdimen\colsep   \colsep=5mm
\newcount\ncols \ncols=1
\newbox\partialpage
\newbox\topbox \newbox\botbox
\newbox\leftbox \newbox\rightbox
\chardef\pagebox=255
\let\pagelist\empty
\let\collist\empty
\newtoks\everypage
\newtoks\everycaption
\newdimen\toptotal \newdimen\bottotal
\def\xxxmakecolumn#1#2#3#4#5{\xmakecolumn
    {\ifdim#1=0pt\else \kern#1 \fi
     \def\tmp{#2}\ifx\tmp\empty\else #2 \fi}
    {#3}
    {\def\tmp{#2}\ifx\tmp\empty\else #2 \fi
     \ifdim#1=0pt\else \kern#1 \fi}%
}
\def\xmakecolumn#1#2#3{%
    \vbox to \colht{
        \dimena\colht
        \setbox0=\vbox{#1}\advance\dimena-\ht0
        \ifdim\ht0>0pt
            \advance\dimena-\capsep
            \advance\dimena\topskip \advance\dimena-\baselineskip
        \fi
        \setbox4=\vbox{\prevdepth=0pt #3}%
        \advance\dimena-\ht4
        \ifdim\ht4>0pt \advance\dimena-\capsep \fi
        \ifvoid\footins\else
            \advance\dimena-\ht\footins
            \advance\dimena-\skip\footins
        \fi
        \ifvoid\topins\else
            \advance\dimena-\ht\topins
            \advance\dimena-\skip\topins
        \fi
        \splittopskip\topskip \splitmaxdepth\maxdepth
        \setbox2=\vsplit#2to\dimena
        \dimena=\ht0 \dimenb=\dp0 \unvbox0
        \ifdim\dimena>0pt
            \prevdepth=\dimenb
            \capseparator
            \dimena\baselineskip \advance\dimena-\topskip
            \kern\dimena
            \nointerlineskip
        \fi
        \ifvoid\topins\else \unvbox\topins \vskip\skip\topins \fi
        \dimena\dp2
        \ifvoid2 \vfil \else \unvbox2 \fi
        \ifvoid\footins \else
            \vskip\skip\footins \footnoterule
            \dimena=\dp\footins \unvbox\footins
        \fi
        \ifdim\ht4>0pt
            \prevdepth=\dimena
            \capseparator \kern-\prevdepth \prevdepth0pt
        \fi
        \unvbox4
    }%
}
\def\capseparator{%
    \hbox to \colwd{%
        \dimena=0.5ex \advance\dimena-0.5\caprule
        \dimenb=\dimena \advance\dimenb\caprule
        \vrule height \dimenb depth-\dimena width\colwd
    }%
}
\def\topillustration#1#2{%
    \setbox0=\vbox{#2}%
    \dimena\ht0 \dimenb\dp0 \unvbox0
    \counta\dimena \divide\counta\baselineskip
    \advance\dimena-\counta\baselineskip \advance\dimena-\topskip
    \kern-\dimena \prevdepth\dimenb
    \hsize=\colwd \vskip-\parskip \noindent#1\par
}
\def\botillustration#1#2{%
    \setbox0\vbox{\hsize=\colwd \vskip-\parskip \prevdepth=0pt #1}
    \setbox2=\vbox{\prevdepth=\dp0 #2}%
    \dimena=\ht0 \advance\dimena\dp0 \advance\dimena\ht2
    \unvbox0
    \counta\dimena \divide\counta\baselineskip
    \advance\dimena-\counta\baselineskip
    \advance\dimena-\baselineskip
    \kern-\dimena
    \unvbox2
}
\def\makebody#1{%
    \hbox to \bodywd{%
        \ifvoid\leftbox\else
            \box\leftbox
            \colseparator
        \fi
        \makecolumnfromcollist{#1}%
        \counta\ncols
        \loop \ifnum\counta>1
            \colseparator
            \makecolumnfromcollist{#1}%
            \advance\counta -1
        \repeat
        \ifvoid\rightbox\else
            \colseparator
            \box\rightbox
        \fi
    }%
}
\def\colseparator{%
    \hfil \vrule width\colrule depth0pt \hfil
}
\def\makecolumnfromcollist#1{%
    \ifx\collist\empty
        \xmakecolumn{}{#1}{}%
    \else
        \qremove\temp\collist
        \temp
    \fi
}

\def\makepage{%
    \vbox{
        \papersizespecial
        \colht\bodyht \advance\colht-\ht\partialpage
        \advance\colht-\ht\topbox \advance\colht-\ht\botbox
        \advance\colht-\dp\partialpage
        \advance\colht-\dp\topbox
        \the\everypage
        \makeheadline \nointerlineskip
        \box\partialpage \nointerlineskip
        \box\topbox \nointerlineskip
        \ifx\pagelist\empty
            \makebody\pagebox
        \else
            \qremove\temp\pagelist
            \temp
        \fi
        \unvbox\botbox
        \makefootline
    }%
}
\def\makeheadline{
    \vbox to 0pt {
        \skip0=\topskip \advance\skip0-2\ht\strutbox
        \advance\skip0-\headlineskip
        \vskip\skip0
        \hbox to \bodywd{\the\headline}
        \vss
    }
}
\def\makefootline{{
    \baselineskip=\footlineskip
    \hbox to \bodywd{\the\footline}
}}
\def\papersizespecial{{%
    \dimena=0.001\paperwd \multiply\dimena\mag
    \dimenb=0.001\paperht \multiply\dimenb\mag
    \special{papersize=\the\dimena,\the\dimenb}%
    \global\let\papersizespecial=\relax
}}

\def\pdcoutput{%
    \ifodd\pageno \hoffset=\innermg \else \hoffset=\outermg \fi
    \advance\hoffset-1 true in
    \voffset=\topmg
    \advance\voffset-1truein
    \shipout\makepage \advancepageno
    \ifnum\outputpenalty>-20000 \else \dosupereject \fi
    \unvbox\pagebox
}
\output={\pdcoutput}
\def\setpaper#1#2{%
    \paperwd=#1\relax
    \paperht=#2\relax
    \OPUTcalcbodywdht
    \OPUTcalctopskip
}
\def\OPUTcalcbodywdht{
    \bodywd=\paperwd \advance\bodywd-\outermg \advance\bodywd-\innermg
    \bodyht=\paperht \advance\bodyht-\topmg \advance\bodyht-\botmg
    \OPUTcalctopskip
}
\def\setpaperA#1{
    \ifcase#1
        \setpaper{841mm}{1189mm}\or
        \setpaper{594mm}{841mm}\or
        \setpaper{420mm}{594mm}\or
        \setpaper{297mm}{420mm}\or
        \setpaper{210mm}{297mm}\or
        \setpaper{148mm}{210mm}
    \else
        \errmessage{Dunno how big DIN A#1 paper is, sorry.}
    \fi
}
\def\OPUTcalctopskip{
    \setbox0=\hbox{AXbl()!gyJQ,}
    \topskip=\ht0 \maxdepth=\dp0
    \advance\bodyht-\topskip \divide\bodyht\baselineskip
    \multiply\bodyht\baselineskip \advance\bodyht\topskip
}
\def\OPUTcalchsizevsize{
    \hsize=\colwd
    \vsize=\bodyht
    \advance\vsize-\topskip \advance\vsize\baselineskip
    \multiply\vsize\ncols
    \advance\vsize-\baselineskip \advance\vsize\topskip
}
\def\setnkgrid#1#2{%
    \gridwd=\bodywd
    \advance\gridwd-#1\colsep \advance\gridwd\colsep \divide\gridwd#1
    \colwd#2\gridwd \advance\colwd#2\colsep \advance\colwd-\colsep
    \ncols#1 \divide\ncols#2
    \gridht=\baselineskip
    \capsep=\gridht
    \OPUTcalchsizevsize
}
\def\setncolumns#1{%
    \colwd=\bodywd
    \advance\colwd-#1\colsep \advance\colwd\colsep \divide\colwd #1
    \gridwd=\colwd
    \ncols=#1
    \OPUTcalchsizevsize
}
\setpaperA4
\setncolumns1
\newcount\RGDncols
\def\rigidbalance#1#2#3#4{%
    \setbox0=\box#1\relax \RGDncols=#2\relax \toksa={#4}%
    \begingroup\splittopskip#3\relax \vbadness=10000
        \valign{##\vfil\cr \RGDdosplits}%
    \endgroup
}
\def\RGDdosplits{
    \dimena\ht0 \divide\dimena\RGDncols
    \advance\dimena\splittopskip
    \setbox1=\vsplit0 to\dimena
    \unvbox1
    \global\advance\RGDncols-1
    \cr
    \ifnum\RGDncols>0
        \noalign{\the\toksa}
        \RGDdosplits
    \fi
}
\def\makepartialpage#1#2{
    \maybepagebreak
    \begingroup
        \output{%
            \message{[partial}
            \setbox0=\vbox{\unvbox\pagebox}%
            \global\setbox\partialpage\vbox{
                \box\partialpage \nointerlineskip
                \the\everypage
                \box\topbox \nointerlineskip
                \hbox to \bodywd{%
                    \ifvoid\leftbox\else
                        \box\leftbox \colseparator
                    \fi
                    #1%
                    \ifvoid\rightbox\else
                        \colseparator \box\rightbox
                    \fi
                }% \nointerlineskip
                \box\botbox
                #2
                \dimena\baselineskip \advance\dimena-\topskip
                \kern\dimena
            }%
            \message{page]}
        }
        \eject
    \endgroup
}
\def\rigidbalancepartialpage{
    \makepartialpage{\rigidbalance0\ncols\topskip\colseparator}
}
\def\maybepagebreak{%
    \vskip0pt plus\baselineskip \penalty-100
    \loop
        \dimena\vsize \advance\dimena-\ncols\ht\partialpage
        \advance\dimena-\ncols\ht\topbox
        \advance\dimena-\ncols\ht\botbox
    \ifdim\pagetotal>\dimena
        \eject\null
    \repeat
}
