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