]>
git.saurik.com Git - wxWidgets.git/blob - src/common/fontmgrcmn.cpp
2c4c06c3b29468239cc51df07aa30ab27aad0a8c
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() ||
114 GetName().CmpNoCase(font
.GetFaceName()) == 0,
115 _T("calling GetFaceForFont for incompatible font") );
117 int type
= FaceType_Regular
;
119 if ( font
.GetWeight() == wxBOLD
)
120 type
|= FaceType_Bold
;
122 // FIXME -- this should read "if ( font->GetStyle() == wxITALIC )",
123 // but since MGL neither DFB supports slant, we try to display it with
124 // italic face (better than nothing...)
125 if ( font
.GetStyle() == wxITALIC
|| font
.GetStyle() == wxSLANT
)
127 if ( HasFace((FaceType
)(type
| FaceType_Italic
)) )
128 type
|= FaceType_Italic
;
131 if ( !HasFace((FaceType
)type
) )
133 for (int i
= 0; i
< FaceType_Max
; i
++)
135 if ( HasFace((FaceType
)i
) )
136 return GetFace((FaceType
)i
);
139 wxFAIL_MSG( _T("no face") );
143 return GetFace((FaceType
)type
);
146 // ----------------------------------------------------------------------------
147 // wxFontsManagerBase
148 // ----------------------------------------------------------------------------
150 wxFontsManager
*wxFontsManagerBase::ms_instance
= NULL
;
152 wxFontsManagerBase::wxFontsManagerBase()
154 m_hash
= new wxFontBundleHash();
155 m_list
= new wxFontBundleList
;
156 m_list
->DeleteContents(true);
159 wxFontsManagerBase::~wxFontsManagerBase()
166 wxFontsManager
*wxFontsManagerBase::Get()
169 ms_instance
= new wxFontsManager();
174 void wxFontsManagerBase::CleanUp()
176 wxDELETE(ms_instance
);
179 wxFontBundle
*wxFontsManagerBase::GetBundle(const wxString
& name
) const
181 return (*m_hash
)[name
.Lower()];
185 wxFontsManagerBase::GetBundleForFont(const wxFontMgrFontRefData
& font
) const
187 wxFontBundle
*bundle
= NULL
;
189 wxString facename
= font
.GetFaceName();
190 if ( !facename
.empty() )
191 bundle
= GetBundle(facename
);
195 facename
= GetDefaultFacename((wxFontFamily
)font
.GetFamily());
196 if ( !facename
.empty() )
197 bundle
= GetBundle(facename
);
202 if ( m_list
->GetFirst() )
203 bundle
= m_list
->GetFirst()->GetData();
205 wxFAIL_MSG(wxT("Fatal error, no fonts available!"));
211 void wxFontsManagerBase::AddBundle(wxFontBundle
*bundle
)
213 (*m_hash
)[bundle
->GetName().Lower()] = bundle
;
214 m_list
->Append(bundle
);
218 // ----------------------------------------------------------------------------
219 // wxFontMgrFontRefData
220 // ----------------------------------------------------------------------------
222 wxFontMgrFontRefData::wxFontMgrFontRefData(int size
,
227 const wxString
& faceName
,
228 wxFontEncoding encoding
)
230 if ( family
== wxDEFAULT
)
232 if ( style
== wxDEFAULT
)
234 if ( weight
== wxDEFAULT
)
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
;
254 wxFontMgrFontRefData::wxFontMgrFontRefData(const wxFontMgrFontRefData
& data
)
256 m_info
= data
.m_info
;
257 m_noAA
= data
.m_noAA
;
259 m_fontFace
= data
.m_fontFace
;
260 m_fontBundle
= data
.m_fontBundle
;
261 m_fontValid
= data
.m_fontValid
;
263 m_fontFace
->Acquire();
266 wxFontMgrFontRefData::~wxFontMgrFontRefData()
269 m_fontFace
->Release();
272 wxFontBundle
*wxFontMgrFontRefData::GetFontBundle() const
274 wxConstCast(this, wxFontMgrFontRefData
)->EnsureValidFont();
279 wxFontMgrFontRefData::GetFontInstance(float scale
, bool antialiased
) const
281 wxConstCast(this, wxFontMgrFontRefData
)->EnsureValidFont();
282 return m_fontFace
->GetFontInstance(m_info
.pointSize
* scale
,
283 antialiased
&& !m_noAA
);
286 void wxFontMgrFontRefData::SetPointSize(int pointSize
)
288 m_info
.pointSize
= pointSize
;
292 void wxFontMgrFontRefData::SetFamily(int family
)
294 m_info
.family
= (wxFontFamily
)family
;
298 void wxFontMgrFontRefData::SetStyle(int style
)
300 m_info
.style
= (wxFontStyle
)style
;
304 void wxFontMgrFontRefData::SetWeight(int weight
)
306 m_info
.weight
= (wxFontWeight
)weight
;
310 void wxFontMgrFontRefData::SetFaceName(const wxString
& faceName
)
312 m_info
.faceName
= faceName
;
316 void wxFontMgrFontRefData::SetUnderlined(bool underlined
)
318 m_info
.underlined
= underlined
;
322 void wxFontMgrFontRefData::SetEncoding(wxFontEncoding encoding
)
324 m_info
.encoding
= encoding
;
328 void wxFontMgrFontRefData::SetNoAntiAliasing(bool no
)
334 void wxFontMgrFontRefData::EnsureValidFont()
338 wxFontFace
*old
= m_fontFace
;
340 m_fontBundle
= wxFontsManager::Get()->GetBundleForFont(*this);
341 m_fontFace
= m_fontBundle
->GetFaceForFont(*this);