Skip to content

Commit 9160d85

Browse files
committed
New SVG Optimizer
Replace SVGO with a custom SVG optimizer.
1 parent 044ae90 commit 9160d85

File tree

12 files changed

+282
-101
lines changed

12 files changed

+282
-101
lines changed

src/app/friction.iss

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,13 @@ WizardImageStretch=yes
3333
Name: "friction"; Description: "Friction"; Types: full compact custom; Flags: fixed
3434
Name: "ffmpeg"; Description: "FFmpeg"; Types: full compact custom; Flags: fixed
3535
Name: "qt"; Description: "Qt"; Types: full compact custom; Flags: fixed
36-
Name: "svgo"; Description: "SVG Optimizer"; Types: full
3736

3837
[Files]
3938
Source: "{#MyAppExeName}"; DestDir: "{app}"; Components: friction ; Flags: ignoreversion
4039
Source: "frictioncore.dll"; DestDir: "{app}"; Components: friction ; Flags: ignoreversion
4140
Source: "frictionui.dll"; DestDir: "{app}"; Components: friction ; Flags: ignoreversion
4241
Source: "skia.dll"; DestDir: "{app}"; Components: friction ; Flags: ignoreversion
4342

44-
Source: "svgo-win.exe"; DestDir: "{app}"; Components: svgo ; Flags: ignoreversion
45-
Source: "svgo-license.txt"; DestDir: "{app}"; Components: svgo ; Flags: ignoreversion
46-
4743
Source: "avcodec-58.dll"; DestDir: "{app}"; Components: ffmpeg ; Flags: ignoreversion
4844
Source: "avdevice-58.dll"; DestDir: "{app}"; Components: ffmpeg ; Flags: ignoreversion
4945
Source: "avformat-58.dll"; DestDir: "{app}"; Components: ffmpeg ; Flags: ignoreversion

src/app/resources.qrc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
<file alias="up-arrow.png">pixmaps/up-arrow.png</file>
2323
<file alias="down-arrow.png">pixmaps/down-arrow.png</file>
2424
</qresource>
25-
<qresource prefix="/config">
26-
<file>svgo.config.js</file>
27-
</qresource>
25+
<qresource prefix="/config"/>
2826
<qresource prefix="/">
2927
<file>presets/render/006-friction-preset-tiff.conf</file>
3028
<file>presets/render/005-friction-preset-png.conf</file>

src/app/svgo.config.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ set(
356356
rendersettings.cpp
357357
renderinstancesettings.cpp
358358
videoencoder.cpp
359+
svgo.cpp
359360
)
360361

361362
set(
@@ -699,6 +700,7 @@ set(
699700
renderinstancesettings.h
700701
videoencoder.h
701702
formatoptions.h
703+
svgo.h
702704
)
703705

704706
if(NOT WIN32)

src/core/appsupport.cpp

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -365,44 +365,6 @@ const QString AppSupport::getAppUserExPresetsPath()
365365
return path;
366366
}
367367

