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
;
173 if (tmp
== wxT("BLACK")) M_FONTDATA
->m_weight
= wxBOLD
;
174 if (tmp
== wxT("EXTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
175 if (tmp
== wxT("DEMIBOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
176 if (tmp
== wxT("ULTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
178 if (tmp
== wxT("LIGHT")) M_FONTDATA
->m_weight
= wxLIGHT
;
179 if (tmp
== wxT("THIN")) M_FONTDATA
->m_weight
= wxLIGHT
;
181 tmp
= tn
.GetNextToken().MakeUpper();
182 if (tmp
== wxT("I")) M_FONTDATA
->m_style
= wxITALIC
;
183 if (tmp
== wxT("O")) M_FONTDATA
->m_style
= wxITALIC
;
185 tn
.GetNextToken(); // set width
186 tn
.GetNextToken(); // ?
187 tn
.GetNextToken(); // pixel size
189 tmp
= tn
.GetNextToken(); // pointsize
190 int num
= wxStrtol (tmp
.c_str(), (wxChar
**) NULL
, 10);
191 M_FONTDATA
->m_pointSize
= num
/ 10;
193 tn
.GetNextToken(); // x-res
194 tn
.GetNextToken(); // y-res
196 tmp
= tn
.GetNextToken().MakeUpper();
197 if (tmp
== wxT("M")) M_FONTDATA
->m_family
= wxMODERN
;
198 else if (M_FONTDATA
->m_faceName
== wxT("TIMES")) M_FONTDATA
->m_family
= wxROMAN
;
199 else if (M_FONTDATA
->m_faceName
== wxT("HELVETICA")) M_FONTDATA
->m_family
= wxSWISS
;
200 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDATYPEWRITER")) M_FONTDATA
->m_family
= wxTELETYPE
;
201 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDA")) M_FONTDATA
->m_family
= wxDECORATIVE
;
202 else if (M_FONTDATA
->m_faceName
== wxT("UTOPIA")) M_FONTDATA
->m_family
= wxSCRIPT
;
205 bool wxFont::Create( int pointSize
,
210 const wxString
& face
,
211 wxFontEncoding encoding
)
213 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
214 underlined
, face
, encoding
);
219 void wxFont::Unshare()
223 m_refData
= new wxFontRefData();
227 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
236 wxTheFontList
->DeleteObject( this );
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 int wxFont::GetPointSize() const
245 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
247 return M_FONTDATA
->m_pointSize
;
250 wxString
wxFont::GetFaceName() const
252 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
254 return M_FONTDATA
->m_faceName
;
257 int wxFont::GetFamily() const
259 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
261 return M_FONTDATA
->m_family
;
264 int wxFont::GetStyle() const
266 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
268 return M_FONTDATA
->m_style
;
271 int wxFont::GetWeight() const
273 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
275 return M_FONTDATA
->m_weight
;
278 bool wxFont::GetUnderlined() const
280 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
282 return M_FONTDATA
->m_underlined
;
286 wxFontEncoding
wxFont::GetEncoding() const
288 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
290 return M_FONTDATA
->m_encoding
;
293 // ----------------------------------------------------------------------------
294 // change font attributes
295 // ----------------------------------------------------------------------------
297 void wxFont::SetPointSize(int pointSize
)
301 M_FONTDATA
->m_pointSize
= pointSize
;
304 void wxFont::SetFamily(int family
)
308 M_FONTDATA
->m_family
= family
;
311 void wxFont::SetStyle(int style
)
315 M_FONTDATA
->m_style
= style
;
318 void wxFont::SetWeight(int weight
)
322 M_FONTDATA
->m_weight
= weight
;
325 void wxFont::SetFaceName(const wxString
& faceName
)
329 M_FONTDATA
->m_faceName
= faceName
;
332 void wxFont::SetUnderlined(bool underlined
)
336 M_FONTDATA
->m_underlined
= underlined
;
339 void wxFont::SetEncoding(wxFontEncoding encoding
)
343 M_FONTDATA
->m_encoding
= encoding
;
346 // ----------------------------------------------------------------------------
347 // get internal representation of font
348 // ----------------------------------------------------------------------------
350 GdkFont
*wxFont::GetInternalFont( float scale
) const
354 wxFAIL_MSG( wxT("invalid font") );
356 return (GdkFont
*) NULL
;
359 long int_scale
= long(scale
* 100.0 + 0.5); /* key for fontlist */
360 int point_scale
= (M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100;
361 GdkFont
*font
= (GdkFont
*) NULL
;
363 wxNode
*node
= M_FONTDATA
->m_scaled_xfonts
.Find(int_scale
);
366 font
= (GdkFont
*)node
->Data();
371 if ((int_scale
== 100) &&
372 (M_FONTDATA
->m_family
== wxSWISS
) &&
373 (M_FONTDATA
->m_style
== wxNORMAL
) &&
374 (M_FONTDATA
->m_pointSize
== 12) &&
375 (M_FONTDATA
->m_weight
== wxNORMAL
) &&
376 (M_FONTDATA
->m_underlined
== FALSE
))
378 font
= gdk_font_load( "-adobe-helvetica-medium-r-normal--*-120-*-*-*-*-*-*" );
383 font
= wxLoadQueryNearestFont( point_scale
,
384 M_FONTDATA
->m_family
,
386 M_FONTDATA
->m_weight
,
387 M_FONTDATA
->m_underlined
,
388 M_FONTDATA
->m_faceName
,
389 M_FONTDATA
->m_encoding
);
392 M_FONTDATA
->m_scaled_xfonts
.Append( int_scale
, (wxObject
*)font
);
397 wxLogError(wxT("could not load any font"));