1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling and Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
22 #include "wx/fontutil.h"
23 #include "wx/cmndata.h"
26 #include "wx/gdicmn.h"
27 #include "wx/tokenzr.h"
28 #include "wx/settings.h"
32 #include "wx/gtk1/private.h"
33 #include <gdk/gdkprivate.h>
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // the default size (in points) for the fonts
40 static const int wxDEFAULT_FONT_SIZE
= 12;
42 // ----------------------------------------------------------------------------
43 // wxScaledFontList: maps the font sizes to the GDK fonts for the given font
44 // ----------------------------------------------------------------------------
46 WX_DECLARE_HASH_MAP(int, GdkFont
*, wxIntegerHash
, wxIntegerEqual
,
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 class wxFontRefData
: public wxObjectRefData
56 // from broken down font parameters, also default ctor
57 wxFontRefData(int size
= -1,
58 int family
= wxFONTFAMILY_DEFAULT
,
59 int style
= wxFONTSTYLE_NORMAL
,
60 int weight
= wxFONTWEIGHT_NORMAL
,
61 bool underlined
= FALSE
,
62 const wxString
& faceName
= wxEmptyString
,
63 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
66 wxFontRefData(const wxString
& fontname
);
69 wxFontRefData( const wxFontRefData
& data
);
71 virtual ~wxFontRefData();
73 // do we have the native font info?
74 bool HasNativeFont() const
76 // only use m_nativeFontInfo if it had been initialized
77 return !m_nativeFontInfo
.IsDefault();
80 // setters: all of them also take care to modify m_nativeFontInfo if we
81 // have it so as to not lose the information not carried by our fields
82 void SetPointSize(int pointSize
);
83 void SetFamily(int family
);
84 void SetStyle(int style
);
85 void SetWeight(int weight
);
86 void SetUnderlined(bool underlined
);
87 void SetFaceName(const wxString
& facename
);
88 void SetEncoding(wxFontEncoding encoding
);
90 void SetNoAntiAliasing( bool no
= TRUE
) { m_noAA
= no
; }
91 bool GetNoAntiAliasing() const { return m_noAA
; }
93 // and this one also modifies all the other font data fields
94 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
96 // debugger helper: shows what the font really is
98 // VZ: I need this as my gdb either shows wildly wrong values or crashes
99 // when I ask it to "p fontRefData" :-(
100 #if defined(__WXDEBUG__)
103 wxPrintf(_T("%s-%s-%s-%d-%d\n"),
105 m_weight
== wxFONTWEIGHT_NORMAL
107 : m_weight
== wxFONTWEIGHT_BOLD
110 m_style
== wxFONTSTYLE_NORMAL
? _T("regular") : _T("italic"),
117 // common part of all ctors
118 void Init(int pointSize
,
123 const wxString
& faceName
,
124 wxFontEncoding encoding
);
126 // set all fields from (already initialized and valid) m_nativeFontInfo
127 void InitFromNative();
130 // clear m_scaled_xfonts if any
131 void ClearGdkFonts();
133 // the map of font sizes to "GdkFont *"
134 wxScaledFontList m_scaled_xfonts
;
142 wxFontEncoding m_encoding
; // Unused under GTK 2.0
143 bool m_noAA
; // No anti-aliasing
145 // The native font info, basicly an XFLD under GTK 1.2 and
146 // the pango font description under GTK 2.0.
147 wxNativeFontInfo m_nativeFontInfo
;
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
156 void wxFontRefData::Init(int pointSize
,
161 const wxString
& faceName
,
162 wxFontEncoding encoding
)
164 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
166 m_faceName
= faceName
;
168 // we accept both wxDEFAULT and wxNORMAL here - should we?
169 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
170 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
172 // and here, do we really want to forbid creation of the font of the size
173 // 90 (the value of wxDEFAULT)??
174 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
175 ? wxDEFAULT_FONT_SIZE
178 m_underlined
= underlined
;
179 m_encoding
= encoding
;
184 void wxFontRefData::InitFromNative()
188 // get the font parameters from the XLFD
189 // -------------------------------------
191 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
193 m_weight
= wxFONTWEIGHT_NORMAL
;
195 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
196 if ( !w
.empty() && w
!= _T('*') )
198 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
200 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
201 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
202 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
204 m_weight
= wxFONTWEIGHT_BOLD
;
206 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
208 m_weight
= wxFONTWEIGHT_LIGHT
;
212 switch ( wxToupper(*m_nativeFontInfo
.
213 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
215 case _T('I'): // italique
216 m_style
= wxFONTSTYLE_ITALIC
;
219 case _T('O'): // oblique
220 m_style
= wxFONTSTYLE_SLANT
;
224 m_style
= wxFONTSTYLE_NORMAL
;
228 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
230 // size in XLFD is in 10 point units
231 m_pointSize
= (int)(ptSize
/ 10);
235 m_pointSize
= wxDEFAULT_FONT_SIZE
;
238 // examine the spacing: if the font is monospaced, assume wxTELETYPE
239 // family for compatibility with the old code which used it instead of
241 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
243 m_family
= wxFONTFAMILY_TELETYPE
;
245 else // not monospaceed
247 // don't even try guessing it, it doesn't work for too many fonts
249 m_family
= wxFONTFAMILY_UNKNOWN
;
252 // X fonts are never underlined...
253 m_underlined
= FALSE
;
255 // deal with font encoding
257 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
258 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
260 if ( registry
== _T("ISO8859") )
263 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
265 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
268 else if ( registry
== _T("MICROSOFT") )
271 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
273 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
276 else if ( registry
== _T("KOI8") )
278 m_encoding
= wxFONTENCODING_KOI8
;
280 else // unknown encoding
282 // may be give a warning here? or use wxFontMapper?
283 m_encoding
= wxFONTENCODING_SYSTEM
;
287 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
290 m_pointSize
= data
.m_pointSize
;
291 m_family
= data
.m_family
;
292 m_style
= data
.m_style
;
293 m_weight
= data
.m_weight
;
295 m_underlined
= data
.m_underlined
;
297 m_faceName
= data
.m_faceName
;
298 m_encoding
= data
.m_encoding
;
300 m_noAA
= data
.m_noAA
;
302 // Forces a copy of the internal data. wxNativeFontInfo should probably
303 // have a copy ctor and assignment operator to fix this properly but that
304 // would break binary compatibility...
305 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
308 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
309 int weight
, bool underlined
,
310 const wxString
& faceName
,
311 wxFontEncoding encoding
)
313 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
316 wxFontRefData::wxFontRefData(const wxString
& fontname
)
318 // FromString() should really work in GTK1 too, doesn't it?
319 m_nativeFontInfo
.SetXFontName(fontname
);
324 void wxFontRefData::ClearGdkFonts()
326 for ( wxScaledFontList::iterator i
= m_scaled_xfonts
.begin();
327 i
!= m_scaled_xfonts
.end();
330 GdkFont
*font
= i
->second
;
331 gdk_font_unref( font
);
334 m_scaled_xfonts
.clear();
337 wxFontRefData::~wxFontRefData()
342 // ----------------------------------------------------------------------------
343 // wxFontRefData SetXXX()
344 // ----------------------------------------------------------------------------
346 void wxFontRefData::SetPointSize(int pointSize
)
348 m_pointSize
= pointSize
;
350 if ( HasNativeFont() )
353 if ( pointSize
== -1 )
356 size
.Printf(_T("%d"), 10*pointSize
);
358 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
362 void wxFontRefData::SetFamily(int family
)
366 // TODO: what are we supposed to do with m_nativeFontInfo here?
369 void wxFontRefData::SetStyle(int style
)
373 if ( HasNativeFont() )
378 case wxFONTSTYLE_ITALIC
:
382 case wxFONTSTYLE_SLANT
:
387 wxFAIL_MSG( _T("unknown font style") );
390 case wxFONTSTYLE_NORMAL
:
394 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
398 void wxFontRefData::SetWeight(int weight
)
402 if ( HasNativeFont() )
407 case wxFONTWEIGHT_BOLD
:
408 boldness
= _T("bold");
411 case wxFONTWEIGHT_LIGHT
:
412 boldness
= _T("light");
416 wxFAIL_MSG( _T("unknown font weight") );
419 case wxFONTWEIGHT_NORMAL
:
421 boldness
= _T("medium");
424 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
428 void wxFontRefData::SetUnderlined(bool underlined
)
430 m_underlined
= underlined
;
432 // the XLFD doesn't have "underlined" field anyhow
435 void wxFontRefData::SetFaceName(const wxString
& facename
)
437 m_faceName
= facename
;
439 if ( HasNativeFont() )
441 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
445 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
447 m_encoding
= encoding
;
449 if ( HasNativeFont() )
451 wxNativeEncodingInfo info
;
452 if ( wxGetNativeFontEncoding(encoding
, &info
) )
454 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
455 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
460 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
462 // previously cached fonts shouldn't be used
465 m_nativeFontInfo
= info
;
467 // set all the other font parameters from the native font info
471 // ----------------------------------------------------------------------------
473 // ----------------------------------------------------------------------------
475 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
477 wxFont::wxFont(const wxNativeFontInfo
& info
)
479 (void) Create(info
.GetXFontName());
482 bool wxFont::Create( int pointSize
,
487 const wxString
& face
,
488 wxFontEncoding encoding
)
492 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
493 underlined
, face
, encoding
);
498 bool wxFont::Create(const wxString
& fontname
)
500 // VZ: does this really happen?
501 if ( fontname
.empty() )
503 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
508 m_refData
= new wxFontRefData(fontname
);
513 void wxFont::Unshare()
517 m_refData
= new wxFontRefData();
521 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
531 // ----------------------------------------------------------------------------
533 // ----------------------------------------------------------------------------
535 int wxFont::GetPointSize() const
537 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
540 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetPointSize()
541 : M_FONTDATA
->m_pointSize
;
543 return M_FONTDATA
->m_pointSize
;
547 wxString
wxFont::GetFaceName() const
549 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
552 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetFaceName()
553 : M_FONTDATA
->m_faceName
;
555 return M_FONTDATA
->m_faceName
;
559 int wxFont::GetFamily() const
561 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
564 int ret
= M_FONTDATA
->m_family
;
565 if (M_FONTDATA
->HasNativeFont())
566 // wxNativeFontInfo::GetFamily is expensive, must not call more than once
567 ret
= M_FONTDATA
->m_nativeFontInfo
.GetFamily();
569 if (ret
== wxFONTFAMILY_DEFAULT
)
570 ret
= M_FONTDATA
->m_family
;
574 return M_FONTDATA
->m_family
;
578 int wxFont::GetStyle() const
580 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
583 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetStyle()
584 : M_FONTDATA
->m_style
;
586 return M_FONTDATA
->m_style
;
590 int wxFont::GetWeight() const
592 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
595 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetWeight()
596 : M_FONTDATA
->m_weight
;
598 return M_FONTDATA
->m_weight
;
602 bool wxFont::GetUnderlined() const
604 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
606 return M_FONTDATA
->m_underlined
;
609 wxFontEncoding
wxFont::GetEncoding() const
611 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
613 // m_encoding is unused in wxGTK2, return encoding that the user set.
614 return M_FONTDATA
->m_encoding
;
617 bool wxFont::GetNoAntiAliasing() const
619 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
621 return M_FONTDATA
->m_noAA
;
624 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
626 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
628 if ( !M_FONTDATA
->HasNativeFont() )
630 // NB: this call has important side-effect: it not only finds
631 // GdkFont representation, it also initializes m_nativeFontInfo
632 // by calling its SetXFontName method
636 return &(M_FONTDATA
->m_nativeFontInfo
);
639 bool wxFont::IsFixedWidth() const
641 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
643 if ( M_FONTDATA
->HasNativeFont() )
645 // the monospace fonts are supposed to have "M" in the spacing field
646 wxString spacing
= M_FONTDATA
->
647 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
649 return spacing
.Upper() == _T('M');
652 return wxFontBase::IsFixedWidth();
655 // ----------------------------------------------------------------------------
656 // change font attributes
657 // ----------------------------------------------------------------------------
659 void wxFont::SetPointSize(int pointSize
)
663 M_FONTDATA
->SetPointSize(pointSize
);
666 void wxFont::SetFamily(int family
)
670 M_FONTDATA
->SetFamily(family
);
673 void wxFont::SetStyle(int style
)
677 M_FONTDATA
->SetStyle(style
);
680 void wxFont::SetWeight(int weight
)
684 M_FONTDATA
->SetWeight(weight
);
687 void wxFont::SetFaceName(const wxString
& faceName
)
691 M_FONTDATA
->SetFaceName(faceName
);
694 void wxFont::SetUnderlined(bool underlined
)
698 M_FONTDATA
->SetUnderlined(underlined
);
701 void wxFont::SetEncoding(wxFontEncoding encoding
)
705 M_FONTDATA
->SetEncoding(encoding
);
708 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
712 M_FONTDATA
->SetNativeFontInfo( info
);
715 void wxFont::SetNoAntiAliasing( bool no
)
719 M_FONTDATA
->SetNoAntiAliasing( no
);
722 // ----------------------------------------------------------------------------
723 // get internal representation of font
724 // ----------------------------------------------------------------------------
726 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
728 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
729 extern GdkFont
*GtkGetDefaultGuiFont()
731 if (!g_systemDefaultGuiFont
)
733 GtkWidget
*widget
= gtk_button_new();
734 GtkStyle
*def
= gtk_rc_get_style( widget
);
737 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
741 def
= gtk_widget_get_default_style();
743 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
745 gtk_widget_destroy( widget
);
749 // already have it, but ref it once more before returning
750 gdk_font_ref(g_systemDefaultGuiFont
);
753 return g_systemDefaultGuiFont
;
756 GdkFont
*wxFont::GetInternalFont( float scale
) const
758 GdkFont
*font
= (GdkFont
*) NULL
;
760 wxCHECK_MSG( Ok(), font
, wxT("invalid font") )
762 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
763 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
765 wxScaledFontList
& list
= M_FONTDATA
->m_scaled_xfonts
;
766 wxScaledFontList::iterator i
= list
.find(int_scale
);
767 if ( i
!= list
.end() )
771 else // we don't have this font in this size yet
773 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
775 font
= GtkGetDefaultGuiFont();
780 // do we have the XLFD?
781 if ( int_scale
== 100 && M_FONTDATA
->HasNativeFont() )
783 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
786 // no XLFD of no exact match - try the approximate one now
790 font
= wxLoadQueryNearestFont( point_scale
,
791 M_FONTDATA
->m_family
,
793 M_FONTDATA
->m_weight
,
794 M_FONTDATA
->m_underlined
,
795 M_FONTDATA
->m_faceName
,
796 M_FONTDATA
->m_encoding
,
798 // NB: wxFont::GetNativeFontInfo relies on this
799 // side-effect of GetInternalFont
800 if ( int_scale
== 100 )
801 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(xfontname
);
807 list
[int_scale
] = font
;
811 // it's quite useless to make it a wxCHECK because we're going to crash
813 wxASSERT_MSG( font
, wxT("could not load any font?") );