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 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
36 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
43 m_info
.Init(10, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
,
44 false, wxEmptyString
, wxFONTENCODING_DEFAULT
);
47 wxFontRefData(const wxFontRefData
& data
);
49 wxFontRefData( const wxNativeFontInfo
& info
) : m_info(info
)
54 wxFontRefData(wxOSXSystemFont font
, int size
);
56 #if wxOSX_USE_CORE_TEXT
57 wxFontRefData( wxUint32 coreTextFontType
);
58 wxFontRefData( CTFontRef font
);
59 wxFontRefData( CTFontDescriptorRef fontdescriptor
, int size
);
62 virtual ~wxFontRefData();
64 void SetPointSize( int size
)
66 if( GetPointSize() != size
)
68 m_info
.SetPointSize(size
);
73 int GetPointSize() const { return m_info
.GetPointSize(); }
75 void SetFamily( wxFontFamily family
)
77 if ( m_info
.m_family
!= family
)
79 m_info
.SetFamily( family
);
84 wxFontFamily
GetFamily() const { return m_info
.GetFamily(); }
86 void SetStyle( wxFontStyle style
)
88 if ( m_info
.m_style
!= style
)
90 m_info
.SetStyle( style
);
96 wxFontStyle
GetStyle() const { return m_info
.GetStyle(); }
98 void SetWeight( wxFontWeight weight
)
100 if ( m_info
.m_weight
!= weight
)
102 m_info
.SetWeight( weight
);
108 wxFontWeight
GetWeight() const { return m_info
.GetWeight(); }
110 void SetUnderlined( bool u
)
112 if ( m_info
.m_underlined
!= u
)
114 m_info
.SetUnderlined( u
);
119 bool GetUnderlined() const { return m_info
.GetUnderlined(); }
121 void SetFaceName( const wxString
& facename
)
123 if ( m_info
.m_faceName
!= facename
)
125 m_info
.SetFaceName( facename
);
130 wxString
GetFaceName() const { return m_info
.GetFaceName(); }
132 void SetEncoding( wxFontEncoding encoding
)
134 if ( m_info
.m_encoding
!= encoding
)
136 m_info
.SetEncoding( encoding
);
141 wxFontEncoding
GetEncoding() const { return m_info
.GetEncoding(); }
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
)
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
277 if ( UMAGetSystemVersion() >= 0x1050 )
279 CTFontUIFontType uifont
= kCTFontSystemFontType
;
282 case wxOSX_SYSTEM_FONT_NORMAL
:
283 uifont
= kCTFontSystemFontType
;
285 case wxOSX_SYSTEM_FONT_BOLD
:
286 uifont
= kCTFontEmphasizedSystemFontType
;
288 case wxOSX_SYSTEM_FONT_SMALL
:
289 uifont
= kCTFontSmallSystemFontType
;
291 case wxOSX_SYSTEM_FONT_SMALL_BOLD
:
292 uifont
= kCTFontSmallEmphasizedSystemFontType
;
294 case wxOSX_SYSTEM_FONT_MINI
:
295 uifont
= kCTFontMiniSystemFontType
;
297 case wxOSX_SYSTEM_FONT_MINI_BOLD
:
298 uifont
= kCTFontMiniEmphasizedSystemFontType
;
300 case wxOSX_SYSTEM_FONT_LABELS
:
301 uifont
= kCTFontLabelFontType
;
303 case wxOSX_SYSTEM_FONT_VIEWS
:
304 uifont
= kCTFontViewsFontType
;
309 m_ctFont
.reset(CTFontCreateUIFontForLanguage( uifont
, (CGFloat
) size
, NULL
));
310 wxCFRef
<CTFontDescriptorRef
> descr
;
311 descr
.reset( CTFontCopyFontDescriptor( m_ctFont
) );
315 #if wxOSX_USE_ATSU_TEXT
317 #if !wxOSX_USE_CARBON
318 // not needed outside
319 ThemeFontID m_macThemeFontID
= kThemeSystemFont
;
323 case wxOSX_SYSTEM_FONT_NORMAL
:
324 m_macThemeFontID
= kThemeSystemFont
;
326 case wxOSX_SYSTEM_FONT_BOLD
:
327 m_macThemeFontID
= kThemeEmphasizedSystemFont
;
329 case wxOSX_SYSTEM_FONT_SMALL
:
330 m_macThemeFontID
= kThemeSmallSystemFont
;
332 case wxOSX_SYSTEM_FONT_SMALL_BOLD
:
333 m_macThemeFontID
= kThemeSmallEmphasizedSystemFont
;
335 case wxOSX_SYSTEM_FONT_MINI
:
336 m_macThemeFontID
= kThemeMiniSystemFont
;
338 case wxOSX_SYSTEM_FONT_MINI_BOLD
:
339 // bold not available under theming
340 m_macThemeFontID
= kThemeMiniSystemFont
;
342 case wxOSX_SYSTEM_FONT_LABELS
:
343 m_macThemeFontID
= kThemeLabelFont
;
345 case wxOSX_SYSTEM_FONT_VIEWS
:
346 m_macThemeFontID
= kThemeViewsFont
;
351 if ( m_info
.m_faceName
.empty() )
357 GetThemeFont( m_macThemeFontID
, GetApplicationScript(), qdFontName
, &fontSize
, &style
);
361 wxFontStyle fontstyle
= wxFONTSTYLE_NORMAL
;
362 wxFontWeight fontweight
= wxFONTWEIGHT_NORMAL
;
363 bool underlined
= false;
366 fontweight
= wxFONTWEIGHT_BOLD
;
368 fontweight
= wxFONTWEIGHT_NORMAL
;
369 if ( style
& italic
)
370 fontstyle
= wxFONTSTYLE_ITALIC
;
371 if ( style
& underline
)
374 m_info
.Init(fontSize
,wxFONTFAMILY_DEFAULT
,fontstyle
,fontweight
,underlined
,
375 wxMacMakeStringFromPascal( qdFontName
), wxFONTENCODING_DEFAULT
);
380 m_nsFont
= wxFont::OSXCreateNSFont( font
, &m_info
);
383 m_uiFont
= wxFont::OSXCreateUIFont( font
, &m_info
);
385 m_info
.EnsureValid();
386 #if wxOSX_USE_ATSU_TEXT
393 #if wxOSX_USE_ATSU_TEXT
394 void wxFontRefData::CreateATSUFont()
396 // we try to get as much styles as possible into ATSU
398 OSStatus status
= ::ATSUCreateStyle(&m_macATSUStyle
);
399 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
401 ATSUAttributeTag atsuTags
[] =
405 kATSUVerticalCharacterTag
,
408 kATSUQDUnderlineTag
,
409 kATSUQDCondensedTag
,
412 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
414 sizeof( ATSUFontID
) ,
416 sizeof( ATSUVerticalCharacterType
),
424 Boolean kTrue
= true ;
425 Boolean kFalse
= false ;
427 Fixed atsuSize
= IntToFixed( m_info
.m_pointSize
);
428 ATSUVerticalCharacterType kHorizontal
= kATSUStronglyHorizontal
;
429 FMFontStyle addQDStyle
= m_info
.m_atsuAdditionalQDStyles
;
430 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
432 &m_info
.m_atsuFontID
,
435 (addQDStyle
& bold
) ? &kTrue
: &kFalse
,
436 (addQDStyle
& italic
) ? &kTrue
: &kFalse
,
437 (addQDStyle
& underline
) ? &kTrue
: &kFalse
,
438 (addQDStyle
& condense
) ? &kTrue
: &kFalse
,
439 (addQDStyle
& extend
) ? &kTrue
: &kFalse
,
442 status
= ::ATSUSetAttributes(
443 (ATSUStyle
)m_macATSUStyle
,
445 atsuTags
, atsuSizes
, atsuValues
);
447 wxASSERT_MSG( status
== noErr
, wxString::Format(wxT("couldn't modify ATSU style. Status was %d"), (int) status
).c_str() );
449 if ( m_cgFont
.get() == NULL
)
451 ATSFontRef fontRef
= FMGetATSFontRefFromFont(m_info
.m_atsuFontID
);
452 m_cgFont
.reset( CGFontCreateWithPlatformFont( &fontRef
) );
457 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
458 static const CGAffineTransform kSlantTransform
= CGAffineTransformMake( 1, 0, tan(DegToRad(11)), 1, 0, 0 );
460 void wxFontRefData::MacFindFont()
465 wxCHECK_RET( m_info
.m_pointSize
> 0, wxT("Point size should not be zero.") );
467 m_info
.EnsureValid();
469 #if wxOSX_USE_CORE_TEXT
470 if ( UMAGetSystemVersion() >= 0x1050 )
472 CTFontSymbolicTraits traits
= 0;
474 if (m_info
.m_weight
== wxFONTWEIGHT_BOLD
)
475 traits
|= kCTFontBoldTrait
;
476 if (m_info
.m_style
== wxFONTSTYLE_ITALIC
|| m_info
.m_style
== wxFONTSTYLE_SLANT
)
477 traits
|= kCTFontItalicTrait
;
480 wxString lookupnameWithSize
= wxString::Format( "%s_%u_%d", m_info
.m_faceName
, traits
, m_info
.m_pointSize
);
482 static std::map
< std::wstring
, wxCFRef
< CTFontRef
> > fontcache
;
483 m_ctFont
= fontcache
[ std::wstring(lookupnameWithSize
.wc_str()) ];
486 m_ctFont
.reset(CTFontCreateWithName( wxCFStringRef(m_info
.m_faceName
), m_info
.m_pointSize
, NULL
));
487 if ( m_ctFont
.get() == NULL
)
489 // TODO try fallbacks according to font type
490 m_ctFont
.reset(CTFontCreateUIFontForLanguage( kCTFontSystemFontType
, m_info
.m_pointSize
, NULL
));
496 // attempt native font variant, if not available, fallback to italic emulation mode and remove bold
497 CTFontRef fontWithTraits
= CTFontCreateCopyWithSymbolicTraits( m_ctFont
, 0, NULL
, traits
, traits
);
498 if ( fontWithTraits
== NULL
)
500 CTFontSymbolicTraits remainingTraits
= traits
;
501 const CGAffineTransform
* remainingTransform
= NULL
;
503 if( remainingTraits
& kCTFontItalicTrait
)
505 remainingTraits
&= ~kCTFontItalicTrait
;
506 remainingTransform
= &kSlantTransform
;
507 if ( remainingTraits
& kCTFontBoldTrait
)
509 // first try an emulated oblique with an existing bold font
510 fontWithTraits
= CTFontCreateCopyWithSymbolicTraits( m_ctFont
, 0, remainingTransform
, remainingTraits
, remainingTraits
);
511 if ( fontWithTraits
== NULL
)
513 // give in on the bold, try native oblique
514 fontWithTraits
= CTFontCreateCopyWithSymbolicTraits( m_ctFont
, 0, NULL
, kCTFontItalicTrait
, kCTFontItalicTrait
);
519 if ( fontWithTraits
== NULL
)
521 fontWithTraits
= CTFontCreateWithName( wxCFStringRef(m_info
.m_faceName
), m_info
.m_pointSize
, remainingTransform
);
525 if ( fontWithTraits
!= NULL
)
526 m_ctFont
.reset(fontWithTraits
);
531 m_cgFont
.reset(CTFontCopyGraphicsFont(m_ctFont
, NULL
));
535 #if wxOSX_USE_ATSU_TEXT
539 m_nsFont
= wxFont::OSXCreateNSFont( &m_info
);
542 m_uiFont
= wxFont::OSXCreateUIFont( &m_info
);
547 // ----------------------------------------------------------------------------
549 // ----------------------------------------------------------------------------
551 bool wxFont::Create(const wxNativeFontInfo
& info
)
555 m_refData
= new wxFontRefData( info
);
561 wxFont::wxFont(wxOSXSystemFont font
)
563 m_refData
= new wxFontRefData( font
, 0 );
566 wxFont::wxFont(const wxString
& fontdesc
)
568 wxNativeFontInfo info
;
569 if ( info
.FromString(fontdesc
) )
573 bool wxFont::Create(int pointSize
,
578 const wxString
& faceNameParam
,
579 wxFontEncoding encoding
)
583 wxString faceName
= faceNameParam
;
585 if ( faceName
.empty() )
589 case wxFONTFAMILY_DEFAULT
:
590 faceName
= wxT("Lucida Grande");
593 case wxFONTFAMILY_SCRIPT
:
594 case wxFONTFAMILY_ROMAN
:
595 case wxFONTFAMILY_DECORATIVE
:
596 faceName
= wxT("Times");
599 case wxFONTFAMILY_SWISS
:
600 faceName
= wxT("Helvetica");
603 case wxFONTFAMILY_MODERN
:
604 case wxFONTFAMILY_TELETYPE
:
605 faceName
= wxT("Courier");
609 faceName
= wxT("Times");
614 wxNativeFontInfo info
;
616 info
.Init(pointSize
, family
, style
, weight
,
617 underlined
, faceName
, encoding
);
619 m_refData
= new wxFontRefData(info
);
628 void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
632 m_refData
= new wxFontRefData( info
);
636 bool wxFont::RealizeResource()
638 M_FONTDATA
->MacFindFont();
643 void wxFont::SetEncoding(wxFontEncoding encoding
)
647 M_FONTDATA
->SetEncoding( encoding
);
650 wxGDIRefData
*wxFont::CreateGDIRefData() const
652 return new wxFontRefData
;
655 wxGDIRefData
*wxFont::CloneGDIRefData(const wxGDIRefData
*data
) const
657 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));
660 void wxFont::SetPointSize(int pointSize
)
662 if ( M_FONTDATA
->GetPointSize() == pointSize
)
667 M_FONTDATA
->SetPointSize( pointSize
);
670 void wxFont::SetFamily(wxFontFamily family
)
674 M_FONTDATA
->SetFamily( family
);
677 void wxFont::SetStyle(wxFontStyle style
)
681 M_FONTDATA
->SetStyle( style
);
684 void wxFont::SetWeight(wxFontWeight weight
)
688 M_FONTDATA
->SetWeight( weight
);
691 bool wxFont::SetFaceName(const wxString
& faceName
)
695 M_FONTDATA
->SetFaceName( faceName
);
697 return wxFontBase::SetFaceName(faceName
);
700 void wxFont::SetUnderlined(bool underlined
)
704 M_FONTDATA
->SetUnderlined( underlined
);
707 // ----------------------------------------------------------------------------
709 // ----------------------------------------------------------------------------
711 // TODO: insert checks everywhere for M_FONTDATA == NULL!
713 int wxFont::GetPointSize() const
715 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
717 return M_FONTDATA
->GetPointSize();
720 wxSize
wxFont::GetPixelSize() const
722 #if wxUSE_GRAPHICS_CONTEXT
723 // TODO: consider caching the value
724 wxGraphicsContext
* dc
= wxGraphicsContext::CreateFromNative((CGContextRef
) NULL
);
725 dc
->SetFont(*(wxFont
*)this,*wxBLACK
);
726 wxDouble width
, height
= 0;
727 dc
->GetTextExtent( wxT("g"), &width
, &height
, NULL
, NULL
);
729 return wxSize((int)width
, (int)height
);
731 return wxFontBase::GetPixelSize();
735 wxFontFamily
wxFont::DoGetFamily() const
737 return M_FONTDATA
->GetFamily();
740 wxFontStyle
wxFont::GetStyle() const
742 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTSTYLE_MAX
, wxT("invalid font") );
744 return M_FONTDATA
->GetStyle() ;
747 wxFontWeight
wxFont::GetWeight() const
749 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTWEIGHT_MAX
, wxT("invalid font") );
751 return M_FONTDATA
->GetWeight();
754 bool wxFont::GetUnderlined() const
756 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
758 return M_FONTDATA
->GetUnderlined();
761 wxString
wxFont::GetFaceName() const
763 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxEmptyString
, wxT("invalid font") );
765 return M_FONTDATA
->GetFaceName() ;
768 wxFontEncoding
wxFont::GetEncoding() const
770 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTENCODING_DEFAULT
, wxT("invalid font") );
772 return M_FONTDATA
->GetEncoding() ;
775 #if wxOSX_USE_ATSU_TEXT && wxOSX_USE_CARBON
777 short wxFont::MacGetFontNum() const
779 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
781 // cast away constness otherwise lazy font resolution is not possible
782 const_cast<wxFont
*>(this)->RealizeResource();
784 return M_FONTDATA
->m_info
.m_qdFontFamily
;
787 wxByte
wxFont::MacGetFontStyle() const
789 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
791 // cast away constness otherwise lazy font resolution is not possible
792 const_cast<wxFont
*>(this)->RealizeResource();
794 return M_FONTDATA
->m_info
.m_qdFontStyle
;
797 wxUint16
wxFont::MacGetThemeFontID() const
799 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
801 return M_FONTDATA
->m_macThemeFontID
;
806 #if wxOSX_USE_ATSU_TEXT
807 void * wxFont::MacGetATSUStyle() const
809 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
811 // cast away constness otherwise lazy font resolution is not possible
812 const_cast<wxFont
*>(this)->RealizeResource();
814 return M_FONTDATA
->m_macATSUStyle
;
817 #if WXWIN_COMPATIBILITY_2_8
818 wxUint32
wxFont::MacGetATSUFontID() const
820 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
822 // cast away constness otherwise lazy font resolution is not possible
823 const_cast<wxFont
*>(this)->RealizeResource();
825 return M_FONTDATA
->m_info
.m_atsuFontID
;
828 wxUint32
wxFont::MacGetATSUAdditionalQDStyles() const
830 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
832 // cast away constness otherwise lazy font resolution is not possible
833 const_cast<wxFont
*>(this)->RealizeResource();
835 return M_FONTDATA
->m_info
.m_atsuAdditionalQDStyles
;
841 #if wxOSX_USE_CORE_TEXT
843 CTFontRef
wxFont::OSXGetCTFont() const
845 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
847 // cast away constness otherwise lazy font resolution is not possible
848 const_cast<wxFont
*>(this)->RealizeResource();
850 return (CTFontRef
)(M_FONTDATA
->m_ctFont
);
855 #if wxOSX_USE_COCOA_OR_CARBON
857 CGFontRef
wxFont::OSXGetCGFont() const
859 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
861 // cast away constness otherwise lazy font resolution is not possible
862 const_cast<wxFont
*>(this)->RealizeResource();
864 return (M_FONTDATA
->m_cgFont
);
872 NSFont
* wxFont::OSXGetNSFont() const
874 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
876 // cast away constness otherwise lazy font resolution is not possible
877 const_cast<wxFont
*>(this)->RealizeResource();
879 return (M_FONTDATA
->m_nsFont
);
886 UIFont
* wxFont::OSXGetUIFont() const
888 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
890 // cast away constness otherwise lazy font resolution is not possible
891 const_cast<wxFont
*>(this)->RealizeResource();
893 return (M_FONTDATA
->m_uiFont
);
898 const wxNativeFontInfo
* wxFont::GetNativeFontInfo() const
900 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
901 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
903 // cast away constness otherwise lazy font resolution is not possible
904 const_cast<wxFont
*>(this)->RealizeResource();
906 // M_FONTDATA->m_info.InitFromFont(*this);
908 return &(M_FONTDATA
->m_info
);
911 // ----------------------------------------------------------------------------
913 // ----------------------------------------------------------------------------
915 #if 0 // wxOSX_USE_CORE_TEXT
917 /* from Core Text Manual Common Operations */
919 static CTFontDescriptorRef
wxMacCreateCTFontDescriptor(CFStringRef iFamilyName
, CTFontSymbolicTraits iTraits
)
921 CTFontDescriptorRef descriptor
= NULL
;
922 CFMutableDictionaryRef attributes
;
924 wxASSERT(iFamilyName
!= NULL
);
925 // Create a mutable dictionary to hold our attributes.
926 attributes
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
927 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
928 wxASSERT(attributes
!= NULL
);
930 if (attributes
!= NULL
) {
931 // Add a family name to our attributes.
932 CFDictionaryAddValue(attributes
, kCTFontFamilyNameAttribute
, iFamilyName
);
936 CFMutableDictionaryRef traits
;
937 CFNumberRef symTraits
;
939 // Create the traits dictionary.
940 symTraits
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
,
942 wxASSERT(symTraits
!= NULL
);
944 if (symTraits
!= NULL
) {
945 // Create a dictionary to hold our traits values.
946 traits
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
947 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
948 wxASSERT(traits
!= NULL
);
950 if (traits
!= NULL
) {
951 // Add the symbolic traits value to the traits dictionary.
952 CFDictionaryAddValue(traits
, kCTFontSymbolicTrait
, symTraits
);
954 // Add the traits attribute to our attributes.
955 CFDictionaryAddValue(attributes
, kCTFontTraitsAttribute
, traits
);
958 CFRelease(symTraits
);
961 // Create the font descriptor with our attributes
962 descriptor
= CTFontDescriptorCreateWithAttributes(attributes
);
963 wxASSERT(descriptor
!= NULL
);
965 CFRelease(attributes
);
967 // Return our font descriptor.
973 void wxNativeFontInfo::Init()
975 #if wxOSX_USE_ATSU_TEXT
977 m_atsuAdditionalQDStyles
= 0;
978 m_atsuFontValid
= false;
985 m_family
= wxFONTFAMILY_DEFAULT
;
986 m_style
= wxFONTSTYLE_NORMAL
;
987 m_weight
= wxFONTWEIGHT_NORMAL
;
988 m_underlined
= false;
990 m_encoding
= wxFont::GetDefaultEncoding();
991 m_descriptorValid
= false;
994 #if wxOSX_USE_CORE_TEXT
995 void wxNativeFontInfo::Init(CTFontDescriptorRef descr
)
999 wxCFRef
< CFNumberRef
> sizevalue( (CFNumberRef
) CTFontDescriptorCopyAttribute( descr
, kCTFontSizeAttribute
) );
1001 if ( CFNumberGetValue( sizevalue
, kCFNumberFloatType
, &fsize
) )
1002 m_pointSize
= (int)( fsize
+ 0.5 );
1004 wxCFRef
< CFDictionaryRef
> traitsvalue( (CFDictionaryRef
) CTFontDescriptorCopyAttribute( descr
, kCTFontTraitsAttribute
) );
1005 CTFontSymbolicTraits traits
;
1006 if ( CFNumberGetValue((CFNumberRef
) CFDictionaryGetValue(traitsvalue
,kCTFontSymbolicTrait
),kCFNumberIntType
,&traits
) )
1008 if ( traits
& kCTFontItalicTrait
)
1009 m_style
= wxFONTSTYLE_ITALIC
;
1010 if ( traits
& kCTFontBoldTrait
)
1011 m_weight
= wxFONTWEIGHT_BOLD
;
1014 wxCFStringRef
familyName( (CFStringRef
) CTFontDescriptorCopyAttribute(descr
, kCTFontFamilyNameAttribute
));
1015 m_faceName
= familyName
.AsString();
1019 void wxNativeFontInfo::EnsureValid()
1021 if ( m_descriptorValid
)
1024 #if wxOSX_USE_ATSU_TEXT
1025 if ( !m_atsuFontValid
)
1027 #if !wxOSX_USE_CARBON
1028 // not needed outside
1029 wxInt16 m_qdFontFamily
;
1030 wxInt16 m_qdFontStyle
;
1032 wxCFStringRef
cf( m_faceName
, wxLocale::GetSystemEncoding() );
1033 ATSFontFamilyRef atsfamily
= ATSFontFamilyFindFromName( cf
, kATSOptionFlagsDefault
);
1034 if ( atsfamily
== (ATSFontFamilyRef
) -1 )
1036 wxLogDebug( wxT("ATSFontFamilyFindFromName failed for ") + m_faceName
);
1037 m_qdFontFamily
= GetAppFont();
1041 m_qdFontFamily
= FMGetFontFamilyFromATSFontFamilyRef( atsfamily
);
1045 if (m_weight
== wxFONTWEIGHT_BOLD
)
1046 m_qdFontStyle
|= bold
;
1047 if (m_style
== wxFONTSTYLE_ITALIC
|| m_style
== wxFONTSTYLE_SLANT
)
1048 m_qdFontStyle
|= italic
;
1050 m_qdFontStyle
|= underline
;
1053 // we try to get as much styles as possible into ATSU
1055 // ATSUFontID and FMFont are equivalent
1056 FMFontStyle intrinsicStyle
= 0 ;
1057 OSStatus status
= FMGetFontFromFontFamilyInstance( m_qdFontFamily
, m_qdFontStyle
, (FMFont
*)&m_atsuFontID
, &intrinsicStyle
);
1058 if ( status
!= noErr
)
1060 wxFAIL_MSG( wxT("couldn't get an ATSUFont from font family") );
1062 m_atsuAdditionalQDStyles
= m_qdFontStyle
& (~intrinsicStyle
);
1063 m_atsuFontValid
= true;
1066 m_descriptorValid
= true;
1069 void wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
1072 #if wxOSX_USE_ATSU_TEXT
1073 m_atsuFontValid
= info
.m_atsuFontValid
;
1074 m_atsuFontID
= info
.m_atsuFontID
;
1075 m_atsuAdditionalQDStyles
= info
.m_atsuAdditionalQDStyles
;
1076 #if wxOSX_USE_CARBON
1077 m_qdFontFamily
= info
.m_qdFontFamily
;
1078 m_qdFontStyle
= info
.m_qdFontStyle
;
1081 m_pointSize
= info
.m_pointSize
;
1082 m_family
= info
.m_family
;
1083 m_style
= info
.m_style
;
1084 m_weight
= info
.m_weight
;
1085 m_underlined
= info
.m_underlined
;
1086 m_faceName
= info
.m_faceName
;
1087 m_encoding
= info
.m_encoding
;
1088 m_descriptorValid
= info
.m_descriptorValid
;
1091 void wxNativeFontInfo::Init(int size
,
1092 wxFontFamily family
,
1094 wxFontWeight weight
,
1096 const wxString
& faceName
,
1097 wxFontEncoding encoding
)
1104 m_underlined
= underlined
;
1105 m_faceName
= faceName
;
1106 if ( encoding
== wxFONTENCODING_DEFAULT
)
1107 encoding
= wxFont::GetDefaultEncoding();
1108 m_encoding
= encoding
;
1112 void wxNativeFontInfo::Free()
1114 #if wxOSX_USE_ATSU_TEXT
1116 m_atsuAdditionalQDStyles
= 0;
1117 m_atsuFontValid
= false;
1119 m_descriptorValid
= false;
1122 bool wxNativeFontInfo::FromString(const wxString
& s
)
1126 wxStringTokenizer
tokenizer(s
, wxT(";"));
1128 wxString token
= tokenizer
.GetNextToken();
1130 // Ignore the version for now
1133 token
= tokenizer
.GetNextToken();
1134 if ( !token
.ToLong(&l
) )
1136 m_pointSize
= (int)l
;
1138 token
= tokenizer
.GetNextToken();
1139 if ( !token
.ToLong(&l
) )
1141 m_family
= (wxFontFamily
)l
;
1143 token
= tokenizer
.GetNextToken();
1144 if ( !token
.ToLong(&l
) )
1146 m_style
= (wxFontStyle
)l
;
1148 token
= tokenizer
.GetNextToken();
1149 if ( !token
.ToLong(&l
) )
1151 m_weight
= (wxFontWeight
)l
;
1153 token
= tokenizer
.GetNextToken();
1154 if ( !token
.ToLong(&l
) )
1156 m_underlined
= l
!= 0;
1158 m_faceName
= tokenizer
.GetNextToken();
1165 token
= tokenizer
.GetNextToken();
1166 if ( !token
.ToLong(&l
) )
1168 m_encoding
= (wxFontEncoding
)l
;
1173 wxString
wxNativeFontInfo::ToString() const
1177 s
.Printf(wxT("%d;%d;%d;%d;%d;%d;%s;%d"),
1184 m_faceName
.GetData(),
1190 int wxNativeFontInfo::GetPointSize() const
1195 wxFontStyle
wxNativeFontInfo::GetStyle() const
1200 wxFontWeight
wxNativeFontInfo::GetWeight() const
1205 bool wxNativeFontInfo::GetUnderlined() const
1207 return m_underlined
;
1210 wxString
wxNativeFontInfo::GetFaceName() const
1215 wxFontFamily
wxNativeFontInfo::GetFamily() const
1220 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
1225 // changing the font descriptor
1227 void wxNativeFontInfo::SetPointSize(int pointsize
)
1229 if ( m_pointSize
!= pointsize
)
1231 m_pointSize
= pointsize
;
1236 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
1238 if ( m_style
!= style_
)
1245 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
1247 if ( m_weight
!= weight_
)
1254 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
1256 if ( m_underlined
!= underlined_
)
1258 m_underlined
= underlined_
;
1263 bool wxNativeFontInfo::SetFaceName(const wxString
& facename_
)
1265 if ( m_faceName
!= facename_
)
1267 m_faceName
= facename_
;
1273 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
1275 if ( m_family
!= family_
)
1282 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
1284 if ( encoding_
== wxFONTENCODING_DEFAULT
)
1285 encoding_
= wxFont::GetDefaultEncoding();
1286 m_encoding
= encoding_
;
1287 // not reflected in native descriptors