1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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"
24 #include "wx/fontutil.h"
25 #include "wx/graphics.h"
26 #include "wx/settings.h"
27 #include "wx/tokenzr.h"
29 #include "wx/osx/private.h"
34 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
41 m_info
.Init(10, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
,
42 false, wxEmptyString
, wxFONTENCODING_DEFAULT
);
45 wxFontRefData(const wxFontRefData
& data
);
47 wxFontRefData( const wxNativeFontInfo
& info
) : m_info(info
)
52 wxFontRefData(wxOSXSystemFont font
, int size
);
54 #if wxOSX_USE_CORE_TEXT
55 wxFontRefData( wxUint32 coreTextFontType
);
56 wxFontRefData( CTFontRef font
);
57 wxFontRefData( CTFontDescriptorRef fontdescriptor
, int size
);
60 virtual ~wxFontRefData();
62 void SetPointSize( int size
)
64 if( GetPointSize() != size
)
66 m_info
.SetPointSize(size
);
71 int GetPointSize() const { return m_info
.GetPointSize(); }
73 void SetFamily( wxFontFamily family
)
75 if ( m_info
.m_family
!= family
)
77 m_info
.SetFamily( family
);
82 wxFontFamily
GetFamily() const { return m_info
.GetFamily(); }
84 void SetStyle( wxFontStyle style
)
86 if ( m_info
.m_style
!= style
)
88 m_info
.SetStyle( style
);
94 wxFontStyle
GetStyle() const { return m_info
.GetStyle(); }
96 void SetWeight( wxFontWeight weight
)
98 if ( m_info
.m_weight
!= weight
)
100 m_info
.SetWeight( weight
);
106 wxFontWeight
GetWeight() const { return m_info
.GetWeight(); }
108 void SetUnderlined( bool u
)
110 if ( m_info
.m_underlined
!= u
)
112 m_info
.SetUnderlined( u
);
117 bool GetUnderlined() const { return m_info
.GetUnderlined(); }
119 void SetFaceName( const wxString
& facename
)
121 if ( m_info
.m_faceName
!= facename
)
123 m_info
.SetFaceName( facename
);
128 wxString
GetFaceName() const { return m_info
.GetFaceName(); }
130 void SetEncoding( wxFontEncoding encoding
)
132 if ( m_info
.m_encoding
!= encoding
)
134 m_info
.SetEncoding( encoding
);
139 wxFontEncoding
GetEncoding() const { return m_info
.GetEncoding(); }
141 bool IsFixedWidth() const;
148 // common part of all ctors
150 #if wxOSX_USE_CORE_TEXT
151 // void Init( CTFontRef font );
155 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
156 // for true theming support we must store the correct font
157 // information here, as this speeds up and optimizes rendering
158 ThemeFontID m_macThemeFontID
;
160 #if wxOSX_USE_CORE_TEXT
161 wxCFRef
<CTFontRef
> m_ctFont
;
163 #if wxOSX_USE_ATSU_TEXT
164 void CreateATSUFont();
166 ATSUStyle m_macATSUStyle
;
168 wxCFRef
<CGFontRef
> m_cgFont
;
175 wxNativeFontInfo m_info
;
178 #define M_FONTDATA ((wxFontRefData*)m_refData)
180 wxFontRefData::wxFontRefData(const wxFontRefData
& data
) : wxGDIRefData()
183 m_info
= data
.m_info
;
184 m_fontValid
= data
.m_fontValid
;
185 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
186 m_macThemeFontID
= data
.m_macThemeFontID
;
188 #if wxOSX_USE_CORE_TEXT
189 m_ctFont
= data
.m_ctFont
;
191 m_cgFont
= data
.m_cgFont
;
192 #if wxOSX_USE_ATSU_TEXT
193 if ( data
.m_macATSUStyle
!= NULL
)
195 ATSUCreateStyle(&m_macATSUStyle
) ;
196 ATSUCopyAttributes(data
.m_macATSUStyle
, m_macATSUStyle
);
200 m_nsFont
= (NSFont
*) wxMacCocoaRetain(data
.m_nsFont
);
203 m_uiFont
= (UIFont
*) wxMacCocoaRetain(data
.m_uiFont
);
208 // ============================================================================
210 // ============================================================================
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
216 void wxFontRefData::Init()
218 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
219 m_macThemeFontID
= kThemeCurrentPortFont
;
221 #if wxOSX_USE_ATSU_TEXT
222 m_macATSUStyle
= NULL
;
233 wxFontRefData::~wxFontRefData()
238 void wxFontRefData::Free()
240 #if wxOSX_USE_CORE_TEXT
244 #if wxOSX_USE_ATSU_TEXT
246 m_macThemeFontID
= kThemeCurrentPortFont
;
248 if ( m_macATSUStyle
)
250 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
251 m_macATSUStyle
= NULL
;
255 if (m_nsFont
!= NULL
)
257 wxMacCocoaRelease(m_nsFont
);
262 if (m_uiFont
!= NULL
)
264 wxMacCocoaRelease(m_uiFont
);
271 wxFontRefData::wxFontRefData(wxOSXSystemFont font
, int size
)
273 wxASSERT( font
!= wxOSX_SYSTEM_FONT_NONE
);
276 #if wxOSX_USE_CORE_TEXT
278 CTFontUIFontType uifont
= kCTFontSystemFontType
;
281 case wxOSX_SYSTEM_FONT_NORMAL
:
282 uifont
= kCTFontSystemFontType
;
284 case wxOSX_SYSTEM_FONT_BOLD
:
285 uifont
= kCTFontEmphasizedSystemFontType
;
287 case wxOSX_SYSTEM_FONT_SMALL
:
288 uifont
= kCTFontSmallSystemFontType
;
290 case wxOSX_SYSTEM_FONT_SMALL_BOLD
:
291 uifont
= kCTFontSmallEmphasizedSystemFontType
;
293 case wxOSX_SYSTEM_FONT_MINI
:
294 uifont
= kCTFontMiniSystemFontType
;
296 case wxOSX_SYSTEM_FONT_MINI_BOLD
:
297 uifont
= kCTFontMiniEmphasizedSystemFontType
;
299 case wxOSX_SYSTEM_FONT_LABELS
:
300 uifont
= kCTFontLabelFontType
;
302 case wxOSX_SYSTEM_FONT_VIEWS
:
303 uifont
= kCTFontViewsFontType
;
308 m_ctFont
.reset(CTFontCreateUIFontForLanguage( uifont
, (CGFloat
) size
, NULL
));
309 wxCFRef
<CTFontDescriptorRef
> descr
;
310 descr
.reset( CTFontCopyFontDescriptor( m_ctFont
) );
314 #if wxOSX_USE_ATSU_TEXT
316 #if !wxOSX_USE_CARBON
317 // not needed outside
318 ThemeFontID m_macThemeFontID
= kThemeSystemFont
;
322 case wxOSX_SYSTEM_FONT_NORMAL
:
323 m_macThemeFontID
= kThemeSystemFont
;
325 case wxOSX_SYSTEM_FONT_BOLD
:
326 m_macThemeFontID
= kThemeEmphasizedSystemFont
;
328 case wxOSX_SYSTEM_FONT_SMALL
:
329 m_macThemeFontID
= kThemeSmallSystemFont
;
331 case wxOSX_SYSTEM_FONT_SMALL_BOLD
:
332 m_macThemeFontID
= kThemeSmallEmphasizedSystemFont
;
334 case wxOSX_SYSTEM_FONT_MINI
:
335 m_macThemeFontID
= kThemeMiniSystemFont
;
337 case wxOSX_SYSTEM_FONT_MINI_BOLD
:
338 // bold not available under theming
339 m_macThemeFontID
= kThemeMiniSystemFont
;
341 case wxOSX_SYSTEM_FONT_LABELS
:
342 m_macThemeFontID
= kThemeLabelFont
;
344 case wxOSX_SYSTEM_FONT_VIEWS
:
345 m_macThemeFontID
= kThemeViewsFont
;
350 if ( m_info
.m_faceName
.empty() )
356 GetThemeFont( m_macThemeFontID
, GetApplicationScript(), qdFontName
, &fontSize
, &style
);
360 wxFontStyle fontstyle
= wxFONTSTYLE_NORMAL
;
361 wxFontWeight fontweight
= wxFONTWEIGHT_NORMAL
;
362 bool underlined
= false;
365 fontweight
= wxFONTWEIGHT_BOLD
;
367 fontweight
= wxFONTWEIGHT_NORMAL
;
368 if ( style
& italic
)
369 fontstyle
= wxFONTSTYLE_ITALIC
;
370 if ( style
& underline
)
373 m_info
.Init(fontSize
,wxFONTFAMILY_DEFAULT
,fontstyle
,fontweight
,underlined
,
374 wxMacMakeStringFromPascal( qdFontName
), wxFONTENCODING_DEFAULT
);
379 m_nsFont
= wxFont::OSXCreateNSFont( font
, &m_info
);
382 m_uiFont
= wxFont::OSXCreateUIFont( font
, &m_info
);
384 m_info
.EnsureValid();
385 #if wxOSX_USE_ATSU_TEXT
392 #if wxOSX_USE_ATSU_TEXT
393 void wxFontRefData::CreateATSUFont()
395 // we try to get as much styles as possible into ATSU
397 OSStatus status
= ::ATSUCreateStyle(&m_macATSUStyle
);
398 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
400 ATSUAttributeTag atsuTags
[] =
404 kATSUVerticalCharacterTag
,
407 kATSUQDUnderlineTag
,
408 kATSUQDCondensedTag
,
411 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
413 sizeof( ATSUFontID
) ,
415 sizeof( ATSUVerticalCharacterType
),
423 Boolean kTrue
= true ;
424 Boolean kFalse
= false ;
426 Fixed atsuSize
= IntToFixed( m_info
.m_pointSize
);
427 ATSUVerticalCharacterType kHorizontal
= kATSUStronglyHorizontal
;
428 FMFontStyle addQDStyle
= m_info
.m_atsuAdditionalQDStyles
;
429 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
431 &m_info
.m_atsuFontID
,
434 (addQDStyle
& bold
) ? &kTrue
: &kFalse
,
435 (addQDStyle
& italic
) ? &kTrue
: &kFalse
,
436 (addQDStyle
& underline
) ? &kTrue
: &kFalse
,
437 (addQDStyle
& condense
) ? &kTrue
: &kFalse
,
438 (addQDStyle
& extend
) ? &kTrue
: &kFalse
,
441 status
= ::ATSUSetAttributes(
442 (ATSUStyle
)m_macATSUStyle
,
444 atsuTags
, atsuSizes
, atsuValues
);
446 wxASSERT_MSG( status
== noErr
, wxString::Format(wxT("couldn't modify ATSU style. Status was %d"), (int) status
).c_str() );
448 if ( m_cgFont
.get() == NULL
)
450 ATSFontRef fontRef
= FMGetATSFontRefFromFont(m_info
.m_atsuFontID
);
451 m_cgFont
.reset( CGFontCreateWithPlatformFont( &fontRef
) );
456 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
457 static const CGAffineTransform kSlantTransform
= CGAffineTransformMake( 1, 0, tan(DegToRad(11)), 1, 0, 0 );
459 void wxFontRefData::MacFindFont()
464 wxCHECK_RET( m_info
.m_pointSize
> 0, wxT("Point size should not be zero.") );
466 m_info
.EnsureValid();
468 #if wxOSX_USE_CORE_TEXT
470 CTFontSymbolicTraits traits
= 0;
472 if (m_info
.m_weight
== wxFONTWEIGHT_BOLD
)
473 traits
|= kCTFontBoldTrait
;
474 if (m_info
.m_style
== wxFONTSTYLE_ITALIC
|| m_info
.m_style
== wxFONTSTYLE_SLANT
)
475 traits
|= kCTFontItalicTrait
;
478 wxString lookupnameWithSize
= wxString::Format( "%s_%u_%d", m_info
.m_faceName
, traits
, m_info
.m_pointSize
);
480 static std::map
< std::wstring
, wxCFRef
< CTFontRef
> > fontcache
;
481 m_ctFont
= fontcache
[ std::wstring(lookupnameWithSize
.wc_str()) ];
484 m_ctFont
.reset(CTFontCreateWithName( wxCFStringRef(m_info
.m_faceName
), m_info
.m_pointSize
, NULL
));
485 if ( m_ctFont
.get() == NULL
)
487 // TODO try fallbacks according to font type
488 m_ctFont
.reset(CTFontCreateUIFontForLanguage( kCTFontSystemFontType
, m_info
.m_pointSize
, NULL
));
494 // attempt native font variant, if not available, fallback to italic emulation mode and remove bold
495 CTFontRef fontWithTraits
= CTFontCreateCopyWithSymbolicTraits( m_ctFont
, 0, NULL
, traits
, traits
);
496 if ( fontWithTraits
== NULL
)
498 CTFontSymbolicTraits remainingTraits
= traits
;
499 const CGAffineTransform
* remainingTransform
= NULL
;
501 if( remainingTraits
& kCTFontItalicTrait
)
503 remainingTraits
&= ~kCTFontItalicTrait
;
504 remainingTransform
= &kSlantTransform
;
505 if ( remainingTraits
& kCTFontBoldTrait
)
507 // first try an emulated oblique with an existing bold font
508 fontWithTraits
= CTFontCreateCopyWithSymbolicTraits( m_ctFont
, 0, remainingTransform
, remainingTraits
, remainingTraits
);
509 if ( fontWithTraits
== NULL
)
511 // give in on the bold, try native oblique
512 fontWithTraits
= CTFontCreateCopyWithSymbolicTraits( m_ctFont
, 0, NULL
, kCTFontItalicTrait
, kCTFontItalicTrait
);
517 if ( fontWithTraits
== NULL
)
519 fontWithTraits
= CTFontCreateWithName( wxCFStringRef(m_info
.m_faceName
), m_info
.m_pointSize
, remainingTransform
);
523 if ( fontWithTraits
!= NULL
)
524 m_ctFont
.reset(fontWithTraits
);
529 m_cgFont
.reset(CTFontCopyGraphicsFont(m_ctFont
, NULL
));
533 #if wxOSX_USE_ATSU_TEXT
537 m_nsFont
= wxFont::OSXCreateNSFont( &m_info
);
540 m_uiFont
= wxFont::OSXCreateUIFont( &m_info
);
545 bool wxFontRefData::IsFixedWidth() const
547 #if wxOSX_USE_CORE_TEXT
548 CTFontSymbolicTraits traits
= CTFontGetSymbolicTraits(m_ctFont
);
549 return (traits
& kCTFontMonoSpaceTrait
) != 0;
555 // ----------------------------------------------------------------------------
557 // ----------------------------------------------------------------------------
559 bool wxFont::Create(const wxNativeFontInfo
& info
)
563 m_refData
= new wxFontRefData( info
);
569 wxFont::wxFont(wxOSXSystemFont font
)
571 m_refData
= new wxFontRefData( font
, 0 );
574 wxFont::wxFont(const wxString
& fontdesc
)
576 wxNativeFontInfo info
;
577 if ( info
.FromString(fontdesc
) )
581 bool wxFont::Create(int pointSize
,
586 const wxString
& faceNameParam
,
587 wxFontEncoding encoding
)
591 wxString faceName
= faceNameParam
;
593 if ( faceName
.empty() )
597 case wxFONTFAMILY_DEFAULT
:
598 faceName
= wxT("Lucida Grande");
601 case wxFONTFAMILY_SCRIPT
:
602 case wxFONTFAMILY_ROMAN
:
603 case wxFONTFAMILY_DECORATIVE
:
604 faceName
= wxT("Times");
607 case wxFONTFAMILY_SWISS
:
608 faceName
= wxT("Helvetica");
611 case wxFONTFAMILY_MODERN
:
612 case wxFONTFAMILY_TELETYPE
:
613 faceName
= wxT("Courier");
617 faceName
= wxT("Times");
622 wxNativeFontInfo info
;
624 info
.Init(pointSize
, family
, style
, weight
,
625 underlined
, faceName
, encoding
);
627 m_refData
= new wxFontRefData(info
);
636 void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
640 m_refData
= new wxFontRefData( info
);
644 bool wxFont::RealizeResource()
646 M_FONTDATA
->MacFindFont();
651 void wxFont::SetEncoding(wxFontEncoding encoding
)
655 M_FONTDATA
->SetEncoding( encoding
);
658 wxGDIRefData
*wxFont::CreateGDIRefData() const
660 return new wxFontRefData
;
663 wxGDIRefData
*wxFont::CloneGDIRefData(const wxGDIRefData
*data
) const
665 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));
668 void wxFont::SetPointSize(int pointSize
)
670 if ( M_FONTDATA
->GetPointSize() == pointSize
)
675 M_FONTDATA
->SetPointSize( pointSize
);
678 void wxFont::SetFamily(wxFontFamily family
)
682 M_FONTDATA
->SetFamily( family
);
685 void wxFont::SetStyle(wxFontStyle style
)
689 M_FONTDATA
->SetStyle( style
);
692 void wxFont::SetWeight(wxFontWeight weight
)
696 M_FONTDATA
->SetWeight( weight
);
699 bool wxFont::SetFaceName(const wxString
& faceName
)
703 M_FONTDATA
->SetFaceName( faceName
);
705 return wxFontBase::SetFaceName(faceName
);
708 void wxFont::SetUnderlined(bool underlined
)
712 M_FONTDATA
->SetUnderlined( underlined
);
715 // ----------------------------------------------------------------------------
717 // ----------------------------------------------------------------------------
719 // TODO: insert checks everywhere for M_FONTDATA == NULL!
721 int wxFont::GetPointSize() const
723 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
725 return M_FONTDATA
->GetPointSize();
728 wxSize
wxFont::GetPixelSize() const
730 #if wxUSE_GRAPHICS_CONTEXT
731 // TODO: consider caching the value
732 wxGraphicsContext
* dc
= wxGraphicsContext::CreateFromNative((CGContextRef
) NULL
);
733 dc
->SetFont(*(wxFont
*)this,*wxBLACK
);
734 wxDouble width
, height
= 0;
735 dc
->GetTextExtent( wxT("g"), &width
, &height
, NULL
, NULL
);
737 return wxSize((int)width
, (int)height
);
739 return wxFontBase::GetPixelSize();
743 bool wxFont::IsFixedWidth() const
745 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTWEIGHT_MAX
, wxT("invalid font") );
747 return M_FONTDATA
->IsFixedWidth();
750 wxFontFamily
wxFont::DoGetFamily() const
752 return M_FONTDATA
->GetFamily();
755 wxFontStyle
wxFont::GetStyle() const
757 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTSTYLE_MAX
, wxT("invalid font") );
759 return M_FONTDATA
->GetStyle() ;
762 wxFontWeight
wxFont::GetWeight() const
764 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTWEIGHT_MAX
, wxT("invalid font") );
766 return M_FONTDATA
->GetWeight();
769 bool wxFont::GetUnderlined() const
771 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
773 return M_FONTDATA
->GetUnderlined();
776 wxString
wxFont::GetFaceName() const
778 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxEmptyString
, wxT("invalid font") );
780 return M_FONTDATA
->GetFaceName() ;
783 wxFontEncoding
wxFont::GetEncoding() const
785 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTENCODING_DEFAULT
, wxT("invalid font") );
787 return M_FONTDATA
->GetEncoding() ;
790 #if wxOSX_USE_ATSU_TEXT && wxOSX_USE_CARBON
792 short wxFont::MacGetFontNum() const
794 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
796 // cast away constness otherwise lazy font resolution is not possible
797 const_cast<wxFont
*>(this)->RealizeResource();
799 return M_FONTDATA
->m_info
.m_qdFontFamily
;
802 wxByte
wxFont::MacGetFontStyle() const
804 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
806 // cast away constness otherwise lazy font resolution is not possible
807 const_cast<wxFont
*>(this)->RealizeResource();
809 return M_FONTDATA
->m_info
.m_qdFontStyle
;
812 wxUint16
wxFont::MacGetThemeFontID() const
814 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
816 return M_FONTDATA
->m_macThemeFontID
;
821 #if wxOSX_USE_ATSU_TEXT
822 void * wxFont::MacGetATSUStyle() const
824 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
826 // cast away constness otherwise lazy font resolution is not possible
827 const_cast<wxFont
*>(this)->RealizeResource();
829 return M_FONTDATA
->m_macATSUStyle
;
832 wxUint32
wxFont::MacGetATSUFontID() const
834 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
836 // cast away constness otherwise lazy font resolution is not possible
837 const_cast<wxFont
*>(this)->RealizeResource();
839 return M_FONTDATA
->m_info
.m_atsuFontID
;
842 wxUint32
wxFont::MacGetATSUAdditionalQDStyles() const
844 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
846 // cast away constness otherwise lazy font resolution is not possible
847 const_cast<wxFont
*>(this)->RealizeResource();
849 return M_FONTDATA
->m_info
.m_atsuAdditionalQDStyles
;
853 #if wxOSX_USE_CORE_TEXT
855 CTFontRef
wxFont::OSXGetCTFont() const
857 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
859 // cast away constness otherwise lazy font resolution is not possible
860 const_cast<wxFont
*>(this)->RealizeResource();
862 return (CTFontRef
)(M_FONTDATA
->m_ctFont
);
867 #if wxOSX_USE_COCOA_OR_CARBON
869 CGFontRef
wxFont::OSXGetCGFont() const
871 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
873 // cast away constness otherwise lazy font resolution is not possible
874 const_cast<wxFont
*>(this)->RealizeResource();
876 return (M_FONTDATA
->m_cgFont
);
884 NSFont
* wxFont::OSXGetNSFont() const
886 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
888 // cast away constness otherwise lazy font resolution is not possible
889 const_cast<wxFont
*>(this)->RealizeResource();
891 return (M_FONTDATA
->m_nsFont
);
898 UIFont
* wxFont::OSXGetUIFont() const
900 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
902 // cast away constness otherwise lazy font resolution is not possible
903 const_cast<wxFont
*>(this)->RealizeResource();
905 return (M_FONTDATA
->m_uiFont
);
910 const wxNativeFontInfo
* wxFont::GetNativeFontInfo() const
912 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
913 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid font") );
915 // cast away constness otherwise lazy font resolution is not possible
916 const_cast<wxFont
*>(this)->RealizeResource();
918 // M_FONTDATA->m_info.InitFromFont(*this);
920 return &(M_FONTDATA
->m_info
);
923 // ----------------------------------------------------------------------------
925 // ----------------------------------------------------------------------------
927 #if 0 // wxOSX_USE_CORE_TEXT
929 /* from Core Text Manual Common Operations */
931 static CTFontDescriptorRef
wxMacCreateCTFontDescriptor(CFStringRef iFamilyName
, CTFontSymbolicTraits iTraits
)
933 CTFontDescriptorRef descriptor
= NULL
;
934 CFMutableDictionaryRef attributes
;
936 wxASSERT(iFamilyName
!= NULL
);
937 // Create a mutable dictionary to hold our attributes.
938 attributes
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
939 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
940 wxASSERT(attributes
!= NULL
);
942 if (attributes
!= NULL
) {
943 // Add a family name to our attributes.
944 CFDictionaryAddValue(attributes
, kCTFontFamilyNameAttribute
, iFamilyName
);
948 CFMutableDictionaryRef traits
;
949 CFNumberRef symTraits
;
951 // Create the traits dictionary.
952 symTraits
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
,
954 wxASSERT(symTraits
!= NULL
);
956 if (symTraits
!= NULL
) {
957 // Create a dictionary to hold our traits values.
958 traits
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
959 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
960 wxASSERT(traits
!= NULL
);
962 if (traits
!= NULL
) {
963 // Add the symbolic traits value to the traits dictionary.
964 CFDictionaryAddValue(traits
, kCTFontSymbolicTrait
, symTraits
);
966 // Add the traits attribute to our attributes.
967 CFDictionaryAddValue(attributes
, kCTFontTraitsAttribute
, traits
);
970 CFRelease(symTraits
);
973 // Create the font descriptor with our attributes
974 descriptor
= CTFontDescriptorCreateWithAttributes(attributes
);
975 wxASSERT(descriptor
!= NULL
);
977 CFRelease(attributes
);
979 // Return our font descriptor.
985 void wxNativeFontInfo::Init()
987 #if wxOSX_USE_ATSU_TEXT
989 m_atsuAdditionalQDStyles
= 0;
990 m_atsuFontValid
= false;
997 m_family
= wxFONTFAMILY_DEFAULT
;
998 m_style
= wxFONTSTYLE_NORMAL
;
999 m_weight
= wxFONTWEIGHT_NORMAL
;
1000 m_underlined
= false;
1002 m_encoding
= wxFont::GetDefaultEncoding();
1003 m_descriptorValid
= false;
1006 #if wxOSX_USE_CORE_TEXT
1007 void wxNativeFontInfo::Init(CTFontDescriptorRef descr
)
1011 wxCFRef
< CFNumberRef
> sizevalue( (CFNumberRef
) CTFontDescriptorCopyAttribute( descr
, kCTFontSizeAttribute
) );
1013 if ( CFNumberGetValue( sizevalue
, kCFNumberFloatType
, &fsize
) )
1014 m_pointSize
= (int)( fsize
+ 0.5 );
1016 wxCFRef
< CFDictionaryRef
> traitsvalue( (CFDictionaryRef
) CTFontDescriptorCopyAttribute( descr
, kCTFontTraitsAttribute
) );
1017 CTFontSymbolicTraits traits
;
1018 if ( CFNumberGetValue((CFNumberRef
) CFDictionaryGetValue(traitsvalue
,kCTFontSymbolicTrait
),kCFNumberIntType
,&traits
) )
1020 if ( traits
& kCTFontItalicTrait
)
1021 m_style
= wxFONTSTYLE_ITALIC
;
1022 if ( traits
& kCTFontBoldTrait
)
1023 m_weight
= wxFONTWEIGHT_BOLD
;
1026 wxCFStringRef
familyName( (CFStringRef
) CTFontDescriptorCopyAttribute(descr
, kCTFontFamilyNameAttribute
));
1027 m_faceName
= familyName
.AsString();
1031 void wxNativeFontInfo::EnsureValid()
1033 if ( m_descriptorValid
)
1036 #if wxOSX_USE_ATSU_TEXT
1037 if ( !m_atsuFontValid
)
1039 #if !wxOSX_USE_CARBON
1040 // not needed outside
1041 wxInt16 m_qdFontFamily
;
1042 wxInt16 m_qdFontStyle
;
1044 wxCFStringRef
cf( m_faceName
, wxLocale::GetSystemEncoding() );
1045 ATSFontFamilyRef atsfamily
= ATSFontFamilyFindFromName( cf
, kATSOptionFlagsDefault
);
1046 if ( atsfamily
== (ATSFontFamilyRef
) -1 )
1048 wxLogDebug( wxT("ATSFontFamilyFindFromName failed for ") + m_faceName
);
1049 m_qdFontFamily
= GetAppFont();
1053 m_qdFontFamily
= FMGetFontFamilyFromATSFontFamilyRef( atsfamily
);
1057 if (m_weight
== wxFONTWEIGHT_BOLD
)
1058 m_qdFontStyle
|= bold
;
1059 if (m_style
== wxFONTSTYLE_ITALIC
|| m_style
== wxFONTSTYLE_SLANT
)
1060 m_qdFontStyle
|= italic
;
1062 m_qdFontStyle
|= underline
;
1065 // we try to get as much styles as possible into ATSU
1067 // ATSUFontID and FMFont are equivalent
1068 FMFontStyle intrinsicStyle
= 0 ;
1069 OSStatus status
= FMGetFontFromFontFamilyInstance( m_qdFontFamily
, m_qdFontStyle
, (FMFont
*)&m_atsuFontID
, &intrinsicStyle
);
1070 if ( status
!= noErr
)
1072 wxFAIL_MSG( wxT("couldn't get an ATSUFont from font family") );
1074 m_atsuAdditionalQDStyles
= m_qdFontStyle
& (~intrinsicStyle
);
1075 m_atsuFontValid
= true;
1078 m_descriptorValid
= true;
1081 void wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
1084 #if wxOSX_USE_ATSU_TEXT
1085 m_atsuFontValid
= info
.m_atsuFontValid
;
1086 m_atsuFontID
= info
.m_atsuFontID
;
1087 m_atsuAdditionalQDStyles
= info
.m_atsuAdditionalQDStyles
;
1088 #if wxOSX_USE_CARBON
1089 m_qdFontFamily
= info
.m_qdFontFamily
;
1090 m_qdFontStyle
= info
.m_qdFontStyle
;
1093 m_pointSize
= info
.m_pointSize
;
1094 m_family
= info
.m_family
;
1095 m_style
= info
.m_style
;
1096 m_weight
= info
.m_weight
;
1097 m_underlined
= info
.m_underlined
;
1098 m_faceName
= info
.m_faceName
;
1099 m_encoding
= info
.m_encoding
;
1100 m_descriptorValid
= info
.m_descriptorValid
;
1103 void wxNativeFontInfo::Init(int size
,
1104 wxFontFamily family
,
1106 wxFontWeight weight
,
1108 const wxString
& faceName
,
1109 wxFontEncoding encoding
)
1116 m_underlined
= underlined
;
1117 m_faceName
= faceName
;
1118 if ( encoding
== wxFONTENCODING_DEFAULT
)
1119 encoding
= wxFont::GetDefaultEncoding();
1120 m_encoding
= encoding
;
1124 void wxNativeFontInfo::Free()
1126 #if wxOSX_USE_ATSU_TEXT
1128 m_atsuAdditionalQDStyles
= 0;
1129 m_atsuFontValid
= false;
1131 m_descriptorValid
= false;
1134 bool wxNativeFontInfo::FromString(const wxString
& s
)
1138 wxStringTokenizer
tokenizer(s
, wxT(";"));
1140 wxString token
= tokenizer
.GetNextToken();
1142 // Ignore the version for now
1145 token
= tokenizer
.GetNextToken();
1146 if ( !token
.ToLong(&l
) )
1148 m_pointSize
= (int)l
;
1150 token
= tokenizer
.GetNextToken();
1151 if ( !token
.ToLong(&l
) )
1153 m_family
= (wxFontFamily
)l
;
1155 token
= tokenizer
.GetNextToken();
1156 if ( !token
.ToLong(&l
) )
1158 m_style
= (wxFontStyle
)l
;
1160 token
= tokenizer
.GetNextToken();
1161 if ( !token
.ToLong(&l
) )
1163 m_weight
= (wxFontWeight
)l
;
1165 token
= tokenizer
.GetNextToken();
1166 if ( !token
.ToLong(&l
) )
1168 m_underlined
= l
!= 0;
1170 m_faceName
= tokenizer
.GetNextToken();
1177 token
= tokenizer
.GetNextToken();
1178 if ( !token
.ToLong(&l
) )
1180 m_encoding
= (wxFontEncoding
)l
;
1185 wxString
wxNativeFontInfo::ToString() const
1189 s
.Printf(wxT("%d;%d;%d;%d;%d;%d;%s;%d"),
1196 m_faceName
.GetData(),
1202 int wxNativeFontInfo::GetPointSize() const
1207 wxFontStyle
wxNativeFontInfo::GetStyle() const
1212 wxFontWeight
wxNativeFontInfo::GetWeight() const
1217 bool wxNativeFontInfo::GetUnderlined() const
1219 return m_underlined
;
1222 wxString
wxNativeFontInfo::GetFaceName() const
1227 wxFontFamily
wxNativeFontInfo::GetFamily() const
1232 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
1237 bool wxNativeFontInfo::GetStrikethrough() const
1243 // changing the font descriptor
1245 void wxNativeFontInfo::SetPointSize(int pointsize
)
1247 if ( m_pointSize
!= pointsize
)
1249 m_pointSize
= pointsize
;
1254 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
1256 if ( m_style
!= style_
)
1263 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
1265 if ( m_weight
!= weight_
)
1272 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
1274 if ( m_underlined
!= underlined_
)
1276 m_underlined
= underlined_
;
1281 bool wxNativeFontInfo::SetFaceName(const wxString
& facename_
)
1283 if ( m_faceName
!= facename_
)
1285 m_faceName
= facename_
;
1291 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
1293 if ( m_family
!= family_
)
1300 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
1302 if ( encoding_
== wxFONTENCODING_DEFAULT
)
1303 encoding_
= wxFont::GetDefaultEncoding();
1304 m_encoding
= encoding_
;
1305 // not reflected in native descriptors
1308 void wxNativeFontInfo::SetStrikethrough(bool WXUNUSED(strikethrough
))