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/graphics.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
;
157 #define M_FONTDATA ((wxFontRefData*)m_refData)
160 // ============================================================================
162 // ============================================================================
164 // ----------------------------------------------------------------------------
166 // ----------------------------------------------------------------------------
168 void wxFontRefData::Init(int pointSize
,
173 const wxString
& faceName
,
174 wxFontEncoding encoding
)
177 m_pointSize
= pointSize
;
181 m_underlined
= underlined
;
182 m_faceName
= faceName
;
183 m_encoding
= encoding
;
185 m_macFontFamily
= 0 ;
189 m_macATSUAdditionalQDStyles
= 0 ;
190 m_macATSUStyle
= NULL
;
192 m_macThemeFontID
= kThemeCurrentPortFont
;
196 wxFontRefData::~wxFontRefData()
198 if ( m_macATSUStyle
)
200 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
201 m_macATSUStyle
= NULL
;
205 void wxFontRefData::MacFindFont()
210 if ( m_macThemeFontID
!= kThemeCurrentPortFont
)
213 GetThemeFont( m_macThemeFontID
, GetApplicationScript(), qdFontName
, &m_macFontSize
, &style
);
214 m_macFontStyle
= style
;
215 m_faceName
= wxMacMakeStringFromPascal( qdFontName
);
216 if ( m_macFontStyle
& bold
)
219 m_weight
= wxNORMAL
;
220 if ( m_macFontStyle
& italic
)
222 if ( m_macFontStyle
& underline
)
223 m_underlined
= true ;
224 m_pointSize
= m_macFontSize
;
226 m_macFontFamily
= FMGetFontFamilyFromName( qdFontName
);
231 if ( m_faceName
.empty() )
233 if ( m_family
== wxDEFAULT
)
236 m_macFontFamily
= GetAppFont();
237 FMGetFontFamilyName(m_macFontFamily
,qdFontName
);
238 m_faceName
= wxMacMakeStringFromPascal( qdFontName
);
248 m_faceName
= wxT("Times");
252 m_faceName
= wxT("Lucida Grande");
256 m_faceName
= wxT("Monaco");
260 m_faceName
= wxT("Times");
264 wxMacStringToPascal( m_faceName
, qdFontName
);
265 m_macFontFamily
= FMGetFontFamilyFromName( qdFontName
);
272 if ( m_faceName
== wxT("systemfont") )
273 m_macFontFamily
= GetSysFont();
274 else if ( m_faceName
== wxT("applicationfont") )
275 m_macFontFamily
= GetAppFont();
278 if ( m_faceName
== wxT("systemfont") )
279 m_faceName
= wxT("Lucida Grande");
280 else if ( m_faceName
== wxT("applicationfont") )
281 m_faceName
= wxT("Lucida Grande");
284 wxMacCFStringHolder
cf( m_faceName
, wxLocale::GetSystemEncoding() );
285 ATSFontFamilyRef atsfamily
= ATSFontFamilyFindFromName( cf
, kATSOptionFlagsDefault
);
286 wxASSERT_MSG( atsfamily
!= (ATSFontFamilyRef
) -1 , wxT("ATSFontFamilyFindFromName failed") );
287 m_macFontFamily
= FMGetFontFamilyFromATSFontFamilyRef( atsfamily
);
292 if (m_weight
== wxBOLD
)
293 m_macFontStyle
|= bold
;
294 if (m_style
== wxITALIC
|| m_style
== wxSLANT
)
295 m_macFontStyle
|= italic
;
297 m_macFontStyle
|= underline
;
298 m_macFontSize
= m_pointSize
;
301 // we try to get as much styles as possible into ATSU
304 // ATSUFontID and FMFont are equivalent
305 FMFontStyle intrinsicStyle
= 0 ;
307 status
= FMGetFontFromFontFamilyInstance( m_macFontFamily
, m_macFontStyle
, &m_macATSUFontID
, &intrinsicStyle
);
308 wxASSERT_MSG( status
== noErr
, wxT("couldn't get an ATSUFont from font family") );
310 m_macATSUAdditionalQDStyles
= m_macFontStyle
& (~intrinsicStyle
);
312 if ( m_macATSUStyle
)
314 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
315 m_macATSUStyle
= NULL
;
318 status
= ::ATSUCreateStyle((ATSUStyle
*)&m_macATSUStyle
);
319 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
321 ATSUAttributeTag atsuTags
[] =
325 kATSUVerticalCharacterTag
,
328 kATSUQDUnderlineTag
,
329 kATSUQDCondensedTag
,
332 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
334 sizeof( ATSUFontID
) ,
336 sizeof( ATSUVerticalCharacterType
),
344 Boolean kTrue
= true ;
345 Boolean kFalse
= false ;
347 Fixed atsuSize
= IntToFixed( m_macFontSize
);
348 ATSUVerticalCharacterType kHorizontal
= kATSUStronglyHorizontal
;
349 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
354 (m_macATSUAdditionalQDStyles
& bold
) ? &kTrue
: &kFalse
,
355 (m_macATSUAdditionalQDStyles
& italic
) ? &kTrue
: &kFalse
,
356 (m_macATSUAdditionalQDStyles
& underline
) ? &kTrue
: &kFalse
,
357 (m_macATSUAdditionalQDStyles
& condense
) ? &kTrue
: &kFalse
,
358 (m_macATSUAdditionalQDStyles
& extend
) ? &kTrue
: &kFalse
,
361 status
= ::ATSUSetAttributes(
362 (ATSUStyle
)m_macATSUStyle
,
363 sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
364 atsuTags
, atsuSizes
, atsuValues
);
366 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
369 // ----------------------------------------------------------------------------
371 // ----------------------------------------------------------------------------
373 bool wxFont::Create(const wxNativeFontInfo
& info
)
376 info
.pointSize
, info
.family
, info
.style
, info
.weight
,
377 info
.underlined
, info
.faceName
, info
.encoding
);
380 wxFont::wxFont(const wxString
& fontdesc
)
382 wxNativeFontInfo info
;
383 if ( info
.FromString(fontdesc
) )
387 bool wxFont::Create(int pointSize
,
392 const wxString
& faceName
,
393 wxFontEncoding encoding
)
397 m_refData
= new wxFontRefData(
398 pointSize
, family
, style
, weight
,
399 underlined
, faceName
, encoding
);
406 bool wxFont::MacCreateThemeFont(wxUint16 themeFontID
)
410 m_refData
= new wxFontRefData(
411 12, wxDEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
,
412 false, wxEmptyString
, wxFONTENCODING_DEFAULT
);
414 M_FONTDATA
->m_macThemeFontID
= themeFontID
;
424 bool wxFont::RealizeResource()
426 M_FONTDATA
->MacFindFont();
431 void wxFont::SetEncoding(wxFontEncoding encoding
)
435 M_FONTDATA
->m_encoding
= encoding
;
440 void wxFont::Unshare()
442 // Don't change shared data
445 m_refData
= new wxFontRefData();
449 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
455 void wxFont::SetPointSize(int pointSize
)
459 M_FONTDATA
->m_pointSize
= pointSize
;
464 void wxFont::SetFamily(int family
)
468 M_FONTDATA
->m_family
= family
;
473 void wxFont::SetStyle(int style
)
477 M_FONTDATA
->m_style
= style
;
482 void wxFont::SetWeight(int weight
)
486 M_FONTDATA
->m_weight
= weight
;
491 bool wxFont::SetFaceName(const wxString
& faceName
)
495 M_FONTDATA
->m_faceName
= faceName
;
499 return wxFontBase::SetFaceName(faceName
);
502 void wxFont::SetUnderlined(bool underlined
)
506 M_FONTDATA
->m_underlined
= underlined
;
511 void wxFont::SetNoAntiAliasing( bool no
)
515 M_FONTDATA
->SetNoAntiAliasing( no
);
520 // ----------------------------------------------------------------------------
522 // ----------------------------------------------------------------------------
524 // TODO: insert checks everywhere for M_FONTDATA == NULL!
526 int wxFont::GetPointSize() const
528 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
530 return M_FONTDATA
->m_pointSize
;
533 wxSize
wxFont::GetPixelSize() const
535 #if wxUSE_GRAPHICS_CONTEXT
536 // TODO: consider caching the value
537 wxGraphicsContext
* dc
= wxGraphicsContext::CreateFromNative((CGContextRef
) NULL
);
538 dc
->SetFont(*(wxFont
*)this,*wxBLACK
);
539 wxDouble width
, height
= 0;
540 dc
->GetTextExtent( wxT("g"), &width
, &height
, NULL
, NULL
);
541 return wxSize((int)width
, (int)height
);
543 wxFontBase::GetPixelSize();
547 int wxFont::GetFamily() const
549 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
551 return M_FONTDATA
->m_family
;
554 int wxFont::GetStyle() const
556 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
558 return M_FONTDATA
->m_style
;
561 int wxFont::GetWeight() const
563 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
565 return M_FONTDATA
->m_weight
;
568 bool wxFont::GetUnderlined() const
570 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
572 return M_FONTDATA
->m_underlined
;
575 wxString
wxFont::GetFaceName() const
577 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxEmptyString
, wxT("invalid font") );
579 return M_FONTDATA
->m_faceName
;
582 wxFontEncoding
wxFont::GetEncoding() const
584 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTENCODING_DEFAULT
, wxT("invalid font") );
586 return M_FONTDATA
->m_encoding
;
589 bool wxFont::GetNoAntiAliasing() const
591 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
593 return M_FONTDATA
->m_noAA
;
596 short wxFont::MacGetFontNum() const
598 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
600 return M_FONTDATA
->m_macFontFamily
;
603 short wxFont::MacGetFontSize() const
605 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
607 return M_FONTDATA
->m_macFontSize
;
610 wxByte
wxFont::MacGetFontStyle() const
612 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
614 return M_FONTDATA
->m_macFontStyle
;
617 wxUint32
wxFont::MacGetATSUFontID() const
619 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
621 return M_FONTDATA
->m_macATSUFontID
;
624 void * wxFont::MacGetATSUStyle() const
626 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
628 return M_FONTDATA
->m_macATSUStyle
;
631 wxUint32
wxFont::MacGetATSUAdditionalQDStyles() const
633 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
635 return M_FONTDATA
->m_macATSUAdditionalQDStyles
;
638 wxUint16
wxFont::MacGetThemeFontID() const
640 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
642 return M_FONTDATA
->m_macThemeFontID
;
645 const wxNativeFontInfo
* wxFont::GetNativeFontInfo() const
647 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
648 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
650 M_FONTDATA
->m_info
.InitFromFont(*this);
652 return &(M_FONTDATA
->m_info
);