]>
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
6 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // (c) 2006 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #include "wx/private/fontmgr.h"
20 #include "wx/listimpl.cpp"
21 #include "wx/hashmap.h"
23 WX_DECLARE_LIST(wxFontInstance
, wxFontInstanceList
);
24 WX_DEFINE_LIST(wxFontInstanceList
)
25 WX_DEFINE_LIST(wxFontBundleList
)
27 WX_DECLARE_HASH_MAP(wxString
, wxFontBundle
*,
28 wxStringHash
, wxStringEqual
,
29 wxFontBundleHashBase
);
30 // in STL build, hash class is typedef to a template, so it can't be forward
31 // declared, as we do; solve it by having a dummy class:
32 class wxFontBundleHash
: public wxFontBundleHashBase
36 // ============================================================================
38 // ============================================================================
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 wxFontFaceBase::wxFontFaceBase()
47 m_instances
= new wxFontInstanceList
;
48 m_instances
->DeleteContents(true);
51 wxFontFaceBase::~wxFontFaceBase()
56 void wxFontFaceBase::Acquire()
61 void wxFontFaceBase::Release()
63 if ( --m_refCnt
== 0 )
69 wxFontInstance
*wxFontFaceBase::GetFontInstance(float ptSize
, bool aa
)
71 wxASSERT_MSG( m_refCnt
> 0, wxT("font library not loaded!") );
73 for ( wxFontInstanceList::const_iterator i
= m_instances
->begin();
74 i
!= m_instances
->end(); ++i
)
76 if ( (*i
)->GetPointSize() == ptSize
&& (*i
)->IsAntiAliased() == aa
)
80 wxFontInstance
*i
= CreateFontInstance(ptSize
, aa
);
81 m_instances
->Append(i
);
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 wxFontBundleBase::wxFontBundleBase()
91 for (int i
= 0; i
< FaceType_Max
; i
++)
95 wxFontBundleBase::~wxFontBundleBase()
97 for (int i
= 0; i
< FaceType_Max
; i
++)
101 wxFontFace
*wxFontBundleBase::GetFace(FaceType type
) const
103 wxFontFace
*f
= m_faces
[type
];
105 wxCHECK_MSG( f
, NULL
, wxT("no such face in font bundle") );
113 wxFontBundleBase::GetFaceForFont(const wxFontMgrFontRefData
& font
) const
115 wxASSERT_MSG( font
.GetFaceName().empty() ||
116 GetName().CmpNoCase(font
.GetFaceName()) == 0,
117 wxT("calling GetFaceForFont for incompatible font") );
119 int type
= FaceType_Regular
;
121 if ( font
.GetWeight() == wxBOLD
)
122 type
|= FaceType_Bold
;
124 // FIXME -- this should read "if ( font->GetStyle() == wxITALIC )",
125 // but since DFB doesn't support slant, we try to display it with italic
126 // face (better than nothing...)
127 if ( font
.GetStyle() == wxITALIC
|| font
.GetStyle() == wxSLANT
)
129 if ( HasFace((FaceType
)(type
| FaceType_Italic
)) )
130 type
|= FaceType_Italic
;
133 if ( !HasFace((FaceType
)type
) )
135 // if we can't get the exact font requested, substitute it with
136 // some other variant:
137 for (int i
= 0; i
< FaceType_Max
; i
++)
139 if ( HasFace((FaceType
)i
) )
140 return GetFace((FaceType
)i
);
143 wxFAIL_MSG( wxT("no face") );
147 return GetFace((FaceType
)type
);
150 // ----------------------------------------------------------------------------
151 // wxFontsManagerBase
152 // ----------------------------------------------------------------------------
154 wxFontsManager
*wxFontsManagerBase::ms_instance
= NULL
;
156 wxFontsManagerBase::wxFontsManagerBase()
158 m_hash
= new wxFontBundleHash();
159 m_list
= new wxFontBundleList
;
160 m_list
->DeleteContents(true);
163 wxFontsManagerBase::~wxFontsManagerBase()
170 wxFontsManager
*wxFontsManagerBase::Get()
173 ms_instance
= new wxFontsManager();
178 void wxFontsManagerBase::CleanUp()
180 wxDELETE(ms_instance
);
183 wxFontBundle
*wxFontsManagerBase::GetBundle(const wxString
& name
) const
185 return (*m_hash
)[name
.Lower()];
189 wxFontsManagerBase::GetBundleForFont(const wxFontMgrFontRefData
& font
) const
191 wxFontBundle
*bundle
= NULL
;
193 wxString facename
= font
.GetFaceName();
194 if ( !facename
.empty() )
195 bundle
= GetBundle(facename
);
199 facename
= GetDefaultFacename((wxFontFamily
)font
.GetFamily());
200 if ( !facename
.empty() )
201 bundle
= GetBundle(facename
);
206 if ( m_list
->GetFirst() )
207 bundle
= m_list
->GetFirst()->GetData();
209 wxFAIL_MSG(wxT("Fatal error, no fonts available!"));
215 void wxFontsManagerBase::AddBundle(wxFontBundle
*bundle
)
217 (*m_hash
)[bundle
->GetName().Lower()] = bundle
;
218 m_list
->Append(bundle
);
222 // ----------------------------------------------------------------------------
223 // wxFontMgrFontRefData
224 // ----------------------------------------------------------------------------
226 wxFontMgrFontRefData::wxFontMgrFontRefData(int size
,
231 const wxString
& faceName
,
232 wxFontEncoding encoding
)
234 if ( family
== wxFONTFAMILY_DEFAULT
)
235 family
= wxFONTFAMILY_SWISS
;
236 if ( size
== wxDEFAULT
)
239 m_info
.family
= (wxFontFamily
)family
;
240 m_info
.faceName
= faceName
;
241 m_info
.style
= (wxFontStyle
)style
;
242 m_info
.weight
= (wxFontWeight
)weight
;
243 m_info
.pointSize
= size
;
244 m_info
.underlined
= underlined
;
245 m_info
.encoding
= encoding
;
252 wxFontMgrFontRefData::wxFontMgrFontRefData(const wxFontMgrFontRefData
& data
)
254 m_info
= data
.m_info
;
256 m_fontFace
= data
.m_fontFace
;
257 m_fontBundle
= data
.m_fontBundle
;
258 m_fontValid
= data
.m_fontValid
;
260 m_fontFace
->Acquire();
263 wxFontMgrFontRefData::~wxFontMgrFontRefData()
266 m_fontFace
->Release();
269 wxFontBundle
*wxFontMgrFontRefData::GetFontBundle() const
271 wxConstCast(this, wxFontMgrFontRefData
)->EnsureValidFont();
276 wxFontMgrFontRefData::GetFontInstance(float scale
, bool antialiased
) const
278 wxConstCast(this, wxFontMgrFontRefData
)->EnsureValidFont();
279 return m_fontFace
->GetFontInstance(m_info
.pointSize
* scale
,
283 void wxFontMgrFontRefData::SetPointSize(int pointSize
)
285 m_info
.pointSize
= pointSize
;
289 void wxFontMgrFontRefData::SetFamily(wxFontFamily family
)
291 m_info
.family
= family
;
295 void wxFontMgrFontRefData::SetStyle(wxFontStyle style
)
297 m_info
.style
= style
;
301 void wxFontMgrFontRefData::SetWeight(wxFontWeight weight
)
303 m_info
.weight
= weight
;
307 void wxFontMgrFontRefData::SetFaceName(const wxString
& faceName
)
309 m_info
.faceName
= faceName
;
313 void wxFontMgrFontRefData::SetUnderlined(bool underlined
)
315 m_info
.underlined
= underlined
;
319 void wxFontMgrFontRefData::SetEncoding(wxFontEncoding encoding
)
321 m_info
.encoding
= encoding
;
325 void wxFontMgrFontRefData::EnsureValidFont()
329 wxFontFace
*old
= m_fontFace
;
331 m_fontBundle
= wxFontsManager::Get()->GetBundleForFont(*this);
332 m_fontFace
= m_fontBundle
->GetFaceForFont(*this);