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