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