]>
git.saurik.com Git - wxWidgets.git/blob - src/common/fontmgrcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fontmgrcmn.cpp
3 // Purpose: font management for ports that don't have their own
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
8 // (c) 2006 REA Elektronik GmbH
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #include "wx/private/fontmgr.h"
21 #include "wx/listimpl.cpp"
22 #include "wx/hashmap.h"
24 WX_DECLARE_LIST(wxFontInstance
, wxFontInstanceList
);
25 WX_DEFINE_LIST(wxFontInstanceList
)
26 WX_DEFINE_LIST(wxFontBundleList
)
28 WX_DECLARE_HASH_MAP(wxString
, wxFontBundle
*,
29 wxStringHash
, wxStringEqual
,
30 wxFontBundleHashBase
);
31 // in STL build, hash class is typedef to a template, so it can't be forward
32 // declared, as we do; solve it by having a dummy class:
33 class wxFontBundleHash
: public wxFontBundleHashBase
37 // ============================================================================
39 // ============================================================================
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 wxFontFaceBase::wxFontFaceBase()
48 m_instances
= new wxFontInstanceList
;
49 m_instances
->DeleteContents(true);
52 wxFontFaceBase::~wxFontFaceBase()
57 void wxFontFaceBase::Acquire()
62 void wxFontFaceBase::Release()
64 if ( --m_refCnt
== 0 )
70 wxFontInstance
*wxFontFaceBase::GetFontInstance(float ptSize
, bool aa
)
72 wxASSERT_MSG( m_refCnt
> 0, wxT("font library not loaded!") );
74 for ( wxFontInstanceList::const_iterator i
= m_instances
->begin();
75 i
!= m_instances
->end(); ++i
)
77 if ( (*i
)->GetPointSize() == ptSize
&& (*i
)->IsAntiAliased() == aa
)
81 wxFontInstance
*i
= CreateFontInstance(ptSize
, aa
);
82 m_instances
->Append(i
);
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 wxFontBundleBase::wxFontBundleBase()
92 for (int i
= 0; i
< FaceType_Max
; i
++)
96 wxFontBundleBase::~wxFontBundleBase()
98 for (int i
= 0; i
< FaceType_Max
; i
++)
102 wxFontFace
*wxFontBundleBase::GetFace(FaceType type
) const
104 wxFontFace
*f
= m_faces
[type
];
106 wxCHECK_MSG( f
, NULL
, wxT("no such face in font bundle") );
114 wxFontBundleBase::GetFaceForFont(const wxFontMgrFontRefData
& font
) const
116 wxASSERT_MSG( font
.GetFaceName().empty() ||
117 GetName().CmpNoCase(font
.GetFaceName()) == 0,
118 wxT("calling GetFaceForFont for incompatible font") );
120 int type
= FaceType_Regular
;
122 if ( font
.GetWeight() == wxBOLD
)
123 type
|= FaceType_Bold
;
125 // FIXME -- this should read "if ( font->GetStyle() == wxITALIC )",
126 // but since MGL neither DFB supports slant, we try to display it with
127 // italic face (better than nothing...)
128 if ( font
.GetStyle() == wxITALIC
|| font
.GetStyle() == wxSLANT
)
130 if ( HasFace((FaceType
)(type
| FaceType_Italic
)) )
131 type
|= FaceType_Italic
;
134 if ( !HasFace((FaceType
)type
) )
136 // if we can't get the exact font requested, substitute it with
137 // some other variant:
138 for (int i
= 0; i
< FaceType_Max
; i
++)
140 if ( HasFace((FaceType
)i
) )
141 return GetFace((FaceType
)i
);
144 wxFAIL_MSG( wxT("no face") );
148 return GetFace((FaceType
)type
);
151 // ----------------------------------------------------------------------------
152 // wxFontsManagerBase
153 // ----------------------------------------------------------------------------
155 wxFontsManager
*wxFontsManagerBase::ms_instance
= NULL
;
157 wxFontsManagerBase::wxFontsManagerBase()
159 m_hash
= new wxFontBundleHash();
160 m_list
= new wxFontBundleList
;
161 m_list
->DeleteContents(true);
164 wxFontsManagerBase::~wxFontsManagerBase()
171 wxFontsManager
*wxFontsManagerBase::Get()
174 ms_instance
= new wxFontsManager();
179 void wxFontsManagerBase::CleanUp()
181 wxDELETE(ms_instance
);
184 wxFontBundle
*wxFontsManagerBase::GetBundle(const wxString
& name
) const
186 return (*m_hash
)[name
.Lower()];
190 wxFontsManagerBase::GetBundleForFont(const wxFontMgrFontRefData
& font
) const
192 wxFontBundle
*bundle
= NULL
;
194 wxString facename
= font
.GetFaceName();
195 if ( !facename
.empty() )
196 bundle
= GetBundle(facename
);
200 facename
= GetDefaultFacename((wxFontFamily
)font
.GetFamily());
201 if ( !facename
.empty() )
202 bundle
= GetBundle(facename
);
207 if ( m_list
->GetFirst() )
208 bundle
= m_list
->GetFirst()->GetData();
210 wxFAIL_MSG(wxT("Fatal error, no fonts available!"));
216 void wxFontsManagerBase::AddBundle(wxFontBundle
*bundle
)
218 (*m_hash
)[bundle
->GetName().Lower()] = bundle
;
219 m_list
->Append(bundle
);
223 // ----------------------------------------------------------------------------
224 // wxFontMgrFontRefData
225 // ----------------------------------------------------------------------------
227 wxFontMgrFontRefData::wxFontMgrFontRefData(int size
,
232 const wxString
& faceName
,
233 wxFontEncoding encoding
)
235 if ( family
== wxFONTFAMILY_DEFAULT
)
236 family
= wxFONTFAMILY_SWISS
;
237 if ( size
== wxDEFAULT
)
240 m_info
.family
= (wxFontFamily
)family
;
241 m_info
.faceName
= faceName
;
242 m_info
.style
= (wxFontStyle
)style
;
243 m_info
.weight
= (wxFontWeight
)weight
;
244 m_info
.pointSize
= size
;
245 m_info
.underlined
= underlined
;
246 m_info
.encoding
= encoding
;
253 wxFontMgrFontRefData::wxFontMgrFontRefData(const wxFontMgrFontRefData
& data
)
255 m_info
= data
.m_info
;
257 m_fontFace
= data
.m_fontFace
;
258 m_fontBundle
= data
.m_fontBundle
;
259 m_fontValid
= data
.m_fontValid
;
261 m_fontFace
->Acquire();
264 wxFontMgrFontRefData::~wxFontMgrFontRefData()
267 m_fontFace
->Release();
270 wxFontBundle
*wxFontMgrFontRefData::GetFontBundle() const
272 wxConstCast(this, wxFontMgrFontRefData
)->EnsureValidFont();
277 wxFontMgrFontRefData::GetFontInstance(float scale
, bool antialiased
) const
279 wxConstCast(this, wxFontMgrFontRefData
)->EnsureValidFont();
280 return m_fontFace
->GetFontInstance(m_info
.pointSize
* scale
,
284 void wxFontMgrFontRefData::SetPointSize(int pointSize
)
286 m_info
.pointSize
= pointSize
;
290 void wxFontMgrFontRefData::SetFamily(wxFontFamily family
)
292 m_info
.family
= family
;
296 void wxFontMgrFontRefData::SetStyle(wxFontStyle style
)
298 m_info
.style
= style
;
302 void wxFontMgrFontRefData::SetWeight(wxFontWeight weight
)
304 m_info
.weight
= weight
;
308 void wxFontMgrFontRefData::SetFaceName(const wxString
& faceName
)
310 m_info
.faceName
= faceName
;
314 void wxFontMgrFontRefData::SetUnderlined(bool underlined
)
316 m_info
.underlined
= underlined
;
320 void wxFontMgrFontRefData::SetEncoding(wxFontEncoding encoding
)
322 m_info
.encoding
= encoding
;
326 void wxFontMgrFontRefData::EnsureValidFont()
330 wxFontFace
*old
= m_fontFace
;
332 m_fontBundle
= wxFontsManager::Get()->GetBundleForFont(*this);
333 m_fontFace
= m_fontBundle
->GetFaceForFont(*this);