Add SVG support

This commit is contained in:
Anton Thomasson 2022-05-29 15:54:11 +02:00
parent 1e6be94965
commit 6c7e09fdb5
14 changed files with 138 additions and 22 deletions

View file

@ -26,6 +26,7 @@ INSTALLS += i18n
system(lrelease $$PWD/translations/*.ts) system(lrelease $$PWD/translations/*.ts)
CONFIG += sailfishapp CONFIG += sailfishapp
QT += svg
PKGCONFIG += mlite5 libcurl poppler glib-2.0 cairo libjpeg PKGCONFIG += mlite5 libcurl poppler glib-2.0 cairo libjpeg
LIBS += -lcurl -lglib-2.0 -lgobject-2.0 -ldl LIBS += -lcurl -lglib-2.0 -lgobject-2.0 -ldl
DEFINES += MADNESS=1 DEFINES += MADNESS=1

View file

@ -75,7 +75,11 @@ ApplicationWindow
} }
function simplifyType(mimetype) { function simplifyType(mimetype) {
if(Mimer.isImage(mimetype)) if(mimetype == Mimer.SVG)
{
return {simple: "svg", translatable: qsTr("SVGs")};
}
else if(Mimer.isImage(mimetype))
{ {
return {simple: "image", translatable: qsTr("images")}; return {simple: "image", translatable: qsTr("images")};
} }

View file

@ -148,7 +148,7 @@ Page {
tag: IppMsg.Keyword tag: IppMsg.Keyword
name: "print-scaling" name: "print-scaling"
prettyName: qsTr("Scaling") prettyName: qsTr("Scaling")
valid: _valid && Mimer.isImage(selectedFileType) valid: _valid && Mimer.isImage(selectedFileType) && selectedFileType != Mimer.SVG
DependentOn { DependentOn {
target: transferFormatSetting target: transferFormatSetting
values: [Mimer.JPEG, Mimer.PNG] values: [Mimer.JPEG, Mimer.PNG]

View file

@ -46,8 +46,7 @@ function supported_formats(printer, considerAdditionalFormats)
mimetypes = mimetypes.concat(Mimer.Mimer.OfficeFormats); mimetypes = mimetypes.concat(Mimer.Mimer.OfficeFormats);
} }
if (raster || has(formats, Mimer.Mimer.JPEG) || has(formats, Mimer.Mimer.PNG) || has(formats, Mimer.Mimer.RBMP) || if(pdf || has(formats, Mimer.Mimer.JPEG) || has(formats, Mimer.Mimer.PNG) || has(formats, Mimer.Mimer.RBMP))
has(formats, Mimer.Mimer.PDF) || has(formats, Mimer.Mimer.Postscript))
{ {
images = true; images = true;
mimetypes.push(Mimer.Mimer.JPEG); mimetypes.push(Mimer.Mimer.JPEG);
@ -56,6 +55,11 @@ function supported_formats(printer, considerAdditionalFormats)
mimetypes.push(Mimer.Mimer.GIF); mimetypes.push(Mimer.Mimer.GIF);
} }
if(pdf)
{
mimetypes.push(Mimer.Mimer.SVG);
}
return {pdf: pdf, postscript: postscript, plaintext: plaintext, office: office, images: images, mimetypes: mimetypes}; return {pdf: pdf, postscript: postscript, plaintext: plaintext, office: office, images: images, mimetypes: mimetypes};
} }
@ -259,6 +263,8 @@ function ippName(name, value, printerStrings)
return qsTr("JPEG"); return qsTr("JPEG");
case Mimer.Mimer.GIF: case Mimer.Mimer.GIF:
return qsTr("GIF"); return qsTr("GIF");
case Mimer.Mimer.SVG:
return qsTr("SVG");
case Mimer.Mimer.RBMP: case Mimer.Mimer.RBMP:
return qsTr("Reverse BMP"); return qsTr("Reverse BMP");
default: default:
@ -401,12 +407,16 @@ function canConvertPlaintextTo(type)
return has(targets, type) return has(targets, type)
} }
function canConvertOfficeDocumentTo(type) function canConvertOfficeDocumentTo(type)
{ {
return has(pdfTargets, type) return has(pdfTargets, type)
} }
function canConvertSvgTo(type)
{
return has(pdfTargets, type)
}
function canConvertImageTo(type) function canConvertImageTo(type)
{ {
var targets = [Mimer.Mimer.OctetStream, Mimer.Mimer.JPEG, Mimer.Mimer.PNG, Mimer.Mimer.RBMP, var targets = [Mimer.Mimer.OctetStream, Mimer.Mimer.JPEG, Mimer.Mimer.PNG, Mimer.Mimer.RBMP,
@ -445,6 +455,10 @@ function fixupChoices(name, choices, mimeType)
{ {
return choices.filter(canConvertOfficeDocumentTo) return choices.filter(canConvertOfficeDocumentTo)
} }
else if(mimeType == Mimer.Mimer.SVG)
{
return choices.filter(canConvertSvgTo)
}
else if(Mimer.Mimer.isImage(mimeType)) else if(Mimer.Mimer.isImage(mimeType))
{ {
return choices.filter(canConvertImageTo); return choices.filter(canConvertImageTo);

View file

@ -383,6 +383,11 @@ QString targetFormatIfAuto(QString documentFormat, QString mimeType, QJsonArray
{ {
return firstMatch(supportedMimeTypes, PdfPrioList); return firstMatch(supportedMimeTypes, PdfPrioList);
} }
else if(documentFormat == Mimer::SVG)
{
QStringList SvgPrioList {Mimer::PWG, Mimer::URF, Mimer::PDF, Mimer::Postscript};
return firstMatch(supportedMimeTypes, SvgPrioList);
}
else if(Mimer::isImage(mimeType)) else if(Mimer::isImage(mimeType))
{ {
QStringList ImageFormatPrioList {Mimer::PNG, Mimer::PWG, Mimer::URF, Mimer::PDF, Mimer::Postscript, Mimer::JPEG}; QStringList ImageFormatPrioList {Mimer::PNG, Mimer::PWG, Mimer::URF, Mimer::PDF, Mimer::Postscript, Mimer::JPEG};
@ -696,7 +701,7 @@ void IppPrinter::print(QJsonObject jobAttrs, QString filename)
{ // Can't process Postscript { // Can't process Postscript
emit doJustUpload(filename, contents); emit doJustUpload(filename, contents);
} }
else if(mimer->isImage(mimeType) && mimer->isImage(targetFormat)) else if((mimeType != Mimer::SVG) && mimer->isImage(mimeType) && mimer->isImage(targetFormat))
{ // Just make sure the image is in the desired format (and jpeg baseline-encoded), don't resize locally { // Just make sure the image is in the desired format (and jpeg baseline-encoded), don't resize locally
emit doPrintImageAsImage(filename, contents, targetFormat); emit doPrintImageAsImage(filename, contents, targetFormat);
} }

View file

@ -1,4 +1,5 @@
#include "mimer.h" #include "mimer.h"
#include <QDebug>
const QString Mimer::OctetStream = "application/octet-stream"; const QString Mimer::OctetStream = "application/octet-stream";
@ -11,6 +12,7 @@ const QString Mimer::PNG = "image/png";
const QString Mimer::GIF = "image/gif"; const QString Mimer::GIF = "image/gif";
const QString Mimer::JPEG = "image/jpeg"; const QString Mimer::JPEG = "image/jpeg";
const QString Mimer::TIFF = "image/tiff"; const QString Mimer::TIFF = "image/tiff";
const QString Mimer::SVG = "image/svg+xml";
const QString Mimer::RBMP = "image/reverse-encoding-bmp"; const QString Mimer::RBMP = "image/reverse-encoding-bmp";
const QString Mimer::DOC = "application/msword"; const QString Mimer::DOC = "application/msword";
@ -51,5 +53,7 @@ Mimer* Mimer::instance()
} }
QString Mimer::get_type(QString filename) { QString Mimer::get_type(QString filename) {
return _db.mimeTypeForFile(filename).name(); QString type = _db.mimeTypeForFile(filename).name();
qDebug() << "MimeType:" << type;
return type;
} }

View file

@ -24,6 +24,7 @@ public:
Q_PROPERTY(const QString GIF MEMBER GIF CONSTANT); Q_PROPERTY(const QString GIF MEMBER GIF CONSTANT);
Q_PROPERTY(const QString JPEG MEMBER JPEG CONSTANT); Q_PROPERTY(const QString JPEG MEMBER JPEG CONSTANT);
Q_PROPERTY(const QString TIFF MEMBER TIFF CONSTANT); Q_PROPERTY(const QString TIFF MEMBER TIFF CONSTANT);
Q_PROPERTY(const QString SVG MEMBER SVG CONSTANT);
Q_PROPERTY(const QString RBMP MEMBER RBMP CONSTANT); Q_PROPERTY(const QString RBMP MEMBER RBMP CONSTANT);
Q_PROPERTY(const QString DOC MEMBER DOC CONSTANT); Q_PROPERTY(const QString DOC MEMBER DOC CONSTANT);
@ -50,6 +51,7 @@ public:
static const QString GIF; static const QString GIF;
static const QString JPEG; static const QString JPEG;
static const QString TIFF; static const QString TIFF;
static const QString SVG;
static const QString RBMP; static const QString RBMP;
static const QString DOC; static const QString DOC;

View file

@ -8,6 +8,7 @@
#include <QTextDocument> #include <QTextDocument>
#include <QPdfWriter> #include <QPdfWriter>
#include <QAbstractTextDocumentLayout> #include <QAbstractTextDocumentLayout>
#include <QtSvg>
#include "ippprinter.h" #include "ippprinter.h"
#include "pdf2printable.h" #include "pdf2printable.h"
#include "ppm2pwg.h" #include "ppm2pwg.h"
@ -258,7 +259,7 @@ catch(const ConvertFailedException& e)
void PrinterWorker::convertImage(QString filename, Bytestream header, PrintParameters Params, QMargins margins) void PrinterWorker::convertImage(QString filename, Bytestream header, PrintParameters Params, QMargins margins)
{ {
try { try {
QString mimeType = Mimer::instance()->get_type(filename);
if(Params.format == PrintParameters::URF && (Params.hwResW != Params.hwResH)) if(Params.format == PrintParameters::URF && (Params.hwResW != Params.hwResH))
{ // URF only supports symmetric resolutions { // URF only supports symmetric resolutions
@ -268,18 +269,6 @@ try {
qDebug() << "Size is" << Params.getPaperSizeWInPixels() << "x" << Params.getPaperSizeHInPixels(); qDebug() << "Size is" << Params.getPaperSizeWInPixels() << "x" << Params.getPaperSizeHInPixels();
QImage inImage;
if(!inImage.load(filename))
{
qDebug() << "failed to load";
throw ConvertFailedException(tr("Failed to load image"));
}
if(inImage.width() > inImage.height())
{
inImage = inImage.transformed(QMatrix().rotate(270.0));
}
int leftMarginPx = (margins.left()/2540.0)*Params.hwResW; int leftMarginPx = (margins.left()/2540.0)*Params.hwResW;
int rightMarginPx = (margins.right()/2540.0)*Params.hwResW; int rightMarginPx = (margins.right()/2540.0)*Params.hwResW;
int topMarginPx = (margins.top()/2540.0)*Params.hwResH; int topMarginPx = (margins.top()/2540.0)*Params.hwResH;
@ -288,8 +277,57 @@ try {
int totalXMarginPx = leftMarginPx+rightMarginPx; int totalXMarginPx = leftMarginPx+rightMarginPx;
int totalYMarginPx = topMarginPx+bottomMarginPx; int totalYMarginPx = topMarginPx+bottomMarginPx;
inImage = inImage.scaled(Params.getPaperSizeWInPixels()-totalXMarginPx, Params.getPaperSizeHInPixels()-totalYMarginPx, size_t targetWidth = Params.getPaperSizeWInPixels()-totalXMarginPx;
Qt::KeepAspectRatio, Qt::SmoothTransformation); size_t targetHeight = Params.getPaperSizeHInPixels()-totalYMarginPx;
QImage inImage;
if(mimeType == Mimer::SVG)
{
QSvgRenderer renderer(filename);
if(!renderer.isValid())
{
qDebug() << "failed to load svg";
throw ConvertFailedException(tr("Failed to load image"));
}
QSize defaultSize = renderer.defaultSize();
QSize targetSize(targetWidth, targetHeight);
if(defaultSize.width() > defaultSize.height())
{
targetSize.transpose();
}
QSize initialSize = defaultSize.scaled(targetSize, Qt::KeepAspectRatio);
inImage = QImage(initialSize, QImage::Format_RGB32);
inImage.fill(QColor("white"));
QPainter painter(&inImage);
renderer.render(&painter);
painter.end(); // Or else the painter segfaults on destruction if we have messed with the image
if(inImage.width() > inImage.height())
{
inImage = inImage.transformed(QMatrix().rotate(270.0));
}
}
else
{
if(!inImage.load(filename))
{
qDebug() << "failed to load";
throw ConvertFailedException(tr("Failed to load image"));
}
if(inImage.width() > inImage.height())
{
inImage = inImage.transformed(QMatrix().rotate(270.0));
}
inImage = inImage.scaled(targetWidth, targetHeight,
Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
if(Params.format == PrintParameters::PDF || Params.format == PrintParameters::Postscript) if(Params.format == PrintParameters::PDF || Params.format == PrintParameters::Postscript)
{ {

View file

@ -611,6 +611,10 @@ auf diesem Drucker</translation>
<source>plaintext</source> <source>plaintext</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>SVGs</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>strings</name> <name>strings</name>
@ -5505,5 +5509,9 @@ auf diesem Drucker</translation>
<source>Reverse BMP</source> <source>Reverse BMP</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>SVG</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
</TS> </TS>

View file

@ -610,6 +610,10 @@
<source>plaintext</source> <source>plaintext</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>SVGs</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>strings</name> <name>strings</name>
@ -5504,5 +5508,9 @@
<source>Reverse BMP</source> <source>Reverse BMP</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>SVG</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
</TS> </TS>

View file

@ -611,6 +611,10 @@ sur cette imprimante</translation>
<source>plaintext</source> <source>plaintext</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>SVGs</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>strings</name> <name>strings</name>
@ -5505,5 +5509,9 @@ sur cette imprimante</translation>
<source>Reverse BMP</source> <source>Reverse BMP</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>SVG</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
</TS> </TS>

View file

@ -610,6 +610,10 @@
<source>plaintext</source> <source>plaintext</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>SVGs</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>strings</name> <name>strings</name>
@ -5504,5 +5508,9 @@
<source>Reverse BMP</source> <source>Reverse BMP</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>SVG</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
</TS> </TS>

View file

@ -610,6 +610,10 @@
<source>plaintext</source> <source>plaintext</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>SVGs</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>strings</name> <name>strings</name>
@ -5504,5 +5508,9 @@
<source>Reverse BMP</source> <source>Reverse BMP</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>SVG</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
</TS> </TS>

View file

@ -610,6 +610,10 @@
<source>plaintext</source> <source>plaintext</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>SVGs</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>strings</name> <name>strings</name>
@ -5504,5 +5508,9 @@
<source>Reverse BMP</source> <source>Reverse BMP</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>SVG</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
</TS> </TS>