summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--vcl/inc/win/scoped_gdi.hxx51
-rw-r--r--vcl/win/gdi/winlayout.cxx22
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 <memory>
-struct HBRUSHDeleter
+template <typename H, auto DeleterFunc> 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 <typename H, auto DeleterFunc>
+using ScopedGDI = std::unique_ptr<H, GDIDeleter<H, DeleterFunc>>;
-struct HDCDeleter
-{
- using pointer = HDC;
- void operator()(HDC hDC) { DeleteDC(hDC); }
-};
+using ScopedHBRUSH = ScopedGDI<HBRUSH, DeleteBrush>;
+using ScopedHRGN = ScopedGDI<HRGN, DeleteRegion>;
+using ScopedHDC = ScopedGDI<HDC, DeleteDC>;
+using ScopedHPEN = ScopedGDI<HPEN, DeletePen>;
+using ScopedHFONT = ScopedGDI<HFONT, DeleteFont>;
-struct HPENDeleter
-{
- using pointer = HPEN;
- void operator()(HPEN hPen) { DeletePen(hPen); }
-};
-
-using ScopedHBRUSH = std::unique_ptr<HBRUSH, HBRUSHDeleter>;
-using ScopedHRGN = std::unique_ptr<HRGN, HRGNDeleter>;
-using ScopedHDC = std::unique_ptr<HDC, HDCDeleter>;
-using ScopedHPEN = std::unique_ptr<HPEN, HPENDeleter>;
-
-class ScopedSelectedHPEN
+template <typename ScopedH, auto SelectorFunc> 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<ScopedHPEN, SelectPen>;
+using ScopedSelectedHFONT = ScopedSelectedGDI<ScopedHFONT, SelectFont>;
+
#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<HFONT>(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);
}