I'm experiencing problems when calculating the on-screen width of a string using the [font=courier new,courier,monospace]DrawTextA[/font] method of the [font=courier new,courier,monospace]ID3DXFont[/font] DirectX9 interface with the [font=courier new,courier,monospace]DT_CALCRECT[/font] parameter. Whenever I use a different font than Arial, the values returned in the [font=courier new,courier,monospace]RECT[/font] 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 [font=courier new,courier,monospace]GetTextExtentPoint32A[/font] GDI API or even by calling [font=courier new,courier,monospace]GetCharABCWidthsFloatA[/font] 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 [font=courier new,courier,monospace]ID3DXFont[/font] 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