From 792d07c7f2816f1afdbed1d0b8129e3f1aecf8d7 Mon Sep 17 00:00:00 2001 From: Jan-Marek Glogowski Date: Fri, 19 Oct 2018 09:45:54 +0000 Subject: WIN fix CacheGlyphToAtlas resource handling Use :comphelper::ScopeGuard to handle cleanup. And actually restore the original font on the correct DC. Change-Id: Ib59d430636b470486da2f8e5e34ed8d71b57aac2 Reviewed-on: https://gerrit.libreoffice.org/62010 Reviewed-by: Noel Grandin Tested-by: Jenkins Reviewed-by: Jan-Marek Glogowski --- vcl/win/gdi/winlayout.cxx | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/vcl/win/gdi/winlayout.cxx b/vcl/win/gdi/winlayout.cxx index 35303fc5c8a5..efbbd0c1edb3 100644 --- a/vcl/win/gdi/winlayout.cxx +++ b/vcl/win/gdi/winlayout.cxx @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -48,11 +49,11 @@ #include #include -GlobalOpenGLGlyphCache * GlobalOpenGLGlyphCache::get() { - SalData * data = GetSalData(); - if (!data->m_pGlobalOpenGLGlyphCache) { +GlobalOpenGLGlyphCache * GlobalOpenGLGlyphCache::get() +{ + SalData *data = GetSalData(); + if (!data->m_pGlobalOpenGLGlyphCache) data->m_pGlobalOpenGLGlyphCache.reset(new GlobalOpenGLGlyphCache); - } return data->m_pGlobalOpenGLGlyphCache.get(); } @@ -67,12 +68,15 @@ bool WinFontInstance::CacheGlyphToAtlas(HDC hDC, HFONT hFont, int nGlyphIndex, S SAL_WARN("vcl.gdi", "CreateCompatibleDC failed: " << WindowsErrorString(GetLastError())); return false; } - HFONT hOrigFont = static_cast(SelectObject(aHDC.get(), hFont)); + + const HFONT hOrigFont = static_cast(SelectObject(aHDC.get(), hFont)); if (hOrigFont == nullptr) { SAL_WARN("vcl.gdi", "SelectObject failed: " << WindowsErrorString(GetLastError())); return false; } + const ::comphelper::ScopeGuard aHFONTrestoreScopeGuard( + [&aHDC,hOrigFont]() { SelectFont(aHDC.get(), hOrigFont); }); // For now we assume DWrite is present and we won't bother with fallback paths. D2DWriteTextOutRenderer * pTxt = dynamic_cast(&TextOutRenderer::get(true)); @@ -86,6 +90,7 @@ bool WinFontInstance::CacheGlyphToAtlas(HDC hDC, HFONT hFont, int nGlyphIndex, S SAL_WARN("vcl.gdi", "Binding of font failed. The font might not be supported by DirectWrite."); return false; } + const ::comphelper::ScopeGuard aFontReleaseScopeGuard([&pTxt]() { pTxt->ReleaseFont(); }); std::vector aGlyphIndices(1); aGlyphIndices[0] = nGlyphIndex; @@ -158,10 +163,7 @@ bool WinFontInstance::CacheGlyphToAtlas(HDC hDC, HFONT hFont, int nGlyphIndex, S ID2D1SolidColorBrush* pBrush = nullptr; if (!SUCCEEDED(pRT->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &pBrush))) - { - pTxt->ReleaseFont(); return false; - } D2D1_POINT_2F baseline = { static_cast(aElement.getExtraOffset()), @@ -194,12 +196,9 @@ bool WinFontInstance::CacheGlyphToAtlas(HDC hDC, HFONT hFont, int nGlyphIndex, S break; default: SAL_WARN("vcl.gdi", "DrawGlyphRun-EndDraw failed: " << WindowsErrorString(GetLastError())); - SelectFont(aDC.getCompatibleHDC(), hOrigFont); return false; } - pTxt->ReleaseFont(); - if (!OpenGLGlyphCache::ReserveTextureSpace(aElement, nBitmapWidth, nBitmapHeight)) return false; if (!aDC.copyToTexture(aElement.maTexture)) @@ -207,8 +206,6 @@ bool WinFontInstance::CacheGlyphToAtlas(HDC hDC, HFONT hFont, int nGlyphIndex, S maOpenGLGlyphCache.PutDrawElementInCache(aElement, nGlyphIndex); - SelectFont(aDC.getCompatibleHDC(), hOrigFont); - return true; } -- cgit