| 1 |
/* poppler-document.cc: qt interface to poppler |
| 2 |
* Copyright (C) 2005, Net Integration Technologies, Inc. |
| 3 |
* Copyright (C) 2005, 2008, Brad Hards <[email protected]> |
| 4 |
* Copyright (C) 2005-2010, 2012, 2013, 2015, 2017-2022, 2025, Albert Astals Cid <[email protected]> |
| 5 |
* Copyright (C) 2006-2010, Pino Toscano <[email protected]> |
| 6 |
* Copyright (C) 2010, 2011 Hib Eris <[email protected]> |
| 7 |
* Copyright (C) 2012 Koji Otani <[email protected]> |
| 8 |
* Copyright (C) 2012, 2013 Thomas Freitag <[email protected]> |
| 9 |
* Copyright (C) 2012 Fabio D'Urso <[email protected]> |
| 10 |
* Copyright (C) 2014, 2018, 2020 Adam Reichold <[email protected]> |
| 11 |
* Copyright (C) 2015 William Bader <[email protected]> |
| 12 |
* Copyright (C) 2016 Jakub Alba <[email protected]> |
| 13 |
* Copyright (C) 2017, 2021 Adrian Johnson <[email protected]> |
| 14 |
* Copyright (C) 2017 Suzuki Toshiya <[email protected]> |
| 15 |
* Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <[email protected]>. Work sponsored by the LiMux project of the city of Munich |
| 16 |
* Copyright (C) 2019-2021 Oliver Sander <[email protected]> |
| 17 |
* Copyright (C) 2019 Alexander Volkov <[email protected]> |
| 18 |
* Copyright (C) 2020 Philipp Knechtges <[email protected]> |
| 19 |
* Copyright (C) 2020 Katarina Behrens <[email protected]> |
| 20 |
* Copyright (C) 2020 Thorsten Behrens <[email protected]> |
| 21 |
* Copyright (C) 2021 Mahmoud Khalil <[email protected]> |
| 22 |
* Copyright (C) 2021 Hubert Figuiere <[email protected]> |
| 23 |
* Copyright (C) 2024 Pratham Gandhi <[email protected]> |
| 24 |
* Copyright (C) 2024 Stefan Brüns <[email protected]> |
| 25 |
* Copyright (C) 2025 g10 Code GmbH, Author: Sune Stolborg Vuorela <[email protected]> |
| 26 |
* Copyright (C) 2025 Arnav V <[email protected]> |
| 27 |
* |
| 28 |
* This program is free software; you can redistribute it and/or modify |
| 29 |
* it under the terms of the GNU General Public License as published by |
| 30 |
* the Free Software Foundation; either version 2, or (at your option) |
| 31 |
* any later version. |
| 32 |
* |
| 33 |
* This program is distributed in the hope that it will be useful, |
| 34 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 35 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 36 |
* GNU General Public License for more details. |
| 37 |
* |
| 38 |
* You should have received a copy of the GNU General Public License |
| 39 |
* along with this program; if not, write to the Free Software |
| 40 |
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
| 41 |
*/ |
| 42 |
|
| 43 |
#include "poppler-qt6.h" |
| 44 |
|
| 45 |
#include <config.h> |
| 46 |
#include <poppler-config.h> |
| 47 |
#include <ErrorCodes.h> |
| 48 |
#include <GlobalParams.h> |
| 49 |
#include <Outline.h> |
| 50 |
#include <PDFDoc.h> |
| 51 |
#include <Stream.h> |
| 52 |
#include <Catalog.h> |
| 53 |
#include <ViewerPreferences.h> |
| 54 |
#include <DateInfo.h> |
| 55 |
#include <GfxState.h> |
| 56 |
|
| 57 |
#include <QtCore/QDebug> |
| 58 |
#include <QtCore/QFile> |
| 59 |
#include <QtCore/QByteArray> |
| 60 |
#include <QTimeZone> |
| 61 |
|
| 62 |
#include "poppler-form.h" |
| 63 |
#include "poppler-private.h" |
| 64 |
#include "poppler-link-private.h" |
| 65 |
#include "poppler-page-private.h" |
| 66 |
#include "poppler-outline-private.h" |
| 67 |
|
| 68 |
#if USE_CMS |
| 69 |
# include <lcms2.h> |
| 70 |
#endif |
| 71 |
|
| 72 |
namespace Poppler { |
| 73 |
|
| 74 |
std::unique_ptr<Document> Document::load(const QString &filePath, const QByteArray &ownerPassword, const QByteArray &userPassword) |
| 75 |
{ |
| 76 |
DocumentData *doc = new DocumentData(filePath, GooString(ownerPassword.data()), GooString(userPassword.data())); |
| 77 |
return DocumentData::checkDocument(doc); |
| 78 |
} |
| 79 |
|
| 80 |
std::unique_ptr<Document> Document::load(QIODevice *device, const QByteArray &ownerPassword, const QByteArray &userPassword) |
| 81 |
{ |
| 82 |
DocumentData *doc = new DocumentData(device, GooString(ownerPassword.data()), GooString(userPassword.data())); |
| 83 |
return DocumentData::checkDocument(doc); |
| 84 |
} |
| 85 |
|
| 86 |
std::unique_ptr<Document> Document::loadFromData(const QByteArray &fileContents, const QByteArray &ownerPassword, const QByteArray &userPassword) |
| 87 |
{ |
| 88 |
// create stream |
| 89 |
DocumentData *doc = new DocumentData(fileContents, GooString(ownerPassword.data()), GooString(userPassword.data())); |
| 90 |
return DocumentData::checkDocument(doc); |
| 91 |
} |
| 92 |
|
| 93 |
std::unique_ptr<Document> DocumentData::checkDocument(DocumentData *doc) |
| 94 |
{ |
| 95 |
if (doc->doc->isOk() || doc->doc->getErrorCode() == errEncrypted) { |
| 96 |
auto pdoc = std::unique_ptr<Document>(new Document(doc)); |
| 97 |
if (doc->doc->getErrorCode() == errEncrypted) { |
| 98 |
pdoc->m_doc->locked = true; |
| 99 |
} else { |
| 100 |
pdoc->m_doc->locked = false; |
| 101 |
pdoc->m_doc->fillMembers(); |
| 102 |
} |
| 103 |
return pdoc; |
| 104 |
} else { |
| 105 |
delete doc; |
| 106 |
} |
| 107 |
return nullptr; |
| 108 |
} |
| 109 |
|
| 110 |
Document::Document(DocumentData *dataA) |
| 111 |
{ |
| 112 |
m_doc = dataA; |
| 113 |
} |
| 114 |
|
| 115 |
Document::~Document() |
| 116 |
{ |
| 117 |
delete m_doc; |
| 118 |
} |
| 119 |
|
| 120 |
std::unique_ptr<Page> Document::page(int index) const |
| 121 |
{ |
| 122 |
// Cannot call std::make_unique, because the constructor of Page is private |
| 123 |
auto page = std::unique_ptr<Page>(new Page(m_doc, index)); |
| 124 |
if (page->m_page->page == nullptr) { |
| 125 |
page.reset(); |
| 126 |
} |
| 127 |
|
| 128 |
return page; |
| 129 |
} |
| 130 |
|
| 131 |
bool Document::isLocked() const |
| 132 |
{ |
| 133 |
return m_doc->locked; |
| 134 |
} |
| 135 |
|
| 136 |
bool Document::unlock(const QByteArray &ownerPassword, const QByteArray &userPassword) |
| 137 |
{ |
| 138 |
if (m_doc->locked) { |
| 139 |
/* racier then it needs to be */ |
| 140 |
DocumentData *doc2; |
| 141 |
if (!m_doc->fileContents.isEmpty()) { |
| 142 |
doc2 = new DocumentData(m_doc->fileContents, GooString(ownerPassword.data()), GooString(userPassword.data())); |
| 143 |
} else if (m_doc->m_device) { |
| 144 |
doc2 = new DocumentData(m_doc->m_device, GooString(ownerPassword.data()), GooString(userPassword.data())); |
| 145 |
} else { |
| 146 |
doc2 = new DocumentData(m_doc->m_filePath, GooString(ownerPassword.data()), GooString(userPassword.data())); |
| 147 |
} |
| 148 |
if (!doc2->doc->isOk()) { |
| 149 |
delete doc2; |
| 150 |
} else { |
| 151 |
delete m_doc; |
| 152 |
m_doc = doc2; |
| 153 |
m_doc->locked = false; |
| 154 |
m_doc->fillMembers(); |
| 155 |
} |
| 156 |
} |
| 157 |
return m_doc->locked; |
| 158 |
} |
| 159 |
|
| 160 |
Document::PageMode Document::pageMode() const |
| 161 |
{ |
| 162 |
switch (m_doc->doc->getCatalog()->getPageMode()) { |
| 163 |
case Catalog::pageModeNone: |
| 164 |
return UseNone; |
| 165 |
case Catalog::pageModeOutlines: |
| 166 |
return UseOutlines; |
| 167 |
case Catalog::pageModeThumbs: |
| 168 |
return UseThumbs; |
| 169 |
case Catalog::pageModeFullScreen: |
| 170 |
return FullScreen; |
| 171 |
case Catalog::pageModeOC: |
| 172 |
return UseOC; |
| 173 |
case Catalog::pageModeAttach: |
| 174 |
return UseAttach; |
| 175 |
default: |
| 176 |
return UseNone; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
Document::PageLayout Document::pageLayout() const |
| 181 |
{ |
| 182 |
switch (m_doc->doc->getCatalog()->getPageLayout()) { |
| 183 |
case Catalog::pageLayoutNone: |
| 184 |
return NoLayout; |
| 185 |
case Catalog::pageLayoutSinglePage: |
| 186 |
return SinglePage; |
| 187 |
case Catalog::pageLayoutOneColumn: |
| 188 |
return OneColumn; |
| 189 |
case Catalog::pageLayoutTwoColumnLeft: |
| 190 |
return TwoColumnLeft; |
| 191 |
case Catalog::pageLayoutTwoColumnRight: |
| 192 |
return TwoColumnRight; |
| 193 |
case Catalog::pageLayoutTwoPageLeft: |
| 194 |
return TwoPageLeft; |
| 195 |
case Catalog::pageLayoutTwoPageRight: |
| 196 |
return TwoPageRight; |
| 197 |
default: |
| 198 |
return NoLayout; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
Qt::LayoutDirection Document::textDirection() const |
| 203 |
{ |
| 204 |
if (!m_doc->doc->getCatalog()->getViewerPreferences()) { |
| 205 |
return Qt::LayoutDirectionAuto; |
| 206 |
} |
| 207 |
|
| 208 |
switch (m_doc->doc->getCatalog()->getViewerPreferences()->getDirection()) { |
| 209 |
case ViewerPreferences::directionL2R: |
| 210 |
return Qt::LeftToRight; |
| 211 |
case ViewerPreferences::directionR2L: |
| 212 |
return Qt::RightToLeft; |
| 213 |
default: |
| 214 |
return Qt::LayoutDirectionAuto; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
int Document::numPages() const |
| 219 |
{ |
| 220 |
return m_doc->doc->getNumPages(); |
| 221 |
} |
| 222 |
|
| 223 |
QList<FontInfo> Document::fonts() const |
| 224 |
{ |
| 225 |
QList<FontInfo> ourList; |
| 226 |
FontIterator it(0, m_doc); |
| 227 |
while (it.hasNext()) { |
| 228 |
ourList += it.next(); |
| 229 |
} |
| 230 |
return ourList; |
| 231 |
} |
| 232 |
|
| 233 |
QList<EmbeddedFile *> Document::embeddedFiles() const |
| 234 |
{ |
| 235 |
return m_doc->m_embeddedFiles; |
| 236 |
} |
| 237 |
|
| 238 |
std::unique_ptr<FontIterator> Document::newFontIterator(int startPage) const |
| 239 |
{ |
| 240 |
// Cannot use std::make_unique, because the FontIterator constructor is private |
| 241 |
return std::unique_ptr<FontIterator>(new FontIterator(startPage, m_doc)); |
| 242 |
} |
| 243 |
|
| 244 |
QByteArray Document::fontData(const FontInfo &fi) const |
| 245 |
{ |
| 246 |
QByteArray result; |
| 247 |
if (fi.isEmbedded()) { |
| 248 |
XRef *xref = m_doc->doc->getXRef()->copy(); |
| 249 |
|
| 250 |
Object refObj(fi.m_data->embRef); |
| 251 |
Object strObj = refObj.fetch(xref); |
| 252 |
if (strObj.isStream() && strObj.streamRewind()) { |
| 253 |
int c; |
| 254 |
while ((c = strObj.streamGetChar()) != EOF) { |
| 255 |
result.append((char)c); |
| 256 |
} |
| 257 |
strObj.streamClose(); |
| 258 |
} |
| 259 |
delete xref; |
| 260 |
} |
| 261 |
return result; |
| 262 |
} |
| 263 |
|
| 264 |
QString Document::info(const QString &type) const |
| 265 |
{ |
| 266 |
if (m_doc->locked) { |
| 267 |
return QString(); |
| 268 |
} |
| 269 |
|
| 270 |
std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoStringEntry(type.toLatin1().constData())); |
| 271 |
return UnicodeParsedString(goo.get()); |
| 272 |
} |
| 273 |
|
| 274 |
bool Document::setInfo(const QString &key, const QString &val) |
| 275 |
{ |
| 276 |
if (m_doc->locked) { |
| 277 |
return false; |
| 278 |
} |
| 279 |
|
| 280 |
std::unique_ptr<GooString> goo = QStringToUnicodeGooString(val); |
| 281 |
m_doc->doc->setDocInfoStringEntry(key.toLatin1().constData(), std::move(goo)); |
| 282 |
return true; |
| 283 |
} |
| 284 |
|
| 285 |
QString Document::title() const |
| 286 |
{ |
| 287 |
if (m_doc->locked) { |
| 288 |
return QString(); |
| 289 |
} |
| 290 |
|
| 291 |
std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoTitle()); |
| 292 |
return UnicodeParsedString(goo.get()); |
| 293 |
} |
| 294 |
|
| 295 |
bool Document::setTitle(const QString &val) |
| 296 |
{ |
| 297 |
if (m_doc->locked) { |
| 298 |
return false; |
| 299 |
} |
| 300 |
|
| 301 |
m_doc->doc->setDocInfoTitle(QStringToUnicodeGooString(val)); |
| 302 |
return true; |
| 303 |
} |
| 304 |
|
| 305 |
QString Document::author() const |
| 306 |
{ |
| 307 |
if (m_doc->locked) { |
| 308 |
return QString(); |
| 309 |
} |
| 310 |
|
| 311 |
std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoAuthor()); |
| 312 |
return UnicodeParsedString(goo.get()); |
| 313 |
} |
| 314 |
|
| 315 |
bool Document::setAuthor(const QString &val) |
| 316 |
{ |
| 317 |
if (m_doc->locked) { |
| 318 |
return false; |
| 319 |
} |
| 320 |
|
| 321 |
m_doc->doc->setDocInfoAuthor(QStringToUnicodeGooString(val)); |
| 322 |
return true; |
| 323 |
} |
| 324 |
|
| 325 |
QString Document::subject() const |
| 326 |
{ |
| 327 |
if (m_doc->locked) { |
| 328 |
return QString(); |
| 329 |
} |
| 330 |
|
| 331 |
std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoSubject()); |
| 332 |
return UnicodeParsedString(goo.get()); |
| 333 |
} |
| 334 |
|
| 335 |
bool Document::setSubject(const QString &val) |
| 336 |
{ |
| 337 |
if (m_doc->locked) { |
| 338 |
return false; |
| 339 |
} |
| 340 |
|
| 341 |
m_doc->doc->setDocInfoSubject(QStringToUnicodeGooString(val)); |
| 342 |
return true; |
| 343 |
} |
| 344 |
|
| 345 |
QString Document::keywords() const |
| 346 |
{ |
| 347 |
if (m_doc->locked) { |
| 348 |
return QString(); |
| 349 |
} |
| 350 |
|
| 351 |
std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoKeywords()); |
| 352 |
return UnicodeParsedString(goo.get()); |
| 353 |
} |
| 354 |
|
| 355 |
bool Document::setKeywords(const QString &val) |
| 356 |
{ |
| 357 |
if (m_doc->locked) { |
| 358 |
return false; |
| 359 |
} |
| 360 |
|
| 361 |
m_doc->doc->setDocInfoKeywords(QStringToUnicodeGooString(val)); |
| 362 |
return true; |
| 363 |
} |
| 364 |
|
| 365 |
QString Document::creator() const |
| 366 |
{ |
| 367 |
if (m_doc->locked) { |
| 368 |
return QString(); |
| 369 |
} |
| 370 |
|
| 371 |
std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoCreator()); |
| 372 |
return UnicodeParsedString(goo.get()); |
| 373 |
} |
| 374 |
|
| 375 |
bool Document::setCreator(const QString &val) |
| 376 |
{ |
| 377 |
if (m_doc->locked) { |
| 378 |
return false; |
| 379 |
} |
| 380 |
|
| 381 |
m_doc->doc->setDocInfoCreator(QStringToUnicodeGooString(val)); |
| 382 |
return true; |
| 383 |
} |
| 384 |
|
| 385 |
QString Document::producer() const |
| 386 |
{ |
| 387 |
if (m_doc->locked) { |
| 388 |
return QString(); |
| 389 |
} |
| 390 |
|
| 391 |
std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoProducer()); |
| 392 |
return UnicodeParsedString(goo.get()); |
| 393 |
} |
| 394 |
|
| 395 |
bool Document::setProducer(const QString &val) |
| 396 |
{ |
| 397 |
if (m_doc->locked) { |
| 398 |
return false; |
| 399 |
} |
| 400 |
|
| 401 |
m_doc->doc->setDocInfoProducer(QStringToUnicodeGooString(val)); |
| 402 |
return true; |
| 403 |
} |
| 404 |
|
| 405 |
bool Document::removeInfo() |
| 406 |
{ |
| 407 |
if (m_doc->locked) { |
| 408 |
return false; |
| 409 |
} |
| 410 |
|
| 411 |
m_doc->doc->removeDocInfo(); |
| 412 |
return true; |
| 413 |
} |
| 414 |
|
| 415 |
QStringList Document::infoKeys() const |
| 416 |
{ |
| 417 |
QStringList keys; |
| 418 |
|
| 419 |
if (m_doc->locked) { |
| 420 |
return QStringList(); |
| 421 |
} |
| 422 |
|
| 423 |
QScopedPointer<XRef> xref(m_doc->doc->getXRef()->copy()); |
| 424 |
if (!xref) { |
| 425 |
return QStringList(); |
| 426 |
} |
| 427 |
Object info = xref->getDocInfo(); |
| 428 |
if (!info.isDict()) { |
| 429 |
return QStringList(); |
| 430 |
} |
| 431 |
|
| 432 |
Dict *infoDict = info.getDict(); |
| 433 |
// somehow iterate over keys in infoDict |
| 434 |
keys.reserve(infoDict->getLength()); |
| 435 |
for (int i = 0; i < infoDict->getLength(); ++i) { |
| 436 |
keys.append(QString::fromLatin1(infoDict->getKey(i))); |
| 437 |
} |
| 438 |
|
| 439 |
return keys; |
| 440 |
} |
| 441 |
|
| 442 |
QDateTime Document::date(const QString &type) const |
| 443 |
{ |
| 444 |
if (m_doc->locked) { |
| 445 |
return QDateTime(); |
| 446 |
} |
| 447 |
|
| 448 |
std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoStringEntry(type.toLatin1().constData())); |
| 449 |
QString str = UnicodeParsedString(goo.get()); |
| 450 |
return Poppler::convertDate(str.toLatin1().constData()); |
| 451 |
} |
| 452 |
|
| 453 |
bool Document::setDate(const QString &key, const QDateTime &val) |
| 454 |
{ |
| 455 |
if (m_doc->locked) { |
| 456 |
return false; |
| 457 |
} |
| 458 |
|
| 459 |
m_doc->doc->setDocInfoStringEntry(key.toLatin1().constData(), QDateTimeToUnicodeGooString(val)); |
| 460 |
return true; |
| 461 |
} |
| 462 |
|
| 463 |
QDateTime Document::creationDate() const |
| 464 |
{ |
| 465 |
if (m_doc->locked) { |
| 466 |
return QDateTime(); |
| 467 |
} |
| 468 |
|
| 469 |
std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoCreatDate()); |
| 470 |
QString str = UnicodeParsedString(goo.get()); |
| 471 |
return Poppler::convertDate(str.toLatin1().constData()); |
| 472 |
} |
| 473 |
|
| 474 |
bool Document::setCreationDate(const QDateTime &val) |
| 475 |
{ |
| 476 |
if (m_doc->locked) { |
| 477 |
return false; |
| 478 |
} |
| 479 |
|
| 480 |
m_doc->doc->setDocInfoCreatDate(QDateTimeToUnicodeGooString(val)); |
| 481 |
return true; |
| 482 |
} |
| 483 |
|
| 484 |
QDateTime Document::modificationDate() const |
| 485 |
{ |
| 486 |
if (m_doc->locked) { |
| 487 |
return QDateTime(); |
| 488 |
} |
| 489 |
|
| 490 |
std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoModDate()); |
| 491 |
QString str = UnicodeParsedString(goo.get()); |
| 492 |
return Poppler::convertDate(str.toLatin1().constData()); |
| 493 |
} |
| 494 |
|
| 495 |
bool Document::setModificationDate(const QDateTime &val) |
| 496 |
{ |
| 497 |
if (m_doc->locked) { |
| 498 |
return false; |
| 499 |
} |
| 500 |
|
| 501 |
m_doc->doc->setDocInfoModDate(QDateTimeToUnicodeGooString(val)); |
| 502 |
return true; |
| 503 |
} |
| 504 |
|
| 505 |
bool Document::isEncrypted() const |
| 506 |
{ |
| 507 |
return m_doc->doc->isEncrypted(); |
| 508 |
} |
| 509 |
|
| 510 |
bool Document::isLinearized() const |
| 511 |
{ |
| 512 |
return m_doc->doc->isLinearized(); |
| 513 |
} |
| 514 |
|
| 515 |
bool Document::okToPrint() const |
| 516 |
{ |
| 517 |
return m_doc->doc->okToPrint(); |
| 518 |
} |
| 519 |
|
| 520 |
bool Document::okToPrintHighRes() const |
| 521 |
{ |
| 522 |
return m_doc->doc->okToPrintHighRes(); |
| 523 |
} |
| 524 |
|
| 525 |
bool Document::okToChange() const |
| 526 |
{ |
| 527 |
return m_doc->doc->okToChange(); |
| 528 |
} |
| 529 |
|
| 530 |
bool Document::okToCopy() const |
| 531 |
{ |
| 532 |
return m_doc->doc->okToCopy(); |
| 533 |
} |
| 534 |
|
| 535 |
bool Document::okToAddNotes() const |
| 536 |
{ |
| 537 |
return m_doc->doc->okToAddNotes(); |
| 538 |
} |
| 539 |
|
| 540 |
bool Document::okToFillForm() const |
| 541 |
{ |
| 542 |
return m_doc->doc->okToFillForm(); |
| 543 |
} |
| 544 |
|
| 545 |
bool Document::okToCreateFormFields() const |
| 546 |
{ |
| 547 |
return (okToFillForm() && okToChange()); |
| 548 |
} |
| 549 |
|
| 550 |
bool Document::okToExtractForAccessibility() const |
| 551 |
{ |
| 552 |
return m_doc->doc->okToAccessibility(); |
| 553 |
} |
| 554 |
|
| 555 |
bool Document::okToAssemble() const |
| 556 |
{ |
| 557 |
return m_doc->doc->okToAssemble(); |
| 558 |
} |
| 559 |
|
| 560 |
Document::PdfVersion Document::getPdfVersion() const |
| 561 |
{ |
| 562 |
return PdfVersion { m_doc->doc->getPDFMajorVersion(), m_doc->doc->getPDFMinorVersion() }; |
| 563 |
} |
| 564 |
|
| 565 |
std::unique_ptr<Page> Document::page(const QString &label) const |
| 566 |
{ |
| 567 |
const GooString label_g(label.toLatin1().data()); |
| 568 |
int index; |
| 569 |
|
| 570 |
if (!m_doc->doc->getCatalog()->labelToIndex(label_g, &index)) { |
| 571 |
const std::unique_ptr<GooString> label_ug(QStringToUnicodeGooString(label)); |
| 572 |
if (!m_doc->doc->getCatalog()->labelToIndex(*label_ug, &index)) { |
| 573 |
return nullptr; |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
return page(index); |
| 578 |
} |
| 579 |
|
| 580 |
bool Document::hasEmbeddedFiles() const |
| 581 |
{ |
| 582 |
return (!(0 == m_doc->doc->getCatalog()->numEmbeddedFiles())); |
| 583 |
} |
| 584 |
|
| 585 |
QVector<OutlineItem> Document::outline() const |
| 586 |
{ |
| 587 |
QVector<OutlineItem> result; |
| 588 |
|
| 589 |
if (::Outline *outline = m_doc->doc->getOutline()) { |
| 590 |
if (const std::vector<::OutlineItem *> *items = outline->getItems()) { |
| 591 |
for (void *item : *items) { |
| 592 |
result.push_back(OutlineItem { new OutlineItemData { static_cast<::OutlineItem *>(item), m_doc } }); |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
return result; |
| 598 |
} |
| 599 |
|
| 600 |
std::unique_ptr<LinkDestination> Document::linkDestination(const QString &name) |
| 601 |
{ |
| 602 |
const std::unique_ptr<GooString> namedDest = QStringToGooString(name); |
| 603 |
LinkDestinationData ldd(nullptr, namedDest.get(), m_doc, false); |
| 604 |
auto ld = std::make_unique<LinkDestination>(ldd); |
| 605 |
return ld; |
| 606 |
} |
| 607 |
|
| 608 |
void Document::setPaperColor(const QColor &color) |
| 609 |
{ |
| 610 |
m_doc->setPaperColor(color); |
| 611 |
} |
| 612 |
|
| 613 |
void Document::setColorDisplayProfile(void *outputProfileA) |
| 614 |
{ |
| 615 |
#if USE_CMS |
| 616 |
if (m_doc->m_sRGBProfile && m_doc->m_sRGBProfile.get() == outputProfileA) { |
| 617 |
// Catch the special case that the user passes the sRGB profile |
| 618 |
m_doc->m_displayProfile = m_doc->m_sRGBProfile; |
| 619 |
return; |
| 620 |
} |
| 621 |
if (m_doc->m_displayProfile && m_doc->m_displayProfile.get() == outputProfileA) { |
| 622 |
// Catch the special case that the user passes the display profile |
| 623 |
return; |
| 624 |
} |
| 625 |
m_doc->m_displayProfile = make_GfxLCMSProfilePtr(outputProfileA); |
| 626 |
#else |
| 627 |
Q_UNUSED(outputProfileA); |
| 628 |
#endif |
| 629 |
} |
| 630 |
|
| 631 |
void Document::setColorDisplayProfileName(const QString &name) |
| 632 |
{ |
| 633 |
#if USE_CMS |
| 634 |
void *rawprofile = cmsOpenProfileFromFile(name.toLocal8Bit().constData(), "r"); |
| 635 |
m_doc->m_displayProfile = make_GfxLCMSProfilePtr(rawprofile); |
| 636 |
#else |
| 637 |
Q_UNUSED(name); |
| 638 |
#endif |
| 639 |
} |
| 640 |
|
| 641 |
void *Document::colorRgbProfile() const |
| 642 |
{ |
| 643 |
#if USE_CMS |
| 644 |
if (!m_doc->m_sRGBProfile) { |
| 645 |
m_doc->m_sRGBProfile = make_GfxLCMSProfilePtr(cmsCreate_sRGBProfile()); |
| 646 |
} |
| 647 |
return m_doc->m_sRGBProfile.get(); |
| 648 |
#else |
| 649 |
return nullptr; |
| 650 |
#endif |
| 651 |
} |
| 652 |
|
| 653 |
void *Document::colorDisplayProfile() const |
| 654 |
{ |
| 655 |
#if USE_CMS |
| 656 |
return m_doc->m_displayProfile.get(); |
| 657 |
#else |
| 658 |
return nullptr; |
| 659 |
#endif |
| 660 |
} |
| 661 |
|
| 662 |
QColor Document::paperColor() const |
| 663 |
{ |
| 664 |
return m_doc->paperColor; |
| 665 |
} |
| 666 |
|
| 667 |
void Document::setRenderBackend(Document::RenderBackend backend) |
| 668 |
{ |
| 669 |
// no need to delete the outputdev as for the moment we always create a splash one |
| 670 |
// as the QPainter one does not allow "precaching" due to its signature |
| 671 |
// delete m_doc->m_outputDev; |
| 672 |
// m_doc->m_outputDev = NULL; |
| 673 |
m_doc->m_backend = backend; |
| 674 |
} |
| 675 |
|
| 676 |
Document::RenderBackend Document::renderBackend() const |
| 677 |
{ |
| 678 |
return m_doc->m_backend; |
| 679 |
} |
| 680 |
|
| 681 |
QSet<Document::RenderBackend> Document::availableRenderBackends() |
| 682 |
{ |
| 683 |
QSet<Document::RenderBackend> ret; |
| 684 |
ret << Document::SplashBackend; |
| 685 |
ret << Document::QPainterBackend; |
| 686 |
return ret; |
| 687 |
} |
| 688 |
|
| 689 |
void Document::setRenderHint(Document::RenderHint hint, bool on) |
| 690 |
{ |
| 691 |
const bool touchesOverprinting = hint & Document::OverprintPreview; |
| 692 |
|
| 693 |
int hintForOperation = hint; |
| 694 |
if (touchesOverprinting && !isOverprintPreviewAvailable()) { |
| 695 |
hintForOperation = hintForOperation & ~(int)Document::OverprintPreview; |
| 696 |
} |
| 697 |
|
| 698 |
if (on) { |
| 699 |
m_doc->m_hints |= hintForOperation; |
| 700 |
} else { |
| 701 |
m_doc->m_hints &= ~hintForOperation; |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
Document::RenderHints Document::renderHints() const |
| 706 |
{ |
| 707 |
return Document::RenderHints(m_doc->m_hints); |
| 708 |
} |
| 709 |
|
| 710 |
std::unique_ptr<PSConverter> Document::psConverter() const |
| 711 |
{ |
| 712 |
// Cannot use std::make_unique, because the PSConverter constructor is private |
| 713 |
return std::unique_ptr<PSConverter>(new PSConverter(m_doc)); |
| 714 |
} |
| 715 |
|
| 716 |
std::unique_ptr<PDFConverter> Document::pdfConverter() const |
| 717 |
{ |
| 718 |
// Cannot use std::make_unique, because the PDFConverter constructor is private |
| 719 |
return std::unique_ptr<PDFConverter>(new PDFConverter(m_doc)); |
| 720 |
} |
| 721 |
|
| 722 |
QString Document::metadata() const |
| 723 |
{ |
| 724 |
QString result; |
| 725 |
Catalog *catalog = m_doc->doc->getCatalog(); |
| 726 |
if (catalog && catalog->isOk()) { |
| 727 |
std::unique_ptr<GooString> s = catalog->readMetadata(); |
| 728 |
if (s) { |
| 729 |
result = UnicodeParsedString(s.get()); |
| 730 |
} |
| 731 |
} |
| 732 |
return result; |
| 733 |
} |
| 734 |
|
| 735 |
bool Document::hasOptionalContent() const |
| 736 |
{ |
| 737 |
return (m_doc->doc->getOptContentConfig() && m_doc->doc->getOptContentConfig()->hasOCGs()); |
| 738 |
} |
| 739 |
|
| 740 |
OptContentModel *Document::optionalContentModel() |
| 741 |
{ |
| 742 |
if (m_doc->m_optContentModel.isNull()) { |
| 743 |
m_doc->m_optContentModel = new OptContentModel(m_doc->doc->getOptContentConfig(), nullptr); |
| 744 |
} |
| 745 |
return (OptContentModel *)m_doc->m_optContentModel; |
| 746 |
} |
| 747 |
|
| 748 |
void Document::applyResetFormsLink(const LinkResetForm &link) |
| 749 |
{ |
| 750 |
const LinkResetFormPrivate *lrfp = link.d_func(); |
| 751 |
Catalog *catalog = m_doc->doc->getCatalog(); |
| 752 |
if (catalog && catalog->isOk()) { |
| 753 |
Form *form = catalog->getForm(); |
| 754 |
if (form) { |
| 755 |
std::vector<std::string> stdStringFields; |
| 756 |
const QStringList fields = lrfp->m_fields; |
| 757 |
stdStringFields.reserve(fields.size()); |
| 758 |
for (const auto &field : fields) { |
| 759 |
stdStringFields.emplace_back(field.toStdString()); |
| 760 |
} |
| 761 |
form->reset(stdStringFields, lrfp->m_exclude); |
| 762 |
} |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
QStringList Document::scripts() const |
| 767 |
{ |
| 768 |
Catalog *catalog = m_doc->doc->getCatalog(); |
| 769 |
const int numScripts = catalog->numJS(); |
| 770 |
QStringList scripts; |
| 771 |
for (int i = 0; i < numScripts; ++i) { |
| 772 |
std::string s = catalog->getJS(i); |
| 773 |
if (!s.empty()) { |
| 774 |
scripts.append(UnicodeParsedString(s)); |
| 775 |
} |
| 776 |
} |
| 777 |
return scripts; |
| 778 |
} |
| 779 |
|
| 780 |
std::unique_ptr<Link> Document::additionalAction(DocumentAdditionalActionsType type) const |
| 781 |
{ |
| 782 |
Catalog::DocumentAdditionalActionsType actionType; |
| 783 |
switch (type) { |
| 784 |
case CloseDocument: |
| 785 |
actionType = Catalog::actionCloseDocument; |
| 786 |
break; |
| 787 |
case SaveDocumentStart: |
| 788 |
actionType = Catalog::actionSaveDocumentStart; |
| 789 |
break; |
| 790 |
case SaveDocumentFinish: |
| 791 |
actionType = Catalog::actionSaveDocumentFinish; |
| 792 |
break; |
| 793 |
case PrintDocumentStart: |
| 794 |
actionType = Catalog::actionPrintDocumentStart; |
| 795 |
break; |
| 796 |
case PrintDocumentFinish: |
| 797 |
actionType = Catalog::actionPrintDocumentFinish; |
| 798 |
break; |
| 799 |
default: |
| 800 |
return {}; |
| 801 |
} |
| 802 |
|
| 803 |
if (std::unique_ptr<::LinkAction> act = m_doc->doc->getCatalog()->getAdditionalAction(actionType)) { |
| 804 |
return PageData::convertLinkActionToLink(act.get(), m_doc, QRectF()); |
| 805 |
} |
| 806 |
|
| 807 |
return {}; |
| 808 |
} |
| 809 |
|
| 810 |
bool Document::getPdfId(QByteArray *permanentId, QByteArray *updateId) const |
| 811 |
{ |
| 812 |
GooString gooPermanentId; |
| 813 |
GooString gooUpdateId; |
| 814 |
|
| 815 |
if (!m_doc->doc->getID(permanentId ? &gooPermanentId : nullptr, updateId ? &gooUpdateId : nullptr)) { |
| 816 |
return false; |
| 817 |
} |
| 818 |
|
| 819 |
if (permanentId) { |
| 820 |
*permanentId = gooPermanentId.c_str(); |
| 821 |
} |
| 822 |
if (updateId) { |
| 823 |
*updateId = gooUpdateId.c_str(); |
| 824 |
} |
| 825 |
|
| 826 |
return true; |
| 827 |
} |
| 828 |
|
| 829 |
Document::FormType Document::formType() const |
| 830 |
{ |
| 831 |
switch (m_doc->doc->getCatalog()->getFormType()) { |
| 832 |
case Catalog::NoForm: |
| 833 |
return Document::NoForm; |
| 834 |
case Catalog::AcroForm: |
| 835 |
return Document::AcroForm; |
| 836 |
case Catalog::XfaForm: |
| 837 |
return Document::XfaForm; |
| 838 |
} |
| 839 |
|
| 840 |
return Document::NoForm; // make gcc happy |
| 841 |
} |
| 842 |
|
| 843 |
QVector<int> Document::formCalculateOrder() const |
| 844 |
{ |
| 845 |
Form *form = m_doc->doc->getCatalog()->getForm(); |
| 846 |
if (!form) { |
| 847 |
return {}; |
| 848 |
} |
| 849 |
|
| 850 |
QVector<int> result; |
| 851 |
const std::vector<Ref> &calculateOrder = form->getCalculateOrder(); |
| 852 |
for (Ref r : calculateOrder) { |
| 853 |
FormWidget *w = form->findWidgetByRef(r); |
| 854 |
if (w) { |
| 855 |
result << w->getID(); |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
return result; |
| 860 |
} |
| 861 |
|
| 862 |
std::vector<std::unique_ptr<FormFieldSignature>> Document::signatures() const |
| 863 |
{ |
| 864 |
std::vector<std::unique_ptr<FormFieldSignature>> result; |
| 865 |
|
| 866 |
const std::vector<::FormFieldSignature *> pSignatures = m_doc->doc->getSignatureFields(); |
| 867 |
|
| 868 |
for (::FormFieldSignature *pSignature : pSignatures) { |
| 869 |
::FormWidget *fw = pSignature->getCreateWidget(); |
| 870 |
::Page *p = m_doc->doc->getPage(fw->getWidgetAnnotation()->getPageNum()); |
| 871 |
result.push_back(std::make_unique<FormFieldSignature>(m_doc, p, static_cast<FormWidgetSignature *>(fw))); |
| 872 |
} |
| 873 |
|
| 874 |
return result; |
| 875 |
} |
| 876 |
|
| 877 |
bool Document::xrefWasReconstructed() const |
| 878 |
{ |
| 879 |
return m_doc->xrefReconstructed; |
| 880 |
} |
| 881 |
|
| 882 |
void Document::setXRefReconstructedCallback(const std::function<void()> &callback) |
| 883 |
{ |
| 884 |
m_doc->xrefReconstructedCallback = callback; |
| 885 |
} |
| 886 |
|
| 887 |
QDateTime convertDate(const char *dateString) |
| 888 |
{ |
| 889 |
int year, mon, day, hour, min, sec, tzHours, tzMins; |
| 890 |
char tz; |
| 891 |
|
| 892 |
GooString date(dateString); |
| 893 |
if (parseDateString(&date, &year, &mon, &day, &hour, &min, &sec, &tz, &tzHours, &tzMins)) { |
| 894 |
QDate d(year, mon, day); |
| 895 |
QTime t(hour, min, sec); |
| 896 |
if (d.isValid() && t.isValid()) { |
| 897 |
int tzSecs = 0; |
| 898 |
if (tz) { |
| 899 |
// then we have some form of timezone |
| 900 |
if ('Z' == tz) { |
| 901 |
// We are already at UTC |
| 902 |
} else if ('+' == tz) { |
| 903 |
// local time is ahead of UTC |
| 904 |
tzSecs = (tzHours * 3600) + (tzMins * 60); |
| 905 |
} else if ('-' == tz) { |
| 906 |
// local time is behind UTC |
| 907 |
tzSecs = (tzHours * -3600) + (tzMins * -60); |
| 908 |
} else { |
| 909 |
qWarning("unexpected tz val"); |
| 910 |
} |
| 911 |
} |
| 912 |
return QDateTime(d, t, QTimeZone(tzSecs)); |
| 913 |
} |
| 914 |
} |
| 915 |
return QDateTime(); |
| 916 |
} |
| 917 |
|
| 918 |
bool isCmsAvailable() |
| 919 |
{ |
| 920 |
#if USE_CMS |
| 921 |
return true; |
| 922 |
#else |
| 923 |
return false; |
| 924 |
#endif |
| 925 |
} |
| 926 |
|
| 927 |
bool isOverprintPreviewAvailable() |
| 928 |
{ |
| 929 |
return true; |
| 930 |
} |
| 931 |
|
| 932 |
} |