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"
25 #include "wx/gdicmn.h"
26 #include "wx/tokenzr.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 class wxFontRefData
: public wxObjectRefData
39 wxFontRefData(int size
= wxDEFAULT
,
40 int family
= wxDEFAULT
,
41 int style
= wxDEFAULT
,
42 int weight
= wxDEFAULT
,
43 bool underlined
= FALSE
,
44 const wxString
& faceName
= wxEmptyString
,
45 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
46 wxFontRefData( const wxFontRefData
& data
);
47 virtual ~wxFontRefData();
50 // common part of all ctors
51 void Init(int pointSize
,
56 const wxString
& faceName
,
57 wxFontEncoding encoding
);
60 wxList m_scaled_xfonts
;
67 wxFontEncoding m_encoding
;
72 // ============================================================================
74 // ============================================================================
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 void wxFontRefData::Init(int pointSize
,
85 const wxString
& faceName
,
86 wxFontEncoding encoding
)
88 if (family
== wxDEFAULT
)
93 m_faceName
= faceName
;
95 if (style
== wxDEFAULT
)
100 if (weight
== wxDEFAULT
)
105 if (pointSize
== wxDEFAULT
)
108 m_pointSize
= pointSize
;
110 m_underlined
= underlined
;
111 m_encoding
= encoding
;
114 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
115 : m_scaled_xfonts(wxKEY_INTEGER
)
117 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
118 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
121 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
122 int weight
, bool underlined
, const wxString
& faceName
, wxFontEncoding encoding
)
123 : m_scaled_xfonts(wxKEY_INTEGER
)
125 Init(size
, family
, style
, weight
,
126 underlined
, faceName
, encoding
);
129 wxFontRefData::~wxFontRefData()
131 wxNode
*node
= m_scaled_xfonts
.First();
134 GdkFont
*font
= (GdkFont
*)node
->Data();
135 wxNode
*next
= node
->Next();
136 gdk_font_unref( font
);
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
150 wxTheFontList
->Append( this );
153 wxFont::wxFont( GdkFont
*WXUNUSED(font
), char *xFontName
)
160 m_refData
= new wxFontRefData();
164 wxString
fontname( xFontName
);
165 wxStringTokenizer
tn( fontname
, wxT("-") );
167 tn
.GetNextToken(); // foundry
169 M_FONTDATA
->m_faceName
= tn
.GetNextToken(); // courier
171 tmp
= tn
.GetNextToken().MakeUpper();
172 if (tmp
== wxT("BOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
174 tmp
= tn
.GetNextToken().MakeUpper();
175 if (tmp
== wxT("I")) M_FONTDATA
->m_style
= wxITALIC
;
176 if (tmp
== wxT("O")) M_FONTDATA
->m_style
= wxITALIC
;
178 tn
.GetNextToken(); // set width
179 tn
.GetNextToken(); // ?
180 tn
.GetNextToken(); // pixel size
182 tmp
= tn
.GetNextToken(); // pointsize
183 int num
= wxStrtol (tmp
.c_str(), (wxChar
**) NULL
, 10);
184 M_FONTDATA
->m_pointSize
= num
/ 10;
186 tn
.GetNextToken(); // x-res
187 tn
.GetNextToken(); // y-res
189 tmp
= tn
.GetNextToken().MakeUpper();
190 if (tmp
== wxT("M")) M_FONTDATA
->m_family
= wxMODERN
;
191 else if (M_FONTDATA
->m_faceName
== wxT("TIMES")) M_FONTDATA
->m_family
= wxROMAN
;
192 else if (M_FONTDATA
->m_faceName
== wxT("HELVETICA")) M_FONTDATA
->m_family
= wxSWISS
;
193 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDATYPEWRITER")) M_FONTDATA
->m_family
= wxTELETYPE
;
194 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDA")) M_FONTDATA
->m_family
= wxDECORATIVE
;
195 else if (M_FONTDATA
->m_faceName
== wxT("UTOPIA")) M_FONTDATA
->m_family
= wxSCRIPT
;
198 bool wxFont::Create( int pointSize
,
203 const wxString
& face
,
204 wxFontEncoding encoding
)
206 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
207 underlined
, face
, encoding
);
212 void wxFont::Unshare()
216 m_refData
= new wxFontRefData();
220 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
229 wxTheFontList
->DeleteObject( this );
232 // ----------------------------------------------------------------------------
234 // ----------------------------------------------------------------------------
236 int wxFont::GetPointSize() const
238 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
240 return M_FONTDATA
->m_pointSize
;
243 wxString
wxFont::GetFaceName() const
245 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
247 return M_FONTDATA
->m_faceName
;
250 int wxFont::GetFamily() const
252 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
254 return M_FONTDATA
->m_family
;
257 int wxFont::GetStyle() const
259 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
261 return M_FONTDATA
->m_style
;
264 int wxFont::GetWeight() const
266 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
268 return M_FONTDATA
->m_weight
;
271 bool wxFont::GetUnderlined() const
273 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
275 return M_FONTDATA
->m_underlined
;
279 wxFontEncoding
wxFont::GetEncoding() const
281 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
283 return M_FONTDATA
->m_encoding
;
286 // ----------------------------------------------------------------------------
287 // change font attributes
288 // ----------------------------------------------------------------------------
290 void wxFont::SetPointSize(int pointSize
)
294 M_FONTDATA
->m_pointSize
= pointSize
;
297 void wxFont::SetFamily(int family
)
301 M_FONTDATA
->m_family
= family
;
304 void wxFont::SetStyle(int style
)
308 M_FONTDATA
->m_style
= style
;
311 void wxFont::SetWeight(int weight
)
315 M_FONTDATA
->m_weight
= weight
;
318 void wxFont::SetFaceName(const wxString
& faceName
)
322 M_FONTDATA
->m_faceName
= faceName
;
325 void wxFont::SetUnderlined(bool underlined
)
329 M_FONTDATA
->m_underlined
= underlined
;
332 void wxFont::SetEncoding(wxFontEncoding encoding
)
336 M_FONTDATA
->m_encoding
= encoding
;
339 // ----------------------------------------------------------------------------
340 // get internal representation of font
341 // ----------------------------------------------------------------------------
343 GdkFont
*wxFont::GetInternalFont( float scale
) const
347 wxFAIL_MSG( wxT("invalid font") );
349 return (GdkFont
*) NULL
;
352 long int_scale
= long(scale
* 100.0 + 0.5); /* key for fontlist */
353 int point_scale
= (M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100;
354 GdkFont
*font
= (GdkFont
*) NULL
;
356 wxNode
*node
= M_FONTDATA
->m_scaled_xfonts
.Find(int_scale
);
359 font
= (GdkFont
*)node
->Data();
364 if ((int_scale
== 100) &&
365 (M_FONTDATA
->m_family
== wxSWISS
) &&
366 (M_FONTDATA
->m_style
== wxNORMAL
) &&
367 (M_FONTDATA
->m_pointSize
== 12) &&
368 (M_FONTDATA
->m_weight
== wxNORMAL
) &&
369 (M_FONTDATA
->m_underlined
== FALSE
))
371 font
= gdk_font_load( "-adobe-helvetica-medium-r-normal--*-120-*-*-*-*-*-*" );
376 font
= wxLoadQueryNearestFont( point_scale
,
377 M_FONTDATA
->m_family
,
379 M_FONTDATA
->m_weight
,
380 M_FONTDATA
->m_underlined
,
381 M_FONTDATA
->m_faceName
,
382 M_FONTDATA
->m_encoding
);
385 M_FONTDATA
->m_scaled_xfonts
.Append( int_scale
, (wxObject
*)font
);
390 wxLogError(wxT("could not load any font"));