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 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
33 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
35 friend class WXDLLEXPORT wxFont
;
44 , m_faceName(wxT("applicationfont"))
45 , m_encoding(wxFONTENCODING_DEFAULT
)
52 Init(m_pointSize
, m_family
, m_style
, m_weight
,
53 m_underlined
, m_faceName
, m_encoding
);
56 wxFontRefData(const wxFontRefData
& data
)
58 , m_fontId(data
.m_fontId
)
59 , m_pointSize(data
.m_pointSize
)
60 , m_family(data
.m_family
)
61 , m_style(data
.m_style
)
62 , m_weight(data
.m_weight
)
63 , m_underlined(data
.m_underlined
)
64 , m_faceName(data
.m_faceName
)
65 , m_encoding(data
.m_encoding
)
66 , m_macFontNum(data
.m_macFontNum
)
67 , m_macFontSize(data
.m_macFontSize
)
68 , m_macFontStyle(data
.m_macFontStyle
)
70 , m_macATSUFontID(data
.m_macATSUFontID
)
72 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
73 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
76 wxFontRefData(int size
,
81 const wxString
& faceName
,
82 wxFontEncoding encoding
)
88 , m_underlined(underlined
)
89 , m_faceName(faceName
)
90 , m_encoding(encoding
)
97 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
100 virtual ~wxFontRefData();
101 void SetNoAntiAliasing( bool no
= TRUE
) { m_noAA
= no
; }
102 bool GetNoAntiAliasing() const { return m_noAA
; }
105 // common part of all ctors
111 const wxString
& faceName
,
112 wxFontEncoding encoding
);
114 // font characterstics
122 wxFontEncoding m_encoding
;
123 bool m_noAA
; // No anti-aliasing
128 Style m_macFontStyle
;
130 // ATSU Font Information
132 // this is split into an ATSU font id that may
133 // contain some styles (special bold fonts etc) and
134 // these are the additional qd styles that are not
135 // included in the ATSU font id
136 ATSUStyle m_macATSUStyle
;
137 ATSUFontID m_macATSUFontID
;
138 Style m_macATSUAdditionalQDStyles
;
140 // for true themeing support we must store the correct font
141 // information here, as this speeds up and optimizes rendering
142 ThemeFontID m_macThemeFontID
;
144 wxNativeFontInfo m_info
;
149 // ============================================================================
151 // ============================================================================
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 void wxFontRefData::Init(int pointSize
,
162 const wxString
& faceName
,
163 wxFontEncoding encoding
)
166 m_pointSize
= pointSize
;
170 m_underlined
= underlined
;
171 m_faceName
= faceName
;
172 m_encoding
= encoding
;
178 m_macATSUAdditionalQDStyles
= 0 ;
179 m_macATSUStyle
= NULL
;
181 m_macThemeFontID
= kThemeCurrentPortFont
;
185 wxFontRefData::~wxFontRefData()
187 if ( m_macATSUStyle
)
189 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
190 m_macATSUStyle
= NULL
;
194 void wxFontRefData::MacFindFont()
196 if ( m_macThemeFontID
!= kThemeCurrentPortFont
)
199 GetThemeFont(m_macThemeFontID
, GetApplicationScript() , fontName
, &m_macFontSize
, &m_macFontStyle
) ;
200 m_faceName
= wxMacMakeStringFromPascal( fontName
) ;
201 if ( m_macFontStyle
& bold
)
204 m_weight
= wxNORMAL
;
205 if ( m_macFontStyle
& italic
)
207 if ( m_macFontStyle
& underline
)
208 m_underlined
= true ;
209 ::GetFNum( fontName
, &m_macFontNum
);
210 m_pointSize
= m_macFontSize
;
214 if( m_faceName
.Length() == 0 )
219 m_macFontNum
= ::GetAppFont() ;
225 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
227 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
232 ::GetFNum( "\pLucida Grande" , &m_macFontNum
) ;
234 ::GetFNum( "\pGeneva" , &m_macFontNum
) ;
239 ::GetFNum( "\pMonaco" , &m_macFontNum
) ;
241 ::GetFNum( "\pMonaco" , &m_macFontNum
) ;
246 ::GetFontName( m_macFontNum
, name
) ;
247 m_faceName
= wxMacMakeStringFromPascal( name
) ;
251 if ( m_faceName
== wxT("systemfont") )
252 m_macFontNum
= ::GetSysFont() ;
253 else if ( m_faceName
== wxT("applicationfont") )
254 m_macFontNum
= ::GetAppFont() ;
258 wxMacStringToPascal( m_faceName
, fontname
) ;
259 ::GetFNum( fontname
, &m_macFontNum
);
264 if (m_weight
== wxBOLD
)
265 m_macFontStyle
|= bold
;
266 if (m_style
== wxITALIC
|| m_style
== wxSLANT
)
267 m_macFontStyle
|= italic
;
269 m_macFontStyle
|= underline
;
270 m_macFontSize
= m_pointSize
;
273 // we try to get as much styles as possible into ATSU
275 Fixed atsuSize
= IntToFixed( m_macFontSize
) ;
277 Style atsuStyle
= normal
;
278 verify_noerr(::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
, (UInt32
*)&m_macATSUFontID
) );
279 if ( m_macFontStyle
& bold
)
282 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| bold
, &test
) == noErr
)
285 m_macATSUFontID
= test
;
288 if ( m_macFontStyle
& italic
)
291 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| italic
, &test
) == noErr
)
293 atsuStyle
|= italic
;
294 m_macATSUFontID
= test
;
297 if ( m_macFontStyle
& underline
)
300 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| underline
, &test
) == noErr
)
302 atsuStyle
|= underline
;
303 m_macATSUFontID
= test
;
307 m_macATSUAdditionalQDStyles
= m_macFontStyle
& (~atsuStyle
) ;
309 if ( m_macATSUStyle
)
311 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
312 m_macATSUStyle
= NULL
;
314 OSStatus status
= ::ATSUCreateStyle((ATSUStyle
*)&m_macATSUStyle
) ;
315 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") ) ;
316 ATSUAttributeTag atsuTags
[] =
320 kATSUVerticalCharacterTag
,
323 kATSUQDUnderlineTag
,
324 kATSUQDCondensedTag
,
327 ByteCount atsuSizes
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
329 sizeof( ATSUFontID
) ,
331 sizeof( ATSUVerticalCharacterType
),
338 Boolean kTrue
= true ;
339 Boolean kFalse
= false ;
341 ATSUVerticalCharacterType kHorizontal
= kATSUStronglyHorizontal
;
342 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
347 (m_macATSUAdditionalQDStyles
& bold
) ? &kTrue
: &kFalse
,
348 (m_macATSUAdditionalQDStyles
& italic
) ? &kTrue
: &kFalse
,
349 (m_macATSUAdditionalQDStyles
& underline
) ? &kTrue
: &kFalse
,
350 (m_macATSUAdditionalQDStyles
& condense
) ? &kTrue
: &kFalse
,
351 (m_macATSUAdditionalQDStyles
& extend
) ? &kTrue
: &kFalse
,
353 status
= ::ATSUSetAttributes((ATSUStyle
)m_macATSUStyle
, sizeof(atsuTags
)/sizeof(ATSUAttributeTag
) ,
354 atsuTags
, atsuSizes
, atsuValues
);
356 wxASSERT_MSG( status
== noErr
, wxT("couldn't Modify ATSU style") ) ;
359 // ----------------------------------------------------------------------------
361 // ----------------------------------------------------------------------------
367 bool wxFont::Create(const wxNativeFontInfo
& info
)
369 return Create(info
.pointSize
, info
.family
, info
.style
, info
.weight
,
370 info
.underlined
, info
.faceName
, info
.encoding
);
373 wxFont::wxFont(const wxString
& fontdesc
)
375 wxNativeFontInfo info
;
376 if ( info
.FromString(fontdesc
) )
380 bool wxFont::Create(int pointSize
,
385 const wxString
& faceName
,
386 wxFontEncoding encoding
)
389 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
390 underlined
, faceName
, encoding
);
397 bool wxFont::MacCreateThemeFont(wxUint16 themeFontID
)
400 m_refData
= new wxFontRefData(12, wxDEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
,false, wxEmptyString
, wxFONTENCODING_DEFAULT
);
401 M_FONTDATA
->m_macThemeFontID
= themeFontID
;
411 bool wxFont::RealizeResource()
413 M_FONTDATA
->MacFindFont() ;
417 void wxFont::SetEncoding(wxFontEncoding encoding
)
421 M_FONTDATA
->m_encoding
= encoding
;
426 void wxFont::Unshare()
428 // Don't change shared data
431 m_refData
= new wxFontRefData();
435 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
441 void wxFont::SetPointSize(int pointSize
)
445 M_FONTDATA
->m_pointSize
= pointSize
;
450 void wxFont::SetFamily(int family
)
454 M_FONTDATA
->m_family
= family
;
459 void wxFont::SetStyle(int style
)
463 M_FONTDATA
->m_style
= style
;
468 void wxFont::SetWeight(int weight
)
472 M_FONTDATA
->m_weight
= weight
;
477 void wxFont::SetFaceName(const wxString
& faceName
)
481 M_FONTDATA
->m_faceName
= faceName
;
486 void wxFont::SetUnderlined(bool underlined
)
490 M_FONTDATA
->m_underlined
= underlined
;
495 void wxFont::SetNoAntiAliasing( bool no
)
499 M_FONTDATA
->SetNoAntiAliasing( no
);
504 // ----------------------------------------------------------------------------
506 // ----------------------------------------------------------------------------
508 // TODO: insert checks everywhere for M_FONTDATA == NULL!
510 int wxFont::GetPointSize() const
512 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
513 return M_FONTDATA
->m_pointSize
;
516 int wxFont::GetFamily() const
518 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
519 return M_FONTDATA
->m_family
;
522 int wxFont::GetStyle() const
524 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
525 return M_FONTDATA
->m_style
;
528 int wxFont::GetWeight() const
530 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
531 return M_FONTDATA
->m_weight
;
534 bool wxFont::GetUnderlined() const
536 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
537 return M_FONTDATA
->m_underlined
;
540 wxString
wxFont::GetFaceName() const
542 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxEmptyString
, wxT("invalid font") );
543 return M_FONTDATA
->m_faceName
;
546 wxFontEncoding
wxFont::GetEncoding() const
548 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTENCODING_DEFAULT
, wxT("invalid font") );
549 return M_FONTDATA
->m_encoding
;
552 bool wxFont::GetNoAntiAliasing() const
554 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
555 return M_FONTDATA
->m_noAA
;
558 short wxFont::MacGetFontNum() const
560 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
561 return M_FONTDATA
->m_macFontNum
;
564 short wxFont::MacGetFontSize() const
566 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
567 return M_FONTDATA
->m_macFontSize
;
570 wxByte
wxFont::MacGetFontStyle() const
572 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
573 return M_FONTDATA
->m_macFontStyle
;
576 wxUint32
wxFont::MacGetATSUFontID() const
578 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
579 return M_FONTDATA
->m_macATSUFontID
;
582 void* wxFont::MacGetATSUStyle() const
584 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
585 return M_FONTDATA
->m_macATSUStyle
;
588 wxUint32
wxFont::MacGetATSUAdditionalQDStyles() const
590 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
591 return M_FONTDATA
->m_macATSUAdditionalQDStyles
;
594 wxUint16
wxFont::MacGetThemeFontID() const
596 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
597 return M_FONTDATA
->m_macThemeFontID
;
601 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
603 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
604 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
606 M_FONTDATA
->m_info
.InitFromFont(*this);
608 return &(M_FONTDATA
->m_info
);