]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/font.cpp
switching implementation to current base technologies
[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 #endif
19
20 #include "wx/fontutil.h"
21 #include "wx/gdicmn.h"
22 #include "wx/utils.h"
23 #include "wx/fontutil.h"
24
25 #include "wx/mac/private.h"
26
27 #ifndef __DARWIN__
28 #include <ATSUnicode.h>
29 #endif
30
31
32 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
33
34
35 class WXDLLEXPORT wxFontRefData: public wxGDIRefData
36 {
37 friend class WXDLLEXPORT wxFont;
38
39 public:
40 wxFontRefData()
41 : m_fontId(0)
42 , m_pointSize(10)
43 , m_family(wxDEFAULT)
44 , m_style(wxNORMAL)
45 , m_weight(wxNORMAL)
46 , m_underlined(false)
47 , m_faceName(wxT("applicationfont"))
48 , m_encoding(wxFONTENCODING_DEFAULT)
49 , m_macFontFamily(0)
50 , m_macFontSize(0)
51 , m_macFontStyle(0)
52 , m_macATSUStyle(0)
53 , m_macATSUFontID(0)
54 {
55 Init(m_pointSize, m_family, m_style, m_weight,
56 m_underlined, m_faceName, m_encoding);
57 }
58
59 wxFontRefData(const wxFontRefData& data)
60 : wxGDIRefData()
61 , m_fontId(data.m_fontId)
62 , m_pointSize(data.m_pointSize)
63 , m_family(data.m_family)
64 , m_style(data.m_style)
65 , m_weight(data.m_weight)
66 , m_underlined(data.m_underlined)
67 , m_faceName(data.m_faceName)
68 , m_encoding(data.m_encoding)
69 , m_macFontFamily(data.m_macFontFamily)
70 , m_macFontSize(data.m_macFontSize)
71 , m_macFontStyle(data.m_macFontStyle)
72 , m_macATSUStyle(0)
73 , m_macATSUFontID(data.m_macATSUFontID)
74 {
75 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
76 data.m_underlined, data.m_faceName, data.m_encoding);
77 }
78
79 wxFontRefData(int size,
80 int family,
81 int style,
82 int weight,
83 bool underlined,
84 const wxString& faceName,
85 wxFontEncoding encoding)
86 : m_fontId(0)
87 , m_pointSize(size)
88 , m_family(family)
89 , m_style(style)
90 , m_weight(weight)
91 , m_underlined(underlined)
92 , m_faceName(faceName)
93 , m_encoding(encoding)
94 , m_macFontFamily(0)
95 , m_macFontSize(0)
96 , m_macFontStyle(0)
97 , m_macATSUStyle(0)
98 , m_macATSUFontID(0)
99 {
100 Init(size, family, style, weight, underlined, faceName, encoding);
101 }
102
103 virtual ~wxFontRefData();
104
105 void SetNoAntiAliasing( bool no = true )
106 { m_noAA = no; }
107
108 bool GetNoAntiAliasing() const
109 { return m_noAA; }
110
111 void MacFindFont();
112
113 protected:
114 // common part of all ctors
115 void Init(int size,
116 int family,
117 int style,
118 int weight,
119 bool underlined,
120 const wxString& faceName,
121 wxFontEncoding encoding);
122
123 // font characterstics
124 int m_fontId;
125 int m_pointSize;
126 int m_family;
127 int m_style;
128 int m_weight;
129 bool m_underlined;
130 wxString m_faceName;
131 wxFontEncoding m_encoding;
132 bool m_noAA; // No anti-aliasing
133
134 public:
135 FMFontFamily m_macFontFamily;
136 FMFontSize m_macFontSize;
137 FMFontStyle m_macFontStyle;
138
139 // ATSU Font Information
140
141 // this is split into an ATSU font id that may
142 // contain some styles (special bold fonts etc) and
143 // these are the additional qd styles that are not
144 // included in the ATSU font id
145 ATSUStyle m_macATSUStyle ;
146 ATSUFontID m_macATSUFontID;
147 FMFontStyle m_macATSUAdditionalQDStyles ;
148
149 // for true themeing support we must store the correct font
150 // information here, as this speeds up and optimizes rendering
151 ThemeFontID m_macThemeFontID ;
152
153 wxNativeFontInfo m_info;
154 };
155
156
157 // ============================================================================
158 // implementation
159 // ============================================================================
160
161 // ----------------------------------------------------------------------------
162 // wxFontRefData
163 // ----------------------------------------------------------------------------
164
165 void wxFontRefData::Init(int pointSize,
166 int family,
167 int style,
168 int weight,
169 bool underlined,
170 const wxString& faceName,
171 wxFontEncoding encoding)
172 {
173 m_style = style;
174 m_pointSize = pointSize;
175 m_family = family;
176 m_style = style;
177 m_weight = weight;
178 m_underlined = underlined;
179 m_faceName = faceName;
180 m_encoding = encoding;
181
182 m_macFontFamily = 0 ;
183 m_macFontSize = 0;
184 m_macFontStyle = 0;
185 m_macATSUFontID = 0;
186 m_macATSUAdditionalQDStyles = 0 ;
187 m_macATSUStyle = NULL ;
188
189 m_macThemeFontID = kThemeCurrentPortFont ;
190 m_noAA = false;
191 }
192
193 wxFontRefData::~wxFontRefData()
194 {
195 if ( m_macATSUStyle )
196 {
197 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
198 m_macATSUStyle = NULL ;
199 }
200 }
201
202 void wxFontRefData::MacFindFont()
203 {
204 OSStatus status ;
205
206 Str255 qdFontName ;
207 if ( m_macThemeFontID != kThemeCurrentPortFont )
208 {
209 Style style ;
210 GetThemeFont( m_macThemeFontID, GetApplicationScript(), qdFontName, &m_macFontSize, &style );
211 m_macFontStyle = style ;
212 m_faceName = wxMacMakeStringFromPascal( qdFontName );
213 if ( m_macFontStyle & bold )
214 m_weight = wxBOLD ;
215 else
216 m_weight = wxNORMAL ;
217 if ( m_macFontStyle & italic )
218 m_style = wxITALIC ;
219 if ( m_macFontStyle & underline )
220 m_underlined = true ;
221 m_pointSize = m_macFontSize ;
222
223 m_macFontFamily = FMGetFontFamilyFromName( qdFontName );
224 }
225 else
226 {
227 if ( m_faceName.empty() )
228 {
229 if ( m_family == wxDEFAULT )
230 {
231 m_macFontFamily = GetAppFont();
232 FMGetFontFamilyName(m_macFontFamily,qdFontName);
233 m_faceName = wxMacMakeStringFromPascal( qdFontName );
234 }
235 else
236 {
237 switch ( m_family )
238 {
239 case wxSCRIPT :
240 case wxROMAN :
241 case wxDECORATIVE :
242 m_faceName = wxT("Times");
243 break ;
244
245 case wxSWISS :
246 m_faceName = wxT("Lucida Grande");
247 break ;
248
249 case wxMODERN :
250 m_faceName = wxT("Monaco");
251 break ;
252
253 default:
254 m_faceName = wxT("Times");
255 break ;
256 }
257 wxMacStringToPascal( m_faceName , qdFontName );
258 m_macFontFamily = FMGetFontFamilyFromName( qdFontName );
259 }
260 }
261 else
262 {
263 if ( m_faceName == wxT("systemfont") )
264 m_macFontFamily = GetSysFont();
265 else if ( m_faceName == wxT("applicationfont") )
266 m_macFontFamily = GetAppFont();
267 else
268 {
269 wxMacCFStringHolder cf( m_faceName, wxLocale::GetSystemEncoding() );
270 ATSFontFamilyRef atsfamily = ATSFontFamilyFindFromName( cf , kATSOptionFlagsDefault );
271 m_macFontFamily = FMGetFontFamilyFromATSFontFamilyRef( atsfamily );
272 }
273 }
274
275 m_macFontStyle = 0;
276 if (m_weight == wxBOLD)
277 m_macFontStyle |= bold;
278 if (m_style == wxITALIC || m_style == wxSLANT)
279 m_macFontStyle |= italic;
280 if (m_underlined)
281 m_macFontStyle |= underline;
282 m_macFontSize = m_pointSize ;
283 }
284
285 // we try to get as much styles as possible into ATSU
286
287
288 // ATSUFontID and FMFont are equivalent
289 FMFontStyle intrinsicStyle = 0 ;
290 status = FMGetFontFromFontFamilyInstance( m_macFontFamily , m_macFontStyle , &m_macATSUFontID , &intrinsicStyle);
291 wxASSERT_MSG( status == noErr , wxT("couldn't get an ATSUFont from font family") );
292
293 m_macATSUAdditionalQDStyles = m_macFontStyle & (~intrinsicStyle );
294
295 if ( m_macATSUStyle )
296 {
297 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
298 m_macATSUStyle = NULL ;
299 }
300
301 status = ::ATSUCreateStyle((ATSUStyle *)&m_macATSUStyle);
302 wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") );
303
304 ATSUAttributeTag atsuTags[] =
305 {
306 kATSUFontTag ,
307 kATSUSizeTag ,
308 kATSUVerticalCharacterTag,
309 kATSUQDBoldfaceTag ,
310 kATSUQDItalicTag ,
311 kATSUQDUnderlineTag ,
312 kATSUQDCondensedTag ,
313 kATSUQDExtendedTag ,
314 };
315 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
316 {
317 sizeof( ATSUFontID ) ,
318 sizeof( Fixed ) ,
319 sizeof( ATSUVerticalCharacterType),
320 sizeof( Boolean ) ,
321 sizeof( Boolean ) ,
322 sizeof( Boolean ) ,
323 sizeof( Boolean ) ,
324 sizeof( Boolean ) ,
325 };
326
327 Boolean kTrue = true ;
328 Boolean kFalse = false ;
329
330 Fixed atsuSize = IntToFixed( m_macFontSize );
331 ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
332 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
333 {
334 &m_macATSUFontID ,
335 &atsuSize ,
336 &kHorizontal,
337 (m_macATSUAdditionalQDStyles & bold) ? &kTrue : &kFalse ,
338 (m_macATSUAdditionalQDStyles & italic) ? &kTrue : &kFalse ,
339 (m_macATSUAdditionalQDStyles & underline) ? &kTrue : &kFalse ,
340 (m_macATSUAdditionalQDStyles & condense) ? &kTrue : &kFalse ,
341 (m_macATSUAdditionalQDStyles & extend) ? &kTrue : &kFalse ,
342 };
343
344 status = ::ATSUSetAttributes(
345 (ATSUStyle)m_macATSUStyle,
346 sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,
347 atsuTags, atsuSizes, atsuValues);
348
349 wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );
350 }
351
352 // ----------------------------------------------------------------------------
353 // wxFont
354 // ----------------------------------------------------------------------------
355
356 bool wxFont::Create(const wxNativeFontInfo& info)
357 {
358 return Create(
359 info.pointSize, info.family, info.style, info.weight,
360 info.underlined, info.faceName, info.encoding );
361 }
362
363 wxFont::wxFont(const wxString& fontdesc)
364 {
365 wxNativeFontInfo info;
366 if ( info.FromString(fontdesc) )
367 (void)Create(info);
368 }
369
370 bool wxFont::Create(int pointSize,
371 int family,
372 int style,
373 int weight,
374 bool underlined,
375 const wxString& faceName,
376 wxFontEncoding encoding)
377 {
378 UnRef();
379
380 m_refData = new wxFontRefData(
381 pointSize, family, style, weight,
382 underlined, faceName, encoding);
383
384 RealizeResource();
385
386 return true;
387 }
388
389 bool wxFont::MacCreateThemeFont(wxUint16 themeFontID)
390 {
391 UnRef();
392
393 m_refData = new wxFontRefData(
394 12, wxDEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
395 false, wxEmptyString, wxFONTENCODING_DEFAULT );
396
397 M_FONTDATA->m_macThemeFontID = themeFontID ;
398 RealizeResource();
399
400 return true;
401 }
402
403 wxFont::~wxFont()
404 {
405 }
406
407 bool wxFont::RealizeResource()
408 {
409 M_FONTDATA->MacFindFont();
410
411 return true;
412 }
413
414 void wxFont::SetEncoding(wxFontEncoding encoding)
415 {
416 Unshare();
417
418 M_FONTDATA->m_encoding = encoding;
419
420 RealizeResource();
421 }
422
423 void wxFont::Unshare()
424 {
425 // Don't change shared data
426 if (!m_refData)
427 {
428 m_refData = new wxFontRefData();
429 }
430 else
431 {
432 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
433 UnRef();
434 m_refData = ref;
435 }
436 }
437
438 void wxFont::SetPointSize(int pointSize)
439 {
440 Unshare();
441
442 M_FONTDATA->m_pointSize = pointSize;
443
444 RealizeResource();
445 }
446
447 void wxFont::SetFamily(int family)
448 {
449 Unshare();
450
451 M_FONTDATA->m_family = family;
452
453 RealizeResource();
454 }
455
456 void wxFont::SetStyle(int style)
457 {
458 Unshare();
459
460 M_FONTDATA->m_style = style;
461
462 RealizeResource();
463 }
464
465 void wxFont::SetWeight(int weight)
466 {
467 Unshare();
468
469 M_FONTDATA->m_weight = weight;
470
471 RealizeResource();
472 }
473
474 void wxFont::SetFaceName(const wxString& faceName)
475 {
476 Unshare();
477
478 M_FONTDATA->m_faceName = faceName;
479
480 RealizeResource();
481 }
482
483 void wxFont::SetUnderlined(bool underlined)
484 {
485 Unshare();
486
487 M_FONTDATA->m_underlined = underlined;
488
489 RealizeResource();
490 }
491
492 void wxFont::SetNoAntiAliasing( bool no )
493 {
494 Unshare();
495
496 M_FONTDATA->SetNoAntiAliasing( no );
497
498 RealizeResource();
499 }
500
501 // ----------------------------------------------------------------------------
502 // accessors
503 // ----------------------------------------------------------------------------
504
505 // TODO: insert checks everywhere for M_FONTDATA == NULL!
506
507 int wxFont::GetPointSize() const
508 {
509 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
510
511 return M_FONTDATA->m_pointSize;
512 }
513
514 int wxFont::GetFamily() const
515 {
516 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
517
518 return M_FONTDATA->m_family;
519 }
520
521 int wxFont::GetStyle() const
522 {
523 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
524
525 return M_FONTDATA->m_style;
526 }
527
528 int wxFont::GetWeight() const
529 {
530 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
531
532 return M_FONTDATA->m_weight;
533 }
534
535 bool wxFont::GetUnderlined() const
536 {
537 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
538
539 return M_FONTDATA->m_underlined;
540 }
541
542 wxString wxFont::GetFaceName() const
543 {
544 wxCHECK_MSG( M_FONTDATA != NULL , wxEmptyString , wxT("invalid font") );
545
546 return M_FONTDATA->m_faceName;
547 }
548
549 wxFontEncoding wxFont::GetEncoding() const
550 {
551 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTENCODING_DEFAULT , wxT("invalid font") );
552
553 return M_FONTDATA->m_encoding;
554 }
555
556 bool wxFont::GetNoAntiAliasing() const
557 {
558 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
559
560 return M_FONTDATA->m_noAA;
561 }
562
563 short wxFont::MacGetFontNum() const
564 {
565 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
566
567 return M_FONTDATA->m_macFontFamily;
568 }
569
570 short wxFont::MacGetFontSize() const
571 {
572 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
573
574 return M_FONTDATA->m_macFontSize;
575 }
576
577 wxByte wxFont::MacGetFontStyle() const
578 {
579 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
580
581 return M_FONTDATA->m_macFontStyle;
582 }
583
584 wxUint32 wxFont::MacGetATSUFontID() const
585 {
586 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
587
588 return M_FONTDATA->m_macATSUFontID;
589 }
590
591 void * wxFont::MacGetATSUStyle() const
592 {
593 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
594
595 return M_FONTDATA->m_macATSUStyle;
596 }
597
598 wxUint32 wxFont::MacGetATSUAdditionalQDStyles() const
599 {
600 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
601
602 return M_FONTDATA->m_macATSUAdditionalQDStyles;
603 }
604
605 wxUint16 wxFont::MacGetThemeFontID() const
606 {
607 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
608
609 return M_FONTDATA->m_macThemeFontID;
610 }
611
612 const wxNativeFontInfo * wxFont::GetNativeFontInfo() const
613 {
614 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
615 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
616
617 M_FONTDATA->m_info.InitFromFont(*this);
618
619 return &(M_FONTDATA->m_info);
620 }