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

% TeX does not allow these to be executed inside a conditional:
\newif\ifm@rginnotesopen 
\newif\ifm@rginalnotesw@rn
\newwrite\m@rginnotes
\plugin@startif{marginnotes}

\def\@marginnote#1#2#3#4#5#6#7#8{%
% #1 = reference, #2 = marker, #3 = side, #4 = valign, #5 = distance from text,
% #6 = width, #7 = height, #8 = depth, #9 = x-shift, #10 = y-shift
  \@m@rginnote{#1}{#2}% chain to collect parameters over #9
}

\def\@m@rginnote#1#2#3#4#5#6#7{%
% #1 = reference, #2 = marker, #3 = x-shift, #4 = y-shift, #5 = pageno, #6 = xpos, #7 = ypos
  \x@\xdef\csname marginnote-#1@#2:xshift\endcsname{#3}%
  \x@\xdef\csname marginnote-#1@#2:yshift\endcsname{#4}%
  \x@\xdef\csname marginnote-#1@#2:pageno\endcsname{#5}%
  % identify which column it actually was/is in
  \dimen0=#6 sp \adjustpdfXsavepos \dimen1=\dimexpr \textwidth + \SideMarginFactor \MarginUnit\relax\divide \dimen1 2
  \ifBindingGutter\ifodd #5\else\advance\dimen1 \BindingGutter\fi\fi
  \ifdim\dimen0 > \dimen1 \ifRTL\edef\c@l{0}\else\edef\c@l{1}\fi\else\ifRTL\edef\c@l{1}\else\edef\c@l{0}\fi\fi
  \x@\xdef\csname marginnote-#1@#2:colno\endcsname{\c@l}%
  \trace{M}{defining marginnote-#1@#2:pageno = #5, xpos=\the\dimen0, col=\c@l, based on \the\dimen0 >\the\dimen1 (smf=\SideMarginFactor x\the\MarginUnit, tw=\the\textwidth, bg=\the\BindingGutter)}%
}

\def\pl@cemnote#1#2#3#4#5#6{%
% #1 = vbox, #2 = side, #3 = valign, #4 = distance from text, #5 = x-shift, #6 = y-shift
  \edef\t@mp{#2}\edef\t@mpv{#3}%
  \dimen9=\ht#1
  \trace{M}{kerns: pstrut=\the\dp\pstr@t, x-shift=\the#5, y-shift=\the#6, text = \the#4, side = #2, valign = #3, ht=\the\dimen9}%
  \begingroup\leftskip=0pt\rightskip=0pt\parfillskip=0pt\everypar={}%
  \setbox0=\vbox to 0pt{\let\par=\endgraf
    \ifx\t@mpv\@bottom\else\vss\fi
    \ifx\t@mp\l@ft\llap{\box #1\kern #5\kern #4}%
    \else\noindent\hfill\rlap{\kern #5\kern #4\box #1}%
    \fi
    \ifx\t@mpv\@top\else\vss\fi}%
  % \ifx\t@mp\l@ft\showbox0
  \dp0=0pt\vadjust {\kern -\dimen9\kern #6\writem@rginnote\box0\kern-#6\kern\dimen9}%
  \endgroup
}

\def\finishm@rginnotes "#1"{\immediate\closeout\m@rginnotes\m@rginnotesopenfalse}

\def\openm@rginnotes "#1"{
  \ifm@rginnotesopen\else
    \trace{M}{reading #1}
    \catcode`\@=11
    \includeifpresent{#1}
    \openout\m@rginnotes="#1"
    \m@rginnotesopentrue
    \addtoendhooks{\finishm@rginnotes "#1"}
  \fi
}

\m@rginnotesopenfalse
\def\writem@rginnote{%
  \pdfsavepos
  \x@\write\x@\m@rginnotes\x@{\wr@tet@mp{\the\pageno}{\the\pdflastxpos}{\the\pdflastypos}}%
}

\def\d@marginnote#1#2#3#4#5#6{%
% #1 = vbox, #2 = reference, #3 = marker #4 = side #5 = valign #6 = y-shift
  \edef\p@s{#4}%
  \ifnum\c@rrentcols=1\edef\t@mpa{page}\else\edef\t@mpa{col}\fi
  \ifcsname marginnote-#2@#3:\t@mpa no\endcsname\edef\p@geno{\csname marginnote-#2@#3:\t@mpa no\endcsname}\else
    \ifm@rginalnotesw@rn\else\MSG{*** marginnotes: no marginal note for #2-#3; re-run for accurate note positions}\global\m@rginalnotesw@rntrue\fi
    \edef\p@geno{\the\pageno}\fi
  \ifx\p@s\@uter\ifodd\p@geno\ifRTL\let\s@de\l@ft\else\let\s@de\r@ght\fi\else\ifRTL\let\s@de\r@ght\else\let\s@de\l@ft\fi\fi
  \else\ifx\p@s\@nner\ifodd\p@geno\ifRTL\let\s@de\r@ght\else\let\s@de\l@ft\fi\else\ifRTL\let\s@de\l@ft\else\let\s@de\r@ght\fi\fi
  \else\ifx\p@s\relax\let\s@de\l@ft\else\let\s@de\p@s\fi\fi\fi
  \getp@ram{spacebefore}{#3}{#3+\styst@k}%
  \ifx\p@ram\relax \dimen0=1pt\else\dimen0=\p@ram\IndentUnit \divide\dimen0 by 12\fi
  \dimen3 = #6
  \trace{M}{marginnote(#1) #2 #3, pageno=\p@geno, side=\s@de, was #4 \ifcsname marginnote-#2@#3:colno\endcsname, col=\csname marginnote-#2@#3:colno\endcsname\fi, yshift=\the\dimen3, text offset=\the\dimen0, stystack=\styst@k}%
  %\dimen0=0pt
  \ifcsname marginnote-#2@#3:xshift\endcsname\dimen1=\csname marginnote-#2@#3:xshift\endcsname\else\dimen1=0pt\fi
  \ifcsname marginnote-#2@#3:yshift\endcsname\dimen2=\csname marginnote-#2@#3:yshift\endcsname\else\dimen2=0pt\fi
  \xdef\wr@tet@mp{\string\@marginnote{#2}{#3}{\p@s}{#5}{\the\dimen0}{\the\wd#1}{\the\ht#1}{\the\dp#1}{\the\dimen1}{\the\dimen2}}%
  \advance\dimen2 by \dimen3%
  %\ifx#3s \showbox#1\fi
  \pl@cemnote{#1}{\s@de}{#5}{\dimen0}{\dimen1}{\dimen2}%
}

\plugin@endif
