Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Scaling text to fit in window

82 views
Skip to first unread message

Mark Tiede

unread,
Jul 18, 1999, 3:00:00 AM7/18/99
to
I have a rectangle in which I must put text. The rectangle gets resized
from time to time and I must figure out how to scale the font to still
fit in the rectangle. Trouble is, the font is user selectable and could
be proportional. So I have to use TextWidth and TextHeight to figure
out whether the text will still fit and then, somehow, figure out what
to scale it to. I ran into troubles trying to calculate a scaling
factor. It would alternately pick a font height that would be scaled
too big and then scale it too small.

Any suggestions for maintaining a font scale inside a resizable
rectangle?

tiede.vcf

Steve Schafer (TeamB)

unread,
Jul 19, 1999, 3:00:00 AM7/19/99
to

Because font glyphs have to be an integral number of pixels wide, you
are unlikely to find a font size such that the text will fit exactly
within the specified width. What you should do is figure out an
approximate scale factor as a first step:

ScaleFactor := MyRect.Width / TextWidth(...);

Then multiply the Height of the font by this factor to get a new font
height:

Font.Height := Round(ScaleFactor * Abs(Font.Height));

And then iteratively test the width of the resulting text and decrease
the font height one pixel at a time until the text fits:

W := TextWidth(...);
while W > MyRect.Width do begin
Font.Height := Abs(Font.Height) - 1;
W := TextWidth(...) end;

-Steve


Mark L. Tiede

unread,
Jul 19, 1999, 3:00:00 AM7/19/99
to
Steve,

Thanks. I am already scaling things. I thought the iterative process
seemed inefficient and maybe there was a trick I should know, but it sounds
like there isn't. I thought of the iterative algorithm also. Only I tried a
couple of others first :-)

I guess that means there aren't any nice tricks on this one.

mtiede.vcf

Steve Schafer (TeamB)

unread,
Jul 20, 1999, 3:00:00 AM7/20/99
to
On Mon, 19 Jul 1999 10:23:17 -0400, "Mark L. Tiede"
<mti...@mjwcorp.com> wrote:

> I guess that means there aren't any nice tricks on this one.

The reason there aren't any tricks is that it is essentially
impossible to predict how a glyph width will "round off" when you
change the font the size slightly.

-Steve


0 new messages