]>
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
)
27 WX_DECLARE_HASH_MAP(wxString
, wxFontBundle
*,
28 wxStringHash
, wxStringEqual
,
31 // ============================================================================
33 // ============================================================================
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 wxFontFaceBase::wxFontFaceBase()
42 m_instances
= new wxFontInstanceList
;
43 m_instances
->DeleteContents(true);
46 wxFontFaceBase::~wxFontFaceBase()
51 void wxFontFaceBase::Acquire()
56 void wxFontFaceBase::Release()
58 if ( --m_refCnt
== 0 )
64 wxFontInstance
*wxFontFaceBase::GetFontInstance(float ptSize
, bool aa
)
66 wxASSERT_MSG( m_refCnt
> 0, _T("font library not loaded!") );
69 wxFontInstanceList::Node
*node
;
71 for ( node
= m_instances
->GetFirst(); node
; node
= node
->GetNext() )
74 if ( i
->GetPointSize() == ptSize
&& i
->IsAntiAliased() == aa
)
78 i
= CreateFontInstance(ptSize
, aa
);
79 m_instances
->Append(i
);
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 wxFontBundleBase::wxFontBundleBase()
89 for (int i
= 0; i
< FaceType_Max
; i
++)
93 wxFontBundleBase::~wxFontBundleBase()
95 for (int i
= 0; i
< FaceType_Max
; i
++)
99 wxFontFace
*wxFontBundleBase::GetFace(FaceType type
) const
101 wxFontFace
*f
= m_faces
[type
];
103 wxCHECK_MSG( f
, NULL
, _T("no such face in font bundle") );
111 wxFontBundleBase::GetFaceForFont(const wxFontMgrFontRefData
& font
) const
113 wxASSERT_MSG( font
.GetFaceName().empty() || font
.GetFaceName() == GetName(),
114 _T("calling GetFaceForFont for incompatible font") );
116 int type
= FaceType_Regular
;
118 if ( font
.GetWeight() == wxBOLD
)
119 type
|= FaceType_Bold
;
121 // FIXME -- this should read "if ( font->GetStyle() == wxITALIC )",
122 // but since MGL neither DFB supports slant, we try to display it with
123 // italic face (better than nothing...)
124 if ( font
.GetStyle() == wxITALIC
|| font
.GetStyle() == wxSLANT
)
126 if ( HasFace((FaceType
)(type
| FaceType_Italic
)) )
127 type
|= FaceType_Italic
;
130 if ( !HasFace((FaceType
)type
) )
132 for (int i
= 0; i
< FaceType_Max
; i
++)
134 if ( HasFace((FaceType
)i
) )
135 return GetFace((FaceType
)i
);
138 wxFAIL_MSG( _T("no face") );
142 return GetFace((FaceType
)type
);
145 // ----------------------------------------------------------------------------
146 // wxFontsManagerBase
147 // ----------------------------------------------------------------------------
149 wxFontsManager
*wxFontsManagerBase::ms_instance
= NULL
;
151 wxFontsManagerBase::wxFontsManagerBase()
153 m_hash
= new wxFontBundleHash();
154 m_list
= new wxFontBundleList
;
155 m_list
->DeleteContents(true);
158 wxFontsManagerBase::~wxFontsManagerBase()
165 wxFontsManager
*wxFontsManagerBase::Get()
168 ms_instance
= new wxFontsManager();
173 void wxFontsManagerBase::CleanUp()
175 wxDELETE(ms_instance
);
178 wxFontBundle
*wxFontsManagerBase::GetBundle(const wxString
& name
) const
180 return (*m_hash
)[name
.Lower()];
184 wxFontsManagerBase::GetBundleForFont(const wxFontMgrFontRefData
& font
) const
186 wxFontBundle
*bundle
= NULL
;
188 wxString facename
= font
.GetFaceName();
189 if ( !facename
.empty() )
190 bundle
= GetBundle(facename
);
194 facename
= GetDefaultFacename((wxFontFamily
)font
.GetFamily());
195 if ( !facename
.empty() )
196 bundle
= GetBundle(facename
);
201 if ( m_list
->GetFirst() )
202 bundle
= m_list
->GetFirst()->GetData();
204 wxFAIL_MSG(wxT("Fatal error, no fonts available!"));
210 void wxFontsManagerBase::AddBundle(wxFontBundle
*bundle
)
212 (*m_hash
)[bundle
->GetName().Lower()] = bundle
;
213 m_list
->Append(bundle
);
217 // ----------------------------------------------------------------------------
218 // wxFontMgrFontRefData
219 // ----------------------------------------------------------------------------
221 wxFontMgrFontRefData::wxFontMgrFontRefData(int size
,
226 const wxString
& faceName
,
227 wxFontEncoding encoding
)
229 if ( family
== wxDEFAULT
)
231 if ( style
== wxDEFAULT
)
233 if ( weight
== wxDEFAULT
)
235 if ( size
== wxDEFAULT
)
238 m_info
.family
= (wxFontFamily
)family
;
239 m_info
.faceName
= faceName
;
240 m_info
.style
= (wxFontStyle
)style
;
241 m_info
.weight
= (wxFontWeight
)weight
;
242 m_info
.pointSize
= size
;
243 m_info
.underlined
= underlined
;
244 m_info
.encoding
= encoding
;
253 wxFontMgrFontRefData::wxFontMgrFontRefData(const wxFontMgrFontRefData
& data
)
255 m_info
= data
.m_info
;
256 m_noAA
= data
.m_noAA
;
258 m_fontFace
= data
.m_fontFace
;
259 m_fontBundle
= data
.m_fontBundle
;
260 m_fontValid
= data
.m_fontValid
;
262 m_fontFace
->Acquire();
265 wxFontMgrFontRefData::~wxFontMgrFontRefData()
268 m_fontFace
->Release();
271 wxFontBundle
*wxFontMgrFontRefData::GetFontBundle() const
273 wxConstCast(this, wxFontMgrFontRefData
)->EnsureValidFont();
278 wxFontMgrFontRefData::GetFontInstance(float scale
, bool antialiased
) const
280 wxConstCast(this, wxFontMgrFontRefData
)->EnsureValidFont();
281 return m_fontFace
->GetFontInstance(m_info
.pointSize
* scale
,
282 antialiased
&& !m_noAA
);
285 void wxFontMgrFontRefData::SetPointSize(int pointSize
)
287 m_info
.pointSize
= pointSize
;
291 void wxFontMgrFontRefData::SetFamily(int family
)
293 m_info
.family
= (wxFontFamily
)family
;
297 void wxFontMgrFontRefData::SetStyle(int style
)
299 m_info
.style
= (wxFontStyle
)style
;
303 void wxFontMgrFontRefData::SetWeight(int weight
)
305 m_info
.weight
= (wxFontWeight
)weight
;
309 void wxFontMgrFontRefData::SetFaceName(const wxString
& faceName
)
311 m_info
.faceName
= faceName
;
315 void wxFontMgrFontRefData::SetUnderlined(bool underlined
)
317 m_info
.underlined
= underlined
;
321 void wxFontMgrFontRefData::SetEncoding(wxFontEncoding encoding
)
323 m_info
.encoding
= encoding
;
327 void wxFontMgrFontRefData::SetNoAntiAliasing(bool no
)
333 void wxFontMgrFontRefData::EnsureValidFont()
337 wxFontFace
*old
= m_fontFace
;
339 m_fontBundle
= wxFontsManager::Get()->GetBundleForFont(*this);
340 m_fontFace
= m_fontBundle
->GetFaceForFont(*this);