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
|
/*
* Copyright (C) 2014 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
* LXQt project: https://lxqt-project.org/
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "fontconfigfile.h"
#include <QTextStream>
#include <QByteArray>
#include <QFile>
#include <QDir>
#include <QDesktopServices>
#include <QStringBuilder>
#include <QDomDocument>
#include <QTimer>
#include <QDebug>
#include <QStandardPaths>
FontConfigFile::FontConfigFile(QObject* parent):
QObject(parent),
mAntialias(true),
mHinting(true),
mSubpixel("rgb"),
mHintStyle("hintslight"),
mDpi(96),
mAutohint(false),
mSaveTimer(nullptr)
{
mDirPath = QString::fromLocal8Bit(qgetenv("XDG_CONFIG_HOME"));
QString homeDir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
if(mDirPath.isEmpty())
mDirPath = homeDir + QStringLiteral("/.config");
mDirPath += QLatin1String("/fontconfig");
mFilePath = mDirPath + QStringLiteral("/fonts.conf");
load();
}
FontConfigFile::~FontConfigFile()
{
if(mSaveTimer) // has pending save request
{
delete mSaveTimer;
mSaveTimer = nullptr;
save();
}
}
void FontConfigFile::setAntialias(bool value)
{
mAntialias = value;
queueSave();
}
void FontConfigFile::setSubpixel(QByteArray value)
{
mSubpixel = value;
queueSave();
}
void FontConfigFile::setHinting(bool value)
{
mHinting = value;
queueSave();
}
void FontConfigFile::setHintStyle(QByteArray value)
{
mHintStyle = value;
queueSave();
}
void FontConfigFile::setDpi(int value)
{
mDpi = value;
queueSave();
}
void FontConfigFile::setAutohint(bool value)
{
mAutohint = value;
queueSave();
}
void FontConfigFile::load()
{
QFile file(mFilePath);
if(file.open(QIODevice::ReadOnly))
{
QByteArray buffer = file.readAll();
file.close();
if(buffer.contains("lxqt-config-appearance")) // the config file is created by us
{
// doing full xml parsing is over-kill. let's use some simpler brute-force methods.
QDomDocument doc;
doc.setContent(&file);
file.close();
QDomElement docElem = doc.documentElement();
QDomNodeList editNodes = docElem.elementsByTagName(QStringLiteral("edit"));
for(int i = 0; i < editNodes.count(); ++i)
{
QDomElement editElem = editNodes.at(i).toElement();
QByteArray name = editElem.attribute(QStringLiteral("name")).toLatin1();
if(name == "antialias")
{
QString value = editElem.firstChildElement(QStringLiteral("bool")).text();
mAntialias = value[0] == QLatin1Char('t') ? true : false;
}
else if(name == "rgba")
{
QString value = editElem.firstChildElement(QStringLiteral("const")).text();
mSubpixel = value.toLatin1();
}
else if(name == "hinting")
{
QString value = editElem.firstChildElement(QStringLiteral("bool")).text();
mHinting = value[0] == QLatin1Char('t') ? true : false;
}
else if(name == "hintstyle")
{
QString value = editElem.firstChildElement(QStringLiteral("const")).text();
mHintStyle = value.toLatin1();
}
else if(name == "dpi")
{
QString value = editElem.firstChildElement(QStringLiteral("double")).text();
mDpi = value.toInt();
}
else if(name == "autohint")
{
QString value = editElem.firstChildElement(QStringLiteral("bool")).text();
mAutohint = value[0] == QLatin1Char('t') ? true : false;
}
}
}
else // the config file is created by others => make a backup and write our config
{
QFile backup(mFilePath + QStringLiteral(".bak"));
if(backup.open(QIODevice::WriteOnly))
{
backup.write(buffer);
backup.close();
}
queueSave(); // overwrite with our file
}
}
}
void FontConfigFile::save()
{
if(mSaveTimer)
{
mSaveTimer->deleteLater();
mSaveTimer = nullptr;
}
QFile file(mFilePath);
QDir().mkdir(mDirPath);
// References: https://wiki.archlinux.org/index.php/Font_Configuration
if(file.open(QIODevice::WriteOnly))
{
QTextStream s(&file);
s <<
"<?xml version=\"1.0\"?>\n"
"<!DOCTYPE fontconfig SYSTEM \"fonts.dtd\">\n"
"<!-- created by lxqt-config-appearance (DO NOT EDIT!) -->\n"
"<fontconfig>\n"
" <include ignore_missing=\"yes\">conf.d</include>\n"
" <match target=\"font\">\n"
" <edit name=\"antialias\" mode=\"assign\">\n"
" <bool>" << (mAntialias ? "true" : "false") << "</bool>\n"
" </edit>\n"
" </match>\n"
" <match target=\"font\">\n"
" <edit name=\"rgba\" mode=\"assign\">\n"
" <const>" << mSubpixel << "</const>\n"
" </edit>\n"
" </match>\n"
" <match target=\"font\">\n"
" <edit name=\"lcdfilter\" mode=\"assign\">\n"
" <const>lcddefault</const>\n"
" </edit>\n"
" </match>\n"
" <match target=\"font\">\n"
" <edit name=\"hinting\" mode=\"assign\">\n"
" <bool>" << (mHinting ? "true" : "false") << "</bool>\n"
" </edit>\n"
" </match>\n"
" <match target=\"font\">\n"
" <edit name=\"hintstyle\" mode=\"assign\">\n"
" <const>" << mHintStyle << "</const>\n"
" </edit>\n"
" </match>\n"
" <match target=\"font\">\n"
" <edit name=\"autohint\" mode=\"assign\">\n"
" <bool>" << (mAutohint ? "true" : "false") << "</bool>\n"
" </edit>\n"
" </match>\n"
" <match target=\"pattern\">\n"
" <edit name=\"dpi\" mode=\"assign\">\n"
" <double>" << mDpi << "</double>\n"
" </edit>\n"
" </match>\n"
"</fontconfig>";
s.flush();
file.close();
}
}
void FontConfigFile::queueSave()
{
if(mSaveTimer)
mSaveTimer->start(1500);
else
{
mSaveTimer = new QTimer();
mSaveTimer->setSingleShot(true);
connect(mSaveTimer, &QTimer::timeout, this, &FontConfigFile::save);
mSaveTimer->start(1500);
}
}
|