[Libreoffice-commits] core.git: vcl/inc vcl/win

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Fri Jan 18 14:21:08 UTC 2019


 vcl/inc/win/scoped_gdi.hxx |   51 +++++++++++++++++----------------------------
 vcl/win/gdi/winlayout.cxx  |   22 ++++++++-----------
 2 files changed, 30 insertions(+), 43 deletions(-)

New commits:
commit f00fc7ee17153ff70fa4fb2052a5b555af0c054c
Author:     Dmitriy Shilin <dshil at fastmail.com>
AuthorDate: Tue Jan 8 04:20:29 2019 -0800
Commit:     Mike Kaganski <mike.kaganski at collabora.com>
CommitDate: Fri Jan 18 15:20:43 2019 +0100

    tdf#107792 vcl/win: introduce ScopedSelectedHFONT
    
    Change-Id: If06ef869fc68976ee3ac6e671edd1bb4f992e716
    Reviewed-on: https://gerrit.libreoffice.org/65964
    Reviewed-by: Mike Kaganski <mike.kaganski at collabora.com>
    Tested-by: Jenkins

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);
     }


More information about the Libreoffice-commits mailing list