%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% HTMLforTeX by Anthony Phan.
% file: htmlgraf.tex (paragraph processor)
% last modification: October 26, 2001.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\catcode`\@=11
%
\newcount\par@count
\newskip\par@remainder
\par@remainder=\z@skip
\newdimen\par@hspace
\par@hspace=2pt
\newdimen\par@vspace
\par@vspace=2pt
%
\let\true@vskip=\vskip
\let\true@par=\par
%
\def\vskip{\bgroup\afterassignment\@vskip\skip@=}%
\def\@vskip{\global\advance\par@remainder by \skip@
	\true@vskip\skip@\egroup}%
%
\def\par@break#1{\par\skip@=\ifcase#1
	\z@skip\or\smallskipamount\or\medskipamount
	\or\medskipamount\advance\skip@ by \smallskipamount
	\or\bigskipamount\else
	\bigskipamount\advance\skip@ by \medskipamount\fi
	\ifdim\lastskip<\skip@
		\ifdim\lastskip=\z@\else\vskip-\lastskip\fi
		\ifcase#1 \or\penalty-50\or\penalty-100\or
		\penalty-150\or\penalty-200\else\penalty-300\fi
		\vskip\skip@\fi}
%
\def\par@nobreak#1{\par\nobreak\skip@=\ifcase#1
	\z@skip\or\smallskipamount\or\medskipamount
	\or\medskipamount\advance\skip@ by \smallskipamount
	\or\bigskipamount\else
	\bigskipamount\advance\skip@ by \medskipamount\fi
	\ifdim\lastskip<\skip@
		\ifdim\lastskip=\z@\else\vskip-\lastskip\fi
		\vskip\skip@\fi}
%
\def\par@vhide#1{\ifvmode\edef\par@@vhide{\noexpand\true@vskip-\the\lastskip
	\noexpand\true@vskip\the\lastskip}\else\let\par@@vhide=\relax\fi
	#1\par@@vhide}%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\par@count=0
%
\edef\par@stack{0pt \the\hsize}% ????????????
%
\def\theparstructure{(\the\par@count)[\par@stack]%
	$<$\the\par@remainder$>$}%
%
\def\par@par{\beforegraf\true@par\aftergraf}%
%
\let\aftergraf=\relax
%
\def\beforegraf{%
	\count@=\par@remainder\divide\count@ by \baselineskip
	\ifnum\count@>0
		\skip@=\baselineskip\multiply\skip@ by \count@
		\global\advance\par@remainder by -\skip@
		\expandafter\par@remove\the\count@\@
	\fi
	\ifnum\par@count>1
		\parshape=\the\par@count\space\par@stack
		\def\@aftergraf{\par@remove\prevgraf\@}%
	\else
		\let\@aftergraf=\relax
		\global\let\par=\true@par
	\fi}%
%
\def\aftergraf{\@aftergraf\prevgraf=0}%
%
\def\par@check#1\@{%
	\ifnum\par@count<2
		\global\let\par=\par@par
		\global\par@remainder=\z@skip
		\prevgraf=0
	\fi
	{%
	\ifnum\par@count>#1
	\else
		\count@=#1\advance\count@ by 1
		\ifnum\par@count<1
			\xdef\par@stack{0pt \the\hsize}\par@count=1
		\fi
		\loop\ifnum\count@>\par@count
			\xdef\par@stack{\par@stack\space 0pt \the\hsize}%
			\advance\count@ by -1
		\repeat
		\par@count=#1\global\advance\par@count by 1
	\fi}}%
%
\def\par@change#1[#2,#3]{{%
	\par@check#1\@\par@change@#1[#2,#3]}}%
%
\def\par@change@#1[#2,#3]{{%
	% conditional definitions for the two following loops
	\ifdim#2>\z@
		\def\tmp@i##1{\dimen@=##1
			\advance\dimen@ by #2
			\ifdim\dimen@i<\dimen@\dimen@i=\dimen@\fi}
		\def\@tmp@i##1{\def\@tmp{\the\dimen@i}%
			\advance\dimen@ by -\dimen@i
			\advance\dimen@ by ##1}%
	\else
		\def\tmp@i##1{}%
		\def\@tmp@i##1{\def\@tmp{##1}}%
	\fi
	\ifdim#3>\z@
		\def\tmp@ii##1##2{\dimen@=\hsize
			\advance\dimen@ by #3
			\advance\dimen@ by -##2
			\advance\dimen@ by -##1
			\ifdim\dimen@ii<\dimen@\dimen@ii=\dimen@\fi}%
		\def\@tmp@ii##1##2{\dimen@=\hsize
			\advance\dimen@ by -\dimen@ii
			\advance\dimen@ by -##1}%
	\else
		\def\tmp@ii##1##2{}%
		\def\@tmp@ii##1##2{\dimen@=##2}%
	\fi
	% get the widest left and right margins lengths
	% while putting top lines on \par@top
	\def\par@top{}%
	\def\@do##1 ##2 ##3\@done{%
		\tmp@i{##1}%
		\tmp@ii{##1}{##2}%
		\edef\par@top{##1\space##2\space\par@top}%
		\def\par@stack{##3}}%
	\dimen@i=\z@\dimen@ii=\z@\count@=0
	\loop\ifnum\count@<#1
		\expandafter\@do\par@stack\@done
		\advance\count@ by 1
	\repeat
	% putting lines from \top@stack back to \par@stack
	\def\@do##1 ##2 ##3\@done{%
		\@tmp@ii{##1}{##2}%
		\@tmp@i{##1}%
		\xdef\par@stack{\@tmp\space
			\the\dimen@\space\par@stack}%
		\def\par@top{##3}}%
	\count@=0
	\loop\ifnum\count@<#1
		\expandafter\@do\par@top\@done
		\advance\count@ by 1
	\repeat}}%
%
\def\par@change@after#1#2[#3,#4]{{\count@=#1
	\advance\count@ by #2
	\expandafter\par@check\the\count@\@
	\def\par@top{}%
	\def\@do##1 ##2 ##3\@done{%
		\edef\par@top{##1\space##2\space\par@top}%
		\def\par@stack{##3}}%
	\count@=0
	\loop\ifnum\count@<#1
		\expandafter\@do\par@stack\@done
		\advance\count@ by 1
	\repeat
	\par@change#2[#3,#4]%
	\def\@do##1 ##2 ##3\@done{%
		\xdef\par@stack{##1\space##2\space\par@stack}%
		\def\par@top{##3}}%
	\count@=0
	\loop\ifnum\count@<#1
		\expandafter\@do\par@top\@done
		\advance\count@ by 1
	\repeat}}%
%
\newcount\loop@count
\def\par@remove#1\@{\count@=\par@count\advance\count@ by -1
	\ifnum#1<\count@\count@=#1\fi
	\global\advance\par@count by -\count@
	\def\@do##1 ##2 ##3\@done{\xdef\par@stack{##3}}%
	\loop@count=0
	\loop\ifnum\loop@count<\count@
		\expandafter\@do\par@stack\@done
		\advance\loop@count by 1\repeat}%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\def\par@align@center{%
	\ifhmode\par\fi\noindent\hfill
	\setbox0=\vbox{\kern\par@vspace\unvbox0\kern\par@vspace}%
	\ifdim\ht0>\baselineskip
		\skip@=\ht0\advance\skip@ by -\baselineskip
		\global\advance\par@remainder by \skip@
	\fi
	$\vcenter{\box0}$\hfill\null\par\noindent}%
%
\def\par@side@valign@top{%
	\setbox0=\vbox{\unvbox0\kern\par@vspace}\dimen@ii=\ht0}%
%
\def\par@side@valign@middle{%
	\dimen@ii=\ht0\advance\dimen@ii by \baselineskip
	\divide\dimen@ii by 2}% try to use lastskip
%
\def\par@side@valign@bot{\dimen@ii=\baselineskip}% try to use lastskip
%
\def\par@align@left{%
	\ifhmode\par\fi
	\let\par@side@valign@default=\par@side@valign@top
	\expandafter\ifx\csname par@side@valign@\par@valign\endcsname\relax
		\def\par@valign{default}\fi
	\csname par@side@valign@\par@valign\endcsname
	\setbox0=\hbox{\box0\kern\par@hspace}%
	\ifnum\par@count>1
	\def\@do##1 ##2 ##3\@done{\dimen@i=##1}%
	\expandafter\@do\par@stack\@done
	\else\dimen@i=\z@\fi
%
	\def\@flip[##1,##2]{[##2,##1]}%
	\par@align@end}%
%
\def\par@align@right{%
	\ifhmode\par\fi
	\let\par@side@valign@default=\par@side@valign@top
	\expandafter\ifx\csname par@side@valign@\par@valign\endcsname\relax
		\def\par@valign{default}\fi
	\csname par@side@valign@\par@valign\endcsname
	\setbox0=\hbox{\kern\par@hspace\box0}%
	\ifnum\par@count>1
	\def\@do##1 ##2 ##3\@done{\dimen@i=##1
		\advance\dimen@i by ##2}%
	\expandafter\@do\par@stack\@done
	\else\dimen@i=\hsize\fi
	\advance\dimen@i by -\wd0
%
	\def\@flip[##1,##2]{[##1,##2]}%
	\par@align@end}%
%
\def\par@align@end{%
	\count@=\dimen@ii
	\divide\count@ by \baselineskip
	\dimen@=\baselineskip
	\multiply\dimen@ by -\count@
	\advance\dimen@ by \dimen@ii
	\ifdim\dimen@>0.25\baselineskip\advance\count@ by 1\fi% fine tuning ?
	\edef\@tmp{\the\count@\@flip[0pt,\the\wd0]}%
	\expandafter\par@change\@tmp
	\advance\dimen@ii by -\ht0
	\dimen@=\prevdepth
	\nointerlineskip
	\par@vhide{\vbox to 0pt{\ifdim\dimen@>\z@\kern\dimen@\fi
		\kern\dimen@ii\hbox{\kern\dimen@i\box0}\vss}}%
	\nobreak\prevdepth=\dimen@}%
%
%
\def\par@inside@valign@top{%
	\setbox1=\hbox{l}%
	\raise\ht1\vtop{\null\unvbox0}}%
%
\def\par@inside@valign@middle{%
	$\vcenter{\box0}$}%
%
\def\par@inside@valign@bot{%
	\box0}%
%
%
\def\par@align@default{%
	% mauvaise strastegie, cela influence trop
	% le haut de paragraphe.
	\let\par@inside@valign@default=\par@inside@valign@bot
	\expandafter\ifx\csname par@inside@valign@\par@align\endcsname\relax
		\def\par@valign{default}\fi
%	\ifdim\ht0>\baselineskip
%		\skip@=\ht0\advance\skip@ by -\baselineskip
%		\global\advance\par@remainder by \skip@
%	\fi
	\leavevmode\csname par@inside@valign@\par@valign\endcsname}%
%
% non-robust: \par@align and \par@valign must have
% a convenient meaning.
%
\def\par@insert#1{{%
	\setbox0=\vbox{#1}%
	\expandafter\ifx\csname par@align@\par@align\endcsname\relax
		\def\par@align{default}\fi
	\csname par@align@\par@align\endcsname}%
	\def\par@align{default}\let\par@valign=\par@align}%
\def\par@align{default}\let\par@valign=\par@align
%
\newdimen\par@linewidth
\newdimen\par@leftskip
\def\par@line@status{\ifnum\par@count>1
	\def\@do##1 ##2 ##3\@done{%
		\par@leftskip=##1\par@linewidth=##2}%
	\expandafter\@do\par@stack\@done
	\else\par@leftskip=\z@\par@linewidth=\hsize\fi}%
%
\endinput




