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