Font width
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?
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.
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 LostSourceThat'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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement