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"
19 #include "wx/fontutil.h"
21 #include "wx/mac/private.h"
24 #include <ATSUnicode.h>
28 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
31 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
33 friend class WXDLLEXPORT wxFont
;
43 , m_faceName(wxT("applicationfont"))
44 , m_encoding(wxFONTENCODING_DEFAULT
)
51 Init(m_pointSize
, m_family
, m_style
, m_weight
,
52 m_underlined
, m_faceName
, m_encoding
);
55 wxFontRefData(const wxFontRefData
& data
)
57 , m_fontId(data
.m_fontId
)
58 , m_pointSize(data
.m_pointSize
)
59 , m_family(data
.m_family
)
60 , m_style(data
.m_style
)
61 , m_weight(data
.m_weight
)
62 , m_underlined(data
.m_underlined
)
63 , m_faceName(data
.m_faceName
)
64 , m_encoding(data
.m_encoding
)
65 , m_macFontNum(data
.m_macFontNum
)
66 , m_macFontSize(data
.m_macFontSize
)
67 , m_macFontStyle(data
.m_macFontStyle
)
69 , m_macATSUFontID(data
.m_macATSUFontID
)
71 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
72 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
75 wxFontRefData(int size
,
80 const wxString
& faceName
,
81 wxFontEncoding encoding
)
87 , m_underlined(underlined
)
88 , m_faceName(faceName
)
89 , m_encoding(encoding
)
96 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
99 virtual ~wxFontRefData();
101 void SetNoAntiAliasing( bool no
= true )
104 bool GetNoAntiAliasing() const
110 // common part of all ctors
116 const wxString
& faceName
,
117 wxFontEncoding encoding
);
119 // font characterstics
127 wxFontEncoding m_encoding
;
128 bool m_noAA
; // No anti-aliasing
133 Style m_macFontStyle
;
135 // ATSU Font Information
137 // this is split into an ATSU font id that may
138 // contain some styles (special bold fonts etc) and
139 // these are the additional qd styles that are not
140 // included in the ATSU font id
141 ATSUStyle m_macATSUStyle
;
142 ATSUFontID m_macATSUFontID
;
143 Style m_macATSUAdditionalQDStyles
;
145 // for true themeing support we must store the correct font
146 // information here, as this speeds up and optimizes rendering
147 ThemeFontID m_macThemeFontID
;
149 wxNativeFontInfo m_info
;
153 // ============================================================================
155 // ============================================================================
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 void wxFontRefData::Init(int pointSize
,
166 const wxString
& faceName
,
167 wxFontEncoding encoding
)
170 m_pointSize
= pointSize
;
174 m_underlined
= underlined
;
175 m_faceName
= faceName
;
176 m_encoding
= encoding
;
182 m_macATSUAdditionalQDStyles
= 0 ;
183 m_macATSUStyle
= NULL
;
185 m_macThemeFontID
= kThemeCurrentPortFont
;
189 wxFontRefData::~wxFontRefData()
191 if ( m_macATSUStyle
)
193 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
194 m_macATSUStyle
= NULL
;
198 void wxFontRefData::MacFindFont()
200 if ( m_macThemeFontID
!= kThemeCurrentPortFont
)
204 GetThemeFont( m_macThemeFontID
, GetApplicationScript(), fontName
, &m_macFontSize
, &m_macFontStyle
);
205 m_faceName
= wxMacMakeStringFromPascal( fontName
) ;
206 if ( m_macFontStyle
& bold
)
209 m_weight
= wxNORMAL
;
210 if ( m_macFontStyle
& italic
)
212 if ( m_macFontStyle
& underline
)
213 m_underlined
= true ;
214 ::GetFNum( fontName
, &m_macFontNum
);
215 m_pointSize
= m_macFontSize
;
219 if ( m_faceName
.Length() == 0 )
224 m_macFontNum
= ::GetAppFont();
230 ::GetFNum( "\pTimes", &m_macFontNum
);
235 ::GetFNum( "\pLucida Grande", &m_macFontNum
);
237 ::GetFNum( "\pGeneva", &m_macFontNum
);
242 ::GetFNum( "\pMonaco", &m_macFontNum
);
250 ::GetFontName( m_macFontNum
, name
) ;
251 m_faceName
= wxMacMakeStringFromPascal( name
) ;
255 if ( m_faceName
== wxT("systemfont") )
256 m_macFontNum
= ::GetSysFont() ;
257 else if ( m_faceName
== wxT("applicationfont") )
258 m_macFontNum
= ::GetAppFont() ;
262 wxMacStringToPascal( m_faceName
, fontname
) ;
263 ::GetFNum( fontname
, &m_macFontNum
);
268 if (m_weight
== wxBOLD
)
269 m_macFontStyle
|= bold
;
270 if (m_style
== wxITALIC
|| m_style
== wxSLANT
)
271 m_macFontStyle
|= italic
;
273 m_macFontStyle
|= underline
;
274 m_macFontSize
= m_pointSize
;
277 // we try to get as much styles as possible into ATSU
279 Fixed atsuSize
= IntToFixed( m_macFontSize
) ;
281 Style atsuStyle
= normal
;
282 verify_noerr(::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
, (UInt32
*)&m_macATSUFontID
) );
283 if ( m_macFontStyle
& bold
)
286 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| bold
, &test
) == noErr
)
289 m_macATSUFontID
= test
;
293 if ( m_macFontStyle
& italic
)
296 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| italic
, &test
) == noErr
)
298 atsuStyle
|= italic
;
299 m_macATSUFontID
= test
;
303 if ( m_macFontStyle
& underline
)
306 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| underline
, &test
) == noErr
)
308 atsuStyle
|= underline
;
309 m_macATSUFontID
= test
;
313 m_macATSUAdditionalQDStyles
= m_macFontStyle
& (~atsuStyle
) ;
315 if ( m_macATSUStyle
)
317 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
318 m_macATSUStyle
= NULL
;
321 OSStatus status
= ::ATSUCreateStyle((ATSUStyle
*)&m_macATSUStyle
) ;
322 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") ) ;
324 ATSUAttributeTag atsuTags
[] =
328 kATSUVerticalCharacterTag
,
331 kATSUQDUnderlineTag
,
332 kATSUQDCondensedTag
,
335 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
337 sizeof( ATSUFontID
) ,
339 sizeof( ATSUVerticalCharacterType
),
347 Boolean kTrue
= true ;
348 Boolean kFalse
= false ;
350 ATSUVerticalCharacterType kHorizontal
= kATSUStronglyHorizontal
;
351 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
356 (m_macATSUAdditionalQDStyles
& bold
) ? &kTrue
: &kFalse
,
357 (m_macATSUAdditionalQDStyles
& italic
) ? &kTrue
: &kFalse
,
358 (m_macATSUAdditionalQDStyles
& underline
) ? &kTrue
: &kFalse
,
359 (m_macATSUAdditionalQDStyles
& condense
) ? &kTrue
: &kFalse
,
360 (m_macATSUAdditionalQDStyles
& extend
) ? &kTrue
: &kFalse
,
363 status
= ::ATSUSetAttributes(
364 (ATSUStyle
)m_macATSUStyle
,
365 sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
366 atsuTags
, atsuSizes
, atsuValues
);
368 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") ) ;
371 // ----------------------------------------------------------------------------
373 // ----------------------------------------------------------------------------
375 bool wxFont::Create(const wxNativeFontInfo
& info
)
378 info
.pointSize
, info
.family
, info
.style
, info
.weight
,
379 info
.underlined
, info
.faceName
, info
.encoding
);
382 wxFont::wxFont(const wxString
& fontdesc
)
384 wxNativeFontInfo info
;
385 if ( info
.FromString(fontdesc
) )
389 bool wxFont::Create(int pointSize
,
394 const wxString
& faceName
,
395 wxFontEncoding encoding
)
399 m_refData
= new wxFontRefData(
400 pointSize
, family
, style
, weight
,
401 underlined
, faceName
, encoding
);
408 bool wxFont::MacCreateThemeFont(wxUint16 themeFontID
)
412 m_refData
= new wxFontRefData(
413 12, wxDEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
,
414 false, wxEmptyString
, wxFONTENCODING_DEFAULT
);
416 M_FONTDATA
->m_macThemeFontID
= themeFontID
;
426 bool wxFont::RealizeResource()
428 M_FONTDATA
->MacFindFont() ;
433 void wxFont::SetEncoding(wxFontEncoding encoding
)
437 M_FONTDATA
->m_encoding
= encoding
;
442 void wxFont::Unshare()
444 // Don't change shared data
447 m_refData
= new wxFontRefData();
451 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
457 void wxFont::SetPointSize(int pointSize
)
461 M_FONTDATA
->m_pointSize
= pointSize
;
466 void wxFont::SetFamily(int family
)
470 M_FONTDATA
->m_family
= family
;
475 void wxFont::SetStyle(int style
)
479 M_FONTDATA
->m_style
= style
;
484 void wxFont::SetWeight(int weight
)
488 M_FONTDATA
->m_weight
= weight
;
493 void wxFont::SetFaceName(const wxString
& faceName
)
497 M_FONTDATA
->m_faceName
= faceName
;
502 void wxFont::SetUnderlined(bool underlined
)
506 M_FONTDATA
->m_underlined
= underlined
;
511 void wxFont::SetNoAntiAliasing( bool no
)
515 M_FONTDATA
->SetNoAntiAliasing( no
);
520 // ----------------------------------------------------------------------------
522 // ----------------------------------------------------------------------------
524 // TODO: insert checks everywhere for M_FONTDATA == NULL!
526 int wxFont::GetPointSize() const
528 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
530 return M_FONTDATA
->m_pointSize
;
533 int wxFont::GetFamily() const
535 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
537 return M_FONTDATA
->m_family
;
540 int wxFont::GetStyle() const
542 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
544 return M_FONTDATA
->m_style
;
547 int wxFont::GetWeight() const
549 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
551 return M_FONTDATA
->m_weight
;
554 bool wxFont::GetUnderlined() const
556 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
558 return M_FONTDATA
->m_underlined
;
561 wxString
wxFont::GetFaceName() const
563 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxEmptyString
, wxT("invalid font") );
565 return M_FONTDATA
->m_faceName
;
568 wxFontEncoding
wxFont::GetEncoding() const
570 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTENCODING_DEFAULT
, wxT("invalid font") );
572 return M_FONTDATA
->m_encoding
;
575 bool wxFont::GetNoAntiAliasing() const
577 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
579 return M_FONTDATA
->m_noAA
;
582 short wxFont::MacGetFontNum() const
584 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
586 return M_FONTDATA
->m_macFontNum
;
589 short wxFont::MacGetFontSize() const
591 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
593 return M_FONTDATA
->m_macFontSize
;
596 wxByte
wxFont::MacGetFontStyle() const
598 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
600 return M_FONTDATA
->m_macFontStyle
;
603 wxUint32
wxFont::MacGetATSUFontID() const
605 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
607 return M_FONTDATA
->m_macATSUFontID
;
610 void * wxFont::MacGetATSUStyle() const
612 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
614 return M_FONTDATA
->m_macATSUStyle
;
617 wxUint32
wxFont::MacGetATSUAdditionalQDStyles() const
619 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
621 return M_FONTDATA
->m_macATSUAdditionalQDStyles
;
624 wxUint16
wxFont::MacGetThemeFontID() const
626 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
628 return M_FONTDATA
->m_macThemeFontID
;
631 const wxNativeFontInfo
* wxFont::GetNativeFontInfo() const
633 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
634 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
636 M_FONTDATA
->m_info
.InitFromFont(*this);
638 return &(M_FONTDATA
->m_info
);