2020-05-01 16:02:29 +03:00
|
|
|
#include "convertworker.h"
|
2020-05-01 21:54:33 +03:00
|
|
|
#include <sailfishapp.h>
|
2020-05-16 16:17:42 +03:00
|
|
|
#include "papersizes.h"
|
2020-08-01 21:18:47 +03:00
|
|
|
#include "convertchecker.h"
|
2021-03-04 23:49:27 +03:00
|
|
|
#include "mimer.h"
|
2021-06-19 17:34:17 +03:00
|
|
|
#include "seaprint_version.h"
|
2020-06-01 22:12:29 +03:00
|
|
|
#include <QImage>
|
|
|
|
#include <QMatrix>
|
|
|
|
#include <QPainter>
|
2021-06-12 13:45:58 +03:00
|
|
|
#include <QTextDocument>
|
|
|
|
#include <QPdfWriter>
|
2021-06-12 19:40:18 +03:00
|
|
|
#include <QAbstractTextDocumentLayout>
|
2021-08-28 23:16:26 +03:00
|
|
|
#include "ippprinter.h"
|
2021-11-08 21:49:47 +03:00
|
|
|
#include "pdf2printable.h"
|
2021-11-15 22:31:28 +03:00
|
|
|
#include "ppm2pwg.h"
|
2020-05-09 21:39:50 +03:00
|
|
|
|
2021-08-28 23:16:26 +03:00
|
|
|
ConvertWorker::ConvertWorker(IppPrinter* parent) // : QObject((QObject*)parent) borks multithereading?!
|
|
|
|
{
|
|
|
|
_printer = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConvertWorker::command(QByteArray msg)
|
|
|
|
{
|
2021-11-08 21:49:47 +03:00
|
|
|
CurlRequester cid(_printer->httpUrl());
|
2021-08-28 23:16:26 +03:00
|
|
|
cid.setFinishedCallback(_printer, &IppPrinter::getPrinterAttributesFinished);
|
|
|
|
|
|
|
|
qDebug() << "command...";
|
|
|
|
|
|
|
|
cid.write(msg.data(), msg.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: de-duplicate
|
|
|
|
void ConvertWorker::getJobs(QByteArray msg)
|
|
|
|
{
|
2021-11-08 21:49:47 +03:00
|
|
|
CurlRequester cid(_printer->httpUrl());
|
2021-08-28 23:16:26 +03:00
|
|
|
cid.setFinishedCallback(_printer, &IppPrinter::getJobsRequestFinished);
|
|
|
|
|
|
|
|
cid.write(msg.data(), msg.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConvertWorker::cancelJob(QByteArray msg)
|
|
|
|
{
|
2021-11-08 21:49:47 +03:00
|
|
|
CurlRequester cid(_printer->httpUrl());
|
2021-08-28 23:16:26 +03:00
|
|
|
cid.setFinishedCallback(_printer, &IppPrinter::cancelJobFinished);
|
|
|
|
|
|
|
|
cid.write(msg.data(), msg.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConvertWorker::justUpload(QString filename, QByteArray header)
|
|
|
|
{
|
|
|
|
qDebug() << "justupload";
|
|
|
|
|
2021-11-08 21:49:47 +03:00
|
|
|
CurlRequester cid(_printer->httpUrl());
|
2021-08-28 23:16:26 +03:00
|
|
|
cid.setFinishedCallback(_printer, &IppPrinter::printRequestFinished);
|
|
|
|
|
|
|
|
QFile file(filename);
|
|
|
|
file.open(QFile::ReadOnly);
|
|
|
|
|
|
|
|
cid.write(header.data(), header.length());
|
|
|
|
QByteArray tmp = file.readAll();
|
|
|
|
cid.write(tmp.data(), tmp.length());
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConvertWorker::convertPdf(QString filename, QByteArray header,
|
2020-06-04 22:31:46 +03:00
|
|
|
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
|
2020-08-01 21:18:47 +03:00
|
|
|
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
|
2021-06-26 15:58:14 +03:00
|
|
|
quint32 PageRangeLow, quint32 PageRangeHigh, bool BackHFlip, bool BackVFlip)
|
2020-05-09 21:39:50 +03:00
|
|
|
{
|
2021-03-05 22:28:10 +03:00
|
|
|
try {
|
2021-11-08 21:49:47 +03:00
|
|
|
Format format;
|
2021-03-05 22:28:10 +03:00
|
|
|
|
2021-11-08 21:49:47 +03:00
|
|
|
qDebug() << "to pdf" << HwResX << HwResY;
|
2021-11-14 23:01:18 +03:00
|
|
|
emit busyMessage(tr("Printing"));
|
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
|
2021-03-04 23:49:27 +03:00
|
|
|
if(targetFormat == Mimer::URF)
|
2020-06-04 22:31:46 +03:00
|
|
|
{
|
2021-11-08 21:49:47 +03:00
|
|
|
format = Format::URF;
|
2020-06-04 22:31:46 +03:00
|
|
|
}
|
2021-03-04 23:49:27 +03:00
|
|
|
else if(targetFormat == Mimer::PWG)
|
2020-06-04 22:31:46 +03:00
|
|
|
{
|
2021-11-08 21:49:47 +03:00
|
|
|
format = Format::PWG;
|
2020-06-04 22:31:46 +03:00
|
|
|
}
|
2021-03-04 23:49:27 +03:00
|
|
|
else if(targetFormat == Mimer::Postscript)
|
2020-06-06 16:47:43 +03:00
|
|
|
{
|
2021-11-08 21:49:47 +03:00
|
|
|
format = Format::Postscript;
|
2020-06-06 16:47:43 +03:00
|
|
|
}
|
2021-03-04 23:49:27 +03:00
|
|
|
else if (targetFormat == Mimer::PDF)
|
2020-09-23 21:14:33 +03:00
|
|
|
{
|
2021-11-08 21:49:47 +03:00
|
|
|
format = Format::PDF;
|
2020-09-23 21:14:33 +03:00
|
|
|
}
|
2020-06-04 22:31:46 +03:00
|
|
|
else
|
|
|
|
{
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException(tr("Unsupported target format"));
|
2020-06-04 22:31:46 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 21:49:47 +03:00
|
|
|
if(Colors == 0)
|
|
|
|
{
|
|
|
|
Colors = 3;
|
2020-05-09 21:39:50 +03:00
|
|
|
}
|
2020-05-05 21:36:01 +03:00
|
|
|
|
2021-11-08 21:49:47 +03:00
|
|
|
CurlRequester cid(_printer->httpUrl());
|
|
|
|
cid.setFinishedCallback(_printer, &IppPrinter::printRequestFinished);
|
2020-05-16 16:17:42 +03:00
|
|
|
|
2021-11-08 21:49:47 +03:00
|
|
|
cid.write(header.data(), header.length());
|
|
|
|
|
|
|
|
write_fun WriteFun([&cid](unsigned char const* buf, unsigned int len) -> bool
|
|
|
|
{
|
|
|
|
cid.write((const char*)buf, len);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2021-11-14 21:28:39 +03:00
|
|
|
progress_fun ProgressFun([this](size_t page, size_t total) -> void
|
|
|
|
{
|
|
|
|
emit progress(page, total);
|
|
|
|
});
|
|
|
|
|
2021-11-08 21:49:47 +03:00
|
|
|
if(!PaperSizes.contains(PaperSize))
|
2021-06-12 12:42:10 +03:00
|
|
|
{
|
2021-11-08 21:49:47 +03:00
|
|
|
qDebug() << "Unsupported paper size" << PaperSize;
|
|
|
|
throw ConvertFailedException(tr("Unsupported paper size"));
|
2020-09-23 21:14:33 +03:00
|
|
|
}
|
2021-11-08 21:49:47 +03:00
|
|
|
QSizeF size = PaperSizes[PaperSize];
|
|
|
|
float Width = size.width();
|
|
|
|
float Height = size.height();
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2021-11-08 21:49:47 +03:00
|
|
|
int res = pdf_to_printable(filename.toStdString(), WriteFun, Colors, Quality, PaperSize.toStdString(), Width, Height, HwResX, HwResY,
|
2021-11-14 21:28:39 +03:00
|
|
|
format, TwoSided, Tumble, BackHFlip, BackVFlip, PageRangeLow, PageRangeHigh, ProgressFun);
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2021-11-08 21:49:47 +03:00
|
|
|
if(res != 0)
|
|
|
|
{
|
2021-11-14 23:01:18 +03:00
|
|
|
throw ConvertFailedException(tr("Conversion failed"));
|
2021-11-08 21:49:47 +03:00
|
|
|
}
|
2020-05-01 16:02:29 +03:00
|
|
|
|
|
|
|
qDebug() << "Finished";
|
2021-03-05 22:28:10 +03:00
|
|
|
}
|
|
|
|
catch(const ConvertFailedException& e)
|
|
|
|
{
|
|
|
|
emit failed(e.what() == QString("") ? tr("Conversion error") : e.what());
|
|
|
|
}
|
2020-05-01 16:02:29 +03:00
|
|
|
}
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2021-08-28 23:16:26 +03:00
|
|
|
void ConvertWorker::convertImage(QString filename, QByteArray header,
|
2020-06-04 22:31:46 +03:00
|
|
|
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
|
2021-07-14 16:07:55 +03:00
|
|
|
quint32 HwResX, quint32 HwResY, QMargins margins)
|
2020-05-13 20:53:44 +03:00
|
|
|
{
|
2021-03-05 22:28:10 +03:00
|
|
|
try {
|
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
bool urf = false;
|
2021-06-13 14:56:44 +03:00
|
|
|
bool pdfOrPostscript = false;
|
2020-06-04 22:31:46 +03:00
|
|
|
QString imageFormat = "";
|
2021-03-04 23:49:27 +03:00
|
|
|
QStringList supportedImageFormats = {Mimer::JPEG, Mimer::PNG};
|
2020-06-04 22:31:46 +03:00
|
|
|
|
2021-03-04 23:49:27 +03:00
|
|
|
if(targetFormat == Mimer::URF)
|
2020-06-04 22:31:46 +03:00
|
|
|
{
|
|
|
|
urf = true;
|
|
|
|
}
|
2021-03-04 23:49:27 +03:00
|
|
|
else if(targetFormat == Mimer::PWG)
|
2020-06-04 22:31:46 +03:00
|
|
|
{
|
|
|
|
//ok
|
|
|
|
}
|
2021-06-13 14:56:44 +03:00
|
|
|
else if(targetFormat == Mimer::PDF || targetFormat == Mimer::Postscript)
|
|
|
|
{
|
|
|
|
HwResX = HwResY = std::min(HwResX, HwResY);
|
|
|
|
pdfOrPostscript = true;
|
|
|
|
}
|
2020-06-04 22:31:46 +03:00
|
|
|
else if(supportedImageFormats.contains(targetFormat))
|
|
|
|
{
|
|
|
|
imageFormat = targetFormat.split("/")[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException(tr("Unsupported target format"));
|
2020-06-04 22:31:46 +03:00
|
|
|
}
|
|
|
|
|
2021-11-15 22:31:28 +03:00
|
|
|
if(Colors == 0)
|
|
|
|
{
|
|
|
|
Colors = 3;
|
|
|
|
}
|
|
|
|
|
2020-06-01 22:56:47 +03:00
|
|
|
if(urf && (HwResX != HwResY))
|
|
|
|
{ // URF only supports symmetric resolutions
|
|
|
|
qDebug() << "Unsupported URF resolution" << PaperSize;
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException(tr("Unsupported resolution (dpi)"));
|
2020-06-01 22:56:47 +03:00
|
|
|
}
|
|
|
|
|
2020-05-16 16:17:42 +03:00
|
|
|
if(!PaperSizes.contains(PaperSize))
|
|
|
|
{
|
|
|
|
qDebug() << "Unsupported paper size" << PaperSize;
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException(tr("Unsupported paper size"));
|
2020-05-16 16:17:42 +03:00
|
|
|
}
|
2021-07-31 13:52:33 +03:00
|
|
|
QSizeF size = PaperSizes[PaperSize];
|
|
|
|
quint32 Width = qRound(size.width()/25.4*HwResX);
|
|
|
|
quint32 Height = qRound(size.height()/25.4*HwResY);
|
2020-05-16 16:17:42 +03:00
|
|
|
|
|
|
|
qDebug() << "Size is" << Width << "x" << Height;
|
2020-05-13 20:53:44 +03:00
|
|
|
|
|
|
|
QImage inImage;
|
|
|
|
if(!inImage.load(filename))
|
|
|
|
{
|
|
|
|
qDebug() << "failed to load";
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException(tr("Failed to load image"));
|
2020-05-13 20:53:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(inImage.width() > inImage.height())
|
|
|
|
{
|
2021-11-15 22:31:28 +03:00
|
|
|
inImage = inImage.transformed(QMatrix().rotate(270.0));
|
2020-05-13 20:53:44 +03:00
|
|
|
}
|
2021-07-14 16:07:55 +03:00
|
|
|
|
|
|
|
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);
|
2021-06-13 14:56:44 +03:00
|
|
|
|
|
|
|
if(pdfOrPostscript)
|
|
|
|
{
|
|
|
|
QTemporaryFile tmpPdfFile;
|
|
|
|
tmpPdfFile.open();
|
|
|
|
QPdfWriter pdfWriter(tmpPdfFile.fileName());
|
2021-07-14 16:07:55 +03:00
|
|
|
pdfWriter.setCreator("SeaPrint " SEAPRINT_VERSION);
|
2021-07-31 13:52:33 +03:00
|
|
|
QPageSize pageSize(size, QPageSize::Millimeter);
|
2021-06-13 14:56:44 +03:00
|
|
|
pdfWriter.setPageSize(pageSize);
|
|
|
|
pdfWriter.setResolution(HwResX);
|
|
|
|
QPainter painter(&pdfWriter);
|
2021-07-14 16:07:55 +03:00
|
|
|
int xOffset = ((pdfWriter.width()-totalXMarginPx)-inImage.width())/2 + leftMarginPx;
|
|
|
|
int yOffset = ((pdfWriter.height()-totalYMarginPx)-inImage.height())/2 + topMarginPx;
|
2021-06-13 14:56:44 +03:00
|
|
|
painter.drawImage(xOffset, yOffset, inImage);
|
|
|
|
painter.end();
|
|
|
|
|
2021-11-14 23:01:18 +03:00
|
|
|
convertPdf(tmpPdfFile.fileName(), header, targetFormat, Colors, Quality, PaperSize,
|
|
|
|
HwResX, HwResY, false, false, 0, 0, false, false);
|
2021-06-13 14:56:44 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
}
|
|
|
|
else
|
2021-06-13 14:56:44 +03:00
|
|
|
{
|
|
|
|
QImage outImage = QImage(Width, Height, inImage.format());
|
|
|
|
outImage.fill(Qt::white);
|
|
|
|
QPainter painter(&outImage);
|
2021-07-14 16:07:55 +03:00
|
|
|
int xOffset = ((outImage.width()-totalXMarginPx)-inImage.width())/2 + leftMarginPx;
|
|
|
|
int yOffset = ((outImage.height()-totalYMarginPx)-inImage.height())/2 + topMarginPx;
|
2021-06-13 14:56:44 +03:00
|
|
|
painter.drawImage(xOffset, yOffset, inImage);
|
|
|
|
painter.end();
|
|
|
|
|
2021-11-15 22:31:28 +03:00
|
|
|
QBuffer buf;
|
|
|
|
buf.open(QIODevice::ReadWrite);
|
|
|
|
Bytestream outBts;
|
|
|
|
|
2021-06-13 14:56:44 +03:00
|
|
|
if(imageFormat != "")
|
|
|
|
{ // We are converting to a supported image format
|
2021-11-15 22:31:28 +03:00
|
|
|
outImage.save(&buf, imageFormat.toStdString().c_str());
|
|
|
|
buf.seek(0);
|
|
|
|
outBts = Bytestream(buf.size());
|
|
|
|
buf.read((char*)(outBts.raw()), buf.size());
|
2021-06-13 14:56:44 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // We are converting to a raster format
|
2020-11-30 22:46:26 +03:00
|
|
|
|
2021-06-13 14:56:44 +03:00
|
|
|
bool gray = Colors == 0 ? inImage.allGray() : Colors == 1;
|
2020-11-24 20:15:31 +03:00
|
|
|
|
2021-11-15 22:31:28 +03:00
|
|
|
outImage.save(&buf, gray ? "pgm" : "ppm");
|
|
|
|
buf.seek(0);
|
|
|
|
// Skip header - TODO consider reimplementing
|
|
|
|
buf.readLine(255);
|
|
|
|
buf.readLine(255);
|
|
|
|
buf.readLine(255);
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2021-11-15 22:31:28 +03:00
|
|
|
Bytestream inBts(Width*Height*Colors);
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2021-11-15 22:31:28 +03:00
|
|
|
if((buf.size()-buf.pos()) != inBts.size())
|
2021-06-13 14:56:44 +03:00
|
|
|
{
|
2021-11-15 22:31:28 +03:00
|
|
|
qDebug() << buf.size() << buf.pos() << inBts.size();
|
2021-06-13 14:56:44 +03:00
|
|
|
throw ConvertFailedException();
|
|
|
|
}
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2021-11-15 22:31:28 +03:00
|
|
|
buf.read((char*)(inBts.raw()), inBts.size());
|
2021-06-13 14:56:44 +03:00
|
|
|
|
2021-11-15 22:31:28 +03:00
|
|
|
outBts << (urf ? make_urf_file_hdr(1) : make_pwg_file_hdr());
|
|
|
|
bmp_to_pwg(inBts, outBts, urf, 1, Colors, Quality, HwResX, HwResY, Width, Height, false, false, PaperSize.toStdString(), false, false);
|
2021-06-13 14:56:44 +03:00
|
|
|
}
|
2021-11-15 22:31:28 +03:00
|
|
|
|
|
|
|
emit busyMessage(tr("Printing"));
|
|
|
|
|
|
|
|
CurlRequester cid(_printer->httpUrl());
|
|
|
|
cid.setFinishedCallback(_printer, &IppPrinter::printRequestFinished);
|
|
|
|
|
|
|
|
cid.write(header.data(), header.length());
|
|
|
|
cid.write((char*)(outBts.raw()), outBts.size());
|
2020-06-04 22:31:46 +03:00
|
|
|
}
|
2020-05-13 20:53:44 +03:00
|
|
|
|
|
|
|
qDebug() << "posted";
|
2021-03-05 22:28:10 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
catch(const ConvertFailedException& e)
|
|
|
|
{
|
|
|
|
emit failed(e.what() == QString("") ? tr("Conversion error") : e.what());
|
|
|
|
}
|
2020-05-13 20:53:44 +03:00
|
|
|
}
|
2021-03-06 14:59:47 +03:00
|
|
|
|
2021-08-28 23:16:26 +03:00
|
|
|
void ConvertWorker::convertOfficeDocument(QString filename, QByteArray header,
|
2021-03-06 14:59:47 +03:00
|
|
|
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
|
|
|
|
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
|
2021-06-26 15:58:14 +03:00
|
|
|
quint32 PageRangeLow, quint32 PageRangeHigh, bool BackHFlip, bool BackVFlip)
|
2021-03-06 14:59:47 +03:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
|
|
|
|
if(targetFormat == Mimer::URF && (HwResX != HwResY))
|
|
|
|
{ // URF only supports symmetric resolutions
|
|
|
|
qDebug() << "Unsupported URF resolution" << PaperSize;
|
|
|
|
throw ConvertFailedException(tr("Unsupported resolution (dpi)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ShortPaperSize;
|
|
|
|
if(CalligraPaperSizes.contains(PaperSize))
|
|
|
|
{
|
|
|
|
ShortPaperSize = CalligraPaperSizes[PaperSize];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug() << "Unsupported PDF paper size" << PaperSize;
|
|
|
|
throw ConvertFailedException(tr("Unsupported PDF paper size"));
|
|
|
|
}
|
|
|
|
|
2021-06-12 12:57:39 +03:00
|
|
|
QProcess CalligraConverter(this);
|
|
|
|
CalligraConverter.setProgram("calligraconverter");
|
2021-03-06 14:59:47 +03:00
|
|
|
QStringList CalligraConverterArgs = {"--batch", "--mimetype", Mimer::PDF, "--print-orientation", "Portrait", "--print-papersize", ShortPaperSize};
|
|
|
|
|
|
|
|
CalligraConverterArgs << filename;
|
|
|
|
|
|
|
|
QTemporaryFile tmpPdfFile;
|
|
|
|
tmpPdfFile.open();
|
|
|
|
CalligraConverterArgs << tmpPdfFile.fileName();
|
|
|
|
|
|
|
|
qDebug() << "CalligraConverteArgs is" << CalligraConverterArgs;
|
2021-06-12 12:57:39 +03:00
|
|
|
CalligraConverter.setArguments(CalligraConverterArgs);
|
2021-03-06 14:59:47 +03:00
|
|
|
|
2021-06-12 12:57:39 +03:00
|
|
|
CalligraConverter.start();
|
2021-03-06 14:59:47 +03:00
|
|
|
|
|
|
|
qDebug() << "CalligraConverter Starting";
|
|
|
|
|
2021-06-12 12:57:39 +03:00
|
|
|
if(!CalligraConverter.waitForStarted())
|
2021-03-06 14:59:47 +03:00
|
|
|
{
|
|
|
|
qDebug() << "CalligraConverter died";
|
|
|
|
throw ConvertFailedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "CalligraConverter Started";
|
|
|
|
|
2021-06-12 12:57:39 +03:00
|
|
|
if(!CalligraConverter.waitForFinished(-1))
|
2021-03-06 14:59:47 +03:00
|
|
|
{
|
|
|
|
qDebug() << "CalligraConverter failed";
|
|
|
|
throw ConvertFailedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
// qDebug() << CalligraConverter->readAllStandardError();
|
|
|
|
|
|
|
|
quint32 pages = ConvertChecker::instance()->pdfPages(tmpPdfFile.fileName());
|
|
|
|
if (!pages)
|
|
|
|
{
|
|
|
|
qDebug() << "pdfinfo returned 0 pages";
|
|
|
|
throw ConvertFailedException(tr("Failed to get info about PDF file"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(PageRangeLow==0)
|
|
|
|
{
|
|
|
|
PageRangeLow=1;
|
|
|
|
}
|
|
|
|
|
2021-03-06 19:20:51 +03:00
|
|
|
if(PageRangeHigh==0 || PageRangeHigh > pages)
|
2021-03-06 14:59:47 +03:00
|
|
|
{
|
|
|
|
PageRangeHigh=pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actual number of pages to print
|
|
|
|
pages = PageRangeHigh-PageRangeLow+1;
|
|
|
|
|
|
|
|
qDebug() << "PageRangeLow" << PageRangeLow << "PageRangeHigh" << PageRangeHigh << "pages" << pages;
|
|
|
|
|
2021-11-14 23:01:18 +03:00
|
|
|
convertPdf(tmpPdfFile.fileName(), header, targetFormat, Colors, Quality, PaperSize, HwResX, HwResY, TwoSided, Tumble,
|
|
|
|
PageRangeLow, PageRangeHigh, BackHFlip, BackVFlip);
|
2021-06-12 11:34:46 +03:00
|
|
|
|
|
|
|
qDebug() << "Finished";
|
|
|
|
|
|
|
|
qDebug() << "posted";
|
|
|
|
|
|
|
|
}
|
|
|
|
catch(const ConvertFailedException& e)
|
|
|
|
{
|
|
|
|
emit failed(e.what() == QString("") ? tr("Conversion error") : e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-28 23:16:26 +03:00
|
|
|
void ConvertWorker::convertPlaintext(QString filename, QByteArray header,
|
2021-06-12 13:45:58 +03:00
|
|
|
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
|
2021-06-26 15:58:14 +03:00
|
|
|
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
|
|
|
|
bool BackHFlip, bool BackVFlip)
|
2021-06-12 13:45:58 +03:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
|
|
|
|
if(!PaperSizes.contains(PaperSize))
|
|
|
|
{
|
|
|
|
qDebug() << "Unsupported paper size" << PaperSize;
|
|
|
|
throw ConvertFailedException(tr("Unsupported paper size"));
|
|
|
|
}
|
2021-07-31 13:52:33 +03:00
|
|
|
QSizeF size = PaperSizes[PaperSize];
|
2021-06-12 13:45:58 +03:00
|
|
|
|
|
|
|
QFile inFile(filename);
|
|
|
|
if(!inFile.open(QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
throw ConvertFailedException(tr("Failed to open file"));
|
|
|
|
}
|
|
|
|
|
|
|
|
quint32 resolution = std::min(HwResX, HwResY);
|
|
|
|
|
|
|
|
QTemporaryFile tmpPdfFile;
|
|
|
|
tmpPdfFile.open();
|
|
|
|
|
|
|
|
QPdfWriter pdfWriter(tmpPdfFile.fileName());
|
2021-06-19 17:34:17 +03:00
|
|
|
pdfWriter.setCreator("SeaPrint " SEAPRINT_VERSION);
|
2021-07-31 13:52:33 +03:00
|
|
|
QPageSize pageSize(size, QPageSize::Millimeter);
|
2021-06-12 13:45:58 +03:00
|
|
|
pdfWriter.setPageSize(pageSize);
|
|
|
|
pdfWriter.setResolution(resolution);
|
2021-06-28 22:55:53 +03:00
|
|
|
|
2021-06-12 13:45:58 +03:00
|
|
|
qreal docHeight = pageSize.sizePixels(resolution).height();
|
|
|
|
|
|
|
|
QTextDocument doc;
|
|
|
|
|
|
|
|
QFont font = QFont("Courier");
|
|
|
|
font.setPointSizeF(1);
|
|
|
|
|
|
|
|
qreal charHeight = 0;
|
|
|
|
|
|
|
|
// Find the optimal font size
|
|
|
|
while(true) {
|
|
|
|
QFont tmpFont = font;
|
|
|
|
tmpFont.setPointSizeF(font.pointSizeF()+0.5);
|
|
|
|
QFontMetricsF qfm(tmpFont, &pdfWriter);
|
|
|
|
|
|
|
|
charHeight = qfm.lineSpacing();
|
|
|
|
|
|
|
|
if(charHeight*66 > docHeight)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
font=tmpFont;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFontMetricsF qfm(font, &pdfWriter);
|
|
|
|
|
|
|
|
charHeight = qfm.height();
|
|
|
|
|
|
|
|
int textHeight = 60*charHeight;
|
|
|
|
qreal margin = ((docHeight-textHeight)/2);
|
2021-06-28 22:55:53 +03:00
|
|
|
qreal mmMargin = margin/(resolution/25.4);
|
2021-06-12 13:45:58 +03:00
|
|
|
|
|
|
|
doc.setDefaultFont(font);
|
|
|
|
(void)doc.documentLayout(); // wat
|
|
|
|
|
|
|
|
|
2021-06-28 22:55:53 +03:00
|
|
|
// Needs to be before painter
|
|
|
|
pdfWriter.setMargins({mmMargin, mmMargin, mmMargin, mmMargin});
|
|
|
|
|
2021-06-12 13:45:58 +03:00
|
|
|
QPainter painter(&pdfWriter);
|
|
|
|
|
|
|
|
doc.documentLayout()->setPaintDevice(painter.device());
|
|
|
|
doc.setDocumentMargin(margin);
|
|
|
|
|
2021-06-28 22:55:53 +03:00
|
|
|
// Hack to make the document and pdfWriter margins overlap
|
|
|
|
// Apparently calls to painter.translate() stack... who knew!
|
|
|
|
painter.translate(-margin, -margin);
|
|
|
|
|
|
|
|
QRectF body = pageSize.rectPixels(resolution);
|
2021-06-12 13:45:58 +03:00
|
|
|
doc.setPageSize(body.size());
|
|
|
|
|
|
|
|
QString allText = inFile.readAll();
|
|
|
|
if(allText.startsWith("\f"))
|
|
|
|
{
|
|
|
|
allText.remove(0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(allText.endsWith("\f"))
|
|
|
|
{
|
|
|
|
allText.chop(1);
|
|
|
|
}
|
|
|
|
else if(allText.endsWith("\f\n"))
|
|
|
|
{
|
|
|
|
allText.chop(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList pages = allText.split('\f');
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
int pageCount = 0;
|
|
|
|
|
|
|
|
foreach(QString page, pages)
|
|
|
|
{
|
|
|
|
if(!first)
|
|
|
|
{
|
|
|
|
pdfWriter.newPage();
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
if(page.endsWith("\n"))
|
|
|
|
{
|
|
|
|
page.chop(1);
|
|
|
|
}
|
|
|
|
doc.setPlainText(page);
|
|
|
|
|
|
|
|
int p = 0; // Page number in this document, starting from 0
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
painter.translate(body.left(), body.top() - p*body.height());
|
|
|
|
QRectF view(0, p*body.height(), body.width(), body.height());
|
|
|
|
painter.setClipRect(view);
|
|
|
|
|
|
|
|
QAbstractTextDocumentLayout::PaintContext context;
|
|
|
|
context.clip = view;
|
|
|
|
context.palette.setColor(QPalette::Text, Qt::black);
|
|
|
|
doc.documentLayout()->draw(&painter, context);
|
|
|
|
|
|
|
|
p++;
|
|
|
|
pageCount++;
|
|
|
|
|
|
|
|
if(p >= doc.pageCount())
|
|
|
|
break;
|
|
|
|
|
|
|
|
pdfWriter.newPage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
painter.end();
|
|
|
|
|
2021-11-14 23:01:18 +03:00
|
|
|
convertPdf(tmpPdfFile.fileName(), header, targetFormat, Colors, Quality, PaperSize, HwResX, HwResY,
|
|
|
|
TwoSided, Tumble, 0, 0, BackHFlip, BackVFlip);
|
2021-06-12 13:45:58 +03:00
|
|
|
|
|
|
|
qDebug() << "Finished";
|
2021-08-28 23:16:26 +03:00
|
|
|
qDebug() << "posted";
|
2021-06-12 13:45:58 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
catch(const ConvertFailedException& e)
|
|
|
|
{
|
|
|
|
emit failed(e.what() == QString("") ? tr("Conversion error") : e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-12 12:17:34 +03:00
|
|
|
QString ConvertWorker::getPopplerShortPaperSize(QString PaperSize)
|
|
|
|
{
|
|
|
|
QString ShortPaperSize;
|
|
|
|
if(PaperSize == "iso_a4_210x297mm")
|
|
|
|
{
|
|
|
|
ShortPaperSize = "A4";
|
|
|
|
}
|
|
|
|
else if (PaperSize == "iso_a3_297x420mm")
|
|
|
|
{
|
|
|
|
ShortPaperSize = "A3";
|
|
|
|
}
|
|
|
|
else if (PaperSize == "na_letter_8.5x11in")
|
|
|
|
{
|
|
|
|
ShortPaperSize = "letter";
|
|
|
|
}
|
|
|
|
else if (PaperSize == "na_legal_8.5x14in")
|
|
|
|
{
|
|
|
|
ShortPaperSize = "legal";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug() << "Unsupported PDF paper size" << PaperSize;
|
|
|
|
throw ConvertFailedException(tr("Unsupported PDF paper size"));
|
|
|
|
}
|
|
|
|
return ShortPaperSize;
|
|
|
|
}
|