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(); // family
171 tmp
= tn
.GetNextToken().MakeUpper(); // weight
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(); // slant
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(); // add. style
187 tn
.GetNextToken(); // pixel size
189 tmp
= tn
.GetNextToken(); // pointsize
190 long 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(); // spacing
199 M_FONTDATA
->m_family
= wxMODERN
;
200 else if (M_FONTDATA
->m_faceName
== wxT("TIMES"))
201 M_FONTDATA
->m_family
= wxROMAN
;
202 else if (M_FONTDATA
->m_faceName
== wxT("HELVETICA"))
203 M_FONTDATA
->m_family
= wxSWISS
;
204 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDATYPEWRITER"))
205 M_FONTDATA
->m_family
= wxTELETYPE
;
206 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDA"))
207 M_FONTDATA
->m_family
= wxDECORATIVE
;
208 else if (M_FONTDATA
->m_faceName
== wxT("UTOPIA"))
209 M_FONTDATA
->m_family
= wxSCRIPT
;
211 tn
.GetNextToken(); // avg width
213 // deal with font encoding
214 wxString registry
= tn
.GetNextToken().MakeUpper(),
215 encoding
= tn
.GetNextToken().MakeUpper();
217 if ( registry
== _T("ISO8859") )
220 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
222 M_FONTDATA
->m_encoding
=
223 (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
226 else if ( registry
== _T("MICROSOFT") )
229 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
231 M_FONTDATA
->m_encoding
=
232 (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
235 else if ( registry
== _T("KOI8") )
237 M_FONTDATA
->m_encoding
= wxFONTENCODING_KOI8
;
239 //else: unknown encoding - may be give a warning here?
242 bool wxFont::Create( int pointSize
,
247 const wxString
& face
,
248 wxFontEncoding encoding
)
250 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
251 underlined
, face
, encoding
);
256 void wxFont::Unshare()
260 m_refData
= new wxFontRefData();
264 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
273 wxTheFontList
->DeleteObject( this );
276 // ----------------------------------------------------------------------------
278 // ----------------------------------------------------------------------------
280 int wxFont::GetPointSize() const
282 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
284 return M_FONTDATA
->m_pointSize
;
287 wxString
wxFont::GetFaceName() const
289 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
291 return M_FONTDATA
->m_faceName
;
294 int wxFont::GetFamily() const
296 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
298 return M_FONTDATA
->m_family
;
301 int wxFont::GetStyle() const
303 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
305 return M_FONTDATA
->m_style
;
308 int wxFont::GetWeight() const
310 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
312 return M_FONTDATA
->m_weight
;
315 bool wxFont::GetUnderlined() const
317 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
319 return M_FONTDATA
->m_underlined
;
323 wxFontEncoding
wxFont::GetEncoding() const
325 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
327 return M_FONTDATA
->m_encoding
;
330 // ----------------------------------------------------------------------------
331 // change font attributes
332 // ----------------------------------------------------------------------------
334 void wxFont::SetPointSize(int pointSize
)
338 M_FONTDATA
->m_pointSize
= pointSize
;
341 void wxFont::SetFamily(int family
)
345 M_FONTDATA
->m_family
= family
;
348 void wxFont::SetStyle(int style
)
352 M_FONTDATA
->m_style
= style
;
355 void wxFont::SetWeight(int weight
)
359 M_FONTDATA
->m_weight
= weight
;
362 void wxFont::SetFaceName(const wxString
& faceName
)
366 M_FONTDATA
->m_faceName
= faceName
;
369 void wxFont::SetUnderlined(bool underlined
)
373 M_FONTDATA
->m_underlined
= underlined
;
376 void wxFont::SetEncoding(wxFontEncoding encoding
)
380 M_FONTDATA
->m_encoding
= encoding
;
383 // ----------------------------------------------------------------------------
384 // get internal representation of font
385 // ----------------------------------------------------------------------------
387 GdkFont
*wxFont::GetInternalFont( float scale
) const
391 wxFAIL_MSG( wxT("invalid font") );
393 return (GdkFont
*) NULL
;
396 long int_scale
= long(scale
* 100.0 + 0.5); /* key for fontlist */
397 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
398 GdkFont
*font
= (GdkFont
*) NULL
;
400 wxNode
*node
= M_FONTDATA
->m_scaled_xfonts
.Find(int_scale
);
403 font
= (GdkFont
*)node
->Data();
408 if ((int_scale
== 100) &&
409 (M_FONTDATA
->m_family
== wxSWISS
) &&
410 (M_FONTDATA
->m_style
== wxNORMAL
) &&
411 (M_FONTDATA
->m_pointSize
== 12) &&
412 (M_FONTDATA
->m_weight
== wxNORMAL
) &&
413 (M_FONTDATA
->m_underlined
== FALSE
))
415 font
= gdk_font_load( "-adobe-helvetica-medium-r-normal--*-120-*-*-*-*-*-*" );
420 font
= wxLoadQueryNearestFont( point_scale
,
421 M_FONTDATA
->m_family
,
423 M_FONTDATA
->m_weight
,
424 M_FONTDATA
->m_underlined
,
425 M_FONTDATA
->m_faceName
,
426 M_FONTDATA
->m_encoding
);
429 M_FONTDATA
->m_scaled_xfonts
.Append( int_scale
, (wxObject
*)font
);
434 wxLogError(wxT("could not load any font"));