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();
89 // setters: all of them also take care to modify m_nativeFontInfo if we
90 // have it so as to not lose the information not carried by our fields
91 void SetPointSize(int pointSize
);
92 void SetFamily(int family
);
93 void SetStyle(int style
);
94 void SetWeight(int weight
);
95 void SetUnderlined(bool underlined
);
96 void SetFaceName(const wxString
& facename
);
97 void SetEncoding(wxFontEncoding encoding
);
99 void SetNoAntiAliasing( bool no
= TRUE
) { m_noAA
= no
; }
100 bool GetNoAntiAliasing() const { return m_noAA
; }
102 // and this one also modifies all the other font data fields
103 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
105 // debugger helper: shows what the font really is
107 // VZ: I need this as my gdb either shows wildly wrong values or crashes
108 // when I ask it to "p fontRefData" :-(
109 #if defined(__WXDEBUG__) && !defined(__WXGTK20__)
112 wxPrintf(_T("%s-%s-%s-%d-%d\n"),
114 m_weight
== wxFONTWEIGHT_NORMAL
116 : m_weight
== wxFONTWEIGHT_BOLD
119 m_style
== wxFONTSTYLE_NORMAL
? _T("regular") : _T("italic"),
126 // common part of all ctors
127 void Init(int pointSize
,
132 const wxString
& faceName
,
133 wxFontEncoding encoding
);
135 // set all fields from (already initialized and valid) m_nativeFontInfo
136 void InitFromNative();
139 // clear m_scaled_xfonts if any
140 void ClearGdkFonts();
143 // the map of font sizes to "GdkFont *"
144 wxScaledFontList m_scaled_xfonts
;
145 #endif // GTK 2.0/1.x
153 wxFontEncoding m_encoding
; // Unused under GTK 2.0
154 bool m_noAA
; // No anti-aliasing
156 // The native font info, basicly an XFLD under GTK 1.2 and
157 // the pango font description under GTK 2.0.
158 wxNativeFontInfo m_nativeFontInfo
;
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 void wxFontRefData::Init(int pointSize
,
172 const wxString
& faceName
,
173 wxFontEncoding encoding
)
175 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
177 m_faceName
= faceName
;
179 // we accept both wxDEFAULT and wxNORMAL here - should we?
180 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
181 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
183 // and here, do we really want to forbid creation of the font of the size
184 // 90 (the value of wxDEFAULT)??
185 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
186 ? wxDEFAULT_FONT_SIZE
189 m_underlined
= underlined
;
190 m_encoding
= encoding
;
195 // Create native font info
196 m_nativeFontInfo
.description
= pango_font_description_new();
198 // And set its values
199 if (!m_faceName
.empty())
201 pango_font_description_set_family( m_nativeFontInfo
.description
, wxGTK_CONV(m_faceName
) );
207 case wxFONTFAMILY_MODERN
:
208 case wxFONTFAMILY_TELETYPE
:
209 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
211 case wxFONTFAMILY_ROMAN
:
212 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
214 case wxFONTFAMILY_SWISS
:
215 // SWISS = sans serif
217 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
223 SetPointSize( m_pointSize
);
224 SetWeight( m_weight
);
228 void wxFontRefData::InitFromNative()
234 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
237 m_faceName
= wxGTK_CONV_BACK( pango_font_description_get_family( desc
) );
239 // Pango sometimes needs to have a size
240 int pango_size
= pango_font_description_get_size( desc
);
242 m_nativeFontInfo
.SetPointSize(12);
244 m_pointSize
= m_nativeFontInfo
.GetPointSize();
245 m_style
= m_nativeFontInfo
.GetStyle();
246 m_weight
= m_nativeFontInfo
.GetWeight();
248 if (m_faceName
== wxT("monospace"))
250 m_family
= wxFONTFAMILY_TELETYPE
;
252 else if (m_faceName
== wxT("sans"))
254 m_family
= wxFONTFAMILY_SWISS
;
256 else if (m_faceName
== wxT("serif"))
258 m_family
= wxFONTFAMILY_ROMAN
;
262 m_family
= wxFONTFAMILY_UNKNOWN
;
265 // Pango description are never underlined (?)
266 m_underlined
= FALSE
;
268 // Cannot we choose that
269 m_encoding
= wxFONTENCODING_SYSTEM
;
271 // get the font parameters from the XLFD
272 // -------------------------------------
274 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
276 m_weight
= wxFONTWEIGHT_NORMAL
;
278 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
279 if ( !w
.empty() && w
!= _T('*') )
281 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
283 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
284 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
285 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
287 m_weight
= wxFONTWEIGHT_BOLD
;
289 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
291 m_weight
= wxFONTWEIGHT_LIGHT
;
295 switch ( wxToupper(*m_nativeFontInfo
.
296 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
298 case _T('I'): // italique
299 m_style
= wxFONTSTYLE_ITALIC
;
302 case _T('O'): // oblique
303 m_style
= wxFONTSTYLE_SLANT
;
307 m_style
= wxFONTSTYLE_NORMAL
;
311 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
313 // size in XLFD is in 10 point units
314 m_pointSize
= (int)(ptSize
/ 10);
318 m_pointSize
= wxDEFAULT_FONT_SIZE
;
321 // examine the spacing: if the font is monospaced, assume wxTELETYPE
322 // family for compatibility with the old code which used it instead of
324 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
326 m_family
= wxFONTFAMILY_TELETYPE
;
328 else // not monospaceed
330 // don't even try guessing it, it doesn't work for too many fonts
332 m_family
= wxFONTFAMILY_UNKNOWN
;
335 // X fonts are never underlined...
336 m_underlined
= FALSE
;
338 // deal with font encoding
340 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
341 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
343 if ( registry
== _T("ISO8859") )
346 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
348 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
351 else if ( registry
== _T("MICROSOFT") )
354 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
356 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
359 else if ( registry
== _T("KOI8") )
361 m_encoding
= wxFONTENCODING_KOI8
;
363 else // unknown encoding
365 // may be give a warning here? or use wxFontMapper?
366 m_encoding
= wxFONTENCODING_SYSTEM
;
368 #endif // GTK 2.0/1.x
371 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
374 m_pointSize
= data
.m_pointSize
;
375 m_family
= data
.m_family
;
376 m_style
= data
.m_style
;
377 m_weight
= data
.m_weight
;
379 m_underlined
= data
.m_underlined
;
381 m_faceName
= data
.m_faceName
;
382 m_encoding
= data
.m_encoding
;
384 m_noAA
= data
.m_noAA
;
386 // Forces a copy of the internal data. wxNativeFontInfo should probably
387 // have a copy ctor and assignment operator to fix this properly but that
388 // would break binary compatibility...
389 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
392 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
393 int weight
, bool underlined
,
394 const wxString
& faceName
,
395 wxFontEncoding encoding
)
397 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
400 wxFontRefData::wxFontRefData(const wxString
& fontname
)
402 // VZ: FromString() should really work in both cases, doesn't it?
404 m_nativeFontInfo
.FromString( fontname
);
406 m_nativeFontInfo
.SetXFontName(fontname
);
407 #endif // GTK 2.0/1.x
412 void wxFontRefData::ClearGdkFonts()
415 for ( wxScaledFontList::iterator i
= m_scaled_xfonts
.begin();
416 i
!= m_scaled_xfonts
.end();
419 GdkFont
*font
= i
->second
;
420 gdk_font_unref( font
);
423 m_scaled_xfonts
.clear();
427 wxFontRefData::~wxFontRefData()
432 // ----------------------------------------------------------------------------
433 // wxFontRefData SetXXX()
434 // ----------------------------------------------------------------------------
436 void wxFontRefData::SetPointSize(int pointSize
)
438 m_pointSize
= pointSize
;
441 m_nativeFontInfo
.SetPointSize(pointSize
);
443 if ( HasNativeFont() )
446 if ( pointSize
== -1 )
449 size
.Printf(_T("%d"), 10*pointSize
);
451 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
456 void wxFontRefData::SetFamily(int family
)
460 // TODO: what are we supposed to do with m_nativeFontInfo here?
463 void wxFontRefData::SetStyle(int style
)
468 m_nativeFontInfo
.SetStyle((wxFontStyle
)style
);
470 if ( HasNativeFont() )
475 case wxFONTSTYLE_ITALIC
:
479 case wxFONTSTYLE_SLANT
:
484 wxFAIL_MSG( _T("unknown font style") );
487 case wxFONTSTYLE_NORMAL
:
491 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
496 void wxFontRefData::SetWeight(int weight
)
501 m_nativeFontInfo
.SetWeight((wxFontWeight
)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 m_nativeFontInfo
.SetFaceName(facename
);
544 if ( HasNativeFont() )
546 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
551 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
553 m_encoding
= encoding
;
556 if ( HasNativeFont() )
558 wxNativeEncodingInfo info
;
559 if ( wxGetNativeFontEncoding(encoding
, &info
) )
561 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
562 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
568 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
570 // previously cached fonts shouldn't be used
573 m_nativeFontInfo
= info
;
575 // set all the other font parameters from the native font info
579 // ----------------------------------------------------------------------------
581 // ----------------------------------------------------------------------------
583 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
589 wxFont::wxFont(const wxNativeFontInfo
& info
)
594 Create( info
.GetPointSize(),
598 info
.GetUnderlined(),
600 info
.GetEncoding() );
602 (void) Create(info
.GetXFontName());
606 bool wxFont::Create( int pointSize
,
611 const wxString
& face
,
612 wxFontEncoding encoding
)
616 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
617 underlined
, face
, encoding
);
622 bool wxFont::Create(const wxString
& fontname
)
624 // VZ: does this really happen?
625 if ( fontname
.empty() )
627 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
632 m_refData
= new wxFontRefData(fontname
);
637 void wxFont::Unshare()
641 m_refData
= new wxFontRefData();
645 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
655 // ----------------------------------------------------------------------------
657 // ----------------------------------------------------------------------------
659 int wxFont::GetPointSize() const
661 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
664 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetPointSize()
665 : M_FONTDATA
->m_pointSize
;
667 return M_FONTDATA
->m_pointSize
;
671 wxString
wxFont::GetFaceName() const
673 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
676 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetFaceName()
677 : M_FONTDATA
->m_faceName
;
679 return M_FONTDATA
->m_faceName
;
683 int wxFont::GetFamily() const
685 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
688 int ret
= M_FONTDATA
->m_family
;
689 if (M_FONTDATA
->HasNativeFont())
690 // wxNativeFontInfo::GetFamily is expensive, must not call more than once
691 ret
= M_FONTDATA
->m_nativeFontInfo
.GetFamily();
693 if (ret
== wxFONTFAMILY_DEFAULT
)
694 ret
= M_FONTDATA
->m_family
;
698 return M_FONTDATA
->m_family
;
702 int wxFont::GetStyle() const
704 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
707 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetStyle()
708 : M_FONTDATA
->m_style
;
710 return M_FONTDATA
->m_style
;
714 int wxFont::GetWeight() const
716 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
719 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetWeight()
720 : M_FONTDATA
->m_weight
;
722 return M_FONTDATA
->m_weight
;
726 bool wxFont::GetUnderlined() const
728 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
730 return M_FONTDATA
->m_underlined
;
733 wxFontEncoding
wxFont::GetEncoding() const
735 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
737 // m_encoding is unused in wxGTK2, return encoding that the user set.
738 return M_FONTDATA
->m_encoding
;
741 bool wxFont::GetNoAntiAliasing() const
743 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
745 return M_FONTDATA
->m_noAA
;
748 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
750 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
753 if ( !M_FONTDATA
->HasNativeFont() )
755 // NB: this call has important side-effect: it not only finds
756 // GdkFont representation, it also initializes m_nativeFontInfo
757 // by calling its SetXFontName method
762 return &(M_FONTDATA
->m_nativeFontInfo
);
765 bool wxFont::IsFixedWidth() const
767 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
770 if ( M_FONTDATA
->HasNativeFont() )
772 // the monospace fonts are supposed to have "M" in the spacing field
773 wxString spacing
= M_FONTDATA
->
774 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
776 return spacing
.Upper() == _T('M');
780 return wxFontBase::IsFixedWidth();
783 // ----------------------------------------------------------------------------
784 // change font attributes
785 // ----------------------------------------------------------------------------
787 void wxFont::SetPointSize(int pointSize
)
791 M_FONTDATA
->SetPointSize(pointSize
);
794 void wxFont::SetFamily(int family
)
798 M_FONTDATA
->SetFamily(family
);
801 void wxFont::SetStyle(int style
)
805 M_FONTDATA
->SetStyle(style
);
808 void wxFont::SetWeight(int weight
)
812 M_FONTDATA
->SetWeight(weight
);
815 void wxFont::SetFaceName(const wxString
& faceName
)
819 M_FONTDATA
->SetFaceName(faceName
);
822 void wxFont::SetUnderlined(bool underlined
)
826 M_FONTDATA
->SetUnderlined(underlined
);
829 void wxFont::SetEncoding(wxFontEncoding encoding
)
833 M_FONTDATA
->SetEncoding(encoding
);
836 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
840 M_FONTDATA
->SetNativeFontInfo( info
);
843 void wxFont::SetNoAntiAliasing( bool no
)
847 M_FONTDATA
->SetNoAntiAliasing( no
);
850 // ----------------------------------------------------------------------------
851 // get internal representation of font
852 // ----------------------------------------------------------------------------
855 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
857 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
858 extern GdkFont
*GtkGetDefaultGuiFont()
860 if (!g_systemDefaultGuiFont
)
862 GtkWidget
*widget
= gtk_button_new();
863 GtkStyle
*def
= gtk_rc_get_style( widget
);
866 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
870 def
= gtk_widget_get_default_style();
872 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
874 gtk_widget_destroy( widget
);
878 // already have it, but ref it once more before returning
879 gdk_font_ref(g_systemDefaultGuiFont
);
882 return g_systemDefaultGuiFont
;
885 GdkFont
*wxFont::GetInternalFont( float scale
) const
887 GdkFont
*font
= (GdkFont
*) NULL
;
889 wxCHECK_MSG( Ok(), font
, wxT("invalid font") )
891 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
892 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
894 wxScaledFontList
& list
= M_FONTDATA
->m_scaled_xfonts
;
895 wxScaledFontList::iterator i
= list
.find(int_scale
);
896 if ( i
!= list
.end() )
900 else // we don't have this font in this size yet
902 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
904 font
= GtkGetDefaultGuiFont();
909 // do we have the XLFD?
910 if ( int_scale
== 100 && M_FONTDATA
->HasNativeFont() )
912 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
915 // no XLFD of no exact match - try the approximate one now
919 font
= wxLoadQueryNearestFont( point_scale
,
920 M_FONTDATA
->m_family
,
922 M_FONTDATA
->m_weight
,
923 M_FONTDATA
->m_underlined
,
924 M_FONTDATA
->m_faceName
,
925 M_FONTDATA
->m_encoding
,
927 // NB: wxFont::GetNativeFontInfo relies on this
928 // side-effect of GetInternalFont
929 if ( int_scale
== 100 )
930 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(xfontname
);
936 list
[int_scale
] = font
;
940 // it's quite useless to make it a wxCHECK because we're going to crash
942 wxASSERT_MSG( font
, wxT("could not load any font?") );
946 #endif // not GTK 2.0