lualatex / luamplib

generic TeX package - including MetaPost code in LuaTeX documents
http://ctan.org/pkg/luamplib
14 stars 11 forks source link

mplibcode is not bidi aware #129

Open u-fischer opened 2 months ago

u-fischer commented 2 months ago

(extracted from https://tex.stackexchange.com/questions/716319/has-anything-gone-wrong-with-mpchess)

mplibcode is not bidi aware. In the following example, the content of the mplibcode spills out of the bounding box if the direction is set to TRT:

\documentclass{book}
\usepackage{luamplib}

\usepackage{fontspec}
\setmainfont{amiri}
\begin{document}\showoutput
\pardir TRT \textdir TRT

هذا النص يمكن أن يتم تركيبه على أي تصميم دون مشكلة فلن يبدو وكأنه نص منسوخ، غير منظم، غير منسق، أو حتى غير مفهوم. لأنه مازال نصاً بديلاً ومؤقتاً.
\fbox{\begin{mplibcode}
beginfig(0);
color red ; red = (1, 0, 0) ;
fill (0,0)--(100,0)--(100,100)--(0,100)--cycle withcolor red;
endfig;
\end{mplibcode}}
هذا النص يمكن أن يتم تركيبه على أي تصميم دون مشكلة فلن يبدو وكأنه نص منسوخ، غير منظم، غير منسق، أو حتى غير مفهوم. لأنه مازال نصاً بديلاً ومؤقتاً.

\end{document}

image

everything is fine without the \pardir TRT \textdir TRT:

image

dohyunkim commented 2 months ago

Thanks for the report. The following patch seems to solve the problem.

diff --git a/usr/local/texlive/2024/texmf-dist/tex/luatex/luamplib/luamplib.sty b/texmf/tex/luatex/luamplib/luamplib.sty
index 6655770..2ce00a4 100644
--- a/usr/local/texlive/2024/texmf-dist/tex/luatex/luamplib/luamplib.sty
+++ b/texmf/tex/luatex/luamplib/luamplib.sty
@@ -202,7 +202,7 @@
 \ifx\mplibscratchbox\undefined \newbox\mplibscratchbox \fi
 \def\mplibstarttoPDF#1#2#3#4{%
   \prependtomplibbox
-  \hbox\bgroup
+  \hbox dir TLT\bgroup
   \xdef\MPllx{#1}\xdef\MPlly{#2}%
   \xdef\MPurx{#3}\xdef\MPury{#4}%
   \xdef\MPwidth{\the\dimexpr#3bp-#1bp\relax}%

As for RTT mode (CJK vertical writing), the bounding box still does not match. But as packages luatexko and luatexja do not seem to use RTT mode, I will leave it as it is. A new version will be uploaded soon.