]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/font.cpp
Include wx/arrstr.h according to precompiled headers of wx/wx.h (with other minor...
[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 #endif
21
22 #include "wx/fontutil.h"
23 #include "wx/gdicmn.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
224 m_macFontFamily = FMGetFontFamilyFromName( qdFontName );
225 }
226 else
227 {
228 if ( m_faceName.empty() )
229 {
230 if ( m_family == wxDEFAULT )
231 {
232 m_macFontFamily = GetAppFont();
233 FMGetFontFamilyName(m_macFontFamily,qdFontName);
234 m_faceName = wxMacMakeStringFromPascal( qdFontName );
235 }
236 else
237 {
238 switch ( m_family )
239 {
240 case wxSCRIPT :
241 case wxROMAN :
242 case wxDECORATIVE :
243 m_faceName = wxT("Times");
244 break ;
245
246 case wxSWISS :
247 m_faceName = wxT("Lucida Grande");
248 break ;
249
250 case wxMODERN :
251 m_faceName = wxT("Monaco");
252 break ;
253
254 default:
255 m_faceName = wxT("Times");
256 break ;
257 }
258 wxMacStringToPascal( m_faceName , qdFontName );
259 m_macFontFamily = FMGetFontFamilyFromName( qdFontName );
260 }
261 }
262 else
263 {
264 if ( m_faceName == wxT("systemfont") )
265 m_macFontFamily = GetSysFont();
266 else if ( m_faceName == wxT("applicationfont") )
267 m_macFontFamily = GetAppFont();
268 else
269 {
270 wxMacCFStringHolder cf( m_faceName, wxLocale::GetSystemEncoding() );
271 ATSFontFamilyRef atsfamily = ATSFontFamilyFindFromName( cf , kATSOptionFlagsDefault );
272 wxASSERT_MSG( atsfamily != (ATSFontFamilyRef) -1 , wxT("ATSFontFamilyFindFromName failed") );
273 m_macFontFamily = FMGetFontFamilyFromATSFontFamilyRef( atsfamily );
274 }
275 }
276
277 m_macFontStyle = 0;
278 if (m_weight == wxBOLD)
279 m_macFontStyle |= bold;
280 if (m_style == wxITALIC || m_style == wxSLANT)
281 m_macFontStyle |= italic;
282 if (m_underlined)
283 m_macFontStyle |= underline;
284 m_macFontSize = m_pointSize ;
285 }
286
287 // we try to get as much styles as possible into ATSU
288
289
290 // ATSUFontID and FMFont are equivalent
291 FMFontStyle intrinsicStyle = 0 ;
292 status = FMGetFontFromFontFamilyInstance( m_macFontFamily , m_macFontStyle , &m_macATSUFontID , &intrinsicStyle);
293 wxASSERT_MSG( status == noErr , wxT("couldn't get an ATSUFont from font family") );
294
295 m_macATSUAdditionalQDStyles = m_macFontStyle & (~intrinsicStyle );
296
297 if ( m_macATSUStyle )
298 {
299 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
300 m_macATSUStyle = NULL ;
301 }
302
303 status = ::ATSUCreateStyle((ATSUStyle *)&m_macATSUStyle);
304 wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") );
305
306 ATSUAttributeTag atsuTags[] =
307 {
308 kATSUFontTag ,
309 kATSUSizeTag ,
310 kATSUVerticalCharacterTag,
311 kATSUQDBoldfaceTag ,
312 kATSUQDItalicTag ,
313 kATSUQDUnderlineTag ,
314 kATSUQDCondensedTag ,
315 kATSUQDExtendedTag ,
316 };
317 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
318 {
319 sizeof( ATSUFontID ) ,
320 sizeof( Fixed ) ,
321 sizeof( ATSUVerticalCharacterType),
322 sizeof( Boolean ) ,
323 sizeof( Boolean ) ,
324 sizeof( Boolean ) ,
325 sizeof( Boolean ) ,
326 sizeof( Boolean ) ,
327 };
328
329 Boolean kTrue = true ;
330 Boolean kFalse = false ;
331
332 Fixed atsuSize = IntToFixed( m_macFontSize );
333 ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
334 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
335 {
336 &m_macATSUFontID ,
337 &atsuSize ,
338 &kHorizontal,
339 (m_macATSUAdditionalQDStyles & bold) ? &kTrue : &kFalse ,
340 (m_macATSUAdditionalQDStyles & italic) ? &kTrue : &kFalse ,
341 (m_macATSUAdditionalQDStyles & underline) ? &kTrue : &kFalse ,
342 (m_macATSUAdditionalQDStyles & condense) ? &kTrue : &kFalse ,
343 (m_macATSUAdditionalQDStyles & extend) ? &kTrue : &kFalse ,
344 };
345
346 status = ::ATSUSetAttributes(
347 (ATSUStyle)m_macATSUStyle,
348 sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,
349 atsuTags, atsuSizes, atsuValues);
350
351 wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );
352 }
353
354 // ----------------------------------------------------------------------------
355 // wxFont
356 // ----------------------------------------------------------------------------
357
358 bool wxFont::Create(const wxNativeFontInfo& info)
359 {
360 return Create(
361 info.pointSize, info.family, info.style, info.weight,
362 info.underlined, info.faceName, info.encoding );
363 }
364
365 wxFont::wxFont(const wxString& fontdesc)
366 {
367 wxNativeFontInfo info;
368 if ( info.FromString(fontdesc) )
369 (void)Create(info);
370 }
371
372 bool wxFont::Create(int pointSize,
373 int family,
374 int style,
375 int weight,
376 bool underlined,
377 const wxString& faceName,
378 wxFontEncoding encoding)
379 {
380 UnRef();
381
382 m_refData = new wxFontRefData(
383 pointSize, family, style, weight,
384 underlined, faceName, encoding);
385
386 RealizeResource();
387
388 return true;
389 }
390
391 bool wxFont::MacCreateThemeFont(wxUint16 themeFontID)
392 {
393 UnRef();
394
395 m_refData = new wxFontRefData(
396 12, wxDEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
397 false, wxEmptyString, wxFONTENCODING_DEFAULT );
398
399 M_FONTDATA->m_macThemeFontID = themeFontID ;
400 RealizeResource();
401
402 return true;
403 }
404
405 wxFont::~wxFont()
406 {
407 }
408
409 bool wxFont::RealizeResource()
410 {
411 M_FONTDATA->MacFindFont();
412
413 return true;
414 }
415
416 void wxFont::SetEncoding(wxFontEncoding encoding)
417 {
418 Unshare();
419
420 M_FONTDATA->m_encoding = encoding;
421
422 RealizeResource();
423 }
424
425 void wxFont::Unshare()
426 {
427 // Don't change shared data
428 if (!m_refData)
429 {
430 m_refData = new wxFontRefData();
431 }
432 else
433 {
434 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
435 UnRef();
436 m_refData = ref;
437 }
438 }
439
440 void wxFont::SetPointSize(int pointSize)
441 {
442 Unshare();
443
444 M_FONTDATA->m_pointSize = pointSize;
445
446 RealizeResource();
447 }
448
449 void wxFont::SetFamily(int family)
450 {
451 Unshare();
452
453 M_FONTDATA->m_family = family;
454
455 RealizeResource();
456 }
457
458 void wxFont::SetStyle(int style)
459 {
460 Unshare();
461
462 M_FONTDATA->m_style = style;
463
464 RealizeResource();
465 }
466
467 void wxFont::SetWeight(int weight)
468 {
469 Unshare();
470
471 M_FONTDATA->m_weight = weight;
472
473 RealizeResource();
474 }
475
476 bool wxFont::SetFaceName(const wxString& faceName)
477 {
478 Unshare();
479
480 M_FONTDATA->m_faceName = faceName;
481
482 RealizeResource();
483
484 return wxFontBase::SetFaceName(faceName);
485 }
486
487 void wxFont::SetUnderlined(bool underlined)
488 {
489 Unshare();
490
491 M_FONTDATA->m_underlined = underlined;
492
493 RealizeResource();
494 }
495
496 void wxFont::SetNoAntiAliasing( bool no )
497 {
498 Unshare();
499
500 M_FONTDATA->SetNoAntiAliasing( no );
501
502 RealizeResource();
503 }
504
505 // ----------------------------------------------------------------------------
506 // accessors
507 // ----------------------------------------------------------------------------
508
509 // TODO: insert checks everywhere for M_FONTDATA == NULL!
510
511 int wxFont::GetPointSize() const
512 {
513 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
514
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
522 return M_FONTDATA->m_family;
523 }
524
525 int wxFont::GetStyle() const
526 {
527 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
528
529 return M_FONTDATA->m_style;
530 }
531
532 int wxFont::GetWeight() const
533 {
534 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
535
536 return M_FONTDATA->m_weight;
537 }
538
539 bool wxFont::GetUnderlined() const
540 {
541 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
542
543 return M_FONTDATA->m_underlined;
544 }
545
546 wxString wxFont::GetFaceName() const
547 {
548 wxCHECK_MSG( M_FONTDATA != NULL , wxEmptyString , wxT("invalid font") );
549
550 return M_FONTDATA->m_faceName;
551 }
552
553 wxFontEncoding wxFont::GetEncoding() const
554 {
555 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTENCODING_DEFAULT , wxT("invalid font") );
556
557 return M_FONTDATA->m_encoding;
558 }
559
560 bool wxFont::GetNoAntiAliasing() const
561 {
562 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
563
564 return M_FONTDATA->m_noAA;
565 }
566
567 short wxFont::MacGetFontNum() const
568 {
569 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
570
571 return M_FONTDATA->m_macFontFamily;
572 }
573
574 short wxFont::MacGetFontSize() const
575 {
576 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
577
578 return M_FONTDATA->m_macFontSize;
579 }
580
581 wxByte wxFont::MacGetFontStyle() const
582 {
583 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
584
585 return M_FONTDATA->m_macFontStyle;
586 }
587
588 wxUint32 wxFont::MacGetATSUFontID() const
589 {
590 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
591
592 return M_FONTDATA->m_macATSUFontID;
593 }
594
595 void * wxFont::MacGetATSUStyle() const
596 {
597 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
598
599 return M_FONTDATA->m_macATSUStyle;
600 }
601
602 wxUint32 wxFont::MacGetATSUAdditionalQDStyles() const
603 {
604 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
605
606 return M_FONTDATA->m_macATSUAdditionalQDStyles;
607 }
608
609 wxUint16 wxFont::MacGetThemeFontID() const
610 {
611 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
612
613 return M_FONTDATA->m_macThemeFontID;
614 }
615
616 const wxNativeFontInfo * wxFont::GetNativeFontInfo() const
617 {
618 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
619 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
620
621 M_FONTDATA->m_info.InitFromFont(*this);
622
623 return &(M_FONTDATA->m_info);
624 }