%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Part of the ptx2pdf macro package for formatting USFM text
% copyright (c) 2007 by SIL International
% written by Jonathan Kew
%
% Permission is hereby granted, free of charge, to any person obtaining  
% a copy of this software and associated documentation files (the  
% "Software"), to deal in the Software without restriction, including  
% without limitation the rights to use, copy, modify, merge, publish,  
% distribute, sublicense, and/or sell copies of the Software, and to  
% permit persons to whom the Software is furnished to do so, subject to  
% the following conditions:
%
% The above copyright notice and this permission notice shall be  
% included in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,  
% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF  
% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  
% NONINFRINGEMENT. IN NO EVENT SHALL SIL INTERNATIONAL BE LIABLE FOR  
% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF  
% CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION  
% WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%
% Except as contained in this notice, the name of SIL International  
% shall not be used in advertising or otherwise to promote the sale,  
% use or other dealings in this Software without prior written  
% authorization from SIL International.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% cropmark support for the ptx2pdf package

%+c_makecropmarks
\newif\ifBookOpenLeft\BookOpenLeftfalse % The friend of RTL. In monoglot these two typically go together, in diglot they may not. ifOpenLeft controls where binding gutter goes, etc.
\newif\ifOuterGutter % Does the user want the 'binding gutter' on the other side of the page?
\newif\ifCropMarks
%\font\idf@nt=cmtt10 scaled 700 % font for the marginal job information
\font\idf@nt="Source Code Pro" at 8pt
\def\id@@@{}% just in case.
%\font\idf@nt="Times New Roman" at 10pt % FIXME: Use something not bitmap?
\newbox\topcr@p \newbox\bottomcr@p
\def\makecr@ps{% construct the cropmark boxes for top and bottom of page
  \trace{cov}{makecrops bleeds=\the\bl@eds, pw=\the\PaperWidth}%
  \trace{o}{id line (\the\pageno): \id@line}%
  \global\setbox\topcr@p=\vbox to 0pt{\toks0=\everypar\everypar={}\vss
    \hbox to \PaperWidth{%
      \kern -30pt \kern -\bl@eds
      \vrule height .2pt depth .2pt width 25pt  % top of page (left)
      \kern 4.8pt \kern\bl@eds
      \dimen0=30pt \advance\dimen0 \bl@eds
      \dimen1=-5pt \advance\dimen1 -\bl@eds
      \vrule height \dimen0 depth \dimen1 width .4pt    % left page (top)
      \kern -30.4pt \kern -\bl@eds
      \dimen0=\topm@rgin \advance\dimen0 by .4pt
      \vrule height -\topm@rgin depth \dimen0 width 15pt    % top margin (left)
      \kern 15pt \kern \bl@eds
      \hss
      \raise\dimexpr 12pt + \bl@eds \hbox to 0pt{\hss\vtop{{\hsize\PaperWidth \everypar={}
        \lineskiplimit=0pt \baselineskip=10pt
        \leftskip=0pt plus 1fil \rightskip=\leftskip \parfillskip=\leftskip
        \noindent \hfil\beginL\idf@nt \id@line  \endL\par
        \ifdim\sp@newidth>0pt\hbox{\kern 0.5\PaperWidth\kern-0.5\sp@newidth\vrule height 6pt depth .5pt width .4pt
          \kern\sp@newidth\kern -0.4pt\vrule height 6pt depth .5pt width .4pt}\fi}}\hss}%
      \hss
      \dimen0=30pt \advance\dimen0 \bl@eds
      \dimen1=-5pt \advance\dimen1 -\bl@eds
      \vrule height \dimen0 depth \dimen1 width .4pt    % right page (top)
      \kern 4.8pt \kern\bl@eds
      \vrule height .2pt depth .2pt width 25pt      % top of page (right)
      \kern -15pt
      \dimen0=\topm@rgin \advance\dimen0 by .4pt
      \vrule height -\topm@rgin depth \dimen0 width 15pt    % top margin (right)
      \kern -30pt \kern-\bl@eds
    }%
  \everypar=\toks0}\dp\topcr@p=0pt % end \vbox for \topcr@p
  \trace{cov}{topcrop done}%
  \global\setbox\bottomcr@p=\vbox to 0pt{\hsize=\PaperWidth \toks0=\everypar\def\par{}\everypar={}%
    \setbox0=\hbox to \PaperWidth{%
      \dimen0=\bottomm@rgin \advance\dimen0 by 0.4pt
      \kern -30pt \kern -\bl@eds
      \vrule height .2pt depth .2pt width 25pt  % bottom of page (left)
      \kern -15pt
      \vrule height \dimen0 depth -\bottomm@rgin width 15pt     % bottom margin (left)
      \kern 4.8pt \kern\bl@eds
      \dimen0=30pt \advance\dimen0 \bl@eds
      \dimen1=-5pt \advance\dimen1 -\bl@eds
      \vrule height \dimen1 depth \dimen0 width .4pt    % left page (bottom)
      \kern -0.2pt
      \ifdim\sp@newidth>0pt\lower \dimexpr 6pt + \bl@eds \hbox to 0pt{\kern 0.5\PaperWidth\kern-0.5\sp@newidth\kern -0.2pt \vrule height 6pt depth .5pt width .4pt
        \kern\sp@newidth\kern -0.4pt\vrule height 6pt depth .5pt width .4pt\hss}\fi
      \hss
      \vrule height \dimen1 depth \dimen0 width .4pt    % right page (bottom)
      \kern 4.8pt \kern\bl@eds
      \dimen0=\bottomm@rgin \advance\dimen0 by 0.4pt
      \vrule height \dimen0 depth -\bottomm@rgin width 15pt % bottom margin (right)
      \kern -15pt
      \vrule height .2pt depth .2pt width 25pt      % bottom page (right)
      \kern -30pt \kern-\bl@eds
    }%
    \dp0=0pt \ht0=0pt \box0
  }\ht\bottomcr@p=0pt \dp\bottomcr@p=0pt% end \vbox for \bottomcr@p
  \trace{cov}{makecrops done}%
}
%-c_makecropmarks

%+c_shipwithcropmarks
\newdimen\pdfcropwidth
\newdimen\pdfcropheight
\newdimen\tabheight % In page-orientation
\newdimen\tabwidth % In page-orientation
\newdimen\TabsStart %Vertical offset to margins for start of thumb tabs
\newdimen\TabsEnd %Vertical offset to margins for end of thumb tabs 
\newcount\NumTabs %How many tabs to be set?
\newif\ifTabTopToEdgeEven %Is the top of the text at the pageedge on even pages
\newif\ifTabTopToEdgeOdd %Is the top of the text at the pageedge on odd pages
\TabTopToEdgeEvenfalse
\TabTopToEdgeOddfalse
\newif\ifholdpageno\holdpagenofalse

