summaryrefslogtreecommitdiffstats
path: root/src/gui/editors/notation/NoteFontFactory.cpp
blob: 2decce4b6c73c8c58aad1f47c866744b48630256 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */

/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.
 
    This program is Copyright 2000-2008
        Guillaume Laurent   <[email protected]>,
        Chris Cannam        <[email protected]>,
        Richard Bown        <[email protected]>
 
    The moral rights of Guillaume Laurent, Chris Cannam, and Richard
    Bown to claim authorship of this work have been asserted.
 
    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/


#include "NoteFontFactory.h"
#include "misc/Debug.h"
#include <kapplication.h>

#include <klocale.h>
#include <kstddirs.h>
#include "misc/Strings.h"
#include "document/ConfigGroups.h"
#include "base/Exception.h"
#include "gui/kdeext/KStartupLogo.h"
#include "NoteFont.h"
#include "NoteFontMap.h"
#include <kconfig.h>
#include <kglobal.h>
#include <kmessagebox.h>
#include <qdir.h>
#include <qstring.h>
#include <qstringlist.h>
#include <algorithm>


namespace Rosegarden
{

std::set<std::string>
NoteFontFactory::getFontNames(bool forceRescan)
{
    NOTATION_DEBUG << "NoteFontFactory::getFontNames: forceRescan = " << forceRescan << endl;

    if (forceRescan)
        m_fontNames.clear();
    if (!m_fontNames.empty())
        return m_fontNames;

    KConfig *config = kapp->config();
    config->setGroup(NotationViewConfigGroup);

    QString fontNameList = "";
    if (!forceRescan) {
        fontNameList = config->readEntry("notefontlist");
    }

    NOTATION_DEBUG << "NoteFontFactory::getFontNames: read from cache: " << fontNameList << endl;

    QStringList names = QStringList::split(",", fontNameList);

    if (names.empty()) {

        NOTATION_DEBUG << "NoteFontFactory::getFontNames: No names available, rescanning..." << endl;

        QString mappingDir =
            KGlobal::dirs()->findResource("appdata", "fonts/mappings/");
        QDir dir(mappingDir);
        if (!dir.exists()) {
            std::cerr << "NoteFontFactory::getFontNames: mapping directory \""
                      << mappingDir << "\" not found" << std::endl;
            return m_fontNames;
        }

        dir.setFilter(QDir::Files | QDir::Readable);
        QStringList files = dir.entryList();
        for (QStringList::Iterator i = files.begin(); i != files.end(); ++i) {

            if ((*i).length() > 4 && (*i).right(4).lower() == ".xml") {

                std::string name(qstrtostr((*i).left((*i).length() - 4)));

                try {
                    NoteFontMap map(name);
                    if (map.ok())
                        names.append(strtoqstr(map.getName()));
                } catch (Exception e) {
                    KStartupLogo::hideIfStillThere();
                    KMessageBox::error(0, strtoqstr(e.getMessage()));
                    throw;
                }
            }
        }
    }

    QString savedNames = "";

    for (QStringList::Iterator i = names.begin(); i != names.end(); ++i) {
        m_fontNames.insert(qstrtostr(*i));
        if (i != names.begin())
            savedNames += ",";
        savedNames += *i;
    }

    config->writeEntry("notefontlist", savedNames);

    return m_fontNames;
}

std::vector<int>
NoteFontFactory::getAllSizes(std::string fontName)
{
    NoteFont *font = getFont(fontName, 0);
    if (!font)
        return std::vector<int>();

    std::set
        <int> s(font->getSizes());
    std::vector<int> v;
    for (std::set
                <int>::iterator i = s.begin(); i != s.end(); ++i) {
            v.push_back(*i);
        }

    std::sort(v.begin(), v.end());
    return v;
}

std::vector<int>
NoteFontFactory::getScreenSizes(std::string fontName)
{
    NoteFont *font = getFont(fontName, 0);
    if (!font)
        return std::vector<int>();

    std::set
        <int> s(font->getSizes());
    std::vector<int> v;
    for (std::set
                <int>::iterator i = s.begin(); i != s.end(); ++i) {
            if (*i <= 16)
                v.push_back(*i);
        }
    std::sort(v.begin(), v.end());
    return v;
}

NoteFont *
NoteFontFactory::getFont(std::string fontName, int size)
{
    std::map<std::pair<std::string, int>, NoteFont *>::iterator i =
        m_fonts.find(std::pair<std::string, int>(fontName, size));

    if (i == m_fonts.end()) {
        try {
            NoteFont *font = new NoteFont(fontName, size);
            m_fonts[std::pair<std::string, int>(fontName, size)] = font;
            return font;
        } catch (Exception e) {
            KStartupLogo::hideIfStillThere();
            KMessageBox::error(0, strtoqstr(e.getMessage()));
            throw;
        }
    } else {
        return i->second;
    }
}

std::string
NoteFontFactory::getDefaultFontName()
{
    static std::string defaultFont = "";
    if (defaultFont != "")
        return defaultFont;

    std::set
        <std::string> fontNames = getFontNames();

    if (fontNames.find("Feta") != fontNames.end())
        defaultFont = "Feta";
    else {
        fontNames = getFontNames(true);
        if (fontNames.find("Feta") != fontNames.end())
            defaultFont = "Feta";
        else if (fontNames.find("Feta Pixmaps") != fontNames.end())
            defaultFont = "Feta Pixmaps";
        else if (fontNames.size() > 0)
            defaultFont = *fontNames.begin();
        else {
            QString message = i18n("Can't obtain a default font -- no fonts found");
            KStartupLogo::hideIfStillThere();
            KMessageBox::error(0, message);
            throw NoFontsAvailable(qstrtostr(message));
        }
    }

    return defaultFont;
}

int
NoteFontFactory::getDefaultSize(std::string fontName)
{
    // always return 8 if it's supported!
    std::vector<int> sizes(getScreenSizes(fontName));
    for (unsigned int i = 0; i < sizes.size(); ++i) {
        if (sizes[i] == 8)
            return sizes[i];
    }
    return sizes[sizes.size() / 2];
}

bool
NoteFontFactory::isAvailableInSize(std::string fontName, int size)
{
    std::vector<int> sizes(getAllSizes(fontName));
    for (unsigned int i = 0; i < sizes.size(); ++i) {
        if (sizes[i] == size)
            return true;
    }
    return false;
}

std::set<std::string> NoteFontFactory::m_fontNames;
std::map<std::pair<std::string, int>, NoteFont *> NoteFontFactory::m_fonts;

}