Jump to content

  • Log In with Google      Sign In   
  • Create Account


ID3DXFont::DrawTextA using DT_CALCRECT calculates imprecise values


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 athreoz   Members   -  Reputation: 111

Like
0Likes
Like

Posted 02 June 2012 - 06:44 AM

Hey GD community,

I'm experiencing problems when calculating the on-screen width of a string using the DrawTextA method of the ID3DXFont DirectX9 interface with the DT_CALCRECT parameter. Whenever I use a different font than Arial, the values returned in the RECT structure become imprecise what makes it impossible for me to correctly draw a caret in my edit-box implementation (when not using Arial as font).
I already tried to solve this problem by calculating the width using the GetTextExtentPoint32A GDI API or even by calling GetCharABCWidthsFloatA and summing up A, B and C for every single character (when initialzing the font) and then manually adding the widths of the single characters together for the whole string - without success. None of the calculated values are precise.
The code I'm using to load the font:

const std::string &rFontName = std::string("Calibri");
unsigned height	             = 16;
// ...
ID3DXFont *pD3DFont = nullptr;
if (FAILED(D3DXCreateFontA(m_pDevice,
			   height, 0,
			   FW_NORMAL,
			   1, false,
			   DEFAULT_CHARSET,
			   OUT_DEFAULT_PRECIS,
			   PROOF_QUALITY,
			   DEFAULT_PITCH | FF_DONTCARE,
			   rFontName.c_str(),
			   &pD3DFont)))
{
	throw NXGFWError("Could not initialize font");
}

And the method I use to determine the on-screen width:

uint D3D9RenderEngine::calcTextWidth(const std::string &rText,
				     uint fontHeight,
			             const std::string &rFontName)
{
   // Grab font
   const sFont &rFont = _retriveFont(rFontName, fontHeight);
   // Calculate
   SIZE s;
   GetTextExtentPoint32A(rFont.pFont->GetDC(),
			 rText.c_str(),
			 rText.length(),
			 &s);
   return (uint)s.cx;
} // ==> calcTextWidth

My question is if this is a bug in the ID3DXFont interface or if I'm doing something wrong - I googled the whole day without finding a suitable solution. You guys are my last hope! :)

Thanks in advance!

Greetings,
athreoz

Sponsor:

#2 athreoz   Members   -  Reputation: 111

Like
0Likes
Like

Posted 11 June 2012 - 07:06 AM

Anyone? I guess I'll have to write my own font class in case noone is capable of providing an answer. :/




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS