User:Luigi.scarso/autosize

From Wiki
Jump to navigation Jump to search

\newbox\linebox \newbox\snapbox \newdimen\CurrentHsize \newdimen\CurrentWdSnapBox \newdimen\TargetWidth \newcount\Cycle \newcount\LineCount \newcount\BadCount \newcount\HBadCount \newif\ifAgain \def\eatlines{% \immediate\write16{*** eatlines ***}% \setbox\linebox=\lastbox % \ifvoid\linebox%\immediate\write16{2a) void}% \else%s\immediate\write16{2b) no void}% if it's not empty \unskip\unpenalty % take whatever is {\eatlines} % above it; % collapse the line \global\advance\LineCount by 1 \setbox\snapbox\hbox to\wd\linebox{\unhcopy\linebox}% \immediate\write16{badness=\the\badness,hbadness=\the\hbadness}% \BadCount=\badness \HBadCount=\hbadness \ifnum\badness >\hbadness\global\Againtrue\fi \setbox\snapbox\hbox{\unhcopy\linebox}% \immediate\write16{wd\\snapbox=\the\wd\snapbox,wd\\linebox=\the\wd\linebox,CurrentWdSnapBox=\the\CurrentWdSnapBox}% \ifdim\wd\snapbox>\CurrentWdSnapBox % \immediate\write16{: wd.snapbox=\the\wd\snapbox >CurrentWdSnapBox=\the\CurrentWdSnapBox}% \global\CurrentWdSnapBox=\wd\snapbox% \immediate\write16{: ==>CurrentWdSnapBox=wd.snapbox=\the\wd\snapbox}% \fi% %% \box\snapbox %%%\ruledhbox{\llap{\the\LineCount~S~\rlap{\kern5ex\kern\hsize\the\wd\snapbox}}\box\snapbox} %%\ruledhbox{\llap{\the\LineCount~S~\rlap{\kern5ex\kern\hsize\the\wd\linebox}}\box\linebox} %% \ifdim\wd\snapbox<\wd\linebox% %% %\box\snapbox % take the one or the other, %% %% \box\snapbox %% \ruledhbox{\llap{\the\LineCount~S~\rlap{\kern2ex\kern\hsize\the\wd\snapbox}}\box\snapbox} %% \else %% %% \box\linebox %% \ruledhbox{\llap{\the\LineCount~L~\rlap{\kern2ex\kern\hsize\the\wd\linebox}}\box\linebox} %% %\box\linebox %% \fi% \ruledhbox{\llap{\rlap{\kern1ex% \kern\ifdim\wd\snapbox<\hsize \hsize\else\wd\snapbox\fi{\tt\the\BadCount::\the\HBadCount::\the\hfuzz::\the\wd\snapbox}}}\box\linebox} %% \box\linebox% \fi% \immediate\write16{** end eatline **}% }


\def\CellTable#1{% \setbox800=\hbox{#1} \ifdim\wd800<\TargetWidth\unhbox800\fi% \ifdim\wd800=\TargetWidth\unhbox800\fi% \ifdim\wd800>\TargetWidth \bgroup% \hsize=\TargetWidth% \global\CurrentWdSnapBox=-1pt% \global\CurrentHsize=\hsize% %% First run \Againfalse \immediate\write16{******************************\the\hsize - ******************************}% \setbox800=\vbox{#1\par\eatlines}% \ruledvbox{\copy800}\par \immediate\write16{CurrentWdSnapBox:\the\CurrentWdSnapBox CurrentHsize:\the\CurrentHsize} %\hsize=\CurrentWdSnapBox \global\advance\CurrentHsize by 1pt\hsize=\CurrentHsize \ifAgain \doloop{% \immediate\write16{******************************\the\hsize - ******************************}% \setbox800=\vbox{#1\par\eatlines}% \immediate\write16{******************************\the\hsize - ******************************}% \ruledvbox{\copy800}\par \immediate\write16{CurrentWdSnapBox:\the\CurrentWdSnapBox CurrentHsize:\the\CurrentHsize} %\hsize=\CurrentWdSnapBox \global\advance\CurrentHsize by 1pt\hsize=\CurrentHsize \ifAgain \global\Againfalse%% another loop

\else\exitloop

\fi }\fi%\ifAgain \egroup\fi}


\starttext \TargetWidth=0.35\textwidth \TargetWidth=1pt \CellTable{\blank % \centerline{\the\hsize} \blank% \vrule width \hsize height 1pt \blank% \input tufte }

\stoptext