%% test-common.tex
%% ===============
%% Helpers shared by the test files; \input this file after
%% \usepackage{tcbpagefootnote}.
%%
%% \TPFfootnote[<kind>]{<text>}
%%   \footnote{<text>} with a check of where its text is typeset.
%%   <kind> (default: page) is one of
%%     page   a page footnote on the page of its mark;
%%     box    a footnote of the box (as in a minipage) on the page of
%%            its mark;
%%     later  a page footnote on a later page than its mark (a box
%%            broken across pages).
%% \TPFmark{<id>} and \TPFtext{<id>}{<kind>}
%%   The two halves of \TPFfootnote, for \footnote[<num>],
%%   \footnotemark and \footnotetext: put \TPFmark right after the mark
%%   and \TPFtext at the beginning of the text.  <id> is any name that
%%   is not a number (numbers are used by \TPFfootnote).
%% \TPFcounter{<counter>}{<value>}
%%   Check the value of <counter> at this point.
%% \TPFwidth (optional, defined before this file is input)
%%   A width, e.g. \def\TPFwidth{.75\columnwidth}: code is put around
%%   \@footnotetext of the page, as a package for a multi-column
%%   layout does, that sets the footnotes of the page in this width
%%   (computed from the \columnwidth that \@footnotetext is called
%%   with).  A page footnote is then a footnote set in this width.
%% \TPFnosplit (optional, defined before this file is input)
%%   The footnotenosplit package is loaded (footnotes are not split;
%%   an empty \box\footins is discarded), with \footnotetopkern=2pt
%%   and an \@makefntext that starts with \kern\footnotetopkern.  The
%%   position of \footnoterule and of each text of a page footnote is
%%   written to the .aux file as well, and two more things are checked
%%   (TPF-RULE and TPF-TOP, see below).
%%
%% For each mark and each text, the page, the mark (\@thefnmark), the
%% hyperref anchor (if any) and, for a text, whether it is set in the
%% full \columnwidth of the page, or in \TPFwidth (a page footnote)
%% are written to the .aux file at shipout.  When the
%% .aux file is read back at \end{document}, every mark is checked:
%%   * its text is typeset exactly once, with the same mark;
%%   * the text is where <kind> says;
%%   * with hyperref, the anchor at the text is that of the mark.
%% With \TPFnosplit also:
%%   * a page with a footnote rule has the text of a page footnote
%%     (TPF-RULE: no lone rule);
%%   * the first text of a page footnote below the rule is as far from
%%     the rule on every page (TPF-TOP): a footnote that TeX has moved
%%     to the next page has the space of \footnotetopkern above it as
%%     well.
%% Logs "TPF-NOTE NG: ..." for each failure, then "TPF-RESULT OK (<n>
%% notes)" or "TPF-RESULT NG".  A single run is enough.
\makeatletter
\ifdefined\TPFnosplit
  \RequirePackage{footnotenosplit}
  \footnotetopkern=2pt
  \renewcommand\@makefntext[1]{%
    \kern\footnotetopkern
    \parindent 1em\noindent
    \hb@xt@1.8em{\hss\@makefnmark}#1}
\fi
\ExplSyntaxOn
\int_new:N  \g__tpf_id_int
\seq_new:N  \g__tpf_ids_seq
\bool_new:N \g__tpf_ng_bool
\dim_new:N  \g__tpf_colwd_dim
\tl_new:N   \l__tpf_tmp_tl
\AddToHook { begindocument/end }
  {
    \cs_if_exist:NTF \TPFwidth
      {
        \dim_gset:Nn \g__tpf_colwd_dim { \TPFwidth }
        \cs_set_eq:NN \__tpf_footnotetext:n \@footnotetext
        \cs_set_protected:Npn \@footnotetext #1
          {
            \group_begin:
              \dim_set:Nn \columnwidth { \TPFwidth }
              \__tpf_footnotetext:n {#1}
            \group_end:
          }
      }
      { \dim_gset:Nn \g__tpf_colwd_dim { \columnwidth } }
  }

\cs_new_protected:Npn \__tpf_ng:n #1
  {
    \bool_gset_true:N \g__tpf_ng_bool
    \iow_term:e { TPF-NOTE~NG:~#1 }
  }

%% Write to the .aux file at shipout; #1 is frozen now except \thepage
\cs_new_protected:Npn \__tpf_shipout:n #1
  {
    \tl_set:Ne \l__tpf_tmp_tl {#1}
    \exp_args:NNV \iow_shipout_e:Nn \@auxout \l__tpf_tmp_tl
  }
\cs_new:Npn \__tpf_frozen_mark:
  { \exp_not:N \exp_not:n { \exp_not:o { \@thefnmark } } }
%% hyperref: the anchor of the mark, and the anchor at the text
%% (hyperref sets \@currentHref to the anchor it puts there).
%% Not with the new footnote code of LaTeX (latex-lab-footnotes), which
%% makes the links itself and does not use \Hy@footnote@currentHref.
\cs_new:Npn \__tpf_frozen_href:N #1
  {
    \bool_lazy_and:nnT
      { \cs_if_exist_p:N \Hy@footnote@currentHref }
      { ! \cs_if_exist_p:N \fnote_footnotetext:n }
      { \exp_not:N \exp_not:n { \exp_not:o {#1} } }
  }

\NewDocumentCommand \TPFmark { m }
  {
    \seq_gput_right:Nn \g__tpf_ids_seq {#1}
    \__tpf_shipout:n
      {
        \exp_not:N \exp_not:N \exp_not:N \TPF@m
        {#1} { \exp_not:N \thepage } { \__tpf_frozen_mark: }
        { \__tpf_frozen_href:N \Hy@footnote@currentHref }
      }
  }
\NewDocumentCommand \TPFtext { m m }
  {
    \__tpf_shipout:n
      {
        \exp_not:N \exp_not:N \exp_not:N \TPF@t
        {#1} {#2} { \exp_not:N \thepage } { \__tpf_frozen_mark: }
        { \dim_compare:nNnTF { \hsize } = { \g__tpf_colwd_dim } { 1 } { 0 } }
        { \__tpf_frozen_href:N \@currentHref }
      }
    \cs_if_exist:NT \TPFnosplit
      {
        \tex_savepos:D
        \__tpf_shipout:n
          {
            \exp_not:N \exp_not:N \exp_not:N \TPF@p
            {#1} {#2} { \exp_not:N \thepage }
            { \dim_compare:nNnTF { \hsize } = { \g__tpf_colwd_dim } { 1 } { 0 } }
            { \exp_not:N \int_use:N \exp_not:N \tex_lastxpos:D }
            { \exp_not:N \int_use:N \exp_not:N \tex_lastypos:D }
          }
      }
  }
\NewDocumentCommand \TPFfootnote { O{page} +m }
  {
    \int_gincr:N \g__tpf_id_int
    \exp_args:NV \__tpf_footnote:nnn \g__tpf_id_int {#1} {#2}
  }
\cs_new_protected:Npn \__tpf_footnote:nnn #1#2#3
  { \footnote { \TPFtext {#1} {#2} #3 } \TPFmark {#1} }

\NewDocumentCommand \TPFcounter { m m }
  {
    \int_compare:nNnTF { \value {#1} } = {#2}
      { \iow_term:e { TPF-COUNTER~OK:~#1=#2 } }
      { \__tpf_ng:n { counter~#1=\int_use:c { c@#1 },~want~#2 } }
  }

%% The records in the .aux file are ignored when it is read at
%% \begin{document} (they come from the previous run)
\cs_new_eq:NN \TPF@m \use_none:nnnn
\cs_new_eq:NN \TPF@t \use_none:nnnnnn
\cs_new_protected:Npn \__tpf_record_mark:nnnn #1#2#3#4
  {
    \tl_gset:cn { g__tpf_mhref_#1_tl } {#4}
    \tl_gset:cn { g__tpf_mpage_#1_tl } {#2}
    \tl_gset:cn { g__tpf_mmark_#1_tl } {#3}
  }
\cs_new_protected:Npn \__tpf_record_text:nnnnnn #1#2#3#4#5#6
  {
    \tl_gset:cn { g__tpf_thref_#1_tl } {#6}
    \int_if_exist:cF { g__tpf_tcount_#1_int }
      { \int_new:c { g__tpf_tcount_#1_int } }
    \int_gincr:c { g__tpf_tcount_#1_int }
    \tl_gset:cn { g__tpf_tkind_#1_tl } {#2}
    \tl_gset:cn { g__tpf_tpage_#1_tl } {#3}
    \tl_gset:cn { g__tpf_tmark_#1_tl } {#4}
    \tl_gset:cn { g__tpf_twide_#1_tl } {#5}
  }
\AddToHook { enddocument/afterlastpage }
  {
    \cs_gset_eq:NN \TPF@m \__tpf_record_mark:nnnn
    \cs_gset_eq:NN \TPF@t \__tpf_record_text:nnnnnn
  }
\AddToHook { enddocument/afteraux } { \__tpf_check: }

\cs_new_protected:Npn \__tpf_check:
  {
    \cs_if_exist:NT \TPFnosplit { \__tpf_check_rules: }
    \seq_map_function:NN \g__tpf_ids_seq \__tpf_check_one:n
    \seq_if_empty:NT \g__tpf_ids_seq { \__tpf_ng:n { no~footnotes } }
    \bool_if:NTF \g__tpf_ng_bool
      { \iow_term:n { TPF-RESULT~NG } }
      {
        \iow_term:e
          { TPF-RESULT~OK~(\seq_count:N \g__tpf_ids_seq\c_space_tl notes) }
      }
  }
\cs_new_protected:Npn \__tpf_check_one:n #1
  {
    \tl_if_exist:cTF { g__tpf_mpage_#1_tl }
      {
        \int_if_exist:cTF { g__tpf_tcount_#1_int }
          {
            \int_compare:nNnTF { \int_use:c { g__tpf_tcount_#1_int } } = { 1 }
              { \__tpf_check_text:n {#1} }
              {
                \__tpf_ng:n
                  { #1:~text~typeset~\int_use:c { g__tpf_tcount_#1_int }~times }
              }
          }
          { \__tpf_ng:n { #1:~text~lost } }
      }
      { \__tpf_ng:n { #1:~mark~not~shipped~out } }
  }
\cs_new_protected:Npn \__tpf_check_text:n #1
  {
    \tl_set:Ne \l__tpf_tmp_tl
      {
        #1:~\tl_use:c { g__tpf_tkind_#1_tl },~
        mark~\tl_use:c { g__tpf_mmark_#1_tl }~
        on~p.\tl_use:c { g__tpf_mpage_#1_tl },~
        text~\tl_use:c { g__tpf_tmark_#1_tl }~
        on~p.\tl_use:c { g__tpf_tpage_#1_tl }~
        \str_if_eq:eeTF { \tl_use:c { g__tpf_twide_#1_tl } } { 1 }
          { (page) } { (box) }
        \tl_if_empty:cF { g__tpf_mhref_#1_tl }
          {
            ,~anchor~\tl_use:c { g__tpf_mhref_#1_tl }~
            ->~\tl_use:c { g__tpf_thref_#1_tl }
          }
      }
    \bool_if:nTF
      {
        \tl_if_eq_p:cc { g__tpf_mmark_#1_tl } { g__tpf_tmark_#1_tl }
        && \__tpf_where_p:n {#1}
        && \tl_if_eq_p:cc { g__tpf_mhref_#1_tl } { g__tpf_thref_#1_tl }
      }
      { \iow_term:e { TPF-NOTE~OK:~\l__tpf_tmp_tl } }
      { \__tpf_ng:n { \l__tpf_tmp_tl } }
  }
%% With \TPFnosplit, TeX may move a page footnote to the next page as
%% a whole, as it does with a footnote of the body text: \@makefntext
%% starts with a \kern, where the insertion can be split.  (Where the
%% parts of a broken box leave room for their footnotes, "make test"
%% looks for "% split" in the log.)
\prg_new_conditional:Npnn \__tpf_moved:n #1 { p }
  {
    \cs_if_exist:NTF \TPFnosplit
      {
        \int_compare:nNnTF { \tl_use:c { g__tpf_tpage_#1_tl } }
          = { \tl_use:c { g__tpf_mpage_#1_tl } + 1 }
          { \prg_return_true: } { \prg_return_false: }
      }
      { \prg_return_false: }
  }
\prg_new_conditional:Npnn \__tpf_where:n #1 { p }
  {
    \str_case_e:nnF { \tl_use:c { g__tpf_tkind_#1_tl } }
      {
        { page }
          {
            \bool_if:nTF
              {
                (
                  \int_compare_p:nNn { \tl_use:c { g__tpf_tpage_#1_tl } }
                    = { \tl_use:c { g__tpf_mpage_#1_tl } }
                  || \__tpf_moved_p:n {#1}
                )
                && \str_if_eq_p:ee { \tl_use:c { g__tpf_twide_#1_tl } } { 1 }
              }
              { \prg_return_true: } { \prg_return_false: }
          }
        { box }
          {
            \bool_if:nTF
              {
                \int_compare_p:nNn { \tl_use:c { g__tpf_tpage_#1_tl } }
                  = { \tl_use:c { g__tpf_mpage_#1_tl } }
                && \str_if_eq_p:ee { \tl_use:c { g__tpf_twide_#1_tl } } { 0 }
              }
              { \prg_return_true: } { \prg_return_false: }
          }
        { later }
          {
            \bool_if:nTF
              {
                \int_compare_p:nNn { \tl_use:c { g__tpf_tpage_#1_tl } }
                  > { \tl_use:c { g__tpf_mpage_#1_tl } }
                && \str_if_eq_p:ee { \tl_use:c { g__tpf_twide_#1_tl } } { 1 }
              }
              { \prg_return_true: } { \prg_return_false: }
          }
      }
      { \prg_return_false: }
  }
\ExplSyntaxOff

%% \TPFnosplit: the footnote rule of each page and the first text of a
%% page footnote below it
\ExplSyntaxOn
\cs_new_eq:NN \TPF@r \use_none:nnn
\cs_new_eq:NN \TPF@p \use_none:nnnnnn
\cs_new_eq:NN \__tpf_footnoterule: \prg_do_nothing:
\cs_new_eq:NN \__tpf_makecol: \prg_do_nothing:
\bool_new:N \l__tpf_makecol_bool
\seq_new:N \g__tpf_rules_seq
\int_new:N \l__tpf_dist_int
\int_new:N \l__tpf_min_int
\int_new:N \l__tpf_max_int
\cs_if_exist:NT \TPFnosplit
  {
    \AddToHook { begindocument/end }
      {
        \cs_set_eq:NN \__tpf_footnoterule: \footnoterule
        %% the rule of the page, not that of a minipage
        \cs_set_eq:NN \__tpf_makecol: \@makecol
        \cs_set_protected:Npn \@makecol
          { \bool_set_true:N \l__tpf_makecol_bool \__tpf_makecol: }
        \cs_set_protected:Npn \footnoterule
          {
            \bool_if:NT \l__tpf_makecol_bool
              {
                \tex_savepos:D
                \__tpf_shipout:n
                  {
                    \exp_not:N \exp_not:N \exp_not:N \TPF@r
                    { \exp_not:N \thepage }
                    { \exp_not:N \int_use:N \exp_not:N \tex_lastxpos:D }
                    { \exp_not:N \int_use:N \exp_not:N \tex_lastypos:D }
                  }
              }
            \__tpf_footnoterule:
          }
      }
    \AddToHook { enddocument/afterlastpage }
      {
        \cs_gset_eq:NN \TPF@r \__tpf_record_rule:nnn
        \cs_gset_eq:NN \TPF@p \__tpf_record_pos:nnnnnn
      }
  }
%% #1: page, #2 and #3: position
\cs_new_protected:Npn \__tpf_record_rule:nnn #1#2#3
  {
    \tl_if_exist:cTF { g__tpf_rx_#1_tl }
      { \__tpf_ng:n { two~footnote~rules~on~p.#1 } }
      { \seq_gput_right:Nn \g__tpf_rules_seq {#1} }
    \tl_gset:cn { g__tpf_rx_#1_tl } {#2}
    \tl_gset:cn { g__tpf_ry_#1_tl } {#3}
  }
%% #1: id, #2: kind, #3: page, #4: in the width of the page,
%% #5 and #6: position.  The first text of a page footnote in the
%% records of a page is the first one below the rule.
\cs_new_protected:Npn \__tpf_record_pos:nnnnnn #1#2#3#4#5#6
  {
    \bool_lazy_and:nnT
      { \str_if_eq_p:nn {#4} { 1 } }
      { ! \str_if_eq_p:nn {#2} { box } }
      {
        \tl_if_exist:cF { g__tpf_px_#3_tl }
          {
            \tl_gset:cn { g__tpf_pid_#3_tl } {#1}
            \tl_gset:cn { g__tpf_px_#3_tl } {#5}
            \tl_gset:cn { g__tpf_py_#3_tl } {#6}
          }
      }
  }
\cs_new_protected:Npn \__tpf_check_rules:
  {
    \int_set_eq:NN \l__tpf_min_int \c_max_int
    \int_zero:N \l__tpf_max_int
    \seq_map_inline:Nn \g__tpf_rules_seq
      {
        \tl_if_exist:cTF { g__tpf_px_##1_tl }
          {
            \int_set:Nn \l__tpf_dist_int
              {
                \int_abs:n
                  { \tl_use:c { g__tpf_px_##1_tl } - \tl_use:c { g__tpf_rx_##1_tl } }
                + \int_abs:n
                  { \tl_use:c { g__tpf_py_##1_tl } - \tl_use:c { g__tpf_ry_##1_tl } }
              }
            \iow_term:e
              {
                TPF-TOP:~p.##1,~text~\tl_use:c { g__tpf_pid_##1_tl }~
                is~\int_use:N \l__tpf_dist_int sp~from~the~rule
              }
            \int_set:Nn \l__tpf_min_int
              { \int_min:nn { \l__tpf_min_int } { \l__tpf_dist_int } }
            \int_set:Nn \l__tpf_max_int
              { \int_max:nn { \l__tpf_max_int } { \l__tpf_dist_int } }
          }
          { \__tpf_ng:n { lone~footnote~rule~on~p.##1 } }
      }
    \seq_if_empty:NTF \g__tpf_rules_seq
      { \__tpf_ng:n { no~footnote~rule } }
      {
        %% 0.25pt: without the space of \footnotetopkern a moved
        %% footnote is 2pt nearer to the rule.  A first line that is
        %% higher than \footnotesep (jarticle: by 0.04pt) is that much
        %% nearer when it is moved, also in the body text.
        \int_compare:nNnTF
          { \l__tpf_max_int - \l__tpf_min_int } > { 16384 }
          {
            \__tpf_ng:n
              {
                the~first~footnote~of~a~page~is~
                \int_use:N \l__tpf_min_int sp~to~
                \int_use:N \l__tpf_max_int sp~from~the~rule
              }
          }
          {
            \iow_term:e
              {
                TPF-RULE~OK~(\seq_count:N \g__tpf_rules_seq \c_space_tl
                rules,~\int_use:N \l__tpf_min_int sp~to~
                \int_use:N \l__tpf_max_int sp)
              }
          }
      }
  }
\ExplSyntaxOff

%% \TPFfiller{<n>}: a paragraph of <n> filler sentences
\newcommand\TPF@filler{%
  This is a filler sentence of plain English text for the test. }
\newcommand\TPFfiller[1]{%
  \count@=0
  \loop\advance\count@ by 1 \TPF@filler\ifnum\count@<#1 \repeat\par}
\makeatother
\endinput
