%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Part of the ptx2pdf macro package for formatting USFM text
% copyright (c) 2007 by SIL International
% written by Jonathan Kew
%
% 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.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% paragraph shape support for the ptx2pdf package

%
% Parameters to the cutout macros:
%   #1 -> width of cutout to create
%   #2 -> begin after this many lines of the paragraph (0 for start of par)
%   #3 -> number of lines to cut
%
%+ccut_cutout
\def\leftcutout{\@cutout{L}}
\def\rightcutout{\@cutout{R}}

\def\@cutout#1#2#3#4{%
  \x@\trace{C}{@cutout #1 #2 #3 #4}%
  \global\advance\@numcuts by 1
  \expandafter\xdef\csname cut@side\the\@numcuts\endcsname{#1}%
  \expandafter\xdef\csname cut@width\the\@numcuts\endcsname{#2}%
  \expandafter\xdef\csname cut@after\the\@numcuts\endcsname{#3}%
  \expandafter\xdef\csname cut@lines\the\@numcuts\endcsname{#4}%
}

\def\save@cutouts#1{%
  \@index=0
  \loop \ifnum\@index<\@numcuts \relax\advance\@index by 1
    \trace{C}{Saving cutout \the\@index: \csname cut@side\the\@index\endcsname/\csname cut@width\the\@index\endcsname/\csname cut@lines\the\@index\endcsname}%
    \expandafter\global\expandafter\let\csname cut@side\the\@index#1\x@\endcsname\csname cut@side\the\@index\endcsname
    \expandafter\global\expandafter\let\csname cut@width\the\@index#1\x@\endcsname\csname cut@width\the\@index\endcsname
    \expandafter\global\expandafter\let\csname cut@after\the\@index#1\x@\endcsname\csname cut@after\the\@index\endcsname
    \expandafter\global\expandafter\let\csname cut@lines\the\@index#1\x@\endcsname\csname cut@lines\the\@index\endcsname
  \repeat
  \x@\xdef\csname cut@numcuts#1\endcsname{\the\@numcuts}%
  \trace{C}{Saved \the\@numcuts cutouts for #1}%
}
\def\restore@cutouts#1{%
  \@index=0
  \ifcsname cut@numcuts#1\endcsname
    \global\@numcuts=\csname cut@numcuts#1\endcsname \relax
    \trace{C}{Restoring \the\@numcuts \space cutouts for #1}%
    \loop \ifnum\@index<\@numcuts \advance\@index by 1
      \x@\global\x@\let\csname cut@side\the\@index\x@\endcsname\csname cut@side\the\@index#1\endcsname
      \x@\global\x@\let\csname cut@width\the\@index\x@\endcsname\csname cut@width\the\@index#1\endcsname
      \x@\global\x@\let\csname cut@after\the\@index\x@\endcsname\csname cut@after\the\@index#1\endcsname
      \x@\global\x@\let\csname cut@lines\the\@index\x@\endcsname\csname cut@lines\the\@index#1\endcsname
      \trace{C}{restored cutout \the\@index: \csname cut@side\the\@index\endcsname/\csname cut@width\the\@index\endcsname/\csname cut@lines\the\@index\endcsname}%
    \repeat
  \fi
}

\newcount\@numcuts

% Forget any current cutouts
\def\cancelcutouts{\ifnum\@numcuts>0 \trace{C}{Cancelled \the\@numcuts\space cutout(s)}\fi\@numcuts=0 }
%-ccut_cutout

%
% This must be called after each paragraph to carry-over any residual amout of cutout
% (e.g., if you ask for a 10-line cutout, but the paragraph only has 6 lines;
% the remaining 4 lines will be cut in the next paragraph)
%
%+ccut_carryover
\def\cutoutcarryover{%
  \trace{C}{cutoutcarryover \the\@numcuts}%
  \ifnum\@numcuts>0
    \count@=\@numcuts \@numcuts=0
    \@index=0 \relax
    \loop \ifnum\@index<\count@ \advance\@index by 1
      \@after=\csname cut@after\the\@index\endcsname
      \advance\@after by -\prevgraf
      \@until=\csname cut@lines\the\@index\endcsname
      \advance\@until by \@after
      \ifnum\@until>0
        \ifnum\@after<0 \@after=0 \fi
        \advance\@until by -\@after
        \@width=\csname cut@width\the\@index\endcsname
        \edef\@side{\csname cut@side\the\@index\endcsname}%
        \@cutout{\@side}{\the\@width}{\the\@after}{\the\@until}%
        \ifx\ch@pterc@tnum\empty\else % keep track of ch@pterc@tnum
          \ifnum\@index=\ch@pterc@tnum
            \xdef\ch@pterc@tnum{\the\@numcuts}%
          \fi
        \fi
      \else
        \ifx\ch@pterc@tnum\empty\else % keep track of ch@pterc@tnum
          \ifnum\@index=\ch@pterc@tnum
            \global\let\ch@pterc@tnum\empty
          \fi
        \fi
      \fi
    \repeat
  \fi
}
%-ccut_carryover

%
% This must be called at the end of each paragraph (e.g., by redefining \par)
% so as to calculate and apply the shape, given all current cutout specifications
%
%+ccut_makecutouts
\def\makecutouts{%
  \ifnum\hangafter<0 \@parshapelines=-\hangafter
  \else \@parshapelines=\hangafter \fi
  \@index=0
  \loop \ifnum\@index<\@numcuts \advance\@index by 1
    \trace{C}{makecutouts (\c@rrdstat) \the\@index, \the\@numcuts}%
    \count@=\csname cut@after\the\@index\endcsname
    \advance\count@ by \csname cut@lines\the\@index\endcsname \relax
    \ifnum\count@>\@parshapelines \@parshapelines=\count@ \fi
    \ifnum\@parshapelines>500 \MSG{*** Impossible number of lines in cutout:\the\count@}\@parshapelines=500\fi
  \repeat
  \advance\@parshapelines by 1
  \def\@shape{}%                                                                %(1)
  \@line=0
  \loop \ifnum\@line<\@parshapelines \advance\@line by 1
    \global\@hsize=\hsize
    \global\@indent=0pt
    \ifnum\hangafter<0
      \ifnum\@line>-\hangafter\else \global\@indent=\hangindent \fi
    \else
      \ifnum\@line>\hangafter \global\@indent=\hangindent \fi
    \fi
    \ifdim\@indent<0pt \global\advance\@hsize by \@indent \global\@indent=0pt
    \else \global\advance\@hsize by -\@indent \fi                               %(2)
    \@cutthisline
    \edef\@shape{\@shape\space\the\@indent\space\the\@hsize}%
  \repeat
  \parshape=\number\@parshapelines \@shape
  \trace{C}{Cutouts: \the\@numcuts, hsize=\the\hsize, \the\parshape(\@shape), \the\@parshapelines\space lines}%
}
%-ccut_makecutouts
%+ccut_cutthisline
\def\@cutthisline{%
  \@index=0
  {\loop \ifnum\@index<\@numcuts \relax
    \advance\@index by 1
    \@after=\csname cut@after\the\@index\endcsname
    \@until=\csname cut@lines\the\@index\endcsname
    \advance\@until by \@after
    \ifnum\@line>\@after
      \ifnum\@line>\@until \else
        \@width=\csname cut@width\the\@index\endcsname
        \global\advance\@hsize by -\@width
        \if L\csname cut@side\the\@index\endcsname
          \global\advance\@indent by \@width
        \fi
        \trace{C}{adjust cutout to (\the\@indent, \the\@hsize) by \the\@width}%
      \fi
    \fi
  \repeat}%
}
\newcount\@parshapelines
\newcount\@index \newcount\@line \newcount\@after \newcount\@until
\newdimen\@width \newdimen\@indent \newdimen\@hsize
%-ccut_cutthisline

%+ccut_pending
\let\pending@items\cstackempty %

% Stack operations for delayed items.
% pending item arguments: 
% 1. Item type 
% 2. Curref
% 3. Y ref (sp) of reference mark (or 0 if no reference yet)
% 4. Width (points) of cutout
% 5. Height (lines) of cutout
% 6. (initially) Lines below reference mark for cutout, later lines below par start
% 7. Cutout side (L or R for Left/Right)
\def\parse@pending#1@#2@#3(#4x#5@#6)#7\E{\def\this@thing{#1}\def\thing@ref{#2}\thing@y=#3
  \def\thing@wd{#4}\def\thing@ht{#5}\def\thing@after{#6}\def\thing@side{#7}%
  \D@IT}%

% This bit is called in proccessing the  pending list to fill the .delay file.
\def\write@pending#1@#2@#3(#4x#5@#6)#7\E{\def\this@thing{#1}\def\thing@ref{#2}\thing@y=#3
  %\MSG{+write@pending#1@#2@#3(#4x#5@#6)#7}%
  \def\thing@wd{#4}\def\thing@ht{#5}\def\thing@after{#6}\def\thing@side{#7}%
  %\MSG{++ \the\prev@y, \the\thing@y, \the\baselineskip, #6}%
  \tmp@lines=\prev@y
  \advance\tmp@lines by -\thing@y
  \divide\tmp@lines by \baselineskip
  \advance\tmp@lines by #6
  \MSG{+++ Writing #1 #2 [#3=>\the\tmp@lines](#4x#5@6)#7}%
  \wr@teDelayedItem{\this@thing}{\thing@ref}{\tmp@lines}{(\thing@wd x\thing@ht @\thing@after)\thing@side}%
}%
\def\get@pending{\let\D@IT=\relax\ifx\pending@items\cstackempty
     \let\this@thing\empty\let\thing@ref\empty\thing@y=0
   \else%
     \let\d@=\parse@pending
     \x@\cstacktop\pending@items\E
     \let\d@\relax
   \fi}
\def\pop@pending{\ifx\pending@items\cstackempty\else
   \xdef\pending@items{\x@\cstackpop\pending@items\E}\fi
   \ifx\pending@items\empty\let\pending@items\cstackempty\fi}

\def\add@pending#1#2#3#4#5#6#7{\trace{C}{add@pending #1@#2 (#4x#5@#6)}%
   \edef\tmp{#1@#2@#3(#4x#5@#6)#7}\ifx\pending@items\cstackempty
   \edef\pending@items{\tmp,}\else\edef\pending@items{\pending@items\tmp,}\fi}

% Having added pending items they are run with run@pending
\def\run@pending{\trace{C}{run@pending: \pending@items (\the\@numcuts)}%
  \ifnum\dropped@ther@lines<0\dropped@ther@lines=0\fi
  \ifx\pending@items\cstackempty\else
    \def\D@IT{%\ifnum\thing@after<1 
      \trace{C}{run@pending Cutout: {\thing@wd}{\thing@after}{\thing@ht}\thing@side}%
      \tmp@lines=\thing@after\advance\tmp@lines\thing@ht
      \ifnum\tmp@lines>\dropped@ther@lines
        \dropped@ther@lines=\tmp@lines
      \fi
      \@cutout{\thing@side}{\thing@wd}{\thing@after}{\thing@ht}%
      %\fi
    }%
    \let\d@\parse@pending
    \x@\cstackdown\pending@items\E
    \let\d@\relax
    \let\pending@items\cstackempty
  \fi\trace{C}{run@pending: done}%
}

\def\write@delayedthing#1#2#3#4#5#6{%
  \edef\dc@ref@rgs{\string\@delayedthing{#1}{\c@rref}{#2}{#3}{#4}{#5}{#6}}%
  %\x@\MSG\x@{+++ CUTOUT \dc@ref@rgs}%
  \ifhmode\beginL\fi
  \pdfsavepos
  \x@\write\x@\p@rlocs\x@{\dc@ref@rgs{\the\pdflastxpos}{\the\pdflastypos}}\ifhmode\endL\fi
  \trace{C}{Cutout data written for \dc@ref@rgs}%
}

%-ccut_pending
\endinput
