]> git.saurik.com Git - wxWidgets.git/blame - src/os2/font.cpp
don't try to remove -g flag from CFLAGS, it is not necessary and doesn't work correct...
[wxWidgets.git] / src / os2 / font.cpp
CommitLineData
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 40IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
0e320a79 41
21802234
DW
42// ----------------------------------------------------------------------------
43// wxFontRefData - the internal description of the font
44// ----------------------------------------------------------------------------
45
46class WXDLLEXPORT wxFontRefData: public wxGDIRefData
0e320a79 47{
21802234
DW
48public:
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 {
2c1e8f2e
DW
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;
cc95f4f9
DW
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
21802234 243protected:
cc95f4f9
DW
244 //
245 // Common part of all ctors
246 //
e99762c0
DW
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
cc95f4f9
DW
256 void Init( const wxNativeFontInfo& rInfo
257 ,WXHFONT hFont = 0
258 ,WXHANDLE hPS = 0
259 );
e99762c0 260 //
21802234
DW
261 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
262 // DELETED by destructor
e99762c0
DW
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;
cc95f4f9
DW
277 WXHFONT m_hFont;
278
279 //
280 // Native font info
281 //
282 wxNativeFontInfo m_vNativeFontInfo;
283 bool m_bNativeFontInfoOk;
e99762c0
DW
284
285 //
286 // Some PM specific stuff
287 //
cc95f4f9
DW
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?
e99762c0 294}; // end of CLASS wxFontRefData
21802234
DW
295
296// ============================================================================
297// implementation
298// ============================================================================
299
300// ----------------------------------------------------------------------------
301// wxFontRefData
302// ----------------------------------------------------------------------------
303
e99762c0
DW
304void 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)
0e320a79 313{
e99762c0
DW
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;
cc95f4f9
DW
322 m_hFont = 0;
323
324 m_bNativeFontInfoOk = FALSE;
325
e99762c0
DW
326 m_nFontId = 0;
327 m_bTemporary = FALSE;
e99762c0
DW
328 m_pFM = (PFONTMETRICS)NULL;
329 m_hPS = NULLHANDLE;
330 m_nNumFonts = 0;
331} // end of wxFontRefData::Init
0e320a79 332
cc95f4f9
DW
333void 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
0e320a79 360wxFontRefData::~wxFontRefData()
cc95f4f9
DW
361{
362 Free();
363}
364
365bool wxFontRefData::Alloc(
366 wxFont* pFont
367)
368{
369 wxString sFaceName;
828621c2 370 long flId = m_hFont;
e1146627
DW
371 long lRc;
372 short nIndex = 0;
373 PFONTMETRICS pFM = NULL;
cc95f4f9
DW
374
375 if (!m_bNativeFontInfoOk)
376 {
377 wxFillLogFont( &m_vNativeFontInfo.fa
378 ,&m_vNativeFontInfo.fn
54ffa107 379 ,&m_hPS
828621c2 380 ,&m_bInternalPS
cc95f4f9
DW
381 ,&flId
382 ,sFaceName
383 ,pFont
384 );
385 m_bNativeFontInfoOk = TRUE;
386 }
387
e1146627
DW
388 if((lRc = ::GpiCreateLogFont( m_hPS
389 ,NULL
390 ,flId
391 ,&m_vNativeFontInfo.fa
392 )) != GPI_ERROR)
393 {
828621c2
DW
394 m_hFont = (WXHFONT)flId;
395 m_nFontId = flId;
e1146627 396 }
cc95f4f9
DW
397 if (!m_hFont)
398 {
399 wxLogLastError("CreateFont");
400 }
401
e1146627 402 ::GpiSetCharSet(m_hPS, flId); // sets font for presentation space
cc95f4f9
DW
403 ::GpiQueryFontMetrics(m_hPS, sizeof(FONTMETRICS), &m_vNativeFontInfo.fm);
404
405 //
406 // Set refData members with the results
407 //
cc95f4f9
DW
408 memcpy(&m_vFattrs, &m_vNativeFontInfo.fa, sizeof(m_vFattrs));
409 memcpy(&m_vFname, &m_vNativeFontInfo.fn, sizeof(m_vFname));
2c1e8f2e
DW
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 //
cc95f4f9
DW
417 if (strcmp(m_vNativeFontInfo.fa.szFacename, "Times New Roman") == 0)
418 m_nFamily = wxROMAN;
e1146627
DW
419 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Tms Rmn") == 0)
420 m_nFamily = wxSWISS;
cc95f4f9
DW
421 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "WarpSans") == 0)
422 m_nFamily = wxSWISS;
e1146627
DW
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;
cc95f4f9
DW
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)
e1146627 430 m_nFamily = wxTELETYPE;
54ffa107 431 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Courier") == 0)
e1146627
DW
432 m_nFamily = wxTELETYPE;
433 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "System VIO") == 0)
54ffa107 434 m_nFamily = wxMODERN;
cc95f4f9
DW
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);
828621c2
DW
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
cc95f4f9
DW
488 return TRUE;
489} // end of wxFontRefData::Alloc
490
491void wxFontRefData::Free()
0e320a79 492{
e99762c0
DW
493 if (m_pFM)
494 delete [] m_pFM;
495 m_pFM = (PFONTMETRICS)NULL;
cc95f4f9
DW
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
0e320a79 511
21802234 512// ----------------------------------------------------------------------------
cc95f4f9 513// wxNativeFontInfo
21802234 514// ----------------------------------------------------------------------------
0e320a79 515
cc95f4f9
DW
516void wxNativeFontInfo::Init()
517{
518 memset(&fa, '\0', sizeof(FATTRS));
519} // end of wxNativeFontInfo::Init
520
521int wxNativeFontInfo::GetPointSize() const
522{
523 return fm.lEmHeight;
524} // end of wxNativeFontInfo::GetPointSize
525
526wxFontStyle wxNativeFontInfo::GetStyle() const
527{
528 return fa.fsSelection & FATTR_SEL_ITALIC ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL;
529} // end of wxNativeFontInfo::GetStyle
530
531wxFontWeight wxNativeFontInfo::GetWeight() const
532{
533 switch(fn.usWeightClass)
19193a2c 534 {
cc95f4f9
DW
535 case FWEIGHT_DONT_CARE:
536 return wxFONTWEIGHT_NORMAL;
537
538 case FWEIGHT_NORMAL:
539 return wxFONTWEIGHT_NORMAL;
19193a2c 540
cc95f4f9
DW
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;
19193a2c 549 }
cc95f4f9
DW
550 return wxFONTWEIGHT_NORMAL;
551} // end of wxNativeFontInfo::GetWeight
19193a2c 552
cc95f4f9
DW
553bool wxNativeFontInfo::GetUnderlined() const
554{
555 return ((fa.fsSelection & FATTR_SEL_UNDERSCORE) != 0);
556} // end of wxNativeFontInfo::GetUnderlined
557
558wxString wxNativeFontInfo::GetFaceName() const
559{
560 return fm.szFacename;
561} // end of wxNativeFontInfo::GetFaceName
562
563wxFontFamily 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
583wxFontEncoding wxNativeFontInfo::GetEncoding() const
584{
585 return wxGetFontEncFromCharSet(fa.usCodePage);
586} // end of wxNativeFontInfo::GetEncoding
587
588void wxNativeFontInfo::SetPointSize(
589 int nPointsize
590)
591{
592 fm.lEmHeight = (LONG)nPointsize;
593} // end of wxNativeFontInfo::SetPointSize
594
595void 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
615void 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
639void wxNativeFontInfo::SetUnderlined(
640 bool bUnderlined
641)
642{
643 if(bUnderlined)
644 fa.fsSelection |= FATTR_SEL_UNDERSCORE;
645} // end of wxNativeFontInfo::SetUnderlined
646
647void wxNativeFontInfo::SetFaceName(
648 wxString sFacename
649)
650{
651 wxStrncpy(fa.szFacename, sFacename, WXSIZEOF(fa.szFacename));
652} // end of wxNativeFontInfo::SetFaceName
653
654void 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
694void wxNativeFontInfo::SetEncoding(
695 wxFontEncoding eEncoding
696)
697{
698 wxNativeEncodingInfo vInfo;
699
700 if ( !wxGetNativeFontEncoding( eEncoding
701 ,&vInfo
702 ))
703 {
142b3bc2 704 if (wxFontMapper::Get()->GetAltForEncoding( eEncoding
cc95f4f9
DW
705 ,&vInfo
706 ))
707 {
708 if (!vInfo.facename.empty())
709 {
710 //
711 // If we have this encoding only in some particular facename, use
712 // the facename - it is better to show the correct characters in a
713 // wrong facename than unreadable text in a correct one
714 //
715 SetFaceName(vInfo.facename);
716 }
717 }
718 else
cc95f4f9
DW
719 {
720 // unsupported encoding, replace with the default
721 vInfo.charset = 850;
722 }
723 }
724 fa.usCodePage = vInfo.charset;
725} // end of wxNativeFontInfo::SetFaceName
726
727bool wxNativeFontInfo::FromString(
728 const wxString& rsStr
729)
730{
731 long lVal;
732
733 wxStringTokenizer vTokenizer(rsStr, _T(";"));
734
735 //
736 // First the version
737 //
738 wxString sToken = vTokenizer.GetNextToken();
739
740 if (sToken != _T('0'))
741 return FALSE;
742
743 sToken = vTokenizer.GetNextToken();
744 if (!sToken.ToLong(&lVal))
745 return FALSE;
746 fm.lEmHeight = lVal;
747
748 sToken = vTokenizer.GetNextToken();
749 if (!sToken.ToLong(&lVal))
750 return FALSE;
751 fa.lAveCharWidth = lVal;
752
753 sToken = vTokenizer.GetNextToken();
754 if (!sToken.ToLong(&lVal))
755 return FALSE;
756 fa.fsSelection = (USHORT)lVal;
757
758 sToken = vTokenizer.GetNextToken();
759 if (!sToken.ToLong(&lVal))
760 return FALSE;
761 fa.fsType = (USHORT)lVal;
762
763 sToken = vTokenizer.GetNextToken();
764 if (!sToken.ToLong(&lVal))
765 return FALSE;
766 fa.fsFontUse = (USHORT)lVal;
767
768 sToken = vTokenizer.GetNextToken();
769 if (!sToken.ToLong(&lVal))
770 return FALSE;
771 fa.idRegistry = (USHORT)lVal;
772
773 sToken = vTokenizer.GetNextToken();
774 if (!sToken.ToLong(&lVal))
775 return FALSE;
776 fa.usCodePage = (USHORT)lVal;
777
778 sToken = vTokenizer.GetNextToken();
779 if (!sToken.ToLong(&lVal))
780 return FALSE;
781 fa.lMatch = lVal;
782
783 sToken = vTokenizer.GetNextToken();
784 if (!sToken.ToLong(&lVal))
785 return FALSE;
786 fn.usWeightClass = (USHORT)lVal;
787
788 sToken = vTokenizer.GetNextToken();
789 if(!sToken)
790 return FALSE;
791 wxStrcpy(fa.szFacename, sToken.c_str());
792 return TRUE;
793} // end of wxNativeFontInfo::FromString
794
795wxString wxNativeFontInfo::ToString() const
796{
797 wxString sStr;
798
799 sStr.Printf(_T("%d;%ld;%ld;%ld;%d;%d;%d;%d;%d;%ld;%d;%s"),
800 0, // version, in case we want to change the format later
801 fm.lEmHeight,
802 fa.lAveCharWidth,
803 fa.lMaxBaselineExt,
804 fa.fsSelection,
805 fa.fsType,
806 fa.fsFontUse,
807 fa.idRegistry,
808 fa.usCodePage,
809 fa.lMatch,
810 fn.usWeightClass,
811 fa.szFacename);
812 return sStr;
813} // end of wxNativeFontInfo::ToString
814
815// ----------------------------------------------------------------------------
816// wxFont
817// ----------------------------------------------------------------------------
19193a2c 818
21802234 819void wxFont::Init()
0e320a79 820{
cc95f4f9
DW
821} // end of wxFont::Init
822
823bool wxFont::Create(
824 const wxNativeFontInfo& rInfo
825, WXHFONT hFont
826)
827{
828 UnRef();
829 m_refData = new wxFontRefData( rInfo
830 ,hFont
831 );
832 RealizeResource();
833 return TRUE;
834} // end of wxFont::Create
835
836wxFont::wxFont(
837 const wxString& rsFontdesc
838)
839{
840 wxNativeFontInfo vInfo;
841
842 if (vInfo.FromString(rsFontdesc))
843 (void)Create(vInfo);
844} // end of wxFont::wxFont
0e320a79 845
e99762c0
DW
846// ----------------------------------------------------------------------------
847// Constructor for a font. Note that the real construction is done
848// in wxDC::SetFont, when information is available about scaling etc.
849// ----------------------------------------------------------------------------
850bool wxFont::Create(
851 int nPointSize
852, int nFamily
853, int nStyle
854, int nWeight
855, bool bUnderlined
856, const wxString& rsFaceName
857, wxFontEncoding vEncoding
858)
0e320a79
DW
859{
860 UnRef();
cc95f4f9
DW
861
862 //
863 // wxDEFAULT is a valid value for the font size too so we must treat it
864 // specially here (otherwise the size would be 70 == wxDEFAULT value)
865 //
866 if (nPointSize == wxDEFAULT)
867 {
868 nPointSize = wxNORMAL_FONT->GetPointSize();
869 }
e99762c0
DW
870 m_refData = new wxFontRefData( nPointSize
871 ,nFamily
872 ,nStyle
873 ,nWeight
874 ,bUnderlined
875 ,rsFaceName
876 ,vEncoding
877 );
0e320a79 878 RealizeResource();
0e320a79 879 return TRUE;
e99762c0 880} // end of wxFont::Create
0e320a79
DW
881
882wxFont::~wxFont()
883{
e99762c0 884} // end of wxFont::~wxFont
0e320a79 885
21802234
DW
886// ----------------------------------------------------------------------------
887// real implementation
f6bcfd97
BP
888// Boris' Kovalenko comments:
889// Because OS/2 fonts are associated with PS we can not create the font
890// here, but we may check that font definition is true
21802234
DW
891// ----------------------------------------------------------------------------
892
0e320a79
DW
893bool wxFont::RealizeResource()
894{
21802234
DW
895 if ( GetResourceHandle() )
896 {
cc95f4f9 897 return TRUE;
21802234 898 }
cc95f4f9 899 return M_FONTDATA->Alloc(this);
e99762c0 900} // end of wxFont::RealizeResource
21802234 901
e99762c0
DW
902bool wxFont::FreeResource(
903 bool bForce
904)
21802234 905{
e99762c0 906 if (GetResourceHandle())
21802234 907 {
cc95f4f9 908 M_FONTDATA->Free();
21802234
DW
909 return TRUE;
910 }
0e320a79 911 return FALSE;
e99762c0 912} // end of wxFont::FreeResource
0e320a79 913
5fd2b2c6
DW
914WXHANDLE wxFont::GetResourceHandle()
915{
916 return GetHFONT();
e99762c0 917} // end of wxFont::GetResourceHandle
21802234 918
cc95f4f9
DW
919WXHFONT wxFont::GetHFONT() const
920{
921 return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0;
922} // end of wxFont::GetHFONT
923
21802234
DW
924bool wxFont::IsFree() const
925{
cc95f4f9 926 return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0);
e99762c0 927} // end of wxFont::IsFree
21802234 928
0e320a79
DW
929void wxFont::Unshare()
930{
21802234
DW
931 // Don't change shared data
932 if ( !m_refData )
0e320a79 933 {
21802234
DW
934 m_refData = new wxFontRefData();
935 }
0e320a79
DW
936 else
937 {
21802234
DW
938 wxFontRefData* ref = new wxFontRefData(*M_FONTDATA);
939 UnRef();
940 m_refData = ref;
941 }
e99762c0 942} // end of wxFont::Unshare
0e320a79 943
21802234
DW
944// ----------------------------------------------------------------------------
945// change font attribute: we recreate font when doing it
946// ----------------------------------------------------------------------------
947
e99762c0
DW
948void wxFont::SetPointSize(
949 int nPointSize
950)
0e320a79
DW
951{
952 Unshare();
953
cc95f4f9 954 M_FONTDATA->SetPointSize(nPointSize);
0e320a79
DW
955
956 RealizeResource();
e99762c0 957} // end of wxFont::SetPointSize
0e320a79 958
e99762c0
DW
959void wxFont::SetFamily(
960 int nFamily
961)
0e320a79
DW
962{
963 Unshare();
964
cc95f4f9 965 M_FONTDATA->SetFamily(nFamily);
0e320a79
DW
966
967 RealizeResource();
e99762c0 968} // end of wxFont::SetFamily
0e320a79 969
e99762c0
DW
970void wxFont::SetStyle(
971 int nStyle
972)
0e320a79
DW
973{
974 Unshare();
975
cc95f4f9 976 M_FONTDATA->SetStyle(nStyle);
0e320a79
DW
977
978 RealizeResource();
e99762c0 979} // end of wxFont::SetStyle
0e320a79 980
e99762c0
DW
981void wxFont::SetWeight(
982 int nWeight
983)
0e320a79
DW
984{
985 Unshare();
986
cc95f4f9 987 M_FONTDATA->SetWeight(nWeight);
0e320a79
DW
988
989 RealizeResource();
e99762c0 990} // end of wxFont::SetWeight
0e320a79 991
e99762c0
DW
992void wxFont::SetFaceName(
993 const wxString& rsFaceName
994)
0e320a79
DW
995{
996 Unshare();
997
cc95f4f9 998 M_FONTDATA->SetFaceName(rsFaceName);
0e320a79
DW
999
1000 RealizeResource();
e99762c0 1001} // end of wxFont::SetFaceName
0e320a79 1002
e99762c0
DW
1003void wxFont::SetUnderlined(
1004 bool bUnderlined
1005)
0e320a79
DW
1006{
1007 Unshare();
1008
cc95f4f9 1009 M_FONTDATA->SetUnderlined(bUnderlined);
0e320a79
DW
1010
1011 RealizeResource();
e99762c0 1012} // end of wxFont::SetUnderlined
0e320a79 1013
e99762c0
DW
1014void wxFont::SetEncoding(
1015 wxFontEncoding vEncoding
1016)
0e320a79 1017{
21802234
DW
1018 Unshare();
1019
cc95f4f9 1020 M_FONTDATA->SetEncoding(vEncoding);
21802234
DW
1021
1022 RealizeResource();
e99762c0
DW
1023} // end of wxFont::SetEncoding
1024
cc95f4f9
DW
1025void wxFont::SetNativeFontInfo(
1026 const wxNativeFontInfo& rInfo
e99762c0
DW
1027)
1028{
1029 Unshare();
1030
cc95f4f9 1031 FreeResource();
e99762c0 1032
cc95f4f9 1033 *M_FONTDATA = wxFontRefData(rInfo);
e99762c0 1034
cc95f4f9
DW
1035 RealizeResource();
1036}
0e320a79 1037
21802234
DW
1038// ----------------------------------------------------------------------------
1039// accessors
1040// ----------------------------------------------------------------------------
1041
1042int wxFont::GetPointSize() const
0e320a79 1043{
cc95f4f9 1044 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
7e99520b 1045
cc95f4f9 1046 return M_FONTDATA->GetPointSize();
e99762c0 1047} // end of wxFont::GetPointSize
0e320a79 1048
21802234 1049int wxFont::GetFamily() const
0e320a79 1050{
cc95f4f9 1051 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
21802234 1052
cc95f4f9
DW
1053 return M_FONTDATA->GetFamily();
1054} // end of wxFont::GetFamily
21802234
DW
1055
1056int wxFont::GetStyle() const
1057{
cc95f4f9
DW
1058 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1059
1060 return M_FONTDATA->GetStyle();
e99762c0 1061} // end of wxFont::GetStyle
21802234
DW
1062
1063int wxFont::GetWeight() const
1064{
cc95f4f9
DW
1065 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1066
1067 return M_FONTDATA->GetWeight();
21802234
DW
1068}
1069
1070bool wxFont::GetUnderlined() const
1071{
cc95f4f9
DW
1072 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
1073
1074 return M_FONTDATA->GetUnderlined();
e99762c0 1075} // end of wxFont::GetUnderlined
21802234
DW
1076
1077wxString wxFont::GetFaceName() const
1078{
cc95f4f9 1079 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
e99762c0 1080
cc95f4f9 1081 return M_FONTDATA->GetFaceName();
e99762c0 1082} // end of wxFont::GetFaceName
21802234
DW
1083
1084wxFontEncoding wxFont::GetEncoding() const
1085{
cc95f4f9 1086 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
e99762c0 1087
cc95f4f9
DW
1088 return M_FONTDATA->GetEncoding();
1089} // end of wxFont::GetEncoding
0e320a79 1090
cc95f4f9
DW
1091wxNativeFontInfo* wxFont::GetNativeFontInfo() const
1092{
1093 if (M_FONTDATA->HasNativeFontInfo())
1094 return new wxNativeFontInfo(M_FONTDATA->GetNativeFontInfo());
1095 return 0;
1096} // end of wxFont::GetNativeFontInfo
1097
1098//
1099// Internal use only method to set the FONTMETRICS array
1100//
1101void wxFont::SetFM(
1102 PFONTMETRICS pFM
1103, int nNumFonts
1104)
1105{
1106 M_FONTDATA->SetFM(pFM);
1107 M_FONTDATA->SetNumFonts(nNumFonts);
1108} // end of wxFont::SetFM
e99762c0
DW
1109
1110
d0a6b279
DW
1111void wxFont::SetPS(
1112 HPS hPS
1113)
1114{
1115 Unshare();
1116
1117 M_FONTDATA->SetPS(hPS);
1118
1119 RealizeResource();
1120} // end of wxFont::SetUnderlined
1121