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 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
186 m_pointSize
= data
.m_pointSize
;
187 m_family
= data
.m_family
;
188 m_style
= data
.m_style
;
189 m_weight
= data
.m_weight
;
191 m_underlined
= data
.m_underlined
;
193 m_faceName
= data
.m_faceName
;
194 m_encoding
= data
.m_encoding
;
196 m_nativeFontInfo
= data
.m_nativeFontInfo
;
199 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
200 int weight
, bool underlined
,
201 const wxString
& faceName
,
202 wxFontEncoding encoding
)
204 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
207 wxFontRefData::wxFontRefData(const wxString
& fontname
)
210 m_nativeFontInfo
.FromString( fontname
);
213 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
216 m_faceName
= wxGTK_CONV_BACK( pango_font_description_get_family( desc
) );
218 m_pointSize
= pango_font_description_get_size( desc
) / PANGO_SCALE
;
220 switch (pango_font_description_get_style( desc
))
222 case PANGO_STYLE_NORMAL
:
223 m_style
= wxFONTSTYLE_NORMAL
;
225 case PANGO_STYLE_ITALIC
:
226 m_style
= wxFONTSTYLE_ITALIC
;
228 case PANGO_STYLE_OBLIQUE
:
229 m_style
= wxFONTSTYLE_SLANT
;
233 switch (pango_font_description_get_weight( desc
))
235 case PANGO_WEIGHT_ULTRALIGHT
:
236 m_weight
= wxFONTWEIGHT_LIGHT
;
238 case PANGO_WEIGHT_LIGHT
:
239 m_weight
= wxFONTWEIGHT_LIGHT
;
241 case PANGO_WEIGHT_NORMAL
:
242 m_weight
= wxFONTWEIGHT_NORMAL
;
244 case PANGO_WEIGHT_BOLD
:
245 m_weight
= wxFONTWEIGHT_BOLD
;
247 case PANGO_WEIGHT_ULTRABOLD
:
248 m_weight
= wxFONTWEIGHT_BOLD
;
250 case PANGO_WEIGHT_HEAVY
:
251 m_weight
= wxFONTWEIGHT_BOLD
;
255 if (m_faceName
== wxT("monospaced"))
257 m_family
= wxFONTFAMILY_TELETYPE
;
259 else if (m_faceName
== wxT("sans"))
261 m_family
= wxFONTFAMILY_SWISS
;
265 m_family
= wxFONTFAMILY_UNKNOWN
;
268 // Pango description are never underlined (?)
269 m_underlined
= FALSE
;
271 // Cannot we choose that
272 m_encoding
= wxFONTENCODING_SYSTEM
;
274 // remember the X font name
275 m_nativeFontInfo
.SetXFontName(fontname
);
277 // get the font parameters from the XLFD
278 // -------------------------------------
280 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
282 m_weight
= wxFONTWEIGHT_NORMAL
;
284 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
285 if ( !w
.empty() && w
!= _T('*') )
287 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
289 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
290 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
291 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
293 m_weight
= wxFONTWEIGHT_BOLD
;
295 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
297 m_weight
= wxFONTWEIGHT_LIGHT
;
301 switch ( wxToupper(*m_nativeFontInfo
.
302 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
304 case _T('I'): // italique
305 m_style
= wxFONTSTYLE_ITALIC
;
308 case _T('O'): // oblique
309 m_style
= wxFONTSTYLE_SLANT
;
313 m_style
= wxFONTSTYLE_NORMAL
;
317 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
319 // size in XLFD is in 10 point units
320 m_pointSize
= (int)(ptSize
/ 10);
324 m_pointSize
= wxDEFAULT_FONT_SIZE
;
327 // examine the spacing: if the font is monospaced, assume wxTELETYPE
328 // family for compatibility with the old code which used it instead of
330 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
332 m_family
= wxFONTFAMILY_TELETYPE
;
334 else // not monospaceed
336 // don't even try guessing it, it doesn't work for too many fonts
338 m_family
= wxFONTFAMILY_UNKNOWN
;
341 // X fonts are never underlined...
342 m_underlined
= FALSE
;
344 // deal with font encoding
346 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
347 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
349 if ( registry
== _T("ISO8859") )
352 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
354 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
357 else if ( registry
== _T("MICROSOFT") )
360 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
362 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
365 else if ( registry
== _T("KOI8") )
367 m_encoding
= wxFONTENCODING_KOI8
;
369 else // unknown encoding
371 // may be give a warning here?
372 m_encoding
= wxFONTENCODING_SYSTEM
;
377 wxFontRefData::~wxFontRefData()
380 wxNode
*node
= m_scaled_xfonts
.First();
383 GdkFont
*font
= (GdkFont
*)node
->Data();
384 wxNode
*next
= node
->Next();
385 gdk_font_unref( font
);
391 // ----------------------------------------------------------------------------
392 // wxFontRefData SetXXX()
393 // ----------------------------------------------------------------------------
395 void wxFontRefData::SetPointSize(int pointSize
)
397 m_pointSize
= pointSize
;
401 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
403 pango_font_description_set_size( desc
, m_pointSize
* PANGO_SCALE
);
405 if ( HasNativeFont() )
408 if ( pointSize
== -1 )
411 size
.Printf(_T("%d"), 10*pointSize
);
413 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
418 void wxFontRefData::SetFamily(int family
)
422 // TODO: what are we supposed to do with m_nativeFontInfo here?
425 void wxFontRefData::SetStyle(int style
)
431 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
435 case wxFONTSTYLE_ITALIC
:
436 pango_font_description_set_style( desc
, PANGO_STYLE_ITALIC
);
438 case wxFONTSTYLE_SLANT
:
439 pango_font_description_set_style( desc
, PANGO_STYLE_OBLIQUE
);
442 wxFAIL_MSG( _T("unknown font style") );
444 case wxFONTSTYLE_NORMAL
:
445 pango_font_description_set_style( desc
, PANGO_STYLE_NORMAL
);
449 if ( HasNativeFont() )
454 case wxFONTSTYLE_ITALIC
:
458 case wxFONTSTYLE_SLANT
:
463 wxFAIL_MSG( _T("unknown font style") );
466 case wxFONTSTYLE_NORMAL
:
470 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
475 void wxFontRefData::SetWeight(int weight
)
480 if ( HasNativeFont() )
485 case wxFONTWEIGHT_BOLD
:
486 boldness
= _T("bold");
489 case wxFONTWEIGHT_LIGHT
:
490 boldness
= _T("light");
494 wxFAIL_MSG( _T("unknown font weight") );
497 case wxFONTWEIGHT_NORMAL
:
499 boldness
= _T("medium");
502 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
507 void wxFontRefData::SetUnderlined(bool underlined
)
509 m_underlined
= underlined
;
511 // the XLFD doesn't have "underlined" field anyhow
514 void wxFontRefData::SetFaceName(const wxString
& facename
)
516 m_faceName
= facename
;
519 if ( HasNativeFont() )
521 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
526 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
528 m_encoding
= encoding
;
531 if ( HasNativeFont() )
533 wxNativeEncodingInfo info
;
534 if ( wxGetNativeFontEncoding(encoding
, &info
) )
536 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
537 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
543 // ============================================================================
544 // wxFont implementation
545 // ============================================================================
547 // ----------------------------------------------------------------------------
549 // ----------------------------------------------------------------------------
551 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
557 wxFont::wxFont(const wxNativeFontInfo
& info
)
562 Create( info
.GetPointSize(),
566 info
.GetUnderlined(),
568 info
.GetEncoding() );
570 Create(info
.GetXFontName());
574 bool wxFont::Create( int pointSize
,
579 const wxString
& face
,
580 wxFontEncoding encoding
)
582 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
583 underlined
, face
, encoding
);
588 bool wxFont::Create(const wxString
& fontname
)
590 // VZ: does this really happen?
591 if ( fontname
.empty() )
593 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
598 m_refData
= new wxFontRefData(fontname
);
603 void wxFont::Unshare()
607 m_refData
= new wxFontRefData();
611 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
621 // ----------------------------------------------------------------------------
623 // ----------------------------------------------------------------------------
625 // all accessors are just forwarded to wxFontRefData which has everything we
628 int wxFont::GetPointSize() const
630 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
632 return M_FONTDATA
->m_pointSize
;
635 wxString
wxFont::GetFaceName() const
637 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
639 return M_FONTDATA
->m_faceName
;
642 int wxFont::GetFamily() const
644 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
646 return M_FONTDATA
->m_family
;
649 int wxFont::GetStyle() const
651 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
653 return M_FONTDATA
->m_style
;
656 int wxFont::GetWeight() const
658 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
660 return M_FONTDATA
->m_weight
;
663 bool wxFont::GetUnderlined() const
665 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
667 return M_FONTDATA
->m_underlined
;
670 wxFontEncoding
wxFont::GetEncoding() const
672 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
674 return M_FONTDATA
->m_encoding
;
677 wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
679 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
681 #ifndef __WXGTK20__ // ???
682 if ( M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty() )
686 return new wxNativeFontInfo(M_FONTDATA
->m_nativeFontInfo
);
689 bool wxFont::IsFixedWidth() const
691 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
694 if ( M_FONTDATA
->HasNativeFont() )
696 // the monospace fonts are supposed to have "M" in the spacing field
697 wxString spacing
= M_FONTDATA
->
698 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
700 return spacing
.Upper() == _T('M');
704 return wxFontBase::IsFixedWidth();
707 // ----------------------------------------------------------------------------
708 // change font attributes
709 // ----------------------------------------------------------------------------
711 void wxFont::SetPointSize(int pointSize
)
715 M_FONTDATA
->SetPointSize(pointSize
);
718 void wxFont::SetFamily(int family
)
722 M_FONTDATA
->SetFamily(family
);
725 void wxFont::SetStyle(int style
)
729 M_FONTDATA
->SetStyle(style
);
732 void wxFont::SetWeight(int weight
)
736 M_FONTDATA
->SetWeight(weight
);
739 void wxFont::SetFaceName(const wxString
& faceName
)
743 M_FONTDATA
->SetFaceName(faceName
);
746 void wxFont::SetUnderlined(bool underlined
)
750 M_FONTDATA
->SetUnderlined(underlined
);
753 void wxFont::SetEncoding(wxFontEncoding encoding
)
757 M_FONTDATA
->SetEncoding(encoding
);
760 void wxFont::SetNativeFontInfo(const wxNativeFontInfo
& info
)
764 M_FONTDATA
->m_nativeFontInfo
= info
;
767 // ----------------------------------------------------------------------------
768 // get internal representation of font
769 // ----------------------------------------------------------------------------
771 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
773 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
774 extern GdkFont
*GtkGetDefaultGuiFont()
776 if (!g_systemDefaultGuiFont
)
778 GtkWidget
*widget
= gtk_button_new();
779 GtkStyle
*def
= gtk_rc_get_style( widget
);
782 g_systemDefaultGuiFont
= gdk_font_ref( GET_STYLE_FONT(def
) );
786 def
= gtk_widget_get_default_style();
788 g_systemDefaultGuiFont
= gdk_font_ref( GET_STYLE_FONT(def
) );
790 gtk_widget_destroy( widget
);
794 // already have it, but ref it once more before returning
795 gdk_font_ref(g_systemDefaultGuiFont
);
798 return g_systemDefaultGuiFont
;
801 GdkFont
*wxFont::GetInternalFont( float scale
) const
803 GdkFont
*font
= (GdkFont
*) NULL
;
805 wxCHECK_MSG( Ok(), font
, wxT("invalid font") )
808 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
810 font
= GtkGetDefaultGuiFont();
814 PangoFontDescription
*font_description
= GetNativeFontInfo()->description
;
816 font
= gdk_font_from_description( font_description
);
819 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
820 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
822 wxNode
*node
= M_FONTDATA
->m_scaled_xfonts
.Find(int_scale
);
825 font
= (GdkFont
*)node
->Data();
827 else // we don't have this font in this size yet
829 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
831 font
= GtkGetDefaultGuiFont();
836 // do we have the XLFD?
837 if ( M_FONTDATA
->HasNativeFont() )
839 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
842 // no XLFD of no exact match - try the approximate one now
846 font
= wxLoadQueryNearestFont( point_scale
,
847 M_FONTDATA
->m_family
,
849 M_FONTDATA
->m_weight
,
850 M_FONTDATA
->m_underlined
,
851 M_FONTDATA
->m_faceName
,
852 M_FONTDATA
->m_encoding
,
856 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(xfontname
);
863 M_FONTDATA
->m_scaled_xfonts
.Append( int_scale
, (wxObject
*)font
);
868 // it's quite useless to make it a wxCHECK because we're going to crash
870 wxASSERT_MSG( font
, wxT("could not load any font?") );