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