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"
29 #include "wx/fontutil.h"
31 #include "wx/tokenzr.h"
35 #include "wx/gtk1/private.h"
36 #include <gdk/gdkprivate.h>
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // the default size (in points) for the fonts
43 static const int wxDEFAULT_FONT_SIZE
= 12;
45 // ----------------------------------------------------------------------------
46 // wxScaledFontList: maps the font sizes to the GDK fonts for the given font
47 // ----------------------------------------------------------------------------
49 WX_DECLARE_HASH_MAP(int, GdkFont
*, wxIntegerHash
, wxIntegerEqual
,
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 class wxFontRefData
: public wxGDIRefData
59 // from broken down font parameters, also default ctor
60 wxFontRefData(int size
= -1,
61 wxFontFamily family
= wxFONTFAMILY_DEFAULT
,
62 wxFontStyle style
= wxFONTSTYLE_NORMAL
,
63 wxFontWeight weight
= wxFONTWEIGHT_NORMAL
,
64 bool underlined
= false,
65 const wxString
& faceName
= wxEmptyString
,
66 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
69 wxFontRefData(const wxString
& fontname
);
72 wxFontRefData( const wxFontRefData
& data
);
74 virtual ~wxFontRefData();
76 // do we have the native font info?
77 bool HasNativeFont() const
79 // only use m_nativeFontInfo if it had been initialized
80 return !m_nativeFontInfo
.IsDefault();
83 // setters: all of them also take care to modify m_nativeFontInfo if we
84 // have it so as to not lose the information not carried by our fields
85 void SetPointSize(int pointSize
);
86 void SetFamily(wxFontFamily family
);
87 void SetStyle(wxFontStyle style
);
88 void SetWeight(wxFontWeight weight
);
89 void SetUnderlined(bool underlined
);
90 bool SetFaceName(const wxString
& facename
);
91 void SetEncoding(wxFontEncoding encoding
);
93 // and this one also modifies all the other font data fields
94 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
97 // common part of all ctors
98 void Init(int pointSize
,
103 const wxString
& faceName
,
104 wxFontEncoding encoding
);
106 // set all fields from (already initialized and valid) m_nativeFontInfo
107 void InitFromNative();
110 // clear m_scaled_xfonts if any
111 void ClearGdkFonts();
113 // the map of font sizes to "GdkFont *"
114 wxScaledFontList m_scaled_xfonts
;
117 wxFontFamily m_family
;
119 wxFontWeight m_weight
;
122 wxFontEncoding m_encoding
; // Unused under GTK 2.0
124 // The native font info, basicly an XFLD under GTK 1.2 and
125 // the pango font description under GTK 2.0.
126 wxNativeFontInfo m_nativeFontInfo
;
131 #define M_FONTDATA ((wxFontRefData*)m_refData)
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 void wxFontRefData::Init(int pointSize
,
142 const wxString
& faceName
,
143 wxFontEncoding encoding
)
145 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
147 m_faceName
= faceName
;
149 // we accept both wxDEFAULT and wxNORMAL here - should we?
150 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
151 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
153 // and here, do we really want to forbid creation of the font of the size
154 // 90 (the value of wxDEFAULT)??
155 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
156 ? wxDEFAULT_FONT_SIZE
159 m_underlined
= underlined
;
160 m_encoding
= encoding
;
163 void wxFontRefData::InitFromNative()
165 // get the font parameters from the XLFD
166 // -------------------------------------
168 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
170 m_weight
= wxFONTWEIGHT_NORMAL
;
172 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
173 if ( !w
.empty() && w
!= wxT('*') )
175 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
177 if ( ((w
[0u] == wxT('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
178 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
179 wxStrstr(w
.c_str() + 1, wxT("BOLD")) )
181 m_weight
= wxFONTWEIGHT_BOLD
;
183 else if ( w
== wxT("LIGHT") || w
== wxT("THIN") )
185 m_weight
= wxFONTWEIGHT_LIGHT
;
189 switch ( wxToupper(m_nativeFontInfo
.
190 GetXFontComponent(wxXLFD_SLANT
)[0u]).GetValue() )
192 case wxT('I'): // italique
193 m_style
= wxFONTSTYLE_ITALIC
;
196 case wxT('O'): // oblique
197 m_style
= wxFONTSTYLE_SLANT
;
201 m_style
= wxFONTSTYLE_NORMAL
;
205 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
207 // size in XLFD is in 10 point units
208 m_pointSize
= (int)(ptSize
/ 10);
212 m_pointSize
= wxDEFAULT_FONT_SIZE
;
215 // examine the spacing: if the font is monospaced, assume wxTELETYPE
216 // family for compatibility with the old code which used it instead of
218 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == wxT('M') )
220 m_family
= wxFONTFAMILY_TELETYPE
;
222 else // not monospaceed
224 // don't even try guessing it, it doesn't work for too many fonts
226 m_family
= wxFONTFAMILY_UNKNOWN
;
229 // X fonts are never underlined...
230 m_underlined
= false;
232 // deal with font encoding
234 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
235 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
237 if ( registry
== wxT("ISO8859") )
240 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
242 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
245 else if ( registry
== wxT("MICROSOFT") )
248 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
250 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
253 else if ( registry
== wxT("KOI8") )
255 m_encoding
= wxFONTENCODING_KOI8
;
257 else // unknown encoding
259 // may be give a warning here? or use wxFontMapper?
260 m_encoding
= wxFONTENCODING_SYSTEM
;
264 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
267 m_pointSize
= data
.m_pointSize
;
268 m_family
= data
.m_family
;
269 m_style
= data
.m_style
;
270 m_weight
= data
.m_weight
;
272 m_underlined
= data
.m_underlined
;
274 m_faceName
= data
.m_faceName
;
275 m_encoding
= data
.m_encoding
;
277 // Forces a copy of the internal data. wxNativeFontInfo should probably
278 // have a copy ctor and assignment operator to fix this properly but that
279 // would break binary compatibility...
280 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
283 wxFontRefData::wxFontRefData(int size
, wxFontFamily family
, wxFontStyle style
,
284 wxFontWeight weight
, bool underlined
,
285 const wxString
& faceName
,
286 wxFontEncoding encoding
)
288 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
291 wxFontRefData::wxFontRefData(const wxString
& fontname
)
293 // FromString() should really work in GTK1 too, doesn't it?
294 m_nativeFontInfo
.SetXFontName(fontname
);
299 void wxFontRefData::ClearGdkFonts()
301 for ( wxScaledFontList::iterator i
= m_scaled_xfonts
.begin();
302 i
!= m_scaled_xfonts
.end();
305 GdkFont
*font
= i
->second
;
306 gdk_font_unref( font
);
309 m_scaled_xfonts
.clear();
312 wxFontRefData::~wxFontRefData()
317 // ----------------------------------------------------------------------------
318 // wxFontRefData SetXXX()
319 // ----------------------------------------------------------------------------
321 void wxFontRefData::SetPointSize(int pointSize
)
323 m_pointSize
= pointSize
;
325 if ( HasNativeFont() )
328 if ( pointSize
== -1 )
331 size
.Printf(wxT("%d"), 10*pointSize
);
333 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
337 void wxFontRefData::SetFamily(wxFontFamily family
)
341 // TODO: what are we supposed to do with m_nativeFontInfo here?
344 void wxFontRefData::SetStyle(wxFontStyle style
)
348 if ( HasNativeFont() )
353 case wxFONTSTYLE_ITALIC
:
357 case wxFONTSTYLE_SLANT
:
362 wxFAIL_MSG( wxT("unknown font style") );
365 case wxFONTSTYLE_NORMAL
:
369 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
373 void wxFontRefData::SetWeight(wxFontWeight weight
)
377 if ( HasNativeFont() )
382 case wxFONTWEIGHT_BOLD
:
383 boldness
= wxT("bold");
386 case wxFONTWEIGHT_LIGHT
:
387 boldness
= wxT("light");
391 wxFAIL_MSG( wxT("unknown font weight") );
394 case wxFONTWEIGHT_NORMAL
:
396 boldness
= wxT("medium");
399 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
403 void wxFontRefData::SetUnderlined(bool underlined
)
405 m_underlined
= underlined
;
407 // the XLFD doesn't have "underlined" field anyhow
410 bool wxFontRefData::SetFaceName(const wxString
& facename
)
412 m_faceName
= facename
;
414 if ( HasNativeFont() )
416 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
422 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
424 m_encoding
= encoding
;
426 if ( HasNativeFont() )
428 wxNativeEncodingInfo info
;
429 if ( wxGetNativeFontEncoding(encoding
, &info
) )
431 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
432 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
437 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
439 // previously cached fonts shouldn't be used
442 m_nativeFontInfo
= info
;
444 // set all the other font parameters from the native font info
448 // ----------------------------------------------------------------------------
450 // ----------------------------------------------------------------------------
452 wxFont::wxFont(const wxNativeFontInfo
& info
)
454 (void) Create(info
.GetXFontName());
457 bool wxFont::Create( int pointSize
,
462 const wxString
& face
,
463 wxFontEncoding encoding
)
467 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
468 underlined
, face
, encoding
);
473 bool wxFont::Create(const wxString
& fontname
)
475 // VZ: does this really happen?
476 if ( fontname
.empty() )
478 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
483 m_refData
= new wxFontRefData(fontname
);
488 void wxFont::Unshare()
492 m_refData
= new wxFontRefData();
496 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
506 wxGDIRefData
*wxFont::CreateGDIRefData() const
508 return new wxFontRefData
;
511 wxGDIRefData
*wxFont::CloneGDIRefData(const wxGDIRefData
*data
) const
513 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));
516 // ----------------------------------------------------------------------------
518 // ----------------------------------------------------------------------------
520 int wxFont::GetPointSize() const
522 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
524 return M_FONTDATA
->m_pointSize
;
527 wxString
wxFont::GetFaceName() const
529 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
531 return M_FONTDATA
->m_faceName
;
534 wxFontFamily
wxFont::DoGetFamily() const
536 return M_FONTDATA
->m_family
;
539 wxFontStyle
wxFont::GetStyle() const
541 wxCHECK_MSG( Ok(), wxFONTSTYLE_MAX
, wxT("invalid font") );
543 return M_FONTDATA
->m_style
;
546 wxFontWeight
wxFont::GetWeight() const
548 wxCHECK_MSG( Ok(), wxFONTWEIGHT_MAX
, wxT("invalid font") );
550 return M_FONTDATA
->m_weight
;
553 bool wxFont::GetUnderlined() const
555 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
557 return M_FONTDATA
->m_underlined
;
560 wxFontEncoding
wxFont::GetEncoding() const
562 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
564 // m_encoding is unused in wxGTK2, return encoding that the user set.
565 return M_FONTDATA
->m_encoding
;
568 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
570 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
572 if ( !M_FONTDATA
->HasNativeFont() )
574 // NB: this call has important side-effect: it not only finds
575 // GdkFont representation, it also initializes m_nativeFontInfo
576 // by calling its SetXFontName method
580 return &(M_FONTDATA
->m_nativeFontInfo
);
583 bool wxFont::IsFixedWidth() const
585 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
587 if ( M_FONTDATA
->HasNativeFont() )
589 // the monospace fonts are supposed to have "M" in the spacing field
590 wxString spacing
= M_FONTDATA
->
591 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
593 return spacing
.Upper() == wxT('M');
596 return wxFontBase::IsFixedWidth();
599 // ----------------------------------------------------------------------------
600 // change font attributes
601 // ----------------------------------------------------------------------------
603 void wxFont::SetPointSize(int pointSize
)
607 M_FONTDATA
->SetPointSize(pointSize
);
610 void wxFont::SetFamily(wxFontFamily family
)
614 M_FONTDATA
->SetFamily(family
);
617 void wxFont::SetStyle(wxFontStyle style
)
621 M_FONTDATA
->SetStyle(style
);
624 void wxFont::SetWeight(wxFontWeight weight
)
628 M_FONTDATA
->SetWeight(weight
);
631 bool wxFont::SetFaceName(const wxString
& faceName
)
635 return M_FONTDATA
->SetFaceName(faceName
) &&
636 wxFontBase::SetFaceName(faceName
);
639 void wxFont::SetUnderlined(bool underlined
)
643 M_FONTDATA
->SetUnderlined(underlined
);
646 void wxFont::SetEncoding(wxFontEncoding encoding
)
650 M_FONTDATA
->SetEncoding(encoding
);
653 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
657 M_FONTDATA
->SetNativeFontInfo( info
);
660 // ----------------------------------------------------------------------------
661 // get internal representation of font
662 // ----------------------------------------------------------------------------
664 static GdkFont
*g_systemDefaultGuiFont
= NULL
;
666 // this is also used from toolbar.cpp and tooltip.cpp, hence extern
667 extern GdkFont
*GtkGetDefaultGuiFont()
669 if (!g_systemDefaultGuiFont
)
671 GtkWidget
*widget
= gtk_button_new();
672 GtkStyle
*def
= gtk_rc_get_style( widget
);
675 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
679 def
= gtk_widget_get_default_style();
681 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
683 gtk_widget_destroy( widget
);
687 // already have it, but ref it once more before returning
688 gdk_font_ref(g_systemDefaultGuiFont
);
691 return g_systemDefaultGuiFont
;
694 GdkFont
*wxFont::GetInternalFont( float scale
) const
696 GdkFont
*font
= NULL
;
698 wxCHECK_MSG( Ok(), font
, wxT("invalid font") );
700 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
701 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
703 wxScaledFontList
& list
= M_FONTDATA
->m_scaled_xfonts
;
704 wxScaledFontList::iterator i
= list
.find(int_scale
);
705 if ( i
!= list
.end() )
709 else // we don't have this font in this size yet
711 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
713 font
= GtkGetDefaultGuiFont();
718 // do we have the XLFD?
719 if ( int_scale
== 100 && M_FONTDATA
->HasNativeFont() )
721 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
724 // no XLFD of no exact match - try the approximate one now
728 font
= wxLoadQueryNearestFont( point_scale
,
729 M_FONTDATA
->m_family
,
731 M_FONTDATA
->m_weight
,
732 M_FONTDATA
->m_underlined
,
733 M_FONTDATA
->m_faceName
,
734 M_FONTDATA
->m_encoding
,
736 // NB: wxFont::GetNativeFontInfo relies on this
737 // side-effect of GetInternalFont
738 if ( int_scale
== 100 )
739 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(xfontname
);
745 list
[int_scale
] = font
;
749 // it's quite useless to make it a wxCHECK because we're going to crash
751 wxASSERT_MSG( font
, wxT("could not load any font?") );