From c5fedfb1ec79b97edec4a82b70f082fba93a5b5d Mon Sep 17 00:00:00 2001 From: Aleks Kissinger Date: Mon, 31 Dec 2018 12:56:32 +0100 Subject: image export in progress --- src/data/pdfdocument.cpp | 132 +++++++++++++++++++++++++++++++++++++++++++++++ src/data/pdfdocument.h | 28 ++++++++++ 2 files changed, 160 insertions(+) create mode 100644 src/data/pdfdocument.cpp create mode 100644 src/data/pdfdocument.h (limited to 'src/data') diff --git a/src/data/pdfdocument.cpp b/src/data/pdfdocument.cpp new file mode 100644 index 0000000..bfedeca --- /dev/null +++ b/src/data/pdfdocument.cpp @@ -0,0 +1,132 @@ +#include "pdfdocument.h" + +#include +#include +#include +#include +#include +#include +#include + +PdfDocument::PdfDocument(QString file, QObject *parent) : QObject(parent) +{ + // use loadFromData to avoid holding a lock on the PDF file in windows + QFile f(file); + if (f.open(QFile::ReadOnly)) { + QByteArray data = f.readAll(); + f.close(); + _doc = Poppler::Document::loadFromData(data); + } else { + _doc = nullptr; + } + + if (!_doc) { + _doc = nullptr; + _page = nullptr; + } else { + _doc->setRenderHint(Poppler::Document::Antialiasing); + _doc->setRenderHint(Poppler::Document::TextAntialiasing); + _doc->setRenderHint(Poppler::Document::TextHinting); + _page = _doc->page(0); + } +} + +void PdfDocument::renderTo(QLabel *label, QRect rect) +{ + if (!isValid()) return; + + QSizeF pageSize = _page->pageSizeF(); + + qreal ratio = label->devicePixelRatioF(); + //QRect rect = ui->scrollArea->visibleRegion().boundingRect(); + int w = static_cast(ratio * (rect.width() - 20)); + int h = static_cast(ratio * (rect.height() - 20)); + qreal scale = fmin(static_cast(w) / pageSize.width(), + static_cast(h) / pageSize.height()); + + + int dpi = static_cast(scale * 72.0); + int w1 = static_cast(scale * pageSize.width()); + int h1 = static_cast(scale * pageSize.height()); + + //qDebug() << "hidpi ratio:" << ratio; + //qDebug() << "visible width:" << w; + //qDebug() << "visible height:" << h; + //qDebug() << "doc width:" << pageSize.width(); + //qDebug() << "doc height:" << pageSize.height(); + //qDebug() << "scale:" << scale; + //qDebug() << "dpi:" << dpi; + + QPixmap pm = QPixmap::fromImage(_page->renderToImage(dpi, dpi, (w1 - w)/2, (h1 - h)/2, w, h)); + pm.setDevicePixelRatio(ratio); + label->setPixmap(pm); +} + +bool PdfDocument::isValid() +{ + return (_page != nullptr); +} + +bool PdfDocument::exportImage(QString file, const char *format, QSize outputSize) +{ + QImage img = asImage(outputSize); + if (!img.isNull()) return img.save(file, format); + else return false; +} + +bool PdfDocument::exportPdf(QString file) +{ + if (!isValid()) return false; + Poppler::PDFConverter *conv = _doc->pdfConverter(); + conv->setOutputFileName(file); + bool success = conv->convert(); + delete conv; + return success; +} + +void PdfDocument::copyImageToClipboard(QSize outputSize) +{ + QImage img = asImage(outputSize); + if (!img.isNull()) { + QApplication::clipboard()->setImage(img, QClipboard::Clipboard); + } +} + +QImage PdfDocument::asImage(QSize outputSize) +{ + if (!isValid()) return QImage(); + if (outputSize.isNull()) outputSize = size(); + QSize pageSize = _page->pageSize(); + int dpix = (72 * outputSize.width()) / pageSize.width(); + int dpiy = (72 * outputSize.width()) / pageSize.width(); + QImage img = _page->renderToImage(dpix, dpiy, 0, 0, + outputSize.width(), outputSize.height()); + return img; +} + +// CRASHES TikZiT when figures contain text, due to limitations of Arthur backend +//void PdfDocument::exportToSvg(QString file, QSize size) { +// QSvgGenerator gen; +// gen.setFileName(file); +// gen.setSize(size); +// gen.setViewBox(QRect(0,0,size.width(),size.height())); +// gen.setDescription("SVG generated from PDF by TikZiT"); +// QPainter painter; + +// // set the backend to Qt for renderToPainter() support +// Poppler::Document::RenderBackend backend = _doc->renderBackend(); +// _doc->setRenderBackend(Poppler::Document::ArthurBackend); +// painter.begin(&gen); +// _page->renderToPainter(&painter); +// painter.end(); +// _doc->setRenderBackend(backend); +//} + +QSize PdfDocument::size() +{ + if (isValid()) { + return _page->pageSize(); + } +} + + diff --git a/src/data/pdfdocument.h b/src/data/pdfdocument.h new file mode 100644 index 0000000..ebd33e9 --- /dev/null +++ b/src/data/pdfdocument.h @@ -0,0 +1,28 @@ +#ifndef PDFDOCUMENT_H +#define PDFDOCUMENT_H + +#include +#include +#include + +#include + +class PdfDocument : public QObject +{ + Q_OBJECT +public: + explicit PdfDocument(QString file, QObject *parent = nullptr); + void renderTo(QLabel *label, QRect rect); + bool isValid(); +// void exportToSvg(QString file, QSize size); + bool exportImage(QString file, const char *format, QSize outputSize=QSize()); + bool exportPdf(QString file); + void copyImageToClipboard(QSize outputSize=QSize()); + QImage asImage(QSize outputSize=QSize()); + QSize size(); +private: + Poppler::Document *_doc; + Poppler::Page *_page; +}; + +#endif // PDFDOCUMENT_H -- cgit v1.2.3