Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

\special issues in right to left mode #8

Open
vafakhalighi opened this issue May 14, 2020 · 0 comments
Open

\special issues in right to left mode #8

vafakhalighi opened this issue May 14, 2020 · 0 comments
Assignees
Labels
bug Something isn't working engine special whatsit

Comments

@vafakhalighi
Copy link
Contributor

The TeX--XeT algorithm reverses whatsits and as a result, the \special pairs will be placed in the wrong order. For instance, color or links or any other thing which is achieved via driver is fundamentally broken in right to left mode.

For instance, the following minimal Plain TeX document illustrates the issue with color.

\TeXXeTstate=1
This is \beginR\special{color push rgb 1 0 0}RED COLOR\special{color pop}\endR{} as you can see.
\bye

As mentioned above, this is Engine issue. Peter was working towards fixing this (See this TeXLive commit and this one.

The bidi package fixes this in a limited way; if the colored text only is on a single line then text will be colored correctly. However, if the colored text is more than a line (it breaks across several lines), then the resulting colored text is wrong.

This can be fixed in the macro level as the following example illustrates.

\documentclass{article}


\usepackage{xepersian}

\settextfont{Yas}

\makeatletter

\long\def\bidi@addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}

\bgroup \catcode`!=3 \catcode`?=3
\gdef\bidi@replacestrings#1#2{\long\def\bidi@replacestringsA##1#1##2!{%
   \ifx!##2!\bidi@addto\bidi@tmpb{##1}\else\bidi@addto\bidi@tmpb{##1#2}\bidi@replacestringsA##2!\fi}%
   \edef\bidi@tmpb{\expandafter}\expandafter\bidi@replacestringsA\bidi@tmpb?#1!%
   \long\def\bidi@replacestringsA##1?{\def\bidi@tmpb{##1}}\expandafter\bidi@replacestringsA\bidi@tmpb
}
\egroup



\newbox\bidi@color@start@box  \newbox\bidi@color@end@box
\setbox\bidi@color@start@box=\hbox{\special{color pop}}
\setbox\bidi@color@end@box=\hbox{\special{color push rgb 1 0 0}}%

\def\bidicolor#1{\def\bidi@tmpb{#1}\bidi@replacestrings{ }{\bidi@specialspace}%
   {\copy\bidi@color@start@box\bidi@tmpb\copy\bidi@color@end@box}%
}
\def\bidi@specialspace{%
   \discretionary{\copy\bidi@color@end@box}{\copy\bidi@color@start@box}{\kern\fontdimen2\font}%      
   \nobreak\hskip 0pt plus\fontdimen3\font minus\fontdimen4\font
}

\makeatother

\begin{document}
این یک متن معمولی است
\bidicolor{این متن با رنگ قرمز نوشته می‌شود که در یک سطر قرار می‌گیرد}
و این متن بعد آن است که به سطر بعد می‌رود و
\bidicolor{%
حالا یک متن رنگی می‌نویسیم که در چندین سطر قرار بگیرید تا کارایی رنگی کردن متن را ببینیم کنون ای خردمند وصف خرد          بدین جایگه گفتن اندرخورد
کنون تا چه داری بیار از خرد          که گوش نیوشنده زو برخورد
خرد بهتر از هر چه ایزد بداد          ستایش خرد را به از راه داد
خرد رهنمای و خرد دلگشای          خرد دست گیرد به هر دو سرای
ازو شادمانی وزویت غمیست          وزویت فزونی وزویت کمیست
خرد تیره و مرد روشن روان          نباشد همی شادمان یک زمان
چه گفت آن خردمند مرد خرد          که دانا ز گفتار از برخور
}
و ادامه متن که با رنگ سیاه نوشته می‌شود همانطوری که می‌بینید هیچ مشکلی ندارد و رنگ چکه نمی‌کند.
\end{document}

The only disadvantage of this is that there is no hyphenation but that is rarely a problem since we do not really have hyphenation in right to left text.

Perhaps this approach can be extended to support any packages which uses \special (e.g. color, xcolor, hyperref, etc).

@vafakhalighi vafakhalighi added bug Something isn't working engine labels May 14, 2020
@vafakhalighi vafakhalighi self-assigned this May 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working engine special whatsit
Projects
None yet
Development

No branches or pull requests

1 participant