Sign in to follow this  
furiousuk

Font width

Recommended Posts

furiousuk    162
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?

Share this post


Link to post
Share on other sites
Evil Steve    2017
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.

Share this post


Link to post
Share on other sites
LostSource    199
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.

Share this post


Link to post
Share on other sites
Evil Steve    2017
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.

Share this post


Link to post
Share on other sites
furiousuk    162
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this