   [DirectX] DrawText() with DT_CALCRECT problem..
  I'm calling DrawText()and using DT_CALCRECT to get the RECT of text,This is my code:POINT CGameFont::GetTextSize(int fontId, const char* pText){RECT rect = {0, 0, 0,0};POINT pt = {0, 0};if (!pText)return pt;for (int i=0; i<m_FontCount; i++){if (m_vecFontInfo.m_FontID == fontId){m_pD3DXFont->DrawText(NULL, pText, -1, &rect, DT_CALCRECT, 0xffffffff);pt.x = rect.right-rect.left;pt.y = rect.bottom-rect.top;return pt;}}return pt;}I find：1: if pText==" ", then the width returned is the size of one space.2: if pText=="    "(four spaces), the width returned is also the size of one space.3: if pText=="abc "(end has a space), the width returned is the size of "abc" instead of "abc "4: if pText=="abc  def", the width is normal. what happend??
  Try DT_CALCRECT | DT_SINGLELINE. Also experiment with DT_EXTERNALLEADING.Also, it seems what you're trying to do is draw separate strings next to each other. Tinkering with text placement too much is rarely a good idea. Instead, I suggest you just concatenate strings when possible and draw one long string instead of trying to draw many strings next to each other. It's even probably faster this way. Also, if you ever translate your game/application, you don't want to find out your code for text placement doesn't work for Japanese or something.   ;Quote:Original post by Amr0Try DT_CALCRECT | DT_SINGLELINE. Also experiment with DT_EXTERNALLEADING.Also, it seems what you're trying to do is draw separate strings next to each other. Tinkering with text placement too much is rarely a good idea. Instead, I suggest you just concatenate strings when possible and draw one long string instead of trying to draw many strings next to each other. It's even probably faster this way. Also, if you ever translate your game/application, you don't want to find out your code for text placement doesn't work for Japanese or something.neither DT_CALCRECT | DT_SINGLELINE nor DT_EXTERNALLEADING can work.in addition, i'm writing the Edit control, i'd like make the cursor follow the strings,so, i must calculate string's pixel size.you mean I should use some different formes to solve this question?   ;Quote:in addition, i'm writing the Edit control, i'd like make the cursor follow the strings,Good luck with that! I don't want to put you down, but look at the code for the edit control in DXUT... it's not trivial to say the least. Now that I mention it, why don't you use that instead of making your own?Well anyway, if you only care about English, then you can test the last character and if it's a space, replace it with, say, a '_' character to remove the "trailing whitespaces" problem.Or you can use something other than D3DXFont. For example, you can use the "Bitmap Font Generator" from AngelCode (see here).Good luck.   ;Quote:Original post by Amr0Quote:in addition, i'm writing the Edit control, i'd like make the cursor follow the strings,Good luck with that! I don't want to put you down, but look at the code for the edit control in DXUT... it's not trivial to say the least. Now that I mention it, why don't you use that instead of making your own?Well anyway, if you only care about English, then you can test the last character and if it's a space, replace it with, say, a '_' character to remove the "trailing whitespaces" problem.Or you can use something other than D3DXFont. For example, you can use the "Bitmap Font Generator" from AngelCode (see here).Good luck.thank youi ready to add a char at the end of string that has space, use this to solve this problem, i think it shouldn't affect capability.   ;  I use this way to solve it:<br>POINT CGameFont::GetTextSize(int fontId, const char* pText)<br>{<br>RECT rect = {0, 0, 0,0};<br>POINT pt = {0, 0};<br>if (!pText)return pt;<br><br>for (int i=0; i&lt;m_FontCount; i++)<br>{<br>if (m_vecFontInfo<span style="font-weight:bold;">.m_FontID == fontId)<br>{<br>m_pD3DXFont<span style="font-weight:bold;">-&gt;DrawText(NULL, pText, -1, &rect, DT_CALCRECT, 0xffffffff);<br>pt.x = rect.right-rect.left;<br>pt.y = rect.bottom-rect.top;<br><br>int strLen = strlen(pText);<br>// do with space char<br>if (pText[strLen-1] == ' ')<br>{<br>string str = pText;<br>RECT rectSpace = {0, 0, 0, 0};<br>int width = 0;<br>str += '0';<br>m_pD3DXFont<span style="font-weight:bold;">-&gt;DrawText(NULL, str.c_str(), -1, &rectSpace, DT_CALCRECT, 0xffffffff);<br>width = rectSpace.right-rectSpace.left;<br><br>str += '0';<br>m_pD3DXFont<span style="font-weight:bold;">-&gt;DrawText(NULL, str.c_str(), -1, &rectSpace, DT_CALCRECT, 0xffffffff);<br>pt.x = width - (rectSpace.right-rectSpace.left-width);<br>}<br><br>return pt;<br>}<br>}<br><br>return pt;<br>}<br></code><br><br>Though it can work, but for it I had to call DrawText() twice..;  a little perfect:POINT CGameFont::GetTextSize(int fontId, const char* pText){RECT rect = {0, 0, 0,0};POINT pt = {0, 0};if (!pText)return pt;for (int i=0; i<m_FontCount; i++){if (m_vecFontInfo.m_FontID == fontId){int strLen = strlen(pText);// do with space charif (pText[strLen-1] == ' '){string strSingle;int numSpace = 0;for (int index=strLen-1; index>-1; index--){if (pText[index] == ' '){numSpace++;}else{strSingle = string(pText, pText+index+1);break;}}m_pD3DXFont->DrawText(NULL, strSingle.c_str(), -1, &rect, DT_CALCRECT, 0xffffffff);pt.x = (rect.right-rect.left) + (numSpace*m_vecFontInfo.m_bCharWidth);pt.y = rect.bottom-rect.top;}else{m_pD3DXFont->DrawText(NULL, pText, -1, &rect, DT_CALCRECT, 0xffffffff);pt.x = rect.right-rect.left;pt.y = rect.bottom-rect.top;}return pt;}}return pt;}in the font struct I add a member "m_bCharWidth", and when create the font object, I calculate the RECT of "0" by calling DrawText() for "m_bCharWidth".;  Try this:RECT r = {0, 0, 0, 0};int len = strlen( pText );if( len == 0 ) return 0;char lastChar = pText[ len-1 ];if( lastChar == ' ' )    pText[ len-1 ] = '_';pFont->DrawText( NULL, pText, -1, &r, DT_CACLRECT, 0xffffffff );pText[len-1] = lastChar; // Restore last character.return r.right - r.left;Also, check the forum faq to learn how to post code properly.   ;Quote:Original post by Amr0Try this:*** Source Snippet Removed ***Also, check the forum faq to learn how to post code properly.good idea, thank you..yes, the size of the '_' is the same as the size of ' '...[Edited by - xychzh on April 19, 2010 11:00:23 AM]