1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
19 #pragma implementation "font.h"
23 #include "wx/fontutil.h"
24 #include "wx/cmndata.h"
27 #include "wx/gdicmn.h"
28 #include "wx/tokenzr.h"
29 #include "wx/settings.h"
33 #include "wx/gtk/private.h"
34 #include <gdk/gdkprivate.h>
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // the default size (in points) for the fonts
41 static const int wxDEFAULT_FONT_SIZE
= 12;
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // TODO: replace this with a type safe list or hash!!
48 class wxScaledFontList
: public wxList
51 wxScaledFontList() : wxList(wxKEY_INTEGER
) { }
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 class wxFontRefData
: public wxObjectRefData
61 // from broken down font parameters, also default ctor
62 wxFontRefData(int size
= -1,
63 int family
= wxFONTFAMILY_DEFAULT
,
64 int style
= wxFONTSTYLE_NORMAL
,
65 int 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 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 void SetFaceName(const wxString
& facename
);
92 void SetEncoding(wxFontEncoding encoding
);
94 // debugger helper: shows what the font really is
96 // VZ: I need this as my gdb either shows wildly wrong values or crashes
97 // when I ask it to "p fontRefData" :-(
101 wxPrintf(_T("%s-%s-%s-%d-%d\n"),
103 m_weight
== wxFONTWEIGHT_NORMAL
105 : m_weight
== wxFONTWEIGHT_BOLD
108 m_style
== wxFONTSTYLE_NORMAL
? _T("regular") : _T("italic"),
115 // common part of all ctors
116 void Init(int pointSize
,
121 const wxString
& faceName
,
122 wxFontEncoding encoding
);
125 // the map of font sizes to "GdkFont *"
126 wxScaledFontList m_scaled_xfonts
;
128 // the broken down font parameters
135 wxFontEncoding m_encoding
;
137 // the native font info, basicly an XFLD
138 wxNativeFontInfo m_nativeFontInfo
;
143 // ============================================================================
144 // wxFontRefData implementation
145 // ============================================================================
147 // ----------------------------------------------------------------------------
148 // wxFontRefData creation
149 // ----------------------------------------------------------------------------
151 void wxFontRefData::Init(int pointSize
,
156 const wxString
& faceName
,
157 wxFontEncoding encoding
)
159 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
161 m_faceName
= faceName
;
163 // we accept both wxDEFAULT and wxNORMAL here - should we?
164 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
165 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
167 // and here, do we really want to forbid creation of the font of the size
168 // 90 (the value of wxDEFAULT)??
169 m_pointSize
= pointSize
== wxDEFAULT
||
170 pointSize
== -1 ? wxDEFAULT_FONT_SIZE
: pointSize
;
172 m_underlined
= underlined
;
173 m_encoding
= encoding
;
176 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
178 m_pointSize
= data
.m_pointSize
;
179 m_family
= data
.m_family
;
180 m_style
= data
.m_style
;
181 m_weight
= data
.m_weight
;
183 m_underlined
= data
.m_underlined
;
185 m_faceName
= data
.m_faceName
;
186 m_encoding
= data
.m_encoding
;
188 m_nativeFontInfo
= data
.m_nativeFontInfo
;
191 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
192 int weight
, bool underlined
,
193 const wxString
& faceName
,
194 wxFontEncoding encoding
)
196 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
199 wxFontRefData::wxFontRefData(const wxString
& fontname
)
201 // remember the X font name
202 m_nativeFontInfo
.SetXFontName(fontname
);
204 // get the font parameters from the XLFD
205 // -------------------------------------
207 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
209 m_weight
= wxFONTWEIGHT_NORMAL
;
211 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
212 if ( !w
.empty() && w
!= _T('*') )
214 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
216 if ( ((w
[0u] == _T('B') && (!strcmp(w
.c_str() + 1, _T("OLD")) ||
217 !strcmp(w
.c_str() + 1, _T("LACK"))))) ||
218 strstr(w
.c_str() + 1, _T("BOLD")) )
220 m_weight
= wxFONTWEIGHT_BOLD
;
222 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
224 m_weight
= wxFONTWEIGHT_LIGHT
;
228 switch ( wxToupper(*m_nativeFontInfo
.
229 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
231 case _T('I'): // italique
232 m_style
= wxFONTSTYLE_ITALIC
;
235 case _T('O'): // oblique
236 m_style
= wxFONTSTYLE_SLANT
;
240 m_style
= wxFONTSTYLE_NORMAL
;
244 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
246 // size in XLFD is in 10 point units
247 m_pointSize
= (int)(ptSize
/ 10);
251 m_pointSize
= wxDEFAULT_FONT_SIZE
;
254 // examine the spacing: if the font is monospaced, assume wxTELETYPE
255 // family for compatibility with the old code which used it instead of
257 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
259 m_family
= wxFONTFAMILY_TELETYPE
;
261 else // not monospaceed
263 // don't even try guessing it, it doesn't work for too many fonts
265 m_family
= wxFONTFAMILY_UNKNOWN
;
268 // X fonts are never underlined...
269 m_underlined
= FALSE
;
271 // deal with font encoding
273 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
274 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
276 if ( registry
== _T("ISO8859") )
279 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
281 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
284 else if ( registry
== _T("MICROSOFT") )
287 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
289 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
292 else if ( registry
== _T("KOI8") )
294 m_encoding
= wxFONTENCODING_KOI8
;
296 else // unknown encoding
298 // may be give a warning here?
299 m_encoding
= wxFONTENCODING_SYSTEM
;
303 wxFontRefData::~wxFontRefData()
305 wxNode
*node
= m_scaled_xfonts
.First();
308 GdkFont
*font
= (GdkFont
*)node
->Data();
309 wxNode
*next
= node
->Next();
310 gdk_font_unref( font
);
315 // ----------------------------------------------------------------------------
316 // wxFontRefData SetXXX()
317 // ----------------------------------------------------------------------------
319 void wxFontRefData::SetPointSize(int pointSize
)
321 m_pointSize
= pointSize
;
323 if ( HasNativeFont() )
326 if ( pointSize
== -1 )
329 size
.Printf(_T("%d"), 10*pointSize
);
331 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
335 void wxFontRefData::SetFamily(int family
)
339 // TODO: what are we supposed to do with m_nativeFontInfo here?
342 void wxFontRefData::SetStyle(int style
)
346 if ( HasNativeFont() )
351 case wxFONTSTYLE_ITALIC
:
355 case wxFONTSTYLE_SLANT
:
360 wxFAIL_MSG( _T("unknown font style") );
363 case wxFONTSTYLE_NORMAL
:
367 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
371 void wxFontRefData::SetWeight(int weight
)
375 if ( HasNativeFont() )
380 case wxFONTWEIGHT_BOLD
:
381 boldness
= _T("bold");
384 case wxFONTWEIGHT_LIGHT
:
385 boldness
= _T("light");
389 wxFAIL_MSG( _T("unknown font weight") );
392 case wxFONTWEIGHT_NORMAL
:
394 boldness
= _T("medium");
397 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
401 void wxFontRefData::SetUnderlined(bool underlined
)
403 m_underlined
= underlined
;
405 // the XLFD doesn't have "underlined" field anyhow
408 void wxFontRefData::SetFaceName(const wxString
& facename
)
410 m_faceName
= facename
;
412 if ( HasNativeFont() )
414 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
418 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
420 m_encoding
= encoding
;
422 if ( HasNativeFont() )
424 wxNativeEncodingInfo info
;
425 if ( wxGetNativeFontEncoding(encoding
, &info
) )
427 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
428 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
433 // ============================================================================
434 // wxFont implementation
435 // ============================================================================
437 // ----------------------------------------------------------------------------
439 // ----------------------------------------------------------------------------
441 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
447 wxFont::wxFont(const wxNativeFontInfo
& info
)
451 Create(info
.GetXFontName());
454 bool wxFont::Create( int pointSize
,
459 const wxString
& face
,
460 wxFontEncoding encoding
)
462 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
463 underlined
, face
, encoding
);
468 bool wxFont::Create(const wxString
& fontname
)
470 // VZ: does this really happen?
471 if ( fontname
.empty() )
473 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
478 m_refData
= new wxFontRefData(fontname
);
483 void wxFont::Unshare()
487 m_refData
= new wxFontRefData();
491 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
501 // ----------------------------------------------------------------------------
503 // ----------------------------------------------------------------------------
505 // all accessors are just forwarded to wxFontRefData which has everything we
508 int wxFont::GetPointSize() const
510 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
512 return M_FONTDATA
->m_pointSize
;
515 wxString
wxFont::GetFaceName() const
517 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
519 return M_FONTDATA
->m_faceName
;
522 int wxFont::GetFamily() const
524 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
526 return M_FONTDATA
->m_family
;
529 int wxFont::GetStyle() const
531 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
533 return M_FONTDATA
->m_style
;
536 int wxFont::GetWeight() const
538 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
540 return M_FONTDATA
->m_weight
;
543 bool wxFont::GetUnderlined() const
545 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
547 return M_FONTDATA
->m_underlined
;
550 wxFontEncoding
wxFont::GetEncoding() const
552 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
554 return M_FONTDATA
->m_encoding
;
557 wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
559 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
561 if ( M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty() )
564 return new wxNativeFontInfo(M_FONTDATA
->m_nativeFontInfo
);
567 bool wxFont::IsFixedWidth() const
569 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
571 if ( M_FONTDATA
->HasNativeFont() )
573 // the monospace fonts are supposed to have "M" in the spacing field
574 wxString spacing
= M_FONTDATA
->
575 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
577 return spacing
.Upper() == _T('M');
580 return wxFontBase::IsFixedWidth();
583 // ----------------------------------------------------------------------------
584 // change font attributes
585 // ----------------------------------------------------------------------------
587 void wxFont::SetPointSize(int pointSize
)
591 M_FONTDATA
->SetPointSize(pointSize
);
594 void wxFont::SetFamily(int family
)
598 M_FONTDATA
->SetFamily(family
);
601 void wxFont::SetStyle(int style
)
605 M_FONTDATA
->SetStyle(style
);
608 void wxFont::SetWeight(int weight
)
612 M_FONTDATA
->SetWeight(weight
);
615 void wxFont::SetFaceName(const wxString
& faceName
)
619 M_FONTDATA
->SetFaceName(faceName
);
622 void wxFont::SetUnderlined(bool underlined
)
626 M_FONTDATA
->SetUnderlined(underlined
);
629 void wxFont::SetEncoding(wxFontEncoding encoding
)
633 M_FONTDATA
->SetEncoding(encoding
);
636 void wxFont::SetNativeFontInfo(const wxNativeFontInfo
& info
)
640 M_FONTDATA
->m_nativeFontInfo
= info
;
643 // ----------------------------------------------------------------------------
644 // get internal representation of font
645 // ----------------------------------------------------------------------------
647 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
649 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
650 extern GdkFont
*GtkGetDefaultGuiFont()
652 if (!g_systemDefaultGuiFont
)
654 GtkWidget
*widget
= gtk_button_new();
655 GtkStyle
*def
= gtk_rc_get_style( widget
);
658 g_systemDefaultGuiFont
= gdk_font_ref( GET_STYLE_FONT(def
) );
662 def
= gtk_widget_get_default_style();
664 g_systemDefaultGuiFont
= gdk_font_ref( GET_STYLE_FONT(def
) );
666 gtk_widget_destroy( widget
);
670 // already have it, but ref it once more before returning
671 gdk_font_ref(g_systemDefaultGuiFont
);
674 return g_systemDefaultGuiFont
;
677 GdkFont
*wxFont::GetInternalFont( float scale
) const
679 GdkFont
*font
= (GdkFont
*) NULL
;
681 wxCHECK_MSG( Ok(), font
, wxT("invalid font") )
683 long int_scale
= long(scale
* 100.0 + 0.5); /* key for fontlist */
684 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
686 wxNode
*node
= M_FONTDATA
->m_scaled_xfonts
.Find(int_scale
);
689 font
= (GdkFont
*)node
->Data();
691 else // we don't have this font in this size yet
693 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
695 font
= GtkGetDefaultGuiFont();
700 // do we have the XLFD?
701 if ( M_FONTDATA
->HasNativeFont() )
703 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
706 // no XLFD of no exact match - try the approximate one now
710 font
= wxLoadQueryNearestFont( point_scale
,
711 M_FONTDATA
->m_family
,
713 M_FONTDATA
->m_weight
,
714 M_FONTDATA
->m_underlined
,
715 M_FONTDATA
->m_faceName
,
716 M_FONTDATA
->m_encoding
,
720 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(xfontname
);
727 M_FONTDATA
->m_scaled_xfonts
.Append( int_scale
, (wxObject
*)font
);
731 // it's quite useless to make it a wxCHECK because we're going to crash
733 wxASSERT_MSG( font
, wxT("could not load any font?") );