[Tex/LaTex] putting a box around each line of text in a paragraph

bidiboxeslanguagesright-to-left

In my environment I would like to automatically make a box around each individual line of text in a paragraph so that I can operate on each line. I think this should really be done in LaTeX rather than plain TeX as not to break text coloring and wrapping. I have been on this for some time now and it is still not obvious how to do this safely with LaTeX so any help would be greatly appreciated.

This is for typesetting boustrophedonic writing as seen in Safaic, Sabean and some ancient Greek writing systems as well as reverse-boustrophedonic text as seen in Rongorongo (Easter Island). For boustrophedon I can just mirror every other line with \reflectbox because this writing direction moves like an ox turns in the field. For rongrongo I can use \rotatebox[origin=c]{180}{ because every other line is rotated 180 degrees.

Best Answer

Do you mean like this? enter image description here

This is just a starter really, I put it together without too much care over finer points. In particular it assumes that each paragraph just produces boxes and inter-line penalty and glue (one of each). If the paragraph contains any vadjust material (\vspace or inserts (begin{figure} or fancy specials (any random package:-) then you are doomed, or at least need to work harder. Also it re-inserts the interline glue that was there before but that might be visually wrong if you have flipped the text so it might be better to discard that and let TeX find its own inter-line glue again.

But anyway this hopefully gets you started:

This takes more care of inter line and inter paragraph space at the start.

\documentclass{article}

\usepackage{graphicx}

\setlength\textheight{20\baselineskip}
\raggedbottom


\def\a{One two three. }
\def\b{Red, Green, Blue. }
\def\c{\a\a\b\b\a\a\a\b\b\b\a\a\b\a\b\a\b}

\makeatletter

\def\hmmx{%
 \@tempcnta\z@
 \loop
  \advance\@tempcnta\@ne
  \setbox\z@\lastbox
  \global\dimen@i\ht\z@
  \skip@\lastskip\unskip
  \count@\lastpenalty\unpenalty
 \ifdim 5sp=\wd\z@
 \else
  \global\setbox\@ne\vbox{%
  \penalty\count@
  \vskip\skip@
  \ifodd\@tempcnta
   \hbox{\reflectbox{\box\z@}}%
  \else
   \box\z@
  \fi
  \unvbox\@ne}%
 \repeat}%


\def\hmm#1{%
 \dimen@\prevdepth
 \setbox0\vbox{%
 \hbox{\vrule\@height\z@\@width5sp\@depth\dimen@}\par
 #1\par
 \hmmx}%
 \unvbox\@ne}

\makeatother

\begin{document}

text with descender: g

\hmm{\c}

\hmm{\a\c\b\c}

\end{document}