2019-02-24 17:48:37 +01:00
|
|
|
// Copyright (C) 2019 Jakub Melka
|
|
|
|
//
|
|
|
|
// 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 PDFPAINTER_H
|
|
|
|
#define PDFPAINTER_H
|
|
|
|
|
|
|
|
#include "pdfutils.h"
|
2019-12-14 14:39:43 +01:00
|
|
|
#include "pdfpattern.h"
|
2019-02-24 17:48:37 +01:00
|
|
|
#include "pdfrenderer.h"
|
|
|
|
#include "pdfpagecontentprocessor.h"
|
|
|
|
|
|
|
|
#include <QPen>
|
|
|
|
#include <QBrush>
|
|
|
|
|
|
|
|
namespace pdf
|
|
|
|
{
|
|
|
|
|
2019-12-14 14:39:43 +01:00
|
|
|
/// Base painter, encapsulating common functionality for all PDF painters (for example,
|
|
|
|
/// direct painter, or painter, which generates list of graphic commands).
|
|
|
|
class PDFPainterBase : public PDFPageContentProcessor
|
|
|
|
{
|
|
|
|
using BaseClass = PDFPageContentProcessor;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit PDFPainterBase(PDFRenderer::Features features,
|
|
|
|
const PDFPage* page,
|
|
|
|
const PDFDocument* document,
|
|
|
|
const PDFFontCache* fontCache,
|
2019-12-25 17:56:17 +01:00
|
|
|
const PDFCMS* cms,
|
2019-12-14 14:39:43 +01:00
|
|
|
const PDFOptionalContentActivity* optionalContentActivity,
|
|
|
|
QMatrix pagePointToDevicePointMatrix,
|
|
|
|
const PDFMeshQualitySettings& meshQualitySettings);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void performUpdateGraphicsState(const PDFPageContentProcessorState& state) override;
|
|
|
|
virtual void performBeginTransparencyGroup(ProcessOrder order, const PDFTransparencyGroup& transparencyGroup);
|
|
|
|
virtual void performEndTransparencyGroup(ProcessOrder order, const PDFTransparencyGroup& transparencyGroup);
|
|
|
|
virtual bool isContentSuppressedByOC(PDFObjectReference ocgOrOcmd) override;
|
|
|
|
virtual void setWorldMatrix(const QMatrix& matrix) = 0;
|
|
|
|
virtual void setCompositionMode(QPainter::CompositionMode mode) = 0;
|
|
|
|
|
|
|
|
/// Returns current pen
|
|
|
|
const QPen& getCurrentPen() { return m_currentPen.get(this, &PDFPainterBase::getCurrentPenImpl); }
|
|
|
|
|
|
|
|
/// Returns current brush
|
|
|
|
const QBrush& getCurrentBrush() { return m_currentBrush.get(this, &PDFPainterBase::getCurrentBrushImpl); }
|
|
|
|
|
|
|
|
/// Returns effective stroking alpha from transparency groups and current graphic state
|
|
|
|
PDFReal getEffectiveStrokingAlpha() const;
|
|
|
|
|
|
|
|
/// Returns effective filling alpha from transparency groups and current graphic state
|
|
|
|
PDFReal getEffectiveFillingAlpha() const;
|
|
|
|
|
|
|
|
/// Returns true, if blend mode can be set according the transparency group stack
|
|
|
|
bool canSetBlendMode(BlendMode mode) const;
|
|
|
|
|
|
|
|
/// Returns, if feature is turned on
|
|
|
|
bool hasFeature(PDFRenderer::Feature feature) const { return m_features.testFlag(feature); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Returns current pen (implementation)
|
|
|
|
QPen getCurrentPenImpl() const;
|
|
|
|
|
|
|
|
/// Returns current brush (implementation)
|
|
|
|
QBrush getCurrentBrushImpl() const;
|
|
|
|
|
|
|
|
struct PDFTransparencyGroupPainterData
|
|
|
|
{
|
|
|
|
PDFTransparencyGroup group;
|
|
|
|
PDFReal alphaStroke = 1.0;
|
|
|
|
PDFReal alphaFill = 1.0;
|
|
|
|
BlendMode blendMode = BlendMode::Normal;
|
|
|
|
};
|
|
|
|
|
|
|
|
PDFRenderer::Features m_features;
|
|
|
|
PDFCachedItem<QPen> m_currentPen;
|
|
|
|
PDFCachedItem<QBrush> m_currentBrush;
|
|
|
|
std::vector<PDFTransparencyGroupPainterData> m_transparencyGroupDataStack;
|
|
|
|
};
|
|
|
|
|
2019-02-24 17:48:37 +01:00
|
|
|
/// Processor, which processes PDF's page commands on the QPainter. It works with QPainter
|
|
|
|
/// and with transformation matrix, which translates page points to the device points.
|
2019-09-29 18:09:09 +02:00
|
|
|
/// Only basic transparency is supported, advanced transparency, such as transparency groups,
|
|
|
|
/// are not supported. Painter will try to emulate them so painting will not fail completely.
|
2019-12-14 14:39:43 +01:00
|
|
|
class PDFPainter : public PDFPainterBase
|
2019-02-24 17:48:37 +01:00
|
|
|
{
|
2019-12-14 14:39:43 +01:00
|
|
|
using BaseClass = PDFPainterBase;
|
|
|
|
|
2019-02-24 17:48:37 +01:00
|
|
|
public:
|
|
|
|
/// Constructs new PDFPainter object, with default parameters.
|
2019-04-12 19:17:19 +02:00
|
|
|
/// \param painter Painter, on which page content is drawn
|
|
|
|
/// \param features Features of the painter
|
|
|
|
/// \param pagePointToDevicePointMatrix Matrix, which translates page points to device points
|
|
|
|
/// \param page Page, which will be drawn
|
|
|
|
/// \param document Document owning the page
|
|
|
|
/// \param fontCache Font cache
|
2019-12-25 17:56:17 +01:00
|
|
|
/// \param cms Color management system
|
2019-07-04 17:52:38 +02:00
|
|
|
/// \param optionalContentActivity Activity of optional content
|
2019-09-28 18:26:31 +02:00
|
|
|
/// \param meshQualitySettings Mesh quality settings
|
2019-04-12 19:17:19 +02:00
|
|
|
explicit PDFPainter(QPainter* painter,
|
|
|
|
PDFRenderer::Features features,
|
|
|
|
QMatrix pagePointToDevicePointMatrix,
|
|
|
|
const PDFPage* page,
|
|
|
|
const PDFDocument* document,
|
2019-07-04 17:52:38 +02:00
|
|
|
const PDFFontCache* fontCache,
|
2019-12-25 17:56:17 +01:00
|
|
|
const PDFCMS* cms,
|
2019-09-28 18:26:31 +02:00
|
|
|
const PDFOptionalContentActivity* optionalContentActivity,
|
|
|
|
const PDFMeshQualitySettings& meshQualitySettings);
|
2019-02-24 17:48:37 +01:00
|
|
|
virtual ~PDFPainter() override;
|
|
|
|
|
|
|
|
protected:
|
2019-05-04 18:22:40 +02:00
|
|
|
virtual void performPathPainting(const QPainterPath& path, bool stroke, bool fill, bool text, Qt::FillRule fillRule) override;
|
2019-02-24 17:48:37 +01:00
|
|
|
virtual void performClipping(const QPainterPath& path, Qt::FillRule fillRule) override;
|
2019-08-31 15:55:59 +02:00
|
|
|
virtual void performImagePainting(const QImage& image) override;
|
|
|
|
virtual void performMeshPainting(const PDFMesh& mesh) override;
|
2019-02-24 17:48:37 +01:00
|
|
|
virtual void performSaveGraphicState(ProcessOrder order) override;
|
|
|
|
virtual void performRestoreGraphicState(ProcessOrder order) override;
|
2019-12-14 14:39:43 +01:00
|
|
|
virtual void setWorldMatrix(const QMatrix& matrix) override;
|
|
|
|
virtual void setCompositionMode(QPainter::CompositionMode mode) override;
|
2019-02-24 17:48:37 +01:00
|
|
|
|
|
|
|
private:
|
2019-12-14 14:39:43 +01:00
|
|
|
QPainter* m_painter;
|
|
|
|
};
|
2019-02-24 17:48:37 +01:00
|
|
|
|
2019-12-14 14:39:43 +01:00
|
|
|
/// Precompiled page contains precompiled graphic instructions of a PDF page to draw it quickly
|
|
|
|
/// on the target painter. It enables very fast drawing, because instructions are not decoded
|
|
|
|
/// and interpreted from the PDF stream, but they are just "played" on the painter.
|
|
|
|
class PDFPrecompiledPage
|
|
|
|
{
|
|
|
|
public:
|
2019-12-14 19:09:34 +01:00
|
|
|
explicit inline PDFPrecompiledPage() = default;
|
|
|
|
|
|
|
|
inline PDFPrecompiledPage(const PDFPrecompiledPage&) = default;
|
|
|
|
inline PDFPrecompiledPage(PDFPrecompiledPage&&) = default;
|
|
|
|
inline PDFPrecompiledPage& operator=(const PDFPrecompiledPage&) = default;
|
|
|
|
inline PDFPrecompiledPage& operator=(PDFPrecompiledPage&&) = default;
|
2019-02-24 17:48:37 +01:00
|
|
|
|
2019-12-14 14:39:43 +01:00
|
|
|
enum class InstructionType
|
|
|
|
{
|
|
|
|
Invalid,
|
|
|
|
DrawPath,
|
|
|
|
DrawImage,
|
|
|
|
DrawMesh,
|
|
|
|
Clip,
|
|
|
|
SaveGraphicState,
|
|
|
|
RestoreGraphicState,
|
|
|
|
SetWorldMatrix,
|
|
|
|
SetCompositionMode
|
|
|
|
};
|
2019-02-24 17:48:37 +01:00
|
|
|
|
2019-12-14 14:39:43 +01:00
|
|
|
struct Instruction
|
|
|
|
{
|
|
|
|
inline Instruction() = default;
|
|
|
|
inline Instruction(InstructionType type, size_t dataIndex) :
|
|
|
|
type(type),
|
|
|
|
dataIndex(dataIndex)
|
|
|
|
{
|
2019-02-24 17:48:37 +01:00
|
|
|
|
2019-12-14 14:39:43 +01:00
|
|
|
}
|
2019-09-29 18:09:09 +02:00
|
|
|
|
2019-12-14 14:39:43 +01:00
|
|
|
InstructionType type = InstructionType::Invalid;
|
|
|
|
size_t dataIndex = 0;
|
|
|
|
};
|
2019-09-29 18:09:09 +02:00
|
|
|
|
2019-12-14 14:39:43 +01:00
|
|
|
/// Paints page onto the painter using matrix
|
|
|
|
/// \param painter Painter, onto which is page drawn
|
|
|
|
/// \param cropBox Page's crop box
|
|
|
|
/// \param pagePointToDevicePointMatrix Page point to device point transformation matrix
|
|
|
|
/// \param features Renderer features
|
|
|
|
void draw(QPainter* painter, const QRectF& cropBox, const QMatrix& pagePointToDevicePointMatrix, PDFRenderer::Features features) const;
|
2019-09-29 18:09:09 +02:00
|
|
|
|
2019-12-14 14:39:43 +01:00
|
|
|
void addPath(QPen pen, QBrush brush, QPainterPath path, bool isText);
|
|
|
|
void addClip(QPainterPath path);
|
|
|
|
void addImage(QImage image);
|
|
|
|
void addMesh(PDFMesh mesh, PDFReal alpha);
|
|
|
|
void addSaveGraphicState() { m_instructions.emplace_back(InstructionType::SaveGraphicState, 0); }
|
|
|
|
void addRestoreGraphicState() { m_instructions.emplace_back(InstructionType::RestoreGraphicState, 0); }
|
|
|
|
void addSetWorldMatrix(const QMatrix& matrix);
|
|
|
|
void addSetCompositionMode(QPainter::CompositionMode compositionMode);
|
|
|
|
|
|
|
|
/// Optimizes page memory allocation to contain less space
|
|
|
|
void optimize();
|
|
|
|
|
|
|
|
/// Finalizes precompiled page
|
|
|
|
/// \param compilingTimeNS Compiling time in nanoseconds
|
|
|
|
/// \param errors List of rendering errors
|
|
|
|
void finalize(qint64 compilingTimeNS, QList<PDFRenderError> errors);
|
|
|
|
|
|
|
|
/// Returns compiling time in nanoseconds
|
|
|
|
qint64 getCompilingTimeNS() const { return m_compilingTimeNS; }
|
|
|
|
|
|
|
|
/// Returns a list of rendering errors
|
|
|
|
const QList<PDFRenderError>& getErrors() const { return m_errors; }
|
|
|
|
|
|
|
|
/// Returns true, if page is valid (i.e. has nonzero instruction count)
|
|
|
|
bool isValid() const { return !m_instructions.empty(); }
|
|
|
|
|
2019-12-14 19:09:34 +01:00
|
|
|
/// Returns memory consumption estimate
|
|
|
|
qint64 getMemoryConsumptionEstimate() const { return m_memoryConsumptionEstimate; }
|
|
|
|
|
2019-12-14 14:39:43 +01:00
|
|
|
private:
|
|
|
|
struct PathPaintData
|
2019-09-29 18:09:09 +02:00
|
|
|
{
|
2019-12-14 14:39:43 +01:00
|
|
|
inline PathPaintData() = default;
|
|
|
|
inline PathPaintData(QPen pen, QBrush brush, QPainterPath path, bool isText) :
|
|
|
|
pen(qMove(pen)),
|
|
|
|
brush(qMove(brush)),
|
|
|
|
path(qMove(path)),
|
|
|
|
isText(isText)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QPen pen;
|
|
|
|
QBrush brush;
|
|
|
|
QPainterPath path;
|
|
|
|
bool isText = false;
|
2019-09-29 18:09:09 +02:00
|
|
|
};
|
|
|
|
|
2019-12-14 14:39:43 +01:00
|
|
|
struct ClipData
|
|
|
|
{
|
|
|
|
inline ClipData() = default;
|
|
|
|
inline ClipData(QPainterPath path) :
|
|
|
|
clipPath(qMove(path))
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QPainterPath clipPath;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ImageData
|
|
|
|
{
|
|
|
|
inline ImageData() = default;
|
|
|
|
inline ImageData(QImage image) :
|
|
|
|
image(qMove(image))
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage image;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MeshPaintData
|
|
|
|
{
|
|
|
|
inline MeshPaintData() = default;
|
|
|
|
inline MeshPaintData(PDFMesh mesh, PDFReal alpha) :
|
|
|
|
mesh(qMove(mesh)),
|
|
|
|
alpha(alpha)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFMesh mesh;
|
|
|
|
PDFReal alpha = 1.0;
|
|
|
|
};
|
|
|
|
|
|
|
|
qint64 m_compilingTimeNS = 0;
|
2019-12-14 19:09:34 +01:00
|
|
|
qint64 m_memoryConsumptionEstimate = 0;
|
2019-12-14 14:39:43 +01:00
|
|
|
std::vector<Instruction> m_instructions;
|
|
|
|
std::vector<PathPaintData> m_paths;
|
|
|
|
std::vector<ClipData> m_clips;
|
|
|
|
std::vector<ImageData> m_images;
|
|
|
|
std::vector<MeshPaintData> m_meshes;
|
|
|
|
std::vector<QMatrix> m_matrices;
|
|
|
|
std::vector<QPainter::CompositionMode> m_compositionModes;
|
|
|
|
QList<PDFRenderError> m_errors;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Processor, which processes PDF's page commands and writes them to the precompiled page.
|
|
|
|
/// Precompiled page then can be used to execute these commands on QPainter.
|
|
|
|
class PDFPrecompiledPageGenerator : public PDFPainterBase
|
|
|
|
{
|
|
|
|
using BaseClass = PDFPainterBase;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit inline PDFPrecompiledPageGenerator(PDFPrecompiledPage* precompiledPage,
|
|
|
|
PDFRenderer::Features features,
|
|
|
|
const PDFPage* page,
|
|
|
|
const PDFDocument* document,
|
|
|
|
const PDFFontCache* fontCache,
|
2019-12-25 17:56:17 +01:00
|
|
|
const PDFCMS* cms,
|
2019-12-14 14:39:43 +01:00
|
|
|
const PDFOptionalContentActivity* optionalContentActivity,
|
|
|
|
const PDFMeshQualitySettings& meshQualitySettings) :
|
2019-12-25 17:56:17 +01:00
|
|
|
BaseClass(features, page, document, fontCache, cms, optionalContentActivity, QMatrix(), meshQualitySettings),
|
2019-12-14 14:39:43 +01:00
|
|
|
m_precompiledPage(precompiledPage)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void performPathPainting(const QPainterPath& path, bool stroke, bool fill, bool text, Qt::FillRule fillRule) override;
|
|
|
|
virtual void performClipping(const QPainterPath& path, Qt::FillRule fillRule) override;
|
|
|
|
virtual void performImagePainting(const QImage& image) override;
|
|
|
|
virtual void performMeshPainting(const PDFMesh& mesh) override;
|
|
|
|
virtual void performSaveGraphicState(ProcessOrder order) override;
|
|
|
|
virtual void performRestoreGraphicState(ProcessOrder order) override;
|
|
|
|
virtual void setWorldMatrix(const QMatrix& matrix) override;
|
|
|
|
virtual void setCompositionMode(QPainter::CompositionMode mode) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
PDFPrecompiledPage* m_precompiledPage;
|
2019-02-24 17:48:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace pdf
|
|
|
|
|
|
|
|
#endif // PDFPAINTER_H
|