1
/***************************************************************************
2
* fqterm, a terminal emulator for both BBS and *nix. *
3
* Copyright (C) 2008 fqterm development group. *
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. *
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. *
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
***************************************************************************/
26
#include <QFontDatabase>
29
#include <QTextStream>
33
#include "fqterm_trace.h"
34
#include "fqterm_font.h"
35
#include "fqterm_path.h"
36
#include "fqterm_config.h"
44
enum Language{US_ENGLISH = 0,
48
QString getLanguageName(Language lang) {
52
case SIMPLIFIED_CHINESE:
59
Language getLanguageByName(QString langName) {
60
if (langName == "zh_CN") {
61
return SIMPLIFIED_CHINESE;
63
if (langName == "en_US") {
66
return LANGUAGE_COUNT;
69
} // namespace Language
71
typedef set<Font::Language> LanguageSet;
72
typedef map<QString, pair<QString, LanguageSet> > FontLanguagesList;
74
typedef map<QString, map<Font::Language, QStringList> > FontSets;
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);
81
QString filename = res_path + "default_font.conf";
84
if (!file.open(QIODevice::ReadOnly)) {
85
FQ_TRACE("font", 0) << "Failed to open the default font configurations file:"
92
is.setCodec(QTextCodec::codecForName("UTF-8"));
95
QString expected_section = "Windows";
96
#elif defined(__APPLE__)
97
QString expected_section = "Apple";
99
QString expected_section = "Linux";
104
QString current_section;
105
while (!is.atEnd()) {
106
line = is.readLine().trimmed();
107
if (line.isEmpty() || line[0] == '#') {
111
if (line.left(1) == "[" && line.right(1) == "]") {
112
current_section = line.mid(1, line.length() - 2);
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();
120
map<Font::Language, QStringList> &font_set = font_sets[en_font];
122
QString lang = fonts_for_lang.section(":", 0, 0).trimmed();
123
QString fonts = fonts_for_lang.section(":", 1).trimmed();
125
QStringList font_list = fonts.split(",", QString::SkipEmptyParts);
127
for (int i = 0; i < font_list.size(); ++i) {
128
font_list[i] = font_list[i].trimmed();
131
font_set[Font::getLanguageByName(lang)] = font_list;
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 ")
150
static QStringList &getSystemFontFamilies() {
151
static QStringList list = QFontDatabase().families();
152
// static int tmp = outputAllSystemFonts(list);
157
static FontSets &getUserConfigFontSets() {
158
static FontSets font_sets = getPreferedFontSets();
162
static QString getEnglishFontFamily() {
163
static const QStringList &families = getSystemFontFamilies();
164
static const FontSets &font_sets = getUserConfigFontSets();
165
static QString en_font_name;
167
if (!en_font_name.isEmpty()) {
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)) {
190
static QString getFontFamilyForLang(Font::Language lang) {
191
static const QStringList &families = getSystemFontFamilies();
192
static const FontSets &font_sets = getUserConfigFontSets();
194
const QString en_font_name = getEnglishFontFamily();
196
if (lang == Font::US_ENGLISH) {
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)) {
210
FQ_TRACE("font", 5) << "Font " << font << " for "
211
<< en_font_name << " not found.";
216
FQ_TRACE("font", 3) << "NO fontset for " << en_font_name << " found.";
222
QString getDefaultFontFamilyForLanguage(bool isEnglish) {
223
Font::Language lang = isEnglish ? Font::US_ENGLISH : Font::SIMPLIFIED_CHINESE;
225
static map<Font::Language, QString> langs_fonts_list;
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);
233
FQ_TRACE("font", 3) << "Defaut font for "
234
<< Font::getLanguageName(lang)
237
<< " (" << it->second.size() << ")";
243
static Font::Language getCurrentSystemLanguage() {
244
if (QLocale::system().language() == QLocale::Chinese) {
245
return Font::SIMPLIFIED_CHINESE;
248
return Font::US_ENGLISH;
251
} // namespace FQTerm