\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{jlreq-trimmarks}[2022/11/28 jlreq-trimmarks]
\RequirePackage{l3keys2e}
\RequirePackage{jlreq-helpers}[2022/11/28]

\ExplSyntaxOn
% check \tombowdatetrue
\ifx\tombowdatetrue\@undefined
  \PackageError{jlreq-trimmarks}{This~package~only~works~with~pLaTeX,~upLaTeX~or~LuaTeX-ja}{\@ehc}
\fi

\cs_set:Nn \__jlreq_trimmarks_undeferr:n {\ClassError{jlreq-trimmarks}{Trying~to~delete~\string#1~even~though~it~is~not~defined,~may~be~a~bug}{\@ehc}}
% クラスファイル内でしか使わないマクロをクラスファイル終了時に未定義にする
\seq_new:N \g__jlreq_trimmarks_afterpkg_deletecs_seq
\seq_new:N \g__jlreq_trimmarks_afterpkg_deleteif_seq
\cs_set:Nn \__jlreq_trimmarks_afterpkg_addtodeletecs:N {
  \seq_gput_right:Nn \g__jlreq_trimmarks_afterpkg_deletecs_seq {#1}
}
\__jlreq_trimmarks_afterpkg_addtodeletecs:N \__jlreq_trimmarks_afterpkg_addtodeletecs:N
\cs_set:Nn \__jlreq_trimmarks_afterpkg_addtodeleteif:N {
  \seq_gput_right:Nn \g__jlreq_trimmarks_afterpkg_deleteif_seq {#1}
}
\__jlreq_trimmarks_afterpkg_addtodeletecs:N \__jlreq_trimmarks_afterpkg_addtodeleteif:N
\AtEndOfPackage{%
  \seq_map_inline:Nn \g__jlreq_trimmarks_afterpkg_deletecs_seq {
    \jlreq@helper@undefcs{#1}{\__jlreq_trimmarks_undeferr:n}
  }
  \seq_map_inline:Nn \g__jlreq_trimmarks_afterpkg_deleteif_seq {
    \jlreq@helper@undefif{#1}{\__jlreq_trimmarks_undeferr:n}
  }
  \cs_undefine:N \g__jlreq_trimmarks_afterpkg_deletecs_seq
  \cs_undefine:N \g__jlreq_trimmarks_afterpkg_deleteif_seq
  \cs_undefine:N \__jlreq_trimmarks_undeferr:n
}

\@ifclassloaded{jlreq}{\let\ifjlreq@trimmarks@jlreqloaded=\@firstoftwo}{\let\ifjlreq@trimmarks@jlreqloaded=\@secondoftwo}

\def\ifjlreq@trimmarks@tate@{\jlreq@if{\if@tate\fi}}%
% \@firstoftwo when \if@tate is defined and \if@tate=\iftrue
\def\ifjlreq@trimmarks@tate{%
  \jlreq@if{\ifnum0\ifx\if@tate\@undefined 1\fi\ifx\if@tate\relax 1\fi>0~\fi}{\@secondoftwo}{\ifjlreq@trimmarks@tate@}%
}

% dvipdfmx=f，dvips=s，dviout = o, lualatex = l
\keys_define:nn { jlreq-trimmarks } {
  dvipdfmx .code:n = {\let\jlreq@trimmarks@driver=f}, .value_forbidden:n = true,
  dvips .code:n = {\let\jlreq@trimmarks@driver=s}, .value_forbidden:n = true,
  dviout .code:n = {\let\jlreq@trimmarks@driver=o}, .value_forbidden:n = true
}
\let\jlreq@trimmarks@engine=\jlreq@engine
\keys_define:nn { jlreq-trimmarks } {
  lualatex .code:n = {\let\jlreq@trimmarks@engine=l}, .value_forbidden:n = true,
  uplatex .code:n = {\let\jlreq@trimmarks@engine=u}, .value_forbidden:n = true,
  platex .code:n = {\let\jlreq@trimmarks@engine=p}, .value_forbidden:n = true
}

\tl_new:N \g__jlreq_trimmarks_papersizelist_tl
\cs_set:Nn \__jlreq_trimmarks_addpapersize:nn {
  \exp_args:Nnv \__jlreq_trimmarks_addpapersize_aux:nn {#1}{jlreq@helper@papersizelist@#2}
}
\cs_set:Nn \__jlreq_trimmarks_addpapersize_aux:nn {
  \__jlreq_trimmarks_addpapersize_auxi:nnn {#1}#2
}
\cs_set:Nn \__jlreq_trimmarks_addpapersize_auxi:nnn {
  \clist_map_inline:nn {#1} {
    \tl_gput_right:Nn \g__jlreq_trimmarks_papersizelist_tl { { ##1 } { \def\jlreq@resulta{#2} \def\jlreq@resultb{#3} } }
  }
}
\tl_set:Nn \g__jlreq_trimmarks_papersizechangeddate_tl {2021-11-05}
\cs_set:Nn \__jlreq_trimmarks_addpapersize_withjisBwarning:nnn {
  \__jlreq_trimmarks_addpapersize_withwarning:nnn {#1}{#2}{
    The~option~`##1'~means~the~papersize~in~the~ISO~B-series,~not~in~the~JIS~B-series.~This~behavior~was~changed~from~jlreq-trimmarks~version~\g__jlreq_trimmarks_papersizechangeddate_tl.~Use~`trimmarks_paper=#3'~for~the~JIS-B-series.~If~you~want~to~specify~the~ISO~B-series~and~if~you~think~this~warning~is~annoying,~use~`trimmarks_paper=#2'
  }
}
\cs_set:Nn \__jlreq_trimmarks_addpapersize_withwarning:nnn {
  \exp_args:Nnv \__jlreq_trimmarks_addpapersize_withwarning_aux:nnn {#1}{jlreq@helper@papersizelist@#2}{#3}
}
\cs_set:Nn \__jlreq_trimmarks_addpapersize_withwarning_aux:nnn {
  \__jlreq_trimmarks_addpapersize_withwarning_auxi:nnnn {#1}#2{#3}
}
\cs_set:Nn \__jlreq_trimmarks_addpapersize_withwarning_auxi:nnnn {
  \cs_set:Nn \l__jlreq_tmpa:n {
    \PackageWarningNoLine{jlreq-trimmarks}{#4}
  }
  \clist_map_inline:nn {#1} {
    \tl_gput_right:Nx \g__jlreq_trimmarks_papersizelist_tl { { ##1 } {
      \exp_not:o { \l__jlreq_tmpa:n {##1} }
      \exp_not:n { \def\jlreq@resulta{#2} \def\jlreq@resultb{#3} }
    } }
  }
}
\__jlreq_trimmarks_afterpkg_addtodeletecs:N \g__jlreq_trimmarks_papersizelist_tl
\__jlreq_trimmarks_afterpkg_addtodeletecs:N \__jlreq_trimmarks_addpapersize:nn
\__jlreq_trimmarks_afterpkg_addtodeletecs:N \__jlreq_trimmarks_addpapersize_aux:nn
\__jlreq_trimmarks_afterpkg_addtodeletecs:N \__jlreq_trimmarks_addpapersize_auxi:nnn
\__jlreq_trimmarks_afterpkg_addtodeletecs:N \__jlreq_trimmarks_addpapersize_withjisBwarning:nnn
\__jlreq_trimmarks_afterpkg_addtodeletecs:N \__jlreq_trimmarks_addpapersize_withwarning:nnn
\__jlreq_trimmarks_afterpkg_addtodeletecs:N \__jlreq_trimmarks_addpapersize_withwarning_aux:nnn
\__jlreq_trimmarks_afterpkg_addtodeletecs:N \__jlreq_trimmarks_addpapersize_withwarning_auxi:nnnn

\__jlreq_trimmarks_addpapersize:nn{a0,A0,a0paper}{a0paper}
\__jlreq_trimmarks_addpapersize:nn{a1,A1,a1paper}{a1paper}
\__jlreq_trimmarks_addpapersize:nn{a2,A2,a2paper}{a2paper}
\__jlreq_trimmarks_addpapersize:nn{a3,A3,a3paper}{a3paper}
\__jlreq_trimmarks_addpapersize:nn{a4,A4,a4paper}{a4paper}
\__jlreq_trimmarks_addpapersize:nn{a5,A5,a5paper}{a5paper}
\__jlreq_trimmarks_addpapersize:nn{a6,A6,a6paper}{a6paper}
\__jlreq_trimmarks_addpapersize:nn{a7,A7,a7paper}{a7paper}
\__jlreq_trimmarks_addpapersize:nn{a8,A8,a8paper}{a8paper}
\__jlreq_trimmarks_addpapersize:nn{a9,A9,a9paper}{a9paper}
\__jlreq_trimmarks_addpapersize:nn{a10,A10,a10paper}{a10paper}

\__jlreq_trimmarks_addpapersize_withjisBwarning:nnn{b0,B0}{b0paper}{b0j}
\__jlreq_trimmarks_addpapersize_withjisBwarning:nnn{b1,B1}{b1paper}{b1j}
\__jlreq_trimmarks_addpapersize_withjisBwarning:nnn{b2,B2}{b2paper}{b2j}
\__jlreq_trimmarks_addpapersize_withjisBwarning:nnn{b3,B3}{b3paper}{b3j}
\__jlreq_trimmarks_addpapersize_withjisBwarning:nnn{b4,B4}{b4paper}{b4j}
\__jlreq_trimmarks_addpapersize_withjisBwarning:nnn{b5,B5}{b5paper}{b5j}
\__jlreq_trimmarks_addpapersize_withjisBwarning:nnn{b6,B6}{b6paper}{b6j}
\__jlreq_trimmarks_addpapersize_withjisBwarning:nnn{b7,B7}{b7paper}{b7j}
\__jlreq_trimmarks_addpapersize_withjisBwarning:nnn{b8,B8}{b8paper}{b8j}
\__jlreq_trimmarks_addpapersize_withjisBwarning:nnn{b9,B9}{b9paper}{b9j}
\__jlreq_trimmarks_addpapersize_withjisBwarning:nnn{b10,B10}{b10paper}{b10j}

\__jlreq_trimmarks_addpapersize:nn{c2,C2,c2paper}{c2paper}
\__jlreq_trimmarks_addpapersize:nn{c3,C3,c3paper}{c3paper}
\__jlreq_trimmarks_addpapersize:nn{c4,C4,c4paper}{c4paper}
\__jlreq_trimmarks_addpapersize:nn{c5,C5,c5paper}{c5paper}
\__jlreq_trimmarks_addpapersize:nn{c6,C6,c6paper}{c6paper}
\__jlreq_trimmarks_addpapersize:nn{c7,C7,c7paper}{c7paper}
\__jlreq_trimmarks_addpapersize:nn{c8,C8,c8paper}{c8paper}

\__jlreq_trimmarks_addpapersize:nn{a4var}{a4var}
\__jlreq_trimmarks_addpapersize:nn{b5var}{b5var}

\__jlreq_trimmarks_addpapersize:nn{letter,letterpaper}{letterpaper}
\__jlreq_trimmarks_addpapersize:nn{legal,legalpaper}{legalpaper}
\__jlreq_trimmarks_addpapersize:nn{executive,executivepaper}{executivepaper}
\__jlreq_trimmarks_addpapersize:nn{ansiapaper}{ansiapaper}
\__jlreq_trimmarks_addpapersize:nn{ansibpaper}{ansibpaper}
\__jlreq_trimmarks_addpapersize:nn{ansicpaper}{ansicpaper}
\__jlreq_trimmarks_addpapersize:nn{ansidpaper}{ansidpaper}
\__jlreq_trimmarks_addpapersize:nn{ansiepaper}{ansiepaper}
\__jlreq_trimmarks_addpapersize:nn{hagaki}{hagaki}

% width -> \jlreq@resulta, height -> \jlreq@resultb
\def\jlreq@trimmarks@analyzepapersize#1{
  \str_case:nVF {#1} \g__jlreq_trimmarks_papersizelist_tl
  {
    \jlreq@helper@dividebycomma{#1}%
    \ifjlreq@result\else
      \def\jlreq@resulta{#1}%
      \def\jlreq@resultb{#1}%
    \fi
  }
}
\__jlreq_trimmarks_afterpkg_addtodeletecs:N \jlreq@trimmarks@analyzepapersize

\keys_define:nn { jlreq-trimmarks } { trimmarks_paper .tl_set:N = \jlreq@trimmarks@option@papersize }
\__jlreq_trimmarks_afterpkg_addtodeletecs:N \jlreq@trimmarks@option@papersize

\newif\ifjlreq@trimmarks@digital \jlreq@trimmarks@digitaltrue
\newif\ifjlreq@trimmarks@show \jlreq@trimmarks@showtrue
\newif\ifjlreq@trimmarks@show@artbox \jlreq@trimmarks@show@artboxfalse
\tombowdatetrue
% flag: `show` is specified or not
\newif\ifjlreq@trimmarks@option@show \jlreq@trimmarks@option@showfalse

\keys_define:nn { jlreq-trimmarks } {
  show .code:n = {
    \jlreq@trimmarks@option@showtrue
    \jlreq@ifempty{#1}{
      \jlreq@trimmarks@showtrue\tombowdatetrue\jlreq@trimmarks@digitaltrue
    }{
      \jlreq@trimmarks@showfalse\tombowdatefalse\jlreq@trimmarks@digitalfalse
      \clist_map_variable:nNn {#1} \jlreq@trimmarks@tempa {
        \expandafter\jlreq@helper@trim\expandafter{\jlreq@trimmarks@tempa}{\jlreq@trimmarks@tempa}
        \jlreq@switch{\jlreq@trimmarks@tempa}{
          {trimmarks}{\jlreq@trimmarks@showtrue}
          {banner}{\tombowdatetrue}
          {digital}{\jlreq@trimmarks@digitaltrue}
          {digital*}{\jlreq@trimmarks@digitaltrue\jlreq@trimmarks@show@artboxtrue}
          {no}{\tombowdatefalse\jlreq@trimmarks@showfalse\jlreq@trimmarks@digitalfalse}
        }[\PackageError{jlreq-trimmarks}{Unknown~value~`##1'~in~the~option~`show'}{\@ehc}]
      }
    }
  },
  bleed_margin .code:n = {
    \begingroup
      \def\jlreq@do{}%
      \jlreq@parsekeyval@nokey{
        \ifjlreq@parsekeyval@nokey@novalue{%
          \clist_map_variable:nNn { top,bottom,gutter,fore-edge } \jlreq@tempa {
          \epreto\jlreq@do{%
              \noexpand\def\exp_not:o {\csname jlreq@trimmarks@bleed@\jlreq@tempa\endcsname}{##1}%
            }%
          }%
        }{%
          \bool_if:nTF {
            \str_if_eq_p:nn {##1} {top} || \str_if_eq_p:nn {##1} {bottom} ||
            \str_if_eq_p:nn {##1} {gutter} || \str_if_eq_p:nn {##1} {fore-edge}
          }{
            \tl_put_right:Nx \jlreq@do{%
              \noexpand\def\exp_not:o {\csname jlreq@trimmarks@bleed@##1\endcsname}{##2}%
            }%
          }{%
            \PackageError{jlreq-trimmarks}{Unknown~position~`##1'~in~bleed_margin}{\@ehc}
          }%
        }%
      }{#1}%
    \expandafter\endgroup
    \jlreq@do
  }
}

\newif\ifjlreq@trimmarks@landscape \jlreq@trimmarks@landscapefalse
\keys_define:nn { jlreq-trimmarks } { landscape .code:n = {\jlreq@trimmarks@landscapetrue}, .value_forbidden:n = true }
\__jlreq_trimmarks_afterpkg_addtodeleteif:N \ifjlreq@trimmarks@landscape

\keys_set:nn { jlreq-trimmarks } {
  trimmarks_paper = {+2in},
  bleed_margin = { top = 3mm, bottom = 3mm, gutter = 3mm, fore-edge = 3mm },
}
\ProcessKeysOptions { jlreq-trimmarks }

\ifjlreq@trimmarks@option@show\else
  \ifx o\jlreq@trimmarks@driver
    \jlreq@trimmarks@digitalfalse
    \jlreq@trimmarks@showtrue
    \jlreq@trimmarks@show@artboxfalse
  \fi
\fi

\def\jlreq@trimmarks@setpapersize#1{
  \jlreq@ifcontains{#1}{+}{
    \def\@tempa##1+##2\jlreq@endmark{
      \jlreq@helper@trim{##1}{\jlreq@trimmarks@tempa}
      \jlreq@helper@trim{##2}{\jlreq@trimmarks@tempb}
      \ifx\jlreq@trimmarks@tempa\@empty
        \let\jlreq@tempa=\paperwidth
        \let\jlreq@tempb=\paperheight
      \else
        \expandafter\jlreq@trimmarks@analyzepapersize\expandafter{\jlreq@trimmarks@tempa}
        \let\jlreq@tempa=\jlreq@resulta
        \let\jlreq@tempb=\jlreq@resultb
        \ifjlreq@trimmarks@landscape\jlreq@helper@swap{\jlreq@tempa}{\jlreq@tempb}\fi
      \fi
      \expandafter\jlreq@trimmarks@analyzepapersize\expandafter{\jlreq@trimmarks@tempb}
      \ifjlreq@trimmarks@landscape\jlreq@helper@swap{\jlreq@resulta}{\jlreq@resultb}\fi
      \edef\jlreq@trimmarks@paperwidth{\the\dimexpr\jlreq@tempa + \jlreq@resulta\relax}
      \edef\jlreq@trimmarks@paperheight{\the\dimexpr\jlreq@tempb + \jlreq@resultb\relax}
    }
    \@tempa#1\jlreq@endmark
  }{
    \jlreq@trimmarks@analyzepapersize{#1}
    \ifjlreq@trimmarks@landscape\jlreq@helper@swap{\jlreq@resulta}{\jlreq@resultb}\fi
    \edef\jlreq@trimmarks@paperwidth{\jlreq@resulta}
    \edef\jlreq@trimmarks@paperheight{\jlreq@resultb}
  }
}
\__jlreq_trimmarks_afterpkg_addtodeletecs:N \jlreq@trimmarks@setpapersize
\expandafter\jlreq@trimmarks@setpapersize\expandafter{\jlreq@trimmarks@option@papersize}

\ifx o\jlreq@trimmarks@driver
  \ifjlreq@trimmarks@digital
    \PackageError{jlreq-trimmarks}{`show=digital'~can't~be~used~with~the~driver~`dviout'}{\@ehc}
  \fi
\fi

\long\def\jlreq@trimmarks@setbanner#1#2#3{%
  \begingroup
    \def\jlreq@do{}%
    \jlreq@parsekeyval@nokey{%
      \ifjlreq@parsekeyval@nokey@novalue{}{%
        \jlreq@switch{##1}{
          {yoko}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@@{yoko@#1}{yoko@#2}{##2}}}
          {horizontal}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@@{yoko@#1}{yoko@#2}{##2}}}
          {tate}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@@{tate@#1}{tate@#2}{##2}}}
          {vertial}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@@{tate@#1}{tate@#2}{##2}}}
          {corner}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@@{corner@#1}{corner@#2}{##2}}}
          {in-yoko}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@@{inyoko@#1}{inyoko@#2}{##2}}}
          {in-horizontal}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@@{inyoko@#1}{inyoko@#2}{##2}}}
          {in-tate}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@@{intate@#1}{intate@#2}{##2}}}
          {in-vertial}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@@{intate@#1}{intate@#2}{##2}}}
        }%
      }%
    }{#3}%
    \jlreq@if{\ifx\jlreq@do\@empty\fi}{%
      \tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@@{yoko@#1}{yoko@#2}{#3}}%
    }{}%
  \expandafter\endgroup
  \jlreq@do
}

\long\def\jlreq@trimmarks@setbanner@#1#2#3{%
  \begingroup
    \def\jlreq@do{}%
    \jlreq@parsekeyval@nokey{%
      \ifjlreq@parsekeyval@nokey@novalue{}{%
        \jlreq@switch*{##1}{
          {left}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@@{before@#1}{before@#2}{##2}}}
          {right}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@@{after@#1}{after@#2}{##2}}}
          {above}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@@{before@#1}{before@#2}{##2}}}
          {below}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@@{after@#1}{after@#2}{##2}}}
          {in}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@@{in@#1}{in@#2}{##2}}}
        }%
      }%
    }{#3}%
    \jlreq@if{\ifx\jlreq@do\@empty\fi}{
      \tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@@{before@#1}{before@#2}{#3}}%
    }{}%
  \expandafter\endgroup
  \jlreq@do
}

% #1 = odd, #2 = even
\long\def\jlreq@trimmarks@setbanner@@#1#2#3{%
  \begingroup
    \def\jlreq@do{}%
    \jlreq@parsekeyval@nokey{%
      \ifjlreq@parsekeyval@nokey@novalue{}{%
        \jlreq@switch*{##1}{
          {odd}{\tl_put_right:Nn \jlreq@do{\expandafter\edef\csname jlreq@trimmarks@banner@odd@#1\endcsname{\unexpanded{##2}}}}
          {even}{\tl_put_right:Nn \jlreq@do{\expandafter\edef\csname jlreq@trimmarks@banner@even@#2\endcsname{\unexpanded{##2}}}}
        }%
      }%
    }{#3}%
    \jlreq@if{\ifx\jlreq@do\@empty\fi}{
      \tl_put_right:Nn \jlreq@do{%
        \expandafter\edef\csname jlreq@trimmarks@banner@odd@#1\endcsname{\unexpanded{#3}}%
        \expandafter\edef\csname jlreq@trimmarks@banner@even@#2\endcsname{\unexpanded{#3}}%
      }%
    }{}%
  \expandafter\endgroup
  \jlreq@do
}

\tl_new:N \g__jlreq_trimmarks_pattern_center_top_tl
\tl_new:N \g__jlreq_trimmarks_pattern_center_bottom_tl
\tl_new:N \g__jlreq_trimmarks_pattern_center_left_tl
\tl_new:N \g__jlreq_trimmarks_pattern_center_right_tl
\cs_set:Nn \__jlreq_check_rotatebox:n {
  \@ifundefined{rotatebox}{
    \PackageError{jlreq-trimmarks}{`\string\rotatebox'~is~needed~to~use~#1,~please~load~the~package~`graphicx'~or~its~variant}{\@ehc}
  }{}
}
\keys_define:nn { jlreq-trimmarks-pattern } {
  top-center .tl_gset:N = \g__jlreq_trimmarks_pattern_center_top_tl,
  center-top .tl_gset:N = \g__jlreq_trimmarks_pattern_center_top_tl,
  bottom-center .tl_gset:N = \g__jlreq_trimmarks_pattern_center_bottom_tl,
  center-bottom .tl_gset:N = \g__jlreq_trimmarks_pattern_center_bottom_tl,
  center-left .tl_gset:N = \g__jlreq_trimmarks_pattern_center_left_tl,
  left-center .tl_gset:N = \g__jlreq_trimmarks_pattern_center_left_tl,
  center-right .tl_gset:N = \g__jlreq_trimmarks_pattern_center_right_tl,
  right-center .tl_gset:N = \g__jlreq_trimmarks_pattern_center_right_tl,
  centers .code:n = {
    \__jlreq_check_rotatebox:n { trimmarks_pattern/centers }
    \tl_gset:Nn \g__jlreq_trimmarks_pattern_center_top_tl {#1}
    \tl_gset:Nn \g__jlreq_trimmarks_pattern_center_left_tl { \rotatebox[origin=cb]{90}{#1} }
    \tl_gset:Nn \g__jlreq_trimmarks_pattern_center_bottom_tl { \rotatebox[origin=cb]{180}{#1} }
    \tl_gset:Nn \g__jlreq_trimmarks_pattern_center_right_tl { \rotatebox[origin=cb]{270}{#1} }
  },
}

\keys_define:nn { jlreqtrimmarkssetup } {
  banner .code:n = {
    \clist_map_variable:nNn { odd,even } \@tempa {
      \clist_map_variable:nNn { tate,yoko,corner,inyoko,intate } \jlreq@tempa {
        \clist_map_variable:nNn {top,bottom} \jlreq@tempb {
          \clist_map_variable:nNn {left,right} \jlreq@tempc {
            \@namedef{jlreq@trimmarks@banner@\@tempa @\jlreq@tempa @\jlreq@tempb @\jlreq@tempc}{}%
          }%
        }%
      }%
      \clist_map_variable:nNn {before,after} \jlreq@tempa {
        \clist_map_variable:nNn {top,bottom,in} \jlreq@tempb {
          \@namedef{jlreq@trimmarks@banner@\@tempa @\jlreq@tempa @\jlreq@tempb @center}{}%
        }%
        \clist_map_variable:nNn {left,right,in} \jlreq@tempb {
          \@namedef{jlreq@trimmarks@banner@\@tempa @\jlreq@tempa @center@\jlreq@tempb}{}%
        }%
      }%
    }%
    \def\jlreq@do{}%
    \jlreq@parsekeyval@nokey{%
      \ifjlreq@parsekeyval@nokey@novalue{}{%
        \edef\jlreq@gutterodd{\ifjlreq@trimmarks@tate{right}{left}}%
        \if@twoside
          \edef\jlreq@guttereven{\ifjlreq@trimmarks@tate{left}{right}}%
        \else
          \let\jlreq@guttereven\jlreq@gutterodd
        \fi
        \jlreq@switch*{##1}{
          {top-left}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner{top@left}{top@left}{##2}}}
          {left-top}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner{top@left}{top@left}{##2}}}
          {top-right}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner{top@right}{top@right}{##2}}}
          {right-top}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner{top@right}{top@right}{##2}}}
          {top-gutter}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner{top@\jlreq@gutterodd}{top@\jlreq@guttereven}{##2}}}
          {gutter-top}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner{top@\jlreq@gutterodd}{top@\jlreq@guttereven}{##2}}}
          {top-fore-edge}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner{top@\jlreq@guttereven}{top@\jlreq@gutterodd}{##2}}}
          {fore-edge-top}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner{top@\jlreq@guttereven}{top@\jlreq@gutterodd}{##2}}}
          {bottom-left}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner{bottom@left}{bottom@left}{##2}}}
          {left-bottom}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner{bottom@left}{bottom@left}{##2}}}
          {bottom-right}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner{bottom@right}{bottom@right}{##2}}}
          {right-bottom}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner{bottom@right}{bottom@right}{##2}}}
          {bottom-gutter}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner{bottom@\jlreq@gutterodd}{bottom@\jlreq@guttereven}{##2}}}
          {gutter-bottom}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner{bottom@\jlreq@gutterodd}{bottom@\jlreq@guttereven}{##2}}}
          {bottom-fore-edge}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner{bottom@\jlreq@guttereven}{bottom@\jlreq@gutterodd}{##2}}}
          {fore-edge-bottom}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner{bottom@\jlreq@guttereven}{bottom@\jlreq@gutterodd}{##2}}}
          {top-center}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@{top@center}{top@center}{##2}}}
          {center-top}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@{top@center}{top@center}{##2}}}
          {bottom-center}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@{bottom@center}{bottom@center}{##2}}}
          {center-bottom}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@{bottom@center}{bottom@center}{##2}}}
          {center-right}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@{center@right}{center@right}{##2}}}
          {right-center}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@{center@right}{center@right}{##2}}}
          {center-left}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@{center@left}{center@left}{##2}}}
          {left-center}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@{center@left}{center@left}{##2}}}
          {center-gutter}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@{center@\jlreq@gutterodd}{center@\jlreq@guttereven}{##2}}}
          {gutter-center}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@{center@\jlreq@gutterodd}{center@\jlreq@guttereven}{##2}}}
          {center-fore-edge}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@{center@\jlreq@guttereven}{center@\jlreq@gutterodd}{##2}}}
          {fore-edge-center}{\tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner@{center@\jlreq@guttereven}{center@\jlreq@gutterodd}{##2}}}
        }%
      }%
    }{#1}%
    \jlreq@if{\ifx\jlreq@do\@empty\fi}{
      \tl_put_right:Nn \jlreq@do{\jlreq@trimmarks@setbanner{top@left}{top@left}{#1}}%
    }{}%
    \jlreq@do
  },
  banner_font .tl_set:N = \jlreq@trimmarks@bannerfont,
  trimmarks_width .code:n = {\setlength{\@tombowwidth}{#1}},
  color .code:n = {
    \jlreq@ifempty{#1}{%
      \def\jlreq@trimmarks@color{\normalcolor}%
    }{%
      \@ifundefined{color}{%
        \PackageError{jlreq-trimmarks}{`\string\color'~is~not~defined,~please~load~the~package~`color'~or~its~variant~before~this~package}{\@ehc}%
      }%
      \jlreq@switch{#1}{
        {c}{\def\jlreq@trimmarks@color{\color[cmyk]{1,0,0,0}}}
        {m}{\def\jlreq@trimmarks@color{\color[cmyk]{0,1,0,0}}}
        {y}{\def\jlreq@trimmarks@color{\color[cmyk]{0,0,1,0}}}
        {k}{\def\jlreq@trimmarks@color{\color[cmyk]{0,0,0,1}}}
        {cm}{\def\jlreq@trimmarks@color{\color[cmyk]{1,1,0,0}}}
        {cy}{\def\jlreq@trimmarks@color{\color[cmyk]{1,0,1,0}}}
        {ck}{\def\jlreq@trimmarks@color{\color[cmyk]{1,0,0,1}}}
        {my}{\def\jlreq@trimmarks@color{\color[cmyk]{0,1,1,0}}}
        {mk}{\def\jlreq@trimmarks@color{\color[cmyk]{0,1,0,1}}}
        {yk}{\def\jlreq@trimmarks@color{\color[cmyk]{0,0,1,1}}}
        {cmy}{\def\jlreq@trimmarks@color{\color[cmyk]{1,1,1,0}}}
        {cmk}{\def\jlreq@trimmarks@color{\color[cmyk]{1,1,0,1}}}
        {cyk}{\def\jlreq@trimmarks@color{\color[cmyk]{1,0,1,1}}}
        {myk}{\def\jlreq@trimmarks@color{\color[cmyk]{0,1,1,1}}}
        {cmyk}{\def\jlreq@trimmarks@color{\color[cmyk]{1,1,1,1}}}
        {%
          \jlreq@iffirsttoken{##1}{[}{%
            \def\@tempa[####1]####2\jlreq@endmark{\def\jlreq@trimmarks@color{\color[####1]{####2}}}%
          }{%
            \def\@tempa####1\jlreq@endmark{\def\jlreq@trimmarks@color{\color{####1}}}%
          }%
          \@tempa##1\jlreq@endmark
        }%
      }%
    }%
  },
  trimmarks_pattern .code:n = {
    \keys_set:nn { jlreq-trimmarks-pattern } {#1}
  }
}
\NewDocumentCommand { \jlreqtrimmarkssetup } { +m } {
  \keys_set:nn { jlreqtrimmarkssetup } {#1}
}
\def\jlreqtrimmarkswidth{\@tombowwidth}
\@onlypreamble\jlreqtrimmarkssetup
\@bannertoken{%
  \jobname\space(\number\year-\two@digits\month-\two@digits\day
  \space\two@digits\hour :\two@digits\minute)%
}
\jlreqtrimmarkssetup{
  banner={\the\@bannertoken},
  banner_font={\@bannerfont},
  color={},
  trimmarks_pattern = {
    top-center = {
      \vrule width10mm~height\@tombowwidth depth\z@
      \vrule height10mm~width\@tombowwidth depth\z@
      \vrule width10mm~height\@tombowwidth depth\z@
    },
    bottom-center = {
      \vrule width10mm~depth\@tombowwidth height\z@
      \vrule depth10mm~width\@tombowwidth height\z@
      \vrule width10mm~depth\@tombowwidth height\z@
    },
    center-right = {
      \vrule height 10mm depth10mm width\@tombowwidth
      \vrule height.5\@tombowwidth depth.5\@tombowwidth width10mm
    },
    center-left = {
      \vrule width10mm height.5\@tombowwidth depth.5\@tombowwidth
      \vrule height10mm depth10mm width\@tombowwidth
    }
  }
}

\ifx\jlreq@trimmarks@engine\@undefined
  \jlreq@helper@guessengine
  \let\jlreq@trimmarks@engine=\jlreq@result
\fi

\ifx\jlreq@trimmarks@driver\@undefined
  \ifx l\jlreq@trimmarks@engine
    \let\jlreq@trimmarks@driver=l
  \else
    % default: dvipdfmx
    \let\jlreq@trimmarks@driver=f
  \fi
\fi

\ifx l\jlreq@trimmarks@engine
  \setlength{\pagewidth}{\dimexpr\jlreq@trimmarks@paperwidth\relax}
  \setlength{\pageheight}{\dimexpr\jlreq@trimmarks@paperheight\relax}
\else
  \setlength{\pdfpagewidth}{\dimexpr\jlreq@trimmarks@paperwidth\relax}
  \setlength{\pdfpageheight}{\dimexpr\jlreq@trimmarks@paperheight\relax}
\fi

\hoffset=\dimexpr(\jlreq@trimmarks@paperwidth - \paperwidth)/2 - \ifx l\jlreq@trimmarks@engine 1in\else 1truein\fi\relax
\voffset=\dimexpr(\jlreq@trimmarks@paperheight - \paperheight)/2 - \ifx l\jlreq@trimmarks@engine 1in\else 1truein\fi\relax
\ifx l\jlreq@trimmarks@engine\else
  \@ifundefined{@tombowreset@@paper}{
    \AtBeginDocument{%
      \addtolength{\oddsidemargin}{\dimexpr 1truein - 1in\relax}%
      \addtolength{\evensidemargin}{\dimexpr 1truein - 1in\relax}%
      \addtolength{\topmargin}{\dimexpr 1truein - 1in\relax}%
    }
  }{
    \def\@tombowreset@@paper{%
       \@@topmargin\topmargin
       \jlreq@if{\iftombow\fi}{
         \@@paperwidth\paperwidth
         \advance\@@paperwidth 2\dimexpr\@tombowbleed\relax
         \@@paperheight\paperheight \advance\@@paperheight 10mm\relax
         \advance\@@paperheight 2\dimexpr\@tombowbleed\relax
         % 1in -> 1truein
         \advance\@@topmargin 1truein\relax \advance\@themargin 1truein\relax
      }{}%
    }
  }
\fi

\ifx l\jlreq@trimmarks@driver\else
  \jlreq@helper@divide{\the\mag pt}{1000pt}
  \edef\@tempa{\noexpand\AtBeginDvi{\noexpand\special{papersize=\the\dimexpr\strip@pt\jlreq@resultdimen\dimexpr\jlreq@trimmarks@paperwidth\relax\relax, \the\dimexpr\strip@pt\jlreq@resultdimen\dimexpr\jlreq@trimmarks@paperheight\relax\relax}}}
  \@tempa
\fi
\@ifundefined{stockheight}{\newlength{\stockheight}}{}
\@ifundefined{stockwidth}{\newlength{\stockwidth}}{}
\setlength{\stockwidth}{\dimexpr\jlreq@trimmarks@paperwidth\relax}
\setlength{\stockheight}{\dimexpr\jlreq@trimmarks@paperheight\relax}

\def\jlreq@trimmarks@outputbanner#1#2{%
  \jlreq@if{\iftombowdate\fi}{%
    \expandafter\let\expandafter\jlreq@tempa\csname jlreq@trimmarks@banner@\ifodd\c@page odd\else even\fi @#1\endcsname
    \jlreq@if{\ifx\jlreq@tempa\@empty\fi}{}{%
      \def\@tempa##1{#2}%
      \expandafter\@tempa\expandafter{\jlreq@tempa}%
    }%
  }{}%
}
\def\jlreq@trimmarks@bleed{3mm}
% left=gutter?
\def\ifjlreq@trimmarks@leftisgutter{%
  \ifjlreq@trimmarks@tate {
    \bool_if:nTF {  ! ( \int_if_odd_p:n { \c@page } || ! \legacy_if_p:n { @twoside } ) )  }
  }{
    \bool_if:nTF { ( \int_if_odd_p:n { \c@page } || ! \legacy_if_p:n { @twoside } ) }
  }
}
\def\jlreq@trimmarks@bleed@left{\ifjlreq@trimmarks@leftisgutter{\jlreq@trimmarks@bleed@gutter}{\expandafter\csname jlreq@trimmarks@bleed@fore-edge\endcsname}}
\def\jlreq@trimmarks@bleed@right{\ifjlreq@trimmarks@leftisgutter{\expandafter\csname jlreq@trimmarks@bleed@fore-edge\endcsname}{\jlreq@trimmarks@bleed@gutter}}

\def\jlreq@trimmarks@outputtombow{%
  \vbox to\z@{%
    \kern-\dimexpr 10mm + \jlreq@trimmarks@bleed@top\relax
    \boxmaxdepth\maxdimen
    \moveleft\jlreq@trimmarks@bleed@left\vbox to\@@paperheight{%
      \color@begingroup\jlreq@trimmarks@color
      \hbox to\@@paperwidth{%
        \hskip\jlreq@trimmarks@bleed@left\relax
        \setbox\jlreq@tempboxa=\hbox to\z@{% \@TL
          \yoko\hss
          \vrule width\dimexpr 10mm + \jlreq@trimmarks@bleed@left\relax height\@tombowwidth depth\z@
          \vrule height10mm width\@tombowwidth depth\z@
          \jlreq@trimmarks@outputbanner{yoko@top@left}{%
            \setbox\jlreq@tempboxa=\hbox to\z@{\hskip5mm\jlreq@trimmarks@bannerfont##1\hss}%
            \dp\jlreq@tempboxa=0pt
            \raise 4pt\box\jlreq@tempboxa
          }%
          \jlreq@trimmarks@outputbanner{inyoko@top@left}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\jlreq@trimmarks@bannerfont##1\hss}%
            \jlreq@tempdima=\dimexpr\ht\jlreq@tempboxa + \jlreq@trimmarks@bleed@top\relax
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt
            \raise-\jlreq@tempdima\box\jlreq@tempboxa
          }%
        }%
        \dp\jlreq@tempboxa=0pt
        \box\jlreq@tempboxa
        \hfill
        \setbox\jlreq@tempboxa=\hbox{% \@TC
          \yoko
          \setbox\jlreq@tempboxb=\hbox{
            \g__jlreq_trimmarks_pattern_center_top_tl
          }%
          \jlreq@tempdima=\wd\jlreq@tempboxb\relax
          \ht\jlreq@tempboxb=0pt \dp\jlreq@tempboxb=0pt \wd\jlreq@tempboxb=0pt
          \kern -0.5\jlreq@tempdima\relax
          \box\jlreq@tempboxb
          \kern 0.5\jlreq@tempdima\relax
          \jlreq@trimmarks@outputbanner{before@top@center}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\hss\jlreq@trimmarks@bannerfont##1\hskip 5mm}%
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt \wd\jlreq@tempboxa=0pt
            \raise 4pt\box\jlreq@tempboxa
          }%
          \jlreq@trimmarks@outputbanner{in@top@center}{%
            \setbox\jlreq@tempboxa=\hbox{\jlreq@trimmarks@bannerfont##1}%
            \jlreq@tempdima=\dimexpr\ht\jlreq@tempboxa + \jlreq@trimmarks@bleed@top\relax
            \jlreq@tempdimb=0.5\wd\jlreq@tempboxa
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt \wd\jlreq@tempboxa=0pt
            \kern-\jlreq@tempdimb\raise-\jlreq@tempdima\box\jlreq@tempboxa\kern\jlreq@tempdimb
          }%
          \jlreq@trimmarks@outputbanner{after@top@center}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\hskip 5mm\jlreq@trimmarks@bannerfont##1\hss}%
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt \wd\jlreq@tempboxa=0pt
            \raise 4pt\box\jlreq@tempboxa
          }%
        }%
        \dp\jlreq@tempboxa=0pt
        \box\jlreq@tempboxa
        \hfill
        \setbox\jlreq@tempboxa=\hbox to\z@{% \@TR
          \yoko
          \jlreq@trimmarks@outputbanner{yoko@top@right}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\yoko\hss\jlreq@trimmarks@bannerfont##1\hskip 5mm}%
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt
            \raise 4pt\box\jlreq@tempboxa
          }%
          \jlreq@trimmarks@outputbanner{inyoko@top@right}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\yoko\hss\jlreq@trimmarks@bannerfont##1}%
            \jlreq@tempdima=\dimexpr\jlreq@trimmarks@bleed@top + \ht\jlreq@tempboxa\relax
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt
            \raise-\jlreq@tempdima\box\jlreq@tempboxa
          }%
          \vrule height10mm width\@tombowwidth depth\z@
          \jlreq@trimmarks@outputbanner{corner@top@right}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\hskip\dimexpr\jlreq@trimmarks@bleed@right + 4pt\relax\jlreq@trimmarks@bannerfont##1\hss}%
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt
            \raise 4pt\box\jlreq@tempboxa
          }%
          \vrule width\dimexpr 10mm + \jlreq@trimmarks@bleed@right\relax height\@tombowwidth depth\z@\hss
        }%
        \dp\jlreq@tempboxa=0pt
        \box\jlreq@tempboxa
        \hskip\jlreq@trimmarks@bleed@right
      }%
      \kern-10mm
      \hbox to\@@paperwidth{%
        \hbox to\z@{% \@Tl
          \yoko\hss
          \vrule width10mm height\@tombowwidth depth\z@
          \jlreq@trimmarks@outputbanner{corner@top@left}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\hss\jlreq@trimmarks@bannerfont##1\hskip 4pt}%
            \dp\jlreq@tempboxa=0pt \ht\jlreq@tempboxa=0pt
            \raise\dimexpr \jlreq@trimmarks@bleed@top + 4pt\relax\box\jlreq@tempboxa
          }%
          \vrule height\dimexpr 10mm + \jlreq@trimmarks@bleed@top\relax width\@tombowwidth depth\z@
          \jlreq@trimmarks@outputbanner{tate@top@left}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\tate\adjustbaseline\jlreq@trimmarks@bannerfont##1\hss}%
            \ifx l\jlreq@trimmarks@engine
              \jlreq@tempdima=\dimexpr 4pt + \ht\jlreq@tempboxa\relax
             \else
              \jlreq@tempdima=\dimexpr 4pt + \wd\jlreq@tempboxa\relax
            \fi
            \wd\jlreq@tempboxa=0pt \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt
            \kern-\jlreq@tempdima\raise-5mm\box\jlreq@tempboxa\kern\jlreq@tempdima
          }%
          \jlreq@trimmarks@outputbanner{intate@top@left}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\tate\adjustbaseline\jlreq@trimmarks@bannerfont##1\hss}%
            \jlreq@tempdima=\dimexpr\jlreq@trimmarks@bleed@left \ifx l\jlreq@trimmarks@engine + \ht\jlreq@tempboxa\fi\relax
            \wd\jlreq@tempboxa=0pt \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt
            \kern\jlreq@tempdima\box\jlreq@tempboxa\kern-\jlreq@tempdima
          }%
        }%
        \hfill
        \hbox to\z@{% \@Tr
          \yoko
          \jlreq@trimmarks@outputbanner{tate@top@right}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\tate\adjustbaseline\jlreq@trimmarks@bannerfont##1\hss}%
            \jlreq@tempdima=\dimexpr 4pt \ifx l\jlreq@trimmarks@engine + \dp\jlreq@tempboxa\fi\relax
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt \wd\jlreq@tempboxa=0pt
            \kern\jlreq@tempdima\raise -5mm\box\jlreq@tempboxa\kern-\jlreq@tempdima
          }%
          \jlreq@trimmarks@outputbanner{intate@top@right}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\tate\adjustbaseline\jlreq@trimmarks@bannerfont##1\hss}%
            \ifx l\jlreq@trimmarks@engine
              \jlreq@tempdima=\dimexpr\jlreq@trimmarks@bleed@top + \ht\jlreq@tempboxa\relax
            \else
              \jlreq@tempdima=\dimexpr\jlreq@trimmarks@bleed@top + \wd\jlreq@tempboxa\relax
            \fi
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt \wd\jlreq@tempboxa=0pt
            \kern-\jlreq@tempdima\box\jlreq@tempboxa\kern\jlreq@tempdima
          }%
          \vrule height\dimexpr 10mm + \jlreq@trimmarks@bleed@top\relax width\@tombowwidth depth\z@
          \vrule width10mm height\@tombowwidth depth\z@
          \hss
        }%
      }%
      \vfill
      \hbox to\@@paperwidth{%
        \hbox to\z@{% \@CL
          \yoko\hss
          \g__jlreq_trimmarks_pattern_center_left_tl
          \jlreq@trimmarks@outputbanner{before@center@left}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\tate\hss\adjustbaseline\jlreq@trimmarks@bannerfont##1\hskip 5mm}%
            \ifx l\jlreq@trimmarks@engine
              \jlreq@tempdima=\dimexpr 4pt + \ht\jlreq@tempboxa\relax
             \else
              \jlreq@tempdima=\dimexpr 4pt + \wd\jlreq@tempboxa\relax
            \fi
            \dp\jlreq@tempboxa=0pt \ht\jlreq@tempboxa=0pt \wd\jlreq@tempboxa=0pt
            \kern-\jlreq@tempdima\box\jlreq@tempboxa\kern\jlreq@tempdima
          }%
          \jlreq@trimmarks@outputbanner{after@center@left}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\tate\hskip 5mm\adjustbaseline\jlreq@trimmarks@bannerfont##1\hss}%
            \ifx l\jlreq@trimmarks@engine
              \jlreq@tempdima=\dimexpr 4pt + \ht\jlreq@tempboxa\relax
             \else
              \jlreq@tempdima=\dimexpr 4pt + \wd\jlreq@tempboxa\relax
            \fi
            \dp\jlreq@tempboxa=0pt \ht\jlreq@tempboxa=0pt \wd\jlreq@tempboxa=0pt
            \kern-\jlreq@tempdima\box\jlreq@tempboxa\kern\jlreq@tempdima
          }%
          \jlreq@trimmarks@outputbanner{in@center@left}{%
            \setbox\jlreq@tempboxa=\hbox{\tate\adjustbaseline\jlreq@trimmarks@bannerfont##1}%
            \ifx l\jlreq@trimmarks@engine
              \jlreq@tempdima=\dimexpr\jlreq@trimmarks@bleed@left + \ht\jlreq@tempboxa\relax
              \jlreq@tempdimb=.5\wd\jlreq@tempboxa
             \else
              \jlreq@tempdima=\dimexpr\jlreq@trimmarks@bleed@left\relax
              \jlreq@tempdimb=.5\dimexpr\ht\jlreq@tempboxa + \dp\jlreq@tempboxa\relax
            \fi
            \dp\jlreq@tempboxa=0pt \ht\jlreq@tempboxa=0pt \wd\jlreq@tempboxa=0pt
            \kern\jlreq@tempdima\raise\jlreq@tempdimb\box\jlreq@tempboxa\kern-\jlreq@tempdima
          }%
        }%
        \hfill
        \hbox to\z@{% \@CR
          \yoko
          \jlreq@trimmarks@outputbanner{in@center@right}{%
            \setbox\jlreq@tempboxa=\hbox{\tate\adjustbaseline\jlreq@trimmarks@bannerfont##1}%
            \ifx l\jlreq@trimmarks@engine
              \jlreq@tempdima=\dimexpr\jlreq@trimmarks@bleed@right  + \ht\jlreq@tempboxa\relax
              \jlreq@tempdimb=0.5\wd\jlreq@tempboxa
            \else
              \jlreq@tempdima=\dimexpr\jlreq@trimmarks@bleed@right  + \wd\jlreq@tempboxa\relax
              \jlreq@tempdimb=0.5\dimexpr\ht\jlreq@tempboxa + \dp\jlreq@tempboxa\relax
            \fi
            \dp\jlreq@tempboxa=0pt \ht\jlreq@tempboxa=0pt \wd\jlreq@tempboxa=0pt
            \kern-\jlreq@tempdima\raise\jlreq@tempdimb\box\jlreq@tempboxa\kern\jlreq@tempdima
          }%
          \jlreq@trimmarks@outputbanner{before@center@right}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\tate\hss\adjustbaseline\jlreq@trimmarks@bannerfont##1\hskip 5mm}%
            \jlreq@tempdima=\dimexpr 4pt \ifx l\jlreq@trimmarks@engine + \dp\jlreq@tempboxa\fi\relax
            \dp\jlreq@tempboxa=0pt \ht\jlreq@tempboxa=0pt \wd\jlreq@tempboxa=0pt
            \kern\jlreq@tempdima\box\jlreq@tempboxa\kern-\jlreq@tempdima
          }%
          \jlreq@trimmarks@outputbanner{after@center@right}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\tate\hskip 5mm\adjustbaseline\jlreq@trimmarks@bannerfont##1\hss}%
            \jlreq@tempdima=\dimexpr 4pt \ifx l\jlreq@trimmarks@engine + \dp\jlreq@tempboxa\fi\relax
            \dp\jlreq@tempboxa=0pt \ht\jlreq@tempboxa=0pt \wd\jlreq@tempboxa=0pt
            \kern\jlreq@tempdima\box\jlreq@tempboxa\kern-\jlreq@tempdima
          }%
          \g__jlreq_trimmarks_pattern_center_right_tl
          \hss
        }%
      }%
      \vfill
      \hbox to\@@paperwidth{%
        \hbox to\z@{% \@Bl
          \yoko\hss
          \vrule width10mm depth\@tombowwidth height\z@
          \vrule depth\dimexpr 10mm + \jlreq@trimmarks@bleed@bottom\relax width\@tombowwidth height\z@
          \jlreq@trimmarks@outputbanner{tate@bottom@left}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\tate\adjustbaseline\hss\jlreq@trimmarks@bannerfont##1}%
            \ifx l\jlreq@trimmarks@engine
              \jlreq@tempdima=\dimexpr 4pt + \ht\jlreq@tempboxa\relax
             \else
              \jlreq@tempdima=\dimexpr 4pt + \wd\jlreq@tempboxa\relax
            \fi
            \wd\jlreq@tempboxa=0pt \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt
            \kern-\jlreq@tempdima\raise 5mm\box\jlreq@tempboxa\kern\jlreq@tempdima
          }%
          \jlreq@trimmarks@outputbanner{intate@bottom@left}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\tate\adjustbaseline\hss\jlreq@trimmarks@bannerfont##1}%
            \jlreq@tempdima=\dimexpr\jlreq@trimmarks@bleed@left \ifx l\jlreq@trimmarks@engine + \ht\jlreq@tempboxa\fi\relax
            \wd\jlreq@tempboxa=0pt \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt
            \kern\jlreq@tempdima\box\jlreq@tempboxa\kern-\jlreq@tempdima
          }%
        }%
        \hfill
        \hbox to\z@{% \@Br
          \yoko
          \jlreq@trimmarks@outputbanner{tate@bottom@right}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\tate\adjustbaseline\hss\jlreq@trimmarks@bannerfont##1}%
            \jlreq@tempdima=\dimexpr\ifx l\jlreq@trimmarks@engine\ht\jlreq@tempboxa + \fi 4pt\relax
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt \wd\jlreq@tempboxa=0pt
            \kern\jlreq@tempdima\raise5mm\box\jlreq@tempboxa\kern-\jlreq@tempdima
          }%
          \jlreq@trimmarks@outputbanner{intate@bottom@right}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\tate\adjustbaseline\hss\jlreq@trimmarks@bannerfont##1}%
            \ifx l\jlreq@trimmarks@engine
              \jlreq@tempdima=\dimexpr\jlreq@trimmarks@bleed@right + \ht\jlreq@tempboxa\relax
            \else
              \jlreq@tempdima=\dimexpr\jlreq@trimmarks@bleed@right + \wd\jlreq@tempboxa\relax
            \fi
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt \wd\jlreq@tempboxa=0pt
            \kern-\jlreq@tempdima\box\jlreq@tempboxa\kern\jlreq@tempdima
          }%
          \vrule depth\dimexpr 10mm + \jlreq@trimmarks@bleed@bottom\relax width\@tombowwidth height\z@
          \jlreq@trimmarks@outputbanner{corner@bottom@right}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\hskip 4pt\relax\jlreq@trimmarks@bannerfont##1\hss}%
            \jlreq@tempdima=\dimexpr\ht\jlreq@tempboxa  + \jlreq@trimmarks@bleed@bottom + 4pt\relax
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt
            \raise-\jlreq@tempdima\box\jlreq@tempboxa
          }%
          \vrule width10mm depth\@tombowwidth height\z@\hss
        }%
      }%
      \kern-10mm
      \hbox to\@@paperwidth{%
        \hskip\jlreq@trimmarks@bleed@left\relax
        \setbox\jlreq@tempboxa=\hbox to\z@{% \@BL
          \yoko\hss
          \vrule width\dimexpr 10mm + \jlreq@trimmarks@bleed@left\relax depth\@tombowwidth height\z@
          \jlreq@trimmarks@outputbanner{corner@bottom@left}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\hss\jlreq@trimmarks@bannerfont##1\hskip\dimexpr\jlreq@trimmarks@bleed@left + 4pt\relax}%
            \jlreq@tempdima=\dimexpr\ht\jlreq@tempboxa + 4pt\relax
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt
            \raise-\jlreq@tempdima\box\jlreq@tempboxa
          }%
          \vrule depth10mm width\@tombowwidth height\z@
          \jlreq@trimmarks@outputbanner{yoko@bottom@left}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\yoko\hskip 5mm\jlreq@trimmarks@bannerfont##1\hss}%
            \jlreq@tempdima=\dimexpr\ht\jlreq@tempboxa + 4pt\relax
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt
            \raise-\jlreq@tempdima\box\jlreq@tempboxa
          }%
          \jlreq@trimmarks@outputbanner{inyoko@bottom@left}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\yoko\jlreq@trimmarks@bannerfont##1\hss}%
            \jlreq@tempdima=\dimexpr\jlreq@trimmarks@bleed@bottom + \dp\jlreq@tempboxa\relax
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt
            \raise\jlreq@tempdima\box\jlreq@tempboxa
          }%
        }%
        \ht\jlreq@tempboxa=0pt
        \box\jlreq@tempboxa
        \hfill
        \jlreq@tempafalse
        \setbox\jlreq@tempboxa=\hbox{% \@BC
          \yoko
          \setbox\jlreq@tempboxb=\hbox{
            \g__jlreq_trimmarks_pattern_center_bottom_tl
          }%
          \jlreq@tempdimb=\wd\jlreq@tempboxb
          \ht\jlreq@tempboxb=0pt \dp\jlreq@tempboxb=0pt \wd\jlreq@tempboxb=0pt
          \kern-0.5\jlreq@tempdimb
          \box\jlreq@tempboxb
          \kern 0.5\jlreq@tempdimb
          \jlreq@trimmarks@outputbanner{before@bottom@center}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\hss\jlreq@trimmarks@bannerfont##1\hskip 5mm}%
            \jlreq@tempdima=\dimexpr\ht\jlreq@tempboxa + 4pt\relax
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt
            \raise-\jlreq@tempdima\box\jlreq@tempboxa
          }%
          \jlreq@trimmarks@outputbanner{in@bottom@center}{%
            \setbox\jlreq@tempboxa=\hbox{\jlreq@trimmarks@bannerfont##1}%
            \jlreq@tempdima=\dimexpr\dp\jlreq@tempboxa + \jlreq@trimmarks@bleed@bottom\relax
            \jlreq@tempdimb=.5\wd\jlreq@tempboxa
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt \wd\jlreq@tempboxa=0pt
            \kern-\jlreq@tempdimb\raise\jlreq@tempdima\box\jlreq@tempboxa\kern\jlreq@tempdimb
          }%
          \jlreq@trimmarks@outputbanner{before@bottom@center}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\hskip 5mm\jlreq@trimmarks@bannerfont##1\hss}%
            \jlreq@tempdima=\dimexpr\ht\jlreq@tempboxa + 4pt\relax
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt
            \raise-\jlreq@tempdima\box\jlreq@tempboxa
          }%
        }%
        \ht\jlreq@tempboxa=0pt
        \box\jlreq@tempboxa
        \hfill
        \setbox\jlreq@tempboxa=\hbox to 0pt{% \@BR
          \yoko
          \jlreq@trimmarks@outputbanner{yoko@bottom@right}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\yoko\hss\jlreq@trimmarks@bannerfont##1\hskip 5mm}%
            \jlreq@tempdima=\dimexpr\ht\jlreq@tempboxa + 4pt\relax
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt
            \raise-\jlreq@tempdima\box\jlreq@tempboxa
          }%
          \jlreq@trimmarks@outputbanner{inyoko@bottom@right}{%
            \setbox\jlreq@tempboxa=\hbox to 0pt{\yoko\hss\jlreq@trimmarks@bannerfont##1}%
            \jlreq@tempdima=\dimexpr\dp\jlreq@tempboxa + \jlreq@trimmarks@bleed@bottom\relax
            \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt
            \raise\jlreq@tempdima\box\jlreq@tempboxa
          }%
          \vrule depth10mm width\@tombowwidth height\z@
          \vrule width\dimexpr 10mm + \jlreq@trimmarks@bleed@right\relax depth\@tombowwidth height\z@\hss
        }%
        \ht\jlreq@tempboxa=0pt \dp\jlreq@tempboxa=0pt
        \box\jlreq@tempboxa
        \hskip\jlreq@trimmarks@bleed@right
      }%
    \color@endgroup}\vss
  }%
}
\def\@outputtombow{%
  % 6mm = \@tombowbleed: from plcore
  \@@paperwidth=\dimexpr\@@paperwidth - 6mm + \dimexpr\jlreq@trimmarks@bleed@left + \jlreq@trimmarks@bleed@right\relax\relax
  \@@paperheight=\dimexpr\@@paperheight - 6mm + \dimexpr\jlreq@trimmarks@bleed@top + \jlreq@trimmarks@bleed@bottom\relax\relax
  \ifjlreq@trimmarks@show\expandafter\jlreq@trimmarks@outputtombow\fi
}

\ifjlreq@trimmarks@digital
  \ifx l\jlreq@trimmarks@engine
    \setlength\jlreq@resultdimen{1pt}
  \else
    \jlreq@helper@divide{\the\mag pt}{1000pt}
  \fi
  % 0.9963 = 1pt/1bp (almost)
  \edef\jlreq@trimmarks@pttobp#1{\noexpand\strip@pt\noexpand\dimexpr\strip@pt\jlreq@resultdimen\noexpand\dimexpr0.9963\noexpand\dimexpr#1\unexpanded{\relax\relax\relax}}
  \edef\jlreq@trimmarks@trimboxsize{%
    [%
      \jlreq@trimmarks@pttobp{(\jlreq@trimmarks@paperwidth - \paperwidth)/2}~
      \jlreq@trimmarks@pttobp{(\jlreq@trimmarks@paperheight - \paperheight)/2}~
      \jlreq@trimmarks@pttobp{(\jlreq@trimmarks@paperwidth + \paperwidth)/2}~
      \jlreq@trimmarks@pttobp{(\jlreq@trimmarks@paperheight + \paperheight)/2}%
    ]
  }
  \jlreq@tempcnta=\c@page
  \def\@tempa{%
    [%
      \jlreq@trimmarks@pttobp{(\jlreq@trimmarks@paperwidth - \paperwidth)/2 - \jlreq@trimmarks@bleed@left}~
      \jlreq@trimmarks@pttobp{(\jlreq@trimmarks@paperheight - \paperheight)/2 - \jlreq@trimmarks@bleed@bottom}~
      \jlreq@trimmarks@pttobp{(\jlreq@trimmarks@paperwidth + \paperwidth)/2 + \jlreq@trimmarks@bleed@right}~
      \jlreq@trimmarks@pttobp{(\jlreq@trimmarks@paperheight + \paperheight)/2 + \jlreq@trimmarks@bleed@top}%
    ]
  }
  \c@page=1
  \edef\jlreq@trimmarks@bleedboxsize@odd{\@tempa}
  \c@page=2
  \edef\jlreq@trimmarks@bleedboxsize@even{\@tempa}%
  \c@page=\jlreq@tempcnta
  \edef\jlreq@trimmarks@pdfattribute@odd{%
    /TrimBox~\jlreq@trimmarks@trimboxsize
    \ifjlreq@trimmarks@show@artbox /ArtBox~\jlreq@trimmarks@trimboxsize\fi
    /BleedBox~\jlreq@trimmarks@bleedboxsize@odd
  }
  \edef\jlreq@trimmarks@pdfattribute@even{%
    /TrimBox~\jlreq@trimmarks@trimboxsize
    \ifjlreq@trimmarks@show@artbox /ArtBox~\jlreq@trimmarks@trimboxsize\fi
    /BleedBox~\jlreq@trimmarks@bleedboxsize@even
  }
  \__jlreq_trimmarks_afterpkg_addtodeletecs:N \jlreq@trimmarks@pttobp
  \__jlreq_trimmarks_afterpkg_addtodeletecs:N \jlreq@trimmarks@trimboxsize
  \__jlreq_trimmarks_afterpkg_addtodeletecs:N \jlreq@trimmarks@bleedboxsize@odd
  \__jlreq_trimmarks_afterpkg_addtodeletecs:N \jlreq@trimmarks@bleedboxsize@even
  \__jlreq_trimmarks_afterpkg_addtodeletecs:N \jlreq@trimmarks@pdfattribute@odd
  \__jlreq_trimmarks_afterpkg_addtodeletecs:N \jlreq@trimmarks@pdfattribute@even

  \begingroup
    \c@page=1\relax
    \jlreq@tempdima=\jlreq@trimmarks@bleed@left
    \c@page=2\relax
    \ifdim\jlreq@trimmarks@bleed@left=\jlreq@tempdima
      \global\let\jlreq@tempa\@firstoftwo
    \else
      \global\let\jlreq@tempa\@secondoftwo
    \fi
  \endgroup
  \jlreq@tempa{
    % no difference between odd pages and even pages
    \ifx l\jlreq@trimmarks@driver
      \edef\@tempa{\noexpand\pdfvariable pageattr{\jlreq@trimmarks@pdfattribute@odd}}
      \@tempa
    \fi
    \ifx f\jlreq@trimmarks@driver
      \tl_put_left:Nx \@outputtombow {\noexpand\special{pdf:~put~@thispage~<<\jlreq@trimmarks@pdfattribute@odd>>}}
    \fi
    \ifx s\jlreq@trimmarks@driver
      \edef\@tempa{\noexpand\special{ps:SDict~begin~
        [~/TrimBox~\jlreq@trimmarks@trimboxsize /PAGE~pdfmark~
        \ifjlreq@trimmarks@show@artbox [~/ArtBox~\jlreq@trimmarks@trimboxsize /PAGE~pdfmark~\fi
        [~/BleedBox~\jlreq@trimmarks@bleedboxsize@odd /PAGE~pdfmark~
      end}}%
      \@tempa
    \fi
  }{
    \ifx l\jlreq@trimmarks@driver
      % in \@outputtombow, it's too late
      \tl_put_left:Nx \@outputpage {%
        \unexpanded{\ifodd\c@page\pdfvariable} pageattr{\jlreq@trimmarks@pdfattribute@odd}%
        \unexpanded{\else\pdfvariable} pageattr{\jlreq@trimmarks@pdfattribute@even}\noexpand\fi
      }
    \fi
    \ifx f\jlreq@trimmarks@driver
      \tl_put_left:Nx \@outputtombow {%
        \unexpanded{\ifodd\c@page\special}{pdf:~put~@thispage~<<\jlreq@trimmarks@pdfattribute@odd>>}%
        \unexpanded{\else\special}{pdf:~put~@thispage~<<\jlreq@trimmarks@pdfattribute@even>>}\noexpand\fi
      }
    \fi
    \ifx s\jlreq@trimmarks@driver
      \tl_put_left:Nx \@outputtombow {%
        \unexpanded{\ifodd\c@page\special}{ps:SDict~begin~
          [~/TrimBox~\jlreq@trimmarks@trimboxsize /PAGE~pdfmark~
          \ifjlreq@trimmarks@show@artbox [~/ArtBox~\jlreq@trimmarks@trimboxsize /PAGE~pdfmark~\fi
          [~/BleedBox~\jlreq@trimmarks@bleedboxsize@odd /PAGE~pdfmark~
        end}\unexpanded{\else\special}{ps:SDict~begin~
          [~/TrimBox~\jlreq@trimmarks@trimboxsize /PAGE~pdfmark~
          \ifjlreq@trimmarks@show@artbox [~/ArtBox~\jlreq@trimmarks@trimboxsize /PAGE~pdfmark~\fi
          [~/BleedBox~\jlreq@trimmarks@bleedboxsize@even /PAGE~pdfmark~
        end}\noexpand\fi
      }
    \fi
  }
\fi

% always true
\tombowtrue

\ExplSyntaxOff
\endinput