\newif\ifTabAutoRotate \TabAutoRotatetrue % Does autorotation of text occur?
\newif\ifTabRotationNormal \TabRotationNormaltrue % TabRotationNormalfalse==rotate text (if auto-rotate off). (If auto-rotate on: invert autorotate logic, i.e. assume the \toc3 are narrower than they are tall).


\NumTabs=2
\tabheight=50pt
\tabwidth=15pt
\TabsStart=10pt %First Tab starts 10pt from the vertical margin
\TabsEnd=10pt %LastTab ends 10 from the vertical margin
% Thumb-tabs codes assume  setup in the form \settumbtab{Gen}{1}, 
% where Gen exactly matches the \toc3 entry for the book.
\def\ThumbTabStyle{toc3}%Which style defines the font? Default is toc3
\def\tabBoxCol{0 0 0}%Bacground as r g b 
\def\tabFontCol{1 1 1}%Foregreound as r g b

\def\sett@bname#1{\xdef\t@bname{t@b-#1}}
\def\sett@bgrpnam#1{\xdef\t@bgrp{t@bg-#1}}
\def\setthumbtab#1#2{\sett@bname{#1}\edef\t@mp{#2}\ifx\t@mp\empty\x@\let\csname\t@bname -num\endcsname\relax\else\x@\xdef\csname\t@bname -num\endcsname{#2}\ifnum\NumTabs<#2 \global\NumTabs=#2 \fi\fi}
\def\setthumbtabBg#1#2{\sett@bname{#1}\x@\xdef\csname\t@bname -boxcol\endcsname{#2}}
\def\setthumbtabFg#1#2{\sett@bname{#1}\x@\xdef\csname\t@bname -col\endcsname{#2}}
% Tab groups
\def\settabgroupFg#1#2{\sett@bgrpnam{#1}\x@\xdef\csname\t@bgrp -col\endcsname{#2}}
\def\settabgroupBg#1#2{\sett@bgrpnam{#1}\x@\xdef\csname\t@bgrp -boxcol\endcsname{#2}}
\def\gett@bgroup{\x@\let\x@\t@bgrp\csname\t@bname-grp\endcsname}

