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(); }
146 // common part of all ctors
148 #if wxOSX_USE_CORE_TEXT
149 // void Init( CTFontRef font );
153 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
154 // for true theming support we must store the correct font
155 // information here, as this speeds up and optimizes rendering
156 ThemeFontID m_macThemeFontID
;
158 #if wxOSX_USE_CORE_TEXT
159 wxCFRef
<CTFontRef
> m_ctFont
;
161 #if wxOSX_USE_ATSU_TEXT
162 void CreateATSUFont();
164 ATSUStyle m_macATSUStyle
;
166 wxCFRef
<CGFontRef
> m_cgFont
;
173 wxNativeFontInfo m_info
;
176 #define M_FONTDATA ((wxFontRefData*)m_refData)
178 wxFontRefData::wxFontRefData(const wxFontRefData
& data
) : wxGDIRefData()
181 m_info
= data
.m_info
;
182 m_fontValid
= data
.m_fontValid
;
183 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
184 m_macThemeFontID
= data
.m_macThemeFontID
;
186 #if wxOSX_USE_CORE_TEXT
187 m_ctFont
= data
.m_ctFont
;
189 m_cgFont
= data
.m_cgFont
;
190 #if wxOSX_USE_ATSU_TEXT
191 if ( data
.m_macATSUStyle
!= NULL
)
193 ATSUCreateStyle(&m_macATSUStyle
) ;
194 ATSUCopyAttributes(data
.m_macATSUStyle
, m_macATSUStyle
);
198 m_nsFont
= (NSFont
*) wxMacCocoaRetain(data
.m_nsFont
);
201 m_uiFont
= (UIFont
*) wxMacCocoaRetain(data
.m_uiFont
);
206 // ============================================================================
208 // ============================================================================
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
214 void wxFontRefData::Init()
216 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
217 m_macThemeFontID
= kThemeCurrentPortFont
;
219 #if wxOSX_USE_ATSU_TEXT
220 m_macATSUStyle
= NULL
;
231 wxFontRefData::~wxFontRefData()
236 void wxFontRefData::Free()
238 #if wxOSX_USE_CORE_TEXT
242 #if wxOSX_USE_ATSU_TEXT
244 m_macThemeFontID
= kThemeCurrentPortFont
;
246 if ( m_macATSUStyle
)
248 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
249 m_macATSUStyle
= NULL
;
253 if (m_nsFont
!= NULL
)
255 wxMacCocoaRelease(m_nsFont
);
260 if (m_uiFont
!= NULL
)
262 wxMacCocoaRelease(m_uiFont
);
269 wxFontRefData::wxFontRefData(wxOSXSystemFont font
, int size
)
271 wxASSERT( font
!= wxOSX_SYSTEM_FONT_NONE
);
274 #if wxOSX_USE_CORE_TEXT
275 if ( UMAGetSystemVersion() >= 0x1050 )
277 CTFontUIFontType uifont
= kCTFontSystemFontType
;
280 case wxOSX_SYSTEM_FONT_NORMAL
:
281 uifont
= kCTFontSystemFontType
;
283 case wxOSX_SYSTEM_FONT_BOLD
:
284 uifont
= kCTFontEmphasizedSystemFontType
;
286 case wxOSX_SYSTEM_FONT_SMALL
:
287 uifont
= kCTFontSmallSystemFontType
;
289 case wxOSX_SYSTEM_FONT_SMALL_BOLD
:
290 uifont
= kCTFontSmallEmphasizedSystemFontType
;
292 case wxOSX_SYSTEM_FONT_MINI
:
293 uifont
= kCTFontMiniSystemFontType
;
295 case wxOSX_SYSTEM_FONT_MINI_BOLD
:
296 uifont
= kCTFontMiniEmphasizedSystemFontType
;
298 case wxOSX_SYSTEM_FONT_LABELS
:
299 uifont
= kCTFontLabelFontType
;
301 case wxOSX_SYSTEM_FONT_VIEWS
:
302 uifont
= kCTFontViewsFontType
;
307 m_ctFont
.reset(CTFontCreateUIFontForLanguage( uifont
, (CGFloat
) size
, NULL
));
308 wxCFRef
<CTFontDescriptorRef
> descr
;
309 descr
.reset( CTFontCopyFontDescriptor( m_ctFont
) );
313 #if wxOSX_USE_ATSU_TEXT
315 #if !wxOSX_USE_CARBON
316 // not needed outside
317 ThemeFontID m_macThemeFontID
= kThemeSystemFont
;
321 case wxOSX_SYSTEM_FONT_NORMAL
:
322 m_macThemeFontID
= kThemeSystemFont
;
324 case wxOSX_SYSTEM_FONT_BOLD
:
325 m_macThemeFontID
= kThemeEmphasizedSystemFont
;
327 case wxOSX_SYSTEM_FONT_SMALL
:
328 m_macThemeFontID
= kThemeSmallSystemFont
;
330 case wxOSX_SYSTEM_FONT_SMALL_BOLD
:
331 m_macThemeFontID
= kThemeSmallEmphasizedSystemFont
;
333 case wxOSX_SYSTEM_FONT_MINI
:
334 m_macThemeFontID
= kThemeMiniSystemFont
;
336 case wxOSX_SYSTEM_FONT_MINI_BOLD
:
337 // bold not available under theming
338 m_macThemeFontID
= kThemeMiniSystemFont
;
340 case wxOSX_SYSTEM_FONT_LABELS
:
341 m_macThemeFontID
= kThemeLabelFont
;
343 case wxOSX_SYSTEM_FONT_VIEWS
:
344 m_macThemeFontID
= kThemeViewsFont
;
349 if ( m_info
.m_faceName
.empty() )
355 GetThemeFont( m_macThemeFontID
, GetApplicationScript(), qdFontName
, &fontSize
, &style
);
359 wxFontStyle fontstyle
= wxFONTSTYLE_NORMAL
;
360 wxFontWeight fontweight
= wxFONTWEIGHT_NORMAL
;
361 bool underlined
= false;
364 fontweight
= wxFONTWEIGHT_BOLD
;
366 fontweight
= wxFONTWEIGHT_NORMAL
;
367 if ( style
& italic
)
368 fontstyle
= wxFONTSTYLE_ITALIC
;
369 if ( style
& underline
)
372 m_info
.Init(fontSize
,wxFONTFAMILY_DEFAULT
,fontstyle
,fontweight
,underlined
,
373 wxMacMakeStringFromPascal( qdFontName
), wxFONTENCODING_DEFAULT
);
378 m_nsFont
= wxFont::OSXCreateNSFont( font
, &m_info
);
381 m_uiFont
= wxFont::OSXCreateUIFont( font
, &m_info
);
383 m_info
.EnsureValid();
384 #if wxOSX_USE_ATSU_TEXT
391 #if wxOSX_USE_ATSU_TEXT
392 void wxFontRefData::CreateATSUFont()
394 // we try to get as much styles as possible into ATSU
396 OSStatus status
= ::ATSUCreateStyle(&m_macATSUStyle
);
397 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
399 ATSUAttributeTag atsuTags
[] =
403 kATSUVerticalCharacterTag
,
406 kATSUQDUnderlineTag
,
407 kATSUQDCondensedTag
,
410 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
412 sizeof( ATSUFontID
) ,
414 sizeof( ATSUVerticalCharacterType
),
422 Boolean kTrue
= true ;
423 Boolean kFalse
= false ;
425 Fixed atsuSize
= IntToFixed( m_info
.m_pointSize
);
426 ATSUVerticalCharacterType kHorizontal
= kATSUStronglyHorizontal
;
427 FMFontStyle addQDStyle
= m_info
.m_atsuAdditionalQDStyles
;
428 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
430 &m_info
.m_atsuFontID
,
433 (addQDStyle
& bold
) ? &kTrue
: &kFalse
,
434 (addQDStyle
& italic
) ? &kTrue
: &kFalse
,
435 (addQDStyle
& underline
) ? &kTrue
: &kFalse
,
436 (addQDStyle
& condense
) ? &kTrue
: &kFalse
,
437 (addQDStyle
& extend
) ? &kTrue
: &kFalse
,
440 status
= ::ATSUSetAttributes(
441 (ATSUStyle
)m_macATSUStyle
,
443 atsuTags
, atsuSizes
, atsuValues
);
445 wxASSERT_MSG( status
== noErr
, wxString::Format(wxT("couldn't modify ATSU style. Status was %d"), (int) status
).c_str() );
447 if ( m_cgFont
.get() == NULL
)
449 ATSFontRef fontRef
= FMGetATSFontRefFromFont(m_info
.m_atsuFontID
);
450 m_cgFont
.reset( CGFontCreateWithPlatformFont( &fontRef
) );
455 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
456 static const CGAffineTransform kSlantTransform
= CGAffineTransformMake( 1, 0, tan(DegToRad(11)), 1, 0, 0 );
458 void wxFontRefData::MacFindFont()
463 wxCHECK_RET( m_info
.m_pointSize
> 0, wxT("Point size should not be zero.") );
465 m_info
.EnsureValid();
467 #if wxOSX_USE_CORE_TEXT
468 if ( UMAGetSystemVersion() >= 0x1050 )
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 // ----------------------------------------------------------------------------
547 // ----------------------------------------------------------------------------
549 bool wxFont::Create(const wxNativeFontInfo
& info
)
553 m_refData
= new wxFontRefData( info
);
559 wxFont::wxFont(wxOSXSystemFont font
)
561 m_refData
= new wxFontRefData( font
, 0 );
564 wxFont::wxFont(const wxString
& fontdesc
)
566 wxNativeFontInfo info
;
567 if ( info
.FromString(fontdesc
) )
571 bool wxFont::Create(int pointSize
,
576 const wxString
& faceNameParam
,
577 wxFontEncoding encoding
)
581 wxString faceName
= faceNameParam
;
583 if ( faceName
.empty() )
587 case wxFONTFAMILY_DEFAULT
:
588 faceName
= wxT("Lucida Grande");
591 case wxFONTFAMILY_SCRIPT
:
592 case wxFONTFAMILY_ROMAN
:
593 case wxFONTFAMILY_DECORATIVE
:
594 faceName
= wxT("Times");
597 case wxFONTFAMILY_SWISS
:
598 faceName
= wxT("Helvetica");
601 case wxFONTFAMILY_MODERN
:
602 case wxFONTFAMILY_TELETYPE
:
603 faceName
= wxT("Courier");
607 faceName
= wxT("Times");
612 wxNativeFontInfo info
;
614 info
.Init(pointSize
, family
, style
, weight
,
615 underlined
, faceName
, encoding
);
617 m_refData
= new wxFontRefData(info
);
626 void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
630 m_refData
= new wxFontRefData( info
);
634 bool wxFont::RealizeResource()
636 M_FONTDATA
->MacFindFont();
641 void wxFont::SetEncoding(wxFontEncoding encoding
)
645 M_FONTDATA
->SetEncoding( encoding
);
648 wxGDIRefData
*wxFont::CreateGDIRefData() const
650 return new wxFontRefData
;
653 wxGDIRefData
*wxFont::CloneGDIRefData(const wxGDIRefData
*data
) const
655 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));
658 void wxFont::SetPointSize(int pointSize
)
660 if ( M_FONTDATA
->GetPointSize() == pointSize
)
665 M_FONTDATA
->SetPointSize( pointSize
);
668 void wxFont::SetFamily(wxFontFamily family
)
672 M_FONTDATA
->SetFamily( family
);
675 void wxFont::SetStyle(wxFontStyle style
)
679 M_FONTDATA
->SetStyle( style
);
682 void wxFont::SetWeight(wxFontWeight weight
)
686 M_FONTDATA
->SetWeight( weight
);
689 bool wxFont::SetFaceName(const wxString
& faceName
)
693 M_FONTDATA
->SetFaceName( faceName
);
695 return wxFontBase::SetFaceName(faceName
);
698 void wxFont::SetUnderlined(bool underlined
)
702 M_FONTDATA
->SetUnderlined( underlined
);
705 // ----------------------------------------------------------------------------
707 // ----------------------------------------------------------------------------
709 // TODO: insert checks everywhere for M_FONTDATA == NULL!
711 int wxFont::GetPointSize() const
713 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
715 return M_FONTDATA
->GetPointSize();
718 wxSize
wxFont::GetPixelSize() const
720 #if wxUSE_GRAPHICS_CONTEXT
721 // TODO: consider caching the value
722 wxGraphicsContext
* dc
= wxGraphicsContext::CreateFromNative((CGContextRef
) NULL
);
723 dc
->SetFont(*(wxFont
*)this,*wxBLACK
);
724 wxDouble width
, height
= 0;
725 dc
->GetTextExtent( wxT("g"), &width
, &height
, NULL
, NULL
);
727 return wxSize((int)width
, (int)height
);
729 return wxFontBase::GetPixelSize();
733 wxFontFamily
wxFont::DoGetFamily() const
735 return M_FONTDATA
->GetFamily();
738 wxFontStyle
wxFont::GetStyle() const
740 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTSTYLE_MAX
, wxT("invalid font") );
742 return M_FONTDATA
->GetStyle() ;
745 wxFontWeight
wxFont::GetWeight() const
747 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTWEIGHT_MAX
, wxT("invalid font") );
749 return M_FONTDATA
->GetWeight();
752 bool wxFont::GetUnderlined() const
754 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
756 return M_FONTDATA
->GetUnderlined();
759 wxString
wxFont::GetFaceName() const
761 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxEmptyString
, wxT("invalid font") );
763 return M_FONTDATA
->GetFaceName() ;
766 wxFontEncoding
wxFont::GetEncoding() const
768 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTENCODING_DEFAULT
, wxT("invalid font") );
770 return M_FONTDATA
->GetEncoding() ;
773 #if wxOSX_USE_ATSU_TEXT && wxOSX_USE_CARBON
775 short wxFont::MacGetFontNum() const
777 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
779 // cast away constness otherwise lazy font resolution is not possible
780 const_cast<wxFont
*>(this)->RealizeResource();
782 return M_FONTDATA
->m_info
.m_qdFontFamily
;
785 wxByte
wxFont::MacGetFontStyle() const
787 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
789 // cast away constness otherwise lazy font resolution is not possible
790 const_cast<wxFont
*>(this)->RealizeResource();
792 return M_FONTDATA
->m_info
.m_qdFontStyle
;
795 wxUint16
wxFont::MacGetThemeFontID() const
797 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
799 return M_FONTDATA
->m_macThemeFontID
;
804 #if wxOSX_USE_ATSU_TEXT
805 void * wxFont::MacGetATSUStyle() const
807 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
809 // cast away constness otherwise lazy font resolution is not possible
810 const_cast<wxFont
*>(this)->RealizeResource();
812 return M_FONTDATA
->m_macATSUStyle
;
815 wxUint32
wxFont::MacGetATSUFontID() const
817 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
819 // cast away constness otherwise lazy font resolution is not possible
820 const_cast<wxFont
*>(this)->RealizeResource();
822 return M_FONTDATA
->m_info
.m_atsuFontID
;
825 wxUint32
wxFont::MacGetATSUAdditionalQDStyles() const
827 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
829 // cast away constness otherwise lazy font resolution is not possible
830 const_cast<wxFont
*>(this)->RealizeResource();
832 return M_FONTDATA
->m_info
.m_atsuAdditionalQDStyles
;
836 #if wxOSX_USE_CORE_TEXT
838 CTFontRef
wxFont::OSXGetCTFont() const
840 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
842 // cast away constness otherwise lazy font resolution is not possible
843 const_cast<wxFont
*>(this)->RealizeResource();
845 return (CTFontRef
)(M_FONTDATA
->m_ctFont
);
850 #if wxOSX_USE_COCOA_OR_CARBON
852 CGFontRef
wxFont::OSXGetCGFont() const
854 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
856 // cast away constness otherwise lazy font resolution is not possible
857 const_cast<wxFont
*>(this)->RealizeResource();
859 return (M_FONTDATA
->m_cgFont
);
867 NSFont
* wxFont::OSXGetNSFont() const
869 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
871 // cast away constness otherwise lazy font resolution is not possible
872 const_cast<wxFont
*>(this)->RealizeResource();
874 return (M_FONTDATA
->m_nsFont
);
881 UIFont
* wxFont::OSXGetUIFont() const
883 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
885 // cast away constness otherwise lazy font resolution is not possible
886 const_cast<wxFont
*>(this)->RealizeResource();
888 return (M_FONTDATA
->m_uiFont
);
893 const wxNativeFontInfo
* wxFont::GetNativeFontInfo() const
895 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
896 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid font") );
898 // cast away constness otherwise lazy font resolution is not possible
899 const_cast<wxFont
*>(this)->RealizeResource();
901 // M_FONTDATA->m_info.InitFromFont(*this);
903 return &(M_FONTDATA
->m_info
);
906 // ----------------------------------------------------------------------------
908 // ----------------------------------------------------------------------------
910 #if 0 // wxOSX_USE_CORE_TEXT
912 /* from Core Text Manual Common Operations */
914 static CTFontDescriptorRef
wxMacCreateCTFontDescriptor(CFStringRef iFamilyName
, CTFontSymbolicTraits iTraits
)
916 CTFontDescriptorRef descriptor
= NULL
;
917 CFMutableDictionaryRef attributes
;
919 wxASSERT(iFamilyName
!= NULL
);
920 // Create a mutable dictionary to hold our attributes.
921 attributes
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
922 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
923 wxASSERT(attributes
!= NULL
);
925 if (attributes
!= NULL
) {
926 // Add a family name to our attributes.
927 CFDictionaryAddValue(attributes
, kCTFontFamilyNameAttribute
, iFamilyName
);
931 CFMutableDictionaryRef traits
;
932 CFNumberRef symTraits
;
934 // Create the traits dictionary.
935 symTraits
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
,
937 wxASSERT(symTraits
!= NULL
);
939 if (symTraits
!= NULL
) {
940 // Create a dictionary to hold our traits values.
941 traits
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
942 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
943 wxASSERT(traits
!= NULL
);
945 if (traits
!= NULL
) {
946 // Add the symbolic traits value to the traits dictionary.
947 CFDictionaryAddValue(traits
, kCTFontSymbolicTrait
, symTraits
);
949 // Add the traits attribute to our attributes.
950 CFDictionaryAddValue(attributes
, kCTFontTraitsAttribute
, traits
);
953 CFRelease(symTraits
);
956 // Create the font descriptor with our attributes
957 descriptor
= CTFontDescriptorCreateWithAttributes(attributes
);
958 wxASSERT(descriptor
!= NULL
);
960 CFRelease(attributes
);
962 // Return our font descriptor.
968 void wxNativeFontInfo::Init()
970 #if wxOSX_USE_ATSU_TEXT
972 m_atsuAdditionalQDStyles
= 0;
973 m_atsuFontValid
= false;
980 m_family
= wxFONTFAMILY_DEFAULT
;
981 m_style
= wxFONTSTYLE_NORMAL
;
982 m_weight
= wxFONTWEIGHT_NORMAL
;
983 m_underlined
= false;
985 m_encoding
= wxFont::GetDefaultEncoding();
986 m_descriptorValid
= false;
989 #if wxOSX_USE_CORE_TEXT
990 void wxNativeFontInfo::Init(CTFontDescriptorRef descr
)
994 wxCFRef
< CFNumberRef
> sizevalue( (CFNumberRef
) CTFontDescriptorCopyAttribute( descr
, kCTFontSizeAttribute
) );
996 if ( CFNumberGetValue( sizevalue
, kCFNumberFloatType
, &fsize
) )
997 m_pointSize
= (int)( fsize
+ 0.5 );
999 wxCFRef
< CFDictionaryRef
> traitsvalue( (CFDictionaryRef
) CTFontDescriptorCopyAttribute( descr
, kCTFontTraitsAttribute
) );
1000 CTFontSymbolicTraits traits
;
1001 if ( CFNumberGetValue((CFNumberRef
) CFDictionaryGetValue(traitsvalue
,kCTFontSymbolicTrait
),kCFNumberIntType
,&traits
) )
1003 if ( traits
& kCTFontItalicTrait
)
1004 m_style
= wxFONTSTYLE_ITALIC
;
1005 if ( traits
& kCTFontBoldTrait
)
1006 m_weight
= wxFONTWEIGHT_BOLD
;
1009 wxCFStringRef
familyName( (CFStringRef
) CTFontDescriptorCopyAttribute(descr
, kCTFontFamilyNameAttribute
));
1010 m_faceName
= familyName
.AsString();
1014 void wxNativeFontInfo::EnsureValid()
1016 if ( m_descriptorValid
)
1019 #if wxOSX_USE_ATSU_TEXT
1020 if ( !m_atsuFontValid
)
1022 #if !wxOSX_USE_CARBON
1023 // not needed outside
1024 wxInt16 m_qdFontFamily
;
1025 wxInt16 m_qdFontStyle
;
1027 wxCFStringRef
cf( m_faceName
, wxLocale::GetSystemEncoding() );
1028 ATSFontFamilyRef atsfamily
= ATSFontFamilyFindFromName( cf
, kATSOptionFlagsDefault
);
1029 if ( atsfamily
== (ATSFontFamilyRef
) -1 )
1031 wxLogDebug( wxT("ATSFontFamilyFindFromName failed for ") + m_faceName
);
1032 m_qdFontFamily
= GetAppFont();
1036 m_qdFontFamily
= FMGetFontFamilyFromATSFontFamilyRef( atsfamily
);
1040 if (m_weight
== wxFONTWEIGHT_BOLD
)
1041 m_qdFontStyle
|= bold
;
1042 if (m_style
== wxFONTSTYLE_ITALIC
|| m_style
== wxFONTSTYLE_SLANT
)
1043 m_qdFontStyle
|= italic
;
1045 m_qdFontStyle
|= underline
;
1048 // we try to get as much styles as possible into ATSU
1050 // ATSUFontID and FMFont are equivalent
1051 FMFontStyle intrinsicStyle
= 0 ;
1052 OSStatus status
= FMGetFontFromFontFamilyInstance( m_qdFontFamily
, m_qdFontStyle
, (FMFont
*)&m_atsuFontID
, &intrinsicStyle
);
1053 if ( status
!= noErr
)
1055 wxFAIL_MSG( wxT("couldn't get an ATSUFont from font family") );
1057 m_atsuAdditionalQDStyles
= m_qdFontStyle
& (~intrinsicStyle
);
1058 m_atsuFontValid
= true;
1061 m_descriptorValid
= true;
1064 void wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
1067 #if wxOSX_USE_ATSU_TEXT
1068 m_atsuFontValid
= info
.m_atsuFontValid
;
1069 m_atsuFontID
= info
.m_atsuFontID
;
1070 m_atsuAdditionalQDStyles
= info
.m_atsuAdditionalQDStyles
;
1071 #if wxOSX_USE_CARBON
1072 m_qdFontFamily
= info
.m_qdFontFamily
;
1073 m_qdFontStyle
= info
.m_qdFontStyle
;
1076 m_pointSize
= info
.m_pointSize
;
1077 m_family
= info
.m_family
;
1078 m_style
= info
.m_style
;
1079 m_weight
= info
.m_weight
;
1080 m_underlined
= info
.m_underlined
;
1081 m_faceName
= info
.m_faceName
;
1082 m_encoding
= info
.m_encoding
;
1083 m_descriptorValid
= info
.m_descriptorValid
;
1086 void wxNativeFontInfo::Init(int size
,
1087 wxFontFamily family
,
1089 wxFontWeight weight
,
1091 const wxString
& faceName
,
1092 wxFontEncoding encoding
)
1099 m_underlined
= underlined
;
1100 m_faceName
= faceName
;
1101 if ( encoding
== wxFONTENCODING_DEFAULT
)
1102 encoding
= wxFont::GetDefaultEncoding();
1103 m_encoding
= encoding
;
1107 void wxNativeFontInfo::Free()
1109 #if wxOSX_USE_ATSU_TEXT
1111 m_atsuAdditionalQDStyles
= 0;
1112 m_atsuFontValid
= false;
1114 m_descriptorValid
= false;
1117 bool wxNativeFontInfo::FromString(const wxString
& s
)
1121 wxStringTokenizer
tokenizer(s
, wxT(";"));
1123 wxString token
= tokenizer
.GetNextToken();
1125 // Ignore the version for now
1128 token
= tokenizer
.GetNextToken();
1129 if ( !token
.ToLong(&l
) )
1131 m_pointSize
= (int)l
;
1133 token
= tokenizer
.GetNextToken();
1134 if ( !token
.ToLong(&l
) )
1136 m_family
= (wxFontFamily
)l
;
1138 token
= tokenizer
.GetNextToken();
1139 if ( !token
.ToLong(&l
) )
1141 m_style
= (wxFontStyle
)l
;
1143 token
= tokenizer
.GetNextToken();
1144 if ( !token
.ToLong(&l
) )
1146 m_weight
= (wxFontWeight
)l
;
1148 token
= tokenizer
.GetNextToken();
1149 if ( !token
.ToLong(&l
) )
1151 m_underlined
= l
!= 0;
1153 m_faceName
= tokenizer
.GetNextToken();
1160 token
= tokenizer
.GetNextToken();
1161 if ( !token
.ToLong(&l
) )
1163 m_encoding
= (wxFontEncoding
)l
;
1168 wxString
wxNativeFontInfo::ToString() const
1172 s
.Printf(wxT("%d;%d;%d;%d;%d;%d;%s;%d"),
1179 m_faceName
.GetData(),
1185 int wxNativeFontInfo::GetPointSize() const
1190 wxFontStyle
wxNativeFontInfo::GetStyle() const
1195 wxFontWeight
wxNativeFontInfo::GetWeight() const
1200 bool wxNativeFontInfo::GetUnderlined() const
1202 return m_underlined
;
1205 wxString
wxNativeFontInfo::GetFaceName() const
1210 wxFontFamily
wxNativeFontInfo::GetFamily() const
1215 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
1220 // changing the font descriptor
1222 void wxNativeFontInfo::SetPointSize(int pointsize
)
1224 if ( m_pointSize
!= pointsize
)
1226 m_pointSize
= pointsize
;
1231 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
1233 if ( m_style
!= style_
)
1240 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
1242 if ( m_weight
!= weight_
)
1249 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
1251 if ( m_underlined
!= underlined_
)
1253 m_underlined
= underlined_
;
1258 bool wxNativeFontInfo::SetFaceName(const wxString
& facename_
)
1260 if ( m_faceName
!= facename_
)
1262 m_faceName
= facename_
;
1268 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
1270 if ( m_family
!= family_
)
1277 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
1279 if ( encoding_
== wxFONTENCODING_DEFAULT
)
1280 encoding_
= wxFont::GetDefaultEncoding();
1281 m_encoding
= encoding_
;
1282 // not reflected in native descriptors