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