]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/font.cpp
popup activation scope and l&f
[wxWidgets.git] / src / mac / carbon / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/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
27 #include "wx/mac/uma.h"
28
29 #ifndef __DARWIN__
30 #include <ATSUnicode.h>
31 #endif
32
33 #include <map>
34 #include <string>
35
36 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
37
38
39 class WXDLLEXPORT wxFontRefData: public wxGDIRefData
40 {
41 public:
42 wxFontRefData()
43 {
44 Init(10, wxDEFAULT, wxNORMAL, wxNORMAL,
45 false, wxT("applicationfont"), wxFONTENCODING_DEFAULT);
46 }
47
48 wxFontRefData(const wxFontRefData& data)
49 {
50 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
51 data.m_underlined, data.m_faceName, data.m_encoding);
52 }
53
54 wxFontRefData(int size,
55 int family,
56 int style,
57 int weight,
58 bool underlined,
59 const wxString& faceName,
60 wxFontEncoding encoding)
61 {
62 Init(size, family, style, weight, underlined, faceName, encoding);
63 }
64
65 #if wxMAC_USE_CORE_TEXT
66 wxFontRefData( wxUint32 coreTextFontType );
67 wxFontRefData( CTFontRef font );
68 wxFontRefData( CTFontDescriptorRef fontdescriptor, int size );
69 #endif
70
71 virtual ~wxFontRefData();
72
73 void SetNoAntiAliasing( bool no = true ) { m_noAA = no; }
74
75 bool GetNoAntiAliasing() const { return m_noAA; }
76
77 void SetPointSize( int size )
78 {
79 m_pointSize = size;
80 MacInvalidateNativeFont();
81 }
82
83 int GetPointSize() const { return m_pointSize; }
84
85 void SetFamily( int family )
86 {
87 m_family = family;
88 MacInvalidateNativeFont();
89 }
90
91
92 int GetFamily() const { return m_family; }
93
94 void SetStyle( int style )
95 {
96 m_style = style;
97 MacInvalidateNativeFont();
98 }
99
100
101 int GetStyle() const { return m_style; }
102
103 void SetWeight( int weight )
104 {
105 m_weight = weight;
106 MacInvalidateNativeFont();
107 }
108
109
110 int GetWeight() const { return m_weight; }
111
112 void SetUnderlined( bool u )
113 {
114 m_underlined = u;
115 MacInvalidateNativeFont();
116 }
117
118 bool GetUnderlined() const { return m_underlined; }
119
120 void SetFaceName( const wxString& facename )
121 {
122 m_faceName = facename;
123 MacInvalidateNativeFont();
124 }
125
126 const wxString& GetFaceName() const { return m_faceName; }
127
128 void SetEncoding( wxFontEncoding encoding )
129 {
130 m_encoding = encoding;
131 MacInvalidateNativeFont();
132 }
133
134 wxFontEncoding GetEncoding() const { return m_encoding; }
135
136 void MacInvalidateNativeFont();
137
138 void MacFindFont();
139
140 protected:
141 // common part of all ctors
142 void Init(int size,
143 int family,
144 int style,
145 int weight,
146 bool underlined,
147 const wxString& faceName,
148 wxFontEncoding encoding);
149
150 #if wxMAC_USE_CORE_TEXT
151 void Init( CTFontRef font );
152 #endif
153 // font characterstics
154 int m_pointSize;
155 int m_family;
156 int m_style;
157 int m_weight;
158 bool m_underlined;
159 wxString m_faceName;
160 wxFontEncoding m_encoding;
161 bool m_noAA; // No anti-aliasing
162
163 public:
164 #if wxMAC_USE_ATSU_TEXT
165 FMFontFamily m_macFontFamily;
166 FMFontSize m_macFontSize;
167 FMFontStyle m_macFontStyle;
168
169 // ATSU Font Information
170
171 // this is split into an ATSU font id that may
172 // contain some styles (special bold fonts etc) and
173 // these are the additional qd styles that are not
174 // included in the ATSU font id
175 ATSUFontID m_macATSUFontID;
176 FMFontStyle m_macATSUAdditionalQDStyles ;
177
178 // for true themeing support we must store the correct font
179 // information here, as this speeds up and optimizes rendering
180 ThemeFontID m_macThemeFontID ;
181 #endif
182 #if wxMAC_USE_CORE_TEXT
183 wxCFRef<CTFontRef> m_ctFont;
184 wxCFRef<CTFontDescriptorRef> m_ctFontDescriptor;
185 #endif
186 #if wxMAC_USE_CORE_TEXT || wxMAC_USE_ATSU_TEXT
187 ATSUStyle m_macATSUStyle ;
188 #endif
189 wxNativeFontInfo m_info;
190 };
191
192 #define M_FONTDATA ((wxFontRefData*)m_refData)
193
194
195 // ============================================================================
196 // implementation
197 // ============================================================================
198
199 // ----------------------------------------------------------------------------
200 // wxFontRefData
201 // ----------------------------------------------------------------------------
202
203 void wxFontRefData::Init(int pointSize,
204 int family,
205 int style,
206 int weight,
207 bool underlined,
208 const wxString& faceName,
209 wxFontEncoding encoding)
210 {
211 m_style = style;
212 m_pointSize = pointSize;
213 m_family = family;
214 m_style = style;
215 m_weight = weight;
216 m_underlined = underlined;
217 m_faceName = faceName;
218 m_encoding = encoding;
219 m_noAA = false;
220 #if wxMAC_USE_ATSU_TEXT
221 m_macFontFamily = 0 ;
222 m_macFontSize = 0;
223 m_macFontStyle = 0;
224 m_macATSUFontID = 0;
225 m_macATSUAdditionalQDStyles = 0 ;
226 m_macThemeFontID = kThemeCurrentPortFont ;
227 #endif
228 #if wxMAC_USE_CORE_TEXT || wxMAC_USE_ATSU_TEXT
229 m_macATSUStyle = NULL ;
230 #endif
231 }
232
233 wxFontRefData::~wxFontRefData()
234 {
235 #if wxMAC_USE_CORE_TEXT || wxMAC_USE_ATSU_TEXT
236 if ( m_macATSUStyle )
237 {
238 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
239 m_macATSUStyle = NULL ;
240 }
241 #endif
242 }
243
244 void wxFontRefData::MacInvalidateNativeFont()
245 {
246 #if wxMAC_USE_CORE_TEXT
247 m_ctFont.reset();
248 m_ctFontDescriptor.reset();
249 #endif
250 #if wxMAC_USE_CORE_TEXT || wxMAC_USE_ATSU_TEXT
251 if ( m_macATSUStyle )
252 {
253 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
254 m_macATSUStyle = NULL ;
255 }
256 #endif
257 }
258
259 #if wxMAC_USE_CORE_TEXT
260
261 /* from Core Text Manual Common Operations */
262
263 static CTFontDescriptorRef wxMacCreateCTFontDescriptor(CFStringRef iFamilyName, CTFontSymbolicTraits iTraits )
264 {
265 CTFontDescriptorRef descriptor = NULL;
266 CFMutableDictionaryRef attributes;
267
268 assert(iFamilyName != NULL);
269 // Create a mutable dictionary to hold our attributes.
270 attributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
271 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
272 check(attributes != NULL);
273
274 if (attributes != NULL) {
275 // Add a family name to our attributes.
276 CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, iFamilyName);
277
278
279 if ( iTraits ) {
280 CFMutableDictionaryRef traits;
281 CFNumberRef symTraits;
282
283 // Create the traits dictionary.
284 symTraits = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type,
285 &iTraits);
286 check(symTraits != NULL);
287
288 if (symTraits != NULL) {
289 // Create a dictionary to hold our traits values.
290 traits = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
291 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
292 check(traits != NULL);
293
294 if (traits != NULL) {
295 // Add the symbolic traits value to the traits dictionary.
296 CFDictionaryAddValue(traits, kCTFontSymbolicTrait, symTraits);
297
298 // Add the traits attribute to our attributes.
299 CFDictionaryAddValue(attributes, kCTFontTraitsAttribute, traits);
300 CFRelease(traits);
301 }
302 CFRelease(symTraits);
303 }
304 }
305 // Create the font descriptor with our attributes
306 descriptor = CTFontDescriptorCreateWithAttributes(attributes);
307 check(descriptor != NULL);
308
309 CFRelease(attributes);
310 }
311 // Return our font descriptor.
312 return descriptor ;
313 }
314
315 wxFontRefData::wxFontRefData( wxUint32 coreTextFontType )
316 {
317 CTFontRef font = CTFontCreateUIFontForLanguage( coreTextFontType, 0.0, NULL ) ;
318 if ( CTFontGetSize(m_ctFont) == 0 )
319 {
320 CFRelease(font);
321 font = CTFontCreateUIFontForLanguage( coreTextFontType, 12.0, NULL );
322 }
323 Init( font );
324 }
325
326 wxFontRefData::wxFontRefData( CTFontRef font )
327 {
328 Init( font );
329 }
330
331 wxFontRefData::wxFontRefData( CTFontDescriptorRef fontdescriptor, int size )
332 {
333 if ( size == 0 )
334 {
335 wxCFRef< CFNumberRef > value( (CFNumberRef) CTFontDescriptorCopyAttribute( fontdescriptor, kCTFontSizeAttribute ) );
336
337 float fsize;
338 if ( CFNumberGetValue( value , kCFNumberFloatType , &fsize ) )
339 {
340 size = (int) fsize + 0.5 ;
341 }
342 }
343 Init( CTFontCreateWithFontDescriptor(fontdescriptor, size,NULL) );
344 }
345
346 void wxFontRefData::Init( CTFontRef font )
347 {
348 Init(10, wxDEFAULT, wxNORMAL, wxNORMAL,
349 false, wxT("applicationfont"), wxFONTENCODING_DEFAULT);
350
351 m_ctFont.reset( font );
352 }
353
354 #endif
355
356 void wxFontRefData::MacFindFont()
357 {
358
359 #if wxMAC_USE_CORE_TEXT
360 if ( UMAGetSystemVersion() >= 0x1050 )
361 {
362 if ( m_faceName.empty() && m_family == wxDEFAULT )
363 {
364 m_ctFont.reset(CTFontCreateUIFontForLanguage( kCTFontSystemFontType, 0.0, NULL ));
365 }
366
367 if ( m_ctFont )
368 {
369 wxCFStringRef name( CTFontCopyFamilyName( m_ctFont ) );
370 m_faceName = name.AsString();
371 m_pointSize = CTFontGetSize(m_ctFont) ;
372 CTFontSymbolicTraits traits = CTFontGetSymbolicTraits( m_ctFont );
373 if ( traits & kCTFontItalicTrait )
374 m_style = wxITALIC;
375 if ( traits & kCTFontBoldTrait )
376 m_weight = wxBOLD ;
377 if ( !m_ctFontDescriptor.get() )
378 m_ctFontDescriptor.reset( CTFontCopyFontDescriptor( m_ctFont ) );
379 }
380 else
381 {
382 if ( m_faceName.empty() )
383 {
384 switch ( m_family )
385 {
386 case wxSCRIPT :
387 case wxROMAN :
388 case wxDECORATIVE :
389 m_faceName = wxT("Times");
390 break ;
391
392 case wxSWISS :
393 m_faceName = wxT("Lucida Grande");
394 break ;
395
396 case wxMODERN :
397 case wxTELETYPE:
398 m_faceName = wxT("Monaco");
399 break ;
400
401 default:
402 m_faceName = wxT("Times");
403 break ;
404 }
405 }
406
407
408 CTFontSymbolicTraits traits = 0;
409
410 if (m_weight == wxBOLD)
411 traits |= kCTFontBoldTrait;
412 if (m_style == wxITALIC || m_style == wxSLANT)
413 traits |= kCTFontItalicTrait;
414
415 // use font descriptor caching
416 #if 1
417 wxString lookupname = wxString::Format( "%s_%ld", m_faceName.c_str(), traits );
418
419 static std::map< std::wstring , wxCFRef< CTFontDescriptorRef > > fontdescriptorcache ;
420
421 m_ctFontDescriptor = fontdescriptorcache[ std::wstring(lookupname.wc_str()) ];
422 if ( !m_ctFontDescriptor )
423 {
424 wxCFStringRef cf( m_faceName, wxLocale::GetSystemEncoding() );
425 m_ctFontDescriptor.reset( wxMacCreateCTFontDescriptor( cf, traits ) );
426 fontdescriptorcache[ std::wstring(lookupname.wc_str()) ] = m_ctFontDescriptor;
427 }
428 #else
429 wxCFStringRef cf( m_faceName, wxLocale::GetSystemEncoding() );
430 m_ctFontDescriptor.reset( wxMacCreateCTFontDescriptor( cf, traits ) );
431 #endif
432
433 // use font caching
434 #if 1
435 wxString lookupnameWithSize = wxString::Format( "%s_%ld_%ld", m_faceName.c_str(), traits, m_pointSize );
436
437 static std::map< std::wstring , wxCFRef< CTFontRef > > fontcache ;
438 m_ctFont = fontcache[ std::wstring(lookupnameWithSize.wc_str()) ];
439 if ( !m_ctFont )
440 {
441 m_ctFont.reset( CTFontCreateWithFontDescriptor( m_ctFontDescriptor, m_pointSize, NULL ) );
442 fontcache[ std::wstring(lookupnameWithSize.wc_str()) ] = m_ctFont;
443 }
444 #else
445 m_ctFont.reset( CTFontCreateWithFontDescriptor( m_ctFontDescriptor, m_pointSize, NULL ) );
446 #endif
447 }
448 #if wxMAC_USE_ATSU_TEXT == 0
449 OSStatus status = noErr;
450 CTFontDescriptorRef desc = m_ctFontDescriptor ;
451 ATSFontRef atsfont = CTFontGetPlatformFont( m_ctFont, &desc );
452 FMFont fmfont = FMGetFontFromATSFontRef( atsfont );
453 ATSUAttributeTag atsuTags[] =
454 {
455 kATSUFontTag ,
456 kATSUSizeTag ,
457 kATSUVerticalCharacterTag,
458 kATSUQDBoldfaceTag ,
459 kATSUQDItalicTag ,
460 kATSUQDUnderlineTag ,
461 };
462 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
463 {
464 sizeof( ATSUFontID ) ,
465 sizeof( Fixed ) ,
466 sizeof( ATSUVerticalCharacterType),
467 sizeof( Boolean ) ,
468 sizeof( Boolean ) ,
469 sizeof( Boolean ) ,
470 };
471 Boolean kTrue = true ;
472 Boolean kFalse = false ;
473
474 Fixed atsuSize = IntToFixed( m_pointSize );
475 ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
476 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
477 {
478 &fmfont ,
479 &atsuSize ,
480 &kHorizontal,
481 (m_weight == wxBOLD) ? &kTrue : &kFalse ,
482 (m_style == wxITALIC || m_style == wxSLANT) ? &kTrue : &kFalse ,
483 (m_underlined) ? &kTrue : &kFalse ,
484 };
485
486 if ( m_macATSUStyle )
487 {
488 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
489 m_macATSUStyle = NULL ;
490 }
491 status = ::ATSUCreateStyle((ATSUStyle *)&m_macATSUStyle);
492 wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") );
493 status = ::ATSUSetAttributes(
494 (ATSUStyle)m_macATSUStyle,
495 sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,
496 atsuTags, atsuSizes, atsuValues);
497 #endif
498 }
499 #endif
500 #if wxMAC_USE_ATSU_TEXT
501 {
502 OSStatus status = noErr;
503 Str255 qdFontName ;
504 if ( m_macThemeFontID != kThemeCurrentPortFont )
505 {
506 Style style ;
507 GetThemeFont( m_macThemeFontID, GetApplicationScript(), qdFontName, &m_macFontSize, &style );
508 if ( m_macFontSize == 0 )
509 m_macFontSize = 12;
510 m_macFontStyle = style ;
511 m_faceName = wxMacMakeStringFromPascal( qdFontName );
512 if ( m_macFontStyle & bold )
513 m_weight = wxBOLD ;
514 else
515 m_weight = wxNORMAL ;
516 if ( m_macFontStyle & italic )
517 m_style = wxITALIC ;
518 if ( m_macFontStyle & underline )
519 m_underlined = true ;
520 m_pointSize = m_macFontSize ;
521 m_macFontFamily = FMGetFontFamilyFromName( qdFontName );
522 }
523 else
524 {
525 if ( m_faceName.empty() )
526 {
527 if ( m_family == wxDEFAULT )
528 {
529 m_macFontFamily = GetAppFont();
530 FMGetFontFamilyName(m_macFontFamily,qdFontName);
531 m_faceName = wxMacMakeStringFromPascal( qdFontName );
532 }
533 else
534 {
535 switch ( m_family )
536 {
537 case wxSCRIPT :
538 case wxROMAN :
539 case wxDECORATIVE :
540 m_faceName = wxT("Times");
541 break ;
542
543 case wxSWISS :
544 m_faceName = wxT("Lucida Grande");
545 break ;
546
547 case wxMODERN :
548 case wxTELETYPE:
549 m_faceName = wxT("Monaco");
550 break ;
551
552 default:
553 m_faceName = wxT("Times");
554 break ;
555 }
556 wxMacStringToPascal( m_faceName , qdFontName );
557 m_macFontFamily = FMGetFontFamilyFromName( qdFontName );
558 if ( m_macFontFamily == kInvalidFontFamily )
559 {
560 wxLogDebug( wxT("ATSFontFamilyFindFromName failed for %s"), m_faceName.c_str() );
561 m_macFontFamily = GetAppFont();
562 }
563 }
564 }
565 else
566 {
567 if ( m_faceName == wxT("systemfont") )
568 m_macFontFamily = GetSysFont();
569 else if ( m_faceName == wxT("applicationfont") )
570 m_macFontFamily = GetAppFont();
571 else
572 {
573 wxCFStringRef cf( m_faceName, wxLocale::GetSystemEncoding() );
574 ATSFontFamilyRef atsfamily = ATSFontFamilyFindFromName( cf , kATSOptionFlagsDefault );
575 if ( atsfamily == (ATSFontFamilyRef) -1 )
576 {
577 wxLogDebug( wxT("ATSFontFamilyFindFromName failed for ") + m_faceName );
578 m_macFontFamily = GetAppFont();
579 }
580 else
581 m_macFontFamily = FMGetFontFamilyFromATSFontFamilyRef( atsfamily );
582 }
583 }
584
585 m_macFontStyle = 0;
586 if (m_weight == wxBOLD)
587 m_macFontStyle |= bold;
588 if (m_style == wxITALIC || m_style == wxSLANT)
589 m_macFontStyle |= italic;
590 if (m_underlined)
591 m_macFontStyle |= underline;
592 m_macFontSize = m_pointSize ;
593 }
594
595 // we try to get as much styles as possible into ATSU
596
597
598 // ATSUFontID and FMFont are equivalent
599 FMFontStyle intrinsicStyle = 0 ;
600 status = FMGetFontFromFontFamilyInstance( m_macFontFamily , m_macFontStyle , &m_macATSUFontID , &intrinsicStyle);
601 wxASSERT_MSG( status == noErr , wxT("couldn't get an ATSUFont from font family") );
602 m_macATSUAdditionalQDStyles = m_macFontStyle & (~intrinsicStyle );
603
604 if ( m_macATSUStyle )
605 {
606 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
607 m_macATSUStyle = NULL ;
608 }
609
610 status = ::ATSUCreateStyle((ATSUStyle *)&m_macATSUStyle);
611 wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") );
612
613 ATSUAttributeTag atsuTags[] =
614 {
615 kATSUFontTag ,
616 kATSUSizeTag ,
617 kATSUVerticalCharacterTag,
618 kATSUQDBoldfaceTag ,
619 kATSUQDItalicTag ,
620 kATSUQDUnderlineTag ,
621 kATSUQDCondensedTag ,
622 kATSUQDExtendedTag ,
623 };
624 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
625 {
626 sizeof( ATSUFontID ) ,
627 sizeof( Fixed ) ,
628 sizeof( ATSUVerticalCharacterType),
629 sizeof( Boolean ) ,
630 sizeof( Boolean ) ,
631 sizeof( Boolean ) ,
632 sizeof( Boolean ) ,
633 sizeof( Boolean ) ,
634 };
635
636 Boolean kTrue = true ;
637 Boolean kFalse = false ;
638
639 Fixed atsuSize = IntToFixed( m_macFontSize );
640 ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
641 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
642 {
643 &m_macATSUFontID ,
644 &atsuSize ,
645 &kHorizontal,
646 (m_macATSUAdditionalQDStyles & bold) ? &kTrue : &kFalse ,
647 (m_macATSUAdditionalQDStyles & italic) ? &kTrue : &kFalse ,
648 (m_macATSUAdditionalQDStyles & underline) ? &kTrue : &kFalse ,
649 (m_macATSUAdditionalQDStyles & condense) ? &kTrue : &kFalse ,
650 (m_macATSUAdditionalQDStyles & extend) ? &kTrue : &kFalse ,
651 };
652
653 status = ::ATSUSetAttributes(
654 (ATSUStyle)m_macATSUStyle,
655 sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,
656 atsuTags, atsuSizes, atsuValues);
657
658 wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );
659 return;
660 }
661 #endif
662 }
663
664 // ----------------------------------------------------------------------------
665 // wxFont
666 // ----------------------------------------------------------------------------
667
668 bool wxFont::Create(const wxNativeFontInfo& info)
669 {
670 return Create(
671 info.pointSize, info.family, info.style, info.weight,
672 info.underlined, info.faceName, info.encoding );
673 }
674
675 wxFont::wxFont(const wxString& fontdesc)
676 {
677 wxNativeFontInfo info;
678 if ( info.FromString(fontdesc) )
679 (void)Create(info);
680 }
681
682 bool wxFont::Create(int pointSize,
683 int family,
684 int style,
685 int weight,
686 bool underlined,
687 const wxString& faceName,
688 wxFontEncoding encoding)
689 {
690 UnRef();
691
692 m_refData = new wxFontRefData(
693 pointSize, family, style, weight,
694 underlined, faceName, encoding);
695
696 RealizeResource();
697
698 return true;
699 }
700
701 #if wxMAC_USE_CORE_TEXT
702
703 bool wxFont::MacCreateFromUIFont(wxUint32 ctFontType )
704 {
705 UnRef();
706
707 m_refData = new wxFontRefData(ctFontType);
708 RealizeResource();
709
710 return true;
711 }
712
713 bool wxFont::MacCreateFromCTFontDescriptor( const void * ctFontDescriptor , int size )
714 {
715 UnRef();
716
717 m_refData = new wxFontRefData((CTFontDescriptorRef)ctFontDescriptor, size);;
718 RealizeResource();
719
720 return true;
721 }
722
723
724 #endif
725
726 bool wxFont::MacCreateFromThemeFont(wxUint16 themeFontID)
727 {
728 #if wxMAC_USE_CORE_TEXT
729 if ( UMAGetSystemVersion() >= 0x1050)
730 {
731 return MacCreateFromUIFont(HIThemeGetUIFontType(themeFontID));
732 }
733 #endif
734 #if wxMAC_USE_ATSU_TEXT
735 {
736 UnRef();
737
738 m_refData = new wxFontRefData(
739 12, wxDEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
740 false, wxEmptyString, wxFONTENCODING_DEFAULT );
741
742 M_FONTDATA->m_macThemeFontID = themeFontID ;
743 RealizeResource();
744 return true;
745 }
746 #endif
747 return false;
748 }
749
750 wxFont::~wxFont()
751 {
752 }
753
754 bool wxFont::RealizeResource()
755 {
756 M_FONTDATA->MacFindFont();
757
758 return true;
759 }
760
761 void wxFont::SetEncoding(wxFontEncoding encoding)
762 {
763 Unshare();
764
765 M_FONTDATA->SetEncoding( encoding );
766
767 RealizeResource();
768 }
769
770 void wxFont::Unshare()
771 {
772 // Don't change shared data
773 if (!m_refData)
774 {
775 m_refData = new wxFontRefData();
776 }
777 else
778 {
779 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
780 UnRef();
781 m_refData = ref;
782 }
783 }
784
785 wxGDIRefData *wxFont::CreateGDIRefData() const
786 {
787 return new wxFontRefData;
788 }
789
790 wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const
791 {
792 return new wxFontRefData(*wx_static_cast(const wxFontRefData *, data));
793 }
794
795 void wxFont::SetPointSize(int pointSize)
796 {
797 if ( M_FONTDATA->GetPointSize() == pointSize )
798 return;
799
800 Unshare();
801
802 M_FONTDATA->SetPointSize( pointSize );
803
804 RealizeResource();
805 }
806
807 void wxFont::SetFamily(int family)
808 {
809 Unshare();
810
811 M_FONTDATA->SetFamily( family );
812
813 RealizeResource();
814 }
815
816 void wxFont::SetStyle(int style)
817 {
818 Unshare();
819
820 M_FONTDATA->SetStyle( style );
821
822 RealizeResource();
823 }
824
825 void wxFont::SetWeight(int weight)
826 {
827 Unshare();
828
829 M_FONTDATA->SetWeight( weight );
830
831 RealizeResource();
832 }
833
834 bool wxFont::SetFaceName(const wxString& faceName)
835 {
836 Unshare();
837
838 M_FONTDATA->SetFaceName( faceName );
839
840 RealizeResource();
841
842 return wxFontBase::SetFaceName(faceName);
843 }
844
845 void wxFont::SetUnderlined(bool underlined)
846 {
847 Unshare();
848
849 M_FONTDATA->SetUnderlined( underlined );
850
851 RealizeResource();
852 }
853
854 void wxFont::SetNoAntiAliasing( bool no )
855 {
856 Unshare();
857
858 M_FONTDATA->SetNoAntiAliasing( no );
859
860 RealizeResource();
861 }
862
863 // ----------------------------------------------------------------------------
864 // accessors
865 // ----------------------------------------------------------------------------
866
867 // TODO: insert checks everywhere for M_FONTDATA == NULL!
868
869 int wxFont::GetPointSize() const
870 {
871 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
872
873 return M_FONTDATA->GetPointSize();
874 }
875
876 wxSize wxFont::GetPixelSize() const
877 {
878 #if wxUSE_GRAPHICS_CONTEXT
879 // TODO: consider caching the value
880 wxGraphicsContext* dc = wxGraphicsContext::CreateFromNative((CGContextRef) NULL);
881 dc->SetFont(*(wxFont *)this,*wxBLACK);
882 wxDouble width, height = 0;
883 dc->GetTextExtent( wxT("g"), &width, &height, NULL, NULL);
884 delete dc;
885 return wxSize((int)width, (int)height);
886 #else
887 return wxFontBase::GetPixelSize();
888 #endif
889 }
890
891 int wxFont::GetFamily() const
892 {
893 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
894
895 return M_FONTDATA->GetFamily();
896 }
897
898 int wxFont::GetStyle() const
899 {
900 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
901
902 return M_FONTDATA->GetStyle() ;
903 }
904
905 int wxFont::GetWeight() const
906 {
907 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
908
909 return M_FONTDATA->GetWeight();
910 }
911
912 bool wxFont::GetUnderlined() const
913 {
914 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
915
916 return M_FONTDATA->GetUnderlined();
917 }
918
919 wxString wxFont::GetFaceName() const
920 {
921 wxCHECK_MSG( M_FONTDATA != NULL , wxEmptyString , wxT("invalid font") );
922
923 return M_FONTDATA->GetFaceName() ;
924 }
925
926 wxFontEncoding wxFont::GetEncoding() const
927 {
928 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTENCODING_DEFAULT , wxT("invalid font") );
929
930 return M_FONTDATA->GetEncoding() ;
931 }
932
933 bool wxFont::GetNoAntiAliasing() const
934 {
935 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
936
937 return M_FONTDATA->GetNoAntiAliasing();
938 }
939
940 #if wxMAC_USE_ATSU_TEXT
941
942 short wxFont::MacGetFontNum() const
943 {
944 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
945
946 return M_FONTDATA->m_macFontFamily;
947 }
948
949 short wxFont::MacGetFontSize() const
950 {
951 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
952
953 return M_FONTDATA->m_macFontSize;
954 }
955
956 wxByte wxFont::MacGetFontStyle() const
957 {
958 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
959
960 return M_FONTDATA->m_macFontStyle;
961 }
962
963 wxUint32 wxFont::MacGetATSUFontID() const
964 {
965 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
966
967 return M_FONTDATA->m_macATSUFontID;
968 }
969
970 wxUint32 wxFont::MacGetATSUAdditionalQDStyles() const
971 {
972 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
973
974 return M_FONTDATA->m_macATSUAdditionalQDStyles;
975 }
976
977 wxUint16 wxFont::MacGetThemeFontID() const
978 {
979 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
980
981 return M_FONTDATA->m_macThemeFontID;
982 }
983 #endif
984
985 #if wxMAC_USE_CORE_TEXT || wxMAC_USE_ATSU_TEXT
986 void * wxFont::MacGetATSUStyle() const
987 {
988 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
989
990 return M_FONTDATA->m_macATSUStyle;
991 }
992 #endif
993
994 #if wxMAC_USE_CORE_TEXT
995
996 const void * wxFont::MacGetCTFont() const
997 {
998 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
999
1000 return (CTFontRef)(M_FONTDATA->m_ctFont);
1001 }
1002
1003 const void * wxFont::MacGetCTFontDescriptor() const
1004 {
1005 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
1006
1007 return (CTFontDescriptorRef)(M_FONTDATA->m_ctFontDescriptor);
1008 }
1009
1010 #endif
1011
1012 const wxNativeFontInfo * wxFont::GetNativeFontInfo() const
1013 {
1014 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
1015 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
1016
1017 M_FONTDATA->m_info.InitFromFont(*this);
1018
1019 return &(M_FONTDATA->m_info);
1020 }