%%
%% This is file `ltshipout.ltx',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% ltshipout.dtx  (with options: `2ekernel')
%% 
%% This is a generated file.
%% 
%% Copyright 2020 Frank Mittelbach
%% 
%% This file was generated from file(s) of the LaTeX `lthooks Bundle'.
%% --------------------------------------------------------------------------
%% 
%% It may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%    http://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%% 
%% This file may only be distributed together with a copy of the LaTeX
%% `lthooks Bundle'. You may however distribute the `lthooks Bundle'
%% without such generated files.
%% 
%% The newest sources can be found below
%% 
%%    https://github.com/FrankMittelbach/fmitex/
%% 
%% where one can also log issues in case there are any.
%% 
%% 
%% File: ltshipout.dtx (C) Copyright 2020 Frank Mittelbach, LaTeX Team
\providecommand\ltshipoutversion{v0.9a}
\providecommand\ltshipoutdate{2020/07/15}










\ExplSyntaxOn
\bool_new:N \g__shipout_debug_bool
\cs_new_eq:NN \__shipout_debug:n  \use_none:n
\cs_new_protected:Npn \shipout_debug_on:
  {
    \bool_gset_true:N \g__shipout_debug_bool
    \__shipout_debug_gset:
  }
\cs_new_protected:Npn \shipout_debug_off:
  {
    \bool_gset_false:N \g__shipout_debug_bool
    \__shipout_debug_gset:
  }
