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