1
//--------------------------------------------------------------------------------------
2
// LenMus Phonascus: The teacher of music
3
// Copyright (c) 2002-2007 Cecilio Salmeron
5
// This program is free software; you can redistribute it and/or modify it under the
6
// terms of the GNU General Public License as published by the Free Software Foundation;
7
// either version 2 of the License, or (at your option) any later version.
9
// This program is distributed in the hope that it will be useful, but WITHOUT ANY
10
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11
// PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
// You should have received a copy of the GNU General Public License along with this
14
// program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
15
// Fifth Floor, Boston, MA 02110-1301, USA.
17
// For any comment, suggestion or feature request, please contact the manager of
18
// the project at cecilios@users.sourceforge.net
20
//-------------------------------------------------------------------------------------
21
/*! @file FontManager.cpp
22
@brief Implementation file for class lmFontManager
25
/*! @class lmFontManager
27
@brief Takes care of all font allocation and management duties for a view.
29
The fontsize to use to draw any musical symbol depends on two factors: stafflines
30
spacing and rendering scale.
32
Allocating the font in the lmStaff object would lead to many allocations of the same
33
font. Therefore it's been decided that the staff object will have a pointer to the
34
font, and all fonts will be uniquely allocated in the lmFontManager object.
36
To my knowledge a score will have, at most, two staff sizes: one smaller for melody
37
and a bigger one for piano. So let's assume that five sizes per score is a reasonable
38
high limit. With this asumption, we are going to use a dynamic array structure for
44
// #pragma implementation
47
// For compilers that support precompilation, includes "wx/wx.h".
48
#include "wx/wxprec.h"
58
#include "FontManager.h"
60
// Definition of the FontsList class
61
#include <wx/listimpl.cpp>
62
WX_DEFINE_LIST(FontsList);
66
lmFontManager::lmFontManager()
70
lmFontManager::~lmFontManager()
72
m_cFonts.DeleteContents(true); // so that Clear() will delete all fonts
73
m_cFonts.Clear(); // now, delete all elements
77
wxFont* lmFontManager::GetFont(int nPointSize, wxString sFontName,
78
int nFamily, int nStyle, int nWeight, bool fUnderline)
81
The font is determined by the following parameters:
83
- nPointSize: size in points
84
- nFamily: Supported families are: wxDEFAULT, wxDECORATIVE, wxROMAN, wxSCRIPT, wxSWISS,
85
wxMODERN. wxMODERN is a fixed pitch font; the others are either fixed or variable pitch.
86
- nStyle: The value can be wxNORMAL, wxSLANT or wxITALIC.
87
- nWeight: The value can be wxNORMAL, wxLIGHT or wxBOLD.
88
- fUnderline: The value can be true or false.
89
- sFontName: An string specifying the actual typeface to be used.
94
int nNumFonts = m_cFonts.GetCount();
96
//iterate over the collection of fonts
97
wxFontsListNode* pNode = m_cFonts.GetFirst();
98
pFont = (pNode ? (wxFont *)pNode->GetData() : (wxFont *)pNode);
100
pFont = (wxFont *)pNode->GetData();
101
if ((pFont->GetPointSize() == nPointSize) &&
102
(pFont->GetFaceName() == sFontName) &&
103
((nFamily == wxDEFAULT) || (pFont->GetFamily() == nFamily)) &&
104
(pFont->GetStyle() == nStyle) &&
105
(pFont->GetWeight() == nWeight) &&
106
(pFont->GetUnderlined() == fUnderline)) return pFont;
109
pNode = pNode->GetNext();
113
// Font does not exist. Allocate it.
114
//wxLogMessage(_T("[lmFontManager::GetFont]: Allocating new font. size=%d, name=%s"),
115
// nPointSize, sFontName);
117
pFont = new wxFont(nPointSize, nFamily, nStyle, nWeight, fUnderline,
118
sFontName, wxFONTENCODING_DEFAULT);
121
wxMessageBox(_("Sorry, an error has occurred while allocating the font."),
122
_T("lmFontManager.GetFont"), wxOK);
123
wxLogMessage(wxString::Format(
124
_T("Error allocating font: PointSize=%d, FontName=%s, Family=%d, Style=%d, Weight=%d, Underline=%s"),
125
nPointSize, sFontName, nFamily, nStyle, nWeight, (fUnderline ? _T("yes") : _T("no")) ));
129
// add the new font to the list and return it
130
m_cFonts.Append(pFont);
136
// Load font data from the .ini file. All files are expected to be found in path /bin,
137
// that is, in the same folder than this exe.
139
void lmFontManager::LoadFile(wxString sFileName)
141
wxTextFile oFile; //the file to process
142
wxString sPath = g_pPaths->GetDataPath();
143
wxFileName oFileName(sPath, sFileName, _T("ini"), wxPATH_NATIVE);
145
//loop to read and analyze lines
146
if (!oFile.Open(oFileName.GetFullPath()) ) {
147
wxLogError(wxString::Format( _("Error opening file <%s>"), oFileName.GetFullPath() ));
152
int nPreviousLesson = 0, nCurLesson;
154
for (sLine = oFile.GetFirstLine(); !oFile.Eof(); sLine = oFile.GetNextLine() ) {
155
//process current line
156
if (sLine.Trim() != _T("") && sLine.Mid(0, 2) != _T("//")) {
157
nCurLesson = AnalyzeGlyphLine(nNumLine, sLine);
158
if (nPreviousLesson > nCurLesson) {
159
wxMessageBox(wxString::Format(
160
_("File '%s' is not ordered. File ignored."),
161
oFileName.GetFullPath() ));
163
nPreviousLesson = nCurLesson;
167
//process the last line
168
if (sLine.Trim(false) != _T("") && sLine.Mid(0, 2) != _T("//")) {
169
AnalyzeLine(iFile, nNumLine, sLine);
177
// Analyzes a line and loads it in the corresponding table.
179
// @returns The level and lesson numbers combined (100*level+lesson) for controlling
180
// that the files are ordered
182
int lmFontManager::AnalyzeGlyphLine(int nLine, wxString sLine)
186
long nLevelLesson = 0;
192
//i.e.: <1;04;Sol;c4;c5;0>
195
iSemicolon = sLine.Find(_T(";"));
196
sData = sLine.Left(iSemicolon);
198
fOK = sData.ToLong(&nLevel);
200
sLine = sLine.Mid(iSemicolon + 1); //skip the semicolon
203
iSemicolon = sLine.Find(_T(";"));
204
sData = sLine.Left(iSemicolon);
206
fOK = sData.ToLong(&nLesson);
208
sLine = sLine.Mid(iSemicolon + 1); //skip the semicolon
211
iSemicolon = sLine.Find(_T(";"));
212
wxString sClef = sLine.Left(iSemicolon);
213
EClefType nClef = LDPNameToClef(sClef);
214
wxASSERT(nClef != (EClefType)-1);
217
iSemicolon = sLine.Find(_T(";"));
218
wxString sLowerScope = sLine.Left(iSemicolon);
221
iSemicolon = sLine.Find(_T(";"));
222
wxString sUpperScope = sLine.Left(iSemicolon);
225
bool fOnlyThisLesson = (sLine.Mid(iSemicolon + 1, 1) == _T("1"));
228
m_pLessonsClefs->AddEntry((int)nLevel, (int)nLesson, fOnlyThisLesson, nClef,
229
sLowerScope, sUpperScope);
b'\\ No newline at end of file'