Added wxDC:GetPartialTextExtents

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@25753 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2004-02-11 23:49:43 +00:00
parent 42bf070c7e
commit 0919e93e52
5 changed files with 164 additions and 1 deletions

View 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
// -------------------
@@ -735,6 +741,8 @@ protected:
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

View 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;

View 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
// ----------------------------------------------------------------------------

View 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"));

View 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"));