% The package will be deleted if TRT pagedir is supported by  package pgfpages
% https://github.com/pgf-tikz/pgf/issues/911

\ProvidesPackage{pgfpages-rl}[2021/08/15 ver 1 adapt pgfpages for TRT pagedir (lualatex)]

\RequirePackage{pgfpages,ifluatex}

\ifluatex
\else
\PackageError{pgfpages-rl}{pgfpages-rl require lualatex}
\fi


\def\pgfpagespoint{\pgfpages@test@pagedir\pgfpagespoint}

\def\pgfpages@test@pagedir{%
  \let\pgfpagespoint\pgfpoint
    \ifcase\pagedirection
        % 0, TLT (latin)
    \or % 1, TRT (arabic)
      \def\pgfpagespoint##1##2{\pgfpoint{##1}{##2}\pgf@x=-\pgf@x}%
    \fi
  }


\renewcommand\pgfpagesdeclarelayout[3]{%
  \expandafter\let\csname pgfpages@layoutbefore@#1\endcsname\relax
  \expandafter\let\csname pgfpages@layout@#1\endcsname\relax
  \expandafter\newcommand\csname pgfpages@layoutbefore@#1\endcsname{#2}
  \expandafter\newcommand\csname pgfpages@layout@#1\endcsname{#3}
  }

\define@key{pgfpagesuselayoutoption}{second right}[]%
{%
  \def\pgfpageoptionfirstcenter{%
  \ifcase\pagedirection
  \pgfpoint{.5\paperwidth}{.5\paperheight}% 
  \or
  \pgfpoint{-1.5\paperwidth}{.5\paperheight}% 
  \fi}
  \def\pgfpageoptionsecondcenter{%
  \ifcase\pagedirection
  \pgfpoint{1.5\paperwidth}{.5\paperheight}% 
  \or
  \pgfpoint{-.5\paperwidth}{.5\paperheight}% 
  \fi}
  \def\pgfpageoptiontwoheight{\paperheight}%
  \def\pgfpageoptiontwowidth{2\paperwidth}%
}

\define@key{pgfpagesuselayoutoption}{second left}[]%
{%
  \def\pgfpageoptionfirstcenter{%
  \ifcase\pagedirection
  \pgfpoint{1.5\paperwidth}{.5\paperheight}% 
  \or
  \pgfpoint{-.5\paperwidth}{.5\paperheight}% 
  \fi}
  \def\pgfpageoptionsecondcenter{%
  \ifcase\pagedirection
  \pgfpoint{.5\paperwidth}{.5\paperheight}% 
  \or
  \pgfpoint{-1.5\paperwidth}{.5\paperheight}% 
  \fi}
  \def\pgfpageoptiontwoheight{\paperheight}%
  \def\pgfpageoptiontwowidth{2\paperwidth}%
}

\define@key{pgfpagesuselayoutoption}{second top}[]%
{%
  \def\pgfpageoptionfirstcenter{\pgfpagespoint{.5\paperwidth}{.5\paperheight}}%
  \def\pgfpageoptionsecondcenter{\pgfpagespoint{.5\paperwidth}{1.5\paperheight}}%
  \def\pgfpageoptiontwoheight{2\paperheight}%
  \def\pgfpageoptiontwowidth{\paperwidth}%
}

\define@key{pgfpagesuselayoutoption}{second bottom}[]%
{%
  \def\pgfpageoptionfirstcenter{\pgfpagespoint{.5\paperwidth}{1.5\paperheight}}%
  \def\pgfpageoptionsecondcenter{\pgfpagespoint{.5\paperwidth}{.5\paperheight}}%
  \def\pgfpageoptiontwoheight{2\paperheight}%
  \def\pgfpageoptiontwowidth{\paperwidth}%
}

\pgfpagesdeclarelayout{rounded corners}
{
  \def\pgfpageoptioncornerwidth{10pt}
}
{
  \pgfpagesphysicalpageoptions
  {%
    logical pages=1
  }
  \pgfpageslogicalpageoptions{1}
  {%
    center=\pgfpagespoint{.5\pgfphysicalwidth}{.5\pgfphysicalheight},%
    corner width=\pgfpageoptioncornerwidth%
  }%
}

