| 1 |
/* poppler-private.cc: qt interface to poppler |
| 2 |
* Copyright (C) 2005, Net Integration Technologies, Inc. |
| 3 |
* Copyright (C) 2006, 2011, 2015, 2017-2020, 2025 by Albert Astals Cid <[email protected]> |
| 4 |
* Copyright (C) 2008, 2010, 2011, 2014 by Pino Toscano <[email protected]> |
| 5 |
* Copyright (C) 2013 by Thomas Freitag <[email protected]> |
| 6 |
* Copyright (C) 2013 Adrian Johnson <[email protected]> |
| 7 |
* Copyright (C) 2016 Jakub Alba <[email protected]> |
| 8 |
* Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <[email protected]>. Work sponsored by the LiMux project of the city of Munich |
| 9 |
* Copyright (C) 2018-2020 Adam Reichold <[email protected]> |
| 10 |
* Copyright (C) 2019, 2020, 2024 Oliver Sander <[email protected]> |
| 11 |
* Copyright (C) 2019 João Netto <[email protected]> |
| 12 |
* Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, <[email protected]> |
| 13 |
* Copyright (C) 2021 Mahmoud Khalil <[email protected]> |
| 14 |
* Copyright (C) 2023 Shivodit Gill <[email protected]> |
| 15 |
* Copyright (C) 2024, 2025 g10 Code GmbH, Author: Sune Stolborg Vuorela <[email protected]> |
| 16 |
* Inspired on code by |
| 17 |
* Copyright (C) 2004 by Albert Astals Cid <[email protected]> |
| 18 |
* Copyright (C) 2004 by Enrico Ros <[email protected]> |
| 19 |
* |
| 20 |
* This program is free software; you can redistribute it and/or modify |
| 21 |
* it under the terms of the GNU General Public License as published by |
| 22 |
* the Free Software Foundation; either version 2, or (at your option) |
| 23 |
* any later version. |
| 24 |
* |
| 25 |
* This program is distributed in the hope that it will be useful, |
| 26 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 27 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 28 |
* GNU General Public License for more details. |
| 29 |
* |
| 30 |
* You should have received a copy of the GNU General Public License |
| 31 |
* along with this program; if not, write to the Free Software |
| 32 |
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
| 33 |
*/ |
| 34 |
|
| 35 |
#include "poppler-private.h" |
| 36 |
#include "poppler-form.h" |
| 37 |
|
| 38 |
#include <QtCore/QByteArray> |
| 39 |
#include <QtCore/QDebug> |
| 40 |
#include <QtCore/QVariant> |
| 41 |
|
| 42 |
#include <Link.h> |
| 43 |
#include <Outline.h> |
| 44 |
#include <PDFDocEncoding.h> |
| 45 |
#include <UnicodeMap.h> |
| 46 |
#include <UTF.h> |
| 47 |
#include <goo/gmem.h> |
| 48 |
|
| 49 |
#ifdef ANDROID |
| 50 |
# include <QtCore/QString> |
| 51 |
# include <QtCore/QDir> |
| 52 |
# include <QtCore/QFile> |
| 53 |
# include <QtCore/QFileInfo> |
| 54 |
# include <QtCore/QStandardPaths> |
| 55 |
# include <QtCore/QDirIterator> |
| 56 |
#endif |
| 57 |
|
| 58 |
namespace Poppler { |
| 59 |
|
| 60 |
namespace Debug { |
| 61 |
|
| 62 |
static void qDebugDebugFunction(const QString &message, const QVariant & /*closure*/) |
| 63 |
{ |
| 64 |
qDebug() << message; |
| 65 |
} |
| 66 |
|
| 67 |
PopplerDebugFunc debugFunction = qDebugDebugFunction; |
| 68 |
QVariant debugClosure; |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
void setDebugErrorFunction(PopplerDebugFunc function, const QVariant &closure) |
| 73 |
{ |
| 74 |
Debug::debugFunction = function ? function : Debug::qDebugDebugFunction; |
| 75 |
Debug::debugClosure = closure; |
| 76 |
} |
| 77 |
|
| 78 |
void qt6ErrorFunction(ErrorCategory /*category*/, Goffset pos, const char *msg) |
| 79 |
{ |
| 80 |
QString emsg; |
| 81 |
|
| 82 |
if (pos >= 0) { |
| 83 |
emsg = QStringLiteral("Error (%1): ").arg(pos); |
| 84 |
} else { |
| 85 |
emsg = QStringLiteral("Error: "); |
| 86 |
} |
| 87 |
emsg += QString::fromLatin1(msg); |
| 88 |
(*Debug::debugFunction)(emsg, Debug::debugClosure); |
| 89 |
} |
| 90 |
|
| 91 |
QString unicodeToQString(const Unicode *u, int len) |
| 92 |
{ |
| 93 |
const UnicodeMap *utf8Map = globalParams->getUtf8Map(); |
| 94 |
|
| 95 |
// ignore the last characters if they are 0x0 |
| 96 |
while ((len > 0) && (u[len - 1] == 0)) { |
| 97 |
--len; |
| 98 |
} |
| 99 |
|
| 100 |
GooString convertedStr; |
| 101 |
for (int i = 0; i < len; ++i) { |
| 102 |
char buf[8]; |
| 103 |
const int n = utf8Map->mapUnicode(u[i], buf, sizeof(buf)); |
| 104 |
convertedStr.append(buf, n); |
| 105 |
} |
| 106 |
|
| 107 |
return QString::fromUtf8(convertedStr.c_str(), convertedStr.size()); |
| 108 |
} |
| 109 |
|
| 110 |
QString unicodeToQString(const std::vector<Unicode> &u) |
| 111 |
{ |
| 112 |
return unicodeToQString(u.data(), u.size()); |
| 113 |
} |
| 114 |
|
| 115 |
QString UnicodeParsedString(const GooString *s1) |
| 116 |
{ |
| 117 |
return (s1) ? UnicodeParsedString(s1->toStr()) : QString(); |
| 118 |
} |
| 119 |
|
| 120 |
QString UnicodeParsedString(const std::string &s1) |
| 121 |
{ |
| 122 |
if (s1.empty()) { |
| 123 |
return QString(); |
| 124 |
} |
| 125 |
|
| 126 |
if (hasUnicodeByteOrderMark(s1) || hasUnicodeByteOrderMarkLE(s1)) { |
| 127 |
return QString::fromUtf16(reinterpret_cast<const char16_t *>(s1.c_str()), s1.size() / 2); |
| 128 |
} else { |
| 129 |
std::string cString = pdfDocEncodingToUTF16(s1); |
| 130 |
auto result = QString::fromUtf16(reinterpret_cast<const char16_t *>(cString.c_str()), cString.size() / 2); |
| 131 |
return result; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
std::unique_ptr<GooString> QStringToUnicodeGooString(const QString &s) |
| 136 |
{ |
| 137 |
if (s.isEmpty()) { |
| 138 |
return std::make_unique<GooString>(); |
| 139 |
} |
| 140 |
int len = s.length() * 2 + 2; |
| 141 |
std::string string; |
| 142 |
string.reserve(len); |
| 143 |
string.push_back((char)0xfe); |
| 144 |
string.push_back((char)0xff); |
| 145 |
for (auto element : s) { |
| 146 |
string.push_back(element.row()); |
| 147 |
string.push_back(element.cell()); |
| 148 |
} |
| 149 |
return std::make_unique<GooString>(string); |
| 150 |
} |
| 151 |
|
| 152 |
std::unique_ptr<GooString> QStringToGooString(const QString &s) |
| 153 |
{ |
| 154 |
int len = s.length(); |
| 155 |
char *cstring = (char *)gmallocn(s.length(), sizeof(char)); |
| 156 |
for (int i = 0; i < len; ++i) { |
| 157 |
cstring[i] = s.at(i).unicode(); |
| 158 |
} |
| 159 |
std::unique_ptr<GooString> ret = std::make_unique<GooString>(cstring, len); |
| 160 |
gfree(cstring); |
| 161 |
return ret; |
| 162 |
} |
| 163 |
|
| 164 |
std::unique_ptr<GooString> QDateTimeToUnicodeGooString(const QDateTime &dt) |
| 165 |
{ |
| 166 |
if (!dt.isValid()) { |
| 167 |
return nullptr; |
| 168 |
} |
| 169 |
|
| 170 |
return QStringToUnicodeGooString(dt.toUTC().toString(QStringLiteral("yyyyMMddhhmmss+00'00'"))); |
| 171 |
} |
| 172 |
|
| 173 |
Annot::AdditionalActionsType toPopplerAdditionalActionType(Annotation::AdditionalActionType type) |
| 174 |
{ |
| 175 |
switch (type) { |
| 176 |
case Annotation::CursorEnteringAction: |
| 177 |
return Annot::actionCursorEntering; |
| 178 |
case Annotation::CursorLeavingAction: |
| 179 |
return Annot::actionCursorLeaving; |
| 180 |
case Annotation::MousePressedAction: |
| 181 |
return Annot::actionMousePressed; |
| 182 |
case Annotation::MouseReleasedAction: |
| 183 |
return Annot::actionMouseReleased; |
| 184 |
case Annotation::FocusInAction: |
| 185 |
return Annot::actionFocusIn; |
| 186 |
case Annotation::FocusOutAction: |
| 187 |
return Annot::actionFocusOut; |
| 188 |
case Annotation::PageOpeningAction: |
| 189 |
return Annot::actionPageOpening; |
| 190 |
case Annotation::PageClosingAction: |
| 191 |
return Annot::actionPageClosing; |
| 192 |
case Annotation::PageVisibleAction: |
| 193 |
return Annot::actionPageVisible; |
| 194 |
case Annotation::PageInvisibleAction: |
| 195 |
return Annot::actionPageInvisible; |
| 196 |
} |
| 197 |
|
| 198 |
return Annot::actionCursorEntering; |
| 199 |
} |
| 200 |
|
| 201 |
DocumentData::~DocumentData() |
| 202 |
{ |
| 203 |
qDeleteAll(m_embeddedFiles); |
| 204 |
delete (OptContentModel *)m_optContentModel; |
| 205 |
delete doc; |
| 206 |
} |
| 207 |
|
| 208 |
void DocumentData::init() |
| 209 |
{ |
| 210 |
m_backend = Document::SplashBackend; |
| 211 |
paperColor = Qt::white; |
| 212 |
m_hints = 0; |
| 213 |
m_optContentModel = nullptr; |
| 214 |
xrefReconstructed = false; |
| 215 |
xrefReconstructedCallback = {}; |
| 216 |
|
| 217 |
#ifdef ANDROID |
| 218 |
// Copy fonts from android apk to the app's storage dir, and |
| 219 |
// set the font directory path |
| 220 |
QString assetsFontDir = QStringLiteral("assets:/share/fonts"); |
| 221 |
QString fontsdir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/fonts"); |
| 222 |
QDir fontPath = QDir(fontsdir); |
| 223 |
|
| 224 |
if (fontPath.mkpath(fontPath.absolutePath())) { |
| 225 |
GlobalParams::setFontDir(fontPath.absolutePath().toStdString()); |
| 226 |
QDirIterator iterator(assetsFontDir, QDir::NoFilter, QDirIterator::Subdirectories); |
| 227 |
|
| 228 |
while (iterator.hasNext()) { |
| 229 |
iterator.next(); |
| 230 |
QFileInfo fontFileInfo = iterator.fileInfo(); |
| 231 |
QString fontFilePath = assetsFontDir + QStringLiteral("/") + fontFileInfo.fileName(); |
| 232 |
QString destPath = fontPath.absolutePath() + QStringLiteral("/") + fontFileInfo.fileName(); |
| 233 |
QFile::copy(fontFilePath, destPath); |
| 234 |
} |
| 235 |
} else { |
| 236 |
GlobalParams::setFontDir(""); |
| 237 |
} |
| 238 |
#endif |
| 239 |
} |
| 240 |
|
| 241 |
void DocumentData::noitfyXRefReconstructed() |
| 242 |
{ |
| 243 |
if (!xrefReconstructed) { |
| 244 |
xrefReconstructed = true; |
| 245 |
} |
| 246 |
|
| 247 |
if (xrefReconstructedCallback) { |
| 248 |
xrefReconstructedCallback(); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
FormWidget *FormFieldData::getFormWidget(const FormField *f) |
| 253 |
{ |
| 254 |
return f->m_formData->fm; |
| 255 |
} |
| 256 |
|
| 257 |
FormFieldIconData *FormFieldIconData::getData(const FormFieldIcon &f) |
| 258 |
{ |
| 259 |
return f.d_ptr; |
| 260 |
} |
| 261 |
|
| 262 |
} |