\cs_new_protected:Npn \__shipout_debug_gset:
  {
    \cs_gset_protected:Npx \__shipout_debug:n ##1
      { \bool_if:NT \g__shipout_debug_bool {##1} }
  }

\hook_new:n{shipout/before}
\hook_new:n{shipout/foreground}
\hook_new:n{shipout/background}
\hook_new:n{shipout/firstpage}
\hook_new:n{shipout/lastpage}

\box_new:N  \l_shipout_box
\cs_set_eq:NN \ShipoutBox \l_shipout_box

\cs_set:Npn\__shipout_execute: {
  \tl_set:Nx \l__shipout_group_level_tl
     { \int_value:w \tex_currentgrouplevel:D }
  \tex_afterassignment:D \__shipout_execute_test_level:
  \tex_setbox:D \l_shipout_box
}

\cs_gset_eq:NN \shipout \__shipout_execute:

\tl_new:N \l__shipout_group_level_tl

\cs_new:Npn \__shipout_execute_test_level: {
  \int_compare:nNnT
     \l__shipout_group_level_tl < \tex_currentgrouplevel:D
     \tex_aftergroup:D
  \__shipout_execute_cont:
}

\cs_new:Npn \__shipout_execute_cont: {
  \box_if_empty:NTF \l_shipout_box
    { \PackageWarning{ltshipout}{Ignoring~ void~ shipout~ box} }
    {
      \bool_gset_false:N \g__shipout_discard_bool
      \set@typeset@protect
      \__shipout_get_box_size:N \l_shipout_box
      \hook_use:n {shipout/before}
      \int_gincr:N \g_shipout_totalpages_int
      \bool_if:NTF \g__shipout_discard_bool
        { \PackageInfo{ltshipout}{Completed~ page~ discarded}
          \bool_gset_false:N \g__shipout_discard_bool
          \tex_deadcycles:D \c_zero_int
        }
        { \box_if_empty:NTF \l_shipout_box
            { \PackageWarning{ltshipout}{
                Shipout~ box~ was~ voided~ by~ hook,\MessageBreak
                ignoring~ shipout~ box  }
            }
            {
              \int_gincr:N \g_shipout_readonly_int
              \__shipout_debug:n {
                \typeout{Absolute~ page~ =~ \int_use:N \g_shipout_readonly_int
                         \space (target:~ \@abspage@last)}
              }
              \__shipout_get_box_size:N \l_shipout_box
              \@kernel@before@shipout@foreground
              \hook_if_empty:nF {shipout/foreground}
                   { \__shipout_add_foreground_picture:n
                     { \hook_use:n {shipout/foreground} } }
              \hook_if_empty:nF {shipout/background}
                   { \__shipout_add_background_picture:n
                     { \hook_use:n {shipout/background} } }
              \__shipout_execute_firstpage_hook:
              \int_compare:nNnT \@abspage@last = \g_shipout_readonly_int
                { \hook_if_empty:nF {shipout/lastpage}
                    { \__shipout_debug:n { \typeout{Executing~ lastpage~ hook~
                          on~ page~ \int_use:N \g_shipout_readonly_int }        }
                      \__shipout_add_foreground_box:n { \UseHook{shipout/lastpage}
                                                  \@kernel@after@shipout@lastpage }
                    }
                    \bool_gset_true:N \g__shipout_lastpage_handled_bool
                }
              \cs_set_eq:NN \protect \exp_not:N
              \tex_shipout:D \box_use:N \l_shipout_box
            }
        }
    }
}

\let\@kernel@after@shipout@lastpage\@empty
\let\@kernel@before@shipout@foreground\@empty

\cs_new:Npn \__shipout_execute_firstpage_hook: {
  \hook_if_empty:nF {shipout/firstpage}
       { \__shipout_add_background_box:n { \UseHook{shipout/firstpage} } }
  \cs_gset_eq:NN \__shipout_execute_firstpage_hook: \prg_do_nothing:
  \cs_gset:Npn \__shipout_add_firstpage_material:Nn ##1 ##2 {
    \PackageWarning{ltshipout}{
        First~ page~ is~ already~ shipped~ out,~ ignoring\MessageBreak
        \string##1 }
  }
}

\bool_new:N \g__shipout_lastpage_handled_bool

\cs_new:Npn \__shipout_add_firstpage_material:Nn #1#2 {
   \AddToHook{shipout/firstpage}{#2}
}

\cs_new:Npn \__shipout_get_box_size:N #1 {
  \dim_set:Nn \l_shipout_box_ht_dim { \box_ht:N #1 }
  \dim_set:Nn \l_shipout_box_dp_dim { \box_dp:N #1 }
  \dim_set:Nn \l_shipout_box_wd_dim { \box_wd:N #1 }
  \dim_set:Nn \l_shipout_box_ht_plus_dp_dim { \l_shipout_box_ht_dim +
                                         \l_shipout_box_dp_dim }
}

\dim_new:N \l_shipout_box_ht_dim
\dim_new:N \l_shipout_box_dp_dim
\dim_new:N \l_shipout_box_wd_dim
\dim_new:N \l_shipout_box_ht_plus_dp_dim

\bool_new:N \g__shipout_discard_bool

\box_new:N \l__shipout_tmp_box
\tl_new:N  \l__shipout_saved_badness_tl

\cs_new:Npn \__shipout_add_background_box:n #1
{ \__shipout_get_box_size:N \l_shipout_box
  \box_if_vertical:NTF \l_shipout_box
      {
        \tl_set:Nx \l__shipout_saved_badness_tl
           { \vfuzz=\the\vfuzz\relax
             \vbadness=\the\vbadness\relax }
        \vfuzz=\c_max_dim
        \vbadness=\c_max_int
        \vbox_set_to_ht:Nnn \l_shipout_box \l_shipout_box_ht_plus_dp_dim
             {
               \hbox_set:Nn \l__shipout_tmp_box
                    { \l__shipout_saved_badness_tl #1 }
               \box_set_wd:Nn \l__shipout_tmp_box \c_zero_dim
               \box_set_ht:Nn \l__shipout_tmp_box \c_zero_dim
               \box_set_dp:Nn \l__shipout_tmp_box \c_zero_dim
               \skip_zero:N \baselineskip
               \skip_zero:N \lineskip
               \skip_zero:N \lineskiplimit
               \box_use:N \l__shipout_tmp_box
               \vbox_unpack:N \l_shipout_box
               \kern \c_zero_dim
             }
        \box_set_ht:Nn \l_shipout_box \l_shipout_box_ht_dim
        \box_set_dp:Nn \l_shipout_box \l_shipout_box_dp_dim
        \l__shipout_saved_badness_tl
      }
      {
        \box_if_horizontal:NT \l_shipout_box
            {
              \tl_set:Nx \l__shipout_saved_badness_tl
                 { \hfuzz=\the\hfuzz\relax
                   \hbadness=\the\hbadness\relax }
              \hfuzz=\c_max_dim
              \hbadness=\c_max_int
              \hbox_set_to_wd:Nnn \l_shipout_box \l_shipout_box_wd_dim
                   {
                     \hbox_set:Nn \l__shipout_tmp_box
                          { \l__shipout_saved_badness_tl #1 }
                     \box_set_wd:Nn \l__shipout_tmp_box \c_zero_dim
                     \box_set_ht:Nn \l__shipout_tmp_box \c_zero_dim
                     \box_set_dp:Nn \l__shipout_tmp_box \c_zero_dim
                     \box_move_up:nn
                         \l_shipout_box_ht_dim
                         { \box_use:N \l__shipout_tmp_box }
                     \hbox_unpack:N \l_shipout_box
                   }
              \l__shipout_saved_badness_tl
            }
      }
}

\cs_new:Npn \__shipout_add_foreground_box:n #1
{
  \box_if_vertical:NTF \l_shipout_box
    {
      \tl_set:Nx \l__shipout_saved_badness_tl
         { \vfuzz=\the\vfuzz\relax
           \vbadness=\the\vbadness\relax }
      \vfuzz=\c_max_dim
      \vbadness=\c_max_int
      \vbox_set_to_ht:Nnn \l_shipout_box \l_shipout_box_ht_plus_dp_dim
           {
             \hbox_set:Nn \l__shipout_tmp_box
                  { \l__shipout_saved_badness_tl #1 }
             \box_set_wd:Nn \l__shipout_tmp_box \c_zero_dim
             \box_set_ht:Nn \l__shipout_tmp_box \c_zero_dim
             \box_set_dp:Nn \l__shipout_tmp_box \c_zero_dim
             \skip_zero:N \baselineskip
             \skip_zero:N \lineskip
             \skip_zero:N \lineskiplimit
             \vbox_unpack:N \l_shipout_box
             \kern -\l_shipout_box_ht_plus_dp_dim
             \box_use:N \l__shipout_tmp_box
             \kern  \l_shipout_box_ht_plus_dp_dim
           }
      \l__shipout_saved_badness_tl
      \box_set_ht:Nn \l_shipout_box \l_shipout_box_ht_dim
      \box_set_dp:Nn \l_shipout_box \l_shipout_box_dp_dim
    }
    {
      \box_if_horizontal:NT \l_shipout_box
        {
          \tl_set:Nx \l__shipout_saved_badness_tl
            { \hfuzz=\the\hfuzz\relax
              \hbadness=\the\hbadness\relax }
          \hfuzz=\c_max_dim
          \hbadness=\c_max_int
          \hbox_set_to_wd:Nnn \l_shipout_box \l_shipout_box_wd_dim
               {
                 \hbox_unpack:N \l_shipout_box
                 \kern -\box_wd:N \l_shipout_box
                 \hbox_set:Nn \l__shipout_tmp_box
                     { \l__shipout_saved_badness_tl #1 }
                 \box_set_wd:Nn \l__shipout_tmp_box \c_zero_dim
                 \box_set_ht:Nn \l__shipout_tmp_box \c_zero_dim
                 \box_set_dp:Nn \l__shipout_tmp_box \c_zero_dim
                 \box_move_up:nn { \box_ht:N \l_shipout_box }
                               { \box_use:N \l__shipout_tmp_box }
                 \kern \box_wd:N \l_shipout_box
               }%
               \l__shipout_saved_badness_tl
        }
    }
}

\tl_gput_right:Nn \@kernel@after@begindocument {
  \tl_const:Nx \c__shipout_horigin_tl
     {
       \cs_if_exist_use:NTF \pdfvariable { horigin }
          { \cs_if_exist_use:NF \pdfhorigin { 1in } }
     }
  \tl_const:Nx \c__shipout_vorigin_tl
     {
       \cs_if_exist_use:NTF \pdfvariable { vorigin }
          { \cs_if_exist_use:NF \pdfvorigin { 1in } }
     }
}

\cs_new:Npn \__shipout_picture_overlay:n #1 {
    \kern -\c__shipout_horigin_tl \scan_stop:
    \vbox_to_zero:n {
      \kern -\c__shipout_vorigin_tl \scan_stop:
      \unitlength 1pt \scan_stop:
      \hbox_set_to_wd:Nnn \l__shipout_tmp_box \c_zero_dim { \ignorespaces #1 }
      \box_set_ht:Nn \l__shipout_tmp_box \c_zero_dim
      \box_set_dp:Nn \l__shipout_tmp_box \c_zero_dim
      \box_use:N \l__shipout_tmp_box
      \tex_vss:D
    }
}

\cs_new:Npn \__shipout_add_background_picture:n #1 {
   \__shipout_add_background_box:n { \__shipout_picture_overlay:n {#1} }
}

\cs_new:Npn \__shipout_add_foreground_picture:n #1 {
   \__shipout_add_foreground_box:n { \__shipout_picture_overlay:n {#1} }
}

\cs_new:Npn \shipout_discard: {
  \bool_gset_true:N \g__shipout_discard_bool
}



\int_new:N \g_shipout_readonly_int
\cs_new_eq:NN \ReadonlyShipoutCounter  \g_shipout_readonly_int

\int_new:N \g_shipout_totalpages_int
\cs_new_eq:NN \c@totalpages \g_shipout_totalpages_int
\cs_new:Npn \thetotalpages { \arabic{totalpages} }

\xdef\@abspage@last{\number\maxdimen}

\g@addto@macro \@kernel@after@enddocument {
  \int_compare:nNnT \@abspage@last = \maxdimen
    {
      \xdef\@abspage@last{ \int_eval:n {\g_shipout_readonly_int + 1} }
    }
}
\g@addto@macro \@kernel@after@enddocument@afterlastpage {
  \int_compare:nNnF \g_shipout_readonly_int = 0
    {
     \if@filesw
        \iow_now:Nx \@auxout {
          \gdef\string\@abspage@last {\int_use:N \g_shipout_readonly_int}}
     \fi
      \bool_if:NF \g__shipout_lastpage_handled_bool
        { \tex_shipout:D\vbox to\textheight{
            \hbox{\UseHook{shipout/lastpage} \@kernel@after@shipout@lastpage }
            \__shipout_excuse_extra_page:
            \null
          }
      }
    }
}

\cs_new:Npn \__shipout_excuse_extra_page: {
  \vfil
  \begin{center}
    \bfseries Temporary~ page!
  \end{center}
    \LaTeX{}~ was~ unable~ to~ guess~ the~ total~ number~ of~ pages~
    correctly.~ ~ As~ there~ was~ some~ unprocessed~ data~ that~
    should~ have~ been~ added~ to~ the~ final~ page~ this~ extra~
    page~ has~ been~ added~ to~ receive~ it.
    \par
    If~ you~ rerun~ the~ document~ (without~ altering~ it)~ this~
    surplus~ page~ will~ go~ away,~ because~ \LaTeX{}~ now~ knows~
    how~ many~ pages~ to~ expect~ for~ this~ document.
  \vfil
}


\cs_new_eq:NN \DiscardShipoutBox \shipout_discard:

\renewcommand \AtBeginDvi {\__shipout_add_firstpage_material:Nn \AtBeginDvi}

\cs_new_eq:NN \DebugShipoutOn  \shipout_debug_on:
\cs_new_eq:NN \DebugShipoutOff \shipout_debug_off:



\cs_new_eq:NN \AtBeginShipoutBox \ShipoutBox

\cs_set_eq:NN\AtBeginShipoutInit\@empty

\newcommand\AtBeginShipout     {\AddToHook{shipout/before}}
\newcommand\AtBeginShipoutNext {\AddToHookNext{shipout/before}}

\newcommand\AtBeginShipoutFirst{\__shipout_add_firstpage_material:Nn \AtBeginShipoutFirst}

\cs_new:Npn \ShipoutBoxHeight { \dim_use:N \l_shipout_box_ht_dim }
\cs_new:Npn \ShipoutBoxDepth  { \dim_use:N \l_shipout_box_dp_dim }
\cs_new:Npn \ShipoutBoxWidth  { \dim_use:N \l_shipout_box_wd_dim }

\cs_new_eq:NN \AtBeginShipoutDiscard \DiscardShipoutBox

\cs_new_eq:NN \AtBeginShipoutAddToBox           \__shipout_add_background_box:n
\cs_new_eq:NN \AtBeginShipoutAddToBoxForeground \__shipout_add_foreground_box:n
\cs_new_eq:NN\AtBeginShipoutUpperLeft           \__shipout_add_background_picture:n
\cs_new_eq:NN\AtBeginShipoutUpperLeftForeground \__shipout_add_foreground_picture:n

\expandafter\cs_set_eq:NN\csname ver@atbegshi.sty\endcsname\fmtversion
\@namedef {ver@atbegshi.sty}{2020/10/01}

\cs_new_eq:NN\EveryShipout\AtBeginShipout
\cs_new_eq:NN\AtNextShipout\AtBeginShipoutNext
\expandafter\cs_set_eq:NN\csname ver@everyshi.sty\endcsname\fmtversion
\@namedef {ver@everyshi.sty}{2020/10/01}

\cs_new:Npn \AtEndDvi {\AddToHook{shipout/lastpage}}
\expandafter\cs_set_eq:NN\csname ver@atenddvi.sty\endcsname\fmtversion
\@namedef {ver@atenddvi.sty}{2020/10/01}

\ExplSyntaxOff


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\endinput
%%
%% End of file `ltshipout.ltx'.
