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