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

View File

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

View File

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

View File

@@ -152,13 +152,29 @@ wxFontRefData::~wxFontRefData()
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;
}
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();
Create(info.ToString());
Create(info.xFontName);
}
bool wxFont::Create(const wxNativeFontInfo& info)

View File

@@ -152,13 +152,29 @@ wxFontRefData::~wxFontRefData()
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;
}
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();
Create(info.ToString());
Create(info.xFontName);
}
bool wxFont::Create(const wxNativeFontInfo& info)

View File

@@ -38,6 +38,7 @@
#endif // WX_PRECOMP
#include "wx/fontutil.h"
#include "wx/tokenzr.h"
#include "wx/msw/private.h"
@@ -84,6 +85,11 @@ public:
Init(size, family, style, weight, underlined, faceName, encoding);
}
wxFontRefData(const wxNativeFontInfo& info)
{
Init(info);
}
virtual ~wxFontRefData();
protected:
@@ -96,6 +102,8 @@ protected:
const wxString& faceName,
wxFontEncoding encoding);
void Init(const wxNativeFontInfo& info);
// If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
// DELETED by destructor
bool m_temporary;
@@ -113,6 +121,10 @@ protected:
// Windows font handle
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_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()
@@ -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
// ----------------------------------------------------------------------------
@@ -169,8 +377,13 @@ void wxFont::Init()
bool wxFont::Create(const wxNativeFontInfo& info)
{
return Create(info.pointSize, info.family, info.style, info.weight,
info.underlined, info.faceName, info.encoding);
UnRef();
m_refData = new wxFontRefData(info);
RealizeResource();
return TRUE;
}
wxFont::wxFont(const wxString& fontdesc)
@@ -225,10 +438,14 @@ bool wxFont::RealizeResource()
return TRUE;
}
LOGFONT lf;
wxFillLogFont(&lf, this);
M_FONTDATA->m_hFont = (WXHFONT)::CreateFontIndirect(&lf);
M_FONTDATA->m_faceName = lf.lfFaceName;
if(!M_FONTDATA->m_nativeFontInfoOk)
{
wxFillLogFont(&M_FONTDATA->m_nativeFontInfo.lf, this);
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 )
{
wxLogLastError(wxT("CreateFont"));
@@ -297,6 +514,7 @@ void wxFont::SetPointSize(int pointSize)
Unshare();
M_FONTDATA->m_pointSize = pointSize;
M_FONTDATA->m_nativeFontInfoOk = FALSE;
RealizeResource();
}
@@ -306,6 +524,7 @@ void wxFont::SetFamily(int family)
Unshare();
M_FONTDATA->m_family = family;
M_FONTDATA->m_nativeFontInfoOk = FALSE;
RealizeResource();
}
@@ -315,6 +534,7 @@ void wxFont::SetStyle(int style)
Unshare();
M_FONTDATA->m_style = style;
M_FONTDATA->m_nativeFontInfoOk = FALSE;
RealizeResource();
}
@@ -324,6 +544,7 @@ void wxFont::SetWeight(int weight)
Unshare();
M_FONTDATA->m_weight = weight;
M_FONTDATA->m_nativeFontInfoOk = FALSE;
RealizeResource();
}
@@ -333,6 +554,7 @@ void wxFont::SetFaceName(const wxString& faceName)
Unshare();
M_FONTDATA->m_faceName = faceName;
M_FONTDATA->m_nativeFontInfoOk = FALSE;
RealizeResource();
}
@@ -342,6 +564,7 @@ void wxFont::SetUnderlined(bool underlined)
Unshare();
M_FONTDATA->m_underlined = underlined;
M_FONTDATA->m_nativeFontInfoOk = FALSE;
RealizeResource();
}
@@ -351,6 +574,18 @@ void wxFont::SetEncoding(wxFontEncoding encoding)
Unshare();
M_FONTDATA->m_encoding = encoding;
M_FONTDATA->m_nativeFontInfoOk = FALSE;
RealizeResource();
}
void wxFont::SetNativeFontInfo(const wxNativeFontInfo& info)
{
Unshare();
FreeResource();
M_FONTDATA->Init(info);
RealizeResource();
}
@@ -402,3 +637,11 @@ wxFontEncoding wxFont::GetEncoding() const
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)
{
// extract family from pitch-and-family
int lfFamily = logFont->lfPitchAndFamily;
if ( lfFamily & FIXED_PITCH )
lfFamily -= FIXED_PITCH;
if ( lfFamily & VARIABLE_PITCH )
lfFamily -= VARIABLE_PITCH;
wxNativeFontInfo info;
int fontFamily;
switch ( lfFamily )
{
case FF_ROMAN:
fontFamily = wxROMAN;
break;
info.lf = *logFont;
case FF_SWISS:
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));
return wxFont(info);
}