Getting EXACT width of a given String?

Getting EXACT width of a given String?

Post by Sam » Thu, 08 May 2003 23:20:49



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

 
 
 

Getting EXACT width of a given String?

Post by Chuc » Thu, 08 May 2003 23:35:35


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


 
 
 

Getting EXACT width of a given String?

Post by Eric Grang » Fri, 09 May 2003 00:31:11


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

 
 
 

Getting EXACT width of a given String?

Post by Avatar Zonderta » Fri, 09 May 2003 03:48:44


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

 
 
 

Getting EXACT width of a given String?

Post by Joe C. Hech » Sat, 10 May 2003 04:59:39


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


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