Board index » cppbuilder » Resizing font size

Resizing font size

Hi,

Does anyone know how to resize the font size, to fit a character/string into
a specified area on a canvas ?

I have a StringGrid which resizes its column widths automatically when the
form is resized. The text on the column header cells are clipped when the
cell width and height is smaller than the text width and height. I would
like to re-draw the text with a smaller font size so that the complete
string will be visible in the cell.

Any comments and suggestions will be greatly appreciated. Thanks in advance.

Roshan

 

Re:Resizing font size


Hi Roshan,
    You will need to figure out how large your text is. You can do this
using the Canvas->TextExtent function.

Then you can iteratively reduce the font size and test it until the size is
acceptable.

Hope this helps,

Regards,
Ananth B.

Quote
"Roshan Jurangpathy" <Ros...@ps-electronic.com> wrote in message

news:3e90d5ae@newsgroups.borland.com...
Quote
> Hi,

> Does anyone know how to resize the font size, to fit a character/string
into
> a specified area on a canvas ? [snip]

Re:Resizing font size


Hi Ananth,

I have set the default font to Tahoma, regular, size 8. Setting the size to
2 does not seem to be having any effect. My code is as follows.

//--------------------------------------------------------------------------
-
void __fastcall TFormTestTable::StringGrid1DrawCell(TObject *Sender,int
ACol, int ARow, TRect &Rect, TGridDrawState State)
{
 int iTextWidth;

 if( State.Contains( gdFixed ) ){
  iTextWidth = StringGrid1->Canvas->TextWidth( IntToStr( 20 ) ); // Text
width returns 12
  if( iTextWidth > Rect.Width() ){ // Rect.Width() = 10
   StringGrid1->Canvas->Font->Size = 2;
   iTextWidth = StringGrid1->Canvas->TextWidth( IntToStr( 20 ) );
   if( iTextWidth <= iTextWidth ){
    // Code here never gets executed !!!!
   }
  }
 }

Quote
}

//--------------------------------------------------------------------------
-

Am I overlooking something ?

Regards
Roshan

Quote
"Ananth B." <ana...@agnisoft.com> wrote in message

news:3e911f0d@newsgroups.borland.com...
Quote
> Hi Roshan,
>     You will need to figure out how large your text is. You can do this
> using the Canvas->TextExtent function.

> Then you can iteratively reduce the font size and test it until the size
is
> acceptable.

> Hope this helps,

> Regards,
> Ananth B.

> "Roshan Jurangpathy" <Ros...@ps-electronic.com> wrote in message
> news:3e90d5ae@newsgroups.borland.com...
> > Hi,

> > Does anyone know how to resize the font size, to fit a character/string
> into
> > a specified area on a canvas ? [snip]

Re:Resizing font size


Hi Roshan,
    Your if statement seems to be comparing the iTextWidth variable with
itself!
"if( iTextWidth <= iTextWidth )"

you probably need to compare it with Rect.Width()

Regards,
Ananth B.

Quote
"Roshan Jurangpathy" <Ros...@ps-electronic.com> wrote in message

news:3e912e95$1@newsgroups.borland.com...
Quote
> Hi Ananth,

> I have set the default font to Tahoma, regular, size 8. Setting the size
to
> 2 does not seem to be having any effect. My code is as follows [snip]

Re:Resizing font size


Sorry, it's a typo.;)  I am actually comparing the result with Rect.Width().
Canvas->TextWidth() returns the same value even after changing the
Font->Size property to 2.

Also tried the following :
Canvas->Font->Height = -2 * Canvas->Font->PixelsPerInch / 72
The result is the same..

Regards
Roshan

Quote
"Ananth B." <ana...@agnisoft.com> wrote in message

news:3e913954@newsgroups.borland.com...
Quote
> Hi Roshan,
>     Your if statement seems to be comparing the iTextWidth variable with
> itself!
> "if( iTextWidth <= iTextWidth )"

> you probably need to compare it with Rect.Width()

> Regards,
> Ananth B.

> "Roshan Jurangpathy" <Ros...@ps-electronic.com> wrote in message
> news:3e912e95$1@newsgroups.borland.com...
> > Hi Ananth,

> > I have set the default font to Tahoma, regular, size 8. Setting the size
> to
> > 2 does not seem to be having any effect. My code is as follows [snip]

Re:Resizing font size


Hi Roshan,

Here is a little snippet of code I wrote in Delphi (sorry, I dont have
Builder installed at work) to test this text fitting thing.....
It might not be the fastest way, but it gets the job done.

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
"Roshan Jurangpathy" <Ros...@ps-electronic.com> wrote in message

