2323#include " canvas.h"
2424
2525#include " appsupport.h"
26+ #include " svgo.h"
27+
28+ using namespace Friction ;
2629
2730SvgExporter::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 }
0 commit comments