%% boustr.sty
%% Copyright 2009 Ariel Barton
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% 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.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Ariel Barton.
%
% This file is part of the tolkienfonts package.

\ProvidesPackage{boustr}[2009/05/01]

\RequirePackage{graphicx}

\def\zeroToSp@boust{\parskip=1sp plus 1pt
	\parfillskip = 1sp plus 1fil
	%\advance\leftskip by 1sp
	%\advance\rightskip by 1sp
	\def\vfil{\vskip 1sp plus 1fil}
	\def\vfill{\vskip 1sp plus 1fill}
	\abovedisplayshortskip = 1sp plus 3pt
	\postdisplaypenalty = 1
	\interlinepenalty=1}
	
\def\oddboxstep@boust{%
	\ifodd\lines@boust\global\setbox1=\vbox{\box0\unvbox1} 
	\else\global\setbox1=\vtop{\noindent\reflectbox{\box0}\unvbox1}\fi
	\global\advance\lines@boust 1
	}
\def\evenboxstep@boust{%
	\ifodd\lines@boust\global\setbox1=\vbox{\noindent\reflectbox{\box0}\unvbox1} 
	\else\global\setbox1=\vtop{\box0\unvbox1}\fi
	\global\advance\lines@boust 1
	}
\def\allboxstep@boust{%
	\global\setbox1=\vtop{\noindent\reflectbox{\box0}\unvbox1} 
	}
	
\def\fliplines@boust#1{%
	\global\lines@boust=0%
	\global\setbox1 = \vbox{}%
	\loop\Anyleft@boustfalse
	\ifdim\lastskip=0pt \ifdim\lastkern=0pt \ifnum\lastpenalty=0
		\global\setbox0=\lastbox \ifvoid0 
	\else\Anyleft@bousttrue \csname #1boxstep@boust\endcsname	\fi
	\else\Anyleft@bousttrue
		\pen@boust=\lastpenalty
		\global\setbox1=\vtop{\penalty\pen@boust\unvbox1}\unpenalty\fi
	\else\Anyleft@bousttrue
		\dimen0=\lastkern
		\global\setbox1=\vtop{\kern\dimen0\unvbox1}\unkern\fi
	\else\Anyleft@bousttrue
		\skip0=\lastskip
		\global\setbox1=\vtop{\vskip\skip0\unvbox1}\unskip\fi
	\ifAnyleft@boust\repeat} 
	
\newcount\lines@boust
\newcount\pen@boust
\newif\ifAnyleft@boust
\newif\ifboustright \boustrighttrue
\newif\ifNoindent@boust

\newenvironment{rtl}{\ifvmode\Noindent@boustfalse\else\Noindent@bousttrue\fi
\par\setbox4=\vtop\bgroup\zeroToSp@boust\ifNoindent@boust\noindent\fi}
{\egroup
\dimen0 = -\ht4 \ifdim\prevdepth>0pt\advance\dimen0 -\prevdepth\fi 
\advance\dimen0 \baselineskip
\ifdim\dimen0 <\lineskiplimit \dimen0=\lineskip \fi
\ifdim\prevdepth>0pt\vskip\dimen0\fi 
\ifNoindent@boust\else\vskip\parskip\fi
\setbox3 = \vbox{\unvcopy4\fliplines@boust{all}}
\ifvoid3\else\unvbox3\fi
\noindent\unvbox1}

\newenvironment{boustrophedon}{\ifvmode\Noindent@boustfalse\else\Noindent@bousttrue\fi
\par\setbox4=\vtop\bgroup\zeroToSp@boust\ifNoindent@boust\noindent\fi}
{\egroup
\dimen0=-\ht4\advance\dimen0-\prevdepth\advance\dimen0\baselineskip
\ifdim\dimen0<\lineskiplimit\dimen0=\lineskip\fi
\ifdim\prevdepth>0pt\vskip\dimen0\fi\ifNoindent@boust\else\vskip\parskip\fi
\setbox3=\vbox{\unvcopy4\fliplines@boust{odd}}
\ifboustright
	\ifodd\lines@boust\else\setbox3=\vbox{\unvcopy4\fliplines@boust{even}}\fi
\else
	\ifodd\lines@boust\setbox3=\vbox{\unvcopy4\fliplines@boust{even}}\fi
\fi
\ifvoid3\else\unvbox3\fi\unvbox1\noindent\ignorespacesafterend}

\newenvironment{sidewaysflip}[1]
{\ifvmode\Noindent@boustfalse\else\Noindent@bousttrue\fi
\par\setbox4=\vtop\bgroup\hsize=#1\zeroToSp@boust\ifNoindent@boust\noindent\fi}
{\egroup\rotatebox[origin=c]{90}{\reflectbox{\box4}}\par\noindent\ignorespacesafterend}