1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mgl/fontutil.cpp
3 // Purpose: Font helper functions for MGL
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
19 #include "wx/fontutil.h"
20 #include "wx/encinfo.h"
21 #include "wx/fontmap.h"
22 #include "wx/tokenzr.h"
25 #include "wx/listimpl.cpp"
26 #include "wx/sysopt.h"
27 #include "wx/mgl/private.h"
31 // ============================================================================
33 // ============================================================================
35 // ----------------------------------------------------------------------------
36 // wxNativeEncodingInfo
37 // ----------------------------------------------------------------------------
39 // convert to/from the string representation: format is
40 // encoding[;facename]
41 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
43 wxStringTokenizer
tokenizer(s
, _T(";"));
45 wxString encid
= tokenizer
.GetNextToken();
47 if ( !encid
.ToLong(&enc
) )
49 encoding
= (wxFontEncoding
)enc
;
52 facename
= tokenizer
.GetNextToken();
57 wxString
wxNativeEncodingInfo::ToString() const
63 s
<< _T(';') << facename
;
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
74 wxNativeEncodingInfo
*info
)
76 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
78 if ( encoding
== wxFONTENCODING_DEFAULT
)
80 encoding
= wxFont::GetDefaultEncoding();
85 case wxFONTENCODING_ISO8859_1
:
86 case wxFONTENCODING_ISO8859_2
:
87 case wxFONTENCODING_ISO8859_3
:
88 case wxFONTENCODING_ISO8859_4
:
89 case wxFONTENCODING_ISO8859_5
:
90 case wxFONTENCODING_ISO8859_6
:
91 case wxFONTENCODING_ISO8859_7
:
92 case wxFONTENCODING_ISO8859_8
:
93 case wxFONTENCODING_ISO8859_9
:
94 case wxFONTENCODING_ISO8859_10
:
95 case wxFONTENCODING_ISO8859_11
:
96 case wxFONTENCODING_ISO8859_13
:
97 case wxFONTENCODING_ISO8859_14
:
98 case wxFONTENCODING_ISO8859_15
:
99 info
->mglEncoding
= MGL_ENCODING_ISO8859_1
+
100 (encoding
- wxFONTENCODING_ISO8859_1
);
103 case wxFONTENCODING_KOI8
:
104 info
->mglEncoding
= MGL_ENCODING_KOI8
;
107 case wxFONTENCODING_CP1250
:
108 case wxFONTENCODING_CP1251
:
109 case wxFONTENCODING_CP1252
:
110 case wxFONTENCODING_CP1253
:
111 case wxFONTENCODING_CP1254
:
112 case wxFONTENCODING_CP1255
:
113 case wxFONTENCODING_CP1256
:
114 case wxFONTENCODING_CP1257
:
115 info
->mglEncoding
= MGL_ENCODING_CP1250
+
116 (encoding
- wxFONTENCODING_CP1250
);
119 case wxFONTENCODING_SYSTEM
:
120 info
->mglEncoding
= MGL_ENCODING_ASCII
;
124 // encoding not known to MGL
128 info
->encoding
= encoding
;
133 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
135 if ( !info
.facename
)
138 wxMGLFontFamily
*family
= wxTheFontsManager
->GetFamily(info
.facename
);
141 if ( family
->GetInfo()->fontLibType
== MGL_BITMAPFONT_LIB
)
142 return (info
.mglEncoding
== MGL_ENCODING_ASCII
||
143 info
.mglEncoding
== MGL_ENCODING_ISO8859_1
||
144 info
.mglEncoding
== MGL_ENCODING_ISO8859_15
||
145 info
.mglEncoding
== MGL_ENCODING_CP1252
);
151 // ----------------------------------------------------------------------------
152 // wxFontFamily, wxMGLFontInstance, wxMGLFontLibrary
153 // ----------------------------------------------------------------------------
155 WX_DECLARE_LIST(wxMGLFontInstance
, wxMGLFontInstanceList
);
156 WX_DEFINE_LIST(wxMGLFontInstanceList
);
157 WX_DEFINE_LIST(wxMGLFontFamilyList
);
159 wxMGLFontInstance::wxMGLFontInstance(wxMGLFontLibrary
*fontLib
,
160 float pt
, bool slant
, bool aa
)
168 float slantAngle
= m_slant
? 15.0 : 0.0;
170 wxLogTrace("mgl_font", "loading instance of '%s' slant=%i pt=%0.1f aa=%i",
171 m_fontLib
->GetMGLfont_lib_t()->name
, m_slant
, m_pt
, m_aa
);
172 m_font
= MGL_loadFontInstance(m_fontLib
->GetMGLfont_lib_t(),
173 m_pt
, slantAngle
, 0.0, aa
);
174 wxASSERT_MSG( m_font
, wxT("Cannot create font instance.") );
177 wxMGLFontInstance::~wxMGLFontInstance()
179 wxLogTrace("mgl_font", "unloading instance of '%s' slant=%i pt=%0.1f aa=%i",
180 m_fontLib
->GetMGLfont_lib_t()->name
, m_slant
, m_pt
, m_aa
);
182 MGL_unloadFontInstance(m_font
);
185 wxMGLFontLibrary::wxMGLFontLibrary(const wxString
& filename
, int type
,
186 wxMGLFontFamily
*parentFamily
)
188 m_family
= parentFamily
;
190 m_fileName
= filename
;
194 m_instances
= new wxMGLFontInstanceList
;
195 m_instances
->DeleteContents(TRUE
);
198 wxMGLFontLibrary::~wxMGLFontLibrary()
200 wxLogTrace("mgl_font", "font library dtor '%s'", m_fileName
.mb_str());
204 void wxMGLFontLibrary::IncRef()
206 wxLogTrace("mgl_font", "incRef(%u) '%s'", m_refs
, m_fileName
.c_str());
209 wxLogTrace("mgl_font", "opening library '%s'", m_fileName
.mb_str());
210 m_fontLib
= MGL_openFontLib(m_fileName
.mb_str());
214 void wxMGLFontLibrary::DecRef()
216 wxLogTrace("mgl_font", "decRef(%u) '%s'", m_refs
, m_fileName
.c_str());
219 wxLogTrace("mgl_font", "killing instances of '%s'", m_fileName
.mb_str());
220 m_instances
->Clear();
221 wxLogTrace("mgl_font", "closing library '%s'", m_fileName
.mb_str());
222 MGL_closeFontLib(m_fontLib
);
227 static int gs_antialiasingThreshold
= -1;
229 wxMGLFontInstance
*wxMGLFontLibrary::GetFontInstance(wxFont
*font
,
230 float scale
, bool aa
)
232 wxASSERT_MSG(m_refs
> 0 && m_fontLib
, wxT("font library not loaded!"));
237 float pt
= (float)font
->GetPointSize() * scale
;
239 if ( gs_antialiasingThreshold
== -1 )
241 gs_antialiasingThreshold
= 10;
242 #if wxUSE_SYSTEM_OPTIONS
243 if ( wxSystemOptions::HasOption(wxT("mgl.aa-threshold")) )
244 gs_antialiasingThreshold
=
245 wxSystemOptions::GetOptionInt(wxT("mgl.aa-threshold"));
246 wxLogTrace("mgl_font", "AA threshold set to %i", gs_antialiasingThreshold
);
250 // Small characters don't look good when antialiased with the algorithm
251 // that FreeType uses (mere 2x2 supersampling), so lets disable it AA
252 // completely for small fonts.
253 if ( pt
<= gs_antialiasingThreshold
)
256 antialiased
= (m_fontLib
->fontLibType
== MGL_BITMAPFONT_LIB
) ? FALSE
: aa
;
258 slant
= (((m_type
& wxFONTFACE_ITALIC
) == 0) &&
259 (font
->GetStyle() == wxSLANT
|| font
->GetStyle() == wxITALIC
));
261 // FIXME_MGL -- MGL does not yet support slant, although the API is there
264 wxLogTrace("mgl_font", "requested instance of '%s' slant=%i pt=%0.1f aa=%i",
265 m_fileName
.mb_str(), slant
, pt
, antialiased
);
267 wxMGLFontInstance
*i
;
268 wxMGLFontInstanceList::Node
*node
;
270 for (node
= m_instances
->GetFirst(); node
; node
= node
->GetNext())
273 if ( i
->GetPt() == pt
&& i
->GetSlant() == slant
&&
274 i
->GetAA() == antialiased
)
276 wxLogTrace("mgl_font", " got from cache: slant=%i pt=%0.1f aa=%i",
277 i
->GetSlant(), i
->GetPt(), i
->GetAA());
282 i
= new wxMGLFontInstance(this, pt
, slant
, antialiased
);
283 m_instances
->Append(i
);
288 wxMGLFontFamily::wxMGLFontFamily(const font_info_t
*info
)
290 m_name
= info
->familyName
;
293 if ( info
->regularFace
[0] == '\0' )
294 m_fontLibs
[wxFONTFACE_REGULAR
] = NULL
;
296 m_fontLibs
[wxFONTFACE_REGULAR
] =
297 new wxMGLFontLibrary(info
->regularFace
, wxFONTFACE_REGULAR
, this);
299 if ( info
->italicFace
[0] == '\0' )
300 m_fontLibs
[wxFONTFACE_ITALIC
] = NULL
;
302 m_fontLibs
[wxFONTFACE_ITALIC
] =
303 new wxMGLFontLibrary(info
->italicFace
, wxFONTFACE_ITALIC
, this);
305 if ( info
->boldFace
[0] == '\0' )
306 m_fontLibs
[wxFONTFACE_BOLD
] = NULL
;
308 m_fontLibs
[wxFONTFACE_BOLD
] =
309 new wxMGLFontLibrary(info
->boldFace
, wxFONTFACE_BOLD
, this);
311 if ( info
->boldItalicFace
[0] == '\0' )
312 m_fontLibs
[wxFONTFACE_BOLD_ITALIC
] = NULL
;
314 m_fontLibs
[wxFONTFACE_BOLD_ITALIC
] =
315 new wxMGLFontLibrary(info
->boldItalicFace
, wxFONTFACE_BOLD_ITALIC
, this);
317 wxLogTrace("mgl_font", "new family '%s' (r=%s, i=%s, b=%s, bi=%s)\n",
318 info
->familyName
, info
->regularFace
, info
->italicFace
,
319 info
->boldFace
, info
->boldItalicFace
);
322 wxMGLFontFamily::~wxMGLFontFamily()
324 for (size_t i
= 0; i
< wxFONTFACE_MAX
; i
++)
325 delete m_fontLibs
[i
];
328 bool wxMGLFontFamily::HasFace(int type
) const
330 return (m_fontLibs
[type
] != NULL
);
334 // ----------------------------------------------------------------------------
336 // ----------------------------------------------------------------------------
338 wxMGLFontLibrary
*wxFontsManager::GetFontLibrary(wxFont
*font
)
340 wxMGLFontFamily
*family
;
342 wxString facename
= font
->GetFaceName();
344 if ( !facename
.IsEmpty() )
345 family
= GetFamily(facename
);
352 switch (font
->GetFamily())
355 facename
= wxT("Script");
358 facename
= wxT("Charter");
361 facename
= wxT("Times");
365 facename
= wxT("Courier");
368 facename
= wxT("Helvetica");
372 facename
= wxT("Helvetica");
376 family
= GetFamily(facename
);
379 if ( m_list
->GetFirst() )
380 family
= m_list
->GetFirst()->GetData();
382 wxFAIL_MSG(wxT("Fatal error, no fonts available!"));
386 type
= wxFONTFACE_REGULAR
;
388 if ( font
->GetWeight() == wxBOLD
)
389 type
|= wxFONTFACE_BOLD
;
391 // FIXME_MGL -- this should read "if ( font->GetStyle() == wxITALIC )",
392 // but since MGL does not yet support slant, we try to display it with
393 // italic face (better than nothing...)
394 if ( font
->GetStyle() == wxITALIC
|| font
->GetStyle() == wxSLANT
)
396 if ( family
->HasFace(type
| wxFONTFACE_ITALIC
) )
397 type
|= wxFONTFACE_ITALIC
;
399 if ( !family
->HasFace(type
) )
401 for (int i
= 0; i
< wxFONTFACE_MAX
; i
++)
402 if ( family
->HasFace(i
) )
409 return family
->GetLibrary(type
);
412 static ibool MGLAPI
enum_callback(const font_info_t
*info
, void *cookie
)
414 wxFontsManager
*db
= (wxFontsManager
*)cookie
;
419 wxFontsManager::wxFontsManager()
421 m_hash
= new wxHashTable(wxKEY_STRING
);
422 m_hash
->DeleteContents(FALSE
);
423 m_list
= new wxMGLFontFamilyList
;
424 m_list
->DeleteContents(TRUE
);
425 MGL_enumerateFonts(enum_callback
, (void*)this);
428 wxFontsManager::~wxFontsManager()
434 void wxFontsManager::AddFamily(const font_info_t
*info
)
436 wxMGLFontFamily
*f
= new wxMGLFontFamily(info
);
437 m_hash
->Put(f
->GetName().Lower(), f
);
441 wxMGLFontFamily
*wxFontsManager::GetFamily(const wxString
& name
) const
443 return (wxMGLFontFamily
*)m_hash
->Get(name
.Lower());
447 wxFontsManager
*wxTheFontsManager
= NULL
;