]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/font.cpp
avoid setting initial position if it was not specified, broken in r70734
[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 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #include "wx/font.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/string.h"
17 #include "wx/utils.h"
18 #include "wx/intl.h"
19 #include "wx/gdicmn.h"
20 #include "wx/log.h"
21 #endif
22
23 #include "wx/fontutil.h"
24 #include "wx/graphics.h"
25 #include "wx/settings.h"
26 #include "wx/tokenzr.h"
27
28 #include "wx/osx/private.h"
29
30 #include <map>
31 #include <string>
32
33 class WXDLLEXPORT wxFontRefData: public wxGDIRefData
34 {
35 public:
36
37 wxFontRefData()
38 {
39 Init();
40 m_info.Init(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
41 false, wxEmptyString, wxFONTENCODING_DEFAULT);
42 }
43
44 wxFontRefData(const wxFontRefData& data);
45
46 wxFontRefData( const wxNativeFontInfo& info ) : m_info(info)
47 {
48 Init();
49 }
50
51 wxFontRefData(wxOSXSystemFont font, int size);
52
53 #if wxOSX_USE_CORE_TEXT
54 wxFontRefData( wxUint32 coreTextFontType );
55 wxFontRefData( CTFontRef font );
56 wxFontRefData( CTFontDescriptorRef fontdescriptor, int size );
57 #endif
58
59 virtual ~wxFontRefData();
60
61 void SetPointSize( int size )
62 {
63 if( GetPointSize() != size )
64 {
65 m_info.SetPointSize(size);
66 Free();
67 }
68 }
69
70 int GetPointSize() const { return m_info.GetPointSize(); }
71
72 void SetFamily( wxFontFamily family )
73 {
74 if ( m_info.m_family != family )
75 {
76 m_info.SetFamily( family );
77 Free();
78 }
79 }
80
81 wxFontFamily GetFamily() const { return m_info.GetFamily(); }
82
83 void SetStyle( wxFontStyle style )
84 {
85 if ( m_info.m_style != style )
86 {
87 m_info.SetStyle( style );
88 Free();
89 }
90 }
91
92
93 wxFontStyle GetStyle() const { return m_info.GetStyle(); }
94
95 void SetWeight( wxFontWeight weight )
96 {
97 if ( m_info.m_weight != weight )
98 {
99 m_info.SetWeight( weight );
100 Free();
101 }
102 }
103
104
105 wxFontWeight GetWeight() const { return m_info.GetWeight(); }
106
107 void SetUnderlined( bool u )
108 {
109 if ( m_info.m_underlined != u )
110 {
111 m_info.SetUnderlined( u );
112 Free();
113 }
114 }
115
116 bool GetUnderlined() const { return m_info.GetUnderlined(); }
117
118 void SetFaceName( const wxString& facename )
119 {
120 if ( m_info.m_faceName != facename )
121 {
122 m_info.SetFaceName( facename );
123 Free();
124 }
125 }
126
127 wxString GetFaceName() const { return m_info.GetFaceName(); }
128
129 void SetEncoding( wxFontEncoding encoding )
130 {
131 if ( m_info.m_encoding != encoding )
132 {
133 m_info.SetEncoding( encoding );
134 Free();
135 }
136 }
137
138 wxFontEncoding GetEncoding() const { return m_info.GetEncoding(); }
139
140 bool IsFixedWidth() const;
141
142 void Free();
143
144 void MacFindFont();
145
146 protected:
147 // common part of all ctors
148 void Init();
149 #if wxOSX_USE_CORE_TEXT
150 // void Init( CTFontRef font );
151 #endif
152 public:
153 bool m_fontValid;
154 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
155 // for true theming support we must store the correct font
156 // information here, as this speeds up and optimizes rendering
157 ThemeFontID m_macThemeFontID ;
158 #endif
159 #if wxOSX_USE_CORE_TEXT
160 wxCFRef<CTFontRef> m_ctFont;
161 #endif
162 #if wxOSX_USE_ATSU_TEXT
163 void CreateATSUFont();
164
165 ATSUStyle m_macATSUStyle ;
166 #endif
167 wxCFRef<CGFontRef> m_cgFont;
168 #if wxOSX_USE_COCOA
169 WX_NSFont m_nsFont;
170 #endif
171 #if wxOSX_USE_IPHONE
172 WX_UIFont m_uiFont;
173 #endif
174 wxNativeFontInfo m_info;
175 };
176
177 #define M_FONTDATA ((wxFontRefData*)m_refData)
178
179 wxFontRefData::wxFontRefData(const wxFontRefData& data) : wxGDIRefData()
180 {
181 Init();
182 m_info = data.m_info;
183 m_fontValid = data.m_fontValid;
184 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
185 m_macThemeFontID = data.m_macThemeFontID;
186 #endif
187 #if wxOSX_USE_CORE_TEXT
188 m_ctFont = data.m_ctFont;
189 #endif
190 m_cgFont = data.m_cgFont;
191 #if wxOSX_USE_ATSU_TEXT
192 if ( data.m_macATSUStyle != NULL )
193 {
194 ATSUCreateStyle(&m_macATSUStyle) ;
195 ATSUCopyAttributes(data.m_macATSUStyle, m_macATSUStyle);
196 }
197 #endif
198 #if wxOSX_USE_COCOA
199 m_nsFont = (NSFont*) wxMacCocoaRetain(data.m_nsFont);
200 #endif
201 #if wxOSX_USE_IPHONE
202 m_uiFont = (UIFont*) wxMacCocoaRetain(data.m_uiFont);
203 #endif
204
205 }
206
207 // ============================================================================
208 // implementation
209 // ============================================================================
210
211 // ----------------------------------------------------------------------------
212 // wxFontRefData
213 // ----------------------------------------------------------------------------
214
215 void wxFontRefData::Init()
216 {
217 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
218 m_macThemeFontID = kThemeCurrentPortFont ;
219 #endif
220 #if wxOSX_USE_ATSU_TEXT
221 m_macATSUStyle = NULL ;
222 #endif
223 #if wxOSX_USE_COCOA
224 m_nsFont = NULL;
225 #endif
226 #if wxOSX_USE_IPHONE
227 m_uiFont = NULL;
228 #endif
229 m_fontValid = false;
230 }
231
232 wxFontRefData::~wxFontRefData()
233 {
234 Free();
235 }
236
237 void wxFontRefData::Free()
238 {
239 #if wxOSX_USE_CORE_TEXT
240 m_ctFont.reset();
241 #endif
242 m_cgFont.reset();
243 #if wxOSX_USE_ATSU_TEXT
244 #if wxOSX_USE_CARBON
245 m_macThemeFontID = kThemeCurrentPortFont ;
246 #endif
247 if ( m_macATSUStyle )
248 {
249 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
250 m_macATSUStyle = NULL ;
251 }
252 #endif
253 #if wxOSX_USE_COCOA
254 if (m_nsFont != NULL)
255 {
256 wxMacCocoaRelease(m_nsFont);
257 m_nsFont = NULL;
258 }
259 #endif
260 #if wxOSX_USE_IPHONE
261 if (m_uiFont != NULL)
262 {
263 wxMacCocoaRelease(m_uiFont);
264 m_uiFont = NULL;
265 }
266 #endif
267 m_fontValid = false;
268 }
269
270 wxFontRefData::wxFontRefData(wxOSXSystemFont font, int size)
271 {
272 wxASSERT( font != wxOSX_SYSTEM_FONT_NONE );
273 Init();
274
275 #if wxOSX_USE_CORE_TEXT
276 {
277 CTFontUIFontType uifont = kCTFontSystemFontType;
278 switch( font )
279 {
280 case wxOSX_SYSTEM_FONT_NORMAL:
281 uifont = kCTFontSystemFontType;
282 break;
283 case wxOSX_SYSTEM_FONT_BOLD:
284 uifont = kCTFontEmphasizedSystemFontType;
285 break;
286 case wxOSX_SYSTEM_FONT_SMALL:
287 uifont = kCTFontSmallSystemFontType;
288 break;
289 case wxOSX_SYSTEM_FONT_SMALL_BOLD:
290 uifont = kCTFontSmallEmphasizedSystemFontType;
291 break;
292 case wxOSX_SYSTEM_FONT_MINI:
293 uifont = kCTFontMiniSystemFontType;
294 break;
295 case wxOSX_SYSTEM_FONT_MINI_BOLD:
296 uifont = kCTFontMiniEmphasizedSystemFontType;
297 break;
298 case wxOSX_SYSTEM_FONT_LABELS:
299 uifont = kCTFontLabelFontType;
300 break;
301 case wxOSX_SYSTEM_FONT_VIEWS:
302 uifont = kCTFontViewsFontType;
303 break;
304 default:
305 break;
306 }
307 m_ctFont.reset(CTFontCreateUIFontForLanguage( uifont, (CGFloat) size, NULL ));
308 wxCFRef<CTFontDescriptorRef> descr;
309 descr.reset( CTFontCopyFontDescriptor( m_ctFont ) );
310 m_info.Init(descr);
311 }
312 #endif
313 #if wxOSX_USE_ATSU_TEXT
314 {
315 #if !wxOSX_USE_CARBON
316 // not needed outside
317 ThemeFontID m_macThemeFontID = kThemeSystemFont;
318 #endif
319 switch( font )
320 {
321 case wxOSX_SYSTEM_FONT_NORMAL:
322 m_macThemeFontID = kThemeSystemFont;
323 break;
324 case wxOSX_SYSTEM_FONT_BOLD:
325 m_macThemeFontID = kThemeEmphasizedSystemFont;
326 break;
327 case wxOSX_SYSTEM_FONT_SMALL:
328 m_macThemeFontID = kThemeSmallSystemFont;
329 break;
330 case wxOSX_SYSTEM_FONT_SMALL_BOLD:
331 m_macThemeFontID = kThemeSmallEmphasizedSystemFont;
332 break;
333 case wxOSX_SYSTEM_FONT_MINI:
334 m_macThemeFontID = kThemeMiniSystemFont;
335 break;
336 case wxOSX_SYSTEM_FONT_MINI_BOLD:
337 // bold not available under theming
338 m_macThemeFontID = kThemeMiniSystemFont;
339 break;
340 case wxOSX_SYSTEM_FONT_LABELS:
341 m_macThemeFontID = kThemeLabelFont;
342 break;
343 case wxOSX_SYSTEM_FONT_VIEWS:
344 m_macThemeFontID = kThemeViewsFont;
345 break;
346 default:
347 break;
348 }
349 if ( m_info.m_faceName.empty() )
350 {
351 Style style ;
352 FMFontSize fontSize;
353 Str255 qdFontName ;
354
355 GetThemeFont( m_macThemeFontID, GetApplicationScript(), qdFontName, &fontSize, &style );
356 if ( size != 0 )
357 fontSize = size;
358
359 wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
360 wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
361 bool underlined = false;
362
363 if ( style & bold )
364 fontweight = wxFONTWEIGHT_BOLD ;
365 else
366 fontweight = wxFONTWEIGHT_NORMAL ;
367 if ( style & italic )
368 fontstyle = wxFONTSTYLE_ITALIC ;
369 if ( style & underline )
370 underlined = true ;
371
372 m_info.Init(fontSize,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined,
373 wxMacMakeStringFromPascal( qdFontName ), wxFONTENCODING_DEFAULT);
374 }
375 }
376 #endif
377 #if wxOSX_USE_COCOA
378 m_nsFont = wxFont::OSXCreateNSFont( font, &m_info );
379 #endif
380 #if wxOSX_USE_IPHONE
381 m_uiFont = wxFont::OSXCreateUIFont( font, &m_info );
382 #endif
383 m_info.EnsureValid();
384 #if wxOSX_USE_ATSU_TEXT
385 CreateATSUFont();
386 #endif
387
388 m_fontValid = true;
389 }
390
391 #if wxOSX_USE_ATSU_TEXT
392 void wxFontRefData::CreateATSUFont()
393 {
394 // we try to get as much styles as possible into ATSU
395
396 OSStatus status = ::ATSUCreateStyle(&m_macATSUStyle);
397 wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") );
398
399 ATSUAttributeTag atsuTags[] =
400 {
401 kATSUFontTag ,
402 kATSUSizeTag ,
403 kATSUVerticalCharacterTag,
404 kATSUQDBoldfaceTag ,
405 kATSUQDItalicTag ,
406 kATSUQDUnderlineTag ,
407 kATSUQDCondensedTag ,
408 kATSUQDExtendedTag ,
409 };
410 ByteCount atsuSizes[WXSIZEOF(atsuTags)] =
411 {
412 sizeof( ATSUFontID ) ,
413 sizeof( Fixed ) ,
414 sizeof( ATSUVerticalCharacterType),
415 sizeof( Boolean ) ,
416 sizeof( Boolean ) ,
417 sizeof( Boolean ) ,
418 sizeof( Boolean ) ,
419 sizeof( Boolean ) ,
420 };
421
422 Boolean kTrue = true ;
423 Boolean kFalse = false ;
424
425 Fixed atsuSize = IntToFixed( m_info.m_pointSize );
426 ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
427 FMFontStyle addQDStyle = m_info.m_atsuAdditionalQDStyles;
428 ATSUAttributeValuePtr atsuValues[WXSIZEOF(atsuTags)] =
429 {
430 &m_info.m_atsuFontID ,
431 &atsuSize ,
432 &kHorizontal,
433 (addQDStyle & bold) ? &kTrue : &kFalse ,
434 (addQDStyle & italic) ? &kTrue : &kFalse ,
435 (addQDStyle & underline) ? &kTrue : &kFalse ,
436 (addQDStyle & condense) ? &kTrue : &kFalse ,
437 (addQDStyle & extend) ? &kTrue : &kFalse ,
438 };
439
440 status = ::ATSUSetAttributes(
441 (ATSUStyle)m_macATSUStyle,
442 WXSIZEOF(atsuTags),
443 atsuTags, atsuSizes, atsuValues);
444
445 wxASSERT_MSG( status == noErr , wxString::Format(wxT("couldn't modify ATSU style. Status was %d"), (int) status).c_str() );
446
447 if ( m_cgFont.get() == NULL )
448 {
449 ATSFontRef fontRef = FMGetATSFontRefFromFont(m_info.m_atsuFontID);
450 m_cgFont.reset( CGFontCreateWithPlatformFont( &fontRef ) );
451 }
452 }
453 #endif
454
455 static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
456 static const CGAffineTransform kSlantTransform = CGAffineTransformMake( 1, 0, tan(DegToRad(11)), 1, 0, 0 );
457
458 void wxFontRefData::MacFindFont()
459 {
460 if ( m_fontValid )
461 return;
462
463 wxCHECK_RET( m_info.m_pointSize > 0, wxT("Point size should not be zero.") );
464
465 m_info.EnsureValid();
466
467 #if wxOSX_USE_CORE_TEXT
468 {
469 CTFontSymbolicTraits traits = 0;
470
471 if (m_info.m_weight == wxFONTWEIGHT_BOLD)
472 traits |= kCTFontBoldTrait;
473 if (m_info.m_style == wxFONTSTYLE_ITALIC || m_info.m_style == wxFONTSTYLE_SLANT)
474 traits |= kCTFontItalicTrait;
475
476 // use font caching
477 wxString lookupnameWithSize = wxString::Format( "%s_%u_%d", m_info.m_faceName, traits, m_info.m_pointSize );
478
479 static std::map< std::wstring , wxCFRef< CTFontRef > > fontcache ;
480 m_ctFont = fontcache[ std::wstring(lookupnameWithSize.wc_str()) ];
481 if ( !m_ctFont )
482 {
483 m_ctFont.reset(CTFontCreateWithName( wxCFStringRef(m_info.m_faceName), m_info.m_pointSize , NULL ));
484 if ( m_ctFont.get() == NULL )
485 {
486 // TODO try fallbacks according to font type
487 m_ctFont.reset(CTFontCreateUIFontForLanguage( kCTFontSystemFontType, m_info.m_pointSize , NULL ));
488 }
489 else
490 {
491 if ( traits != 0 )
492 {
493 // attempt native font variant, if not available, fallback to italic emulation mode and remove bold
494 CTFontRef fontWithTraits = CTFontCreateCopyWithSymbolicTraits( m_ctFont, 0, NULL, traits, traits );
495 if ( fontWithTraits == NULL )
496 {
497 CTFontSymbolicTraits remainingTraits = traits;
498 const CGAffineTransform* remainingTransform = NULL;
499
500 if( remainingTraits & kCTFontItalicTrait )
501 {
502 remainingTraits &= ~kCTFontItalicTrait;
503 remainingTransform = &kSlantTransform;
504 if ( remainingTraits & kCTFontBoldTrait )
505 {
506 // first try an emulated oblique with an existing bold font
507 fontWithTraits = CTFontCreateCopyWithSymbolicTraits( m_ctFont, 0, remainingTransform, remainingTraits, remainingTraits );
508 if ( fontWithTraits == NULL )
509 {
510 // give in on the bold, try native oblique
511 fontWithTraits = CTFontCreateCopyWithSymbolicTraits( m_ctFont, 0, NULL, kCTFontItalicTrait, kCTFontItalicTrait );
512 }
513 }
514 }
515
516 if ( fontWithTraits == NULL )
517 {
518 fontWithTraits = CTFontCreateWithName( wxCFStringRef(m_info.m_faceName), m_info.m_pointSize, remainingTransform );
519 }
520
521 }
522 if ( fontWithTraits != NULL )
523 m_ctFont.reset(fontWithTraits);
524 }
525 }
526 }
527
528 m_cgFont.reset(CTFontCopyGraphicsFont(m_ctFont, NULL));
529 }
530
531 #endif
532 #if wxOSX_USE_ATSU_TEXT
533 CreateATSUFont();
534 #endif
535 #if wxOSX_USE_COCOA
536 m_nsFont = wxFont::OSXCreateNSFont( &m_info );
537 #endif
538 #if wxOSX_USE_IPHONE
539 m_uiFont = wxFont::OSXCreateUIFont( &m_info );
540 #endif
541 m_fontValid = true;
542 }
543
544 bool wxFontRefData::IsFixedWidth() const
545 {
546 #if wxOSX_USE_CORE_TEXT
547 CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(m_ctFont);
548 return (traits & kCTFontMonoSpaceTrait) != 0;
549 #else
550 return false;
551 #endif
552 }
553
554 // ----------------------------------------------------------------------------
555 // wxFont
556 // ----------------------------------------------------------------------------
557
558 bool wxFont::Create(const wxNativeFontInfo& info)
559 {
560 UnRef();
561
562 m_refData = new wxFontRefData( info );
563 RealizeResource();
564
565 return true;
566 }
567
568 wxFont::wxFont(wxOSXSystemFont font)
569 {
570 m_refData = new wxFontRefData( font, 0 );
571 }
572
573 wxFont::wxFont(const wxString& fontdesc)
574 {
575 wxNativeFontInfo info;
576 if ( info.FromString(fontdesc) )
577 (void)Create(info);
578 }
579
580 bool wxFont::Create(int pointSize,
581 wxFontFamily family,
582 wxFontStyle style,
583 wxFontWeight weight,
584 bool underlined,
585 const wxString& faceNameParam,
586 wxFontEncoding encoding)
587 {
588 UnRef();
589
590 wxString faceName = faceNameParam;
591
592 if ( faceName.empty() )
593 {
594 switch ( family )
595 {
596 case wxFONTFAMILY_DEFAULT :
597 faceName = wxT("Lucida Grande");
598 break;
599
600 case wxFONTFAMILY_SCRIPT :
601 case wxFONTFAMILY_ROMAN :
602 case wxFONTFAMILY_DECORATIVE :
603 faceName = wxT("Times");
604 break ;
605
606 case wxFONTFAMILY_SWISS :
607 faceName = wxT("Helvetica");
608 break ;
609
610 case wxFONTFAMILY_MODERN :
611 case wxFONTFAMILY_TELETYPE:
612 faceName = wxT("Courier");
613 break ;
614
615 default:
616 faceName = wxT("Times");
617 break ;
618 }
619 }
620
621 wxNativeFontInfo info;
622
623 info.Init(pointSize, family, style, weight,
624 underlined, faceName, encoding);
625
626 m_refData = new wxFontRefData(info);
627
628 return true;
629 }
630
631 wxFont::~wxFont()
632 {
633 }
634
635 void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo& info)
636 {
637 UnRef();
638
639 m_refData = new wxFontRefData( info);
640 }
641
642
643 bool wxFont::RealizeResource()
644 {
645 M_FONTDATA->MacFindFont();
646
647 return true;
648 }
649
650 void wxFont::SetEncoding(wxFontEncoding encoding)
651 {
652 AllocExclusive();
653
654 M_FONTDATA->SetEncoding( encoding );
655 }
656
657 wxGDIRefData *wxFont::CreateGDIRefData() const
658 {
659 return new wxFontRefData;
660 }
661
662 wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const
663 {
664 return new wxFontRefData(*static_cast<const wxFontRefData *>(data));
665 }
666
667 void wxFont::SetPointSize(int pointSize)
668 {
669 if ( M_FONTDATA != NULL && M_FONTDATA->GetPointSize() == pointSize )
670 return;
671
672 AllocExclusive();
673
674 M_FONTDATA->SetPointSize( pointSize );
675 }
676
677 void wxFont::SetFamily(wxFontFamily family)
678 {
679 AllocExclusive();
680
681 M_FONTDATA->SetFamily( family );
682 }
683
684 void wxFont::SetStyle(wxFontStyle style)
685 {
686 AllocExclusive();
687
688 M_FONTDATA->SetStyle( style );
689 }
690
691 void wxFont::SetWeight(wxFontWeight weight)
692 {
693 AllocExclusive();
694
695 M_FONTDATA->SetWeight( weight );
696 }
697
698 bool wxFont::SetFaceName(const wxString& faceName)
699 {
700 AllocExclusive();
701
702 M_FONTDATA->SetFaceName( faceName );
703
704 return wxFontBase::SetFaceName(faceName);
705 }
706
707 void wxFont::SetUnderlined(bool underlined)
708 {
709 AllocExclusive();
710
711 M_FONTDATA->SetUnderlined( underlined );
712 }
713
714 // ----------------------------------------------------------------------------
715 // accessors
716 // ----------------------------------------------------------------------------
717
718 // TODO: insert checks everywhere for M_FONTDATA == NULL!
719
720 int wxFont::GetPointSize() const
721 {
722 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
723
724 return M_FONTDATA->GetPointSize();
725 }
726
727 wxSize wxFont::GetPixelSize() const
728 {
729 #if wxUSE_GRAPHICS_CONTEXT
730 // TODO: consider caching the value
731 wxGraphicsContext* dc = wxGraphicsContext::CreateFromNative((CGContextRef) NULL);
732 dc->SetFont(*(wxFont *)this,*wxBLACK);
733 wxDouble width, height = 0;
734 dc->GetTextExtent( wxT("g"), &width, &height, NULL, NULL);
735 delete dc;
736 return wxSize((int)width, (int)height);
737 #else
738 return wxFontBase::GetPixelSize();
739 #endif
740 }
741
742 bool wxFont::IsFixedWidth() const
743 {
744 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
745
746 // cast away constness otherwise lazy font resolution is not possible
747 const_cast<wxFont *>(this)->RealizeResource();
748
749 return M_FONTDATA->IsFixedWidth();
750 }
751
752 wxFontFamily wxFont::DoGetFamily() const
753 {
754 return M_FONTDATA->GetFamily();
755 }
756
757 wxFontStyle wxFont::GetStyle() const
758 {
759 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTSTYLE_MAX, wxT("invalid font") );
760
761 return M_FONTDATA->GetStyle() ;
762 }
763
764 wxFontWeight wxFont::GetWeight() const
765 {
766 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTWEIGHT_MAX, wxT("invalid font") );
767
768 return M_FONTDATA->GetWeight();
769 }
770
771 bool wxFont::GetUnderlined() const
772 {
773 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
774
775 return M_FONTDATA->GetUnderlined();
776 }
777
778 wxString wxFont::GetFaceName() const
779 {
780 wxCHECK_MSG( M_FONTDATA != NULL , wxEmptyString , wxT("invalid font") );
781
782 return M_FONTDATA->GetFaceName() ;
783 }
784
785 wxFontEncoding wxFont::GetEncoding() const
786 {
787 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTENCODING_DEFAULT , wxT("invalid font") );
788
789 return M_FONTDATA->GetEncoding() ;
790 }
791
792 #if wxOSX_USE_ATSU_TEXT && wxOSX_USE_CARBON
793
794 short wxFont::MacGetFontNum() const
795 {
796 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
797
798 // cast away constness otherwise lazy font resolution is not possible
799 const_cast<wxFont *>(this)->RealizeResource();
800
801 return M_FONTDATA->m_info.m_qdFontFamily;
802 }
803
804 wxByte wxFont::MacGetFontStyle() const
805 {
806 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
807
808 // cast away constness otherwise lazy font resolution is not possible
809 const_cast<wxFont *>(this)->RealizeResource();
810
811 return M_FONTDATA->m_info.m_qdFontStyle;
812 }
813
814 wxUint16 wxFont::MacGetThemeFontID() const
815 {
816 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
817
818 return M_FONTDATA->m_macThemeFontID;
819 }
820
821 #endif
822
823 #if wxOSX_USE_ATSU_TEXT
824 void * wxFont::MacGetATSUStyle() const
825 {
826 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
827
828 // cast away constness otherwise lazy font resolution is not possible
829 const_cast<wxFont *>(this)->RealizeResource();
830
831 return M_FONTDATA->m_macATSUStyle;
832 }
833
834 wxUint32 wxFont::MacGetATSUFontID() const
835 {
836 wxCHECK_MSG( M_FONTDATA != NULL, 0, wxT("invalid font") );
837
838 // cast away constness otherwise lazy font resolution is not possible
839 const_cast<wxFont *>(this)->RealizeResource();
840
841 return M_FONTDATA->m_info.m_atsuFontID;
842 }
843
844 wxUint32 wxFont::MacGetATSUAdditionalQDStyles() const
845 {
846 wxCHECK_MSG( M_FONTDATA != NULL, 0, wxT("invalid font") );
847
848 // cast away constness otherwise lazy font resolution is not possible
849 const_cast<wxFont *>(this)->RealizeResource();
850
851 return M_FONTDATA->m_info.m_atsuAdditionalQDStyles;
852 }
853 #endif
854
855 #if wxOSX_USE_CORE_TEXT
856
857 CTFontRef wxFont::OSXGetCTFont() const
858 {
859 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
860
861 // cast away constness otherwise lazy font resolution is not possible
862 const_cast<wxFont *>(this)->RealizeResource();
863
864 return (CTFontRef)(M_FONTDATA->m_ctFont);
865 }
866
867 #endif
868
869 #if wxOSX_USE_COCOA_OR_CARBON
870
871 CGFontRef wxFont::OSXGetCGFont() const
872 {
873 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
874
875 // cast away constness otherwise lazy font resolution is not possible
876 const_cast<wxFont *>(this)->RealizeResource();
877
878 return (M_FONTDATA->m_cgFont);
879 }
880
881 #endif
882
883
884 #if wxOSX_USE_COCOA
885
886 NSFont* wxFont::OSXGetNSFont() const
887 {
888 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
889
890 // cast away constness otherwise lazy font resolution is not possible
891 const_cast<wxFont *>(this)->RealizeResource();
892
893 return (M_FONTDATA->m_nsFont);
894 }
895
896 #endif
897
898 #if wxOSX_USE_IPHONE
899
900 UIFont* wxFont::OSXGetUIFont() const
901 {
902 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
903
904 // cast away constness otherwise lazy font resolution is not possible
905 const_cast<wxFont *>(this)->RealizeResource();
906
907 return (M_FONTDATA->m_uiFont);
908 }
909
910 #endif
911
912 const wxNativeFontInfo * wxFont::GetNativeFontInfo() const
913 {
914 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
915 wxCHECK_MSG( IsOk(), NULL, wxT("invalid font") );
916
917 // cast away constness otherwise lazy font resolution is not possible
918 const_cast<wxFont *>(this)->RealizeResource();
919
920 // M_FONTDATA->m_info.InitFromFont(*this);
921
922 return &(M_FONTDATA->m_info);
923 }
924
925 // ----------------------------------------------------------------------------
926 // wxNativeFontInfo
927 // ----------------------------------------------------------------------------
928
929 #if 0 // wxOSX_USE_CORE_TEXT
930
931 /* from Core Text Manual Common Operations */
932
933 static CTFontDescriptorRef wxMacCreateCTFontDescriptor(CFStringRef iFamilyName, CTFontSymbolicTraits iTraits )
934 {
935 CTFontDescriptorRef descriptor = NULL;
936 CFMutableDictionaryRef attributes;
937
938 wxASSERT(iFamilyName != NULL);
939 // Create a mutable dictionary to hold our attributes.
940 attributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
941 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
942 wxASSERT(attributes != NULL);
943
944 if (attributes != NULL) {
945 // Add a family name to our attributes.
946 CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, iFamilyName);
947
948
949 if ( iTraits ) {
950 CFMutableDictionaryRef traits;
951 CFNumberRef symTraits;
952
953 // Create the traits dictionary.
954 symTraits = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type,
955 &iTraits);
956 wxASSERT(symTraits != NULL);
957
958 if (symTraits != NULL) {
959 // Create a dictionary to hold our traits values.
960 traits = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
961 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
962 wxASSERT(traits != NULL);
963
964 if (traits != NULL) {
965 // Add the symbolic traits value to the traits dictionary.
966 CFDictionaryAddValue(traits, kCTFontSymbolicTrait, symTraits);
967
968 // Add the traits attribute to our attributes.
969 CFDictionaryAddValue(attributes, kCTFontTraitsAttribute, traits);
970 CFRelease(traits);
971 }
972 CFRelease(symTraits);
973 }
974 }
975 // Create the font descriptor with our attributes
976 descriptor = CTFontDescriptorCreateWithAttributes(attributes);
977 wxASSERT(descriptor != NULL);
978
979 CFRelease(attributes);
980 }
981 // Return our font descriptor.
982 return descriptor ;
983 }
984
985 #endif
986
987 void wxNativeFontInfo::Init()
988 {
989 #if wxOSX_USE_ATSU_TEXT
990 m_atsuFontID = 0 ;
991 m_atsuAdditionalQDStyles = 0;
992 m_atsuFontValid = false;
993 #if wxOSX_USE_CARBON
994 m_qdFontStyle = 0;
995 m_qdFontFamily = 0;
996 #endif
997 #endif
998 m_pointSize = 0;
999 m_family = wxFONTFAMILY_DEFAULT;
1000 m_style = wxFONTSTYLE_NORMAL;
1001 m_weight = wxFONTWEIGHT_NORMAL;
1002 m_underlined = false;
1003 m_faceName.clear();
1004 m_encoding = wxFont::GetDefaultEncoding();
1005 m_descriptorValid = false;
1006 }
1007
1008 #if wxOSX_USE_CORE_TEXT
1009 void wxNativeFontInfo::Init(CTFontDescriptorRef descr)
1010 {
1011 Init();
1012
1013 wxCFRef< CFNumberRef > sizevalue( (CFNumberRef) CTFontDescriptorCopyAttribute( descr, kCTFontSizeAttribute ) );
1014 float fsize;
1015 if ( CFNumberGetValue( sizevalue , kCFNumberFloatType , &fsize ) )
1016 m_pointSize = (int)( fsize + 0.5 );
1017
1018 wxCFRef< CFDictionaryRef > traitsvalue( (CFDictionaryRef) CTFontDescriptorCopyAttribute( descr, kCTFontTraitsAttribute ) );
1019 CTFontSymbolicTraits traits;
1020 if ( CFNumberGetValue((CFNumberRef) CFDictionaryGetValue(traitsvalue,kCTFontSymbolicTrait),kCFNumberIntType,&traits) )
1021 {
1022 if ( traits & kCTFontItalicTrait )
1023 m_style = wxFONTSTYLE_ITALIC;
1024 if ( traits & kCTFontBoldTrait )
1025 m_weight = wxFONTWEIGHT_BOLD ;
1026 }
1027
1028 wxCFStringRef familyName( (CFStringRef) CTFontDescriptorCopyAttribute(descr, kCTFontFamilyNameAttribute));
1029 m_faceName = familyName.AsString();
1030 }
1031 #endif
1032
1033 void wxNativeFontInfo::EnsureValid()
1034 {
1035 if ( m_descriptorValid )
1036 return;
1037
1038 #if wxOSX_USE_ATSU_TEXT
1039 if ( !m_atsuFontValid )
1040 {
1041 #if !wxOSX_USE_CARBON
1042 // not needed outside
1043 wxInt16 m_qdFontFamily;
1044 wxInt16 m_qdFontStyle;
1045 #endif
1046 wxCFStringRef cf( m_faceName, wxLocale::GetSystemEncoding() );
1047 ATSFontFamilyRef atsfamily = ATSFontFamilyFindFromName( cf , kATSOptionFlagsDefault );
1048 if ( atsfamily == (ATSFontFamilyRef) -1 )
1049 {
1050 wxLogDebug( wxT("ATSFontFamilyFindFromName failed for ") + m_faceName );
1051 m_qdFontFamily = GetAppFont();
1052 }
1053 else
1054 {
1055 m_qdFontFamily = FMGetFontFamilyFromATSFontFamilyRef( atsfamily );
1056 }
1057
1058 m_qdFontStyle = 0;
1059 if (m_weight == wxFONTWEIGHT_BOLD)
1060 m_qdFontStyle |= bold;
1061 if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT)
1062 m_qdFontStyle |= italic;
1063 if (m_underlined)
1064 m_qdFontStyle |= underline;
1065
1066
1067 // we try to get as much styles as possible into ATSU
1068
1069 // ATSUFontID and FMFont are equivalent
1070 FMFontStyle intrinsicStyle = 0 ;
1071 OSStatus status = FMGetFontFromFontFamilyInstance( m_qdFontFamily , m_qdFontStyle , (FMFont*)&m_atsuFontID , &intrinsicStyle);
1072 if ( status != noErr )
1073 {
1074 wxFAIL_MSG( wxT("couldn't get an ATSUFont from font family") );
1075 }
1076 m_atsuAdditionalQDStyles = m_qdFontStyle & (~intrinsicStyle );
1077 m_atsuFontValid = true;
1078 }
1079 #endif
1080 m_descriptorValid = true;
1081 }
1082
1083 void wxNativeFontInfo::Init(const wxNativeFontInfo& info)
1084 {
1085 Init();
1086 #if wxOSX_USE_ATSU_TEXT
1087 m_atsuFontValid = info.m_atsuFontValid;
1088 m_atsuFontID = info.m_atsuFontID ;
1089 m_atsuAdditionalQDStyles = info.m_atsuAdditionalQDStyles;
1090 #if wxOSX_USE_CARBON
1091 m_qdFontFamily = info.m_qdFontFamily;
1092 m_qdFontStyle = info.m_qdFontStyle;
1093 #endif
1094 #endif
1095 m_pointSize = info.m_pointSize;
1096 m_family = info.m_family;
1097 m_style = info.m_style;
1098 m_weight = info.m_weight;
1099 m_underlined = info.m_underlined;
1100 m_faceName = info.m_faceName;
1101 m_encoding = info.m_encoding;
1102 m_descriptorValid = info.m_descriptorValid;
1103 }
1104
1105 void wxNativeFontInfo::Init(int size,
1106 wxFontFamily family,
1107 wxFontStyle style,
1108 wxFontWeight weight,
1109 bool underlined,
1110 const wxString& faceName,
1111 wxFontEncoding encoding)
1112 {
1113 Init();
1114
1115 // We should use the default font size if the special value wxDEFAULT is
1116 // specified and we also handle -1 as a synonym for wxDEFAULT for
1117 // compatibility with wxGTK (see #12541).
1118 //
1119 // Notice that we rely on the fact that wxNORMAL_FONT itself is not
1120 // initialized using this ctor, but from native font info.
1121 m_pointSize = size == -1 || size == wxDEFAULT
1122 ? wxNORMAL_FONT->GetPointSize()
1123 : size;
1124 m_family = family;
1125 m_style = style;
1126 m_weight = weight;
1127 m_underlined = underlined;
1128 m_faceName = faceName;
1129 if ( encoding == wxFONTENCODING_DEFAULT )
1130 encoding = wxFont::GetDefaultEncoding();
1131 m_encoding = encoding;
1132
1133 }
1134
1135 void wxNativeFontInfo::Free()
1136 {
1137 #if wxOSX_USE_ATSU_TEXT
1138 m_atsuFontID = 0 ;
1139 m_atsuAdditionalQDStyles = 0;
1140 m_atsuFontValid = false;
1141 #endif
1142 m_descriptorValid = false;
1143 }
1144
1145 bool wxNativeFontInfo::FromString(const wxString& s)
1146 {
1147 long l;
1148
1149 wxStringTokenizer tokenizer(s, wxT(";"));
1150
1151 wxString token = tokenizer.GetNextToken();
1152 //
1153 // Ignore the version for now
1154 //
1155
1156 token = tokenizer.GetNextToken();
1157 if ( !token.ToLong(&l) )
1158 return false;
1159 m_pointSize = (int)l;
1160
1161 token = tokenizer.GetNextToken();
1162 if ( !token.ToLong(&l) )
1163 return false;
1164 m_family = (wxFontFamily)l;
1165
1166 token = tokenizer.GetNextToken();
1167 if ( !token.ToLong(&l) )
1168 return false;
1169 m_style = (wxFontStyle)l;
1170
1171 token = tokenizer.GetNextToken();
1172 if ( !token.ToLong(&l) )
1173 return false;
1174 m_weight = (wxFontWeight)l;
1175
1176 token = tokenizer.GetNextToken();
1177 if ( !token.ToLong(&l) )
1178 return false;
1179 m_underlined = l != 0;
1180
1181 m_faceName = tokenizer.GetNextToken();
1182
1183 #ifndef __WXMAC__
1184 if( !faceName )
1185 return false;
1186 #endif
1187
1188 token = tokenizer.GetNextToken();
1189 if ( !token.ToLong(&l) )
1190 return false;
1191 m_encoding = (wxFontEncoding)l;
1192
1193 return true;
1194 }
1195
1196 wxString wxNativeFontInfo::ToString() const
1197 {
1198 wxString s;
1199
1200 s.Printf(wxT("%d;%d;%d;%d;%d;%d;%s;%d"),
1201 0, // version
1202 m_pointSize,
1203 m_family,
1204 (int)m_style,
1205 (int)m_weight,
1206 m_underlined,
1207 m_faceName.GetData(),
1208 (int)m_encoding);
1209
1210 return s;
1211 }
1212
1213 int wxNativeFontInfo::GetPointSize() const
1214 {
1215 return m_pointSize;
1216 }
1217
1218 wxFontStyle wxNativeFontInfo::GetStyle() const
1219 {
1220 return m_style;
1221 }
1222
1223 wxFontWeight wxNativeFontInfo::GetWeight() const
1224 {
1225 return m_weight;
1226 }
1227
1228 bool wxNativeFontInfo::GetUnderlined() const
1229 {
1230 return m_underlined;
1231 }
1232
1233 wxString wxNativeFontInfo::GetFaceName() const
1234 {
1235 return m_faceName;
1236 }
1237
1238 wxFontFamily wxNativeFontInfo::GetFamily() const
1239 {
1240 return m_family;
1241 }
1242
1243 wxFontEncoding wxNativeFontInfo::GetEncoding() const
1244 {
1245 return m_encoding;
1246 }
1247
1248 bool wxNativeFontInfo::GetStrikethrough() const
1249 {
1250 return false;
1251 }
1252
1253
1254 // changing the font descriptor
1255
1256 void wxNativeFontInfo::SetPointSize(int pointsize)
1257 {
1258 if ( m_pointSize != pointsize )
1259 {
1260 m_pointSize = pointsize;
1261 Free();
1262 }
1263 }
1264
1265 void wxNativeFontInfo::SetStyle(wxFontStyle style_)
1266 {
1267 if ( m_style != style_ )
1268 {
1269 m_style = style_;
1270 Free();
1271 }
1272 }
1273
1274 void wxNativeFontInfo::SetWeight(wxFontWeight weight_)
1275 {
1276 if ( m_weight != weight_ )
1277 {
1278 m_weight = weight_;
1279 Free();
1280 }
1281 }
1282
1283 void wxNativeFontInfo::SetUnderlined(bool underlined_)
1284 {
1285 if ( m_underlined != underlined_ )
1286 {
1287 m_underlined = underlined_;
1288 Free();
1289 }
1290 }
1291
1292 bool wxNativeFontInfo::SetFaceName(const wxString& facename_)
1293 {
1294 if ( m_faceName != facename_ )
1295 {
1296 m_faceName = facename_;
1297 Free();
1298 }
1299 return true;
1300 }
1301
1302 void wxNativeFontInfo::SetFamily(wxFontFamily family_)
1303 {
1304 if ( m_family != family_ )
1305 {
1306 m_family = family_;
1307 Free();
1308 }
1309 }
1310
1311 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_)
1312 {
1313 if ( encoding_ == wxFONTENCODING_DEFAULT )
1314 encoding_ = wxFont::GetDefaultEncoding();
1315 m_encoding = encoding_;
1316 // not reflected in native descriptors
1317 }
1318
1319 void wxNativeFontInfo::SetStrikethrough(bool WXUNUSED(strikethrough))
1320 {
1321 }
1322
1323