PDF4QT/Pdf4QtLib/sources/pdfjavascriptscanner.h

97 lines
3.1 KiB
C
Raw Normal View History

2020-10-04 16:56:55 +02:00
// Copyright (C) 2020 Jakub Melka
//
2020-12-20 19:03:58 +01:00
// This file is part of Pdf4Qt.
2020-10-04 16:56:55 +02:00
//
2020-12-20 19:03:58 +01:00
// Pdf4Qt is free software: you can redistribute it and/or modify
2020-10-04 16:56:55 +02:00
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
2020-12-20 19:03:58 +01:00
// Pdf4Qt is distributed in the hope that it will be useful,
2020-10-04 16:56:55 +02:00
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
2020-12-20 19:03:58 +01:00
// along with Pdf4Qt. If not, see <https://www.gnu.org/licenses/>.
2020-10-04 16:56:55 +02:00
#ifndef PDFJAVASCRIPTSCANNER_H
#define PDFJAVASCRIPTSCANNER_H
#include "pdfdocument.h"
namespace pdf
{
struct PDFJavaScriptEntry
{
enum class Type
{
Invalid,
Document,
Named,
Form,
Page,
Annotation
};
explicit PDFJavaScriptEntry() = default;
explicit PDFJavaScriptEntry(Type type, PDFInteger pageIndex, QString javaScript) :
type(type), pageIndex(pageIndex), javaScript(javaScript)
{
}
2020-10-06 19:21:05 +02:00
bool operator <(const PDFJavaScriptEntry& other) const
{
return std::tie(type, pageIndex, javaScript) < std::tie(other.type, other.pageIndex, other.javaScript);
}
bool operator==(const PDFJavaScriptEntry&) const = default;
2020-10-04 16:56:55 +02:00
Type type = Type::Invalid;
PDFInteger pageIndex = -1;
QString javaScript;
};
/// Scans document for all javascript presence (in actions). Several option
/// can be set, for example, scan only document actions, or stop scanning,
/// when first javascript is found.
2020-12-20 19:03:58 +01:00
class Pdf4QtLIBSHARED_EXPORT PDFJavaScriptScanner
2020-10-04 16:56:55 +02:00
{
public:
explicit PDFJavaScriptScanner(const PDFDocument* document);
enum Option
{
AllPages = 0x0001, ///< Scan all pages
FindFirstOnly = 0x0002, ///< Return only first javascript found
ScanDocument = 0x0004, ///< Scan document related actions for javascript
ScanNamed = 0x0008, ///< Scan named javascript in catalog
ScanForm = 0x0010, ///< Scan javascript in form actions
ScanPage = 0x0020, ///< Scan javascript in page annotations
2020-10-06 19:21:05 +02:00
Optimize = 0x0040, ///< Remove redundant entries
2020-10-05 19:50:04 +02:00
ScanMask = ScanDocument | ScanNamed | ScanForm | ScanPage
2020-10-04 16:56:55 +02:00
};
Q_DECLARE_FLAGS(Options, Option)
using Entries = std::vector<PDFJavaScriptEntry>;
/// Scans document for javascript actions using flags
Entries scan(const std::vector<PDFInteger>& pages, Options options) const;
/// Returns true, if document has any java script action. Calling
/// this function can be slow.
bool hasJavaScript() const;
private:
const PDFDocument* m_document;
};
} // namespace pdf
Q_DECLARE_OPERATORS_FOR_FLAGS(pdf::PDFJavaScriptScanner::Options)
#endif // PDFJAVASCRIPTSCANNER_H