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