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