]> git.saurik.com Git - wxWidgets.git/commitdiff
Use a single global StringFormat in all wxMSW wxGraphicsContext code.
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 9 Sep 2012 00:43:18 +0000 (00:43 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 9 Sep 2012 00:43:18 +0000 (00:43 +0000)
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

index 78f1e4aa0cbe0c6921d8399408f2bb34b9b804d8..eeef95239a3e246314f458da535d10aa0ec73faa 100644 (file)
@@ -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)