Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 66 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#include <QDebug>
#include <QMessageBox>
#include <QRegularExpression>
#include <QByteArray>
#include <QtEndian>
#include <zlib.h>

#include "iconsimageprovider.h"
#include "iconthemeimageprovider.h"
Expand Down Expand Up @@ -156,6 +159,43 @@ CommandLineOptions getCommandLineOptions(const QApplication& app) {

} // namespace

QByteArray gunzip(const QByteArray &compressed)
{
// Smallest gzip file is 19 bytes:
// 10 headers, 0+ optional fields, 1+ deflate byte, 8 trailer (CRC32 + ISIZE).
if (compressed.size() < 19) {
return {};
}

// Read uncompressed size from last 4 bytes (little-endian).
quint32 size = qFromLittleEndian<quint32>(reinterpret_cast<const uchar*>(
compressed.constData() + compressed.size() - 4));

QByteArray decompressed;
decompressed.resize(size);

z_stream stream{};
stream.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(compressed.data()));
stream.avail_in = compressed.size();
stream.next_out = reinterpret_cast<Bytef*>(decompressed.data());
stream.avail_out = decompressed.size();

if (inflateInit2(&stream, 16 + MAX_WBITS) != Z_OK) {
return {};
}

int ret = inflate(&stream, Z_FINISH);
inflateEnd(&stream);

if (ret != Z_STREAM_END) {
return {};
}

Q_ASSERT(stream.total_out == decompressed.size());

return decompressed;
}

int main(int argc, char *argv[])
{
Sys::initApplicationName();
Expand All @@ -164,14 +204,32 @@ int main(int argc, char *argv[])
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);

// The font is already needed to display our arg parsing error on Linux
int fontId = QFontDatabase::addApplicationFont(":resources/Roboto-Regular.ttf");
if (fontId == -1) {
qDebug() << "Failed to register Roboto font";
} else {
QFont font("Roboto");
font.setPointSize(10);
app.setFont(font);
QString fontNames[] = {
"Roboto",
"NotoSansSymbols",
};

for ( auto& fontName : fontNames )
{
QFile compressedFile(":/resources/" + fontName + "-Regular.ttf.gz");

compressedFile.open(QIODevice::ReadOnly);
QByteArray compressed = compressedFile.readAll();
QByteArray decompressed = gunzip(compressed);


if (!decompressed.isEmpty()) {
if (QFontDatabase::addApplicationFontFromData(decompressed) == -1) {
qDebug() << "Failed to register" << fontName << "font";
} else {
QFont font(fontName);
font.setPointSize(10);
app.setFont(font);
}
}
else {
qDebug() << "Failed to decompress" << fontName << "font";
}
}

CommandLineOptions options = getCommandLineOptions(app);
Expand Down
3 changes: 2 additions & 1 deletion qml.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<qresource prefix="/">
<file>main.qml</file>
<file>qtquickcontrols2.conf</file>
<file>resources/Roboto-Regular.ttf</file>
<file>resources/Roboto-Regular.ttf.gz</file>
<file>resources/NotoSansSymbols-Regular.ttf.gz</file>
<file>resources/updater.png</file>
<file>News.qml</file>
<file>NewsCard.qml</file>
Expand Down
Binary file added resources/NotoSansSymbols-Regular.ttf.gz
Binary file not shown.
Binary file removed resources/Roboto-Regular.ttf
Binary file not shown.
Binary file added resources/Roboto-Regular.ttf.gz
Binary file not shown.