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/fontutil.h"
21 #include "wx/gdicmn.h"
23 #include "wx/fontutil.h"
25 #include "wx/mac/private.h"
28 #include <ATSUnicode.h>
32 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
35 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
37 friend class WXDLLEXPORT wxFont
;
47 , m_faceName(wxT("applicationfont"))
48 , m_encoding(wxFONTENCODING_DEFAULT
)
55 Init(m_pointSize
, m_family
, m_style
, m_weight
,
56 m_underlined
, m_faceName
, m_encoding
);
59 wxFontRefData(const wxFontRefData
& data
)
61 , m_fontId(data
.m_fontId
)
62 , m_pointSize(data
.m_pointSize
)
63 , m_family(data
.m_family
)
64 , m_style(data
.m_style
)
65 , m_weight(data
.m_weight
)
66 , m_underlined(data
.m_underlined
)
67 , m_faceName(data
.m_faceName
)
68 , m_encoding(data
.m_encoding
)
69 , m_macFontNum(data
.m_macFontNum
)
70 , m_macFontSize(data
.m_macFontSize
)
71 , m_macFontStyle(data
.m_macFontStyle
)
73 , m_macATSUFontID(data
.m_macATSUFontID
)
75 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
76 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
79 wxFontRefData(int size
,
84 const wxString
& faceName
,
85 wxFontEncoding encoding
)
91 , m_underlined(underlined
)
92 , m_faceName(faceName
)
93 , m_encoding(encoding
)
100 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
103 virtual ~wxFontRefData();
105 void SetNoAntiAliasing( bool no
= true )
108 bool GetNoAntiAliasing() const
114 // common part of all ctors
120 const wxString
& faceName
,
121 wxFontEncoding encoding
);
123 // font characterstics
131 wxFontEncoding m_encoding
;
132 bool m_noAA
; // No anti-aliasing
137 Style m_macFontStyle
;
139 // ATSU Font Information
141 // this is split into an ATSU font id that may
142 // contain some styles (special bold fonts etc) and
143 // these are the additional qd styles that are not
144 // included in the ATSU font id
145 ATSUStyle m_macATSUStyle
;
146 ATSUFontID m_macATSUFontID
;
147 Style m_macATSUAdditionalQDStyles
;
149 // for true themeing support we must store the correct font
150 // information here, as this speeds up and optimizes rendering
151 ThemeFontID m_macThemeFontID
;
153 wxNativeFontInfo m_info
;
157 // ============================================================================
159 // ============================================================================
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
165 void wxFontRefData::Init(int pointSize
,
170 const wxString
& faceName
,
171 wxFontEncoding encoding
)
174 m_pointSize
= pointSize
;
178 m_underlined
= underlined
;
179 m_faceName
= faceName
;
180 m_encoding
= encoding
;
186 m_macATSUAdditionalQDStyles
= 0 ;
187 m_macATSUStyle
= NULL
;
189 m_macThemeFontID
= kThemeCurrentPortFont
;
193 wxFontRefData::~wxFontRefData()
195 if ( m_macATSUStyle
)
197 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
198 m_macATSUStyle
= NULL
;
202 void wxFontRefData::MacFindFont()
204 if ( m_macThemeFontID
!= kThemeCurrentPortFont
)
208 GetThemeFont( m_macThemeFontID
, GetApplicationScript(), fontName
, &m_macFontSize
, &m_macFontStyle
);
209 m_faceName
= wxMacMakeStringFromPascal( fontName
) ;
210 if ( m_macFontStyle
& bold
)
213 m_weight
= wxNORMAL
;
214 if ( m_macFontStyle
& italic
)
216 if ( m_macFontStyle
& underline
)
217 m_underlined
= true ;
218 ::GetFNum( fontName
, &m_macFontNum
);
219 m_pointSize
= m_macFontSize
;
223 if ( m_faceName
.empty() )
228 m_macFontNum
= ::GetAppFont();
234 ::GetFNum( "\pTimes", &m_macFontNum
);
239 ::GetFNum( "\pLucida Grande", &m_macFontNum
);
241 ::GetFNum( "\pGeneva", &m_macFontNum
);
246 ::GetFNum( "\pMonaco", &m_macFontNum
);
254 ::GetFontName( m_macFontNum
, name
) ;
255 m_faceName
= wxMacMakeStringFromPascal( name
) ;
259 if ( m_faceName
== wxT("systemfont") )
260 m_macFontNum
= ::GetSysFont() ;
261 else if ( m_faceName
== wxT("applicationfont") )
262 m_macFontNum
= ::GetAppFont() ;
266 wxMacStringToPascal( m_faceName
, fontname
) ;
267 ::GetFNum( fontname
, &m_macFontNum
);
272 if (m_weight
== wxBOLD
)
273 m_macFontStyle
|= bold
;
274 if (m_style
== wxITALIC
|| m_style
== wxSLANT
)
275 m_macFontStyle
|= italic
;
277 m_macFontStyle
|= underline
;
278 m_macFontSize
= m_pointSize
;
281 // we try to get as much styles as possible into ATSU
283 Fixed atsuSize
= IntToFixed( m_macFontSize
) ;
285 Style atsuStyle
= normal
;
286 verify_noerr(::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
, (UInt32
*)&m_macATSUFontID
) );
287 if ( m_macFontStyle
& bold
)
290 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| bold
, &test
) == noErr
)
293 m_macATSUFontID
= test
;
297 if ( m_macFontStyle
& italic
)
300 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| italic
, &test
) == noErr
)
302 atsuStyle
|= italic
;
303 m_macATSUFontID
= test
;
307 if ( m_macFontStyle
& underline
)
310 if ( ::ATSUFONDtoFontID(m_macFontNum
, atsuStyle
| underline
, &test
) == noErr
)
312 atsuStyle
|= underline
;
313 m_macATSUFontID
= test
;
317 m_macATSUAdditionalQDStyles
= m_macFontStyle
& (~atsuStyle
) ;
319 if ( m_macATSUStyle
)
321 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
322 m_macATSUStyle
= NULL
;
325 OSStatus status
= ::ATSUCreateStyle((ATSUStyle
*)&m_macATSUStyle
) ;
326 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") ) ;
328 ATSUAttributeTag atsuTags
[] =
332 kATSUVerticalCharacterTag
,
335 kATSUQDUnderlineTag
,
336 kATSUQDCondensedTag
,
339 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
341 sizeof( ATSUFontID
) ,
343 sizeof( ATSUVerticalCharacterType
),
351 Boolean kTrue
= true ;
352 Boolean kFalse
= false ;
354 ATSUVerticalCharacterType kHorizontal
= kATSUStronglyHorizontal
;
355 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
360 (m_macATSUAdditionalQDStyles
& bold
) ? &kTrue
: &kFalse
,
361 (m_macATSUAdditionalQDStyles
& italic
) ? &kTrue
: &kFalse
,
362 (m_macATSUAdditionalQDStyles
& underline
) ? &kTrue
: &kFalse
,
363 (m_macATSUAdditionalQDStyles
& condense
) ? &kTrue
: &kFalse
,
364 (m_macATSUAdditionalQDStyles
& extend
) ? &kTrue
: &kFalse
,
367 status
= ::ATSUSetAttributes(
368 (ATSUStyle
)m_macATSUStyle
,
369 sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
370 atsuTags
, atsuSizes
, atsuValues
);
372 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") ) ;
375 // ----------------------------------------------------------------------------
377 // ----------------------------------------------------------------------------
379 bool wxFont::Create(const wxNativeFontInfo
& info
)
382 info
.pointSize
, info
.family
, info
.style
, info
.weight
,
383 info
.underlined
, info
.faceName
, info
.encoding
);
386 wxFont::wxFont(const wxString
& fontdesc
)
388 wxNativeFontInfo info
;
389 if ( info
.FromString(fontdesc
) )
393 bool wxFont::Create(int pointSize
,
398 const wxString
& faceName
,
399 wxFontEncoding encoding
)
403 m_refData
= new wxFontRefData(
404 pointSize
, family
, style
, weight
,
405 underlined
, faceName
, encoding
);
412 bool wxFont::MacCreateThemeFont(wxUint16 themeFontID
)
416 m_refData
= new wxFontRefData(
417 12, wxDEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
,
418 false, wxEmptyString
, wxFONTENCODING_DEFAULT
);
420 M_FONTDATA
->m_macThemeFontID
= themeFontID
;
430 bool wxFont::RealizeResource()
432 M_FONTDATA
->MacFindFont() ;
437 void wxFont::SetEncoding(wxFontEncoding encoding
)
441 M_FONTDATA
->m_encoding
= encoding
;
446 void wxFont::Unshare()
448 // Don't change shared data
451 m_refData
= new wxFontRefData();
455 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
461 void wxFont::SetPointSize(int pointSize
)
465 M_FONTDATA
->m_pointSize
= pointSize
;
470 void wxFont::SetFamily(int family
)
474 M_FONTDATA
->m_family
= family
;
479 void wxFont::SetStyle(int style
)
483 M_FONTDATA
->m_style
= style
;
488 void wxFont::SetWeight(int weight
)
492 M_FONTDATA
->m_weight
= weight
;
497 void wxFont::SetFaceName(const wxString
& faceName
)
501 M_FONTDATA
->m_faceName
= faceName
;
506 void wxFont::SetUnderlined(bool underlined
)
510 M_FONTDATA
->m_underlined
= underlined
;
515 void wxFont::SetNoAntiAliasing( bool no
)
519 M_FONTDATA
->SetNoAntiAliasing( no
);
524 // ----------------------------------------------------------------------------
526 // ----------------------------------------------------------------------------
528 // TODO: insert checks everywhere for M_FONTDATA == NULL!
530 int wxFont::GetPointSize() const
532 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
534 return M_FONTDATA
->m_pointSize
;
537 int wxFont::GetFamily() const
539 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
541 return M_FONTDATA
->m_family
;
544 int wxFont::GetStyle() const
546 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
548 return M_FONTDATA
->m_style
;
551 int wxFont::GetWeight() const
553 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
555 return M_FONTDATA
->m_weight
;
558 bool wxFont::GetUnderlined() const
560 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
562 return M_FONTDATA
->m_underlined
;
565 wxString
wxFont::GetFaceName() const
567 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxEmptyString
, wxT("invalid font") );
569 return M_FONTDATA
->m_faceName
;
572 wxFontEncoding
wxFont::GetEncoding() const
574 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTENCODING_DEFAULT
, wxT("invalid font") );
576 return M_FONTDATA
->m_encoding
;
579 bool wxFont::GetNoAntiAliasing() const
581 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
583 return M_FONTDATA
->m_noAA
;
586 short wxFont::MacGetFontNum() const
588 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
590 return M_FONTDATA
->m_macFontNum
;
593 short wxFont::MacGetFontSize() const
595 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
597 return M_FONTDATA
->m_macFontSize
;
600 wxByte
wxFont::MacGetFontStyle() const
602 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
604 return M_FONTDATA
->m_macFontStyle
;
607 wxUint32
wxFont::MacGetATSUFontID() const
609 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
611 return M_FONTDATA
->m_macATSUFontID
;
614 void * wxFont::MacGetATSUStyle() const
616 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
618 return M_FONTDATA
->m_macATSUStyle
;
621 wxUint32
wxFont::MacGetATSUAdditionalQDStyles() const
623 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
625 return M_FONTDATA
->m_macATSUAdditionalQDStyles
;
628 wxUint16
wxFont::MacGetThemeFontID() const
630 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
632 return M_FONTDATA
->m_macThemeFontID
;
635 const wxNativeFontInfo
* wxFont::GetNativeFontInfo() const
637 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
638 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
640 M_FONTDATA
->m_info
.InitFromFont(*this);
642 return &(M_FONTDATA
->m_info
);