]>
Commit | Line | Data |
---|---|---|
8f7fa6f8 | 1 | ///////////////////////////////////////////////////////////////////////////// |
da80ae71 | 2 | // Name: src/common/fontcmn.cpp |
0c5d3e1c VZ |
3 | // Purpose: implementation of wxFontBase methods |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 20.09.99 | |
7 | // RCS-ID: $Id$ | |
77ffb593 | 8 | // Copyright: (c) wxWidgets team |
65571936 | 9 | // Licence: wxWindows licence |
0c5d3e1c VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
da80ae71 | 24 | #pragma hdrstop |
0c5d3e1c VZ |
25 | #endif |
26 | ||
da80ae71 WS |
27 | #include "wx/font.h" |
28 | ||
0c5d3e1c | 29 | #ifndef WX_PRECOMP |
452aa069 | 30 | #include "wx/dc.h" |
9342fdbe VZ |
31 | #include "wx/intl.h" |
32 | #include "wx/dcscreen.h" | |
8d3ba63b | 33 | #include "wx/log.h" |
dd05139a | 34 | #include "wx/gdicmn.h" |
0c5d3e1c VZ |
35 | #endif // WX_PRECOMP |
36 | ||
82ef81ed | 37 | #if defined(__WXMSW__) |
7bd236e6 WS |
38 | #include "wx/msw/private.h" // includes windows.h for LOGFONT |
39 | #include "wx/msw/winundef.h" | |
1c193821 JS |
40 | #endif |
41 | ||
76e23cdb | 42 | #include "wx/fontutil.h" // for wxNativeFontInfo |
ab5fe833 | 43 | #include "wx/fontmap.h" |
85ab460e | 44 | #include "wx/fontenum.h" |
76e23cdb | 45 | |
30764ab5 VZ |
46 | #include "wx/tokenzr.h" |
47 | ||
0c5d3e1c VZ |
48 | // ============================================================================ |
49 | // implementation | |
50 | // ============================================================================ | |
51 | ||
544229d1 VZ |
52 | // ---------------------------------------------------------------------------- |
53 | // helper functions | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
8093538c | 56 | static void AdjustFontSize(wxFont& font, wxDC& dc, const wxSize& pixelSize) |
544229d1 | 57 | { |
d141b218 | 58 | int currentSize = 0; |
8093538c VZ |
59 | int largestGood = 0; |
60 | int smallestBad = 0; | |
544229d1 VZ |
61 | |
62 | bool initialGoodFound = false; | |
63 | bool initialBadFound = false; | |
64 | ||
d141b218 DS |
65 | // NB: this assignment was separated from the variable definition |
66 | // in order to fix a gcc v3.3.3 compiler crash | |
bf26e9e0 | 67 | currentSize = font.GetPointSize(); |
544229d1 VZ |
68 | while (currentSize > 0) |
69 | { | |
70 | dc.SetFont(font); | |
71 | ||
72 | // if currentSize (in points) results in a font that is smaller | |
73 | // than required by pixelSize it is considered a good size | |
74 | if (dc.GetCharHeight() <= pixelSize.GetHeight() && | |
75 | (!pixelSize.GetWidth() || | |
76 | dc.GetCharWidth() <= pixelSize.GetWidth())) | |
77 | { | |
78 | largestGood = currentSize; | |
79 | initialGoodFound = true; | |
80 | } | |
81 | else | |
82 | { | |
83 | smallestBad = currentSize; | |
84 | initialBadFound = true; | |
85 | } | |
86 | if (!initialGoodFound) | |
87 | { | |
88 | currentSize /= 2; | |
89 | } | |
90 | else if (!initialBadFound) | |
91 | { | |
92 | currentSize *= 2; | |
93 | } | |
94 | else | |
95 | { | |
96 | int distance = smallestBad - largestGood; | |
97 | if (distance == 1) | |
98 | break; | |
99 | ||
100 | currentSize = largestGood + distance / 2; | |
101 | } | |
102 | ||
103 | font.SetPointSize(currentSize); | |
104 | } | |
105 | ||
106 | if (currentSize != largestGood) | |
107 | font.SetPointSize(largestGood); | |
108 | } | |
109 | ||
0c5d3e1c VZ |
110 | // ---------------------------------------------------------------------------- |
111 | // wxFontBase | |
112 | // ---------------------------------------------------------------------------- | |
113 | ||
114 | wxFontEncoding wxFontBase::ms_encodingDefault = wxFONTENCODING_SYSTEM; | |
115 | ||
cafbf6fb VZ |
116 | /* static */ |
117 | void wxFontBase::SetDefaultEncoding(wxFontEncoding encoding) | |
118 | { | |
119 | // GetDefaultEncoding() should return something != wxFONTENCODING_DEFAULT | |
120 | // and, besides, using this value here doesn't make any sense | |
121 | wxCHECK_RET( encoding != wxFONTENCODING_DEFAULT, | |
122 | _T("can't set default encoding to wxFONTENCODING_DEFAULT") ); | |
123 | ||
124 | ms_encodingDefault = encoding; | |
125 | } | |
126 | ||
799ea011 GD |
127 | wxFontBase::~wxFontBase() |
128 | { | |
129 | // this destructor is required for Darwin | |
130 | } | |
131 | ||
7beba2fc | 132 | /* static */ |
0c5d3e1c VZ |
133 | wxFont *wxFontBase::New(int size, |
134 | int family, | |
135 | int style, | |
136 | int weight, | |
137 | bool underlined, | |
138 | const wxString& face, | |
139 | wxFontEncoding encoding) | |
140 | { | |
141 | return new wxFont(size, family, style, weight, underlined, face, encoding); | |
142 | } | |
143 | ||
544229d1 | 144 | static inline int flags2Style(int flags) |
01cb1c26 | 145 | { |
544229d1 | 146 | return flags & wxFONTFLAG_ITALIC |
01cb1c26 VZ |
147 | ? wxFONTSTYLE_ITALIC |
148 | : flags & wxFONTFLAG_SLANT | |
149 | ? wxFONTSTYLE_SLANT | |
544229d1 VZ |
150 | : wxFONTSTYLE_NORMAL; |
151 | } | |
152 | ||
153 | static inline int flags2Weight(int flags) | |
154 | { | |
155 | return flags & wxFONTFLAG_LIGHT | |
01cb1c26 VZ |
156 | ? wxFONTWEIGHT_LIGHT |
157 | : flags & wxFONTFLAG_BOLD | |
158 | ? wxFONTWEIGHT_BOLD | |
544229d1 VZ |
159 | : wxFONTWEIGHT_NORMAL; |
160 | } | |
161 | ||
162 | static inline bool flags2Underlined(int flags) | |
163 | { | |
164 | return (flags & wxFONTFLAG_UNDERLINED) != 0; | |
165 | } | |
166 | ||
167 | /* static */ | |
168 | wxFont *wxFontBase::New(int pointSize, | |
169 | wxFontFamily family, | |
170 | int flags, | |
171 | const wxString& face, | |
172 | wxFontEncoding encoding) | |
173 | { | |
174 | return New(pointSize, family, flags2Style(flags), flags2Weight(flags), | |
175 | flags2Underlined(flags), face, encoding); | |
176 | } | |
177 | ||
178 | /* static */ | |
179 | wxFont *wxFontBase::New(const wxSize& pixelSize, | |
180 | int family, | |
181 | int style, | |
182 | int weight, | |
183 | bool underlined, | |
184 | const wxString& face, | |
185 | wxFontEncoding encoding) | |
186 | { | |
187 | #if defined(__WXMSW__) | |
188 | return new wxFont(pixelSize, family, style, weight, underlined, | |
189 | face, encoding); | |
190 | #else | |
9342fdbe | 191 | wxFont *self = New(10, family, style, weight, underlined, face, encoding); |
544229d1 | 192 | wxScreenDC dc; |
9342fdbe VZ |
193 | AdjustFontSize(*(wxFont *)self, dc, pixelSize); |
194 | return self; | |
544229d1 VZ |
195 | #endif |
196 | } | |
197 | ||
198 | /* static */ | |
199 | wxFont *wxFontBase::New(const wxSize& pixelSize, | |
200 | wxFontFamily family, | |
201 | int flags, | |
202 | const wxString& face, | |
203 | wxFontEncoding encoding) | |
204 | { | |
205 | return New(pixelSize, family, flags2Style(flags), flags2Weight(flags), | |
206 | flags2Underlined(flags), face, encoding); | |
207 | } | |
208 | ||
209 | wxSize wxFontBase::GetPixelSize() const | |
210 | { | |
211 | wxScreenDC dc; | |
212 | dc.SetFont(*(wxFont *)this); | |
213 | return wxSize(dc.GetCharWidth(), dc.GetCharHeight()); | |
214 | } | |
215 | ||
216 | bool wxFontBase::IsUsingSizeInPixels() const | |
217 | { | |
218 | return false; | |
219 | } | |
220 | ||
221 | void wxFontBase::SetPixelSize( const wxSize& pixelSize ) | |
222 | { | |
223 | wxScreenDC dc; | |
224 | AdjustFontSize(*(wxFont *)this, dc, pixelSize); | |
01cb1c26 VZ |
225 | } |
226 | ||
30764ab5 VZ |
227 | /* static */ |
228 | wxFont *wxFontBase::New(const wxNativeFontInfo& info) | |
229 | { | |
230 | return new wxFont(info); | |
231 | } | |
232 | ||
7826e2dd VZ |
233 | /* static */ |
234 | wxFont *wxFontBase::New(const wxString& strNativeFontDesc) | |
30764ab5 | 235 | { |
30764ab5 | 236 | wxNativeFontInfo fontInfo; |
7826e2dd | 237 | if ( !fontInfo.FromString(strNativeFontDesc) ) |
09fcd889 | 238 | return new wxFont(*wxNORMAL_FONT); |
7826e2dd VZ |
239 | |
240 | return New(fontInfo); | |
241 | } | |
242 | ||
53f6aab7 VZ |
243 | bool wxFontBase::IsFixedWidth() const |
244 | { | |
245 | return GetFamily() == wxFONTFAMILY_TELETYPE; | |
246 | } | |
247 | ||
9045ad9d | 248 | void wxFontBase::DoSetNativeFontInfo(const wxNativeFontInfo& info) |
30764ab5 | 249 | { |
ab5fe833 | 250 | #ifdef wxNO_NATIVE_FONTINFO |
30764ab5 VZ |
251 | SetPointSize(info.pointSize); |
252 | SetFamily(info.family); | |
253 | SetStyle(info.style); | |
254 | SetWeight(info.weight); | |
255 | SetUnderlined(info.underlined); | |
256 | SetFaceName(info.faceName); | |
257 | SetEncoding(info.encoding); | |
33ac7e6f | 258 | #else |
1e6feb95 | 259 | (void)info; |
30764ab5 VZ |
260 | #endif |
261 | } | |
262 | ||
7826e2dd VZ |
263 | wxString wxFontBase::GetNativeFontInfoDesc() const |
264 | { | |
265 | wxString fontDesc; | |
3bf5a59b | 266 | const wxNativeFontInfo *fontInfo = GetNativeFontInfo(); |
7826e2dd VZ |
267 | if ( fontInfo ) |
268 | { | |
269 | fontDesc = fontInfo->ToString(); | |
dd05139a | 270 | wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!")); |
85ab460e VZ |
271 | } |
272 | else | |
273 | { | |
7bd236e6 | 274 | wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!")); |
7826e2dd VZ |
275 | } |
276 | ||
277 | return fontDesc; | |
278 | } | |
279 | ||
ab5fe833 VZ |
280 | wxString wxFontBase::GetNativeFontInfoUserDesc() const |
281 | { | |
282 | wxString fontDesc; | |
3bf5a59b | 283 | const wxNativeFontInfo *fontInfo = GetNativeFontInfo(); |
ab5fe833 VZ |
284 | if ( fontInfo ) |
285 | { | |
286 | fontDesc = fontInfo->ToUserString(); | |
dd05139a | 287 | wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!")); |
85ab460e VZ |
288 | } |
289 | else | |
290 | { | |
7456f382 | 291 | wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!")); |
ab5fe833 VZ |
292 | } |
293 | ||
294 | return fontDesc; | |
295 | } | |
296 | ||
85ab460e | 297 | bool wxFontBase::SetNativeFontInfo(const wxString& info) |
31d1b66e VZ |
298 | { |
299 | wxNativeFontInfo fontInfo; | |
300 | if ( !info.empty() && fontInfo.FromString(info) ) | |
301 | { | |
302 | SetNativeFontInfo(fontInfo); | |
85ab460e | 303 | return true; |
31d1b66e | 304 | } |
85ab460e | 305 | |
85ab460e | 306 | return false; |
31d1b66e VZ |
307 | } |
308 | ||
85ab460e | 309 | bool wxFontBase::SetNativeFontInfoUserDesc(const wxString& info) |
ab5fe833 VZ |
310 | { |
311 | wxNativeFontInfo fontInfo; | |
312 | if ( !info.empty() && fontInfo.FromUserString(info) ) | |
313 | { | |
314 | SetNativeFontInfo(fontInfo); | |
85ab460e | 315 | return true; |
ab5fe833 | 316 | } |
85ab460e | 317 | |
85ab460e | 318 | return false; |
ab5fe833 VZ |
319 | } |
320 | ||
0c5d3e1c VZ |
321 | bool wxFontBase::operator==(const wxFont& font) const |
322 | { | |
8bf30fe9 VZ |
323 | // either it is the same font, i.e. they share the same common data or they |
324 | // have different ref datas but still describe the same font | |
a38cd629 | 325 | return IsSameAs(font) || |
8bf30fe9 VZ |
326 | ( |
327 | Ok() == font.Ok() && | |
328 | GetPointSize() == font.GetPointSize() && | |
82d0e7fe VZ |
329 | // in wxGTK1 GetPixelSize() calls GetInternalFont() which uses |
330 | // operator==() resulting in infinite recursion so we can't use it | |
331 | // in that port | |
332 | #if !defined(__WXGTK__) || defined(__WXGTK20__) | |
cc3de8a3 | 333 | GetPixelSize() == font.GetPixelSize() && |
82d0e7fe | 334 | #endif |
8bf30fe9 VZ |
335 | GetFamily() == font.GetFamily() && |
336 | GetStyle() == font.GetStyle() && | |
e6adf058 | 337 | GetWeight() == font.GetWeight() && |
8bf30fe9 | 338 | GetUnderlined() == font.GetUnderlined() && |
85ab460e | 339 | GetFaceName().IsSameAs(font.GetFaceName(), false) && |
8bf30fe9 VZ |
340 | GetEncoding() == font.GetEncoding() |
341 | ); | |
0c5d3e1c VZ |
342 | } |
343 | ||
344 | bool wxFontBase::operator!=(const wxFont& font) const | |
345 | { | |
8bf30fe9 | 346 | return !(*this == font); |
0c5d3e1c VZ |
347 | } |
348 | ||
349 | wxString wxFontBase::GetFamilyString() const | |
350 | { | |
223d09f6 | 351 | wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") ); |
0c5d3e1c VZ |
352 | |
353 | switch ( GetFamily() ) | |
354 | { | |
223d09f6 KB |
355 | case wxDECORATIVE: return wxT("wxDECORATIVE"); |
356 | case wxROMAN: return wxT("wxROMAN"); | |
357 | case wxSCRIPT: return wxT("wxSCRIPT"); | |
358 | case wxSWISS: return wxT("wxSWISS"); | |
359 | case wxMODERN: return wxT("wxMODERN"); | |
360 | case wxTELETYPE: return wxT("wxTELETYPE"); | |
361 | default: return wxT("wxDEFAULT"); | |
0c5d3e1c VZ |
362 | } |
363 | } | |
364 | ||
365 | wxString wxFontBase::GetStyleString() const | |
366 | { | |
223d09f6 | 367 | wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") ); |
0c5d3e1c VZ |
368 | |
369 | switch ( GetStyle() ) | |
370 | { | |
223d09f6 KB |
371 | case wxNORMAL: return wxT("wxNORMAL"); |
372 | case wxSLANT: return wxT("wxSLANT"); | |
373 | case wxITALIC: return wxT("wxITALIC"); | |
374 | default: return wxT("wxDEFAULT"); | |
0c5d3e1c VZ |
375 | } |
376 | } | |
377 | ||
378 | wxString wxFontBase::GetWeightString() const | |
379 | { | |
223d09f6 | 380 | wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") ); |
0c5d3e1c VZ |
381 | |
382 | switch ( GetWeight() ) | |
383 | { | |
223d09f6 KB |
384 | case wxNORMAL: return wxT("wxNORMAL"); |
385 | case wxBOLD: return wxT("wxBOLD"); | |
386 | case wxLIGHT: return wxT("wxLIGHT"); | |
387 | default: return wxT("wxDEFAULT"); | |
0c5d3e1c VZ |
388 | } |
389 | } | |
390 | ||
ff427585 | 391 | bool wxFontBase::SetFaceName(const wxString& facename) |
85ab460e | 392 | { |
ff427585 | 393 | #if wxUSE_FONTENUM |
85ab460e VZ |
394 | if (!wxFontEnumerator::IsValidFacename(facename)) |
395 | { | |
396 | UnRef(); // make Ok() return false | |
397 | return false; | |
398 | } | |
ff427585 VZ |
399 | #else // !wxUSE_FONTENUM |
400 | wxUnusedVar(facename); | |
401 | #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM | |
85ab460e VZ |
402 | |
403 | return true; | |
404 | } | |
405 | ||
406 | ||
30764ab5 VZ |
407 | // ---------------------------------------------------------------------------- |
408 | // wxNativeFontInfo | |
409 | // ---------------------------------------------------------------------------- | |
410 | ||
85ab460e | 411 | // Up to now, there are no native implementations of this function: |
ff427585 | 412 | void wxNativeFontInfo::SetFaceName(const wxArrayString& facenames) |
85ab460e | 413 | { |
ff427585 | 414 | #if wxUSE_FONTENUM |
85ab460e VZ |
415 | for (size_t i=0; i < facenames.GetCount(); i++) |
416 | { | |
417 | if (wxFontEnumerator::IsValidFacename(facenames[i])) | |
418 | { | |
419 | SetFaceName(facenames[i]); | |
420 | return; | |
421 | } | |
422 | } | |
423 | ||
424 | // set the first valid facename we can find on this system | |
425 | wxString validfacename = wxFontEnumerator::GetFacenames().Item(0); | |
426 | wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename.c_str()); | |
427 | SetFaceName(validfacename); | |
ff427585 VZ |
428 | #else // !wxUSE_FONTENUM |
429 | SetFaceName(facenames[0]); | |
430 | #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM | |
85ab460e VZ |
431 | } |
432 | ||
433 | ||
ab5fe833 VZ |
434 | #ifdef wxNO_NATIVE_FONTINFO |
435 | ||
30764ab5 VZ |
436 | // These are the generic forms of FromString()/ToString. |
437 | // | |
438 | // convert to/from the string representation: format is | |
09fcd889 | 439 | // version;pointsize;family;style;weight;underlined;facename;encoding |
30764ab5 VZ |
440 | |
441 | bool wxNativeFontInfo::FromString(const wxString& s) | |
442 | { | |
443 | long l; | |
444 | ||
445 | wxStringTokenizer tokenizer(s, _T(";")); | |
446 | ||
447 | wxString token = tokenizer.GetNextToken(); | |
09fcd889 VZ |
448 | // |
449 | // Ignore the version for now | |
450 | // | |
33ac7e6f | 451 | |
09fcd889 | 452 | token = tokenizer.GetNextToken(); |
30764ab5 | 453 | if ( !token.ToLong(&l) ) |
a62848fd | 454 | return false; |
30764ab5 VZ |
455 | pointSize = (int)l; |
456 | ||
457 | token = tokenizer.GetNextToken(); | |
458 | if ( !token.ToLong(&l) ) | |
a62848fd | 459 | return false; |
f7b301fa | 460 | family = (wxFontFamily)l; |
30764ab5 VZ |
461 | |
462 | token = tokenizer.GetNextToken(); | |
463 | if ( !token.ToLong(&l) ) | |
a62848fd | 464 | return false; |
ab5fe833 | 465 | style = (wxFontStyle)l; |
30764ab5 VZ |
466 | |
467 | token = tokenizer.GetNextToken(); | |
468 | if ( !token.ToLong(&l) ) | |
a62848fd | 469 | return false; |
ab5fe833 | 470 | weight = (wxFontWeight)l; |
30764ab5 VZ |
471 | |
472 | token = tokenizer.GetNextToken(); | |
473 | if ( !token.ToLong(&l) ) | |
a62848fd | 474 | return false; |
189e08b4 | 475 | underlined = l != 0; |
30764ab5 VZ |
476 | |
477 | faceName = tokenizer.GetNextToken(); | |
0a9f0ef7 JS |
478 | |
479 | #ifndef __WXMAC__ | |
30764ab5 | 480 | if( !faceName ) |
a62848fd | 481 | return false; |
0a9f0ef7 | 482 | #endif |
30764ab5 VZ |
483 | |
484 | token = tokenizer.GetNextToken(); | |
485 | if ( !token.ToLong(&l) ) | |
a62848fd | 486 | return false; |
30764ab5 VZ |
487 | encoding = (wxFontEncoding)l; |
488 | ||
a62848fd | 489 | return true; |
30764ab5 VZ |
490 | } |
491 | ||
492 | wxString wxNativeFontInfo::ToString() const | |
493 | { | |
494 | wxString s; | |
495 | ||
09fcd889 VZ |
496 | s.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"), |
497 | 0, // version | |
30764ab5 VZ |
498 | pointSize, |
499 | family, | |
ab5fe833 VZ |
500 | (int)style, |
501 | (int)weight, | |
30764ab5 VZ |
502 | underlined, |
503 | faceName.GetData(), | |
504 | (int)encoding); | |
505 | ||
506 | return s; | |
507 | } | |
508 | ||
ab5fe833 VZ |
509 | void wxNativeFontInfo::Init() |
510 | { | |
3bf5a59b | 511 | pointSize = 0; |
ab5fe833 VZ |
512 | family = wxFONTFAMILY_DEFAULT; |
513 | style = wxFONTSTYLE_NORMAL; | |
514 | weight = wxFONTWEIGHT_NORMAL; | |
a62848fd | 515 | underlined = false; |
ab5fe833 VZ |
516 | faceName.clear(); |
517 | encoding = wxFONTENCODING_DEFAULT; | |
518 | } | |
519 | ||
520 | int wxNativeFontInfo::GetPointSize() const | |
521 | { | |
522 | return pointSize; | |
523 | } | |
524 | ||
525 | wxFontStyle wxNativeFontInfo::GetStyle() const | |
526 | { | |
527 | return style; | |
528 | } | |
529 | ||
530 | wxFontWeight wxNativeFontInfo::GetWeight() const | |
531 | { | |
532 | return weight; | |
533 | } | |
534 | ||
535 | bool wxNativeFontInfo::GetUnderlined() const | |
536 | { | |
537 | return underlined; | |
538 | } | |
539 | ||
540 | wxString wxNativeFontInfo::GetFaceName() const | |
541 | { | |
542 | return faceName; | |
543 | } | |
544 | ||
7936354d VZ |
545 | wxFontFamily wxNativeFontInfo::GetFamily() const |
546 | { | |
547 | return family; | |
548 | } | |
549 | ||
ab5fe833 VZ |
550 | wxFontEncoding wxNativeFontInfo::GetEncoding() const |
551 | { | |
552 | return encoding; | |
553 | } | |
554 | ||
555 | void wxNativeFontInfo::SetPointSize(int pointsize) | |
556 | { | |
557 | pointSize = pointsize; | |
558 | } | |
559 | ||
560 | void wxNativeFontInfo::SetStyle(wxFontStyle style_) | |
561 | { | |
562 | style = style_; | |
563 | } | |
564 | ||
565 | void wxNativeFontInfo::SetWeight(wxFontWeight weight_) | |
566 | { | |
567 | weight = weight_; | |
568 | } | |
569 | ||
570 | void wxNativeFontInfo::SetUnderlined(bool underlined_) | |
571 | { | |
572 | underlined = underlined_; | |
573 | } | |
574 | ||
85ab460e | 575 | bool wxNativeFontInfo::SetFaceName(const wxString& facename_) |
ab5fe833 | 576 | { |
f7b301fa | 577 | faceName = facename_; |
85ab460e | 578 | return true; |
ab5fe833 VZ |
579 | } |
580 | ||
3f1d1373 | 581 | void wxNativeFontInfo::SetFamily(wxFontFamily family_) |
7936354d VZ |
582 | { |
583 | family = family_; | |
584 | } | |
585 | ||
ab5fe833 VZ |
586 | void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_) |
587 | { | |
588 | encoding = encoding_; | |
589 | } | |
590 | ||
7826e2dd | 591 | #endif // generic wxNativeFontInfo implementation |
30764ab5 | 592 | |
ab5fe833 VZ |
593 | // conversion to/from user-readable string: this is used in the generic |
594 | // versions and under MSW as well because there is no standard font description | |
595 | // format there anyhow (but there is a well-defined standard for X11 fonts used | |
596 | // by wxGTK and wxMotif) | |
597 | ||
0eb529d9 | 598 | #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__) |
ab5fe833 VZ |
599 | |
600 | wxString wxNativeFontInfo::ToUserString() const | |
601 | { | |
602 | wxString desc; | |
603 | ||
604 | // first put the adjectives, if any - this is English-centric, of course, | |
605 | // but what else can we do? | |
606 | if ( GetUnderlined() ) | |
607 | { | |
85ab460e | 608 | desc << _("underlined"); |
ab5fe833 VZ |
609 | } |
610 | ||
611 | switch ( GetWeight() ) | |
612 | { | |
613 | default: | |
614 | wxFAIL_MSG( _T("unknown font weight") ); | |
615 | // fall through | |
616 | ||
617 | case wxFONTWEIGHT_NORMAL: | |
618 | break; | |
619 | ||
620 | case wxFONTWEIGHT_LIGHT: | |
85ab460e | 621 | desc << _(" light"); |
ab5fe833 VZ |
622 | break; |
623 | ||
624 | case wxFONTWEIGHT_BOLD: | |
85ab460e | 625 | desc << _(" bold"); |
ab5fe833 VZ |
626 | break; |
627 | } | |
628 | ||
629 | switch ( GetStyle() ) | |
630 | { | |
631 | default: | |
632 | wxFAIL_MSG( _T("unknown font style") ); | |
633 | // fall through | |
634 | ||
635 | case wxFONTSTYLE_NORMAL: | |
636 | break; | |
637 | ||
638 | // we don't distinguish between the two for now anyhow... | |
639 | case wxFONTSTYLE_ITALIC: | |
640 | case wxFONTSTYLE_SLANT: | |
85ab460e | 641 | desc << _(" italic"); |
ab5fe833 VZ |
642 | break; |
643 | } | |
644 | ||
a9249b2e VZ |
645 | wxString face = GetFaceName(); |
646 | if ( !face.empty() ) | |
ab5fe833 | 647 | { |
a9249b2e | 648 | desc << _T(' ') << face; |
ab5fe833 VZ |
649 | } |
650 | ||
a9249b2e VZ |
651 | int size = GetPointSize(); |
652 | if ( size != wxNORMAL_FONT->GetPointSize() ) | |
ab5fe833 | 653 | { |
a9249b2e | 654 | desc << _T(' ') << size; |
ab5fe833 | 655 | } |
a9249b2e | 656 | |
e7e52b6d | 657 | #if wxUSE_FONTMAP |
a9249b2e VZ |
658 | wxFontEncoding enc = GetEncoding(); |
659 | if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM ) | |
660 | { | |
2a1f999f | 661 | desc << _T(' ') << wxFontMapper::GetEncodingName(enc); |
a9249b2e | 662 | } |
e7e52b6d | 663 | #endif // wxUSE_FONTMAP |
a9249b2e | 664 | |
85ab460e | 665 | return desc.Strip(wxString::both).MakeLower(); |
ab5fe833 VZ |
666 | } |
667 | ||
668 | bool wxNativeFontInfo::FromUserString(const wxString& s) | |
669 | { | |
670 | // reset to the default state | |
671 | Init(); | |
672 | ||
673 | // parse a more or less free form string | |
674 | // | |
675 | // TODO: we should handle at least the quoted facenames | |
676 | wxStringTokenizer tokenizer(s, _T(";, "), wxTOKEN_STRTOK); | |
677 | ||
678 | wxString face; | |
679 | unsigned long size; | |
77f15ffe VS |
680 | bool weightfound = false, pointsizefound = false; |
681 | #if wxUSE_FONTMAP | |
682 | bool encodingfound = false; | |
683 | #endif | |
ab5fe833 VZ |
684 | |
685 | while ( tokenizer.HasMoreTokens() ) | |
686 | { | |
687 | wxString token = tokenizer.GetNextToken(); | |
688 | ||
689 | // normalize it | |
a62848fd | 690 | token.Trim(true).Trim(false).MakeLower(); |
ab5fe833 VZ |
691 | |
692 | // look for the known tokens | |
693 | if ( token == _T("underlined") || token == _("underlined") ) | |
694 | { | |
a62848fd | 695 | SetUnderlined(true); |
ab5fe833 VZ |
696 | } |
697 | else if ( token == _T("light") || token == _("light") ) | |
698 | { | |
699 | SetWeight(wxFONTWEIGHT_LIGHT); | |
85ab460e | 700 | weightfound = true; |
ab5fe833 VZ |
701 | } |
702 | else if ( token == _T("bold") || token == _("bold") ) | |
703 | { | |
704 | SetWeight(wxFONTWEIGHT_BOLD); | |
85ab460e | 705 | weightfound = true; |
ab5fe833 VZ |
706 | } |
707 | else if ( token == _T("italic") || token == _("italic") ) | |
708 | { | |
709 | SetStyle(wxFONTSTYLE_ITALIC); | |
710 | } | |
711 | else if ( token.ToULong(&size) ) | |
712 | { | |
a9249b2e | 713 | SetPointSize(size); |
85ab460e | 714 | pointsizefound = true; |
ab5fe833 | 715 | } |
85ab460e VZ |
716 | else |
717 | { | |
e7e52b6d | 718 | #if wxUSE_FONTMAP |
85ab460e VZ |
719 | // try to interpret this as an encoding |
720 | wxFontEncoding encoding = wxFontMapper::Get()->CharsetToEncoding(token, false); | |
721 | if ( encoding != wxFONTENCODING_DEFAULT && | |
722 | encoding != wxFONTENCODING_SYSTEM ) // returned when the recognition failed | |
ab5fe833 VZ |
723 | { |
724 | SetEncoding(encoding); | |
85ab460e | 725 | encodingfound = true; |
ab5fe833 | 726 | } |
85ab460e | 727 | else |
ab5fe833 | 728 | { |
85ab460e VZ |
729 | #endif // wxUSE_FONTMAP |
730 | ||
731 | // assume it is the face name | |
ab5fe833 VZ |
732 | if ( !face.empty() ) |
733 | { | |
734 | face += _T(' '); | |
735 | } | |
736 | ||
737 | face += token; | |
738 | ||
739 | // skip the code which resets face below | |
740 | continue; | |
85ab460e VZ |
741 | |
742 | #if wxUSE_FONTMAP | |
743 | } | |
744 | #endif // wxUSE_FONTMAP | |
ab5fe833 VZ |
745 | } |
746 | ||
747 | // if we had had the facename, we shouldn't continue appending tokens | |
748 | // to it (i.e. "foo bold bar" shouldn't result in the facename "foo | |
749 | // bar") | |
750 | if ( !face.empty() ) | |
751 | { | |
85ab460e VZ |
752 | // NB: the check on the facename is implemented in wxFontBase::SetFaceName |
753 | // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely | |
754 | // call here wxFontEnumerator::IsValidFacename | |
ff427585 VZ |
755 | if ( |
756 | #if wxUSE_FONTENUM | |
757 | !wxFontEnumerator::IsValidFacename(face) || | |
758 | #endif // wxUSE_FONTENUM | |
759 | !SetFaceName(face) ) | |
760 | { | |
85ab460e | 761 | SetFaceName(wxNORMAL_FONT->GetFaceName()); |
ff427585 VZ |
762 | } |
763 | ||
ab5fe833 VZ |
764 | face.clear(); |
765 | } | |
766 | } | |
767 | ||
768 | // we might not have flushed it inside the loop | |
769 | if ( !face.empty() ) | |
770 | { | |
85ab460e VZ |
771 | // NB: the check on the facename is implemented in wxFontBase::SetFaceName |
772 | // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely | |
773 | // call here wxFontEnumerator::IsValidFacename | |
ff427585 VZ |
774 | if ( |
775 | #if wxUSE_FONTENUM | |
776 | !wxFontEnumerator::IsValidFacename(face) || | |
777 | #endif // wxUSE_FONTENUM | |
778 | !SetFaceName(face) ) | |
779 | { | |
780 | SetFaceName(wxNORMAL_FONT->GetFaceName()); | |
781 | } | |
ab5fe833 VZ |
782 | } |
783 | ||
85ab460e VZ |
784 | // set point size to default value if size was not given |
785 | if ( !pointsizefound ) | |
786 | SetPointSize(wxNORMAL_FONT->GetPointSize()); | |
787 | ||
788 | // set font weight to default value if weight was not given | |
789 | if ( !weightfound ) | |
790 | SetWeight(wxFONTWEIGHT_NORMAL); | |
791 | ||
792 | #if wxUSE_FONTMAP | |
793 | // set font encoding to default value if encoding was not given | |
794 | if ( !encodingfound ) | |
795 | SetEncoding(wxFONTENCODING_SYSTEM); | |
796 | #endif // wxUSE_FONTMAP | |
797 | ||
a62848fd | 798 | return true; |
ab5fe833 VZ |
799 | } |
800 | ||
0eb529d9 | 801 | #endif // generic or wxMSW or wxOS2 |
fc9361e3 VZ |
802 | |
803 | ||
804 | // wxFont <-> wxString utilities, used by wxConfig | |
805 | wxString wxToString(const wxFontBase& font) | |
806 | { | |
807 | return font.IsOk() ? font.GetNativeFontInfoDesc() | |
808 | : wxString(); | |
809 | } | |
810 | ||
811 | bool wxFromString(const wxString& str, wxFontBase *font) | |
812 | { | |
813 | wxCHECK_MSG( font, false, _T("NULL output parameter") ); | |
814 | ||
815 | if ( str.empty() ) | |
816 | { | |
817 | *font = wxNullFont; | |
818 | return true; | |
819 | } | |
820 | ||
821 | return font->SetNativeFontInfo(str); | |
822 | } | |
823 | ||
824 |