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
;
135 // ============================================================================
137 // ============================================================================
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 void wxFontRefData::Init(int pointSize
,
148 const wxString
& faceName
,
149 wxFontEncoding encoding
)
152 m_pointSize
= pointSize
;
156 m_underlined
= underlined
;
157 m_faceName
= faceName
;
158 m_encoding
= encoding
;
167 wxFontRefData::~wxFontRefData()
171 void wxFontRefData::MacFindFont()
173 if( m_faceName
.empty() )
178 m_macFontNum
= ::GetAppFont() ;
181 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
184 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
187 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
190 ::GetFNum( "\pGeneva" , &m_macFontNum
) ;
193 ::GetFNum( "\pMonaco" , &m_macFontNum
) ;
197 GetFontName( m_macFontNum
, name
) ;
198 m_faceName
= wxMacMakeStringFromPascal( name
) ;
202 if ( m_faceName
== wxT("systemfont") )
203 m_macFontNum
= ::GetSysFont() ;
204 else if ( m_faceName
== wxT("applicationfont") )
205 m_macFontNum
= ::GetAppFont() ;
209 wxMacStringToPascal( m_faceName
, fontname
) ;
210 ::GetFNum( fontname
, &m_macFontNum
);
215 if (m_weight
== wxBOLD
)
216 m_macFontStyle
|= bold
;
217 if (m_style
== wxITALIC
|| m_style
== wxSLANT
)
218 m_macFontStyle
|= italic
;
220 m_macFontStyle
|= underline
;
221 m_macFontSize
= m_pointSize
;
223 //TODO:if we supply the style as an additional parameter we must make a testing
224 //sequence in order to degrade gracefully while trying to maintain most of the style
225 //information, meanwhile we just take the normal font and apply the features after
228 #endif // __WXDEBUG__
229 ::ATSUFONDtoFontID(m_macFontNum
, normal
/*qdStyle*/, (UInt32
*)&m_macATSUFontID
);
231 status = ATSUFindFontFromName ( (Ptr) m_faceName , strlen( m_faceName ) ,
232 kFontFullName, kFontMacintoshPlatform, kFontRomanScript , kFontNoLanguage , (UInt32*)&m_macATSUFontID ) ;
234 wxASSERT_MSG( status
== noErr
, wxT("couldn't retrieve font identifier") ) ;
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
242 bool wxFont::Create(const wxNativeFontInfo
& info
)
244 return Create(info
.pointSize
, info
.family
, info
.style
, info
.weight
,
245 info
.underlined
, info
.faceName
, info
.encoding
);
248 wxFont::wxFont(const wxString
& fontdesc
)
250 wxNativeFontInfo info
;
251 if ( info
.FromString(fontdesc
) )
255 bool wxFont::Create(int pointSize
,
260 const wxString
& faceName
,
261 wxFontEncoding encoding
)
264 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
265 underlined
, faceName
, encoding
);
276 bool wxFont::RealizeResource()
278 M_FONTDATA
->MacFindFont() ;
282 void wxFont::SetEncoding(wxFontEncoding encoding
)
286 M_FONTDATA
->m_encoding
= encoding
;
291 void wxFont::Unshare()
293 // Don't change shared data
296 m_refData
= new wxFontRefData();
300 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
306 void wxFont::SetPointSize(int pointSize
)
310 M_FONTDATA
->m_pointSize
= pointSize
;
315 void wxFont::SetFamily(int family
)
319 M_FONTDATA
->m_family
= family
;
324 void wxFont::SetStyle(int style
)
328 M_FONTDATA
->m_style
= style
;
333 void wxFont::SetWeight(int weight
)
337 M_FONTDATA
->m_weight
= weight
;
342 bool wxFont::SetFaceName(const wxString
& faceName
)
346 M_FONTDATA
->m_faceName
= faceName
;
350 return wxFontBase::SetFaceName(faceName
);
353 void wxFont::SetUnderlined(bool underlined
)
357 M_FONTDATA
->m_underlined
= underlined
;
362 void wxFont::SetNoAntiAliasing( bool no
)
366 M_FONTDATA
->SetNoAntiAliasing( no
);
371 // ----------------------------------------------------------------------------
373 // ----------------------------------------------------------------------------
375 // TODO: insert checks everywhere for M_FONTDATA == NULL!
377 int wxFont::GetPointSize() const
379 return M_FONTDATA
->m_pointSize
;
382 int wxFont::GetFamily() const
384 return M_FONTDATA
->m_family
;
387 int wxFont::GetStyle() const
389 return M_FONTDATA
->m_style
;
392 int wxFont::GetWeight() const
394 return M_FONTDATA
->m_weight
;
397 bool wxFont::GetUnderlined() const
399 return M_FONTDATA
->m_underlined
;
402 wxString
wxFont::GetFaceName() const
406 str
= M_FONTDATA
->m_faceName
;
410 wxFontEncoding
wxFont::GetEncoding() const
412 return M_FONTDATA
->m_encoding
;
415 bool wxFont::GetNoAntiAliasing() const
417 return M_FONTDATA
->m_noAA
;
420 short wxFont::GetMacFontNum() const
422 return M_FONTDATA
->m_macFontNum
;
425 short wxFont::GetMacFontSize() const
427 return M_FONTDATA
->m_macFontSize
;
430 wxByte
wxFont::GetMacFontStyle() const
432 return M_FONTDATA
->m_macFontStyle
;
435 wxUint32
wxFont::GetMacATSUFontID() const
437 return M_FONTDATA
->m_macATSUFontID
;
440 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
442 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
444 M_FONTDATA
->m_info
.InitFromFont(*this);
446 return &(M_FONTDATA
->m_info
);