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