]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/font.cpp
fixed pagebreaks computation in tables (#9935, patch by D.J.Stauffer)
[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 0 // debugging coretext font matching
455 if ( (CTFontGetSymbolicTraits( m_ctFont ) & 0x03) != traits )
456 {
457 wxMessageBox( wxString::Format( "expected %d but got %d traits" , traits, (CTFontGetSymbolicTraits( m_ctFont ) & 0x03) ) );
458 }
459 #endif
460 }
461 }
462 #if wxOSX_USE_ATSU_TEXT
463 OSStatus status = noErr;
464 CTFontDescriptorRef desc = m_ctFontDescriptor ;
465 ATSFontRef atsfont = CTFontGetPlatformFont( m_ctFont, &desc );
466 FMFont fmfont = FMGetFontFromATSFontRef( atsfont );
467 ATSUAttributeTag atsuTags[] =
468 {
469 kATSUFontTag ,
470 kATSUSizeTag ,
471 kATSUVerticalCharacterTag,
472 kATSUQDBoldfaceTag ,
473 kATSUQDItalicTag ,
474 kATSUQDUnderlineTag ,
475 };
476 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
477 {
478 sizeof( ATSUFontID ) ,
479 sizeof( Fixed ) ,
480 sizeof( ATSUVerticalCharacterType),
481 sizeof( Boolean ) ,
482 sizeof( Boolean ) ,
483 sizeof( Boolean ) ,
484 };
485 Boolean kTrue = true ;
486 Boolean kFalse = false ;
487
488 Fixed atsuSize = IntToFixed( m_pointSize );
489 ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
490 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
491 {
492 &fmfont ,
493 &atsuSize ,
494 &kHorizontal,
495 (m_weight == wxBOLD) ? &kTrue : &kFalse ,
496 (m_style == wxITALIC || m_style == wxSLANT) ? &kTrue : &kFalse ,
497 (m_underlined) ? &kTrue : &kFalse ,
498 };
499
500 if ( m_macATSUStyle )
501 {
502 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
503 m_macATSUStyle = NULL ;
504 }
505 status = ::ATSUCreateStyle((ATSUStyle *)&m_macATSUStyle);
506 wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") );
507 status = ::ATSUSetAttributes(
508 (ATSUStyle)m_macATSUStyle,
509 sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,
510 atsuTags, atsuSizes, atsuValues);
511 #endif
512 }
513 #endif
514 #if wxOSX_USE_ATSU_TEXT
515 {
516 OSStatus status = noErr;
517 Str255 qdFontName ;
518 if ( m_macThemeFontID != kThemeCurrentPortFont )
519 {
520 Style style ;
521 GetThemeFont( m_macThemeFontID, GetApplicationScript(), qdFontName, &m_macFontSize, &style );
522 if ( m_macFontSize == 0 )
523 m_macFontSize = 12;
524 m_macFontStyle = style ;
525 m_faceName = wxMacMakeStringFromPascal( qdFontName );
526 if ( m_macFontStyle & bold )
527 m_weight = wxBOLD ;
528 else
529 m_weight = wxNORMAL ;
530 if ( m_macFontStyle & italic )
531 m_style = wxITALIC ;
532 if ( m_macFontStyle & underline )
533 m_underlined = true ;
534 m_pointSize = m_macFontSize ;
535 m_macFontFamily = FMGetFontFamilyFromName( qdFontName );
536 }
537 else
538 {
539 if ( m_faceName.empty() )
540 {
541 if ( m_family == wxDEFAULT )
542 {
543 m_macFontFamily = GetAppFont();
544 FMGetFontFamilyName(m_macFontFamily,qdFontName);
545 m_faceName = wxMacMakeStringFromPascal( qdFontName );
546 }
547 else
548 {
549 switch ( m_family )
550 {
551 case wxSCRIPT :
552 case wxROMAN :
553 case wxDECORATIVE :
554 m_faceName = wxT("Times");
555 break ;
556
557 case wxSWISS :
558 m_faceName = wxT("Helvetica");
559 break ;
560
561 case wxMODERN :
562 case wxTELETYPE:
563 m_faceName = wxT("Courier");
564 break ;
565
566 default:
567 m_faceName = wxT("Times");
568 break ;
569 }
570 wxMacStringToPascal( m_faceName , qdFontName );
571 m_macFontFamily = FMGetFontFamilyFromName( qdFontName );
572 if ( m_macFontFamily == kInvalidFontFamily )
573 {
574 wxLogDebug( wxT("ATSFontFamilyFindFromName failed for %s"), m_faceName.c_str() );
575 m_macFontFamily = GetAppFont();
576 }
577 }
578 }
579 else
580 {
581 if ( m_faceName == wxT("systemfont") )
582 m_macFontFamily = GetSysFont();
583 else if ( m_faceName == wxT("applicationfont") )
584 m_macFontFamily = GetAppFont();
585 else
586 {
587 wxCFStringRef cf( m_faceName, wxLocale::GetSystemEncoding() );
588 ATSFontFamilyRef atsfamily = ATSFontFamilyFindFromName( cf , kATSOptionFlagsDefault );
589 if ( atsfamily == (ATSFontFamilyRef) -1 )
590 {
591 wxLogDebug( wxT("ATSFontFamilyFindFromName failed for ") + m_faceName );
592 m_macFontFamily = GetAppFont();
593 }
594 else
595 m_macFontFamily = FMGetFontFamilyFromATSFontFamilyRef( atsfamily );
596 }
597 }
598
599 m_macFontStyle = 0;
600 if (m_weight == wxBOLD)
601 m_macFontStyle |= bold;
602 if (m_style == wxITALIC || m_style == wxSLANT)
603 m_macFontStyle |= italic;
604 if (m_underlined)
605 m_macFontStyle |= underline;
606 m_macFontSize = m_pointSize ;
607 }
608
609 // we try to get as much styles as possible into ATSU
610
611
612 // ATSUFontID and FMFont are equivalent
613 FMFontStyle intrinsicStyle = 0 ;
614 status = FMGetFontFromFontFamilyInstance( m_macFontFamily , m_macFontStyle , &m_macATSUFontID , &intrinsicStyle);
615 wxASSERT_MSG( status == noErr , wxT("couldn't get an ATSUFont from font family") );
616 m_macATSUAdditionalQDStyles = m_macFontStyle & (~intrinsicStyle );
617
618 if ( m_macATSUStyle )
619 {
620 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
621 m_macATSUStyle = NULL ;
622 }
623
624 status = ::ATSUCreateStyle((ATSUStyle *)&m_macATSUStyle);
625 wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") );
626
627 ATSUAttributeTag atsuTags[] =
628 {
629 kATSUFontTag ,
630 kATSUSizeTag ,
631 kATSUVerticalCharacterTag,
632 kATSUQDBoldfaceTag ,
633 kATSUQDItalicTag ,
634 kATSUQDUnderlineTag ,
635 kATSUQDCondensedTag ,
636 kATSUQDExtendedTag ,
637 };
638 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
639 {
640 sizeof( ATSUFontID ) ,
641 sizeof( Fixed ) ,
642 sizeof( ATSUVerticalCharacterType),
643 sizeof( Boolean ) ,
644 sizeof( Boolean ) ,
645 sizeof( Boolean ) ,
646 sizeof( Boolean ) ,
647 sizeof( Boolean ) ,
648 };
649
650 Boolean kTrue = true ;
651 Boolean kFalse = false ;
652
653 Fixed atsuSize = IntToFixed( m_macFontSize );
654 ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
655 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
656 {
657 &m_macATSUFontID ,
658 &atsuSize ,
659 &kHorizontal,
660 (m_macATSUAdditionalQDStyles & bold) ? &kTrue : &kFalse ,
661 (m_macATSUAdditionalQDStyles & italic) ? &kTrue : &kFalse ,
662 (m_macATSUAdditionalQDStyles & underline) ? &kTrue : &kFalse ,
663 (m_macATSUAdditionalQDStyles & condense) ? &kTrue : &kFalse ,
664 (m_macATSUAdditionalQDStyles & extend) ? &kTrue : &kFalse ,
665 };
666
667 status = ::ATSUSetAttributes(
668 (ATSUStyle)m_macATSUStyle,
669 sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,
670 atsuTags, atsuSizes, atsuValues);
671
672 wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );
673 return;
674 }
675 #endif
676 }
677
678 // ----------------------------------------------------------------------------
679 // wxFont
680 // ----------------------------------------------------------------------------
681
682 bool wxFont::Create(const wxNativeFontInfo& info)
683 {
684 return Create(
685 info.pointSize, info.family, info.style, info.weight,
686 info.underlined, info.faceName, info.encoding );
687 }
688
689 wxFont::wxFont(const wxString& fontdesc)
690 {
691 wxNativeFontInfo info;
692 if ( info.FromString(fontdesc) )
693 (void)Create(info);
694 }
695
696 bool wxFont::Create(int pointSize,
697 int family,
698 int style,
699 int weight,
700 bool underlined,
701 const wxString& faceName,
702 wxFontEncoding encoding)
703 {
704 UnRef();
705
706 m_refData = new wxFontRefData(
707 pointSize, family, style, weight,
708 underlined, faceName, encoding);
709
710 RealizeResource();
711
712 return true;
713 }
714
715 #if wxOSX_USE_CORE_TEXT
716
717 bool wxFont::MacCreateFromUIFont(wxUint32 ctFontType )
718 {
719 UnRef();
720
721 m_refData = new wxFontRefData(ctFontType);
722 RealizeResource();
723
724 return true;
725 }
726
727 bool wxFont::MacCreateFromCTFontDescriptor( const void * ctFontDescriptor , int size )
728 {
729 UnRef();
730
731 m_refData = new wxFontRefData((CTFontDescriptorRef)ctFontDescriptor, size);;
732 RealizeResource();
733
734 return true;
735 }
736
737
738 #endif
739
740 #if wxOSX_USE_CARBON
741 bool wxFont::MacCreateFromThemeFont(wxUint16 themeFontID)
742 {
743 #if wxOSX_USE_CORE_TEXT
744 if ( UMAGetSystemVersion() >= 0x1050)
745 {
746 return MacCreateFromUIFont(HIThemeGetUIFontType(themeFontID));
747 }
748 #endif
749 #if wxOSX_USE_ATSU_TEXT
750 {
751 UnRef();
752
753 m_refData = new wxFontRefData(
754 12, wxDEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
755 false, wxEmptyString, wxFONTENCODING_DEFAULT );
756
757 M_FONTDATA->m_macThemeFontID = themeFontID ;
758 RealizeResource();
759 return true;
760 }
761 #endif
762 return false;
763 }
764 #endif
765
766 wxFont::~wxFont()
767 {
768 }
769
770 bool wxFont::RealizeResource()
771 {
772 M_FONTDATA->MacFindFont();
773
774 return true;
775 }
776
777 void wxFont::SetEncoding(wxFontEncoding encoding)
778 {
779 Unshare();
780
781 M_FONTDATA->SetEncoding( encoding );
782
783 RealizeResource();
784 }
785
786 void wxFont::Unshare()
787 {
788 // Don't change shared data
789 if (!m_refData)
790 {
791 m_refData = new wxFontRefData();
792 }
793 else
794 {
795 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
796 UnRef();
797 m_refData = ref;
798 }
799 }
800
801 wxGDIRefData *wxFont::CreateGDIRefData() const
802 {
803 return new wxFontRefData;
804 }
805
806 wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const
807 {
808 return new wxFontRefData(*wx_static_cast(const wxFontRefData *, data));
809 }
810
811 void wxFont::SetPointSize(int pointSize)
812 {
813 if ( M_FONTDATA->GetPointSize() == pointSize )
814 return;
815
816 Unshare();
817
818 M_FONTDATA->SetPointSize( pointSize );
819
820 RealizeResource();
821 }
822
823 void wxFont::SetFamily(int family)
824 {
825 Unshare();
826
827 M_FONTDATA->SetFamily( family );
828
829 RealizeResource();
830 }
831
832 void wxFont::SetStyle(int style)
833 {
834 Unshare();
835
836 M_FONTDATA->SetStyle( style );
837
838 RealizeResource();
839 }
840
841 void wxFont::SetWeight(int weight)
842 {
843 Unshare();
844
845 M_FONTDATA->SetWeight( weight );
846
847 RealizeResource();
848 }
849
850 bool wxFont::SetFaceName(const wxString& faceName)
851 {
852 Unshare();
853
854 M_FONTDATA->SetFaceName( faceName );
855
856 RealizeResource();
857
858 return wxFontBase::SetFaceName(faceName);
859 }
860
861 void wxFont::SetUnderlined(bool underlined)
862 {
863 Unshare();
864
865 M_FONTDATA->SetUnderlined( underlined );
866
867 RealizeResource();
868 }
869
870 void wxFont::SetNoAntiAliasing( bool no )
871 {
872 Unshare();
873
874 M_FONTDATA->SetNoAntiAliasing( no );
875
876 RealizeResource();
877 }
878
879 // ----------------------------------------------------------------------------
880 // accessors
881 // ----------------------------------------------------------------------------
882
883 // TODO: insert checks everywhere for M_FONTDATA == NULL!
884
885 int wxFont::GetPointSize() const
886 {
887 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
888
889 return M_FONTDATA->GetPointSize();
890 }
891
892 wxSize wxFont::GetPixelSize() const
893 {
894 #if wxUSE_GRAPHICS_CONTEXT
895 // TODO: consider caching the value
896 wxGraphicsContext* dc = wxGraphicsContext::CreateFromNative((CGContextRef) NULL);
897 dc->SetFont(*(wxFont *)this,*wxBLACK);
898 wxDouble width, height = 0;
899 dc->GetTextExtent( wxT("g"), &width, &height, NULL, NULL);
900 delete dc;
901 return wxSize((int)width, (int)height);
902 #else
903 return wxFontBase::GetPixelSize();
904 #endif
905 }
906
907 int wxFont::GetFamily() const
908 {
909 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
910
911 return M_FONTDATA->GetFamily();
912 }
913
914 int wxFont::GetStyle() const
915 {
916 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
917
918 return M_FONTDATA->GetStyle() ;
919 }
920
921 int wxFont::GetWeight() const
922 {
923 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
924
925 return M_FONTDATA->GetWeight();
926 }
927
928 bool wxFont::GetUnderlined() const
929 {
930 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
931
932 return M_FONTDATA->GetUnderlined();
933 }
934
935 wxString wxFont::GetFaceName() const
936 {
937 wxCHECK_MSG( M_FONTDATA != NULL , wxEmptyString , wxT("invalid font") );
938
939 return M_FONTDATA->GetFaceName() ;
940 }
941
942 wxFontEncoding wxFont::GetEncoding() const
943 {
944 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTENCODING_DEFAULT , wxT("invalid font") );
945
946 return M_FONTDATA->GetEncoding() ;
947 }
948
949 bool wxFont::GetNoAntiAliasing() const
950 {
951 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
952
953 return M_FONTDATA->GetNoAntiAliasing();
954 }
955
956 #if wxOSX_USE_ATSU_TEXT
957
958 short wxFont::MacGetFontNum() const
959 {
960 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
961
962 return M_FONTDATA->m_macFontFamily;
963 }
964
965 short wxFont::MacGetFontSize() const
966 {
967 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
968
969 return M_FONTDATA->m_macFontSize;
970 }
971
972 wxByte wxFont::MacGetFontStyle() const
973 {
974 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
975
976 return M_FONTDATA->m_macFontStyle;
977 }
978
979 wxUint32 wxFont::MacGetATSUFontID() const
980 {
981 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
982
983 return M_FONTDATA->m_macATSUFontID;
984 }
985
986 wxUint32 wxFont::MacGetATSUAdditionalQDStyles() const
987 {
988 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
989
990 return M_FONTDATA->m_macATSUAdditionalQDStyles;
991 }
992
993 wxUint16 wxFont::MacGetThemeFontID() const
994 {
995 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
996
997 return M_FONTDATA->m_macThemeFontID;
998 }
999 #endif
1000
1001 #if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT
1002 void * wxFont::MacGetATSUStyle() const
1003 {
1004 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
1005
1006 return M_FONTDATA->m_macATSUStyle;
1007 }
1008 #endif
1009
1010 #if wxOSX_USE_CORE_TEXT
1011
1012 const void * wxFont::MacGetCTFont() const
1013 {
1014 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
1015
1016 return (CTFontRef)(M_FONTDATA->m_ctFont);
1017 }
1018
1019 const void * wxFont::MacGetCTFontDescriptor() const
1020 {
1021 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
1022
1023 return (CTFontDescriptorRef)(M_FONTDATA->m_ctFontDescriptor);
1024 }
1025
1026 #endif
1027
1028 const wxNativeFontInfo * wxFont::GetNativeFontInfo() const
1029 {
1030 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
1031 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
1032
1033 M_FONTDATA->m_info.InitFromFont(*this);
1034
1035 return &(M_FONTDATA->m_info);
1036 }