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 class wxFontRefData
: public wxObjectRefData
44 wxFontRefData(int size
= wxDEFAULT
,
45 int family
= wxDEFAULT
,
46 int style
= wxDEFAULT
,
47 int weight
= wxDEFAULT
,
48 bool underlined
= FALSE
,
49 const wxString
& faceName
= wxEmptyString
,
50 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
51 wxFontRefData( const wxFontRefData
& data
);
52 virtual ~wxFontRefData();
55 // common part of all ctors
56 void Init(int pointSize
,
61 const wxString
& faceName
,
62 wxFontEncoding encoding
);
65 wxList m_scaled_xfonts
;
72 wxFontEncoding m_encoding
;
74 wxNativeFontInfo m_nativeFontInfo
;
79 // ============================================================================
81 // ============================================================================
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 void wxFontRefData::Init(int pointSize
,
92 const wxString
& faceName
,
93 wxFontEncoding encoding
)
95 if (family
== wxDEFAULT
)
100 m_faceName
= faceName
;
102 if (style
== wxDEFAULT
)
107 if (weight
== wxDEFAULT
)
112 if (pointSize
== wxDEFAULT
)
115 m_pointSize
= pointSize
;
117 m_underlined
= underlined
;
118 m_encoding
= encoding
;
121 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
122 : m_scaled_xfonts(wxKEY_INTEGER
)
124 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
125 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
128 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
129 int weight
, bool underlined
,
130 const wxString
& faceName
,
131 wxFontEncoding encoding
)
132 : m_scaled_xfonts(wxKEY_INTEGER
)
134 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
137 wxFontRefData::~wxFontRefData()
139 wxNode
*node
= m_scaled_xfonts
.First();
142 GdkFont
*font
= (GdkFont
*)node
->Data();
143 wxNode
*next
= node
->Next();
144 gdk_font_unref( font
);
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 bool wxNativeFontInfo::FromString(const wxString
& s
)
155 wxStringTokenizer
tokenizer(s
, _T(";"));
157 wxString token
= tokenizer
.GetNextToken();
159 // Ignore the version for now
162 xFontName
= tokenizer
.GetNextToken();
169 wxString
wxNativeFontInfo::ToString() const
173 s
.Printf(_T("%d;%s"),
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
184 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
190 wxFont::wxFont(const wxNativeFontInfo
& info
)
194 Create(info
.xFontName
);
197 bool wxFont::Create(const wxNativeFontInfo
& info
)
199 return Create(info
.xFontName
);
202 bool wxFont::Create( int pointSize
,
207 const wxString
& face
,
208 wxFontEncoding encoding
)
210 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
211 underlined
, face
, encoding
);
216 bool wxFont::Create(const wxString
& fontname
, wxFontEncoding enc
)
220 *this = wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
224 m_refData
= new wxFontRefData();
226 M_FONTDATA
->m_nativeFontInfo
.xFontName
= fontname
; // X font name
230 wxStringTokenizer
tn( fontname
, wxT("-") );
232 tn
.GetNextToken(); // skip initial empty token
233 tn
.GetNextToken(); // foundry
236 M_FONTDATA
->m_faceName
= tn
.GetNextToken(); // family
238 tmp
= tn
.GetNextToken().MakeUpper(); // weight
239 if (tmp
== wxT("BOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
240 if (tmp
== wxT("BLACK")) M_FONTDATA
->m_weight
= wxBOLD
;
241 if (tmp
== wxT("EXTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
242 if (tmp
== wxT("DEMIBOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
243 if (tmp
== wxT("ULTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
245 if (tmp
== wxT("LIGHT")) M_FONTDATA
->m_weight
= wxLIGHT
;
246 if (tmp
== wxT("THIN")) M_FONTDATA
->m_weight
= wxLIGHT
;
248 tmp
= tn
.GetNextToken().MakeUpper(); // slant
249 if (tmp
== wxT("I")) M_FONTDATA
->m_style
= wxITALIC
;
250 if (tmp
== wxT("O")) M_FONTDATA
->m_style
= wxITALIC
;
252 tn
.GetNextToken(); // set width
253 tn
.GetNextToken(); // add. style
254 tn
.GetNextToken(); // pixel size
256 tmp
= tn
.GetNextToken(); // pointsize
259 long num
= wxStrtol (tmp
.c_str(), (wxChar
**) NULL
, 10);
260 M_FONTDATA
->m_pointSize
= (int)(num
/ 10);
263 tn
.GetNextToken(); // x-res
264 tn
.GetNextToken(); // y-res
266 tmp
= tn
.GetNextToken().MakeUpper(); // spacing
269 M_FONTDATA
->m_family
= wxMODERN
;
270 else if (M_FONTDATA
->m_faceName
== wxT("TIMES"))
271 M_FONTDATA
->m_family
= wxROMAN
;
272 else if (M_FONTDATA
->m_faceName
== wxT("HELVETICA"))
273 M_FONTDATA
->m_family
= wxSWISS
;
274 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDATYPEWRITER"))
275 M_FONTDATA
->m_family
= wxTELETYPE
;
276 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDA"))
277 M_FONTDATA
->m_family
= wxDECORATIVE
;
278 else if (M_FONTDATA
->m_faceName
== wxT("UTOPIA"))
279 M_FONTDATA
->m_family
= wxSCRIPT
;
281 tn
.GetNextToken(); // avg width
283 // deal with font encoding
284 M_FONTDATA
->m_encoding
= enc
;
285 if ( M_FONTDATA
->m_encoding
== wxFONTENCODING_SYSTEM
)
287 wxString registry
= tn
.GetNextToken().MakeUpper(),
288 encoding
= tn
.GetNextToken().MakeUpper();
290 if ( registry
== _T("ISO8859") )
293 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
295 M_FONTDATA
->m_encoding
=
296 (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
299 else if ( registry
== _T("MICROSOFT") )
302 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
304 M_FONTDATA
->m_encoding
=
305 (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
308 else if ( registry
== _T("KOI8") )
310 M_FONTDATA
->m_encoding
= wxFONTENCODING_KOI8
;
312 //else: unknown encoding - may be give a warning here?
319 void wxFont::Unshare()
323 m_refData
= new wxFontRefData();
327 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
337 // ----------------------------------------------------------------------------
339 // ----------------------------------------------------------------------------
341 int wxFont::GetPointSize() const
343 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
345 return M_FONTDATA
->m_pointSize
;
348 wxString
wxFont::GetFaceName() const
350 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
352 return M_FONTDATA
->m_faceName
;
355 int wxFont::GetFamily() const
357 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
359 return M_FONTDATA
->m_family
;
362 int wxFont::GetStyle() const
364 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
366 return M_FONTDATA
->m_style
;
369 int wxFont::GetWeight() const
371 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
373 return M_FONTDATA
->m_weight
;
376 bool wxFont::GetUnderlined() const
378 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
380 return M_FONTDATA
->m_underlined
;
384 wxFontEncoding
wxFont::GetEncoding() const
386 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
388 return M_FONTDATA
->m_encoding
;
391 wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
393 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
395 if(M_FONTDATA
->m_nativeFontInfo
.xFontName
.IsEmpty())
398 return new wxNativeFontInfo(M_FONTDATA
->m_nativeFontInfo
);
402 // ----------------------------------------------------------------------------
403 // change font attributes
404 // ----------------------------------------------------------------------------
406 void wxFont::SetPointSize(int pointSize
)
410 M_FONTDATA
->m_pointSize
= pointSize
;
411 M_FONTDATA
->m_nativeFontInfo
.xFontName
.Clear(); // invalid now
414 void wxFont::SetFamily(int family
)
418 M_FONTDATA
->m_family
= family
;
419 M_FONTDATA
->m_nativeFontInfo
.xFontName
.Clear(); // invalid now
422 void wxFont::SetStyle(int style
)
426 M_FONTDATA
->m_style
= style
;
427 M_FONTDATA
->m_nativeFontInfo
.xFontName
.Clear(); // invalid now
430 void wxFont::SetWeight(int weight
)
434 M_FONTDATA
->m_weight
= weight
;
435 M_FONTDATA
->m_nativeFontInfo
.xFontName
.Clear(); // invalid now
438 void wxFont::SetFaceName(const wxString
& faceName
)
442 M_FONTDATA
->m_faceName
= faceName
;
443 M_FONTDATA
->m_nativeFontInfo
.xFontName
.Clear(); // invalid now
446 void wxFont::SetUnderlined(bool underlined
)
450 M_FONTDATA
->m_underlined
= underlined
;
453 void wxFont::SetEncoding(wxFontEncoding encoding
)
457 M_FONTDATA
->m_encoding
= encoding
;
458 M_FONTDATA
->m_nativeFontInfo
.xFontName
.Clear(); // invalid now
461 void wxFont::SetNativeFontInfo(const wxNativeFontInfo
& info
)
465 M_FONTDATA
->m_nativeFontInfo
= info
;
468 // ----------------------------------------------------------------------------
469 // get internal representation of font
470 // ----------------------------------------------------------------------------
472 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
474 GdkFont
*GtkGetDefaultGuiFont()
476 if (!g_systemDefaultGuiFont
)
478 GtkWidget
*widget
= gtk_button_new();
479 GtkStyle
*def
= gtk_rc_get_style( widget
);
482 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
486 def
= gtk_widget_get_default_style();
488 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
490 gtk_widget_destroy( widget
);
494 // already have it, but ref it once more before returning
495 gdk_font_ref(g_systemDefaultGuiFont
);
498 return g_systemDefaultGuiFont
;
501 GdkFont
*wxFont::GetInternalFont( float scale
) const
505 wxFAIL_MSG( wxT("invalid font") );
507 return (GdkFont
*) NULL
;
510 long int_scale
= long(scale
* 100.0 + 0.5); /* key for fontlist */
511 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
512 GdkFont
*font
= (GdkFont
*) NULL
;
514 wxNode
*node
= M_FONTDATA
->m_scaled_xfonts
.Find(int_scale
);
517 font
= (GdkFont
*)node
->Data();
521 if (*this == wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
))
523 font
= GtkGetDefaultGuiFont();
527 font
= wxLoadQueryNearestFont( point_scale
,
528 M_FONTDATA
->m_family
,
530 M_FONTDATA
->m_weight
,
531 M_FONTDATA
->m_underlined
,
532 M_FONTDATA
->m_faceName
,
533 M_FONTDATA
->m_encoding
,
534 &M_FONTDATA
->m_nativeFontInfo
.xFontName
);
537 M_FONTDATA
->m_scaled_xfonts
.Append( int_scale
, (wxObject
*)font
);
540 // it's quite useless to make it a wxCHECK because we're going to crash
542 wxASSERT_MSG( font
, wxT("could not load any font?") );