]> git.saurik.com Git - wxWidgets.git/commitdiff
Added wxDC:GetPartialTextExtents
authorRobin Dunn <robin@alldunn.com>
Wed, 11 Feb 2004 23:49:43 +0000 (23:49 +0000)
committerRobin Dunn <robin@alldunn.com>
Wed, 11 Feb 2004 23:49:43 +0000 (23:49 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@25753 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/dc.h
include/wx/mac/dc.h
src/common/dcbase.cpp
src/mac/carbon/dc.cpp
src/mac/dc.cpp

index cb7d80bd14872fa9ea2d50d56efbaec60bcb8dcd..a01bb775ca84f5f458a92a068a9e95da7024382f 100644 (file)
@@ -29,6 +29,7 @@
 #include "wx/pen.h"
 #include "wx/palette.h"
 #include "wx/list.h"            // we use wxList in inline functions
+#include "wx/dynarray.h"
 
 class WXDLLEXPORT wxDC;
 class WXDLLEXPORT wxDCBase;
@@ -427,6 +428,11 @@ public:
                                         wxCoord *heightLine = NULL,
                                         wxFont *font = NULL);
 
+    // Measure cumulative width of text after each character
+    bool GetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
+        { return DoGetPartialTextExtents(text, widths); }
+
+    
     // size and resolution
     // -------------------
 
@@ -734,7 +740,9 @@ protected:
                                  wxCoord *descent = NULL,
                                  wxCoord *externalLeading = NULL,
                                  wxFont *theFont = NULL) const = 0;
-
+    
+    virtual bool DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const;
+    
 #if wxUSE_SPLINES
     virtual void DoDrawSpline(wxList *points);
 #endif
index 6411ccaf560d13dd077d3afb036121452e3f7393..5fc2601e55765eee0cc35bc86b55f2ca2ed47b9f 100644 (file)
@@ -86,6 +86,7 @@ class WXDLLEXPORT wxDC: public wxDCBase
                                  wxCoord *descent = NULL,
                                  wxCoord *externalLeading = NULL,
                                  wxFont *theFont = NULL) const;
+    virtual bool DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const;
 
     virtual bool CanDrawBitmap() const;
     virtual bool CanGetTextExtent() const;
index 75b4c6ac7968aeceb0df3d0e46e0f550bdb1739c..7f1b5170ec5ae8db0b75218d44e14f05e81ed330 100644 (file)
@@ -365,6 +365,36 @@ void wxDCBase::DoDrawSpline( wxList *points )
 
 #endif // wxUSE_SPLINES
 
+// ----------------------------------------------------------------------------
+// Partial Text Extents
+// ----------------------------------------------------------------------------
+
+
+// Each element of the array will be the width of the string up to and
+// including the coresoponding character in text.  This is the generic
+// implementation, the port-specific classes should do this with native APIs
+// if available.
+
+bool wxDCBase::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
+{
+    int totalWidth = 0;
+    size_t i;
+
+    widths.Empty();
+    widths.Add(0, text.Length());
+    
+    // Calculate the position of each character based on the widths of
+    // the previous characters
+    for (i=0; i<text.Length(); i++) {
+        int w, h;
+        GetTextExtent(text[i], &w, &h);
+        totalWidth += w;
+        widths[i] = totalWidth;
+    }
+    return true;
+}
+
+
 // ----------------------------------------------------------------------------
 // enhanced text drawing
 // ----------------------------------------------------------------------------
index 7b95aef88a1d2587d0397555a2e07657f5d0b964..bfd36c8421f7a034fa538bf5c30d6db69a139f44 100644 (file)
@@ -1668,6 +1668,68 @@ void  wxDC::DoGetTextExtent( const wxString &strtext, wxCoord *width, wxCoord *h
     }
 }
 
