File indexing completed on 2024-05-19 04:36:37

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2013 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU Library General Public License as published
0007  * by the Free Software Foundation, either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, see
0017  * <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "PdfRenderer.h"
0021 
0022 #include <QPainter>
0023 #include <QPixmap>
0024 
0025 #include <QDebug>
0026 
0027 #include <poppler-qt4.h>
0028 
0029 namespace tex {
0030 
0031 class PdfRendererPrivate
0032 {
0033     public:
0034         QPixmap pixmap;
0035         Poppler::Document * document;
0036 };
0037 
0038 PdfRenderer::PdfRenderer(QObject* parent)
0039     : QObject(parent)
0040     , d(new PdfRendererPrivate())
0041 {
0042     d->document = 0;
0043 }
0044 
0045 PdfRenderer::~PdfRenderer()
0046 {
0047     delete d;
0048 }
0049 
0050 void PdfRenderer::loadPdf(const QString& filename)
0051 {
0052     delete d->document;
0053     d->document = Poppler::Document::load(filename);
0054 }
0055 
0056 QPixmap PdfRenderer::render(qreal physicalDpiX, qreal physicalDpiY)
0057 {
0058     if (!d->document) {
0059         return QPixmap();
0060     }
0061 
0062     const int page = 0;
0063     d->document->setRenderBackend(Poppler::Document::ArthurBackend);
0064     d->document->setRenderHint(Poppler::Document::Antialiasing, true);
0065     d->document->setRenderHint(Poppler::Document::TextAntialiasing, true);
0066     d->document->setRenderHint(Poppler::Document::TextHinting, true);
0067     d->document->setRenderHint(Poppler::Document::TextSlightHinting, true);
0068     d->document->setPaperColor(Qt::transparent);
0069     QImage image = d->document->page(page)->renderToImage(physicalDpiX, physicalDpiY);
0070 
0071     //BEGIN hack: convert white background to transparent
0072     struct ArgbData {
0073         unsigned char b, g, r, a;
0074     };
0075     const int imageWidth = image.width();
0076     const int imageHeight = image.height();
0077 
0078     for (int x=0; x<imageHeight; ++x) {
0079         ArgbData* scanLine = reinterpret_cast<ArgbData*>(image.scanLine(x));
0080         for (int y=0; y<imageWidth; ++y) {
0081             ArgbData* unit = &scanLine[y];
0082             unit->a = 0xff - unit->r;
0083             unit->r = unit->g = unit->b = 0;
0084         }
0085     }
0086     //END hack
0087     d->pixmap = QPixmap::fromImage(image);
0088     return d->pixmap;
0089 }
0090 
0091 }
0092 
0093 // kate: indent-width 4; replace-tabs on;