~ubuntu-branches/ubuntu/jaunty/fqterm/jaunty

« back to all changes in this revision

Viewing changes to src/common/fqterm_font.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LI Daobing
  • Date: 2009-02-14 09:32:53 UTC
  • Revision ID: james.westby@ubuntu.com-20090214093253-s2e6544ox2aj79rj
Tags: upstream-0.9.3+svn632
ImportĀ upstreamĀ versionĀ 0.9.3+svn632

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   fqterm, a terminal emulator for both BBS and *nix.                    *
 
3
 *   Copyright (C) 2008 fqterm development group.                          *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (at your option) any later version.                                   *
 
9
 *                                                                         *
 
10
 *   This program is distributed in the hope that it will be useful,       *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
 *   GNU General Public License for more details.                          *
 
14
 *                                                                         *
 
15
 *   You should have received a copy of the GNU General Public License     *
 
16
 *   along with this program; if not, write to the                         *
 
17
 *   Free Software Foundation, Inc.,                                       *
 
18
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.               *
 
19
 ***************************************************************************/
 
20
 
 
21
#include <list>
 
22
#include <set>
 
23
#include <utility>
 
24
#include <map>
 
25
 
 
26
#include <QFontDatabase>
 
27
#include <QFontInfo>
 
28
#include <QFile>
 
29
#include <QTextStream>
 
30
#include <QTextCodec>
 
31
#include <QLocale>
 
32
 
 
33
#include "fqterm_trace.h"
 
34
#include "fqterm_font.h"
 
35
#include "fqterm_path.h"
 
36
#include "fqterm_config.h"
 
37
 
 
38
using namespace std;
 