\pgfpagesdeclarelayout{resize to}
{
  \def\pgfpageoptionborder{0pt}
}
{
  \pgfpagesphysicalpageoptions
  {%
    logical pages=1,%
    physical height=\pgfpageoptionheight,%
    physical width=\pgfpageoptionwidth%
  }
  \pgfpageslogicalpageoptions{1}
  {%
    resized width=\pgfphysicalwidth,%
    resized height=\pgfphysicalheight,%
    border shrink=\pgfpageoptionborder,%
    center=\pgfpagespoint{.5\pgfphysicalwidth}{.5\pgfphysicalheight}%
  }%
}

\pgfpagesdeclarelayout{2 on 1}
{
  \edef\pgfpageoptionheight{\the\paperwidth} % landscaped by default
  \edef\pgfpageoptionwidth{\the\paperheight}
  \def\pgfpageoptionborder{0pt}
  \def\pgfpageoptionfirstshipout{1}
}
{
  \pgfpagesphysicalpageoptions
  {%
    logical pages=2,%
    physical height=\pgfpageoptionheight,%
    physical width=\pgfpageoptionwidth,%
    current logical shipout=\pgfpageoptionfirstshipout%
  }
  \ifdim\paperheight>\paperwidth\relax
    % put side-by-side
    \pgfpageslogicalpageoptions{1}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=\pgfphysicalheight,%
      center=\pgfpagespoint{.25\pgfphysicalwidth}{.5\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{2}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=\pgfphysicalheight,%
      center=\pgfpagespoint{.75\pgfphysicalwidth}{.5\pgfphysicalheight}%
    }%
  \else
    % stack on top of one another
    \pgfpageslogicalpageoptions{1}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpagespoint{.5\pgfphysicalwidth}{.75\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{2}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpagespoint{.5\pgfphysicalwidth}{.25\pgfphysicalheight}%
    }%
  \fi    
}

