]>
Commit | Line | Data |
---|---|---|
0e320a79 DW |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: font.cpp | |
3 | // Purpose: wxFont class | |
21802234 | 4 | // Author: David Webster |
0e320a79 | 5 | // Modified by: |
21802234 | 6 | // Created: 10/06/99 |
0e320a79 | 7 | // RCS-ID: $Id$ |
21802234 DW |
8 | // Copyright: (c) David Webster |
9 | // Licence: wxWindows licence | |
0e320a79 DW |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
21802234 DW |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
7e99520b | 20 | #include <malloc.h> |
21802234 DW |
21 | // For compilers that support precompilation, includes "wx.h". |
22 | #include "wx/wxprec.h" | |
0e320a79 | 23 | |
21802234 DW |
24 | #ifndef WX_PRECOMP |
25 | #include <stdio.h> | |
26 | #include "wx/setup.h" | |
27 | #include "wx/list.h" | |
28 | #include "wx/utils.h" | |
29 | #include "wx/app.h" | |
30 | #include "wx/font.h" | |
31 | #endif // WX_PRECOMP | |
32 | ||
33 | #include "wx/os2/private.h" | |
0e320a79 | 34 | |
cc95f4f9 DW |
35 | #include "wx/fontutil.h" |
36 | #include "wx/fontmap.h" | |
37 | ||
38 | #include "wx/tokenzr.h" | |
21802234 | 39 | |
cc95f4f9 | 40 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) |
0e320a79 | 41 | |
21802234 DW |
42 | // ---------------------------------------------------------------------------- |
43 | // wxFontRefData - the internal description of the font | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
46 | class WXDLLEXPORT wxFontRefData: public wxGDIRefData | |
0e320a79 | 47 | { |
21802234 DW |
48 | public: |
49 | wxFontRefData() | |
50 | { | |
cc95f4f9 | 51 | Init(-1, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, FALSE, |
21802234 DW |
52 | "", wxFONTENCODING_DEFAULT); |
53 | } | |
54 | ||
e99762c0 DW |
55 | wxFontRefData( int nSize |
56 | ,int nFamily | |
57 | ,int nStyle | |
58 | ,int nWeight | |
59 | ,bool bUnderlined | |
60 | ,const wxString& sFaceName | |
61 | ,wxFontEncoding vEncoding | |
62 | ) | |
21802234 | 63 | { |
cc95f4f9 DW |
64 | Init( nSize |
65 | ,nFamily | |
66 | ,nStyle | |
67 | ,nWeight | |
68 | ,bUnderlined | |
69 | ,sFaceName | |
70 | ,vEncoding | |
71 | ); | |
72 | } | |
73 | ||
74 | wxFontRefData( const wxNativeFontInfo& rInfo | |
75 | ,WXHFONT hFont = 0 | |
76 | ,WXHANDLE hPS = 0 | |
77 | ) | |
78 | { | |
79 | Init( rInfo | |
80 | ,hFont | |
81 | ,hPS | |
82 | ); | |
83 | } | |
84 | ||
85 | wxFontRefData(const wxFontRefData& rData) | |
86 | { | |
87 | Init( rData.m_nPointSize | |
88 | ,rData.m_nFamily | |
89 | ,rData.m_nStyle | |
90 | ,rData.m_nWeight | |
91 | ,rData.m_bUnderlined | |
92 | ,rData.m_sFaceName | |
93 | ,rData.m_vEncoding | |
94 | ); | |
95 | m_nFontId = rData.m_nFontId; | |
21802234 | 96 | } |
0e320a79 | 97 | |
21802234 DW |
98 | virtual ~wxFontRefData(); |
99 | ||
cc95f4f9 DW |
100 | // |
101 | // Operations | |
102 | // | |
103 | bool Alloc(wxFont* pFont); | |
104 | void Free(void); | |
105 | ||
106 | // | |
107 | // All wxFont accessors | |
108 | // | |
109 | inline int GetPointSize(void) const | |
110 | { | |
111 | return m_bNativeFontInfoOk ? m_vNativeFontInfo.GetPointSize() | |
112 | : m_nPointSize; | |
113 | } | |
114 | ||
115 | inline int GetFamily(void) const | |
116 | { | |
117 | return m_nFamily; | |
118 | } | |
119 | ||
120 | inline int GetStyle(void) const | |
121 | { | |
122 | return m_bNativeFontInfoOk ? m_vNativeFontInfo.GetStyle() | |
123 | : m_nStyle; | |
124 | } | |
125 | ||
126 | inline int GetWeight(void) const | |
127 | { | |
128 | return m_bNativeFontInfoOk ? m_vNativeFontInfo.GetWeight() | |
129 | : m_nWeight; | |
130 | } | |
131 | ||
132 | inline bool GetUnderlined(void) const | |
133 | { | |
134 | return m_bNativeFontInfoOk ? m_vNativeFontInfo.GetUnderlined() | |
135 | : m_bUnderlined; | |
136 | } | |
137 | ||
138 | inline wxString GetFaceName(void) const | |
139 | { | |
140 | wxString sFaceName; | |
141 | ||
142 | if (m_bNativeFontInfoOk) | |
143 | sFaceName = m_vNativeFontInfo.GetFaceName(); | |
144 | else | |
145 | sFaceName = m_sFaceName; | |
146 | ||
147 | return sFaceName; | |
148 | } | |
149 | ||
150 | inline wxFontEncoding GetEncoding(void) const | |
151 | { | |
152 | return m_bNativeFontInfoOk ? m_vNativeFontInfo.GetEncoding() | |
153 | : m_vEncoding; | |
154 | } | |
155 | ||
156 | inline WXHFONT GetHFONT(void) const { return m_hFont; } | |
157 | inline HPS GetPS(void) const { return m_hPS; } | |
158 | inline PFONTMETRICS GetFM(void) const { return m_pFM; } | |
159 | inline int GetNumFonts(void) const { return m_nNumFonts; } | |
160 | ||
161 | // ... and setters | |
162 | inline void SetPointSize(int nPointSize) | |
163 | { | |
164 | if (m_bNativeFontInfoOk) | |
165 | m_vNativeFontInfo.SetPointSize(nPointSize); | |
166 | else | |
167 | m_nPointSize = nPointSize; | |
168 | } | |
169 | ||
170 | inline void SetFamily(int nFamily) | |
171 | { | |
172 | m_nFamily = nFamily; | |
173 | } | |
174 | ||
175 | inline void SetStyle(int nStyle) | |
176 | { | |
177 | if (m_bNativeFontInfoOk) | |
178 | m_vNativeFontInfo.SetStyle((wxFontStyle)nStyle); | |
179 | else | |
180 | m_nStyle = nStyle; | |
181 | } | |
182 | ||
183 | inline void SetWeight(int nWeight) | |
184 | { | |
185 | if (m_bNativeFontInfoOk) | |
186 | m_vNativeFontInfo.SetWeight((wxFontWeight)nWeight); | |
187 | else | |
188 | m_nWeight = nWeight; | |
189 | } | |
190 | ||
191 | inline void SetFaceName(const wxString& sFaceName) | |
192 | { | |
193 | if (m_bNativeFontInfoOk) | |
194 | m_vNativeFontInfo.SetFaceName(sFaceName); | |
195 | else | |
196 | m_sFaceName = sFaceName; | |
197 | } | |
198 | ||
199 | inline void SetUnderlined(bool bUnderlined) | |
200 | { | |
201 | if (m_bNativeFontInfoOk) | |
202 | m_vNativeFontInfo.SetUnderlined(bUnderlined); | |
203 | else | |
204 | m_bUnderlined = bUnderlined; | |
205 | } | |
206 | ||
207 | inline void SetEncoding(wxFontEncoding vEncoding) | |
208 | { | |
209 | if (m_bNativeFontInfoOk) | |
210 | m_vNativeFontInfo.SetEncoding(vEncoding); | |
211 | else | |
212 | m_vEncoding = vEncoding; | |
213 | } | |
214 | ||
215 | inline void SetPS(HPS hPS) | |
216 | { | |
217 | m_hPS = hPS; | |
218 | } | |
219 | ||
220 | inline void SetFM(PFONTMETRICS pFM) | |
221 | { | |
222 | m_pFM = pFM; | |
223 | } | |
224 | ||
225 | inline void SetNumFonts(int nNumFonts) | |
226 | { | |
227 | m_nNumFonts = nNumFonts; | |
228 | } | |
229 | ||
230 | // | |
231 | // Native font info tests | |
232 | // | |
233 | bool HasNativeFontInfo() const { return m_bNativeFontInfoOk; } | |
234 | ||
235 | const wxNativeFontInfo& GetNativeFontInfo() const | |
236 | { return m_vNativeFontInfo; } | |
237 | ||
21802234 | 238 | protected: |
cc95f4f9 DW |
239 | // |
240 | // Common part of all ctors | |
241 | // | |
e99762c0 DW |
242 | void Init( int nSize |
243 | ,int nFamily | |
244 | ,int nStyle | |
245 | ,int nWeight | |
246 | ,bool bUnderlined | |
247 | ,const wxString& rsFaceName | |
248 | ,wxFontEncoding vEncoding | |
249 | ); | |
250 | ||
cc95f4f9 DW |
251 | void Init( const wxNativeFontInfo& rInfo |
252 | ,WXHFONT hFont = 0 | |
253 | ,WXHANDLE hPS = 0 | |
254 | ); | |
e99762c0 | 255 | // |
21802234 DW |
256 | // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE |
257 | // DELETED by destructor | |
e99762c0 DW |
258 | // |
259 | bool m_bTemporary; | |
260 | int m_nFontId; | |
261 | ||
262 | // | |
263 | // Font characterstics | |
264 | // | |
265 | int m_nPointSize; | |
266 | int m_nFamily; | |
267 | int m_nStyle; | |
268 | int m_nWeight; | |
269 | bool m_bUnderlined; | |
270 | wxString m_sFaceName; | |
271 | wxFontEncoding m_vEncoding; | |
cc95f4f9 DW |
272 | WXHFONT m_hFont; |
273 | ||
274 | // | |
275 | // Native font info | |
276 | // | |
277 | wxNativeFontInfo m_vNativeFontInfo; | |
278 | bool m_bNativeFontInfoOk; | |
e99762c0 DW |
279 | |
280 | // | |
281 | // Some PM specific stuff | |
282 | // | |
cc95f4f9 DW |
283 | PFONTMETRICS m_pFM; // array of FONTMETRICS structs |
284 | int m_nNumFonts; // number of fonts in array | |
285 | HPS m_hPS; // PS handle this font belongs to | |
286 | FATTRS m_vFattrs; // Current fattrs struct | |
287 | FACENAMEDESC m_vFname; // Current facename struct | |
288 | bool m_bInternalPS; // Internally generated PS? | |
e99762c0 | 289 | }; // end of CLASS wxFontRefData |
21802234 DW |
290 | |
291 | // ============================================================================ | |
292 | // implementation | |
293 | // ============================================================================ | |
294 | ||
295 | // ---------------------------------------------------------------------------- | |
296 | // wxFontRefData | |
297 | // ---------------------------------------------------------------------------- | |
298 | ||
e99762c0 DW |
299 | void wxFontRefData::Init( |
300 | int nPointSize | |
301 | , int nFamily | |
302 | , int nStyle | |
303 | , int nWeight | |
304 | , bool bUnderlined | |
305 | , const wxString& rsFaceName | |
306 | , wxFontEncoding vEncoding | |
307 | ) | |
0e320a79 | 308 | { |
e99762c0 DW |
309 | m_nStyle = nStyle; |
310 | m_nPointSize = nPointSize; | |
311 | m_nFamily = nFamily; | |
312 | m_nStyle = nStyle; | |
313 | m_nWeight = nWeight; | |
314 | m_bUnderlined = bUnderlined; | |
315 | m_sFaceName = rsFaceName; | |
316 | m_vEncoding = vEncoding; | |
cc95f4f9 DW |
317 | m_hFont = 0; |
318 | ||
319 | m_bNativeFontInfoOk = FALSE; | |
320 | ||
e99762c0 DW |
321 | m_nFontId = 0; |
322 | m_bTemporary = FALSE; | |
e99762c0 DW |
323 | m_pFM = (PFONTMETRICS)NULL; |
324 | m_hPS = NULLHANDLE; | |
325 | m_nNumFonts = 0; | |
326 | } // end of wxFontRefData::Init | |
0e320a79 | 327 | |
cc95f4f9 DW |
328 | void wxFontRefData::Init( |
329 | const wxNativeFontInfo& rInfo | |
330 | , WXHFONT hFont //this is the FontId -- functions as the hFont for OS/2 | |
331 | , WXHANDLE hPS // Presentation Space we are using | |
332 | ) | |
333 | { | |
334 | // | |
335 | // hFont may be zero, or it be passed in case we really want to | |
336 | // use the exact font created in the underlying system | |
337 | // (for example where we can't guarantee conversion from HFONT | |
338 | // to LOGFONT back to HFONT) | |
339 | // | |
340 | m_hFont = hFont; | |
341 | m_nFontId = (int)hFont; | |
342 | ||
343 | m_bNativeFontInfoOk = TRUE; | |
344 | m_vNativeFontInfo = rInfo; | |
345 | ||
346 | if (m_hPS == NULLHANDLE) | |
347 | { | |
348 | m_hPS = ::WinGetPS(HWND_DESKTOP); | |
349 | m_bInternalPS; | |
350 | } | |
351 | else | |
352 | m_hPS = (HPS)hPS; | |
353 | } | |
354 | ||
0e320a79 | 355 | wxFontRefData::~wxFontRefData() |
cc95f4f9 DW |
356 | { |
357 | Free(); | |
358 | } | |
359 | ||
360 | bool wxFontRefData::Alloc( | |
361 | wxFont* pFont | |
362 | ) | |
363 | { | |
364 | wxString sFaceName; | |
365 | long flId; | |
366 | ||
367 | if (!m_bNativeFontInfoOk) | |
368 | { | |
369 | wxFillLogFont( &m_vNativeFontInfo.fa | |
370 | ,&m_vNativeFontInfo.fn | |
371 | ,m_hPS | |
372 | ,&flId | |
373 | ,sFaceName | |
374 | ,pFont | |
375 | ); | |
376 | m_bNativeFontInfoOk = TRUE; | |
377 | } | |
378 | ||
379 | if(::GpiCreateLogFont( m_hPS | |
380 | ,NULL | |
381 | ,flId | |
382 | ,&m_vNativeFontInfo.fa | |
383 | ) != GPI_ERROR) | |
384 | m_hFont = (WXHFONT)1; | |
385 | ||
386 | // | |
387 | // We don't actuall keep the font around if using a temporary PS | |
388 | // | |
389 | if (m_bInternalPS) | |
390 | { | |
391 | if(m_hFont) | |
392 | ::GpiDeleteSetId( m_hPS | |
393 | ,flId | |
394 | ); | |
395 | ||
396 | ::WinReleasePS(m_hPS); | |
397 | } | |
398 | else | |
399 | // | |
400 | // Select the font into the Presentation space | |
401 | // | |
402 | ::GpiSetCharSet(m_hPS, flId); // sets font for presentation space | |
403 | if (!m_hFont) | |
404 | { | |
405 | wxLogLastError("CreateFont"); | |
406 | } | |
407 | ||
408 | // | |
409 | // Query for the actual metrics of the current font being used | |
410 | // | |
411 | ::GpiQueryFontMetrics(m_hPS, sizeof(FONTMETRICS), &m_vNativeFontInfo.fm); | |
412 | ||
413 | // | |
414 | // Set refData members with the results | |
415 | // | |
416 | m_hFont = (WXHFONT)m_nFontId; | |
417 | memcpy(&m_vFattrs, &m_vNativeFontInfo.fa, sizeof(m_vFattrs)); | |
418 | memcpy(&m_vFname, &m_vNativeFontInfo.fn, sizeof(m_vFname)); | |
419 | m_nPointSize = m_vNativeFontInfo.fm.lEmHeight; | |
420 | if (strcmp(m_vNativeFontInfo.fa.szFacename, "Times New Roman") == 0) | |
421 | m_nFamily = wxROMAN; | |
422 | else if (strcmp(m_vNativeFontInfo.fa.szFacename, "WarpSans") == 0) | |
423 | m_nFamily = wxSWISS; | |
424 | else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Script") == 0) | |
425 | m_nFamily = wxSCRIPT; | |
426 | else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Courier New") == 0) | |
427 | m_nFamily = wxMODERN; | |
428 | else | |
429 | m_nFamily = wxSWISS; | |
430 | ||
431 | if (m_vNativeFontInfo.fa.fsSelection & FATTR_SEL_ITALIC) | |
432 | m_nStyle = wxFONTSTYLE_ITALIC; | |
433 | else | |
434 | m_nStyle = wxFONTSTYLE_NORMAL; | |
435 | switch(m_vNativeFontInfo.fn.usWeightClass) | |
436 | { | |
437 | case FWEIGHT_DONT_CARE: | |
438 | m_nWeight = wxFONTWEIGHT_NORMAL; | |
439 | break; | |
440 | ||
441 | case FWEIGHT_NORMAL: | |
442 | m_nWeight = wxFONTWEIGHT_NORMAL; | |
443 | break; | |
444 | ||
445 | case FWEIGHT_LIGHT: | |
446 | m_nWeight = wxFONTWEIGHT_LIGHT; | |
447 | break; | |
448 | ||
449 | case FWEIGHT_BOLD: | |
450 | m_nWeight = wxFONTWEIGHT_BOLD; | |
451 | break; | |
452 | ||
453 | case FWEIGHT_ULTRA_BOLD: | |
454 | m_nWeight = wxFONTWEIGHT_MAX; | |
455 | break; | |
456 | ||
457 | default: | |
458 | m_nWeight = wxFONTWEIGHT_NORMAL; | |
459 | } | |
460 | m_bUnderlined = ((m_vNativeFontInfo.fa.fsSelection & FATTR_SEL_UNDERSCORE) != 0); | |
461 | m_sFaceName = m_vNativeFontInfo.fa.szFacename; | |
462 | m_vEncoding = wxGetFontEncFromCharSet(m_vNativeFontInfo.fa.usCodePage); | |
463 | return TRUE; | |
464 | } // end of wxFontRefData::Alloc | |
465 | ||
466 | void wxFontRefData::Free() | |
0e320a79 | 467 | { |
e99762c0 DW |
468 | if (m_pFM) |
469 | delete [] m_pFM; | |
470 | m_pFM = (PFONTMETRICS)NULL; | |
cc95f4f9 DW |
471 | |
472 | if ( m_hFont ) | |
473 | { | |
474 | if (!::GpiSetCharSet(m_hPS, LCID_DEFAULT)) | |
475 | { | |
476 | wxLogLastError(wxT("DeleteObject(font)")); | |
477 | } | |
478 | ::GpiDeleteSetId(m_hPS, 1L); /* delete the logical font */ | |
479 | m_nFontId = 0; | |
480 | m_hFont = 0; | |
481 | } | |
482 | if (m_bInternalPS) | |
483 | ::WinReleasePS(m_hPS); | |
484 | m_hPS = NULLHANDLE; | |
485 | } // end of wxFontRefData::Free | |
0e320a79 | 486 | |
21802234 | 487 | // ---------------------------------------------------------------------------- |
cc95f4f9 | 488 | // wxNativeFontInfo |
21802234 | 489 | // ---------------------------------------------------------------------------- |
0e320a79 | 490 | |
cc95f4f9 DW |
491 | void wxNativeFontInfo::Init() |
492 | { | |
493 | memset(&fa, '\0', sizeof(FATTRS)); | |
494 | } // end of wxNativeFontInfo::Init | |
495 | ||
496 | int wxNativeFontInfo::GetPointSize() const | |
497 | { | |
498 | return fm.lEmHeight; | |
499 | } // end of wxNativeFontInfo::GetPointSize | |
500 | ||
501 | wxFontStyle wxNativeFontInfo::GetStyle() const | |
502 | { | |
503 | return fa.fsSelection & FATTR_SEL_ITALIC ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL; | |
504 | } // end of wxNativeFontInfo::GetStyle | |
505 | ||
506 | wxFontWeight wxNativeFontInfo::GetWeight() const | |
507 | { | |
508 | switch(fn.usWeightClass) | |
19193a2c | 509 | { |
cc95f4f9 DW |
510 | case FWEIGHT_DONT_CARE: |
511 | return wxFONTWEIGHT_NORMAL; | |
512 | ||
513 | case FWEIGHT_NORMAL: | |
514 | return wxFONTWEIGHT_NORMAL; | |
19193a2c | 515 | |
cc95f4f9 DW |
516 | case FWEIGHT_LIGHT: |
517 | return wxFONTWEIGHT_LIGHT; | |
518 | ||
519 | case FWEIGHT_BOLD: | |
520 | return wxFONTWEIGHT_BOLD; | |
521 | ||
522 | case FWEIGHT_ULTRA_BOLD: | |
523 | return wxFONTWEIGHT_MAX; | |
19193a2c | 524 | } |
cc95f4f9 DW |
525 | return wxFONTWEIGHT_NORMAL; |
526 | } // end of wxNativeFontInfo::GetWeight | |
19193a2c | 527 | |
cc95f4f9 DW |
528 | bool wxNativeFontInfo::GetUnderlined() const |
529 | { | |
530 | return ((fa.fsSelection & FATTR_SEL_UNDERSCORE) != 0); | |
531 | } // end of wxNativeFontInfo::GetUnderlined | |
532 | ||
533 | wxString wxNativeFontInfo::GetFaceName() const | |
534 | { | |
535 | return fm.szFacename; | |
536 | } // end of wxNativeFontInfo::GetFaceName | |
537 | ||
538 | wxFontFamily wxNativeFontInfo::GetFamily() const | |
539 | { | |
540 | int nFamily; | |
541 | ||
542 | // | |
543 | // Extract family from facename | |
544 | // | |
545 | if (strcmp(fa.szFacename, "Times New Roman") == 0) | |
546 | nFamily = wxROMAN; | |
547 | else if (strcmp(fa.szFacename, "WarpSans") == 0) | |
548 | nFamily = wxSWISS; | |
549 | else if (strcmp(fa.szFacename, "Script") == 0) | |
550 | nFamily = wxSCRIPT; | |
551 | else if (strcmp(fa.szFacename, "Courier New") == 0) | |
552 | nFamily = wxMODERN; | |
553 | else | |
554 | nFamily = wxSWISS; | |
555 | return (wxFontFamily)nFamily; | |
556 | } // end of wxNativeFontInfo::GetFamily | |
557 | ||
558 | wxFontEncoding wxNativeFontInfo::GetEncoding() const | |
559 | { | |
560 | return wxGetFontEncFromCharSet(fa.usCodePage); | |
561 | } // end of wxNativeFontInfo::GetEncoding | |
562 | ||
563 | void wxNativeFontInfo::SetPointSize( | |
564 | int nPointsize | |
565 | ) | |
566 | { | |
567 | fm.lEmHeight = (LONG)nPointsize; | |
568 | } // end of wxNativeFontInfo::SetPointSize | |
569 | ||
570 | void wxNativeFontInfo::SetStyle( | |
571 | wxFontStyle eStyle | |
572 | ) | |
573 | { | |
574 | switch (eStyle) | |
575 | { | |
576 | default: | |
577 | wxFAIL_MSG( _T("unknown font style") ); | |
578 | // fall through | |
579 | ||
580 | case wxFONTSTYLE_NORMAL: | |
581 | break; | |
582 | ||
583 | case wxFONTSTYLE_ITALIC: | |
584 | case wxFONTSTYLE_SLANT: | |
585 | fa.fsSelection |= FATTR_SEL_ITALIC; | |
586 | break; | |
587 | } | |
588 | } // end of wxNativeFontInfo::SetStyle | |
589 | ||
590 | void wxNativeFontInfo::SetWeight( | |
591 | wxFontWeight eWeight | |
592 | ) | |
593 | { | |
594 | switch (eWeight) | |
595 | { | |
596 | default: | |
597 | wxFAIL_MSG( _T("unknown font weight") ); | |
598 | // fall through | |
599 | ||
600 | case wxFONTWEIGHT_NORMAL: | |
601 | fn.usWeightClass = FWEIGHT_NORMAL; | |
602 | break; | |
603 | ||
604 | case wxFONTWEIGHT_LIGHT: | |
605 | fn.usWeightClass = FWEIGHT_LIGHT; | |
606 | break; | |
607 | ||
608 | case wxFONTWEIGHT_BOLD: | |
609 | fn.usWeightClass = FWEIGHT_BOLD; | |
610 | break; | |
611 | } | |
612 | } // end of wxNativeFontInfo::SetWeight | |
613 | ||
614 | void wxNativeFontInfo::SetUnderlined( | |
615 | bool bUnderlined | |
616 | ) | |
617 | { | |
618 | if(bUnderlined) | |
619 | fa.fsSelection |= FATTR_SEL_UNDERSCORE; | |
620 | } // end of wxNativeFontInfo::SetUnderlined | |
621 | ||
622 | void wxNativeFontInfo::SetFaceName( | |
623 | wxString sFacename | |
624 | ) | |
625 | { | |
626 | wxStrncpy(fa.szFacename, sFacename, WXSIZEOF(fa.szFacename)); | |
627 | } // end of wxNativeFontInfo::SetFaceName | |
628 | ||
629 | void wxNativeFontInfo::SetFamily( | |
630 | wxFontFamily eFamily | |
631 | ) | |
632 | { | |
633 | wxString sFacename; | |
634 | ||
635 | switch (eFamily) | |
636 | { | |
637 | case wxSCRIPT: | |
638 | sFacename = _T("Script"); | |
639 | break; | |
640 | ||
641 | case wxDECORATIVE: | |
642 | sFacename = _T("Times New Roman"); | |
643 | break; | |
644 | ||
645 | case wxROMAN: | |
646 | sFacename = _T("Times New Roman"); | |
647 | break; | |
648 | ||
649 | case wxTELETYPE: | |
650 | case wxMODERN: | |
651 | sFacename = _T("Courier New"); | |
652 | break; | |
653 | ||
654 | case wxSWISS: | |
655 | sFacename = _T("WarpSans"); | |
656 | break; | |
657 | ||
658 | case wxDEFAULT: | |
659 | default: | |
660 | sFacename = _T("Helv"); | |
661 | } | |
662 | ||
663 | if (!wxStrlen(fa.szFacename) ) | |
664 | { | |
665 | SetFaceName(sFacename); | |
666 | } | |
667 | } // end of wxNativeFontInfo::SetFamily | |
668 | ||
669 | void wxNativeFontInfo::SetEncoding( | |
670 | wxFontEncoding eEncoding | |
671 | ) | |
672 | { | |
673 | wxNativeEncodingInfo vInfo; | |
674 | ||
675 | if ( !wxGetNativeFontEncoding( eEncoding | |
676 | ,&vInfo | |
677 | )) | |
678 | { | |
679 | #if wxUSE_FONTMAP | |
680 | if (wxTheFontMapper->GetAltForEncoding( eEncoding | |
681 | ,&vInfo | |
682 | )) | |
683 | { | |
684 | if (!vInfo.facename.empty()) | |
685 | { | |
686 | // | |
687 | // If we have this encoding only in some particular facename, use | |
688 | // the facename - it is better to show the correct characters in a | |
689 | // wrong facename than unreadable text in a correct one | |
690 | // | |
691 | SetFaceName(vInfo.facename); | |
692 | } | |
693 | } | |
694 | else | |
695 | #endif // wxUSE_FONTMAP | |
696 | { | |
697 | // unsupported encoding, replace with the default | |
698 | vInfo.charset = 850; | |
699 | } | |
700 | } | |
701 | fa.usCodePage = vInfo.charset; | |
702 | } // end of wxNativeFontInfo::SetFaceName | |
703 | ||
704 | bool wxNativeFontInfo::FromString( | |
705 | const wxString& rsStr | |
706 | ) | |
707 | { | |
708 | long lVal; | |
709 | ||
710 | wxStringTokenizer vTokenizer(rsStr, _T(";")); | |
711 | ||
712 | // | |
713 | // First the version | |
714 | // | |
715 | wxString sToken = vTokenizer.GetNextToken(); | |
716 | ||
717 | if (sToken != _T('0')) | |
718 | return FALSE; | |
719 | ||
720 | sToken = vTokenizer.GetNextToken(); | |
721 | if (!sToken.ToLong(&lVal)) | |
722 | return FALSE; | |
723 | fm.lEmHeight = lVal; | |
724 | ||
725 | sToken = vTokenizer.GetNextToken(); | |
726 | if (!sToken.ToLong(&lVal)) | |
727 | return FALSE; | |
728 | fa.lAveCharWidth = lVal; | |
729 | ||
730 | sToken = vTokenizer.GetNextToken(); | |
731 | if (!sToken.ToLong(&lVal)) | |
732 | return FALSE; | |
733 | fa.fsSelection = (USHORT)lVal; | |
734 | ||
735 | sToken = vTokenizer.GetNextToken(); | |
736 | if (!sToken.ToLong(&lVal)) | |
737 | return FALSE; | |
738 | fa.fsType = (USHORT)lVal; | |
739 | ||
740 | sToken = vTokenizer.GetNextToken(); | |
741 | if (!sToken.ToLong(&lVal)) | |
742 | return FALSE; | |
743 | fa.fsFontUse = (USHORT)lVal; | |
744 | ||
745 | sToken = vTokenizer.GetNextToken(); | |
746 | if (!sToken.ToLong(&lVal)) | |
747 | return FALSE; | |
748 | fa.idRegistry = (USHORT)lVal; | |
749 | ||
750 | sToken = vTokenizer.GetNextToken(); | |
751 | if (!sToken.ToLong(&lVal)) | |
752 | return FALSE; | |
753 | fa.usCodePage = (USHORT)lVal; | |
754 | ||
755 | sToken = vTokenizer.GetNextToken(); | |
756 | if (!sToken.ToLong(&lVal)) | |
757 | return FALSE; | |
758 | fa.lMatch = lVal; | |
759 | ||
760 | sToken = vTokenizer.GetNextToken(); | |
761 | if (!sToken.ToLong(&lVal)) | |
762 | return FALSE; | |
763 | fn.usWeightClass = (USHORT)lVal; | |
764 | ||
765 | sToken = vTokenizer.GetNextToken(); | |
766 | if(!sToken) | |
767 | return FALSE; | |
768 | wxStrcpy(fa.szFacename, sToken.c_str()); | |
769 | return TRUE; | |
770 | } // end of wxNativeFontInfo::FromString | |
771 | ||
772 | wxString wxNativeFontInfo::ToString() const | |
773 | { | |
774 | wxString sStr; | |
775 | ||
776 | sStr.Printf(_T("%d;%ld;%ld;%ld;%d;%d;%d;%d;%d;%ld;%d;%s"), | |
777 | 0, // version, in case we want to change the format later | |
778 | fm.lEmHeight, | |
779 | fa.lAveCharWidth, | |
780 | fa.lMaxBaselineExt, | |
781 | fa.fsSelection, | |
782 | fa.fsType, | |
783 | fa.fsFontUse, | |
784 | fa.idRegistry, | |
785 | fa.usCodePage, | |
786 | fa.lMatch, | |
787 | fn.usWeightClass, | |
788 | fa.szFacename); | |
789 | return sStr; | |
790 | } // end of wxNativeFontInfo::ToString | |
791 | ||
792 | // ---------------------------------------------------------------------------- | |
793 | // wxFont | |
794 | // ---------------------------------------------------------------------------- | |
19193a2c | 795 | |
21802234 | 796 | void wxFont::Init() |
0e320a79 | 797 | { |
cc95f4f9 DW |
798 | } // end of wxFont::Init |
799 | ||
800 | bool wxFont::Create( | |
801 | const wxNativeFontInfo& rInfo | |
802 | , WXHFONT hFont | |
803 | ) | |
804 | { | |
805 | UnRef(); | |
806 | m_refData = new wxFontRefData( rInfo | |
807 | ,hFont | |
808 | ); | |
809 | RealizeResource(); | |
810 | return TRUE; | |
811 | } // end of wxFont::Create | |
812 | ||
813 | wxFont::wxFont( | |
814 | const wxString& rsFontdesc | |
815 | ) | |
816 | { | |
817 | wxNativeFontInfo vInfo; | |
818 | ||
819 | if (vInfo.FromString(rsFontdesc)) | |
820 | (void)Create(vInfo); | |
821 | } // end of wxFont::wxFont | |
0e320a79 | 822 | |
e99762c0 DW |
823 | // ---------------------------------------------------------------------------- |
824 | // Constructor for a font. Note that the real construction is done | |
825 | // in wxDC::SetFont, when information is available about scaling etc. | |
826 | // ---------------------------------------------------------------------------- | |
827 | bool wxFont::Create( | |
828 | int nPointSize | |
829 | , int nFamily | |
830 | , int nStyle | |
831 | , int nWeight | |
832 | , bool bUnderlined | |
833 | , const wxString& rsFaceName | |
834 | , wxFontEncoding vEncoding | |
835 | ) | |
0e320a79 DW |
836 | { |
837 | UnRef(); | |
cc95f4f9 DW |
838 | |
839 | // | |
840 | // wxDEFAULT is a valid value for the font size too so we must treat it | |
841 | // specially here (otherwise the size would be 70 == wxDEFAULT value) | |
842 | // | |
843 | if (nPointSize == wxDEFAULT) | |
844 | { | |
845 | nPointSize = wxNORMAL_FONT->GetPointSize(); | |
846 | } | |
e99762c0 DW |
847 | m_refData = new wxFontRefData( nPointSize |
848 | ,nFamily | |
849 | ,nStyle | |
850 | ,nWeight | |
851 | ,bUnderlined | |
852 | ,rsFaceName | |
853 | ,vEncoding | |
854 | ); | |
0e320a79 | 855 | RealizeResource(); |
0e320a79 | 856 | return TRUE; |
e99762c0 | 857 | } // end of wxFont::Create |
0e320a79 DW |
858 | |
859 | wxFont::~wxFont() | |
860 | { | |
e99762c0 | 861 | } // end of wxFont::~wxFont |
0e320a79 | 862 | |
21802234 DW |
863 | // ---------------------------------------------------------------------------- |
864 | // real implementation | |
f6bcfd97 BP |
865 | // Boris' Kovalenko comments: |
866 | // Because OS/2 fonts are associated with PS we can not create the font | |
867 | // here, but we may check that font definition is true | |
21802234 DW |
868 | // ---------------------------------------------------------------------------- |
869 | ||
0e320a79 DW |
870 | bool wxFont::RealizeResource() |
871 | { | |
21802234 DW |
872 | if ( GetResourceHandle() ) |
873 | { | |
cc95f4f9 | 874 | return TRUE; |
21802234 | 875 | } |
cc95f4f9 | 876 | return M_FONTDATA->Alloc(this); |
e99762c0 | 877 | } // end of wxFont::RealizeResource |
21802234 | 878 | |
e99762c0 DW |
879 | bool wxFont::FreeResource( |
880 | bool bForce | |
881 | ) | |
21802234 | 882 | { |
e99762c0 | 883 | if (GetResourceHandle()) |
21802234 | 884 | { |
cc95f4f9 | 885 | M_FONTDATA->Free(); |
21802234 DW |
886 | return TRUE; |
887 | } | |
0e320a79 | 888 | return FALSE; |
e99762c0 | 889 | } // end of wxFont::FreeResource |
0e320a79 | 890 | |
5fd2b2c6 DW |
891 | WXHANDLE wxFont::GetResourceHandle() |
892 | { | |
893 | return GetHFONT(); | |
e99762c0 | 894 | } // end of wxFont::GetResourceHandle |
21802234 | 895 | |
cc95f4f9 DW |
896 | WXHFONT wxFont::GetHFONT() const |
897 | { | |
898 | return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0; | |
899 | } // end of wxFont::GetHFONT | |
900 | ||
21802234 DW |
901 | bool wxFont::IsFree() const |
902 | { | |
cc95f4f9 | 903 | return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0); |
e99762c0 | 904 | } // end of wxFont::IsFree |
21802234 | 905 | |
0e320a79 DW |
906 | void wxFont::Unshare() |
907 | { | |
21802234 DW |
908 | // Don't change shared data |
909 | if ( !m_refData ) | |
0e320a79 | 910 | { |
21802234 DW |
911 | m_refData = new wxFontRefData(); |
912 | } | |
0e320a79 DW |
913 | else |
914 | { | |
21802234 DW |
915 | wxFontRefData* ref = new wxFontRefData(*M_FONTDATA); |
916 | UnRef(); | |
917 | m_refData = ref; | |
918 | } | |
e99762c0 | 919 | } // end of wxFont::Unshare |
0e320a79 | 920 | |
21802234 DW |
921 | // ---------------------------------------------------------------------------- |
922 | // change font attribute: we recreate font when doing it | |
923 | // ---------------------------------------------------------------------------- | |
924 | ||
e99762c0 DW |
925 | void wxFont::SetPointSize( |
926 | int nPointSize | |
927 | ) | |
0e320a79 DW |
928 | { |
929 | Unshare(); | |
930 | ||
cc95f4f9 | 931 | M_FONTDATA->SetPointSize(nPointSize); |
0e320a79 DW |
932 | |
933 | RealizeResource(); | |
e99762c0 | 934 | } // end of wxFont::SetPointSize |
0e320a79 | 935 | |
e99762c0 DW |
936 | void wxFont::SetFamily( |
937 | int nFamily | |
938 | ) | |
0e320a79 DW |
939 | { |
940 | Unshare(); | |
941 | ||
cc95f4f9 | 942 | M_FONTDATA->SetFamily(nFamily); |
0e320a79 DW |
943 | |
944 | RealizeResource(); | |
e99762c0 | 945 | } // end of wxFont::SetFamily |
0e320a79 | 946 | |
e99762c0 DW |
947 | void wxFont::SetStyle( |
948 | int nStyle | |
949 | ) | |
0e320a79 DW |
950 | { |
951 | Unshare(); | |
952 | ||
cc95f4f9 | 953 | M_FONTDATA->SetStyle(nStyle); |
0e320a79 DW |
954 | |
955 | RealizeResource(); | |
e99762c0 | 956 | } // end of wxFont::SetStyle |
0e320a79 | 957 | |
e99762c0 DW |
958 | void wxFont::SetWeight( |
959 | int nWeight | |
960 | ) | |
0e320a79 DW |
961 | { |
962 | Unshare(); | |
963 | ||
cc95f4f9 | 964 | M_FONTDATA->SetWeight(nWeight); |
0e320a79 DW |
965 | |
966 | RealizeResource(); | |
e99762c0 | 967 | } // end of wxFont::SetWeight |
0e320a79 | 968 | |
e99762c0 DW |
969 | void wxFont::SetFaceName( |
970 | const wxString& rsFaceName | |
971 | ) | |
0e320a79 DW |
972 | { |
973 | Unshare(); | |
974 | ||
cc95f4f9 | 975 | M_FONTDATA->SetFaceName(rsFaceName); |
0e320a79 DW |
976 | |
977 | RealizeResource(); | |
e99762c0 | 978 | } // end of wxFont::SetFaceName |
0e320a79 | 979 | |
e99762c0 DW |
980 | void wxFont::SetUnderlined( |
981 | bool bUnderlined | |
982 | ) | |
0e320a79 DW |
983 | { |
984 | Unshare(); | |
985 | ||
cc95f4f9 | 986 | M_FONTDATA->SetUnderlined(bUnderlined); |
0e320a79 DW |
987 | |
988 | RealizeResource(); | |
e99762c0 | 989 | } // end of wxFont::SetUnderlined |
0e320a79 | 990 | |
e99762c0 DW |
991 | void wxFont::SetEncoding( |
992 | wxFontEncoding vEncoding | |
993 | ) | |
0e320a79 | 994 | { |
21802234 DW |
995 | Unshare(); |
996 | ||
cc95f4f9 | 997 | M_FONTDATA->SetEncoding(vEncoding); |
21802234 DW |
998 | |
999 | RealizeResource(); | |
e99762c0 DW |
1000 | } // end of wxFont::SetEncoding |
1001 | ||
cc95f4f9 DW |
1002 | void wxFont::SetNativeFontInfo( |
1003 | const wxNativeFontInfo& rInfo | |
e99762c0 DW |
1004 | ) |
1005 | { | |
1006 | Unshare(); | |
1007 | ||
cc95f4f9 | 1008 | FreeResource(); |
e99762c0 | 1009 | |
cc95f4f9 | 1010 | *M_FONTDATA = wxFontRefData(rInfo); |
e99762c0 | 1011 | |
cc95f4f9 DW |
1012 | RealizeResource(); |
1013 | } | |
0e320a79 | 1014 | |
21802234 DW |
1015 | // ---------------------------------------------------------------------------- |
1016 | // accessors | |
1017 | // ---------------------------------------------------------------------------- | |
1018 | ||
1019 | int wxFont::GetPointSize() const | |
0e320a79 | 1020 | { |
cc95f4f9 | 1021 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
7e99520b | 1022 | |
cc95f4f9 | 1023 | return M_FONTDATA->GetPointSize(); |
e99762c0 | 1024 | } // end of wxFont::GetPointSize |
0e320a79 | 1025 | |
21802234 | 1026 | int wxFont::GetFamily() const |
0e320a79 | 1027 | { |
cc95f4f9 | 1028 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
21802234 | 1029 | |
cc95f4f9 DW |
1030 | return M_FONTDATA->GetFamily(); |
1031 | } // end of wxFont::GetFamily | |
21802234 DW |
1032 | |
1033 | int wxFont::GetStyle() const | |
1034 | { | |
cc95f4f9 DW |
1035 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
1036 | ||
1037 | return M_FONTDATA->GetStyle(); | |
e99762c0 | 1038 | } // end of wxFont::GetStyle |
21802234 DW |
1039 | |
1040 | int wxFont::GetWeight() const | |
1041 | { | |
cc95f4f9 DW |
1042 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
1043 | ||
1044 | return M_FONTDATA->GetWeight(); | |
21802234 DW |
1045 | } |
1046 | ||
1047 | bool wxFont::GetUnderlined() const | |
1048 | { | |
cc95f4f9 DW |
1049 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); |
1050 | ||
1051 | return M_FONTDATA->GetUnderlined(); | |
e99762c0 | 1052 | } // end of wxFont::GetUnderlined |
21802234 DW |
1053 | |
1054 | wxString wxFont::GetFaceName() const | |
1055 | { | |
cc95f4f9 | 1056 | wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") ); |
e99762c0 | 1057 | |
cc95f4f9 | 1058 | return M_FONTDATA->GetFaceName(); |
e99762c0 | 1059 | } // end of wxFont::GetFaceName |
21802234 DW |
1060 | |
1061 | wxFontEncoding wxFont::GetEncoding() const | |
1062 | { | |
cc95f4f9 | 1063 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); |
e99762c0 | 1064 | |
cc95f4f9 DW |
1065 | return M_FONTDATA->GetEncoding(); |
1066 | } // end of wxFont::GetEncoding | |
0e320a79 | 1067 | |
cc95f4f9 DW |
1068 | wxNativeFontInfo* wxFont::GetNativeFontInfo() const |
1069 | { | |
1070 | if (M_FONTDATA->HasNativeFontInfo()) | |
1071 | return new wxNativeFontInfo(M_FONTDATA->GetNativeFontInfo()); | |
1072 | return 0; | |
1073 | } // end of wxFont::GetNativeFontInfo | |
1074 | ||
1075 | // | |
1076 | // Internal use only method to set the FONTMETRICS array | |
1077 | // | |
1078 | void wxFont::SetFM( | |
1079 | PFONTMETRICS pFM | |
1080 | , int nNumFonts | |
1081 | ) | |
1082 | { | |
1083 | M_FONTDATA->SetFM(pFM); | |
1084 | M_FONTDATA->SetNumFonts(nNumFonts); | |
1085 | } // end of wxFont::SetFM | |
e99762c0 DW |
1086 | |
1087 | ||
d0a6b279 DW |
1088 | void wxFont::SetPS( |
1089 | HPS hPS | |
1090 | ) | |
1091 | { | |
1092 | Unshare(); | |
1093 | ||
1094 | M_FONTDATA->SetPS(hPS); | |
1095 | ||
1096 | RealizeResource(); | |
1097 | } // end of wxFont::SetUnderlined | |
1098 |