Skip to content

Commit

Permalink
Respect margins in convertImage
Browse files Browse the repository at this point in the history
  • Loading branch information
attah committed Jul 14, 2021
1 parent 03c51db commit a7f169c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
25 changes: 17 additions & 8 deletions src/convertworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/convertworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
37 changes: 31 additions & 6 deletions src/ippprinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "overrider.h"
#include "settings.h"

Q_DECLARE_METATYPE(QMargins)

IppPrinter::IppPrinter()
{
_nam = new QNetworkAccessManager(this);
Expand Down Expand Up @@ -40,6 +42,8 @@ IppPrinter::IppPrinter()
connect(_worker, &ConvertWorker::progress, this, &IppPrinter::setProgress);
connect(_worker, &ConvertWorker::failed, this, &IppPrinter::convertFailed);

qRegisterMetaType<QMargins>();

_workerThread.start();
_tainted = false;
}
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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"];
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/ippprinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class IppPrinter : public QObject

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,
Expand Down Expand Up @@ -102,7 +102,7 @@ public slots:
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());

Expand Down

0 comments on commit a7f169c

Please sign in to comment.