Respect margins in convertImage

This commit is contained in:
Anton Thomasson 2021-07-14 15:07:55 +02:00
parent 03c51db3e5
commit a7f169c42a
4 changed files with 51 additions and 17 deletions

View file

@ -151,7 +151,7 @@ catch(const ConvertFailedException& e)
void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY) quint32 HwResX, quint32 HwResY, QMargins margins)
{ {
try { try {
@ -210,21 +210,30 @@ try {
{ {
inImage = inImage.transformed(QMatrix().rotate(90.0)); inImage = inImage.transformed(QMatrix().rotate(90.0));
} }
inImage = inImage.scaled(Width, Height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
int leftMarginPx = (margins.left()/2540.0)*HwResX;
int rightMarginPx = (margins.right()/2540.0)*HwResX;
int topMarginPx = (margins.top()/2540.0)*HwResY;
int bottomMarginPx = (margins.bottom()/2540.0)*HwResY;
int totalXMarginPx = leftMarginPx+rightMarginPx;
int totalYMarginPx = topMarginPx+bottomMarginPx;
inImage = inImage.scaled(Width-totalXMarginPx, Height-totalYMarginPx,
Qt::KeepAspectRatio, Qt::SmoothTransformation);
if(pdfOrPostscript) if(pdfOrPostscript)
{ {
QTemporaryFile tmpPdfFile; QTemporaryFile tmpPdfFile;
tmpPdfFile.open(); tmpPdfFile.open();
QPdfWriter pdfWriter(tmpPdfFile.fileName()); QPdfWriter pdfWriter(tmpPdfFile.fileName());
pdfWriter.setCreator("SeaPrint " SEAPRINT_VERSION);
QPageSize pageSize(QSizeF {wh.first, wh.second}, QPageSize::Millimeter); QPageSize pageSize(QSizeF {wh.first, wh.second}, QPageSize::Millimeter);
pdfWriter.setPageSize(pageSize); pdfWriter.setPageSize(pageSize);
pdfWriter.setResolution(HwResX); pdfWriter.setResolution(HwResX);
// Needs to be before painter
pdfWriter.setMargins({0, 0, 0, 0});
QPainter painter(&pdfWriter); QPainter painter(&pdfWriter);
int xOffset = (pdfWriter.width()-inImage.width())/2; int xOffset = ((pdfWriter.width()-totalXMarginPx)-inImage.width())/2 + leftMarginPx;
int yOffset = (pdfWriter.height()-inImage.height())/2; int yOffset = ((pdfWriter.height()-totalYMarginPx)-inImage.height())/2 + topMarginPx;
painter.drawImage(xOffset, yOffset, inImage); painter.drawImage(xOffset, yOffset, inImage);
painter.end(); painter.end();
@ -246,8 +255,8 @@ try {
QImage outImage = QImage(Width, Height, inImage.format()); QImage outImage = QImage(Width, Height, inImage.format());
outImage.fill(Qt::white); outImage.fill(Qt::white);
QPainter painter(&outImage); QPainter painter(&outImage);
int xOffset = (outImage.width()-inImage.width())/2; int xOffset = ((outImage.width()-totalXMarginPx)-inImage.width())/2 + leftMarginPx;
int yOffset = (outImage.height()-inImage.height())/2; int yOffset = ((outImage.height()-totalYMarginPx)-inImage.height())/2 + topMarginPx;
painter.drawImage(xOffset, yOffset, inImage); painter.drawImage(xOffset, yOffset, inImage);
painter.end(); painter.end();

View file

@ -29,7 +29,7 @@ public slots:
void convertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, void convertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY); quint32 HwResX, quint32 HwResY, QMargins margins);
void convertOfficeDocument(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, void convertOfficeDocument(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,

View file

@ -5,6 +5,8 @@
#include "overrider.h" #include "overrider.h"
#include "settings.h" #include "settings.h"
Q_DECLARE_METATYPE(QMargins)
IppPrinter::IppPrinter() IppPrinter::IppPrinter()
{ {
_nam = new QNetworkAccessManager(this); _nam = new QNetworkAccessManager(this);
@ -40,6 +42,8 @@ IppPrinter::IppPrinter()
connect(_worker, &ConvertWorker::progress, this, &IppPrinter::setProgress); connect(_worker, &ConvertWorker::progress, this, &IppPrinter::setProgress);
connect(_worker, &ConvertWorker::failed, this, &IppPrinter::convertFailed); connect(_worker, &ConvertWorker::failed, this, &IppPrinter::convertFailed);
qRegisterMetaType<QMargins>();
_workerThread.start(); _workerThread.start();
_tainted = false; _tainted = false;
} }
@ -662,8 +666,13 @@ void IppPrinter::print(QJsonObject jobAttrs, QString filename)
} }
else if (Mimer::isImage(mimeType)) else if (Mimer::isImage(mimeType))
{ {
QMargins margins(getAttrOrDefault(jobAttrs, "media-left-margin", "media-col").toInt(),
getAttrOrDefault(jobAttrs, "media-top-margin", "media-col").toInt(),
getAttrOrDefault(jobAttrs, "media-right-margin", "media-col").toInt(),
getAttrOrDefault(jobAttrs, "media-bottom-margin", "media-col").toInt());
emit doConvertImage(request, filename, tempfile, documentFormat, Colors, Quality, emit doConvertImage(request, filename, tempfile, documentFormat, Colors, Quality,
PaperSize, HwResX, HwResY); PaperSize, HwResX, HwResY, margins);
} }
else if(Mimer::isOffice(mimeType)) else if(Mimer::isOffice(mimeType))
{ {
@ -796,15 +805,31 @@ void IppPrinter::setProgress(qint64 sent, qint64 total)
emit progressChanged(); emit progressChanged();
} }
QJsonValue IppPrinter::getAttrOrDefault(QJsonObject jobAttrs, QString name) QJsonValue IppPrinter::getAttrOrDefault(QJsonObject jobAttrs, QString name, QString subkey)
{ {
if(subkey == "")
{
if(jobAttrs.contains(name)) if(jobAttrs.contains(name))
{ {
return jobAttrs[name].toObject()["value"]; return jobAttrs[name].toObject()["value"];
} }
else { else
{
return _attrs[name+"-default"].toObject()["value"]; return _attrs[name+"-default"].toObject()["value"];
} }
}
else
{
QJsonObject subObj = jobAttrs[subkey].toObject()["value"].toObject();
if(subObj.contains(name))
{
return subObj[name].toObject()["value"];
}
else
{
return _attrs[name+"-default"].toObject()["value"];
}
}
} }
IppMsg IppPrinter::mk_msg(QJsonObject opAttrs, QJsonObject jobAttrs) IppMsg IppPrinter::mk_msg(QJsonObject opAttrs, QJsonObject jobAttrs)

View file

@ -56,7 +56,7 @@ signals:
void doConvertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, void doConvertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY); quint32 HwResX, quint32 HwResY, QMargins margins);
void doConvertOfficeDocument(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, void doConvertOfficeDocument(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
@ -102,7 +102,7 @@ private:
void setBusyMessage(QString msg); void setBusyMessage(QString msg);
void setProgress(qint64 sent, qint64 total); void setProgress(qint64 sent, qint64 total);
QJsonValue getAttrOrDefault(QJsonObject jobAttrs, QString name); QJsonValue getAttrOrDefault(QJsonObject jobAttrs, QString name, QString subkey = "");
IppMsg mk_msg(QJsonObject opAttrs, QJsonObject jobAttrs=QJsonObject()); IppMsg mk_msg(QJsonObject opAttrs, QJsonObject jobAttrs=QJsonObject());