]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/font.cpp
lowercase bools
[wxWidgets.git] / src / mac / carbon / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "font.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #include "wx/string.h"
19 #include "wx/font.h"
20 #include "wx/fontutil.h"
21 #include "wx/gdicmn.h"
22 #include "wx/utils.h"
23
24 #include "wx/fontutil.h"
25
26 #include "wx/mac/private.h"
27 #ifndef __DARWIN__
28 #include <ATSUnicode.h>
29 #endif
30
31 #if !USE_SHARED_LIBRARIES
32 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
33 #endif
34
35 class WXDLLEXPORT wxFontRefData: public wxGDIRefData
36 {
37 friend class WXDLLEXPORT wxFont;
38 public:
39 wxFontRefData()
40 : m_fontId(0)
41 , m_pointSize(10)
42 , m_family(wxDEFAULT)
43 , m_style(wxNORMAL)
44 , m_weight(wxNORMAL)
45 , m_underlined(FALSE)
46 , m_faceName(wxT("applicationfont"))
47 , m_encoding(wxFONTENCODING_DEFAULT)
48 , m_macFontNum(0)
49 , m_macFontSize(0)
50 , m_macFontStyle(0)
51 , m_macATSUStyle(0)
52 , m_macATSUFontID(0)
53 {
54 Init(m_pointSize, m_family, m_style, m_weight,
55 m_underlined, m_faceName, m_encoding);
56 }
57
58 wxFontRefData(const wxFontRefData& data)
59 : wxGDIRefData()
60 , m_fontId(data.m_fontId)
61 , m_pointSize(data.m_pointSize)
62 , m_family(data.m_family)
63 , m_style(data.m_style)
64 , m_weight(data.m_weight)
65 , m_underlined(data.m_underlined)
66 , m_faceName(data.m_faceName)
67 , m_encoding(data.m_encoding)
68 , m_macFontNum(data.m_macFontNum)
69 , m_macFontSize(data.m_macFontSize)
70 , m_macFontStyle(data.m_macFontStyle)
71 , m_macATSUStyle(0)
72 , m_macATSUFontID(data.m_macATSUFontID)
73 {
74 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
75 data.m_underlined, data.m_faceName, data.m_encoding);
76 }
77
78 wxFontRefData(int size,
79 int family,
80 int style,
81 int weight,
82 bool underlined,
83 const wxString& faceName,
84 wxFontEncoding encoding)
85 : m_fontId(0)
86 , m_pointSize(size)
87 , m_family(family)
88 , m_style(style)
89 , m_weight(weight)
90 , m_underlined(underlined)
91 , m_faceName(faceName)
92 , m_encoding(encoding)
93 , m_macFontNum(0)
94 , m_macFontSize(0)
95 , m_macFontStyle(0)
96 , m_macATSUStyle(0)
97 , m_macATSUFontID(0)
98 {
99 Init(size, family, style, weight, underlined, faceName, encoding);
100 }
101
102 virtual ~wxFontRefData();
103 void SetNoAntiAliasing( bool no = TRUE ) { m_noAA = no; }
104 bool GetNoAntiAliasing() const { return m_noAA; }
105
106 protected:
107 // common part of all ctors
108 void Init(int size,
109 int family,
110 int style,
111 int weight,
112 bool underlined,
113 const wxString& faceName,
114 wxFontEncoding encoding);
115
116 // font characterstics
117 int m_fontId;
118 int m_pointSize;
119 int m_family;
120 int m_style;
121 int m_weight;
122 bool m_underlined;
123 wxString m_faceName;
124 wxFontEncoding m_encoding;
125 bool m_noAA; // No anti-aliasing
126
127 public:
128 short m_macFontNum;
129 short m_macFontSize;
130 Style m_macFontStyle;
131
132 // ATSU Font Information
133
134 // this is split into an ATSU font id that may
135 // contain some styles (special bold fonts etc) and
136 // these are the additional qd styles that are not
137 // included in the ATSU font id
138 ATSUStyle m_macATSUStyle ;
139 ATSUFontID m_macATSUFontID;
140 Style m_macATSUAdditionalQDStyles ;
141
142 // for true themeing support we must store the correct font
143 // information here, as this speeds up and optimizes rendering
144 ThemeFontID m_macThemeFontID ;
145
146 wxNativeFontInfo m_info;
147
148 public:
149 void MacFindFont() ;
150 };
151 // ============================================================================
152 // implementation
153 // ============================================================================
154
155 // ----------------------------------------------------------------------------
156 // wxFontRefData
157 // ----------------------------------------------------------------------------
158
159 void wxFontRefData::Init(int pointSize,
160 int family,
161 int style,
162 int weight,
163 bool underlined,
164 const wxString& faceName,
165 wxFontEncoding encoding)
166 {
167 m_style = style;
168 m_pointSize = pointSize;
169 m_family = family;
170 m_style = style;
171 m_weight = weight;
172 m_underlined = underlined;
173 m_faceName = faceName;
174 m_encoding = encoding;
175
176 m_macFontNum = 0 ;
177 m_macFontSize = 0;
178 m_macFontStyle = 0;
179 m_macATSUFontID = 0;
180 m_macATSUAdditionalQDStyles = 0 ;
181 m_macATSUStyle = NULL ;
182
183 m_macThemeFontID = kThemeCurrentPortFont ;
184 m_noAA = FALSE;
185 }
186
187 wxFontRefData::~wxFontRefData()
188 {
189 if ( m_macATSUStyle )
190 {
191 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
192 m_macATSUStyle = NULL ;
193 }
194 }
195
196 void wxFontRefData::MacFindFont()
197 {
198 if ( m_macThemeFontID != kThemeCurrentPortFont )
199 {
200 Str255 fontName ;
201 GetThemeFont(m_macThemeFontID , GetApplicationScript() , fontName , &m_macFontSize , &m_macFontStyle ) ;
202 m_faceName = wxMacMakeStringFromPascal( fontName ) ;
203 if ( m_macFontStyle & bold )
204 m_weight = wxBOLD ;
205 else
206 m_weight = wxNORMAL ;
207 if ( m_macFontStyle & italic )
208 m_style = wxITALIC ;
209 if ( m_macFontStyle & underline )
210 m_underlined = true ;
211 ::GetFNum( fontName, &m_macFontNum);
212 m_pointSize = m_macFontSize ;
213 }
214 else
215 {
216 if( m_faceName.Length() == 0 )
217 {
218 switch( m_family )
219 {
220 case wxDEFAULT :
221 m_macFontNum = ::GetAppFont() ;
222 break ;
223 case wxSCRIPT :
224 case wxROMAN :
225 case wxDECORATIVE :
226 #ifdef __WXMAC_OSX__
227 ::GetFNum( "\pTimes" , &m_macFontNum) ;
228 #else
229 ::GetFNum( "\pTimes" , &m_macFontNum) ;
230 #endif
231 break ;
232 case wxSWISS :
233 #ifdef __WXMAC_OSX__
234 ::GetFNum( "\pLucida Grande" , &m_macFontNum) ;
235 #else
236 ::GetFNum( "\pGeneva" , &m_macFontNum) ;
237 #endif
238 break ;
239 case wxMODERN :
240 #ifdef __WXMAC_OSX__
241 ::GetFNum( "\pMonaco" , &m_macFontNum) ;
242 #else
243 ::GetFNum( "\pMonaco" , &m_macFontNum) ;
244 #endif
245 break ;
246 }
247 Str255 name ;
248 ::GetFontName( m_macFontNum , name ) ;
249 m_faceName = wxMacMakeStringFromPascal( name ) ;
250 }
251 else
252 {
253 if ( m_faceName == wxT("systemfont") )
254 m_macFontNum = ::GetSysFont() ;
255 else if ( m_faceName == wxT("applicationfont") )
256 m_macFontNum = ::GetAppFont() ;
257 else
258 {
259 Str255 fontname ;
260 wxMacStringToPascal( m_faceName , fontname ) ;
261 ::GetFNum( fontname, &m_macFontNum);
262 }
263 }
264
265 m_macFontStyle = 0;
266 if (m_weight == wxBOLD)
267 m_macFontStyle |= bold;
268 if (m_style == wxITALIC || m_style == wxSLANT)
269 m_macFontStyle |= italic;
270 if (m_underlined)
271 m_macFontStyle |= underline;
272 m_macFontSize = m_pointSize ;
273 }
274
275 // we try to get as much styles as possible into ATSU
276
277 Fixed atsuSize = IntToFixed( m_macFontSize ) ;
278
279 Style atsuStyle = normal ;
280 verify_noerr(::ATSUFONDtoFontID(m_macFontNum, atsuStyle , (UInt32*)&m_macATSUFontID) );
281 if ( m_macFontStyle & bold )
282 {
283 ATSUFontID test ;
284 if ( ::ATSUFONDtoFontID(m_macFontNum, atsuStyle | bold , &test) == noErr )
285 {
286 atsuStyle |= bold ;
287 m_macATSUFontID = test ;
288 }
289 }
290 if ( m_macFontStyle & italic )
291 {
292 ATSUFontID test ;
293 if ( ::ATSUFONDtoFontID(m_macFontNum, atsuStyle | italic , &test) == noErr )
294 {
295 atsuStyle |= italic ;
296 m_macATSUFontID = test ;
297 }
298 }
299 if ( m_macFontStyle & underline )
300 {
301 ATSUFontID test ;
302 if ( ::ATSUFONDtoFontID(m_macFontNum, atsuStyle | underline , &test) == noErr )
303 {
304 atsuStyle |= underline ;
305 m_macATSUFontID = test ;
306 }
307 }
308
309 m_macATSUAdditionalQDStyles = m_macFontStyle & (~atsuStyle ) ;
310
311 if ( m_macATSUStyle )
312 {
313 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
314 m_macATSUStyle = NULL ;
315 }
316 OSStatus status = ::ATSUCreateStyle((ATSUStyle *)&m_macATSUStyle) ;
317 wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") ) ;
318 ATSUAttributeTag atsuTags[] =
319 {
320 kATSUFontTag ,
321 kATSUSizeTag ,
322 kATSUVerticalCharacterTag,
323 kATSUQDBoldfaceTag ,
324 kATSUQDItalicTag ,
325 kATSUQDUnderlineTag ,
326 kATSUQDCondensedTag ,
327 kATSUQDExtendedTag ,
328 } ;
329 ByteCount atsuSizes[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
330 {
331 sizeof( ATSUFontID ) ,
332 sizeof( Fixed ) ,
333 sizeof( ATSUVerticalCharacterType),
334 sizeof( Boolean ) ,
335 sizeof( Boolean ) ,
336 sizeof( Boolean ) ,
337 sizeof( Boolean ) ,
338 sizeof( Boolean ) ,
339 } ;
340 Boolean kTrue = true ;
341 Boolean kFalse = false ;
342
343 ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
344 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
345 {
346 &m_macATSUFontID ,
347 &atsuSize ,
348 &kHorizontal,
349 (m_macATSUAdditionalQDStyles & bold) ? &kTrue : &kFalse ,
350 (m_macATSUAdditionalQDStyles & italic) ? &kTrue : &kFalse ,
351 (m_macATSUAdditionalQDStyles & underline) ? &kTrue : &kFalse ,
352 (m_macATSUAdditionalQDStyles & condense) ? &kTrue : &kFalse ,
353 (m_macATSUAdditionalQDStyles & extend) ? &kTrue : &kFalse ,
354 } ;
355 status = ::ATSUSetAttributes((ATSUStyle)m_macATSUStyle, sizeof(atsuTags)/sizeof(ATSUAttributeTag) ,
356 atsuTags, atsuSizes, atsuValues);
357
358 wxASSERT_MSG( status == noErr , wxT("couldn't Modify ATSU style") ) ;
359 }
360
361 // ----------------------------------------------------------------------------
362 // wxFont
363 // ----------------------------------------------------------------------------
364
365 void wxFont::Init()
366 {
367 }
368
369 bool wxFont::Create(const wxNativeFontInfo& info)
370 {
371 return Create(info.pointSize, info.family, info.style, info.weight,
372 info.underlined, info.faceName, info.encoding);
373 }
374
375 wxFont::wxFont(const wxString& fontdesc)
376 {
377 wxNativeFontInfo info;
378 if ( info.FromString(fontdesc) )
379 (void)Create(info);
380 }
381
382 bool wxFont::Create(int pointSize,
383 int family,
384 int style,
385 int weight,
386 bool underlined,
387 const wxString& faceName,
388 wxFontEncoding encoding)
389 {
390 UnRef();
391 m_refData = new wxFontRefData(pointSize, family, style, weight,
392 underlined, faceName, encoding);
393
394 RealizeResource();
395
396 return TRUE;
397 }
398
399 bool wxFont::MacCreateThemeFont(wxUint16 themeFontID )
400 {
401 UnRef();
402 m_refData = new wxFontRefData(12, wxDEFAULT , wxFONTSTYLE_NORMAL , wxFONTWEIGHT_NORMAL ,false, wxEmptyString, wxFONTENCODING_DEFAULT);
403 M_FONTDATA->m_macThemeFontID = themeFontID ;
404 RealizeResource();
405
406 return TRUE;
407 }
408
409 wxFont::~wxFont()
410 {
411 }
412
413 bool wxFont::RealizeResource()
414 {
415 M_FONTDATA->MacFindFont() ;
416 return TRUE;
417 }
418
419 void wxFont::SetEncoding(wxFontEncoding encoding)
420 {
421 Unshare();
422
423 M_FONTDATA->m_encoding = encoding;
424
425 RealizeResource();
426 }
427
428 void wxFont::Unshare()
429 {
430 // Don't change shared data
431 if (!m_refData)
432 {
433 m_refData = new wxFontRefData();
434 }
435 else
436 {
437 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
438 UnRef();
439 m_refData = ref;
440 }
441 }
442
443 void wxFont::SetPointSize(int pointSize)
444 {
445 Unshare();
446
447 M_FONTDATA->m_pointSize = pointSize;
448
449 RealizeResource();
450 }
451
452 void wxFont::SetFamily(int family)
453 {
454 Unshare();
455
456 M_FONTDATA->m_family = family;
457
458 RealizeResource();
459 }
460
461 void wxFont::SetStyle(int style)
462 {
463 Unshare();
464
465 M_FONTDATA->m_style = style;
466
467 RealizeResource();
468 }
469
470 void wxFont::SetWeight(int weight)
471 {
472 Unshare();
473
474 M_FONTDATA->m_weight = weight;
475
476 RealizeResource();
477 }
478
479 void wxFont::SetFaceName(const wxString& faceName)
480 {
481 Unshare();
482
483 M_FONTDATA->m_faceName = faceName;
484
485 RealizeResource();
486 }
487
488 void wxFont::SetUnderlined(bool underlined)
489 {
490 Unshare();
491
492 M_FONTDATA->m_underlined = underlined;
493
494 RealizeResource();
495 }
496
497 void wxFont::SetNoAntiAliasing( bool no )
498 {
499 Unshare();
500
501 M_FONTDATA->SetNoAntiAliasing( no );
502
503 RealizeResource();
504 }
505
506 // ----------------------------------------------------------------------------
507 // accessors
508 // ----------------------------------------------------------------------------
509
510 // TODO: insert checks everywhere for M_FONTDATA == NULL!
511
512 int wxFont::GetPointSize() const
513 {
514 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
515 return M_FONTDATA->m_pointSize;
516 }
517
518 int wxFont::GetFamily() const
519 {
520 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
521 return M_FONTDATA->m_family;
522 }
523
524 int wxFont::GetStyle() const
525 {
526 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
527 return M_FONTDATA->m_style;
528 }
529
530 int wxFont::GetWeight() const
531 {
532 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
533 return M_FONTDATA->m_weight;
534 }
535
536 bool wxFont::GetUnderlined() const
537 {
538 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
539 return M_FONTDATA->m_underlined;
540 }
541
542 wxString wxFont::GetFaceName() const
543 {
544 wxCHECK_MSG( M_FONTDATA != NULL , wxEmptyString , wxT("invalid font") );
545 return M_FONTDATA->m_faceName;
546 }
547
548 wxFontEncoding wxFont::GetEncoding() const
549 {
550 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTENCODING_DEFAULT , wxT("invalid font") );
551 return M_FONTDATA->m_encoding;
552 }
553
554 bool wxFont::GetNoAntiAliasing() const
555 {
556 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
557 return M_FONTDATA->m_noAA;
558 }
559
560 short wxFont::MacGetFontNum() const
561 {
562 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
563 return M_FONTDATA->m_macFontNum;
564 }
565
566 short wxFont::MacGetFontSize() const
567 {
568 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
569 return M_FONTDATA->m_macFontSize;
570 }
571
572 wxByte wxFont::MacGetFontStyle() const
573 {
574 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
575 return M_FONTDATA->m_macFontStyle;
576 }
577
578 wxUint32 wxFont::MacGetATSUFontID() const
579 {
580 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
581 return M_FONTDATA->m_macATSUFontID;
582 }
583
584 void* wxFont::MacGetATSUStyle() const
585 {
586 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
587 return M_FONTDATA->m_macATSUStyle;
588 }
589
590 wxUint32 wxFont::MacGetATSUAdditionalQDStyles() const
591 {
592 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
593 return M_FONTDATA->m_macATSUAdditionalQDStyles;
594 }
595
596 wxUint16 wxFont::MacGetThemeFontID() const
597 {
598 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
599 return M_FONTDATA->m_macThemeFontID;
600 }
601
602
603 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
604 {
605 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
606 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
607
608 M_FONTDATA->m_info.InitFromFont(*this);
609
610 return &(M_FONTDATA->m_info);
611 }
612