PDF4QT/Pdf4QtLib/sources/pdfpainter.h

341 lines
13 KiB
C
Raw Normal View History

2021-04-30 20:12:10 +02:00
// Copyright (C) 2019-2021 Jakub Melka
2019-02-24 17:48:37 +01:00
//
2021-08-10 19:22:56 +02:00
// This file is part of PDF4QT.
2019-02-24 17:48:37 +01:00
//
2021-08-10 19:22:56 +02:00
// PDF4QT is free software: you can redistribute it and/or modify
2019-02-24 17:48:37 +01: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
2021-04-30 20:12:10 +02:00
// with the written consent of the copyright owner, any later version.
2019-02-24 17:48:37 +01:00
//
2021-08-10 19:22:56 +02:00
// PDF4QT is distributed in the hope that it will be useful,
2019-02-24 17:48:37 +01: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
2021-08-10 19:22:56 +02:00
// along with PDF4QT. If not, see <https://www.gnu.org/licenses/>.
2019-02-24 17:48:37 +01:00
#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"
2019-12-28 19:21:29 +01:00
#include "pdftextlayout.h"
2020-02-23 18:59:54 +01:00
#include "pdfsnapper.h"
2019-02-24 17:48:37 +01:00
#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);
2020-03-07 17:38:50 +01:00
virtual bool isContentSuppressedByOC(PDFObjectReference ocgOrOcmd) override;
2019-12-14 14:39:43 +01:00
protected:
virtual void performUpdateGraphicsState(const PDFPageContentProcessorState& state) override;
2021-03-06 18:13:21 +01:00
virtual void performBeginTransparencyGroup(ProcessOrder order, const PDFTransparencyGroup& transparencyGroup) override;
virtual void performEndTransparencyGroup(ProcessOrder order, const PDFTransparencyGroup& transparencyGroup) override;
2019-12-14 14:39:43 +01:00
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); }
/// Is transparency group active?
bool isTransparencyGroupActive() const { return !m_transparencyGroupDataStack.empty(); }
2019-12-14 14:39:43 +01:00
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
/// \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,
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:
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
2020-12-29 18:33:25 +01:00
/// Redact path - remove all content intersecting given path,
/// and fill redact path with given color.
/// \param redactPath Redaction path in page coordinates
/// \param color Redaction color (if invalid, nothing is being drawn)
void redact(QPainterPath redactPath, const QMatrix& matrix, QColor color);
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();
2020-01-28 19:17:45 +01:00
/// Inverts all colors
void invertColors();
2019-12-14 14:39:43 +01:00
/// 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(); }
/// Returns memory consumption estimate
qint64 getMemoryConsumptionEstimate() const { return m_memoryConsumptionEstimate; }
2020-01-28 19:17:45 +01:00
/// Returns paper color
QColor getPaperColor() const { return m_paperColor; }
void setPaperColor(QColor paperColor) { m_paperColor = paperColor; }
2020-02-23 18:59:54 +01:00
PDFSnapInfo* getSnapInfo() { return &m_snapInfo; }
const PDFSnapInfo* getSnapInfo() const { return &m_snapInfo; }
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;
qint64 m_memoryConsumptionEstimate = 0;
2020-01-28 19:17:45 +01:00
QColor m_paperColor = QColor(Qt::white);
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;
2020-02-23 18:59:54 +01:00
PDFSnapInfo m_snapInfo;
2019-12-14 14:39:43 +01:00
};
/// 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.
2021-08-10 19:22:56 +02:00
class PDF4QTLIBSHARED_EXPORT PDFPrecompiledPageGenerator : public PDFPainterBase
2019-12-14 14:39:43 +01:00
{
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,
2020-01-28 19:17:45 +01:00
const PDFMeshQualitySettings& meshQualitySettings);
2019-12-14 14:39:43 +01:00
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