2
votes

I'm trying to draw a string to the screen using different fonts with different lengths.

wxScreenDC dc;
dc.SetFont(f.font);
f.height=dc.GetCharHeight();
f.width=dc.GetCharWidth();

This is the code I'm using, the height is always correct and drawn on the screen correctly.. However the width is some fonts drawn correctly in all font sizes (eg. Fixedsys ,Courier New) but in some fonts doesn't drawn correctly (calculated less then the actual display width) (eg. Arial)

I checked the docs and saw that

wxCoord wxDC::GetCharWidth () const

Gets the average character width of the currently set font.

"Gets the average character width.."

Can't i get the actual width of the font character?

If not, how can I calculate correctly the display width of the string?

Thanks.

1

1 Answers

4
votes

Instead of getting the width of a font character, use the DC to measure the width and height of a given single-line string using wxDC::GetTextExtent

wxScreenDC dc;
dc.SetFont(f.font);
wxString tempString('M', length);
wxCoord width, height;
dc.GetTextExtent(tempString, &width, &height);


Update after comment:

By repeating the letter 'M' by the string length you know, you get pretty much the widest possible string you should cater for. You have little option because for a variable width font like Arial it's not possible to get one single value - each char is a different width, so cater for the worst possible scenario.