1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/font.cpp
3 // Purpose: wxFont class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
21 #include "wx/string.h"
23 #include "wx/gdicmn.h"
26 #include "wx/fontutil.h"
28 #include "wx/fontutil.h"
30 #include "wx/mac/private.h"
31 #include <ATSUnicode.h>
33 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
35 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
37 friend class WXDLLEXPORT wxFont
;
46 , m_faceName(wxT("Geneva"))
47 , m_encoding(wxFONTENCODING_DEFAULT
)
53 Init(10, wxDEFAULT
, wxNORMAL
, wxNORMAL
, false,
54 wxT("Geneva"), wxFONTENCODING_DEFAULT
);
57 wxFontRefData(const wxFontRefData
& data
)
59 , m_fontId(data
.m_fontId
)
60 , m_pointSize(data
.m_pointSize
)
61 , m_family(data
.m_family
)
62 , m_style(data
.m_style
)
63 , m_weight(data
.m_weight
)
64 , m_underlined(data
.m_underlined
)
65 , m_faceName(data
.m_faceName
)
66 , m_encoding(data
.m_encoding
)
67 , m_macFontNum(data
.m_macFontNum
)
68 , m_macFontSize(data
.m_macFontSize
)
69 , 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
)
96 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
99 virtual ~wxFontRefData();
100 void SetNoAntiAliasing( bool no
= true ) { m_noAA
= no
; }
101 bool GetNoAntiAliasing() const { return m_noAA
; }
104 // common part of all ctors
110 const wxString
& faceName
,
111 wxFontEncoding encoding
);
113 // font characterstics
121 wxFontEncoding m_encoding
;
122 bool m_noAA
; // No anti-aliasing
127 unsigned char m_macFontStyle
;
128 wxUint32 m_macATSUFontID
;
130 wxNativeFontInfo m_info
;
136 #define M_FONTDATA (wxFontRefData*)m_refData)
138 // ============================================================================
140 // ============================================================================
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 void wxFontRefData::Init(int pointSize
,
151 const wxString
& faceName
,
152 wxFontEncoding encoding
)
155 m_pointSize
= pointSize
;
159 m_underlined
= underlined
;
160 m_faceName
= faceName
;
161 m_encoding
= encoding
;
170 wxFontRefData::~wxFontRefData()
174 void wxFontRefData::MacFindFont()
176 if( m_faceName
.empty() )
181 m_macFontNum
= ::GetAppFont() ;
184 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
187 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
190 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
193 ::GetFNum( "\pGeneva" , &m_macFontNum
) ;
196 ::GetFNum( "\pMonaco" , &m_macFontNum
) ;
200 GetFontName( m_macFontNum
, name
) ;
201 m_faceName
= wxMacMakeStringFromPascal( name
) ;
205 if ( m_faceName
== wxT("systemfont") )
206 m_macFontNum
= ::GetSysFont() ;
207 else if ( m_faceName
== wxT("applicationfont") )
208 m_macFontNum
= ::GetAppFont() ;
212 wxMacStringToPascal( m_faceName
, fontname
) ;
213 ::GetFNum( fontname
, &m_macFontNum
);
218 if (m_weight
== wxBOLD
)
219 m_macFontStyle
|= bold
;
220 if (m_style
== wxITALIC
|| m_style
== wxSLANT
)
221 m_macFontStyle
|= italic
;
223 m_macFontStyle
|= underline
;
224 m_macFontSize
= m_pointSize
;
226 //TODO:if we supply the style as an additional parameter we must make a testing
227 //sequence in order to degrade gracefully while trying to maintain most of the style
228 //information, meanwhile we just take the normal font and apply the features after
231 #endif // __WXDEBUG__
232 ::ATSUFONDtoFontID(m_macFontNum
, normal
/*qdStyle*/, (UInt32
*)&m_macATSUFontID
);
234 status = ATSUFindFontFromName ( (Ptr) m_faceName , strlen( m_faceName ) ,
235 kFontFullName, kFontMacintoshPlatform, kFontRomanScript , kFontNoLanguage , (UInt32*)&m_macATSUFontID ) ;
237 wxASSERT_MSG( status
== noErr
, wxT("couldn't retrieve font identifier") ) ;
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
245 bool wxFont::Create(const wxNativeFontInfo
& info
)
247 return Create(info
.pointSize
, info
.family
, info
.style
, info
.weight
,
248 info
.underlined
, info
.faceName
, info
.encoding
);
251 wxFont::wxFont(const wxString
& fontdesc
)
253 wxNativeFontInfo info
;
254 if ( info
.FromString(fontdesc
) )
258 bool wxFont::Create(int pointSize
,
263 const wxString
& faceName
,
264 wxFontEncoding encoding
)
267 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
268 underlined
, faceName
, encoding
);
279 bool wxFont::RealizeResource()
281 M_FONTDATA
->MacFindFont() ;
285 void wxFont::SetEncoding(wxFontEncoding encoding
)
289 M_FONTDATA
->m_encoding
= encoding
;
294 void wxFont::Unshare()
296 // Don't change shared data
299 m_refData
= new wxFontRefData();
303 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
309 void wxFont::SetPointSize(int pointSize
)
313 M_FONTDATA
->m_pointSize
= pointSize
;
318 void wxFont::SetFamily(int family
)
322 M_FONTDATA
->m_family
= family
;
327 void wxFont::SetStyle(int style
)
331 M_FONTDATA
->m_style
= style
;
336 void wxFont::SetWeight(int weight
)
340 M_FONTDATA
->m_weight
= weight
;
345 bool wxFont::SetFaceName(const wxString
& faceName
)
349 M_FONTDATA
->m_faceName
= faceName
;
353 return wxFontBase::SetFaceName(faceName
);
356 void wxFont::SetUnderlined(bool underlined
)
360 M_FONTDATA
->m_underlined
= underlined
;
365 void wxFont::SetNoAntiAliasing( bool no
)
369 M_FONTDATA
->SetNoAntiAliasing( no
);
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
378 // TODO: insert checks everywhere for M_FONTDATA == NULL!
380 int wxFont::GetPointSize() const
382 return M_FONTDATA
->m_pointSize
;
385 int wxFont::GetFamily() const
387 return M_FONTDATA
->m_family
;
390 int wxFont::GetStyle() const
392 return M_FONTDATA
->m_style
;
395 int wxFont::GetWeight() const
397 return M_FONTDATA
->m_weight
;
400 bool wxFont::GetUnderlined() const
402 return M_FONTDATA
->m_underlined
;
405 wxString
wxFont::GetFaceName() const
409 str
= M_FONTDATA
->m_faceName
;
413 wxFontEncoding
wxFont::GetEncoding() const
415 return M_FONTDATA
->m_encoding
;
418 bool wxFont::GetNoAntiAliasing() const
420 return M_FONTDATA
->m_noAA
;
423 short wxFont::GetMacFontNum() const
425 return M_FONTDATA
->m_macFontNum
;
428 short wxFont::GetMacFontSize() const
430 return M_FONTDATA
->m_macFontSize
;
433 wxByte
wxFont::GetMacFontStyle() const
435 return M_FONTDATA
->m_macFontStyle
;
438 wxUint32
wxFont::GetMacATSUFontID() const
440 return M_FONTDATA
->m_macATSUFontID
;
443 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
445 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
447 M_FONTDATA
->m_info
.InitFromFont(*this);
449 return &(M_FONTDATA
->m_info
);