1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mgl/fontutil.cpp
3 // Purpose: Font helper functions for MGL
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "fontutil.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/fontutil.h"
26 #include "wx/fontmap.h"
27 #include "wx/tokenzr.h"
32 #include "wx/listimpl.cpp"
33 #include "wx/mgl/private.h"
37 // ============================================================================
39 // ============================================================================
41 // ----------------------------------------------------------------------------
42 // wxNativeEncodingInfo
43 // ----------------------------------------------------------------------------
45 // convert to/from the string representation: format is
46 // encoding[;facename]
47 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
49 wxStringTokenizer
tokenizer(s
, _T(";"));
51 wxString encid
= tokenizer
.GetNextToken();
53 if ( !encid
.ToLong(&enc
) )
55 encoding
= (wxFontEncoding
)enc
;
58 facename
= tokenizer
.GetNextToken();
63 wxString
wxNativeEncodingInfo::ToString() const
69 s
<< _T(';') << facename
;
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
80 wxNativeEncodingInfo
*info
)
82 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
84 if ( encoding
== wxFONTENCODING_DEFAULT
)
86 encoding
= wxFont::GetDefaultEncoding();
91 case wxFONTENCODING_ISO8859_1
:
92 case wxFONTENCODING_ISO8859_2
:
93 case wxFONTENCODING_ISO8859_3
:
94 case wxFONTENCODING_ISO8859_4
:
95 case wxFONTENCODING_ISO8859_5
:
96 case wxFONTENCODING_ISO8859_6
:
97 case wxFONTENCODING_ISO8859_7
:
98 case wxFONTENCODING_ISO8859_8
:
99 case wxFONTENCODING_ISO8859_9
:
100 case wxFONTENCODING_ISO8859_10
:
101 case wxFONTENCODING_ISO8859_11
:
102 case wxFONTENCODING_ISO8859_13
:
103 case wxFONTENCODING_ISO8859_14
:
104 case wxFONTENCODING_ISO8859_15
:
105 info
->mglEncoding
= MGL_ENCODING_ISO8859_1
+
106 (encoding
- wxFONTENCODING_ISO8859_1
);
109 case wxFONTENCODING_KOI8
:
110 info
->mglEncoding
= MGL_ENCODING_KOI8
;
113 case wxFONTENCODING_CP1250
:
114 case wxFONTENCODING_CP1251
:
115 case wxFONTENCODING_CP1252
:
116 case wxFONTENCODING_CP1253
:
117 case wxFONTENCODING_CP1254
:
118 case wxFONTENCODING_CP1255
:
119 case wxFONTENCODING_CP1256
:
120 case wxFONTENCODING_CP1257
:
121 info
->mglEncoding
= MGL_ENCODING_CP1250
+
122 (encoding
- wxFONTENCODING_CP1250
);
125 case wxFONTENCODING_SYSTEM
:
126 info
->mglEncoding
= MGL_ENCODING_ASCII
;
130 // encoding not known to MGL
134 info
->encoding
= encoding
;
139 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
141 if ( !info
.facename
)
144 wxMGLFontFamily
*family
= wxTheFontsManager
->GetFamily(info
.facename
);
147 if ( family
->GetInfo()->fontLibType
== MGL_BITMAPFONT_LIB
)
148 return (info
.mglEncoding
== MGL_ENCODING_ASCII
||
149 info
.mglEncoding
== MGL_ENCODING_ISO8859_1
||
150 info
.mglEncoding
== MGL_ENCODING_ISO8859_15
||
151 info
.mglEncoding
== MGL_ENCODING_CP1252
);
157 // ----------------------------------------------------------------------------
158 // wxFontFamily, wxMGLFontInstance, wxMGLFontLibrary
159 // ----------------------------------------------------------------------------
161 WX_DECLARE_LIST(wxMGLFontInstance
, wxMGLFontInstanceList
);
162 WX_DEFINE_LIST(wxMGLFontInstanceList
);
163 WX_DEFINE_LIST(wxMGLFontFamilyList
);
165 wxMGLFontInstance::wxMGLFontInstance(wxMGLFontLibrary
*fontLib
,
166 float pt
, bool slant
, bool aa
)
174 float slantAngle
= m_slant
? 15.0 : 0.0;
176 wxLogTrace("mgl_font", "loading instance of '%s' slant=%i pt=%0.1f aa=%i",
177 m_fontLib
->GetMGLfont_lib_t()->name
, m_slant
, m_pt
, m_aa
);
178 m_font
= MGL_loadFontInstance(m_fontLib
->GetMGLfont_lib_t(),
179 m_pt
, slantAngle
, 0.0, aa
);
180 wxASSERT_MSG( m_font
, wxT("Cannot create font instance.") );
183 wxMGLFontInstance::~wxMGLFontInstance()
185 wxLogTrace("mgl_font", "unloading instance of '%s' slant=%i pt=%0.1f aa=%i",
186 m_fontLib
->GetMGLfont_lib_t()->name
, m_slant
, m_pt
, m_aa
);
188 MGL_unloadFontInstance(m_font
);
191 wxMGLFontLibrary::wxMGLFontLibrary(const wxString
& filename
, int type
)
194 m_fileName
= filename
;
198 m_instances
= new wxMGLFontInstanceList
;
199 m_instances
->DeleteContents(TRUE
);
202 wxMGLFontLibrary::~wxMGLFontLibrary()
204 wxLogTrace("mgl_font", "font library dtor '%s'", m_fileName
.mb_str());
208 void wxMGLFontLibrary::IncRef()
210 wxLogTrace("mgl_font", "incRef(%u) '%s'", m_refs
, m_fileName
.c_str());
213 wxLogTrace("mgl_font", "opening library '%s'", m_fileName
.mb_str());
214 m_fontLib
= MGL_openFontLib(m_fileName
.mb_str());
218 void wxMGLFontLibrary::DecRef()
220 wxLogTrace("mgl_font", "decRef(%u) '%s'", m_refs
, m_fileName
.c_str());
223 wxLogTrace("mgl_font", "killing instances of '%s'", m_fileName
.mb_str());
224 m_instances
->Clear();
225 wxLogTrace("mgl_font", "closing library '%s'", m_fileName
.mb_str());
226 MGL_closeFontLib(m_fontLib
);
231 wxMGLFontInstance
*wxMGLFontLibrary::GetFontInstance(wxFont
*font
,
232 float scale
, bool aa
)
234 wxASSERT_MSG(m_refs
> 0 && m_fontLib
, wxT("font library not loaded!"));
239 (m_fontLib
->fontLibType
== MGL_BITMAPFONT_LIB
) ? FALSE
: aa
;
240 float pt
= (float)font
->GetPointSize() * scale
;
242 slant
= (((m_type
& wxFONTFACE_ITALIC
) == 0) &&
243 (font
->GetStyle() == wxSLANT
|| font
->GetStyle() == wxITALIC
));
245 // FIXME_MGL -- MGL does not yet support slant, although the API is there
248 wxLogTrace("mgl_font", "requested instance of '%s' slant=%i pt=%0.1f aa=%i",
249 m_fileName
.mb_str(), slant
, pt
, antialiased
);
251 wxMGLFontInstance
*i
;
252 wxMGLFontInstanceList::Node
*node
;
254 for (node
= m_instances
->GetFirst(); node
; node
= node
->GetNext())
257 if ( i
->GetPt() == pt
&& i
->GetSlant() == slant
&&
258 i
->GetAA() == antialiased
)
260 wxLogTrace("mgl_font", " got from cache: slant=%i pt=%0.1f aa=%i",
261 i
->GetSlant(), i
->GetPt(), i
->GetAA());
266 i
= new wxMGLFontInstance(this, pt
, slant
, antialiased
);
267 m_instances
->Append(i
);
272 wxMGLFontFamily::wxMGLFontFamily(const font_info_t
*info
)
274 m_name
= info
->familyName
;
277 if ( info
->regularFace
[0] == '\0' )
278 m_fontLibs
[wxFONTFACE_REGULAR
] = NULL
;
280 m_fontLibs
[wxFONTFACE_REGULAR
] =
281 new wxMGLFontLibrary(info
->regularFace
, wxFONTFACE_REGULAR
);
283 if ( info
->italicFace
[0] == '\0' )
284 m_fontLibs
[wxFONTFACE_ITALIC
] = NULL
;
286 m_fontLibs
[wxFONTFACE_ITALIC
] =
287 new wxMGLFontLibrary(info
->italicFace
, wxFONTFACE_ITALIC
);
289 if ( info
->boldFace
[0] == '\0' )
290 m_fontLibs
[wxFONTFACE_BOLD
] = NULL
;
292 m_fontLibs
[wxFONTFACE_BOLD
] =
293 new wxMGLFontLibrary(info
->boldFace
, wxFONTFACE_BOLD
);
295 if ( info
->boldItalicFace
[0] == '\0' )
296 m_fontLibs
[wxFONTFACE_BOLD_ITALIC
] = NULL
;
298 m_fontLibs
[wxFONTFACE_BOLD_ITALIC
] =
299 new wxMGLFontLibrary(info
->boldItalicFace
, wxFONTFACE_BOLD_ITALIC
);
301 wxLogTrace("mgl_font", "new family '%s' (r=%s, i=%s, b=%s, bi=%s)\n",
302 info
->familyName
, info
->regularFace
, info
->italicFace
,
303 info
->boldFace
, info
->boldItalicFace
);
306 wxMGLFontFamily::~wxMGLFontFamily()
308 for (size_t i
= 0; i
< wxFONTFACE_MAX
; i
++)
309 delete m_fontLibs
[i
];
312 bool wxMGLFontFamily::HasFace(int type
) const
314 return (m_fontLibs
[type
] != NULL
);
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 wxMGLFontLibrary
*wxFontsManager::GetFontLibrary(wxFont
*font
)
324 wxMGLFontFamily
*family
;
326 wxString facename
= font
->GetFaceName();
328 if ( !facename
.IsEmpty() )
329 family
= GetFamily(facename
);
336 switch (font
->GetFamily())
339 facename
= wxT("Script");
342 facename
= wxT("Charter");
345 facename
= wxT("Times");
349 facename
= wxT("Courier");
352 facename
= wxT("Helvetica");
356 facename
= wxT("Helvetica");
360 family
= GetFamily(facename
);
363 if ( m_list
->GetFirst() )
364 family
= m_list
->GetFirst()->GetData();
366 wxFAIL_MSG(wxT("Fatal error, no fonts available!"));
370 type
= wxFONTFACE_REGULAR
;
372 if ( font
->GetWeight() == wxBOLD
)
373 type
|= wxFONTFACE_BOLD
;
375 // FIXME_MGL -- this should read "if ( font->GetStyle() == wxITALIC )",
376 // but since MGL does not yet support slant, we try to display it with
377 // italic face (better than nothing...)
378 if ( font
->GetStyle() == wxITALIC
|| font
->GetStyle() == wxSLANT
)
380 if ( family
->HasFace(type
| wxFONTFACE_ITALIC
) )
381 type
|= wxFONTFACE_ITALIC
;
383 if ( !family
->HasFace(type
) )
385 for (int i
= 0; i
< wxFONTFACE_MAX
; i
++)
386 if ( family
->HasFace(i
) )
393 return family
->GetLibrary(type
);
396 static ibool
enum_callback(const font_info_t
*info
, void *cookie
)
398 wxFontsManager
*db
= (wxFontsManager
*)cookie
;
403 wxFontsManager::wxFontsManager()
405 m_hash
= new wxHashTable(wxKEY_STRING
);
406 m_hash
->DeleteContents(FALSE
);
407 m_list
= new wxMGLFontFamilyList
;
408 m_list
->DeleteContents(TRUE
);
409 MGL_enumerateFonts(enum_callback
, (void*)this);
412 wxFontsManager::~wxFontsManager()
418 void wxFontsManager::AddFamily(const font_info_t
*info
)
420 wxMGLFontFamily
*f
= new wxMGLFontFamily(info
);
421 m_hash
->Put(f
->GetName().Lower(), f
);
425 wxMGLFontFamily
*wxFontsManager::GetFamily(const wxString
& name
) const
427 return (wxMGLFontFamily
*)m_hash
->Get(name
.Lower());
431 wxFontsManager
*wxTheFontsManager
= NULL
;