]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/textmeasurecmn.cpp
fixing overrelease and out-of-bounds write, fixes #13725
[wxWidgets.git] / src / common / textmeasurecmn.cpp
index cc8f7489be52976392341f91781928a0d9a5f564..4db3b892fba74583cbe06b494414eb638972a237 100644 (file)
@@ -3,6 +3,7 @@
 // Purpose:     wxTextMeasureBase implementation
 // Author:      Manuel Martin
 // Created:     2012-10-05
+// RCS-ID:      $Id:
 // Copyright:   (c) 1997-2012 wxWidgets team
 // Licence:     wxWindows licence
 ///////////////////////////////////////////////////////////////////////////////
@@ -35,6 +36,10 @@ wxTextMeasureBase::wxTextMeasureBase(const wxDC *dc, const wxFont *theFont)
       m_font(theFont)
 {
     wxASSERT_MSG( dc, wxS("wxTextMeasure needs a valid wxDC") );
+
+    // By default, use wxDC version, we'll explicitly reset this to false in
+    // the derived classes if the DC is of native variety.
+    m_useDCImpl = true;
 }
 
 wxTextMeasureBase::wxTextMeasureBase(const wxWindow *win, const wxFont *theFont)
@@ -43,6 +48,28 @@ wxTextMeasureBase::wxTextMeasureBase(const wxWindow *win, const wxFont *theFont)
       m_font(theFont)
 {
     wxASSERT_MSG( win, wxS("wxTextMeasure needs a valid wxWindow") );
+
+    // We don't have any wxDC so we can't forward to it.
+    m_useDCImpl = false;
+}
+
+wxFont wxTextMeasureBase::GetFont() const
+{
+    return m_font ? *m_font
+                  : m_win ? m_win->GetFont()
+                          : m_dc->GetFont();
+}
+
+void wxTextMeasureBase::CallGetTextExtent(const wxString& string,
+                                          wxCoord *width,
+                                          wxCoord *height,
+                                          wxCoord *descent,
+                                          wxCoord *externalLeading)
+{
+    if ( m_useDCImpl )
+        m_dc->GetTextExtent(string, width, height, descent, externalLeading);
+    else
+        DoGetTextExtent(string, width, height, descent, externalLeading);
 }
 
 void wxTextMeasureBase::GetTextExtent(const wxString& string,
@@ -69,7 +96,7 @@ void wxTextMeasureBase::GetTextExtent(const wxString& string,
 
     MeasuringGuard guard(*this);
 
-    DoGetTextExtent(string, width, height, descent, externalLeading);
+    CallGetTextExtent(string, width, height, descent, externalLeading);
 }
 
 void wxTextMeasureBase::GetMultiLineTextExtent(const wxString& text,
@@ -102,14 +129,14 @@ void wxTextMeasureBase::GetMultiLineTextExtent(const wxString& text,
                 {
                     // but we don't know it yet - choose something reasonable
                     int dummy;
-                    DoGetTextExtent(wxS("W"), &dummy, &heightLineDefault);
+                    CallGetTextExtent(wxS("W"), &dummy, &heightLineDefault);
                 }
 
                 heightTextTotal += heightLineDefault;
             }
             else
             {
-                DoGetTextExtent(curLine, &widthLine, &heightLine);
+                CallGetTextExtent(curLine, &widthLine, &heightLine);
                 if ( widthLine > widthTextMax )
                     widthTextMax = widthLine;
                 heightTextTotal += heightLine;
@@ -138,18 +165,15 @@ void wxTextMeasureBase::GetMultiLineTextExtent(const wxString& text,
         *heightOneLine = heightLine;
 }
 
-void wxTextMeasureBase::GetLargestStringExtent(const wxVector<wxString>& strings,
-                                               wxCoord *width,
-                                               wxCoord *height)
+wxSize wxTextMeasureBase::GetLargestStringExtent(size_t n,
+                                                 const wxString* strings)
 {
     MeasuringGuard guard(*this);
 
     wxCoord w, h, widthMax = 0, heightMax = 0;
-    for ( wxVector<wxString>::const_iterator i = strings.begin();
-          i != strings.end();
-          ++i )
+    for ( size_t i = 0; i < n; ++i )
     {
-        DoGetTextExtent(*i, &w, &h);
+        CallGetTextExtent(strings[i], &w, &h);
 
         if ( w > widthMax )
             widthMax = w;
@@ -157,10 +181,7 @@ void wxTextMeasureBase::GetLargestStringExtent(const wxVector<wxString>& strings
             heightMax = h;
     }
 
-    if ( width )
-        *width = widthMax;
-    if ( height )
-        *height = heightMax;
+    return wxSize(widthMax, heightMax);
 }
 
 bool wxTextMeasureBase::GetPartialTextExtents(const wxString& text,