From 9d525b1c1a4d8f1811abe9c24ce49a1b2b0e30d1 Mon Sep 17 00:00:00 2001 From: Brian Ferguson Date: Fri, 2 Aug 2013 14:32:07 -0600 Subject: [PATCH] D2D: More tweaks (partially reverts ccb2731) Should account for extra padding when #CRLF# is used. --- Common/Gfx/CanvasD2D.cpp | 5 +++-- Common/Gfx/Util/DWriteHelpers.cpp | 10 ++++++++-- Common/Gfx/Util/DWriteHelpers.h | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Common/Gfx/CanvasD2D.cpp b/Common/Gfx/CanvasD2D.cpp index 6b22ed1d..1b067375 100644 --- a/Common/Gfx/CanvasD2D.cpp +++ b/Common/Gfx/CanvasD2D.cpp @@ -350,8 +350,9 @@ bool CanvasD2D::MeasureTextW(const WCHAR* str, UINT strLen, const TextFormat& fo textLayout.GetAddressOf()); if (SUCCEEDED(hr)) { + std::wstring s = str; const DWRITE_TEXT_METRICS metrics = - Util::GetAdjustedDWriteTextLayoutMetrics(textLayout.Get(), !m_AccurateText); + Util::GetAdjustedDWriteTextLayoutMetrics(textLayout.Get(), !m_AccurateText, s.rfind(L"\n") != std::wstring::npos); rect.Width = metrics.width; rect.Height = metrics.height; return true; @@ -375,7 +376,7 @@ bool CanvasD2D::MeasureTextLinesW(const WCHAR* str, UINT strLen, const TextForma if (SUCCEEDED(hr)) { const DWRITE_TEXT_METRICS metrics = - Util::GetAdjustedDWriteTextLayoutMetrics(textLayout.Get(), !m_AccurateText); + Util::GetAdjustedDWriteTextLayoutMetrics(textLayout.Get(), !m_AccurateText, false); rect.Width = metrics.width; lines = metrics.lineCount; diff --git a/Common/Gfx/Util/DWriteHelpers.cpp b/Common/Gfx/Util/DWriteHelpers.cpp index 5320b6f2..af17e829 100644 --- a/Common/Gfx/Util/DWriteHelpers.cpp +++ b/Common/Gfx/Util/DWriteHelpers.cpp @@ -24,7 +24,7 @@ namespace Gfx { namespace Util { DWRITE_TEXT_METRICS GetAdjustedDWriteTextLayoutMetrics( - IDWriteTextLayout* textLayout, bool gdiEmulation) + IDWriteTextLayout* textLayout, bool gdiEmulation, bool containsNewLine) { DWRITE_TEXT_METRICS metrics; textLayout->GetMetrics(&metrics); @@ -34,7 +34,13 @@ DWRITE_TEXT_METRICS GetAdjustedDWriteTextLayoutMetrics( float size = 0.0f; textLayout->GetFontSize(0, &size); metrics.width = floor(metrics.width + ((size / 6.0f) * 4.0f) + ((size * 3.0f / 4.0f) / 17.5f) + 0.255f); - metrics.height = floor(metrics.height * 1.087f); + + if (containsNewLine) + { + metrics.width -= ((size * 3.0f / 4.0f) / 4.0f); + } + + metrics.height = floor(metrics.height + (size / 9.25f) + 0.3f); } return metrics; diff --git a/Common/Gfx/Util/DWriteHelpers.h b/Common/Gfx/Util/DWriteHelpers.h index 278f0b36..c7ef3f0d 100644 --- a/Common/Gfx/Util/DWriteHelpers.h +++ b/Common/Gfx/Util/DWriteHelpers.h @@ -27,7 +27,7 @@ namespace Util { // If |gdiEmulation| is true, the returns metrics have similar characteristics to those provided // by GDI+. DWRITE_TEXT_METRICS GetAdjustedDWriteTextLayoutMetrics( - IDWriteTextLayout* textLayout, bool gdiEmulation); + IDWriteTextLayout* textLayout, bool gdiEmulation, bool containsNewLine); // Maps the GDI family name and italic/bold flags to the DirectWrite family name, weight, style, // and stretch.