2020-01-18 11:38:54 +01:00
|
|
|
// Copyright (C) 2019-2020 Jakub Melka
|
2019-12-14 19:09:34 +01:00
|
|
|
//
|
|
|
|
// This file is part of PdfForQt.
|
|
|
|
//
|
|
|
|
// PdfForQt is free software: you can redistribute it and/or modify
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// PdfForQt is distributed in the hope that it will be useful,
|
|
|
|
// 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
|
|
|
|
// along with PDFForQt. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#ifndef PDFCOMPILER_H
|
|
|
|
#define PDFCOMPILER_H
|
|
|
|
|
|
|
|
#include "pdfrenderer.h"
|
|
|
|
#include "pdfpainter.h"
|
2019-12-31 17:39:31 +01:00
|
|
|
#include "pdftextlayout.h"
|
2019-12-14 19:09:34 +01:00
|
|
|
|
|
|
|
#include <QCache>
|
|
|
|
#include <QFuture>
|
|
|
|
#include <QFutureWatcher>
|
|
|
|
|
|
|
|
namespace pdf
|
|
|
|
{
|
|
|
|
class PDFDrawWidgetProxy;
|
|
|
|
|
|
|
|
/// Asynchronous page compiler compiles pages asynchronously, and stores them in the
|
|
|
|
/// cache. Cache size can be set. This object is designed to cooperate with
|
|
|
|
/// draw widget proxy.
|
|
|
|
class PDFAsynchronousPageCompiler : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private:
|
|
|
|
using BaseClass = QObject;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit PDFAsynchronousPageCompiler(PDFDrawWidgetProxy* proxy);
|
|
|
|
|
|
|
|
/// Starts the engine. Call this function only if the engine
|
|
|
|
/// is stopped.
|
|
|
|
void start();
|
|
|
|
|
|
|
|
/// Stops the engine and all underlying asynchronous tasks. Also
|
|
|
|
/// clears the cache. Call this function only if engine is active.
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
/// Resets the engine - calls stop and then calls start.
|
|
|
|
void reset();
|
|
|
|
|
2019-12-15 19:28:25 +01:00
|
|
|
/// Sets cache limit in bytes
|
|
|
|
/// \param limit Cache limit [bytes]
|
|
|
|
void setCacheLimit(int limit);
|
|
|
|
|
2019-12-14 19:09:34 +01:00
|
|
|
enum class State
|
|
|
|
{
|
|
|
|
Inactive,
|
|
|
|
Active,
|
|
|
|
Stopping
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Tries to retrieve precompiled page from the cache. If page is not found,
|
|
|
|
/// then nullptr is returned (no exception is thrown). If \p compile is set to true,
|
|
|
|
/// and page is not found, and compiler is active, then new asynchronous compile
|
|
|
|
/// task is performed.
|
|
|
|
/// \param pageIndex Index of page
|
|
|
|
/// \param compile Compile the page, if it is not found in the cache
|
2019-12-15 17:46:58 +01:00
|
|
|
const PDFPrecompiledPage* getCompiledPage(PDFInteger pageIndex, bool compile);
|
2019-12-14 19:09:34 +01:00
|
|
|
|
|
|
|
signals:
|
|
|
|
void pageImageChanged(bool all, const std::vector<PDFInteger>& pages);
|
|
|
|
void renderingError(PDFInteger pageIndex, const QList<PDFRenderError>& errors);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void onPageCompiled();
|
|
|
|
|
|
|
|
struct CompileTask
|
|
|
|
{
|
|
|
|
QFuture<PDFPrecompiledPage> taskFuture;
|
|
|
|
QFutureWatcher<PDFPrecompiledPage>* taskWatcher = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
PDFDrawWidgetProxy* m_proxy;
|
|
|
|
State m_state = State::Inactive;
|
|
|
|
QCache<PDFInteger, PDFPrecompiledPage> m_cache;
|
|
|
|
std::map<PDFInteger, CompileTask> m_tasks;
|
2020-01-01 18:23:18 +01:00
|
|
|
};
|
|
|
|
|
2020-01-03 18:11:03 +01:00
|
|
|
class PDFFORQTLIBSHARED_EXPORT PDFAsynchronousTextLayoutCompiler : public QObject
|
2020-01-01 18:23:18 +01:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private:
|
|
|
|
using BaseClass = QObject;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit PDFAsynchronousTextLayoutCompiler(PDFDrawWidgetProxy* proxy);
|
|
|
|
|
|
|
|
/// Starts the engine. Call this function only if the engine
|
|
|
|
/// is stopped.
|
|
|
|
void start();
|
|
|
|
|
|
|
|
/// Stops the engine and all underlying asynchronous tasks. Also
|
|
|
|
/// clears the cache. Call this function only if engine is active.
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
/// Resets the engine - calls stop and then calls start.
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
enum class State
|
|
|
|
{
|
|
|
|
Inactive,
|
|
|
|
Active,
|
|
|
|
Stopping
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Returns text layout of the page. If page index is invalid,
|
|
|
|
/// then empty text layout is returned.
|
|
|
|
/// \param pageIndex Page index
|
|
|
|
PDFTextLayout getTextLayout(PDFInteger pageIndex);
|
|
|
|
|
2020-01-08 19:02:29 +01:00
|
|
|
/// Returns getter for text layout of the page. If page index is invalid,
|
|
|
|
/// then empty text layout getter is returned.
|
|
|
|
/// \param pageIndex Page index
|
|
|
|
PDFTextLayoutGetter getTextLayoutLazy(PDFInteger pageIndex);
|
|
|
|
|
2020-01-01 18:23:18 +01:00
|
|
|
/// Create text layout for the document. Function is asynchronous,
|
|
|
|
/// it returns immediately. After text layout is created, signal
|
|
|
|
/// \p textLayoutChanged is emitted.
|
|
|
|
void makeTextLayout();
|
|
|
|
|
2020-01-03 18:11:03 +01:00
|
|
|
/// Returns true, if text layout is ready
|
|
|
|
bool isTextLayoutReady() const { return m_textLayouts.has_value(); }
|
|
|
|
|
2020-01-04 17:58:55 +01:00
|
|
|
/// Returns text layout storage (if it is ready), or nullptr
|
|
|
|
const PDFTextLayoutStorage* getTextLayoutStorage() const { return isTextLayoutReady() ? &m_textLayouts.value() : nullptr; }
|
|
|
|
|
2020-01-01 18:23:18 +01:00
|
|
|
signals:
|
|
|
|
void textLayoutChanged();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void onTextLayoutCreated();
|
|
|
|
|
|
|
|
PDFDrawWidgetProxy* m_proxy;
|
|
|
|
State m_state = State::Inactive;
|
2020-01-02 12:06:09 +01:00
|
|
|
bool m_isRunning;
|
2020-01-01 18:23:18 +01:00
|
|
|
std::optional<PDFTextLayoutStorage> m_textLayouts;
|
|
|
|
QFuture<PDFTextLayoutStorage> m_textLayoutCompileFuture;
|
|
|
|
QFutureWatcher<PDFTextLayoutStorage> m_textLayoutCompileFutureWatcher;
|
2019-12-14 19:09:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace pdf
|
|
|
|
|
|
|
|
#endif // PDFCOMPILER_H
|