Board index » delphi » Getting EXACT width of a given String?

Getting EXACT width of a given String?

Getting EXACT width of a given String?

Languages: C, C++, Java or Delphi (with/without WinAPIs)

Question: I need a function (inbuilt or custom) which gives the exact width
of a given string and given Font.

Like: int GetStringWidth(char *str, CFont font)

Already tried:
1) GetTextMetrics to get average width * strlen(str)
   -- does not work for non-fixed-width-fonts, for long strings
2) GetTextABCWidths
3) GetTextExtentPoint32
   -- very close, but not accurate.

Note: Could not get how to use GetKerningPairs to find the string width.
Some code would be very useful.

Thank You for all your help, in advance.

Vikram.

 

Re:Getting EXACT width of a given String?


Quote
"Sam" <anish...@gnostice.com> wrote in message

news:3eb9133d$1@newsgroups.borland.com...

Quote
> Getting EXACT width of a given String?

NOTE: This should have been posted in .graphics.

TCanvas.TextWidth

Re:Getting EXACT width of a given String?


Sorry that I posted it here.
But TCanvas.TextWidth gives the same value as GetTextExtentPoint32 which is
less than the actual width. I tried to draw a line to match the width, but
was unsuccessful :-(

Thank You,

S. Vikram

Quote
"Ignacio Vazquez" <ivazquezATorioncommunications.com> wrote in message

news:3eb91405$1@newsgroups.borland.com...

Quote
> "Sam" <anish...@gnostice.com> wrote in message
> news:3eb9133d$1@newsgroups.borland.com...
> > Getting EXACT width of a given String?

> NOTE: This should have been posted in .graphics.

> TCanvas.TextWidth

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.478 / Virus Database: 275 - Release Date: 5/6/2003

Re:Getting EXACT width of a given String?


take a tLabel. Set its AutoSize property to True.  Assign the string to its
caption.  Get the width of the caption.  A kludge, but may satisfy...

(Then go look at the tCaption source to see how they did it...)

Re:Getting EXACT width of a given String?


You're doing something terribly wrong, because any of these is working
perfectly for me with any font.
You didn't forget to select the actual font into device context, did you?

BTW, without win api you don't have these functions, besides that it does
not matter which language you use, it's practically the same. Except that
you have CFont class only in MFC. I suggest you to stick with winapi
constructs (TLogFont)
And, there's no GetTextABCWidths, it's GetCharABCWidths, which gives you
widths of chars, not a text string.

In Delphi use:
  Canvas.Font := <some font>
  Result := Canvas.TextWidth(text);

--
Robert Cerny
http://codecentral.borland.com/codecentral/ccWeb.exe/author?authorid=...

Quote
"Sam" <anish...@gnostice.com> wrote in message

news:3eb9133d$1@newsgroups.borland.com...
Quote
> Getting EXACT width of a given String?

> Languages: C, C++, Java or Delphi (with/without WinAPIs)

> Question: I need a function (inbuilt or custom) which gives the exact
width
> of a given string and given Font.

> Like: int GetStringWidth(char *str, CFont font)

> Already tried:
> 1) GetTextMetrics to get average width * strlen(str)
>    -- does not work for non-fixed-width-fonts, for long strings
> 2) GetTextABCWidths
> 3) GetTextExtentPoint32
>    -- very close, but not accurate.

> Note: Could not get how to use GetKerningPairs to find the string width.
> Some code would be very useful.

> Thank You for all your help, in advance.

> Vikram.

Other Threads