1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFont class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "font.h"
16 #include "wx/wxprec.h"
18 #include "wx/string.h"
20 #include "wx/fontutil.h"
21 #include "wx/gdicmn.h"
24 #include "wx/fontutil.h"
26 #include "wx/mac/private.h"
28 #include <ATSUnicode.h>
31 #if !USE_SHARED_LIBRARIES
32 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
35 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
37 friend class WXDLLEXPORT wxFont
;
46 , m_faceName(wxT("applicationfont"))
47 , m_encoding(wxFONTENCODING_DEFAULT
)
54 Init(m_pointSize
, m_family
, m_style
, m_weight
,
55 m_underlined
, m_faceName
, m_encoding
);
58 wxFontRefData(const wxFontRefData
& data
)
60 , m_fontId(data
.m_fontId
)
61 , m_pointSize(data
.m_pointSize
)
62 , m_family(data
.m_family
)
63 , m_style(data
.m_style
)
64 , m_weight(data
.m_weight
)
65 , m_underlined(data
.m_underlined
)
66 , m_faceName(data
.m_faceName
)
67 , m_encoding(data
.m_encoding
)
68 , m_macFontNum(data
.m_macFontNum
)
69 , m_macFontSize(data
.m_macFontSize
)
70 , m_macFontStyle(data
.m_macFontStyle
)
72 , m_macATSUFontID(data
.m_macATSUFontID
)
74 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
75 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
78 wxFontRefData(int size
,
83 const wxString
& faceName
,
84 wxFontEncoding encoding
)
90 , m_underlined(underlined
)
91 , m_faceName(faceName
)
92 , m_encoding(encoding
)
99 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
102 virtual ~wxFontRefData();
103 void SetNoAntiAliasing( bool no
= TRUE
) { m_noAA
= no
; }
104 bool GetNoAntiAliasing() const { return m_noAA
; }
107 // common part of all ctors
113 const wxString
& faceName
,
114 wxFontEncoding encoding
);
116 // font characterstics
124 wxFontEncoding m_encoding
;
125 bool m_noAA
; // No anti-aliasing
130 Style m_macFontStyle
;
132 // ATSU Font Information
134 // this is split into an ATSU font id that may
135 // contain some styles (special bold fonts etc) and
136 // these are the additional qd styles that are not
137 // included in the ATSU font id
138 ATSUStyle m_macATSUStyle
;
139 ATSUFontID m_macATSUFontID
;
140 Style m_macATSUAdditionalQDStyles
;
142 // for true themeing support we must store the correct font
143 // information here, as this speeds up and optimizes rendering
144 ThemeFontID m_macThemeFontID
;
146 wxNativeFontInfo m_info
;
151 // ============================================================================
153 // ============================================================================
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 void wxFontRefData::Init(int pointSize
,
164 const wxString
& faceName
,
165 wxFontEncoding encoding
)
168 m_pointSize
= pointSize
;
172 m_underlined
= underlined
;
173 m_faceName
= faceName
;
174 m_encoding
= encoding
;
180 m_macATSUAdditionalQDStyles
= 0 ;
181 m_macATSUStyle
= NULL
;
183 m_macThemeFontID
= kThemeCurrentPortFont
;
187 wxFontRefData::~wxFontRefData()
189 if ( m_macATSUStyle
)
191 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
192 m_macATSUStyle
= NULL
;
196 void wxFontRefData::MacFindFont()
198 if ( m_macThemeFontID
!= kThemeCurrentPortFont
)
201 GetThemeFont(m_macThemeFontID
, GetApplicationScript() , fontName
, &m_macFontSize
, &m_macFontStyle
) ;
202 m_faceName
= wxMacMakeStringFromPascal( fontName
) ;
203 if ( m_macFontStyle
& bold
)
206 m_weight
= wxNORMAL
;
207 if ( m_macFontStyle
& italic
)
209 if ( m_macFontStyle
& underline
)
210 m_underlined
= true ;
211 ::GetFNum( fontName
, &m_macFontNum
);
212 m_pointSize
= m_macFontSize
;
216 if( m_faceName
.Length() == 0 )
221 m_macFontNum
= ::GetAppFont() ;
227 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
229 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
234 ::GetFNum( "\pLucida Grande" , &m_macFontNum
) ;
236 ::GetFNum( "\pGeneva" , &m_macFontNum
) ;
241 ::GetFNum( "\pMonaco" , &m_macFontNum
) ;
243 ::GetFNum( "\pMonaco" , &m_macFontNum
) ;
248 ::GetFontName( m_macFontNum
, name
) ;
249 m_faceName
= wxMacMakeStringFromPascal( name
) ;
253 if ( m_faceName
== wxT("systemfont") )
254 m_macFontNum
= ::GetSysFont() ;
255 else if ( m_faceName
== wxT("applicationfont") )
256 m_macFontNum
= ::GetAppFont() ;
260 wxMacStringToPascal( m_faceName
, fontname
) ;
261 ::GetFNum( fontname
, &m_macFontNum
);
266 if (m_weight
== wxBOLD
)
267 m_macFontStyle
|= bold
;
268 if (m_style
== wxITALIC
|| m_style
== wxSLANT
)
269 m_macFontStyle
|= italic
;
271 m_macFontStyle
|= underline
;
272 m_macFontSize
= m_pointSize
;
275 // we try to get as much styles as possible into ATSU
277 Fixed atsuSize
= IntToFixed( m_macFontSize
) ;
279 Style atsuStyle
= normal
;
280 verify_noerr(::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
, (UInt32
*)&m_macATSUFontID
) );
281 if ( m_macFontStyle
& bold
)
284 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| bold
, &test
) == noErr
)
287 m_macATSUFontID
= test
;
290 if ( m_macFontStyle
& italic
)
293 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| italic
, &test
) == noErr
)
295 atsuStyle
|= italic
;
296 m_macATSUFontID
= test
;
299 if ( m_macFontStyle
& underline
)
302 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| underline
, &test
) == noErr
)
304 atsuStyle
|= underline
;
305 m_macATSUFontID
= test
;
309 m_macATSUAdditionalQDStyles
= m_macFontStyle
& (~atsuStyle
) ;
311 if ( m_macATSUStyle
)
313 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
314 m_macATSUStyle
= NULL
;
316 OSStatus status
= ::ATSUCreateStyle((ATSUStyle
*)&m_macATSUStyle
) ;
317 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") ) ;
318 ATSUAttributeTag atsuTags
[] =
322 kATSUVerticalCharacterTag
,
325 kATSUQDUnderlineTag
,
326 kATSUQDCondensedTag
,
329 ByteCount atsuSizes
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
331 sizeof( ATSUFontID
) ,
333 sizeof( ATSUVerticalCharacterType
),
340 Boolean kTrue
= true ;
341 Boolean kFalse
= false ;
343 ATSUVerticalCharacterType kHorizontal
= kATSUStronglyHorizontal
;
344 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
349 (m_macATSUAdditionalQDStyles
& bold
) ? &kTrue
: &kFalse
,
350 (m_macATSUAdditionalQDStyles
& italic
) ? &kTrue
: &kFalse
,
351 (m_macATSUAdditionalQDStyles
& underline
) ? &kTrue
: &kFalse
,
352 (m_macATSUAdditionalQDStyles
& condense
) ? &kTrue
: &kFalse
,
353 (m_macATSUAdditionalQDStyles
& extend
) ? &kTrue
: &kFalse
,
355 status
= ::ATSUSetAttributes((ATSUStyle
)m_macATSUStyle
, sizeof(atsuTags
)/sizeof(ATSUAttributeTag
) ,
356 atsuTags
, atsuSizes
, atsuValues
);
358 wxASSERT_MSG( status
== noErr
, wxT("couldn't Modify ATSU style") ) ;
361 // ----------------------------------------------------------------------------
363 // ----------------------------------------------------------------------------
369 bool wxFont::Create(const wxNativeFontInfo
& info
)
371 return Create(info
.pointSize
, info
.family
, info
.style
, info
.weight
,
372 info
.underlined
, info
.faceName
, info
.encoding
);
375 wxFont::wxFont(const wxString
& fontdesc
)
377 wxNativeFontInfo info
;
378 if ( info
.FromString(fontdesc
) )
382 bool wxFont::Create(int pointSize
,
387 const wxString
& faceName
,
388 wxFontEncoding encoding
)
391 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
392 underlined
, faceName
, encoding
);
399 bool wxFont::MacCreateThemeFont(wxUint16 themeFontID
)
402 m_refData
= new wxFontRefData(12, wxDEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
,false, wxEmptyString
, wxFONTENCODING_DEFAULT
);
403 M_FONTDATA
->m_macThemeFontID
= themeFontID
;
413 bool wxFont::RealizeResource()
415 M_FONTDATA
->MacFindFont() ;
419 void wxFont::SetEncoding(wxFontEncoding encoding
)
423 M_FONTDATA
->m_encoding
= encoding
;
428 void wxFont::Unshare()
430 // Don't change shared data
433 m_refData
= new wxFontRefData();
437 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
443 void wxFont::SetPointSize(int pointSize
)
447 M_FONTDATA
->m_pointSize
= pointSize
;
452 void wxFont::SetFamily(int family
)
456 M_FONTDATA
->m_family
= family
;
461 void wxFont::SetStyle(int style
)
465 M_FONTDATA
->m_style
= style
;
470 void wxFont::SetWeight(int weight
)
474 M_FONTDATA
->m_weight
= weight
;
479 void wxFont::SetFaceName(const wxString
& faceName
)
483 M_FONTDATA
->m_faceName
= faceName
;
488 void wxFont::SetUnderlined(bool underlined
)
492 M_FONTDATA
->m_underlined
= underlined
;
497 void wxFont::SetNoAntiAliasing( bool no
)
501 M_FONTDATA
->SetNoAntiAliasing( no
);
506 // ----------------------------------------------------------------------------
508 // ----------------------------------------------------------------------------
510 // TODO: insert checks everywhere for M_FONTDATA == NULL!
512 int wxFont::GetPointSize() const
514 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
515 return M_FONTDATA
->m_pointSize
;
518 int wxFont::GetFamily() const
520 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
521 return M_FONTDATA
->m_family
;
524 int wxFont::GetStyle() const
526 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
527 return M_FONTDATA
->m_style
;
530 int wxFont::GetWeight() const
532 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
533 return M_FONTDATA
->m_weight
;
536 bool wxFont::GetUnderlined() const
538 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
539 return M_FONTDATA
->m_underlined
;
542 wxString
wxFont::GetFaceName() const
544 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxEmptyString
, wxT("invalid font") );
545 return M_FONTDATA
->m_faceName
;
548 wxFontEncoding
wxFont::GetEncoding() const
550 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTENCODING_DEFAULT
, wxT("invalid font") );
551 return M_FONTDATA
->m_encoding
;
554 bool wxFont::GetNoAntiAliasing() const
556 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
557 return M_FONTDATA
->m_noAA
;
560 short wxFont::MacGetFontNum() const
562 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
563 return M_FONTDATA
->m_macFontNum
;
566 short wxFont::MacGetFontSize() const
568 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
569 return M_FONTDATA
->m_macFontSize
;
572 wxByte
wxFont::MacGetFontStyle() const
574 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
575 return M_FONTDATA
->m_macFontStyle
;
578 wxUint32
wxFont::MacGetATSUFontID() const
580 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
581 return M_FONTDATA
->m_macATSUFontID
;
584 void* wxFont::MacGetATSUStyle() const
586 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
587 return M_FONTDATA
->m_macATSUStyle
;
590 wxUint32
wxFont::MacGetATSUAdditionalQDStyles() const
592 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
593 return M_FONTDATA
->m_macATSUAdditionalQDStyles
;
596 wxUint16
wxFont::MacGetThemeFontID() const
598 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
599 return M_FONTDATA
->m_macThemeFontID
;
603 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
605 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
606 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
608 M_FONTDATA
->m_info
.InitFromFont(*this);
610 return &(M_FONTDATA
->m_info
);