| 1 |
/* poppler-private.h: qt interface to poppler |
| 2 |
* Copyright (C) 2005, Net Integration Technologies, Inc. |
| 3 |
* Copyright (C) 2005, 2008, Brad Hards <[email protected]> |
| 4 |
* Copyright (C) 2006-2009, 2011, 2012, 2017-2022, 2024, 2025 by Albert Astals Cid <[email protected]> |
| 5 |
* Copyright (C) 2007-2009, 2011, 2014 by Pino Toscano <[email protected]> |
| 6 |
* Copyright (C) 2011 Andreas Hartmetz <[email protected]> |
| 7 |
* Copyright (C) 2011 Hib Eris <[email protected]> |
| 8 |
* Copyright (C) 2012, 2013 Thomas Freitag <[email protected]> |
| 9 |
* Copyright (C) 2013 Anthony Granger <[email protected]> |
| 10 |
* Copyright (C) 2014 Bogdan Cristea <[email protected]> |
| 11 |
* Copyright (C) 2014 Aki Koskinen <[email protected]> |
| 12 |
* Copyright (C) 2016 Jakub Alba <[email protected]> |
| 13 |
* Copyright (C) 2017 Christoph Cullmann <[email protected]> |
| 14 |
* Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <[email protected]>. Work sponsored by the LiMux project of the city of Munich |
| 15 |
* Copyright (C) 2018, 2020 Adam Reichold <[email protected]> |
| 16 |
* Copyright (C) 2019, 2020 Oliver Sander <[email protected]> |
| 17 |
* Copyright (C) 2019 João Netto <[email protected]> |
| 18 |
* Copyright (C) 2019 Jan Grulich <[email protected]> |
| 19 |
* Copyright (C) 2019 Alexander Volkov <[email protected]> |
| 20 |
* Copyright (C) 2020 Philipp Knechtges <[email protected]> |
| 21 |
* Copyright (C) 2021 Mahmoud Khalil <[email protected]> |
| 22 |
* Copyright (C) 2021 Hubert Figuiere <[email protected]> |
| 23 |
* Copyright (C) 2021 Georgiy Sgibnev <[email protected]>. Work sponsored by lab50.net. |
| 24 |
* Copyright (C) 2024, 2025 g10 Code GmbH, Author: Sune Stolborg Vuorela <[email protected]> |
| 25 |
* Inspired on code by |
| 26 |
* Copyright (C) 2004 by Albert Astals Cid <[email protected]> |
| 27 |
* Copyright (C) 2004 by Enrico Ros <[email protected]> |
| 28 |
* |
| 29 |
* This program is free software; you can redistribute it and/or modify |
| 30 |
* it under the terms of the GNU General Public License as published by |
| 31 |
* the Free Software Foundation; either version 2, or (at your option) |
| 32 |
* any later version. |
| 33 |
* |
| 34 |
* This program is distributed in the hope that it will be useful, |
| 35 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 36 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 37 |
* GNU General Public License for more details. |
| 38 |
* |
| 39 |
* You should have received a copy of the GNU General Public License |
| 40 |
* along with this program; if not, write to the Free Software |
| 41 |
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
| 42 |
*/ |
| 43 |
|
| 44 |
#ifndef _POPPLER_PRIVATE_H_ |
| 45 |
#define _POPPLER_PRIVATE_H_ |
| 46 |
|
| 47 |
#include <QtCore/QFile> |
| 48 |
#include <QtCore/QMutex> |
| 49 |
#include <QtCore/QPointer> |
| 50 |
#include <QtCore/QVector> |
| 51 |
|
| 52 |
#include <functional> |
| 53 |
#include <config.h> |
| 54 |
#include <poppler-config.h> |
| 55 |
#include <GfxState.h> |
| 56 |
#include <GlobalParams.h> |
| 57 |
#include <FileSpec.h> |
| 58 |
#include <Form.h> |
| 59 |
#include <PDFDoc.h> |
| 60 |
#include <FontInfo.h> |
| 61 |
#include <OutputDev.h> |
| 62 |
#include <Error.h> |
| 63 |
#include <SplashOutputDev.h> |
| 64 |
|
| 65 |
#include "poppler-qt5.h" |
| 66 |
#include "poppler-embeddedfile-private.h" |
| 67 |
#include "poppler-qiodeviceinstream-private.h" |
| 68 |
|
| 69 |
class LinkDest; |
| 70 |
class FormWidget; |
| 71 |
class OutlineItem; |
| 72 |
|
| 73 |
namespace Poppler { |
| 74 |
|
| 75 |
/* borrowed from kpdf */ |
| 76 |
POPPLER_QT5_EXPORT QString unicodeToQString(const Unicode *u, int len); |
| 77 |
POPPLER_QT5_EXPORT QString unicodeToQString(const std::vector<Unicode> &u); |
| 78 |
|
| 79 |
POPPLER_QT5_EXPORT QString UnicodeParsedString(const GooString *s1); |
| 80 |
|
| 81 |
POPPLER_QT5_EXPORT QString UnicodeParsedString(const std::string &s1); |
| 82 |
|
| 83 |
// Returns a big endian UTF-16 string with BOM or an empty string without BOM. |
| 84 |
// The caller owns the returned pointer. |
| 85 |
POPPLER_QT5_EXPORT std::unique_ptr<GooString> QStringToUnicodeGooString(const QString &s); |
| 86 |
|
| 87 |
POPPLER_QT5_EXPORT std::unique_ptr<GooString> QStringToGooString(const QString &s); |
| 88 |
|
| 89 |
std::unique_ptr<GooString> QDateTimeToUnicodeGooString(const QDateTime &dt); |
| 90 |
|
| 91 |
void qt5ErrorFunction(ErrorCategory /*category*/, Goffset pos, const char *msg); |
| 92 |
|
| 93 |
Annot::AdditionalActionsType toPopplerAdditionalActionType(Annotation::AdditionalActionType type); |
| 94 |
|
| 95 |
class LinkDestinationData |
| 96 |
{ |
| 97 |
public: |
| 98 |
LinkDestinationData(const LinkDest *l, const GooString *nd, Poppler::DocumentData *pdfdoc, bool external) : ld(l), namedDest(nd), doc(pdfdoc), externalDest(external) { } |
| 99 |
|
| 100 |
const LinkDest *ld; |
| 101 |
const GooString *namedDest; |
| 102 |
Poppler::DocumentData *doc; |
| 103 |
bool externalDest; |
| 104 |
}; |
| 105 |
|
| 106 |
class DocumentData : private GlobalParamsIniter |
| 107 |
{ |
| 108 |
public: |
| 109 |
DocumentData(const QString &filePath, const std::optional<GooString> &ownerPassword, const std::optional<GooString> &userPassword) : GlobalParamsIniter(qt5ErrorFunction) |
| 110 |
{ |
| 111 |
init(); |
| 112 |
m_device = nullptr; |
| 113 |
m_filePath = filePath; |
| 114 |
|
| 115 |
#ifdef _WIN32 |
| 116 |
doc = new PDFDoc((wchar_t *)filePath.utf16(), filePath.length(), ownerPassword, userPassword, std::bind(&DocumentData::noitfyXRefReconstructed, this)); |
| 117 |
#else |
| 118 |
doc = new PDFDoc(std::make_unique<GooString>(QFile::encodeName(filePath).constData()), ownerPassword, userPassword, std::bind(&DocumentData::noitfyXRefReconstructed, this)); |
| 119 |
#endif |
| 120 |
} |
| 121 |
|
| 122 |
DocumentData(QIODevice *device, const std::optional<GooString> &ownerPassword, const std::optional<GooString> &userPassword) : GlobalParamsIniter(qt5ErrorFunction) |
| 123 |
{ |
| 124 |
m_device = device; |
| 125 |
QIODeviceInStream *str = new QIODeviceInStream(device, 0, false, device->size(), Object::null()); |
| 126 |
init(); |
| 127 |
doc = new PDFDoc(str, ownerPassword, userPassword, std::bind(&DocumentData::noitfyXRefReconstructed, this)); |
| 128 |
} |
| 129 |
|
| 130 |
DocumentData(const QByteArray &data, const std::optional<GooString> &ownerPassword, const std::optional<GooString> &userPassword) : GlobalParamsIniter(qt5ErrorFunction) |
| 131 |
{ |
| 132 |
m_device = nullptr; |
| 133 |
fileContents = data; |
| 134 |
MemStream *str = new MemStream((char *)fileContents.data(), 0, fileContents.length(), Object::null()); |
| 135 |
init(); |
| 136 |
doc = new PDFDoc(str, ownerPassword, userPassword, std::bind(&DocumentData::noitfyXRefReconstructed, this)); |
| 137 |
} |
| 138 |
|
| 139 |
void init(); |
| 140 |
|
| 141 |
~DocumentData(); |
| 142 |
|
| 143 |
DocumentData(const DocumentData &) = delete; |
| 144 |
DocumentData &operator=(const DocumentData &) = delete; |
| 145 |
|
| 146 |
void addTocChildren(QDomDocument *docSyn, QDomNode *parent, const std::vector<::OutlineItem *> *items); |
| 147 |
|
| 148 |
void setPaperColor(const QColor &color) { paperColor = color; } |
| 149 |
|
| 150 |
void fillMembers() |
| 151 |
{ |
| 152 |
int numEmb = doc->getCatalog()->numEmbeddedFiles(); |
| 153 |
if (!(0 == numEmb)) { |
| 154 |
// we have some embedded documents, build the list |
| 155 |
for (int yalv = 0; yalv < numEmb; ++yalv) { |
| 156 |
std::unique_ptr<FileSpec> fs = doc->getCatalog()->embeddedFile(yalv); |
| 157 |
m_embeddedFiles.append(new EmbeddedFile(*new EmbeddedFileData(std::move(fs)))); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* a method that is being called whenever PDFDoc's XRef is reconstructed |
| 164 |
* where we'll set xrefReconstructed flag and notify users of the |
| 165 |
* reconstruction event |
| 166 |
*/ |
| 167 |
void noitfyXRefReconstructed(); |
| 168 |
|
| 169 |
static Document *checkDocument(DocumentData *doc); |
| 170 |
|
| 171 |
PDFDoc *doc; |
| 172 |
QString m_filePath; |
| 173 |
QIODevice *m_device; |
| 174 |
QByteArray fileContents; |
| 175 |
bool locked; |
| 176 |
Document::RenderBackend m_backend; |
| 177 |
QList<EmbeddedFile *> m_embeddedFiles; |
| 178 |
QPointer<OptContentModel> m_optContentModel; |
| 179 |
QColor paperColor; |
| 180 |
int m_hints; |
| 181 |
#if USE_CMS |
| 182 |
GfxLCMSProfilePtr m_sRGBProfile; |
| 183 |
GfxLCMSProfilePtr m_displayProfile; |
| 184 |
#endif |
| 185 |
bool xrefReconstructed; |
| 186 |
// notifies the user whenever the backend's PDFDoc XRef is reconstructed |
| 187 |
std::function<void()> xrefReconstructedCallback; |
| 188 |
}; |
| 189 |
|
| 190 |
class FontInfoData |
| 191 |
{ |
| 192 |
public: |
| 193 |
FontInfoData() |
| 194 |
{ |
| 195 |
isEmbedded = false; |
| 196 |
isSubset = false; |
| 197 |
type = FontInfo::unknown; |
| 198 |
} |
| 199 |
|
| 200 |
explicit FontInfoData(::FontInfo *fi) |
| 201 |
{ |
| 202 |
const std::optional<std::string> &fiName = fi->getName(); |
| 203 |
if (fiName) { |
| 204 |
fontName = QString::fromStdString(fiName.value()); |
| 205 |
} |
| 206 |
const std::optional<std::string> &fiFile = fi->getFile(); |
| 207 |
if (fiFile) { |
| 208 |
fontFile = QString::fromStdString(fiFile.value()); |
| 209 |
} |
| 210 |
const std::optional<std::string> &fiSubstituteName = fi->getSubstituteName(); |
| 211 |
if (fiSubstituteName) { |
| 212 |
fontSubstituteName = QString::fromStdString(fiSubstituteName.value()); |
| 213 |
} |
| 214 |
isEmbedded = fi->getEmbedded(); |
| 215 |
isSubset = fi->getSubset(); |
| 216 |
type = (Poppler::FontInfo::Type)fi->getType(); |
| 217 |
embRef = fi->getEmbRef(); |
| 218 |
} |
| 219 |
|
| 220 |
FontInfoData(const FontInfoData &fid) = default; |
| 221 |
FontInfoData &operator=(const FontInfoData &) = default; |
| 222 |
|
| 223 |
QString fontName; |
| 224 |
QString fontSubstituteName; |
| 225 |
QString fontFile; |
| 226 |
bool isEmbedded : 1; |
| 227 |
bool isSubset : 1; |
| 228 |
FontInfo::Type type; |
| 229 |
Ref embRef; |
| 230 |
}; |
| 231 |
|
| 232 |
class FontIteratorData |
| 233 |
{ |
| 234 |
public: |
| 235 |
FontIteratorData(int startPage, DocumentData *dd) : fontInfoScanner(dd->doc, startPage), totalPages(dd->doc->getNumPages()), currentPage(qMax(startPage, 0) - 1) { } |
| 236 |
|
| 237 |
~FontIteratorData() = default; |
| 238 |
|
| 239 |
FontInfoScanner fontInfoScanner; |
| 240 |
int totalPages; |
| 241 |
int currentPage; |
| 242 |
}; |
| 243 |
|
| 244 |
class TextBoxData |
| 245 |
{ |
| 246 |
public: |
| 247 |
TextBoxData() : nextWord(nullptr), hasSpaceAfter(false) { } |
| 248 |
|
| 249 |
QString text; |
| 250 |
QRectF bBox; |
| 251 |
TextBox *nextWord; |
| 252 |
QVector<QRectF> charBBoxes; // the boundingRect of each character |
| 253 |
bool hasSpaceAfter; |
| 254 |
}; |
| 255 |
|
| 256 |
inline Poppler::ErrorStringType fromPopplerCore(::ErrorStringType type) |
| 257 |
{ |
| 258 |
switch (type) { |
| 259 |
case ::ErrorStringType::ErrorCodeString: |
| 260 |
return Poppler::ErrorStringType::ErrorCodeString; |
| 261 |
case ::ErrorStringType::UserString: |
| 262 |
return Poppler::ErrorStringType::UserString; |
| 263 |
} |
| 264 |
return Poppler::ErrorStringType::ErrorCodeString; |
| 265 |
} |
| 266 |
|
| 267 |
inline Poppler::ErrorString fromPopplerCore(const ::ErrorString &str) |
| 268 |
{ |
| 269 |
Poppler::ErrorString res; |
| 270 |
res.type = fromPopplerCore(str.type); |
| 271 |
res.data = QString::fromStdString(str.text); |
| 272 |
return res; |
| 273 |
} |
| 274 |
|
| 275 |
class FormFieldData |
| 276 |
{ |
| 277 |
public: |
| 278 |
FormFieldData(DocumentData *_doc, ::Page *p, ::FormWidget *w) : doc(_doc), page(p), fm(w) { } |
| 279 |
|
| 280 |
DocumentData *doc; |
| 281 |
::Page *page; // Note for some signatures it can be null since there's signatures that don't belong to a given page |
| 282 |
::FormWidget *fm; |
| 283 |
QRectF box; |
| 284 |
static POPPLER_QT5_EXPORT ::FormWidget *getFormWidget(const FormField *f); |
| 285 |
Poppler::ErrorString lastSigningErrorDetails; |
| 286 |
}; |
| 287 |
|
| 288 |
class FormFieldIcon; |
| 289 |
class FormFieldIconData |
| 290 |
{ |
| 291 |
public: |
| 292 |
static POPPLER_QT5_EXPORT FormFieldIconData *getData(const FormFieldIcon &f); |
| 293 |
Dict *icon; |
| 294 |
}; |
| 295 |
|
| 296 |
} |
| 297 |
|
| 298 |
#endif |