]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/font.cpp
made wx-config work in place on systems where /bin/sh is a Bourne shell (such as...
[wxWidgets.git] / src / mac / classic / font.cpp
CommitLineData
2646f485
SC
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
65571936 9// Licence: wxWindows licence
2646f485
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"
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
2646f485 28IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
2646f485
SC
29
30class WXDLLEXPORT wxFontRefData: public wxGDIRefData
31{
32 friend class WXDLLEXPORT wxFont;
33public:
34 wxFontRefData()
35 : m_fontId(0)
36 , m_pointSize(10)
37 , m_family(wxDEFAULT)
38 , m_style(wxNORMAL)
39 , m_weight(wxNORMAL)
40 , m_underlined(FALSE)
41 , m_faceName(wxT("Geneva"))
42 , m_encoding(wxFONTENCODING_DEFAULT)
43 , m_macFontNum(0)
44 , m_macFontSize(0)
45 , m_macFontStyle(0)
46 , m_macATSUFontID()
47 {
48 Init(10, wxDEFAULT, wxNORMAL, wxNORMAL, FALSE,
49 wxT("Geneva"), wxFONTENCODING_DEFAULT);
50 }
51
52 wxFontRefData(const wxFontRefData& data)
53 : wxGDIRefData()
54 , m_fontId(data.m_fontId)
55 , m_pointSize(data.m_pointSize)
56 , m_family(data.m_family)
57 , m_style(data.m_style)
58 , m_weight(data.m_weight)
59 , m_underlined(data.m_underlined)
60 , m_faceName(data.m_faceName)
61 , m_encoding(data.m_encoding)
62 , m_macFontNum(data.m_macFontNum)
63 , m_macFontSize(data.m_macFontSize)
64 , m_macFontStyle(data.m_macFontStyle)
65 , m_macATSUFontID(data.m_macATSUFontID)
66 {
67 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
68 data.m_underlined, data.m_faceName, data.m_encoding);
69 }
70
71 wxFontRefData(int size,
72 int family,
73 int style,
74 int weight,
75 bool underlined,
76 const wxString& faceName,
77 wxFontEncoding encoding)
78 : m_fontId(0)
79 , m_pointSize(size)
80 , m_family(family)
81 , m_style(style)
82 , m_weight(weight)
83 , m_underlined(underlined)
84 , m_faceName(faceName)
85 , m_encoding(encoding)
86 , m_macFontNum(0)
87 , m_macFontSize(0)
88 , m_macFontStyle(0)
89 , m_macATSUFontID(0)
90 {
91 Init(size, family, style, weight, underlined, faceName, encoding);
92 }
93
94 virtual ~wxFontRefData();
95 void SetNoAntiAliasing( bool no = TRUE ) { m_noAA = no; }
da0a6b01 96 bool GetNoAntiAliasing() const { return m_noAA; }
2646f485
SC
97
98protected:
99 // common part of all ctors
100 void Init(int size,
101 int family,
102 int style,
103 int weight,
104 bool underlined,
105 const wxString& faceName,
106 wxFontEncoding encoding);
107
108 // font characterstics
109 int m_fontId;
110 int m_pointSize;
111 int m_family;
112 int m_style;
113 int m_weight;
114 bool m_underlined;
115 wxString m_faceName;
116 wxFontEncoding m_encoding;
117 bool m_noAA; // No anti-aliasing
118
119public:
120 short m_macFontNum;
121 short m_macFontSize;
122 unsigned char m_macFontStyle;
123 wxUint32 m_macATSUFontID;
124
125 wxNativeFontInfo m_info;
126
127public:
128 void MacFindFont() ;
129};
130// ============================================================================
131// implementation
132// ============================================================================
133
134// ----------------------------------------------------------------------------
135// wxFontRefData
136// ----------------------------------------------------------------------------
137
138void wxFontRefData::Init(int pointSize,
139 int family,
140 int style,
141 int weight,
142 bool underlined,
143 const wxString& faceName,
144 wxFontEncoding encoding)
145{
146 m_style = style;
147 m_pointSize = pointSize;
148 m_family = family;
149 m_style = style;
150 m_weight = weight;
151 m_underlined = underlined;
152 m_faceName = faceName;
153 m_encoding = encoding;
154
155 m_macFontNum = 0 ;
156 m_macFontSize = 0;
157 m_macFontStyle = 0;
158 m_fontId = 0;
159 m_noAA = FALSE;
160}
161
162wxFontRefData::~wxFontRefData()
163{
164}
165
166void wxFontRefData::MacFindFont()
167{
168 if( m_faceName.Length() == 0 )
169 {
170 switch( m_family )
171 {
172 case wxDEFAULT :
173 m_macFontNum = ::GetAppFont() ;
174 break ;
175 case wxDECORATIVE :
176 ::GetFNum( "\pTimes" , &m_macFontNum) ;
177 break ;
178 case wxROMAN :
179 ::GetFNum( "\pTimes" , &m_macFontNum) ;
180 break ;
181 case wxSCRIPT :
182 ::GetFNum( "\pTimes" , &m_macFontNum) ;
183 break ;
184 case wxSWISS :
185 ::GetFNum( "\pGeneva" , &m_macFontNum) ;
186 break ;
187 case wxMODERN :
188 ::GetFNum( "\pMonaco" , &m_macFontNum) ;
189 break ;
190 }
191 Str255 name ;
192 GetFontName( m_macFontNum , name ) ;
193 m_faceName = wxMacMakeStringFromPascal( name ) ;
194 }
195 else
196 {
197 if ( m_faceName == wxT("systemfont") )
198 m_macFontNum = ::GetSysFont() ;
199 else if ( m_faceName == wxT("applicationfont") )
200 m_macFontNum = ::GetAppFont() ;
201 else
202 {
203 Str255 fontname ;
204 wxMacStringToPascal( m_faceName , fontname ) ;
205 ::GetFNum( fontname, &m_macFontNum);
206 }
207 }
208
209 m_macFontStyle = 0;
210 if (m_weight == wxBOLD)
211 m_macFontStyle |= bold;
212 if (m_style == wxITALIC || m_style == wxSLANT)
213 m_macFontStyle |= italic;
214 if (m_underlined)
215 m_macFontStyle |= underline;
216 m_macFontSize = m_pointSize ;
217
218 //TODO:if we supply the style as an additional parameter we must make a testing
219 //sequence in order to degrade gracefully while trying to maintain most of the style
220 //information, meanwhile we just take the normal font and apply the features after
221#ifdef __WXDEBUG__
222 OSStatus status =
223#endif // __WXDEBUG__
224 ::ATSUFONDtoFontID(m_macFontNum, normal /*qdStyle*/, (UInt32*)&m_macATSUFontID);
225 /*
226 status = ATSUFindFontFromName ( (Ptr) m_faceName , strlen( m_faceName ) ,
227 kFontFullName, kFontMacintoshPlatform, kFontRomanScript , kFontNoLanguage , (UInt32*)&m_macATSUFontID ) ;
228 */
229 wxASSERT_MSG( status == noErr , wxT("couldn't retrieve font identifier") ) ;
230}
231
232// ----------------------------------------------------------------------------
233// wxFont
234// ----------------------------------------------------------------------------
235
236void wxFont::Init()
237{
238}
239
240bool wxFont::Create(const wxNativeFontInfo& info)
241{
242 return Create(info.pointSize, info.family, info.style, info.weight,
243 info.underlined, info.faceName, info.encoding);
244}
245
246wxFont::wxFont(const wxString& fontdesc)
247{
248 wxNativeFontInfo info;
249 if ( info.FromString(fontdesc) )
250 (void)Create(info);
251}
252
253bool wxFont::Create(int pointSize,
254 int family,
255 int style,
256 int weight,
257 bool underlined,
258 const wxString& faceName,
259 wxFontEncoding encoding)
260{
261 UnRef();
262 m_refData = new wxFontRefData(pointSize, family, style, weight,
263 underlined, faceName, encoding);
264
265 RealizeResource();
266
267 return TRUE;
268}
269
270wxFont::~wxFont()
271{
272}
273
274bool wxFont::RealizeResource()
275{
276 M_FONTDATA->MacFindFont() ;
277 return TRUE;
278}
279
280void wxFont::SetEncoding(wxFontEncoding encoding)
281{
282 Unshare();
283
284 M_FONTDATA->m_encoding = encoding;
285
286 RealizeResource();
287}
288
289void wxFont::Unshare()
290{
291 // Don't change shared data
292 if (!m_refData)
293 {
294 m_refData = new wxFontRefData();
295 }
296 else
297 {
298 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
299 UnRef();
300 m_refData = ref;
301 }
302}
303
304void wxFont::SetPointSize(int pointSize)
305{
306 Unshare();
307
308 M_FONTDATA->m_pointSize = pointSize;
309
310 RealizeResource();
311}
312
313void wxFont::SetFamily(int family)
314{
315 Unshare();
316
317 M_FONTDATA->m_family = family;
318
319 RealizeResource();
320}
321
322void wxFont::SetStyle(int style)
323{
324 Unshare();
325
326 M_FONTDATA->m_style = style;
327
328 RealizeResource();
329}
330
331void wxFont::SetWeight(int weight)
332{
333 Unshare();
334
335 M_FONTDATA->m_weight = weight;
336
337 RealizeResource();
338}
339
340void wxFont::SetFaceName(const wxString& faceName)
341{
342 Unshare();
343
344 M_FONTDATA->m_faceName = faceName;
345
346 RealizeResource();
347}
348
349void wxFont::SetUnderlined(bool underlined)
350{
351 Unshare();
352
353 M_FONTDATA->m_underlined = underlined;
354
355 RealizeResource();
356}
357
358void wxFont::SetNoAntiAliasing( bool no )
359{
360 Unshare();
361
362 M_FONTDATA->SetNoAntiAliasing( no );
363
364 RealizeResource();
365}
366
367// ----------------------------------------------------------------------------
368// accessors
369// ----------------------------------------------------------------------------
370
371// TODO: insert checks everywhere for M_FONTDATA == NULL!
372
373int wxFont::GetPointSize() const
374{
375 return M_FONTDATA->m_pointSize;
376}
377
378int wxFont::GetFamily() const
379{
380 return M_FONTDATA->m_family;
381}
382
383int wxFont::GetStyle() const
384{
385 return M_FONTDATA->m_style;
386}
387
388int wxFont::GetWeight() const
389{
390 return M_FONTDATA->m_weight;
391}
392
393bool wxFont::GetUnderlined() const
394{
395 return M_FONTDATA->m_underlined;
396}
397
398wxString wxFont::GetFaceName() const
399{
400 wxString str;
401 if ( M_FONTDATA )
402 str = M_FONTDATA->m_faceName ;
403 return str;
404}
405
406wxFontEncoding wxFont::GetEncoding() const
407{
408 return M_FONTDATA->m_encoding;
409}
410
da0a6b01 411bool wxFont::GetNoAntiAliasing() const
2646f485
SC
412{
413 return M_FONTDATA->m_noAA;
414}
415
416short wxFont::GetMacFontNum() const
417{
418 return M_FONTDATA->m_macFontNum;
419}
420
421short wxFont::GetMacFontSize() const
422{
423 return M_FONTDATA->m_macFontSize;
424}
425
426wxByte wxFont::GetMacFontStyle() const
427{
428 return M_FONTDATA->m_macFontStyle;
429}
430
431wxUint32 wxFont::GetMacATSUFontID() const
432{
433 return M_FONTDATA->m_macATSUFontID;
434}
435
436const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
437{
438 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
439
440 M_FONTDATA->m_info.InitFromFont(*this);
441
442 return &(M_FONTDATA->m_info);
443}
444