From 7a05aa6dc0621bd148feaad6173e4ed257e40d2a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 3 Feb 2019 22:42:55 +0100 Subject: [PATCH 1/3] Don't pass unused arguments to GetTextExtent() in printing sample No real changes, just remove the completely unused local variables and parameters to wxGraphicsContext::GetTextExtent() call. --- samples/printing/printing.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/printing/printing.cpp b/samples/printing/printing.cpp index 682dac26c3..bb6c250427 100644 --- a/samples/printing/printing.cpp +++ b/samples/printing/printing.cpp @@ -243,8 +243,8 @@ void MyApp::Draw(wxDC&dc) gc->DrawText(text, 25.0, 60.0); // draw rectangle around the text - double w, h, d, el; - gc->GetTextExtent(text, &w, &h, &d, &el); + double w, h; + gc->GetTextExtent(text, &w, &h); gc->SetPen( *wxBLACK_PEN ); gc->DrawRectangle(25.0, 60.0, w, h); From 21a7ff2aa31762920b8687057c23251aff3f97cd Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 3 Feb 2019 22:45:13 +0100 Subject: [PATCH 2/3] Use wxScopedPtr instead of explicit delete in printing sample No real changes, just make the code a bit safer. --- samples/printing/printing.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/samples/printing/printing.cpp b/samples/printing/printing.cpp index bb6c250427..0050833cdc 100644 --- a/samples/printing/printing.cpp +++ b/samples/printing/printing.cpp @@ -38,6 +38,7 @@ #if wxUSE_GRAPHICS_CONTEXT #include "wx/graphics.h" + #include "wx/scopedptr.h" #endif #ifdef __WXMAC__ @@ -219,7 +220,7 @@ void MyApp::Draw(wxDC&dc) dc.DrawBitmap( m_bitmap, 10, 10 ); #if wxUSE_GRAPHICS_CONTEXT - wxGraphicsContext *gc = wxGraphicsContext::CreateFromUnknownDC(dc); + wxScopedPtr gc(wxGraphicsContext::CreateFromUnknownDC(dc)); if (gc) { @@ -247,8 +248,6 @@ void MyApp::Draw(wxDC&dc) gc->GetTextExtent(text, &w, &h); gc->SetPen( *wxBLACK_PEN ); gc->DrawRectangle(25.0, 60.0, w, h); - - delete gc; } #endif From c08ea68b4aaa9bcf600f0e8c8e7fc511fc7549a8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 3 Feb 2019 22:46:02 +0100 Subject: [PATCH 3/3] Avoid unnecessary calls in wxGDIPlusContext::GetTextExtent() Don't always call several GDI+ functions when we often don't need their results: only retrieve the cell descent, ascent and the line spacing if we are going to really use any of these values. --- src/msw/graphics.cpp | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/msw/graphics.cpp b/src/msw/graphics.cpp index ebecbeb72a..4d201c7a9f 100644 --- a/src/msw/graphics.cpp +++ b/src/msw/graphics.cpp @@ -2111,33 +2111,40 @@ void wxGDIPlusContext::GetTextExtent( const wxString &str, wxDouble *width, wxDo wxCHECK_RET( !m_font.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") ); wxWCharBuffer s = str.wc_str( *wxConvUI ); - FontFamily ffamily ; Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont(); - f->GetFamily(&ffamily) ; + // Get the font metrics if we actually need them. + if ( descent || externalLeading || (height && str.empty()) ) + { + FontFamily ffamily ; + f->GetFamily(&ffamily) ; - REAL factorY = m_fontScaleRatio; + REAL factorY = m_fontScaleRatio; - // Notice that we must use the real font style or the results would be - // incorrect for italic/bold fonts. - const INT style = f->GetStyle(); - const REAL size = f->GetSize(); - const REAL emHeight = ffamily.GetEmHeight(style); - REAL rDescent = ffamily.GetCellDescent(style) * size / emHeight; - REAL rAscent = ffamily.GetCellAscent(style) * size / emHeight; - REAL rHeight = ffamily.GetLineSpacing(style) * size / emHeight; + // Notice that we must use the real font style or the results would be + // incorrect for italic/bold fonts. + const INT style = f->GetStyle(); + const REAL size = f->GetSize(); + const REAL emHeight = ffamily.GetEmHeight(style); + REAL rDescent = ffamily.GetCellDescent(style) * size / emHeight; + REAL rAscent = ffamily.GetCellAscent(style) * size / emHeight; + REAL rHeight = ffamily.GetLineSpacing(style) * size / emHeight; + + if ( height && str.empty() ) + *height = rHeight * factorY; + if ( descent ) + *descent = rDescent * factorY; + if ( externalLeading ) + *externalLeading = (rHeight - rAscent - rDescent) * factorY; + } - if ( height ) - *height = rHeight * factorY; - if ( descent ) - *descent = rDescent * factorY; - if ( externalLeading ) - *externalLeading = (rHeight - rAscent - rDescent) * factorY; // measuring empty strings is not guaranteed, so do it by hand if ( str.IsEmpty()) { if ( width ) *width = 0 ; + + // Height already assigned above if necessary. } else {