+
+bool wxDC::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
+{
+    wxCHECK_MSG(Ok(), false, wxT("Invalid DC"));
+
+    widths.Empty();
+    widths.Add(0, text.Length());
+
+    if (text.Length() == 0)
+        return false;
+    
+    wxMacFastPortSetter helper(this) ;
+    MacInstallFont() ;
+#if TARGET_CARBON    
+    bool useGetThemeText = ( GetThemeTextDimensions != (void*) kUnresolvedCFragSymbolAddress ) ;
+    if ( UMAGetSystemVersion() < 0x1000 || IsKindOf(CLASSINFO( wxPrinterDC ) ) || ((wxFont*)&m_font)->GetNoAntiAliasing() )
+        useGetThemeText = false ;
+
+    if ( useGetThemeText )
+    {
+        // If anybody knows how to do this more efficiently yet still handle
+        // the fractional glyph widths that may be present when using AA
+        // fonts, please change it.  Currently it is measuring from the
+        // begining of the string for each succeding substring, which is much
+        // slower than this should be.
+        for (size_t i=0; i<text.Length(); i++)
+        {
+            wxString str(text.Left(i+1));
+            Point bounds = {0,0};
+            SInt16 baseline ;
+            wxMacCFStringHolder mString(str, m_font.GetEncoding());
+            ::GetThemeTextDimensions( mString,
+                                      kThemeCurrentPortFont,
+                                      kThemeStateActive,
+                                      false,
+                                      &bounds,
+                                      &baseline );
+            widths[i] = XDEV2LOGREL(bounds.h);
+        }
+    }
+    else        
+#endif
+    {
+        wxCharBuffer buff = text.mb_str(wxConvLocal);
+        size_t len = strlen(buff);
+        short* measurements = new short[len+1];
+        MeasureText(len, buff.data(), measurements);
+
+        // Copy to widths, starting at measurements[1]
+        // NOTE: this doesn't take into account any multi-byte characters
+        // in buff, it probabkly should...
+        for (size_t i=0; i<text.Length(); i++)
+            widths[i] = XDEV2LOGREL(measurements[i+1]);
+
+        delete [] measurements;        
+    }
+
+    return true;
+}
+
+
+
 wxCoord   wxDC::GetCharWidth(void) const
 {
     wxCHECK_MSG(Ok(), 1, wxT("Invalid DC"));
index 7b95aef88a1d2587d0397555a2e07657f5d0b964..bfd36c8421f7a034fa538bf5c30d6db69a139f44 100644 (file)
@@ -1668,6 +1668,68 @@ void  wxDC::DoGetTextExtent( const wxString &strtext, wxCoord *width, wxCoord *h
     }
 }
 
+
+bool wxDC::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
+{
+    wxCHECK_MSG(Ok(), false, wxT("Invalid DC"));
+
+    widths.Empty();
+    widths.Add(0, text.Length());
+
+    if (text.Length() == 0)
+        return false;
+    
+    wxMacFastPortSetter helper(this) ;
+    MacInstallFont() ;
+#if TARGET_CARBON    
+    bool useGetThemeText = ( GetThemeTextDimensions != (void*) kUnresolvedCFragSymbolAddress ) ;
+    if ( UMAGetSystemVersion() < 0x1000 || IsKindOf(CLASSINFO( wxPrinterDC ) ) || ((wxFont*)&m_font)->GetNoAntiAliasing() )
+        useGetThemeText = false ;
+
+    if ( useGetThemeText )
+    {
+        // If anybody knows how to do this more efficiently yet still handle
+        // the fractional glyph widths that may be present when using AA
+        // fonts, please change it.  Currently it is measuring from the
+        // begining of the string for each succeding substring, which is much
+        // slower than this should be.
+        for (size_t i=0; i<text.Length(); i++)
+        {
+            wxString str(text.Left(i+1));
+            Point bounds = {0,0};
+            SInt16 baseline ;
+            wxMacCFStringHolder mString(str, m_font.GetEncoding());
+            ::GetThemeTextDimensions( mString,
+                                      kThemeCurrentPortFont,
+                                      kThemeStateActive,
+                                      false,
+                                      &bounds,
+                                      &baseline );
+            widths[i] = XDEV2LOGREL(bounds.h);
+        }
+    }
+    else        
+#endif
+    {
+        wxCharBuffer buff = text.mb_str(wxConvLocal);
+        size_t len = strlen(buff);
+        short* measurements = new short[len+1];
+        MeasureText(len, buff.data(), measurements);
+
+        // Copy to widths, starting at measurements[1]
+        // NOTE: this doesn't take into account any multi-byte characters
+        // in buff, it probabkly should...
+        for (size_t i=0; i<text.Length(); i++)
+            widths[i] = XDEV2LOGREL(measurements[i+1]);
+
+        delete [] measurements;        
+    }
+
+    return true;
+}
+
+
+
 wxCoord   wxDC::GetCharWidth(void) const
 {
     wxCHECK_MSG(Ok(), 1, wxT("Invalid DC"));