summaryrefslogtreecommitdiffstats
path: root/src/pdf/qpdfdocument.cpp
blob: d6aed74513fe1078e5e541c75c96d01561fbc5b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
129
130
131
132
133
134
135
136
137
#include "qpdfdocument.h"

#include "qpdfdocument_p.h"

#include <QFile>
#include <QIODevice>
#include <QMutex>

// The library is not thread-safe at all, it has a lot of global variables.
Q_GLOBAL_STATIC_WITH_ARGS(QMutex, pdfMutex, (QMutex::Recursive));
static int libraryRefCount;

QPdfDocumentPrivate::QPdfDocumentPrivate()
    : doc(0)
{
    QMutexLocker lock(pdfMutex());
    if (libraryRefCount == 0)
        FPDF_InitLibrary();
    ++libraryRefCount;

    // FPDF_FILEACCESS setup
    m_Param = this;
    m_GetBlock = fpdf_GetBlock;
}

QPdfDocumentPrivate::~QPdfDocumentPrivate()
{
    QMutexLocker lock(pdfMutex());
    if (doc)
        FPDF_CloseDocument(doc);
    doc = 0;

    if (!--libraryRefCount)
        FPDF_DestroyLibrary();
}

QPdfDocument::Error QPdfDocumentPrivate::load(QIODevice *newDevice, bool transferDeviceOwnership, const QString &documentPassword)
{
    QMutexLocker lock(pdfMutex());

    if (doc)
        FPDF_CloseDocument(doc);

    if (transferDeviceOwnership)
        ownDevice.reset(newDevice);
    else
        ownDevice.reset();
    device = newDevice;

    if (!device->isOpen() && !device->open(QIODevice::ReadOnly))
        return QPdfDocument::FileNotFoundError;

    // FPDF_FILEACCESS setup
    m_FileLen = device->size();

    password = documentPassword.toUtf8();

    doc = FPDF_LoadCustomDocument(this, password.constData());
    switch (FPDF_GetLastError()) {
    case FPDF_ERR_SUCCESS: return QPdfDocument::NoError;
    case FPDF_ERR_UNKNOWN: return QPdfDocument::UnknownError;
    case FPDF_ERR_FILE: return QPdfDocument::FileNotFoundError;
    case FPDF_ERR_FORMAT: return QPdfDocument::InvalidFileFormatError;
    case FPDF_ERR_PASSWORD: return QPdfDocument::IncorrectPasswordError;
    case FPDF_ERR_SECURITY: return QPdfDocument::UnsupportedSecuritySchemeError;
    default:
        Q_UNREACHABLE();
    }
    return QPdfDocument::UnknownError;
}

int QPdfDocumentPrivate::fpdf_GetBlock(void *param, unsigned long position, unsigned char *pBuf, unsigned long size)
{
    QPdfDocumentPrivate *d = static_cast<QPdfDocumentPrivate*>(reinterpret_cast<FPDF_FILEACCESS*>(param));
    d->device->seek(position);
    return qMax(qint64(0), d->device->read(reinterpret_cast<char *>(pBuf), size));

}

QPdfDocument::QPdfDocument(QObject *parent)
    : QObject(parent)
    , d(new QPdfDocumentPrivate)
{
}

QPdfDocument::~QPdfDocument()
{
}

QPdfDocument::Error QPdfDocument::load(const QString &fileName, const QString &password)
{
    return d->load(new QFile(fileName), /*transfer ownership*/true, password);
}

QPdfDocument::Error QPdfDocument::load(QIODevice *device, const QString &password)
{
    return d->load(device, /*transfer ownership*/false, password);
}

int QPdfDocument::pageCount() const
{
    if (!d->doc)
        return 0;
    QMutexLocker lock(pdfMutex());
    return FPDF_GetPageCount(d->doc);
}

QSizeF QPdfDocument::pageSize(int page) const
{
    QSizeF result;
    if (!d->doc)
        return result;
    QMutexLocker lock(pdfMutex());
    FPDF_GetPageSizeByIndex(d->doc, page, &result.rwidth(), &result.rheight());
    return result;
}

QImage QPdfDocument::render(int page, const QSizeF &pageSize)
{
    if (!d->doc)
        return QImage();

    QMutexLocker lock(pdfMutex());

    FPDF_PAGE pdfPage = FPDF_LoadPage(d->doc, page);
    if (!pdfPage)
        return QImage();

    QImage result(pageSize.toSize(), QImage::Format_ARGB32);
    result.fill(Qt::transparent);
    FPDF_BITMAP bitmap = FPDFBitmap_CreateEx(result.width(), result.height(), FPDFBitmap_BGRA, result.bits(), result.bytesPerLine());

    FPDF_RenderPageBitmap(bitmap, pdfPage, 0, 0, result.width(), result.height(), 0, 0);

    FPDFBitmap_Destroy(bitmap);
    return result;
}