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