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"
34 #include <gdk/gdkprivate.h>
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // the default size (in points) for the fonts
42 static const int wxDEFAULT_FONT_SIZE
= 12;
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // TODO: replace this with a type safe list or hash!!
49 class wxScaledFontList
: public wxList
52 wxScaledFontList() : wxList(wxKEY_INTEGER
) { }
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 class wxFontRefData
: public wxObjectRefData
62 // from broken down font parameters, also default ctor
63 wxFontRefData(int size
= -1,
64 int family
= wxFONTFAMILY_DEFAULT
,
65 int style
= wxFONTSTYLE_NORMAL
,
66 int weight
= wxFONTWEIGHT_NORMAL
,
67 bool underlined
= FALSE
,
68 const wxString
& faceName
= wxEmptyString
,
69 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
72 wxFontRefData(const wxString
& fontname
);
75 wxFontRefData( const wxFontRefData
& data
);
77 virtual ~wxFontRefData();
79 // do we have the native font info?
80 bool HasNativeFont() const
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(int family
);
89 void SetStyle(int style
);
90 void SetWeight(int weight
);
91 void SetUnderlined(bool underlined
);
92 void SetFaceName(const wxString
& facename
);
93 void SetEncoding(wxFontEncoding encoding
);
95 // debugger helper: shows what the font really is
97 // VZ: I need this as my gdb either shows wildly wrong values or crashes
98 // when I ask it to "p fontRefData" :-(
102 wxPrintf(_T("%s-%s-%s-%d-%d\n"),
104 m_weight
== wxFONTWEIGHT_NORMAL
106 : m_weight
== wxFONTWEIGHT_BOLD
109 m_style
== wxFONTSTYLE_NORMAL
? _T("regular") : _T("italic"),
116 // common part of all ctors
117 void Init(int pointSize
,
122 const wxString
& faceName
,
123 wxFontEncoding encoding
);
126 // the map of font sizes to "GdkFont *"
127 wxScaledFontList m_scaled_xfonts
;
129 // the broken down font parameters
136 wxFontEncoding m_encoding
;
138 // the native font info, basicly an XFLD
139 wxNativeFontInfo m_nativeFontInfo
;
144 // ============================================================================
145 // wxFontRefData implementation
146 // ============================================================================
148 // ----------------------------------------------------------------------------
149 // wxFontRefData creation
150 // ----------------------------------------------------------------------------
152 void wxFontRefData::Init(int pointSize
,
157 const wxString
& faceName
,
158 wxFontEncoding encoding
)
160 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
162 m_faceName
= faceName
;
164 // we accept both wxDEFAULT and wxNORMAL here - should we?
165 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
166 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
168 // and here, do we really want to forbid creation of the font of the size
169 // 90 (the value of wxDEFAULT)??
170 m_pointSize
= pointSize
== wxDEFAULT
||
171 pointSize
== -1 ? wxDEFAULT_FONT_SIZE
: pointSize
;
173 m_underlined
= underlined
;
174 m_encoding
= encoding
;
177 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
179 m_pointSize
= data
.m_pointSize
;
180 m_family
= data
.m_family
;
181 m_style
= data
.m_style
;
182 m_weight
= data
.m_weight
;
184 m_underlined
= data
.m_underlined
;
186 m_faceName
= data
.m_faceName
;
187 m_encoding
= data
.m_encoding
;
189 m_nativeFontInfo
= data
.m_nativeFontInfo
;
192 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
193 int weight
, bool underlined
,
194 const wxString
& faceName
,
195 wxFontEncoding encoding
)
197 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
200 wxFontRefData::wxFontRefData(const wxString
& fontname
)
202 // remember the X font name
203 m_nativeFontInfo
.SetXFontName(fontname
);
205 // get the font parameters from the XLFD
206 // -------------------------------------
208 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
210 m_weight
= wxFONTWEIGHT_NORMAL
;
212 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
213 if ( !w
.empty() && w
!= _T('*') )
215 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
217 if ( ((w
[0u] == _T('B') && (!strcmp(w
.c_str() + 1, _T("OLD")) ||
218 !strcmp(w
.c_str() + 1, _T("LACK"))))) ||
219 strstr(w
.c_str() + 1, _T("BOLD")) )
221 m_weight
= wxFONTWEIGHT_BOLD
;
223 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
225 m_weight
= wxFONTWEIGHT_LIGHT
;
229 switch ( wxToupper(*m_nativeFontInfo
.
230 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
232 case _T('I'): // italique
233 m_style
= wxFONTSTYLE_ITALIC
;
236 case _T('O'): // oblique
237 m_style
= wxFONTSTYLE_SLANT
;
241 m_style
= wxFONTSTYLE_NORMAL
;
245 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
247 // size in XLFD is in 10 point units
248 m_pointSize
= (int)(ptSize
/ 10);
252 m_pointSize
= wxDEFAULT_FONT_SIZE
;
255 // examine the spacing: if the font is monospaced, assume wxTELETYPE
256 // family for compatibility with the old code which used it instead of
258 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
260 m_family
= wxFONTFAMILY_TELETYPE
;
262 else // not monospaceed
264 // don't even try guessing it, it doesn't work for too many fonts
266 m_family
= wxFONTFAMILY_UNKNOWN
;
269 // X fonts are never underlined...
270 m_underlined
= FALSE
;
272 // deal with font encoding
274 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
275 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
277 if ( registry
== _T("ISO8859") )
280 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
282 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
285 else if ( registry
== _T("MICROSOFT") )
288 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
290 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
293 else if ( registry
== _T("KOI8") )
295 m_encoding
= wxFONTENCODING_KOI8
;
297 else // unknown encoding
299 // may be give a warning here?
300 m_encoding
= wxFONTENCODING_SYSTEM
;
304 wxFontRefData::~wxFontRefData()
306 wxNode
*node
= m_scaled_xfonts
.First();
309 GdkFont
*font
= (GdkFont
*)node
->Data();
310 wxNode
*next
= node
->Next();
311 gdk_font_unref( font
);
316 // ----------------------------------------------------------------------------
317 // wxFontRefData SetXXX()
318 // ----------------------------------------------------------------------------
320 void wxFontRefData::SetPointSize(int pointSize
)
322 m_pointSize
= pointSize
;
324 if ( HasNativeFont() )
327 if ( pointSize
== -1 )
330 size
.Printf(_T("%d"), 10*pointSize
);
332 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
336 void wxFontRefData::SetFamily(int family
)
340 // TODO: what are we supposed to do with m_nativeFontInfo here?
343 void wxFontRefData::SetStyle(int style
)
347 if ( HasNativeFont() )
352 case wxFONTSTYLE_ITALIC
:
356 case wxFONTSTYLE_SLANT
:
361 wxFAIL_MSG( _T("unknown font style") );
364 case wxFONTSTYLE_NORMAL
:
368 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
372 void wxFontRefData::SetWeight(int weight
)
376 if ( HasNativeFont() )
381 case wxFONTWEIGHT_BOLD
:
382 boldness
= _T("bold");
385 case wxFONTWEIGHT_LIGHT
:
386 boldness
= _T("light");
390 wxFAIL_MSG( _T("unknown font weight") );
393 case wxFONTWEIGHT_NORMAL
:
395 boldness
= _T("medium");
398 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
402 void wxFontRefData::SetUnderlined(bool underlined
)
404 m_underlined
= underlined
;
406 // the XLFD doesn't have "underlined" field anyhow
409 void wxFontRefData::SetFaceName(const wxString
& facename
)
411 m_faceName
= facename
;
413 if ( HasNativeFont() )
415 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
419 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
421 m_encoding
= encoding
;
423 if ( HasNativeFont() )
425 wxNativeEncodingInfo info
;
426 if ( wxGetNativeFontEncoding(encoding
, &info
) )
428 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
429 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
434 // ============================================================================
435 // wxFont implementation
436 // ============================================================================
438 // ----------------------------------------------------------------------------
440 // ----------------------------------------------------------------------------
442 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
448 wxFont::wxFont(const wxNativeFontInfo
& info
)
452 Create(info
.GetXFontName());
455 bool wxFont::Create( int pointSize
,
460 const wxString
& face
,
461 wxFontEncoding encoding
)
463 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
464 underlined
, face
, encoding
);
469 bool wxFont::Create(const wxString
& fontname
)
471 // VZ: does this really happen?
472 if ( fontname
.empty() )
474 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
479 m_refData
= new wxFontRefData(fontname
);
484 void wxFont::Unshare()
488 m_refData
= new wxFontRefData();
492 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
502 // ----------------------------------------------------------------------------
504 // ----------------------------------------------------------------------------
506 // all accessors are just forwarded to wxFontRefData which has everything we
509 int wxFont::GetPointSize() const
511 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
513 return M_FONTDATA
->m_pointSize
;
516 wxString
wxFont::GetFaceName() const
518 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
520 return M_FONTDATA
->m_faceName
;
523 int wxFont::GetFamily() const
525 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
527 return M_FONTDATA
->m_family
;
530 int wxFont::GetStyle() const
532 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
534 return M_FONTDATA
->m_style
;
537 int wxFont::GetWeight() const
539 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
541 return M_FONTDATA
->m_weight
;
544 bool wxFont::GetUnderlined() const
546 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
548 return M_FONTDATA
->m_underlined
;
551 wxFontEncoding
wxFont::GetEncoding() const
553 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
555 return M_FONTDATA
->m_encoding
;
558 wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
560 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
562 if ( M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty() )
565 return new wxNativeFontInfo(M_FONTDATA
->m_nativeFontInfo
);
568 bool wxFont::IsFixedWidth() const
570 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
572 if ( M_FONTDATA
->HasNativeFont() )
574 // the monospace fonts are supposed to have "M" in the spacing field
575 wxString spacing
= M_FONTDATA
->
576 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
578 return spacing
.Upper() == _T('M');
581 return wxFontBase::IsFixedWidth();
584 // ----------------------------------------------------------------------------
585 // change font attributes
586 // ----------------------------------------------------------------------------
588 void wxFont::SetPointSize(int pointSize
)
592 M_FONTDATA
->SetPointSize(pointSize
);
595 void wxFont::SetFamily(int family
)
599 M_FONTDATA
->SetFamily(family
);
602 void wxFont::SetStyle(int style
)
606 M_FONTDATA
->SetStyle(style
);
609 void wxFont::SetWeight(int weight
)
613 M_FONTDATA
->SetWeight(weight
);
616 void wxFont::SetFaceName(const wxString
& faceName
)
620 M_FONTDATA
->SetFaceName(faceName
);
623 void wxFont::SetUnderlined(bool underlined
)
627 M_FONTDATA
->SetUnderlined(underlined
);
630 void wxFont::SetEncoding(wxFontEncoding encoding
)
634 M_FONTDATA
->SetEncoding(encoding
);
637 void wxFont::SetNativeFontInfo(const wxNativeFontInfo
& info
)
641 M_FONTDATA
->m_nativeFontInfo
= info
;
644 // ----------------------------------------------------------------------------
645 // get internal representation of font
646 // ----------------------------------------------------------------------------
648 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
650 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
651 extern GdkFont
*GtkGetDefaultGuiFont()
653 if (!g_systemDefaultGuiFont
)
655 GtkWidget
*widget
= gtk_button_new();
656 GtkStyle
*def
= gtk_rc_get_style( widget
);
659 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
663 def
= gtk_widget_get_default_style();
665 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
667 gtk_widget_destroy( widget
);
671 // already have it, but ref it once more before returning
672 gdk_font_ref(g_systemDefaultGuiFont
);
675 return g_systemDefaultGuiFont
;
678 GdkFont
*wxFont::GetInternalFont( float scale
) const
680 GdkFont
*font
= (GdkFont
*) NULL
;
682 wxCHECK_MSG( Ok(), font
, wxT("invalid font") )
684 long int_scale
= long(scale
* 100.0 + 0.5); /* key for fontlist */
685 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
687 wxNode
*node
= M_FONTDATA
->m_scaled_xfonts
.Find(int_scale
);
690 font
= (GdkFont
*)node
->Data();
692 else // we don't have this font in this size yet
694 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
696 font
= GtkGetDefaultGuiFont();
701 // do we have the XLFD?
702 if ( M_FONTDATA
->HasNativeFont() )
704 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
707 // no XLFD of no exact match - try the approximate one now
711 font
= wxLoadQueryNearestFont( point_scale
,
712 M_FONTDATA
->m_family
,
714 M_FONTDATA
->m_weight
,
715 M_FONTDATA
->m_underlined
,
716 M_FONTDATA
->m_faceName
,
717 M_FONTDATA
->m_encoding
,
721 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(xfontname
);
728 M_FONTDATA
->m_scaled_xfonts
.Append( int_scale
, (wxObject
*)font
);
732 // it's quite useless to make it a wxCHECK because we're going to crash
734 wxASSERT_MSG( font
, wxT("could not load any font?") );