/****************************************************************************
**
-** Copyright (C) 2011 Takumi Asaki
+** Copyright (C) 2012 Takumi Asaki
** All rights reserved.
** Contact: Takumi Asaki (takumi.asaki@gmail.com)
**
#include <QVariant>
#include <QFontInfo>
#include <QFontMetrics>
+#include <QFontDatabase>
+#include <QUrl>
+#include <QTextStream>
+#undef FONTMANAGER_DEBUG_RECOMMENDED_SETTINGS
+
+#ifdef FONTMANAGER_DEBUG_RECOMMENDED_SETTINGS
#include <QDebug>
+#endif
#define FCBIN_PATH "/usr/bin/"
#define FCCACHE_COMMAND "fc-cache"
#define FCLIST_COMMAND "fc-list"
#define FCLIST_OPTION "-v"
-//static QStringList configKeys;
-
FontConfigManager::FontConfigManager(QObject *parent) :
QObject(parent), mLang("en"), mLocalFontsConf(0)
{
connect(this, SIGNAL(localFontsConfPathChanged()), SIGNAL(localFontsConfExistsChanged()));
- connect(this, SIGNAL(localFontsConfPathChanged()), SIGNAL(fontsConfUpdated()));
+ // connect(this, SIGNAL(localFontsConfPathChanged()), SIGNAL(fontsConfUpdated()));
mLocalFontsConf = new FontsConf(this);
mSystemLocalConf = new FontsConf(this);
emit startUpdateFontsConfig();
resetFontsConf();
-#if 0
+#ifdef FONTMANAGER_DEBUG_RECOMMENDED_SETTINGS
QStringList f;
foreach (InstalledFontInfo *info, monospaceFonts)
f << info->localefamily();
if (monospaceFonts.count())
foreach (InstalledFontInfo *info, monospaceFonts) {
addPreferFamily(MONOSPACE_DEF, info->enfamily());
- addPreferFamily(MONOSPACE_DEF, info->localefamily());
}
else if (monospaceSansSerifFonts.count())
foreach (InstalledFontInfo *info, monospaceSansSerifFonts) {
addPreferFamily(MONOSPACE_DEF, info->enfamily());
- addPreferFamily(MONOSPACE_DEF, info->localefamily());
}
else if (monospaceSerifFonts.count())
foreach (InstalledFontInfo *info, monospaceSerifFonts) {
addPreferFamily(MONOSPACE_DEF, info->enfamily());
- addPreferFamily(MONOSPACE_DEF, info->localefamily());
}
if (sansSerifFonts.count())
foreach (InstalledFontInfo *info, sansSerifFonts) {
addPreferFamily(SANSSERIF_DEF, info->enfamily());
- addPreferFamily(SANSSERIF_DEF, info->localefamily());
+ addPreferFamily(SYSTEM_DEF, info->enfamily());
}
else if (monospaceSansSerifFonts.count())
foreach (InstalledFontInfo *info, monospaceSansSerifFonts) {
addPreferFamily(SANSSERIF_DEF, info->enfamily());
- addPreferFamily(SANSSERIF_DEF, info->localefamily());
+ addPreferFamily(SYSTEM_DEF, info->enfamily());
}
else if (unknownFonts.count())
foreach (InstalledFontInfo *info, unknownFonts) {
addPreferFamily(SANSSERIF_DEF, info->enfamily());
- addPreferFamily(SANSSERIF_DEF, info->localefamily());
+ addPreferFamily(SYSTEM_DEF, info->enfamily());
}
else if (monospaceFonts.count())
foreach (InstalledFontInfo *info, monospaceFonts) {
addPreferFamily(SANSSERIF_DEF, info->enfamily());
- addPreferFamily(SANSSERIF_DEF, info->localefamily());
+ addPreferFamily(SYSTEM_DEF, info->enfamily());
}
if (serifFonts.count())
foreach (InstalledFontInfo *info, serifFonts) {
addPreferFamily(SERIF_DEF, info->enfamily());
- addPreferFamily(SERIF_DEF, info->localefamily());
}
else if (monospaceSerifFonts.count())
foreach (InstalledFontInfo *info, monospaceSerifFonts) {
addPreferFamily(SANSSERIF_DEF, info->enfamily());
- addPreferFamily(SANSSERIF_DEF, info->localefamily());
}
else if (unknownFonts.count())
foreach (InstalledFontInfo *info, unknownFonts) {
addPreferFamily(SERIF_DEF, info->enfamily());
- addPreferFamily(SERIF_DEF, info->localefamily());
}
else if (monospaceFonts.count())
foreach (InstalledFontInfo *info, monospaceFonts) {
addPreferFamily(SERIF_DEF, info->enfamily());
- addPreferFamily(SERIF_DEF, info->localefamily());
}
foreach (const QString &f, FontsConf::genericFamilies())
return false;
}
+#define DEFAULT_POINTSIZE 24
+
bool FontConfigManager::maybeMonospaceFont(InstalledFontInfo *info) const
{
- QFont font(info->enfamily());
+ int fontId = -1;
+ QFont font = font4info(info, DEFAULT_POINTSIZE);
+#ifdef FONTMANAGER_DEBUG_RECOMMENDED_SETTINGS
+ qDebug() << "0 - FontConfigManager::maybeMonospaceFont(" << info->enfamily() << info->enstyle() << point << ")" << font.family() << font.exactMatch();
+#endif
+ if (!font.exactMatch() || font.family() != info->enfamily()) {
+ fontId = QFontDatabase::addApplicationFont(info->file());
+#ifdef FONTMANAGER_DEBUG_RECOMMENDED_SETTINGS
+ qDebug() << "\tfontId:" << fontId << info->file();
+#endif
+ if (fontId >= 0) {
+ font = font4info(info, DEFAULT_POINTSIZE);
+#ifdef FONTMANAGER_DEBUG_RECOMMENDED_SETTINGS
+ qDebug() << "1 - FontConfigManager::maybeMonospaceFont(" << info->enfamily() << info->enstyle() << point << ")" << font.family() << font.exactMatch();
+#endif
+ if (!font.exactMatch() || font.family() != info->enfamily()) {
+ font = QFont(info->enfamily());
+#ifdef FONTMANAGER_DEBUG_RECOMMENDED_SETTINGS
+ qDebug() << "2 - FontConfigManager::maybeMonospaceFont(" << info->enfamily() << info->enstyle() << point << ")" << font.family() << font.exactMatch();
+#endif
+ }
+ }
+ }
+ bool isFixed = false;
if (font.exactMatch()) {
QFontInfo fi(font);
- if (fi.fixedPitch())
- return true;
- QFontMetrics fm(font);
- int w = fm.width(QLatin1Char('A'));
- if (fm.width(QLatin1Char('i')) == w && fm.width(QLatin1Char('X')) == w)
- return true;
+#ifdef FONTMANAGER_DEBUG_RECOMMENDED_SETTINGS
+ qDebug() << "\tfixedPitch:" << fi.fixedPitch();
+#endif
+ if (fi.fixedPitch()) {
+ isFixed = true;
+ } else {
+ QFontMetrics fm(font);
+ int w = fm.width(QLatin1Char('A'));
+#ifdef FONTMANAGER_DEBUG_RECOMMENDED_SETTINGS
+ qDebug() << "\twidth:" << w << fm.width(QLatin1Char('i')) << fm.width(QLatin1Char('X'));
+#endif
+ if (fm.width(QLatin1Char('i')) == w && fm.width(QLatin1Char('X')) == w) {
+ isFixed = true;
+ }
+ }
}
+ if (fontId >= 0)
+ QFontDatabase::removeApplicationFont(fontId);
+ if (isFixed)
+ return true;
foreach (const QString &f, info->family()) {
if (f.contains("courier", Qt::CaseInsensitive) ||
f.contains("mono", Qt::CaseInsensitive))
return false;
}
+QFont FontConfigManager::font4info(InstalledFontInfo *info, int pointSize) const
+{
+ QFontDatabase fdb;
+ int point = pointSize;
+ if (!fdb.isScalable(info->enfamily(), info->enstyle())) {
+ QList<int> points = fdb.pointSizes(info->enfamily(), info->enstyle());
+ if (points.count() > 0)
+ point = points.first();
+ }
+ QFont font = fdb.font(info->enfamily(), info->enstyle(), point);
+ return font;
+}
+
void FontConfigManager::runFcCache()
{
QProcess *proc = new QProcess(this);
QByteArray buf = proc->readAllStandardOutput();
QByteArray errbuf = proc->readAllStandardError();
if (!errbuf.isEmpty())
- qWarning() << errbuf;
+ qWarning("%s", errbuf.constData());
Q_ASSERT(errbuf.isEmpty());
static QByteArray emptyLine("\n");
}
} else {
mLocalFontsConf->save(mLocalFontsConfPath);
- if (!check) {
+ if (!check)
emit localFontsConfExistsChanged();
- }
+ }
+}
+
+void FontConfigManager::backupFontsConf(const QString &filepath)
+{
+ if (filepath.isEmpty() || isEmptyLocalFontsConf())
+ return;
+ mLocalFontsConf->save(filepath);
+}
+
+void FontConfigManager::restoreFontsConf(const QString &filepath)
+{
+ if (filepath.isEmpty() || !QFile::exists(filepath))
+ return;
+ FontsConf *restoredConf = new FontsConf(this);
+ restoredConf->load(filepath);
+ if (restoredConf->isValid()) {
+ mLocalFontsConf->copy(restoredConf);
+ emit fontsConfUpdated();
+ delete restoredConf;
}
}
emit fontsConfUpdated();
}
+void FontConfigManager::insertPreferFamily(const QString &family, const QString &value, int index)
+{
+ mLocalFontsConf->insertPreferFamilyFor(family, value, index);
+ emit fontsConfUpdated();
+}
+
+void FontConfigManager::insertAcceptFamily(const QString &family, const QString &value, int index)
+{
+ mLocalFontsConf->insertAcceptFamilyFor(family, value, index);
+ emit fontsConfUpdated();
+}
+
+void FontConfigManager::insertPrependFamily(const QString &family, const QString &value, int index)
+{
+ mLocalFontsConf->insertPrependFamilyFor(family, value, index);
+ emit fontsConfUpdated();
+}
+
+void FontConfigManager::insertAppendFamily(const QString &family, const QString &value, int index)
+{
+ mLocalFontsConf->insertAppendFamilyFor(family, value, index);
+ emit fontsConfUpdated();
+}
+
void FontConfigManager::removePreferFamily(const QString &family, const QString &value)
{
mLocalFontsConf->removePreferFamilyFor(family, value);