\pgfpagesdeclarelayout{4 on 1}
{
  \edef\pgfpageoptionheight{\the\paperheight} 
  \edef\pgfpageoptionwidth{\the\paperwidth}
  \edef\pgfpageoptionborder{0pt}
}
{
  \pgfpagesphysicalpageoptions
  {%
    logical pages=4,%
    physical height=\pgfpageoptionheight,%
    physical width=\pgfpageoptionwidth%
  }
  \pgfpageslogicalpageoptions{1}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.5\pgfphysicalwidth,%
    resized height=.5\pgfphysicalheight,%
    center=\pgfpagespoint{.25\pgfphysicalwidth}{.75\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{2}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.5\pgfphysicalwidth,%
    resized height=.5\pgfphysicalheight,%
    center=\pgfpagespoint{.75\pgfphysicalwidth}{.75\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{3}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.5\pgfphysicalwidth,%
    resized height=.5\pgfphysicalheight,%
    center=\pgfpagespoint{.25\pgfphysicalwidth}{.25\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{4}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.5\pgfphysicalwidth,%
    resized height=.5\pgfphysicalheight,%
    center=\pgfpagespoint{.75\pgfphysicalwidth}{.25\pgfphysicalheight}%
  }%
}

\pgfpagesdeclarelayout{6 on 1}
{
  \edef\pgfpageoptionheight{\the\paperwidth} % landscaped by default
  \edef\pgfpageoptionwidth{\the\paperheight}
  \def\pgfpageoptionborder{0pt}
}
{
  \pgfpagesphysicalpageoptions
  {%
    logical pages=6,%
    physical height=\pgfpageoptionheight,%
    physical width=\pgfpageoptionwidth%
  }
  \ifdim\paperheight>\paperwidth\relax
    % put side-by-side
    \pgfpageslogicalpageoptions{1}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.25\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpagespoint{.167\pgfphysicalwidth}{.75\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{2}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.25\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpagespoint{.5\pgfphysicalwidth}{.75\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{3}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.25\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpagespoint{.833\pgfphysicalwidth}{.75\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{4}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.25\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpagespoint{.167\pgfphysicalwidth}{.25\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{5}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.25\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpagespoint{.5\pgfphysicalwidth}{.25\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{6}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.25\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpagespoint{.833\pgfphysicalwidth}{.25\pgfphysicalheight}%
    }%
  \else
    % stack on top of one another
    \pgfpageslogicalpageoptions{1}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=.25\pgfphysicalheight,%
      center=\pgfpagespoint{.25\pgfphysicalwidth}{.833\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{2}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=.25\pgfphysicalheight,%
      center=\pgfpagespoint{.75\pgfphysicalwidth}{.833\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{3}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=.25\pgfphysicalheight,%
      center=\pgfpagespoint{.25\pgfphysicalwidth}{.5\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{4}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=.25\pgfphysicalheight,%
      center=\pgfpagespoint{.75\pgfphysicalwidth}{.5\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{5}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=.25\pgfphysicalheight,%
      center=\pgfpagespoint{.25\pgfphysicalwidth}{.167\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{6}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=.25\pgfphysicalheight,%
      center=\pgfpagespoint{.75\pgfphysicalwidth}{.167\pgfphysicalheight}%
    }%
  \fi    
}

\pgfpagesdeclarelayout{8 on 1}
{
  \edef\pgfpageoptionheight{\the\paperwidth} % landscaped by default
  \edef\pgfpageoptionwidth{\the\paperheight}
  \def\pgfpageoptionborder{0pt}
}
{
  \pgfpagesphysicalpageoptions
  {%
    logical pages=8,%
    physical height=\pgfpageoptionheight,%
    physical width=\pgfpageoptionwidth%
  }
  \ifdim\paperheight>\paperwidth\relax
    % put side-by-side
    \pgfpageslogicalpageoptions{1}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.25\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpagespoint{.125\pgfphysicalwidth}{.75\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{2}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.25\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpagespoint{.375\pgfphysicalwidth}{.75\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{3}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.25\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpagespoint{.625\pgfphysicalwidth}{.75\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{4}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.25\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpagespoint{.875\pgfphysicalwidth}{.75\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{5}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.25\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpagespoint{.125\pgfphysicalwidth}{.25\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{6}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.25\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpagespoint{.375\pgfphysicalwidth}{.25\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{7}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.25\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpagespoint{.625\pgfphysicalwidth}{.25\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{8}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.25\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpagespoint{.875\pgfphysicalwidth}{.25\pgfphysicalheight}%
    }%
  \else
    % stack on top of one another
    \pgfpageslogicalpageoptions{1}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=.25\pgfphysicalheight,%
      center=\pgfpagespoint{.25\pgfphysicalwidth}{.875\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{2}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=.25\pgfphysicalheight,%
      center=\pgfpagespoint{.75\pgfphysicalwidth}{.875\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{3}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=.25\pgfphysicalheight,%
      center=\pgfpagespoint{.25\pgfphysicalwidth}{.625\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{4}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=.25\pgfphysicalheight,%
      center=\pgfpagespoint{.75\pgfphysicalwidth}{.625\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{5}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=.25\pgfphysicalheight,%
      center=\pgfpagespoint{.25\pgfphysicalwidth}{.375\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{6}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=.25\pgfphysicalheight,%
      center=\pgfpagespoint{.75\pgfphysicalwidth}{.375\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{7}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=.25\pgfphysicalheight,%
      center=\pgfpagespoint{.25\pgfphysicalwidth}{.125\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{8}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=.25\pgfphysicalheight,%
      center=\pgfpagespoint{.75\pgfphysicalwidth}{.125\pgfphysicalheight}%
    }%
  \fi    
}

\pgfpagesdeclarelayout{16 on 1}
{
  \edef\pgfpageoptionheight{\the\paperheight} 
  \edef\pgfpageoptionwidth{\the\paperwidth}
  \edef\pgfpageoptionborder{0pt}
}
{
  \pgfpagesphysicalpageoptions
  {%
    logical pages=16,%
    physical height=\pgfpageoptionheight,%
    physical width=\pgfpageoptionwidth%
  }
  \pgfpageslogicalpageoptions{1}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.25\pgfphysicalwidth,%
    resized height=.25\pgfphysicalheight,%
    center=\pgfpagespoint{.125\pgfphysicalwidth}{.875\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{2}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.25\pgfphysicalwidth,%
    resized height=.25\pgfphysicalheight,%
    center=\pgfpagespoint{.375\pgfphysicalwidth}{.875\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{3}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.25\pgfphysicalwidth,%
    resized height=.25\pgfphysicalheight,%
    center=\pgfpagespoint{.625\pgfphysicalwidth}{.875\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{4}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.25\pgfphysicalwidth,%
    resized height=.25\pgfphysicalheight,%
    center=\pgfpagespoint{.875\pgfphysicalwidth}{.875\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{5}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.25\pgfphysicalwidth,%
    resized height=.25\pgfphysicalheight,%
    center=\pgfpagespoint{.125\pgfphysicalwidth}{.625\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{6}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.25\pgfphysicalwidth,%
    resized height=.25\pgfphysicalheight,%
    center=\pgfpagespoint{.375\pgfphysicalwidth}{.625\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{7}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.25\pgfphysicalwidth,%
    resized height=.25\pgfphysicalheight,%
    center=\pgfpagespoint{.625\pgfphysicalwidth}{.625\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{8}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.25\pgfphysicalwidth,%
    resized height=.25\pgfphysicalheight,%
    center=\pgfpagespoint{.875\pgfphysicalwidth}{.625\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{9}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.25\pgfphysicalwidth,%
    resized height=.25\pgfphysicalheight,%
    center=\pgfpagespoint{.125\pgfphysicalwidth}{.375\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{10}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.25\pgfphysicalwidth,%
    resized height=.25\pgfphysicalheight,%
    center=\pgfpagespoint{.375\pgfphysicalwidth}{.375\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{11}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.25\pgfphysicalwidth,%
    resized height=.25\pgfphysicalheight,%
    center=\pgfpagespoint{.625\pgfphysicalwidth}{.375\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{12}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.25\pgfphysicalwidth,%
    resized height=.25\pgfphysicalheight,%
    center=\pgfpagespoint{.875\pgfphysicalwidth}{.375\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{13}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.25\pgfphysicalwidth,%
    resized height=.25\pgfphysicalheight,%
    center=\pgfpagespoint{.125\pgfphysicalwidth}{.125\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{14}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.25\pgfphysicalwidth,%
    resized height=.25\pgfphysicalheight,%
    center=\pgfpagespoint{.375\pgfphysicalwidth}{.125\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{15}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.25\pgfphysicalwidth,%
    resized height=.25\pgfphysicalheight,%
    center=\pgfpagespoint{.625\pgfphysicalwidth}{.125\pgfphysicalheight}%
  }%
  \pgfpageslogicalpageoptions{16}
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.25\pgfphysicalwidth,%
    resized height=.25\pgfphysicalheight,%
    center=\pgfpagespoint{.875\pgfphysicalwidth}{.125\pgfphysicalheight}%
  }%
}



\def\pgfpages@buildshipoutbox{%
  \setbox\pgfpages@shipoutbox=\vbox{{%
    \set@typeset@protect%
    \offinterlineskip%
    \pgfsys@beginpicture%
    \pgf@cpn=1\relax%
    \loop%
      \setbox0=\hbox to \csname pgfpages@p@\the\pgf@cpn @width\endcsname{%
        \hskip1in%
        \vbox to \csname pgfpages@p@\the\pgf@cpn @height\endcsname%
          {\vskip1in\offinterlineskip\expandafter\copy\csname
            pgfpages@box@\the\pgf@cpn\endcsname\vss}\hss}%
      \pgfsys@beginscope%
        % Translate lower left corner
        \expandafter\ifx\csname pgfpages@p@\the\pgf@cpn @center\endcsname\relax%
        \else%
          \pgflowlevel{\pgftransformshift{\csname pgfpages@p@\the\pgf@cpn @center\endcsname}}%
        \fi%
        \expandafter\ifx\csname pgfpages@p@\the\pgf@cpn @scale\endcsname\relax%
        \else%
          \pgflowlevel{\pgftransformscale{\csname pgfpages@p@\the\pgf@cpn @scale\endcsname}}%
        \fi%
        \expandafter\ifx\csname pgfpages@p@\the\pgf@cpn @xscale\endcsname\relax%
        \else%
          \pgflowlevel{\pgftransformxscale{\csname pgfpages@p@\the\pgf@cpn @xscale\endcsname}{1}}%
        \fi%
        \expandafter\ifx\csname pgfpages@p@\the\pgf@cpn @yscale\endcsname\relax%
        \else%
          \pgflowlevel{\pgftransformyscale{\csname pgfpages@p@\the\pgf@cpn @yscale\endcsname}}%
        \fi%
        \pgfscope%
        \expandafter\ifx\csname pgfpages@p@\the\pgf@cpn @rotation\endcsname\relax%
        \else%
        \pgflowlevel{\pgftransformrotate{\csname
            pgfpages@p@\the\pgf@cpn @rotation\endcsname}}%
        \fi%
        \pgfutil@tempdima=\csname pgfpages@p@\the\pgf@cpn @width\endcsname\relax%
        \pgfutil@tempdimb=\csname pgfpages@p@\the\pgf@cpn @height\endcsname\relax%
        \pgflowlevel{\pgftransformshift{\pgfpagespoint{-.5\pgfutil@tempdima}{-.5\pgfutil@tempdimb}}}%
        \expandafter\ifx\csname pgfpages@p@\the\pgf@cpn @bordercode\endcsname\relax%
        \else%
          \pgfpathmoveto{\pgfpointorigin}%
          \pgfpathlineto{\pgfpagespoint{\wd0}{0pt}}%
          \pgfpathlineto{\pgfpagespoint{\wd0}{\ht0}}%
          \pgfpathlineto{\pgfpoint{0pt}{\ht0}}%
          \pgfpathclose%
          {\csname pgfpages@p@\the\pgf@cpn @bordercode\endcsname}%
        \fi%
        \expandafter\ifx\csname pgfpages@p@\the\pgf@cpn @cornerwidth\endcsname\relax%
        \else%
          {\ifcase\pagedirection
          \or%
          \pgftransformcm{-1}{0}{0}{1}{\pgfpointorigin}%
          \fi%
            \expandafter\pgfutil@tempdima\csname pgfpages@p@\the\pgf@cpn @cornerwidth\endcsname\relax%
            \color{black}
            \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\pgfutil@tempdima}{\pgfutil@tempdima}}%
            \pgfpathrectangle{\pgfpoint{0pt}{\ht0-\pgfutil@tempdima}}{\pgfpoint{\pgfutil@tempdima}{\pgfutil@tempdima}}%
            \pgfpathrectangle{\pgfpoint{\wd0-\pgfutil@tempdima}{0pt}}{\pgfpoint{\pgfutil@tempdima}{\pgfutil@tempdima}}%
            \pgfpathrectangle{\pgfpoint{\wd0-\pgfutil@tempdima}{\ht0-\pgfutil@tempdima}}{\pgfpoint{\pgfutil@tempdima}{\pgfutil@tempdima}}%
            \pgfusepath{fill}%
            \pgfpathmoveto{\pgfpoint{0pt}{\pgfutil@tempdima}}
            \pgfpathcurveto{\pgfpoint{0pt}{0.555\pgfutil@tempdima}}{\pgfpoint{.555\pgfutil@tempdima}{0pt}}{\pgfpoint{\pgfutil@tempdima}{0pt}}
            \pgfpathlineto{\pgfpoint{\wd0-\pgfutil@tempdima}{0pt}}
            \pgfpathcurveto{\pgfpoint{\wd0-.555\pgfutil@tempdima}{0pt}}{\pgfpoint{\wd0}{.555\pgfutil@tempdima}}{\pgfpoint{\wd0}{\pgfutil@tempdima}}
            \pgfpathlineto{\pgfpoint{\wd0}{\ht0-\pgfutil@tempdima}}
            \pgfpathcurveto{\pgfpoint{\wd0}{\ht0-.555\pgfutil@tempdima}}{\pgfpoint{\wd0-.555\pgfutil@tempdima}{\ht0}}{\pgfpoint{\wd0-\pgfutil@tempdima}{\ht0}}
            \pgfpathlineto{\pgfpoint{\pgfutil@tempdima}{\ht0}}
            \pgfpathcurveto{\pgfpoint{.555\pgfutil@tempdima}{\ht0}}{\pgfpoint{0pt}{\ht0-.555\pgfutil@tempdima}}{\pgfpoint{0pt}{\ht0-\pgfutil@tempdima}}
            \pgfpathclose
            \pgfusepath{clip}
            \color{white}
            \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\wd0}{\ht0}}
            \pgfusepath{fill}
          }
        \fi%
        \ht0=0pt%
        \wd0=0pt%
        \dp0=0pt%
        \pgfsys@hbox0%
        \endpgfscope%
      \pgfsys@endscope%
    \ifnum\pgf@cpn<\pgf@logicalpages%
      \advance \pgf@cpn by 1\relax%  
    \repeat%
    \pgfsys@endpicture%
    }}%
}
