pgf
pgf copied to clipboard
Create an adapted pgfpicture environment for RTL text
Brief outline of the proposed feature
In RTL documents (pdflatex, xelatex and lualatex engine) \begin{pgfpicture}{<xmin>}{<ymin>}{<xmax>}{<ymax>} produce contents of environment to appears in wrong diretion, my hope is to addapt \pgfpicture for RTL context
https://tex.stackexchange.com/q/566856/54817
Usage example
Example with xelatex and pdflatex
\documentclass{article}
\usepackage{pgf}
\usepackage[showframe]{geometry}
\TeXXeTstate=1
\parindent0pt
\begin{document}
\beginR
A \beginL\begin{pgfpicture}{0pt}{0bp}{50pt}{3cm}
\color{red}
\pgfpathrectangle{\pgfpointorigin}{\pgfpoint{10cm}{2cm}}
\pgfpathmoveto{\pgfpointorigin}
\pgfpathlineto{\pgfpoint{10cm}{2cm}}
\pgfusepath{stroke}
\end{pgfpicture}\endL B
\beginR
A \beginL\begin{pgfpicture}
\color{red}
\pgfpathrectangle{\pgfpointorigin}{\pgfpoint{10cm}{2cm}}
\pgfpathmoveto{\pgfpointorigin}
\pgfpathlineto{\pgfpoint{10cm}{2cm}}
\pgfusepath{stroke}
\end{pgfpicture}\endL B
\end{document}
Example with lualatex
\documentclass{article}
\usepackage{pgf}
\usepackage[showframe]{geometry}
\parindent0pt
\begin{document}
\pagedir TRT \pardir TRT \textdir TRT \bodydir TRT
A {\textdir TLT\begin{pgfpicture}{0pt}{0bp}{50pt}{3cm}
\color{red}
\pgfpathrectangle{\pgfpointorigin}{\pgfpoint{10cm}{2cm}}
\pgfpathmoveto{\pgfpointorigin}
\pgfpathlineto{\pgfpoint{10cm}{2cm}}
\pgfusepath{stroke}
\end{pgfpicture}} B%
A {\textdir TLT\begin{pgfpicture}
\color{red}
\pgfpathrectangle{\pgfpointorigin}{\pgfpoint{10cm}{2cm}}
\pgfpathmoveto{\pgfpointorigin}
\pgfpathlineto{\pgfpoint{10cm}{2cm}}
\pgfusepath{stroke}
\end{pgfpicture}} B
\end{document}

Result in RTL context should look like this ( may this should be controlled with a boolean for direction LTR or RTL)

