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