Fix wxGTK1 build after wxFont API changes

Implement the new {Get,Set}{FractionalPointSize,NumericWeight} methods.

Also change wxLoadQueryFont() to use wxNativeFontInfo methods as a side
effect, to reduce code duplication and reuse the existing support for
numeric weights and fractional point sizes in wxNativeFontInfo.
This commit is contained in:
Vadim Zeitlin
2018-09-15 02:26:07 +02:00
parent aff4b82663
commit 90dd87ee65
4 changed files with 82 additions and 247 deletions

View File

@@ -90,19 +90,19 @@ public:
virtual ~wxFont();
// implement base class pure virtuals
virtual int GetPointSize() const;
virtual float GetFractionalPointSize() const;
virtual wxFontStyle GetStyle() const;
virtual wxFontWeight GetWeight() const;
virtual int GetNumericWeight() const;
virtual wxString GetFaceName() const;
virtual bool GetUnderlined() const;
virtual wxFontEncoding GetEncoding() const;
virtual const wxNativeFontInfo *GetNativeFontInfo() const;
virtual bool IsFixedWidth() const;
virtual void SetPointSize( int pointSize );
virtual void SetFractionalPointSize(float pointSize);
virtual void SetFamily(wxFontFamily family);
virtual void SetStyle(wxFontStyle style);
virtual void SetWeight(wxFontWeight weight);
virtual void SetNumericWeight(int weight);
virtual bool SetFaceName( const wxString& faceName );
virtual void SetUnderlined( bool underlined );
virtual void SetEncoding(wxFontEncoding encoding);

View File

