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