Board index » delphi » Determine font size

Determine font size

Could somebody help with some sample codes to determine what will be the font size of a string that must fit in one line on a canvas of the given bitmap.

Thanx

 

Re:Determine font size


Quote
In article <3eecb...@newsgroups.borland.com>, Basheer wrote:
> Could somebody help with some sample codes to determine what will be the

font size of a string that must fit in one line on a canvas of the given
bitmap.

Quote

Simple solution (untested):

if Bitmap.Canvas.TextWidth(YourString) >= Bitmap.Width then
 while Bitmap.Canvas.TextWidth(YourString) > Bitmap.Width do
  begin
    if Bitmap.Canvas.Font.Size = 1 then
        break;
    Bitmap.Canvas.Font.Size:= Bitmap.Canvas.Font.Size-1;
  end
  else
  while Bitmap.Canvas.TextWidth(YourString) < Bitmap.Width do
    Bitmap.Canvas.Font.Size:= Bitmap.Canvas.Font.Size+1;

Gordon

Re:Determine font size


Quote
"Basheer" <chuk...@nwrc-sa.org> wrote in message

news:3eecb3f6@newsgroups.borland.com...
Quote

> Could somebody help with some sample codes to determine what will be the

font size of a string that must fit in one line on a canvas of the given
bitmap.

This question breaks down into these two problems:
- determining the largest font size that results in width/height <= a given
value
- determining width/height of a string given a font size

The first problem is best solved like this, in pseudo-code

smaller := a font size that is guaranteed to be smaller, eg 0
larger := a font size that is guaranteed to be larger, eg 100
while smaller+1<larger do
begin
  mid := ((smaller+larger) div 2);
  test mid
  if test proves that mid is smaller then
    smaller := mid
  else
    larger := mid;
end;
smaller is now the font size you are after

The benefit is that, while testing all values from 1 to 100 requires 100
tests, this method only needs about 8 tests to yield the same result, and
thus is a lot faster.

For the second problem, a usual method will suffice to get the width and
height of the text, NOT counting underhang of the first and overhang of the
last character. These can be determined only using GetCharABCWidths for
true-type fonts. Consult your win32 help file for more info on this.

Joris

Re:Determine font size


Hi Basheer,
  This code is from an earlier post of mine.

function FitTextInRect(aCanvas: TCanvas; aRect: TRect; aText: string):
integer;
var
  i: integer;
  size: TSize;
  saveHeight: integer;
begin
  saveHeight:= aCanvas.Font.Height;
  for i := (aRect.Bottom - aRect.Top) downto 0 do
  begin
    aCanvas.Font.Height := i;
    size := aCanvas.TextExtent(aText);
    if (size.cx < (aRect.Right - aRect.Left)) or
       (size.cy < (aRect.Bottom - aRect.Top)) then
    begin
      result := i;
      break;
    end;
  end;

  aCanvas.Font.Height := saveHeight;
end;

To make use of this routine, do the following....

  Canvas.Font.Height := FitTextInRect(Canvas, Bounds(10, 10, 50, 50), 'Test
text');
  Canvas.TextOut(10, 10, 'Test text');

Hope this helps,

Regards,
Ananth B.

Quote
"Basheer" <chuk...@nwrc-sa.org> wrote in message

news:3eecb3f6@newsgroups.borland.com...
Quote

> Could somebody help with some sample codes to determine what will be the

font size of a string that must fit in one line on a canvas of [snip]

Other Threads