From b8333dc66d535f8e89c14a1f25b81bb49a0f68a8 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Sat, 10 Aug 2013 16:58:30 +0300 Subject: [PATCH] Gfx: Fix clipping with D2D when a transformation has been applied --- Common/Gfx/CanvasD2D.cpp | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/Common/Gfx/CanvasD2D.cpp b/Common/Gfx/CanvasD2D.cpp index 3b0276bf..235d5b58 100644 --- a/Common/Gfx/CanvasD2D.cpp +++ b/Common/Gfx/CanvasD2D.cpp @@ -315,7 +315,9 @@ void CanvasD2D::DrawTextW(const WCHAR* str, UINT strLen, const TextFormat& forma formatD2D.CreateLayout( str, strLen, rect.Width, rect.Height, !m_AccurateText && m_TextAntiAliasing); - const float xPos = [&]() + D2D1_POINT_2F drawPosition; + drawPosition.y = rect.Y - formatD2D.m_LineGap; + drawPosition.x = [&]() { if (!m_AccurateText) { @@ -330,15 +332,33 @@ void CanvasD2D::DrawTextW(const WCHAR* str, UINT strLen, const TextFormat& forma return rect.X; } (); - // TODO: Check for transformation. - m_Target->PushAxisAlignedClip(ToRectF(rect), D2D1_ANTIALIAS_MODE_ALIASED); + D2D1::Matrix3x2F transformMatrix; + m_Target->GetTransform(&transformMatrix); + const bool identityTransform = transformMatrix.IsIdentity(); - m_Target->DrawTextLayout( - D2D1::Point2F(xPos, rect.Y - formatD2D.m_LineGap), - formatD2D.m_TextLayout.Get(), - solidBrush.Get()); + // TODO: Determine if we can avoid clipping. + D2D1_RECT_F clipRect = ToRectF(rect); + if (identityTransform) + { + m_Target->PushAxisAlignedClip(clipRect, D2D1_ANTIALIAS_MODE_ALIASED); + } + else + { + const D2D1_LAYER_PARAMETERS layerParams = + D2D1::LayerParameters(clipRect, nullptr, D2D1_ANTIALIAS_MODE_ALIASED); + m_Target->PushLayer(layerParams, nullptr); + } - m_Target->PopAxisAlignedClip(); + m_Target->DrawTextLayout(drawPosition, formatD2D.m_TextLayout.Get(), solidBrush.Get()); + + if (identityTransform) + { + m_Target->PopAxisAlignedClip(); + } + else + { + m_Target->PopLayer(); + } } }