applied wxNativeFontInfo patch #103089

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@9160 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2001-01-24 17:16:04 +00:00
parent b92fd37cb0
commit 09fcd88955
7 changed files with 312 additions and 89 deletions

View File

@@ -26,6 +26,11 @@
#include "wx/font.h" // for wxFont and wxFontEncoding #include "wx/font.h" // for wxFont and wxFontEncoding
#if defined(__WXMSW__)
#include <windows.h>
#include "wx/msw/winundef.h"
#endif
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// types // types
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -39,6 +44,8 @@ struct WXDLLEXPORT wxNativeFontInfo
{ {
#if defined(__WXGTK__) #if defined(__WXGTK__)
wxString xFontName; wxString xFontName;
#elif defined(__WXMSW__)
LOGFONT lf;
#else // other platforms #else // other platforms
// //
// This is a generic implementation that should work on all ports // This is a generic implementation that should work on all ports

View File

@@ -72,6 +72,7 @@ public:
virtual bool GetUnderlined() const; virtual bool GetUnderlined() const;
virtual wxString GetFaceName() const; virtual wxString GetFaceName() const;
virtual wxFontEncoding GetEncoding() const; virtual wxFontEncoding GetEncoding() const;
virtual wxNativeFontInfo* GetNativeFontInfo() const;
virtual void SetPointSize(int pointSize); virtual void SetPointSize(int pointSize);
virtual void SetFamily(int family); virtual void SetFamily(int family);
@@ -80,6 +81,7 @@ public:
virtual void SetFaceName(const wxString& faceName); virtual void SetFaceName(const wxString& faceName);
virtual void SetUnderlined(bool underlined); virtual void SetUnderlined(bool underlined);
virtual void SetEncoding(wxFontEncoding encoding); virtual void SetEncoding(wxFontEncoding encoding);
virtual void SetNativeFontInfo(const wxNativeFontInfo& info);
// implementation only from now on // implementation only from now on
// ------------------------------- // -------------------------------

View File

@@ -32,6 +32,7 @@
#include "wx/font.h" #include "wx/font.h"
#endif // WX_PRECOMP #endif // WX_PRECOMP
#include "wx/gdicmn.h"
#include "wx/fontutil.h" // for wxNativeFontInfo #include "wx/fontutil.h" // for wxNativeFontInfo
#include "wx/tokenzr.h" #include "wx/tokenzr.h"
@@ -69,14 +70,14 @@ wxFont *wxFontBase::New(const wxString& strNativeFontDesc)
{ {
wxNativeFontInfo fontInfo; wxNativeFontInfo fontInfo;
if ( !fontInfo.FromString(strNativeFontDesc) ) if ( !fontInfo.FromString(strNativeFontDesc) )
return (wxFont *)NULL; return new wxFont(*wxNORMAL_FONT);
return New(fontInfo); return New(fontInfo);
} }
wxNativeFontInfo *wxFontBase::GetNativeFontInfo() const wxNativeFontInfo *wxFontBase::GetNativeFontInfo() const
{ {
#if !defined(__WXGTK__) #if !defined(__WXGTK__) && !defined(__WXMSW__)
wxNativeFontInfo *fontInfo = new wxNativeFontInfo; wxNativeFontInfo *fontInfo = new wxNativeFontInfo;
fontInfo->pointSize = GetPointSize(); fontInfo->pointSize = GetPointSize();
@@ -95,7 +96,7 @@ wxNativeFontInfo *wxFontBase::GetNativeFontInfo() const
void wxFontBase::SetNativeFontInfo(const wxNativeFontInfo& info) void wxFontBase::SetNativeFontInfo(const wxNativeFontInfo& info)
{ {
#if !defined(__WXGTK__) #if !defined(__WXGTK__) && !defined(__WXMSW__)
SetPointSize(info.pointSize); SetPointSize(info.pointSize);
SetFamily(info.family); SetFamily(info.family);
SetStyle(info.style); SetStyle(info.style);
@@ -180,7 +181,7 @@ wxString wxFontBase::GetWeightString() const
} }
} }
#if !defined(__WXGTK__) #if !defined(__WXGTK__) && !defined(__WXMSW__)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxNativeFontInfo // wxNativeFontInfo
@@ -189,7 +190,7 @@ wxString wxFontBase::GetWeightString() const
// These are the generic forms of FromString()/ToString. // These are the generic forms of FromString()/ToString.
// //
// convert to/from the string representation: format is // convert to/from the string representation: format is
// pointsize;family;style;weight;underlined;facename;encoding // version;pointsize;family;style;weight;underlined;facename;encoding
bool wxNativeFontInfo::FromString(const wxString& s) bool wxNativeFontInfo::FromString(const wxString& s)
{ {
@@ -198,6 +199,11 @@ bool wxNativeFontInfo::FromString(const wxString& s)
wxStringTokenizer tokenizer(s, _T(";")); wxStringTokenizer tokenizer(s, _T(";"));
wxString token = tokenizer.GetNextToken(); wxString token = tokenizer.GetNextToken();
//
// Ignore the version for now
//
token = tokenizer.GetNextToken();
if ( !token.ToLong(&l) ) if ( !token.ToLong(&l) )
return FALSE; return FALSE;
pointSize = (int)l; pointSize = (int)l;
@@ -238,7 +244,8 @@ wxString wxNativeFontInfo::ToString() const
{ {
wxString s; wxString s;
s.Printf(_T("%d;%d;%d;%d;%d;%s;%d"), s.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
0, // version
pointSize, pointSize,
family, family,
style, style,

View File

@@ -152,13 +152,29 @@ wxFontRefData::~wxFontRefData()
bool wxNativeFontInfo::FromString(const wxString& s) bool wxNativeFontInfo::FromString(const wxString& s)
{ {
xFontName = s; wxStringTokenizer tokenizer(s, _T(";"));
wxString token = tokenizer.GetNextToken();
//
// Ignore the version for now
//
xFontName = tokenizer.GetNextToken();
if(!xFontName)
return FALSE;
return TRUE; return TRUE;
} }
wxString wxNativeFontInfo::ToString() const wxString wxNativeFontInfo::ToString() const
{ {
return xFontName; wxString s;
s.Printf("%d;%s",
0, // version
xFontName.c_str());
return s;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -177,7 +193,7 @@ wxFont::wxFont(const wxNativeFontInfo& info)
{ {
Init(); Init();
Create(info.ToString()); Create(info.xFontName);
} }
bool wxFont::Create(const wxNativeFontInfo& info) bool wxFont::Create(const wxNativeFontInfo& info)

View File

@@ -152,13 +152,29 @@ wxFontRefData::~wxFontRefData()
bool wxNativeFontInfo::FromString(const wxString& s) bool wxNativeFontInfo::FromString(const wxString& s)
{ {
xFontName = s; wxStringTokenizer tokenizer(s, _T(";"));
wxString token = tokenizer.GetNextToken();
//
// Ignore the version for now
//
xFontName = tokenizer.GetNextToken();
if(!xFontName)
return FALSE;
return TRUE; return TRUE;
} }
wxString wxNativeFontInfo::ToString() const wxString wxNativeFontInfo::ToString() const
{ {
return xFontName; wxString s;
s.Printf("%d;%s",
0, // version
xFontName.c_str());
return s;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -177,7 +193,7 @@ wxFont::wxFont(const wxNativeFontInfo& info)
{ {
Init(); Init();
Create(info.ToString()); Create(info.xFontName);
} }
bool wxFont::Create(const wxNativeFontInfo& info) bool wxFont::Create(const wxNativeFontInfo& info)

View File

@@ -38,6 +38,7 @@
#endif // WX_PRECOMP #endif // WX_PRECOMP
#include "wx/fontutil.h" #include "wx/fontutil.h"
#include "wx/tokenzr.h"
#include "wx/msw/private.h" #include "wx/msw/private.h"
@@ -84,6 +85,11 @@ public:
Init(size, family, style, weight, underlined, faceName, encoding); Init(size, family, style, weight, underlined, faceName, encoding);
} }
wxFontRefData(const wxNativeFontInfo& info)
{
Init(info);
}
virtual ~wxFontRefData(); virtual ~wxFontRefData();
protected: protected:
@@ -96,6 +102,8 @@ protected:
const wxString& faceName, const wxString& faceName,
wxFontEncoding encoding); wxFontEncoding encoding);
void Init(const wxNativeFontInfo& info);
// If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
// DELETED by destructor // DELETED by destructor
bool m_temporary; bool m_temporary;
@@ -113,6 +121,10 @@ protected:
// Windows font handle // Windows font handle
WXHFONT m_hFont; WXHFONT m_hFont;
// Native font info
wxNativeFontInfo m_nativeFontInfo;
bool m_nativeFontInfoOk;
}; };
// ============================================================================ // ============================================================================
@@ -144,6 +156,89 @@ void wxFontRefData::Init(int pointSize,
m_temporary = FALSE; m_temporary = FALSE;
m_hFont = 0; m_hFont = 0;
m_nativeFontInfoOk = FALSE;
}
void wxFontRefData::Init(const wxNativeFontInfo& info)
{
// extract family from pitch-and-family
int lfFamily = info.lf.lfPitchAndFamily;
if ( lfFamily & FIXED_PITCH )
lfFamily -= FIXED_PITCH;
if ( lfFamily & VARIABLE_PITCH )
lfFamily -= VARIABLE_PITCH;
switch ( lfFamily )
{
case FF_ROMAN:
m_family = wxROMAN;
break;
case FF_SWISS:
m_family = wxSWISS;
break;
case FF_SCRIPT:
m_family = wxSCRIPT;
break;
case FF_MODERN:
m_family = wxMODERN;
break;
case FF_DECORATIVE:
m_family = wxDECORATIVE;
break;
default:
m_family = wxSWISS;
}
// weight and style
switch ( info.lf.lfWeight )
{
case FW_LIGHT:
m_weight = wxLIGHT;
break;
default:
case FW_NORMAL:
m_weight = wxNORMAL;
break;
case FW_BOLD:
m_weight = wxBOLD;
break;
}
m_style = info.lf.lfItalic ? wxITALIC : wxNORMAL;
m_underlined = info.lf.lfUnderline != 0;
m_faceName = info.lf.lfFaceName;
// remember that 1pt = 1/72inch
int height = abs(info.lf.lfHeight);
#if wxUSE_SCREEN_DPI
HDC dc = ::GetDC(NULL);
static const int ppInch = GetDeviceCaps(dc, LOGPIXELSY);
::ReleaseDC(NULL, dc);
#else
static const int ppInch = 96;
#endif
m_pointSize = (int) (((72.0*((double)height))/(double) ppInch) + 0.5);
m_encoding = wxGetFontEncFromCharSet(info.lf.lfCharSet);
m_fontId = 0;
m_temporary = FALSE;
m_hFont = 0;
m_nativeFontInfoOk = TRUE;
m_nativeFontInfo = info;
} }
wxFontRefData::~wxFontRefData() wxFontRefData::~wxFontRefData()
@@ -157,6 +252,119 @@ wxFontRefData::~wxFontRefData()
} }
} }
// ----------------------------------------------------------------------------
// wxNativeFontInfo
// ----------------------------------------------------------------------------
bool wxNativeFontInfo::FromString(const wxString& s)
{
long l;
wxStringTokenizer tokenizer(s, _T(";"));
wxString token = tokenizer.GetNextToken();
//
// Ignore the version for now
//
token = tokenizer.GetNextToken();
if ( !token.ToLong(&l) )
return FALSE;
lf.lfHeight = l;
token = tokenizer.GetNextToken();
if ( !token.ToLong(&l) )
return FALSE;
lf.lfWidth = l;
token = tokenizer.GetNextToken();
if ( !token.ToLong(&l) )
return FALSE;
lf.lfEscapement = l;
token = tokenizer.GetNextToken();
if ( !token.ToLong(&l) )
return FALSE;
lf.lfOrientation = l;
token = tokenizer.GetNextToken();
if ( !token.ToLong(&l) )
return FALSE;
lf.lfWeight = l;
token = tokenizer.GetNextToken();
if ( !token.ToLong(&l) )
return FALSE;
lf.lfItalic = l;
token = tokenizer.GetNextToken();
if ( !token.ToLong(&l) )
return FALSE;
lf.lfUnderline = l;
token = tokenizer.GetNextToken();
if ( !token.ToLong(&l) )
return FALSE;
lf.lfStrikeOut = l;
token = tokenizer.GetNextToken();
if ( !token.ToLong(&l) )
return FALSE;
lf.lfCharSet = l;
token = tokenizer.GetNextToken();
if ( !token.ToLong(&l) )
return FALSE;
lf.lfOutPrecision = l;
token = tokenizer.GetNextToken();
if ( !token.ToLong(&l) )
return FALSE;
lf.lfClipPrecision = l;
token = tokenizer.GetNextToken();
if ( !token.ToLong(&l) )
return FALSE;
lf.lfQuality = l;
token = tokenizer.GetNextToken();
if ( !token.ToLong(&l) )
return FALSE;
lf.lfPitchAndFamily = l;
token = tokenizer.GetNextToken();
if(!token)
return FALSE;
wxStrcpy(lf.lfFaceName, token.c_str());
return TRUE;
}
wxString wxNativeFontInfo::ToString() const
{
wxString s;
s.Printf(_T("%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%s"),
0, // version, in case we want to change the format later
lf.lfHeight,
lf.lfWidth,
lf.lfEscapement,
lf.lfOrientation,
lf.lfWeight,
lf.lfItalic,
lf.lfUnderline,
lf.lfStrikeOut,
lf.lfCharSet,
lf.lfOutPrecision,
lf.lfClipPrecision,
lf.lfQuality,
lf.lfPitchAndFamily,
lf.lfFaceName);
return s;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxFont // wxFont
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -169,8 +377,13 @@ void wxFont::Init()
bool wxFont::Create(const wxNativeFontInfo& info) bool wxFont::Create(const wxNativeFontInfo& info)
{ {
return Create(info.pointSize, info.family, info.style, info.weight, UnRef();
info.underlined, info.faceName, info.encoding);
m_refData = new wxFontRefData(info);
RealizeResource();
return TRUE;
} }
wxFont::wxFont(const wxString& fontdesc) wxFont::wxFont(const wxString& fontdesc)
@@ -225,10 +438,14 @@ bool wxFont::RealizeResource()
return TRUE; return TRUE;
} }
LOGFONT lf; if(!M_FONTDATA->m_nativeFontInfoOk)
wxFillLogFont(&lf, this); {
M_FONTDATA->m_hFont = (WXHFONT)::CreateFontIndirect(&lf); wxFillLogFont(&M_FONTDATA->m_nativeFontInfo.lf, this);
M_FONTDATA->m_faceName = lf.lfFaceName; M_FONTDATA->m_nativeFontInfoOk = TRUE;
}
M_FONTDATA->m_hFont = (WXHFONT)::CreateFontIndirect(&M_FONTDATA->m_nativeFontInfo.lf);
M_FONTDATA->m_faceName = M_FONTDATA->m_nativeFontInfo.lf.lfFaceName;
if ( !M_FONTDATA->m_hFont ) if ( !M_FONTDATA->m_hFont )
{ {
wxLogLastError(wxT("CreateFont")); wxLogLastError(wxT("CreateFont"));
@@ -297,6 +514,7 @@ void wxFont::SetPointSize(int pointSize)
Unshare(); Unshare();
M_FONTDATA->m_pointSize = pointSize; M_FONTDATA->m_pointSize = pointSize;
M_FONTDATA->m_nativeFontInfoOk = FALSE;
RealizeResource(); RealizeResource();
} }
@@ -306,6 +524,7 @@ void wxFont::SetFamily(int family)
Unshare(); Unshare();
M_FONTDATA->m_family = family; M_FONTDATA->m_family = family;
M_FONTDATA->m_nativeFontInfoOk = FALSE;
RealizeResource(); RealizeResource();
} }
@@ -315,6 +534,7 @@ void wxFont::SetStyle(int style)
Unshare(); Unshare();
M_FONTDATA->m_style = style; M_FONTDATA->m_style = style;
M_FONTDATA->m_nativeFontInfoOk = FALSE;
RealizeResource(); RealizeResource();
} }
@@ -324,6 +544,7 @@ void wxFont::SetWeight(int weight)
Unshare(); Unshare();
M_FONTDATA->m_weight = weight; M_FONTDATA->m_weight = weight;
M_FONTDATA->m_nativeFontInfoOk = FALSE;
RealizeResource(); RealizeResource();
} }
@@ -333,6 +554,7 @@ void wxFont::SetFaceName(const wxString& faceName)
Unshare(); Unshare();
M_FONTDATA->m_faceName = faceName; M_FONTDATA->m_faceName = faceName;
M_FONTDATA->m_nativeFontInfoOk = FALSE;
RealizeResource(); RealizeResource();
} }
@@ -342,6 +564,7 @@ void wxFont::SetUnderlined(bool underlined)
Unshare(); Unshare();
M_FONTDATA->m_underlined = underlined; M_FONTDATA->m_underlined = underlined;
M_FONTDATA->m_nativeFontInfoOk = FALSE;
RealizeResource(); RealizeResource();
} }
@@ -351,6 +574,18 @@ void wxFont::SetEncoding(wxFontEncoding encoding)
Unshare(); Unshare();
M_FONTDATA->m_encoding = encoding; M_FONTDATA->m_encoding = encoding;
M_FONTDATA->m_nativeFontInfoOk = FALSE;
RealizeResource();
}
void wxFont::SetNativeFontInfo(const wxNativeFontInfo& info)
{
Unshare();
FreeResource();
M_FONTDATA->Init(info);
RealizeResource(); RealizeResource();
} }
@@ -402,3 +637,11 @@ wxFontEncoding wxFont::GetEncoding() const
return M_FONTDATA->m_encoding; return M_FONTDATA->m_encoding;
} }
wxNativeFontInfo *wxFont::GetNativeFontInfo() const
{
if( M_FONTDATA->m_nativeFontInfoOk )
return new wxNativeFontInfo(M_FONTDATA->m_nativeFontInfo);
return 0;
}

