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
;
77 // ============================================================================
79 // ============================================================================
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 void wxFontRefData::Init(int pointSize
,
90 const wxString
& faceName
,
91 wxFontEncoding encoding
)
93 if (family
== wxDEFAULT
)
98 m_faceName
= faceName
;
100 if (style
== wxDEFAULT
)
105 if (weight
== wxDEFAULT
)
110 if (pointSize
== wxDEFAULT
)
113 m_pointSize
= pointSize
;
115 m_underlined
= underlined
;
116 m_encoding
= encoding
;
119 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
120 : m_scaled_xfonts(wxKEY_INTEGER
)
122 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
123 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
126 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
127 int weight
, bool underlined
, const wxString
& faceName
, wxFontEncoding encoding
)
128 : m_scaled_xfonts(wxKEY_INTEGER
)
130 Init(size
, family
, style
, weight
,
131 underlined
, faceName
, encoding
);
134 wxFontRefData::~wxFontRefData()
136 wxNode
*node
= m_scaled_xfonts
.First();
139 GdkFont
*font
= (GdkFont
*)node
->Data();
140 wxNode
*next
= node
->Next();
141 gdk_font_unref( font
);
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
155 wxTheFontList
->Append( this );
158 wxFont::wxFont( const wxString
& fontname
, const wxFontData
& fontdata
)
162 wxCHECK_RET( !!fontname
, _T("invalid font spec") );
164 m_refData
= new wxFontRefData();
168 wxStringTokenizer
tn( fontname
, wxT("-") );
170 tn
.GetNextToken(); // skip initial empty token
171 tn
.GetNextToken(); // foundry
173 M_FONTDATA
->m_faceName
= tn
.GetNextToken(); // family
175 tmp
= tn
.GetNextToken().MakeUpper(); // weight
176 if (tmp
== wxT("BOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
177 if (tmp
== wxT("BLACK")) M_FONTDATA
->m_weight
= wxBOLD
;
178 if (tmp
== wxT("EXTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
179 if (tmp
== wxT("DEMIBOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
180 if (tmp
== wxT("ULTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
182 if (tmp
== wxT("LIGHT")) M_FONTDATA
->m_weight
= wxLIGHT
;
183 if (tmp
== wxT("THIN")) M_FONTDATA
->m_weight
= wxLIGHT
;
185 tmp
= tn
.GetNextToken().MakeUpper(); // slant
186 if (tmp
== wxT("I")) M_FONTDATA
->m_style
= wxITALIC
;
187 if (tmp
== wxT("O")) M_FONTDATA
->m_style
= wxITALIC
;
189 tn
.GetNextToken(); // set width
190 tn
.GetNextToken(); // add. style
191 tn
.GetNextToken(); // pixel size
193 tmp
= tn
.GetNextToken(); // pointsize
196 long num
= wxStrtol (tmp
.c_str(), (wxChar
**) NULL
, 10);
197 M_FONTDATA
->m_pointSize
= (int)(num
/ 10);
200 tn
.GetNextToken(); // x-res
201 tn
.GetNextToken(); // y-res
203 tmp
= tn
.GetNextToken().MakeUpper(); // spacing
206 M_FONTDATA
->m_family
= wxMODERN
;
207 else if (M_FONTDATA
->m_faceName
== wxT("TIMES"))
208 M_FONTDATA
->m_family
= wxROMAN
;
209 else if (M_FONTDATA
->m_faceName
== wxT("HELVETICA"))
210 M_FONTDATA
->m_family
= wxSWISS
;
211 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDATYPEWRITER"))
212 M_FONTDATA
->m_family
= wxTELETYPE
;
213 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDA"))
214 M_FONTDATA
->m_family
= wxDECORATIVE
;
215 else if (M_FONTDATA
->m_faceName
== wxT("UTOPIA"))
216 M_FONTDATA
->m_family
= wxSCRIPT
;
218 tn
.GetNextToken(); // avg width
220 // deal with font encoding
221 M_FONTDATA
->m_encoding
= fontdata
.GetEncoding();
222 if ( M_FONTDATA
->m_encoding
== wxFONTENCODING_SYSTEM
)
224 wxString registry
= tn
.GetNextToken().MakeUpper(),
225 encoding
= tn
.GetNextToken().MakeUpper();
227 if ( registry
== _T("ISO8859") )
230 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
232 M_FONTDATA
->m_encoding
=
233 (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
236 else if ( registry
== _T("MICROSOFT") )
239 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
241 M_FONTDATA
->m_encoding
=
242 (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
245 else if ( registry
== _T("KOI8") )
247 M_FONTDATA
->m_encoding
= wxFONTENCODING_KOI8
;
249 //else: unknown encoding - may be give a warning here?
253 bool wxFont::Create( int pointSize
,
258 const wxString
& face
,
259 wxFontEncoding encoding
)
261 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
262 underlined
, face
, encoding
);
267 void wxFont::Unshare()
271 m_refData
= new wxFontRefData();
275 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
284 wxTheFontList
->DeleteObject( this );
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
291 int wxFont::GetPointSize() const
293 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
295 return M_FONTDATA
->m_pointSize
;
298 wxString
wxFont::GetFaceName() const
300 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
302 return M_FONTDATA
->m_faceName
;
305 int wxFont::GetFamily() const
307 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
309 return M_FONTDATA
->m_family
;
312 int wxFont::GetStyle() const
314 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
316 return M_FONTDATA
->m_style
;
319 int wxFont::GetWeight() const
321 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
323 return M_FONTDATA
->m_weight
;
326 bool wxFont::GetUnderlined() const
328 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
330 return M_FONTDATA
->m_underlined
;
334 wxFontEncoding
wxFont::GetEncoding() const
336 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
338 return M_FONTDATA
->m_encoding
;
341 // ----------------------------------------------------------------------------
342 // change font attributes
343 // ----------------------------------------------------------------------------
345 void wxFont::SetPointSize(int pointSize
)
349 M_FONTDATA
->m_pointSize
= pointSize
;
352 void wxFont::SetFamily(int family
)
356 M_FONTDATA
->m_family
= family
;
359 void wxFont::SetStyle(int style
)
363 M_FONTDATA
->m_style
= style
;
366 void wxFont::SetWeight(int weight
)
370 M_FONTDATA
->m_weight
= weight
;
373 void wxFont::SetFaceName(const wxString
& faceName
)
377 M_FONTDATA
->m_faceName
= faceName
;
380 void wxFont::SetUnderlined(bool underlined
)
384 M_FONTDATA
->m_underlined
= underlined
;
387 void wxFont::SetEncoding(wxFontEncoding encoding
)
391 M_FONTDATA
->m_encoding
= encoding
;
394 // ----------------------------------------------------------------------------
395 // get internal representation of font
396 // ----------------------------------------------------------------------------
398 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
400 static GdkFont
*GtkGetDefaultGuiFont()
402 if (!g_systemDefaultGuiFont
)
404 GtkWidget
*widget
= gtk_button_new();
405 GtkStyle
*def
= gtk_rc_get_style( widget
);
408 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
412 def
= gtk_widget_get_default_style();
414 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
416 gtk_widget_destroy( widget
);
418 return g_systemDefaultGuiFont
;
421 GdkFont
*wxFont::GetInternalFont( float scale
) const
425 wxFAIL_MSG( wxT("invalid font") );
427 return (GdkFont
*) NULL
;
430 long int_scale
= long(scale
* 100.0 + 0.5); /* key for fontlist */
431 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
432 GdkFont
*font
= (GdkFont
*) NULL
;
434 wxNode
*node
= M_FONTDATA
->m_scaled_xfonts
.Find(int_scale
);
437 font
= (GdkFont
*)node
->Data();
441 if (*this == wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
))
443 font
= GtkGetDefaultGuiFont();
447 font
= wxLoadQueryNearestFont( point_scale
,
448 M_FONTDATA
->m_family
,
450 M_FONTDATA
->m_weight
,
451 M_FONTDATA
->m_underlined
,
452 M_FONTDATA
->m_faceName
,
453 M_FONTDATA
->m_encoding
);
456 M_FONTDATA
->m_scaled_xfonts
.Append( int_scale
, (wxObject
*)font
);
459 // it's quite useless to make it a wxCHECK because we're going to crash
461 wxASSERT_MSG( font
, wxT("could not load any font?") );