@@ -21,10 +21,10 @@
// returns the handle of the nearest available font or 0
extern wxNativeFont
wxLoadQueryNearestFont(int pointSize,
wxLoadQueryNearestFont(float pointSize,
wxFontFamily family,
wxFontStyle style,
wxFontWeight weight,
int weight,
bool underlined,
const wxString &facename,
wxFontEncoding encoding,

View File

@@ -77,10 +77,10 @@ public:
// setters: all of them also take care to modify m_nativeFontInfo if we
// have it so as to not lose the information not carried by our fields
void SetPointSize(int pointSize);
void SetFractionalPointSize(float pointSize);
void SetFamily(wxFontFamily family);
void SetStyle(wxFontStyle style);
void SetWeight(wxFontWeight weight);
void SetNumericWeight(int weight);
void SetUnderlined(bool underlined);
bool SetFaceName(const wxString& facename);
void SetEncoding(wxFontEncoding encoding);
@@ -90,10 +90,10 @@ public:
protected:
// common part of all ctors
void Init(int pointSize,
void Init(float pointSize,
wxFontFamily family,
wxFontStyle style,
wxFontWeight weight,
int weight,
bool underlined,
const wxString& faceName,
wxFontEncoding encoding);
@@ -108,10 +108,10 @@ private:
// the map of font sizes to "GdkFont *"
wxScaledFontList m_scaled_xfonts;
int m_pointSize;
float m_pointSize;
wxFontFamily m_family;
wxFontStyle m_style;
wxFontWeight m_weight;
int m_weight;
bool m_underlined;
wxString m_faceName;
wxFontEncoding m_encoding; // Unused under GTK 2.0
@@ -129,10 +129,10 @@ private:
// wxFontRefData
// ----------------------------------------------------------------------------
void wxFontRefData::Init(int pointSize,
void wxFontRefData::Init(float pointSize,
wxFontFamily family,
wxFontStyle style,
wxFontWeight weight,
int weight,
bool underlined,
const wxString& faceName,
wxFontEncoding encoding)
@@ -144,7 +144,7 @@ void wxFontRefData::Init(int pointSize,
m_style = style;
m_weight = weight;
m_pointSize = pointSize == -1 ? wxDEFAULT_FONT_SIZE : pointSize;
m_pointSize = pointSize < 0 ? wxDEFAULT_FONT_SIZE : pointSize;
m_underlined = underlined;
m_encoding = encoding;
@@ -195,7 +195,7 @@ void wxFontRefData::InitFromNative()
if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_POINTSIZE).ToLong(&ptSize) )
{
// size in XLFD is in 10 point units
m_pointSize = (int)(ptSize / 10);
m_pointSize = ptSize / 10;
}
else
{
@@ -272,10 +272,10 @@ wxFontRefData::wxFontRefData( const wxFontRefData& data )
wxFontRefData::wxFontRefData(const wxFontInfo& info)
{
Init(info.GetPointSize(),
Init(info.GetFractionalPointSize(),
info.GetFamily(),
info.GetStyle(),
info.GetWeight(),
info.GetNumericWeight(),
info.IsUnderlined(),
info.GetFaceName(),
info.GetEncoding());
@@ -311,19 +311,13 @@ wxFontRefData::~wxFontRefData()
// wxFontRefData SetXXX()
// ----------------------------------------------------------------------------
void wxFontRefData::SetPointSize(int pointSize)
void wxFontRefData::SetFractionalPointSize(float pointSize)
{
m_pointSize = pointSize;
if ( HasNativeFont() )
{
wxString size;
if ( pointSize == -1 )
size = wxT('*');
else
size.Printf(wxT("%d"), 10*pointSize);
m_nativeFontInfo.SetXFontComponent(wxXLFD_POINTSIZE, size);
m_nativeFontInfo.SetFractionalPointSize(pointSize);
}
}
@@ -363,33 +357,13 @@ void wxFontRefData::SetStyle(wxFontStyle style)
}
}
void wxFontRefData::SetWeight(wxFontWeight weight)
void wxFontRefData::SetNumericWeight(int weight)
{
m_weight = weight;
if ( HasNativeFont() )
{
wxString boldness;
switch ( weight )
{
case wxFONTWEIGHT_BOLD:
boldness = wxT("bold");
break;
case wxFONTWEIGHT_LIGHT:
boldness = wxT("light");
break;
default:
wxFAIL_MSG( wxT("unknown font weight") );
// fall through
case wxFONTWEIGHT_NORMAL:
// unspecified
boldness = wxT("medium");
}
m_nativeFontInfo.SetXFontComponent(wxXLFD_WEIGHT, boldness);
m_nativeFontInfo.SetNumericWeight(weight);
}
}
@@ -511,7 +485,7 @@ wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const
// accessors
// ----------------------------------------------------------------------------
int wxFont::GetPointSize() const
float wxFont::GetFractionalPointSize() const
{
wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
@@ -537,7 +511,7 @@ wxFontStyle wxFont::GetStyle() const
return M_FONTDATA->m_style;
}
wxFontWeight wxFont::GetWeight() const
int wxFont::GetNumericWeight() const
{
wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX, wxT("invalid font") );
@@ -594,11 +568,11 @@ bool wxFont::IsFixedWidth() const
// change font attributes
// ----------------------------------------------------------------------------
void wxFont::SetPointSize(int pointSize)
void wxFont::SetFractionalPointSize(float pointSize)
{
Unshare();
M_FONTDATA->SetPointSize(pointSize);
M_FONTDATA->SetFractionalPointSize(pointSize);
}
void wxFont::SetFamily(wxFontFamily family)
@@ -615,11 +589,11 @@ void wxFont::SetStyle(wxFontStyle style)
M_FONTDATA->SetStyle(style);
}
void wxFont::SetWeight(wxFontWeight weight)
void wxFont::SetNumericWeight(int weight)
{
Unshare();
M_FONTDATA->SetWeight(weight);
M_FONTDATA->SetNumericWeight(weight);
}
bool wxFont::SetFaceName(const wxString& faceName)
@@ -692,7 +666,7 @@ GdkFont *wxFont::GetInternalFont( float scale ) const
wxCHECK_MSG( IsOk(), font, wxT("invalid font") );
long int_scale = long(scale * 100.0 + 0.5); // key for fontlist
int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100);
float point_scale = (M_FONTDATA->m_pointSize * 10 * scale) / 100.0;
wxScaledFontList& list = M_FONTDATA->m_scaled_xfonts;
wxScaledFontList::iterator i = list.find(int_scale);

View File

@@ -489,10 +489,10 @@ static wxHashTable *g_fontHash = NULL;
static bool wxTestFontSpec(const wxString& fontspec);
static wxNativeFont wxLoadQueryFont(int pointSize,
static wxNativeFont wxLoadQueryFont(float pointSize,
wxFontFamily family,
wxFontStyle style,
wxFontWeight weight,
int weight,
bool underlined,
const wxString& facename,
const wxString& xregistry,
@@ -671,18 +671,10 @@ wxNativeFontInfo::SetXFontComponent(wxXLFDField field, const wxString& value)
{
wxCHECK_RET( field < wxXLFD_MAX, wxT("invalid XLFD field") );
// this class should be initialized with a valid font spec first and only
// then the fields may be modified!
wxASSERT_MSG( !IsDefault(), wxT("can't modify an uninitialized XLFD") );
if ( !HasElements() )
{
if ( !const_cast<wxNativeFontInfo *>(this)->FromXFontName(xFontName) )
{
wxFAIL_MSG( wxT("can't set font element for invalid XLFD") );
return;
}
for ( int field = 0; field < wxXLFD_MAX; field++ )
fontElements[field] = '*';
}
fontElements[field] = value;
@@ -900,12 +892,24 @@ bool wxNativeFontInfo::SetFaceName(const wxString& facename)
return true;
}
void wxNativeFontInfo::SetFamily(wxFontFamily WXUNUSED(family))
void wxNativeFontInfo::SetFamily(wxFontFamily family)
{
// wxFontFamily -> X foundry, anyone?
wxFAIL_MSG( wxT("not implemented") );
wxString xfamily;
switch (family)
{
case wxFONTFAMILY_DECORATIVE: xfamily = "lucida"; break;
case wxFONTFAMILY_ROMAN: xfamily = "times"; break;
case wxFONTFAMILY_MODERN: xfamily = "courier"; break;
case wxFONTFAMILY_DEFAULT:
case wxFONTFAMILY_SWISS: xfamily = "helvetica"; break;
case wxFONTFAMILY_TELETYPE: xfamily = "lucidatypewriter"; break;
case wxFONTFAMILY_SCRIPT: xfamily = "utopia"; break;
case wxFONTFAMILY_UNKNOWN: break;
}
// SetXFontComponent(wxXLFD_FOUNDRY, ...);
wxCHECK_RET( !xfamily.empty(), "Unknown wxFontFamily" );
SetXFontComponent(wxXLFD_FAMILY, xfamily);
}
void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding)
@@ -1025,10 +1029,10 @@ bool wxTestFontEncoding(const wxNativeEncodingInfo& info)
// X-specific functions
// ----------------------------------------------------------------------------
wxNativeFont wxLoadQueryNearestFont(int pointSize,
wxNativeFont wxLoadQueryNearestFont(float pointSize,
wxFontFamily family,
wxFontStyle style,
wxFontWeight weight,
int weight,
bool underlined,
const wxString &facename,
wxFontEncoding encoding,
@@ -1105,7 +1109,7 @@ wxNativeFont wxLoadQueryNearestFont(int pointSize,
// first round: search for equal, then for smaller and for larger size
// with the given weight and style
wxFontWeight testweight = weight;
int testweight = weight;
wxFontStyle teststyle = style;
for ( round = 0; round < 3; round++ )
@@ -1248,26 +1252,16 @@ static bool wxTestFontSpec(const wxString& fontspec)
}
}
static wxNativeFont wxLoadQueryFont(int pointSize,
static wxNativeFont wxLoadQueryFont(float pointSize,
wxFontFamily family,
wxFontStyle style,
wxFontWeight weight,
int weight,
bool WXUNUSED(underlined),
const wxString& facename,
const wxString& xregistry,
const wxString& xencoding,
wxString* xFontName)
{
wxString xfamily("*");
switch (family)
{
case wxFONTFAMILY_DECORATIVE: xfamily = wxT("lucida"); break;
case wxFONTFAMILY_ROMAN: xfamily = wxT("times"); break;
case wxFONTFAMILY_MODERN: xfamily = wxT("courier"); break;
case wxFONTFAMILY_SWISS: xfamily = wxT("helvetica"); break;
case wxFONTFAMILY_TELETYPE: xfamily = wxT("lucidatypewriter"); break;
case wxFONTFAMILY_SCRIPT: xfamily = wxT("utopia"); break;
}
#if wxUSE_NANOX
int xweight;
switch (weight)
@@ -1339,176 +1333,43 @@ static wxNativeFont wxLoadQueryFont(int pointSize,
return (wxNativeFont) fontInfo;
#else
wxString fontSpec;
wxNativeFontInfo info;
info.SetFractionalPointSize(pointSize);
if ( !facename.empty() )
{
fontSpec.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
facename.c_str());
if ( wxTestFontSpec(fontSpec) )
info.SetFaceName(facename);
if ( !wxTestFontSpec(info.GetXFontName()) )
{
xfamily = facename;
// No such face name, use just the family (we assume this will
// never fail).
info.SetFamily(family);
}
//else: no such family, use default one instead
}
wxString xstyle;
switch (style)
{
case wxFONTSTYLE_SLANT:
fontSpec.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
xfamily.c_str());
if ( wxTestFontSpec(fontSpec) )
{
xstyle = wxT("o");
break;
}
// fall through - try wxFONTSTYLE_ITALIC now
case wxFONTSTYLE_ITALIC:
fontSpec.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
xfamily.c_str());
if ( wxTestFontSpec(fontSpec) )
{
xstyle = wxT("i");
}
else if ( style == wxFONTSTYLE_ITALIC ) // and not wxFONTSTYLE_SLANT
{
// try wxFONTSTYLE_SLANT
fontSpec.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
xfamily.c_str());
if ( wxTestFontSpec(fontSpec) )
{
xstyle = wxT("o");
}
else
{
// no italic, no slant - leave default
xstyle = wxT("*");
}
}
break;
default:
wxFAIL_MSG(wxT("unknown font style"));
// fall back to normal
case wxFONTSTYLE_NORMAL:
xstyle = wxT("r");
break;
info.SetFamily(family);
}
wxString xweight;
switch (weight)
{
case wxFONTWEIGHT_BOLD:
{
fontSpec.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
xfamily.c_str());
if ( wxTestFontSpec(fontSpec) )
{
xweight = wxT("bold");
break;
}
fontSpec.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
xfamily.c_str());
if ( wxTestFontSpec(fontSpec) )
{
xweight = wxT("heavy");
break;
}
fontSpec.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
xfamily.c_str());
if ( wxTestFontSpec(fontSpec) )
{
xweight = wxT("extrabold");
break;
}
fontSpec.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
xfamily.c_str());
if ( wxTestFontSpec(fontSpec) )
{
xweight = wxT("demibold");
break;
}
fontSpec.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
xfamily.c_str());
if ( wxTestFontSpec(fontSpec) )
{
xweight = wxT("black");
break;
}
fontSpec.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
xfamily.c_str());
if ( wxTestFontSpec(fontSpec) )
{
xweight = wxT("ultrablack");
break;
}
}
break;
case wxFONTWEIGHT_LIGHT:
{
fontSpec.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
xfamily.c_str());
if ( wxTestFontSpec(fontSpec) )
{
xweight = wxT("light");
break;
}
fontSpec.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
xfamily.c_str());
if ( wxTestFontSpec(fontSpec) )
{
xweight = wxT("thin");
break;
}
}
break;
case wxFONTWEIGHT_NORMAL:
{
fontSpec.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
xfamily.c_str());
if ( wxTestFontSpec(fontSpec) )
{
xweight = wxT("medium");
break;
}
fontSpec.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
xfamily.c_str());
if ( wxTestFontSpec(fontSpec) )
{
xweight = wxT("normal");
break;
}
fontSpec.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
xfamily.c_str());
if ( wxTestFontSpec(fontSpec) )
{
xweight = wxT("regular");
break;
}
xweight = wxT("*");
}
break;
default: xweight = wxT("*"); break;
}
wxNativeFontInfo infoWithStyle(info);
infoWithStyle.SetStyle(style);
if ( wxTestFontSpec(infoWithStyle.GetXFontName()) )
info = infoWithStyle;
// if pointSize is -1, don't specify any
wxString sizeSpec;
if ( pointSize == -1 )
{
sizeSpec = wxT('*');
}
else
{
sizeSpec.Printf(wxT("%d"), pointSize);
}
wxNativeFontInfo infoWithWeight(info);
infoWithWeight.SetNumericWeight(weight);
if ( wxTestFontSpec(infoWithWeight.GetXFontName()) )
info = infoWithWeight;
// construct the X font spec from our data
fontSpec.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
xfamily.c_str(), xweight.c_str(), xstyle.c_str(),
sizeSpec.c_str(), xregistry.c_str(), xencoding.c_str());
wxString fontSpec;
fontSpec.Printf("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s",
info.GetXFontComponent(wxXLFD_FAMILY),
info.GetXFontComponent(wxXLFD_WEIGHT),
info.GetXFontComponent(wxXLFD_SLANT),
info.GetXFontComponent(wxXLFD_POINTSIZE),
xregistry,
xencoding);
if( xFontName )
*xFontName = fontSpec;