1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/font.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling and Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
25 #include "wx/settings.h"
26 #include "wx/cmndata.h"
27 #include "wx/gdicmn.h"
30 #include "wx/fontutil.h"
32 #include "wx/tokenzr.h"
36 #include "wx/gtk1/private.h"
37 #include <gdk/gdkprivate.h>
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // the default size (in points) for the fonts
44 static const int wxDEFAULT_FONT_SIZE
= 12;
46 // ----------------------------------------------------------------------------
47 // wxScaledFontList: maps the font sizes to the GDK fonts for the given font
48 // ----------------------------------------------------------------------------
50 WX_DECLARE_HASH_MAP(int, GdkFont
*, wxIntegerHash
, wxIntegerEqual
,
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 class wxFontRefData
: public wxGDIRefData
60 // from broken down font parameters, also default ctor
61 wxFontRefData(int size
= -1,
62 wxFontFamily family
= wxFONTFAMILY_DEFAULT
,
63 wxFontStyle style
= wxFONTSTYLE_NORMAL
,
64 wxFontWeight weight
= wxFONTWEIGHT_NORMAL
,
65 bool underlined
= false,
66 const wxString
& faceName
= wxEmptyString
,
67 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
70 wxFontRefData(const wxString
& fontname
);
73 wxFontRefData( const wxFontRefData
& data
);
75 virtual ~wxFontRefData();
77 // do we have the native font info?
78 bool HasNativeFont() const
80 // only use m_nativeFontInfo if it had been initialized
81 return !m_nativeFontInfo
.IsDefault();
84 // setters: all of them also take care to modify m_nativeFontInfo if we
85 // have it so as to not lose the information not carried by our fields
86 void SetPointSize(int pointSize
);
87 void SetFamily(wxFontFamily family
);
88 void SetStyle(wxFontStyle style
);
89 void SetWeight(wxFontWeight weight
);
90 void SetUnderlined(bool underlined
);
91 bool SetFaceName(const wxString
& facename
);
92 void SetEncoding(wxFontEncoding encoding
);
94 void SetNoAntiAliasing( bool no
= true ) { m_noAA
= no
; }
95 bool GetNoAntiAliasing() const { return m_noAA
; }
97 // and this one also modifies all the other font data fields
98 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
101 // common part of all ctors
102 void Init(int pointSize
,
107 const wxString
& faceName
,
108 wxFontEncoding encoding
);
110 // set all fields from (already initialized and valid) m_nativeFontInfo
111 void InitFromNative();
114 // clear m_scaled_xfonts if any
115 void ClearGdkFonts();
117 // the map of font sizes to "GdkFont *"
118 wxScaledFontList m_scaled_xfonts
;
121 wxFontFamily m_family
;
123 wxFontWeight m_weight
;
126 wxFontEncoding m_encoding
; // Unused under GTK 2.0
127 bool m_noAA
; // No anti-aliasing
129 // The native font info, basicly an XFLD under GTK 1.2 and
130 // the pango font description under GTK 2.0.
131 wxNativeFontInfo m_nativeFontInfo
;
136 #define M_FONTDATA ((wxFontRefData*)m_refData)
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 void wxFontRefData::Init(int pointSize
,
147 const wxString
& faceName
,
148 wxFontEncoding encoding
)
150 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
152 m_faceName
= faceName
;
154 // we accept both wxDEFAULT and wxNORMAL here - should we?
155 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
156 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
158 // and here, do we really want to forbid creation of the font of the size
159 // 90 (the value of wxDEFAULT)??
160 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
161 ? wxDEFAULT_FONT_SIZE
164 m_underlined
= underlined
;
165 m_encoding
= encoding
;
170 void wxFontRefData::InitFromNative()
174 // get the font parameters from the XLFD
175 // -------------------------------------
177 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
179 m_weight
= wxFONTWEIGHT_NORMAL
;
181 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
182 if ( !w
.empty() && w
!= _T('*') )
184 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
186 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
187 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
188 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
190 m_weight
= wxFONTWEIGHT_BOLD
;
192 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
194 m_weight
= wxFONTWEIGHT_LIGHT
;
198 switch ( wxToupper(m_nativeFontInfo
.
199 GetXFontComponent(wxXLFD_SLANT
)[0u]).GetValue() )
201 case _T('I'): // italique
202 m_style
= wxFONTSTYLE_ITALIC
;
205 case _T('O'): // oblique
206 m_style
= wxFONTSTYLE_SLANT
;
210 m_style
= wxFONTSTYLE_NORMAL
;
214 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
216 // size in XLFD is in 10 point units
217 m_pointSize
= (int)(ptSize
/ 10);
221 m_pointSize
= wxDEFAULT_FONT_SIZE
;
224 // examine the spacing: if the font is monospaced, assume wxTELETYPE
225 // family for compatibility with the old code which used it instead of
227 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
229 m_family
= wxFONTFAMILY_TELETYPE
;
231 else // not monospaceed
233 // don't even try guessing it, it doesn't work for too many fonts
235 m_family
= wxFONTFAMILY_UNKNOWN
;
238 // X fonts are never underlined...
239 m_underlined
= false;
241 // deal with font encoding
243 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
244 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
246 if ( registry
== _T("ISO8859") )
249 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
251 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
254 else if ( registry
== _T("MICROSOFT") )
257 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
259 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
262 else if ( registry
== _T("KOI8") )
264 m_encoding
= wxFONTENCODING_KOI8
;
266 else // unknown encoding
268 // may be give a warning here? or use wxFontMapper?
269 m_encoding
= wxFONTENCODING_SYSTEM
;
273 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
276 m_pointSize
= data
.m_pointSize
;
277 m_family
= data
.m_family
;
278 m_style
= data
.m_style
;
279 m_weight
= data
.m_weight
;
281 m_underlined
= data
.m_underlined
;
283 m_faceName
= data
.m_faceName
;
284 m_encoding
= data
.m_encoding
;
286 m_noAA
= data
.m_noAA
;
288 // Forces a copy of the internal data. wxNativeFontInfo should probably
289 // have a copy ctor and assignment operator to fix this properly but that
290 // would break binary compatibility...
291 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
294 wxFontRefData::wxFontRefData(int size
, wxFontFamily family
, wxFontStyle style
,
295 wxFontWeight weight
, bool underlined
,
296 const wxString
& faceName
,
297 wxFontEncoding encoding
)
299 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
302 wxFontRefData::wxFontRefData(const wxString
& fontname
)
304 // FromString() should really work in GTK1 too, doesn't it?
305 m_nativeFontInfo
.SetXFontName(fontname
);
310 void wxFontRefData::ClearGdkFonts()
312 for ( wxScaledFontList::iterator i
= m_scaled_xfonts
.begin();
313 i
!= m_scaled_xfonts
.end();
316 GdkFont
*font
= i
->second
;
317 gdk_font_unref( font
);
320 m_scaled_xfonts
.clear();
323 wxFontRefData::~wxFontRefData()
328 // ----------------------------------------------------------------------------
329 // wxFontRefData SetXXX()
330 // ----------------------------------------------------------------------------
332 void wxFontRefData::SetPointSize(int pointSize
)
334 m_pointSize
= pointSize
;
336 if ( HasNativeFont() )
339 if ( pointSize
== -1 )
342 size
.Printf(_T("%d"), 10*pointSize
);
344 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
348 void wxFontRefData::SetFamily(wxFontFamily family
)
352 // TODO: what are we supposed to do with m_nativeFontInfo here?
355 void wxFontRefData::SetStyle(wxFontStyle style
)
359 if ( HasNativeFont() )
364 case wxFONTSTYLE_ITALIC
:
368 case wxFONTSTYLE_SLANT
:
373 wxFAIL_MSG( _T("unknown font style") );
376 case wxFONTSTYLE_NORMAL
:
380 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
384 void wxFontRefData::SetWeight(wxFontWeight weight
)
388 if ( HasNativeFont() )
393 case wxFONTWEIGHT_BOLD
:
394 boldness
= _T("bold");
397 case wxFONTWEIGHT_LIGHT
:
398 boldness
= _T("light");
402 wxFAIL_MSG( _T("unknown font weight") );
405 case wxFONTWEIGHT_NORMAL
:
407 boldness
= _T("medium");
410 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
414 void wxFontRefData::SetUnderlined(bool underlined
)
416 m_underlined
= underlined
;
418 // the XLFD doesn't have "underlined" field anyhow
421 bool wxFontRefData::SetFaceName(const wxString
& facename
)
423 m_faceName
= facename
;
425 if ( HasNativeFont() )
427 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
433 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
435 m_encoding
= encoding
;
437 if ( HasNativeFont() )
439 wxNativeEncodingInfo info
;
440 if ( wxGetNativeFontEncoding(encoding
, &info
) )
442 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
443 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
448 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
450 // previously cached fonts shouldn't be used
453 m_nativeFontInfo
= info
;
455 // set all the other font parameters from the native font info
459 // ----------------------------------------------------------------------------
461 // ----------------------------------------------------------------------------
463 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
465 wxFont::wxFont(const wxNativeFontInfo
& info
)
467 (void) Create(info
.GetXFontName());
470 bool wxFont::Create( int pointSize
,
475 const wxString
& face
,
476 wxFontEncoding encoding
)
480 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
481 underlined
, face
, encoding
);
486 bool wxFont::Create(const wxString
& fontname
)
488 // VZ: does this really happen?
489 if ( fontname
.empty() )
491 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
496 m_refData
= new wxFontRefData(fontname
);
501 void wxFont::Unshare()
505 m_refData
= new wxFontRefData();
509 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
519 wxGDIRefData
*wxFont::CreateGDIRefData() const
521 return new wxFontRefData
;
524 wxGDIRefData
*wxFont::CloneGDIRefData(const wxGDIRefData
*data
) const
526 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));
529 // ----------------------------------------------------------------------------
531 // ----------------------------------------------------------------------------
533 int wxFont::GetPointSize() const
535 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
537 return M_FONTDATA
->m_pointSize
;
540 wxString
wxFont::GetFaceName() const
542 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
544 return M_FONTDATA
->m_faceName
;
547 wxFontFamily
wxFont::GetFamily() const
549 wxCHECK_MSG( Ok(), wxFONTFAMILY_MAX
, wxT("invalid font") );
551 return M_FONTDATA
->m_family
;
554 wxFontStyle
wxFont::GetStyle() const
556 wxCHECK_MSG( Ok(), wxFONTSTYLE_MAX
, wxT("invalid font") );
558 return M_FONTDATA
->m_style
;
561 wxFontWeight
wxFont::GetWeight() const
563 wxCHECK_MSG( Ok(), wxFONTWEIGHT_MAX
, wxT("invalid font") );
565 return M_FONTDATA
->m_weight
;
568 bool wxFont::GetUnderlined() const
570 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
572 return M_FONTDATA
->m_underlined
;
575 wxFontEncoding
wxFont::GetEncoding() const
577 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
579 // m_encoding is unused in wxGTK2, return encoding that the user set.
580 return M_FONTDATA
->m_encoding
;
583 bool wxFont::GetNoAntiAliasing() const
585 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
587 return M_FONTDATA
->m_noAA
;
590 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
592 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
594 if ( !M_FONTDATA
->HasNativeFont() )
596 // NB: this call has important side-effect: it not only finds
597 // GdkFont representation, it also initializes m_nativeFontInfo
598 // by calling its SetXFontName method
602 return &(M_FONTDATA
->m_nativeFontInfo
);
605 bool wxFont::IsFixedWidth() const
607 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
609 if ( M_FONTDATA
->HasNativeFont() )
611 // the monospace fonts are supposed to have "M" in the spacing field
612 wxString spacing
= M_FONTDATA
->
613 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
615 return spacing
.Upper() == _T('M');
618 return wxFontBase::IsFixedWidth();
621 // ----------------------------------------------------------------------------
622 // change font attributes
623 // ----------------------------------------------------------------------------
625 void wxFont::SetPointSize(int pointSize
)
629 M_FONTDATA
->SetPointSize(pointSize
);
632 void wxFont::SetFamily(wxFontFamily family
)
636 M_FONTDATA
->SetFamily(family
);
639 void wxFont::SetStyle(wxFontStyle style
)
643 M_FONTDATA
->SetStyle(style
);
646 void wxFont::SetWeight(wxFontWeight weight
)
650 M_FONTDATA
->SetWeight(weight
);
653 bool wxFont::SetFaceName(const wxString
& faceName
)
657 return M_FONTDATA
->SetFaceName(faceName
) &&
658 wxFontBase::SetFaceName(faceName
);
661 void wxFont::SetUnderlined(bool underlined
)
665 M_FONTDATA
->SetUnderlined(underlined
);
668 void wxFont::SetEncoding(wxFontEncoding encoding
)
672 M_FONTDATA
->SetEncoding(encoding
);
675 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
679 M_FONTDATA
->SetNativeFontInfo( info
);
682 void wxFont::SetNoAntiAliasing( bool no
)
686 M_FONTDATA
->SetNoAntiAliasing( no
);
689 // ----------------------------------------------------------------------------
690 // get internal representation of font
691 // ----------------------------------------------------------------------------
693 static GdkFont
*g_systemDefaultGuiFont
= NULL
;
695 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
696 extern GdkFont
*GtkGetDefaultGuiFont()
698 if (!g_systemDefaultGuiFont
)
700 GtkWidget
*widget
= gtk_button_new();
701 GtkStyle
*def
= gtk_rc_get_style( widget
);
704 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
708 def
= gtk_widget_get_default_style();
710 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
712 gtk_widget_destroy( widget
);
716 // already have it, but ref it once more before returning
717 gdk_font_ref(g_systemDefaultGuiFont
);
720 return g_systemDefaultGuiFont
;
723 GdkFont
*wxFont::GetInternalFont( float scale
) const
725 GdkFont
*font
= NULL
;
727 wxCHECK_MSG( Ok(), font
, wxT("invalid font") );
729 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
730 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
732 wxScaledFontList
& list
= M_FONTDATA
->m_scaled_xfonts
;
733 wxScaledFontList::iterator i
= list
.find(int_scale
);
734 if ( i
!= list
.end() )
738 else // we don't have this font in this size yet
740 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
742 font
= GtkGetDefaultGuiFont();
747 // do we have the XLFD?
748 if ( int_scale
== 100 && M_FONTDATA
->HasNativeFont() )
750 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
753 // no XLFD of no exact match - try the approximate one now
757 font
= wxLoadQueryNearestFont( point_scale
,
758 M_FONTDATA
->m_family
,
760 M_FONTDATA
->m_weight
,
761 M_FONTDATA
->m_underlined
,
762 M_FONTDATA
->m_faceName
,
763 M_FONTDATA
->m_encoding
,
765 // NB: wxFont::GetNativeFontInfo relies on this
766 // side-effect of GetInternalFont
767 if ( int_scale
== 100 )
768 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(xfontname
);
774 list
[int_scale
] = font
;
778 // it's quite useless to make it a wxCHECK because we're going to crash
780 wxASSERT_MSG( font
, wxT("could not load any font?") );