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