Skip to content

Commit

Permalink
Support Optimize SVG
Browse files Browse the repository at this point in the history
Powered by SVGO (https://svgo.dev).

Fix #213
  • Loading branch information
rodlie committed Jul 26, 2024
1 parent 8db61c8 commit 9751d85
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/app/resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@
<file alias="up-arrow.png">pixmaps/up-arrow.png</file>
<file alias="down-arrow.png">pixmaps/down-arrow.png</file>
</qresource>
<qresource prefix="/config">
<file>svgo.config.js</file>
</qresource>
</RCC>
9 changes: 9 additions & 0 deletions src/app/svgo.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
plugins: [
'removeXMLProcInst',
'collapseGroups',
'removeUselessDefs',
'removeEmptyContainers',
],
};

47 changes: 45 additions & 2 deletions src/core/appsupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ const QString AppSupport::getAppConfigPath()
return path;
}

const QString AppSupport::getAppPath()
{
return QApplication::applicationDirPath();
}

const QString AppSupport::getAppOutputProfilesPath()
{
QString path = QString::fromUtf8("%1/OutputProfiles").arg(getAppConfigPath());
Expand Down Expand Up @@ -227,7 +232,7 @@ const QString AppSupport::getAppShaderEffectsPath(bool restore)

const QString AppSupport::getAppShaderPresetsPath()
{
QString path1 = QApplication::applicationDirPath();
QString path1 = getAppPath();
QString path2 = path1;
path1.append(QString::fromUtf8("/plugins/shaders"));
path2.append(QString::fromUtf8("/../share/friction/plugins/shaders"));
Expand All @@ -238,7 +243,7 @@ const QString AppSupport::getAppShaderPresetsPath()

const QString AppSupport::getAppExPresetsPath()
{
QString path1 = QApplication::applicationDirPath();
QString path1 = getAppPath();
QString path2 = path1;
path1.append(QString::fromUtf8("/presets/expressions"));
path2.append(QString::fromUtf8("/../share/friction/presets/expressions"));
Expand All @@ -255,6 +260,44 @@ const QString AppSupport::getAppUserExPresetsPath()
return path;
}

const QString AppSupport::getSVGO()
{
#ifdef Q_OS_WIN
const QString svgo = "svgo-win.exe";
#else
const QString svgo = "svgo-linux";
#endif
const QString path = QString("%1%2%3").arg(getAppPath(),
QDir::separator(),
svgo);
qDebug() << "check for svgo" << path;
if (QFile::exists(path)) { return path; }
return QString();
}

const QString AppSupport::getSVGOConfig()
{
QString filename = "svgo.config.js";
QString path = getAppConfigPath() + QDir::separator() + filename;
qDebug() << "check for" << filename;
if (!QFile::exists(path)) {
QString config;
QFile file(":/config/" + filename);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
config = file.readAll();
file.close();
}
if (!config.isEmpty()) {
QFile file(path);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
file.write(config.toUtf8());
file.close();
}
}
}
return path;
}

const QString AppSupport::getFileMimeType(const QString &path)
{
QMimeDatabase db;
Expand Down
3 changes: 3 additions & 0 deletions src/core/appsupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ class CORE_EXPORT AppSupport : public QObject
static const QString getAppCommitUrl();
static const QString getAppBranchUrl();
static const QString getAppConfigPath();
static const QString getAppPath();
static const QString getAppOutputProfilesPath();
static const QString getAppPathEffectsPath();
static const QString getAppRasterEffectsPath();
static const QString getAppShaderEffectsPath(bool restore = false);
static const QString getAppShaderPresetsPath();
static const QString getAppExPresetsPath();
static const QString getAppUserExPresetsPath();
static const QString getSVGO();
static const QString getSVGOConfig();
static const QString getFileMimeType(const QString &path);
static const QString getFileIcon(const QString &path);
static const QPair<QString,QString> getShaderID(const QString &path);
Expand Down
30 changes: 29 additions & 1 deletion src/ui/dialogs/exportsvgdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include <QMenuBar>
#include <QDesktopServices>
#include <QPlainTextEdit>
#include <QProcess>
#include <QMessageBox>

ExportSvgDialog::ExportSvgDialog(QWidget* const parent,
const QString &warnings)
Expand Down Expand Up @@ -88,6 +90,12 @@ ExportSvgDialog::ExportSvgDialog(QWidget* const parent,
"loop",
true).toBool());

mOptimize = new QCheckBox(tr("Optimize"), this);
mOptimize->setChecked(AppSupport::getSettings("exportSVG",
"optimize",
false).toBool());
mOptimize->setDisabled(AppSupport::getSVGO().isEmpty());

connect(mBackground, &QCheckBox::stateChanged,
this, [this] {
AppSupport::setSettings("exportSVG",
Expand All @@ -106,6 +114,12 @@ ExportSvgDialog::ExportSvgDialog(QWidget* const parent,
"loop",
mLoop->isChecked());
});
connect(mOptimize, &QCheckBox::stateChanged,
this, [this] {
AppSupport::setSettings("exportSVG",
"optimize",
mOptimize->isChecked());
});

twoColLayout->addPair(new QLabel(tr("Scene:")), sceneButton);
twoColLayout->addPair(new QLabel(tr("First Frame:")), mFirstFrame);
Expand Down Expand Up @@ -160,6 +174,7 @@ ExportSvgDialog::ExportSvgDialog(QWidget* const parent,
settingsLayout->addWidget(mBackground);
settingsLayout->addWidget(mFixedSize);
settingsLayout->addWidget(mLoop);
settingsLayout->addWidget(mOptimize);
settingsLayout->addStretch();

connect(mFirstFrame, qOverload<int>(&QSpinBox::valueChanged),
Expand Down Expand Up @@ -190,7 +205,20 @@ ExportSvgDialog::ExportSvgDialog(QWidget* const parent,
"recentExported",
saveInfo.absoluteDir().absolutePath());
const bool success = exportTo(saveAs);
if (success) { accept(); }
if (success) {
if (mOptimize->isEnabled() &&
mOptimize->isChecked()) {
if (!QProcess::startDetached(AppSupport::getSVGO(),
QStringList() << "--config"
<< AppSupport::getSVGOConfig()
<< saveAs)) {
QMessageBox::warning(this,
tr("Optimizer Failed"),
tr("Failed to launch SVG optimizer."));
}
}
accept();
}
});

connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
Expand Down
1 change: 1 addition & 0 deletions src/ui/dialogs/exportsvgdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class UI_EXPORT ExportSvgDialog : public QDialog
QCheckBox *mBackground;
QCheckBox *mFixedSize;
QCheckBox *mLoop;
QCheckBox *mOptimize;

QComboBox *mImageFormat;
QSpinBox *mImageQuality;
Expand Down

0 comments on commit 9751d85

Please sign in to comment.