Skip to content
Permalink
Browse files

Addendum to c9e2e2a: Include trailing whitespace

  • Loading branch information...
botder committed Jul 30, 2019
1 parent f01dfdd commit 6a39d447a11333030b1d50b9bb5fbc9b6d66a88b
Showing with 31 additions and 2 deletions.
  1. +31 −2 Client/core/Graphics/CGraphics.cpp
@@ -683,10 +683,39 @@ float CGraphics::GetDXTextExtentW(const wchar_t* wszText, float fScale, LPD3DXFO

if (pDXFont)
{
// DT_CALCRECT may not take space characters at the end of a line into consideration for the rect size.
// Count the amount of space characters at the end
size_t spaceCount = 0;
const size_t textLength = wcslen(wszText);

for (int i = textLength - 1; i >= 0; --i)
{
const wchar_t c = wszText[i];

if (c == L' ')
++spaceCount;
else
break;
}

// Compute the size of a single space and use that to get the width of the ignored space characters
size_t trailingSpacePixels = 0;

if (spaceCount > 0)
{
SIZE size = {};
GetTextExtentPoint32W(pDXFont->GetDC(), L" ", 1, &size);
trailingSpacePixels = spaceCount * size.cx;
}

RECT rect = {};
pDXFont->DrawTextW(nullptr, wszText, wcslen(wszText), &rect, DT_CALCRECT | DT_SINGLELINE, D3DCOLOR_XRGB(0, 0, 0));

return static_cast<float>(rect.right - rect.left) * fScale;
if ((textLength - spaceCount) > 0)
{
pDXFont->DrawTextW(nullptr, wszText, textLength - spaceCount, &rect, DT_CALCRECT | DT_SINGLELINE, D3DCOLOR_XRGB(0, 0, 0));
}

return static_cast<float>(rect.right - rect.left + trailingSpacePixels) * fScale;
}

return 0.0f;

0 comments on commit 6a39d44

Please sign in to comment.
You can’t perform that action at this time.