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