From 174ee1b3d307340b029cd4e84124484c3ba51241 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Mon, 26 Apr 2004 23:33:55 +0000 Subject: [PATCH 1/1] Patch [ 942051 ] generic wxDCBase::DoGetPartialTextExtents speed improvement git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@26979 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/dcbase.cpp | 63 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/src/common/dcbase.cpp b/src/common/dcbase.cpp index 88612bfdb1..d7ffb61948 100644 --- a/src/common/dcbase.cpp +++ b/src/common/dcbase.cpp @@ -370,27 +370,76 @@ void wxDCBase::DoDrawSpline( wxList *points ) // ---------------------------------------------------------------------------- -// Each element of the array will be the width of the string up to and +// Each element of the widths 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. +// if available and if faster. Note: pango_layout_index_to_pos is much slower +// than calling GetTextExtent!! + +#define FWC_SIZE 128 + +class FontWidthCache +{ +public: + FontWidthCache() : m_scaleX(1), m_widths(NULL) { } + ~FontWidthCache() { delete []m_widths; } + + void Reset() + { + if (!m_widths) + m_widths = new int[FWC_SIZE]; + + memset(m_widths, 0, sizeof(int)*FWC_SIZE); + } + + wxFont m_font; + double m_scaleX; + int *m_widths; +}; + +static FontWidthCache s_fontWidthCache; bool wxDCBase::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const { int totalWidth = 0; - size_t i; + size_t i, len = text.Length(); widths.Empty(); - widths.Add(0, text.Length()); + widths.Add(0, len); + int w, h; + // reset the cache if font or horizontal scale have changed + if (!s_fontWidthCache.m_widths || + (s_fontWidthCache.m_scaleX != m_scaleX) || + (s_fontWidthCache.m_font != GetFont())) + { + s_fontWidthCache.Reset(); + s_fontWidthCache.m_font = GetFont(); + s_fontWidthCache.m_scaleX = m_scaleX; + } + // Calculate the position of each character based on the widths of // the previous characters - for (i=0; i