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(); virtual ~wxFont();
// implement base class pure virtuals // implement base class pure virtuals
virtual int GetPointSize() const; virtual float GetFractionalPointSize() const;
virtual wxFontStyle GetStyle() const; virtual wxFontStyle GetStyle() const;
virtual wxFontWeight GetWeight() const; virtual int GetNumericWeight() const;
virtual wxString GetFaceName() const; virtual wxString GetFaceName() const;
virtual bool GetUnderlined() const; virtual bool GetUnderlined() const;
virtual wxFontEncoding GetEncoding() const; virtual wxFontEncoding GetEncoding() const;
virtual const wxNativeFontInfo *GetNativeFontInfo() const; virtual const wxNativeFontInfo *GetNativeFontInfo() const;
virtual bool IsFixedWidth() const; virtual bool IsFixedWidth() const;
virtual void SetPointSize( int pointSize ); virtual void SetFractionalPointSize(float pointSize);
virtual void SetFamily(wxFontFamily family); virtual void SetFamily(wxFontFamily family);
virtual void SetStyle(wxFontStyle style); virtual void SetStyle(wxFontStyle style);
virtual void SetWeight(wxFontWeight weight); virtual void SetNumericWeight(int weight);
virtual bool SetFaceName( const wxString& faceName ); virtual bool SetFaceName( const wxString& faceName );
virtual void SetUnderlined( bool underlined ); virtual void SetUnderlined( bool underlined );
virtual void SetEncoding(wxFontEncoding encoding); virtual void SetEncoding(wxFontEncoding encoding);

View File

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

View File