\def\setthumbtabgroup#1#2{% #1 - Name e.g. Pent, #2 list of book IDs e.g GEN,EXO,NUM,DEU,JOS
 \sett@bgrpnam{#1}\let\n@xtcmd\s@tt@bgroup \n@xtcmd #2,,\endtab}
\def\l@stcmd#1,\endtab{}

\def\s@tt@bgroup#1,#2,\endtab{%Iterate through comma separated list, setting group name
  \edef\tmp{#2}%
  \ifx\tmp\empty\let\n@xtcmd\l@stcmd\fi
  \sett@bname{#1}\x@\xdef\csname \t@bname-grp\endcsname{\t@bgrp}%
  \n@xtcmd #2,\endtab}


\def\cp@p{\special{color pop}}%
\def\colourbox#1#2#3#4#5#6#7{%1 - box colour, 2 - text colour 
  % 3- boxwidth 
  % 4- box depth 
  % 5 - box height
  % 6 - =0, no movement >0pt  separation of text from bottom =1sp, centre text.  < 0pt separation from top of box to top of text 
  % 7 - text (should include glue to align / centre e.g. \hss)
  %\dimen2=\baselineskip\advance\dimen2 by -.2ex
  % For tabs, Box height is set by \tabwidth, text is set 2pt below top 
  \trace{pt}{(#1) (#2) #3 #4 #5 #6 #7}%
  \dimen2=#6%
  \setbox0\hbox to #3{#7}%
  \ifdim\dimen2<0pt
   \advance\dimen2 by #5\advance\dimen2 by -\ht0
   \ifdim\dimen2<0pt \dimen2=0pt \fi
  \else 
    \ifdim\dimen2=1sp%Magic number to indicate vertical centering.
     %To centre text, it goes UP by ((#5-ht0)-(#4-dp0))/2
     %can't use -#4 because #4 may be -ve already.
     \dimen2=#4\multiply\dimen2 by -1
     \advance\dimen2 by #5\advance\dimen2 by -\ht0
     \advance\dimen2 by \dp0
     \divide\dimen2 by 2
    \else
      \ifdim\dimen2=0pt \else
        %Distance from bottom  shift=#6+dp0 -#4%
        %can't use -#4 because #4 may be -ve already.
        \dimen2=#4\multiply\dimen2 by -1
        \advance\dimen2 by #6%
        \advance\dimen2 by \dp0
      \fi
    \fi
  \fi
  \setbgc@l{#1}%
  \setfgc@l{#2}%
  \setbox0\hbox{\hbox to 0pt{\bgc@l\vrule depth #4 height #5 width #3 \hss}\endbgc@l\fgc@l\raise \dimen2\box0}\ht0=#5\dp0=#4\box0\endfgc@l}

\def\setbgc@l#1{\edef\bgc@l{#1}\ifx\bgc@l\empty\let\endbgc@l\empty\else\edef\bgc@l{\special{color push rgb #1}}\let\endbgc@l=\cp@p\fi}%
\def\setfgc@l#1{\edef\fgc@l{#1}\ifx\fgc@l\empty\let\endfgc@l\empty\else\edef\fgc@l{\special{color push rgb #1}}\let\endfgc@l=\cp@p\fi}%



%Rotate box0 about a point on it's baseline, half way along its width. Adjust size so that height/depth and width are correct for rotated box. box height/depth becomes left-most edge.
\def\r@acwSet{\edef\@ngle{90}\kern\ht0}% anticlockwise
\def\r@cwSet{\edef\@ngle{-90}\kern\dp0}%clockwise.
\def\rot@tebz{\dimen1=\wd0\dimen2=\ht0
  \advance\dimen2 by \dp0%
  \hbox to \dimen2{\kern -0.5\wd0%
    %kern so that the correct edge of the box is at the centrepoint location, and set rotation
    \r@tSet
    \trace{pt}{Rotating tab \@ngle (+ is anticlockwise)}%
    \vbox to 0pt{\vss\hbox to 0pt{\kern 0.5\wd0\special{x:gsave}\special{x:rotate \@ngle}\hss}\vss}%
    \ht0=0.5\dimen1\dp0=0.5\dimen1\box0\special{x:grestore}\hss}}
\def\rot@tebzoneeighty{\dimen1=\wd0\dimen2=\ht0\dimen3=\dp0
  %\advance\dimen2 by \dp0%
  \hbox to \dimen1{\kern -0.5\wd0%
    %kern so that the correct edge of the box is at the centrepoint location, and set rotation
    %\ifodd\pageno \kern\ht0\else\kern\dp0\fi %Old-style
    \r@tSet
    \trace{pt}{Rotating tab \@ngle (+ is anticlockwise)}%
    \vbox to 0pt{\vss\hbox to 0pt{\kern 0.5\wd0\special{x:gsave}\special{x:rotate \@ngle}\hss}\vss}%
    \ht0=\dimen3\dp0=\dimen2\kern-0.5\dimen1\box0\special{x:grestore}\hss}}


\def\TabTxtFarFromEdge#1{\if\st@rtatedge F\kern 2pt \else\hss\fi #1\if\st@rtatedge F\hss\else\kern 2pt \fi}% Normal position for horizontal text - left-aligned on odd pages / bottom when rotated
\def\TabTxtCloseToEdge#1{\if\st@rtatedge T\kern 2pt \else\hss\fi #1\if\st@rtatedge T\hss\else\kern 2pt \fi}% Right-aligned on odd-pages / Top when rotated
\def\TabTxtCentred#1{\hss #1\hss}% 
\let\horizThumbtabContents=\TabTxtFarFromEdge% How should the thumbtab be positioned when horizontal?
\let\vertThumbtabContents=\TabTxtCentred% How should the thumbtab be positioned when vertical?
\def\vertThumbtabVadj{-2pt}%2pt below top.
\def\horizThumbtabVadj{1sp}%vertically centred.
\def\lastw@rning{}

\def\in@tThumbT@bs{%Thumb-tab setup code goes here.
  \global\let\initThumbT@bs\relax
}

\def\reinitThumbTabs{%Function for users to call if they've altered something that affects initialisation
  \let\initThumbT@bs\in@tThumbT@bs
}
     
\reinitThumbTabs

\def\TabBleed{5pt}%
\newif\ifstretchtabs
\def\t@bbox{%
 \s@tfont{\ThumbTabStyle}{\ThumbTabStyle}%
 \x@\let\x@\t@bBoxCol\csname\t@bname -boxcol\endcsname
 \x@\let\x@\t@bFontCol\csname\t@bname -col\endcsname
 \ifx\t@bgrp\relax
   \let\t@bgFontCol\relax
   \let\t@bgBoxCol\relax
 \else
   \x@\let\x@\t@bgBoxCol\csname\t@bgrp -boxcol\endcsname
   \x@\let\x@\t@bgFontCol\csname\t@bgrp -col\endcsname
 \fi
 \ifx\t@bBoxCol\relax\ifx\t@bgBoxCol\relax\let\t@bBoxCol\tabBoxCol\else\let\t@bBoxCol\t@bgBoxCol\fi\fi
 \ifx\t@bFontCol\relax\ifx\t@bgFontCol\relax\let\t@bFontCol\tabFontCol\else\let\t@bFontCol\t@bgFontCol\fi\fi
 \tempfalse% Don't rotate
 \ifTabAutoRotate 
   \ifdim\tabheight>\tabwidth% Normally rotate
     \ifTabRotationNormal
       \temptrue% DO  rotate
     \fi
   \else%widith>height, don't normally rotate
     \ifTabRotationNormal\else
       \temptrue %Do rotate
     \fi
   \fi
 \else
   \ifTabRotationNormal\else
     \temptrue %Do rotate
   \fi
 \fi
 \setbox0\hbox{\b@okShort}%
 \iftemp%Rotated tabs
   \edef\t@bdim{\the\tabheight}%
   \ifdim\wd0>\tabheight
     \ifstretchtabs
       \edef\t@bdim{\the\dimexpr \wd0 + 1pt\relax}%
     \fi
     \ifx\lastw@rning\b@okShort\else\let\lastw@rning\b@okShort\message{Thumb tab contents "\b@okShort" too wide (\the\wd0) for tab height (\the\tabheight)}\fi
   \fi
   \tempfalse
   \ifodd\pageno
     \ifTabTopToEdgeOdd\temptrue\fi
   \else
     \ifTabTopToEdgeEven\temptrue\fi
   \fi
   \trace{pt}{pg \the\pageno, Top to \iftemp edge \else page\fi}%
   \dimen0=\TabBleed\advance\dimen0 by 1pt
   \iftemp %Rotated text has its top at the edge of the page (decenders to text)
     \edef\bxht{\the\dimen0}% Bleed distance
     \edef\bxdp{\the\tabwidth}%
     \bgroup
       \dimen1=\vertThumbtabVadj%
       \ifdim\dimen1=1sp
         % Something should happen here. TabBleed is confusing the positioning 
       \else 
         \multiply\dimen1 by -1%
       \fi
       \xdef\V@dj{\the\dimen1}%
     \egroup
   \else %Rotated text has its bottom at the edge of the page (descenders to edge)
     \edef\bxht{\the\tabwidth}%
     \edef\bxdp{\the\dimen0}%
     \edef\V@dj{\vertThumbtabVadj}%
   \fi
   \setbox0\hbox{\colourbox{\t@bBoxCol}{\t@bFontCol}{\t@bdim}{\bxdp}{\bxht}{\V@dj}{\vertThumbtabContents{\b@okShort}}}%
   \ht0=\tabwidth
   \let\r@tSet\r@cwSet 
   \ifodd\pageno
     \ifTabTopToEdgeOdd\else\let\r@tSet\r@acwSet\fi
   \else
     \ifTabTopToEdgeEven\let\r@tSet\r@acwSet\fi
   \fi
   \hbox to 0pt{%
    \if\st@rtatedge F\kern -\tabwidth\else\hss\fi
     %\setbox2\hbox{\vrule height 0.5\wd0 depth 0.5\wd0 width 1pt}%
     %\copy2 \rot@tebz \box2
     \rot@tebz 
     \if\st@rtatedge F\hss\else \kern-1\tabwidth\fi
   }%
 \else%Unrotated tabs
   \edef\t@bdim{\tabwidth}%
   \ifdim\wd0>\tabwidth
     \ifstretchtabs
       \edef\t@bdim{\the\dimexpr \wd0 + 1pt\relax}%
     \fi
     \ifx\lastw@rning\b@okShort\else\let\lastw@rning\b@okShort\message{Thumb tab contents "\b@okShort" too wide for tab width}\fi
   \fi
   \hbox to 0pt{%
    \dimen0=\ifodd\pageno\TabBleed\else-\TabBleed\fi
    \if\st@rtatedge F\hss\else\kern \dimen0\fi% Bleed of 1pt
    \dimen0=\TabBleed \advance\dimen0\t@bdim
    \setbox0\hbox{\colourbox{\t@bBoxCol}{\t@bFontCol}{\dimen0}{0.1\tabheight}{0.9\tabheight}{\horizThumbtabVadj}{\horizThumbtabContents{\b@okShort}}}%
   % \setbox2\hbox{\vrule height \ht0 depth \dp0 width 1pt}%
   % \copy2 
   %\ifodd\pageno\else\showbox0\fi
    \box0
   % \box2 
    \if\st@rtatedge F\kern -\TabBleed \else\hss\fi%bleed of 1pt.
    }%
 \fi
}

\newif\ifTabsOddOnly \TabsOddOnlyfalse
\def\putthumbtab{%Using the current book ID, and the book's short name complete and position a thumbtab mark
  \initThumbT@bs%becomes \relax after 1st run, unless reset
  \let\st@rtatedge=T% Does a horizontal chunk of text in a box  start nearest to the page edge away from the binding (False for English on odd-numbered pages) (depends on page sequence, matching LTR/RTL)
  \ifodd\pageno
    \ifRTL
      \ifBookOpenLeft\let\st@rtatedge=F\fi
    \else
      \ifBookOpenLeft\else\let\st@rtatedge=F\fi
    \fi
  \else
    \ifRTL
      \ifBookOpenLeft\else\let\st@rtatedge=F\fi
    \else
      \ifBookOpenLeft\let\st@rtatedge=F\fi
    \fi
  \fi
 \trace{pt}{PTT nt:\the\NumTabs, bk:\b@okShort, pg:\the\pageno}%
 \edef\@seglyphmetrics{\the\XeTeXuseglyphmetrics}%
 \XeTeXuseglyphmetrics=3
 \ifnum \ifnum\NumTabs=0 0\else \ifTabsOddOnly\ifodd\pageno 1\else 0\fi\else 1\fi\fi =1
   \sett@bname{\id@@@}\gett@bgroup\x@\let\x@\tabnum\csname\t@bname -num\endcsname
  \trace{pt}{\t@bname, Tabnum: \tabnum}%
   \ifx\tabnum\relax\else %NOT ifx..  This allows setting the number to \relax and it'll disable it.
      {%Protect temporary dimensions and box
        \dimen1=\textheight
        \advance\dimen1 by -\TabsStart
        \advance\dimen1 by -\TabsEnd
        \advance\dimen1 by -\tabheight
        \count255=\NumTabs
        \ifnum\count255 >1
          \advance\count255 by -1
          \divide\dimen1 by\count255
        \fi
        \count255=\tabnum
        \advance\count255 by -1
        \multiply\dimen1 by \count255
        \advance\dimen1 by \TabsStart
        \advance\dimen1 by \dimen0
        \advance\dimen1 by \topm@rgin
        \trace{pt}{d1=\the\dimen1}%
        \vbox to 0pt{%
          \kern\dimen1
          \setbox0\hbox to \PaperWidth{%
             \ifodd\pageno\hss\fi\t@bbox\ifodd\pageno\else\hss\fi}%
          \moveright\dimen0\box0
          %\setbox0\hbox to \PaperWidth{\kern -0.5pt\vrule width 1pt height 5pt\hss\vrule width 1pt height 5pt\kern -0.5pt}%
          %\moveright\dimen0\box0
          %\kern\textheight
          \vss
          %\hrule
        }%
      }%
   \fi
 \fi %ifnum
 \XeTeXuseglyphmetrics=\@seglyphmetrics% Restore to normal value.
}


\def\pdfp@geout{%
    %%%%% The following was added to help produce proper x1a PDF output (djd - 20150504)
    %%%%% Note the \special line here seems to work but there may be a better location for it
    %%%%% Also, the numbers used are hard coded now but need to be calculated
  \bgroup
    \dimen1=0.99626401\pdfpagewidth \dimen2=0.99626401\pdfpageheight
    \dimen3=0.99626401\dimen0 \trace{s}{pdfpagewidth=\the\dimen1 from \the\pdfpagewidth}%
    \ifdim\dimen0 > 0pt
      \pdfcropwidth=\pdfpagewidth\advance\pdfcropwidth by -\dimen0 \pdfcropwidth=0.99626401\pdfcropwidth
      \pdfcropheight=\pdfpageheight\advance\pdfcropheight by -\dimen0 \pdfcropheight=0.99626401\pdfcropheight
      \special {pdf:put @thispage <</MediaBox [0 0 \strip@pt{\dimen1} \space
        \strip@pt{\dimen2} ] /TrimBox [\strip@pt{\dimen3} \space
        \strip@pt{\dimen3} \space \strip@pt\pdfcropwidth \space
        \strip@pt\pdfcropheight ] >>}
    \else\special {pdf:put @thispage <</MediaBox [0 0 \strip@pt{\dimen1} \space
        \strip@pt{\dimen2} ] /TrimBox [0 0 \strip@pt{\dimen1} \space
        \strip@pt{\dimen2} ]>> }\fi
    \getm@rknum{pdftag}%Tag pages if pdftag mark is active issued.
    \xdef\@@pdftag{\firstmarks\m@rknum}%
    \trace{ma}{topmark for pdftag on page \the\pageno is '\@@pdftag'}%
    \ifx\@@pdftag\empty\else
      \special {pdf:put @thispage <</PieceInfo <</ptxprint 
          <</LastModified /D:20221126103720Z /Private <</Insertion /\@@pdftag>> >>
      >> >>}%
    \fi
  \egroup
}

\def\shipwithcr@pmarks#1{% \shipout box #1, adding cropmarks if required
  % figure out if we're adding a binding gutter, and which side
  \immediate\write\p@rlocs{\string\@pgstart{\the\pageno}{\the\availht}{\the\pdfpagewidth}{\the\pdfpageheight}}%
  \ifnum 1=\numexpr \ifDoubleSided \ifodd\pageno 1 \else -1 \fi\else 1 \fi 
      \ifBookOpenLeft *-1 \fi \ifOuterGutter *-1 \fi\relax
    \def\g@tterside{L} \else\def\g@tterside{R}%
  \fi
  \ifx\doLines\empty\else
    \x@\ifvoid\csname gp@box\g@tterside\endcsname
      \ifrotate\XeTeXupwardsmode=0\fi
      \global\x@\setbox\csname gp@box\g@tterside\endcsname\vbox{\doLines}%
      \ifrotate\XeTeXupwardsmode=1\fi
    \fi
  \fi
  \dimen0=0\ifCropMarks .5\fi in
  \advance\pdfpagewidth by 2\dimen0 % increase PDF media size
  \advance\pdfpageheight by 2\dimen0
  \hoffset=-1in \voffset=-1in % shift the origin to (0,0)
  \let\pr@tect=\noexpand
  \trace{G}{\the\pdfpagewidth x \the\pdfpageheight}
  \global\advance\totalp@ges by 1
  \shipout\vbox to 0pt{% ship the actual page content, with \BindingGutter added if wanted
    \pdfp@geout
    \ifvoid\ornXobjects\else
      \trace{orn}{Emmitting Xobj definitions: \ornXFid@list}%
      \global\let\ornXFid@list\empty
      \vbox to 0pt{\vss\box\ornXobjects}%
    \fi
    \edef\oldup{\the\XeTeXupwardsmode}
    \XeTeXupwardsmode=0 %
    \ifrotate
      \vbox to 0pt{\kern.5\pdfpagewidth  %\PaperWidth % swapped because rotated
        \hbox to 0pt{\kern.5\pdfpagewidth \special{x:gsave}\special{x:rotate -90}\hss}
        \vss}
    \fi
    \m@rgepdf
    \pl@ceborder % add PageBorder (or watermark) graphic, if defined
    \trace{o}{p@ = \the\p@}\offinterlineskip
    \x@\ifvoid\csname gp@box\g@tterside\endcsname\else
      \vbox to 0pt{\vbox to \ifrotate\pdfpagewidth\else\pdfpageheight\fi
        {\ifCropMarks\kern0.5in\hbox{\kern 0.5in\x@\copy\csname gp@box\g@tterside\endcsname}\else
          \copy\csname gp@box\g@tterside\endcsname\fi\vss}\vss}%
    \fi
    \vbox to \ifrotate\pdfpagewidth\else\pdfpageheight\fi{%\vss
      \ifCropMarks\kern 0.5in\fi
      \kern\topm@rgin
      %\trace{g}{\the\topm@rgin - \the\bottomm@rgin}%
      \vbox{\hbox to \ifrotate\pdfpageheight\else\pdfpagewidth\fi{\hss\hbox{%
        \ifBindingGutter\if L\g@tterside\kern\BindingGutter\fi\fi
        \XeTeXupwardsmode=\oldup
        #1%
        \ifBindingGutter\if R\g@tterside\kern\BindingGutter\fi\fi
      }\hss}}
      \kern\bottomm@rgin}
    \vss
    \docr@pmarks
    \putthumbtab
    \ifrotate\special{x:grestore}\fi
    \specialfooter
  }%
  \let\pr@tect=\relax
}
%-c_shipwithcropmarks

%+c_shipcomplete
\def\shipcompletep@gewithcr@pmarks#1{% \shipout box #1, adding cropmarks if required
                                     % but without adding margins, borders, etc
                                     % (used for \includepdf)
  \immediate\write\p@rlocs{\string\@pgstart{\the\pageno}{\the\availht}{\the\pdfpagewidth}{\the\pdfpageheight}}%
  \dimen0=0\ifCropMarks .5\fi in
  \dimen9=\pdfpageheight \dimen8=\pdfpagewidth
  \advance\pdfpagewidth by 2\dimen0 % increase PDF media size
  \advance\pdfpageheight by 2\dimen0
  \trace{G}{shipcomplete \the\pdfpagewidth x\the\pdfpageheight}%
  \hoffset=-1in \voffset=-1in % shift the origin to (0,0)
  \global\advance\totalp@ges by 1
  \shipout\vbox to 0pt{% ship the actual page
    \pdfp@geout
    \offinterlineskip                                                           %(1)
    \vbox to \pdfpageheight{\vss
      \ifvoid\ornXobjects\else
        \trace{orn}{Emmitting Xobj definitions: \ornXFid@list}%
        \global\let\ornXFid@list\empty
        \vbox to 0pt{\vss\box\ornXobjects}%
      \fi
      \hbox to \pdfpagewidth{\hss#1\hss}
      \vss}
    \vss
    \docr@pmarks
  \pdfpageheight=\dimen9 \pdfpagewidth=\dimen8
  }%
}
%-c_shipcomplete
%+c_docropmarks
\def\m@keid@line{
  \xdef\id@line{\jobname\ :: \timestamp \ifx\@@pdftag\empty\else\ ::\@@pdftag\fi}%
  \ifx\lastid@line\id@line\else\setbox\topcr@p\box\voidb@x\global\let\lastid@line\id@line\fi
}

\def\docr@pmarks{%
    \ifCropMarks % if crop marks are enabled
      \m@keid@line
      \ifvoid\topcr@p \bl@eds=0pt \makecr@ps \fi % create them (first time)
      \ifrotate\XeTeXupwardsmode=0\fi
      \vbox to 0pt{
        \kern\dimen0
        \moveright\dimen0\copy\topcr@p
        \kern\PaperHeight
        \moveright\dimen0\copy\bottomcr@p
        \moveright\dimen0\vbox to 0pt{\kern12pt\hsize\ifrotate\PaperHeight\else\PaperWidth\fi \everypar={}
          \lineskiplimit=0pt \baselineskip=10pt \linepenalty=200
          \leftskip=0pt plus 1fil \rightskip=\leftskip \parfillskip=0pt
          \noindent \beginL\idf@nt
            \csname c@rrID\endcsname\endL\endgraf % add the current \id line
          \vss}
        \vss
      }
      %\ifrotate\XeTeXupwardsmode=1\fi
    \fi}
%-c_docropmarks

%+c_graphpaper
\newif\ifGraphPaper
\def\GraphPaperX{2mm}
\def\GraphPaperY{2mm}
\def\GraphPaperXoffset{0.0cm} % GraphPaperXoffset is a user setting. GridPaper equivalent gets redefined.
\def\GraphPaperYoffset{0.0cm}
\def\GraphPaperMajorDiv{5} % How many minor divisions are there between major ones?
\def\GraphPaperLineMajor{0.4pt}
\def\GraphPaperLineMinor{0.25pt}
\def\GraphPaperColMajor{0.8 1.0 0.8} % Colour (R G B)
\def\GraphPaperColMinor{0.9 1.0 1.0} % Colour (R G B)
\def\GraphPaperRuleType{rule}
\def\GridPaperLineMajor{0.8pt}
\def\GridPaperLineMinor{0.5pt}
\def\GridPaperColMajor{0.8 0.6 0.6} % Colour (R G B)
\def\GridPaperColMinor{0.68 0.85 0.68} % Colour (R G B)
\def\GridPaperRuleType{rule}
\def\NoteLineXoffset{3mm} % Gap at edge of text
\def\NoteLineXmargin{10mm} % Gap at edge of page
\def\NoteLineY{\onel@neunit} % line repeat frequency
\def\NoteLineLineMajor{0.8pt}
\def\NoteLineLineMinor{0.5pt}
\def\NoteLineRuleType{rule}
\def\NoteLineColMajor{0.0 0.0 0.0} % Colour (R G B)
\def\NoteLineMajorDiv{0} % How many minor divisions are there between major ones?
\def\NoteLineColMinor{0.5 0.5 0.0} % Colour (R G B)
\def\l@stgprule{}
\def\gpc@l{
  \ifx\th@sgprule\l@stgprule\trace{G}{Color test \th@sgprule<=\l@stgprule}\else
    \ifx\l@stgprule\empty\else
      \cp@p
    \fi
    \x@\special{color push rgb \csname \gp@type Col\th@sgprule\endcsname}%
    \xdef\l@stgprule{\th@sgprule}%
    \global\x@\let\x@\gprulew@d\csname \gp@type Line\th@sgprule\endcsname
    \ifdim\gprulew@d<0pt \trace{G}{Negative rule width \gprulew@d}%
      \gdef\gprulew@d{0pt}%
    \fi
  \fi
}
\def\gp@@hmarkrule{\vrule height 0.5\dimen0 depth 0.5\dimen0 width\dimen3}
\def\gp@@hmarkdots{\lower 0.5\dimen0\hbox{\cleaders\gp@@hm@rkdot\hskip \dimen3}}
\def\DotRuleMark{0.5pt}
\def\DotRuleSpace{0.7pt}
\def\gp@@hm@rkdot{\hbox{\vrule height \dimen0 width \DotRuleMark depth 0pt\kern \DotRuleSpace}}
\def\gp@hrule{% Horizontal rule across the page
  \gpc@l %change colour/width if necessary
  \bgroup\parindent=0pt
    \dimen0=\gprulew@d
    \hbox to 0pt{\kern\csname \gp@type Xoffset\endcsname\csname gp@@hmark\csname \gp@type RuleType\endcsname\endcsname \hss}%
    \vskip -\dimen0\relax
  \egroup
}
\def\gp@vrule{% Vertical rule down the page
  \gpc@l %change colour/width if necessary
  \bgroup\parindent=0pt
    \dimen5=\gprulew@d\hsize=\dimen5
    \hbox{\kern-0.5\dimen5\vbox to 0pt{\kern\csname \gp@type Yoffset\endcsname\vrule height \dimen3 width \gprulew@d\vss}%
    \kern-0.5\dimen5}%
  \egroup
}

\def\gp@vrules{%
  \setbox0\hbox{\unhbox0\hskip\dimen1\gp@vrule}%
  \gp@step
  \ifdim\wd0<\dimen2
    \let\gp@nxt\gp@vrules
  \else
    \let\gp@nxt\relax
  \fi
  \gp@nxt
}
\def\gp@hrules{%
  \setbox0\vbox{\unvbox0\vskip\dimen1\gp@hrule}%
  \ifdim\ht0>\dimen2
    \let\gp@nxt\relax
  \else
    \gp@step
    \let\gp@nxt\gp@hrules
  \fi
  \gp@nxt
}

\def\gp@step{%
  \advance\count255 by -1
  \ifnum\count255<1
    \count255=\csname \gp@type MajorDiv\endcsname
    \let\th@sgprule\m@jor
  \else
    \let\th@sgprule\m@nor
  \fi
}

\newbox\gp@boxN
\newbox\gp@boxL
\newbox\gp@boxR
\def\doGraphPaper{%
  \bgroup\topskip=0pt\baselineskip=0pt\parindent=0pt\everypar{}\let\par\endgraf\relax
    %vertical rules
    %
    \let\th@sgprule\m@jor
    \def\gp@type{GraphPaper}%
    \count255=\GraphPaperMajorDiv
    \dimen1=\GraphPaperX
    \dimen2=\pdfpagewidth
    \dimen3=\pdfpageheight
    \dimen0=\GraphPaperYoffset
    \advance\dimen3 by -\dimen0
    \advance\dimen2 by -\dimen1
    \advance\dimen2 by -\GraphPaperXoffset
    \setbox0=\hbox{\kern\GraphPaperXoffset\gp@vrule}%
    \gp@step
    \trace{G}{doGraphPaper \the\dimen0, \the\dimen1, \the\dimen2, \the\dimen3, \the\dimen4, \the\dimen5 : \gprulew@d, \the\hsize}%
    \gp@vrules
    %\showbox0
    \vbox to 0pt{\hbox to 0pt{\unhbox 0\gp@vrule\hss}\vss}%
    % Horizontal rules
    %
    \let\th@sgprule\m@jor
    \count255=\GraphPaperMajorDiv
    \dimen1=\GraphPaperY
    \dimen2=\pdfpageheight
    \advance\dimen2 by -\dimen1
    %\advance\dimen2 by -\GraphPaperYoffset
    \dimen3=\pdfpagewidth
    \dimen0=\GraphPaperXoffset
    \advance\dimen3 by -2\dimen0 % assume identical lr margins
    \setbox0=\vbox{\kern\GraphPaperYoffset\gp@hrule}%
    \gp@step
    \trace{G}{doGraphPaper \the\dimen0, \the\dimen1, \the\dimen2, \the\dimen3, \the\dimen4, \the\dimen5 : \gprulew@d}%
    \gp@hrules
    \vbox to 0pt{\unvbox 0\vss}%
    \cp@p\global\let\l@stgprule\empty
  \egroup
}

\def\doNoteLines{%
  \trace{G}{doNoteLines}%
  \bgroup\topskip=0pt\baselineskip=0pt\parindent=0pt\everypar{}\let\par\endgraf\relax
    \def\gp@type{NoteLine}%
    \count255=\NoteLineMajorDiv
    \let\th@sgprule\m@jor
    \dimen3=\NoteLineLength
    \dimen2=\textheight
    \trace{G}{Notearea \the\dimen3 x \the\dimen2}%
    \dimen1=\NoteLineY
    \dimen0=\topm@rgin
    %\advance\dimen3-
    \ifBindingGutter\if\g@tterside L
      \def\pr@line{\kern\dimexpr \NoteLineXmargin-\NoteLineXoffset\relax}\def\p@stline{\hss}
    \else
      \def\pr@line{\kern\dimexpr \textwidth + \SideMarginFactor\MarginUnit\relax}\def\p@stline{\hss}%\kern\dimexpr \BindingGutter+\SideMarginFactor\MarginUnit+\NoteLineXoffset-\NoteLineXmargin\relax}
    \fi\fi
    \setbox0\vbox{\kern\dimen1}%
    \gp@hrules
    \trace{G}{doNoteLines d4=\the\dimen4, \the\ht0 x \the\wd0}%
    \setbox0=\hbox to \pdfpagewidth{\pr@line\box0\p@stline}
    %\showbox0
    \vbox to 0pt{\kern\dimexpr \topm@rgin-\dimen1\relax\box0\vss}
    \cp@p\global\let\l@stgprule\empty
  \egroup
}
\def\doGridLines{%
  \bgroup\topskip=0pt\parindent=0pt\everypar{}\relax
    \ifrotate\hsize=\pdfpageheight\vsize=\pdfpagewidth\else
    \hsize=\pdfpagewidth\vsize=\pdfpageheight\fi
    \leftskip=0pt\parshape=0\hangindent=0pt\parindent=0pt
    \let\par=\endgraf
    \s@tfont{h}{h}\mkstr@t
    %
    % horizontal rules
    \def\gp@type{GridPaper}%
    \let\th@sgprule\m@nor
    \count255=500
    \dimen3=\ifrotate\pdfpageheight\else\pdfpagewidth\fi
    \dimen2=\ifrotate\pdfpagewidth\else\pdfpageheight\fi
    \advance\dimen2 by -\bottomm@rgin
    \dimen1=\onel@neunit
    \baselineskip=0pt
    \dimen0=\HeaderPosition\MarginUnit
    \edef\GridPaperXoffset{\SideMarginFactor\MarginUnit}
    \dimen3=\GridPaperXoffset \multiply\dimen3 by -2
    \advance\dimen3 by \ifrotate\pdfpageheight\else\pdfpagewidth\fi
    \setbox0=\vbox{\kern\dimen0\gp@hrule}
    \setbox0=\vbox{\unvbox0\vskip\ht\str@tbox\gp@hrule}
    \edef\GridPaperXoffset{0pt}
    \dimen3=\ifrotate\pdfpageheight\else\pdfpagewidth\fi
    \advance\dimen0 by \ht\str@tbox
    \advance\dimen0 by -\topm@rgin
    \dimen0=-\dimen0
    \let\th@sgprule\m@jor
    \setbox0=\vbox{\unvbox0\vskip\dimen0\gp@hrule}
    \gp@step
    \gp@hrules
    \advance\dimen2 by -\ht0
    \let\th@sgprule\m@jor
    \setbox0=\vbox{\unvbox0\vskip\dimen2\gp@hrule}
    \dimen2=\bottomm@rgin
    \advance\dimen2 by -\FooterPosition\MarginUnit
    \let\th@sgprule\m@nor
    \edef\GridPaperXoffset{\SideMarginFactor\MarginUnit}
    \dimen3=\GridPaperXoffset \multiply\dimen3 by -2
    \advance\dimen3 by \ifrotate\pdfpageheight\else\pdfpagewidth\fi
    \setbox0=\vbox{\unvbox0\vskip\dimen2\gp@hrule}
    \vbox to 0pt{\unvbox0\vbox{\unvbox0\vskip\ht\str@tbox\gp@hrule}\vss}
    %
    % vertical rules
    \edef\GridPaperYoffset{0pt}
    \let\th@sgprule\m@jor
    \dimen3=\pdfpageheight
    \dimen4=\SideMarginFactor\MarginUnit
    \ifBindingGutter\if L\g@tterside\advance\dimen4 by \BindingGutter\fi\fi
    \setbox0=\hbox{\kern\dimen4\gp@vrule}
    \let\th@sgprule\m@nor
    %\dimen4=\IndentUnit
    \ifdim\columnshift>0pt
        \setbox0=\hbox{\unhbox0\hskip\columnshift\gp@vrule}
        \advance\dimen1\columnshift
    %    \advance\dimen4 by -\columnshift
    \fi
    %\setbox0=\hbox{\unhbox0\hskip\dimen4\gp@vrule}
    %\advance\dimen1\dimen4
    \ifnum\mainBodyColumns=2 \dimen2=\ColumnGutterFactor\FontSizeUnit
      \if\XrefNotes\empty\else\advance\dimen2 by \XrefNotesWidth\advance\dimen2 -2\XrefNotesMargin\fi
    \else\dimen2=0pt \fi
    \divide\dimen2 by 2
    \ifdiglot
      \ifnum 1=\numexpr 1 \ifdiglotInnerOuter \ifodd\pageno\else * -1\fi\fi \ifdiglotSwap *-1\fi\relax
        \def\atmp{L}\def\btmp{R}\else\def\atmp{R}\def\btmp{L}\fi
      \MSG{side \the\pageno, gs:\g@tterside, at:\atmp}%
      \dimen4=\csname column\atmp width\endcsname\advance\dimen4 0.5\gutter
      \ifx\TextBorderColumns\empty\else
        \advance\dimen4 by \csname @textborder@\atmp\endcsname
      \fi
    \else\dimen4=0.5\textwidth\fi
    \advance\dimen4 by -\dimen2
    \ifdim\columnshift>0pt \advance\dimen4 by -\columnshift\fi % compensate for earlier added columnshift
    \ifnum\mainBodyColumns=2
      \let\th@sgprule\m@nor
      \setbox0=\hbox{\unhbox0\hskip\dimen4\gp@vrule}
      \let\th@sgprule\m@jor
      \setbox0=\hbox{\unhbox0\hskip\dimen2\gp@vrule}
      \let\th@sgprule\m@nor
      \setbox0=\hbox{\unhbox0\hskip\dimen2\gp@vrule}
    \else
      \let\th@sgprule\m@jor
      \setbox0=\hbox{\unhbox0\hskip\dimen4\gp@vrule}
    \fi
    \ifdim\columnshift>0pt
      \dimen0=\columnshift
      \let\th@sgprule\m@nor
      \setbox0=\hbox{\unhbox0\hskip\dimen0\gp@vrule}
      \advance\dimen2\columnshift
    \fi
    \let\th@sgprule\m@jor
    \ifdiglot
      \dimen4=\csname column\btmp width\endcsname
      \advance\dimen4 by 0.5\gutter
      \ifx\TextBorderColumns\empty\else
        \advance\dimen4 by \csname @textborder@\btmp\endcsname
      \fi
    \else\dimen4=0.5\textwidth\advance\dimen4 by -\dimen2\fi
    %\ifBindingGutter\if R\g@tterside\advance\dimen4 by -\BindingGutter\fi\fi
    \setbox0=\hbox{\unhbox0\hskip\dimen4\gp@vrule}
    \vbox to 0pt{\hbox to 0pt{\unhbox 0\hss}\vss}%
    \cp@p\global\let\l@stgprule\empty
  \egroup
}

\def\doLines{} % GraphPaper/Gridlines

%-c_graphpaper

%+c_pageborders
\def\PageBorder{}
\newbox\b@rder
\def\NewPageBorder#1{\global\setbox\b@rder\box\voidb@x % Remove old border
  \gdef\PageBorder{#1}% Set the new filename
}
\def\pl@ceborder{\ifx\PageBorder\empty\else % if \PageBorder is empty, this does nothing
  \ifvoid\b@rder % set up the \b@rder box the first time it's needed
    \global\setbox\b@rder=\hbox{\mapimagefile{\XeTeXpdffile}{\PageBorder}{}\relax}%
    \global\setbox\b@rder=\vbox to \pdfpageheight{\vss
      \hbox to \pdfpagewidth{\hss\box\b@rder\hss}\vss}%
  \fi
  \vbox to 0pt{% respect binding gutter, just like main page content
    \hbox to \pdfpagewidth{\hss\hbox{%
      \ifBindingGutter\if\g@tterside L\kern\BindingGutter\fi\fi
      \copy\b@rder
      \ifBindingGutter\if\g@tterside R\kern\BindingGutter\fi\fi
    }\hss}
  \vss}% output a copy of \box\b@rder
\fi}

\def\MergePDF{}
\def\addqu@tes#1#2\E{\if #1"\relax \def\m@rgePDF{#1#2 }\else\def\m@rgePDF{"#1#2" }\fi}
\def\m@rgepdf{\ifx\MergePDF\empty\else
  \x@\addqu@tes \MergePDF\E  %Make \m@rgePDF a copy of \MergePDF, adding quotes if needed
  \vbox to 0pt{\vbox to \pdfpageheight{\vss\hbox to \pdfpagewidth{\hss\mapimagefile{\XeTeXpdffile}{\m@rgePDF}{media}\relax\hss}\vss}\vss}
\fi}
%-c_pageborders

%+c_plainoutput
\m@rksonpagefalse
\newif\ifp@geone\p@geonetrue
% redefine plain TeX's output routine to add the cropmarks
\def\specialfooter{}
\def\plainoutput{\pl@inoutput{\vbox{\makeheadline\pagebody}}}%\makefootline}}}
\def\emptyoutput{\pl@inoutput{\vbox{}}}
\def\pl@inoutput#1{%
  \traceifset{pl@inoutput}%
  \let\opS@ve@msprefix\mspr@fix\let\mspr@fix\empty%
  \let\opS@ve@catprefix\c@tprefix\let\c@tprefix\empty% Category and milestones have no place in determining page.
  \ifm@rksonpage\else\trace{m}{No marks on page \the\pageno}\fi
  \trace{G}{Before shipout \the\pdfpagewidth x\the\pdfpageheight}%
  \dimen9=\pdfpagewidth \dimen8=\pdfpageheight
  \holdXobjectsfalse
  \shipwithcr@pmarks{#1}%
  \pdfpagewidth=\dimen9 \pdfpageheight=\dimen8
  \holdpagenofalse
  \ifcsname periph-\@@pdftag-nocount\endcsname
    \x@\ifx\csname periph-\@@pdftag-nocount\endcsname\tr@e      
      \holdpagenotrue
    \fi
  \fi
  \ifholdpageno\else\advancepageno\fi
  \global\p@geonefalse
%NB: dosupereject (from plain.tex) will continue triggering outputs as long as
%there are footnotes/floats coming.
  \ifnum\outputpenalty>-\@MM \else\dosupereject\fi\global\m@rksonpagefalse\global\noteseenfalse
  \global\let\mspr@fix\opS@ve@msprefix
  \let\c@tprefix\opS@ve@catprefix % NOT global, as catprefix needs to stay grouped
  \traceifcheck{pl@inoutput}%
  }
%-c_plainoutput

\newbox\instopcr@p
\newbox\insbottomcr@p
\newdimen\sp@newidth \newdimen\bl@eds
\def\ship@utcoverinsert#1#2#3#4{%
  \trace{cov}{ship@utcoverinsert #1, \the\wd#2 \space x \the\ht#2, #3, #4}%
% #1 - insertion name, #2 - box, #3 - spine width, #4 - bleed
% make cropmarks if spine width >=0pt, if -ve, no cropmarks
  \dimen5=\PaperWidth \dimen6=\PaperHeight
  \PaperWidth=\wd#2 \PaperHeight=\ht#2%
  \dimen7=\pdfpagewidth \dimen8=\pdfpageheight
  \ifdim #3<0pt
    \dimen0=0pt
  \else
    \m@keid@line
    \setbox1=\box\topcr@p \setbox2=\box\bottomcr@p
    \sp@newidth=#3 \bl@eds=#4 \makecr@ps
    \setbox\instopcr@p=\box\topcr@p \setbox\insbottomcr@p=\box\bottomcr@p
    \setbox\topcr@p=\box1 \setbox\bottomcr@p=\box2
    \dimen0=0.5in
  \fi
  %\advance\PaperWidth by 2\dimen0 % increase PDF media size
  %\advance\PaperHeight by 2\dimen0
  \let\pr@tect=\noexpand
  \trace{cov}{\the\PaperWidth\space x\space \the\PaperHeight}
  %\tracingoutput=1 
  \showboxdepth=9999
  \showboxbreadth=9999
  \pdfpagewidth=\PaperWidth
  \pdfpageheight=\PaperHeight
  \hoffset=-1in \voffset=-1in% shift the origin to (0,0)
  \advance\hoffset #4 \advance\voffset #4
  \advance\hoffset \dimen0\advance\voffset \dimen0
  \advance\pdfpagewidth 2\dimen0 \advance\pdfpagewidth #4 \advance\pdfpagewidth #4
  \advance\pdfpageheight 2\dimen0 \advance\pdfpageheight #4 \advance\pdfpageheight #4
  %\pdfpagewidth=0.99626401\pdfpagewidth
  %\pdfpageheight=0.99626401\pdfpageheight
  \shipout\vbox to 0pt{% ship the actual page content, with \BindingGutter added if wanted
    \bgroup
      \dimen1=0.99626401\PaperWidth
      \dimen2=0.99626401\PaperHeight
      \dimen0=0.99626401\dimen0 %convert to bp
      \trace{cov}{trimbox: \the\dimen0, \the\dimen0, \the\dimen1, \the\dimen2. MediaBox: 0, 0, \the\pdfcropwidth, \the\pdfcropheight. offset = \the\hoffset \space x \the\voffset}%
      \special {pdf:put @thispage <</TrimBox [\strip@pt{\dimen0} \strip@pt{\dimen0} \strip@pt{\dimen1} \space
        \strip@pt{\dimen2} ] /MediaBox [0 0 \strip@pt\pdfpagewidth \space
        \strip@pt\pdfpageheight ] >>}%
    \egroup
    \special {pdf:put @thispage <</PieceInfo <</ptxprint 
        <</LastModified /D:20221126103720Z /Private <</Insertion /#1>> >>
    >> >>}%
    \edef\oldup{\the\XeTeXupwardsmode}
    \XeTeXupwardsmode=0 %
    \trace{o}{p@ = \the\p@}\offinterlineskip
    \vbox to \PaperHeight{\vss \box#2\vss}
    \vss
    \ifdim #3<0pt\else
      \vbox to 0pt{
        %\kern\dimen0 \moveright\dimen0
        \copy\instopcr@p
        \kern\PaperHeight% \moveright\dimen0
        \copy\insbottomcr@p
        \vss
        }
    \fi
  }%
  \PaperWidth=\dimen5 \PaperHeight=\dimen6
  \pdfpagewidth=\dimen7 \pdfpageheight=\dimen8
}
\endinput
