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/gdicmn.h"
27 #include "wx/encinfo.h"
31 #include "wx/fontutil.h"
33 #include "wx/tokenzr.h"
37 #include "wx/gtk1/private.h"
38 #include <gdk/gdkprivate.h>
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // the default size (in points) for the fonts
45 static const int wxDEFAULT_FONT_SIZE
= 12;
47 // ----------------------------------------------------------------------------
48 // wxScaledFontList: maps the font sizes to the GDK fonts for the given font
49 // ----------------------------------------------------------------------------
51 WX_DECLARE_HASH_MAP(int, GdkFont
*, wxIntegerHash
, wxIntegerEqual
,
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 class wxFontRefData
: public wxGDIRefData
61 // from broken down font parameters, also default ctor
62 wxFontRefData(int size
= -1,
63 wxFontFamily family
= wxFONTFAMILY_DEFAULT
,
64 wxFontStyle style
= wxFONTSTYLE_NORMAL
,
65 wxFontWeight weight
= wxFONTWEIGHT_NORMAL
,
66 bool underlined
= false,
67 const wxString
& faceName
= wxEmptyString
,
68 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
71 wxFontRefData(const wxString
& fontname
);
74 wxFontRefData( const wxFontRefData
& data
);
76 virtual ~wxFontRefData();
78 // do we have the native font info?
79 bool HasNativeFont() const
81 // only use m_nativeFontInfo if it had been initialized
82 return !m_nativeFontInfo
.IsDefault();
85 // setters: all of them also take care to modify m_nativeFontInfo if we
86 // have it so as to not lose the information not carried by our fields
87 void SetPointSize(int pointSize
);
88 void SetFamily(wxFontFamily family
);
89 void SetStyle(wxFontStyle style
);
90 void SetWeight(wxFontWeight weight
);
91 void SetUnderlined(bool underlined
);
92 bool SetFaceName(const wxString
& facename
);
93 void SetEncoding(wxFontEncoding encoding
);
95 // and this one also modifies all the other font data fields
96 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
99 // common part of all ctors
100 void Init(int pointSize
,
105 const wxString
& faceName
,
106 wxFontEncoding encoding
);
108 // set all fields from (already initialized and valid) m_nativeFontInfo
109 void InitFromNative();
112 // clear m_scaled_xfonts if any
113 void ClearGdkFonts();
115 // the map of font sizes to "GdkFont *"
116 wxScaledFontList m_scaled_xfonts
;
119 wxFontFamily m_family
;
121 wxFontWeight m_weight
;
124 wxFontEncoding m_encoding
; // Unused under GTK 2.0
126 // The native font info, basicly an XFLD under GTK 1.2 and
127 // the pango font description under GTK 2.0.
128 wxNativeFontInfo m_nativeFontInfo
;
133 #define M_FONTDATA ((wxFontRefData*)m_refData)
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 void wxFontRefData::Init(int pointSize
,
144 const wxString
& faceName
,
145 wxFontEncoding encoding
)
147 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
149 m_faceName
= faceName
;
151 // we accept both wxDEFAULT and wxNORMAL here - should we?
152 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
153 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
155 // and here, do we really want to forbid creation of the font of the size
156 // 90 (the value of wxDEFAULT)??
157 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
158 ? wxDEFAULT_FONT_SIZE
161 m_underlined
= underlined
;
162 m_encoding
= encoding
;
165 void wxFontRefData::InitFromNative()
167 // get the font parameters from the XLFD
168 // -------------------------------------
170 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
172 m_weight
= wxFONTWEIGHT_NORMAL
;
174 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
175 if ( !w
.empty() && w
!= wxT('*') )
177 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
179 if ( ((w
[0u] == wxT('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
180 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
181 wxStrstr(w
.c_str() + 1, wxT("BOLD")) )
183 m_weight
= wxFONTWEIGHT_BOLD
;
185 else if ( w
== wxT("LIGHT") || w
== wxT("THIN") )
187 m_weight
= wxFONTWEIGHT_LIGHT
;
191 switch ( wxToupper(m_nativeFontInfo
.
192 GetXFontComponent(wxXLFD_SLANT
)[0u]).GetValue() )
194 case wxT('I'): // italique
195 m_style
= wxFONTSTYLE_ITALIC
;
198 case wxT('O'): // oblique
199 m_style
= wxFONTSTYLE_SLANT
;
203 m_style
= wxFONTSTYLE_NORMAL
;
207 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
209 // size in XLFD is in 10 point units
210 m_pointSize
= (int)(ptSize
/ 10);
214 m_pointSize
= wxDEFAULT_FONT_SIZE
;
217 // examine the spacing: if the font is monospaced, assume wxTELETYPE
218 // family for compatibility with the old code which used it instead of
220 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == wxT('M') )
222 m_family
= wxFONTFAMILY_TELETYPE
;
224 else // not monospaceed
226 // don't even try guessing it, it doesn't work for too many fonts
228 m_family
= wxFONTFAMILY_UNKNOWN
;
231 // X fonts are never underlined...
232 m_underlined
= false;
234 // deal with font encoding
236 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
237 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
239 if ( registry
== wxT("ISO8859") )
242 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
244 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
247 else if ( registry
== wxT("MICROSOFT") )
250 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
252 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
255 else if ( registry
== wxT("KOI8") )
257 m_encoding
= wxFONTENCODING_KOI8
;
259 else // unknown encoding
261 // may be give a warning here? or use wxFontMapper?
262 m_encoding
= wxFONTENCODING_SYSTEM
;
266 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
269 m_pointSize
= data
.m_pointSize
;
270 m_family
= data
.m_family
;
271 m_style
= data
.m_style
;
272 m_weight
= data
.m_weight
;
274 m_underlined
= data
.m_underlined
;
276 m_faceName
= data
.m_faceName
;
277 m_encoding
= data
.m_encoding
;
279 // Forces a copy of the internal data. wxNativeFontInfo should probably
280 // have a copy ctor and assignment operator to fix this properly but that
281 // would break binary compatibility...
282 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
285 wxFontRefData::wxFontRefData(int size
, wxFontFamily family
, wxFontStyle style
,
286 wxFontWeight weight
, bool underlined
,
287 const wxString
& faceName
,
288 wxFontEncoding encoding
)
290 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
293 wxFontRefData::wxFontRefData(const wxString
& fontname
)
295 // FromString() should really work in GTK1 too, doesn't it?
296 m_nativeFontInfo
.SetXFontName(fontname
);
301 void wxFontRefData::ClearGdkFonts()
303 for ( wxScaledFontList::iterator i
= m_scaled_xfonts
.begin();
304 i
!= m_scaled_xfonts
.end();
307 GdkFont
*font
= i
->second
;
308 gdk_font_unref( font
);
311 m_scaled_xfonts
.clear();
314 wxFontRefData::~wxFontRefData()
319 // ----------------------------------------------------------------------------
320 // wxFontRefData SetXXX()
321 // ----------------------------------------------------------------------------
323 void wxFontRefData::SetPointSize(int pointSize
)
325 m_pointSize
= pointSize
;
327 if ( HasNativeFont() )
330 if ( pointSize
== -1 )
333 size
.Printf(wxT("%d"), 10*pointSize
);
335 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
339 void wxFontRefData::SetFamily(wxFontFamily family
)
343 // TODO: what are we supposed to do with m_nativeFontInfo here?
346 void wxFontRefData::SetStyle(wxFontStyle style
)
350 if ( HasNativeFont() )
355 case wxFONTSTYLE_ITALIC
:
359 case wxFONTSTYLE_SLANT
:
364 wxFAIL_MSG( wxT("unknown font style") );
367 case wxFONTSTYLE_NORMAL
:
371 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
375 void wxFontRefData::SetWeight(wxFontWeight weight
)
379 if ( HasNativeFont() )
384 case wxFONTWEIGHT_BOLD
:
385 boldness
= wxT("bold");
388 case wxFONTWEIGHT_LIGHT
:
389 boldness
= wxT("light");
393 wxFAIL_MSG( wxT("unknown font weight") );
396 case wxFONTWEIGHT_NORMAL
:
398 boldness
= wxT("medium");
401 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
405 void wxFontRefData::SetUnderlined(bool underlined
)
407 m_underlined
= underlined
;
409 // the XLFD doesn't have "underlined" field anyhow
412 bool wxFontRefData::SetFaceName(const wxString
& facename
)
414 m_faceName
= facename
;
416 if ( HasNativeFont() )
418 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
424 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
426 m_encoding
= encoding
;
428 if ( HasNativeFont() )
430 wxNativeEncodingInfo info
;
431 if ( wxGetNativeFontEncoding(encoding
, &info
) )
433 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
434 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
439 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
441 // previously cached fonts shouldn't be used
444 m_nativeFontInfo
= info
;
446 // set all the other font parameters from the native font info
450 // ----------------------------------------------------------------------------
452 // ----------------------------------------------------------------------------
454 wxFont::wxFont(const wxNativeFontInfo
& info
)
456 (void) Create(info
.GetXFontName());
459 bool wxFont::Create( int pointSize
,
464 const wxString
& face
,
465 wxFontEncoding encoding
)
469 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
470 underlined
, face
, encoding
);
475 bool wxFont::Create(const wxString
& fontname
)
477 // VZ: does this really happen?
478 if ( fontname
.empty() )
480 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
485 m_refData
= new wxFontRefData(fontname
);
490 void wxFont::Unshare()
494 m_refData
= new wxFontRefData();
498 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
508 wxGDIRefData
*wxFont::CreateGDIRefData() const
510 return new wxFontRefData
;
513 wxGDIRefData
*wxFont::CloneGDIRefData(const wxGDIRefData
*data
) const
515 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));
518 // ----------------------------------------------------------------------------
520 // ----------------------------------------------------------------------------
522 int wxFont::GetPointSize() const
524 wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
526 return M_FONTDATA
->m_pointSize
;
529 wxString
wxFont::GetFaceName() const
531 wxCHECK_MSG( IsOk(), wxEmptyString
, wxT("invalid font") );
533 return M_FONTDATA
->m_faceName
;
536 wxFontFamily
wxFont::DoGetFamily() const
538 return M_FONTDATA
->m_family
;
541 wxFontStyle
wxFont::GetStyle() const
543 wxCHECK_MSG( IsOk(), wxFONTSTYLE_MAX
, wxT("invalid font") );
545 return M_FONTDATA
->m_style
;
548 wxFontWeight
wxFont::GetWeight() const
550 wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX
, wxT("invalid font") );
552 return M_FONTDATA
->m_weight
;
555 bool wxFont::GetUnderlined() const
557 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
559 return M_FONTDATA
->m_underlined
;
562 wxFontEncoding
wxFont::GetEncoding() const
564 wxCHECK_MSG( IsOk(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
566 // m_encoding is unused in wxGTK2, return encoding that the user set.
567 return M_FONTDATA
->m_encoding
;
570 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
572 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid font") );
574 if ( !M_FONTDATA
->HasNativeFont() )
576 // NB: this call has important side-effect: it not only finds
577 // GdkFont representation, it also initializes m_nativeFontInfo
578 // by calling its SetXFontName method
582 return &(M_FONTDATA
->m_nativeFontInfo
);
585 bool wxFont::IsFixedWidth() const
587 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
589 if ( M_FONTDATA
->HasNativeFont() )
591 // the monospace fonts are supposed to have "M" in the spacing field
592 wxString spacing
= M_FONTDATA
->
593 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
595 return spacing
.Upper() == wxT('M');
598 return wxFontBase::IsFixedWidth();
601 // ----------------------------------------------------------------------------
602 // change font attributes
603 // ----------------------------------------------------------------------------
605 void wxFont::SetPointSize(int pointSize
)
609 M_FONTDATA
->SetPointSize(pointSize
);
612 void wxFont::SetFamily(wxFontFamily family
)
616 M_FONTDATA
->SetFamily(family
);
619 void wxFont::SetStyle(wxFontStyle style
)
623 M_FONTDATA
->SetStyle(style
);
626 void wxFont::SetWeight(wxFontWeight weight
)
630 M_FONTDATA
->SetWeight(weight
);
633 bool wxFont::SetFaceName(const wxString
& faceName
)
637 return M_FONTDATA
->SetFaceName(faceName
) &&
638 wxFontBase::SetFaceName(faceName
);
641 void wxFont::SetUnderlined(bool underlined
)
645 M_FONTDATA
->SetUnderlined(underlined
);
648 void wxFont::SetEncoding(wxFontEncoding encoding
)
652 M_FONTDATA
->SetEncoding(encoding
);
655 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
659 M_FONTDATA
->SetNativeFontInfo( info
);
662 // ----------------------------------------------------------------------------
663 // get internal representation of font
664 // ----------------------------------------------------------------------------
666 static GdkFont
*g_systemDefaultGuiFont
= NULL
;
668 // this is also used from toolbar.cpp and tooltip.cpp, hence extern
669 extern GdkFont
*GtkGetDefaultGuiFont()
671 if (!g_systemDefaultGuiFont
)
673 GtkWidget
*widget
= gtk_button_new();
674 GtkStyle
*def
= gtk_rc_get_style( widget
);
677 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
681 def
= gtk_widget_get_default_style();
683 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
685 gtk_widget_destroy( widget
);
689 // already have it, but ref it once more before returning
690 gdk_font_ref(g_systemDefaultGuiFont
);
693 return g_systemDefaultGuiFont
;
696 GdkFont
*wxFont::GetInternalFont( float scale
) const
698 GdkFont
*font
= NULL
;
700 wxCHECK_MSG( IsOk(), font
, wxT("invalid font") );
702 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
703 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
705 wxScaledFontList
& list
= M_FONTDATA
->m_scaled_xfonts
;
706 wxScaledFontList::iterator i
= list
.find(int_scale
);
707 if ( i
!= list
.end() )
711 else // we don't have this font in this size yet
713 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
715 font
= GtkGetDefaultGuiFont();
720 // do we have the XLFD?
721 if ( int_scale
== 100 && M_FONTDATA
->HasNativeFont() )
723 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
726 // no XLFD of no exact match - try the approximate one now
730 font
= wxLoadQueryNearestFont( point_scale
,
731 M_FONTDATA
->m_family
,
733 M_FONTDATA
->m_weight
,
734 M_FONTDATA
->m_underlined
,
735 M_FONTDATA
->m_faceName
,
736 M_FONTDATA
->m_encoding
,
738 // NB: wxFont::GetNativeFontInfo relies on this
739 // side-effect of GetInternalFont
740 if ( int_scale
== 100 )
741 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(xfontname
);
747 list
[int_scale
] = font
;
751 // it's quite useless to make it a wxCHECK because we're going to crash
753 wxASSERT_MSG( font
, wxT("could not load any font?") );