Font width

Started by
3 comments, last by furiousuk 17 years, 1 month ago
I'm using an ID3DXFont object to display any text to the screen but was wondering if there's a way to calculate the dimensions of the text displayed. For example, suppose I wanted to say 'Welcome <name>', one way could be to print Welcome to the screen and then the variable <name> at the location that Welcome ended (does that make sense?). I'll eventually be using resizable text boxes so will have to know if a word at the end of a line fits into the text box or needs to be placed on a new line. Surely there is a quick a simple call to find out the dimensions of the printed text?
Advertisement
From the docs for ID3DXFont::DrawText():
Quote:Format
[in] Specifies the method of formatting the text. It can be any combination of the following values:

[snip]

DT_CALCRECT
Determines the width and height of the rectangle. If there are multiple lines of text, ID3DXFont::DrawText uses the width of the rectangle pointed to by the pRect parameter and extends the base of the rectangle to bound the last line of text. If there is only one line of text, ID3DXFont::DrawText modifies the right side of the rectangle so that it bounds the last character in the line. In either case, ID3DXFont::DrawText returns the height of the formatted text but does not draw the text.
Since your using ID3DXFont class, you can use the function that is in the ID3DXFont class called "GetDesc(D3DXFONT_DESC* pDesc)". This function will return the current font (font face that is loaded into ID3DXFont class) description.

Example:
D3DXFONT_DESC desc;

your_id3dxfont.GetDesc(&desc);

the character width of the font is now equal to desc.width

now to see how long the text will span across the screen do something like this
(Note: Im using the stl::string class)

text.length() * desc.width

Hope this helps.
Quote:Original post by LostSource
Since your using ID3DXFont class, you can use the function that is in the ID3DXFont class called "GetDesc(D3DXFONT_DESC* pDesc)". This function will return the current font (font face that is loaded into ID3DXFont class) description.
That'll only work if it's a monospaced font, and only if the font doesn't have any overlapping. But for a monospaced font, yes, that would be an option.
Thanx for that, had tried the GetDesc solution and it didnt work as I wasnt using a monospaced font. Didnt see CALCRECT but all works fine now.
Thanks again.

This topic is closed to new replies.

Advertisement