XPDFRenderer.cpp 5.72 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
Craig Watson's avatar
Craig Watson committed
2 3
 * Copyright (C) 2015-2016 Département de l'Instruction Publique (DIP-SEM)
 *
Claudio Valerio's avatar
Claudio Valerio committed
4
 * Copyright (C) 2013 Open Education Foundation
Claudio Valerio's avatar
Claudio Valerio committed
5
 *
Claudio Valerio's avatar
Claudio Valerio committed
6 7
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
8
 *
Claudio Valerio's avatar
Claudio Valerio committed
9 10 11
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
14 15 16 17
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
Claudio Valerio's avatar
Claudio Valerio committed
18
 * OpenBoard is distributed in the hope that it will be useful,
Claudio Valerio's avatar
Claudio Valerio committed
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
21
 * GNU General Public License for more details.
Claudio Valerio's avatar
Claudio Valerio committed
22
 *
Claudio Valerio's avatar
Claudio Valerio committed
23
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
24
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
25
 */
Claudio Valerio's avatar
Claudio Valerio committed
26

27

Claudio Valerio's avatar
Claudio Valerio committed
28

Claudio Valerio's avatar
Claudio Valerio committed
29

Claudio Valerio's avatar
Claudio Valerio committed
30 31 32 33 34 35
#include "XPDFRenderer.h"

#include <QtGui>

#include <frameworks/UBPlatformUtils.h>

36 37
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
38 39
QAtomicInt XPDFRenderer::sInstancesCount = 0;

Didier's avatar
Didier committed
40
XPDFRenderer::XPDFRenderer(const QString &filename, bool importingFile)
Claudio Valerio's avatar
Claudio Valerio committed
41
    : mDocument(0)
Claudio Valerio's avatar
Claudio Valerio committed
42 43
    , mpSplashBitmap(0)
    , mSplash(0)
Claudio Valerio's avatar
Claudio Valerio committed
44
{
45
    Q_UNUSED(importingFile);
Claudio Valerio's avatar
Claudio Valerio committed
46 47 48 49 50 51 52 53
    if (!globalParams)
    {
        // globalParams must be allocated once and never be deleted
        // note that this is *not* an instance variable of this XPDFRenderer class
        globalParams = new GlobalParams(0);
        globalParams->setupBaseFonts(QFile::encodeName(UBPlatformUtils::applicationResourcesDirectory() + "/" + "fonts").data());
    }

-f's avatar
-f committed
54
    mDocument = new PDFDoc(new GString(filename.toLocal8Bit()), 0, 0, 0); // the filename GString is deleted on PDFDoc desctruction
Claudio Valerio's avatar
Claudio Valerio committed
55 56 57 58 59
    sInstancesCount.ref();
}

XPDFRenderer::~XPDFRenderer()
{
Claudio Valerio's avatar
Claudio Valerio committed
60 61 62 63 64
    if(mSplash){
        delete mSplash;
        mSplash = NULL;
    }

Claudio Valerio's avatar
Claudio Valerio committed
65 66 67 68 69 70
    if (mDocument)
    {
        delete mDocument;
        sInstancesCount.deref();
    }

71
    if (sInstancesCount.loadAcquire() == 0 && globalParams)
Claudio Valerio's avatar
Claudio Valerio committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    {
        delete globalParams;
        globalParams = 0;
    }
}

bool XPDFRenderer::isValid() const
{
    if (mDocument)
    {
        return mDocument->isOk();
    }
    else
    {
        return false;
    }
}

int XPDFRenderer::pageCount() const
{
    if (isValid())
        return mDocument->getNumPages();
    else
        return 0;
}

QString XPDFRenderer::title() const
{
    if (isValid())
    {
        Object pdfInfo;
        mDocument->getDocInfo(&pdfInfo);
        if (pdfInfo.isDict())
        {
            Object title;
            Dict *infoDict = pdfInfo.getDict();
            if (infoDict->lookup((char*)"Title", &title)->isString())
            {
                GString *gstring = title.getString();
                return QString(gstring->getCString());
            }
        }
    }

    return QString();
}


