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