1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/textmeasure.cpp
3 // Purpose: wxTextMeasure implementation for wxGTK
4 // Author: Manuel Martin
7 // Copyright: (c) 1997-2012 wxWidgets team
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
23 #include "wx/window.h"
27 #include "wx/private/textmeasure.h"
29 #include "wx/fontutil.h"
30 #include "wx/gtk/private.h"
33 #include "wx/gtk/dcclient.h"
36 // ============================================================================
37 // wxTextMeasure implementation
38 // ============================================================================
40 void wxTextMeasure
::Init()
42 wxASSERT_MSG( m_font
, wxT("wxTextMeasure needs a valid wxFont") );
51 // Get Gtk needed elements, if we have not them yet.
52 void wxTextMeasure
::BeginMeasuring()
57 m_wdc
= wxDynamicCast(m_dc
->GetImpl(), wxWindowDCImpl
);
60 m_context
= m_wdc
->m_context
;
61 m_layout
= m_wdc
->m_layout
;
67 m_context
= gtk_widget_get_pango_context( m_win
->GetHandle() );
69 m_layout
= pango_layout_new(m_context
);
72 // set the font to use
75 pango_layout_set_font_description(m_layout
,
76 m_font
->GetNativeFontInfo()->description
);
80 void wxTextMeasure
::EndMeasuring()
88 // Reset dc own font description
89 pango_layout_set_font_description( m_wdc
->m_layout
, m_wdc
->m_fontdesc
);
94 g_object_unref (m_layout
);
98 // Notice we don't check here the font. It is supposed to be OK before the call.
99 void wxTextMeasure
::DoGetTextExtent(const wxString
& string
,
103 wxCoord
*externalLeading
)
113 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(string
, *m_font
);
116 // hardly ideal, but what else can we do if conversion failed?
117 wxLogLastError(wxT("GetTextExtent"));
120 pango_layout_set_text(m_layout
, dataUTF8
, -1);
125 pango_layout_get_pixel_size(m_layout
, width
, height
);
129 // the logical rect bounds the ink rect
131 pango_layout_get_extents(m_layout
, NULL
, &rect
);
132 *width
= PANGO_PIXELS(rect
.width
);
133 *height
= PANGO_PIXELS(rect
.height
);
138 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
139 int baseline
= pango_layout_iter_get_baseline(iter
);
140 pango_layout_iter_free(iter
);
141 *descent
= *height
- PANGO_PIXELS(baseline
);
146 // No support for MSW-like "external leading" in Pango.
147 *externalLeading
= 0;
151 bool wxTextMeasure
::DoGetPartialTextExtents(const wxString
& text
,
153 double WXUNUSED(scaleX
))
156 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(text
, *m_font
);
159 // hardly ideal, but what else can we do if conversion failed?
160 wxLogLastError(wxT("GetPartialTextExtents"));
164 pango_layout_set_text(m_layout
, dataUTF8
, -1);
166 // Calculate the position of each character based on the widths of
167 // the previous characters
169 // Code borrowed from Scintilla's PlatGTK
170 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
172 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
174 while (pango_layout_iter_next_cluster(iter
))
176 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
177 int position
= PANGO_PIXELS(pos
.x
);
178 widths
[i
++] = position
;
181 const size_t len
= text
.length();
183 widths
[i
++] = PANGO_PIXELS(pos
.x
+ pos
.width
);
184 pango_layout_iter_free(iter
);