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