]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/font.cpp
Replaced the old wxGetResource implementation with one based
[wxWidgets.git] / src / gtk1 / font.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: font.cpp
3// Purpose:
4// Author: Robert Roebling
a81258be 5// Id: $Id$
c801d85f 6// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
0c5d3e1c 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
0c5d3e1c
VZ
10// ============================================================================
11// declarations
12// ============================================================================
13
14// ----------------------------------------------------------------------------
15// headers
16// ----------------------------------------------------------------------------
17
c801d85f 18#ifdef __GNUG__
0c5d3e1c 19 #pragma implementation "font.h"
c801d85f
KB
20#endif
21
22#include "wx/font.h"
7beba2fc
VZ
23#include "wx/fontutil.h"
24#include "wx/cmndata.h"
c801d85f 25#include "wx/utils.h"
5705323e 26#include "wx/log.h"
4cb122de 27#include "wx/gdicmn.h"
8636aed8 28#include "wx/tokenzr.h"
0c5d3e1c 29
c801d85f
KB
30#include <strings.h>
31
071a2d78 32#include <gdk/gdk.h>
83624f79 33
0c5d3e1c
VZ
34// ----------------------------------------------------------------------------
35// wxFontRefData
36// ----------------------------------------------------------------------------
37
38class wxFontRefData : public wxObjectRefData
c801d85f 39{
8bbe427f 40public:
0c5d3e1c
VZ
41 wxFontRefData(int size = wxDEFAULT,
42 int family = wxDEFAULT,
43 int style = wxDEFAULT,
44 int weight = wxDEFAULT,
45 bool underlined = FALSE,
46 const wxString& faceName = wxEmptyString,
f35c2659 47 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
358fc25c 48 wxFontRefData( const wxFontRefData& data );
0c5d3e1c
VZ
49 virtual ~wxFontRefData();
50
51protected:
52 // common part of all ctors
53 void Init(int pointSize,
54 int family,
55 int style,
56 int weight,
57 bool underlined,
58 const wxString& faceName,
59 wxFontEncoding encoding);
60
61private:
f35c2659
RR
62 wxList m_scaled_xfonts;
63 int m_pointSize;
64 int m_family,
65 m_style,
66 m_weight;
67 bool m_underlined;
68 wxString m_faceName;
69 wxFontEncoding m_encoding;
8bbe427f 70
c801d85f
KB
71 friend wxFont;
72};
73
0c5d3e1c
VZ
74// ============================================================================
75// implementation
76// ============================================================================
77
78// ----------------------------------------------------------------------------
79// wxFontRefData
80// ----------------------------------------------------------------------------
81
82void wxFontRefData::Init(int pointSize,
83 int family,
84 int style,
85 int weight,
86 bool underlined,
87 const wxString& faceName,
88 wxFontEncoding encoding)
8bbe427f 89{
0c5d3e1c
VZ
90 if (family == wxDEFAULT)
91 m_family = wxSWISS;
92 else
93 m_family = family;
94
95 m_faceName = faceName;
96
97 if (style == wxDEFAULT)
98 m_style = wxNORMAL;
99 else
100 m_style = style;
101
102 if (weight == wxDEFAULT)
103 m_weight = wxNORMAL;
104 else
105 m_weight = weight;
106
107 if (pointSize == wxDEFAULT)
108 m_pointSize = 12;
109 else
110 m_pointSize = pointSize;
111
112 m_underlined = underlined;
113 m_encoding = encoding;
8bbe427f
VZ
114}
115
0c5d3e1c 116wxFontRefData::wxFontRefData( const wxFontRefData& data )
f35c2659 117 : m_scaled_xfonts(wxKEY_INTEGER)
358fc25c 118{
0c5d3e1c 119 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
f35c2659
RR
120 data.m_underlined, data.m_faceName, data.m_encoding);
121}
0c5d3e1c 122
f35c2659
RR
123wxFontRefData::wxFontRefData(int size, int family, int style,
124 int weight, bool underlined, const wxString& faceName, wxFontEncoding encoding )
125 : m_scaled_xfonts(wxKEY_INTEGER)
126{
7beba2fc 127 Init(size, family, style, weight,
f35c2659 128 underlined, faceName, encoding);
358fc25c
RR
129}
130
8bbe427f
VZ
131wxFontRefData::~wxFontRefData()
132{
133 wxNode *node = m_scaled_xfonts.First();
134 while (node)
135 {
136 GdkFont *font = (GdkFont*)node->Data();
137 wxNode *next = node->Next();
138 gdk_font_unref( font );
139 node = next;
140 }
0c5d3e1c 141}
c801d85f 142
0c5d3e1c
VZ
143// ----------------------------------------------------------------------------
144// wxFont
145// ----------------------------------------------------------------------------
c801d85f
KB
146
147IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
148
0c5d3e1c 149void wxFont::Init()
c801d85f 150{
0c5d3e1c
VZ
151 if (wxTheFontList)
152 wxTheFontList->Append( this );
ff7b1510 153}
c801d85f 154
7beba2fc 155wxFont::wxFont( const wxString& fontname, const wxFontData& fontdata )
c801d85f 156{
0c5d3e1c 157 Init();
8bbe427f 158
7beba2fc
VZ
159 wxCHECK_RET( !!fontname, _T("invalid font spec") );
160
8bbe427f
VZ
161 m_refData = new wxFontRefData();
162
8636aed8 163 wxString tmp;
284b4c88 164
223d09f6 165 wxStringTokenizer tn( fontname, wxT("-") );
284b4c88 166
8636aed8 167 tn.GetNextToken(); // foundry
284b4c88 168
36f210c8 169 M_FONTDATA->m_faceName = tn.GetNextToken(); // family
8636aed8 170
36f210c8 171 tmp = tn.GetNextToken().MakeUpper(); // weight
223d09f6 172 if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxBOLD;
30760ce7
RR
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;
177
178 if (tmp == wxT("LIGHT")) M_FONTDATA->m_weight = wxLIGHT;
179 if (tmp == wxT("THIN")) M_FONTDATA->m_weight = wxLIGHT;
7beba2fc 180
36f210c8 181 tmp = tn.GetNextToken().MakeUpper(); // slant
223d09f6
KB
182 if (tmp == wxT("I")) M_FONTDATA->m_style = wxITALIC;
183 if (tmp == wxT("O")) M_FONTDATA->m_style = wxITALIC;
284b4c88 184
8636aed8 185 tn.GetNextToken(); // set width
36f210c8 186 tn.GetNextToken(); // add. style
8636aed8 187 tn.GetNextToken(); // pixel size
284b4c88 188
8636aed8 189 tmp = tn.GetNextToken(); // pointsize
b02da6b1 190 long num = wxStrtol (tmp.c_str(), (wxChar **) NULL, 10);
7941ba11 191 M_FONTDATA->m_pointSize = (int)(num / 10);
284b4c88 192
8636aed8
RR
193 tn.GetNextToken(); // x-res
194 tn.GetNextToken(); // y-res
284b4c88 195
36f210c8
VZ
196 tmp = tn.GetNextToken().MakeUpper(); // spacing
197
198 if (tmp == wxT("M"))
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;
210
211 tn.GetNextToken(); // avg width
212
213 // deal with font encoding
7beba2fc
VZ
214 M_FONTDATA->m_encoding = fontdata.GetEncoding();
215 if ( M_FONTDATA->m_encoding == wxFONTENCODING_SYSTEM )
36f210c8 216 {
7beba2fc
VZ
217 wxString registry = tn.GetNextToken().MakeUpper(),
218 encoding = tn.GetNextToken().MakeUpper();
219
220 if ( registry == _T("ISO8859") )
36f210c8 221 {
7beba2fc
VZ
222 int cp;
223 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
224 {
225 M_FONTDATA->m_encoding =
226 (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
227 }
36f210c8 228 }
7beba2fc 229 else if ( registry == _T("MICROSOFT") )
36f210c8 230 {
7beba2fc
VZ
231 int cp;
232 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
233 {
234 M_FONTDATA->m_encoding =
235 (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
236 }
36f210c8 237 }
7beba2fc
VZ
238 else if ( registry == _T("KOI8") )
239 {
240 M_FONTDATA->m_encoding = wxFONTENCODING_KOI8;
241 }
242 //else: unknown encoding - may be give a warning here?
36f210c8 243 }
ff7b1510 244}
c801d85f 245
0c5d3e1c
VZ
246bool wxFont::Create( int pointSize,
247 int family,
248 int style,
249 int weight,
250 bool underlined,
251 const wxString& face,
252 wxFontEncoding encoding )
8bbe427f 253{
0c5d3e1c
VZ
254 m_refData = new wxFontRefData(pointSize, family, style, weight,
255 underlined, face, encoding);
8bbe427f 256
0c5d3e1c 257 return TRUE;
ff7b1510 258}
c801d85f 259
0c5d3e1c 260void wxFont::Unshare()
8bbe427f 261{
0c5d3e1c
VZ
262 if (!m_refData)
263 {
264 m_refData = new wxFontRefData();
265 }
266 else
267 {
268 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
269 UnRef();
270 m_refData = ref;
271 }
ff7b1510 272}
c801d85f 273
8bbe427f 274wxFont::~wxFont()
c801d85f 275{
0c5d3e1c
VZ
276 if (wxTheFontList)
277 wxTheFontList->DeleteObject( this );
ff7b1510 278}
c801d85f 279
0c5d3e1c
VZ
280// ----------------------------------------------------------------------------
281// accessors
282// ----------------------------------------------------------------------------
c801d85f 283
8bbe427f 284int wxFont::GetPointSize() const
c801d85f 285{
223d09f6 286 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
287
288 return M_FONTDATA->m_pointSize;
ff7b1510 289}
c801d85f 290
8bbe427f 291wxString wxFont::GetFaceName() const
c801d85f 292{
223d09f6 293 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
8bbe427f 294
36b3b54a 295 return M_FONTDATA->m_faceName;
ff7b1510 296}
c801d85f 297
8bbe427f 298int wxFont::GetFamily() const
c801d85f 299{
223d09f6 300 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
301
302 return M_FONTDATA->m_family;
ff7b1510 303}
c801d85f 304
8bbe427f 305int wxFont::GetStyle() const
c801d85f 306{
223d09f6 307 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
d84eb083 308
8bbe427f 309 return M_FONTDATA->m_style;
ff7b1510 310}
c801d85f 311
8bbe427f 312int wxFont::GetWeight() const
c801d85f 313{
223d09f6 314 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
315
316 return M_FONTDATA->m_weight;
317}
318
8bbe427f
VZ
319bool wxFont::GetUnderlined() const
320{
223d09f6 321 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
8bbe427f
VZ
322
323 return M_FONTDATA->m_underlined;
ff7b1510 324}
c801d85f 325
0c5d3e1c
VZ
326
327wxFontEncoding wxFont::GetEncoding() const
358fc25c 328{
223d09f6 329 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
0c5d3e1c
VZ
330
331 return M_FONTDATA->m_encoding;
358fc25c
RR
332}
333
0c5d3e1c
VZ
334// ----------------------------------------------------------------------------
335// change font attributes
336// ----------------------------------------------------------------------------
337
358fc25c
RR
338void wxFont::SetPointSize(int pointSize)
339{
340 Unshare();
341
342 M_FONTDATA->m_pointSize = pointSize;
343}
344
345void wxFont::SetFamily(int family)
346{
347 Unshare();
348
349 M_FONTDATA->m_family = family;
350}
351
352void wxFont::SetStyle(int style)
353{
354 Unshare();
355
356 M_FONTDATA->m_style = style;
357}
358
359void wxFont::SetWeight(int weight)
360{
361 Unshare();
362
363 M_FONTDATA->m_weight = weight;
364}
365
366void wxFont::SetFaceName(const wxString& faceName)
367{
368 Unshare();
369
370 M_FONTDATA->m_faceName = faceName;
371}
372
373void wxFont::SetUnderlined(bool underlined)
374{
375 Unshare();
376
377 M_FONTDATA->m_underlined = underlined;
378}
379
0c5d3e1c
VZ
380void wxFont::SetEncoding(wxFontEncoding encoding)
381{
382 Unshare();
c801d85f 383
0c5d3e1c
VZ
384 M_FONTDATA->m_encoding = encoding;
385}
386
387// ----------------------------------------------------------------------------
388// get internal representation of font
389// ----------------------------------------------------------------------------
c801d85f 390
36b3b54a 391GdkFont *wxFont::GetInternalFont( float scale ) const
c801d85f 392{
8bbe427f
VZ
393 if (!Ok())
394 {
223d09f6 395 wxFAIL_MSG( wxT("invalid font") );
0c5d3e1c 396
8bbe427f
VZ
397 return (GdkFont*) NULL;
398 }
399
36b3b54a 400 long int_scale = long(scale * 100.0 + 0.5); /* key for fontlist */
b02da6b1 401 int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100);
8bbe427f
VZ
402 GdkFont *font = (GdkFont *) NULL;
403
404 wxNode *node = M_FONTDATA->m_scaled_xfonts.Find(int_scale);
405 if (node)
406 {
407 font = (GdkFont*)node->Data();
408 }
409 else
410 {
0c5d3e1c 411#if 0
8bbe427f
VZ
412 if ((int_scale == 100) &&
413 (M_FONTDATA->m_family == wxSWISS) &&
414 (M_FONTDATA->m_style == wxNORMAL) &&
415 (M_FONTDATA->m_pointSize == 12) &&
416 (M_FONTDATA->m_weight == wxNORMAL) &&
417 (M_FONTDATA->m_underlined == FALSE))
418 {
419 font = gdk_font_load( "-adobe-helvetica-medium-r-normal--*-120-*-*-*-*-*-*" );
420 }
421 else
0c5d3e1c 422#endif // 0
8bbe427f 423 {
0c5d3e1c
VZ
424 font = wxLoadQueryNearestFont( point_scale,
425 M_FONTDATA->m_family,
426 M_FONTDATA->m_style,
427 M_FONTDATA->m_weight,
428 M_FONTDATA->m_underlined,
429 M_FONTDATA->m_faceName,
430 M_FONTDATA->m_encoding );
8bbe427f 431 }
0c5d3e1c 432
8bbe427f
VZ
433 M_FONTDATA->m_scaled_xfonts.Append( int_scale, (wxObject*)font );
434 }
284b4c88 435
7beba2fc
VZ
436 // it's quite useless to make it a wxCHECK because we're going to crash
437 // anyhow...
438 wxASSERT_MSG( font, wxT("could not load any font?") );
284b4c88 439
8bbe427f 440 return font;
ff7b1510 441}
c801d85f 442