]> git.saurik.com Git - wxWidgets.git/blob - src/os2/font.cpp
signed/unsigned comparison warning (and probably even error) fixed
[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 (hPS == NULLHANDLE)
352 {
353 m_hPS = ::WinGetPS(HWND_DESKTOP);
354 m_bInternalPS = TRUE;
355 }
356 else
357 m_hPS = (HPS)hPS;
358
359 m_nFontId = 0;
360 m_bTemporary = FALSE;
361 m_pFM = (PFONTMETRICS)NULL;
362 m_nNumFonts = 0;
363 } // end of wxFontRefData::Init
364
365 wxFontRefData::~wxFontRefData()
366 {
367 Free();
368 }
369
370 bool wxFontRefData::Alloc(
371 wxFont* pFont
372 )
373 {
374 wxString sFaceName;
375 long flId = m_hFont;
376 long lRc;
377 short nIndex = 0;
378 PFONTMETRICS pFM = NULL;
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::SetNativeFontInfo(
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 wxNativeFontInfo* wxFont::GetNativeFontInfo() const
1142 {
1143 if (M_FONTDATA->HasNativeFontInfo())
1144 return new wxNativeFontInfo(M_FONTDATA->GetNativeFontInfo());
1145 return 0;
1146 } // end of wxFont::GetNativeFontInfo
1147
1148 //
1149 // Internal use only method to set the FONTMETRICS array
1150 //
1151 void wxFont::SetFM(
1152 PFONTMETRICS pFM
1153 , int nNumFonts
1154 )
1155 {
1156 M_FONTDATA->SetFM(pFM);
1157 M_FONTDATA->SetNumFonts(nNumFonts);
1158 } // end of wxFont::SetFM
1159
1160
1161 void wxFont::SetPS(
1162 HPS hPS
1163 )
1164 {
1165 Unshare();
1166
1167 M_FONTDATA->SetPS(hPS);
1168
1169 RealizeResource();
1170 } // end of wxFont::SetPS
1171