]> git.saurik.com Git - wxWidgets.git/blob - src/os2/font.cpp
517053a9155ed3e5f432b5636608b87a32ff20b9
[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 #include "wx/log.h"
32 #endif // WX_PRECOMP
33
34 #include "wx/os2/private.h"
35
36 #include "wx/fontutil.h"
37 #include "wx/fontmap.h"
38 #include "wx/encinfo.h"
39
40 #include "wx/tokenzr.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 "", 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 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
245 protected:
246 //
247 // Common part of all ctors
248 //
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
258 void Init( const wxNativeFontInfo& rInfo
259 ,WXHFONT hFont = 0
260 ,WXHANDLE hPS = 0
261 );
262 //
263 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
264 // DELETED by destructor
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;
279 WXHFONT m_hFont;
280
281 //
282 // Native font info
283 //
284 wxNativeFontInfo m_vNativeFontInfo;
285 bool m_bNativeFontInfoOk;
286
287 //
288 // Some PM specific stuff
289 //
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?
296 }; // end of CLASS wxFontRefData
297
298 // ============================================================================
299 // implementation
300 // ============================================================================
301
302 // ----------------------------------------------------------------------------
303 // wxFontRefData
304 // ----------------------------------------------------------------------------
305
306 void 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 )
315 {
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;
324 m_hFont = 0;
325
326 m_bNativeFontInfoOk = FALSE;
327
328 m_nFontId = 0;
329 m_bTemporary = FALSE;
330 m_pFM = (PFONTMETRICS)NULL;
331 m_hPS = NULLHANDLE;
332 m_nNumFonts = 0;
333 } // end of wxFontRefData::Init
334
335 void 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
353 if (hPS == NULLHANDLE)
354 {
355 m_hPS = ::WinGetPS(HWND_DESKTOP);
356 m_bInternalPS = TRUE;
357 }
358 else
359 m_hPS = (HPS)hPS;
360
361 m_nFontId = 0;
362 m_bTemporary = FALSE;
363 m_pFM = (PFONTMETRICS)NULL;
364 m_nNumFonts = 0;
365 } // end of wxFontRefData::Init
366
367 wxFontRefData::~wxFontRefData()
368 {
369 Free();
370 }
371
372 bool wxFontRefData::Alloc(
373 wxFont* pFont
374 )
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("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 = 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 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 void wxNativeFontInfo::SetFaceName(
695 wxString sFacename
696 )
697 {
698 wxStrncpy(fa.szFacename, sFacename, WXSIZEOF(fa.szFacename));
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(fa.szFacename) )
739 {
740 SetFaceName(sFacename);
741 }
742 } // end of wxNativeFontInfo::SetFamily
743
744 void wxNativeFontInfo::SetEncoding(
745 wxFontEncoding eEncoding
746 )
747 {
748 wxNativeEncodingInfo vInfo;
749
750 if ( !wxGetNativeFontEncoding( eEncoding
751 ,&vInfo
752 ))
753 {
754 if (wxFontMapper::Get()->GetAltForEncoding( eEncoding
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
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
777 bool 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
845 wxString 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 // ----------------------------------------------------------------------------
868
869 void wxFont::Init()
870 {
871 } // end of wxFont::Init
872
873 bool 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
886 wxFont::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
895
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 // ----------------------------------------------------------------------------
900 bool 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 )
909 {
910 UnRef();
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 }
920 m_refData = new wxFontRefData( nPointSize
921 ,nFamily
922 ,nStyle
923 ,nWeight
924 ,bUnderlined
925 ,rsFaceName
926 ,vEncoding
927 );
928 RealizeResource();
929 return TRUE;
930 } // end of wxFont::Create
931
932 wxFont::~wxFont()
933 {
934 } // end of wxFont::~wxFont
935
936 // ----------------------------------------------------------------------------
937 // real implementation
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
941 // ----------------------------------------------------------------------------
942
943 bool wxFont::RealizeResource()
944 {
945 if ( GetResourceHandle() )
946 {
947 return TRUE;
948 }
949 return M_FONTDATA->Alloc(this);
950 } // end of wxFont::RealizeResource
951
952 bool wxFont::FreeResource(
953 bool bForce
954 )
955 {
956 if (GetResourceHandle())
957 {
958 M_FONTDATA->Free();
959 return TRUE;
960 }
961 return FALSE;
962 } // end of wxFont::FreeResource
963
964 WXHANDLE wxFont::GetResourceHandle()
965 {
966 return GetHFONT();
967 } // end of wxFont::GetResourceHandle
968
969 WXHFONT wxFont::GetHFONT() const
970 {
971 return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0;
972 } // end of wxFont::GetHFONT
973
974 bool wxFont::IsFree() const
975 {
976 return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0);
977 } // end of wxFont::IsFree
978
979 void wxFont::Unshare()
980 {
981 // Don't change shared data
982 if ( !m_refData )
983 {
984 m_refData = new wxFontRefData();
985 }
986 else
987 {
988 wxFontRefData* ref = new wxFontRefData(*M_FONTDATA);
989 UnRef();
990 m_refData = ref;
991 }
992 } // end of wxFont::Unshare
993
994 // ----------------------------------------------------------------------------
995 // change font attribute: we recreate font when doing it
996 // ----------------------------------------------------------------------------
997
998 void wxFont::SetPointSize(
999 int nPointSize
1000 )
1001 {
1002 Unshare();
1003
1004 M_FONTDATA->SetPointSize(nPointSize);
1005
1006 RealizeResource();
1007 } // end of wxFont::SetPointSize
1008
1009 void wxFont::SetFamily(
1010 int nFamily
1011 )
1012 {
1013 Unshare();
1014
1015 M_FONTDATA->SetFamily(nFamily);
1016
1017 RealizeResource();
1018 } // end of wxFont::SetFamily
1019
1020 void wxFont::SetStyle(
1021 int nStyle
1022 )
1023 {
1024 Unshare();
1025
1026 M_FONTDATA->SetStyle(nStyle);
1027
1028 RealizeResource();
1029 } // end of wxFont::SetStyle
1030
1031 void wxFont::SetWeight(
1032 int nWeight
1033 )
1034 {
1035 Unshare();
1036
1037 M_FONTDATA->SetWeight(nWeight);
1038
1039 RealizeResource();
1040 } // end of wxFont::SetWeight
1041
1042 void wxFont::SetFaceName(
1043 const wxString& rsFaceName
1044 )
1045 {
1046 Unshare();
1047
1048 M_FONTDATA->SetFaceName(rsFaceName);
1049
1050 RealizeResource();
1051 } // end of wxFont::SetFaceName
1052
1053 void wxFont::SetUnderlined(
1054 bool bUnderlined
1055 )
1056 {
1057 Unshare();
1058
1059 M_FONTDATA->SetUnderlined(bUnderlined);
1060
1061 RealizeResource();
1062 } // end of wxFont::SetUnderlined
1063
1064 void wxFont::SetEncoding(
1065 wxFontEncoding vEncoding
1066 )
1067 {
1068 Unshare();
1069
1070 M_FONTDATA->SetEncoding(vEncoding);
1071
1072 RealizeResource();
1073 } // end of wxFont::SetEncoding
1074
1075 void wxFont::DoSetNativeFontInfo(
1076 const wxNativeFontInfo& rInfo
1077 )
1078 {
1079 Unshare();
1080
1081 FreeResource();
1082
1083 *M_FONTDATA = wxFontRefData(rInfo);
1084
1085 RealizeResource();
1086 }
1087
1088 // ----------------------------------------------------------------------------
1089 // accessors
1090 // ----------------------------------------------------------------------------
1091
1092 int wxFont::GetPointSize() const
1093 {
1094 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1095
1096 return M_FONTDATA->GetPointSize();
1097 } // end of wxFont::GetPointSize
1098
1099 int wxFont::GetFamily() const
1100 {
1101 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1102
1103 return M_FONTDATA->GetFamily();
1104 } // end of wxFont::GetFamily
1105
1106 int wxFont::GetStyle() const
1107 {
1108 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1109
1110 return M_FONTDATA->GetStyle();
1111 } // end of wxFont::GetStyle
1112
1113 int wxFont::GetWeight() const
1114 {
1115 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1116
1117 return M_FONTDATA->GetWeight();
1118 }
1119
1120 bool wxFont::GetUnderlined() const
1121 {
1122 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
1123
1124 return M_FONTDATA->GetUnderlined();
1125 } // end of wxFont::GetUnderlined
1126
1127 wxString wxFont::GetFaceName() const
1128 {
1129 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
1130
1131 return M_FONTDATA->GetFaceName();
1132 } // end of wxFont::GetFaceName
1133
1134 wxFontEncoding wxFont::GetEncoding() const
1135 {
1136 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
1137
1138 return M_FONTDATA->GetEncoding();
1139 } // end of wxFont::GetEncoding
1140
1141 const wxNativeFontInfo* wxFont::GetNativeFontInfo() const
1142 {
1143 return M_FONTDATA->HasNativeFontInfo() ? &(M_FONTDATA->GetNativeFontInfo())
1144 : NULL;
1145 } // end of wxFont::GetNativeFontInfo
1146
1147 //
1148 // Internal use only method to set the FONTMETRICS array
1149 //
1150 void 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
1158
1159
1160 void wxFont::SetPS(
1161 HPS hPS
1162 )
1163 {
1164 Unshare();
1165
1166 M_FONTDATA->SetPS(hPS);
1167
1168 RealizeResource();
1169 } // end of wxFont::SetPS
1170