]>
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() ||
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 // if we can't get the exact font requested, substitute it with
134 // some other variant:
135 for (int i
= 0; i
< FaceType_Max
; i
++)
137 if ( HasFace((FaceType
)i
) )
138 return GetFace((FaceType
)i
);
141 wxFAIL_MSG( _T("no face") );
145 return GetFace((FaceType
)type
);
148 // ----------------------------------------------------------------------------
149 // wxFontsManagerBase
150 // ----------------------------------------------------------------------------
152 wxFontsManager
*wxFontsManagerBase::ms_instance
= NULL
;
154 wxFontsManagerBase::wxFontsManagerBase()
156 m_hash
= new wxFontBundleHash();
157 m_list
= new wxFontBundleList
;
158 m_list
->DeleteContents(true);
161 wxFontsManagerBase::~wxFontsManagerBase()
168 wxFontsManager
*wxFontsManagerBase::Get()
171 ms_instance
= new wxFontsManager();
176 void wxFontsManagerBase::CleanUp()
178 wxDELETE(ms_instance
);
181 wxFontBundle
*wxFontsManagerBase::GetBundle(const wxString
& name
) const
183 return (*m_hash
)[name
.Lower()];
187 wxFontsManagerBase::GetBundleForFont(const wxFontMgrFontRefData
& font
) const
189 wxFontBundle
*bundle
= NULL
;
191 wxString facename
= font
.GetFaceName();
192 if ( !facename
.empty() )
193 bundle
= GetBundle(facename
);
197 facename
= GetDefaultFacename((wxFontFamily
)font
.GetFamily());
198 if ( !facename
.empty() )
199 bundle
= GetBundle(facename
);
204 if ( m_list
->GetFirst() )
205 bundle
= m_list
->GetFirst()->GetData();
207 wxFAIL_MSG(wxT("Fatal error, no fonts available!"));
213 void wxFontsManagerBase::AddBundle(wxFontBundle
*bundle
)
215 (*m_hash
)[bundle
->GetName().Lower()] = bundle
;
216 m_list
->Append(bundle
);
220 // ----------------------------------------------------------------------------
221 // wxFontMgrFontRefData
222 // ----------------------------------------------------------------------------
224 wxFontMgrFontRefData::wxFontMgrFontRefData(int size
,
229 const wxString
& faceName
,
230 wxFontEncoding encoding
)
232 if ( family
== wxDEFAULT
)
234 if ( style
== wxDEFAULT
)
236 if ( weight
== wxDEFAULT
)
238 if ( size
== wxDEFAULT
)
241 m_info
.family
= (wxFontFamily
)family
;
242 m_info
.faceName
= faceName
;
243 m_info
.style
= (wxFontStyle
)style
;
244 m_info
.weight
= (wxFontWeight
)weight
;
245 m_info
.pointSize
= size
;
246 m_info
.underlined
= underlined
;
247 m_info
.encoding
= encoding
;
256 wxFontMgrFontRefData::wxFontMgrFontRefData(const wxFontMgrFontRefData
& data
)
258 m_info
= data
.m_info
;
259 m_noAA
= data
.m_noAA
;
261 m_fontFace
= data
.m_fontFace
;
262 m_fontBundle
= data
.m_fontBundle
;
263 m_fontValid
= data
.m_fontValid
;
265 m_fontFace
->Acquire();
268 wxFontMgrFontRefData::~wxFontMgrFontRefData()
271 m_fontFace
->Release();
274 wxFontBundle
*wxFontMgrFontRefData::GetFontBundle() const
276 wxConstCast(this, wxFontMgrFontRefData
)->EnsureValidFont();
281 wxFontMgrFontRefData::GetFontInstance(float scale
, bool antialiased
) const
283 wxConstCast(this, wxFontMgrFontRefData
)->EnsureValidFont();
284 return m_fontFace
->GetFontInstance(m_info
.pointSize
* scale
,
285 antialiased
&& !m_noAA
);
288 void wxFontMgrFontRefData::SetPointSize(int pointSize
)
290 m_info
.pointSize
= pointSize
;
294 void wxFontMgrFontRefData::SetFamily(int family
)
296 m_info
.family
= (wxFontFamily
)family
;
300 void wxFontMgrFontRefData::SetStyle(int style
)
302 m_info
.style
= (wxFontStyle
)style
;
306 void wxFontMgrFontRefData::SetWeight(int weight
)
308 m_info
.weight
= (wxFontWeight
)weight
;
312 void wxFontMgrFontRefData::SetFaceName(const wxString
& faceName
)
314 m_info
.faceName
= faceName
;
318 void wxFontMgrFontRefData::SetUnderlined(bool underlined
)
320 m_info
.underlined
= underlined
;
324 void wxFontMgrFontRefData::SetEncoding(wxFontEncoding encoding
)
326 m_info
.encoding
= encoding
;
330 void wxFontMgrFontRefData::SetNoAntiAliasing(bool no
)
336 void wxFontMgrFontRefData::EnsureValidFont()
340 wxFontFace
*old
= m_fontFace
;
342 m_fontBundle
= wxFontsManager::Get()->GetBundleForFont(*this);
343 m_fontFace
= m_fontBundle
->GetFaceForFont(*this);