1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFont class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "font.h"
17 #include "wx/string.h"
19 #include "wx/fontutil.h"
20 #include "wx/gdicmn.h"
23 #include "wx/fontutil.h"
25 #include "wx/mac/private.h"
26 #include <ATSUnicode.h>
28 #if !USE_SHARED_LIBRARIES
29 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
32 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
34 friend class WXDLLEXPORT wxFont
;
43 , m_faceName(wxT("Geneva"))
44 , m_encoding(wxFONTENCODING_DEFAULT
)
50 Init(10, wxDEFAULT
, wxNORMAL
, wxNORMAL
, FALSE
,
51 wxT("Geneva"), wxFONTENCODING_DEFAULT
);
54 wxFontRefData(const wxFontRefData
& data
)
56 , m_fontId(data
.m_fontId
)
57 , m_pointSize(data
.m_pointSize
)
58 , m_family(data
.m_family
)
59 , m_style(data
.m_style
)
60 , m_weight(data
.m_weight
)
61 , m_underlined(data
.m_underlined
)
62 , m_faceName(data
.m_faceName
)
63 , m_encoding(data
.m_encoding
)
64 , m_macFontNum(data
.m_macFontNum
)
65 , m_macFontSize(data
.m_macFontSize
)
66 , m_macFontStyle(data
.m_macFontStyle
)
67 , m_macATSUFontID(data
.m_macATSUFontID
)
69 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
70 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
73 wxFontRefData(int size
,
78 const wxString
& faceName
,
79 wxFontEncoding encoding
)
85 , m_underlined(underlined
)
86 , m_faceName(faceName
)
87 , m_encoding(encoding
)
93 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
96 virtual ~wxFontRefData();
97 void SetNoAntiAliasing( bool no
= TRUE
) { m_noAA
= no
; }
98 bool GetNoAntiAliasing() const { return m_noAA
; }
101 // common part of all ctors
107 const wxString
& faceName
,
108 wxFontEncoding encoding
);
110 // font characterstics
118 wxFontEncoding m_encoding
;
119 bool m_noAA
; // No anti-aliasing
124 Style m_macFontStyle
;
126 // ATSU Font Information
128 // this is splitted into an ATSU font id that may
129 // contain some styles (special bold fonts etc) and
130 // these are the additional qd styles that are not
131 // included in the ATSU font id
132 ATSUFontID m_macATSUFontID
;
133 Style m_macATSUAdditionalQDStyles
;
135 // for true themeing support we must store the correct font
136 // information here, as this speeds up and optimizes rendering
137 ThemeFontID m_macThemeFontID
;
139 wxNativeFontInfo m_info
;
144 // ============================================================================
146 // ============================================================================
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 void wxFontRefData::Init(int pointSize
,
157 const wxString
& faceName
,
158 wxFontEncoding encoding
)
161 m_pointSize
= pointSize
;
165 m_underlined
= underlined
;
166 m_faceName
= faceName
;
167 m_encoding
= encoding
;
173 m_macATSUAdditionalQDStyles
= 0 ;
175 m_macThemeFontID
= kThemeCurrentPortFont
;
179 wxFontRefData::~wxFontRefData()
183 void wxFontRefData::MacFindFont()
185 if ( m_macThemeFontID
!= kThemeCurrentPortFont
)
188 GetThemeFont(m_macThemeFontID
, GetApplicationScript() , fontName
, &m_macFontSize
, &m_macFontStyle
) ;
189 m_faceName
= wxMacMakeStringFromPascal( fontName
) ;
190 if ( m_macFontStyle
& bold
)
193 m_weight
= wxNORMAL
;
194 if ( m_macFontStyle
& italic
)
196 if ( m_macFontStyle
& underline
)
197 m_underlined
= true ;
198 ::GetFNum( fontName
, &m_macFontNum
);
199 m_pointSize
= m_macFontSize
;
203 if( m_faceName
.Length() == 0 )
208 m_macFontNum
= ::GetAppFont() ;
214 ::GetFNum( "\pTimes New Roman" , &m_macFontNum
) ;
216 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
221 ::GetFNum( "\pLucida Grande" , &m_macFontNum
) ;
223 ::GetFNum( "\pGeneva" , &m_macFontNum
) ;
228 ::GetFNum( "\pMonaco" , &m_macFontNum
) ;
230 ::GetFNum( "\pMonaco" , &m_macFontNum
) ;
235 ::GetFontName( m_macFontNum
, name
) ;
236 m_faceName
= wxMacMakeStringFromPascal( name
) ;
240 if ( m_faceName
== wxT("systemfont") )
241 m_macFontNum
= ::GetSysFont() ;
242 else if ( m_faceName
== wxT("applicationfont") )
243 m_macFontNum
= ::GetAppFont() ;
247 wxMacStringToPascal( m_faceName
, fontname
) ;
248 ::GetFNum( fontname
, &m_macFontNum
);
253 if (m_weight
== wxBOLD
)
254 m_macFontStyle
|= bold
;
255 if (m_style
== wxITALIC
|| m_style
== wxSLANT
)
256 m_macFontStyle
|= italic
;
258 m_macFontStyle
|= underline
;
259 m_macFontSize
= m_pointSize
;
262 // we try to get as much styles as possible into ATSU
263 Style atsuStyle
= normal
;
264 verify_noerr(::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
, (UInt32
*)&m_macATSUFontID
) );
265 if ( m_macFontStyle
& bold
)
268 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| bold
, &test
) == noErr
)
271 m_macATSUFontID
= test
;
274 if ( m_macFontStyle
& italic
)
277 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| italic
, &test
) == noErr
)
279 atsuStyle
|= italic
;
280 m_macATSUFontID
= test
;
283 if ( m_macFontStyle
& underline
)
286 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| underline
, &test
) == noErr
)
288 atsuStyle
|= underline
;
289 m_macATSUFontID
= test
;
292 m_macATSUAdditionalQDStyles
= m_macFontStyle
& (~atsuStyle
) ;
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
303 bool wxFont::Create(const wxNativeFontInfo
& info
)
305 return Create(info
.pointSize
, info
.family
, info
.style
, info
.weight
,
306 info
.underlined
, info
.faceName
, info
.encoding
);
309 wxFont::wxFont(const wxString
& fontdesc
)
311 wxNativeFontInfo info
;
312 if ( info
.FromString(fontdesc
) )
316 bool wxFont::Create(int pointSize
,
321 const wxString
& faceName
,
322 wxFontEncoding encoding
)
325 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
326 underlined
, faceName
, encoding
);
333 bool wxFont::MacCreateThemeFont(wxUint16 themeFontID
)
336 m_refData
= new wxFontRefData(12, 0, 0, wxNORMAL
,false, wxEmptyString
, wxFONTENCODING_DEFAULT
);
337 M_FONTDATA
->m_macThemeFontID
= themeFontID
;
347 bool wxFont::RealizeResource()
349 M_FONTDATA
->MacFindFont() ;
353 void wxFont::SetEncoding(wxFontEncoding encoding
)
357 M_FONTDATA
->m_encoding
= encoding
;
362 void wxFont::Unshare()
364 // Don't change shared data
367 m_refData
= new wxFontRefData();
371 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
377 void wxFont::SetPointSize(int pointSize
)
381 M_FONTDATA
->m_pointSize
= pointSize
;
386 void wxFont::SetFamily(int family
)
390 M_FONTDATA
->m_family
= family
;
395 void wxFont::SetStyle(int style
)
399 M_FONTDATA
->m_style
= style
;
404 void wxFont::SetWeight(int weight
)
408 M_FONTDATA
->m_weight
= weight
;
413 void wxFont::SetFaceName(const wxString
& faceName
)
417 M_FONTDATA
->m_faceName
= faceName
;
422 void wxFont::SetUnderlined(bool underlined
)
426 M_FONTDATA
->m_underlined
= underlined
;
431 void wxFont::SetNoAntiAliasing( bool no
)
435 M_FONTDATA
->SetNoAntiAliasing( no
);
440 // ----------------------------------------------------------------------------
442 // ----------------------------------------------------------------------------
444 // TODO: insert checks everywhere for M_FONTDATA == NULL!
446 int wxFont::GetPointSize() const
448 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
449 return M_FONTDATA
->m_pointSize
;
452 int wxFont::GetFamily() const
454 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
455 return M_FONTDATA
->m_family
;
458 int wxFont::GetStyle() const
460 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
461 return M_FONTDATA
->m_style
;
464 int wxFont::GetWeight() const
466 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
467 return M_FONTDATA
->m_weight
;
470 bool wxFont::GetUnderlined() const
472 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
473 return M_FONTDATA
->m_underlined
;
476 wxString
wxFont::GetFaceName() const
478 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxEmptyString
, wxT("invalid font") );
479 return M_FONTDATA
->m_faceName
;
482 wxFontEncoding
wxFont::GetEncoding() const
484 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTENCODING_DEFAULT
, wxT("invalid font") );
485 return M_FONTDATA
->m_encoding
;
488 bool wxFont::GetNoAntiAliasing() const
490 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
491 return M_FONTDATA
->m_noAA
;
494 short wxFont::MacGetFontNum() const
496 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
497 return M_FONTDATA
->m_macFontNum
;
500 short wxFont::MacGetFontSize() const
502 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
503 return M_FONTDATA
->m_macFontSize
;
506 wxByte
wxFont::MacGetFontStyle() const
508 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
509 return M_FONTDATA
->m_macFontStyle
;
512 wxUint32
wxFont::MacGetATSUFontID() const
514 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
515 return M_FONTDATA
->m_macATSUFontID
;
518 wxUint32
wxFont::MacGetATSUAdditionalQDStyles() const
520 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
521 return M_FONTDATA
->m_macATSUAdditionalQDStyles
;
524 wxUint16
wxFont::MacGetThemeFontID() const
526 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
527 return M_FONTDATA
->m_macThemeFontID
;
531 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
533 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
534 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
536 M_FONTDATA
->m_info
.InitFromFont(*this);
538 return &(M_FONTDATA
->m_info
);