% rputover.sty
% Version 1.0
% Martin J. Osborne, 2017.3.25
% Based partially on the answer of Werner at
% http://tex.stackexchange.com/questions/353748/is-there-a-variant-of-rput-in-pstricks-that-preserves-background-fill-colors 
% Released under the LaTeX project public license 1.3

\RequirePackage{pst-node}

\newcounter{rpo@pstfiglabel}
\newcounter{rpo@coverable}
\newlength{\rpo@fboxrule}
\setlength{\rpo@fboxrule}{\fboxrule}

\def\rputover{%
  \fboxrule=0pt
  \@ifnextchar[%
          {\@ifnextchar({\@putoveron}{\@putoveror}}%
          {\@ifnextchar({\@putovernn}{\@putovernr}}%
}%

\def\@putoveror[#1]#2(#3,#4)#5{%
  \rput[#1]{#2}(#3,#4){\psDefBoxNodes{label\therpo@pstfiglabel}{\fbox{#5}}}%
  \stepcounter{rpo@pstfiglabel}%
  \setlength{\fboxrule}{\rpo@fboxrule}%
}%

\def\@putoveron[#1](#2,#3)#4{%
  \rput[#1](#2,#3){\psDefBoxNodes{label\therpo@pstfiglabel}{\fbox{#4}}}%
  \stepcounter{rpo@pstfiglabel}%
  \setlength{\fboxrule}{\rpo@fboxrule}%
}%

\def\@putovernr#1(#2,#3)#4{%
  \rput{#1}(#2,#3){\psDefBoxNodes{label\therpo@pstfiglabel}{\fbox{#4}}}%
  \stepcounter{rpo@pstfiglabel}%
  \setlength{\fboxrule}{\rpo@fboxrule}%
}%

\def\@putovernn(#1,#2)#3{%
  \rput(#1,#2){\psDefBoxNodes{label\therpo@pstfiglabel}{\fbox{#3}}}%
  \stepcounter{rpo@pstfiglabel}%
  \setlength{\fboxrule}{\rpo@fboxrule}%
}%

% Optional argument of \coverable is intended to be the region (x0,y0)(x1,y1) with
% respect to which the reverse clip is done.  The default, (-\maxdimen,-\maxdimen)(\maxdimen,\maxdimen),
% seems to work fine, so the possibility of specifying the region manually doesn't seem to be necessary (or useful).
\def\coverable{%
  \@ifnextchar[{\@coverableo}{\@coverablen}%
}%              

\def\@coverableo[(#1,#2)(#3,#4)]#5{\@coverableg{#1}{#2}{#3}{#4}{#5}}
\def\@coverablen#1{\@coverableg{-\maxdimen}{-\maxdimen}{\maxdimen}{\maxdimen}{#1}}

\def\@coverableg#1#2#3#4#5{%
  \psclip{%
    \pscustom[linestyle=none]{%
      \loop%
        \psline[liftpen=2](label\therpo@coverable:bl)(label\therpo@coverable:br)(label\therpo@coverable:tr)(label\therpo@coverable:tl)(label\therpo@coverable:bl)%
        \stepcounter{rpo@coverable}%
        \ifnum\value{rpo@coverable}<\value{rpo@pstfiglabel}%
      \repeat%
      \psline[liftpen=2](#1,#2)(#1,#4)(#3,#4)(#3,#2)%
      }%
    }%
    #5
  \endpsclip%
}

