]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/font.cpp
c68ff2bd84776642852ea5cb85fae8d84f43ce8d
[wxWidgets.git] / src / osx / carbon / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/font.cpp
3 // Purpose: wxFont class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/font.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/string.h"
18 #include "wx/utils.h"
19 #include "wx/intl.h"
20 #include "wx/gdicmn.h"
21 #include "wx/log.h"
22 #endif
23
24 #include "wx/fontutil.h"
25 #include "wx/graphics.h"
26 #include "wx/settings.h"
27 #include "wx/tokenzr.h"
28
29 #include "wx/osx/private.h"
30
31 #if wxOSX_USE_ATSU_TEXT && !wxOSX_USE_CARBON
32 // include themeing support
33 #include <Carbon/Carbon.h>
34 #endif
35
36 #include <map>
37 #include <string>
38
39 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
40
41 class WXDLLEXPORT wxFontRefData: public wxGDIRefData
42 {
43 public:
44
45 wxFontRefData()
46 {
47 Init();
48 m_info.Init(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
49 false, wxEmptyString, wxFONTENCODING_DEFAULT);
50 }
51
52 wxFontRefData(const wxFontRefData& data);
53
54 wxFontRefData( const wxNativeFontInfo& info ) : m_info(info)
55 {
56 Init();
57 }
58
59 wxFontRefData(wxOSXSystemFont font, int size);
60
61 #if wxOSX_USE_CORE_TEXT
62 wxFontRefData( wxUint32 coreTextFontType );
63 wxFontRefData( CTFontRef font );
64 wxFontRefData( CTFontDescriptorRef fontdescriptor, int size );
65 #endif
66
67 virtual ~wxFontRefData();
68
69 void SetNoAntiAliasing( bool no = true ) { m_noAA = no; }
70
71 bool GetNoAntiAliasing() const { return m_noAA; }
72
73 void SetPointSize( int size )
74 {
75 if( GetPointSize() != size )
76 {
77 m_info.SetPointSize(size);
78 Free();
79 }
80 }
81
82 int GetPointSize() const { return m_info.GetPointSize(); }
83
84 void SetFamily( wxFontFamily family )
85 {
86 if ( m_info.m_family != family )
87 {
88 m_info.SetFamily( family );
89 Free();
90 }
91 }
92
93 wxFontFamily GetFamily() const { return m_info.GetFamily(); }
94
95 void SetStyle( wxFontStyle style )
96 {
97 if ( m_info.m_style != style )
98 {
99 m_info.SetStyle( style );
100 Free();
101 }
102 }
103
104
105 wxFontStyle GetStyle() const { return m_info.GetStyle(); }
106
107 void SetWeight( wxFontWeight weight )
108 {
109 if ( m_info.m_weight != weight )
110 {
111 m_info.SetWeight( weight );
112 Free();
113 }
114 }
115
116
117 wxFontWeight GetWeight() const { return m_info.GetWeight(); }
118
119 void SetUnderlined( bool u )
120 {
121 if ( m_info.m_underlined != u )
122 {
123 m_info.SetUnderlined( u );
124 Free();
125 }
126 }
127
128 bool GetUnderlined() const { return m_info.GetUnderlined(); }
129
130 void SetFaceName( const wxString& facename )
131 {
132 if ( m_info.m_faceName != facename )
133 {
134 m_info.SetFaceName( facename );
135 Free();
136 }
137 }
138
139 wxString GetFaceName() const { return m_info.GetFaceName(); }
140
141 void SetEncoding( wxFontEncoding encoding )
142 {
143 if ( m_info.m_encoding != encoding )
144 {
145 m_info.SetEncoding( encoding );
146 Free();
147 }
148 }
149
150 wxFontEncoding GetEncoding() const { return m_info.GetEncoding(); }
151
152 void Free();
153
154 void MacFindFont();
155
156 protected:
157 // common part of all ctors
158 void Init();
159 #if wxOSX_USE_CORE_TEXT
160 // void Init( CTFontRef font );
161 #endif
162 bool m_noAA; // No anti-aliasing
163 public:
164 bool m_fontValid;
165 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
166 // for true themeing support we must store the correct font
167 // information here, as this speeds up and optimizes rendering
168 ThemeFontID m_macThemeFontID ;
169 #endif
170 #if wxOSX_USE_CORE_TEXT
171 wxCFRef<CTFontRef> m_ctFont;
172 #endif
173 #if wxOSX_USE_ATSU_TEXT
174 ATSUStyle m_macATSUStyle ;
175 #endif
176 wxCFRef<CGFontRef> m_cgFont;
177 #if wxOSX_USE_COCOA
178 WX_NSFont m_nsFont;
179 #endif
180 #if wxOSX_USE_IPHONE
181 WX_UIFont m_uiFont;
182 #endif
183 wxNativeFontInfo m_info;
184 };
185
186 #define M_FONTDATA ((wxFontRefData*)m_refData)
187
188 wxFontRefData::wxFontRefData(const wxFontRefData& data)
189 {
190 Init();
191 m_info = data.m_info;
192 m_noAA = data.m_noAA;
193 m_fontValid = data.m_fontValid;
194 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
195 m_macThemeFontID = data.m_macThemeFontID;
196 #endif
197 #if wxOSX_USE_CORE_TEXT
198 m_ctFont = data.m_ctFont;
199 #endif
200 m_cgFont = data.m_cgFont;
201 #if wxOSX_USE_ATSU_TEXT
202 if ( data.m_macATSUStyle != NULL )
203 {
204 ATSUCreateStyle(&m_macATSUStyle) ;
205 ATSUCopyAttributes(data.m_macATSUStyle, m_macATSUStyle);
206 }
207 #endif
208 #if wxOSX_USE_COCOA
209 m_nsFont = (NSFont*) wxMacCocoaRetain(data.m_nsFont);
210 #endif
211 #if wxOSX_USE_IPHONE
212 m_uiFont = (UIFont*) wxMacCocoaRetain(data.m_uiFont);
213 #endif
214
215 }
216
217 // ============================================================================
218 // implementation
219 // ============================================================================
220
221 // ----------------------------------------------------------------------------
222 // wxFontRefData
223 // ----------------------------------------------------------------------------
224
225 void wxFontRefData::Init()
226 {
227 m_noAA = false;
228 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
229 m_macThemeFontID = kThemeCurrentPortFont ;
230 #endif
231 #if wxOSX_USE_ATSU_TEXT
232 m_macATSUStyle = NULL ;
233 #endif
234 #if wxOSX_USE_COCOA
235 m_nsFont = NULL;
236 #endif
237 #if wxOSX_USE_IPHONE
238 m_uiFont = NULL;
239 #endif
240 m_fontValid = false;
241 }
242
243 wxFontRefData::~wxFontRefData()
244 {
245 Free();
246 }
247
248 void wxFontRefData::Free()
249 {
250 #if wxOSX_USE_CORE_TEXT
251 m_ctFont.reset();
252 #endif
253 m_cgFont.reset();
254 #if wxOSX_USE_ATSU_TEXT
255 #if wxOSX_USE_CARBON
256 m_macThemeFontID = kThemeCurrentPortFont ;
257 #endif
258 if ( m_macATSUStyle )
259 {
260 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
261 m_macATSUStyle = NULL ;
262 }
263 #endif
264 #if wxOSX_USE_COCOA
265 if (m_nsFont != NULL)
266 {
267 wxMacCocoaRelease(m_nsFont);
268 m_nsFont = NULL;
269 }
270 #endif
271 #if wxOSX_USE_IPHONE
272 if (m_uiFont != NULL)
273 {
274 wxMacCocoaRelease(m_uiFont);
275 m_uiFont = NULL;
276 }
277 #endif
278 m_fontValid = false;
279 }
280
281 wxFontRefData::wxFontRefData(wxOSXSystemFont font, int size)
282 {
283 wxASSERT( font != wxOSX_SYSTEM_FONT_NONE );
284 Init();
285
286 #if wxOSX_USE_CORE_TEXT
287 if ( UMAGetSystemVersion() >= 0x1050 )
288 {
289 CTFontUIFontType uifont = kCTFontSystemFontType;
290 switch( font )
291 {
292 case wxOSX_SYSTEM_FONT_NORMAL:
293 uifont = kCTFontSystemFontType;
294 break;
295 case wxOSX_SYSTEM_FONT_BOLD:
296 uifont = kCTFontEmphasizedSystemFontType;
297 break;
298 case wxOSX_SYSTEM_FONT_SMALL:
299 uifont = kCTFontSmallSystemFontType;
300 break;
301 case wxOSX_SYSTEM_FONT_SMALL_BOLD:
302 uifont = kCTFontSmallEmphasizedSystemFontType;
303 break;
304 case wxOSX_SYSTEM_FONT_MINI:
305 uifont = kCTFontMiniSystemFontType;
306 break;
307 case wxOSX_SYSTEM_FONT_MINI_BOLD:
308 uifont = kCTFontMiniEmphasizedSystemFontType;
309 break;
310 case wxOSX_SYSTEM_FONT_LABELS:
311 uifont = kCTFontLabelFontType;
312 break;
313 case wxOSX_SYSTEM_FONT_VIEWS:
314 uifont = kCTFontViewsFontType;
315 break;
316 default:
317 break;
318 }
319 m_ctFont.reset(CTFontCreateUIFontForLanguage( uifont, (CGFloat) size, NULL ));
320 wxCFRef<CTFontDescriptorRef> descr;
321 descr.reset( CTFontCopyFontDescriptor( m_ctFont ) );
322 m_info.Init(descr);
323 }
324 #endif
325 #if wxOSX_USE_ATSU_TEXT
326 {
327 #if !wxOSX_USE_CARBON
328 // not needed outside
329 ThemeFontID m_macThemeFontID = kThemeSystemFont;
330 #endif
331 switch( font )
332 {
333 case wxOSX_SYSTEM_FONT_NORMAL:
334 m_macThemeFontID = kThemeSystemFont;
335 break;
336 case wxOSX_SYSTEM_FONT_BOLD:
337 m_macThemeFontID = kThemeEmphasizedSystemFont;
338 break;
339 case wxOSX_SYSTEM_FONT_SMALL:
340 m_macThemeFontID = kThemeSmallSystemFont;
341 break;
342 case wxOSX_SYSTEM_FONT_SMALL_BOLD:
343 m_macThemeFontID = kThemeSmallEmphasizedSystemFont;
344 break;
345 case wxOSX_SYSTEM_FONT_MINI:
346 m_macThemeFontID = kThemeMiniSystemFont;
347 break;
348 case wxOSX_SYSTEM_FONT_MINI_BOLD:
349 // bold not available under themeing
350 m_macThemeFontID = kThemeMiniSystemFont;
351 break;
352 case wxOSX_SYSTEM_FONT_LABELS:
353 m_macThemeFontID = kThemeLabelFont;
354 break;
355 case wxOSX_SYSTEM_FONT_VIEWS:
356 m_macThemeFontID = kThemeViewsFont;
357 break;
358 default:
359 break;
360 }
361 if ( m_info.m_faceName.empty() )
362 {
363 Style style ;
364 FMFontSize fontSize;
365 Str255 qdFontName ;
366
367 GetThemeFont( m_macThemeFontID, GetApplicationScript(), qdFontName, &fontSize, &style );
368 if ( size != 0 )
369 fontSize = size;
370
371 wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
372 wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
373 bool underlined = false;
374
375 if ( style & bold )
376 fontweight = wxFONTWEIGHT_BOLD ;
377 else
378 fontweight = wxFONTWEIGHT_NORMAL ;
379 if ( style & italic )
380 fontstyle = wxFONTSTYLE_ITALIC ;
381 if ( style & underline )
382 underlined = true ;
383
384 m_info.Init(fontSize,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined,
385 wxMacMakeStringFromPascal( qdFontName ), wxFONTENCODING_DEFAULT);
386 }
387 }
388 #endif
389 #if wxOSX_USE_COCOA
390 m_nsFont = wxFont::OSXCreateNSFont( font, &m_info );
391 #endif
392 #if wxOSX_USE_IPHONE
393 m_uiFont = wxFont::OSXCreateUIFont( font, &m_info );
394 #endif
395 }
396
397 void wxFontRefData::MacFindFont()
398 {
399 if ( m_fontValid )
400 return;
401
402 m_info.EnsureValid();
403
404 #if wxOSX_USE_CORE_TEXT
405 if ( UMAGetSystemVersion() >= 0x1050 )
406 {
407 CTFontSymbolicTraits traits = 0;
408
409 if (m_info.m_weight == wxFONTWEIGHT_BOLD)
410 traits |= kCTFontBoldTrait;
411 if (m_info.m_style == wxFONTSTYLE_ITALIC || m_info.m_style == wxFONTSTYLE_SLANT)
412 traits |= kCTFontItalicTrait;
413
414 // use font caching
415 wxString lookupnameWithSize = wxString::Format( "%s_%ld_%ld", m_info.m_faceName.c_str(), traits, m_info.m_pointSize );
416
417 static std::map< std::wstring , wxCFRef< CTFontRef > > fontcache ;
418 m_ctFont = fontcache[ std::wstring(lookupnameWithSize.wc_str()) ];
419 if ( !m_ctFont )
420 {
421 m_ctFont.reset( CTFontCreateWithFontDescriptor( m_info.m_ctFontDescriptor, 0/*m_pointSize */, NULL ) );
422 }
423 m_cgFont.reset(CTFontCopyGraphicsFont(m_ctFont, NULL));
424 }
425
426 #endif
427 #if wxOSX_USE_ATSU_TEXT
428 {
429 // we try to get as much styles as possible into ATSU
430
431 OSStatus status = ::ATSUCreateStyle(&m_macATSUStyle);
432 wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") );
433
434 ATSUAttributeTag atsuTags[] =
435 {
436 kATSUFontTag ,
437 kATSUSizeTag ,
438 kATSUVerticalCharacterTag,
439 kATSUQDBoldfaceTag ,
440 kATSUQDItalicTag ,
441 kATSUQDUnderlineTag ,
442 kATSUQDCondensedTag ,
443 kATSUQDExtendedTag ,
444 };
445 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
446 {
447 sizeof( ATSUFontID ) ,
448 sizeof( Fixed ) ,
449 sizeof( ATSUVerticalCharacterType),
450 sizeof( Boolean ) ,
451 sizeof( Boolean ) ,
452 sizeof( Boolean ) ,
453 sizeof( Boolean ) ,
454 sizeof( Boolean ) ,
455 };
456
457 Boolean kTrue = true ;
458 Boolean kFalse = false ;
459
460 Fixed atsuSize = IntToFixed( m_info.m_pointSize );
461 ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
462 FMFontStyle addQDStyle = m_info.m_atsuAdditionalQDStyles;
463 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
464 {
465 &m_info.m_atsuFontID ,
466 &atsuSize ,
467 &kHorizontal,
468 (addQDStyle & bold) ? &kTrue : &kFalse ,
469 (addQDStyle & italic) ? &kTrue : &kFalse ,
470 (addQDStyle & underline) ? &kTrue : &kFalse ,
471 (addQDStyle & condense) ? &kTrue : &kFalse ,
472 (addQDStyle & extend) ? &kTrue : &kFalse ,
473 };
474
475 status = ::ATSUSetAttributes(
476 (ATSUStyle)m_macATSUStyle,
477 sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,
478 atsuTags, atsuSizes, atsuValues);
479
480 wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );
481 if ( m_cgFont.get() == NULL )
482 {
483 ATSFontRef fontRef = FMGetATSFontRefFromFont(m_info.m_atsuFontID);
484 m_cgFont.reset( CGFontCreateWithPlatformFont( &fontRef ) );
485 }
486 }
487 #endif
488 #if wxOSX_USE_COCOA
489 m_nsFont = wxFont::OSXCreateNSFont( &m_info );
490 #endif
491 #if wxOSX_USE_IPHONE
492 m_uiFont = wxFont::OSXCreateUIFont( &m_info );
493 #endif
494 m_fontValid = true;
495 }
496
497 // ----------------------------------------------------------------------------
498 // wxFont
499 // ----------------------------------------------------------------------------
500
501 bool wxFont::Create(const wxNativeFontInfo& info)
502 {
503 UnRef();
504
505 m_refData = new wxFontRefData( info );
506 RealizeResource();
507
508 return true;
509 }
510
511 wxFont::wxFont(const wxString& fontdesc)
512 {
513 wxNativeFontInfo info;
514 if ( info.FromString(fontdesc) )
515 (void)Create(info);
516 }
517
518 bool wxFont::Create(int pointSize,
519 wxFontFamily family,
520 wxFontStyle style,
521 wxFontWeight weight,
522 bool underlined,
523 const wxString& faceNameParam,
524 wxFontEncoding encoding)
525 {
526 UnRef();
527
528 wxString faceName = faceNameParam;
529
530 if ( faceName.empty() )
531 {
532 switch ( family )
533 {
534 case wxFONTFAMILY_DEFAULT :
535 faceName = wxT("Lucida Grande");
536 break;
537
538 case wxFONTFAMILY_SCRIPT :
539 case wxFONTFAMILY_ROMAN :
540 case wxFONTFAMILY_DECORATIVE :
541 faceName = wxT("Times");
542 break ;
543
544 case wxFONTFAMILY_SWISS :
545 faceName = wxT("Helvetica");
546 break ;
547
548 case wxFONTFAMILY_MODERN :
549 case wxFONTFAMILY_TELETYPE:
550 faceName = wxT("Courier");
551 break ;
552
553 default:
554 faceName = wxT("Times");
555 break ;
556 }
557 }
558
559 wxNativeFontInfo info;
560
561 info.Init(pointSize, family, style, weight,
562 underlined, faceName, encoding);
563
564 m_refData = new wxFontRefData(info);
565
566 return true;
567 }
568
569 bool wxFont::CreateSystemFont(wxOSXSystemFont font)
570 {
571 UnRef();
572
573 m_refData = new wxFontRefData( font, 0 );
574
575 return true;
576 }
577
578 wxFont::~wxFont()
579 {
580 }
581
582 void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo& info)
583 {
584 UnRef();
585
586 m_refData = new wxFontRefData( info);
587 }
588
589
590 bool wxFont::RealizeResource()
591 {
592 M_FONTDATA->MacFindFont();
593
594 return true;
595 }
596
597 void wxFont::SetEncoding(wxFontEncoding encoding)
598 {
599 AllocExclusive();
600
601 M_FONTDATA->SetEncoding( encoding );
602 }
603
604 wxGDIRefData *wxFont::CreateGDIRefData() const
605 {
606 return new wxFontRefData;
607 }
608
609 wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const
610 {
611 return new wxFontRefData(*static_cast<const wxFontRefData *>(data));
612 }
613
614 void wxFont::SetPointSize(int pointSize)
615 {
616 if ( M_FONTDATA->GetPointSize() == pointSize )
617 return;
618
619 AllocExclusive();
620
621 M_FONTDATA->SetPointSize( pointSize );
622 }
623
624 void wxFont::SetFamily(wxFontFamily family)
625 {
626 AllocExclusive();
627
628 M_FONTDATA->SetFamily( family );
629 }
630
631 void wxFont::SetStyle(wxFontStyle style)
632 {
633 AllocExclusive();
634
635 M_FONTDATA->SetStyle( style );
636 }
637
638 void wxFont::SetWeight(wxFontWeight weight)
639 {
640 AllocExclusive();
641
642 M_FONTDATA->SetWeight( weight );
643 }
644
645 bool wxFont::SetFaceName(const wxString& faceName)
646 {
647 AllocExclusive();
648
649 M_FONTDATA->SetFaceName( faceName );
650
651 return wxFontBase::SetFaceName(faceName);
652 }
653
654 void wxFont::SetUnderlined(bool underlined)
655 {
656 AllocExclusive();
657
658 M_FONTDATA->SetUnderlined( underlined );
659 }
660
661 void wxFont::SetNoAntiAliasing( bool no )
662 {
663 AllocExclusive();
664
665 M_FONTDATA->SetNoAntiAliasing( no );
666 }
667
668 // ----------------------------------------------------------------------------
669 // accessors
670 // ----------------------------------------------------------------------------
671
672 // TODO: insert checks everywhere for M_FONTDATA == NULL!
673
674 int wxFont::GetPointSize() const
675 {
676 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
677
678 return M_FONTDATA->GetPointSize();
679 }
680
681 wxSize wxFont::GetPixelSize() const
682 {
683 #if wxUSE_GRAPHICS_CONTEXT
684 // TODO: consider caching the value
685 wxGraphicsContext* dc = wxGraphicsContext::CreateFromNative((CGContextRef) NULL);
686 dc->SetFont(*(wxFont *)this,*wxBLACK);
687 wxDouble width, height = 0;
688 dc->GetTextExtent( wxT("g"), &width, &height, NULL, NULL);
689 delete dc;
690 return wxSize((int)width, (int)height);
691 #else
692 return wxFontBase::GetPixelSize();
693 #endif
694 }
695
696 wxFontFamily wxFont::GetFamily() const
697 {
698 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTFAMILY_MAX, wxT("invalid font") );
699
700 return M_FONTDATA->GetFamily();
701 }
702
703 wxFontStyle wxFont::GetStyle() const
704 {
705 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTSTYLE_MAX, wxT("invalid font") );
706
707 return M_FONTDATA->GetStyle() ;
708 }
709
710 wxFontWeight wxFont::GetWeight() const
711 {
712 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTWEIGHT_MAX, wxT("invalid font") );
713
714 return M_FONTDATA->GetWeight();
715 }
716
717 bool wxFont::GetUnderlined() const
718 {
719 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
720
721 return M_FONTDATA->GetUnderlined();
722 }
723
724 wxString wxFont::GetFaceName() const
725 {
726 wxCHECK_MSG( M_FONTDATA != NULL , wxEmptyString , wxT("invalid font") );
727
728 return M_FONTDATA->GetFaceName() ;
729 }
730
731 wxFontEncoding wxFont::GetEncoding() const
732 {
733 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTENCODING_DEFAULT , wxT("invalid font") );
734
735 return M_FONTDATA->GetEncoding() ;
736 }
737
738 bool wxFont::GetNoAntiAliasing() const
739 {
740 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
741
742 return M_FONTDATA->GetNoAntiAliasing();
743 }
744
745 #if wxOSX_USE_ATSU_TEXT && wxOSX_USE_CARBON
746
747 short wxFont::MacGetFontNum() const
748 {
749 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
750
751 // cast away constness otherwise lazy font resolution is not possible
752 const_cast<wxFont *>(this)->RealizeResource();
753
754 return M_FONTDATA->m_info.m_qdFontFamily;
755 }
756
757 wxByte wxFont::MacGetFontStyle() const
758 {
759 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
760
761 // cast away constness otherwise lazy font resolution is not possible
762 const_cast<wxFont *>(this)->RealizeResource();
763
764 return M_FONTDATA->m_info.m_qdFontStyle;
765 }
766
767 wxUint16 wxFont::MacGetThemeFontID() const
768 {
769 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
770
771 return M_FONTDATA->m_macThemeFontID;
772 }
773
774 #endif
775
776 #if wxOSX_USE_ATSU_TEXT
777 void * wxFont::MacGetATSUStyle() const
778 {
779 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
780
781 // cast away constness otherwise lazy font resolution is not possible
782 const_cast<wxFont *>(this)->RealizeResource();
783
784 return M_FONTDATA->m_macATSUStyle;
785 }
786
787 #if WXWIN_COMPATIBILITY_2_8
788 wxUint32 wxFont::MacGetATSUFontID() const
789 {
790 wxCHECK_MSG( M_FONTDATA != NULL, 0, wxT("invalid font") );
791
792 // cast away constness otherwise lazy font resolution is not possible
793 const_cast<wxFont *>(this)->RealizeResource();
794
795 return M_FONTDATA->m_info.m_atsuFontID;
796 }
797
798 wxUint32 wxFont::MacGetATSUAdditionalQDStyles() const
799 {
800 wxCHECK_MSG( M_FONTDATA != NULL, 0, wxT("invalid font") );
801
802 // cast away constness otherwise lazy font resolution is not possible
803 const_cast<wxFont *>(this)->RealizeResource();
804
805 return M_FONTDATA->m_info.m_atsuAdditionalQDStyles;
806 }
807 #endif
808
809 #endif
810
811 #if wxOSX_USE_CORE_TEXT
812
813 CTFontRef wxFont::OSXGetCTFont() const
814 {
815 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
816
817 // cast away constness otherwise lazy font resolution is not possible
818 const_cast<wxFont *>(this)->RealizeResource();
819
820 return (CTFontRef)(M_FONTDATA->m_ctFont);
821 }
822
823 #endif
824
825 #if wxOSX_USE_COCOA_OR_CARBON
826
827 CGFontRef wxFont::OSXGetCGFont() const
828 {
829 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
830
831 // cast away constness otherwise lazy font resolution is not possible
832 const_cast<wxFont *>(this)->RealizeResource();
833
834 return (M_FONTDATA->m_cgFont);
835 }
836
837 #endif
838
839
840 #if wxOSX_USE_COCOA
841
842 NSFont* wxFont::OSXGetNSFont() const
843 {
844 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
845
846 // cast away constness otherwise lazy font resolution is not possible
847 const_cast<wxFont *>(this)->RealizeResource();
848
849 return (M_FONTDATA->m_nsFont);
850 }
851
852 #endif
853
854 const wxNativeFontInfo * wxFont::GetNativeFontInfo() const
855 {
856 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
857 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
858
859 // cast away constness otherwise lazy font resolution is not possible
860 const_cast<wxFont *>(this)->RealizeResource();
861
862 // M_FONTDATA->m_info.InitFromFont(*this);
863
864 return &(M_FONTDATA->m_info);
865 }
866
867 // ----------------------------------------------------------------------------
868 // wxNativeFontInfo
869 // ----------------------------------------------------------------------------
870
871 #if wxOSX_USE_CORE_TEXT
872
873 /* from Core Text Manual Common Operations */
874
875 static CTFontDescriptorRef wxMacCreateCTFontDescriptor(CFStringRef iFamilyName, CTFontSymbolicTraits iTraits )
876 {
877 CTFontDescriptorRef descriptor = NULL;
878 CFMutableDictionaryRef attributes;
879
880 assert(iFamilyName != NULL);
881 // Create a mutable dictionary to hold our attributes.
882 attributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
883 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
884 check(attributes != NULL);
885
886 if (attributes != NULL) {
887 // Add a family name to our attributes.
888 CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, iFamilyName);
889
890
891 if ( iTraits ) {
892 CFMutableDictionaryRef traits;
893 CFNumberRef symTraits;
894
895 // Create the traits dictionary.
896 symTraits = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type,
897 &iTraits);
898 check(symTraits != NULL);
899
900 if (symTraits != NULL) {
901 // Create a dictionary to hold our traits values.
902 traits = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
903 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
904 check(traits != NULL);
905
906 if (traits != NULL) {
907 // Add the symbolic traits value to the traits dictionary.
908 CFDictionaryAddValue(traits, kCTFontSymbolicTrait, symTraits);
909
910 // Add the traits attribute to our attributes.
911 CFDictionaryAddValue(attributes, kCTFontTraitsAttribute, traits);
912 CFRelease(traits);
913 }
914 CFRelease(symTraits);
915 }
916 }
917 // Create the font descriptor with our attributes
918 descriptor = CTFontDescriptorCreateWithAttributes(attributes);
919 check(descriptor != NULL);
920
921 CFRelease(attributes);
922 }
923 // Return our font descriptor.
924 return descriptor ;
925 }
926
927 #endif
928
929 void wxNativeFontInfo::Init()
930 {
931 #if wxOSX_USE_CORE_TEXT
932 m_ctFontDescriptor = NULL;
933 #endif
934 #if wxOSX_USE_ATSU_TEXT
935 m_atsuFontID = 0 ;
936 m_atsuAdditionalQDStyles = 0;
937 m_atsuFontValid = false;
938 #if wxOSX_USE_CARBON
939 m_qdFontStyle = 0;
940 m_qdFontFamily = 0;
941 #endif
942 #endif
943 #if wxOSX_USE_COCOA
944 m_nsFontDescriptor = NULL;
945 #endif
946 m_pointSize = 0;
947 m_family = wxFONTFAMILY_DEFAULT;
948 m_style = wxFONTSTYLE_NORMAL;
949 m_weight = wxFONTWEIGHT_NORMAL;
950 m_underlined = false;
951 m_faceName.clear();
952 m_encoding = wxFont::GetDefaultEncoding();
953 m_descriptorValid = false;
954 }
955
956 #if wxOSX_USE_CORE_TEXT
957 void wxNativeFontInfo::Init(CTFontDescriptorRef descr)
958 {
959 Init();
960 m_ctFontDescriptor = wxCFRetain(descr);
961
962 wxCFRef< CFNumberRef > sizevalue( (CFNumberRef) CTFontDescriptorCopyAttribute( m_ctFontDescriptor, kCTFontSizeAttribute ) );
963 float fsize;
964 if ( CFNumberGetValue( sizevalue , kCFNumberFloatType , &fsize ) )
965 m_pointSize = (int)( fsize + 0.5 );
966
967 wxCFRef< CFDictionaryRef > traitsvalue( (CFDictionaryRef) CTFontDescriptorCopyAttribute( m_ctFontDescriptor, kCTFontTraitsAttribute ) );
968 CTFontSymbolicTraits traits;
969 if ( CFNumberGetValue((CFNumberRef) CFDictionaryGetValue(traitsvalue,kCTFontSymbolicTrait),kCFNumberIntType,&traits) )
970 {
971 if ( traits & kCTFontItalicTrait )
972 m_style = wxFONTSTYLE_ITALIC;
973 if ( traits & kCTFontBoldTrait )
974 m_weight = wxFONTWEIGHT_BOLD ;
975 }
976
977 wxCFStringRef familyName( (CFStringRef) CTFontDescriptorCopyAttribute(m_ctFontDescriptor, kCTFontFamilyNameAttribute));
978 m_faceName = familyName.AsString();
979 }
980 #endif
981
982 void wxNativeFontInfo::EnsureValid()
983 {
984 if ( m_descriptorValid )
985 return;
986
987 #if wxOSX_USE_CORE_TEXT
988 if ( m_ctFontDescriptor == NULL && UMAGetSystemVersion() >= 0x1050 )
989 {
990 CTFontSymbolicTraits traits = 0;
991
992 if (m_weight == wxFONTWEIGHT_BOLD)
993 traits |= kCTFontBoldTrait;
994 if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT)
995 traits |= kCTFontItalicTrait;
996
997 // use font caching
998 wxString lookupnameWithSize = wxString::Format( "%s_%ld_%ld", m_faceName.c_str(), traits, m_pointSize );
999
1000 static std::map< std::wstring , wxCFRef< CTFontDescriptorRef > > fontdescriptorcache ;
1001 m_ctFontDescriptor = wxCFRetain((CTFontDescriptorRef)fontdescriptorcache[ std::wstring(lookupnameWithSize.wc_str()) ]);
1002 if ( !m_ctFontDescriptor )
1003 {
1004 // QD selection algorithm is the fastest by orders of magnitude on 10.5
1005 if ( m_faceName.IsAscii() )
1006 {
1007 uint8_t qdstyle = 0;
1008 if (m_weight == wxFONTWEIGHT_BOLD)
1009 qdstyle |= bold;
1010 if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT)
1011 qdstyle |= italic;
1012
1013 Str255 qdFontName ;
1014 wxMacStringToPascal( m_faceName , qdFontName );
1015 wxCFRef< CTFontRef > font;
1016 font.reset( CTFontCreateWithQuickdrawInstance(qdFontName, 0 , qdstyle, m_pointSize) );
1017 m_ctFontDescriptor = CTFontCopyFontDescriptor(font);
1018 }
1019 else
1020 {
1021 m_ctFontDescriptor = wxMacCreateCTFontDescriptor( wxCFStringRef(m_faceName),traits );
1022 }
1023 fontdescriptorcache[ std::wstring(lookupnameWithSize.wc_str()) ].reset(wxCFRetain(m_ctFontDescriptor));
1024 }
1025 }
1026 #endif
1027 #if wxOSX_USE_ATSU_TEXT
1028 if ( !m_atsuFontValid )
1029 {
1030 #if !wxOSX_USE_CARBON
1031 // not needed outside
1032 wxInt16 m_qdFontFamily;
1033 wxInt16 m_qdFontStyle;
1034 #endif
1035 wxCFStringRef cf( m_faceName, wxLocale::GetSystemEncoding() );
1036 ATSFontFamilyRef atsfamily = ATSFontFamilyFindFromName( cf , kATSOptionFlagsDefault );
1037 if ( atsfamily == (ATSFontFamilyRef) -1 )
1038 {
1039 wxLogDebug( wxT("ATSFontFamilyFindFromName failed for ") + m_faceName );
1040 m_qdFontFamily = GetAppFont();
1041 }
1042 else
1043 {
1044 m_qdFontFamily = FMGetFontFamilyFromATSFontFamilyRef( atsfamily );
1045 }
1046
1047 m_qdFontStyle = 0;
1048 if (m_weight == wxFONTWEIGHT_BOLD)
1049 m_qdFontStyle |= bold;
1050 if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT)
1051 m_qdFontStyle |= italic;
1052 if (m_underlined)
1053 m_qdFontStyle |= underline;
1054
1055
1056 // we try to get as much styles as possible into ATSU
1057
1058 // ATSUFontID and FMFont are equivalent
1059 FMFontStyle intrinsicStyle = 0 ;
1060 OSStatus status = FMGetFontFromFontFamilyInstance( m_qdFontFamily , m_qdFontStyle , (FMFont*)&m_atsuFontID , &intrinsicStyle);
1061 if ( status != noErr )
1062 {
1063 wxFAIL_MSG( wxT("couldn't get an ATSUFont from font family") );
1064 }
1065 m_atsuAdditionalQDStyles = m_qdFontStyle & (~intrinsicStyle );
1066 m_atsuFontValid = true;
1067 }
1068 #endif
1069 #if wxOSX_USE_COCOA
1070 if ( m_nsFontDescriptor == NULL )
1071 OSXValidateNSFontDescriptor();
1072 #endif
1073 #if wxOSX_USE_IPHONE
1074 // TODO
1075 #endif
1076 m_descriptorValid = true;
1077 }
1078
1079 void wxNativeFontInfo::Init(const wxNativeFontInfo& info)
1080 {
1081 Init();
1082 #if wxOSX_USE_CORE_TEXT
1083 m_ctFontDescriptor = wxCFRetain(info.m_ctFontDescriptor);
1084 #endif
1085 #if wxOSX_USE_ATSU_TEXT
1086 m_atsuFontValid = info.m_atsuFontValid;
1087 m_atsuFontID = info.m_atsuFontID ;
1088 m_atsuAdditionalQDStyles = info.m_atsuAdditionalQDStyles;
1089 #if wxOSX_USE_CARBON
1090 m_qdFontFamily = info.m_qdFontFamily;
1091 m_qdFontStyle = info.m_qdFontStyle;
1092 #endif
1093 #endif
1094 #if wxOSX_USE_COCOA
1095 m_nsFontDescriptor = (NSFontDescriptor*) wxMacCocoaRetain(info.m_nsFontDescriptor);
1096 #endif
1097 m_pointSize = info.m_pointSize;
1098 m_family = info.m_family;
1099 m_style = info.m_style;
1100 m_weight = info.m_weight;
1101 m_underlined = info.m_underlined;
1102 m_faceName = info.m_faceName;
1103 m_encoding = info.m_encoding;
1104 m_descriptorValid = info.m_descriptorValid;
1105 }
1106
1107 void wxNativeFontInfo::Init(int size,
1108 wxFontFamily family,
1109 wxFontStyle style,
1110 wxFontWeight weight,
1111 bool underlined,
1112 const wxString& faceName,
1113 wxFontEncoding encoding)
1114 {
1115 Init();
1116 m_pointSize = size;
1117 m_family = family;
1118 m_style = style;
1119 m_weight = weight;
1120 m_underlined = underlined;
1121 m_faceName = faceName;
1122 if ( encoding == wxFONTENCODING_DEFAULT )
1123 encoding = wxFont::GetDefaultEncoding();
1124 m_encoding = encoding;
1125
1126 }
1127
1128 void wxNativeFontInfo::Free()
1129 {
1130 #if wxOSX_USE_CORE_TEXT
1131 wxCFRelease(m_ctFontDescriptor);
1132 m_ctFontDescriptor = NULL;
1133 #endif
1134 #if wxOSX_USE_ATSU_TEXT
1135 m_atsuFontID = 0 ;
1136 m_atsuAdditionalQDStyles = 0;
1137 m_atsuFontValid = false;
1138 #endif
1139 #if wxOSX_USE_COCOA
1140 wxMacCocoaRelease(m_nsFontDescriptor);
1141 m_nsFontDescriptor = NULL;
1142 #endif
1143 m_descriptorValid = false;
1144 }
1145
1146 bool wxNativeFontInfo::FromString(const wxString& s)
1147 {
1148 long l;
1149
1150 wxStringTokenizer tokenizer(s, _T(";"));
1151
1152 wxString token = tokenizer.GetNextToken();
1153 //
1154 // Ignore the version for now
1155 //
1156
1157 token = tokenizer.GetNextToken();
1158 if ( !token.ToLong(&l) )
1159 return false;
1160 m_pointSize = (int)l;
1161
1162 token = tokenizer.GetNextToken();
1163 if ( !token.ToLong(&l) )
1164 return false;
1165 m_family = (wxFontFamily)l;
1166
1167 token = tokenizer.GetNextToken();
1168 if ( !token.ToLong(&l) )
1169 return false;
1170 m_style = (wxFontStyle)l;
1171
1172 token = tokenizer.GetNextToken();
1173 if ( !token.ToLong(&l) )
1174 return false;
1175 m_weight = (wxFontWeight)l;
1176
1177 token = tokenizer.GetNextToken();
1178 if ( !token.ToLong(&l) )
1179 return false;
1180 m_underlined = l != 0;
1181
1182 m_faceName = tokenizer.GetNextToken();
1183
1184 #ifndef __WXMAC__
1185 if( !faceName )
1186 return false;
1187 #endif
1188
1189 token = tokenizer.GetNextToken();
1190 if ( !token.ToLong(&l) )
1191 return false;
1192 m_encoding = (wxFontEncoding)l;
1193
1194 return true;
1195 }
1196
1197 wxString wxNativeFontInfo::ToString() const
1198 {
1199 wxString s;
1200
1201 s.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
1202 0, // version
1203 m_pointSize,
1204 m_family,
1205 (int)m_style,
1206 (int)m_weight,
1207 m_underlined,
1208 m_faceName.GetData(),
1209 (int)m_encoding);
1210
1211 return s;
1212 }
1213
1214 int wxNativeFontInfo::GetPointSize() const
1215 {
1216 return m_pointSize;
1217 }
1218
1219 wxFontStyle wxNativeFontInfo::GetStyle() const
1220 {
1221 return m_style;
1222 }
1223
1224 wxFontWeight wxNativeFontInfo::GetWeight() const
1225 {
1226 return m_weight;
1227 }
1228
1229 bool wxNativeFontInfo::GetUnderlined() const
1230 {
1231 return m_underlined;
1232 }
1233
1234 wxString wxNativeFontInfo::GetFaceName() const
1235 {
1236 return m_faceName;
1237 }
1238
1239 wxFontFamily wxNativeFontInfo::GetFamily() const
1240 {
1241 return m_family;
1242 }
1243
1244 wxFontEncoding wxNativeFontInfo::GetEncoding() const
1245 {
1246 return m_encoding;
1247 }
1248
1249 // changing the font descriptor
1250
1251 void wxNativeFontInfo::SetPointSize(int pointsize)
1252 {
1253 if ( m_pointSize != pointsize )
1254 {
1255 m_pointSize = pointsize;
1256 Free();
1257 }
1258 }
1259
1260 void wxNativeFontInfo::SetStyle(wxFontStyle style_)
1261 {
1262 if ( m_style != style_ )
1263 {
1264 m_style = style_;
1265 Free();
1266 }
1267 }
1268
1269 void wxNativeFontInfo::SetWeight(wxFontWeight weight_)
1270 {
1271 if ( m_weight != weight_ )
1272 {
1273 m_weight = weight_;
1274 Free();
1275 }
1276 }
1277
1278 void wxNativeFontInfo::SetUnderlined(bool underlined_)
1279 {
1280 if ( m_underlined != underlined_ )
1281 {
1282 m_underlined = underlined_;
1283 Free();
1284 }
1285 }
1286
1287 bool wxNativeFontInfo::SetFaceName(const wxString& facename_)
1288 {
1289 if ( m_faceName != facename_ )
1290 {
1291 m_faceName = facename_;
1292 Free();
1293 }
1294 return true;
1295 }
1296
1297 void wxNativeFontInfo::SetFamily(wxFontFamily family_)
1298 {
1299 if ( m_family != family_ )
1300 {
1301 m_family = family_;
1302 Free();
1303 }
1304 }
1305
1306 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_)
1307 {
1308 if ( encoding_ == wxFONTENCODING_DEFAULT )
1309 encoding_ = wxFont::GetDefaultEncoding();
1310 m_encoding = encoding_;
1311 // not reflected in native descriptors
1312 }