1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling and Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
19 #pragma implementation "font.h"
22 // For compilers that support precompilation, includes "wx.h".
23 #include "wx/wxprec.h"
26 #include "wx/fontutil.h"
27 #include "wx/cmndata.h"
30 #include "wx/gdicmn.h"
31 #include "wx/tokenzr.h"
32 #include "wx/settings.h"
36 #include "wx/gtk/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 wxObjectRefData
60 // from broken down font parameters, also default ctor
61 wxFontRefData(int size
= -1,
62 int family
= wxFONTFAMILY_DEFAULT
,
63 int style
= wxFONTSTYLE_NORMAL
,
64 int 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
81 // we always have a Pango font description
84 // only use m_nativeFontInfo if it had been initialized
85 return !m_nativeFontInfo
.IsDefault();
90 // reinitilize the font with the gived XFLD
91 void ReInit(const wxString
& fontname
);
94 // setters: all of them also take care to modify m_nativeFontInfo if we
95 // have it so as to not lose the information not carried by our fields
96 void SetPointSize(int pointSize
);
97 void SetFamily(int family
);
98 void SetStyle(int style
);
99 void SetWeight(int weight
);
100 void SetUnderlined(bool underlined
);
101 void SetFaceName(const wxString
& facename
);
102 void SetEncoding(wxFontEncoding encoding
);
104 void SetNoAntiAliasing( bool no
= TRUE
) { m_noAA
= no
; }
105 bool GetNoAntiAliasing() { return m_noAA
; }
107 // and this one also modifies all the other font data fields
108 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
110 // debugger helper: shows what the font really is
112 // VZ: I need this as my gdb either shows wildly wrong values or crashes
113 // when I ask it to "p fontRefData" :-(
114 #if defined(__WXDEBUG__) && !defined(__WXGTK20__)
117 wxPrintf(_T("%s-%s-%s-%d-%d\n"),
119 m_weight
== wxFONTWEIGHT_NORMAL
121 : m_weight
== wxFONTWEIGHT_BOLD
124 m_style
== wxFONTSTYLE_NORMAL
? _T("regular") : _T("italic"),
131 // common part of all ctors
132 void Init(int pointSize
,
137 const wxString
& faceName
,
138 wxFontEncoding encoding
);
140 // set all fields from (already initialized and valid) m_nativeFontInfo
141 void InitFromNative();
144 // clear m_scaled_xfonts if any
145 void ClearGdkFonts();
148 // the map of font sizes to "GdkFont *"
149 wxScaledFontList m_scaled_xfonts
;
150 #endif // GTK 2.0/1.x
158 wxFontEncoding m_encoding
; // Unused under GTK 2.0
159 bool m_noAA
; // No anti-aliasing
161 // The native font info, basicly an XFLD under GTK 1.2 and
162 // the pango font description under GTK 2.0.
163 wxNativeFontInfo m_nativeFontInfo
;
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 void wxFontRefData::Init(int pointSize
,
177 const wxString
& faceName
,
178 wxFontEncoding encoding
)
180 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
182 m_faceName
= faceName
;
184 // we accept both wxDEFAULT and wxNORMAL here - should we?
185 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
186 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
188 // and here, do we really want to forbid creation of the font of the size
189 // 90 (the value of wxDEFAULT)??
190 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
191 ? wxDEFAULT_FONT_SIZE
194 m_underlined
= underlined
;
195 m_encoding
= encoding
;
200 // Create native font info
201 m_nativeFontInfo
.description
= pango_font_description_new();
203 // And set its values
204 if (!m_faceName
.empty())
206 pango_font_description_set_family( m_nativeFontInfo
.description
, wxGTK_CONV(m_faceName
) );
212 case wxFONTFAMILY_MODERN
:
213 case wxFONTFAMILY_TELETYPE
:
214 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
216 case wxFONTFAMILY_ROMAN
:
217 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
219 case wxFONTFAMILY_SWISS
:
220 // SWISS = sans serif
222 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
228 SetPointSize( m_pointSize
);
229 SetWeight( m_weight
);
233 void wxFontRefData::InitFromNative()
239 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
242 m_faceName
= wxGTK_CONV_BACK( pango_font_description_get_family( desc
) );
244 m_pointSize
= pango_font_description_get_size( desc
) / PANGO_SCALE
;
246 switch (pango_font_description_get_style( desc
))
248 case PANGO_STYLE_NORMAL
:
249 m_style
= wxFONTSTYLE_NORMAL
;
251 case PANGO_STYLE_ITALIC
:
252 m_style
= wxFONTSTYLE_ITALIC
;
254 case PANGO_STYLE_OBLIQUE
:
255 m_style
= wxFONTSTYLE_SLANT
;
259 switch (pango_font_description_get_weight( desc
))
261 case PANGO_WEIGHT_ULTRALIGHT
:
262 m_weight
= wxFONTWEIGHT_LIGHT
;
264 case PANGO_WEIGHT_LIGHT
:
265 m_weight
= wxFONTWEIGHT_LIGHT
;
267 case PANGO_WEIGHT_NORMAL
:
268 m_weight
= wxFONTWEIGHT_NORMAL
;
270 case PANGO_WEIGHT_BOLD
:
271 m_weight
= wxFONTWEIGHT_BOLD
;
273 case PANGO_WEIGHT_ULTRABOLD
:
274 m_weight
= wxFONTWEIGHT_BOLD
;
276 case PANGO_WEIGHT_HEAVY
:
277 m_weight
= wxFONTWEIGHT_BOLD
;
281 if (m_faceName
== wxT("monospace"))
283 m_family
= wxFONTFAMILY_TELETYPE
;
285 else if (m_faceName
== wxT("sans"))
287 m_family
= wxFONTFAMILY_SWISS
;
289 else if (m_faceName
== wxT("serif"))
291 m_family
= wxFONTFAMILY_ROMAN
;
295 m_family
= wxFONTFAMILY_UNKNOWN
;
298 // Pango description are never underlined (?)
299 m_underlined
= FALSE
;
301 // Cannot we choose that
302 m_encoding
= wxFONTENCODING_SYSTEM
;
304 // get the font parameters from the XLFD
305 // -------------------------------------
307 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
309 m_weight
= wxFONTWEIGHT_NORMAL
;
311 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
312 if ( !w
.empty() && w
!= _T('*') )
314 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
316 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
317 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
318 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
320 m_weight
= wxFONTWEIGHT_BOLD
;
322 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
324 m_weight
= wxFONTWEIGHT_LIGHT
;
328 switch ( wxToupper(*m_nativeFontInfo
.
329 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
331 case _T('I'): // italique
332 m_style
= wxFONTSTYLE_ITALIC
;
335 case _T('O'): // oblique
336 m_style
= wxFONTSTYLE_SLANT
;
340 m_style
= wxFONTSTYLE_NORMAL
;
344 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
346 // size in XLFD is in 10 point units
347 m_pointSize
= (int)(ptSize
/ 10);
351 m_pointSize
= wxDEFAULT_FONT_SIZE
;
354 // examine the spacing: if the font is monospaced, assume wxTELETYPE
355 // family for compatibility with the old code which used it instead of
357 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
359 m_family
= wxFONTFAMILY_TELETYPE
;
361 else // not monospaceed
363 // don't even try guessing it, it doesn't work for too many fonts
365 m_family
= wxFONTFAMILY_UNKNOWN
;
368 // X fonts are never underlined...
369 m_underlined
= FALSE
;
371 // deal with font encoding
373 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
374 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
376 if ( registry
== _T("ISO8859") )
379 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
381 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
384 else if ( registry
== _T("MICROSOFT") )
387 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
389 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
392 else if ( registry
== _T("KOI8") )
394 m_encoding
= wxFONTENCODING_KOI8
;
396 else // unknown encoding
398 // may be give a warning here? or use wxFontMapper?
399 m_encoding
= wxFONTENCODING_SYSTEM
;
401 #endif // GTK 2.0/1.x
404 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
407 m_pointSize
= data
.m_pointSize
;
408 m_family
= data
.m_family
;
409 m_style
= data
.m_style
;
410 m_weight
= data
.m_weight
;
412 m_underlined
= data
.m_underlined
;
414 m_faceName
= data
.m_faceName
;
415 m_encoding
= data
.m_encoding
;
417 m_noAA
= data
.m_noAA
;
419 // Forces a copy of the internal data. wxNativeFontInfo should probably
420 // have a copy ctor and assignment operator to fix this properly but that
421 // would break binary compatibility...
422 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
425 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
426 int weight
, bool underlined
,
427 const wxString
& faceName
,
428 wxFontEncoding encoding
)
430 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
433 wxFontRefData::wxFontRefData(const wxString
& fontname
)
435 // VZ: FromString() should really work in both cases, doesn't it?
437 m_nativeFontInfo
.FromString( fontname
);
439 m_nativeFontInfo
.SetXFontName(fontname
);
440 #endif // GTK 2.0/1.x
446 void wxFontRefData::ReInit(const wxString
& fontname
)
448 m_nativeFontInfo
.SetXFontName(fontname
);
454 void wxFontRefData::ClearGdkFonts()
457 for ( wxScaledFontList::iterator i
= m_scaled_xfonts
.begin();
458 i
!= m_scaled_xfonts
.end();
461 GdkFont
*font
= i
->second
;
462 gdk_font_unref( font
);
465 m_scaled_xfonts
.clear();
469 wxFontRefData::~wxFontRefData()
474 // ----------------------------------------------------------------------------
475 // wxFontRefData SetXXX()
476 // ----------------------------------------------------------------------------
478 void wxFontRefData::SetPointSize(int pointSize
)
480 m_pointSize
= pointSize
;
484 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
486 pango_font_description_set_size( desc
, m_pointSize
* PANGO_SCALE
);
488 if ( HasNativeFont() )
491 if ( pointSize
== -1 )
494 size
.Printf(_T("%d"), 10*pointSize
);
496 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
501 void wxFontRefData::SetFamily(int family
)
505 // TODO: what are we supposed to do with m_nativeFontInfo here?
508 void wxFontRefData::SetStyle(int style
)
514 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
518 case wxFONTSTYLE_ITALIC
:
519 pango_font_description_set_style( desc
, PANGO_STYLE_ITALIC
);
521 case wxFONTSTYLE_SLANT
:
522 pango_font_description_set_style( desc
, PANGO_STYLE_OBLIQUE
);
525 wxFAIL_MSG( _T("unknown font style") );
527 case wxFONTSTYLE_NORMAL
:
528 pango_font_description_set_style( desc
, PANGO_STYLE_NORMAL
);
532 if ( HasNativeFont() )
537 case wxFONTSTYLE_ITALIC
:
541 case wxFONTSTYLE_SLANT
:
546 wxFAIL_MSG( _T("unknown font style") );
549 case wxFONTSTYLE_NORMAL
:
553 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
558 void wxFontRefData::SetWeight(int weight
)
563 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
566 case wxFONTWEIGHT_BOLD
:
567 pango_font_description_set_weight(desc
, PANGO_WEIGHT_BOLD
);
570 case wxFONTWEIGHT_LIGHT
:
571 pango_font_description_set_weight(desc
, PANGO_WEIGHT_LIGHT
);
575 wxFAIL_MSG( _T("unknown font weight") );
578 case wxFONTWEIGHT_NORMAL
:
580 pango_font_description_set_weight(desc
, PANGO_WEIGHT_NORMAL
);
583 if ( HasNativeFont() )
588 case wxFONTWEIGHT_BOLD
:
589 boldness
= _T("bold");
592 case wxFONTWEIGHT_LIGHT
:
593 boldness
= _T("light");
597 wxFAIL_MSG( _T("unknown font weight") );
600 case wxFONTWEIGHT_NORMAL
:
602 boldness
= _T("medium");
605 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
610 void wxFontRefData::SetUnderlined(bool underlined
)
612 m_underlined
= underlined
;
614 // the XLFD doesn't have "underlined" field anyhow
617 void wxFontRefData::SetFaceName(const wxString
& facename
)
619 m_faceName
= facename
;
622 if ( HasNativeFont() )
624 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
629 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
631 m_encoding
= encoding
;
634 if ( HasNativeFont() )
636 wxNativeEncodingInfo info
;
637 if ( wxGetNativeFontEncoding(encoding
, &info
) )
639 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
640 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
646 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
648 // previously cached fonts shouldn't be used
651 m_nativeFontInfo
= info
;
653 // set all the other font parameters from the native font info
657 // ----------------------------------------------------------------------------
659 // ----------------------------------------------------------------------------
661 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
667 wxFont::wxFont(const wxNativeFontInfo
& info
)
672 Create( info
.GetPointSize(),
676 info
.GetUnderlined(),
678 info
.GetEncoding() );
680 (void) Create(info
.GetXFontName());
684 bool wxFont::Create( int pointSize
,
689 const wxString
& face
,
690 wxFontEncoding encoding
)
694 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
695 underlined
, face
, encoding
);
700 bool wxFont::Create(const wxString
& fontname
)
702 // VZ: does this really happen?
703 if ( fontname
.empty() )
705 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
710 m_refData
= new wxFontRefData(fontname
);
715 void wxFont::Unshare()
719 m_refData
= new wxFontRefData();
723 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
733 // ----------------------------------------------------------------------------
735 // ----------------------------------------------------------------------------
737 int wxFont::GetPointSize() const
739 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
741 return M_FONTDATA
->m_pointSize
;
744 wxString
wxFont::GetFaceName() const
746 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
748 return M_FONTDATA
->m_faceName
;
751 int wxFont::GetFamily() const
753 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
755 return M_FONTDATA
->m_family
;
758 int wxFont::GetStyle() const
760 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
762 return M_FONTDATA
->m_style
;
765 int wxFont::GetWeight() const
767 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
769 return M_FONTDATA
->m_weight
;
772 bool wxFont::GetUnderlined() const
774 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
776 return M_FONTDATA
->m_underlined
;
779 wxFontEncoding
wxFont::GetEncoding() const
781 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
783 return M_FONTDATA
->m_encoding
;
786 bool wxFont::GetNoAntiAliasing()
788 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
790 return M_FONTDATA
->m_noAA
;
793 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
795 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
798 if ( M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty() )
802 return &(M_FONTDATA
->m_nativeFontInfo
);
805 bool wxFont::IsFixedWidth() const
807 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
810 if ( M_FONTDATA
->HasNativeFont() )
812 // the monospace fonts are supposed to have "M" in the spacing field
813 wxString spacing
= M_FONTDATA
->
814 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
816 return spacing
.Upper() == _T('M');
820 return wxFontBase::IsFixedWidth();
823 // ----------------------------------------------------------------------------
824 // change font attributes
825 // ----------------------------------------------------------------------------
827 void wxFont::SetPointSize(int pointSize
)
831 M_FONTDATA
->SetPointSize(pointSize
);
834 void wxFont::SetFamily(int family
)
838 M_FONTDATA
->SetFamily(family
);
841 void wxFont::SetStyle(int style
)
845 M_FONTDATA
->SetStyle(style
);
848 void wxFont::SetWeight(int weight
)
852 M_FONTDATA
->SetWeight(weight
);
855 void wxFont::SetFaceName(const wxString
& faceName
)
859 M_FONTDATA
->SetFaceName(faceName
);
862 void wxFont::SetUnderlined(bool underlined
)
866 M_FONTDATA
->SetUnderlined(underlined
);
869 void wxFont::SetEncoding(wxFontEncoding encoding
)
873 M_FONTDATA
->SetEncoding(encoding
);
876 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
880 M_FONTDATA
->SetNativeFontInfo( info
);
883 void wxFont::SetNoAntiAliasing( bool no
)
887 M_FONTDATA
->SetNoAntiAliasing( no
);
890 // ----------------------------------------------------------------------------
891 // get internal representation of font
892 // ----------------------------------------------------------------------------
895 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
897 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
898 extern GdkFont
*GtkGetDefaultGuiFont()
900 if (!g_systemDefaultGuiFont
)
902 GtkWidget
*widget
= gtk_button_new();
903 GtkStyle
*def
= gtk_rc_get_style( widget
);
906 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
910 def
= gtk_widget_get_default_style();
912 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
914 gtk_widget_destroy( widget
);
918 // already have it, but ref it once more before returning
919 gdk_font_ref(g_systemDefaultGuiFont
);
922 return g_systemDefaultGuiFont
;
925 GdkFont
*wxFont::GetInternalFont( float scale
) const
927 GdkFont
*font
= (GdkFont
*) NULL
;
929 wxCHECK_MSG( Ok(), font
, wxT("invalid font") )
931 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
932 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
934 wxScaledFontList
& list
= M_FONTDATA
->m_scaled_xfonts
;
935 wxScaledFontList::iterator i
= list
.find(int_scale
);
936 if ( i
!= list
.end() )
940 else // we don't have this font in this size yet
942 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
944 font
= GtkGetDefaultGuiFont();
949 // do we have the XLFD?
950 if ( M_FONTDATA
->HasNativeFont() )
952 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
955 // no XLFD of no exact match - try the approximate one now
959 font
= wxLoadQueryNearestFont( point_scale
,
960 M_FONTDATA
->m_family
,
962 M_FONTDATA
->m_weight
,
963 M_FONTDATA
->m_underlined
,
964 M_FONTDATA
->m_faceName
,
965 M_FONTDATA
->m_encoding
,
969 M_FONTDATA
->ReInit(xfontname
);
976 list
[int_scale
] = font
;
980 // it's quite useless to make it a wxCHECK because we're going to crash
982 wxASSERT_MSG( font
, wxT("could not load any font?") );
986 #endif // not GTK 2.0