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_macFontFamily(data
.m_macFontFamily
)
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
135 FMFontFamily m_macFontFamily
;
136 FMFontSize m_macFontSize
;
137 FMFontStyle 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 FMFontStyle 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
;
182 m_macFontFamily
= 0 ;
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()
207 if ( m_macThemeFontID
!= kThemeCurrentPortFont
)
210 GetThemeFont( m_macThemeFontID
, GetApplicationScript(), qdFontName
, &m_macFontSize
, &style
);
211 m_macFontStyle
= style
;
212 m_faceName
= wxMacMakeStringFromPascal( qdFontName
);
213 if ( m_macFontStyle
& bold
)
216 m_weight
= wxNORMAL
;
217 if ( m_macFontStyle
& italic
)
219 if ( m_macFontStyle
& underline
)
220 m_underlined
= true ;
221 m_pointSize
= m_macFontSize
;
223 m_macFontFamily
= FMGetFontFamilyFromName( qdFontName
);
227 if ( m_faceName
.empty() )
229 if ( m_family
== wxDEFAULT
)
231 m_macFontFamily
= GetAppFont();
232 FMGetFontFamilyName(m_macFontFamily
,qdFontName
);
233 m_faceName
= wxMacMakeStringFromPascal( qdFontName
);
242 m_faceName
= wxT("Times");
246 m_faceName
= wxT("Lucida Grande");
250 m_faceName
= wxT("Monaco");
254 m_faceName
= wxT("Times");
257 wxMacStringToPascal( m_faceName
, qdFontName
);
258 m_macFontFamily
= FMGetFontFamilyFromName( qdFontName
);
263 if ( m_faceName
== wxT("systemfont") )
264 m_macFontFamily
= GetSysFont();
265 else if ( m_faceName
== wxT("applicationfont") )
266 m_macFontFamily
= GetAppFont();
269 wxMacCFStringHolder
cf( m_faceName
, wxLocale::GetSystemEncoding() );
270 ATSFontFamilyRef atsfamily
= ATSFontFamilyFindFromName( cf
, kATSOptionFlagsDefault
);
271 m_macFontFamily
= FMGetFontFamilyFromATSFontFamilyRef( atsfamily
);
276 if (m_weight
== wxBOLD
)
277 m_macFontStyle
|= bold
;
278 if (m_style
== wxITALIC
|| m_style
== wxSLANT
)
279 m_macFontStyle
|= italic
;
281 m_macFontStyle
|= underline
;
282 m_macFontSize
= m_pointSize
;
285 // we try to get as much styles as possible into ATSU
288 // ATSUFontID and FMFont are equivalent
289 FMFontStyle intrinsicStyle
= 0 ;
290 status
= FMGetFontFromFontFamilyInstance( m_macFontFamily
, m_macFontStyle
, &m_macATSUFontID
, &intrinsicStyle
);
291 wxASSERT_MSG( status
== noErr
, wxT("couldn't get an ATSUFont from font family") );
293 m_macATSUAdditionalQDStyles
= m_macFontStyle
& (~intrinsicStyle
);
295 if ( m_macATSUStyle
)
297 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
298 m_macATSUStyle
= NULL
;
301 status
= ::ATSUCreateStyle((ATSUStyle
*)&m_macATSUStyle
);
302 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
304 ATSUAttributeTag atsuTags
[] =
308 kATSUVerticalCharacterTag
,
311 kATSUQDUnderlineTag
,
312 kATSUQDCondensedTag
,
315 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
317 sizeof( ATSUFontID
) ,
319 sizeof( ATSUVerticalCharacterType
),
327 Boolean kTrue
= true ;
328 Boolean kFalse
= false ;
330 Fixed atsuSize
= IntToFixed( m_macFontSize
);
331 ATSUVerticalCharacterType kHorizontal
= kATSUStronglyHorizontal
;
332 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
337 (m_macATSUAdditionalQDStyles
& bold
) ? &kTrue
: &kFalse
,
338 (m_macATSUAdditionalQDStyles
& italic
) ? &kTrue
: &kFalse
,
339 (m_macATSUAdditionalQDStyles
& underline
) ? &kTrue
: &kFalse
,
340 (m_macATSUAdditionalQDStyles
& condense
) ? &kTrue
: &kFalse
,
341 (m_macATSUAdditionalQDStyles
& extend
) ? &kTrue
: &kFalse
,
344 status
= ::ATSUSetAttributes(
345 (ATSUStyle
)m_macATSUStyle
,
346 sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
347 atsuTags
, atsuSizes
, atsuValues
);
349 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
352 // ----------------------------------------------------------------------------
354 // ----------------------------------------------------------------------------
356 bool wxFont::Create(const wxNativeFontInfo
& info
)
359 info
.pointSize
, info
.family
, info
.style
, info
.weight
,
360 info
.underlined
, info
.faceName
, info
.encoding
);
363 wxFont::wxFont(const wxString
& fontdesc
)
365 wxNativeFontInfo info
;
366 if ( info
.FromString(fontdesc
) )
370 bool wxFont::Create(int pointSize
,
375 const wxString
& faceName
,
376 wxFontEncoding encoding
)
380 m_refData
= new wxFontRefData(
381 pointSize
, family
, style
, weight
,
382 underlined
, faceName
, encoding
);
389 bool wxFont::MacCreateThemeFont(wxUint16 themeFontID
)
393 m_refData
= new wxFontRefData(
394 12, wxDEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
,
395 false, wxEmptyString
, wxFONTENCODING_DEFAULT
);
397 M_FONTDATA
->m_macThemeFontID
= themeFontID
;
407 bool wxFont::RealizeResource()
409 M_FONTDATA
->MacFindFont();
414 void wxFont::SetEncoding(wxFontEncoding encoding
)
418 M_FONTDATA
->m_encoding
= encoding
;
423 void wxFont::Unshare()
425 // Don't change shared data
428 m_refData
= new wxFontRefData();
432 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
438 void wxFont::SetPointSize(int pointSize
)
442 M_FONTDATA
->m_pointSize
= pointSize
;
447 void wxFont::SetFamily(int family
)
451 M_FONTDATA
->m_family
= family
;
456 void wxFont::SetStyle(int style
)
460 M_FONTDATA
->m_style
= style
;
465 void wxFont::SetWeight(int weight
)
469 M_FONTDATA
->m_weight
= weight
;
474 void wxFont::SetFaceName(const wxString
& faceName
)
478 M_FONTDATA
->m_faceName
= faceName
;
483 void wxFont::SetUnderlined(bool underlined
)
487 M_FONTDATA
->m_underlined
= underlined
;
492 void wxFont::SetNoAntiAliasing( bool no
)
496 M_FONTDATA
->SetNoAntiAliasing( no
);
501 // ----------------------------------------------------------------------------
503 // ----------------------------------------------------------------------------
505 // TODO: insert checks everywhere for M_FONTDATA == NULL!
507 int wxFont::GetPointSize() const
509 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
511 return M_FONTDATA
->m_pointSize
;
514 int wxFont::GetFamily() const
516 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
518 return M_FONTDATA
->m_family
;
521 int wxFont::GetStyle() const
523 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
525 return M_FONTDATA
->m_style
;
528 int wxFont::GetWeight() const
530 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
532 return M_FONTDATA
->m_weight
;
535 bool wxFont::GetUnderlined() const
537 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
539 return M_FONTDATA
->m_underlined
;
542 wxString
wxFont::GetFaceName() const
544 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxEmptyString
, wxT("invalid font") );
546 return M_FONTDATA
->m_faceName
;
549 wxFontEncoding
wxFont::GetEncoding() const
551 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTENCODING_DEFAULT
, wxT("invalid font") );
553 return M_FONTDATA
->m_encoding
;
556 bool wxFont::GetNoAntiAliasing() const
558 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
560 return M_FONTDATA
->m_noAA
;
563 short wxFont::MacGetFontNum() const
565 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
567 return M_FONTDATA
->m_macFontFamily
;
570 short wxFont::MacGetFontSize() const
572 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
574 return M_FONTDATA
->m_macFontSize
;
577 wxByte
wxFont::MacGetFontStyle() const
579 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
581 return M_FONTDATA
->m_macFontStyle
;
584 wxUint32
wxFont::MacGetATSUFontID() const
586 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
588 return M_FONTDATA
->m_macATSUFontID
;
591 void * wxFont::MacGetATSUStyle() const
593 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
595 return M_FONTDATA
->m_macATSUStyle
;
598 wxUint32
wxFont::MacGetATSUAdditionalQDStyles() const
600 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
602 return M_FONTDATA
->m_macATSUAdditionalQDStyles
;
605 wxUint16
wxFont::MacGetThemeFontID() const
607 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
609 return M_FONTDATA
->m_macThemeFontID
;
612 const wxNativeFontInfo
* wxFont::GetNativeFontInfo() const
614 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
615 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
617 M_FONTDATA
->m_info
.InitFromFont(*this);
619 return &(M_FONTDATA
->m_info
);