From a7f169c42a0267ccd5be6805fe0838f73b51d30b Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Wed, 14 Jul 2021 15:07:55 +0200 Subject: [PATCH] Respect margins in convertImage --- src/convertworker.cpp | 25 +++++++++++++++++-------- src/convertworker.h | 2 +- src/ippprinter.cpp | 37 +++++++++++++++++++++++++++++++------ src/ippprinter.h | 4 ++-- 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/convertworker.cpp b/src/convertworker.cpp index 3e4d413..d3b597c 100644 --- a/src/convertworker.cpp +++ b/src/convertworker.cpp @@ -151,7 +151,7 @@ catch(const ConvertFailedException& e) void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, - quint32 HwResX, quint32 HwResY) + quint32 HwResX, quint32 HwResY, QMargins margins) { try { @@ -210,21 +210,30 @@ try { { 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) { QTemporaryFile tmpPdfFile; tmpPdfFile.open(); QPdfWriter pdfWriter(tmpPdfFile.fileName()); + pdfWriter.setCreator("SeaPrint " SEAPRINT_VERSION); QPageSize pageSize(QSizeF {wh.first, wh.second}, QPageSize::Millimeter); pdfWriter.setPageSize(pageSize); pdfWriter.setResolution(HwResX); - // Needs to be before painter - pdfWriter.setMargins({0, 0, 0, 0}); QPainter painter(&pdfWriter); - int xOffset = (pdfWriter.width()-inImage.width())/2; - int yOffset = (pdfWriter.height()-inImage.height())/2; + int xOffset = ((pdfWriter.width()-totalXMarginPx)-inImage.width())/2 + leftMarginPx; + int yOffset = ((pdfWriter.height()-totalYMarginPx)-inImage.height())/2 + topMarginPx; painter.drawImage(xOffset, yOffset, inImage); painter.end(); @@ -246,8 +255,8 @@ try { QImage outImage = QImage(Width, Height, inImage.format()); outImage.fill(Qt::white); QPainter painter(&outImage); - int xOffset = (outImage.width()-inImage.width())/2; - int yOffset = (outImage.height()-inImage.height())/2; + int xOffset = ((outImage.width()-totalXMarginPx)-inImage.width())/2 + leftMarginPx; + int yOffset = ((outImage.height()-totalYMarginPx)-inImage.height())/2 + topMarginPx; painter.drawImage(xOffset, yOffset, inImage); painter.end(); diff --git a/src/convertworker.h b/src/convertworker.h index 7b34ce6..2f48fa4 100644 --- a/src/convertworker.h +++ b/src/convertworker.h @@ -29,7 +29,7 @@ public slots: void convertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, 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, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, diff --git a/src/ippprinter.cpp b/src/ippprinter.cpp index 46f6203..d1aca05 100644 --- a/src/ippprinter.cpp +++ b/src/ippprinter.cpp @@ -5,6 +5,8 @@ #include "overrider.h" #include "settings.h" +Q_DECLARE_METATYPE(QMargins) + IppPrinter::IppPrinter() { _nam = new QNetworkAccessManager(this); @@ -40,6 +42,8 @@ IppPrinter::IppPrinter() connect(_worker, &ConvertWorker::progress, this, &IppPrinter::setProgress); connect(_worker, &ConvertWorker::failed, this, &IppPrinter::convertFailed); + qRegisterMetaType(); + _workerThread.start(); _tainted = false; } @@ -662,8 +666,13 @@ void IppPrinter::print(QJsonObject jobAttrs, QString filename) } 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, - PaperSize, HwResX, HwResY); + PaperSize, HwResX, HwResY, margins); } else if(Mimer::isOffice(mimeType)) { @@ -796,14 +805,30 @@ void IppPrinter::setProgress(qint64 sent, qint64 total) emit progressChanged(); } -QJsonValue IppPrinter::getAttrOrDefault(QJsonObject jobAttrs, QString name) +QJsonValue IppPrinter::getAttrOrDefault(QJsonObject jobAttrs, QString name, QString subkey) { - if(jobAttrs.contains(name)) + if(subkey == "") { - return jobAttrs[name].toObject()["value"]; + if(jobAttrs.contains(name)) + { + return jobAttrs[name].toObject()["value"]; + } + else + { + return _attrs[name+"-default"].toObject()["value"]; + } } - else { - 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"]; + } } } diff --git a/src/ippprinter.h b/src/ippprinter.h index 770005c..69510bc 100644 --- a/src/ippprinter.h +++ b/src/ippprinter.h @@ -56,7 +56,7 @@ signals: void doConvertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, 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, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, @@ -102,7 +102,7 @@ private: void setBusyMessage(QString msg); 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());