From 122b3d0df35bba6bff337ea37a80412907da85d6 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 9 Sep 2012 00:43:18 +0000 Subject: [PATCH] Use a single global StringFormat in all wxMSW wxGraphicsContext code. Instead of creating StringFormat in DrawText() and Get[Partial]TextExtent[s], create it once and simply use it from both functions. This might be slightly more efficient as we don't waste time recreating it but the main advantage is that it ensures that these functions use the same string format and no discrepancies between them are possible. See #14537. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@72441 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/msw/graphics.cpp | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/src/msw/graphics.cpp b/src/msw/graphics.cpp index 78f1e4aa0c..eeef95239a 100644 --- a/src/msw/graphics.cpp +++ b/src/msw/graphics.cpp @@ -78,6 +78,31 @@ inline Color wxColourToColor(const wxColour& col) return Color(col.Alpha(), col.Red(), col.Green(), col.Blue()); } +// Do not use this pointer directly, it's only used by +// GetDrawTextStringFormat() and the cleanup code in wxGDIPlusRendererModule. +StringFormat* gs_drawTextStringFormat = NULL; + +// Get the string format used for the text drawing and measuring functions: +// notice that it must be the same one for all of them, otherwise the drawn +// text might be of different size than what measuring it returned. +inline StringFormat* GetDrawTextStringFormat() +{ + if ( !gs_drawTextStringFormat ) + { + gs_drawTextStringFormat = new StringFormat(StringFormat::GenericTypographic()); + + // This doesn't make any difference for DrawText() actually but we want + // this behaviour when measuring text. + gs_drawTextStringFormat->SetFormatFlags + ( + gs_drawTextStringFormat->GetFormatFlags() + | StringFormatFlagsMeasureTrailingSpaces + ); + } + + return gs_drawTextStringFormat; +} + } // anonymous namespace //----------------------------------------------------------------------------- @@ -1752,7 +1777,7 @@ void wxGDIPlusContext::DoDrawText(const wxString& str, -1, // length: string is NUL-terminated fontData->GetGDIPlusFont(), PointF(x, y), - StringFormat::GenericTypographic(), + GetDrawTextStringFormat(), fontData->GetGDIPlusBrush() ); } @@ -1794,11 +1819,9 @@ void wxGDIPlusContext::GetTextExtent( const wxString &str, wxDouble *width, wxDo else { RectF layoutRect(0,0, 100000.0f, 100000.0f); - StringFormat strFormat( StringFormat::GenericTypographic() ); - strFormat.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces | strFormat.GetFormatFlags() ); RectF bounds ; - m_context->MeasureString((const wchar_t *) s , wcslen(s) , f, layoutRect, &strFormat, &bounds ) ; + m_context->MeasureString((const wchar_t *) s , wcslen(s) , f, layoutRect, GetDrawTextStringFormat(), &bounds ) ; if ( width ) *width = bounds.Width; if ( height ) @@ -1822,7 +1845,7 @@ void wxGDIPlusContext::GetPartialTextExtents(const wxString& text, wxArrayDouble wxASSERT_MSG(text.length() == len , wxT("GetPartialTextExtents not yet implemented for multichar situations")); RectF layoutRect(0,0, 100000.0f, 100000.0f); - StringFormat strFormat( StringFormat::GenericTypographic() ); + StringFormat strFormat( GetDrawTextStringFormat() ); size_t startPosition = 0; size_t remainder = len; @@ -1840,7 +1863,6 @@ void wxGDIPlusContext::GetPartialTextExtents(const wxString& text, wxArrayDouble ranges[i].Length = startPosition+i+1 ; } strFormat.SetMeasurableCharacterRanges(span,ranges); - strFormat.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces | strFormat.GetFormatFlags() ); m_context->MeasureCharacterRanges(ws, -1 , f,layoutRect, &strFormat,span,regions) ; RectF bbox ; @@ -2255,7 +2277,12 @@ class wxGDIPlusRendererModule : public wxModule { public: virtual bool OnInit() { return true; } - virtual void OnExit() { gs_GDIPlusRenderer.Unload(); } + virtual void OnExit() + { + wxDELETE(gs_drawTextStringFormat); + + gs_GDIPlusRenderer.Unload(); + } private: DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule) -- 2.45.2