]>
Commit | Line | Data |
---|---|---|
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" |
c7985368 | 29 | #include "wx/settings.h" |
0c5d3e1c | 30 | |
c801d85f KB |
31 | #include <strings.h> |
32 | ||
071a2d78 | 33 | #include <gdk/gdk.h> |
c7985368 | 34 | #include <gtk/gtk.h> |
83624f79 | 35 | |
0c5d3e1c VZ |
36 | // ---------------------------------------------------------------------------- |
37 | // wxFontRefData | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | class wxFontRefData : public wxObjectRefData | |
c801d85f | 41 | { |
8bbe427f | 42 | public: |
0c5d3e1c VZ |
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, | |
f35c2659 | 49 | wxFontEncoding encoding = wxFONTENCODING_DEFAULT); |
358fc25c | 50 | wxFontRefData( const wxFontRefData& data ); |
0c5d3e1c VZ |
51 | virtual ~wxFontRefData(); |
52 | ||
53 | protected: | |
54 | // common part of all ctors | |
55 | void Init(int pointSize, | |
56 | int family, | |
57 | int style, | |
58 | int weight, | |
59 | bool underlined, | |
60 | const wxString& faceName, | |
61 | wxFontEncoding encoding); | |
62 | ||
63 | private: | |
f35c2659 RR |
64 | wxList m_scaled_xfonts; |
65 | int m_pointSize; | |
66 | int m_family, | |
67 | m_style, | |
68 | m_weight; | |
69 | bool m_underlined; | |
70 | wxString m_faceName; | |
71 | wxFontEncoding m_encoding; | |
8bbe427f | 72 | |
c801d85f KB |
73 | friend wxFont; |
74 | }; | |
75 | ||
0c5d3e1c VZ |
76 | // ============================================================================ |
77 | // implementation | |
78 | // ============================================================================ | |
79 | ||
80 | // ---------------------------------------------------------------------------- | |
81 | // wxFontRefData | |
82 | // ---------------------------------------------------------------------------- | |
83 | ||
84 | void wxFontRefData::Init(int pointSize, | |
85 | int family, | |
86 | int style, | |
87 | int weight, | |
88 | bool underlined, | |
89 | const wxString& faceName, | |
90 | wxFontEncoding encoding) | |
8bbe427f | 91 | { |
0c5d3e1c VZ |
92 | if (family == wxDEFAULT) |
93 | m_family = wxSWISS; | |
94 | else | |
95 | m_family = family; | |
96 | ||
97 | m_faceName = faceName; | |
98 | ||
99 | if (style == wxDEFAULT) | |
100 | m_style = wxNORMAL; | |
101 | else | |
102 | m_style = style; | |
103 | ||
104 | if (weight == wxDEFAULT) | |
105 | m_weight = wxNORMAL; | |
106 | else | |
107 | m_weight = weight; | |
108 | ||
109 | if (pointSize == wxDEFAULT) | |
110 | m_pointSize = 12; | |
111 | else | |
112 | m_pointSize = pointSize; | |
113 | ||
114 | m_underlined = underlined; | |
115 | m_encoding = encoding; | |
8bbe427f VZ |
116 | } |
117 | ||
0c5d3e1c | 118 | wxFontRefData::wxFontRefData( const wxFontRefData& data ) |
f35c2659 | 119 | : m_scaled_xfonts(wxKEY_INTEGER) |
358fc25c | 120 | { |
0c5d3e1c | 121 | Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight, |
f35c2659 RR |
122 | data.m_underlined, data.m_faceName, data.m_encoding); |
123 | } | |
0c5d3e1c | 124 | |
f35c2659 RR |
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) | |
128 | { | |
7beba2fc | 129 | Init(size, family, style, weight, |
f35c2659 | 130 | underlined, faceName, encoding); |
358fc25c RR |
131 | } |
132 | ||
8bbe427f VZ |
133 | wxFontRefData::~wxFontRefData() |
134 | { | |
135 | wxNode *node = m_scaled_xfonts.First(); | |
136 | while (node) | |
137 | { | |
138 | GdkFont *font = (GdkFont*)node->Data(); | |
139 | wxNode *next = node->Next(); | |
140 | gdk_font_unref( font ); | |
141 | node = next; | |
142 | } | |
0c5d3e1c | 143 | } |
c801d85f | 144 | |
0c5d3e1c VZ |
145 | // ---------------------------------------------------------------------------- |
146 | // wxFont | |
147 | // ---------------------------------------------------------------------------- | |
c801d85f KB |
148 | |
149 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) | |
150 | ||
0c5d3e1c | 151 | void wxFont::Init() |
c801d85f | 152 | { |
0c5d3e1c VZ |
153 | if (wxTheFontList) |
154 | wxTheFontList->Append( this ); | |
ff7b1510 | 155 | } |
c801d85f | 156 | |
7beba2fc | 157 | wxFont::wxFont( const wxString& fontname, const wxFontData& fontdata ) |
c801d85f | 158 | { |
0c5d3e1c | 159 | Init(); |
8bbe427f | 160 | |
7beba2fc VZ |
161 | wxCHECK_RET( !!fontname, _T("invalid font spec") ); |
162 | ||
8bbe427f VZ |
163 | m_refData = new wxFontRefData(); |
164 | ||
8636aed8 | 165 | wxString tmp; |
284b4c88 | 166 | |
223d09f6 | 167 | wxStringTokenizer tn( fontname, wxT("-") ); |
284b4c88 | 168 | |
a7987cc1 | 169 | tn.GetNextToken(); // skip initial empty token |
8636aed8 | 170 | tn.GetNextToken(); // foundry |
284b4c88 | 171 | |
36f210c8 | 172 | M_FONTDATA->m_faceName = tn.GetNextToken(); // family |
8636aed8 | 173 | |
36f210c8 | 174 | tmp = tn.GetNextToken().MakeUpper(); // weight |
223d09f6 | 175 | if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxBOLD; |
30760ce7 RR |
176 | if (tmp == wxT("BLACK")) M_FONTDATA->m_weight = wxBOLD; |
177 | if (tmp == wxT("EXTRABOLD")) M_FONTDATA->m_weight = wxBOLD; | |
178 | if (tmp == wxT("DEMIBOLD")) M_FONTDATA->m_weight = wxBOLD; | |
179 | if (tmp == wxT("ULTRABOLD")) M_FONTDATA->m_weight = wxBOLD; | |
180 | ||
181 | if (tmp == wxT("LIGHT")) M_FONTDATA->m_weight = wxLIGHT; | |
182 | if (tmp == wxT("THIN")) M_FONTDATA->m_weight = wxLIGHT; | |
7beba2fc | 183 | |
36f210c8 | 184 | tmp = tn.GetNextToken().MakeUpper(); // slant |
223d09f6 KB |
185 | if (tmp == wxT("I")) M_FONTDATA->m_style = wxITALIC; |
186 | if (tmp == wxT("O")) M_FONTDATA->m_style = wxITALIC; | |
284b4c88 | 187 | |
8636aed8 | 188 | tn.GetNextToken(); // set width |
36f210c8 | 189 | tn.GetNextToken(); // add. style |
8636aed8 | 190 | tn.GetNextToken(); // pixel size |
284b4c88 | 191 | |
8636aed8 | 192 | tmp = tn.GetNextToken(); // pointsize |
b02da6b1 | 193 | long num = wxStrtol (tmp.c_str(), (wxChar **) NULL, 10); |
7941ba11 | 194 | M_FONTDATA->m_pointSize = (int)(num / 10); |
284b4c88 | 195 | |
8636aed8 RR |
196 | tn.GetNextToken(); // x-res |
197 | tn.GetNextToken(); // y-res | |
284b4c88 | 198 | |
36f210c8 VZ |
199 | tmp = tn.GetNextToken().MakeUpper(); // spacing |
200 | ||
201 | if (tmp == wxT("M")) | |
202 | M_FONTDATA->m_family = wxMODERN; | |
203 | else if (M_FONTDATA->m_faceName == wxT("TIMES")) | |
204 | M_FONTDATA->m_family = wxROMAN; | |
205 | else if (M_FONTDATA->m_faceName == wxT("HELVETICA")) | |
206 | M_FONTDATA->m_family = wxSWISS; | |
207 | else if (M_FONTDATA->m_faceName == wxT("LUCIDATYPEWRITER")) | |
208 | M_FONTDATA->m_family = wxTELETYPE; | |
209 | else if (M_FONTDATA->m_faceName == wxT("LUCIDA")) | |
210 | M_FONTDATA->m_family = wxDECORATIVE; | |
211 | else if (M_FONTDATA->m_faceName == wxT("UTOPIA")) | |
212 | M_FONTDATA->m_family = wxSCRIPT; | |
213 | ||
214 | tn.GetNextToken(); // avg width | |
215 | ||
216 | // deal with font encoding | |
7beba2fc VZ |
217 | M_FONTDATA->m_encoding = fontdata.GetEncoding(); |
218 | if ( M_FONTDATA->m_encoding == wxFONTENCODING_SYSTEM ) | |
36f210c8 | 219 | { |
7beba2fc VZ |
220 | wxString registry = tn.GetNextToken().MakeUpper(), |
221 | encoding = tn.GetNextToken().MakeUpper(); | |
222 | ||
223 | if ( registry == _T("ISO8859") ) | |
36f210c8 | 224 | { |
7beba2fc VZ |
225 | int cp; |
226 | if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 ) | |
227 | { | |
228 | M_FONTDATA->m_encoding = | |
229 | (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1); | |
230 | } | |
36f210c8 | 231 | } |
7beba2fc | 232 | else if ( registry == _T("MICROSOFT") ) |
36f210c8 | 233 | { |
7beba2fc VZ |
234 | int cp; |
235 | if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 ) | |
236 | { | |
237 | M_FONTDATA->m_encoding = | |
238 | (wxFontEncoding)(wxFONTENCODING_CP1250 + cp); | |
239 | } | |
36f210c8 | 240 | } |
7beba2fc VZ |
241 | else if ( registry == _T("KOI8") ) |
242 | { | |
243 | M_FONTDATA->m_encoding = wxFONTENCODING_KOI8; | |
244 | } | |
245 | //else: unknown encoding - may be give a warning here? | |
36f210c8 | 246 | } |
ff7b1510 | 247 | } |
c801d85f | 248 | |
0c5d3e1c VZ |
249 | bool wxFont::Create( int pointSize, |
250 | int family, | |
251 | int style, | |
252 | int weight, | |
253 | bool underlined, | |
254 | const wxString& face, | |
255 | wxFontEncoding encoding ) | |
8bbe427f | 256 | { |
0c5d3e1c VZ |
257 | m_refData = new wxFontRefData(pointSize, family, style, weight, |
258 | underlined, face, encoding); | |
8bbe427f | 259 | |
0c5d3e1c | 260 | return TRUE; |
ff7b1510 | 261 | } |
c801d85f | 262 | |
0c5d3e1c | 263 | void wxFont::Unshare() |
8bbe427f | 264 | { |
0c5d3e1c VZ |
265 | if (!m_refData) |
266 | { | |
267 | m_refData = new wxFontRefData(); | |
268 | } | |
269 | else | |
270 | { | |
271 | wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData); | |
272 | UnRef(); | |
273 | m_refData = ref; | |
274 | } | |
ff7b1510 | 275 | } |
c801d85f | 276 | |
8bbe427f | 277 | wxFont::~wxFont() |
c801d85f | 278 | { |
0c5d3e1c VZ |
279 | if (wxTheFontList) |
280 | wxTheFontList->DeleteObject( this ); | |
ff7b1510 | 281 | } |
c801d85f | 282 | |
0c5d3e1c VZ |
283 | // ---------------------------------------------------------------------------- |
284 | // accessors | |
285 | // ---------------------------------------------------------------------------- | |
c801d85f | 286 | |
8bbe427f | 287 | int wxFont::GetPointSize() const |
c801d85f | 288 | { |
223d09f6 | 289 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
8bbe427f VZ |
290 | |
291 | return M_FONTDATA->m_pointSize; | |
ff7b1510 | 292 | } |
c801d85f | 293 | |
8bbe427f | 294 | wxString wxFont::GetFaceName() const |
c801d85f | 295 | { |
223d09f6 | 296 | wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") ); |
8bbe427f | 297 | |
36b3b54a | 298 | return M_FONTDATA->m_faceName; |
ff7b1510 | 299 | } |
c801d85f | 300 | |
8bbe427f | 301 | int wxFont::GetFamily() const |
c801d85f | 302 | { |
223d09f6 | 303 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
8bbe427f VZ |
304 | |
305 | return M_FONTDATA->m_family; | |
ff7b1510 | 306 | } |
c801d85f | 307 | |
8bbe427f | 308 | int wxFont::GetStyle() const |
c801d85f | 309 | { |
223d09f6 | 310 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
d84eb083 | 311 | |
8bbe427f | 312 | return M_FONTDATA->m_style; |
ff7b1510 | 313 | } |
c801d85f | 314 | |
8bbe427f | 315 | int wxFont::GetWeight() const |
c801d85f | 316 | { |
223d09f6 | 317 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
8bbe427f VZ |
318 | |
319 | return M_FONTDATA->m_weight; | |
320 | } | |
321 | ||
8bbe427f VZ |
322 | bool wxFont::GetUnderlined() const |
323 | { | |
223d09f6 | 324 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); |
8bbe427f VZ |
325 | |
326 | return M_FONTDATA->m_underlined; | |
ff7b1510 | 327 | } |
c801d85f | 328 | |
0c5d3e1c VZ |
329 | |
330 | wxFontEncoding wxFont::GetEncoding() const | |
358fc25c | 331 | { |
223d09f6 | 332 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); |
0c5d3e1c VZ |
333 | |
334 | return M_FONTDATA->m_encoding; | |
358fc25c RR |
335 | } |
336 | ||
0c5d3e1c VZ |
337 | // ---------------------------------------------------------------------------- |
338 | // change font attributes | |
339 | // ---------------------------------------------------------------------------- | |
340 | ||
358fc25c RR |
341 | void wxFont::SetPointSize(int pointSize) |
342 | { | |
343 | Unshare(); | |
344 | ||
345 | M_FONTDATA->m_pointSize = pointSize; | |
346 | } | |
347 | ||
348 | void wxFont::SetFamily(int family) | |
349 | { | |
350 | Unshare(); | |
351 | ||
352 | M_FONTDATA->m_family = family; | |
353 | } | |
354 | ||
355 | void wxFont::SetStyle(int style) | |
356 | { | |
357 | Unshare(); | |
358 | ||
359 | M_FONTDATA->m_style = style; | |
360 | } | |
361 | ||
362 | void wxFont::SetWeight(int weight) | |
363 | { | |
364 | Unshare(); | |
365 | ||
366 | M_FONTDATA->m_weight = weight; | |
367 | } | |
368 | ||
369 | void wxFont::SetFaceName(const wxString& faceName) | |
370 | { | |
371 | Unshare(); | |
372 | ||
373 | M_FONTDATA->m_faceName = faceName; | |
374 | } | |
375 | ||
376 | void wxFont::SetUnderlined(bool underlined) | |
377 | { | |
378 | Unshare(); | |
379 | ||
380 | M_FONTDATA->m_underlined = underlined; | |
381 | } | |
382 | ||
0c5d3e1c VZ |
383 | void wxFont::SetEncoding(wxFontEncoding encoding) |
384 | { | |
385 | Unshare(); | |
c801d85f | 386 | |
0c5d3e1c VZ |
387 | M_FONTDATA->m_encoding = encoding; |
388 | } | |
389 | ||
390 | // ---------------------------------------------------------------------------- | |
391 | // get internal representation of font | |
392 | // ---------------------------------------------------------------------------- | |
c801d85f | 393 | |
c7985368 RR |
394 | static GdkFont *g_systemDefaultGuiFont = (GdkFont*) NULL; |
395 | ||
396 | static GdkFont *GtkGetDefaultGuiFont() | |
397 | { | |
398 | if (!g_systemDefaultGuiFont) | |
399 | { | |
400 | GtkWidget *widget = gtk_button_new(); | |
401 | GtkStyle *def = gtk_rc_get_style( widget ); | |
e6527f9d RR |
402 | if (def) |
403 | { | |
404 | g_systemDefaultGuiFont = gdk_font_ref( def->font ); | |
405 | } | |
406 | else | |
407 | { | |
408 | def = gtk_widget_get_default_style(); | |
409 | if (def) | |
410 | g_systemDefaultGuiFont = gdk_font_ref( def->font ); | |
411 | } | |
c7985368 RR |
412 | gtk_widget_destroy( widget ); |
413 | } | |
414 | return g_systemDefaultGuiFont; | |
415 | } | |
416 | ||
36b3b54a | 417 | GdkFont *wxFont::GetInternalFont( float scale ) const |
c801d85f | 418 | { |
8bbe427f VZ |
419 | if (!Ok()) |
420 | { | |
223d09f6 | 421 | wxFAIL_MSG( wxT("invalid font") ); |
0c5d3e1c | 422 | |
8bbe427f VZ |
423 | return (GdkFont*) NULL; |
424 | } | |
425 | ||
36b3b54a | 426 | long int_scale = long(scale * 100.0 + 0.5); /* key for fontlist */ |
b02da6b1 | 427 | int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100); |
8bbe427f VZ |
428 | GdkFont *font = (GdkFont *) NULL; |
429 | ||
430 | wxNode *node = M_FONTDATA->m_scaled_xfonts.Find(int_scale); | |
431 | if (node) | |
432 | { | |
433 | font = (GdkFont*)node->Data(); | |
434 | } | |
435 | else | |
436 | { | |
c7985368 | 437 | if (*this == wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT)) |
8bbe427f | 438 | { |
c7985368 | 439 | font = GtkGetDefaultGuiFont(); |
8bbe427f | 440 | } |
e6527f9d | 441 | if (!font) |
8bbe427f | 442 | { |
0c5d3e1c VZ |
443 | font = wxLoadQueryNearestFont( point_scale, |
444 | M_FONTDATA->m_family, | |
445 | M_FONTDATA->m_style, | |
446 | M_FONTDATA->m_weight, | |
447 | M_FONTDATA->m_underlined, | |
448 | M_FONTDATA->m_faceName, | |
449 | M_FONTDATA->m_encoding ); | |
8bbe427f | 450 | } |
0c5d3e1c | 451 | |
8bbe427f VZ |
452 | M_FONTDATA->m_scaled_xfonts.Append( int_scale, (wxObject*)font ); |
453 | } | |
284b4c88 | 454 | |
7beba2fc VZ |
455 | // it's quite useless to make it a wxCHECK because we're going to crash |
456 | // anyhow... | |
457 | wxASSERT_MSG( font, wxT("could not load any font?") ); | |
284b4c88 | 458 | |
8bbe427f | 459 | return font; |
ff7b1510 | 460 | } |
c801d85f | 461 |