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() { 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() ;
211 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
214 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
217 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
220 ::GetFNum( "\pGeneva" , &m_macFontNum
) ;
223 ::GetFNum( "\pMonaco" , &m_macFontNum
) ;
227 ::GetFontName( m_macFontNum
, name
) ;
228 m_faceName
= wxMacMakeStringFromPascal( name
) ;
232 if ( m_faceName
== wxT("systemfont") )
233 m_macFontNum
= ::GetSysFont() ;
234 else if ( m_faceName
== wxT("applicationfont") )
235 m_macFontNum
= ::GetAppFont() ;
239 wxMacStringToPascal( m_faceName
, fontname
) ;
240 ::GetFNum( fontname
, &m_macFontNum
);
245 if (m_weight
== wxBOLD
)
246 m_macFontStyle
|= bold
;
247 if (m_style
== wxITALIC
|| m_style
== wxSLANT
)
248 m_macFontStyle
|= italic
;
250 m_macFontStyle
|= underline
;
251 m_macFontSize
= m_pointSize
;
254 // we try to get as much styles as possible into ATSU
255 Style atsuStyle
= normal
;
256 verify_noerr(::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
, (UInt32
*)&m_macATSUFontID
) );
257 if ( m_macFontStyle
& bold
)
260 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| bold
, &test
) == noErr
)
263 m_macATSUFontID
= test
;
266 if ( m_macFontStyle
& italic
)
269 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| italic
, &test
) == noErr
)
271 atsuStyle
|= italic
;
272 m_macATSUFontID
= test
;
275 if ( m_macFontStyle
& underline
)
278 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| underline
, &test
) == noErr
)
280 atsuStyle
|= underline
;
281 m_macATSUFontID
= test
;
284 m_macATSUAdditionalQDStyles
= m_macFontStyle
& (~atsuStyle
) ;
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
295 bool wxFont::Create(const wxNativeFontInfo
& info
)
297 return Create(info
.pointSize
, info
.family
, info
.style
, info
.weight
,
298 info
.underlined
, info
.faceName
, info
.encoding
);
301 wxFont::wxFont(const wxString
& fontdesc
)
303 wxNativeFontInfo info
;
304 if ( info
.FromString(fontdesc
) )
308 bool wxFont::Create(int pointSize
,
313 const wxString
& faceName
,
314 wxFontEncoding encoding
)
317 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
318 underlined
, faceName
, encoding
);
325 bool wxFont::MacCreateThemeFont(wxUint16 themeFontID
)
328 m_refData
= new wxFontRefData(12, 0, 0, wxNORMAL
,false, wxEmptyString
, wxFONTENCODING_DEFAULT
);
329 M_FONTDATA
->m_macThemeFontID
= themeFontID
;
339 bool wxFont::RealizeResource()
341 M_FONTDATA
->MacFindFont() ;
345 void wxFont::SetEncoding(wxFontEncoding encoding
)
349 M_FONTDATA
->m_encoding
= encoding
;
354 void wxFont::Unshare()
356 // Don't change shared data
359 m_refData
= new wxFontRefData();
363 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
369 void wxFont::SetPointSize(int pointSize
)
373 M_FONTDATA
->m_pointSize
= pointSize
;
378 void wxFont::SetFamily(int family
)
382 M_FONTDATA
->m_family
= family
;
387 void wxFont::SetStyle(int style
)
391 M_FONTDATA
->m_style
= style
;
396 void wxFont::SetWeight(int weight
)
400 M_FONTDATA
->m_weight
= weight
;
405 void wxFont::SetFaceName(const wxString
& faceName
)
409 M_FONTDATA
->m_faceName
= faceName
;
414 void wxFont::SetUnderlined(bool underlined
)
418 M_FONTDATA
->m_underlined
= underlined
;
423 void wxFont::SetNoAntiAliasing( bool no
)
427 M_FONTDATA
->SetNoAntiAliasing( no
);
432 // ----------------------------------------------------------------------------
434 // ----------------------------------------------------------------------------
436 // TODO: insert checks everywhere for M_FONTDATA == NULL!
438 int wxFont::GetPointSize() const
440 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
441 return M_FONTDATA
->m_pointSize
;
444 int wxFont::GetFamily() const
446 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
447 return M_FONTDATA
->m_family
;
450 int wxFont::GetStyle() const
452 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
453 return M_FONTDATA
->m_style
;
456 int wxFont::GetWeight() const
458 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
459 return M_FONTDATA
->m_weight
;
462 bool wxFont::GetUnderlined() const
464 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
465 return M_FONTDATA
->m_underlined
;
468 wxString
wxFont::GetFaceName() const
470 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxEmptyString
, wxT("invalid font") );
471 return M_FONTDATA
->m_faceName
;
474 wxFontEncoding
wxFont::GetEncoding() const
476 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTENCODING_DEFAULT
, wxT("invalid font") );
477 return M_FONTDATA
->m_encoding
;
480 bool wxFont::GetNoAntiAliasing()
482 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
483 return M_FONTDATA
->m_noAA
;
486 short wxFont::MacGetFontNum() const
488 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
489 return M_FONTDATA
->m_macFontNum
;
492 short wxFont::MacGetFontSize() const
494 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
495 return M_FONTDATA
->m_macFontSize
;
498 wxByte
wxFont::MacGetFontStyle() const
500 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
501 return M_FONTDATA
->m_macFontStyle
;
504 wxUint32
wxFont::MacGetATSUFontID() const
506 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
507 return M_FONTDATA
->m_macATSUFontID
;
510 wxUint32
wxFont::MacGetATSUAdditionalQDStyles() const
512 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
513 return M_FONTDATA
->m_macATSUAdditionalQDStyles
;
516 wxUint16
wxFont::MacGetThemeFontID() const
518 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
519 return M_FONTDATA
->m_macThemeFontID
;
523 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
525 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
526 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
528 M_FONTDATA
->m_info
.InitFromFont(*this);
530 return &(M_FONTDATA
->m_info
);