Board index » delphi » 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.

Sam

 

Re:Getting EXACT width of a given String?


Just a guess, but what if you create a Label dynamically and set it to
Autosize. Then set the text and the font, and check the width. If you don't
want to do that, perhaps if you look at the AutoSize code you will see how
Label determines the width and height automatically.

--
Chuck

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

news:3eb9154b@newsgroups.borland.com...
Quote
> 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.

> Sam

Re:Getting EXACT width of a given String?


Quote
> 3) GetTextExtentPoint32
>    -- very close, but not accurate.

This is the one, double-check your parameters, because AFAIK
you won't find more accurate than this one besides drawing
into a bitmap and checking pixel by pixel.

Eric

Re:Getting EXACT width of a given String?


Why not use TCanvas.TextWidth? This function is meant for the purpose and
doesn't create unneccesairy overhead (like the TLabel solution). It should
give an answer that is as exact as possible and probality calls the most
appropriate API function (which may be GetTextExtentPoint32, but even in
this case using the Delphi component is easier).

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

news:3eb9154b@newsgroups.borland.com...
Quote
> 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.

> Sam

Re:Getting EXACT width of a given String?


Faster: Use GetGlyphOutline, flatten the curves, then checkout the poly points :)

Joe
--
Delphi, graphics, and printing specialist available - $35/hr
http://www.code4sale.com/codeit/index.htm

Quote
"Eric Grange" <egra...@glscene.org> wrote in message news:3EB926BF.B591B9D7@glscene.org...
> > 3) GetTextExtentPoint32
> >    -- very close, but not accurate.

> This is the one, double-check your parameters, because AFAIK
> you won't find more accurate than this one besides drawing
> into a bitmap and checking pixel by pixel.

> Eric

Other Threads