1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/font.cpp
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"
25 #include "wx/settings.h"
26 #include "wx/cmndata.h"
29 #include "wx/fontutil.h"
31 #include "wx/gdicmn.h"
32 #include "wx/tokenzr.h"
36 #include "wx/gtk1/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
80 // only use m_nativeFontInfo if it had been initialized
81 return !m_nativeFontInfo
.IsDefault();
84 // setters: all of them also take care to modify m_nativeFontInfo if we
85 // have it so as to not lose the information not carried by our fields
86 void SetPointSize(int pointSize
);
87 void SetFamily(int family
);
88 void SetStyle(int style
);
89 void SetWeight(int weight
);
90 void SetUnderlined(bool underlined
);
91 bool SetFaceName(const wxString
& facename
);
92 void SetEncoding(wxFontEncoding encoding
);
94 void SetNoAntiAliasing( bool no
= true ) { m_noAA
= no
; }
95 bool GetNoAntiAliasing() const { return m_noAA
; }
97 // and this one also modifies all the other font data fields
98 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
100 // debugger helper: shows what the font really is
102 // VZ: I need this as my gdb either shows wildly wrong values or crashes
103 // when I ask it to "p fontRefData" :-(
104 #if defined(__WXDEBUG__)
107 wxPrintf(_T("%s-%s-%s-%d-%d\n"),
109 m_weight
== wxFONTWEIGHT_NORMAL
111 : m_weight
== wxFONTWEIGHT_BOLD
114 m_style
== wxFONTSTYLE_NORMAL
? _T("regular") : _T("italic"),
121 // common part of all ctors
122 void Init(int pointSize
,
127 const wxString
& faceName
,
128 wxFontEncoding encoding
);
130 // set all fields from (already initialized and valid) m_nativeFontInfo
131 void InitFromNative();
134 // clear m_scaled_xfonts if any
135 void ClearGdkFonts();
137 // the map of font sizes to "GdkFont *"
138 wxScaledFontList m_scaled_xfonts
;
146 wxFontEncoding m_encoding
; // Unused under GTK 2.0
147 bool m_noAA
; // No anti-aliasing
149 // The native font info, basicly an XFLD under GTK 1.2 and
150 // the pango font description under GTK 2.0.
151 wxNativeFontInfo m_nativeFontInfo
;
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
160 void wxFontRefData::Init(int pointSize
,
165 const wxString
& faceName
,
166 wxFontEncoding encoding
)
168 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
170 m_faceName
= faceName
;
172 // we accept both wxDEFAULT and wxNORMAL here - should we?
173 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
174 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
176 // and here, do we really want to forbid creation of the font of the size
177 // 90 (the value of wxDEFAULT)??
178 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
179 ? wxDEFAULT_FONT_SIZE
182 m_underlined
= underlined
;
183 m_encoding
= encoding
;
188 void wxFontRefData::InitFromNative()
192 // get the font parameters from the XLFD
193 // -------------------------------------
195 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
197 m_weight
= wxFONTWEIGHT_NORMAL
;
199 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
200 if ( !w
.empty() && w
!= _T('*') )
202 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
204 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
205 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
206 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
208 m_weight
= wxFONTWEIGHT_BOLD
;
210 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
212 m_weight
= wxFONTWEIGHT_LIGHT
;
216 switch ( wxToupper(*m_nativeFontInfo
.
217 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
219 case _T('I'): // italique
220 m_style
= wxFONTSTYLE_ITALIC
;
223 case _T('O'): // oblique
224 m_style
= wxFONTSTYLE_SLANT
;
228 m_style
= wxFONTSTYLE_NORMAL
;
232 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
234 // size in XLFD is in 10 point units
235 m_pointSize
= (int)(ptSize
/ 10);
239 m_pointSize
= wxDEFAULT_FONT_SIZE
;
242 // examine the spacing: if the font is monospaced, assume wxTELETYPE
243 // family for compatibility with the old code which used it instead of
245 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
247 m_family
= wxFONTFAMILY_TELETYPE
;
249 else // not monospaceed
251 // don't even try guessing it, it doesn't work for too many fonts
253 m_family
= wxFONTFAMILY_UNKNOWN
;
256 // X fonts are never underlined...
257 m_underlined
= false;
259 // deal with font encoding
261 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
262 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
264 if ( registry
== _T("ISO8859") )
267 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
269 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
272 else if ( registry
== _T("MICROSOFT") )
275 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
277 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
280 else if ( registry
== _T("KOI8") )
282 m_encoding
= wxFONTENCODING_KOI8
;
284 else // unknown encoding
286 // may be give a warning here? or use wxFontMapper?
287 m_encoding
= wxFONTENCODING_SYSTEM
;
291 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
294 m_pointSize
= data
.m_pointSize
;
295 m_family
= data
.m_family
;
296 m_style
= data
.m_style
;
297 m_weight
= data
.m_weight
;
299 m_underlined
= data
.m_underlined
;
301 m_faceName
= data
.m_faceName
;
302 m_encoding
= data
.m_encoding
;
304 m_noAA
= data
.m_noAA
;
306 // Forces a copy of the internal data. wxNativeFontInfo should probably
307 // have a copy ctor and assignment operator to fix this properly but that
308 // would break binary compatibility...
309 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
312 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
313 int weight
, bool underlined
,
314 const wxString
& faceName
,
315 wxFontEncoding encoding
)
317 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
320 wxFontRefData::wxFontRefData(const wxString
& fontname
)
322 // FromString() should really work in GTK1 too, doesn't it?
323 m_nativeFontInfo
.SetXFontName(fontname
);
328 void wxFontRefData::ClearGdkFonts()
330 for ( wxScaledFontList::iterator i
= m_scaled_xfonts
.begin();
331 i
!= m_scaled_xfonts
.end();
334 GdkFont
*font
= i
->second
;
335 gdk_font_unref( font
);
338 m_scaled_xfonts
.clear();
341 wxFontRefData::~wxFontRefData()
346 // ----------------------------------------------------------------------------
347 // wxFontRefData SetXXX()
348 // ----------------------------------------------------------------------------
350 void wxFontRefData::SetPointSize(int pointSize
)
352 m_pointSize
= pointSize
;
354 if ( HasNativeFont() )
357 if ( pointSize
== -1 )
360 size
.Printf(_T("%d"), 10*pointSize
);
362 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
366 void wxFontRefData::SetFamily(int family
)
370 // TODO: what are we supposed to do with m_nativeFontInfo here?
373 void wxFontRefData::SetStyle(int style
)
377 if ( HasNativeFont() )
382 case wxFONTSTYLE_ITALIC
:
386 case wxFONTSTYLE_SLANT
:
391 wxFAIL_MSG( _T("unknown font style") );
394 case wxFONTSTYLE_NORMAL
:
398 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
402 void wxFontRefData::SetWeight(int weight
)
406 if ( HasNativeFont() )
411 case wxFONTWEIGHT_BOLD
:
412 boldness
= _T("bold");
415 case wxFONTWEIGHT_LIGHT
:
416 boldness
= _T("light");
420 wxFAIL_MSG( _T("unknown font weight") );
423 case wxFONTWEIGHT_NORMAL
:
425 boldness
= _T("medium");
428 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
432 void wxFontRefData::SetUnderlined(bool underlined
)
434 m_underlined
= underlined
;
436 // the XLFD doesn't have "underlined" field anyhow
439 bool wxFontRefData::SetFaceName(const wxString
& facename
)
441 m_faceName
= facename
;
443 if ( HasNativeFont() )
445 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
451 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
453 m_encoding
= encoding
;
455 if ( HasNativeFont() )
457 wxNativeEncodingInfo info
;
458 if ( wxGetNativeFontEncoding(encoding
, &info
) )
460 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
461 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
466 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
468 // previously cached fonts shouldn't be used
471 m_nativeFontInfo
= info
;
473 // set all the other font parameters from the native font info
477 // ----------------------------------------------------------------------------
479 // ----------------------------------------------------------------------------
481 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
483 wxFont::wxFont(const wxNativeFontInfo
& info
)
485 (void) Create(info
.GetXFontName());
488 bool wxFont::Create( int pointSize
,
493 const wxString
& face
,
494 wxFontEncoding encoding
)
498 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
499 underlined
, face
, encoding
);
504 bool wxFont::Create(const wxString
& fontname
)
506 // VZ: does this really happen?
507 if ( fontname
.empty() )
509 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
514 m_refData
= new wxFontRefData(fontname
);
519 void wxFont::Unshare()
523 m_refData
= new wxFontRefData();
527 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
537 // ----------------------------------------------------------------------------
539 // ----------------------------------------------------------------------------
541 int wxFont::GetPointSize() const
543 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
546 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetPointSize()
547 : M_FONTDATA
->m_pointSize
;
549 return M_FONTDATA
->m_pointSize
;
553 wxString
wxFont::GetFaceName() const
555 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
558 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetFaceName()
559 : M_FONTDATA
->m_faceName
;
561 return M_FONTDATA
->m_faceName
;
565 int wxFont::GetFamily() const
567 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
570 int ret
= M_FONTDATA
->m_family
;
571 if (M_FONTDATA
->HasNativeFont())
572 // wxNativeFontInfo::GetFamily is expensive, must not call more than once
573 ret
= M_FONTDATA
->m_nativeFontInfo
.GetFamily();
575 if (ret
== wxFONTFAMILY_DEFAULT
)
576 ret
= M_FONTDATA
->m_family
;
580 return M_FONTDATA
->m_family
;
584 int wxFont::GetStyle() const
586 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
589 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetStyle()
590 : M_FONTDATA
->m_style
;
592 return M_FONTDATA
->m_style
;
596 int wxFont::GetWeight() const
598 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
601 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetWeight()
602 : M_FONTDATA
->m_weight
;
604 return M_FONTDATA
->m_weight
;
608 bool wxFont::GetUnderlined() const
610 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
612 return M_FONTDATA
->m_underlined
;
615 wxFontEncoding
wxFont::GetEncoding() const
617 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
619 // m_encoding is unused in wxGTK2, return encoding that the user set.
620 return M_FONTDATA
->m_encoding
;
623 bool wxFont::GetNoAntiAliasing() const
625 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
627 return M_FONTDATA
->m_noAA
;
630 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
632 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
634 if ( !M_FONTDATA
->HasNativeFont() )
636 // NB: this call has important side-effect: it not only finds
637 // GdkFont representation, it also initializes m_nativeFontInfo
638 // by calling its SetXFontName method
642 return &(M_FONTDATA
->m_nativeFontInfo
);
645 bool wxFont::IsFixedWidth() const
647 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
649 if ( M_FONTDATA
->HasNativeFont() )
651 // the monospace fonts are supposed to have "M" in the spacing field
652 wxString spacing
= M_FONTDATA
->
653 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
655 return spacing
.Upper() == _T('M');
658 return wxFontBase::IsFixedWidth();
661 // ----------------------------------------------------------------------------
662 // change font attributes
663 // ----------------------------------------------------------------------------
665 void wxFont::SetPointSize(int pointSize
)
669 M_FONTDATA
->SetPointSize(pointSize
);
672 void wxFont::SetFamily(int family
)
676 M_FONTDATA
->SetFamily(family
);
679 void wxFont::SetStyle(int style
)
683 M_FONTDATA
->SetStyle(style
);
686 void wxFont::SetWeight(int weight
)
690 M_FONTDATA
->SetWeight(weight
);
693 bool wxFont::SetFaceName(const wxString
& faceName
)
697 return M_FONTDATA
->SetFaceName(faceName
) &&
698 wxFontBase::SetFaceName(faceName
);
701 void wxFont::SetUnderlined(bool underlined
)
705 M_FONTDATA
->SetUnderlined(underlined
);
708 void wxFont::SetEncoding(wxFontEncoding encoding
)
712 M_FONTDATA
->SetEncoding(encoding
);
715 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
719 M_FONTDATA
->SetNativeFontInfo( info
);
722 void wxFont::SetNoAntiAliasing( bool no
)
726 M_FONTDATA
->SetNoAntiAliasing( no
);
729 // ----------------------------------------------------------------------------
730 // get internal representation of font
731 // ----------------------------------------------------------------------------
733 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
735 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
736 extern GdkFont
*GtkGetDefaultGuiFont()
738 if (!g_systemDefaultGuiFont
)
740 GtkWidget
*widget
= gtk_button_new();
741 GtkStyle
*def
= gtk_rc_get_style( widget
);
744 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
748 def
= gtk_widget_get_default_style();
750 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
752 gtk_widget_destroy( widget
);
756 // already have it, but ref it once more before returning
757 gdk_font_ref(g_systemDefaultGuiFont
);
760 return g_systemDefaultGuiFont
;
763 GdkFont
*wxFont::GetInternalFont( float scale
) const
765 GdkFont
*font
= (GdkFont
*) NULL
;
767 wxCHECK_MSG( Ok(), font
, wxT("invalid font") );
769 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
770 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
772 wxScaledFontList
& list
= M_FONTDATA
->m_scaled_xfonts
;
773 wxScaledFontList::iterator i
= list
.find(int_scale
);
774 if ( i
!= list
.end() )
778 else // we don't have this font in this size yet
780 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
782 font
= GtkGetDefaultGuiFont();
787 // do we have the XLFD?
788 if ( int_scale
== 100 && M_FONTDATA
->HasNativeFont() )
790 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
793 // no XLFD of no exact match - try the approximate one now
797 font
= wxLoadQueryNearestFont( point_scale
,
798 M_FONTDATA
->m_family
,
800 M_FONTDATA
->m_weight
,
801 M_FONTDATA
->m_underlined
,
802 M_FONTDATA
->m_faceName
,
803 M_FONTDATA
->m_encoding
,
805 // NB: wxFont::GetNativeFontInfo relies on this
806 // side-effect of GetInternalFont
807 if ( int_scale
== 100 )
808 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(xfontname
);
814 list
[int_scale
] = font
;
818 // it's quite useless to make it a wxCHECK because we're going to crash
820 wxASSERT_MSG( font
, wxT("could not load any font?") );