news:3e913e2e@newsgroups.borland.com...
Quote
> Sorry, it's a typo.;)  I am actually comparing the result with
Rect.Width().
> Canvas->TextWidth() returns the same value even after changing the
> Font->Size property to 2.
>[snip]

Re:Resizing font size


Hi Ananth,

Thanks for the code snippet. I converted it to Builder. It does not seem to
changing the size. aCanvas->TextExtent( aText ) always returns the
same(initial) value regardless of the different values assigned to the font
height property.

Is there any restrictions on how small a particular font could be ? Maybe, I
am trying to assign a value which could be smaller than the minimum font
size..

Regards
Roshan

Quote
"Ananth B." <ana...@agnisoft.com> wrote in message

news:3e914557@newsgroups.borland.com...

Quote
> Hi Roshan,

> Here is a little snippet of code I wrote in Delphi (sorry, I dont have
> Builder installed at work) to test this text fitting thing.....
> It might not be the fastest way, but it gets the job done.

> 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.

[snip]

- Show quoted text -

Quote

Re:Resizing font size


Roshan,
    Your problem is strange. I am using the same code snippet here...and it
seems to resize the text even for a 5x5 rectangle.
I am using Tahoma as the font on this canvas.
All of the TTF's I have on my system seem to give me the correct results.
FON's like "System" (fixed width fonts) seem to give me problems. Otherwise,
I get the proper results.

I could mail you my executable if you like, so u can see if this works on ur
system

Regards,
Ananth B.

Quote
"Roshan Jurangpathy" <Ros...@ps-electronic.com> wrote in message

news:3e915148$1@newsgroups.borland.com...
Quote
> Hi Ananth,

> Thanks for the code snippet. I converted it to Builder. It does not seem
to
> changing the size. aCanvas->TextExtent( aText ) always returns the
> same(initial) value regardless of the different values assigned to the

font
[snip]

Re:Resizing font size


I am quite puzzled too. TextExtent() does not return a value that changes
with reference to the Font->Height set. I am developing on Win2K. I tried
the opposite, where the font size was increased, instead of decreasing, this
worked ok. The return values from TextExtent() varied depending on the font
size assigned.

Regards
Roshan

Quote
"Ananth B." <ana...@agnisoft.com> wrote in message

news:3e915818$1@newsgroups.borland.com...
Quote
> Roshan,
>     Your problem is strange. I am using the same code snippet here...and
it
> seems to resize the text even for a 5x5 rectangle.
> I am using Tahoma as the font on this canvas.
> All of the TTF's I have on my system seem to give me the correct results.
> FON's like "System" (fixed width fonts) seem to give me problems.
Otherwise,
> I get the proper results.

> I could mail you my executable if you like, so u can see if this works on
ur
> system

> Regards,
> Ananth B.

> "Roshan Jurangpathy" <Ros...@ps-electronic.com> wrote in message
> news:3e915148$1@newsgroups.borland.com...
> > Hi Ananth,

> > Thanks for the code snippet. I converted it to Builder. It does not seem
> to
> > changing the size. aCanvas->TextExtent( aText ) always returns the
> > same(initial) value regardless of the different values assigned to the
> font
> [snip]

Re:Resizing font size


Hi Ananth,

I managed to find the problem. It was in my code. During application
initialization all VCL components sets the font to default MS Sans Serif.
This is not a TTF and the resizing only works for TTFs. So setting the
default font to a TTF fixes everything. ;)

Thank you very much for your help.

Regards
Roshan

Quote
"Ananth B." <ana...@agnisoft.com> wrote in message

news:3e915818$1@newsgroups.borland.com...
Quote
> Roshan,
>     Your problem is strange. I am using the same code snippet here...and
it
> seems to resize the text even for a 5x5 rectangle.
> I am using Tahoma as the font on this canvas.
> All of the TTF's I have on my system seem to give me the correct results.
> FON's like "System" (fixed width fonts) seem to give me problems.
Otherwise,
> I get the proper results.

> I could mail you my executable if you like, so u can see if this works on
ur
> system

> Regards,
> Ananth B.

> "Roshan Jurangpathy" <Ros...@ps-electronic.com> wrote in message
> news:3e915148$1@newsgroups.borland.com...
> > Hi Ananth,

> > Thanks for the code snippet. I converted it to Builder. It does not seem
> to
> > changing the size. aCanvas->TextExtent( aText ) always returns the
> > same(initial) value regardless of the different values assigned to the
> font
> [snip]

Other Threads