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: fontmgr.cpp 43598 2006-07-25 06:48:03Z ABX $
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#ifndef WX_PRECOMP
20    #include "wx/log.h"
21#endif
22
23#include "wx/private/fontmgr.h"
24#include "wx/sysopt.h"
25
26#include <mgraph.h>
27
28static int gs_antialiasingThreshold = -1;
29
30// ============================================================================
31// implementation
32// ============================================================================
33
34// ----------------------------------------------------------------------------
35// wxFontInstance
36// ----------------------------------------------------------------------------
37
38wxFontInstance::wxFontInstance(float ptSize, bool aa, font_lib_t *fontLib)
39    : wxFontInstanceBase(ptSize, aa)
40{
41    m_font = MGL_loadFontInstance(fontLib, ptSize, 0.0, 0.0, aa);
42
43    wxASSERT_MSG( m_font, _T("cannot create font instance") );
44}
45
46wxFontInstance::~wxFontInstance()
47{
48    if ( m_font )
49        MGL_unloadFontInstance(m_font);
50}
51
52// ----------------------------------------------------------------------------
53// wxFontFace
54// ----------------------------------------------------------------------------
55
56void wxFontFace::Acquire()
57{
58    wxFontFaceBase::Acquire();
59
60    if ( m_refCnt == 1 )
61    {
62        wxCHECK_RET( m_fontLib == NULL, _T("font lib already created") );
63
64        wxLogTrace("mgl_font", "opening library '%s'", m_fileName.mb_str());
65        m_fontLib = MGL_openFontLib(m_fileName.fn_str());
66    }
67}
68
69void wxFontFace::Release()
70{
71    wxFontFaceBase::Release();
72
73    if ( m_refCnt == 0 )
74    {
75        wxCHECK_RET( m_fontLib != NULL, _T("font lib not created") );
76
77        wxLogTrace("mgl_font", "closing library '%s'", m_fileName.mb_str());
78        MGL_closeFontLib(m_fontLib);
79        m_fontLib = NULL;
80    }
81}
82
83wxFontInstance *wxFontFace::GetFontInstance(float ptSize, bool aa)
84{
85    if ( gs_antialiasingThreshold == -1 )
86    {
87        gs_antialiasingThreshold = 10;
88#if wxUSE_SYSTEM_OPTIONS
89        if ( wxSystemOptions::HasOption(wxT("mgl.aa-threshold")) )
90            gs_antialiasingThreshold =
91                wxSystemOptions::GetOptionInt(wxT("mgl.aa-threshold"));
92        wxLogTrace("mgl_font", "AA threshold set to %i", gs_antialiasingThreshold);
93#endif
94    }
95
96    // Small characters don't look good when antialiased with the algorithm
97    // that FreeType uses (mere 2x2 supersampling), so lets disable AA
98    // completely for small fonts. Bitmap fonts are not antialiased either.
99    if ( ptSize <= gs_antialiasingThreshold ||
100         m_fontLib->fontLibType == MGL_BITMAPFONT_LIB )
101    {
102        aa = false;
103    }
104
105    return wxFontFaceBase::GetFontInstance(ptSize, aa);
106}
107
108wxFontInstance *wxFontFace::CreateFontInstance(float ptSize, bool aa)
109{
110    wxASSERT_MSG( m_fontLib, _T("font library not loaded!") );
111
112    return new wxFontInstance(ptSize, aa, m_fontLib);
113}
114
115// ----------------------------------------------------------------------------
116// wxFontBundle
117// ----------------------------------------------------------------------------
118
119wxFontBundle::wxFontBundle(const font_info_t *info)
120    : m_fontInfo(info)
121{
122    if ( info->regularFace[0] != '\0' )
123        m_faces[FaceType_Regular] = new wxFontFace(info->regularFace);
124
125    if ( info->italicFace[0] != '\0' )
126        m_faces[FaceType_Italic] = new wxFontFace(info->italicFace);
127
128    if ( info->boldFace[0] != '\0' )
129        m_faces[FaceType_Bold] = new wxFontFace(info->boldFace);
130
131    if ( info->boldItalicFace[0] != '\0' )
132        m_faces[FaceType_BoldItalic] = new wxFontFace(info->boldItalicFace);
133
134    wxLogTrace("mgl_font", "new family '%s' (r=%s, i=%s, b=%s, bi=%s)\n",
135               info->familyName, info->regularFace, info->italicFace,
136               info->boldFace, info->boldItalicFace);
137}
138
139wxString wxFontBundle::GetName() const
140{
141    return wxString::FromAscii(m_fontInfo->familyName);
142}
143
144bool wxFontBundle::IsFixed() const
145{
146    return m_fontInfo->isFixed;
147}
148
149
150// ----------------------------------------------------------------------------
151// wxFontsManager
152// ----------------------------------------------------------------------------
153
154wxString wxFontsManager::GetDefaultFacename(wxFontFamily family) const
155{
156    switch ( family )
157    {
158        case wxSCRIPT:
159            return _T("Script");
160        case wxDECORATIVE:
161            return _T("Charter");
162        case wxROMAN:
163            return _T("Times");
164        case wxTELETYPE:
165        case wxMODERN:
166            return _T("Courier");
167        case wxSWISS:
168        case wxDEFAULT:
169        default:
170            return wxT("Helvetica");
171    }
172}
173
174static ibool MGLAPI enum_callback(const font_info_t *info, void *cookie)
175{
176    wxFontsManager *db = (wxFontsManager*)cookie;
177    db->AddBundle(new wxFontBundle(info));
178    return TRUE;
179}
180
181void wxFontsManager::AddAllFonts()
182{
183    MGL_enumerateFonts(enum_callback, (void*)this);
184}
185