• Advertisement
Sign in to follow this  

ID3DXFont::DrawTextA using DT_CALCRECT calculates imprecise values

This topic is 2085 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey GD community,

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,
1, false,
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
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!


Share this post

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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement