Skip to main content
gamedev.net
🔒 Locked

Font width

Started by furiousuk Mar 19, 2007 at 6:07 AM 3 replies 2k views
Original Post
furiousuk
furiousuk
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 ', one way could be to print Welcome to the screen and then the variable 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?
Evil Steve
Evil Steve
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.
LostSource
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.

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.
Evil Steve
Evil Steve
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.
furiousuk
furiousuk
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.