]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/textmeasure.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/textmeasure.cpp
3 // Purpose: wxTextMeasure implementation for wxGTK
4 // Author: Manuel Martin
6 // Copyright: (c) 1997-2012 wxWidgets team
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // for compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
22 #include "wx/window.h"
25 #include "wx/private/textmeasure.h"
27 #include "wx/fontutil.h"
28 #include "wx/gtk/private.h"
31 #include "wx/gtk/dcclient.h"
34 // ============================================================================
35 // wxTextMeasure implementation
36 // ============================================================================
38 void wxTextMeasure::Init()
40 wxASSERT_MSG( m_font
, wxT("wxTextMeasure needs a valid wxFont") );
49 // Get Gtk needed elements, if we have not them yet.
50 void wxTextMeasure::BeginMeasuring()
55 m_wdc
= wxDynamicCast(m_dc
->GetImpl(), wxWindowDCImpl
);
58 m_context
= m_wdc
->m_context
;
59 m_layout
= m_wdc
->m_layout
;
65 m_context
= gtk_widget_get_pango_context( m_win
->GetHandle() );
67 m_layout
= pango_layout_new(m_context
);
70 // set the font to use
73 pango_layout_set_font_description(m_layout
,
74 m_font
->GetNativeFontInfo()->description
);
78 void wxTextMeasure::EndMeasuring()
86 // Reset dc own font description
87 pango_layout_set_font_description( m_wdc
->m_layout
, m_wdc
->m_fontdesc
);
92 g_object_unref (m_layout
);
96 // Notice we don't check here the font. It is supposed to be OK before the call.
97 void wxTextMeasure::DoGetTextExtent(const wxString
& string
,
101 wxCoord
*externalLeading
)
111 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(string
, *m_font
);
114 // hardly ideal, but what else can we do if conversion failed?
115 wxLogLastError(wxT("GetTextExtent"));
118 pango_layout_set_text(m_layout
, dataUTF8
, -1);
123 pango_layout_get_pixel_size(m_layout
, width
, height
);
127 // the logical rect bounds the ink rect
129 pango_layout_get_extents(m_layout
, NULL
, &rect
);
130 *width
= PANGO_PIXELS(rect
.width
);
131 *height
= PANGO_PIXELS(rect
.height
);
136 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
137 int baseline
= pango_layout_iter_get_baseline(iter
);
138 pango_layout_iter_free(iter
);
139 *descent
= *height
- PANGO_PIXELS(baseline
);
144 // No support for MSW-like "external leading" in Pango.
145 *externalLeading
= 0;
149 bool wxTextMeasure::DoGetPartialTextExtents(const wxString
& text
,
151 double WXUNUSED(scaleX
))
154 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(text
, *m_font
);
157 // hardly ideal, but what else can we do if conversion failed?
158 wxLogLastError(wxT("GetPartialTextExtents"));
162 pango_layout_set_text(m_layout
, dataUTF8
, -1);
164 // Calculate the position of each character based on the widths of
165 // the previous characters
167 // Code borrowed from Scintilla's PlatGTK
168 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
170 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
172 while (pango_layout_iter_next_cluster(iter
))
174 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
175 int position
= PANGO_PIXELS(pos
.x
);
176 widths
[i
++] = position
;
179 const size_t len
= text
.length();
181 widths
[i
++] = PANGO_PIXELS(pos
.x
+ pos
.width
);
182 pango_layout_iter_free(iter
);