]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/font.cpp
extracted common initialization/cleanup functions in common/init.cpp; standardized...
[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 #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"
19 #include "wx/fontutil.h"
20 #include "wx/gdicmn.h"
21 #include "wx/utils.h"
22
23 #include "wx/fontutil.h"
24
25 #include "wx/mac/private.h"
26 #include "ATSUnicode.h"
27
28 #if !USE_SHARED_LIBRARIES
29 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
30 #endif
31
32 // ============================================================================
33 // implementation
34 // ============================================================================
35
36 // ----------------------------------------------------------------------------
37 // wxFontRefData
38 // ----------------------------------------------------------------------------
39
40 void wxFontRefData::Init(int pointSize,
41 int family,
42 int style,
43 int weight,
44 bool underlined,
45 const wxString& faceName,
46 wxFontEncoding encoding)
47 {
48 m_style = style;
49 m_pointSize = pointSize;
50 m_family = family;
51 m_style = style;
52 m_weight = weight;
53 m_underlined = underlined;
54 m_faceName = faceName;
55 m_encoding = encoding;
56
57 m_macFontNum = 0 ;
58 m_macFontSize = 0;
59 m_macFontStyle = 0;
60 m_fontId = 0;
61 m_noAA = FALSE;
62 }
63
64 wxFontRefData::~wxFontRefData()
65 {
66 }
67
68 void wxFontRefData::MacFindFont()
69 {
70 if( m_faceName.Length() == 0 )
71 {
72 switch( m_family )
73 {
74 case wxDEFAULT :
75 m_macFontNum = ::GetAppFont() ;
76 break ;
77 case wxDECORATIVE :
78 ::GetFNum( "\pTimes" , &m_macFontNum) ;
79 break ;
80 case wxROMAN :
81 ::GetFNum( "\pTimes" , &m_macFontNum) ;
82 break ;
83 case wxSCRIPT :
84 ::GetFNum( "\pTimes" , &m_macFontNum) ;
85 break ;
86 case wxSWISS :
87 ::GetFNum( "\pGeneva" , &m_macFontNum) ;
88 break ;
89 case wxMODERN :
90 ::GetFNum( "\pMonaco" , &m_macFontNum) ;
91 break ;
92 }
93 Str255 name ;
94 GetFontName( m_macFontNum , name ) ;
95 m_faceName = wxMacMakeStringFromPascal( name ) ;
96 }
97 else
98 {
99 if ( m_faceName == wxT("systemfont") )
100 m_macFontNum = ::GetSysFont() ;
101 else if ( m_faceName == wxT("applicationfont") )
102 m_macFontNum = ::GetAppFont() ;
103 else
104 {
105 Str255 fontname ;
106 wxMacStringToPascal( m_faceName , fontname ) ;
107 ::GetFNum( fontname, &m_macFontNum);
108 }
109 }
110
111 m_macFontStyle = 0;
112 if (m_weight == wxBOLD)
113 m_macFontStyle |= bold;
114 if (m_style == wxITALIC || m_style == wxSLANT)
115 m_macFontStyle |= italic;
116 if (m_underlined)
117 m_macFontStyle |= underline;
118 m_macFontSize = m_pointSize ;
119
120 //TODO:if we supply the style as an additional parameter we must make a testing
121 //sequence in order to degrade gracefully while trying to maintain most of the style
122 //information, meanwhile we just take the normal font and apply the features after
123 #ifdef __WXDEBUG__
124 OSStatus status =
125 #endif // __WXDEBUG__
126 ::ATSUFONDtoFontID(m_macFontNum, normal /*qdStyle*/, (UInt32*)&m_macATSUFontID);
127 /*
128 status = ATSUFindFontFromName ( (Ptr) m_faceName , strlen( m_faceName ) ,
129 kFontFullName, kFontMacintoshPlatform, kFontRomanScript , kFontNoLanguage , (UInt32*)&m_macATSUFontID ) ;
130 */
131 wxASSERT_MSG( status == noErr , wxT("couldn't retrieve font identifier") ) ;
132 }
133
134 // ----------------------------------------------------------------------------
135 // wxFont
136 // ----------------------------------------------------------------------------
137
138 void wxFont::Init()
139 {
140 }
141
142 bool wxFont::Create(const wxNativeFontInfo& info)
143 {
144 return Create(info.pointSize, info.family, info.style, info.weight,
145 info.underlined, info.faceName, info.encoding);
146 }
147
148 wxFont::wxFont(const wxString& fontdesc)
149 {
150 wxNativeFontInfo info;
151 if ( info.FromString(fontdesc) )
152 (void)Create(info);
153 }
154
155 bool wxFont::Create(int pointSize,
156 int family,
157 int style,
158 int weight,
159 bool underlined,
160 const wxString& faceName,
161 wxFontEncoding encoding)
162 {
163 UnRef();
164 m_refData = new wxFontRefData(pointSize, family, style, weight,
165 underlined, faceName, encoding);
166
167 RealizeResource();
168
169 return TRUE;
170 }
171
172 wxFont::~wxFont()
173 {
174 }
175
176 bool wxFont::RealizeResource()
177 {
178 M_FONTDATA->MacFindFont() ;
179 return TRUE;
180 }
181
182 void wxFont::SetEncoding(wxFontEncoding encoding)
183 {
184 Unshare();
185
186 M_FONTDATA->m_encoding = encoding;
187
188 RealizeResource();
189 }
190
191 void wxFont::Unshare()
192 {
193 // Don't change shared data
194 if (!m_refData)
195 {
196 m_refData = new wxFontRefData();
197 }
198 else
199 {
200 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
201 UnRef();
202 m_refData = ref;
203 }
204 }
205
206 void wxFont::SetPointSize(int pointSize)
207 {
208 Unshare();
209
210 M_FONTDATA->m_pointSize = pointSize;
211
212 RealizeResource();
213 }
214
215 void wxFont::SetFamily(int family)
216 {
217 Unshare();
218
219 M_FONTDATA->m_family = family;
220
221 RealizeResource();
222 }
223
224 void wxFont::SetStyle(int style)
225 {
226 Unshare();
227
228 M_FONTDATA->m_style = style;
229
230 RealizeResource();
231 }
232
233 void wxFont::SetWeight(int weight)
234 {
235 Unshare();
236
237 M_FONTDATA->m_weight = weight;
238
239 RealizeResource();
240 }
241
242 void wxFont::SetFaceName(const wxString& faceName)
243 {
244 Unshare();
245
246 M_FONTDATA->m_faceName = faceName;
247
248 RealizeResource();
249 }
250
251 void wxFont::SetUnderlined(bool underlined)
252 {
253 Unshare();
254
255 M_FONTDATA->m_underlined = underlined;
256
257 RealizeResource();
258 }
259
260 void wxFont::SetNoAntiAliasing( bool no )
261 {
262 Unshare();
263
264 M_FONTDATA->SetNoAntiAliasing( no );
265
266 RealizeResource();
267 }
268
269 // ----------------------------------------------------------------------------
270 // accessors
271 // ----------------------------------------------------------------------------
272
273 int wxFont::GetPointSize() const
274 {
275 return M_FONTDATA->m_pointSize;
276 }
277
278 int wxFont::GetFamily() const
279 {
280 return M_FONTDATA->m_family;
281 }
282
283 int wxFont::GetStyle() const
284 {
285 return M_FONTDATA->m_style;
286 }
287
288 int wxFont::GetWeight() const
289 {
290 return M_FONTDATA->m_weight;
291 }
292
293 bool wxFont::GetUnderlined() const
294 {
295 return M_FONTDATA->m_underlined;
296 }
297
298 wxString wxFont::GetFaceName() const
299 {
300 wxString str;
301 if ( M_FONTDATA )
302 str = M_FONTDATA->m_faceName ;
303 return str;
304 }
305
306 wxFontEncoding wxFont::GetEncoding() const
307 {
308 return M_FONTDATA->m_encoding;
309 }
310
311 bool wxFont::GetNoAntiAliasing()
312 {
313 return M_FONTDATA->m_noAA;
314 }
315