Can you take a look into this (only for lualatex engine)
\documentclass{article}
\usepackage{pgf,tikz}
\usepackage[showframe]{geometry}
\parindent0pt
\makeatletter
\newif\ifpgf@rldir
\pgf@rldirfalse
\def\pgfrldirtrue{\pgf@rldirtrue}
\def\pgfrldirfalse{\pgf@rldirfalse}
\newdimen\pgf@rlpicminx
\newdimen\pgf@rlpicmaxx
\def\pgfpicture{%
\begingroup%
\pgfpicturetrue%
\global\advance\pgf@picture@serial@count by1\relax%
\edef\pgfpictureid{pgfid\the\pgf@picture@serial@count}%
\let\pgf@nodecallback=\pgfutil@gobble%
\pgf@picmaxx=-16000pt\relax%
\pgf@picminx=16000pt\relax%
\pgf@picmaxy=-16000pt\relax%
\pgf@picminy=16000pt\relax%
\ifpgf@rldir%
\pgf@rlpicminx=16000pt\relax%
\pgf@rlpicmaxx=-16000pt\relax%
\fi%
\pgf@relevantforpicturesizetrue%
\pgf@resetpathsizes%
\pgfutil@ifnextchar\bgroup\pgf@oldpicture\pgf@picture}
\def\pgfsys@typesetpicturebox#1{%
\pgf@ya=\pgf@shift@baseline\relax%
\advance\pgf@ya by-\pgf@picminy\relax%
%
%
\advance\pgf@picmaxy by-\pgf@picminy\relax% maxy is now the height
\advance\pgf@picmaxx by-\pgf@picminx\relax% maxx is now the width
\setbox#1=\hbox{\hskip-\pgf@picminx\lower\pgf@picminy\box#1}%
\ifpgf@rldir%
\advance\pgf@rlpicmaxx by-\pgf@rlpicminx\relax%
\setbox#1=\hbox{\hskip\pgf@rlpicmaxx\lower\pgf@picminy\box#1}%
\fi%
\ht#1=\pgf@picmaxy%
\wd#1=\pgf@picmaxx%
\dp#1=0pt%
\leavevmode%
\pgf@xa=\pgf@trimleft@final\relax \ifdim\pgf@xa=0pt \else\kern\pgf@xa\fi
\raise-\pgf@ya\box#1 %
\pgf@xa=\pgf@trimright@final\relax \ifdim\pgf@xa=0pt \else\kern\pgf@xa\fi
}
\def\pgf@protocolsizes#1#2{%
\ifpgf@rldir
\ifdim#1<\pgf@rlpicminx\global\pgf@rlpicminx#1\fi%
\ifdim#1>\pgf@rlpicmaxx\global\pgf@rlpicmaxx#1\fi%
\fi%
\ifpgf@relevantforpicturesize%
\ifdim#1<\pgf@picminx\global\pgf@picminx#1\fi%
\ifdim#1>\pgf@picmaxx\global\pgf@picmaxx#1\fi%
\ifdim#2<\pgf@picminy\global\pgf@picminy#2\fi%
\ifdim#2>\pgf@picmaxy\global\pgf@picmaxy#2\fi%
\ifpgf@size@hooked%
\let\pgf@size@hook@x#1\let\pgf@size@hook@y#2\pgf@path@size@hook%
\fi%
\fi%
\ifdim#1<\pgf@pathminx\global\pgf@pathminx#1\fi%
\ifdim#1>\pgf@pathmaxx\global\pgf@pathmaxx#1\fi%
\ifdim#2<\pgf@pathminy\global\pgf@pathminy#2\fi%
\ifdim#2>\pgf@pathmaxy\global\pgf@pathmaxy#2\fi%
}
\def\pgfusepath#1{%
\pgf@up@path@neededfalse%
\pgf@up@draw@arrowsfalse%
\let\pgf@up@stroke\pgfutil@empty%
\let\pgf@up@fill\pgfutil@empty%
\let\pgf@up@clip\pgfutil@empty%
\let\pgf@up@bb\pgfutil@empty%
\pgfset{#1}%
\expandafter\def\expandafter\pgf@up@action\expandafter{\csname pgfsys@\pgf@up@fill\pgf@up@stroke\endcsname}%
\ifnum\pgf@tips@mode=2\relax%
\pgf@up@path@neededtrue%
\fi%
\ifnum\pgf@tips@mode=4\relax%
\pgf@up@path@neededtrue%
\fi%
\ifpgf@up@path@needed%
\else%
\pgfsyssoftpath@setcurrentpath\pgfutil@empty%
\fi%
\ifx\pgf@up@stroke\pgfutil@empty%
\ifx\pgf@up@fill\pgfutil@empty%
\let\pgf@up@action=\pgfutil@empty%
\ifx\pgf@up@clip\pgfutil@empty%
\else%
% only clipping
\let\pgf@up@action=\pgfsys@discardpath%
\fi%
\fi%
\fi%
\pgfsyssoftpath@getcurrentpath\pgf@last@processed@path
\pgfprocessround{\pgf@last@processed@path}{\pgf@last@processed@path}% change the path
\pgfsyssoftpath@setcurrentpath\pgf@last@processed@path%
%
% Check whether the path is stroked. If so, add half the line width
% to the bounding box.
%
\ifpgf@relevantforpicturesize%
\ifx\pgf@up@stroke\pgfutil@empty%
\else%
\ifdim\pgf@picmaxx=-16000pt\relax%
\else%
\pgf@x=\pgf@pathminx\advance\pgf@x by-.5\pgflinewidth%
\ifdim\pgf@x<\pgf@picminx\global\pgf@picminx\pgf@x\fi%
\pgf@y=\pgf@pathminy\advance\pgf@y by-.5\pgflinewidth%
\ifdim\pgf@y<\pgf@picminy\global\pgf@picminy\pgf@y\fi%
\pgf@x=\pgf@pathmaxx\advance\pgf@x by.5\pgflinewidth%
\ifdim\pgf@x>\pgf@picmaxx\global\pgf@picmaxx\pgf@x\fi%
\pgf@y=\pgf@pathmaxy\advance\pgf@y by.5\pgflinewidth%
\ifdim\pgf@y>\pgf@picmaxy\global\pgf@picmaxy\pgf@y\fi%
\fi%
\fi%
\else%
\ifx\pgf@up@stroke\pgfutil@empty%
\else%
\ifdim\pgf@picmaxx=-16000pt\relax%
\else%
\ifpgf@rldir%
\pgf@x=\pgf@pathminx\advance\pgf@x by-.5\pgflinewidth%
\ifdim\pgf@x<\pgf@rlpicminx\global\pgf@rlpicminx\pgf@x\fi%
\pgf@x=\pgf@pathmaxx\advance\pgf@x by.5\pgflinewidth%
\ifdim\pgf@x>\pgf@rlpicmaxx\global\pgf@rlpicmaxx\pgf@x\fi%
\fi%
\fi%
\fi%
\fi%
%
\ifx\pgf@up@clip\pgfutil@empty%
\ifx\pgf@up@stroke\pgfutil@empty%
\ifx\pgf@up@action\pgfutil@empty%
\ifnum\pgf@tips@mode=2\relax%
\pgf@up@draw@arrows@only%
\fi%
\ifnum\pgf@tips@mode=4\relax%
\pgf@up@draw@arrows@only%
\fi%
\else%
\pgfsyssoftpath@invokecurrentpath%
\pgf@up@action%
\fi%
\else%
\pgfgetpath\pgf@arrowpath%
\pgf@path@setup@tempswa%
\pgfprocesscheckclosed{\pgf@arrowpath}{\pgfutil@tempswafalse}%
\pgf@path@check@proper%
\ifpgfutil@tempswa%
\pgf@check@for@arrow@and@animation%
\pgf@prepare@end@of@path%
\begingroup%
\pgf@prepare@start@of@path%
\fi%
\pgfsyssoftpath@invokecurrentpath%
\pgf@up@action%
\ifdim\pgfinnerlinewidth>0pt\relax%
\pgf@stroke@inner@line%
\fi%
\ifpgfutil@tempswa%
\pgf@add@arrow@at@start%
\endgroup%
\pgf@add@arrow@at@end%
\fi%
\fi%
\else%
\pgfsyssoftpath@invokecurrentpath%
\pgfsys@clipnext%
\pgf@up@action%
\pgf@relevantforpicturesizefalse%
\fi%
\pgf@up@bb%
\pgfsyssoftpath@setcurrentpath\pgfutil@empty%
\pgf@resetpathsizes%
\ignorespaces%
}
\expandafter\def\expandafter\pgfsys@beginpicture\expandafter{\pgfsys@beginpicture\bodydir TLT}
\makeatother
\begin{document}
\pagedir TRT \pardir TRT \textdir TRT \bodydir TRT
A{\pgfrldirtrue\begin{pgfpicture}{50pt}{0pt}{200pt}{60pt}
\color{red}
\pgfpathrectangle{\pgfpointorigin}{\pgfpoint{10cm}{2cm}}
\pgfpathmoveto{\pgfpointorigin}
\pgfpathlineto{\pgfpoint{10cm}{2cm}}
\pgfusepath{stroke}
\end{pgfpicture}}B%
A{\pgfrldirtrue%
\begin{tikzpicture}
\draw (0,0) rectangle (150pt,60pt);
\end{tikzpicture}}B
\pardir TLT \textdir TLT
A\begin{pgfpicture}{50pt}{0pt}{200pt}{60pt}
\color{red}
\pgfpathrectangle{\pgfpointorigin}{\pgfpoint{10cm}{2cm}}
\pgfpathmoveto{\pgfpointorigin}
\pgfpathlineto{\pgfpoint{10cm}{2cm}}
\pgfusepath{stroke}
\end{pgfpicture}B
\end{document}
