]>
Commit | Line | Data |
---|---|---|
83df96d6 | 1 | ///////////////////////////////////////////////////////////////////////////// |
788519c6 | 2 | // Name: src/x11/font.cpp |
83df96d6 JS |
3 | // Purpose: wxFont class |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
83df96d6 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
83df96d6 JS |
20 | #ifdef __VMS |
21 | #pragma message disable nosimpint | |
22 | #include "wx/vms_x_fix.h" | |
23 | #endif | |
bc797f4c | 24 | |
83df96d6 JS |
25 | #ifdef __VMS |
26 | #pragma message enable nosimpint | |
27 | #endif | |
28 | ||
29 | #include "wx/defs.h" | |
30 | #include "wx/string.h" | |
31 | #include "wx/font.h" | |
32 | #include "wx/gdicmn.h" | |
33 | #include "wx/utils.h" // for wxGetDisplay() | |
34 | #include "wx/fontutil.h" // for wxNativeFontInfo | |
35 | #include "wx/tokenzr.h" | |
36 | #include "wx/settings.h" | |
2b5f62a0 | 37 | |
8354aa92 | 38 | #include "wx/x11/private.h" |
83df96d6 JS |
39 | |
40 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) | |
41 | ||
42 | // ---------------------------------------------------------------------------- | |
2b5f62a0 VZ |
43 | // constants |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
46 | // the default size (in points) for the fonts | |
47 | static const int wxDEFAULT_FONT_SIZE = 12; | |
48 | ||
49 | ||
50 | #if wxUSE_UNICODE | |
51 | #else | |
52 | // ---------------------------------------------------------------------------- | |
53 | // wxXFont | |
83df96d6 JS |
54 | // ---------------------------------------------------------------------------- |
55 | ||
56 | // For every wxFont, there must be a font for each display and scale requested. | |
57 | // So these objects are stored in wxFontRefData::m_fonts | |
58 | class wxXFont : public wxObject | |
59 | { | |
60 | public: | |
61 | wxXFont(); | |
62 | ~wxXFont(); | |
63 | ||
64 | WXFontStructPtr m_fontStruct; // XFontStruct | |
83df96d6 JS |
65 | WXDisplay* m_display; // XDisplay |
66 | int m_scale; // Scale * 100 | |
67 | }; | |
68 | ||
2b5f62a0 VZ |
69 | wxXFont::wxXFont() |
70 | { | |
71 | m_fontStruct = (WXFontStructPtr) 0; | |
72 | m_display = (WXDisplay*) 0; | |
73 | m_scale = 100; | |
74 | } | |
75 | ||
76 | wxXFont::~wxXFont() | |
77 | { | |
6811251b JS |
78 | // Freeing the font used to produce a segv, but |
79 | // appears to be OK now (bug fix in X11?) | |
80 | XFontStruct* fontStruct = (XFontStruct*) m_fontStruct; | |
81 | XFreeFont((Display*) m_display, fontStruct); | |
2b5f62a0 VZ |
82 | } |
83 | #endif | |
84 | ||
85 | // ---------------------------------------------------------------------------- | |
86 | // wxFontRefData | |
87 | // ---------------------------------------------------------------------------- | |
88 | ||
89 | class wxFontRefData: public wxObjectRefData | |
83df96d6 JS |
90 | { |
91 | friend class wxFont; | |
92 | ||
93 | public: | |
94 | wxFontRefData(int size = wxDEFAULT, | |
95 | int family = wxDEFAULT, | |
96 | int style = wxDEFAULT, | |
97 | int weight = wxDEFAULT, | |
98 | bool underlined = FALSE, | |
99 | const wxString& faceName = wxEmptyString, | |
2b5f62a0 | 100 | wxFontEncoding encoding = wxFONTENCODING_DEFAULT); |
83df96d6 | 101 | |
2b5f62a0 VZ |
102 | // copy cstr |
103 | wxFontRefData(const wxFontRefData& data); | |
9045ad9d | 104 | |
2b5f62a0 VZ |
105 | // from XFLD |
106 | wxFontRefData(const wxString& fontname); | |
9045ad9d | 107 | |
2b5f62a0 VZ |
108 | // dstr |
109 | virtual ~wxFontRefData(); | |
110 | ||
111 | // setters: all of them also take care to modify m_nativeFontInfo if we | |
112 | // have it so as to not lose the information not carried by our fields | |
113 | void SetPointSize(int pointSize); | |
114 | void SetFamily(int family); | |
115 | void SetStyle(int style); | |
116 | void SetWeight(int weight); | |
117 | void SetUnderlined(bool underlined); | |
118 | void SetFaceName(const wxString& facename); | |
119 | void SetEncoding(wxFontEncoding encoding); | |
120 | ||
121 | void SetNoAntiAliasing( bool no = TRUE ) { m_noAA = no; } | |
5ac2e80c | 122 | bool GetNoAntiAliasing() const { return m_noAA; } |
9045ad9d | 123 | |
2b5f62a0 VZ |
124 | // and this one also modifies all the other font data fields |
125 | void SetNativeFontInfo(const wxNativeFontInfo& info); | |
9045ad9d | 126 | |
83df96d6 JS |
127 | protected: |
128 | // common part of all ctors | |
129 | void Init(int size, | |
130 | int family, | |
131 | int style, | |
132 | int weight, | |
133 | bool underlined, | |
134 | const wxString& faceName, | |
135 | wxFontEncoding encoding); | |
136 | ||
2b5f62a0 VZ |
137 | // set all fields from (already initialized and valid) m_nativeFontInfo |
138 | void InitFromNative(); | |
9045ad9d | 139 | |
83df96d6 JS |
140 | // font attributes |
141 | int m_pointSize; | |
142 | int m_family; | |
143 | int m_style; | |
144 | int m_weight; | |
145 | bool m_underlined; | |
146 | wxString m_faceName; | |
2b5f62a0 VZ |
147 | wxFontEncoding m_encoding; // Unused in Unicode mode |
148 | bool m_noAA; // No anti-aliasing | |
83df96d6 JS |
149 | |
150 | wxNativeFontInfo m_nativeFontInfo; | |
9045ad9d | 151 | |
2b5f62a0 VZ |
152 | void ClearX11Fonts(); |
153 | ||
154 | #if wxUSE_UNICODE | |
155 | #else | |
83df96d6 JS |
156 | // A list of wxXFonts |
157 | wxList m_fonts; | |
2b5f62a0 | 158 | #endif |
83df96d6 JS |
159 | }; |
160 | ||
83df96d6 JS |
161 | // ---------------------------------------------------------------------------- |
162 | // wxFontRefData | |
163 | // ---------------------------------------------------------------------------- | |
164 | ||
165 | void wxFontRefData::Init(int pointSize, | |
166 | int family, | |
167 | int style, | |
168 | int weight, | |
169 | bool underlined, | |
170 | const wxString& faceName, | |
171 | wxFontEncoding encoding) | |
172 | { | |
2b5f62a0 | 173 | m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family; |
83df96d6 JS |
174 | |
175 | m_faceName = faceName; | |
176 | ||
2b5f62a0 VZ |
177 | // we accept both wxDEFAULT and wxNORMAL here - should we? |
178 | m_style = style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style; | |
179 | m_weight = weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight; | |
180 | ||
181 | // and here, do we really want to forbid creation of the font of the size | |
182 | // 90 (the value of wxDEFAULT)?? | |
183 | m_pointSize = pointSize == wxDEFAULT || pointSize == -1 | |
184 | ? wxDEFAULT_FONT_SIZE | |
185 | : pointSize; | |
186 | ||
187 | m_underlined = underlined; | |
188 | m_encoding = encoding; | |
9045ad9d | 189 | |
2b5f62a0 VZ |
190 | #if wxUSE_UNICODE |
191 | // Create native font info | |
192 | m_nativeFontInfo.description = pango_font_description_new(); | |
193 | ||
194 | // And set its values | |
195 | switch (m_family) | |
196 | { | |
197 | case wxFONTFAMILY_MODERN: | |
198 | case wxFONTFAMILY_TELETYPE: | |
199 | pango_font_description_set_family( m_nativeFontInfo.description, "monospace" ); | |
200 | break; | |
5844fc07 | 201 | case wxFONTFAMILY_ROMAN: |
2b5f62a0 VZ |
202 | pango_font_description_set_family( m_nativeFontInfo.description, "serif" ); |
203 | break; | |
204 | default: | |
205 | pango_font_description_set_family( m_nativeFontInfo.description, "sans" ); | |
206 | break; | |
207 | } | |
208 | SetStyle( m_style ); | |
209 | SetPointSize( m_pointSize ); | |
210 | SetWeight( m_weight ); | |
211 | #endif | |
212 | } | |
213 | ||
214 | void wxFontRefData::InitFromNative() | |
215 | { | |
216 | m_noAA = FALSE; | |
217 | ||
218 | #if wxUSE_UNICODE | |
219 | // Get native info | |
220 | PangoFontDescription *desc = m_nativeFontInfo.description; | |
221 | ||
222 | // init fields | |
223 | m_faceName = wxGTK_CONV_BACK( pango_font_description_get_family( desc ) ); | |
83df96d6 | 224 | |
2b5f62a0 VZ |
225 | m_pointSize = pango_font_description_get_size( desc ) / PANGO_SCALE; |
226 | ||
227 | switch (pango_font_description_get_style( desc )) | |
228 | { | |
229 | case PANGO_STYLE_NORMAL: | |
230 | m_style = wxFONTSTYLE_NORMAL; | |
231 | break; | |
232 | case PANGO_STYLE_ITALIC: | |
233 | m_style = wxFONTSTYLE_ITALIC; | |
234 | break; | |
235 | case PANGO_STYLE_OBLIQUE: | |
236 | m_style = wxFONTSTYLE_SLANT; | |
237 | break; | |
238 | } | |
239 | ||
240 | switch (pango_font_description_get_weight( desc )) | |
241 | { | |
242 | case PANGO_WEIGHT_ULTRALIGHT: | |
243 | m_weight = wxFONTWEIGHT_LIGHT; | |
244 | break; | |
245 | case PANGO_WEIGHT_LIGHT: | |
246 | m_weight = wxFONTWEIGHT_LIGHT; | |
247 | break; | |
248 | case PANGO_WEIGHT_NORMAL: | |
249 | m_weight = wxFONTWEIGHT_NORMAL; | |
250 | break; | |
251 | case PANGO_WEIGHT_BOLD: | |
252 | m_weight = wxFONTWEIGHT_BOLD; | |
253 | break; | |
254 | case PANGO_WEIGHT_ULTRABOLD: | |
255 | m_weight = wxFONTWEIGHT_BOLD; | |
256 | break; | |
257 | case PANGO_WEIGHT_HEAVY: | |
258 | m_weight = wxFONTWEIGHT_BOLD; | |
259 | break; | |
260 | } | |
261 | ||
262 | if (m_faceName == wxT("monospace")) | |
263 | { | |
264 | m_family = wxFONTFAMILY_TELETYPE; | |
265 | } | |
266 | else if (m_faceName == wxT("sans")) | |
267 | { | |
268 | m_family = wxFONTFAMILY_SWISS; | |
269 | } | |
83df96d6 | 270 | else |
2b5f62a0 VZ |
271 | { |
272 | m_family = wxFONTFAMILY_UNKNOWN; | |
273 | } | |
274 | ||
275 | // Pango description are never underlined (?) | |
276 | m_underlined = FALSE; | |
277 | ||
278 | // Cannot we choose that | |
279 | m_encoding = wxFONTENCODING_SYSTEM; | |
280 | #else // X11 | |
281 | // get the font parameters from the XLFD | |
282 | // ------------------------------------- | |
283 | ||
284 | m_faceName = m_nativeFontInfo.GetXFontComponent(wxXLFD_FAMILY); | |
285 | ||
286 | m_weight = wxFONTWEIGHT_NORMAL; | |
287 | ||
288 | wxString w = m_nativeFontInfo.GetXFontComponent(wxXLFD_WEIGHT).Upper(); | |
289 | if ( !w.empty() && w != _T('*') ) | |
290 | { | |
291 | // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD | |
292 | // and BLACK | |
293 | if ( ((w[0u] == _T('B') && (!wxStrcmp(w.c_str() + 1, wxT("OLD")) || | |
294 | !wxStrcmp(w.c_str() + 1, wxT("LACK"))))) || | |
295 | wxStrstr(w.c_str() + 1, _T("BOLD")) ) | |
296 | { | |
297 | m_weight = wxFONTWEIGHT_BOLD; | |
298 | } | |
299 | else if ( w == _T("LIGHT") || w == _T("THIN") ) | |
300 | { | |
301 | m_weight = wxFONTWEIGHT_LIGHT; | |
302 | } | |
303 | } | |
304 | ||
305 | switch ( wxToupper(*m_nativeFontInfo. | |
306 | GetXFontComponent(wxXLFD_SLANT).c_str()) ) | |
307 | { | |
308 | case _T('I'): // italique | |
309 | m_style = wxFONTSTYLE_ITALIC; | |
310 | break; | |
311 | ||
312 | case _T('O'): // oblique | |
313 | m_style = wxFONTSTYLE_SLANT; | |
314 | break; | |
83df96d6 | 315 | |
2b5f62a0 VZ |
316 | default: |
317 | m_style = wxFONTSTYLE_NORMAL; | |
318 | } | |
319 | ||
320 | long ptSize; | |
321 | if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_POINTSIZE).ToLong(&ptSize) ) | |
322 | { | |
323 | // size in XLFD is in 10 point units | |
324 | m_pointSize = (int)(ptSize / 10); | |
325 | } | |
83df96d6 | 326 | else |
2b5f62a0 VZ |
327 | { |
328 | m_pointSize = wxDEFAULT_FONT_SIZE; | |
329 | } | |
83df96d6 | 330 | |
2b5f62a0 VZ |
331 | // examine the spacing: if the font is monospaced, assume wxTELETYPE |
332 | // family for compatibility with the old code which used it instead of | |
333 | // IsFixedWidth() | |
334 | if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING).Upper() == _T('M') ) | |
335 | { | |
336 | m_family = wxFONTFAMILY_TELETYPE; | |
337 | } | |
338 | else // not monospaceed | |
339 | { | |
340 | // don't even try guessing it, it doesn't work for too many fonts | |
341 | // anyhow | |
342 | m_family = wxFONTFAMILY_UNKNOWN; | |
343 | } | |
344 | ||
345 | // X fonts are never underlined... | |
346 | m_underlined = FALSE; | |
347 | ||
348 | // deal with font encoding | |
349 | wxString | |
350 | registry = m_nativeFontInfo.GetXFontComponent(wxXLFD_REGISTRY).Upper(), | |
351 | encoding = m_nativeFontInfo.GetXFontComponent(wxXLFD_ENCODING).Upper(); | |
352 | ||
353 | if ( registry == _T("ISO8859") ) | |
354 | { | |
355 | int cp; | |
356 | if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 ) | |
357 | { | |
358 | m_encoding = (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1); | |
359 | } | |
360 | } | |
361 | else if ( registry == _T("MICROSOFT") ) | |
362 | { | |
363 | int cp; | |
364 | if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 ) | |
365 | { | |
366 | m_encoding = (wxFontEncoding)(wxFONTENCODING_CP1250 + cp); | |
367 | } | |
368 | } | |
369 | else if ( registry == _T("KOI8") ) | |
370 | { | |
371 | m_encoding = wxFONTENCODING_KOI8; | |
372 | } | |
373 | else // unknown encoding | |
374 | { | |
375 | // may be give a warning here? or use wxFontMapper? | |
376 | m_encoding = wxFONTENCODING_SYSTEM; | |
377 | } | |
378 | #endif // Pango/X11 | |
83df96d6 JS |
379 | } |
380 | ||
2b5f62a0 VZ |
381 | wxFontRefData::wxFontRefData( const wxFontRefData& data ) |
382 | : wxObjectRefData() | |
83df96d6 | 383 | { |
2b5f62a0 VZ |
384 | m_pointSize = data.m_pointSize; |
385 | m_family = data.m_family; | |
386 | m_style = data.m_style; | |
387 | m_weight = data.m_weight; | |
388 | ||
389 | m_underlined = data.m_underlined; | |
390 | ||
391 | m_faceName = data.m_faceName; | |
392 | m_encoding = data.m_encoding; | |
393 | ||
394 | m_noAA = data.m_noAA; | |
9045ad9d | 395 | |
2b5f62a0 VZ |
396 | m_nativeFontInfo = data.m_nativeFontInfo; |
397 | } | |
398 | ||
399 | wxFontRefData::wxFontRefData(int size, int family, int style, | |
400 | int weight, bool underlined, | |
401 | const wxString& faceName, | |
402 | wxFontEncoding encoding) | |
403 | { | |
404 | Init(size, family, style, weight, underlined, faceName, encoding); | |
405 | } | |
406 | ||
407 | wxFontRefData::wxFontRefData(const wxString& fontname) | |
408 | { | |
409 | // VZ: FromString() should really work in both cases, doesn't it? | |
410 | #if wxUSE_UNICODE | |
411 | m_nativeFontInfo.FromString( fontname ); | |
412 | #else | |
413 | m_nativeFontInfo.SetXFontName(fontname); | |
414 | #endif | |
415 | ||
416 | InitFromNative(); | |
417 | } | |
418 | ||
419 | void wxFontRefData::ClearX11Fonts() | |
420 | { | |
421 | #if wxUSE_UNICODE | |
422 | #else | |
ac32ba44 | 423 | wxList::compatibility_iterator node = m_fonts.GetFirst(); |
83df96d6 JS |
424 | while (node) |
425 | { | |
09a1dffa | 426 | wxXFont* f = (wxXFont*) node->GetData(); |
83df96d6 | 427 | delete f; |
09a1dffa | 428 | node = node->GetNext(); |
83df96d6 JS |
429 | } |
430 | m_fonts.Clear(); | |
2b5f62a0 VZ |
431 | #endif |
432 | } | |
433 | ||
434 | wxFontRefData::~wxFontRefData() | |
435 | { | |
436 | ClearX11Fonts(); | |
83df96d6 JS |
437 | } |
438 | ||
439 | // ---------------------------------------------------------------------------- | |
2b5f62a0 | 440 | // wxFontRefData SetXXX() |
83df96d6 JS |
441 | // ---------------------------------------------------------------------------- |
442 | ||
2b5f62a0 | 443 | void wxFontRefData::SetPointSize(int pointSize) |
83df96d6 | 444 | { |
2b5f62a0 VZ |
445 | m_pointSize = pointSize; |
446 | ||
447 | #if wxUSE_UNICODE | |
448 | // Get native info | |
449 | PangoFontDescription *desc = m_nativeFontInfo.description; | |
450 | ||
451 | pango_font_description_set_size( desc, m_pointSize * PANGO_SCALE ); | |
452 | #endif | |
453 | } | |
454 | ||
455 | void wxFontRefData::SetFamily(int family) | |
456 | { | |
457 | m_family = family; | |
458 | ||
459 | // TODO: what are we supposed to do with m_nativeFontInfo here? | |
460 | } | |
461 | ||
462 | void wxFontRefData::SetStyle(int style) | |
463 | { | |
464 | m_style = style; | |
465 | ||
466 | #if wxUSE_UNICODE | |
467 | // Get native info | |
468 | PangoFontDescription *desc = m_nativeFontInfo.description; | |
469 | ||
470 | switch ( style ) | |
471 | { | |
472 | case wxFONTSTYLE_ITALIC: | |
473 | pango_font_description_set_style( desc, PANGO_STYLE_ITALIC ); | |
474 | break; | |
475 | case wxFONTSTYLE_SLANT: | |
476 | pango_font_description_set_style( desc, PANGO_STYLE_OBLIQUE ); | |
477 | break; | |
478 | default: | |
479 | wxFAIL_MSG( _T("unknown font style") ); | |
480 | // fall through | |
481 | case wxFONTSTYLE_NORMAL: | |
482 | pango_font_description_set_style( desc, PANGO_STYLE_NORMAL ); | |
483 | break; | |
484 | } | |
485 | #endif | |
486 | } | |
487 | ||
488 | void wxFontRefData::SetWeight(int weight) | |
489 | { | |
490 | m_weight = weight; | |
491 | } | |
492 | ||
493 | void wxFontRefData::SetUnderlined(bool underlined) | |
494 | { | |
495 | m_underlined = underlined; | |
83df96d6 | 496 | |
2b5f62a0 | 497 | // the XLFD doesn't have "underlined" field anyhow |
83df96d6 JS |
498 | } |
499 | ||
2b5f62a0 VZ |
500 | void wxFontRefData::SetFaceName(const wxString& facename) |
501 | { | |
502 | m_faceName = facename; | |
503 | } | |
504 | ||
505 | void wxFontRefData::SetEncoding(wxFontEncoding encoding) | |
506 | { | |
507 | m_encoding = encoding; | |
508 | } | |
509 | ||
510 | void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info) | |
511 | { | |
512 | // previously cached fonts shouldn't be used | |
513 | ClearX11Fonts(); | |
514 | ||
515 | m_nativeFontInfo = info; | |
516 | ||
517 | // set all the other font parameters from the native font info | |
518 | InitFromNative(); | |
519 | } | |
520 | ||
521 | // ---------------------------------------------------------------------------- | |
522 | // wxFont | |
523 | // ---------------------------------------------------------------------------- | |
524 | ||
83df96d6 JS |
525 | void wxFont::Init() |
526 | { | |
527 | } | |
528 | ||
2b5f62a0 VZ |
529 | wxFont::wxFont(const wxNativeFontInfo& info) |
530 | { | |
531 | Init(); | |
532 | ||
533 | #if wxUSE_UNICODE | |
534 | Create( info.GetPointSize(), | |
535 | info.GetFamily(), | |
536 | info.GetStyle(), | |
537 | info.GetWeight(), | |
538 | info.GetUnderlined(), | |
539 | info.GetFaceName(), | |
540 | info.GetEncoding() ); | |
541 | #else | |
542 | (void) Create(info.GetXFontName()); | |
543 | #endif | |
544 | } | |
545 | ||
83df96d6 JS |
546 | bool wxFont::Create(int pointSize, |
547 | int family, | |
548 | int style, | |
549 | int weight, | |
550 | bool underlined, | |
551 | const wxString& faceName, | |
552 | wxFontEncoding encoding) | |
553 | { | |
554 | UnRef(); | |
9045ad9d | 555 | |
83df96d6 JS |
556 | m_refData = new wxFontRefData(pointSize, family, style, weight, |
557 | underlined, faceName, encoding); | |
558 | ||
83df96d6 JS |
559 | return TRUE; |
560 | } | |
561 | ||
9045ad9d VZ |
562 | #if !wxUSE_UNICODE |
563 | ||
83df96d6 JS |
564 | bool wxFont::Create(const wxString& fontname, wxFontEncoding enc) |
565 | { | |
566 | if( !fontname ) | |
567 | { | |
568 | *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT); | |
569 | return TRUE; | |
570 | } | |
571 | ||
572 | m_refData = new wxFontRefData(); | |
573 | ||
574 | M_FONTDATA->m_nativeFontInfo.SetXFontName(fontname); // X font name | |
575 | ||
576 | wxString tmp; | |
577 | ||
578 | wxStringTokenizer tn( fontname, wxT("-") ); | |
579 | ||
580 | tn.GetNextToken(); // skip initial empty token | |
581 | tn.GetNextToken(); // foundry | |
582 | ||
583 | ||
584 | M_FONTDATA->m_faceName = tn.GetNextToken(); // family | |
585 | ||
586 | tmp = tn.GetNextToken().MakeUpper(); // weight | |
587 | if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxBOLD; | |
588 | if (tmp == wxT("BLACK")) M_FONTDATA->m_weight = wxBOLD; | |
589 | if (tmp == wxT("EXTRABOLD")) M_FONTDATA->m_weight = wxBOLD; | |
590 | if (tmp == wxT("DEMIBOLD")) M_FONTDATA->m_weight = wxBOLD; | |
591 | if (tmp == wxT("ULTRABOLD")) M_FONTDATA->m_weight = wxBOLD; | |
592 | ||
593 | if (tmp == wxT("LIGHT")) M_FONTDATA->m_weight = wxLIGHT; | |
594 | if (tmp == wxT("THIN")) M_FONTDATA->m_weight = wxLIGHT; | |
595 | ||
596 | tmp = tn.GetNextToken().MakeUpper(); // slant | |
597 | if (tmp == wxT("I")) M_FONTDATA->m_style = wxITALIC; | |
598 | if (tmp == wxT("O")) M_FONTDATA->m_style = wxITALIC; | |
599 | ||
600 | tn.GetNextToken(); // set width | |
601 | tn.GetNextToken(); // add. style | |
602 | tn.GetNextToken(); // pixel size | |
603 | ||
604 | tmp = tn.GetNextToken(); // pointsize | |
605 | if (tmp != wxT("*")) | |
606 | { | |
607 | long num = wxStrtol (tmp.c_str(), (wxChar **) NULL, 10); | |
608 | M_FONTDATA->m_pointSize = (int)(num / 10); | |
609 | } | |
610 | ||
611 | tn.GetNextToken(); // x-res | |
612 | tn.GetNextToken(); // y-res | |
613 | ||
614 | tmp = tn.GetNextToken().MakeUpper(); // spacing | |
615 | ||
616 | if (tmp == wxT("M")) | |
617 | M_FONTDATA->m_family = wxMODERN; | |
618 | else if (M_FONTDATA->m_faceName == wxT("TIMES")) | |
619 | M_FONTDATA->m_family = wxROMAN; | |
620 | else if (M_FONTDATA->m_faceName == wxT("HELVETICA")) | |
621 | M_FONTDATA->m_family = wxSWISS; | |
622 | else if (M_FONTDATA->m_faceName == wxT("LUCIDATYPEWRITER")) | |
623 | M_FONTDATA->m_family = wxTELETYPE; | |
624 | else if (M_FONTDATA->m_faceName == wxT("LUCIDA")) | |
625 | M_FONTDATA->m_family = wxDECORATIVE; | |
626 | else if (M_FONTDATA->m_faceName == wxT("UTOPIA")) | |
627 | M_FONTDATA->m_family = wxSCRIPT; | |
628 | ||
629 | tn.GetNextToken(); // avg width | |
630 | ||
631 | // deal with font encoding | |
632 | M_FONTDATA->m_encoding = enc; | |
633 | if ( M_FONTDATA->m_encoding == wxFONTENCODING_SYSTEM ) | |
634 | { | |
635 | wxString registry = tn.GetNextToken().MakeUpper(), | |
636 | encoding = tn.GetNextToken().MakeUpper(); | |
637 | ||
638 | if ( registry == _T("ISO8859") ) | |
639 | { | |
640 | int cp; | |
641 | if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 ) | |
642 | { | |
643 | M_FONTDATA->m_encoding = | |
644 | (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1); | |
645 | } | |
646 | } | |
647 | else if ( registry == _T("MICROSOFT") ) | |
648 | { | |
649 | int cp; | |
650 | if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 ) | |
651 | { | |
652 | M_FONTDATA->m_encoding = | |
653 | (wxFontEncoding)(wxFONTENCODING_CP1250 + cp); | |
654 | } | |
655 | } | |
656 | else if ( registry == _T("KOI8") ) | |
657 | { | |
658 | M_FONTDATA->m_encoding = wxFONTENCODING_KOI8; | |
659 | } | |
660 | //else: unknown encoding - may be give a warning here? | |
661 | else | |
662 | return FALSE; | |
663 | } | |
9045ad9d | 664 | return TRUE; |
83df96d6 | 665 | } |
9045ad9d | 666 | #endif // !wxUSE_UNICODE |
83df96d6 JS |
667 | |
668 | wxFont::~wxFont() | |
669 | { | |
670 | } | |
671 | ||
672 | // ---------------------------------------------------------------------------- | |
673 | // change the font attributes | |
674 | // ---------------------------------------------------------------------------- | |
675 | ||
676 | void wxFont::Unshare() | |
677 | { | |
678 | // Don't change shared data | |
679 | if (!m_refData) | |
680 | { | |
681 | m_refData = new wxFontRefData(); | |
682 | } | |
683 | else | |
684 | { | |
685 | wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData); | |
686 | UnRef(); | |
687 | m_refData = ref; | |
688 | } | |
689 | } | |
690 | ||
2b5f62a0 VZ |
691 | // ---------------------------------------------------------------------------- |
692 | // accessors | |
693 | // ---------------------------------------------------------------------------- | |
694 | ||
695 | int wxFont::GetPointSize() const | |
83df96d6 | 696 | { |
2b5f62a0 | 697 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
83df96d6 | 698 | |
2b5f62a0 | 699 | return M_FONTDATA->m_pointSize; |
83df96d6 JS |
700 | } |
701 | ||
2b5f62a0 | 702 | wxString wxFont::GetFaceName() const |
83df96d6 | 703 | { |
2b5f62a0 | 704 | wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") ); |
83df96d6 | 705 | |
2b5f62a0 | 706 | return M_FONTDATA->m_faceName; |
83df96d6 JS |
707 | } |
708 | ||
2b5f62a0 | 709 | int wxFont::GetFamily() const |
83df96d6 | 710 | { |
2b5f62a0 | 711 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
83df96d6 | 712 | |
2b5f62a0 | 713 | return M_FONTDATA->m_family; |
83df96d6 JS |
714 | } |
715 | ||
2b5f62a0 | 716 | int wxFont::GetStyle() const |
83df96d6 | 717 | { |
2b5f62a0 | 718 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
83df96d6 | 719 | |
2b5f62a0 | 720 | return M_FONTDATA->m_style; |
83df96d6 JS |
721 | } |
722 | ||
2b5f62a0 | 723 | int wxFont::GetWeight() const |
83df96d6 | 724 | { |
2b5f62a0 | 725 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
83df96d6 | 726 | |
2b5f62a0 | 727 | return M_FONTDATA->m_weight; |
83df96d6 JS |
728 | } |
729 | ||
2b5f62a0 | 730 | bool wxFont::GetUnderlined() const |
83df96d6 | 731 | { |
2b5f62a0 | 732 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); |
83df96d6 | 733 | |
2b5f62a0 | 734 | return M_FONTDATA->m_underlined; |
83df96d6 JS |
735 | } |
736 | ||
2b5f62a0 | 737 | wxFontEncoding wxFont::GetEncoding() const |
83df96d6 | 738 | { |
2b5f62a0 | 739 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); |
83df96d6 | 740 | |
2b5f62a0 | 741 | return M_FONTDATA->m_encoding; |
83df96d6 JS |
742 | } |
743 | ||
5ac2e80c | 744 | bool wxFont::GetNoAntiAliasing() const |
83df96d6 | 745 | { |
2b5f62a0 VZ |
746 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); |
747 | ||
748 | return M_FONTDATA->m_noAA; | |
749 | } | |
750 | ||
3bf5a59b | 751 | const wxNativeFontInfo *wxFont::GetNativeFontInfo() const |
2b5f62a0 VZ |
752 | { |
753 | wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") ); | |
754 | ||
755 | #if wxUSE_UNICODE | |
756 | #else | |
757 | if ( M_FONTDATA->m_nativeFontInfo.GetXFontName().empty() ) | |
758 | GetInternalFont(); | |
759 | #endif | |
760 | ||
3bf5a59b | 761 | return &(M_FONTDATA->m_nativeFontInfo); |
2b5f62a0 VZ |
762 | } |
763 | ||
764 | bool wxFont::IsFixedWidth() const | |
765 | { | |
766 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); | |
767 | ||
768 | #if wxUSE_UNICODE | |
a371f703 | 769 | return wxFontBase::IsFixedWidth(); |
2b5f62a0 VZ |
770 | #else |
771 | // Robert, is this right? HasNativeFont doesn't exist. | |
772 | if ( TRUE ) | |
773 | // if ( M_FONTDATA->HasNativeFont() ) | |
774 | { | |
775 | // the monospace fonts are supposed to have "M" in the spacing field | |
776 | wxString spacing = M_FONTDATA-> | |
777 | m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING); | |
778 | ||
779 | return spacing.Upper() == _T('M'); | |
780 | } | |
a371f703 JJ |
781 | // Unreaceable code for now |
782 | // return wxFontBase::IsFixedWidth(); | |
2b5f62a0 | 783 | #endif |
83df96d6 | 784 | |
83df96d6 JS |
785 | } |
786 | ||
787 | // ---------------------------------------------------------------------------- | |
2b5f62a0 | 788 | // change font attributes |
83df96d6 JS |
789 | // ---------------------------------------------------------------------------- |
790 | ||
2b5f62a0 | 791 | void wxFont::SetPointSize(int pointSize) |
83df96d6 | 792 | { |
2b5f62a0 VZ |
793 | Unshare(); |
794 | ||
795 | M_FONTDATA->SetPointSize(pointSize); | |
83df96d6 JS |
796 | } |
797 | ||
2b5f62a0 | 798 | void wxFont::SetFamily(int family) |
83df96d6 | 799 | { |
2b5f62a0 | 800 | Unshare(); |
83df96d6 | 801 | |
2b5f62a0 | 802 | M_FONTDATA->SetFamily(family); |
83df96d6 JS |
803 | } |
804 | ||
2b5f62a0 | 805 | void wxFont::SetStyle(int style) |
83df96d6 | 806 | { |
2b5f62a0 | 807 | Unshare(); |
83df96d6 | 808 | |
2b5f62a0 | 809 | M_FONTDATA->SetStyle(style); |
83df96d6 JS |
810 | } |
811 | ||
2b5f62a0 | 812 | void wxFont::SetWeight(int weight) |
83df96d6 | 813 | { |
2b5f62a0 | 814 | Unshare(); |
83df96d6 | 815 | |
2b5f62a0 | 816 | M_FONTDATA->SetWeight(weight); |
83df96d6 JS |
817 | } |
818 | ||
2b5f62a0 | 819 | void wxFont::SetFaceName(const wxString& faceName) |
83df96d6 | 820 | { |
2b5f62a0 | 821 | Unshare(); |
83df96d6 | 822 | |
2b5f62a0 | 823 | M_FONTDATA->SetFaceName(faceName); |
83df96d6 JS |
824 | } |
825 | ||
2b5f62a0 | 826 | void wxFont::SetUnderlined(bool underlined) |
83df96d6 | 827 | { |
2b5f62a0 | 828 | Unshare(); |
83df96d6 | 829 | |
2b5f62a0 | 830 | M_FONTDATA->SetUnderlined(underlined); |
83df96d6 JS |
831 | } |
832 | ||
2b5f62a0 | 833 | void wxFont::SetEncoding(wxFontEncoding encoding) |
83df96d6 | 834 | { |
2b5f62a0 | 835 | Unshare(); |
83df96d6 | 836 | |
2b5f62a0 | 837 | M_FONTDATA->SetEncoding(encoding); |
83df96d6 JS |
838 | } |
839 | ||
9045ad9d | 840 | void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info ) |
83df96d6 | 841 | { |
2b5f62a0 | 842 | Unshare(); |
83df96d6 | 843 | |
2b5f62a0 VZ |
844 | M_FONTDATA->SetNativeFontInfo( info ); |
845 | } | |
83df96d6 | 846 | |
2b5f62a0 VZ |
847 | void wxFont::SetNoAntiAliasing( bool no ) |
848 | { | |
849 | Unshare(); | |
850 | ||
851 | M_FONTDATA->SetNoAntiAliasing( no ); | |
83df96d6 JS |
852 | } |
853 | ||
2b5f62a0 VZ |
854 | #if wxUSE_UNICODE |
855 | #else | |
856 | ||
83df96d6 | 857 | // ---------------------------------------------------------------------------- |
2b5f62a0 | 858 | // X11 implementation |
83df96d6 JS |
859 | // ---------------------------------------------------------------------------- |
860 | ||
861 | // Find an existing, or create a new, XFontStruct | |
862 | // based on this wxFont and the given scale. Append the | |
863 | // font to list in the private data for future reference. | |
864 | wxXFont* wxFont::GetInternalFont(double scale, WXDisplay* display) const | |
865 | { | |
866 | if ( !Ok() ) | |
867 | return (wxXFont *)NULL; | |
868 | ||
869 | long intScale = long(scale * 100.0 + 0.5); // key for wxXFont | |
870 | int pointSize = (M_FONTDATA->m_pointSize * 10 * intScale) / 100; | |
871 | ||
872 | // search existing fonts first | |
ac32ba44 | 873 | wxList::compatibility_iterator node = M_FONTDATA->m_fonts.GetFirst(); |
83df96d6 JS |
874 | while (node) |
875 | { | |
09a1dffa | 876 | wxXFont* f = (wxXFont*) node->GetData(); |
83df96d6 JS |
877 | if ((!display || (f->m_display == display)) && (f->m_scale == intScale)) |
878 | return f; | |
09a1dffa | 879 | node = node->GetNext(); |
83df96d6 JS |
880 | } |
881 | ||
69ffc7b2 | 882 | wxString xFontName = M_FONTDATA->m_nativeFontInfo.GetXFontName(); |
97bdb4c8 JS |
883 | if (xFontName == "-*-*-*-*-*--*-*-*-*-*-*-*-*") |
884 | // wxFont constructor not called with native font info parameter => take M_FONTDATA values | |
885 | xFontName.Clear(); | |
69ffc7b2 | 886 | |
83df96d6 JS |
887 | // not found, create a new one |
888 | XFontStruct *font = (XFontStruct *) | |
889 | wxLoadQueryNearestFont(pointSize, | |
890 | M_FONTDATA->m_family, | |
891 | M_FONTDATA->m_style, | |
892 | M_FONTDATA->m_weight, | |
893 | M_FONTDATA->m_underlined, | |
894 | wxT(""), | |
69ffc7b2 JS |
895 | M_FONTDATA->m_encoding, |
896 | & xFontName); | |
83df96d6 JS |
897 | |
898 | if ( !font ) | |
899 | { | |
900 | wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") ); | |
901 | ||
902 | return (wxXFont*) NULL; | |
903 | } | |
904 | ||
905 | wxXFont* f = new wxXFont; | |
906 | f->m_fontStruct = (WXFontStructPtr)font; | |
907 | f->m_display = ( display ? display : wxGetDisplay() ); | |
908 | f->m_scale = intScale; | |
83df96d6 JS |
909 | M_FONTDATA->m_fonts.Append(f); |
910 | ||
911 | return f; | |
912 | } | |
913 | ||
914 | WXFontStructPtr wxFont::GetFontStruct(double scale, WXDisplay* display) const | |
915 | { | |
916 | wxXFont* f = GetInternalFont(scale, display); | |
917 | ||
918 | return (f ? f->m_fontStruct : (WXFontStructPtr) 0); | |
919 | } | |
920 | ||
2b5f62a0 | 921 | #endif |