368-
const QString AppSupport::getSVGO()
369-
{
370-
#if defined(Q_OS_WIN)
371-
const QString svgo = "svgo-win.exe";
372-
#elif defined(Q_OS_LINUX)
373-
const QString svgo = "svgo-linux";
374-
#elif defined(Q_OS_MAC)
375-
const QString svgo = "svgo-macos";
376-
#else
377-
const QString svgo = "svgo";
378-
#endif
379-
const QString path = QString("%1/%2").arg(getAppPath(), svgo);
380-
if (QFile::exists(path)) { return path; }
381-
return QStandardPaths::findExecutable("svgo");
382-
}
383-
384-
const QString AppSupport::getSVGOConfig()
385-
{
386-
QString filename = "svgo.config.js";
387-
QString path = getAppConfigPath() + QDir::separator() + filename;
388-
if (!QFile::exists(path)) {
389-
QString config;
390-
QFile file(":/config/" + filename);
391-
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
392-
config = file.readAll();
393-
file.close();
394-
}
395-
if (!config.isEmpty()) {
396-
QFile file(path);
397-
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
398-
file.write(config.toUtf8());
399-
file.close();
400-
}
401-
}
402-
}
403-
return path;
404-
}
405-
406368
const QString AppSupport::getFileMimeType(const QString &path)
407369
{
408370
QMimeDatabase db;

src/core/appsupport.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ class CORE_EXPORT AppSupport : public QObject
9090
static const QString getAppShaderPresetsPath();
9191
static const QString getAppExPresetsPath();
9292
static const QString getAppUserExPresetsPath();
93-
static const QString getSVGO();
94-
static const QString getSVGOConfig();
9593
static const QString getFileMimeType(const QString &path);
9694
static const QString getFileIcon(const QString &path);
9795
static const QPair<QString,QString> getShaderID(const QString &path);

src/core/svgexporter.cpp

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include "canvas.h"
2424

2525
#include "appsupport.h"
26+
#include "svgo.h"
27+
28+
using namespace Friction;
2629

2730
SvgExporter::SvgExporter(const QString& path,
2831
Canvas* const scene,
@@ -35,7 +38,8 @@ SvgExporter::SvgExporter(const QString& path,
3538
const int imageQuality,
3639
bool html,
3740
bool blendMix,
38-
bool colors11)
41+
bool colors11,
42+
bool optimize)
3943
: ComplexTask(INT_MAX, tr("SVG Export"))
4044
, fScene(scene)
4145
, fAbsRange(frameRange)
@@ -47,6 +51,7 @@ SvgExporter::SvgExporter(const QString& path,
4751
, fImageQuality(imageQuality)
4852
, fBlendMix(blendMix)
4953
, fColors11(colors11)
54+
, fOptimize(optimize)
5055
, mHtml(html)
5156
, mOpen(false)
5257
, mFile(path)
@@ -61,20 +66,6 @@ void SvgExporter::nextStep()
6166
if (!mOpen) {
6267
if (mFile.open(QIODevice::WriteOnly)) {
6368
mStream.setDevice(&mFile);
64-
if (mHtml) {
65-
mStream << QString::fromUtf8("<!DOCTYPE html>") << Qt::endl;
66-
mStream << QString::fromUtf8("<html>") << Qt::endl;
67-
mStream << QString::fromUtf8("<head>") << Qt::endl;
68-
mStream << QString::fromUtf8("<meta charset=\"utf-8\" />") << Qt::endl;
69-
mStream << QString::fromUtf8("<title>%1</title>").arg(tr("Preview")) << Qt::endl;
70-
mStream << QString::fromUtf8("<style>html, body { width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden; } html { background: repeating-conic-gradient(#b0b0b0 0% 25%, transparent 0% 50%) 50% / 40px 40px; } svg { margin: auto; width: 100%; height: 100%; object-fit: contain; overflow: hidden; }</style>") << Qt::endl;
71-
mStream << QString::fromUtf8("</head>") << Qt::endl;
72-
mStream << QString::fromUtf8("<body>") << Qt::endl;
73-
} else {
74-
mStream << QString::fromUtf8("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>") << Qt::endl;
75-
}
76-
mStream << QString::fromUtf8("<!-- Created with %1 - %2 -->").arg(AppSupport::getAppDisplayName(),
77-
AppSupport::getAppUrl()) << Qt::endl << Qt::endl;
7869
fScene->saveSceneSVG(*this);
7970
} else {
8071
RuntimeThrow("Could not open:\n\"" + mFile.fileName() + "\"");
@@ -96,11 +87,36 @@ void SvgExporter::finish()
9687
if (mOpen) {
9788
mSvg.appendChild(mDefs);
9889
mDoc.appendChild(mSvg);
99-
mStream << mDoc.toString();
90+
91+
QString result;
92+
if (mHtml) {
93+
result.append("<!DOCTYPE html>");
94+
result.append("<html>");
95+
result.append("<head>");
96+
result.append("<meta charset=\"utf-8\" />");
97+
result.append(QString("<title>%1</title>").arg(tr("Preview")));
98+
result.append("<style>html, body { width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden; } html { background: repeating-conic-gradient(#b0b0b0 0% 25%, transparent 0% 50%) 50% / 40px 40px; } svg { margin: auto; width: 100%; height: 100%; object-fit: contain; overflow: hidden; }</style>");
99+
result.append("</head>");
100+
result.append("<body>");
101+
result.append("\n");
102+
}
103+
if (!mHtml && !fOptimize) {
104+
result.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
105+
result.append("\n");
106+
}
107+
108+
QString content = QString("<!-- Created with %1 - %2 -->").arg(AppSupport::getAppDisplayName(),
109+
AppSupport::getAppUrl());
110+
content.append("\n");
111+
content.append(mDoc.toString());
112+
result.append(fOptimize && !mHtml ? Core::SVGO::optimize(content) : content);
113+
100114
if (mHtml) {
101-
mStream << QString::fromUtf8("</body>") << Qt::endl;
102-
mStream << QString::fromUtf8("</html>") << Qt::endl;
115+
result.append("</body>");
116+
result.append("</html>");
103117
}
118+
119+
mStream << result.toUtf8() << Qt::endl;
104120
mStream.flush();
105121
mFile.close();
106122
}

src/core/svgexporter.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class CORE_EXPORT SvgExporter : public ComplexTask
4343
const int imageQuality = 100,
4444
bool html = false,
4545
bool blendMix = false,
46-
bool colors11 = false);
46+
bool colors11 = false,
47+
bool optimize = false);
4748

4849
void nextStep() override;
4950

@@ -59,6 +60,7 @@ class CORE_EXPORT SvgExporter : public ComplexTask
5960
const int fImageQuality;
6061
const bool fBlendMix;
6162
const bool fColors11;
63+
const bool fOptimize;
6264

6365
QDomElement createElement(const QString& tagName)
6466
{

0 commit comments

Comments
 (0)