1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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"
27 #include "wx/gdicmn.h"
30 #include "wx/fontutil.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 wxGDIRefData
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 #define M_FONTDATA ((wxFontRefData*)m_refData)
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 void wxFontRefData::Init(int pointSize
,
167 const wxString
& faceName
,
168 wxFontEncoding encoding
)
170 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
172 m_faceName
= faceName
;
174 // we accept both wxDEFAULT and wxNORMAL here - should we?
175 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
176 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
178 // and here, do we really want to forbid creation of the font of the size
179 // 90 (the value of wxDEFAULT)??
180 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
181 ? wxDEFAULT_FONT_SIZE
184 m_underlined
= underlined
;
185 m_encoding
= encoding
;
190 void wxFontRefData::InitFromNative()
194 // get the font parameters from the XLFD
195 // -------------------------------------
197 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
199 m_weight
= wxFONTWEIGHT_NORMAL
;
201 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
202 if ( !w
.empty() && w
!= _T('*') )
204 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
206 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
207 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
208 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
210 m_weight
= wxFONTWEIGHT_BOLD
;
212 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
214 m_weight
= wxFONTWEIGHT_LIGHT
;
218 switch ( wxToupper(m_nativeFontInfo
.
219 GetXFontComponent(wxXLFD_SLANT
)[0u]).GetValue() )
221 case _T('I'): // italique
222 m_style
= wxFONTSTYLE_ITALIC
;
225 case _T('O'): // oblique
226 m_style
= wxFONTSTYLE_SLANT
;
230 m_style
= wxFONTSTYLE_NORMAL
;
234 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
236 // size in XLFD is in 10 point units
237 m_pointSize
= (int)(ptSize
/ 10);
241 m_pointSize
= wxDEFAULT_FONT_SIZE
;
244 // examine the spacing: if the font is monospaced, assume wxTELETYPE
245 // family for compatibility with the old code which used it instead of
247 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
249 m_family
= wxFONTFAMILY_TELETYPE
;
251 else // not monospaceed
253 // don't even try guessing it, it doesn't work for too many fonts
255 m_family
= wxFONTFAMILY_UNKNOWN
;
258 // X fonts are never underlined...
259 m_underlined
= false;
261 // deal with font encoding
263 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
264 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
266 if ( registry
== _T("ISO8859") )
269 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
271 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
274 else if ( registry
== _T("MICROSOFT") )
277 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
279 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
282 else if ( registry
== _T("KOI8") )
284 m_encoding
= wxFONTENCODING_KOI8
;
286 else // unknown encoding
288 // may be give a warning here? or use wxFontMapper?
289 m_encoding
= wxFONTENCODING_SYSTEM
;
293 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
296 m_pointSize
= data
.m_pointSize
;
297 m_family
= data
.m_family
;
298 m_style
= data
.m_style
;
299 m_weight
= data
.m_weight
;
301 m_underlined
= data
.m_underlined
;
303 m_faceName
= data
.m_faceName
;
304 m_encoding
= data
.m_encoding
;
306 m_noAA
= data
.m_noAA
;
308 // Forces a copy of the internal data. wxNativeFontInfo should probably
309 // have a copy ctor and assignment operator to fix this properly but that
310 // would break binary compatibility...
311 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
314 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
315 int weight
, bool underlined
,
316 const wxString
& faceName
,
317 wxFontEncoding encoding
)
319 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
322 wxFontRefData::wxFontRefData(const wxString
& fontname
)
324 // FromString() should really work in GTK1 too, doesn't it?
325 m_nativeFontInfo
.SetXFontName(fontname
);
330 void wxFontRefData::ClearGdkFonts()
332 for ( wxScaledFontList::iterator i
= m_scaled_xfonts
.begin();
333 i
!= m_scaled_xfonts
.end();
336 GdkFont
*font
= i
->second
;
337 gdk_font_unref( font
);
340 m_scaled_xfonts
.clear();
343 wxFontRefData::~wxFontRefData()
348 // ----------------------------------------------------------------------------
349 // wxFontRefData SetXXX()
350 // ----------------------------------------------------------------------------
352 void wxFontRefData::SetPointSize(int pointSize
)
354 m_pointSize
= pointSize
;
356 if ( HasNativeFont() )
359 if ( pointSize
== -1 )
362 size
.Printf(_T("%d"), 10*pointSize
);
364 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
368 void wxFontRefData::SetFamily(int family
)
372 // TODO: what are we supposed to do with m_nativeFontInfo here?
375 void wxFontRefData::SetStyle(int style
)
379 if ( HasNativeFont() )
384 case wxFONTSTYLE_ITALIC
:
388 case wxFONTSTYLE_SLANT
:
393 wxFAIL_MSG( _T("unknown font style") );
396 case wxFONTSTYLE_NORMAL
:
400 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
404 void wxFontRefData::SetWeight(int weight
)
408 if ( HasNativeFont() )
413 case wxFONTWEIGHT_BOLD
:
414 boldness
= _T("bold");
417 case wxFONTWEIGHT_LIGHT
:
418 boldness
= _T("light");
422 wxFAIL_MSG( _T("unknown font weight") );
425 case wxFONTWEIGHT_NORMAL
:
427 boldness
= _T("medium");
430 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
434 void wxFontRefData::SetUnderlined(bool underlined
)
436 m_underlined
= underlined
;
438 // the XLFD doesn't have "underlined" field anyhow
441 bool wxFontRefData::SetFaceName(const wxString
& facename
)
443 m_faceName
= facename
;
445 if ( HasNativeFont() )
447 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
453 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
455 m_encoding
= encoding
;
457 if ( HasNativeFont() )
459 wxNativeEncodingInfo info
;
460 if ( wxGetNativeFontEncoding(encoding
, &info
) )
462 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
463 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
468 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
470 // previously cached fonts shouldn't be used
473 m_nativeFontInfo
= info
;
475 // set all the other font parameters from the native font info
479 // ----------------------------------------------------------------------------
481 // ----------------------------------------------------------------------------
483 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
485 wxFont::wxFont(const wxNativeFontInfo
& info
)
487 (void) Create(info
.GetXFontName());
490 bool wxFont::Create( int pointSize
,
495 const wxString
& face
,
496 wxFontEncoding encoding
)
500 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
501 underlined
, face
, encoding
);
506 bool wxFont::Create(const wxString
& fontname
)
508 // VZ: does this really happen?
509 if ( fontname
.empty() )
511 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
516 m_refData
= new wxFontRefData(fontname
);
521 void wxFont::Unshare()
525 m_refData
= new wxFontRefData();
529 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
539 wxGDIRefData
*wxFont::CreateGDIRefData() const
541 return new wxFontRefData
;
544 wxGDIRefData
*wxFont::CloneGDIRefData(const wxGDIRefData
*data
) const
546 return new wxFontRefData(*wx_static_cast(const wxFontRefData
*, data
));
549 // ----------------------------------------------------------------------------
551 // ----------------------------------------------------------------------------
553 int wxFont::GetPointSize() const
555 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
557 return M_FONTDATA
->m_pointSize
;
560 wxString
wxFont::GetFaceName() const
562 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
564 return M_FONTDATA
->m_faceName
;
567 int wxFont::GetFamily() const
569 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
571 return M_FONTDATA
->m_family
;
574 int wxFont::GetStyle() const
576 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
578 return M_FONTDATA
->m_style
;
581 int wxFont::GetWeight() const
583 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
585 return M_FONTDATA
->m_weight
;
588 bool wxFont::GetUnderlined() const
590 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
592 return M_FONTDATA
->m_underlined
;
595 wxFontEncoding
wxFont::GetEncoding() const
597 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
599 // m_encoding is unused in wxGTK2, return encoding that the user set.
600 return M_FONTDATA
->m_encoding
;
603 bool wxFont::GetNoAntiAliasing() const
605 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
607 return M_FONTDATA
->m_noAA
;
610 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
612 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
614 if ( !M_FONTDATA
->HasNativeFont() )
616 // NB: this call has important side-effect: it not only finds
617 // GdkFont representation, it also initializes m_nativeFontInfo
618 // by calling its SetXFontName method
622 return &(M_FONTDATA
->m_nativeFontInfo
);
625 bool wxFont::IsFixedWidth() const
627 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
629 if ( M_FONTDATA
->HasNativeFont() )
631 // the monospace fonts are supposed to have "M" in the spacing field
632 wxString spacing
= M_FONTDATA
->
633 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
635 return spacing
.Upper() == _T('M');
638 return wxFontBase::IsFixedWidth();
641 // ----------------------------------------------------------------------------
642 // change font attributes
643 // ----------------------------------------------------------------------------
645 void wxFont::SetPointSize(int pointSize
)
649 M_FONTDATA
->SetPointSize(pointSize
);
652 void wxFont::SetFamily(int family
)
656 M_FONTDATA
->SetFamily(family
);
659 void wxFont::SetStyle(int style
)
663 M_FONTDATA
->SetStyle(style
);
666 void wxFont::SetWeight(int weight
)
670 M_FONTDATA
->SetWeight(weight
);
673 bool wxFont::SetFaceName(const wxString
& faceName
)
677 return M_FONTDATA
->SetFaceName(faceName
) &&
678 wxFontBase::SetFaceName(faceName
);
681 void wxFont::SetUnderlined(bool underlined
)
685 M_FONTDATA
->SetUnderlined(underlined
);
688 void wxFont::SetEncoding(wxFontEncoding encoding
)
692 M_FONTDATA
->SetEncoding(encoding
);
695 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
699 M_FONTDATA
->SetNativeFontInfo( info
);
702 void wxFont::SetNoAntiAliasing( bool no
)
706 M_FONTDATA
->SetNoAntiAliasing( no
);
709 // ----------------------------------------------------------------------------
710 // get internal representation of font
711 // ----------------------------------------------------------------------------
713 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
715 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
716 extern GdkFont
*GtkGetDefaultGuiFont()
718 if (!g_systemDefaultGuiFont
)
720 GtkWidget
*widget
= gtk_button_new();
721 GtkStyle
*def
= gtk_rc_get_style( widget
);
724 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
728 def
= gtk_widget_get_default_style();
730 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
732 gtk_widget_destroy( widget
);
736 // already have it, but ref it once more before returning
737 gdk_font_ref(g_systemDefaultGuiFont
);
740 return g_systemDefaultGuiFont
;
743 GdkFont
*wxFont::GetInternalFont( float scale
) const
745 GdkFont
*font
= (GdkFont
*) NULL
;
747 wxCHECK_MSG( Ok(), font
, wxT("invalid font") );
749 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
750 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
752 wxScaledFontList
& list
= M_FONTDATA
->m_scaled_xfonts
;
753 wxScaledFontList::iterator i
= list
.find(int_scale
);
754 if ( i
!= list
.end() )
758 else // we don't have this font in this size yet
760 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
762 font
= GtkGetDefaultGuiFont();
767 // do we have the XLFD?
768 if ( int_scale
== 100 && M_FONTDATA
->HasNativeFont() )
770 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
773 // no XLFD of no exact match - try the approximate one now
777 font
= wxLoadQueryNearestFont( point_scale
,
778 M_FONTDATA
->m_family
,
780 M_FONTDATA
->m_weight
,
781 M_FONTDATA
->m_underlined
,
782 M_FONTDATA
->m_faceName
,
783 M_FONTDATA
->m_encoding
,
785 // NB: wxFont::GetNativeFontInfo relies on this
786 // side-effect of GetInternalFont
787 if ( int_scale
== 100 )
788 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(xfontname
);
794 list
[int_scale
] = font
;
798 // it's quite useless to make it a wxCHECK because we're going to crash
800 wxASSERT_MSG( font
, wxT("could not load any font?") );