%% This is file `novel-Footnotes.sty', part of `novel' document class.
%% Copyright 2017-2023 Robert Allgeyer.
%%
%% 
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, version 1.3c only.
%%
\ProvidesFile{novel-Footnotes.sty}%
[2023/03/25 v1.81b LaTeX file (commands for footnotes and endnotes)]
%%



%% Markers (superscript, subscripts):
\newtoggle{@usesymbolmark} % true when footnote markers are symbolic
\newtoggle{@realmarker} % true when OT features sups, sinf, subs requested
%


%% superscripts and subscripts
\let\subs\relax
\let\sups\relax
\let\textsuperscript\relax
\let\textsubscript\relax
% Perhaps superscripts and subscripts (scientific) are in the font, or not.
% Problem: The best fonts have these characters at size/weight designed
%   to mingle with normal size. But many fonts merely scaled characters,
%   which will be too light at the small size.
% The unstarred version of these commands use the Open Type features when
%   they are present, then faking (auto scale/raise) if features not present. 
% The starred version just fakes, without regard for features.
% 

% Note that the effect of these commands continues, not just one-shot:
\gdef\realmarker{\global\toggletrue{@realmarker}} % anywhere in text
\gdef\fakemarker{\global\togglefalse{@realmarker}} % anywhere in text
\newlength\@hoAhl % height of Ahl, locally measured and set when needed
%%
\ExplSyntaxOn
%
\DeclareDocumentCommand \sups { m } {%
  \iftoggle{@realmarker}{%
    \fontspec_if_feature:nTF {sups}{%
      \ifvmode\else\unskip\fi%
      {\addfontfeature{VerticalPosition=Superior}\smash{#1}}%
    }{%
      \setlength\@hoAhl{\heightof{Ahl}}% local
      \ifvmode\else\unskip\fi\charscale[0.7,0pt,0.5\@hoAhl]{#1}%
    }%
  }{%
    \setlength\@hoAhl{\heightof{Ahl}}% local
    \ifvmode\else\unskip\fi%
    {\addfontfeature{Numbers=Lining}\charscale[0.7,0pt,0.5\@hoAhl]{#1}}%
  }%
}% end \sups
%
\DeclareDocumentCommand \subs { m } {%
  \iftoggle{@realmarker}{%
    \fontspec_if_feature:nTF {sinf}{%
      \ifvmode\else\unskip\fi%
      {\addfontfeature{VerticalPosition=ScientificInferior}\smash{#1}}%
    }{%
      \fontspec_if_feature:nTF {subs}{%
        \ifvmode\else\unskip\fi%
        {\addfontfeature{VerticalPosition=Inferior}\smash{#1}}%
      }{%
        \setlength\@hoAhl{\heightof{Ahl}}% local
        \ifvmode\else\unskip\fi\charscale[0.7,0pt,-0.2\@hoAhl]{#1}%
      }%
    }%
  }{%
    \setlength\@hoAhl{\heightof{Ahl}}% local
    \ifvmode\else\unskip\fi%
    {\addfontfeature{Numbers=Lining}\charscale[0.7,0pt,-0.2\@hoAhl]{#1}}%
  }%
}% end \subs
%
\cs_new_eq:NN \textsuperscript \sups
\cs_new_eq:NN \textsubscript \subs
\ExplSyntaxOff
%%



% \SetMarkerStyle does two things.
% Option real: OT features sups, sinf, subs requested. Fake if not available.
% Option fake: Always fake OT features sups, sinf, subs.
% Mandatory: asterisk, dagger, number. Pertains to footnote markers only.
% No matter real or fake, the asterisk symbol is always real, and the
%   dagger, dbldagger, and section (when used as footnote markers)
%   are always partially faked.
\newcommand\SetMarkerStyle[2][real]{
  \def\@goodfnm{no}
  \ifthenelse{\equal{#2}{asterisk}}{%
    \def\@goodfnm{yes}\global\toggletrue{@usesymbolmark}%
    \gdef\@myfnsymbols{*}}{}
  \ifthenelse{\equal{#2}{dagger}}{%
    \def\@goodfnm{yes}\global\toggletrue{@usesymbolmark}%
    \gdef\@myfnsymbols{†}}{}
  \ifthenelse{\equal{#2}{number}}{%
    \def\@goodfnm{yes}\global\togglefalse{@usesymbolmark}}{}
  \def\@goodfnk{no}
  \ifthenelse{\equal{#1}{real}\OR\equal{#1}{}}{%
    \def\@goodfnk{yes}\global\toggletrue{@realmarker}}{}
  \ifthenelse{\equal{#1}{fake}}{%
    \def\@goodfnk{yes}\global\togglefalse{@realmarker}}{}
  \ifthenelse{\equal{\@goodfnm}{no}\OR\equal{\@goodfnk}{no}}{%
    \ClassError{novel}{Bad choice for \string\SetMarkers}%
    {Optional: real, fake. Required: asterisk, dagger, number.}%
  }{}
}
\LetLtxMacro{\SetMarkers}{\SetMarkerStyle} % backward compatibilty pre-v1.40.3.
%
\SetMarkerStyle[real]{asterisk} % default
%%



%% FOOTNOTES
%% ----------------------------------------------------------------------------
% In `novel' class, you may place footnotes at the botom of the textblock.
% The markers may be symbolic (default begins with asterisk), or
% set so that the asterisk is not used, or the markers may be numbers,
% If symbolic, they will be automatically reset by ChapterStart.
% You can manually reset them at any time.
% You can also make a non-marked footnote, which does not increment the count.


% Lengths used to tweak vertical position of symbolic (text) footnote markers:
\newlength\@twast % tweaks asterisk
\newlength\@twdag % tweaks dagger
\newlength\@twdbldag % tweaks double dagger
\newlength\@twsect % tweaks section mark
\newlength\@tweight % tweaks 8, reliably the tallest digit


%% footnotesize is calculated by layout, based on normal font size and skip.
%% It is used only for actual footnotes!

%
\newcounter{@fnsymbolcount} % counter, footenote symbolic markers
  \setcounter{@fnsymbolcount}{1}
\newcounter{@fnnumbercount} % counter, footnote numerical markers
  \setcounter{@fnnumbercount}{1}
\newcounter{@rnnumbercount} % counter, endnote markers (always numerical)
  \setcounter{@rnnumbercount}{1}
\newcounter{@maxfnsymbol} % max footnote symbol count before recycle
%
\gdef\ResetFootnote{% symbols or numbers
  \setcounter{@fnsymbolcount}{1}%
  \setcounter{@fnnumbercount}{1}%
}
\gdef\ResetFootnoteSymbol{\setcounter{@fnsymbolcount}{1}} % only symbols
\gdef\Resetendnote{\setcounter{@rnnumbercount}{1}}
%
\newtoggle{@usenomark} % when true: no mark, no increment count
\newtoggle{@placedfootnotenumber} % true if any numerical footnote placed
%
\let\@myoldfootnote\footnote\relax % copies the original definition
\let\footnote\relax % so it can be re-defined, below
%
\gdef\@fourem{% length of rule separating footnotes from main text
  \vspace{-\nbs}%
  \noindent\strut\smash{\rule[-2pt]{0.33\textwidth}{0.05\@SetFontSize}}%
}
\let\footnoterule\@fourem
%


% \@makefnmark applies to the footnote marker in the flow of main text:
\gdef\@makefnmark{\iftoggle{@usenomark}{}{\@getfnmark}}
%
\long\gdef\@makefntext#1{% the actual footnote, at bottom of textblock
  \iftoggle{@usenomark}%
  {\forceindent#1}%
  {\forceindent\@getbotfnmark\,#1}%
} % end \@makefntext
% Now re-define \footnote:
\newcommand\footnote[2][]{%
  \ifthenelse{\equal{#1}{0}}{%
    \global\toggletrue{@usenomark}%
  }{%
    \global\togglefalse{@usenomark}%
  }%
  \gdef\footnoterule{\vfill\@fourem}%
  \begingroup%
  \def\footnotesize{% Not gdef. Local to footnote. Calculated during layout.
    \@setfontsize\footnotesize{\@myfootnotesizeN pt}{\@myfootnoteskipN pt}%
  }%
  \unskip\@myoldfootnote{#2}%
  \endgroup%
} % end \footnote
%
% \@rollfnsymbol prevents running out of symbolic markers:
\gdef\@rollfnsymbol{%
  \stepcounter{@fnsymbolcount}%
  \ifthenelse{\equal{\value{@fnsymbolcount}}{\value{@maxfnsymbol}}}{%
    \setcounter{@fnsymbolcount}{1}%
  }{}%
} % end \@rollfnsymbol
%
% \@getfnmark places a footnote marker in the running text.
% The marker depends upon whether it should be a number or a symbol.
% If the latter, then it depends on whether the symbols include asterisks.
% And, it depends upon the current count for that type of marker.
\gdef\@getfnmark{%
  \iftoggle{@usesymbolmark}%
  {%
    \global\togglefalse{@placedfootnotenumber}%
    \ifthenelse{\equal{\@myfnsymbols}{*}}{%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{1}}{%
        \charscale[1,0pt,-\@twast]{*}%
      }{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{2}}{%
        \charscale[0.8,0pt,-\@twdag]{†}%
      }{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{3}}{%
        \charscale[0.8,0pt,-\@twdbldag]{‡}%
      }{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{4}}{%
        \charscale[0.8,0pt,-\@twsect]{§}%
       }{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{5}}{%
        \charscale[1,0pt,-\@twast]{**}%
      }{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{6}}{%
        \charscale[0.8,0pt,-\@twdag]{††}%
       }{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{7}}{%
        \charscale[0.8,0pt,-\@twdbldag]{‡‡}%
      }{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{8}}{%
        \charscale[0.8,0pt,-\@twsect]{§§}%
      }{}%
    }{}%
    \ifthenelse{\equal{\@myfnsymbols}{†}}{%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{1}}{%
        \charscale[0.8,0pt,-\@twdag]{†}%
      }{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{2}}{%
        \charscale[0.8,0pt,-\@twdbldag]{‡}%
       }{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{3}}{%
        \charscale[0.8,0pt,-\@twsect]{§}%
      }{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{4}}{%
        \charscale[0.8,0pt,-\@twdag]{††}%
      }{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{5}}{%
        \charscale[0.8,0pt,-\@twdbldag]{‡‡}%
      }{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{6}}{%
        \charscale[0.8,0pt,-\@twsect]{§§}%
      }{}%
    }{}%
  }{%
    \global\toggletrue{@placedfootnotenumber}%
    \sups{\arabic{@fnnumbercount}}%
  }%
} % end \@getfnmark
%
% \@getbotfnmark places the marker in front of the actual footnote,
% towards the bottom of the textblock. It duplicates the marker used in
% running text, but is not elevated. Then, it increments the relevant count.
% Note that (unlike some other TeX document classes) this marker is at
% the indent, so that the footnote text is farther inward than the indent.
% This is the preferred style.
\gdef\@getbotfnmark{%
  \iftoggle{@usesymbolmark}%
  {%
    \ifthenelse{\equal{\@myfnsymbols}{*}}{%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{1}}{*}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{2}}{†}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{3}}{‡}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{4}}{§}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{5}}{**}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{6}}{††}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{7}}{‡‡}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{8}}{§§}{}%
    }{}%
    \ifthenelse{\equal{\@myfnsymbols}{†}}{%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{1}}{†}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{2}}{‡}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{3}}{§}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{4}}{††}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{5}}{‡‡}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{6}}{§§}{}%
    }{}%
    \@rollfnsymbol%
  }{%
    \textrm{\arabic{@fnnumbercount}.}%
    \stepcounter{@fnnumbercount}%
  }%
} %end \@getbotfnmark
%
%% end footnotes



%% ENDNOTES
%% ----------------------------------------------------------------------------
% \endnote only places a marker. It does not store a note for later use.
% Any argument is non-printing. So \endnote{ancestry of yada} does not print
%   anything other than the endnote marker number, and does not save
%   `ancestry of Yada' for later use. The argument is a self-memo, so that you
%   remember why you want an endnote there, when you look at the source.
% Following \endnote or \endnotetext with [ will produce an error.
%   Reason: Confuses novel's commands with those from banned `endnote' package.
% Whenever \endnote is used, the marker number and page is added to a list,
%   and the list is typed into the log file after the document ends. That way,
%   you can hunt down where the endnotes markers were placed.
\newtoggle{@gotendnotes} % true if \endnote used
%
\long\gdef\@endnotelist{}
\newcounter{@endnotecount}
%
\def\@addendnote{%
  \global\toggletrue{@gotendnotes}%
  \stepcounter{@endnotecount}\sups{\arabic{@endnotecount}}%
  \long\edef\@endnotelist{%
    \@endnotelist\space (\arabic{@endnotecount},p.\thepage)%
  }%
}
\def\@nendnote#1{\@addendnote}
\def\@nenspace{\@addendnote\space}
\def\@nenerror{%
  \ClassError{novel}{\string\endnote\space cannot be followed by [ character}%
   {To avoid possible confusion with similar command from other packages, ^^J%
    you may not follow \string\endnote\space with [ character. ^^J%
    However, you may do \string\endnote\string{\string}[ if desired.}%
}
\def\@ndospace{\@ifnextchar[\@nenerror\@nenspace}
\def\endnote{\@ifnextchar\bgroup\@nendnote\@ndospace}
%%
% \endnotetext{ID}{text} where ID is (usually) the endnote number.
% If ID is other than space or tilde, it prints at left margin, followed by
%   period. Then the entire text is block-indented. Only one paragraph allowed.
% If ID is space or empty, then the entire text is block-indented. This is
%   the method for continuing a prior endnote to a new paragraph. The text
%   may begin with \forceindent, according to your needs.
\def\@nentexterror{%
  \ClassError{novel}{\string\endnotetext\space cannot be followed by [ character}%
   {To avoid possible confusion with similar command from other packages, ^^J%
    you may not follow \string\endnotetext\space with [ character. ^^J%
    This command takes two mandatory arguments. No optional argument.}%
}
%
\newcommand\nentext[2]{%
  \ifthenelse{\equal{#1}{\space}\OR\equal{#1}{}}%
    {\noindent\hangindent=\parindent\flexbox{\space}#2\par}%
    {\noindent\hangindent=\parindent\flexbox{\lnum{#1}. }#2\par}%
}
% In case the user inadvertently uses systax from `endnotes' package:
\def\endnotetext{\@ifnextchar[\@nentexterror\nentext}
%
%% end endnotes


%% See novel.cls for the accompanying \AtBeginDocument routine.


%% Pertains to some standard LaTeX footnote code:
\AtBeginShipout{\gdef\@fnafter{}}
%%


\gdef\@ActivateFootnotes{% called by `novel.cls' \AtBeginDocument
  % Pertains to novel-footnotes.sty:
  \LetLtxMacro\SetMarkerStyle\relax
  \gsetlength\footnotesep{0.8\nbs}
  % Adjusted position of footnote markers. Must wait for main font size:
  \gsetlength\@twast{\heightof{*}-\normalAringheight}
  \gsetlength\@twdag{\heightof{†}}
  \gsetlength\@twdag{0.8\@twdag-\normalAringheight}
  \gsetlength\@twdbldag{\heightof{‡}}
  \gsetlength\@twdbldag{0.8\@twdbldag-\normalAringheight}
  \gsetlength\@twsect{\heightof{§}}
  \gsetlength\@twsect{0.8\@twsect-\normalAringheight}
  \gsetlength\@tweight{\heightof{8}}%
  \gsetlength\@tweight{0.8\@tweight-\normalAringheight}%
  % Must wait to see how \SetFootnoteMarker was used:
  \ifthenelse{\equal{\@myfnsymbols}{*}}{\setcounter{@maxfnsymbol}{8}}{}
  \ifthenelse{\equal{\@myfnsymbols}{†}}{\setcounter{@maxfnsymbol}{6}}{}
  % Values to be used by footnotes:
  \FPdiv{\@myFontScale}{\strip@pt\@SetFontSize}{12}
  % Substitute for \footnotesize, but only works for actual footnotes,
  %   about 10pt with similarly reduced skip:
  \FPmul{\@myfootnotesizeN}{\@myFontScale}{10}
  \FPround{\@myfootnotesizeN}{\@myfootnotesizeN}{2}
  \FPmul{\@myfootnoteskipN}{\@myFontScale}{\strip@pt\nbs}
  \FPmul{\@myfootnoteskipN}{\@myfootnoteskipN}{0.84}
  \FPround{\@myfootnoteskipN}{\@myfootnoteskipN}{2}
}% end \@ActivateFootnotes






%% ATENDDOCUMENT
%% ----------------------------------------------------------------------------
%% Write a list of endnote (number,page) into log file,
% and Big Bad Warning if numerical footnotes and endnotes both used:
\AtEndDocument{%
  \iftoggle{@gotendnotes}{%
    \typeout{^^JClass `novel' Alert: You placed endnotes (number,page): ^^J%
     \@endnotelist ^^J}%
  }{}
  %
  \iftoggle{@placedfootnotenumber}{%
    \iftoggle{@gotendnotes}{%
      \ClassWarning{novel}{^^JBIG BAD WARNING! You used numbered footnotes ^^J%
        and numbered endnotes. Better change to symbolic footnotes! ^^J}%
    }{}
  }{}%
}% end \AtEndDocument
%%


%%
\endinput
%%
%% end of file `novel-Footnotes.sty'

