]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/fontmgr.cpp
moved wxMGL's font management code to common so that it can be reused by wxDFB; imple...
[wxWidgets.git] / src / mgl / fontmgr.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mgl/fontmgr.cpp
3 // Purpose: font management for wxMGL
4 // Author: Vaclav Slavik
5 // Created: 2006-11-18
6 // RCS-ID: $Id$
7 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
8 // (c) 2006 REA Elektronik GmbH
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #include "wx/private/fontmgr.h"
20 #include "wx/sysopt.h"
21
22 #include <mgraph.h>
23
24 static int gs_antialiasingThreshold = -1;
25
26 // ============================================================================
27 // implementation
28 // ============================================================================
29
30 // ----------------------------------------------------------------------------
31 // wxFontInstance
32 // ----------------------------------------------------------------------------
33
34 wxFontInstance::wxFontInstance(float ptSize, bool aa, font_lib_t *fontLib)
35 : wxFontInstanceBase(ptSize, aa)
36 {
37 m_font = MGL_loadFontInstance(fontLib, ptSize, 0.0, 0.0, aa);
38
39 wxASSERT_MSG( m_font, _T("cannot create font instance") );
40 }
41
42 wxFontInstance::~wxFontInstance()
43 {
44 if ( m_font )
45 MGL_unloadFontInstance(m_font);
46 }
47
48 // ----------------------------------------------------------------------------
49 // wxFontFace
50 // ----------------------------------------------------------------------------
51
52 void wxFontFace::Acquire()
53 {
54 wxFontFaceBase::Acquire();
55
56 if ( m_refCnt == 1 )
57 {
58 wxCHECK_RET( m_fontLib == NULL, _T("font lib already created") );
59
60 wxLogTrace("mgl_font", "opening library '%s'", m_fileName.mb_str());
61 m_fontLib = MGL_openFontLib(m_fileName.fn_str());
62 }
63 }
64
65 void wxFontFace::Release()
66 {
67 wxFontFaceBase::Release();
68
69 if ( m_refCnt == 0 )
70 {
71 wxCHECK_RET( m_fontLib != NULL, _T("font lib not created") );
72
73 wxLogTrace("mgl_font", "closing library '%s'", m_fileName.mb_str());
74 MGL_closeFontLib(m_fontLib);
75 m_fontLib = NULL;
76 }
77 }
78
79 wxFontInstance *wxFontFace::GetFontInstance(float ptSize, bool aa)
80 {
81 if ( gs_antialiasingThreshold == -1 )
82 {
83 gs_antialiasingThreshold = 10;
84 #if wxUSE_SYSTEM_OPTIONS
85 if ( wxSystemOptions::HasOption(wxT("mgl.aa-threshold")) )
86 gs_antialiasingThreshold =
87 wxSystemOptions::GetOptionInt(wxT("mgl.aa-threshold"));
88 wxLogTrace("mgl_font", "AA threshold set to %i", gs_antialiasingThreshold);
89 #endif
90 }
91
92 // Small characters don't look good when antialiased with the algorithm
93 // that FreeType uses (mere 2x2 supersampling), so lets disable AA
94 // completely for small fonts. Bitmap fonts are not antialiased either.
95 if ( ptSize <= gs_antialiasingThreshold ||
96 m_fontLib->fontLibType == MGL_BITMAPFONT_LIB )
97 {
98 aa = false;
99 }
100
101 return wxFontFaceBase::GetFontInstance(ptSize, aa);
102 }
103
104 wxFontInstance *wxFontFace::CreateFontInstance(float ptSize, bool aa)
105 {
106 wxASSERT_MSG( m_fontLib, _T("font library not loaded!") );
107
108 return new wxFontInstance(ptSize, aa, m_fontLib);
109 }
110
111 // ----------------------------------------------------------------------------
112 // wxFontBundle
113 // ----------------------------------------------------------------------------
114
115 wxFontBundle::wxFontBundle(const font_info_t *info)
116 : m_fontInfo(info)
117 {
118 if ( info->regularFace[0] != '\0' )
119 m_faces[FaceType_Regular] = new wxFontFace(info->regularFace);
120
121 if ( info->italicFace[0] != '\0' )
122 m_faces[FaceType_Italic] = new wxFontFace(info->italicFace);
123
124 if ( info->boldFace[0] != '\0' )
125 m_faces[FaceType_Bold] = new wxFontFace(info->boldFace);
126
127 if ( info->boldItalicFace[0] != '\0' )
128 m_faces[FaceType_BoldItalic] = new wxFontFace(info->boldItalicFace);
129
130 wxLogTrace("mgl_font", "new family '%s' (r=%s, i=%s, b=%s, bi=%s)\n",
131 info->familyName, info->regularFace, info->italicFace,
132 info->boldFace, info->boldItalicFace);
133 }
134
135 // ----------------------------------------------------------------------------
136 // wxFontsManager
137 // ----------------------------------------------------------------------------
138
139 wxString wxFontsManager::GetDefaultFacename(wxFontFamily family) const
140 {
141 switch ( family )
142 {
143 case wxSCRIPT:
144 return _T("Script");
145 case wxDECORATIVE:
146 return _T("Charter");
147 case wxROMAN:
148 return _T("Times");
149 case wxTELETYPE:
150 case wxMODERN:
151 return _T("Courier");
152 case wxSWISS:
153 case wxDEFAULT:
154 default:
155 return wxT("Helvetica");
156 }
157 }
158
159 static ibool MGLAPI enum_callback(const font_info_t *info, void *cookie)
160 {
161 wxFontsManager *db = (wxFontsManager*)cookie;
162 db->AddBundle(new wxFontBundle(info));
163 return TRUE;
164 }
165
166 void wxFontsManager::AddAllFonts()
167 {
168 MGL_enumerateFonts(enum_callback, (void*)this);
169 }