1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
19 #pragma implementation "font.h"
23 #include "wx/fontutil.h"
24 #include "wx/cmndata.h"
27 #include "wx/gdicmn.h"
28 #include "wx/tokenzr.h"
29 #include "wx/settings.h"
33 #include "wx/gtk/private.h"
34 #include <gdk/gdkprivate.h>
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // the default size (in points) for the fonts
41 static const int wxDEFAULT_FONT_SIZE
= 12;
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // TODO: replace this with a type safe list or hash!!
48 class wxScaledFontList
: public wxList
51 wxScaledFontList() : wxList(wxKEY_INTEGER
) { }
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 class wxFontRefData
: public wxObjectRefData
61 // from broken down font parameters, also default ctor
62 wxFontRefData(int size
= -1,
63 int family
= wxFONTFAMILY_DEFAULT
,
64 int style
= wxFONTSTYLE_NORMAL
,
65 int weight
= wxFONTWEIGHT_NORMAL
,
66 bool underlined
= FALSE
,
67 const wxString
& faceName
= wxEmptyString
,
68 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
71 wxFontRefData(const wxString
& fontname
);
74 wxFontRefData( const wxFontRefData
& data
);
76 virtual ~wxFontRefData();
78 // do we have the native font info?
79 bool HasNativeFont() const
84 return !m_nativeFontInfo
.IsDefault();
88 // setters: all of them also take care to modify m_nativeFontInfo if we
89 // have it so as to not lose the information not carried by our fields
90 void SetPointSize(int pointSize
);
91 void SetFamily(int family
);
92 void SetStyle(int style
);
93 void SetWeight(int weight
);
94 void SetUnderlined(bool underlined
);
95 void SetFaceName(const wxString
& facename
);
96 void SetEncoding(wxFontEncoding encoding
);
98 // debugger helper: shows what the font really is
100 // VZ: I need this as my gdb either shows wildly wrong values or crashes
101 // when I ask it to "p fontRefData" :-(
102 #if defined(__WXDEBUG__) && !defined(__WXGTK20__)
105 wxPrintf(_T("%s-%s-%s-%d-%d\n"),
107 m_weight
== wxFONTWEIGHT_NORMAL
109 : m_weight
== wxFONTWEIGHT_BOLD
112 m_style
== wxFONTSTYLE_NORMAL
? _T("regular") : _T("italic"),
119 // common part of all ctors
120 void Init(int pointSize
,
125 const wxString
& faceName
,
126 wxFontEncoding encoding
);
130 // the map of font sizes to "GdkFont *"
131 wxScaledFontList m_scaled_xfonts
;
134 // the broken down font parameters
141 wxFontEncoding m_encoding
; // Unused under GTK 2.0
143 // The native font info, basicly an XFLD under GTK 1.2 and
144 // the pango font description under GTK 2.0.
145 wxNativeFontInfo m_nativeFontInfo
;
150 // ============================================================================
151 // wxFontRefData implementation
152 // ============================================================================
154 // ----------------------------------------------------------------------------
155 // wxFontRefData creation
156 // ----------------------------------------------------------------------------
158 void wxFontRefData::Init(int pointSize
,
163 const wxString
& faceName
,
164 wxFontEncoding encoding
)
166 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
168 m_faceName
= faceName
;
170 // we accept both wxDEFAULT and wxNORMAL here - should we?
171 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
172 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
174 // and here, do we really want to forbid creation of the font of the size
175 // 90 (the value of wxDEFAULT)??
176 m_pointSize
= pointSize
== wxDEFAULT
||
177 pointSize
== -1 ? wxDEFAULT_FONT_SIZE
: pointSize
;
179 m_underlined
= underlined
;
180 m_encoding
= encoding
;
183 // Create native font info
184 m_nativeFontInfo
.description
= pango_font_description_new();
186 // And set its values
189 case wxFONTFAMILY_MODERN
:
190 case wxFONTFAMILY_TELETYPE
:
191 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
193 case wxFONTFAMILY_SWISS
:
194 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
197 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
201 SetPointSize( m_pointSize
);
202 SetWeight( m_weight
);
206 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
209 m_pointSize
= data
.m_pointSize
;
210 m_family
= data
.m_family
;
211 m_style
= data
.m_style
;
212 m_weight
= data
.m_weight
;
214 m_underlined
= data
.m_underlined
;
216 m_faceName
= data
.m_faceName
;
217 m_encoding
= data
.m_encoding
;
219 m_nativeFontInfo
= data
.m_nativeFontInfo
;
222 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
223 int weight
, bool underlined
,
224 const wxString
& faceName
,
225 wxFontEncoding encoding
)
227 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
230 wxFontRefData::wxFontRefData(const wxString
& fontname
)
233 m_nativeFontInfo
.FromString( fontname
);
236 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
239 m_faceName
= wxGTK_CONV_BACK( pango_font_description_get_family( desc
) );
241 m_pointSize
= pango_font_description_get_size( desc
) / PANGO_SCALE
;
243 switch (pango_font_description_get_style( desc
))
245 case PANGO_STYLE_NORMAL
:
246 m_style
= wxFONTSTYLE_NORMAL
;
248 case PANGO_STYLE_ITALIC
:
249 m_style
= wxFONTSTYLE_ITALIC
;
251 case PANGO_STYLE_OBLIQUE
:
252 m_style
= wxFONTSTYLE_SLANT
;
256 switch (pango_font_description_get_weight( desc
))
258 case PANGO_WEIGHT_ULTRALIGHT
:
259 m_weight
= wxFONTWEIGHT_LIGHT
;
261 case PANGO_WEIGHT_LIGHT
:
262 m_weight
= wxFONTWEIGHT_LIGHT
;
264 case PANGO_WEIGHT_NORMAL
:
265 m_weight
= wxFONTWEIGHT_NORMAL
;
267 case PANGO_WEIGHT_BOLD
:
268 m_weight
= wxFONTWEIGHT_BOLD
;
270 case PANGO_WEIGHT_ULTRABOLD
:
271 m_weight
= wxFONTWEIGHT_BOLD
;
273 case PANGO_WEIGHT_HEAVY
:
274 m_weight
= wxFONTWEIGHT_BOLD
;
278 if (m_faceName
== wxT("monospace"))
280 m_family
= wxFONTFAMILY_TELETYPE
;
282 else if (m_faceName
== wxT("sans"))
284 m_family
= wxFONTFAMILY_SWISS
;
288 m_family
= wxFONTFAMILY_UNKNOWN
;
291 // Pango description are never underlined (?)
292 m_underlined
= FALSE
;
294 // Cannot we choose that
295 m_encoding
= wxFONTENCODING_SYSTEM
;
297 // remember the X font name
298 m_nativeFontInfo
.SetXFontName(fontname
);
300 // get the font parameters from the XLFD
301 // -------------------------------------
303 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
305 m_weight
= wxFONTWEIGHT_NORMAL
;
307 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
308 if ( !w
.empty() && w
!= _T('*') )
310 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
312 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
313 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
314 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
316 m_weight
= wxFONTWEIGHT_BOLD
;
318 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
320 m_weight
= wxFONTWEIGHT_LIGHT
;
324 switch ( wxToupper(*m_nativeFontInfo
.
325 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
327 case _T('I'): // italique
328 m_style
= wxFONTSTYLE_ITALIC
;
331 case _T('O'): // oblique
332 m_style
= wxFONTSTYLE_SLANT
;
336 m_style
= wxFONTSTYLE_NORMAL
;
340 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
342 // size in XLFD is in 10 point units
343 m_pointSize
= (int)(ptSize
/ 10);
347 m_pointSize
= wxDEFAULT_FONT_SIZE
;
350 // examine the spacing: if the font is monospaced, assume wxTELETYPE
351 // family for compatibility with the old code which used it instead of
353 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
355 m_family
= wxFONTFAMILY_TELETYPE
;
357 else // not monospaceed
359 // don't even try guessing it, it doesn't work for too many fonts
361 m_family
= wxFONTFAMILY_UNKNOWN
;
364 // X fonts are never underlined...
365 m_underlined
= FALSE
;
367 // deal with font encoding
369 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
370 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
372 if ( registry
== _T("ISO8859") )
375 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
377 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
380 else if ( registry
== _T("MICROSOFT") )
383 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
385 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
388 else if ( registry
== _T("KOI8") )
390 m_encoding
= wxFONTENCODING_KOI8
;
392 else // unknown encoding
394 // may be give a warning here?
395 m_encoding
= wxFONTENCODING_SYSTEM
;
400 wxFontRefData::~wxFontRefData()
403 wxNode
*node
= m_scaled_xfonts
.First();
406 GdkFont
*font
= (GdkFont
*)node
->Data();
407 wxNode
*next
= node
->Next();
408 gdk_font_unref( font
);
414 // ----------------------------------------------------------------------------
415 // wxFontRefData SetXXX()
416 // ----------------------------------------------------------------------------
418 void wxFontRefData::SetPointSize(int pointSize
)
420 m_pointSize
= pointSize
;
424 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
426 pango_font_description_set_size( desc
, m_pointSize
* PANGO_SCALE
);
428 if ( HasNativeFont() )
431 if ( pointSize
== -1 )
434 size
.Printf(_T("%d"), 10*pointSize
);
436 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
441 void wxFontRefData::SetFamily(int family
)
445 // TODO: what are we supposed to do with m_nativeFontInfo here?
448 void wxFontRefData::SetStyle(int style
)
454 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
458 case wxFONTSTYLE_ITALIC
:
459 pango_font_description_set_style( desc
, PANGO_STYLE_ITALIC
);
461 case wxFONTSTYLE_SLANT
:
462 pango_font_description_set_style( desc
, PANGO_STYLE_OBLIQUE
);
465 wxFAIL_MSG( _T("unknown font style") );
467 case wxFONTSTYLE_NORMAL
:
468 pango_font_description_set_style( desc
, PANGO_STYLE_NORMAL
);
472 if ( HasNativeFont() )
477 case wxFONTSTYLE_ITALIC
:
481 case wxFONTSTYLE_SLANT
:
486 wxFAIL_MSG( _T("unknown font style") );
489 case wxFONTSTYLE_NORMAL
:
493 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
498 void wxFontRefData::SetWeight(int weight
)
503 if ( HasNativeFont() )
508 case wxFONTWEIGHT_BOLD
:
509 boldness
= _T("bold");
512 case wxFONTWEIGHT_LIGHT
:
513 boldness
= _T("light");
517 wxFAIL_MSG( _T("unknown font weight") );
520 case wxFONTWEIGHT_NORMAL
:
522 boldness
= _T("medium");
525 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
530 void wxFontRefData::SetUnderlined(bool underlined
)
532 m_underlined
= underlined
;
534 // the XLFD doesn't have "underlined" field anyhow
537 void wxFontRefData::SetFaceName(const wxString
& facename
)
539 m_faceName
= facename
;
542 if ( HasNativeFont() )
544 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
549 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
551 m_encoding
= encoding
;
554 if ( HasNativeFont() )
556 wxNativeEncodingInfo info
;
557 if ( wxGetNativeFontEncoding(encoding
, &info
) )
559 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
560 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
566 // ============================================================================
567 // wxFont implementation
568 // ============================================================================
570 // ----------------------------------------------------------------------------
572 // ----------------------------------------------------------------------------
574 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
580 wxFont::wxFont(const wxNativeFontInfo
& info
)
585 Create( info
.GetPointSize(),
589 info
.GetUnderlined(),
591 info
.GetEncoding() );
593 Create(info
.GetXFontName());
597 bool wxFont::Create( int pointSize
,
602 const wxString
& face
,
603 wxFontEncoding encoding
)
605 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
606 underlined
, face
, encoding
);
611 bool wxFont::Create(const wxString
& fontname
)
613 // VZ: does this really happen?
614 if ( fontname
.empty() )
616 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
621 m_refData
= new wxFontRefData(fontname
);
626 void wxFont::Unshare()
630 m_refData
= new wxFontRefData();
634 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
644 // ----------------------------------------------------------------------------
646 // ----------------------------------------------------------------------------
648 // all accessors are just forwarded to wxFontRefData which has everything we
651 int wxFont::GetPointSize() const
653 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
655 return M_FONTDATA
->m_pointSize
;
658 wxString
wxFont::GetFaceName() const
660 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
662 return M_FONTDATA
->m_faceName
;
665 int wxFont::GetFamily() const
667 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
669 return M_FONTDATA
->m_family
;
672 int wxFont::GetStyle() const
674 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
676 return M_FONTDATA
->m_style
;
679 int wxFont::GetWeight() const
681 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
683 return M_FONTDATA
->m_weight
;
686 bool wxFont::GetUnderlined() const
688 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
690 return M_FONTDATA
->m_underlined
;
693 wxFontEncoding
wxFont::GetEncoding() const
695 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
697 return M_FONTDATA
->m_encoding
;
700 wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
702 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
704 #ifndef __WXGTK20__ // ???
705 if ( M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty() )
709 return new wxNativeFontInfo(M_FONTDATA
->m_nativeFontInfo
);
712 bool wxFont::IsFixedWidth() const
714 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
717 if ( M_FONTDATA
->HasNativeFont() )
719 // the monospace fonts are supposed to have "M" in the spacing field
720 wxString spacing
= M_FONTDATA
->
721 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
723 return spacing
.Upper() == _T('M');
727 return wxFontBase::IsFixedWidth();
730 // ----------------------------------------------------------------------------
731 // change font attributes
732 // ----------------------------------------------------------------------------
734 void wxFont::SetPointSize(int pointSize
)
738 M_FONTDATA
->SetPointSize(pointSize
);
741 void wxFont::SetFamily(int family
)
745 M_FONTDATA
->SetFamily(family
);
748 void wxFont::SetStyle(int style
)
752 M_FONTDATA
->SetStyle(style
);
755 void wxFont::SetWeight(int weight
)
759 M_FONTDATA
->SetWeight(weight
);
762 void wxFont::SetFaceName(const wxString
& faceName
)
766 M_FONTDATA
->SetFaceName(faceName
);
769 void wxFont::SetUnderlined(bool underlined
)
773 M_FONTDATA
->SetUnderlined(underlined
);
776 void wxFont::SetEncoding(wxFontEncoding encoding
)
780 M_FONTDATA
->SetEncoding(encoding
);
783 void wxFont::SetNativeFontInfo(const wxNativeFontInfo
& info
)
787 M_FONTDATA
->m_nativeFontInfo
= info
;
790 // ----------------------------------------------------------------------------
791 // get internal representation of font
792 // ----------------------------------------------------------------------------
794 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
796 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
797 extern GdkFont
*GtkGetDefaultGuiFont()
799 if (!g_systemDefaultGuiFont
)
801 GtkWidget
*widget
= gtk_button_new();
802 GtkStyle
*def
= gtk_rc_get_style( widget
);
805 g_systemDefaultGuiFont
= gdk_font_ref( GET_STYLE_FONT(def
) );
809 def
= gtk_widget_get_default_style();
811 g_systemDefaultGuiFont
= gdk_font_ref( GET_STYLE_FONT(def
) );
813 gtk_widget_destroy( widget
);
817 // already have it, but ref it once more before returning
818 gdk_font_ref(g_systemDefaultGuiFont
);
821 return g_systemDefaultGuiFont
;
824 GdkFont
*wxFont::GetInternalFont( float scale
) const
826 GdkFont
*font
= (GdkFont
*) NULL
;
828 wxCHECK_MSG( Ok(), font
, wxT("invalid font") )
831 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
833 font
= GtkGetDefaultGuiFont();
837 PangoFontDescription
*font_description
= GetNativeFontInfo()->description
;
839 font
= gdk_font_from_description( font_description
);
842 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
843 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
845 wxNode
*node
= M_FONTDATA
->m_scaled_xfonts
.Find(int_scale
);
848 font
= (GdkFont
*)node
->Data();
850 else // we don't have this font in this size yet
852 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
854 font
= GtkGetDefaultGuiFont();
859 // do we have the XLFD?
860 if ( M_FONTDATA
->HasNativeFont() )
862 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
865 // no XLFD of no exact match - try the approximate one now
869 font
= wxLoadQueryNearestFont( point_scale
,
870 M_FONTDATA
->m_family
,
872 M_FONTDATA
->m_weight
,
873 M_FONTDATA
->m_underlined
,
874 M_FONTDATA
->m_faceName
,
875 M_FONTDATA
->m_encoding
,
879 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(xfontname
);
886 M_FONTDATA
->m_scaled_xfonts
.Append( int_scale
, (wxObject
*)font
);
891 // it's quite useless to make it a wxCHECK because we're going to crash
893 wxASSERT_MSG( font
, wxT("could not load any font?") );