1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/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"
17 #include "wx/string.h"
20 #include "wx/gdicmn.h"
23 #include "wx/fontutil.h"
24 #include "wx/fontutil.h"
26 #include "wx/mac/private.h"
29 #include <ATSUnicode.h>
33 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
36 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
38 friend class WXDLLEXPORT wxFont
;
48 , m_faceName(wxT("applicationfont"))
49 , m_encoding(wxFONTENCODING_DEFAULT
)
56 Init(m_pointSize
, m_family
, m_style
, m_weight
,
57 m_underlined
, m_faceName
, m_encoding
);
60 wxFontRefData(const wxFontRefData
& data
)
62 , m_fontId(data
.m_fontId
)
63 , m_pointSize(data
.m_pointSize
)
64 , m_family(data
.m_family
)
65 , m_style(data
.m_style
)
66 , m_weight(data
.m_weight
)
67 , m_underlined(data
.m_underlined
)
68 , m_faceName(data
.m_faceName
)
69 , m_encoding(data
.m_encoding
)
70 , m_macFontFamily(data
.m_macFontFamily
)
71 , m_macFontSize(data
.m_macFontSize
)
72 , m_macFontStyle(data
.m_macFontStyle
)
74 , m_macATSUFontID(data
.m_macATSUFontID
)
76 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
77 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
80 wxFontRefData(int size
,
85 const wxString
& faceName
,
86 wxFontEncoding encoding
)
92 , m_underlined(underlined
)
93 , m_faceName(faceName
)
94 , m_encoding(encoding
)
101 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
104 virtual ~wxFontRefData();
106 void SetNoAntiAliasing( bool no
= true )
109 bool GetNoAntiAliasing() const
115 // common part of all ctors
121 const wxString
& faceName
,
122 wxFontEncoding encoding
);
124 // font characterstics
132 wxFontEncoding m_encoding
;
133 bool m_noAA
; // No anti-aliasing
136 FMFontFamily m_macFontFamily
;
137 FMFontSize m_macFontSize
;
138 FMFontStyle m_macFontStyle
;
140 // ATSU Font Information
142 // this is split into an ATSU font id that may
143 // contain some styles (special bold fonts etc) and
144 // these are the additional qd styles that are not
145 // included in the ATSU font id
146 ATSUStyle m_macATSUStyle
;
147 ATSUFontID m_macATSUFontID
;
148 FMFontStyle m_macATSUAdditionalQDStyles
;
150 // for true themeing support we must store the correct font
151 // information here, as this speeds up and optimizes rendering
152 ThemeFontID m_macThemeFontID
;
154 wxNativeFontInfo m_info
;
158 // ============================================================================
160 // ============================================================================
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 void wxFontRefData::Init(int pointSize
,
171 const wxString
& faceName
,
172 wxFontEncoding encoding
)
175 m_pointSize
= pointSize
;
179 m_underlined
= underlined
;
180 m_faceName
= faceName
;
181 m_encoding
= encoding
;
183 m_macFontFamily
= 0 ;
187 m_macATSUAdditionalQDStyles
= 0 ;
188 m_macATSUStyle
= NULL
;
190 m_macThemeFontID
= kThemeCurrentPortFont
;
194 wxFontRefData::~wxFontRefData()
196 if ( m_macATSUStyle
)
198 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
199 m_macATSUStyle
= NULL
;
203 void wxFontRefData::MacFindFont()
208 if ( m_macThemeFontID
!= kThemeCurrentPortFont
)
211 GetThemeFont( m_macThemeFontID
, GetApplicationScript(), qdFontName
, &m_macFontSize
, &style
);
212 m_macFontStyle
= style
;
213 m_faceName
= wxMacMakeStringFromPascal( qdFontName
);
214 if ( m_macFontStyle
& bold
)
217 m_weight
= wxNORMAL
;
218 if ( m_macFontStyle
& italic
)
220 if ( m_macFontStyle
& underline
)
221 m_underlined
= true ;
222 m_pointSize
= m_macFontSize
;
224 m_macFontFamily
= FMGetFontFamilyFromName( qdFontName
);
229 if ( m_faceName
.empty() )
231 if ( m_family
== wxDEFAULT
)
234 m_macFontFamily
= GetAppFont();
235 FMGetFontFamilyName(m_macFontFamily
,qdFontName
);
236 m_faceName
= wxMacMakeStringFromPascal( qdFontName
);
246 m_faceName
= wxT("Times");
250 m_faceName
= wxT("Lucida Grande");
254 m_faceName
= wxT("Monaco");
258 m_faceName
= wxT("Times");
262 wxMacStringToPascal( m_faceName
, qdFontName
);
263 m_macFontFamily
= FMGetFontFamilyFromName( qdFontName
);
270 if ( m_faceName
== wxT("systemfont") )
271 m_macFontFamily
= GetSysFont();
272 else if ( m_faceName
== wxT("applicationfont") )
273 m_macFontFamily
= GetAppFont();
276 if ( m_faceName
== wxT("systemfont") )
277 m_faceName
= wxT("Lucida Grande");
278 else if ( m_faceName
== wxT("applicationfont") )
279 m_faceName
= wxT("Lucida Grande");
282 wxMacCFStringHolder
cf( m_faceName
, wxLocale::GetSystemEncoding() );
283 ATSFontFamilyRef atsfamily
= ATSFontFamilyFindFromName( cf
, kATSOptionFlagsDefault
);
284 wxASSERT_MSG( atsfamily
!= (ATSFontFamilyRef
) -1 , wxT("ATSFontFamilyFindFromName failed") );
285 m_macFontFamily
= FMGetFontFamilyFromATSFontFamilyRef( atsfamily
);
290 if (m_weight
== wxBOLD
)
291 m_macFontStyle
|= bold
;
292 if (m_style
== wxITALIC
|| m_style
== wxSLANT
)
293 m_macFontStyle
|= italic
;
295 m_macFontStyle
|= underline
;
296 m_macFontSize
= m_pointSize
;
299 // we try to get as much styles as possible into ATSU
302 // ATSUFontID and FMFont are equivalent
303 FMFontStyle intrinsicStyle
= 0 ;
305 status
= FMGetFontFromFontFamilyInstance( m_macFontFamily
, m_macFontStyle
, &m_macATSUFontID
, &intrinsicStyle
);
306 wxASSERT_MSG( status
== noErr
, wxT("couldn't get an ATSUFont from font family") );
308 m_macATSUAdditionalQDStyles
= m_macFontStyle
& (~intrinsicStyle
);
310 if ( m_macATSUStyle
)
312 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
313 m_macATSUStyle
= NULL
;
316 status
= ::ATSUCreateStyle((ATSUStyle
*)&m_macATSUStyle
);
317 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
319 ATSUAttributeTag atsuTags
[] =
323 kATSUVerticalCharacterTag
,
326 kATSUQDUnderlineTag
,
327 kATSUQDCondensedTag
,
330 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
332 sizeof( ATSUFontID
) ,
334 sizeof( ATSUVerticalCharacterType
),
342 Boolean kTrue
= true ;
343 Boolean kFalse
= false ;
345 Fixed atsuSize
= IntToFixed( m_macFontSize
);
346 ATSUVerticalCharacterType kHorizontal
= kATSUStronglyHorizontal
;
347 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
352 (m_macATSUAdditionalQDStyles
& bold
) ? &kTrue
: &kFalse
,
353 (m_macATSUAdditionalQDStyles
& italic
) ? &kTrue
: &kFalse
,
354 (m_macATSUAdditionalQDStyles
& underline
) ? &kTrue
: &kFalse
,
355 (m_macATSUAdditionalQDStyles
& condense
) ? &kTrue
: &kFalse
,
356 (m_macATSUAdditionalQDStyles
& extend
) ? &kTrue
: &kFalse
,
359 status
= ::ATSUSetAttributes(
360 (ATSUStyle
)m_macATSUStyle
,
361 sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
362 atsuTags
, atsuSizes
, atsuValues
);
364 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
367 // ----------------------------------------------------------------------------
369 // ----------------------------------------------------------------------------
371 bool wxFont::Create(const wxNativeFontInfo
& info
)
374 info
.pointSize
, info
.family
, info
.style
, info
.weight
,
375 info
.underlined
, info
.faceName
, info
.encoding
);
378 wxFont::wxFont(const wxString
& fontdesc
)
380 wxNativeFontInfo info
;
381 if ( info
.FromString(fontdesc
) )
385 bool wxFont::Create(int pointSize
,
390 const wxString
& faceName
,
391 wxFontEncoding encoding
)
395 m_refData
= new wxFontRefData(
396 pointSize
, family
, style
, weight
,
397 underlined
, faceName
, encoding
);
404 bool wxFont::MacCreateThemeFont(wxUint16 themeFontID
)
408 m_refData
= new wxFontRefData(
409 12, wxDEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
,
410 false, wxEmptyString
, wxFONTENCODING_DEFAULT
);
412 M_FONTDATA
->m_macThemeFontID
= themeFontID
;
422 bool wxFont::RealizeResource()
424 M_FONTDATA
->MacFindFont();
429 void wxFont::SetEncoding(wxFontEncoding encoding
)
433 M_FONTDATA
->m_encoding
= encoding
;
438 void wxFont::Unshare()
440 // Don't change shared data
443 m_refData
= new wxFontRefData();
447 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
453 void wxFont::SetPointSize(int pointSize
)
457 M_FONTDATA
->m_pointSize
= pointSize
;
462 void wxFont::SetFamily(int family
)
466 M_FONTDATA
->m_family
= family
;
471 void wxFont::SetStyle(int style
)
475 M_FONTDATA
->m_style
= style
;
480 void wxFont::SetWeight(int weight
)
484 M_FONTDATA
->m_weight
= weight
;
489 bool wxFont::SetFaceName(const wxString
& faceName
)
493 M_FONTDATA
->m_faceName
= faceName
;
497 return wxFontBase::SetFaceName(faceName
);
500 void wxFont::SetUnderlined(bool underlined
)
504 M_FONTDATA
->m_underlined
= underlined
;
509 void wxFont::SetNoAntiAliasing( bool no
)
513 M_FONTDATA
->SetNoAntiAliasing( no
);
518 // ----------------------------------------------------------------------------
520 // ----------------------------------------------------------------------------
522 // TODO: insert checks everywhere for M_FONTDATA == NULL!
524 int wxFont::GetPointSize() const
526 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
528 return M_FONTDATA
->m_pointSize
;
531 int wxFont::GetFamily() const
533 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
535 return M_FONTDATA
->m_family
;
538 int wxFont::GetStyle() const
540 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
542 return M_FONTDATA
->m_style
;
545 int wxFont::GetWeight() const
547 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
549 return M_FONTDATA
->m_weight
;
552 bool wxFont::GetUnderlined() const
554 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
556 return M_FONTDATA
->m_underlined
;
559 wxString
wxFont::GetFaceName() const
561 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxEmptyString
, wxT("invalid font") );
563 return M_FONTDATA
->m_faceName
;
566 wxFontEncoding
wxFont::GetEncoding() const
568 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTENCODING_DEFAULT
, wxT("invalid font") );
570 return M_FONTDATA
->m_encoding
;
573 bool wxFont::GetNoAntiAliasing() const
575 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
577 return M_FONTDATA
->m_noAA
;
580 short wxFont::MacGetFontNum() const
582 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
584 return M_FONTDATA
->m_macFontFamily
;
587 short wxFont::MacGetFontSize() const
589 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
591 return M_FONTDATA
->m_macFontSize
;
594 wxByte
wxFont::MacGetFontStyle() const
596 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
598 return M_FONTDATA
->m_macFontStyle
;
601 wxUint32
wxFont::MacGetATSUFontID() const
603 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
605 return M_FONTDATA
->m_macATSUFontID
;
608 void * wxFont::MacGetATSUStyle() const
610 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
612 return M_FONTDATA
->m_macATSUStyle
;
615 wxUint32
wxFont::MacGetATSUAdditionalQDStyles() const
617 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
619 return M_FONTDATA
->m_macATSUAdditionalQDStyles
;
622 wxUint16
wxFont::MacGetThemeFontID() const
624 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
626 return M_FONTDATA
->m_macThemeFontID
;
629 const wxNativeFontInfo
* wxFont::GetNativeFontInfo() const
631 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
632 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
634 M_FONTDATA
->m_info
.InitFromFont(*this);
636 return &(M_FONTDATA
->m_info
);