1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFont class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/string.h"
16 #include "wx/fontutil.h"
17 #include "wx/gdicmn.h"
20 #include "wx/fontutil.h"
22 #include "wx/mac/private.h"
24 #include <ATSUnicode.h>
27 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
29 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
31 friend class WXDLLEXPORT wxFont
;
40 , m_faceName(wxT("applicationfont"))
41 , m_encoding(wxFONTENCODING_DEFAULT
)
48 Init(m_pointSize
, m_family
, m_style
, m_weight
,
49 m_underlined
, m_faceName
, m_encoding
);
52 wxFontRefData(const wxFontRefData
& data
)
54 , m_fontId(data
.m_fontId
)
55 , m_pointSize(data
.m_pointSize
)
56 , m_family(data
.m_family
)
57 , m_style(data
.m_style
)
58 , m_weight(data
.m_weight
)
59 , m_underlined(data
.m_underlined
)
60 , m_faceName(data
.m_faceName
)
61 , m_encoding(data
.m_encoding
)
62 , m_macFontNum(data
.m_macFontNum
)
63 , m_macFontSize(data
.m_macFontSize
)
64 , m_macFontStyle(data
.m_macFontStyle
)
66 , m_macATSUFontID(data
.m_macATSUFontID
)
68 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
69 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
72 wxFontRefData(int size
,
77 const wxString
& faceName
,
78 wxFontEncoding encoding
)
84 , m_underlined(underlined
)
85 , m_faceName(faceName
)
86 , 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 split 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 ATSUStyle m_macATSUStyle
;
133 ATSUFontID m_macATSUFontID
;
134 Style m_macATSUAdditionalQDStyles
;
136 // for true themeing support we must store the correct font
137 // information here, as this speeds up and optimizes rendering
138 ThemeFontID m_macThemeFontID
;
140 wxNativeFontInfo m_info
;
145 // ============================================================================
147 // ============================================================================
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 void wxFontRefData::Init(int pointSize
,
158 const wxString
& faceName
,
159 wxFontEncoding encoding
)
162 m_pointSize
= pointSize
;
166 m_underlined
= underlined
;
167 m_faceName
= faceName
;
168 m_encoding
= encoding
;
174 m_macATSUAdditionalQDStyles
= 0 ;
175 m_macATSUStyle
= NULL
;
177 m_macThemeFontID
= kThemeCurrentPortFont
;
181 wxFontRefData::~wxFontRefData()
183 if ( m_macATSUStyle
)
185 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
186 m_macATSUStyle
= NULL
;
190 void wxFontRefData::MacFindFont()
192 if ( m_macThemeFontID
!= kThemeCurrentPortFont
)
195 GetThemeFont(m_macThemeFontID
, GetApplicationScript() , fontName
, &m_macFontSize
, &m_macFontStyle
) ;
196 m_faceName
= wxMacMakeStringFromPascal( fontName
) ;
197 if ( m_macFontStyle
& bold
)
200 m_weight
= wxNORMAL
;
201 if ( m_macFontStyle
& italic
)
203 if ( m_macFontStyle
& underline
)
204 m_underlined
= true ;
205 ::GetFNum( fontName
, &m_macFontNum
);
206 m_pointSize
= m_macFontSize
;
210 if( m_faceName
.Length() == 0 )
215 m_macFontNum
= ::GetAppFont() ;
221 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
223 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
228 ::GetFNum( "\pLucida Grande" , &m_macFontNum
) ;
230 ::GetFNum( "\pGeneva" , &m_macFontNum
) ;
235 ::GetFNum( "\pMonaco" , &m_macFontNum
) ;
237 ::GetFNum( "\pMonaco" , &m_macFontNum
) ;
242 ::GetFontName( m_macFontNum
, name
) ;
243 m_faceName
= wxMacMakeStringFromPascal( name
) ;
247 if ( m_faceName
== wxT("systemfont") )
248 m_macFontNum
= ::GetSysFont() ;
249 else if ( m_faceName
== wxT("applicationfont") )
250 m_macFontNum
= ::GetAppFont() ;
254 wxMacStringToPascal( m_faceName
, fontname
) ;
255 ::GetFNum( fontname
, &m_macFontNum
);
260 if (m_weight
== wxBOLD
)
261 m_macFontStyle
|= bold
;
262 if (m_style
== wxITALIC
|| m_style
== wxSLANT
)
263 m_macFontStyle
|= italic
;
265 m_macFontStyle
|= underline
;
266 m_macFontSize
= m_pointSize
;
269 // we try to get as much styles as possible into ATSU
271 Fixed atsuSize
= IntToFixed( m_macFontSize
) ;
273 Style atsuStyle
= normal
;
274 verify_noerr(::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
, (UInt32
*)&m_macATSUFontID
) );
275 if ( m_macFontStyle
& bold
)
278 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| bold
, &test
) == noErr
)
281 m_macATSUFontID
= test
;
284 if ( m_macFontStyle
& italic
)
287 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| italic
, &test
) == noErr
)
289 atsuStyle
|= italic
;
290 m_macATSUFontID
= test
;
293 if ( m_macFontStyle
& underline
)
296 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| underline
, &test
) == noErr
)
298 atsuStyle
|= underline
;
299 m_macATSUFontID
= test
;
303 m_macATSUAdditionalQDStyles
= m_macFontStyle
& (~atsuStyle
) ;
305 if ( m_macATSUStyle
)
307 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
308 m_macATSUStyle
= NULL
;
310 OSStatus status
= ::ATSUCreateStyle((ATSUStyle
*)&m_macATSUStyle
) ;
311 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") ) ;
312 ATSUAttributeTag atsuTags
[] =
316 kATSUVerticalCharacterTag
,
319 kATSUQDUnderlineTag
,
320 kATSUQDCondensedTag
,
323 ByteCount atsuSizes
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
325 sizeof( ATSUFontID
) ,
327 sizeof( ATSUVerticalCharacterType
),
334 Boolean kTrue
= true ;
335 Boolean kFalse
= false ;
337 ATSUVerticalCharacterType kHorizontal
= kATSUStronglyHorizontal
;
338 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
343 (m_macATSUAdditionalQDStyles
& bold
) ? &kTrue
: &kFalse
,
344 (m_macATSUAdditionalQDStyles
& italic
) ? &kTrue
: &kFalse
,
345 (m_macATSUAdditionalQDStyles
& underline
) ? &kTrue
: &kFalse
,
346 (m_macATSUAdditionalQDStyles
& condense
) ? &kTrue
: &kFalse
,
347 (m_macATSUAdditionalQDStyles
& extend
) ? &kTrue
: &kFalse
,
349 status
= ::ATSUSetAttributes((ATSUStyle
)m_macATSUStyle
, sizeof(atsuTags
)/sizeof(ATSUAttributeTag
) ,
350 atsuTags
, atsuSizes
, atsuValues
);
352 wxASSERT_MSG( status
== noErr
, wxT("couldn't Modify ATSU style") ) ;
355 // ----------------------------------------------------------------------------
357 // ----------------------------------------------------------------------------
363 bool wxFont::Create(const wxNativeFontInfo
& info
)
365 return Create(info
.pointSize
, info
.family
, info
.style
, info
.weight
,
366 info
.underlined
, info
.faceName
, info
.encoding
);
369 wxFont::wxFont(const wxString
& fontdesc
)
371 wxNativeFontInfo info
;
372 if ( info
.FromString(fontdesc
) )
376 bool wxFont::Create(int pointSize
,
381 const wxString
& faceName
,
382 wxFontEncoding encoding
)
385 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
386 underlined
, faceName
, encoding
);
393 bool wxFont::MacCreateThemeFont(wxUint16 themeFontID
)
396 m_refData
= new wxFontRefData(12, wxDEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
,false, wxEmptyString
, wxFONTENCODING_DEFAULT
);
397 M_FONTDATA
->m_macThemeFontID
= themeFontID
;
407 bool wxFont::RealizeResource()
409 M_FONTDATA
->MacFindFont() ;
413 void wxFont::SetEncoding(wxFontEncoding encoding
)
417 M_FONTDATA
->m_encoding
= encoding
;
422 void wxFont::Unshare()
424 // Don't change shared data
427 m_refData
= new wxFontRefData();
431 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
437 void wxFont::SetPointSize(int pointSize
)
441 M_FONTDATA
->m_pointSize
= pointSize
;
446 void wxFont::SetFamily(int family
)
450 M_FONTDATA
->m_family
= family
;
455 void wxFont::SetStyle(int style
)
459 M_FONTDATA
->m_style
= style
;
464 void wxFont::SetWeight(int weight
)
468 M_FONTDATA
->m_weight
= weight
;
473 void wxFont::SetFaceName(const wxString
& faceName
)
477 M_FONTDATA
->m_faceName
= faceName
;
482 void wxFont::SetUnderlined(bool underlined
)
486 M_FONTDATA
->m_underlined
= underlined
;
491 void wxFont::SetNoAntiAliasing( bool no
)
495 M_FONTDATA
->SetNoAntiAliasing( no
);
500 // ----------------------------------------------------------------------------
502 // ----------------------------------------------------------------------------
504 // TODO: insert checks everywhere for M_FONTDATA == NULL!
506 int wxFont::GetPointSize() const
508 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
509 return M_FONTDATA
->m_pointSize
;
512 int wxFont::GetFamily() const
514 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
515 return M_FONTDATA
->m_family
;
518 int wxFont::GetStyle() const
520 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
521 return M_FONTDATA
->m_style
;
524 int wxFont::GetWeight() const
526 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
527 return M_FONTDATA
->m_weight
;
530 bool wxFont::GetUnderlined() const
532 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
533 return M_FONTDATA
->m_underlined
;
536 wxString
wxFont::GetFaceName() const
538 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxEmptyString
, wxT("invalid font") );
539 return M_FONTDATA
->m_faceName
;
542 wxFontEncoding
wxFont::GetEncoding() const
544 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTENCODING_DEFAULT
, wxT("invalid font") );
545 return M_FONTDATA
->m_encoding
;
548 bool wxFont::GetNoAntiAliasing() const
550 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
551 return M_FONTDATA
->m_noAA
;
554 short wxFont::MacGetFontNum() const
556 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
557 return M_FONTDATA
->m_macFontNum
;
560 short wxFont::MacGetFontSize() const
562 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
563 return M_FONTDATA
->m_macFontSize
;
566 wxByte
wxFont::MacGetFontStyle() const
568 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
569 return M_FONTDATA
->m_macFontStyle
;
572 wxUint32
wxFont::MacGetATSUFontID() const
574 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
575 return M_FONTDATA
->m_macATSUFontID
;
578 void* wxFont::MacGetATSUStyle() const
580 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
581 return M_FONTDATA
->m_macATSUStyle
;
584 wxUint32
wxFont::MacGetATSUAdditionalQDStyles() const
586 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
587 return M_FONTDATA
->m_macATSUAdditionalQDStyles
;
590 wxUint16
wxFont::MacGetThemeFontID() const
592 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
593 return M_FONTDATA
->m_macThemeFontID
;
597 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
599 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
600 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
602 M_FONTDATA
->m_info
.InitFromFont(*this);
604 return &(M_FONTDATA
->m_info
);