Debian Codegraph @

qt5/src/poppler-private.cc

raw
1 /* poppler-private.cc: qt interface to poppler
2 * Copyright (C) 2005, Net Integration Technologies, Inc.
3 * Copyright (C) 2006, 2011, 2015, 2017-2020, 2024, 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 qt5ErrorFunction(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 ushort *>(s1.c_str()), s1.size() / 2);
128 } else {
129 std::string cString = pdfDocEncodingToUTF16(s1);
130 auto result = QString::fromUtf16(reinterpret_cast<const ushort *>(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>(std::move(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 static void linkActionToTocItem(const ::LinkAction *a, DocumentData *doc, QDomElement *e)
202 {
203 if (!a || !e) {
204 return;
205 }
206
207 switch (a->getKind()) {
208 case actionGoTo: {
209 // page number is contained/referenced in a LinkGoTo
210 const LinkGoTo *g = static_cast<const LinkGoTo *>(a);
211 const LinkDest *destination = g->getDest();
212 if (!destination && g->getNamedDest()) {
213 // no 'destination' but an internal 'named reference'. we could
214 // get the destination for the page now, but it's VERY time consuming,
215 // so better storing the reference and provide the viewport on demand
216 const GooString *s = g->getNamedDest();
217 QChar *charArray = new QChar[s->size()];
218 for (size_t i = 0; i < s->size(); ++i) {
219 charArray[i] = QChar::fromLatin1(s->c_str()[i]);
220 }
221 QString aux(charArray, s->size());
222 e->setAttribute(QStringLiteral("DestinationName"), aux);
223 delete[] charArray;
224 } else if (destination && destination->isOk()) {
225 LinkDestinationData ldd(destination, nullptr, doc, false);
226 e->setAttribute(QStringLiteral("Destination"), LinkDestination(ldd).toString());
227 }
228 break;
229 }
230 case actionGoToR: {
231 // page number is contained/referenced in a LinkGoToR
232 const LinkGoToR *g = static_cast<const LinkGoToR *>(a);
233 const LinkDest *destination = g->getDest();
234 if (!destination && g->getNamedDest()) {
235 // no 'destination' but an internal 'named reference'. we could
236 // get the destination for the page now, but it's VERY time consuming,
237 // so better storing the reference and provide the viewport on demand
238 const GooString *s = g->getNamedDest();
239 QChar *charArray = new QChar[s->size()];
240 for (size_t i = 0; i < s->size(); ++i) {
241 charArray[i] = QChar::fromLatin1(s->c_str()[i]);
242 }
243 QString aux(charArray, s->size());
244 e->setAttribute(QStringLiteral("DestinationName"), aux);
245 delete[] charArray;
246 } else if (destination && destination->isOk()) {
247 LinkDestinationData ldd(destination, nullptr, doc, g->getFileName() != nullptr);
248 e->setAttribute(QStringLiteral("Destination"), LinkDestination(ldd).toString());
249 }
250 e->setAttribute(QStringLiteral("ExternalFileName"), QString::fromStdString(g->getFileName()->toStr()));
251 break;
252 }
253 case actionURI: {
254 const LinkURI *u = static_cast<const LinkURI *>(a);
255 e->setAttribute(QStringLiteral("DestinationURI"), QString::fromStdString(u->getURI()));
256 }
257 default:;
258 }
259 }
260
261 DocumentData::~DocumentData()
262 {
263 qDeleteAll(m_embeddedFiles);
264 delete (OptContentModel *)m_optContentModel;
265 delete doc;
266 }
267
268 void DocumentData::init()
269 {
270 m_backend = Document::SplashBackend;
271 paperColor = Qt::white;
272 m_hints = 0;
273 m_optContentModel = nullptr;
274 xrefReconstructed = false;
275 xrefReconstructedCallback = {};
276
277 #ifdef ANDROID
278 // Copy fonts from android apk to the app's storage dir, and
279 // set the font directory path
280 QString assetsFontDir = QStringLiteral("assets:/share/fonts");
281 QString fontsdir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/fonts");
282 QDir fontPath = QDir(fontsdir);
283
284 if (fontPath.mkpath(fontPath.absolutePath())) {
285 GlobalParams::setFontDir(fontPath.absolutePath().toStdString());
286 QDirIterator iterator(assetsFontDir, QDir::NoFilter, QDirIterator::Subdirectories);
287
288 while (iterator.hasNext()) {
289 iterator.next();
290 QFileInfo fontFileInfo = iterator.fileInfo();
291 QString fontFilePath = assetsFontDir + QStringLiteral("/") + fontFileInfo.fileName();
292 QString destPath = fontPath.absolutePath() + QStringLiteral("/") + fontFileInfo.fileName();
293 QFile::copy(fontFilePath, destPath);
294 }
295 } else {
296 GlobalParams::setFontDir("");
297 }
298 #endif
299 }
300
301 void DocumentData::addTocChildren(QDomDocument *docSyn, QDomNode *parent, const std::vector<::OutlineItem *> *items)
302 {
303 for (::OutlineItem *outlineItem : *items) {
304 // iterate over every object in 'items'
305
306 // 1. create element using outlineItem's title as tagName
307 QString name = unicodeToQString(outlineItem->getTitle());
308
309 QDomElement item = docSyn->createElement(name);
310 parent->appendChild(item);
311
312 // 2. find the page the link refers to
313 const ::LinkAction *a = outlineItem->getAction();
314 linkActionToTocItem(a, this, &item);
315
316 item.setAttribute(QStringLiteral("Open"), QVariant((bool)outlineItem->isOpen()).toString());
317
318 // 3. recursively descend over children
319 outlineItem->open();
320 const std::vector<::OutlineItem *> *children = outlineItem->getKids();
321 if (children) {
322 addTocChildren(docSyn, &item, children);
323 }
324 }
325 }
326
327 void DocumentData::noitfyXRefReconstructed()
328 {
329 if (!xrefReconstructed) {
330 xrefReconstructed = true;
331 }
332
333 if (xrefReconstructedCallback) {
334 xrefReconstructedCallback();
335 }
336 }
337
338 FormWidget *FormFieldData::getFormWidget(const FormField *f)
339 {
340 return f->m_formData->fm;
341 }
342
343 FormFieldIconData *FormFieldIconData::getData(const FormFieldIcon &f)
344 {
345 return f.d_ptr;
346 }
347 }