From f00fc7ee17153ff70fa4fb2052a5b555af0c054c Mon Sep 17 00:00:00 2001 From: Dmitriy Shilin Date: Tue, 8 Jan 2019 04:20:29 -0800 Subject: tdf#107792 vcl/win: introduce ScopedSelectedHFONT Change-Id: If06ef869fc68976ee3ac6e671edd1bb4f992e716 Reviewed-on: https://gerrit.libreoffice.org/65964 Reviewed-by: Mike Kaganski Tested-by: Jenkins --- vcl/inc/win/scoped_gdi.hxx | 51 ++++++++++++++++++---------------------------- vcl/win/gdi/winlayout.cxx | 22 +++++++++----------- 2 files changed, 30 insertions(+), 43 deletions(-) diff --git a/vcl/inc/win/scoped_gdi.hxx b/vcl/inc/win/scoped_gdi.hxx index ce6eadc8d0af..edbc086ada9c 100644 --- a/vcl/inc/win/scoped_gdi.hxx +++ b/vcl/inc/win/scoped_gdi.hxx @@ -15,53 +15,42 @@ #include -struct HBRUSHDeleter +template struct GDIDeleter { - using pointer = HBRUSH; - void operator()(HBRUSH hBrush) { DeleteBrush(hBrush); } + using pointer = H; + void operator()(H h) { DeleterFunc(h); } }; -struct HRGNDeleter -{ - using pointer = HRGN; - void operator()(HRGN hRgn) { DeleteRegion(hRgn); } -}; +template +using ScopedGDI = std::unique_ptr>; -struct HDCDeleter -{ - using pointer = HDC; - void operator()(HDC hDC) { DeleteDC(hDC); } -}; +using ScopedHBRUSH = ScopedGDI; +using ScopedHRGN = ScopedGDI; +using ScopedHDC = ScopedGDI; +using ScopedHPEN = ScopedGDI; +using ScopedHFONT = ScopedGDI; -struct HPENDeleter -{ - using pointer = HPEN; - void operator()(HPEN hPen) { DeletePen(hPen); } -}; - -using ScopedHBRUSH = std::unique_ptr; -using ScopedHRGN = std::unique_ptr; -using ScopedHDC = std::unique_ptr; -using ScopedHPEN = std::unique_ptr; - -class ScopedSelectedHPEN +template class ScopedSelectedGDI { public: - ScopedSelectedHPEN(HDC hDC, HPEN hPen) + ScopedSelectedGDI(HDC hDC, typename ScopedH::pointer h) : m_hDC(hDC) - , m_hOrigPen(SelectPen(hDC, hPen)) - , m_hSelectedPen(hPen) + , m_hSelectedH(h) + , m_hOrigH(SelectorFunc(hDC, h)) { } - ~ScopedSelectedHPEN() { SelectPen(m_hDC, m_hOrigPen); } + ~ScopedSelectedGDI() { SelectorFunc(m_hDC, m_hOrigH); } private: HDC m_hDC; - HPEN m_hOrigPen; - ScopedHPEN m_hSelectedPen; + ScopedH m_hSelectedH; + typename ScopedH::pointer m_hOrigH; }; +using ScopedSelectedHPEN = ScopedSelectedGDI; +using ScopedSelectedHFONT = ScopedSelectedGDI; + #endif // INCLUDED_VCL_INC_WIN_SCOPED_GDI_HXX /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/vcl/win/gdi/winlayout.cxx b/vcl/win/gdi/winlayout.cxx index ebaf2f1be2d6..120f6bb65e22 100644 --- a/vcl/win/gdi/winlayout.cxx +++ b/vcl/win/gdi/winlayout.cxx @@ -235,7 +235,7 @@ bool ExTextOutRenderer::operator ()(GenericSalLayout const &rLayout, HDC hDC) { HFONT hFont = static_cast(GetCurrentObject( hDC, OBJ_FONT )); - HFONT hAltFont = nullptr; + ScopedHFONT hAltFont; bool bUseAltFont = false; bool bShift = false; if (rLayout.GetFont().GetFontSelectPattern().mbVertical) @@ -246,14 +246,14 @@ bool ExTextOutRenderer::operator ()(GenericSalLayout const &rLayout, { memmove(&aLogFont.lfFaceName[0], &aLogFont.lfFaceName[1], sizeof(aLogFont.lfFaceName)-sizeof(aLogFont.lfFaceName[0])); - hAltFont = CreateFontIndirectW(&aLogFont); + hAltFont.reset(CreateFontIndirectW(&aLogFont)); } else { bShift = true; aLogFont.lfEscapement += 2700; aLogFont.lfOrientation = aLogFont.lfEscapement; - hAltFont = CreateFontIndirectW(&aLogFont); + hAltFont.reset(CreateFontIndirectW(&aLogFont)); } } @@ -267,7 +267,7 @@ bool ExTextOutRenderer::operator ()(GenericSalLayout const &rLayout, if (hAltFont && pGlyph->IsVertical() == bUseAltFont) { bUseAltFont = !bUseAltFont; - SelectFont(hDC, bUseAltFont ? hAltFont : hFont); + SelectFont(hDC, bUseAltFont ? hAltFont.get() : hFont); } if (bShift && pGlyph->IsVertical()) SetTextAlign(hDC, TA_TOP|TA_LEFT); @@ -281,7 +281,6 @@ bool ExTextOutRenderer::operator ()(GenericSalLayout const &rLayout, { if (bUseAltFont) SelectFont(hDC, hFont); - DeleteObject(hAltFont); } return true; @@ -364,14 +363,13 @@ hb_font_t* WinFontInstance::ImplInitHbFont() // Set width to the default to get the original value in the metrics. aLogFont.lfWidth = 0; - // Get the font metrics. - HDC hDC = m_pGraphics->getHDC(); - HFONT hNewFont = CreateFontIndirectW(&aLogFont); - HGDIOBJ hOrigFont = SelectObject(hDC, hNewFont); TEXTMETRICW aFontMetric; - GetTextMetricsW(hDC, &aFontMetric); - SelectObject(hDC, hOrigFont); - DeleteObject(hNewFont); + { + // Get the font metrics. + HDC hDC = m_pGraphics->getHDC(); + ScopedSelectedHFONT(hDC, CreateFontIndirectW(&aLogFont)); + GetTextMetricsW(hDC, &aFontMetric); + } SetAverageWidthFactor(nUPEM / aFontMetric.tmAveCharWidth); } -- cgit