X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5ce4d9fa3788e3588105437688590d8490e557c8..174ee1b3d307340b029cd4e84124484c3ba51241:/src/common/dcbase.cpp 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