@@ -77,10 +77,10 @@ public:
// setters: all of them also take care to modify m_nativeFontInfo if we // 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 // 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 SetFamily(wxFontFamily family);
void SetStyle(wxFontStyle style); void SetStyle(wxFontStyle style);
void SetWeight(wxFontWeight weight); void SetNumericWeight(int weight);
void SetUnderlined(bool underlined); void SetUnderlined(bool underlined);
bool SetFaceName(const wxString& facename); bool SetFaceName(const wxString& facename);
void SetEncoding(wxFontEncoding encoding); void SetEncoding(wxFontEncoding encoding);
@@ -90,10 +90,10 @@ public:
protected: protected:
// common part of all ctors // common part of all ctors
void Init(int pointSize, void Init(float pointSize,
wxFontFamily family, wxFontFamily family,
wxFontStyle style, wxFontStyle style,
wxFontWeight weight, int weight,
bool underlined, bool underlined,
const wxString& faceName, const wxString& faceName,
wxFontEncoding encoding); wxFontEncoding encoding);
@@ -108,10 +108,10 @@ private:
// the map of font sizes to "GdkFont *" // the map of font sizes to "GdkFont *"
wxScaledFontList m_scaled_xfonts; wxScaledFontList m_scaled_xfonts;
int m_pointSize; float m_pointSize;
wxFontFamily m_family; wxFontFamily m_family;
wxFontStyle m_style; wxFontStyle m_style;
wxFontWeight m_weight; int m_weight;
bool m_underlined; bool m_underlined;
wxString m_faceName; wxString m_faceName;
wxFontEncoding m_encoding; // Unused under GTK 2.0 wxFontEncoding m_encoding; // Unused under GTK 2.0
@@ -129,10 +129,10 @@ private:
// wxFontRefData // wxFontRefData
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void wxFontRefData::Init(int pointSize, void wxFontRefData::Init(float pointSize,
wxFontFamily family, wxFontFamily family,
wxFontStyle style, wxFontStyle style,
wxFontWeight weight, int weight,
bool underlined, bool underlined,
const wxString& faceName, const wxString& faceName,
wxFontEncoding encoding) wxFontEncoding encoding)
@@ -144,7 +144,7 @@ void wxFontRefData::Init(int pointSize,
m_style = style; m_style = style;
m_weight = weight; m_weight = weight;
m_pointSize = pointSize == -1 ? wxDEFAULT_FONT_SIZE : pointSize; m_pointSize = pointSize < 0 ? wxDEFAULT_FONT_SIZE : pointSize;
m_underlined = underlined; m_underlined = underlined;
m_encoding = encoding; m_encoding = encoding;
@@ -195,7 +195,7 @@ void wxFontRefData::InitFromNative()
if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_POINTSIZE).ToLong(&ptSize) ) if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_POINTSIZE).ToLong(&ptSize) )
{ {
// size in XLFD is in 10 point units // size in XLFD is in 10 point units
m_pointSize = (int)(ptSize / 10); m_pointSize = ptSize / 10;
} }
else else
{ {
@@ -272,10 +272,10 @@ wxFontRefData::wxFontRefData( const wxFontRefData& data )
wxFontRefData::wxFontRefData(const wxFontInfo& info) wxFontRefData::wxFontRefData(const wxFontInfo& info)
{ {
Init(info.GetPointSize(), Init(info.GetFractionalPointSize(),
info.GetFamily(), info.GetFamily(),
info.GetStyle(), info.GetStyle(),
info.GetWeight(), info.GetNumericWeight(),
info.IsUnderlined(), info.IsUnderlined(),
info.GetFaceName(), info.GetFaceName(),
info.GetEncoding()); info.GetEncoding());
@@ -311,19 +311,13 @@ wxFontRefData::~wxFontRefData()
// wxFontRefData SetXXX() // wxFontRefData SetXXX()
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void wxFontRefData::SetPointSize(int pointSize) void wxFontRefData::SetFractionalPointSize(float pointSize)
{ {
m_pointSize = pointSize; m_pointSize = pointSize;
if ( HasNativeFont() ) if ( HasNativeFont() )
{ {
wxString size; m_nativeFontInfo.SetFractionalPointSize(pointSize);
if ( pointSize == -1 )
size = wxT('*');
else
size.Printf(wxT("%d"), 10*pointSize);
m_nativeFontInfo.SetXFontComponent(wxXLFD_POINTSIZE, size);
} }
} }
@@ -363,33 +357,13 @@ void wxFontRefData::SetStyle(wxFontStyle style)
} }
} }
void wxFontRefData::SetWeight(wxFontWeight weight) void wxFontRefData::SetNumericWeight(int weight)
{ {
m_weight = weight; m_weight = weight;
if ( HasNativeFont() ) if ( HasNativeFont() )
{ {
wxString boldness; m_nativeFontInfo.SetNumericWeight(weight);
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);
} }
} }
@@ -511,7 +485,7 @@ wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const
// accessors // accessors
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
int wxFont::GetPointSize() const float wxFont::GetFractionalPointSize() const
{ {
wxCHECK_MSG( IsOk(), 0, wxT("invalid font") ); wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
@@ -537,7 +511,7 @@ wxFontStyle wxFont::GetStyle() const
return M_FONTDATA->m_style; return M_FONTDATA->m_style;
} }
wxFontWeight wxFont::GetWeight() const int wxFont::GetNumericWeight() const
{ {
wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX, wxT("invalid font") ); wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX, wxT("invalid font") );
@@ -594,11 +568,11 @@ bool wxFont::IsFixedWidth() const
// change font attributes // change font attributes
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void wxFont::SetPointSize(int pointSize) void wxFont::SetFractionalPointSize(float pointSize)
{ {
Unshare(); Unshare();
M_FONTDATA->SetPointSize(pointSize); M_FONTDATA->SetFractionalPointSize(pointSize);
} }
void wxFont::SetFamily(wxFontFamily family) void wxFont::SetFamily(wxFontFamily family)
@@ -615,11 +589,11 @@ void wxFont::SetStyle(wxFontStyle style)
M_FONTDATA->SetStyle(style); M_FONTDATA->SetStyle(style);
} }
void wxFont::SetWeight(wxFontWeight weight) void wxFont::SetNumericWeight(int weight)
{ {
Unshare(); Unshare();
M_FONTDATA->SetWeight(weight); M_FONTDATA->SetNumericWeight(weight);
} }
bool wxFont::SetFaceName(const wxString& faceName) bool wxFont::SetFaceName(const wxString& faceName)
@@ -692,7 +666,7 @@ GdkFont *wxFont::GetInternalFont( float scale ) const
wxCHECK_MSG( IsOk(), font, wxT("invalid font") ); wxCHECK_MSG( IsOk(), font, wxT("invalid font") );
long int_scale = long(scale * 100.0 + 0.5); // key for fontlist 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& list = M_FONTDATA->m_scaled_xfonts;
wxScaledFontList::iterator i = list.find(int_scale); 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 bool wxTestFontSpec(const wxString& fontspec);
static wxNativeFont wxLoadQueryFont(int pointSize, static wxNativeFont wxLoadQueryFont(float pointSize,
wxFontFamily family, wxFontFamily family,
wxFontStyle style, wxFontStyle style,
wxFontWeight weight, int weight,
bool underlined, bool underlined,
const wxString& facename, const wxString& facename,
const wxString& xregistry, const wxString& xregistry,
@@ -671,18 +671,10 @@ wxNativeFontInfo::SetXFontComponent(wxXLFDField field, const wxString& value)
{ {
wxCHECK_RET( field < wxXLFD_MAX, wxT("invalid XLFD field") ); 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 ( !HasElements() )
{ {
if ( !const_cast<wxNativeFontInfo *>(this)->FromXFontName(xFontName) ) for ( int field = 0; field < wxXLFD_MAX; field++ )
{ fontElements[field] = '*';
wxFAIL_MSG( wxT("can't set font element for invalid XLFD") );
return;
}
} }
fontElements[field] = value; fontElements[field] = value;
@@ -900,12 +892,24 @@ bool wxNativeFontInfo::SetFaceName(const wxString& facename)
return true; return true;
} }
void wxNativeFontInfo::SetFamily(wxFontFamily WXUNUSED(family)) void wxNativeFontInfo::SetFamily(wxFontFamily family)
{ {
// wxFontFamily -> X foundry, anyone? wxString xfamily;
wxFAIL_MSG( wxT("not implemented") ); 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) void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding)
@@ -1025,10 +1029,10 @@ bool wxTestFontEncoding(const wxNativeEncodingInfo& info)
// X-specific functions // X-specific functions
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
wxNativeFont wxLoadQueryNearestFont(int pointSize, wxNativeFont wxLoadQueryNearestFont(float pointSize,
wxFontFamily family, wxFontFamily family,
wxFontStyle style, wxFontStyle style,
wxFontWeight weight, int weight,
bool underlined, bool underlined,
const wxString &facename, const wxString &facename,
wxFontEncoding encoding, wxFontEncoding encoding,
@@ -1105,7 +1109,7 @@ wxNativeFont wxLoadQueryNearestFont(int pointSize,
// first round: search for equal, then for smaller and for larger size // first round: search for equal, then for smaller and for larger size
// with the given weight and style // with the given weight and style
wxFontWeight testweight = weight; int testweight = weight;
wxFontStyle teststyle = style; wxFontStyle teststyle = style;
for ( round = 0; round < 3; round++ ) 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, wxFontFamily family,
wxFontStyle style, wxFontStyle style,
wxFontWeight weight, int weight,
bool WXUNUSED(underlined), bool WXUNUSED(underlined),
const wxString& facename, const wxString& facename,
const wxString& xregistry, const wxString& xregistry,
const wxString& xencoding, const wxString& xencoding,
wxString* xFontName) 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 #if wxUSE_NANOX
int xweight; int xweight;
switch (weight) switch (weight)
@@ -1339,176 +1333,43 @@ static wxNativeFont wxLoadQueryFont(int pointSize,
return (wxNativeFont) fontInfo; return (wxNativeFont) fontInfo;
#else #else
wxString fontSpec; wxNativeFontInfo info;
if (!facename.empty()) info.SetFractionalPointSize(pointSize);
{
fontSpec.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
facename.c_str());
if ( wxTestFontSpec(fontSpec) ) if ( !facename.empty() )
{ {
xfamily = facename; info.SetFaceName(facename);
if ( !wxTestFontSpec(info.GetXFontName()) )
{
// 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 else
{ {
// no italic, no slant - leave default info.SetFamily(family);
xstyle = wxT("*");
}
}
break;
default:
wxFAIL_MSG(wxT("unknown font style"));
// fall back to normal
case wxFONTSTYLE_NORMAL:
xstyle = wxT("r");
break;
} }
wxString xweight; wxNativeFontInfo infoWithStyle(info);
switch (weight) infoWithStyle.SetStyle(style);
{ if ( wxTestFontSpec(infoWithStyle.GetXFontName()) )
case wxFONTWEIGHT_BOLD: info = infoWithStyle;
{
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;
}
// if pointSize is -1, don't specify any wxNativeFontInfo infoWithWeight(info);
wxString sizeSpec; infoWithWeight.SetNumericWeight(weight);
if ( pointSize == -1 ) if ( wxTestFontSpec(infoWithWeight.GetXFontName()) )
{ info = infoWithWeight;
sizeSpec = wxT('*');
}
else
{
sizeSpec.Printf(wxT("%d"), pointSize);
}
// construct the X font spec from our data // construct the X font spec from our data
fontSpec.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"), wxString fontSpec;
xfamily.c_str(), xweight.c_str(), xstyle.c_str(), fontSpec.Printf("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s",
sizeSpec.c_str(), xregistry.c_str(), xencoding.c_str()); info.GetXFontComponent(wxXLFD_FAMILY),
info.GetXFontComponent(wxXLFD_WEIGHT),
info.GetXFontComponent(wxXLFD_SLANT),
info.GetXFontComponent(wxXLFD_POINTSIZE),
xregistry,
xencoding);
if( xFontName ) if( xFontName )
*xFontName = fontSpec; *xFontName = fontSpec;