Fixing font utilities
[wxWidgets.git] / src / os2 / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: font.cpp
3 // Purpose: wxFont class
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/06/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include <malloc.h>
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
23
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"
34
35 #include "wx/fontutil.h"
36 #include "wx/fontmap.h"
37
38 #include "wx/tokenzr.h"
39
40 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
41
42 // ----------------------------------------------------------------------------
43 // wxFontRefData - the internal description of the font
44 // ----------------------------------------------------------------------------
45
46 class WXDLLEXPORT wxFontRefData: public wxGDIRefData
47 {
48 public:
49 wxFontRefData()
50 {
51 Init(-1, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, FALSE,
52 "", wxFONTENCODING_DEFAULT);
53 }
54
55 wxFontRefData( int nSize
56 ,int nFamily
57 ,int nStyle
58 ,int nWeight
59 ,bool bUnderlined
60 ,const wxString& sFaceName
61 ,wxFontEncoding vEncoding
62 )
63 {
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;
96 }
97
98 virtual ~wxFontRefData();
99
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
238 protected:
239 //
240 // Common part of all ctors
241 //
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
251 void Init( const wxNativeFontInfo& rInfo
252 ,WXHFONT hFont = 0
253 ,WXHANDLE hPS = 0
254 );
255 //
256 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
257 // DELETED by destructor
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;
272 WXHFONT m_hFont;
273
274 //
275 // Native font info
276 //
277 wxNativeFontInfo m_vNativeFontInfo;
278 bool m_bNativeFontInfoOk;
279
280 //
281 // Some PM specific stuff
282 //
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?
289 }; // end of CLASS wxFontRefData
290
291 // ============================================================================
292 // implementation
293 // ============================================================================
294
295 // ----------------------------------------------------------------------------
296 // wxFontRefData
297 // ----------------------------------------------------------------------------
298
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 )
308 {
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;
317 m_hFont = 0;
318
319 m_bNativeFontInfoOk = FALSE;
320
321 m_nFontId = 0;
322 m_bTemporary = FALSE;
323 m_pFM = (PFONTMETRICS)NULL;
324 m_hPS = NULLHANDLE;
325 m_nNumFonts = 0;
326 } // end of wxFontRefData::Init
327
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
355 wxFontRefData::~wxFontRefData()
356 {
357 Free();
358 }
359
360 bool wxFontRefData::Alloc(
361 wxFont* pFont
362 )
363 {
364 wxString sFaceName;
365 long flId = m_hFont;
366 long lRc;
367 short nIndex = 0;
368 PFONTMETRICS pFM = NULL;
369
370 if (!m_bNativeFontInfoOk)
371 {
372 wxFillLogFont( &m_vNativeFontInfo.fa
373 ,&m_vNativeFontInfo.fn
374 ,&m_hPS
375 ,&m_bInternalPS
376 ,&flId
377 ,sFaceName
378 ,pFont
379 );
380 m_bNativeFontInfoOk = TRUE;
381 }
382
383 if((lRc = ::GpiCreateLogFont( m_hPS
384 ,NULL
385 ,flId
386 ,&m_vNativeFontInfo.fa
387 )) != GPI_ERROR)
388 {
389 m_hFont = (WXHFONT)flId;
390 m_nFontId = flId;
391 }
392 if (!m_hFont)
393 {
394 wxLogLastError("CreateFont");
395 }
396
397 ::GpiSetCharSet(m_hPS, flId); // sets font for presentation space
398 ::GpiQueryFontMetrics(m_hPS, sizeof(FONTMETRICS), &m_vNativeFontInfo.fm);
399
400 //
401 // Set refData members with the results
402 //
403 memcpy(&m_vFattrs, &m_vNativeFontInfo.fa, sizeof(m_vFattrs));
404 memcpy(&m_vFname, &m_vNativeFontInfo.fn, sizeof(m_vFname));
405 m_nPointSize = m_vNativeFontInfo.fm.lEmHeight;
406 if (strcmp(m_vNativeFontInfo.fa.szFacename, "Times New Roman") == 0)
407 m_nFamily = wxROMAN;
408 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Tms Rmn") == 0)
409 m_nFamily = wxSWISS;
410 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "WarpSans") == 0)
411 m_nFamily = wxSWISS;
412 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Helvitica") == 0)
413 m_nFamily = wxSWISS;
414 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Helv") == 0)
415 m_nFamily = wxSWISS;
416 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Script") == 0)
417 m_nFamily = wxSCRIPT;
418 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Courier New") == 0)
419 m_nFamily = wxTELETYPE;
420 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Courier") == 0)
421 m_nFamily = wxTELETYPE;
422 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "System VIO") == 0)
423 m_nFamily = wxMODERN;
424 else
425 m_nFamily = wxSWISS;
426
427 if (m_vNativeFontInfo.fa.fsSelection & FATTR_SEL_ITALIC)
428 m_nStyle = wxFONTSTYLE_ITALIC;
429 else
430 m_nStyle = wxFONTSTYLE_NORMAL;
431 switch(m_vNativeFontInfo.fn.usWeightClass)
432 {
433 case FWEIGHT_DONT_CARE:
434 m_nWeight = wxFONTWEIGHT_NORMAL;
435 break;
436
437 case FWEIGHT_NORMAL:
438 m_nWeight = wxFONTWEIGHT_NORMAL;
439 break;
440
441 case FWEIGHT_LIGHT:
442 m_nWeight = wxFONTWEIGHT_LIGHT;
443 break;
444
445 case FWEIGHT_BOLD:
446 m_nWeight = wxFONTWEIGHT_BOLD;
447 break;
448
449 case FWEIGHT_ULTRA_BOLD:
450 m_nWeight = wxFONTWEIGHT_MAX;
451 break;
452
453 default:
454 m_nWeight = wxFONTWEIGHT_NORMAL;
455 }
456 m_bUnderlined = ((m_vNativeFontInfo.fa.fsSelection & FATTR_SEL_UNDERSCORE) != 0);
457 m_sFaceName = m_vNativeFontInfo.fa.szFacename;
458 m_vEncoding = wxGetFontEncFromCharSet(m_vNativeFontInfo.fa.usCodePage);
459
460 //
461 // We don't actuall keep the font around if using a temporary PS
462 //
463 if (m_bInternalPS)
464 {
465 if(m_hFont)
466 ::GpiDeleteSetId( m_hPS
467 ,flId
468 );
469
470 ::WinReleasePS(m_hPS);
471 }
472 else
473 //
474 // Select the font into the Presentation space
475 //
476 ::GpiSetCharSet(m_hPS, flId); // sets font for presentation space
477 return TRUE;
478 } // end of wxFontRefData::Alloc
479
480 void wxFontRefData::Free()
481 {
482 if (m_pFM)
483 delete [] m_pFM;
484 m_pFM = (PFONTMETRICS)NULL;
485
486 if ( m_hFont )
487 {
488 if (!::GpiSetCharSet(m_hPS, LCID_DEFAULT))
489 {
490 wxLogLastError(wxT("DeleteObject(font)"));
491 }
492 ::GpiDeleteSetId(m_hPS, 1L); /* delete the logical font */
493 m_nFontId = 0;
494 m_hFont = 0;
495 }
496 if (m_bInternalPS)
497 ::WinReleasePS(m_hPS);
498 m_hPS = NULLHANDLE;
499 } // end of wxFontRefData::Free
500
501 // ----------------------------------------------------------------------------
502 // wxNativeFontInfo
503 // ----------------------------------------------------------------------------
504
505 void wxNativeFontInfo::Init()
506 {
507 memset(&fa, '\0', sizeof(FATTRS));
508 } // end of wxNativeFontInfo::Init
509
510 int wxNativeFontInfo::GetPointSize() const
511 {
512 return fm.lEmHeight;
513 } // end of wxNativeFontInfo::GetPointSize
514
515 wxFontStyle wxNativeFontInfo::GetStyle() const
516 {
517 return fa.fsSelection & FATTR_SEL_ITALIC ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL;
518 } // end of wxNativeFontInfo::GetStyle
519
520 wxFontWeight wxNativeFontInfo::GetWeight() const
521 {
522 switch(fn.usWeightClass)
523 {
524 case FWEIGHT_DONT_CARE:
525 return wxFONTWEIGHT_NORMAL;
526
527 case FWEIGHT_NORMAL:
528 return wxFONTWEIGHT_NORMAL;
529
530 case FWEIGHT_LIGHT:
531 return wxFONTWEIGHT_LIGHT;
532
533 case FWEIGHT_BOLD:
534 return wxFONTWEIGHT_BOLD;
535
536 case FWEIGHT_ULTRA_BOLD:
537 return wxFONTWEIGHT_MAX;
538 }
539 return wxFONTWEIGHT_NORMAL;
540 } // end of wxNativeFontInfo::GetWeight
541
542 bool wxNativeFontInfo::GetUnderlined() const
543 {
544 return ((fa.fsSelection & FATTR_SEL_UNDERSCORE) != 0);
545 } // end of wxNativeFontInfo::GetUnderlined
546
547 wxString wxNativeFontInfo::GetFaceName() const
548 {
549 return fm.szFacename;
550 } // end of wxNativeFontInfo::GetFaceName
551
552 wxFontFamily wxNativeFontInfo::GetFamily() const
553 {
554 int nFamily;
555
556 //
557 // Extract family from facename
558 //
559 if (strcmp(fa.szFacename, "Times New Roman") == 0)
560 nFamily = wxROMAN;
561 else if (strcmp(fa.szFacename, "WarpSans") == 0)
562 nFamily = wxSWISS;
563 else if (strcmp(fa.szFacename, "Script") == 0)
564 nFamily = wxSCRIPT;
565 else if (strcmp(fa.szFacename, "Courier New") == 0)
566 nFamily = wxMODERN;
567 else
568 nFamily = wxSWISS;
569 return (wxFontFamily)nFamily;
570 } // end of wxNativeFontInfo::GetFamily
571
572 wxFontEncoding wxNativeFontInfo::GetEncoding() const
573 {
574 return wxGetFontEncFromCharSet(fa.usCodePage);
575 } // end of wxNativeFontInfo::GetEncoding
576
577 void wxNativeFontInfo::SetPointSize(
578 int nPointsize
579 )
580 {
581 fm.lEmHeight = (LONG)nPointsize;
582 } // end of wxNativeFontInfo::SetPointSize
583
584 void wxNativeFontInfo::SetStyle(
585 wxFontStyle eStyle
586 )
587 {
588 switch (eStyle)
589 {
590 default:
591 wxFAIL_MSG( _T("unknown font style") );
592 // fall through
593
594 case wxFONTSTYLE_NORMAL:
595 break;
596
597 case wxFONTSTYLE_ITALIC:
598 case wxFONTSTYLE_SLANT:
599 fa.fsSelection |= FATTR_SEL_ITALIC;
600 break;
601 }
602 } // end of wxNativeFontInfo::SetStyle
603
604 void wxNativeFontInfo::SetWeight(
605 wxFontWeight eWeight
606 )
607 {
608 switch (eWeight)
609 {
610 default:
611 wxFAIL_MSG( _T("unknown font weight") );
612 // fall through
613
614 case wxFONTWEIGHT_NORMAL:
615 fn.usWeightClass = FWEIGHT_NORMAL;
616 break;
617
618 case wxFONTWEIGHT_LIGHT:
619 fn.usWeightClass = FWEIGHT_LIGHT;
620 break;
621
622 case wxFONTWEIGHT_BOLD:
623 fn.usWeightClass = FWEIGHT_BOLD;
624 break;
625 }
626 } // end of wxNativeFontInfo::SetWeight
627
628 void wxNativeFontInfo::SetUnderlined(
629 bool bUnderlined
630 )
631 {
632 if(bUnderlined)
633 fa.fsSelection |= FATTR_SEL_UNDERSCORE;
634 } // end of wxNativeFontInfo::SetUnderlined
635
636 void wxNativeFontInfo::SetFaceName(
637 wxString sFacename
638 )
639 {
640 wxStrncpy(fa.szFacename, sFacename, WXSIZEOF(fa.szFacename));
641 } // end of wxNativeFontInfo::SetFaceName
642
643 void wxNativeFontInfo::SetFamily(
644 wxFontFamily eFamily
645 )
646 {
647 wxString sFacename;
648
649 switch (eFamily)
650 {
651 case wxSCRIPT:
652 sFacename = _T("Script");
653 break;
654
655 case wxDECORATIVE:
656 sFacename = _T("Times New Roman");
657 break;
658
659 case wxROMAN:
660 sFacename = _T("Times New Roman");
661 break;
662
663 case wxTELETYPE:
664 case wxMODERN:
665 sFacename = _T("Courier New");
666 break;
667
668 case wxSWISS:
669 sFacename = _T("WarpSans");
670 break;
671
672 case wxDEFAULT:
673 default:
674 sFacename = _T("Helv");
675 }
676
677 if (!wxStrlen(fa.szFacename) )
678 {
679 SetFaceName(sFacename);
680 }
681 } // end of wxNativeFontInfo::SetFamily
682
683 void wxNativeFontInfo::SetEncoding(
684 wxFontEncoding eEncoding
685 )
686 {
687 wxNativeEncodingInfo vInfo;
688
689 if ( !wxGetNativeFontEncoding( eEncoding
690 ,&vInfo
691 ))
692 {
693 #if wxUSE_FONTMAP
694 if (wxTheFontMapper->GetAltForEncoding( eEncoding
695 ,&vInfo
696 ))
697 {
698 if (!vInfo.facename.empty())
699 {
700 //
701 // If we have this encoding only in some particular facename, use
702 // the facename - it is better to show the correct characters in a
703 // wrong facename than unreadable text in a correct one
704 //
705 SetFaceName(vInfo.facename);
706 }
707 }
708 else
709 #endif // wxUSE_FONTMAP
710 {
711 // unsupported encoding, replace with the default
712 vInfo.charset = 850;
713 }
714 }
715 fa.usCodePage = vInfo.charset;
716 } // end of wxNativeFontInfo::SetFaceName
717
718 bool wxNativeFontInfo::FromString(
719 const wxString& rsStr
720 )
721 {
722 long lVal;
723
724 wxStringTokenizer vTokenizer(rsStr, _T(";"));
725
726 //
727 // First the version
728 //
729 wxString sToken = vTokenizer.GetNextToken();
730
731 if (sToken != _T('0'))
732 return FALSE;
733
734 sToken = vTokenizer.GetNextToken();
735 if (!sToken.ToLong(&lVal))
736 return FALSE;
737 fm.lEmHeight = lVal;
738
739 sToken = vTokenizer.GetNextToken();
740 if (!sToken.ToLong(&lVal))
741 return FALSE;
742 fa.lAveCharWidth = lVal;
743
744 sToken = vTokenizer.GetNextToken();
745 if (!sToken.ToLong(&lVal))
746 return FALSE;
747 fa.fsSelection = (USHORT)lVal;
748
749 sToken = vTokenizer.GetNextToken();
750 if (!sToken.ToLong(&lVal))
751 return FALSE;
752 fa.fsType = (USHORT)lVal;
753
754 sToken = vTokenizer.GetNextToken();
755 if (!sToken.ToLong(&lVal))
756 return FALSE;
757 fa.fsFontUse = (USHORT)lVal;
758
759 sToken = vTokenizer.GetNextToken();
760 if (!sToken.ToLong(&lVal))
761 return FALSE;
762 fa.idRegistry = (USHORT)lVal;
763
764 sToken = vTokenizer.GetNextToken();
765 if (!sToken.ToLong(&lVal))
766 return FALSE;
767 fa.usCodePage = (USHORT)lVal;
768
769 sToken = vTokenizer.GetNextToken();
770 if (!sToken.ToLong(&lVal))
771 return FALSE;
772 fa.lMatch = lVal;
773
774 sToken = vTokenizer.GetNextToken();
775 if (!sToken.ToLong(&lVal))
776 return FALSE;
777 fn.usWeightClass = (USHORT)lVal;
778
779 sToken = vTokenizer.GetNextToken();
780 if(!sToken)
781 return FALSE;
782 wxStrcpy(fa.szFacename, sToken.c_str());
783 return TRUE;
784 } // end of wxNativeFontInfo::FromString
785
786 wxString wxNativeFontInfo::ToString() const
787 {
788 wxString sStr;
789
790 sStr.Printf(_T("%d;%ld;%ld;%ld;%d;%d;%d;%d;%d;%ld;%d;%s"),
791 0, // version, in case we want to change the format later
792 fm.lEmHeight,
793 fa.lAveCharWidth,
794 fa.lMaxBaselineExt,
795 fa.fsSelection,
796 fa.fsType,
797 fa.fsFontUse,
798 fa.idRegistry,
799 fa.usCodePage,
800 fa.lMatch,
801 fn.usWeightClass,
802 fa.szFacename);
803 return sStr;
804 } // end of wxNativeFontInfo::ToString
805
806 // ----------------------------------------------------------------------------
807 // wxFont
808 // ----------------------------------------------------------------------------
809
810 void wxFont::Init()
811 {
812 } // end of wxFont::Init
813
814 bool wxFont::Create(
815 const wxNativeFontInfo& rInfo
816 , WXHFONT hFont
817 )
818 {
819 UnRef();
820 m_refData = new wxFontRefData( rInfo
821 ,hFont
822 );
823 RealizeResource();
824 return TRUE;
825 } // end of wxFont::Create
826
827 wxFont::wxFont(
828 const wxString& rsFontdesc
829 )
830 {
831 wxNativeFontInfo vInfo;
832
833 if (vInfo.FromString(rsFontdesc))
834 (void)Create(vInfo);
835 } // end of wxFont::wxFont
836
837 // ----------------------------------------------------------------------------
838 // Constructor for a font. Note that the real construction is done
839 // in wxDC::SetFont, when information is available about scaling etc.
840 // ----------------------------------------------------------------------------
841 bool wxFont::Create(
842 int nPointSize
843 , int nFamily
844 , int nStyle
845 , int nWeight
846 , bool bUnderlined
847 , const wxString& rsFaceName
848 , wxFontEncoding vEncoding
849 )
850 {
851 UnRef();
852
853 //
854 // wxDEFAULT is a valid value for the font size too so we must treat it
855 // specially here (otherwise the size would be 70 == wxDEFAULT value)
856 //
857 if (nPointSize == wxDEFAULT)
858 {
859 nPointSize = wxNORMAL_FONT->GetPointSize();
860 }
861 m_refData = new wxFontRefData( nPointSize
862 ,nFamily
863 ,nStyle
864 ,nWeight
865 ,bUnderlined
866 ,rsFaceName
867 ,vEncoding
868 );
869 RealizeResource();
870 return TRUE;
871 } // end of wxFont::Create
872
873 wxFont::~wxFont()
874 {
875 } // end of wxFont::~wxFont
876
877 // ----------------------------------------------------------------------------
878 // real implementation
879 // Boris' Kovalenko comments:
880 // Because OS/2 fonts are associated with PS we can not create the font
881 // here, but we may check that font definition is true
882 // ----------------------------------------------------------------------------
883
884 bool wxFont::RealizeResource()
885 {
886 if ( GetResourceHandle() )
887 {
888 return TRUE;
889 }
890 return M_FONTDATA->Alloc(this);
891 } // end of wxFont::RealizeResource
892
893 bool wxFont::FreeResource(
894 bool bForce
895 )
896 {
897 if (GetResourceHandle())
898 {
899 M_FONTDATA->Free();
900 return TRUE;
901 }
902 return FALSE;
903 } // end of wxFont::FreeResource
904
905 WXHANDLE wxFont::GetResourceHandle()
906 {
907 return GetHFONT();
908 } // end of wxFont::GetResourceHandle
909
910 WXHFONT wxFont::GetHFONT() const
911 {
912 return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0;
913 } // end of wxFont::GetHFONT
914
915 bool wxFont::IsFree() const
916 {
917 return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0);
918 } // end of wxFont::IsFree
919
920 void wxFont::Unshare()
921 {
922 // Don't change shared data
923 if ( !m_refData )
924 {
925 m_refData = new wxFontRefData();
926 }
927 else
928 {
929 wxFontRefData* ref = new wxFontRefData(*M_FONTDATA);
930 UnRef();
931 m_refData = ref;
932 }
933 } // end of wxFont::Unshare
934
935 // ----------------------------------------------------------------------------
936 // change font attribute: we recreate font when doing it
937 // ----------------------------------------------------------------------------
938
939 void wxFont::SetPointSize(
940 int nPointSize
941 )
942 {
943 Unshare();
944
945 M_FONTDATA->SetPointSize(nPointSize);
946
947 RealizeResource();
948 } // end of wxFont::SetPointSize
949
950 void wxFont::SetFamily(
951 int nFamily
952 )
953 {
954 Unshare();
955
956 M_FONTDATA->SetFamily(nFamily);
957
958 RealizeResource();
959 } // end of wxFont::SetFamily
960
961 void wxFont::SetStyle(
962 int nStyle
963 )
964 {
965 Unshare();
966
967 M_FONTDATA->SetStyle(nStyle);
968
969 RealizeResource();
970 } // end of wxFont::SetStyle
971
972 void wxFont::SetWeight(
973 int nWeight
974 )
975 {
976 Unshare();
977
978 M_FONTDATA->SetWeight(nWeight);
979
980 RealizeResource();
981 } // end of wxFont::SetWeight
982
983 void wxFont::SetFaceName(
984 const wxString& rsFaceName
985 )
986 {
987 Unshare();
988
989 M_FONTDATA->SetFaceName(rsFaceName);
990
991 RealizeResource();
992 } // end of wxFont::SetFaceName
993
994 void wxFont::SetUnderlined(
995 bool bUnderlined
996 )
997 {
998 Unshare();
999
1000 M_FONTDATA->SetUnderlined(bUnderlined);
1001
1002 RealizeResource();
1003 } // end of wxFont::SetUnderlined
1004
1005 void wxFont::SetEncoding(
1006 wxFontEncoding vEncoding
1007 )
1008 {
1009 Unshare();
1010
1011 M_FONTDATA->SetEncoding(vEncoding);
1012
1013 RealizeResource();
1014 } // end of wxFont::SetEncoding
1015
1016 void wxFont::SetNativeFontInfo(
1017 const wxNativeFontInfo& rInfo
1018 )
1019 {
1020 Unshare();
1021
1022 FreeResource();
1023
1024 *M_FONTDATA = wxFontRefData(rInfo);
1025
1026 RealizeResource();
1027 }
1028
1029 // ----------------------------------------------------------------------------
1030 // accessors
1031 // ----------------------------------------------------------------------------
1032
1033 int wxFont::GetPointSize() const
1034 {
1035 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1036
1037 return M_FONTDATA->GetPointSize();
1038 } // end of wxFont::GetPointSize
1039
1040 int wxFont::GetFamily() const
1041 {
1042 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1043
1044 return M_FONTDATA->GetFamily();
1045 } // end of wxFont::GetFamily
1046
1047 int wxFont::GetStyle() const
1048 {
1049 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1050
1051 return M_FONTDATA->GetStyle();
1052 } // end of wxFont::GetStyle
1053
1054 int wxFont::GetWeight() const
1055 {
1056 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1057
1058 return M_FONTDATA->GetWeight();
1059 }
1060
1061 bool wxFont::GetUnderlined() const
1062 {
1063 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
1064
1065 return M_FONTDATA->GetUnderlined();
1066 } // end of wxFont::GetUnderlined
1067
1068 wxString wxFont::GetFaceName() const
1069 {
1070 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
1071
1072 return M_FONTDATA->GetFaceName();
1073 } // end of wxFont::GetFaceName
1074
1075 wxFontEncoding wxFont::GetEncoding() const
1076 {
1077 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
1078
1079 return M_FONTDATA->GetEncoding();
1080 } // end of wxFont::GetEncoding
1081
1082 wxNativeFontInfo* wxFont::GetNativeFontInfo() const
1083 {
1084 if (M_FONTDATA->HasNativeFontInfo())
1085 return new wxNativeFontInfo(M_FONTDATA->GetNativeFontInfo());
1086 return 0;
1087 } // end of wxFont::GetNativeFontInfo
1088
1089 //
1090 // Internal use only method to set the FONTMETRICS array
1091 //
1092 void wxFont::SetFM(
1093 PFONTMETRICS pFM
1094 , int nNumFonts
1095 )
1096 {
1097 M_FONTDATA->SetFM(pFM);
1098 M_FONTDATA->SetNumFonts(nNumFonts);
1099 } // end of wxFont::SetFM
1100
1101
1102 void wxFont::SetPS(
1103 HPS hPS
1104 )
1105 {
1106 Unshare();
1107
1108 M_FONTDATA->SetPS(hPS);
1109
1110 RealizeResource();
1111 } // end of wxFont::SetUnderlined
1112