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_TELETYPE
:
190 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospaced" );
192 case wxFONTFAMILY_SWISS
:
193 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
196 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
200 SetPointSize( m_pointSize
);
201 SetWeight( m_weight
);
205 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
208 m_pointSize
= data
.m_pointSize
;
209 m_family
= data
.m_family
;
210 m_style
= data
.m_style
;
211 m_weight
= data
.m_weight
;
213 m_underlined
= data
.m_underlined
;
215 m_faceName
= data
.m_faceName
;
216 m_encoding
= data
.m_encoding
;
218 m_nativeFontInfo
= data
.m_nativeFontInfo
;
221 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
222 int weight
, bool underlined
,
223 const wxString
& faceName
,
224 wxFontEncoding encoding
)
226 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
229 wxFontRefData::wxFontRefData(const wxString
& fontname
)
232 m_nativeFontInfo
.FromString( fontname
);
235 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
238 m_faceName
= wxGTK_CONV_BACK( pango_font_description_get_family( desc
) );
240 m_pointSize
= pango_font_description_get_size( desc
) / PANGO_SCALE
;
242 switch (pango_font_description_get_style( desc
))
244 case PANGO_STYLE_NORMAL
:
245 m_style
= wxFONTSTYLE_NORMAL
;
247 case PANGO_STYLE_ITALIC
:
248 m_style
= wxFONTSTYLE_ITALIC
;
250 case PANGO_STYLE_OBLIQUE
:
251 m_style
= wxFONTSTYLE_SLANT
;
255 switch (pango_font_description_get_weight( desc
))
257 case PANGO_WEIGHT_ULTRALIGHT
:
258 m_weight
= wxFONTWEIGHT_LIGHT
;
260 case PANGO_WEIGHT_LIGHT
:
261 m_weight
= wxFONTWEIGHT_LIGHT
;
263 case PANGO_WEIGHT_NORMAL
:
264 m_weight
= wxFONTWEIGHT_NORMAL
;
266 case PANGO_WEIGHT_BOLD
:
267 m_weight
= wxFONTWEIGHT_BOLD
;
269 case PANGO_WEIGHT_ULTRABOLD
:
270 m_weight
= wxFONTWEIGHT_BOLD
;
272 case PANGO_WEIGHT_HEAVY
:
273 m_weight
= wxFONTWEIGHT_BOLD
;
277 if (m_faceName
== wxT("monospaced"))
279 m_family
= wxFONTFAMILY_TELETYPE
;
281 else if (m_faceName
== wxT("sans"))
283 m_family
= wxFONTFAMILY_SWISS
;
287 m_family
= wxFONTFAMILY_UNKNOWN
;
290 // Pango description are never underlined (?)
291 m_underlined
= FALSE
;
293 // Cannot we choose that
294 m_encoding
= wxFONTENCODING_SYSTEM
;
296 // remember the X font name
297 m_nativeFontInfo
.SetXFontName(fontname
);
299 // get the font parameters from the XLFD
300 // -------------------------------------
302 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
304 m_weight
= wxFONTWEIGHT_NORMAL
;
306 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
307 if ( !w
.empty() && w
!= _T('*') )
309 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
311 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
312 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
313 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
315 m_weight
= wxFONTWEIGHT_BOLD
;
317 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
319 m_weight
= wxFONTWEIGHT_LIGHT
;
323 switch ( wxToupper(*m_nativeFontInfo
.
324 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
326 case _T('I'): // italique
327 m_style
= wxFONTSTYLE_ITALIC
;
330 case _T('O'): // oblique
331 m_style
= wxFONTSTYLE_SLANT
;
335 m_style
= wxFONTSTYLE_NORMAL
;
339 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
341 // size in XLFD is in 10 point units
342 m_pointSize
= (int)(ptSize
/ 10);
346 m_pointSize
= wxDEFAULT_FONT_SIZE
;
349 // examine the spacing: if the font is monospaced, assume wxTELETYPE
350 // family for compatibility with the old code which used it instead of
352 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
354 m_family
= wxFONTFAMILY_TELETYPE
;
356 else // not monospaceed
358 // don't even try guessing it, it doesn't work for too many fonts
360 m_family
= wxFONTFAMILY_UNKNOWN
;
363 // X fonts are never underlined...
364 m_underlined
= FALSE
;
366 // deal with font encoding
368 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
369 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
371 if ( registry
== _T("ISO8859") )
374 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
376 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
379 else if ( registry
== _T("MICROSOFT") )
382 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
384 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
387 else if ( registry
== _T("KOI8") )
389 m_encoding
= wxFONTENCODING_KOI8
;
391 else // unknown encoding
393 // may be give a warning here?
394 m_encoding
= wxFONTENCODING_SYSTEM
;
399 wxFontRefData::~wxFontRefData()
402 wxNode
*node
= m_scaled_xfonts
.First();
405 GdkFont
*font
= (GdkFont
*)node
->Data();
406 wxNode
*next
= node
->Next();
407 gdk_font_unref( font
);
413 // ----------------------------------------------------------------------------
414 // wxFontRefData SetXXX()
415 // ----------------------------------------------------------------------------
417 void wxFontRefData::SetPointSize(int pointSize
)
419 m_pointSize
= pointSize
;
423 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
425 pango_font_description_set_size( desc
, m_pointSize
* PANGO_SCALE
);
427 if ( HasNativeFont() )
430 if ( pointSize
== -1 )
433 size
.Printf(_T("%d"), 10*pointSize
);
435 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
440 void wxFontRefData::SetFamily(int family
)
444 // TODO: what are we supposed to do with m_nativeFontInfo here?
447 void wxFontRefData::SetStyle(int style
)
453 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
457 case wxFONTSTYLE_ITALIC
:
458 pango_font_description_set_style( desc
, PANGO_STYLE_ITALIC
);
460 case wxFONTSTYLE_SLANT
:
461 pango_font_description_set_style( desc
, PANGO_STYLE_OBLIQUE
);
464 wxFAIL_MSG( _T("unknown font style") );
466 case wxFONTSTYLE_NORMAL
:
467 pango_font_description_set_style( desc
, PANGO_STYLE_NORMAL
);
471 if ( HasNativeFont() )
476 case wxFONTSTYLE_ITALIC
:
480 case wxFONTSTYLE_SLANT
:
485 wxFAIL_MSG( _T("unknown font style") );
488 case wxFONTSTYLE_NORMAL
:
492 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
497 void wxFontRefData::SetWeight(int weight
)
502 if ( HasNativeFont() )
507 case wxFONTWEIGHT_BOLD
:
508 boldness
= _T("bold");
511 case wxFONTWEIGHT_LIGHT
:
512 boldness
= _T("light");
516 wxFAIL_MSG( _T("unknown font weight") );
519 case wxFONTWEIGHT_NORMAL
:
521 boldness
= _T("medium");
524 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
529 void wxFontRefData::SetUnderlined(bool underlined
)
531 m_underlined
= underlined
;
533 // the XLFD doesn't have "underlined" field anyhow
536 void wxFontRefData::SetFaceName(const wxString
& facename
)
538 m_faceName
= facename
;
541 if ( HasNativeFont() )
543 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
548 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
550 m_encoding
= encoding
;
553 if ( HasNativeFont() )
555 wxNativeEncodingInfo info
;
556 if ( wxGetNativeFontEncoding(encoding
, &info
) )
558 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
559 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
565 // ============================================================================
566 // wxFont implementation
567 // ============================================================================
569 // ----------------------------------------------------------------------------
571 // ----------------------------------------------------------------------------
573 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
579 wxFont::wxFont(const wxNativeFontInfo
& info
)
584 Create( info
.GetPointSize(),
588 info
.GetUnderlined(),
590 info
.GetEncoding() );
592 Create(info
.GetXFontName());
596 bool wxFont::Create( int pointSize
,
601 const wxString
& face
,
602 wxFontEncoding encoding
)
604 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
605 underlined
, face
, encoding
);
610 bool wxFont::Create(const wxString
& fontname
)
612 // VZ: does this really happen?
613 if ( fontname
.empty() )
615 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
620 m_refData
= new wxFontRefData(fontname
);
625 void wxFont::Unshare()
629 m_refData
= new wxFontRefData();
633 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
643 // ----------------------------------------------------------------------------
645 // ----------------------------------------------------------------------------
647 // all accessors are just forwarded to wxFontRefData which has everything we
650 int wxFont::GetPointSize() const
652 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
654 return M_FONTDATA
->m_pointSize
;
657 wxString
wxFont::GetFaceName() const
659 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
661 return M_FONTDATA
->m_faceName
;
664 int wxFont::GetFamily() const
666 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
668 return M_FONTDATA
->m_family
;
671 int wxFont::GetStyle() const
673 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
675 return M_FONTDATA
->m_style
;
678 int wxFont::GetWeight() const
680 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
682 return M_FONTDATA
->m_weight
;
685 bool wxFont::GetUnderlined() const
687 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
689 return M_FONTDATA
->m_underlined
;
692 wxFontEncoding
wxFont::GetEncoding() const
694 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
696 return M_FONTDATA
->m_encoding
;
699 wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
701 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
703 #ifndef __WXGTK20__ // ???
704 if ( M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty() )
708 return new wxNativeFontInfo(M_FONTDATA
->m_nativeFontInfo
);
711 bool wxFont::IsFixedWidth() const
713 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
716 if ( M_FONTDATA
->HasNativeFont() )
718 // the monospace fonts are supposed to have "M" in the spacing field
719 wxString spacing
= M_FONTDATA
->
720 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
722 return spacing
.Upper() == _T('M');
726 return wxFontBase::IsFixedWidth();
729 // ----------------------------------------------------------------------------
730 // change font attributes
731 // ----------------------------------------------------------------------------
733 void wxFont::SetPointSize(int pointSize
)
737 M_FONTDATA
->SetPointSize(pointSize
);
740 void wxFont::SetFamily(int family
)
744 M_FONTDATA
->SetFamily(family
);
747 void wxFont::SetStyle(int style
)
751 M_FONTDATA
->SetStyle(style
);
754 void wxFont::SetWeight(int weight
)
758 M_FONTDATA
->SetWeight(weight
);
761 void wxFont::SetFaceName(const wxString
& faceName
)
765 M_FONTDATA
->SetFaceName(faceName
);
768 void wxFont::SetUnderlined(bool underlined
)
772 M_FONTDATA
->SetUnderlined(underlined
);
775 void wxFont::SetEncoding(wxFontEncoding encoding
)
779 M_FONTDATA
->SetEncoding(encoding
);
782 void wxFont::SetNativeFontInfo(const wxNativeFontInfo
& info
)
786 M_FONTDATA
->m_nativeFontInfo
= info
;
789 // ----------------------------------------------------------------------------
790 // get internal representation of font
791 // ----------------------------------------------------------------------------
793 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
795 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
796 extern GdkFont
*GtkGetDefaultGuiFont()
798 if (!g_systemDefaultGuiFont
)
800 GtkWidget
*widget
= gtk_button_new();
801 GtkStyle
*def
= gtk_rc_get_style( widget
);
804 g_systemDefaultGuiFont
= gdk_font_ref( GET_STYLE_FONT(def
) );
808 def
= gtk_widget_get_default_style();
810 g_systemDefaultGuiFont
= gdk_font_ref( GET_STYLE_FONT(def
) );
812 gtk_widget_destroy( widget
);
816 // already have it, but ref it once more before returning
817 gdk_font_ref(g_systemDefaultGuiFont
);
820 return g_systemDefaultGuiFont
;
823 GdkFont
*wxFont::GetInternalFont( float scale
) const
825 GdkFont
*font
= (GdkFont
*) NULL
;
827 wxCHECK_MSG( Ok(), font
, wxT("invalid font") )
830 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
832 font
= GtkGetDefaultGuiFont();
836 PangoFontDescription
*font_description
= GetNativeFontInfo()->description
;
838 font
= gdk_font_from_description( font_description
);
841 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
842 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
844 wxNode
*node
= M_FONTDATA
->m_scaled_xfonts
.Find(int_scale
);
847 font
= (GdkFont
*)node
->Data();
849 else // we don't have this font in this size yet
851 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
853 font
= GtkGetDefaultGuiFont();
858 // do we have the XLFD?
859 if ( M_FONTDATA
->HasNativeFont() )
861 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
864 // no XLFD of no exact match - try the approximate one now
868 font
= wxLoadQueryNearestFont( point_scale
,
869 M_FONTDATA
->m_family
,
871 M_FONTDATA
->m_weight
,
872 M_FONTDATA
->m_underlined
,
873 M_FONTDATA
->m_faceName
,
874 M_FONTDATA
->m_encoding
,
878 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(xfontname
);
885 M_FONTDATA
->m_scaled_xfonts
.Append( int_scale
, (wxObject
*)font
);
890 // it's quite useless to make it a wxCHECK because we're going to crash
892 wxASSERT_MSG( font
, wxT("could not load any font?") );