View File

@@ -403,78 +403,10 @@ void wxFillLogFont(LOGFONT *logFont, const wxFont *font)
wxFont wxCreateFontFromLogFont(const LOGFONT *logFont) wxFont wxCreateFontFromLogFont(const LOGFONT *logFont)
{ {
// extract family from pitch-and-family wxNativeFontInfo info;
int lfFamily = logFont->lfPitchAndFamily;
if ( lfFamily & FIXED_PITCH )
lfFamily -= FIXED_PITCH;
if ( lfFamily & VARIABLE_PITCH )
lfFamily -= VARIABLE_PITCH;
int fontFamily; info.lf = *logFont;
switch ( lfFamily )
{
case FF_ROMAN:
fontFamily = wxROMAN;
break;
case FF_SWISS: return wxFont(info);
fontFamily = wxSWISS;
break;
case FF_SCRIPT:
fontFamily = wxSCRIPT;
break;
case FF_MODERN:
fontFamily = wxMODERN;
break;
case FF_DECORATIVE:
fontFamily = wxDECORATIVE;
break;
default:
fontFamily = wxSWISS;
}
// weight and style
int fontWeight = wxNORMAL;
switch ( logFont->lfWeight )
{
case FW_LIGHT:
fontWeight = wxLIGHT;
break;
default:
case FW_NORMAL:
fontWeight = wxNORMAL;
break;
case FW_BOLD:
fontWeight = wxBOLD;
break;
}
int fontStyle = logFont->lfItalic ? wxITALIC : wxNORMAL;
bool fontUnderline = logFont->lfUnderline != 0;
wxString fontFace = logFont->lfFaceName;
// remember that 1pt = 1/72inch
int height = abs(logFont->lfHeight);
#if wxUSE_SCREEN_DPI
HDC dc = ::GetDC(NULL);
static const int ppInch = GetDeviceCaps(dc, LOGPIXELSY);
::ReleaseDC(NULL, dc);
#else
static const int ppInch = 96;
#endif
int fontPoints = (int) (((72.0*((double)height))/(double) ppInch) + 0.5);
return wxFont(fontPoints, fontFamily, fontStyle,
fontWeight, fontUnderline, fontFace,
wxGetFontEncFromCharSet(logFont->lfCharSet));
} }