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