1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mgl/font.cpp
3 // Author: Vaclav Slavik
5 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // ============================================================================
11 // ============================================================================
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
29 #include "wx/settings.h"
30 #include "wx/cmndata.h"
31 #include "wx/gdicmn.h"
34 #include "wx/fontutil.h"
35 #include "wx/tokenzr.h"
36 #include "wx/mgl/private.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 class wxFontRefData
: public wxObjectRefData
45 wxFontRefData(int size
= wxDEFAULT
,
46 int family
= wxDEFAULT
,
47 int style
= wxDEFAULT
,
48 int weight
= wxDEFAULT
,
49 bool underlined
= false,
50 const wxString
& faceName
= wxEmptyString
,
51 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
52 wxFontRefData(const wxFontRefData
& data
);
53 virtual ~wxFontRefData();
56 // common part of all ctors
57 void Init(int pointSize
,
62 const wxString
& faceName
,
63 wxFontEncoding encoding
);
72 wxFontEncoding m_encoding
;
74 wxMGLFontLibrary
*m_library
;
77 wxNativeFontInfo m_info
;
82 #define M_FONTDATA ((wxFontRefData*)m_refData)
84 // ============================================================================
86 // ============================================================================
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 void wxFontRefData::Init(int pointSize
,
97 const wxString
& faceName
,
98 wxFontEncoding encoding
)
100 if ( family
== wxDEFAULT
)
105 m_faceName
= faceName
;
107 if ( style
== wxDEFAULT
)
112 if ( weight
== wxDEFAULT
)
117 if ( pointSize
== wxDEFAULT
)
120 m_pointSize
= pointSize
;
122 m_underlined
= underlined
;
123 m_encoding
= encoding
;
129 wxFontRefData::wxFontRefData(const wxFontRefData
& data
)
131 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
132 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
134 m_library
= data
.m_library
;
135 m_valid
= data
.m_valid
;
138 wxLogTrace("mgl_font", "created fntrefdata %p, library is %p", this, m_library
);
141 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
142 int weight
, bool underlined
,
143 const wxString
& faceName
,
144 wxFontEncoding encoding
)
146 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
147 wxLogTrace("mgl_font", "created fntrefdata %p, library is %p", this, m_library
);
150 wxFontRefData::~wxFontRefData()
152 wxLogTrace("mgl_font", "destructing fntrefdata %p, library is %p", this, m_library
);
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
163 bool wxFont::Create(const wxNativeFontInfo
& info
)
165 return Create(info
.pointSize
, info
.family
, info
.style
, info
.weight
,
166 info
.underlined
, info
.faceName
, info
.encoding
);
169 bool wxFont::Create(int pointSize
,
174 const wxString
& face
,
175 wxFontEncoding encoding
)
177 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
178 underlined
, face
, encoding
);
182 struct font_t
*wxFont::GetMGLfont_t(float scale
, bool antialiased
)
184 if ( !M_FONTDATA
->m_valid
)
186 wxMGLFontLibrary
*old
= M_FONTDATA
->m_library
;
187 M_FONTDATA
->m_library
= wxTheFontsManager
->GetFontLibrary(this);
188 M_FONTDATA
->m_library
->IncRef();
193 wxMGLFontInstance
*instance
=
194 M_FONTDATA
->m_library
->GetFontInstance(this, scale
, antialiased
);
196 return instance
->GetMGLfont_t();
199 wxObjectRefData
*wxFont::CreateRefData() const
201 return new wxFontRefData
;
204 wxObjectRefData
*wxFont::CloneRefData(const wxObjectRefData
*data
) const
206 return new wxFontRefData(*(wxFontRefData
*)data
);
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
214 int wxFont::GetPointSize() const
216 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
218 return M_FONTDATA
->m_pointSize
;
221 wxString
wxFont::GetFaceName() const
223 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
225 return M_FONTDATA
->m_faceName
;
228 int wxFont::GetFamily() const
230 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
232 return M_FONTDATA
->m_family
;
235 int wxFont::GetStyle() const
237 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
239 return M_FONTDATA
->m_style
;
242 int wxFont::GetWeight() const
244 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
246 return M_FONTDATA
->m_weight
;
249 bool wxFont::GetUnderlined() const
251 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
253 return M_FONTDATA
->m_underlined
;
257 wxFontEncoding
wxFont::GetEncoding() const
259 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
261 return M_FONTDATA
->m_encoding
;
264 bool wxFont::IsFixedWidth() const
266 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
268 return (bool)(M_FONTDATA
->m_library
->GetFamily()->GetInfo()->isFixed
);
271 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
273 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
275 M_FONTDATA
->m_info
.InitFromFont(*this);
277 return &(M_FONTDATA
->m_info
);
280 // ----------------------------------------------------------------------------
281 // change font attributes
282 // ----------------------------------------------------------------------------
284 void wxFont::SetPointSize(int pointSize
)
288 M_FONTDATA
->m_pointSize
= pointSize
;
289 M_FONTDATA
->m_valid
= false;
292 void wxFont::SetFamily(int family
)
296 M_FONTDATA
->m_family
= family
;
297 M_FONTDATA
->m_valid
= false;
300 void wxFont::SetStyle(int style
)
304 M_FONTDATA
->m_style
= style
;
305 M_FONTDATA
->m_valid
= false;
308 void wxFont::SetWeight(int weight
)
312 M_FONTDATA
->m_weight
= weight
;
313 M_FONTDATA
->m_valid
= false;
316 bool wxFont::SetFaceName(const wxString
& faceName
)
320 M_FONTDATA
->m_faceName
= faceName
;
321 M_FONTDATA
->m_valid
= false;
323 return wxFontBase::SetFaceName(faceName
);
326 void wxFont::SetUnderlined(bool underlined
)
330 M_FONTDATA
->m_underlined
= underlined
;
333 void wxFont::SetEncoding(wxFontEncoding encoding
)
337 M_FONTDATA
->m_encoding
= encoding
;
338 M_FONTDATA
->m_valid
= false;