]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/fontmgr.cpp
bcb4d7b93c5855969651a3ace9fda033960c7724
[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 wxString wxFontBundle::GetName() const
136 {
137 return wxString::FromAscii(m_fontInfo->familyName);
138 }
139
140 bool wxFontBundle::IsFixed() const
141 {
142 return m_fontInfo->isFixed;
143 }
144
145
146 // ----------------------------------------------------------------------------
147 // wxFontsManager
148 // ----------------------------------------------------------------------------
149
150 wxString wxFontsManager::GetDefaultFacename(wxFontFamily family) const
151 {
152 switch ( family )
153 {
154 case wxSCRIPT:
155 return _T("Script");
156 case wxDECORATIVE:
157 return _T("Charter");
158 case wxROMAN:
159 return _T("Times");
160 case wxTELETYPE:
161 case wxMODERN:
162 return _T("Courier");
163 case wxSWISS:
164 case wxDEFAULT:
165 default:
166 return wxT("Helvetica");
167 }
168 }
169
170 static ibool MGLAPI enum_callback(const font_info_t *info, void *cookie)
171 {
172 wxFontsManager *db = (wxFontsManager*)cookie;
173 db->AddBundle(new wxFontBundle(info));
174 return TRUE;
175 }
176
177 void wxFontsManager::AddAllFonts()
178 {
179 MGL_enumerateFonts(enum_callback, (void*)this);
180 }