QSizeF XPDFRenderer::pageSizeF(int pageNumber) const
{
    qreal cropWidth = 0;
    qreal cropHeight = 0;

    if (isValid())
    {
        int rotate = mDocument->getPageRotate(pageNumber);

Claudio Valerio's avatar
Claudio Valerio committed
129
        cropWidth = mDocument->getPageCropWidth(pageNumber) * this->dpiForRendering / 72.0;
130
        cropHeight = mDocument->getPageCropHeight(pageNumber) * this->dpiForRendering / 72.0;
Claudio Valerio's avatar
Claudio Valerio committed
131

132
        if (rotate == 90 || rotate == 270)
Claudio Valerio's avatar
Claudio Valerio committed
133
        {
134
            //switching width and height
Claudio Valerio's avatar
Claudio Valerio committed
135 136 137
            qreal tmpVar = cropWidth;
            cropWidth = cropHeight;
            cropHeight = tmpVar;
Claudio Valerio's avatar
Claudio Valerio committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151
        }
    }
    return QSizeF(cropWidth, cropHeight);
}


int XPDFRenderer::pageRotation(int pageNumber) const
{
    if (mDocument)
        return  mDocument->getPageRotate(pageNumber);
    else
        return 0;
}

152
void XPDFRenderer::render(QPainter *p, int pageNumber, const QRectF &bounds)
153
{
Didier's avatar
Didier committed
154 155 156 157 158
    if (isValid())
    {
        qreal xscale = p->worldTransform().m11();
        qreal yscale = p->worldTransform().m22();

159
        QImage *pdfImage = createPDFImage(pageNumber, xscale, yscale, bounds);
Didier's avatar
Didier committed
160 161
        QTransform savedTransform = p->worldTransform();
        p->resetTransform();
Claudio Valerio's avatar
Claudio Valerio committed
162
        p->drawImage(QPointF(savedTransform.dx() + mSliceX, savedTransform.dy() + mSliceY), *pdfImage);
Didier's avatar
Didier committed
163
        p->setWorldTransform(savedTransform);
164
        delete pdfImage;
Claudio Valerio's avatar
Claudio Valerio committed
165
    }
Didier's avatar
Didier committed
166 167
}

168
QImage* XPDFRenderer::createPDFImage(int pageNumber, qreal xscale, qreal yscale, const QRectF &bounds)
Didier's avatar
Didier committed
169
{
Claudio Valerio's avatar
Claudio Valerio committed
170 171 172
    if (isValid())
    {
        SplashColor paperColor = {0xFF, 0xFF, 0xFF}; // white
Claudio Valerio's avatar
Claudio Valerio committed
173 174
        if(mSplash)
            delete mSplash;
Didier's avatar
Didier committed
175 176
        mSplash = new SplashOutputDev(splashModeRGB8, 1, gFalse, paperColor);
        mSplash->startDoc(mDocument->getXRef());
Claudio Valerio's avatar
Claudio Valerio committed
177 178 179 180
        int rotation = 0; // in degrees (get it from the worldTransform if we want to support rotation)
        GBool useMediaBox = gFalse;
        GBool crop = gTrue;
        GBool printing = gFalse;
Didier's avatar
Didier committed
181 182
        mSliceX = 0.;
        mSliceY = 0.;
Claudio Valerio's avatar
Claudio Valerio committed
183 184 185

        if (bounds.isNull())
        {
Claudio Valerio's avatar
Claudio Valerio committed
186
            mDocument->displayPage(mSplash, pageNumber, this->dpiForRendering * xscale, this->dpiForRendering *yscale,
Claudio Valerio's avatar
Claudio Valerio committed
187 188 189 190
                                   rotation, useMediaBox, crop, printing);
        }
        else
        {
Claudio Valerio's avatar
Claudio Valerio committed
191
            mSliceX = bounds.x() * xscale;
192 193 194
            mSliceY = bounds.y() * yscale;
            qreal sliceW = bounds.width() * xscale;
            qreal sliceH = bounds.height() * yscale;
Claudio Valerio's avatar
Claudio Valerio committed
195

196
            mDocument->displayPageSlice(mSplash, pageNumber, this->dpiForRendering * xscale, this->dpiForRendering * yscale,
Claudio Valerio's avatar
Claudio Valerio committed
197
                rotation, useMediaBox, crop, printing, mSliceX, mSliceY, sliceW, sliceH);
Claudio Valerio's avatar
Claudio Valerio committed
198 199
        }

Didier's avatar
Didier committed
200
        mpSplashBitmap = mSplash->getBitmap();
Claudio Valerio's avatar
Claudio Valerio committed
201
    }
202
    return new QImage(mpSplashBitmap->getDataPtr(), mpSplashBitmap->getWidth(), mpSplashBitmap->getHeight(), mpSplashBitmap->getWidth() * 3, QImage::Format_RGB888);
Didier's avatar
Didier committed
203
}