Merge branch 'gc-get-text-extent'
Micro-optimization in wxGDIPlusContext::GetTextExtent() and minor printing sample cleanup. See https://github.com/wxWidgets/wxWidgets/pull/1220
This commit is contained in:
@@ -38,6 +38,7 @@
|
|||||||
|
|
||||||
#if wxUSE_GRAPHICS_CONTEXT
|
#if wxUSE_GRAPHICS_CONTEXT
|
||||||
#include "wx/graphics.h"
|
#include "wx/graphics.h"
|
||||||
|
#include "wx/scopedptr.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __WXMAC__
|
#ifdef __WXMAC__
|
||||||
@@ -219,7 +220,7 @@ void MyApp::Draw(wxDC&dc)
|
|||||||
dc.DrawBitmap( m_bitmap, 10, 10 );
|
dc.DrawBitmap( m_bitmap, 10, 10 );
|
||||||
|
|
||||||
#if wxUSE_GRAPHICS_CONTEXT
|
#if wxUSE_GRAPHICS_CONTEXT
|
||||||
wxGraphicsContext *gc = wxGraphicsContext::CreateFromUnknownDC(dc);
|
wxScopedPtr<wxGraphicsContext> gc(wxGraphicsContext::CreateFromUnknownDC(dc));
|
||||||
|
|
||||||
if (gc)
|
if (gc)
|
||||||
{
|
{
|
||||||
@@ -243,12 +244,10 @@ void MyApp::Draw(wxDC&dc)
|
|||||||
gc->DrawText(text, 25.0, 60.0);
|
gc->DrawText(text, 25.0, 60.0);
|
||||||
|
|
||||||
// draw rectangle around the text
|
// draw rectangle around the text
|
||||||
double w, h, d, el;
|
double w, h;
|
||||||
gc->GetTextExtent(text, &w, &h, &d, &el);
|
gc->GetTextExtent(text, &w, &h);
|
||||||
gc->SetPen( *wxBLACK_PEN );
|
gc->SetPen( *wxBLACK_PEN );
|
||||||
gc->DrawRectangle(25.0, 60.0, w, h);
|
gc->DrawRectangle(25.0, 60.0, w, h);
|
||||||
|
|
||||||
delete gc;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@@ -2111,33 +2111,40 @@ void wxGDIPlusContext::GetTextExtent( const wxString &str, wxDouble *width, wxDo
|
|||||||
wxCHECK_RET( !m_font.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
|
wxCHECK_RET( !m_font.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
|
||||||
|
|
||||||
wxWCharBuffer s = str.wc_str( *wxConvUI );
|
wxWCharBuffer s = str.wc_str( *wxConvUI );
|
||||||
FontFamily ffamily ;
|
|
||||||
Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont();
|
Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont();
|
||||||
|
|
||||||
f->GetFamily(&ffamily) ;
|
// Get the font metrics if we actually need them.
|
||||||
|
if ( descent || externalLeading || (height && str.empty()) )
|
||||||
|
{
|
||||||
|
FontFamily ffamily ;
|
||||||
|
f->GetFamily(&ffamily) ;
|
||||||
|
|
||||||
REAL factorY = m_fontScaleRatio;
|
REAL factorY = m_fontScaleRatio;
|
||||||
|
|
||||||
// Notice that we must use the real font style or the results would be
|
// Notice that we must use the real font style or the results would be
|
||||||
// incorrect for italic/bold fonts.
|
// incorrect for italic/bold fonts.
|
||||||
const INT style = f->GetStyle();
|
const INT style = f->GetStyle();
|
||||||
const REAL size = f->GetSize();
|
const REAL size = f->GetSize();
|
||||||
const REAL emHeight = ffamily.GetEmHeight(style);
|
const REAL emHeight = ffamily.GetEmHeight(style);
|
||||||
REAL rDescent = ffamily.GetCellDescent(style) * size / emHeight;
|
REAL rDescent = ffamily.GetCellDescent(style) * size / emHeight;
|
||||||
REAL rAscent = ffamily.GetCellAscent(style) * size / emHeight;
|
REAL rAscent = ffamily.GetCellAscent(style) * size / emHeight;
|
||||||
REAL rHeight = ffamily.GetLineSpacing(style) * size / emHeight;
|
REAL rHeight = ffamily.GetLineSpacing(style) * size / emHeight;
|
||||||
|
|
||||||
|
if ( height && str.empty() )
|
||||||
|
*height = rHeight * factorY;
|
||||||
|
if ( descent )
|
||||||
|
*descent = rDescent * factorY;
|
||||||
|
if ( externalLeading )
|
||||||
|
*externalLeading = (rHeight - rAscent - rDescent) * factorY;
|
||||||
|
}
|
||||||
|
|
||||||
if ( height )
|
|
||||||
*height = rHeight * factorY;
|
|
||||||
if ( descent )
|
|
||||||
*descent = rDescent * factorY;
|
|
||||||
if ( externalLeading )
|
|
||||||
*externalLeading = (rHeight - rAscent - rDescent) * factorY;
|
|
||||||
// measuring empty strings is not guaranteed, so do it by hand
|
// measuring empty strings is not guaranteed, so do it by hand
|
||||||
if ( str.IsEmpty())
|
if ( str.IsEmpty())
|
||||||
{
|
{
|
||||||
if ( width )
|
if ( width )
|
||||||
*width = 0 ;
|
*width = 0 ;
|
||||||
|
|
||||||
|
// Height already assigned above if necessary.
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user