]> git.saurik.com Git - wxWidgets.git/blame - src/generic/textmeasure.cpp
Make hack for button creation in wxOSX more robust.
[wxWidgets.git] / src / generic / textmeasure.cpp
CommitLineData
8cd79b7a
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/generic/textmeasure.cpp
3// Purpose:
4// Author: Vadim Zeitlin
5// Created: 2012-10-17
6// RCS-ID: $Id$
7// Copyright: (c) 2012 Vadim Zeitlin <vadim@wxwidgets.org>
8// Licence: wxWindows licence
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// for compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
88f70d8c
RD
26#ifndef WX_PRECOMP
27 #include "wx/window.h"
28 #include "wx/dc.h"
29#endif //WX_PRECOMP
30
8cd79b7a
VZ
31#include "wx/private/textmeasure.h"
32
33#if wxUSE_GENERIC_TEXTMEASURE
34
35// ============================================================================
36// wxTextMeasure generic implementation
37// ============================================================================
38
39// We assume that the ports not providing platform-specific wxTextMeasure
40// implementation implement the corresponding functions in their wxDC and
41// wxWindow classes, so forward back to them instead of using wxTextMeasure
42// from there, as usual.
43void wxTextMeasure::DoGetTextExtent(const wxString& string,
44 wxCoord *width,
45 wxCoord *height,
46 wxCoord *descent,
47 wxCoord *externalLeading)
48{
49 if ( m_dc )
50 {
51 m_dc->GetTextExtent(string, width, height,
52 descent, externalLeading, m_font);
53 }
54 else if ( m_win )
55 {
56 m_win->GetTextExtent(string, width, height,
57 descent, externalLeading, m_font);
58 }
59 //else: we already asserted in the ctor, don't do it any more
60}
61
62// Each element of the widths array will be the width of the string up to and
63// including the corresponding character in text. This is the generic
64// implementation, the port-specific classes should do this with native APIs
65// if available and if faster. Note: pango_layout_index_to_pos is much slower
66// than calling GetTextExtent!!
67
68#define FWC_SIZE 256
69
70class FontWidthCache
71{
72public:
73 FontWidthCache() : m_scaleX(1), m_widths(NULL) { }
74 ~FontWidthCache() { delete []m_widths; }
75
76 void Reset()
77 {
78 if ( !m_widths )
79 m_widths = new int[FWC_SIZE];
80
81 memset(m_widths, 0, sizeof(int)*FWC_SIZE);
82 }
83
84 wxFont m_font;
85 double m_scaleX;
86 int *m_widths;
87};
88
89static FontWidthCache s_fontWidthCache;
90
91bool wxTextMeasure::DoGetPartialTextExtents(const wxString& text,
92 wxArrayInt& widths,
93 double scaleX)
94{
95 int totalWidth = 0;
96
97 // reset the cache if font or horizontal scale have changed
98 if ( !s_fontWidthCache.m_widths ||
99 !wxIsSameDouble(s_fontWidthCache.m_scaleX, scaleX) ||
100 (s_fontWidthCache.m_font != *m_font) )
101 {
102 s_fontWidthCache.Reset();
103 s_fontWidthCache.m_font = *m_font;
104 s_fontWidthCache.m_scaleX = scaleX;
105 }
106
107 // Calculate the position of each character based on the widths of
108 // the previous characters. This is inexact for not fixed fonts.
109 int n = 0;
110 for ( wxString::const_iterator it = text.begin();
111 it != text.end();
112 ++it )
113 {
114 const wxChar c = *it;
115 unsigned int c_int = (unsigned int)c;
116
117 int w;
118 if ((c_int < FWC_SIZE) && (s_fontWidthCache.m_widths[c_int] != 0))
119 {
120 w = s_fontWidthCache.m_widths[c_int];
121 }
122 else
123 {
124 DoGetTextExtent(c, &w, NULL);
125 if (c_int < FWC_SIZE)
126 s_fontWidthCache.m_widths[c_int] = w;
127 }
128
129 totalWidth += w;
130 widths[n++] = totalWidth;
131 }
132
133 return true;
134}
135
136#endif // wxUSE_GENERIC_TEXTMEASURE