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