# Why shapepar figure shifts to the left?

I modified \SH@restack so that it can typeset RTL shapepars but now the shapepar figures are not centered, can anyone tell me why?

\documentclass{article}
\usepackage[left=1cm,right=1cm]{geometry}
\usepackage{shapepar}
\usepackage{bidi}
\makeatletter
\def\SH@restack#1#2#3{% line num, shift, width
\unpenalty \skip@\lastskip \unskip \unpenalty
\setbox\@tempboxa\lastbox
\setbox\SH@boxa\vbox{\hbox{\if@RTL\beginR\fi\vbox{%
\ifhbox\@tempboxa
\moveright-\SH@posold \hbox to#3
{\hskip\leftskip \unhbox\@tempboxa \unskip \hskip\leftskip}%
\nobreak
\fi
\vskip\skip@
\ifnum\SH@nline>\z@ \begingroup
\vskip-\count@\baselineskip
\endgroup\fi
\unvbox\SH@boxa}\if@RTL\endR\fi}}%
\SH@nline#1\relax
% Keep track of line num for last box (\SH@highline) and
% lowest box on page (\SH@lowline)
\ifnum\SH@highline=-\@m \SH@highline\SH@nline \fi
\ifnum\SH@lowline<\SH@nline \SH@lowline\SH@nline \fi
}
\makeatother
\begin{document}
\heartpar{%
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
}

\bigskip
\setRTL
\heartpar{%
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
}
\end{document}

-

The 'why' is easy enough, and is due to the fact that TeX--XeT is pretty simplistic. To see the issue, try the demo

\documentclass{article}
\TeXXeTstate=1 %
\usepackage{lipsum}
\begin{document}

\lipsum[1]

\beginR
ABC
\endR

\end{document}


with either pdfLaTeX or XeLaTeX. Notice that the RTL paragraph is in the 'wrong' place, and that if you extend it:

\documentclass{article}
\TeXXeTstate=1 %
\usepackage{lipsum}
\begin{document}

\lipsum[1]

\beginR
\lipsum[1]
\endR

\end{document}


then so is the parindent. This all happens as \beginR only reverses the direction that each hbox is filled in the line of the paragraph, not where they are placed relative to the 'outline' of the paragraph. Thus the calculations made by shapepar move the text toward the left margin here, not away. Fixing that needs more adjustments to the code, but really just serves to highlight the issues with TeX--XeT.

-