Skip to content

Commit

Permalink
Add SVG support
Browse files Browse the repository at this point in the history
  • Loading branch information
attah committed May 30, 2022
1 parent 1e6be94 commit 6c7e09f
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 22 deletions.
1 change: 1 addition & 0 deletions harbour-seaprint.pro
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ INSTALLS += i18n
system(lrelease $$PWD/translations/*.ts)

CONFIG += sailfishapp
QT += svg
PKGCONFIG += mlite5 libcurl poppler glib-2.0 cairo libjpeg
LIBS += -lcurl -lglib-2.0 -lgobject-2.0 -ldl
DEFINES += MADNESS=1
Expand Down
6 changes: 5 additions & 1 deletion qml/harbour-seaprint.qml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ ApplicationWindow
}

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")};
}
Expand Down
2 changes: 1 addition & 1 deletion qml/pages/PrinterPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Page {
tag: IppMsg.Keyword
name: "print-scaling"
prettyName: qsTr("Scaling")
valid: _valid && Mimer.isImage(selectedFileType)
valid: _valid && Mimer.isImage(selectedFileType) && selectedFileType != Mimer.SVG
DependentOn {
target: transferFormatSetting
values: [Mimer.JPEG, Mimer.PNG]
Expand Down
20 changes: 17 additions & 3 deletions qml/pages/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ function supported_formats(printer, considerAdditionalFormats)
mimetypes = mimetypes.concat(Mimer.Mimer.OfficeFormats);
}

if (raster || 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))
if(pdf || has(formats, Mimer.Mimer.JPEG) || has(formats, Mimer.Mimer.PNG) || has(formats, Mimer.Mimer.RBMP))
{
images = true;
mimetypes.push(Mimer.Mimer.JPEG);
Expand All @@ -56,6 +55,11 @@ function supported_formats(printer, considerAdditionalFormats)
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};
}

Expand Down Expand Up @@ -259,6 +263,8 @@ function ippName(name, value, printerStrings)
return qsTr("JPEG");
case Mimer.Mimer.GIF:
return qsTr("GIF");
case Mimer.Mimer.SVG:
return qsTr("SVG");
case Mimer.Mimer.RBMP:
return qsTr("Reverse BMP");
default:
Expand Down Expand Up @@ -401,12 +407,16 @@ function canConvertPlaintextTo(type)
return has(targets, type)
}


function canConvertOfficeDocumentTo(type)
{
return has(pdfTargets, type)
}

function canConvertSvgTo(type)
{
return has(pdfTargets, type)
}

function canConvertImageTo(type)
{
var targets = [Mimer.Mimer.OctetStream, Mimer.Mimer.JPEG, Mimer.Mimer.PNG, Mimer.Mimer.RBMP,
Expand Down Expand Up @@ -445,6 +455,10 @@ function fixupChoices(name, choices, mimeType)
{
return choices.filter(canConvertOfficeDocumentTo)
}
else if(mimeType == Mimer.Mimer.SVG)
{
return choices.filter(canConvertSvgTo)
}
else if(Mimer.Mimer.isImage(mimeType))
{
return choices.filter(canConvertImageTo);
Expand Down
7 changes: 6 additions & 1 deletion src/ippprinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ QString targetFormatIfAuto(QString documentFormat, QString mimeType, QJsonArray
{
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))
{
QStringList ImageFormatPrioList {Mimer::PNG, Mimer::PWG, Mimer::URF, Mimer::PDF, Mimer::Postscript, Mimer::JPEG};
Expand Down Expand Up @@ -696,7 +701,7 @@ void IppPrinter::print(QJsonObject jobAttrs, QString filename)
{ // Can't process Postscript
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
emit doPrintImageAsImage(filename, contents, targetFormat);
}
Expand Down
6 changes: 5 additions & 1 deletion src/mimer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "mimer.h"
#include <QDebug>

const QString Mimer::OctetStream = "application/octet-stream";

Expand All @@ -11,6 +12,7 @@ const QString Mimer::PNG = "image/png";
const QString Mimer::GIF = "image/gif";
const QString Mimer::JPEG = "image/jpeg";
const QString Mimer::TIFF = "image/tiff";
const QString Mimer::SVG = "image/svg+xml";
const QString Mimer::RBMP = "image/reverse-encoding-bmp";

const QString Mimer::DOC = "application/msword";
Expand Down Expand Up @@ -51,5 +53,7 @@ Mimer* Mimer::instance()
}

QString Mimer::get_type(QString filename) {
return _db.mimeTypeForFile(filename).name();
QString type = _db.mimeTypeForFile(filename).name();
qDebug() << "MimeType:" << type;
return type;
}
2 changes: 2 additions & 0 deletions src/mimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Mimer : public QObject
Q_PROPERTY(const QString GIF MEMBER GIF CONSTANT);
Q_PROPERTY(const QString JPEG MEMBER JPEG 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 DOC MEMBER DOC CONSTANT);
Expand All @@ -50,6 +51,7 @@ class Mimer : public QObject
static const QString GIF;
static const QString JPEG;
static const QString TIFF;
static const QString SVG;
static const QString RBMP;

static const QString DOC;
Expand Down
68 changes: 53 additions & 15 deletions src/printerworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <QTextDocument>
#include <QPdfWriter>
#include <QAbstractTextDocumentLayout>
#include <QtSvg>
#include "ippprinter.h"
#include "pdf2printable.h"
#include "ppm2pwg.h"
Expand Down Expand Up @@ -258,7 +259,7 @@ catch(const ConvertFailedException& e)
void PrinterWorker::convertImage(QString filename, Bytestream header, PrintParameters Params, QMargins margins)
{
try {

QString mimeType = Mimer::instance()->get_type(filename);

if(Params.format == PrintParameters::URF && (Params.hwResW != Params.hwResH))
{ // URF only supports symmetric resolutions
Expand All @@ -268,18 +269,6 @@ try {

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 rightMarginPx = (margins.right()/2540.0)*Params.hwResW;
int topMarginPx = (margins.top()/2540.0)*Params.hwResH;
Expand All @@ -288,8 +277,57 @@ try {
int totalXMarginPx = leftMarginPx+rightMarginPx;
int totalYMarginPx = topMarginPx+bottomMarginPx;

inImage = inImage.scaled(Params.getPaperSizeWInPixels()-totalXMarginPx, Params.getPaperSizeHInPixels()-totalYMarginPx,
Qt::KeepAspectRatio, Qt::SmoothTransformation);
size_t targetWidth = Params.getPaperSizeWInPixels()-totalXMarginPx;
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)
{
Expand Down
8 changes: 8 additions & 0 deletions translations/harbour-seaprint-de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,10 @@ auf diesem Drucker</translation>
<source>plaintext</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>SVGs</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>strings</name>
Expand Down Expand Up @@ -5505,5 +5509,9 @@ auf diesem Drucker</translation>
<source>Reverse BMP</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>SVG</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>
8 changes: 8 additions & 0 deletions translations/harbour-seaprint-es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,10 @@
<source>plaintext</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>SVGs</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>strings</name>
Expand Down Expand Up @@ -5504,5 +5508,9 @@
<source>Reverse BMP</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>SVG</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>
8 changes: 8 additions & 0 deletions translations/harbour-seaprint-fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,10 @@ sur cette imprimante</translation>
<source>plaintext</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>SVGs</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>strings</name>
Expand Down Expand Up @@ -5505,5 +5509,9 @@ sur cette imprimante</translation>
<source>Reverse BMP</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>SVG</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>
8 changes: 8 additions & 0 deletions translations/harbour-seaprint-nl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,10 @@
<source>plaintext</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>SVGs</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>strings</name>
Expand Down Expand Up @@ -5504,5 +5508,9 @@
<source>Reverse BMP</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>SVG</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>
8 changes: 8 additions & 0 deletions translations/harbour-seaprint-pl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,10 @@
<source>plaintext</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>SVGs</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>strings</name>
Expand Down Expand Up @@ -5504,5 +5508,9 @@
<source>Reverse BMP</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>SVG</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>
8 changes: 8 additions & 0 deletions translations/harbour-seaprint-zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,10 @@
<source>plaintext</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>SVGs</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>strings</name>
Expand Down Expand Up @@ -5504,5 +5508,9 @@
<source>Reverse BMP</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>SVG</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>

0 comments on commit 6c7e09f

Please sign in to comment.