]>
Commit | Line | Data |
---|---|---|
489468fe SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/mac/carbon/font.cpp | |
3 | // Purpose: wxFont class | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #include "wx/font.h" | |
15 | ||
16 | #ifndef WX_PRECOMP | |
17 | #include "wx/string.h" | |
18 | #include "wx/utils.h" | |
19 | #include "wx/intl.h" | |
20 | #include "wx/gdicmn.h" | |
21 | #include "wx/log.h" | |
22 | #endif | |
23 | ||
24 | #include "wx/fontutil.h" | |
25 | #include "wx/graphics.h" | |
26 | #include "wx/settings.h" | |
27 | ||
b2680ced | 28 | #if wxOSX_USE_CARBON |
1f0c8f31 | 29 | #include "wx/osx/uma.h" |
b2680ced SC |
30 | #else |
31 | #include "wx/osx/private.h" | |
32 | #endif | |
489468fe SC |
33 | |
34 | #ifndef __DARWIN__ | |
35 | #include <ATSUnicode.h> | |
36 | #endif | |
37 | ||
38 | #include <map> | |
39 | #include <string> | |
40 | ||
41 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) | |
42 | ||
43 | ||
44 | class WXDLLEXPORT wxFontRefData: public wxGDIRefData | |
45 | { | |
46 | public: | |
47 | wxFontRefData() | |
48 | { | |
49 | Init(10, wxDEFAULT, wxNORMAL, wxNORMAL, | |
50 | false, wxT("applicationfont"), wxFONTENCODING_DEFAULT); | |
51 | } | |
52 | ||
53 | wxFontRefData(const wxFontRefData& data) | |
54 | { | |
55 | Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight, | |
56 | data.m_underlined, data.m_faceName, data.m_encoding); | |
57 | } | |
58 | ||
59 | wxFontRefData(int size, | |
60 | int family, | |
61 | int style, | |
62 | int weight, | |
63 | bool underlined, | |
64 | const wxString& faceName, | |
65 | wxFontEncoding encoding) | |
66 | { | |
67 | Init(size, family, style, weight, underlined, faceName, encoding); | |
68 | } | |
69 | ||
292e5e1f | 70 | #if wxOSX_USE_CORE_TEXT |
489468fe SC |
71 | wxFontRefData( wxUint32 coreTextFontType ); |
72 | wxFontRefData( CTFontRef font ); | |
73 | wxFontRefData( CTFontDescriptorRef fontdescriptor, int size ); | |
74 | #endif | |
75 | ||
76 | virtual ~wxFontRefData(); | |
77 | ||
78 | void SetNoAntiAliasing( bool no = true ) { m_noAA = no; } | |
79 | ||
80 | bool GetNoAntiAliasing() const { return m_noAA; } | |
81 | ||
82 | void SetPointSize( int size ) | |
83 | { | |
84 | m_pointSize = size; | |
85 | MacInvalidateNativeFont(); | |
86 | } | |
87 | ||
88 | int GetPointSize() const { return m_pointSize; } | |
89 | ||
90 | void SetFamily( int family ) | |
91 | { | |
92 | m_family = family; | |
93 | MacInvalidateNativeFont(); | |
94 | } | |
95 | ||
96 | ||
97 | int GetFamily() const { return m_family; } | |
98 | ||
99 | void SetStyle( int style ) | |
100 | { | |
101 | m_style = style; | |
102 | MacInvalidateNativeFont(); | |
103 | } | |
104 | ||
105 | ||
106 | int GetStyle() const { return m_style; } | |
107 | ||
108 | void SetWeight( int weight ) | |
109 | { | |
110 | m_weight = weight; | |
111 | MacInvalidateNativeFont(); | |
112 | } | |
113 | ||
114 | ||
115 | int GetWeight() const { return m_weight; } | |
116 | ||
117 | void SetUnderlined( bool u ) | |
118 | { | |
119 | m_underlined = u; | |
120 | MacInvalidateNativeFont(); | |
121 | } | |
122 | ||
123 | bool GetUnderlined() const { return m_underlined; } | |
124 | ||
125 | void SetFaceName( const wxString& facename ) | |
126 | { | |
127 | m_faceName = facename; | |
128 | MacInvalidateNativeFont(); | |
129 | } | |
130 | ||
131 | const wxString& GetFaceName() const { return m_faceName; } | |
132 | ||
133 | void SetEncoding( wxFontEncoding encoding ) | |
134 | { | |
135 | m_encoding = encoding; | |
136 | MacInvalidateNativeFont(); | |
137 | } | |
138 | ||
139 | wxFontEncoding GetEncoding() const { return m_encoding; } | |
140 | ||
141 | void MacInvalidateNativeFont(); | |
142 | ||
143 | void MacFindFont(); | |
144 | ||
145 | protected: | |
146 | // common part of all ctors | |
147 | void Init(int size, | |
148 | int family, | |
149 | int style, | |
150 | int weight, | |
151 | bool underlined, | |
152 | const wxString& faceName, | |
153 | wxFontEncoding encoding); | |
154 | ||
292e5e1f | 155 | #if wxOSX_USE_CORE_TEXT |
489468fe SC |
156 | void Init( CTFontRef font ); |
157 | #endif | |
158 | // font characterstics | |
159 | int m_pointSize; | |
160 | int m_family; | |
161 | int m_style; | |
162 | int m_weight; | |
163 | bool m_underlined; | |
164 | wxString m_faceName; | |
165 | wxFontEncoding m_encoding; | |
166 | bool m_noAA; // No anti-aliasing | |
167 | ||
168 | public: | |
292e5e1f | 169 | #if wxOSX_USE_ATSU_TEXT |
489468fe SC |
170 | FMFontFamily m_macFontFamily; |
171 | FMFontSize m_macFontSize; | |
172 | FMFontStyle m_macFontStyle; | |
173 | ||
174 | // ATSU Font Information | |
175 | ||
176 | // this is split into an ATSU font id that may | |
177 | // contain some styles (special bold fonts etc) and | |
178 | // these are the additional qd styles that are not | |
179 | // included in the ATSU font id | |
180 | ATSUFontID m_macATSUFontID; | |
181 | FMFontStyle m_macATSUAdditionalQDStyles ; | |
182 | ||
183 | // for true themeing support we must store the correct font | |
184 | // information here, as this speeds up and optimizes rendering | |
185 | ThemeFontID m_macThemeFontID ; | |
186 | #endif | |
292e5e1f | 187 | #if wxOSX_USE_CORE_TEXT |
489468fe SC |
188 | wxCFRef<CTFontRef> m_ctFont; |
189 | wxCFRef<CTFontDescriptorRef> m_ctFontDescriptor; | |
190 | #endif | |
292e5e1f | 191 | #if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT |
489468fe SC |
192 | ATSUStyle m_macATSUStyle ; |
193 | #endif | |
194 | wxNativeFontInfo m_info; | |
195 | }; | |
196 | ||
197 | #define M_FONTDATA ((wxFontRefData*)m_refData) | |
198 | ||
199 | ||
200 | // ============================================================================ | |
201 | // implementation | |
202 | // ============================================================================ | |
203 | ||
204 | // ---------------------------------------------------------------------------- | |
205 | // wxFontRefData | |
206 | // ---------------------------------------------------------------------------- | |
207 | ||
208 | void wxFontRefData::Init(int pointSize, | |
209 | int family, | |
210 | int style, | |
211 | int weight, | |
212 | bool underlined, | |
213 | const wxString& faceName, | |
214 | wxFontEncoding encoding) | |
215 | { | |
216 | m_style = style; | |
217 | m_pointSize = (pointSize == -1) ? wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT).GetPointSize() : pointSize; | |
218 | m_family = family; | |
219 | m_style = style; | |
220 | m_weight = weight; | |
221 | m_underlined = underlined; | |
222 | m_faceName = faceName; | |
223 | m_encoding = encoding; | |
224 | m_noAA = false; | |
292e5e1f | 225 | #if wxOSX_USE_ATSU_TEXT |
489468fe SC |
226 | m_macFontFamily = 0 ; |
227 | m_macFontSize = 0; | |
228 | m_macFontStyle = 0; | |
229 | m_macATSUFontID = 0; | |
230 | m_macATSUAdditionalQDStyles = 0 ; | |
231 | m_macThemeFontID = kThemeCurrentPortFont ; | |
232 | #endif | |
292e5e1f | 233 | #if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT |
489468fe SC |
234 | m_macATSUStyle = NULL ; |
235 | #endif | |
236 | } | |
237 | ||
238 | wxFontRefData::~wxFontRefData() | |
239 | { | |
292e5e1f | 240 | #if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT |
489468fe SC |
241 | if ( m_macATSUStyle ) |
242 | { | |
243 | ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle); | |
244 | m_macATSUStyle = NULL ; | |
245 | } | |
246 | #endif | |
247 | } | |
248 | ||
249 | void wxFontRefData::MacInvalidateNativeFont() | |
250 | { | |
292e5e1f | 251 | #if wxOSX_USE_CORE_TEXT |
489468fe SC |
252 | m_ctFont.reset(); |
253 | m_ctFontDescriptor.reset(); | |
254 | #endif | |
292e5e1f | 255 | #if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT |
489468fe SC |
256 | if ( m_macATSUStyle ) |
257 | { | |
258 | ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle); | |
259 | m_macATSUStyle = NULL ; | |
260 | } | |
261 | #endif | |
262 | } | |
263 | ||
292e5e1f | 264 | #if wxOSX_USE_CORE_TEXT |
489468fe SC |
265 | |
266 | /* from Core Text Manual Common Operations */ | |
267 | ||
268 | static CTFontDescriptorRef wxMacCreateCTFontDescriptor(CFStringRef iFamilyName, CTFontSymbolicTraits iTraits ) | |
269 | { | |
270 | CTFontDescriptorRef descriptor = NULL; | |
271 | CFMutableDictionaryRef attributes; | |
272 | ||
273 | assert(iFamilyName != NULL); | |
274 | // Create a mutable dictionary to hold our attributes. | |
275 | attributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, | |
276 | &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); | |
277 | check(attributes != NULL); | |
278 | ||
279 | if (attributes != NULL) { | |
280 | // Add a family name to our attributes. | |
281 | CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, iFamilyName); | |
282 | ||
283 | ||
284 | if ( iTraits ) { | |
285 | CFMutableDictionaryRef traits; | |
286 | CFNumberRef symTraits; | |
287 | ||
288 | // Create the traits dictionary. | |
289 | symTraits = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, | |
290 | &iTraits); | |
291 | check(symTraits != NULL); | |
292 | ||
293 | if (symTraits != NULL) { | |
294 | // Create a dictionary to hold our traits values. | |
295 | traits = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, | |
296 | &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); | |
297 | check(traits != NULL); | |
298 | ||
299 | if (traits != NULL) { | |
300 | // Add the symbolic traits value to the traits dictionary. | |
301 | CFDictionaryAddValue(traits, kCTFontSymbolicTrait, symTraits); | |
302 | ||
303 | // Add the traits attribute to our attributes. | |
304 | CFDictionaryAddValue(attributes, kCTFontTraitsAttribute, traits); | |
305 | CFRelease(traits); | |
306 | } | |
307 | CFRelease(symTraits); | |
308 | } | |
309 | } | |
310 | // Create the font descriptor with our attributes | |
311 | descriptor = CTFontDescriptorCreateWithAttributes(attributes); | |
312 | check(descriptor != NULL); | |
313 | ||
314 | CFRelease(attributes); | |
315 | } | |
316 | // Return our font descriptor. | |
317 | return descriptor ; | |
318 | } | |
319 | ||
320 | wxFontRefData::wxFontRefData( wxUint32 coreTextFontType ) | |
321 | { | |
322 | CTFontRef font = CTFontCreateUIFontForLanguage( coreTextFontType, 0.0, NULL ) ; | |
cde80c92 | 323 | if ( CTFontGetSize(font) == 0 ) |
489468fe SC |
324 | { |
325 | CFRelease(font); | |
326 | font = CTFontCreateUIFontForLanguage( coreTextFontType, 12.0, NULL ); | |
327 | } | |
328 | Init( font ); | |
329 | } | |
330 | ||
331 | wxFontRefData::wxFontRefData( CTFontRef font ) | |
332 | { | |
333 | Init( font ); | |
334 | } | |
335 | ||
336 | wxFontRefData::wxFontRefData( CTFontDescriptorRef fontdescriptor, int size ) | |
337 | { | |
338 | if ( size == 0 ) | |
339 | { | |
340 | wxCFRef< CFNumberRef > value( (CFNumberRef) CTFontDescriptorCopyAttribute( fontdescriptor, kCTFontSizeAttribute ) ); | |
341 | ||
342 | float fsize; | |
343 | if ( CFNumberGetValue( value , kCFNumberFloatType , &fsize ) ) | |
344 | { | |
345 | size = (int) fsize + 0.5 ; | |
346 | } | |
347 | } | |
348 | Init( CTFontCreateWithFontDescriptor(fontdescriptor, size,NULL) ); | |
349 | } | |
350 | ||
351 | void wxFontRefData::Init( CTFontRef font ) | |
352 | { | |
353 | Init(10, wxDEFAULT, wxNORMAL, wxNORMAL, | |
354 | false, wxT("applicationfont"), wxFONTENCODING_DEFAULT); | |
355 | ||
356 | m_ctFont.reset( font ); | |
357 | } | |
358 | ||
359 | #endif | |
360 | ||
361 | void wxFontRefData::MacFindFont() | |
362 | { | |
363 | ||
292e5e1f | 364 | #if wxOSX_USE_CORE_TEXT |
489468fe SC |
365 | if ( UMAGetSystemVersion() >= 0x1050 ) |
366 | { | |
367 | if ( m_faceName.empty() && m_family == wxDEFAULT ) | |
368 | { | |
369 | m_ctFont.reset(CTFontCreateUIFontForLanguage( kCTFontSystemFontType, 0.0, NULL )); | |
370 | } | |
371 | ||
372 | if ( m_ctFont ) | |
373 | { | |
374 | wxCFStringRef name( CTFontCopyFamilyName( m_ctFont ) ); | |
375 | m_faceName = name.AsString(); | |
376 | m_pointSize = CTFontGetSize(m_ctFont) ; | |
377 | CTFontSymbolicTraits traits = CTFontGetSymbolicTraits( m_ctFont ); | |
378 | if ( traits & kCTFontItalicTrait ) | |
379 | m_style = wxITALIC; | |
380 | if ( traits & kCTFontBoldTrait ) | |
381 | m_weight = wxBOLD ; | |
382 | if ( !m_ctFontDescriptor.get() ) | |
383 | m_ctFontDescriptor.reset( CTFontCopyFontDescriptor( m_ctFont ) ); | |
384 | } | |
385 | else | |
386 | { | |
387 | if ( m_faceName.empty() ) | |
388 | { | |
389 | switch ( m_family ) | |
390 | { | |
391 | case wxSCRIPT : | |
392 | case wxROMAN : | |
393 | case wxDECORATIVE : | |
394 | m_faceName = wxT("Times"); | |
395 | break ; | |
396 | ||
397 | case wxSWISS : | |
92b6654b | 398 | m_faceName = wxT("Helvetica"); |
489468fe SC |
399 | break ; |
400 | ||
401 | case wxMODERN : | |
402 | case wxTELETYPE: | |
92b6654b | 403 | m_faceName = wxT("Courier"); |
489468fe SC |
404 | break ; |
405 | ||
406 | default: | |
407 | m_faceName = wxT("Times"); | |
408 | break ; | |
409 | } | |
410 | } | |
411 | ||
412 | ||
413 | CTFontSymbolicTraits traits = 0; | |
414 | ||
415 | if (m_weight == wxBOLD) | |
416 | traits |= kCTFontBoldTrait; | |
417 | if (m_style == wxITALIC || m_style == wxSLANT) | |
418 | traits |= kCTFontItalicTrait; | |
419 | ||
420 | // use font descriptor caching | |
b2680ced | 421 | #if 0 |
489468fe SC |
422 | wxString lookupname = wxString::Format( "%s_%ld", m_faceName.c_str(), traits ); |
423 | ||
424 | static std::map< std::wstring , wxCFRef< CTFontDescriptorRef > > fontdescriptorcache ; | |
425 | ||
426 | m_ctFontDescriptor = fontdescriptorcache[ std::wstring(lookupname.wc_str()) ]; | |
427 | if ( !m_ctFontDescriptor ) | |
428 | { | |
429 | wxCFStringRef cf( m_faceName, wxLocale::GetSystemEncoding() ); | |
430 | m_ctFontDescriptor.reset( wxMacCreateCTFontDescriptor( cf, traits ) ); | |
431 | fontdescriptorcache[ std::wstring(lookupname.wc_str()) ] = m_ctFontDescriptor; | |
432 | } | |
433 | #else | |
434 | wxCFStringRef cf( m_faceName, wxLocale::GetSystemEncoding() ); | |
435 | m_ctFontDescriptor.reset( wxMacCreateCTFontDescriptor( cf, traits ) ); | |
436 | #endif | |
437 | ||
438 | // use font caching | |
b2680ced | 439 | #if 0 |
489468fe SC |
440 | wxString lookupnameWithSize = wxString::Format( "%s_%ld_%ld", m_faceName.c_str(), traits, m_pointSize ); |
441 | ||
442 | static std::map< std::wstring , wxCFRef< CTFontRef > > fontcache ; | |
443 | m_ctFont = fontcache[ std::wstring(lookupnameWithSize.wc_str()) ]; | |
444 | if ( !m_ctFont ) | |
445 | { | |
446 | m_ctFont.reset( CTFontCreateWithFontDescriptor( m_ctFontDescriptor, m_pointSize, NULL ) ); | |
447 | fontcache[ std::wstring(lookupnameWithSize.wc_str()) ] = m_ctFont; | |
448 | } | |
449 | #else | |
450 | m_ctFont.reset( CTFontCreateWithFontDescriptor( m_ctFontDescriptor, m_pointSize, NULL ) ); | |
451 | #endif | |
b2680ced SC |
452 | if ( /* (CTFontGetSymbolicTraits( m_ctFont ) & 0x03) !=*/ traits ) |
453 | { | |
454 | CTFontRef font = CTFontCreateWithName( cf, m_pointSize, NULL ); | |
455 | CTFontRef font2 = CTFontCreateCopyWithSymbolicTraits( font, m_pointSize, NULL, traits, 0x03 ); | |
456 | CFRelease(font); | |
457 | m_ctFont.reset( font2 ); | |
458 | if ( (CTFontGetSymbolicTraits( m_ctFont ) & 0x03) != traits ) | |
459 | { | |
460 | wxMessageBox( wxString::Format( "expected %d but got %d traits" , traits, (CTFontGetSymbolicTraits( m_ctFont ) & 0x03) ) ); | |
461 | } | |
462 | } | |
489468fe | 463 | } |
292e5e1f | 464 | #if wxOSX_USE_ATSU_TEXT |
489468fe SC |
465 | OSStatus status = noErr; |
466 | CTFontDescriptorRef desc = m_ctFontDescriptor ; | |
467 | ATSFontRef atsfont = CTFontGetPlatformFont( m_ctFont, &desc ); | |
468 | FMFont fmfont = FMGetFontFromATSFontRef( atsfont ); | |
469 | ATSUAttributeTag atsuTags[] = | |
470 | { | |
471 | kATSUFontTag , | |
472 | kATSUSizeTag , | |
473 | kATSUVerticalCharacterTag, | |
474 | kATSUQDBoldfaceTag , | |
475 | kATSUQDItalicTag , | |
476 | kATSUQDUnderlineTag , | |
477 | }; | |
478 | ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = | |
479 | { | |
480 | sizeof( ATSUFontID ) , | |
481 | sizeof( Fixed ) , | |
482 | sizeof( ATSUVerticalCharacterType), | |
483 | sizeof( Boolean ) , | |
484 | sizeof( Boolean ) , | |
485 | sizeof( Boolean ) , | |
486 | }; | |
487 | Boolean kTrue = true ; | |
488 | Boolean kFalse = false ; | |
489 | ||
490 | Fixed atsuSize = IntToFixed( m_pointSize ); | |
491 | ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal; | |
492 | ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = | |
493 | { | |
494 | &fmfont , | |
495 | &atsuSize , | |
496 | &kHorizontal, | |
497 | (m_weight == wxBOLD) ? &kTrue : &kFalse , | |
498 | (m_style == wxITALIC || m_style == wxSLANT) ? &kTrue : &kFalse , | |
499 | (m_underlined) ? &kTrue : &kFalse , | |
500 | }; | |
501 | ||
502 | if ( m_macATSUStyle ) | |
503 | { | |
504 | ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle); | |
505 | m_macATSUStyle = NULL ; | |
506 | } | |
507 | status = ::ATSUCreateStyle((ATSUStyle *)&m_macATSUStyle); | |
508 | wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") ); | |
509 | status = ::ATSUSetAttributes( | |
510 | (ATSUStyle)m_macATSUStyle, | |
511 | sizeof(atsuTags) / sizeof(ATSUAttributeTag) , | |
512 | atsuTags, atsuSizes, atsuValues); | |
513 | #endif | |
514 | } | |
515 | #endif | |
292e5e1f | 516 | #if wxOSX_USE_ATSU_TEXT |
489468fe SC |
517 | { |
518 | OSStatus status = noErr; | |
519 | Str255 qdFontName ; | |
520 | if ( m_macThemeFontID != kThemeCurrentPortFont ) | |
521 | { | |
522 | Style style ; | |
523 | GetThemeFont( m_macThemeFontID, GetApplicationScript(), qdFontName, &m_macFontSize, &style ); | |
524 | if ( m_macFontSize == 0 ) | |
525 | m_macFontSize = 12; | |
526 | m_macFontStyle = style ; | |
527 | m_faceName = wxMacMakeStringFromPascal( qdFontName ); | |
528 | if ( m_macFontStyle & bold ) | |
529 | m_weight = wxBOLD ; | |
530 | else | |
531 | m_weight = wxNORMAL ; | |
532 | if ( m_macFontStyle & italic ) | |
533 | m_style = wxITALIC ; | |
534 | if ( m_macFontStyle & underline ) | |
535 | m_underlined = true ; | |
536 | m_pointSize = m_macFontSize ; | |
537 | m_macFontFamily = FMGetFontFamilyFromName( qdFontName ); | |
538 | } | |
539 | else | |
540 | { | |
541 | if ( m_faceName.empty() ) | |
542 | { | |
543 | if ( m_family == wxDEFAULT ) | |
544 | { | |
545 | m_macFontFamily = GetAppFont(); | |
546 | FMGetFontFamilyName(m_macFontFamily,qdFontName); | |
547 | m_faceName = wxMacMakeStringFromPascal( qdFontName ); | |
548 | } | |
549 | else | |
550 | { | |
551 | switch ( m_family ) | |
552 | { | |
553 | case wxSCRIPT : | |
554 | case wxROMAN : | |
555 | case wxDECORATIVE : | |
556 | m_faceName = wxT("Times"); | |
557 | break ; | |
558 | ||
559 | case wxSWISS : | |
92b6654b | 560 | m_faceName = wxT("Helvetica"); |
489468fe SC |
561 | break ; |
562 | ||
563 | case wxMODERN : | |
564 | case wxTELETYPE: | |
92b6654b | 565 | m_faceName = wxT("Courier"); |
489468fe SC |
566 | break ; |
567 | ||
568 | default: | |
569 | m_faceName = wxT("Times"); | |
570 | break ; | |
571 | } | |
572 | wxMacStringToPascal( m_faceName , qdFontName ); | |
573 | m_macFontFamily = FMGetFontFamilyFromName( qdFontName ); | |
574 | if ( m_macFontFamily == kInvalidFontFamily ) | |
575 | { | |
576 | wxLogDebug( wxT("ATSFontFamilyFindFromName failed for %s"), m_faceName.c_str() ); | |
577 | m_macFontFamily = GetAppFont(); | |
578 | } | |
579 | } | |
580 | } | |
581 | else | |
582 | { | |
583 | if ( m_faceName == wxT("systemfont") ) | |
584 | m_macFontFamily = GetSysFont(); | |
585 | else if ( m_faceName == wxT("applicationfont") ) | |
586 | m_macFontFamily = GetAppFont(); | |
587 | else | |
588 | { | |
589 | wxCFStringRef cf( m_faceName, wxLocale::GetSystemEncoding() ); | |
590 | ATSFontFamilyRef atsfamily = ATSFontFamilyFindFromName( cf , kATSOptionFlagsDefault ); | |
591 | if ( atsfamily == (ATSFontFamilyRef) -1 ) | |
592 | { | |
593 | wxLogDebug( wxT("ATSFontFamilyFindFromName failed for ") + m_faceName ); | |
594 | m_macFontFamily = GetAppFont(); | |
595 | } | |
596 | else | |
597 | m_macFontFamily = FMGetFontFamilyFromATSFontFamilyRef( atsfamily ); | |
598 | } | |
599 | } | |
600 | ||
601 | m_macFontStyle = 0; | |
602 | if (m_weight == wxBOLD) | |
603 | m_macFontStyle |= bold; | |
604 | if (m_style == wxITALIC || m_style == wxSLANT) | |
605 | m_macFontStyle |= italic; | |
606 | if (m_underlined) | |
607 | m_macFontStyle |= underline; | |
608 | m_macFontSize = m_pointSize ; | |
609 | } | |
610 | ||
611 | // we try to get as much styles as possible into ATSU | |
612 | ||
613 | ||
614 | // ATSUFontID and FMFont are equivalent | |
615 | FMFontStyle intrinsicStyle = 0 ; | |
616 | status = FMGetFontFromFontFamilyInstance( m_macFontFamily , m_macFontStyle , &m_macATSUFontID , &intrinsicStyle); | |
617 | wxASSERT_MSG( status == noErr , wxT("couldn't get an ATSUFont from font family") ); | |
618 | m_macATSUAdditionalQDStyles = m_macFontStyle & (~intrinsicStyle ); | |
619 | ||
620 | if ( m_macATSUStyle ) | |
621 | { | |
622 | ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle); | |
623 | m_macATSUStyle = NULL ; | |
624 | } | |
625 | ||
626 | status = ::ATSUCreateStyle((ATSUStyle *)&m_macATSUStyle); | |
627 | wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") ); | |
628 | ||
629 | ATSUAttributeTag atsuTags[] = | |
630 | { | |
631 | kATSUFontTag , | |
632 | kATSUSizeTag , | |
633 | kATSUVerticalCharacterTag, | |
634 | kATSUQDBoldfaceTag , | |
635 | kATSUQDItalicTag , | |
636 | kATSUQDUnderlineTag , | |
637 | kATSUQDCondensedTag , | |
638 | kATSUQDExtendedTag , | |
639 | }; | |
640 | ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = | |
641 | { | |
642 | sizeof( ATSUFontID ) , | |
643 | sizeof( Fixed ) , | |
644 | sizeof( ATSUVerticalCharacterType), | |
645 | sizeof( Boolean ) , | |
646 | sizeof( Boolean ) , | |
647 | sizeof( Boolean ) , | |
648 | sizeof( Boolean ) , | |
649 | sizeof( Boolean ) , | |
650 | }; | |
651 | ||
652 | Boolean kTrue = true ; | |
653 | Boolean kFalse = false ; | |
654 | ||
655 | Fixed atsuSize = IntToFixed( m_macFontSize ); | |
656 | ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal; | |
657 | ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = | |
658 | { | |
659 | &m_macATSUFontID , | |
660 | &atsuSize , | |
661 | &kHorizontal, | |
662 | (m_macATSUAdditionalQDStyles & bold) ? &kTrue : &kFalse , | |
663 | (m_macATSUAdditionalQDStyles & italic) ? &kTrue : &kFalse , | |
664 | (m_macATSUAdditionalQDStyles & underline) ? &kTrue : &kFalse , | |
665 | (m_macATSUAdditionalQDStyles & condense) ? &kTrue : &kFalse , | |
666 | (m_macATSUAdditionalQDStyles & extend) ? &kTrue : &kFalse , | |
667 | }; | |
668 | ||
669 | status = ::ATSUSetAttributes( | |
670 | (ATSUStyle)m_macATSUStyle, | |
671 | sizeof(atsuTags) / sizeof(ATSUAttributeTag) , | |
672 | atsuTags, atsuSizes, atsuValues); | |
673 | ||
674 | wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") ); | |
675 | return; | |
676 | } | |
677 | #endif | |
678 | } | |
679 | ||
680 | // ---------------------------------------------------------------------------- | |
681 | // wxFont | |
682 | // ---------------------------------------------------------------------------- | |
683 | ||
684 | bool wxFont::Create(const wxNativeFontInfo& info) | |
685 | { | |
686 | return Create( | |
687 | info.pointSize, info.family, info.style, info.weight, | |
688 | info.underlined, info.faceName, info.encoding ); | |
689 | } | |
690 | ||
691 | wxFont::wxFont(const wxString& fontdesc) | |
692 | { | |
693 | wxNativeFontInfo info; | |
694 | if ( info.FromString(fontdesc) ) | |
695 | (void)Create(info); | |
696 | } | |
697 | ||
698 | bool wxFont::Create(int pointSize, | |
699 | int family, | |
700 | int style, | |
701 | int weight, | |
702 | bool underlined, | |
703 | const wxString& faceName, | |
704 | wxFontEncoding encoding) | |
705 | { | |
706 | UnRef(); | |
707 | ||
708 | m_refData = new wxFontRefData( | |
709 | pointSize, family, style, weight, | |
710 | underlined, faceName, encoding); | |
711 | ||
712 | RealizeResource(); | |
713 | ||
714 | return true; | |
715 | } | |
716 | ||
292e5e1f | 717 | #if wxOSX_USE_CORE_TEXT |
489468fe SC |
718 | |
719 | bool wxFont::MacCreateFromUIFont(wxUint32 ctFontType ) | |
720 | { | |
721 | UnRef(); | |
722 | ||
723 | m_refData = new wxFontRefData(ctFontType); | |
724 | RealizeResource(); | |
725 | ||
726 | return true; | |
727 | } | |
728 | ||
729 | bool wxFont::MacCreateFromCTFontDescriptor( const void * ctFontDescriptor , int size ) | |
730 | { | |
731 | UnRef(); | |
732 | ||
733 | m_refData = new wxFontRefData((CTFontDescriptorRef)ctFontDescriptor, size);; | |
734 | RealizeResource(); | |
735 | ||
736 | return true; | |
737 | } | |
738 | ||
739 | ||
740 | #endif | |
741 | ||
742 | bool wxFont::MacCreateFromThemeFont(wxUint16 themeFontID) | |
743 | { | |
292e5e1f | 744 | #if wxOSX_USE_CORE_TEXT |
489468fe SC |
745 | if ( UMAGetSystemVersion() >= 0x1050) |
746 | { | |
747 | return MacCreateFromUIFont(HIThemeGetUIFontType(themeFontID)); | |
748 | } | |
749 | #endif | |
292e5e1f | 750 | #if wxOSX_USE_ATSU_TEXT |
489468fe SC |
751 | { |
752 | UnRef(); | |
753 | ||
754 | m_refData = new wxFontRefData( | |
755 | 12, wxDEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, | |
756 | false, wxEmptyString, wxFONTENCODING_DEFAULT ); | |
757 | ||
758 | M_FONTDATA->m_macThemeFontID = themeFontID ; | |
759 | RealizeResource(); | |
760 | return true; | |
761 | } | |
762 | #endif | |
763 | return false; | |
764 | } | |
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 | ||
292e5e1f | 956 | #if wxOSX_USE_ATSU_TEXT |
489468fe SC |
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 | ||
292e5e1f | 1001 | #if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT |
489468fe SC |
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 | ||
292e5e1f | 1010 | #if wxOSX_USE_CORE_TEXT |
489468fe SC |
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 | } |