OS/2 bug fixes and new mod file
[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;
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 if (strcmp(m_vNativeFontInfo.fa.szFacename, "Courier") == 0)
429 m_nFamily = wxMODERN;
430 else
431 m_nFamily = wxSWISS;
432
433 if (m_vNativeFontInfo.fa.fsSelection & FATTR_SEL_ITALIC)
434 m_nStyle = wxFONTSTYLE_ITALIC;
435 else
436 m_nStyle = wxFONTSTYLE_NORMAL;
437 switch(m_vNativeFontInfo.fn.usWeightClass)
438 {
439 case FWEIGHT_DONT_CARE:
440 m_nWeight = wxFONTWEIGHT_NORMAL;
441 break;
442
443 case FWEIGHT_NORMAL:
444 m_nWeight = wxFONTWEIGHT_NORMAL;
445 break;
446
447 case FWEIGHT_LIGHT:
448 m_nWeight = wxFONTWEIGHT_LIGHT;
449 break;
450
451 case FWEIGHT_BOLD:
452 m_nWeight = wxFONTWEIGHT_BOLD;
453 break;
454
455 case FWEIGHT_ULTRA_BOLD:
456 m_nWeight = wxFONTWEIGHT_MAX;
457 break;
458
459 default:
460 m_nWeight = wxFONTWEIGHT_NORMAL;
461 }
462 m_bUnderlined = ((m_vNativeFontInfo.fa.fsSelection & FATTR_SEL_UNDERSCORE) != 0);
463 m_sFaceName = m_vNativeFontInfo.fa.szFacename;
464 m_vEncoding = wxGetFontEncFromCharSet(m_vNativeFontInfo.fa.usCodePage);
465 return TRUE;
466 } // end of wxFontRefData::Alloc
467
468 void wxFontRefData::Free()
469 {
470 if (m_pFM)
471 delete [] m_pFM;
472 m_pFM = (PFONTMETRICS)NULL;
473
474 if ( m_hFont )
475 {
476 if (!::GpiSetCharSet(m_hPS, LCID_DEFAULT))
477 {
478 wxLogLastError(wxT("DeleteObject(font)"));
479 }
480 ::GpiDeleteSetId(m_hPS, 1L); /* delete the logical font */
481 m_nFontId = 0;
482 m_hFont = 0;
483 }
484 if (m_bInternalPS)
485 ::WinReleasePS(m_hPS);
486 m_hPS = NULLHANDLE;
487 } // end of wxFontRefData::Free
488
489 // ----------------------------------------------------------------------------
490 // wxNativeFontInfo
491 // ----------------------------------------------------------------------------
492
493 void wxNativeFontInfo::Init()
494 {
495 memset(&fa, '\0', sizeof(FATTRS));
496 } // end of wxNativeFontInfo::Init
497
498 int wxNativeFontInfo::GetPointSize() const
499 {
500 return fm.lEmHeight;
501 } // end of wxNativeFontInfo::GetPointSize
502
503 wxFontStyle wxNativeFontInfo::GetStyle() const
504 {
505 return fa.fsSelection & FATTR_SEL_ITALIC ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL;
506 } // end of wxNativeFontInfo::GetStyle
507
508 wxFontWeight wxNativeFontInfo::GetWeight() const
509 {
510 switch(fn.usWeightClass)
511 {
512 case FWEIGHT_DONT_CARE:
513 return wxFONTWEIGHT_NORMAL;
514
515 case FWEIGHT_NORMAL:
516 return wxFONTWEIGHT_NORMAL;
517
518 case FWEIGHT_LIGHT:
519 return wxFONTWEIGHT_LIGHT;
520
521 case FWEIGHT_BOLD:
522 return wxFONTWEIGHT_BOLD;
523
524 case FWEIGHT_ULTRA_BOLD:
525 return wxFONTWEIGHT_MAX;
526 }
527 return wxFONTWEIGHT_NORMAL;
528 } // end of wxNativeFontInfo::GetWeight
529
530 bool wxNativeFontInfo::GetUnderlined() const
531 {
532 return ((fa.fsSelection & FATTR_SEL_UNDERSCORE) != 0);
533 } // end of wxNativeFontInfo::GetUnderlined
534
535 wxString wxNativeFontInfo::GetFaceName() const
536 {
537 return fm.szFacename;
538 } // end of wxNativeFontInfo::GetFaceName
539
540 wxFontFamily wxNativeFontInfo::GetFamily() const
541 {
542 int nFamily;
543
544 //
545 // Extract family from facename
546 //
547 if (strcmp(fa.szFacename, "Times New Roman") == 0)
548 nFamily = wxROMAN;
549 else if (strcmp(fa.szFacename, "WarpSans") == 0)
550 nFamily = wxSWISS;
551 else if (strcmp(fa.szFacename, "Script") == 0)
552 nFamily = wxSCRIPT;
553 else if (strcmp(fa.szFacename, "Courier New") == 0)
554 nFamily = wxMODERN;
555 else
556 nFamily = wxSWISS;
557 return (wxFontFamily)nFamily;
558 } // end of wxNativeFontInfo::GetFamily
559
560 wxFontEncoding wxNativeFontInfo::GetEncoding() const
561 {
562 return wxGetFontEncFromCharSet(fa.usCodePage);
563 } // end of wxNativeFontInfo::GetEncoding
564
565 void wxNativeFontInfo::SetPointSize(
566 int nPointsize
567 )
568 {
569 fm.lEmHeight = (LONG)nPointsize;
570 } // end of wxNativeFontInfo::SetPointSize
571
572 void wxNativeFontInfo::SetStyle(
573 wxFontStyle eStyle
574 )
575 {
576 switch (eStyle)
577 {
578 default:
579 wxFAIL_MSG( _T("unknown font style") );
580 // fall through
581
582 case wxFONTSTYLE_NORMAL:
583 break;
584
585 case wxFONTSTYLE_ITALIC:
586 case wxFONTSTYLE_SLANT:
587 fa.fsSelection |= FATTR_SEL_ITALIC;
588 break;
589 }
590 } // end of wxNativeFontInfo::SetStyle
591
592 void wxNativeFontInfo::SetWeight(
593 wxFontWeight eWeight
594 )
595 {
596 switch (eWeight)
597 {
598 default:
599 wxFAIL_MSG( _T("unknown font weight") );
600 // fall through
601
602 case wxFONTWEIGHT_NORMAL:
603 fn.usWeightClass = FWEIGHT_NORMAL;
604 break;
605
606 case wxFONTWEIGHT_LIGHT:
607 fn.usWeightClass = FWEIGHT_LIGHT;
608 break;
609
610 case wxFONTWEIGHT_BOLD:
611 fn.usWeightClass = FWEIGHT_BOLD;
612 break;
613 }
614 } // end of wxNativeFontInfo::SetWeight
615
616 void wxNativeFontInfo::SetUnderlined(
617 bool bUnderlined
618 )
619 {
620 if(bUnderlined)
621 fa.fsSelection |= FATTR_SEL_UNDERSCORE;
622 } // end of wxNativeFontInfo::SetUnderlined
623
624 void wxNativeFontInfo::SetFaceName(
625 wxString sFacename
626 )
627 {
628 wxStrncpy(fa.szFacename, sFacename, WXSIZEOF(fa.szFacename));
629 } // end of wxNativeFontInfo::SetFaceName
630
631 void wxNativeFontInfo::SetFamily(
632 wxFontFamily eFamily
633 )
634 {
635 wxString sFacename;
636
637 switch (eFamily)
638 {
639 case wxSCRIPT:
640 sFacename = _T("Script");
641 break;
642
643 case wxDECORATIVE:
644 sFacename = _T("Times New Roman");
645 break;
646
647 case wxROMAN:
648 sFacename = _T("Times New Roman");
649 break;
650
651 case wxTELETYPE:
652 case wxMODERN:
653 sFacename = _T("Courier New");
654 break;
655
656 case wxSWISS:
657 sFacename = _T("WarpSans");
658 break;
659
660 case wxDEFAULT:
661 default:
662 sFacename = _T("Helv");
663 }
664
665 if (!wxStrlen(fa.szFacename) )
666 {
667 SetFaceName(sFacename);
668 }
669 } // end of wxNativeFontInfo::SetFamily
670
671 void wxNativeFontInfo::SetEncoding(
672 wxFontEncoding eEncoding
673 )
674 {
675 wxNativeEncodingInfo vInfo;
676
677 if ( !wxGetNativeFontEncoding( eEncoding
678 ,&vInfo
679 ))
680 {
681 #if wxUSE_FONTMAP
682 if (wxTheFontMapper->GetAltForEncoding( eEncoding
683 ,&vInfo
684 ))
685 {
686 if (!vInfo.facename.empty())
687 {
688 //
689 // If we have this encoding only in some particular facename, use
690 // the facename - it is better to show the correct characters in a
691 // wrong facename than unreadable text in a correct one
692 //
693 SetFaceName(vInfo.facename);
694 }
695 }
696 else
697 #endif // wxUSE_FONTMAP
698 {
699 // unsupported encoding, replace with the default
700 vInfo.charset = 850;
701 }
702 }
703 fa.usCodePage = vInfo.charset;
704 } // end of wxNativeFontInfo::SetFaceName
705
706 bool wxNativeFontInfo::FromString(
707 const wxString& rsStr
708 )
709 {
710 long lVal;
711
712 wxStringTokenizer vTokenizer(rsStr, _T(";"));
713
714 //
715 // First the version
716 //
717 wxString sToken = vTokenizer.GetNextToken();
718
719 if (sToken != _T('0'))
720 return FALSE;
721
722 sToken = vTokenizer.GetNextToken();
723 if (!sToken.ToLong(&lVal))
724 return FALSE;
725 fm.lEmHeight = lVal;
726
727 sToken = vTokenizer.GetNextToken();
728 if (!sToken.ToLong(&lVal))
729 return FALSE;
730 fa.lAveCharWidth = lVal;
731
732 sToken = vTokenizer.GetNextToken();
733 if (!sToken.ToLong(&lVal))
734 return FALSE;
735 fa.fsSelection = (USHORT)lVal;
736
737 sToken = vTokenizer.GetNextToken();
738 if (!sToken.ToLong(&lVal))
739 return FALSE;
740 fa.fsType = (USHORT)lVal;
741
742 sToken = vTokenizer.GetNextToken();
743 if (!sToken.ToLong(&lVal))
744 return FALSE;
745 fa.fsFontUse = (USHORT)lVal;
746
747 sToken = vTokenizer.GetNextToken();
748 if (!sToken.ToLong(&lVal))
749 return FALSE;
750 fa.idRegistry = (USHORT)lVal;
751
752 sToken = vTokenizer.GetNextToken();
753 if (!sToken.ToLong(&lVal))
754 return FALSE;
755 fa.usCodePage = (USHORT)lVal;
756
757 sToken = vTokenizer.GetNextToken();
758 if (!sToken.ToLong(&lVal))
759 return FALSE;
760 fa.lMatch = lVal;
761
762 sToken = vTokenizer.GetNextToken();
763 if (!sToken.ToLong(&lVal))
764 return FALSE;
765 fn.usWeightClass = (USHORT)lVal;
766
767 sToken = vTokenizer.GetNextToken();
768 if(!sToken)
769 return FALSE;
770 wxStrcpy(fa.szFacename, sToken.c_str());
771 return TRUE;
772 } // end of wxNativeFontInfo::FromString
773
774 wxString wxNativeFontInfo::ToString() const
775 {
776 wxString sStr;
777
778 sStr.Printf(_T("%d;%ld;%ld;%ld;%d;%d;%d;%d;%d;%ld;%d;%s"),
779 0, // version, in case we want to change the format later
780 fm.lEmHeight,
781 fa.lAveCharWidth,
782 fa.lMaxBaselineExt,
783 fa.fsSelection,
784 fa.fsType,
785 fa.fsFontUse,
786 fa.idRegistry,
787 fa.usCodePage,
788 fa.lMatch,
789 fn.usWeightClass,
790 fa.szFacename);
791 return sStr;
792 } // end of wxNativeFontInfo::ToString
793
794 // ----------------------------------------------------------------------------
795 // wxFont
796 // ----------------------------------------------------------------------------
797
798 void wxFont::Init()
799 {
800 } // end of wxFont::Init
801
802 bool wxFont::Create(
803 const wxNativeFontInfo& rInfo
804 , WXHFONT hFont
805 )
806 {
807 UnRef();
808 m_refData = new wxFontRefData( rInfo
809 ,hFont
810 );
811 RealizeResource();
812 return TRUE;
813 } // end of wxFont::Create
814
815 wxFont::wxFont(
816 const wxString& rsFontdesc
817 )
818 {
819 wxNativeFontInfo vInfo;
820
821 if (vInfo.FromString(rsFontdesc))
822 (void)Create(vInfo);
823 } // end of wxFont::wxFont
824
825 // ----------------------------------------------------------------------------
826 // Constructor for a font. Note that the real construction is done
827 // in wxDC::SetFont, when information is available about scaling etc.
828 // ----------------------------------------------------------------------------
829 bool wxFont::Create(
830 int nPointSize
831 , int nFamily
832 , int nStyle
833 , int nWeight
834 , bool bUnderlined
835 , const wxString& rsFaceName
836 , wxFontEncoding vEncoding
837 )
838 {
839 UnRef();
840
841 //
842 // wxDEFAULT is a valid value for the font size too so we must treat it
843 // specially here (otherwise the size would be 70 == wxDEFAULT value)
844 //
845 if (nPointSize == wxDEFAULT)
846 {
847 nPointSize = wxNORMAL_FONT->GetPointSize();
848 }
849 m_refData = new wxFontRefData( nPointSize
850 ,nFamily
851 ,nStyle
852 ,nWeight
853 ,bUnderlined
854 ,rsFaceName
855 ,vEncoding
856 );
857 RealizeResource();
858 return TRUE;
859 } // end of wxFont::Create
860
861 wxFont::~wxFont()
862 {
863 } // end of wxFont::~wxFont
864
865 // ----------------------------------------------------------------------------
866 // real implementation
867 // Boris' Kovalenko comments:
868 // Because OS/2 fonts are associated with PS we can not create the font
869 // here, but we may check that font definition is true
870 // ----------------------------------------------------------------------------
871
872 bool wxFont::RealizeResource()
873 {
874 if ( GetResourceHandle() )
875 {
876 return TRUE;
877 }
878 return M_FONTDATA->Alloc(this);
879 } // end of wxFont::RealizeResource
880
881 bool wxFont::FreeResource(
882 bool bForce
883 )
884 {
885 if (GetResourceHandle())
886 {
887 M_FONTDATA->Free();
888 return TRUE;
889 }
890 return FALSE;
891 } // end of wxFont::FreeResource
892
893 WXHANDLE wxFont::GetResourceHandle()
894 {
895 return GetHFONT();
896 } // end of wxFont::GetResourceHandle
897
898 WXHFONT wxFont::GetHFONT() const
899 {
900 return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0;
901 } // end of wxFont::GetHFONT
902
903 bool wxFont::IsFree() const
904 {
905 return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0);
906 } // end of wxFont::IsFree
907
908 void wxFont::Unshare()
909 {
910 // Don't change shared data
911 if ( !m_refData )
912 {
913 m_refData = new wxFontRefData();
914 }
915 else
916 {
917 wxFontRefData* ref = new wxFontRefData(*M_FONTDATA);
918 UnRef();
919 m_refData = ref;
920 }
921 } // end of wxFont::Unshare
922
923 // ----------------------------------------------------------------------------
924 // change font attribute: we recreate font when doing it
925 // ----------------------------------------------------------------------------
926
927 void wxFont::SetPointSize(
928 int nPointSize
929 )
930 {
931 Unshare();
932
933 M_FONTDATA->SetPointSize(nPointSize);
934
935 RealizeResource();
936 } // end of wxFont::SetPointSize
937
938 void wxFont::SetFamily(
939 int nFamily
940 )
941 {
942 Unshare();
943
944 M_FONTDATA->SetFamily(nFamily);
945
946 RealizeResource();
947 } // end of wxFont::SetFamily
948
949 void wxFont::SetStyle(
950 int nStyle
951 )
952 {
953 Unshare();
954
955 M_FONTDATA->SetStyle(nStyle);
956
957 RealizeResource();
958 } // end of wxFont::SetStyle
959
960 void wxFont::SetWeight(
961 int nWeight
962 )
963 {
964 Unshare();
965
966 M_FONTDATA->SetWeight(nWeight);
967
968 RealizeResource();
969 } // end of wxFont::SetWeight
970
971 void wxFont::SetFaceName(
972 const wxString& rsFaceName
973 )
974 {
975 Unshare();
976
977 M_FONTDATA->SetFaceName(rsFaceName);
978
979 RealizeResource();
980 } // end of wxFont::SetFaceName
981
982 void wxFont::SetUnderlined(
983 bool bUnderlined
984 )
985 {
986 Unshare();
987
988 M_FONTDATA->SetUnderlined(bUnderlined);
989
990 RealizeResource();
991 } // end of wxFont::SetUnderlined
992
993 void wxFont::SetEncoding(
994 wxFontEncoding vEncoding
995 )
996 {
997 Unshare();
998
999 M_FONTDATA->SetEncoding(vEncoding);
1000
1001 RealizeResource();
1002 } // end of wxFont::SetEncoding
1003
1004 void wxFont::SetNativeFontInfo(
1005 const wxNativeFontInfo& rInfo
1006 )
1007 {
1008 Unshare();
1009
1010 FreeResource();
1011
1012 *M_FONTDATA = wxFontRefData(rInfo);
1013
1014 RealizeResource();
1015 }
1016
1017 // ----------------------------------------------------------------------------
1018 // accessors
1019 // ----------------------------------------------------------------------------
1020
1021 int wxFont::GetPointSize() const
1022 {
1023 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1024
1025 return M_FONTDATA->GetPointSize();
1026 } // end of wxFont::GetPointSize
1027
1028 int wxFont::GetFamily() const
1029 {
1030 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1031
1032 return M_FONTDATA->GetFamily();
1033 } // end of wxFont::GetFamily
1034
1035 int wxFont::GetStyle() const
1036 {
1037 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1038
1039 return M_FONTDATA->GetStyle();
1040 } // end of wxFont::GetStyle
1041
1042 int wxFont::GetWeight() const
1043 {
1044 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1045
1046 return M_FONTDATA->GetWeight();
1047 }
1048
1049 bool wxFont::GetUnderlined() const
1050 {
1051 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
1052
1053 return M_FONTDATA->GetUnderlined();
1054 } // end of wxFont::GetUnderlined
1055
1056 wxString wxFont::GetFaceName() const
1057 {
1058 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
1059
1060 return M_FONTDATA->GetFaceName();
1061 } // end of wxFont::GetFaceName
1062
1063 wxFontEncoding wxFont::GetEncoding() const
1064 {
1065 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
1066
1067 return M_FONTDATA->GetEncoding();
1068 } // end of wxFont::GetEncoding
1069
1070 wxNativeFontInfo* wxFont::GetNativeFontInfo() const
1071 {
1072 if (M_FONTDATA->HasNativeFontInfo())
1073 return new wxNativeFontInfo(M_FONTDATA->GetNativeFontInfo());
1074 return 0;
1075 } // end of wxFont::GetNativeFontInfo
1076
1077 //
1078 // Internal use only method to set the FONTMETRICS array
1079 //
1080 void wxFont::SetFM(
1081 PFONTMETRICS pFM
1082 , int nNumFonts
1083 )
1084 {
1085 M_FONTDATA->SetFM(pFM);
1086 M_FONTDATA->SetNumFonts(nNumFonts);
1087 } // end of wxFont::SetFM
1088
1089
1090 void wxFont::SetPS(
1091 HPS hPS
1092 )
1093 {
1094 Unshare();
1095
1096 M_FONTDATA->SetPS(hPS);
1097
1098 RealizeResource();
1099 } // end of wxFont::SetUnderlined
1100