39
 
 
40
namespace FQTerm {
 
41
 
 
42
namespace Font {
 
43
 
 
44
enum Language{US_ENGLISH = 0,
 
45
              SIMPLIFIED_CHINESE,
 
46
              LANGUAGE_COUNT };
 
47
 
 
48
QString getLanguageName(Language lang) {
 
49
  switch (lang){
 
50
    case US_ENGLISH:
 
51
      return "en_US";
 
52
    case SIMPLIFIED_CHINESE:
 
53
      return "zh_CN";
 
54
    default:
 
55
      return "C";
 
56
  }
 
57
}
 
58
 
 
59
Language getLanguageByName(QString langName) {
 
60
  if (langName == "zh_CN") {
 
61
    return SIMPLIFIED_CHINESE;
 
62
  }
 
63
  if (langName == "en_US") {
 
64
    return US_ENGLISH;
 
65
  }
 
66
  return LANGUAGE_COUNT;
 
67
}
 
68
 
 
69
}  // namespace Language
 
70
 
 
71
typedef set<Font::Language> LanguageSet;
 
72
typedef map<QString, pair<QString, LanguageSet> >  FontLanguagesList;
 
73
 
 
74
typedef map<QString, map<Font::Language, QStringList> > FontSets;
 
75
 
 
76
// This function will be called only once.
 
77
static const FontSets &getPreferedFontSets() {
 
78
  static FontSets font_sets;
 
79
  const QString &res_path = getPath(RESOURCE);
 
80
 
 
81
  QString filename = res_path + "default_font.conf";
 
82
 
 
83
  QFile file(filename);
 
84
  if (!file.open(QIODevice::ReadOnly)) {
 
85
    FQ_TRACE("font", 0) << "Failed to open the default font configurations file:"
 
86
                        << filename;
 
87
    return font_sets;
 
88
  }
 
89
 
 
90
  QTextStream is;
 
91
  is.setDevice(&file);
 
92
  is.setCodec(QTextCodec::codecForName("UTF-8"));
 
93
 
 
94
#if defined(WIN32)
 
95
  QString expected_section = "Windows";
 
96
#elif defined(__APPLE__)
 
97
  QString expected_section = "Apple";
 
98
#else
 
99
  QString expected_section = "Linux";
 
100
#endif
 
101
 
 
102
  QString line;
 
103
 
 
104
  QString current_section;
 
105
  while (!is.atEnd()) {
 
106
    line = is.readLine().trimmed();
 
107
    if (line.isEmpty() || line[0] == '#') {
 
108
      continue;
 
109
    }
 
110
 
 
111
    if (line.left(1) == "[" && line.right(1) == "]") {
 
112
      current_section = line.mid(1, line.length() - 2);
 
113
      continue;
 
114
    }
 
115
 
 
116
    if (current_section == expected_section) {
 
117
      QString en_font = line.section('=', 0, 0).trimmed().toLower();
 
118
      QString fonts_for_lang = line.section('=', 1).trimmed();
 
119
 
 
120
      map<Font::Language, QStringList> &font_set = font_sets[en_font];
 
121
 
 
122
      QString lang = fonts_for_lang.section(":", 0, 0).trimmed();
 
123
      QString fonts = fonts_for_lang.section(":", 1).trimmed();
 
124
 
 
125
      QStringList font_list = fonts.split(",", QString::SkipEmptyParts);
 
126
 
 
127
      for (int i = 0; i < font_list.size(); ++i) {
 
128
        font_list[i] = font_list[i].trimmed();
 
129
      }
 
130
 
 
131
      font_set[Font::getLanguageByName(lang)] = font_list;
 
132
    }
 
133
  }
 
134
 
 
135
  return font_sets;
 
136
}
 
137
 
 
138
static int outputAllSystemFonts(const QStringList &fonts) {
 
139
  if (isAllowedCategory("font", 9)) {
 
140
    for (int i = 0; i < fonts.size(); ++i) {
 
141
      FQ_TRACE("font", 9) << "Found a font: "
 
142
                          << (QFontInfo(QFont(fonts.at(i))).fixedPitch() ?
 
143
                              "fixed-pitch     " : "variable-pitch  ")
 
144
                          << fonts.at(i);
 
145
    }
 
146
  }
 
147
  return 0;
 
148
}
 
149
 
 
150
static QStringList &getSystemFontFamilies() {
 
151
  static QStringList list = QFontDatabase().families();
 
152
  // static int tmp = outputAllSystemFonts(list);
 
153
  
 
154
  return list;
 
155
}
 
156
 
 
157
static FontSets &getUserConfigFontSets() {
 
158
  static FontSets font_sets = getPreferedFontSets();
 
159
  return font_sets;
 
160
}
 
161
 
 
162
static QString getEnglishFontFamily() {
 
163
  static const QStringList &families = getSystemFontFamilies();
 
164
  static const FontSets &font_sets = getUserConfigFontSets();
 
165
  static QString en_font_name;
 
166
 
 
167
  if (!en_font_name.isEmpty()) {
 
168
    return en_font_name;
 
169
  }
 
170
 
 
171
  FontSets::const_iterator it = font_sets.find("default");
 
172
  if (it != font_sets.end()) {
 
173
    map<Font::Language, QStringList>::const_iterator itt
 
174
        = it->second.find(Font::US_ENGLISH);
 
175
    if (itt != it->second.end()) {
 
176
      for (int i = 0; i < itt->second.size(); ++i) {
 
177
        const QString &font = itt->second[i];
 
178
        if (families.contains(font)) {
 
179
          en_font_name = font;
 
180
          break;
 
181
        }
 
182
      }
 
183
    }
 
184
  }
 
185
 
 
186
  return en_font_name;
 
187
}
 
188
 
 
189
 
 
190
static QString getFontFamilyForLang(Font::Language lang) {
 
191
  static const QStringList &families = getSystemFontFamilies();
 
192
  static const FontSets &font_sets = getUserConfigFontSets();
 
193
 
 
194
  const QString en_font_name = getEnglishFontFamily();
 
195
 
 
196
  if (lang == Font::US_ENGLISH) {
 
197
    return en_font_name;
 
198
  }
 
199
 
 
200
  FontSets::const_iterator it = font_sets.find(en_font_name.toLower());
 
201
  if (it != font_sets.end()) {
 
202
    map<Font::Language, QStringList>::const_iterator itt
 
203
        = it->second.find(lang);
 
204
    if (itt != it->second.end()) {
 
205
      for (int i = 0; i < itt->second.size(); ++i) {
 
206
        const QString &font = itt->second[i];
 
207
        if (families.contains(font)) {
 
208
          return font;
 
209
        } else {
 
210
          FQ_TRACE("font", 5) << "Font " << font << " for "
 
211
                              << en_font_name << " not found.";
 
212
        }
 
213
      }
 
214
    }
 
215
  } else {
 
216
    FQ_TRACE("font", 3) << "NO fontset for " << en_font_name << " found.";
 
217
  }
 
218
 
 
219
  return en_font_name;
 
220
}
 
221
 
 
222
QString getDefaultFontFamilyForLanguage(bool isEnglish) {
 
223
  Font::Language lang = isEnglish ? Font::US_ENGLISH : Font::SIMPLIFIED_CHINESE;
 
224
  
 
225
  static map<Font::Language, QString> langs_fonts_list;
 
226
 
 
227
  map<Font::Language, QString>::iterator it = langs_fonts_list.find(lang);
 
228
  if (it == langs_fonts_list.end()) {
 
229
    langs_fonts_list.insert(
 
230
        make_pair(lang, getFontFamilyForLang(lang)));
 
231
    it = langs_fonts_list.find(lang);
 
232
 
 
233
    FQ_TRACE("font", 3)  << "Defaut font for "
 
234
                         << Font::getLanguageName(lang)
 
235
                         << " is: "
 
236
                         << it->second
 
237
                         << " (" << it->second.size() << ")";
 
238
  }
 
239
 
 
240
  return it->second;
 
241
}
 
242
 
 
243
static Font::Language getCurrentSystemLanguage() {
 
244
  if (QLocale::system().language() == QLocale::Chinese) {
 
245
    return Font::SIMPLIFIED_CHINESE;
 
246
  }
 
247
 
 
248
  return Font::US_ENGLISH;
 
249
}
 
250
 
 
251
}  // namespace FQTerm