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 wxFontFamily family
= wxFONTFAMILY_DEFAULT
,
63 wxFontStyle style
= wxFONTSTYLE_NORMAL
,
64 wxFontWeight 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(wxFontFamily family
);
88 void SetStyle(wxFontStyle style
);
89 void SetWeight(wxFontWeight weight
);
90 void SetUnderlined(bool underlined
);
91 bool SetFaceName(const wxString
& facename
);
92 void SetEncoding(wxFontEncoding encoding
);
94 // and this one also modifies all the other font data fields
95 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
98 // common part of all ctors
99 void Init(int pointSize
,
104 const wxString
& faceName
,
105 wxFontEncoding encoding
);
107 // set all fields from (already initialized and valid) m_nativeFontInfo
108 void InitFromNative();
111 // clear m_scaled_xfonts if any
112 void ClearGdkFonts();
114 // the map of font sizes to "GdkFont *"
115 wxScaledFontList m_scaled_xfonts
;
118 wxFontFamily m_family
;
120 wxFontWeight m_weight
;
123 wxFontEncoding m_encoding
; // Unused under GTK 2.0
125 // The native font info, basicly an XFLD under GTK 1.2 and
126 // the pango font description under GTK 2.0.
127 wxNativeFontInfo m_nativeFontInfo
;
132 #define M_FONTDATA ((wxFontRefData*)m_refData)
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 void wxFontRefData::Init(int pointSize
,
143 const wxString
& faceName
,
144 wxFontEncoding encoding
)
146 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
148 m_faceName
= faceName
;
150 // we accept both wxDEFAULT and wxNORMAL here - should we?
151 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
152 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
154 // and here, do we really want to forbid creation of the font of the size
155 // 90 (the value of wxDEFAULT)??
156 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
157 ? wxDEFAULT_FONT_SIZE
160 m_underlined
= underlined
;
161 m_encoding
= encoding
;
164 void wxFontRefData::InitFromNative()
166 // get the font parameters from the XLFD
167 // -------------------------------------
169 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
171 m_weight
= wxFONTWEIGHT_NORMAL
;
173 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
174 if ( !w
.empty() && w
!= wxT('*') )
176 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
178 if ( ((w
[0u] == wxT('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
179 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
180 wxStrstr(w
.c_str() + 1, wxT("BOLD")) )
182 m_weight
= wxFONTWEIGHT_BOLD
;
184 else if ( w
== wxT("LIGHT") || w
== wxT("THIN") )
186 m_weight
= wxFONTWEIGHT_LIGHT
;
190 switch ( wxToupper(m_nativeFontInfo
.
191 GetXFontComponent(wxXLFD_SLANT
)[0u]).GetValue() )
193 case wxT('I'): // italique
194 m_style
= wxFONTSTYLE_ITALIC
;
197 case wxT('O'): // oblique
198 m_style
= wxFONTSTYLE_SLANT
;
202 m_style
= wxFONTSTYLE_NORMAL
;
206 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
208 // size in XLFD is in 10 point units
209 m_pointSize
= (int)(ptSize
/ 10);
213 m_pointSize
= wxDEFAULT_FONT_SIZE
;
216 // examine the spacing: if the font is monospaced, assume wxTELETYPE
217 // family for compatibility with the old code which used it instead of
219 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == wxT('M') )
221 m_family
= wxFONTFAMILY_TELETYPE
;
223 else // not monospaceed
225 // don't even try guessing it, it doesn't work for too many fonts
227 m_family
= wxFONTFAMILY_UNKNOWN
;
230 // X fonts are never underlined...
231 m_underlined
= false;
233 // deal with font encoding
235 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
236 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
238 if ( registry
== wxT("ISO8859") )
241 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
243 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
246 else if ( registry
== wxT("MICROSOFT") )
249 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
251 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
254 else if ( registry
== wxT("KOI8") )
256 m_encoding
= wxFONTENCODING_KOI8
;
258 else // unknown encoding
260 // may be give a warning here? or use wxFontMapper?
261 m_encoding
= wxFONTENCODING_SYSTEM
;
265 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
268 m_pointSize
= data
.m_pointSize
;
269 m_family
= data
.m_family
;
270 m_style
= data
.m_style
;
271 m_weight
= data
.m_weight
;
273 m_underlined
= data
.m_underlined
;
275 m_faceName
= data
.m_faceName
;
276 m_encoding
= data
.m_encoding
;
278 // Forces a copy of the internal data. wxNativeFontInfo should probably
279 // have a copy ctor and assignment operator to fix this properly but that
280 // would break binary compatibility...
281 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
284 wxFontRefData::wxFontRefData(int size
, wxFontFamily family
, wxFontStyle style
,
285 wxFontWeight weight
, bool underlined
,
286 const wxString
& faceName
,
287 wxFontEncoding encoding
)
289 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
292 wxFontRefData::wxFontRefData(const wxString
& fontname
)
294 // FromString() should really work in GTK1 too, doesn't it?
295 m_nativeFontInfo
.SetXFontName(fontname
);
300 void wxFontRefData::ClearGdkFonts()
302 for ( wxScaledFontList::iterator i
= m_scaled_xfonts
.begin();
303 i
!= m_scaled_xfonts
.end();
306 GdkFont
*font
= i
->second
;
307 gdk_font_unref( font
);
310 m_scaled_xfonts
.clear();
313 wxFontRefData::~wxFontRefData()
318 // ----------------------------------------------------------------------------
319 // wxFontRefData SetXXX()
320 // ----------------------------------------------------------------------------
322 void wxFontRefData::SetPointSize(int pointSize
)
324 m_pointSize
= pointSize
;
326 if ( HasNativeFont() )
329 if ( pointSize
== -1 )
332 size
.Printf(wxT("%d"), 10*pointSize
);
334 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
338 void wxFontRefData::SetFamily(wxFontFamily family
)
342 // TODO: what are we supposed to do with m_nativeFontInfo here?
345 void wxFontRefData::SetStyle(wxFontStyle style
)
349 if ( HasNativeFont() )
354 case wxFONTSTYLE_ITALIC
:
358 case wxFONTSTYLE_SLANT
:
363 wxFAIL_MSG( wxT("unknown font style") );
366 case wxFONTSTYLE_NORMAL
:
370 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
374 void wxFontRefData::SetWeight(wxFontWeight weight
)
378 if ( HasNativeFont() )
383 case wxFONTWEIGHT_BOLD
:
384 boldness
= wxT("bold");
387 case wxFONTWEIGHT_LIGHT
:
388 boldness
= wxT("light");
392 wxFAIL_MSG( wxT("unknown font weight") );
395 case wxFONTWEIGHT_NORMAL
:
397 boldness
= wxT("medium");
400 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
404 void wxFontRefData::SetUnderlined(bool underlined
)
406 m_underlined
= underlined
;
408 // the XLFD doesn't have "underlined" field anyhow
411 bool wxFontRefData::SetFaceName(const wxString
& facename
)
413 m_faceName
= facename
;
415 if ( HasNativeFont() )
417 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
423 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
425 m_encoding
= encoding
;
427 if ( HasNativeFont() )
429 wxNativeEncodingInfo info
;
430 if ( wxGetNativeFontEncoding(encoding
, &info
) )
432 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
433 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
438 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
440 // previously cached fonts shouldn't be used
443 m_nativeFontInfo
= info
;
445 // set all the other font parameters from the native font info
449 // ----------------------------------------------------------------------------
451 // ----------------------------------------------------------------------------
453 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
455 wxFont::wxFont(const wxNativeFontInfo
& info
)
457 (void) Create(info
.GetXFontName());
460 bool wxFont::Create( int pointSize
,
465 const wxString
& face
,
466 wxFontEncoding encoding
)
470 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
471 underlined
, face
, encoding
);
476 bool wxFont::Create(const wxString
& fontname
)
478 // VZ: does this really happen?
479 if ( fontname
.empty() )
481 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
486 m_refData
= new wxFontRefData(fontname
);
491 void wxFont::Unshare()
495 m_refData
= new wxFontRefData();
499 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
509 wxGDIRefData
*wxFont::CreateGDIRefData() const
511 return new wxFontRefData
;
514 wxGDIRefData
*wxFont::CloneGDIRefData(const wxGDIRefData
*data
) const
516 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));
519 // ----------------------------------------------------------------------------
521 // ----------------------------------------------------------------------------
523 int wxFont::GetPointSize() const
525 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
527 return M_FONTDATA
->m_pointSize
;
530 wxString
wxFont::GetFaceName() const
532 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
534 return M_FONTDATA
->m_faceName
;
537 wxFontFamily
wxFont::DoGetFamily() const
539 return M_FONTDATA
->m_family
;
542 wxFontStyle
wxFont::GetStyle() const
544 wxCHECK_MSG( Ok(), wxFONTSTYLE_MAX
, wxT("invalid font") );
546 return M_FONTDATA
->m_style
;
549 wxFontWeight
wxFont::GetWeight() const
551 wxCHECK_MSG( Ok(), wxFONTWEIGHT_MAX
, wxT("invalid font") );
553 return M_FONTDATA
->m_weight
;
556 bool wxFont::GetUnderlined() const
558 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
560 return M_FONTDATA
->m_underlined
;
563 wxFontEncoding
wxFont::GetEncoding() const
565 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
567 // m_encoding is unused in wxGTK2, return encoding that the user set.
568 return M_FONTDATA
->m_encoding
;
571 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
573 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
575 if ( !M_FONTDATA
->HasNativeFont() )
577 // NB: this call has important side-effect: it not only finds
578 // GdkFont representation, it also initializes m_nativeFontInfo
579 // by calling its SetXFontName method
583 return &(M_FONTDATA
->m_nativeFontInfo
);
586 bool wxFont::IsFixedWidth() const
588 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
590 if ( M_FONTDATA
->HasNativeFont() )
592 // the monospace fonts are supposed to have "M" in the spacing field
593 wxString spacing
= M_FONTDATA
->
594 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
596 return spacing
.Upper() == wxT('M');
599 return wxFontBase::IsFixedWidth();
602 // ----------------------------------------------------------------------------
603 // change font attributes
604 // ----------------------------------------------------------------------------
606 void wxFont::SetPointSize(int pointSize
)
610 M_FONTDATA
->SetPointSize(pointSize
);
613 void wxFont::SetFamily(wxFontFamily family
)
617 M_FONTDATA
->SetFamily(family
);
620 void wxFont::SetStyle(wxFontStyle style
)
624 M_FONTDATA
->SetStyle(style
);
627 void wxFont::SetWeight(wxFontWeight weight
)
631 M_FONTDATA
->SetWeight(weight
);
634 bool wxFont::SetFaceName(const wxString
& faceName
)
638 return M_FONTDATA
->SetFaceName(faceName
) &&
639 wxFontBase::SetFaceName(faceName
);
642 void wxFont::SetUnderlined(bool underlined
)
646 M_FONTDATA
->SetUnderlined(underlined
);
649 void wxFont::SetEncoding(wxFontEncoding encoding
)
653 M_FONTDATA
->SetEncoding(encoding
);
656 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
660 M_FONTDATA
->SetNativeFontInfo( info
);
663 // ----------------------------------------------------------------------------
664 // get internal representation of font
665 // ----------------------------------------------------------------------------
667 static GdkFont
*g_systemDefaultGuiFont
= NULL
;
669 // this is also used from toolbar.cpp and tooltip.cpp, hence extern
670 extern GdkFont
*GtkGetDefaultGuiFont()
672 if (!g_systemDefaultGuiFont
)
674 GtkWidget
*widget
= gtk_button_new();
675 GtkStyle
*def
= gtk_rc_get_style( widget
);
678 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
682 def
= gtk_widget_get_default_style();
684 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
686 gtk_widget_destroy( widget
);
690 // already have it, but ref it once more before returning
691 gdk_font_ref(g_systemDefaultGuiFont
);
694 return g_systemDefaultGuiFont
;
697 GdkFont
*wxFont::GetInternalFont( float scale
) const
699 GdkFont
*font
= NULL
;
701 wxCHECK_MSG( Ok(), font
, wxT("invalid font") );
703 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
704 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
706 wxScaledFontList
& list
= M_FONTDATA
->m_scaled_xfonts
;
707 wxScaledFontList::iterator i
= list
.find(int_scale
);
708 if ( i
!= list
.end() )
712 else // we don't have this font in this size yet
714 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
716 font
= GtkGetDefaultGuiFont();
721 // do we have the XLFD?
722 if ( int_scale
== 100 && M_FONTDATA
->HasNativeFont() )
724 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
727 // no XLFD of no exact match - try the approximate one now
731 font
= wxLoadQueryNearestFont( point_scale
,
732 M_FONTDATA
->m_family
,
734 M_FONTDATA
->m_weight
,
735 M_FONTDATA
->m_underlined
,
736 M_FONTDATA
->m_faceName
,
737 M_FONTDATA
->m_encoding
,
739 // NB: wxFont::GetNativeFontInfo relies on this
740 // side-effect of GetInternalFont
741 if ( int_scale
== 100 )
742 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(xfontname
);
748 list
[int_scale
] = font
;
752 // it's quite useless to make it a wxCHECK because we're going to crash
754 wxASSERT_MSG( font
, wxT("could not load any font?") );