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"
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"
27 #include <ATSUnicode.h>
29 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
31 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
33 friend class WXDLLEXPORT wxFont
;
42 , m_faceName(wxT("Geneva"))
43 , m_encoding(wxFONTENCODING_DEFAULT
)
49 Init(10, wxDEFAULT
, wxNORMAL
, wxNORMAL
, false,
50 wxT("Geneva"), wxFONTENCODING_DEFAULT
);
53 wxFontRefData(const wxFontRefData
& data
)
55 , m_fontId(data
.m_fontId
)
56 , m_pointSize(data
.m_pointSize
)
57 , m_family(data
.m_family
)
58 , m_style(data
.m_style
)
59 , m_weight(data
.m_weight
)
60 , m_underlined(data
.m_underlined
)
61 , m_faceName(data
.m_faceName
)
62 , m_encoding(data
.m_encoding
)
63 , m_macFontNum(data
.m_macFontNum
)
64 , m_macFontSize(data
.m_macFontSize
)
65 , 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
)
92 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
95 virtual ~wxFontRefData();
96 void SetNoAntiAliasing( bool no
= true ) { m_noAA
= no
; }
97 bool GetNoAntiAliasing() const { return m_noAA
; }
100 // common part of all ctors
106 const wxString
& faceName
,
107 wxFontEncoding encoding
);
109 // font characterstics
117 wxFontEncoding m_encoding
;
118 bool m_noAA
; // No anti-aliasing
123 unsigned char m_macFontStyle
;
124 wxUint32 m_macATSUFontID
;
126 wxNativeFontInfo m_info
;
131 // ============================================================================
133 // ============================================================================
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 void wxFontRefData::Init(int pointSize
,
144 const wxString
& faceName
,
145 wxFontEncoding encoding
)
148 m_pointSize
= pointSize
;
152 m_underlined
= underlined
;
153 m_faceName
= faceName
;
154 m_encoding
= encoding
;
163 wxFontRefData::~wxFontRefData()
167 void wxFontRefData::MacFindFont()
169 if( m_faceName
.Length() == 0 )
174 m_macFontNum
= ::GetAppFont() ;
177 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
180 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
183 ::GetFNum( "\pTimes" , &m_macFontNum
) ;
186 ::GetFNum( "\pGeneva" , &m_macFontNum
) ;
189 ::GetFNum( "\pMonaco" , &m_macFontNum
) ;
193 GetFontName( m_macFontNum
, name
) ;
194 m_faceName
= wxMacMakeStringFromPascal( name
) ;
198 if ( m_faceName
== wxT("systemfont") )
199 m_macFontNum
= ::GetSysFont() ;
200 else if ( m_faceName
== wxT("applicationfont") )
201 m_macFontNum
= ::GetAppFont() ;
205 wxMacStringToPascal( m_faceName
, fontname
) ;
206 ::GetFNum( fontname
, &m_macFontNum
);
211 if (m_weight
== wxBOLD
)
212 m_macFontStyle
|= bold
;
213 if (m_style
== wxITALIC
|| m_style
== wxSLANT
)
214 m_macFontStyle
|= italic
;
216 m_macFontStyle
|= underline
;
217 m_macFontSize
= m_pointSize
;
219 //TODO:if we supply the style as an additional parameter we must make a testing
220 //sequence in order to degrade gracefully while trying to maintain most of the style
221 //information, meanwhile we just take the normal font and apply the features after
224 #endif // __WXDEBUG__
225 ::ATSUFONDtoFontID(m_macFontNum
, normal
/*qdStyle*/, (UInt32
*)&m_macATSUFontID
);
227 status = ATSUFindFontFromName ( (Ptr) m_faceName , strlen( m_faceName ) ,
228 kFontFullName, kFontMacintoshPlatform, kFontRomanScript , kFontNoLanguage , (UInt32*)&m_macATSUFontID ) ;
230 wxASSERT_MSG( status
== noErr
, wxT("couldn't retrieve font identifier") ) ;
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
238 bool wxFont::Create(const wxNativeFontInfo
& info
)
240 return Create(info
.pointSize
, info
.family
, info
.style
, info
.weight
,
241 info
.underlined
, info
.faceName
, info
.encoding
);
244 wxFont::wxFont(const wxString
& fontdesc
)
246 wxNativeFontInfo info
;
247 if ( info
.FromString(fontdesc
) )
251 bool wxFont::Create(int pointSize
,
256 const wxString
& faceName
,
257 wxFontEncoding encoding
)
260 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
261 underlined
, faceName
, encoding
);
272 bool wxFont::RealizeResource()
274 M_FONTDATA
->MacFindFont() ;
278 void wxFont::SetEncoding(wxFontEncoding encoding
)
282 M_FONTDATA
->m_encoding
= encoding
;
287 void wxFont::Unshare()
289 // Don't change shared data
292 m_refData
= new wxFontRefData();
296 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
302 void wxFont::SetPointSize(int pointSize
)
306 M_FONTDATA
->m_pointSize
= pointSize
;
311 void wxFont::SetFamily(int family
)
315 M_FONTDATA
->m_family
= family
;
320 void wxFont::SetStyle(int style
)
324 M_FONTDATA
->m_style
= style
;
329 void wxFont::SetWeight(int weight
)
333 M_FONTDATA
->m_weight
= weight
;
338 void wxFont::SetFaceName(const wxString
& faceName
)
342 M_FONTDATA
->m_faceName
= faceName
;
347 void wxFont::SetUnderlined(bool underlined
)
351 M_FONTDATA
->m_underlined
= underlined
;
356 void wxFont::SetNoAntiAliasing( bool no
)
360 M_FONTDATA
->SetNoAntiAliasing( no
);
365 // ----------------------------------------------------------------------------
367 // ----------------------------------------------------------------------------
369 // TODO: insert checks everywhere for M_FONTDATA == NULL!
371 int wxFont::GetPointSize() const
373 return M_FONTDATA
->m_pointSize
;
376 int wxFont::GetFamily() const
378 return M_FONTDATA
->m_family
;
381 int wxFont::GetStyle() const
383 return M_FONTDATA
->m_style
;
386 int wxFont::GetWeight() const
388 return M_FONTDATA
->m_weight
;
391 bool wxFont::GetUnderlined() const
393 return M_FONTDATA
->m_underlined
;
396 wxString
wxFont::GetFaceName() const
400 str
= M_FONTDATA
->m_faceName
;
404 wxFontEncoding
wxFont::GetEncoding() const
406 return M_FONTDATA
->m_encoding
;
409 bool wxFont::GetNoAntiAliasing() const
411 return M_FONTDATA
->m_noAA
;
414 short wxFont::GetMacFontNum() const
416 return M_FONTDATA
->m_macFontNum
;
419 short wxFont::GetMacFontSize() const
421 return M_FONTDATA
->m_macFontSize
;
424 wxByte
wxFont::GetMacFontStyle() const
426 return M_FONTDATA
->m_macFontStyle
;
429 wxUint32
wxFont::GetMacATSUFontID() const
431 return M_FONTDATA
->m_macATSUFontID
;
434 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
436 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
438 M_FONTDATA
->m_info
.InitFromFont(*this);
440 return &(M_FONTDATA
->m_info
);