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