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"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 class wxFontRefData
: public wxObjectRefData
43 wxFontRefData(int size
= wxDEFAULT
,
44 int family
= wxDEFAULT
,
45 int style
= wxDEFAULT
,
46 int weight
= wxDEFAULT
,
47 bool underlined
= FALSE
,
48 const wxString
& faceName
= wxEmptyString
,
49 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
50 wxFontRefData( const wxFontRefData
& data
);
51 virtual ~wxFontRefData();
54 // common part of all ctors
55 void Init(int pointSize
,
60 const wxString
& faceName
,
61 wxFontEncoding encoding
);
64 wxList m_scaled_xfonts
;
71 wxFontEncoding m_encoding
;
76 // ============================================================================
78 // ============================================================================
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 void wxFontRefData::Init(int pointSize
,
89 const wxString
& faceName
,
90 wxFontEncoding encoding
)
92 if (family
== wxDEFAULT
)
97 m_faceName
= faceName
;
99 if (style
== wxDEFAULT
)
104 if (weight
== wxDEFAULT
)
109 if (pointSize
== wxDEFAULT
)
112 m_pointSize
= pointSize
;
114 m_underlined
= underlined
;
115 m_encoding
= encoding
;
118 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
119 : m_scaled_xfonts(wxKEY_INTEGER
)
121 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
122 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
125 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
126 int weight
, bool underlined
, const wxString
& faceName
, wxFontEncoding encoding
)
127 : m_scaled_xfonts(wxKEY_INTEGER
)
129 Init(size
, family
, style
, weight
,
130 underlined
, faceName
, encoding
);
133 wxFontRefData::~wxFontRefData()
135 wxNode
*node
= m_scaled_xfonts
.First();
138 GdkFont
*font
= (GdkFont
*)node
->Data();
139 wxNode
*next
= node
->Next();
140 gdk_font_unref( font
);
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
154 wxTheFontList
->Append( this );
157 wxFont::wxFont( const wxString
& fontname
, const wxFontData
& fontdata
)
161 wxCHECK_RET( !!fontname
, _T("invalid font spec") );
163 m_refData
= new wxFontRefData();
167 wxStringTokenizer
tn( fontname
, wxT("-") );
169 tn
.GetNextToken(); // foundry
171 M_FONTDATA
->m_faceName
= tn
.GetNextToken(); // family
173 tmp
= tn
.GetNextToken().MakeUpper(); // weight
174 if (tmp
== wxT("BOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
175 if (tmp
== wxT("BLACK")) M_FONTDATA
->m_weight
= wxBOLD
;
176 if (tmp
== wxT("EXTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
177 if (tmp
== wxT("DEMIBOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
178 if (tmp
== wxT("ULTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
180 if (tmp
== wxT("LIGHT")) M_FONTDATA
->m_weight
= wxLIGHT
;
181 if (tmp
== wxT("THIN")) M_FONTDATA
->m_weight
= wxLIGHT
;
183 tmp
= tn
.GetNextToken().MakeUpper(); // slant
184 if (tmp
== wxT("I")) M_FONTDATA
->m_style
= wxITALIC
;
185 if (tmp
== wxT("O")) M_FONTDATA
->m_style
= wxITALIC
;
187 tn
.GetNextToken(); // set width
188 tn
.GetNextToken(); // add. style
189 tn
.GetNextToken(); // pixel size
191 tmp
= tn
.GetNextToken(); // pointsize
192 long num
= wxStrtol (tmp
.c_str(), (wxChar
**) NULL
, 10);
193 M_FONTDATA
->m_pointSize
= (int)(num
/ 10);
195 tn
.GetNextToken(); // x-res
196 tn
.GetNextToken(); // y-res
198 tmp
= tn
.GetNextToken().MakeUpper(); // spacing
201 M_FONTDATA
->m_family
= wxMODERN
;
202 else if (M_FONTDATA
->m_faceName
== wxT("TIMES"))
203 M_FONTDATA
->m_family
= wxROMAN
;
204 else if (M_FONTDATA
->m_faceName
== wxT("HELVETICA"))
205 M_FONTDATA
->m_family
= wxSWISS
;
206 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDATYPEWRITER"))
207 M_FONTDATA
->m_family
= wxTELETYPE
;
208 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDA"))
209 M_FONTDATA
->m_family
= wxDECORATIVE
;
210 else if (M_FONTDATA
->m_faceName
== wxT("UTOPIA"))
211 M_FONTDATA
->m_family
= wxSCRIPT
;
213 tn
.GetNextToken(); // avg width
215 // deal with font encoding
216 M_FONTDATA
->m_encoding
= fontdata
.GetEncoding();
217 if ( M_FONTDATA
->m_encoding
== wxFONTENCODING_SYSTEM
)
219 wxString registry
= tn
.GetNextToken().MakeUpper(),
220 encoding
= tn
.GetNextToken().MakeUpper();
222 if ( registry
== _T("ISO8859") )
225 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
227 M_FONTDATA
->m_encoding
=
228 (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
231 else if ( registry
== _T("MICROSOFT") )
234 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
236 M_FONTDATA
->m_encoding
=
237 (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
240 else if ( registry
== _T("KOI8") )
242 M_FONTDATA
->m_encoding
= wxFONTENCODING_KOI8
;
244 //else: unknown encoding - may be give a warning here?
248 bool wxFont::Create( int pointSize
,
253 const wxString
& face
,
254 wxFontEncoding encoding
)
256 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
257 underlined
, face
, encoding
);
262 void wxFont::Unshare()
266 m_refData
= new wxFontRefData();
270 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
279 wxTheFontList
->DeleteObject( this );
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
286 int wxFont::GetPointSize() const
288 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
290 return M_FONTDATA
->m_pointSize
;
293 wxString
wxFont::GetFaceName() const
295 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
297 return M_FONTDATA
->m_faceName
;
300 int wxFont::GetFamily() const
302 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
304 return M_FONTDATA
->m_family
;
307 int wxFont::GetStyle() const
309 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
311 return M_FONTDATA
->m_style
;
314 int wxFont::GetWeight() const
316 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
318 return M_FONTDATA
->m_weight
;
321 bool wxFont::GetUnderlined() const
323 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
325 return M_FONTDATA
->m_underlined
;
329 wxFontEncoding
wxFont::GetEncoding() const
331 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
333 return M_FONTDATA
->m_encoding
;
336 // ----------------------------------------------------------------------------
337 // change font attributes
338 // ----------------------------------------------------------------------------
340 void wxFont::SetPointSize(int pointSize
)
344 M_FONTDATA
->m_pointSize
= pointSize
;
347 void wxFont::SetFamily(int family
)
351 M_FONTDATA
->m_family
= family
;
354 void wxFont::SetStyle(int style
)
358 M_FONTDATA
->m_style
= style
;
361 void wxFont::SetWeight(int weight
)
365 M_FONTDATA
->m_weight
= weight
;
368 void wxFont::SetFaceName(const wxString
& faceName
)
372 M_FONTDATA
->m_faceName
= faceName
;
375 void wxFont::SetUnderlined(bool underlined
)
379 M_FONTDATA
->m_underlined
= underlined
;
382 void wxFont::SetEncoding(wxFontEncoding encoding
)
386 M_FONTDATA
->m_encoding
= encoding
;
389 // ----------------------------------------------------------------------------
390 // get internal representation of font
391 // ----------------------------------------------------------------------------
393 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
395 static GdkFont
*GtkGetDefaultGuiFont()
397 if (!g_systemDefaultGuiFont
)
399 GtkWidget
*widget
= gtk_button_new();
400 GtkStyle
*def
= gtk_rc_get_style( widget
);
403 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
407 def
= gtk_widget_get_default_style();
409 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
411 gtk_widget_destroy( widget
);
413 return g_systemDefaultGuiFont
;
416 GdkFont
*wxFont::GetInternalFont( float scale
) const
420 wxFAIL_MSG( wxT("invalid font") );
422 return (GdkFont
*) NULL
;
425 long int_scale
= long(scale
* 100.0 + 0.5); /* key for fontlist */
426 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
427 GdkFont
*font
= (GdkFont
*) NULL
;
429 wxNode
*node
= M_FONTDATA
->m_scaled_xfonts
.Find(int_scale
);
432 font
= (GdkFont
*)node
->Data();
436 if (*this == wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
))
438 font
= GtkGetDefaultGuiFont();
442 font
= wxLoadQueryNearestFont( point_scale
,
443 M_FONTDATA
->m_family
,
445 M_FONTDATA
->m_weight
,
446 M_FONTDATA
->m_underlined
,
447 M_FONTDATA
->m_faceName
,
448 M_FONTDATA
->m_encoding
);
451 M_FONTDATA
->m_scaled_xfonts
.Append( int_scale
, (wxObject
*)font
);
454 // it's quite useless to make it a wxCHECK because we're going to crash
456 wxASSERT_MSG( font
, wxT("could not load any font?") );