%%%  @LaTeX-style-file{
%%%     author          = "Sebastian Rahtz and Phil Taylor",
%%%     version         = "1",
%%%     date            = "5th February 1993",
%%%     filename        = "textfit.sty",
%%%     email           = "spqr@minster.york.ac.uk (Internet)",
%%%     keywords        = "LaTeX, scaling",
%%%     supported       = "yes",
%%%     docstring       = "
%%%
%%%     textfit.sty
%%%
%%% LaTeX style option for scaling up text to a desired size. Requires NFSS
%%%
%%% The user needs to be able to specify the width or height for text, and
%%% use the largest size necessary to fill that width or height. You
%%% can choose whether to have the size increased by simple points, or
%%% by using magsteps.
%%%  Usage:
%%%       \scaletowidth{3in}{This is the way of the world}
%%%       \scaletoheight{7mm}{This is the way of the world}
%%% Details are controlled by two booleans: \ScalebyMagstepstrue will
%%% make the system use magsteps, not points, and \NoisyFittingtrue
%%% will produce useful messages.
%%% "}
\newif\ifScalebyMagsteps
\newif\ifNoisyFitting
\NoisyFittingfalse
\ScalebyMagstepsfalse
\newcount \pts@f@r
\newdimen \desired@size
\newcount \r@mainder
\newcount \m@gstepcount
\newcount \m@gsteplimit
\newcount \m@gstepvalue
\newdimen \m@gstepdimen

\def \magsteps {\afterassignment \m@gsteps \m@gsteplimit = }

\def \m@gsteps
    {\let \@r = \or
     \def \or {\noexpand \or}%
     \m@gstepdimen = 0.166667 \maxdimen
     \m@gstepvalue = 1000 
     \m@gstepcount = 0
     \def \magstep {}%
     \loop
     \ifnum     \m@gstepcount < \m@gsteplimit
                \advance \m@gstepcount by 1
                \ifnum \m@gstepvalue > \m@gstepdimen
                        \advance \m@gstepcount by -1
                        \message {Sorry --- integer overflow would occur if
                                        I went any further; stopped at 
                                                \the \m@gstepcount.}
                        \m@gsteplimit = \m@gstepcount 
                \else
                        \multiply \m@gstepvalue by 12
                        \multiply \r@mainder by 12
                        \divide \r@mainder by 10
                        \advance \m@gstepvalue by \r@mainder
                        \r@mainder = \m@gstepvalue
                        \divide \m@gstepvalue by 10
                        \multiply \m@gstepvalue by 10
                        \advance \r@mainder by -\m@gstepvalue
                        \divide \m@gstepvalue by 10
                        \ifnum \r@mainder < 5
                               \edef \magstep {\magstep \or \the \m@gstepvalue}%
                        \else
                               \advance \m@gstepvalue by 1
                               \edef \magstep {\magstep \or \the \m@gstepvalue}%
                               \advance \m@gstepvalue by -1
                        \fi
                \fi
     \repeat
     \edef \magstep ##1%
          {\noexpand \ifcase ##1
                        1000\magstep 
           \noexpand \else 0%
           \noexpand \message 
                {Sorry --- \string \magstep \space ##1
                                is not in range 0 .. \the \m@gsteplimit}%
           \noexpand \fi
           \noexpand \relax
          }%
     \let \or = \@r
    }

\magsteps 63


\def \Fontname #1{\expandafter \strip@size \fontname #1 }
\def \strip@size #1 #2#{#1\void@@@}
\def \void@@@ #1{}

\def \@scaletofit[#1]#2#3{%
     \def\Hb@x{h}\def\H@rV{#1}%
     \ifx\H@rV\Hb@x\def\C@mpare{\wd}\else\def\C@mpare{\ht}\fi
     \desired@size #2 
     \def\curr@fontname{\Fontname \font {} }%
     \ifScalebyMagsteps
        \pts@f@r 0
     \else
        \pts@f@r \f@size
     \fi
     \setbox 0 = \hbox{#3}%
     \ifdim \C@mpare 0 < \desired@size
	\def\@increment{1}%
	\def\@test{<}%
     \else
        \ifScalebyMagsteps
           \message{When scaling by Magsteps, you cannot go downwards!}%
           \def\@increment{1}%
	   \def\@test{<}%
        \else  
           \def\@increment{-1}%
 	   \def\@test{>}%
        \fi
     \fi
%
     \loop
     \ifScalebyMagsteps
        \font \temp@font = \curr@fontname  scaled \magstep \pts@f@r
     \else
        \font\temp@font=\curr@fontname at \the\pts@f@r pt
     \fi
	\setbox 0 = \hbox{{\temp@font #3}}%
        \ifNoisyFitting
        \ifScalebyMagsteps
           \message {Trying \noexpand \magstep \the \pts@f@r...}%
        \else
           \message {\curr@fontname->\the \pts@f@r...}%
        \fi
        \fi
        \ifdim \C@mpare 0 \@test \desired@size
           \advance \pts@f@r by \@increment
     \repeat
%
     \ifScalebyMagsteps
       \ifnum \pts@f@r=0
         \pts@f@r1
       \fi
     \fi
     \advance \pts@f@r by -\@increment
     \ifNoisyFitting
     \ifScalebyMagsteps
        \message {The calculated font is \curr@fontname 
           scaled \noexpand \magstep \the \pts@f@r}%
        \font \temp@font = \curr@fontname  scaled \magstep \pts@f@r
     \else
        \message {The calculated font is \curr@fontname ->\the \pts@f@r pt}%
        \font\temp@font=\curr@fontname at \the\pts@f@r pt
     \fi
     \fi
     \ifx\H@rV\Hb@x
        \hbox to \desired@size {\hss{\temp@font #3}\hss}%
     \else
        \hbox {{\temp@font #3}}%
     \fi     
    }

% user interface
\def\scaletowidth{\@scaletofit[h]}
\def\scaletoheight{\@scaletofit[v]}

\endinput


Examples; note that the base font is taken from the current state when
you enter the macros. so if you start off with a large font (eg
cmr17), it will give a different result from that obtained by scaling
up cmr5


\documentstyle[textfit]{article}

\begin{document}
\NoisyFittingtrue
\scaletowidth{1in}{This is the way of the world}

{\Huge \scaletowidth{1in}{This is the way of the world}}

{\Huge \scaletowidth{5in}{This is the way of the world}}

\scaletowidth{5in}{This is the way of the world}

{\Huge \scaletoheight{1.3in}{Cowabunga!} }

\ScalebyMagstepstrue

\scaletowidth{1in}{This is the way of the world}

{\Huge \scaletowidth{1in}{This is the way of the world}}

{\Huge \scaletowidth{5in}{This is the way of the world}}

\scaletowidth{5in}{This is the way of the world}

{\Huge \scaletoheight{1.3in}{Cowabunga!} }


\end{document}

